/* Copyright (c) 2013-2014, Matthias Schiffer All rights reserved. Redistribution and use in source and binary forms, with or without modification, are permitted provided that the following conditions are met: 1. Redistributions of source code must retain the above copyright notice, this list of conditions and the following disclaimer. 2. Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the following disclaimer in the documentation and/or other materials provided with the distribution. THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. */ #include "babel.h" #include "neigh.h" #include #include gp_babel_route_t* gp_babel_route_new(gmrf_context_t *ctx) { gp_babel_route_t *route = calloc(1, sizeof(gp_babel_route_t)); route->metric.metric = route->feasibility_distance.metric = route->last_metric = GP_BABEL_INFINITY; route->last_nexthop = gmrf_time_unspec; route->next = ctx->routes; ctx->routes = route; return route; } gp_babel_route_t* gp_babel_route_find(gmrf_context_t *ctx, const gp_babel_node_id_t *node) { gp_babel_route_t *route; for (route = ctx->routes; route; route = route->next) { if (gp_babel_node_id_equal(&route->node, node)) return route; } return NULL; } gp_babel_route_t* gp_babel_route_get(gmrf_context_t *ctx, const gp_babel_node_id_t *node) { gp_babel_route_t *route = gp_babel_route_find(ctx, node); if (!route) { route = gp_babel_route_new(ctx); route->node = *node; } return route; } void gp_babel_route_free(gmrf_context_t *ctx UNUSED, gp_babel_route_t *route) { free(route); } gp_babel_nexthop_t* gp_babel_route_nexthop_find(const gp_babel_route_t *route, gp_babel_neigh_t *neigh) { gp_babel_nexthop_t *nexthop; for (nexthop = route->nexthops; nexthop; nexthop = nexthop->next) { if (nexthop->neigh == neigh) return nexthop; } return NULL; } gp_babel_nexthop_t* gp_babel_route_nexthop_new(gp_babel_route_t *route, gp_babel_neigh_t *neigh) { gp_babel_nexthop_t *nexthop = calloc(1, sizeof(gp_babel_nexthop_t)); nexthop->neigh = neigh; nexthop->next = route->nexthops; route->nexthops = nexthop; gp_babel_neigh_ref(neigh); return nexthop; } static inline bool nexthop_has_expired(gmrf_context_t *ctx, gp_babel_nexthop_t *nexthop) { return (gp_babel_since(ctx, nexthop->last_update) > GP_BABEL_UPDATE_TIMEOUT(nexthop->interval)); } static gp_babel_nexthop_t* select_nexthop(gmrf_context_t *ctx, const gp_babel_route_t *route) { uint16_t ret_metric = GP_BABEL_INFINITY; gp_babel_nexthop_t *ret = NULL; gp_babel_nexthop_t *nexthop; for (nexthop = route->nexthops; nexthop; nexthop = nexthop->next) { if (!nexthop->neigh) /* local */ return nexthop; if (!gp_babel_is_feasible(route, nexthop->metric_seqno)) continue; uint32_t metric = nexthop->metric_seqno.metric + gp_babel_neigh_get_cost(ctx, nexthop->neigh); if (metric < ret_metric) { ret = nexthop; ret_metric = metric; } } return ret; } static gp_babel_metric_seqno_t get_metric(gmrf_context_t *ctx, const gp_babel_route_t *route) { if (route->selected) { uint32_t metric = route->selected->metric_seqno.metric + gp_babel_neigh_get_cost(ctx, route->selected->neigh); if (metric < GP_BABEL_INFINITY) return (gp_babel_metric_seqno_t){metric, route->selected->metric_seqno.seqno}; } return (gp_babel_metric_seqno_t){GP_BABEL_INFINITY, 0}; } static void update_nexthop(gmrf_context_t *ctx, gp_babel_route_t *route) { bool had_selected = route->selected; route->selected = select_nexthop(ctx, route); route->metric = get_metric(ctx, route); if (route->selected) { if (route->selected->neigh) gmrf_debug_route(ctx->gmrf, route->node.id, sizeof(gp_babel_node_id_t), route->selected->neigh->iface->gmrf_iface, &route->selected->neigh->addr, route->metric.metric); } else { if (had_selected) { gp_babel_send_seqno_request_for(ctx, NULL, route); gmrf_debug_route_lost(ctx->gmrf, route->node.id, sizeof(gp_babel_node_id_t)); } } /* triggered updates */ int diff = route->metric.metric - route->last_metric; if (((route->last_metric == GP_BABEL_INFINITY) != (route->metric.metric == GP_BABEL_INFINITY)) || diff <= -1024 || diff >= 384) { gmrf_logf(ctx->gmrf, LOG_INFO, "route metric has changed significantly, sending updates"); gmrf_iface_state_t *iface; for (iface = ctx->interfaces; iface; iface = iface->next) gp_babel_send_update_for_route(ctx, iface, NULL, route); } } static void delete_nexthop(gmrf_context_t *ctx, gp_babel_route_t *route, gp_babel_nexthop_t *nexthop) { gp_babel_nexthop_t **nexthopp; for (nexthopp = &route->nexthops; *nexthopp; nexthopp = &(*nexthopp)->next) { if (*nexthopp == nexthop) break; } assert(*nexthopp == nexthop); *nexthopp = nexthop->next; if (route->selected == nexthop) route->selected = NULL; gp_babel_neigh_unref(nexthop->neigh); free(nexthop); route->last_nexthop = gmrf_now(ctx->gmrf); } void gp_babel_route_nexthop_delete(gmrf_context_t *ctx, gp_babel_route_t *route, gp_babel_nexthop_t *nexthop) { delete_nexthop(ctx, route, nexthop); if (!route->selected) gp_babel_route_maintain(ctx, route); } static void maintain_nexthop(gmrf_context_t *ctx, gp_babel_route_t *route, gp_babel_nexthop_t *nexthop) { if (!nexthop->neigh) /* local */ return; if (nexthop_has_expired(ctx, nexthop)) { delete_nexthop(ctx, route, nexthop); return; } if (gp_babel_since(ctx, nexthop->last_update) > GP_BABEL_UPDATE_REQUEST_TIMEOUT(nexthop->interval) && route->selected == nexthop && !nexthop->requested_update) { gmrf_logf(ctx->gmrf, LOG_INFO, "route about to expire, requesting update"); gp_babel_send_route_request(ctx, NULL, nexthop->neigh, &route->node); nexthop->requested_update = true; } } static void maintain_nexthops(gmrf_context_t *ctx, gp_babel_route_t *route) { gp_babel_nexthop_t *nexthop, *next; for (nexthop = route->nexthops; nexthop; nexthop = next) { next = nexthop->next; maintain_nexthop(ctx, route, nexthop); } } void gp_babel_route_maintain(gmrf_context_t *ctx, gp_babel_route_t *route) { maintain_nexthops(ctx, route); update_nexthop(ctx, route); } void gp_babel_route_update_nexthop(gmrf_context_t *ctx, gp_babel_route_t *route, gp_babel_nexthop_t *nexthop, gp_babel_metric_seqno_t ms, uint16_t interval) { if (ms.metric == GP_BABEL_INFINITY) { gmrf_logf(ctx->gmrf, LOG_DEBUG, "retract received, deleting nexthop"); delete_nexthop(ctx, route, nexthop); } else { nexthop->metric_seqno = ms; nexthop->interval = interval; nexthop->requested_update = false; nexthop->last_update = gmrf_now(ctx->gmrf); } gp_babel_route_maintain(ctx, route); }