summaryrefslogtreecommitdiff
path: root/proto/static/static.c
diff options
context:
space:
mode:
Diffstat (limited to 'proto/static/static.c')
-rw-r--r--proto/static/static.c455
1 files changed, 237 insertions, 218 deletions
diff --git a/proto/static/static.c b/proto/static/static.c
index fb547537..e5251bf6 100644
--- a/proto/static/static.c
+++ b/proto/static/static.c
@@ -58,81 +58,88 @@ p_igp_table(struct proto *p)
}
static void
-static_install(struct proto *p, struct static_route *r, struct iface *ifa)
+static_install(struct proto *p, struct static_route *r)
{
- rta a;
+ rta *ap = allocz(RTA_MAX_SIZE);
rte *e;
- if (r->installed > 0)
- return;
+ if (!(r->state & STS_WANT) && (r->state & (STS_INSTALLED | STS_FORCE)) && r->dest != RTD_UNICAST)
+ goto drop;
DBG("Installing static route %N, rtd=%d\n", r->net, r->dest);
- bzero(&a, sizeof(a));
- a.src = p->main_source;
- a.source = (r->dest == RTD_DEVICE) ? RTS_STATIC_DEVICE : RTS_STATIC;
- a.scope = SCOPE_UNIVERSE;
- a.cast = RTC_UNICAST;
- a.dest = r->dest;
- a.gw = r->via;
- a.iface = ifa;
-
- if (r->dest == RTD_MULTIPATH)
+ ap->src = p->main_source;
+ ap->source = RTS_STATIC;
+ ap->scope = SCOPE_UNIVERSE;
+ ap->dest = r->dest;
+
+ if (r->dest == RTD_UNICAST)
{
+ struct nexthop *nhs = NULL;
struct static_route *r2;
- struct mpnh *nhs = NULL;
+ int update = 0;
- for (r2 = r->mp_next; r2; r2 = r2->mp_next)
- if (r2->installed)
- {
- struct mpnh *nh = alloca(sizeof(struct mpnh));
- nh->gw = r2->via;
- nh->iface = r2->neigh->iface;
- nh->weight = r2->weight;
- mpnh_insert(&nhs, nh);
- }
+ r = r->mp_head;
+ for (r2 = r; r2; r2 = r2->mp_next)
+ {
+ if ((r2->state & STS_FORCE) ||
+ (!!(r2->state & STS_INSTALLED) != !!(r2->state & STS_WANT)))
+ update++;
- /* There is at least one nexthop */
- if (!nhs->next)
+ if (r2->state & STS_WANT)
{
- /* Fallback to unipath route for exactly one nexthop */
- a.dest = RTD_ROUTER;
- a.gw = nhs->gw;
- a.iface = nhs->iface;
+ struct nexthop *nh = allocz(NEXTHOP_MAX_SIZE);
+
+ nh->gw = r2->via;
+ nh->iface = r2->neigh ? r2->neigh->iface : r2->iface;
+ nh->weight = r2->weight;
+ nh->labels = r2->label_count;
+ memcpy(nh->label, r2->label_stack, r2->label_count * sizeof(u32));
+
+ r2->state |= STS_INSTALLED;
+ nexthop_insert(&nhs, nh);
}
- else
- a.nexthops = nhs;
+ else
+ r2->state = 0;
+ }
+
+ if (!update) // Nothing changed
+ return;
+
+ if (!nhs) // No nexthop to install
+ {
+drop:
+ rte_update(p, r->net, NULL);
+ return;
+ }
+
+ ap->dest = RTD_UNICAST;
+ nexthop_link(ap, nhs);
}
+ else
+ r->state |= STS_INSTALLED;
if (r->dest == RTDX_RECURSIVE)
- rta_set_recursive_next_hop(p->main_channel->table, &a, p_igp_table(p), r->via, IPA_NONE);
+ {
+ ap->nh.labels_append = ap->nh.labels = r->label_count;
+ memcpy(ap->nh.label, r->label_stack, r->label_count * sizeof(u32));
+ rta_set_recursive_next_hop(p->main_channel->table, ap, p_igp_table(p), r->via, IPA_NONE);
+ }
/* We skip rta_lookup() here */
- e = rte_get_temp(&a);
+ e = rte_get_temp(ap);
e->pflags = 0;
if (r->cmds)
f_eval_rte(r->cmds, &e, static_lp);
rte_update(p, r->net, e);
- r->installed = 1;
if (r->cmds)
lp_flush(static_lp);
}
static void
-static_remove(struct proto *p, struct static_route *r)
-{
- if (!r->installed)
- return;
-
- DBG("Removing static route %N via %I\n", r->net, r->via);
- rte_update(p, r->net, NULL);
- r->installed = 0;
-}
-
-static void
static_bfd_notify(struct bfd_request *req);
static void
@@ -161,6 +168,8 @@ static_decide(struct static_config *cf, struct static_route *r)
/* r->dest != RTD_MULTIPATH, but may be RTD_NONE (part of multipath route)
the route also have to be valid (r->neigh != NULL) */
+ r->state &= ~STS_WANT;
+
if (r->neigh->scope < 0)
return 0;
@@ -170,6 +179,7 @@ static_decide(struct static_config *cf, struct static_route *r)
if (r->bfd_req && r->bfd_req->state != BFD_STATE_UP)
return 0;
+ r->state |= STS_WANT;
return 1;
}
@@ -177,43 +187,23 @@ static_decide(struct static_config *cf, struct static_route *r)
static void
static_add(struct proto *p, struct static_config *cf, struct static_route *r)
{
+ if (r->mp_head && r != r->mp_head)
+ return;
+
DBG("static_add(%N,%d)\n", r->net, r->dest);
switch (r->dest)
{
- case RTD_ROUTER:
- {
- struct neighbor *n = neigh_find2(p, &r->via, r->via_if, NEF_STICKY);
- if (n)
- {
- r->chain = n->data;
- n->data = r;
- r->neigh = n;
-
- static_update_bfd(p, r);
- if (static_decide(cf, r))
- static_install(p, r, n->iface);
- else
- static_remove(p, r);
- }
- else
- {
- log(L_ERR "Static route destination %I is invalid. Ignoring.", r->via);
- static_remove(p, r);
- }
- break;
- }
-
- case RTD_DEVICE:
- break;
-
- case RTD_MULTIPATH:
+ case RTD_UNICAST:
{
int count = 0;
struct static_route *r2;
- for (r2 = r->mp_next; r2; r2 = r2->mp_next)
+ for (r2 = r; r2; r2 = r2->mp_next)
{
- struct neighbor *n = neigh_find2(p, &r2->via, r2->via_if, NEF_STICKY);
+ if (ipa_zero(r2->via)) // No struct neighbor for device routes
+ continue;
+
+ struct neighbor *n = neigh_find2(p, &r2->via, r2->iface, NEF_STICKY);
if (n)
{
r2->chain = n->data;
@@ -221,46 +211,41 @@ static_add(struct proto *p, struct static_config *cf, struct static_route *r)
r2->neigh = n;
static_update_bfd(p, r2);
- r2->installed = static_decide(cf, r2);
- count += r2->installed;
+ static_decide(cf,r2);
+ count++;
}
else
{
log(L_ERR "Static route destination %I is invalid. Ignoring.", r2->via);
- r2->installed = 0;
+ r2->state = 0;
}
}
if (count)
- static_install(p, r, NULL);
- else
- static_remove(p, r);
+ static_install(p, r);
+
break;
}
default:
- static_install(p, r, NULL);
+ static_install(p, r);
}
}
static void
static_rte_cleanup(struct proto *p UNUSED, struct static_route *r)
{
- struct static_route *r2;
-
- if (r->bfd_req)
- {
- rfree(r->bfd_req);
- r->bfd_req = NULL;
- }
+ if (r->mp_head && (r != r->mp_head))
+ return;
- if (r->dest == RTD_MULTIPATH)
- for (r2 = r->mp_next; r2; r2 = r2->mp_next)
- if (r2->bfd_req)
- {
- rfree(r2->bfd_req);
- r2->bfd_req = NULL;
- }
+ struct static_route *r2;
+
+ for (r2 = r; r2; r2 = r2->mp_next)
+ if (r2->bfd_req)
+ {
+ rfree(r2->bfd_req);
+ r2->bfd_req = NULL;
+ }
}
static int
@@ -280,8 +265,15 @@ static_start(struct proto *p)
/* We have to go UP before routes could be installed */
proto_notify_state(p, PS_UP);
- WALK_LIST(r, cf->other_routes)
+ WALK_LIST(r, cf->neigh_routes)
+ static_add(p, cf, r);
+
+ WALK_LIST(r, cf->iface_routes)
static_add(p, cf, r);
+
+ WALK_LIST(r, cf->other_routes)
+ static_install(p, r);
+
return PS_UP;
}
@@ -292,12 +284,17 @@ static_shutdown(struct proto *p)
struct static_route *r;
/* Just reset the flag, the routes will be flushed by the nest */
- WALK_LIST(r, cf->iface_routes)
- r->installed = 0;
WALK_LIST(r, cf->other_routes)
{
static_rte_cleanup(p, r);
- r->installed = 0;
+ r->state = 0;
+ }
+ WALK_LIST(r, cf->iface_routes)
+ r->state = 0;
+ WALK_LIST(r, cf->neigh_routes)
+ {
+ static_rte_cleanup(p, r);
+ r->state = 0;
}
/* Handle failure during channel reconfigure */
@@ -305,10 +302,12 @@ static_shutdown(struct proto *p)
cf = (void *) p->cf_new;
if (cf)
{
- WALK_LIST(r, cf->iface_routes)
- r->installed = 0;
WALK_LIST(r, cf->other_routes)
- r->installed = 0;
+ r->state = 0;
+ WALK_LIST(r, cf->iface_routes)
+ r->state = 0;
+ WALK_LIST(r, cf->neigh_routes)
+ r->state = 0;
}
return PS_DOWN;
@@ -326,40 +325,11 @@ static_cleanup(struct proto *p)
static void
static_update_rte(struct proto *p, struct static_route *r)
{
- switch (r->dest)
- {
- case RTD_ROUTER:
- if (static_decide((struct static_config *) p->cf, r))
- static_install(p, r, r->neigh->iface);
- else
- static_remove(p, r);
- break;
-
- case RTD_NONE: /* a part of multipath route */
- {
- int decision = static_decide((struct static_config *) p->cf, r);
- if (decision == r->installed)
- break; /* no change */
- r->installed = decision;
-
- struct static_route *r1, *r2;
- int count = 0;
- r1 = (void *) r->if_name; /* really */
- for (r2 = r1->mp_next; r2; r2 = r2->mp_next)
- count += r2->installed;
-
- if (count)
- {
- /* Set of nexthops changed - force reinstall */
- r1->installed = 0;
- static_install(p, r1, NULL);
- }
- else
- static_remove(p, r1);
+ if (r->dest != RTD_UNICAST)
+ return;
- break;
- }
- }
+ static_decide((struct static_config *) p->cf, r);
+ static_install(p, r);
}
static void
@@ -391,18 +361,13 @@ static void
static_dump_rt(struct static_route *r)
{
debug("%-1N: ", r->net);
- switch (r->dest)
- {
- case RTD_ROUTER:
- debug("via %I\n", r->via);
- break;
- case RTD_DEVICE:
+ if (r->dest == RTD_UNICAST)
+ if (ipa_zero(r->via))
debug("dev %s\n", r->if_name);
- break;
- default:
- debug("rtd %d\n", r->dest);
- break;
- }
+ else
+ debug("via %I\n", r->via);
+ else
+ debug("rtd %d\n", r->dest);
}
static void
@@ -411,12 +376,15 @@ static_dump(struct proto *p)
struct static_config *c = (void *) p->cf;
struct static_route *r;
- debug("Independent static routes:\n");
- WALK_LIST(r, c->other_routes)
+ debug("Independent static nexthops:\n");
+ WALK_LIST(r, c->neigh_routes)
static_dump_rt(r);
- debug("Device static routes:\n");
+ debug("Device static nexthops:\n");
WALK_LIST(r, c->iface_routes)
static_dump_rt(r);
+ debug("Other static routes:\n");
+ WALK_LIST(r, c->other_routes)
+ static_dump_rt(r);
}
static void
@@ -429,13 +397,21 @@ static_if_notify(struct proto *p, unsigned flags, struct iface *i)
{
WALK_LIST(r, c->iface_routes)
if (!strcmp(r->if_name, i->name))
- static_install(p, r, i);
+ {
+ r->state |= STS_WANT;
+ r->iface = i;
+ static_install(p, r);
+ }
}
else if (flags & IF_CHANGE_DOWN)
{
WALK_LIST(r, c->iface_routes)
if (!strcmp(r->if_name, i->name))
- static_remove(p, r);
+ {
+ r->state &= ~STS_WANT;
+ r->iface = NULL;
+ static_install(p, r);
+ }
}
}
@@ -448,6 +424,7 @@ static_rte_mergable(rte *pri UNUSED, rte *sec UNUSED)
void
static_init_config(struct static_config *c)
{
+ init_list(&c->neigh_routes);
init_list(&c->iface_routes);
init_list(&c->other_routes);
}
@@ -462,8 +439,12 @@ static_postconfig(struct proto_config *CF)
cf_error("Channel not specified");
+ WALK_LIST(r, cf->neigh_routes)
+ if (r->net && (r->net->type != CF->net_type))
+ cf_error("Route %N incompatible with channel type", r->net);
+
WALK_LIST(r, cf->iface_routes)
- if (r->net->type != CF->net_type)
+ if (r->net && (r->net->type != CF->net_type))
cf_error("Route %N incompatible with channel type", r->net);
WALK_LIST(r, cf->other_routes)
@@ -496,22 +477,33 @@ static_same_dest(struct static_route *x, struct static_route *y)
switch (x->dest)
{
- case RTD_ROUTER:
- return ipa_equal(x->via, y->via) && (x->via_if == y->via_if);
-
- case RTD_DEVICE:
- return !strcmp(x->if_name, y->if_name);
-
- case RTD_MULTIPATH:
- for (x = x->mp_next, y = y->mp_next;
- x && y;
- x = x->mp_next, y = y->mp_next)
- if (!ipa_equal(x->via, y->via) ||
- (x->via_if != y->via_if) ||
- (x->use_bfd != y->use_bfd) ||
- (x->weight != y->weight))
- return 0;
- return !x && !y;
+ case RTD_UNICAST:
+ {
+ struct static_route *xc, *yc;
+ for (xc = x, yc = y; xc && yc; xc = xc->mp_next, yc = yc->mp_next)
+ {
+ if (ipa_nonzero(xc->via) && ipa_nonzero(yc->via))
+ {
+ if (!ipa_equal(x->via, y->via) ||
+ (x->iface != y->iface) ||
+ (x->use_bfd != y->use_bfd) ||
+ (x->weight != y->weight) ||
+ (x->label_count != y->label_count))
+ return 0;
+ for (int i=0; i<x->label_count; i++)
+ if (x->label_stack[i] != y->label_stack[i])
+ return 0;
+ }
+ else
+ if ((!x->if_name) || (!y->if_name) ||
+ strcmp(x->if_name, y->if_name) ||
+ (x->use_bfd != y->use_bfd) ||
+ (x->weight != y->weight))
+ return 0;
+
+ }
+ return 1;
+ }
case RTDX_RECURSIVE:
return ipa_equal(x->via, y->via);
@@ -533,6 +525,9 @@ static_match(struct proto *p, struct static_route *r, struct static_config *n)
{
struct static_route *t;
+ if (r->mp_head && (r->mp_head != r))
+ return;
+
/*
* For given old route *r we find whether a route to the same
* network is also in the new route list. In that case, we keep the
@@ -543,23 +538,28 @@ static_match(struct proto *p, struct static_route *r, struct static_config *n)
if (r->neigh)
r->neigh->data = NULL;
+ WALK_LIST(t, n->neigh_routes)
+ if ((!t->mp_head || (t->mp_head == t)) && net_equal(r->net, t->net))
+ goto found;
+
WALK_LIST(t, n->iface_routes)
- if (net_equal(r->net, t->net))
+ if ((!t->mp_head || (t->mp_head == t)) && net_equal(r->net, t->net))
goto found;
WALK_LIST(t, n->other_routes)
if (net_equal(r->net, t->net))
goto found;
- static_remove(p, r);
+ r->state &= ~STS_WANT;
+ static_install(p, r);
return;
found:
+ t->state = r->state;
+
/* If destination is different, force reinstall */
- if ((r->installed > 0) && !static_same_rte(r, t))
- t->installed = -1;
- else
- t->installed = r->installed;
+ if (!static_same_rte(r, t))
+ t->state |= STS_FORCE;
}
static inline rtable *
@@ -582,20 +582,34 @@ static_reconfigure(struct proto *p, struct proto_config *CF)
return 0;
/* Delete all obsolete routes and reset neighbor entries */
+ WALK_LIST(r, o->other_routes)
+ static_match(p, r, n);
WALK_LIST(r, o->iface_routes)
static_match(p, r, n);
- WALK_LIST(r, o->other_routes)
+ WALK_LIST(r, o->neigh_routes)
static_match(p, r, n);
/* Now add all new routes, those not changed will be ignored by static_install() */
+ WALK_LIST(r, n->neigh_routes)
+ static_add(p, n, r);
+ WALK_LIST(r, o->neigh_routes)
+ static_rte_cleanup(p, r);
+
WALK_LIST(r, n->iface_routes)
{
struct iface *ifa;
if ((ifa = if_find_by_name(r->if_name)) && (ifa->flags & IF_UP))
- static_install(p, r, ifa);
+ {
+ r->iface = ifa;
+ static_install(p, r);
+ }
}
+
WALK_LIST(r, n->other_routes)
- static_add(p, n, r);
+ {
+ r->state |= STS_WANT;
+ static_install(p, r);
+ }
WALK_LIST(r, o->other_routes)
static_rte_cleanup(p, r);
@@ -606,37 +620,24 @@ static_reconfigure(struct proto *p, struct proto_config *CF)
static void
static_copy_routes(list *dlst, list *slst)
{
- struct static_route *dr, *sr;
+ struct static_route *sr;
init_list(dlst);
WALK_LIST(sr, *slst)
{
- /* copy one route */
- dr = cfg_alloc(sizeof(struct static_route));
- memcpy(dr, sr, sizeof(struct static_route));
-
- /* This fn is supposed to be called on fresh src routes, which have 'live'
- fields (like .chain, .neigh or .installed) zero, so no need to zero them */
-
- /* We need to copy multipath chain, because there are backptrs in 'if_name' */
- if (dr->dest == RTD_MULTIPATH)
- {
- struct static_route *md, *ms, **mp_last;
-
- mp_last = &(dr->mp_next);
- for (ms = sr->mp_next; ms; ms = ms->mp_next)
- {
- md = cfg_alloc(sizeof(struct static_route));
- memcpy(md, ms, sizeof(struct static_route));
- md->if_name = (void *) dr; /* really */
-
- *mp_last = md;
- mp_last = &(md->mp_next);
- }
- *mp_last = NULL;
- }
+ struct static_route *srr, *drr = NULL;
+ for (srr = sr->mp_head; srr; srr = srr->mp_next)
+ {
+ /* copy one route */
+ struct static_route *dr = cfg_alloc(sizeof(struct static_route));
+ if (drr)
+ drr->mp_next = dr;
+ else
+ add_tail(dlst, &(dr->n));
- add_tail(dlst, (node *) dr);
+ memcpy(dr, sr, sizeof(struct static_route));
+ drr = dr;
+ }
}
}
@@ -647,6 +648,7 @@ static_copy_config(struct proto_config *dest, struct proto_config *src)
struct static_config *s = (struct static_config *) src;
/* Copy route lists */
+ static_copy_routes(&d->neigh_routes, &s->neigh_routes);
static_copy_routes(&d->iface_routes, &s->iface_routes);
static_copy_routes(&d->other_routes, &s->other_routes);
}
@@ -668,30 +670,45 @@ struct protocol proto_static = {
.copy_config = static_copy_config
};
-static void
-static_show_rt(struct static_route *r)
+static byte *
+static_format_via(struct static_route *r)
{
- byte via[IPA_MAX_TEXT_LENGTH + 25];
+ static byte via[IPA_MAX_TEXT_LENGTH + 25];
switch (r->dest)
{
- case RTD_ROUTER: bsprintf(via, "via %I%J", r->via, r->via_if); break;
- case RTD_DEVICE: bsprintf(via, "dev %s", r->if_name); break;
+ case RTD_UNICAST: if (ipa_zero(r->via)) bsprintf(via, "dev %s", r->if_name);
+ else bsprintf(via, "via %I%J", r->via, r->iface);
+ break;
case RTD_BLACKHOLE: bsprintf(via, "blackhole"); break;
case RTD_UNREACHABLE: bsprintf(via, "unreachable"); break;
case RTD_PROHIBIT: bsprintf(via, "prohibited"); break;
- case RTD_MULTIPATH: bsprintf(via, "multipath"); break;
case RTDX_RECURSIVE: bsprintf(via, "recursive %I", r->via); break;
default: bsprintf(via, "???");
}
- cli_msg(-1009, "%N %s%s%s", r->net, via,
- r->bfd_req ? " (bfd)" : "", r->installed ? "" : " (dormant)");
+ return via;
+}
- struct static_route *r2;
- if (r->dest == RTD_MULTIPATH)
- for (r2 = r->mp_next; r2; r2 = r2->mp_next)
- cli_msg(-1009, "\tvia %I%J weight %d%s%s", r2->via, r2->via_if, r2->weight + 1,
- r2->bfd_req ? " (bfd)" : "", r2->installed ? "" : " (dormant)");
+static void
+static_show_rt(struct static_route *r)
+{
+ if (r->mp_head && (r != r->mp_head))
+ return;
+ if (r->mp_next)
+ {
+ cli_msg(-1009, "%N", r->net);
+ struct static_route *r2;
+ for (r2 = r; r2; r2 = r2->mp_next)
+ {
+ cli_msg(-1009, "\t%s weight %d%s%s", static_format_via(r2), r2->weight + 1,
+ r2->bfd_req ? " (bfd)" : "", (r2->state & STS_INSTALLED) ? "" : " (dormant)");
+ if (r2->mp_next == r)
+ break;
+ }
+ }
+ else
+ cli_msg(-1009, "%N %s%s%s", r->net, static_format_via(r),
+ r->bfd_req ? " (bfd)" : "", (r->state & STS_INSTALLED) ? "" : " (dormant)");
}
void
@@ -700,9 +717,11 @@ static_show(struct proto *P)
struct static_config *c = (void *) P->cf;
struct static_route *r;
- WALK_LIST(r, c->other_routes)
+ WALK_LIST(r, c->neigh_routes)
static_show_rt(r);
WALK_LIST(r, c->iface_routes)
static_show_rt(r);
+ WALK_LIST(r, c->other_routes)
+ static_show_rt(r);
cli_msg(0, "");
}