gtphub: cosmetic: for_each_side,_plane macros.

Simplify looping over sides and planes. I'm tired of typing the same for
loops all the time.

Sponsored-by: On-Waves ehi
This commit is contained in:
Neels Hofmeyr 2015-11-27 00:05:56 +01:00
parent ba9e9f63bc
commit f977320736
3 changed files with 24 additions and 24 deletions

View File

@ -141,6 +141,10 @@ enum gtphub_side_idx {
GTPH_SIDE_N GTPH_SIDE_N
}; };
#define for_each_side(I) for (I = 0; I < GTPH_SIDE_N; I++)
#define for_each_plane(I) for (I = 0; I < GTPH_PLANE_N; I++)
#define for_each_side_and_plane(I,J) for_each_side(I) for_each_plane(J)
static inline int other_side_idx(int side_idx) static inline int other_side_idx(int side_idx)
{ {
return (side_idx + 1) & 1; return (side_idx + 1) & 1;

View File

@ -1014,13 +1014,11 @@ int gtphub_tunnel_complete(struct gtphub_tunnel *tun)
return 0; return 0;
int side_idx; int side_idx;
int plane_idx; int plane_idx;
for (side_idx = 0; side_idx < GTPH_SIDE_N; side_idx++) { for_each_side_and_plane(side_idx, plane_idx) {
for (plane_idx = 0; plane_idx < GTPH_PLANE_N; plane_idx++) { struct gtphub_tunnel_endpoint *te =
struct gtphub_tunnel_endpoint *te = &tun->endpoint[side_idx][plane_idx];
&tun->endpoint[side_idx][plane_idx]; if (!(te->peer && te->tei_orig && te->tei_repl))
if (!(te->peer && te->tei_orig && te->tei_repl)) return 0;
return 0;
}
} }
return 1; return 1;
} }
@ -1040,12 +1038,10 @@ static void gtphub_tunnel_del_cb(struct expiring_item *expi)
int side_idx; int side_idx;
int plane_idx; int plane_idx;
for (side_idx = 0; side_idx < GTPH_SIDE_N; side_idx++) { for_each_side_and_plane(side_idx, plane_idx) {
for (plane_idx = 0; plane_idx < GTPH_PLANE_N; plane_idx++) { /* clear ref count */
/* clear ref count */ gtphub_tunnel_endpoint_set_peer(&tun->endpoint[side_idx][plane_idx],
gtphub_tunnel_endpoint_set_peer( NULL);
&tun->endpoint[side_idx][plane_idx], NULL);
}
} }
talloc_free(tun); talloc_free(tun);
@ -2077,7 +2073,7 @@ void gtphub_gc(struct gtphub *hub, time_t now)
if (expired) { if (expired) {
int i; int i;
for (i = 0; i < GTPH_PLANE_N; i++) { for_each_plane(i) {
gtphub_gc_bind(&hub->to_sgsns[i]); gtphub_gc_bind(&hub->to_sgsns[i]);
gtphub_gc_bind(&hub->to_ggsns[i]); gtphub_gc_bind(&hub->to_ggsns[i]);
} }
@ -2110,7 +2106,7 @@ void gtphub_init(struct gtphub *hub)
expiry_init(&hub->expire_slowly, GTPH_EXPIRE_SLOWLY_MINUTES * 60); expiry_init(&hub->expire_slowly, GTPH_EXPIRE_SLOWLY_MINUTES * 60);
int plane_idx; int plane_idx;
for (plane_idx = 0; plane_idx < GTPH_PLANE_N; plane_idx++) { for_each_plane(plane_idx) {
nr_pool_init(&hub->tei_pool[plane_idx], 1, 0xffffffff); nr_pool_init(&hub->tei_pool[plane_idx], 1, 0xffffffff);
gtphub_bind_init(&hub->to_ggsns[plane_idx]); gtphub_bind_init(&hub->to_ggsns[plane_idx]);
@ -2135,7 +2131,7 @@ void gtphub_free(struct gtphub *hub)
expiry_clear(&hub->expire_slowly); expiry_clear(&hub->expire_slowly);
int plane_idx; int plane_idx;
for (plane_idx = 0; plane_idx < GTPH_PLANE_N; plane_idx++) { for_each_plane(plane_idx) {
gtphub_gc_bind(&hub->to_ggsns[plane_idx]); gtphub_gc_bind(&hub->to_ggsns[plane_idx]);
gtphub_bind_free(&hub->to_ggsns[plane_idx]); gtphub_bind_free(&hub->to_ggsns[plane_idx]);
@ -2147,7 +2143,7 @@ void gtphub_free(struct gtphub *hub)
void gtphub_stop(struct gtphub *hub) void gtphub_stop(struct gtphub *hub)
{ {
int plane_idx; int plane_idx;
for (plane_idx = 0; plane_idx < GTPH_PLANE_N; plane_idx++) { for_each_plane(plane_idx) {
gtphub_bind_stop(&hub->to_ggsns[plane_idx]); gtphub_bind_stop(&hub->to_ggsns[plane_idx]);
gtphub_bind_stop(&hub->to_sgsns[plane_idx]); gtphub_bind_stop(&hub->to_sgsns[plane_idx]);
} }
@ -2193,7 +2189,7 @@ int gtphub_start(struct gtphub *hub, struct gtphub_cfg *cfg,
/* TODO set hub->restart_counter from external file. */ /* TODO set hub->restart_counter from external file. */
int plane_idx; int plane_idx;
for (plane_idx = 0; plane_idx < GTPH_PLANE_N; plane_idx++) { for_each_plane(plane_idx) {
rc = gtphub_bind_start(&hub->to_ggsns[plane_idx], rc = gtphub_bind_start(&hub->to_ggsns[plane_idx],
&cfg->to_ggsns[plane_idx], &cfg->to_ggsns[plane_idx],
from_ggsns_read_cb, hub, plane_idx); from_ggsns_read_cb, hub, plane_idx);
@ -2213,7 +2209,7 @@ int gtphub_start(struct gtphub *hub, struct gtphub_cfg *cfg,
} }
} }
for (plane_idx = 0; plane_idx < GTPH_PLANE_N; plane_idx++) { for_each_plane(plane_idx) {
if (gtphub_make_proxy(hub, if (gtphub_make_proxy(hub,
&hub->sgsn_proxy[plane_idx], &hub->sgsn_proxy[plane_idx],
&hub->to_sgsns[plane_idx], &hub->to_sgsns[plane_idx],
@ -2235,14 +2231,14 @@ int gtphub_start(struct gtphub *hub, struct gtphub_cfg *cfg,
} }
} }
for (plane_idx = 0; plane_idx < GTPH_PLANE_N; plane_idx++) { for_each_plane(plane_idx) {
if (hub->sgsn_proxy[plane_idx]) if (hub->sgsn_proxy[plane_idx])
LOG(LOGL_NOTICE, "Using SGSN %s proxy %s\n", LOG(LOGL_NOTICE, "Using SGSN %s proxy %s\n",
gtphub_plane_idx_names[plane_idx], gtphub_plane_idx_names[plane_idx],
gtphub_port_str(hub->sgsn_proxy[plane_idx])); gtphub_port_str(hub->sgsn_proxy[plane_idx]));
} }
for (plane_idx = 0; plane_idx < GTPH_PLANE_N; plane_idx++) { for_each_plane(plane_idx) {
if (hub->ggsn_proxy[plane_idx]) if (hub->ggsn_proxy[plane_idx])
LOG(LOGL_NOTICE, "Using GGSN %s proxy %s\n", LOG(LOGL_NOTICE, "Using GGSN %s proxy %s\n",
gtphub_plane_idx_names[plane_idx], gtphub_plane_idx_names[plane_idx],

View File

@ -122,7 +122,7 @@ DEFUN(cfg_gtphub_bind_to_sgsns_short, cfg_gtphub_bind_to_sgsns_short_cmd,
) )
{ {
int i; int i;
for (i = 0; i < GTPH_PLANE_N; i++) for_each_plane(i)
g_cfg->to_sgsns[i].bind.addr_str = talloc_strdup(tall_vty_ctx, argv[0]); g_cfg->to_sgsns[i].bind.addr_str = talloc_strdup(tall_vty_ctx, argv[0]);
g_cfg->to_sgsns[GTPH_PLANE_CTRL].bind.port = GTPH_DEFAULT_CONTROL_PORT; g_cfg->to_sgsns[GTPH_PLANE_CTRL].bind.port = GTPH_DEFAULT_CONTROL_PORT;
g_cfg->to_sgsns[GTPH_PLANE_USER].bind.port = GTPH_DEFAULT_USER_PORT; g_cfg->to_sgsns[GTPH_PLANE_USER].bind.port = GTPH_DEFAULT_USER_PORT;
@ -137,7 +137,7 @@ DEFUN(cfg_gtphub_bind_to_ggsns_short, cfg_gtphub_bind_to_ggsns_short_cmd,
) )
{ {
int i; int i;
for (i = 0; i < GTPH_PLANE_N; i++) for_each_plane(i)
g_cfg->to_ggsns[i].bind.addr_str = talloc_strdup(tall_vty_ctx, argv[0]); g_cfg->to_ggsns[i].bind.addr_str = talloc_strdup(tall_vty_ctx, argv[0]);
g_cfg->to_ggsns[GTPH_PLANE_CTRL].bind.port = GTPH_DEFAULT_CONTROL_PORT; g_cfg->to_ggsns[GTPH_PLANE_CTRL].bind.port = GTPH_DEFAULT_CONTROL_PORT;
g_cfg->to_ggsns[GTPH_PLANE_USER].bind.port = GTPH_DEFAULT_USER_PORT; g_cfg->to_ggsns[GTPH_PLANE_USER].bind.port = GTPH_DEFAULT_USER_PORT;
@ -254,7 +254,7 @@ DEFUN(cfg_grx_ggsn, cfg_grx_ggsn_cmd,
static void show_bind_stats_all(struct vty *vty) static void show_bind_stats_all(struct vty *vty)
{ {
int plane_idx; int plane_idx;
for (plane_idx = 0; plane_idx < GTPH_PLANE_N; plane_idx++) { for_each_plane(plane_idx) {
vty_out(vty, "- %s Plane:%s", vty_out(vty, "- %s Plane:%s",
gtphub_plane_idx_names[plane_idx], VTY_NEWLINE); gtphub_plane_idx_names[plane_idx], VTY_NEWLINE);