summaryrefslogtreecommitdiffstats
path: root/src/route.c
blob: bffb4c78bf496cbac9dcb3109dd8854498ea6195 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
/*
  Copyright (c) 2013-2014, Matthias Schiffer <mschiffer@universe-factory.net>
  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 <assert.h>
#include <stdlib.h>


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);
}