]>
Commit | Line | Data |
---|---|---|
fea12efb | 1 | /* |
2 | * Label Manager for FRR | |
3 | * | |
4 | * Copyright (C) 2017 by Bingen Eguzkitza, | |
5 | * Volta Networks Inc. | |
6 | * | |
7 | * This file is part of FreeRangeRouting (FRR) | |
8 | * | |
9 | * FRR is free software; you can redistribute it and/or modify it | |
10 | * under the terms of the GNU General Public License as published by the | |
11 | * Free Software Foundation; either version 2, or (at your option) any | |
12 | * later version. | |
13 | * | |
14 | * FRR is distributed in the hope that it will be useful, but | |
15 | * WITHOUT ANY WARRANTY; without even the implied warranty of | |
16 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU | |
17 | * General Public License for more details. | |
18 | * | |
896014f4 DL |
19 | * You should have received a copy of the GNU General Public License along |
20 | * with this program; see the file COPYING; if not, write to the Free Software | |
21 | * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA | |
fea12efb | 22 | */ |
23 | ||
43e52561 | 24 | #include <zebra.h> |
fea12efb | 25 | #include <stdio.h> |
26 | #include <string.h> | |
27 | #include <sys/types.h> | |
28 | ||
fea12efb | 29 | #include "lib/log.h" |
30 | #include "lib/memory.h" | |
31 | #include "lib/mpls.h" | |
32 | #include "lib/network.h" | |
33 | #include "lib/stream.h" | |
34 | #include "lib/zclient.h" | |
689f5a8c | 35 | #include "lib/libfrr.h" |
fea12efb | 36 | |
3801e764 DS |
37 | //#include "zebra/zserv.h" |
38 | #include "zebra/zebra_router.h" | |
43e52561 QY |
39 | #include "zebra/label_manager.h" |
40 | #include "zebra/zebra_errors.h" | |
e11d7c96 | 41 | #include "zebra/zapi_msg.h" |
8f86bb06 | 42 | #include "zebra/debug.h" |
fea12efb | 43 | |
44 | #define CONNECTION_DELAY 5 | |
45 | ||
46 | struct label_manager lbl_mgr; | |
47 | ||
48 | DEFINE_MGROUP(LBL_MGR, "Label Manager"); | |
49 | DEFINE_MTYPE_STATIC(LBL_MGR, LM_CHUNK, "Label Manager Chunk"); | |
50 | ||
e11d7c96 EDP |
51 | /* define hooks for the basic API, so that it can be specialized or served |
52 | * externally | |
fea12efb | 53 | */ |
fea12efb | 54 | |
e11d7c96 EDP |
55 | DEFINE_HOOK(lm_client_connect, |
56 | (uint8_t proto, uint16_t instance, vrf_id_t vrf_id), | |
57 | (proto, instance, vrf_id)); | |
58 | DEFINE_HOOK(lm_client_disconnect, (uint8_t proto, uint16_t instance), | |
59 | (proto, instance)); | |
60 | DEFINE_HOOK(lm_get_chunk, | |
61 | (struct label_manager_chunk * *lmc, uint8_t proto, | |
62 | uint16_t instance, uint8_t keep, uint32_t size, uint32_t base, | |
63 | vrf_id_t vrf_id), | |
64 | (lmc, proto, instance, keep, size, base, vrf_id)); | |
65 | DEFINE_HOOK(lm_release_chunk, | |
66 | (uint8_t proto, uint16_t instance, uint32_t start, uint32_t end), | |
67 | (proto, instance, start, end)); | |
68 | DEFINE_HOOK(lm_cbs_inited, (), ()); | |
69 | ||
70 | /* define wrappers to be called in zapi_msg.c (as hooks must be called in | |
71 | * source file where they were defined) | |
72 | */ | |
73 | void lm_client_connect_call(uint8_t proto, uint16_t instance, vrf_id_t vrf_id) | |
fea12efb | 74 | { |
e11d7c96 | 75 | hook_call(lm_client_connect, proto, instance, vrf_id); |
fea12efb | 76 | } |
e11d7c96 EDP |
77 | void lm_get_chunk_call(struct label_manager_chunk **lmc, uint8_t proto, |
78 | uint16_t instance, uint8_t keep, uint32_t size, | |
79 | uint32_t base, vrf_id_t vrf_id) | |
5c7ef8dc | 80 | { |
e11d7c96 | 81 | hook_call(lm_get_chunk, lmc, proto, instance, keep, size, base, vrf_id); |
5c7ef8dc | 82 | } |
e11d7c96 EDP |
83 | void lm_release_chunk_call(uint8_t proto, uint16_t instance, uint32_t start, |
84 | uint32_t end) | |
5c7ef8dc | 85 | { |
e11d7c96 | 86 | hook_call(lm_release_chunk, proto, instance, start, end); |
fea12efb | 87 | } |
88 | ||
e11d7c96 EDP |
89 | /* forward declarations of the static functions to be used for some hooks */ |
90 | static int label_manager_connect(uint8_t proto, uint16_t instance, | |
91 | vrf_id_t vrf_id); | |
92 | static int label_manager_disconnect(uint8_t proto, uint16_t instance); | |
93 | static int label_manager_get_chunk(struct label_manager_chunk **lmc, | |
94 | uint8_t proto, uint16_t instance, | |
95 | uint8_t keep, uint32_t size, uint32_t base, | |
96 | vrf_id_t vrf_id); | |
fea12efb | 97 | |
e11d7c96 | 98 | void delete_label_chunk(void *val) |
fea12efb | 99 | { |
e11d7c96 | 100 | XFREE(MTYPE_LM_CHUNK, val); |
fea12efb | 101 | } |
102 | ||
453844ab QY |
103 | /** |
104 | * Release label chunks from a client. | |
105 | * | |
106 | * Called on client disconnection or reconnection. It only releases chunks | |
107 | * with empty keep value. | |
108 | * | |
109 | * @param proto Daemon protocol of client, to identify the owner | |
110 | * @param instance Instance, to identify the owner | |
111 | * @return Number of chunks released | |
112 | */ | |
e11d7c96 | 113 | int release_daemon_label_chunks(uint8_t proto, unsigned short instance) |
453844ab | 114 | { |
453844ab QY |
115 | struct listnode *node; |
116 | struct label_manager_chunk *lmc; | |
117 | int count = 0; | |
118 | int ret; | |
119 | ||
8f86bb06 DS |
120 | if (IS_ZEBRA_DEBUG_PACKET) |
121 | zlog_debug("%s: Releasing chunks for client proto %s, instance %d", | |
122 | __func__, zebra_route_string(proto), instance); | |
e11d7c96 | 123 | |
453844ab QY |
124 | for (ALL_LIST_ELEMENTS_RO(lbl_mgr.lc_list, node, lmc)) { |
125 | if (lmc->proto == proto && lmc->instance == instance | |
126 | && lmc->keep == 0) { | |
127 | ret = release_label_chunk(lmc->proto, lmc->instance, | |
128 | lmc->start, lmc->end); | |
129 | if (ret == 0) | |
130 | count++; | |
131 | } | |
132 | } | |
133 | ||
8f86bb06 DS |
134 | if (IS_ZEBRA_DEBUG_PACKET) |
135 | zlog_debug("%s: Released %d label chunks", __func__, count); | |
453844ab QY |
136 | |
137 | return count; | |
138 | } | |
139 | ||
e11d7c96 EDP |
140 | int lm_client_disconnect_cb(struct zserv *client) |
141 | { | |
142 | uint8_t proto = client->proto; | |
143 | uint16_t instance = client->instance; | |
144 | ||
145 | hook_call(lm_client_disconnect, proto, instance); | |
146 | return 0; | |
147 | } | |
148 | ||
149 | void lm_hooks_register(void) | |
150 | { | |
151 | hook_register(lm_client_connect, label_manager_connect); | |
152 | hook_register(lm_client_disconnect, label_manager_disconnect); | |
153 | hook_register(lm_get_chunk, label_manager_get_chunk); | |
154 | hook_register(lm_release_chunk, release_label_chunk); | |
155 | } | |
156 | void lm_hooks_unregister(void) | |
157 | { | |
158 | hook_unregister(lm_client_connect, label_manager_connect); | |
159 | hook_unregister(lm_client_disconnect, label_manager_disconnect); | |
160 | hook_unregister(lm_get_chunk, label_manager_get_chunk); | |
161 | hook_unregister(lm_release_chunk, release_label_chunk); | |
162 | } | |
163 | ||
fea12efb | 164 | /** |
165 | * Init label manager (or proxy to an external one) | |
166 | */ | |
e11d7c96 | 167 | void label_manager_init(void) |
fea12efb | 168 | { |
e11d7c96 EDP |
169 | lbl_mgr.lc_list = list_new(); |
170 | lbl_mgr.lc_list->del = delete_label_chunk; | |
171 | hook_register(zserv_client_close, lm_client_disconnect_cb); | |
1002497a | 172 | |
e11d7c96 EDP |
173 | /* register default hooks for the label manager actions */ |
174 | lm_hooks_register(); | |
453844ab | 175 | |
e11d7c96 EDP |
176 | /* notify any external module that we are done */ |
177 | hook_call(lm_cbs_inited); | |
fea12efb | 178 | } |
179 | ||
0e3b6a92 | 180 | /* alloc and fill a label chunk */ |
e11d7c96 EDP |
181 | struct label_manager_chunk *create_label_chunk(uint8_t proto, |
182 | unsigned short instance, | |
183 | uint8_t keep, uint32_t start, | |
184 | uint32_t end) | |
0e3b6a92 EDP |
185 | { |
186 | /* alloc chunk, fill it and return it */ | |
187 | struct label_manager_chunk *lmc = | |
188 | XCALLOC(MTYPE_LM_CHUNK, sizeof(struct label_manager_chunk)); | |
189 | ||
190 | lmc->start = start; | |
191 | lmc->end = end; | |
192 | lmc->proto = proto; | |
193 | lmc->instance = instance; | |
194 | lmc->keep = keep; | |
195 | ||
196 | return lmc; | |
197 | } | |
198 | ||
199 | /* attempt to get a specific label chunk */ | |
e11d7c96 | 200 | static struct label_manager_chunk * |
0e3b6a92 EDP |
201 | assign_specific_label_chunk(uint8_t proto, unsigned short instance, |
202 | uint8_t keep, uint32_t size, uint32_t base) | |
203 | { | |
204 | struct label_manager_chunk *lmc; | |
205 | struct listnode *node, *next = NULL; | |
206 | struct listnode *first_node = NULL; | |
207 | struct listnode *last_node = NULL; | |
208 | struct listnode *insert_node = NULL; | |
209 | ||
210 | /* precompute last label from base and size */ | |
211 | uint32_t end = base + size - 1; | |
212 | ||
213 | /* sanities */ | |
214 | if ((base < MPLS_LABEL_UNRESERVED_MIN) | |
215 | || (end > MPLS_LABEL_UNRESERVED_MAX)) { | |
216 | zlog_err("Invalid LM request arguments: base: %u, size: %u", | |
217 | base, size); | |
218 | return NULL; | |
219 | } | |
220 | ||
221 | /* Scan the existing chunks to see if the requested range of labels | |
222 | * falls inside any of such chunks */ | |
223 | for (ALL_LIST_ELEMENTS_RO(lbl_mgr.lc_list, node, lmc)) { | |
224 | ||
225 | /* skip chunks for labels < base */ | |
226 | if (base > lmc->end) | |
227 | continue; | |
228 | ||
229 | /* requested range is not covered by any existing, free chunk. | |
230 | * Therefore, need to insert a chunk */ | |
231 | if ((end < lmc->start) && !first_node) { | |
232 | insert_node = node; | |
233 | break; | |
234 | } | |
235 | ||
236 | if (!first_node) | |
237 | first_node = node; | |
238 | ||
239 | /* if chunk is used, cannot honor request */ | |
240 | if (lmc->proto != NO_PROTO) | |
241 | return NULL; | |
242 | ||
243 | if (end < lmc->end) { | |
244 | last_node = node; | |
245 | break; | |
246 | } | |
247 | } | |
248 | ||
249 | /* insert chunk between existing chunks */ | |
250 | if (insert_node) { | |
251 | lmc = create_label_chunk(proto, instance, keep, base, end); | |
252 | listnode_add_before(lbl_mgr.lc_list, insert_node, lmc); | |
253 | return lmc; | |
254 | } | |
255 | ||
256 | if (first_node) { | |
257 | /* get node past the last one, if there */ | |
258 | if (last_node) | |
259 | last_node = listnextnode(last_node); | |
260 | ||
261 | /* delete node coming after the above chunk whose labels are | |
262 | * included in the previous one */ | |
263 | for (node = first_node; node && (node != last_node); | |
264 | node = next) { | |
7feb884d DS |
265 | struct label_manager_chunk *death; |
266 | ||
0e3b6a92 | 267 | next = listnextnode(node); |
7feb884d | 268 | death = listgetdata(node); |
0e3b6a92 | 269 | list_delete_node(lbl_mgr.lc_list, node); |
7feb884d | 270 | delete_label_chunk(death); |
0e3b6a92 EDP |
271 | } |
272 | ||
273 | lmc = create_label_chunk(proto, instance, keep, base, end); | |
274 | if (last_node) | |
275 | listnode_add_before(lbl_mgr.lc_list, last_node, lmc); | |
276 | else | |
277 | listnode_add(lbl_mgr.lc_list, lmc); | |
278 | ||
279 | return lmc; | |
280 | } else { | |
281 | /* create a new chunk past all the existing ones and link at | |
282 | * tail */ | |
283 | lmc = create_label_chunk(proto, instance, keep, base, end); | |
284 | listnode_add(lbl_mgr.lc_list, lmc); | |
285 | return lmc; | |
286 | } | |
287 | } | |
288 | ||
fea12efb | 289 | /** |
0e3b6a92 | 290 | * Core function, assigns label chunks |
fea12efb | 291 | * |
292 | * It first searches through the list to check if there's one available | |
293 | * (previously released). Otherwise it creates and assigns a new one | |
294 | * | |
295 | * @param proto Daemon protocol of client, to identify the owner | |
296 | * @param instance Instance, to identify the owner | |
297 | * @param keep If set, avoid garbage collection | |
0e3b6a92 EDP |
298 | * @param size Size of the label chunk |
299 | * @param base Desired starting label of the chunk; if MPLS_LABEL_BASE_ANY it does not apply | |
300 | * @return Pointer to the assigned label chunk, or NULL if the request could not be satisfied | |
fea12efb | 301 | */ |
d7c0a89a QY |
302 | struct label_manager_chunk *assign_label_chunk(uint8_t proto, |
303 | unsigned short instance, | |
0e3b6a92 EDP |
304 | uint8_t keep, uint32_t size, |
305 | uint32_t base) | |
fea12efb | 306 | { |
307 | struct label_manager_chunk *lmc; | |
308 | struct listnode *node; | |
0e3b6a92 EDP |
309 | uint32_t prev_end = 0; |
310 | ||
311 | /* handle chunks request with a specific base label */ | |
312 | if (base != MPLS_LABEL_BASE_ANY) | |
313 | return assign_specific_label_chunk(proto, instance, keep, size, | |
314 | base); | |
315 | ||
316 | /* appease scan-build, who gets confused by the use of macros */ | |
317 | assert(lbl_mgr.lc_list); | |
fea12efb | 318 | |
fea12efb | 319 | /* first check if there's one available */ |
320 | for (ALL_LIST_ELEMENTS_RO(lbl_mgr.lc_list, node, lmc)) { | |
d62a17ae | 321 | if (lmc->proto == NO_PROTO |
322 | && lmc->end - lmc->start + 1 == size) { | |
fea12efb | 323 | lmc->proto = proto; |
324 | lmc->instance = instance; | |
325 | lmc->keep = keep; | |
326 | return lmc; | |
327 | } | |
0e3b6a92 EDP |
328 | /* check if we hadve a "hole" behind us that we can squeeze into |
329 | */ | |
330 | if ((lmc->start > prev_end) | |
331 | && (lmc->start - prev_end >= size)) { | |
332 | lmc = create_label_chunk(proto, instance, keep, | |
333 | prev_end + 1, prev_end + size); | |
334 | listnode_add_before(lbl_mgr.lc_list, node, lmc); | |
335 | return lmc; | |
336 | } | |
337 | prev_end = lmc->end; | |
fea12efb | 338 | } |
339 | /* otherwise create a new one */ | |
0e3b6a92 | 340 | uint32_t start_free; |
fea12efb | 341 | |
342 | if (list_isempty(lbl_mgr.lc_list)) | |
0e3b6a92 | 343 | start_free = MPLS_LABEL_UNRESERVED_MIN; |
fea12efb | 344 | else |
0e3b6a92 | 345 | start_free = ((struct label_manager_chunk *)listgetdata( |
d62a17ae | 346 | listtail(lbl_mgr.lc_list))) |
347 | ->end | |
348 | + 1; | |
0e3b6a92 EDP |
349 | |
350 | if (start_free > MPLS_LABEL_UNRESERVED_MAX - size + 1) { | |
e914ccbe | 351 | flog_err(EC_ZEBRA_LM_EXHAUSTED_LABELS, |
0e3b6a92 | 352 | "Reached max labels. Start: %u, size: %u", start_free, |
1c50c1c0 | 353 | size); |
fea12efb | 354 | return NULL; |
355 | } | |
fea12efb | 356 | |
0e3b6a92 EDP |
357 | /* create chunk and link at tail */ |
358 | lmc = create_label_chunk(proto, instance, keep, start_free, | |
359 | start_free + size - 1); | |
360 | listnode_add(lbl_mgr.lc_list, lmc); | |
fea12efb | 361 | return lmc; |
362 | } | |
363 | ||
364 | /** | |
0e3b6a92 | 365 | * Core function, release no longer used label chunks |
fea12efb | 366 | * |
367 | * @param proto Daemon protocol of client, to identify the owner | |
368 | * @param instance Instance, to identify the owner | |
369 | * @param start First label of the chunk | |
370 | * @param end Last label of the chunk | |
371 | * @return 0 on success, -1 otherwise | |
372 | */ | |
d7c0a89a | 373 | int release_label_chunk(uint8_t proto, unsigned short instance, uint32_t start, |
d62a17ae | 374 | uint32_t end) |
fea12efb | 375 | { |
376 | struct listnode *node; | |
377 | struct label_manager_chunk *lmc; | |
378 | int ret = -1; | |
379 | ||
380 | /* check that size matches */ | |
8f86bb06 DS |
381 | if (IS_ZEBRA_DEBUG_PACKET) |
382 | zlog_debug("Releasing label chunk: %u - %u", start, end); | |
fea12efb | 383 | /* find chunk and disown */ |
384 | for (ALL_LIST_ELEMENTS_RO(lbl_mgr.lc_list, node, lmc)) { | |
385 | if (lmc->start != start) | |
386 | continue; | |
387 | if (lmc->end != end) | |
388 | continue; | |
389 | if (lmc->proto != proto || lmc->instance != instance) { | |
e914ccbe | 390 | flog_err(EC_ZEBRA_LM_DAEMON_MISMATCH, |
1c50c1c0 | 391 | "%s: Daemon mismatch!!", __func__); |
fea12efb | 392 | continue; |
393 | } | |
394 | lmc->proto = NO_PROTO; | |
395 | lmc->instance = 0; | |
396 | lmc->keep = 0; | |
397 | ret = 0; | |
398 | break; | |
399 | } | |
400 | if (ret != 0) | |
e914ccbe | 401 | flog_err(EC_ZEBRA_LM_UNRELEASED_CHUNK, |
1c50c1c0 | 402 | "%s: Label chunk not released!!", __func__); |
fea12efb | 403 | |
404 | return ret; | |
405 | } | |
406 | ||
e11d7c96 EDP |
407 | /* default functions to be called on hooks */ |
408 | static int label_manager_connect(uint8_t proto, uint16_t instance, | |
409 | vrf_id_t vrf_id) | |
410 | { | |
411 | /* | |
412 | * Release previous labels of same protocol and instance. | |
413 | * This is done in case it restarted from an unexpected shutdown. | |
414 | */ | |
415 | release_daemon_label_chunks(proto, instance); | |
416 | return lm_client_connect_response(proto, instance, vrf_id, 0); | |
417 | } | |
418 | static int label_manager_disconnect(uint8_t proto, uint16_t instance) | |
419 | { | |
420 | release_daemon_label_chunks(proto, instance); | |
421 | return 0; | |
422 | } | |
423 | static int label_manager_get_chunk(struct label_manager_chunk **lmc, | |
424 | uint8_t proto, uint16_t instance, | |
425 | uint8_t keep, uint32_t size, uint32_t base, | |
426 | vrf_id_t vrf_id) | |
427 | { | |
428 | *lmc = assign_label_chunk(proto, instance, keep, size, base); | |
429 | return lm_get_chunk_response(*lmc, proto, instance, vrf_id); | |
430 | } | |
431 | ||
432 | /* Respond to a connect request */ | |
433 | int lm_client_connect_response(uint8_t proto, uint16_t instance, | |
434 | vrf_id_t vrf_id, uint8_t result) | |
435 | { | |
436 | struct zserv *client = zserv_find_client(proto, instance); | |
437 | if (!client) { | |
438 | zlog_err("%s: could not find client for daemon %s instance %u", | |
439 | __func__, zebra_route_string(proto), instance); | |
440 | return 1; | |
441 | } | |
442 | return zsend_label_manager_connect_response(client, vrf_id, result); | |
443 | } | |
444 | ||
445 | /* Respond to a get_chunk request */ | |
446 | int lm_get_chunk_response(struct label_manager_chunk *lmc, uint8_t proto, | |
447 | uint16_t instance, vrf_id_t vrf_id) | |
448 | { | |
449 | struct zserv *client = zserv_find_client(proto, instance); | |
450 | if (!client) { | |
451 | zlog_err("%s: could not find client for daemon %s instance %u", | |
452 | __func__, zebra_route_string(proto), instance); | |
453 | return 1; | |
454 | } | |
455 | return zsend_assign_label_chunk_response(client, vrf_id, proto, | |
456 | instance, lmc); | |
457 | } | |
fea12efb | 458 | |
4d762f26 | 459 | void label_manager_close(void) |
fea12efb | 460 | { |
6a154c88 | 461 | list_delete(&lbl_mgr.lc_list); |
fea12efb | 462 | } |