]> git.proxmox.com Git - mirror_frr.git/blame - zebra/label_manager.c
Merge pull request #11852 from gpnaveen/bgp_admin_dist
[mirror_frr.git] / zebra / label_manager.c
CommitLineData
fea12efb 1/*
2 * Label Manager for FRR
3 *
4 * Copyright (C) 2017 by Bingen Eguzkitza,
5 * Volta Networks Inc.
6 *
8678d638 7 * This file is part of FRRouting (FRR)
fea12efb 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
46struct label_manager lbl_mgr;
47
48DEFINE_MGROUP(LBL_MGR, "Label Manager");
49DEFINE_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
4cebdb9b
MS
55DEFINE_HOOK(lm_client_connect, (struct zserv *client, vrf_id_t vrf_id),
56 (client, vrf_id));
57DEFINE_HOOK(lm_client_disconnect, (struct zserv *client), (client));
e11d7c96 58DEFINE_HOOK(lm_get_chunk,
4cebdb9b
MS
59 (struct label_manager_chunk * *lmc, struct zserv *client,
60 uint8_t keep, uint32_t size, uint32_t base, vrf_id_t vrf_id),
61 (lmc, client, keep, size, base, vrf_id));
e11d7c96 62DEFINE_HOOK(lm_release_chunk,
4cebdb9b
MS
63 (struct zserv *client, uint32_t start, uint32_t end),
64 (client, start, end));
e11d7c96
EDP
65DEFINE_HOOK(lm_cbs_inited, (), ());
66
67/* define wrappers to be called in zapi_msg.c (as hooks must be called in
68 * source file where they were defined)
69 */
4cebdb9b 70void lm_client_connect_call(struct zserv *client, vrf_id_t vrf_id)
fea12efb 71{
4cebdb9b 72 hook_call(lm_client_connect, client, vrf_id);
fea12efb 73}
4cebdb9b
MS
74void lm_get_chunk_call(struct label_manager_chunk **lmc, struct zserv *client,
75 uint8_t keep, uint32_t size, uint32_t base,
76 vrf_id_t vrf_id)
5c7ef8dc 77{
4cebdb9b 78 hook_call(lm_get_chunk, lmc, client, keep, size, base, vrf_id);
5c7ef8dc 79}
4cebdb9b 80void lm_release_chunk_call(struct zserv *client, uint32_t start, uint32_t end)
5c7ef8dc 81{
4cebdb9b 82 hook_call(lm_release_chunk, client, start, end);
fea12efb 83}
84
e11d7c96 85/* forward declarations of the static functions to be used for some hooks */
4cebdb9b
MS
86static int label_manager_connect(struct zserv *client, vrf_id_t vrf_id);
87static int label_manager_disconnect(struct zserv *client);
e11d7c96 88static int label_manager_get_chunk(struct label_manager_chunk **lmc,
4cebdb9b
MS
89 struct zserv *client, uint8_t keep,
90 uint32_t size, uint32_t base,
e11d7c96 91 vrf_id_t vrf_id);
4cebdb9b
MS
92static int label_manager_release_label_chunk(struct zserv *client,
93 uint32_t start, uint32_t end);
fea12efb 94
507d2737 95void delete_label_chunk(void *val)
fea12efb 96{
e11d7c96 97 XFREE(MTYPE_LM_CHUNK, val);
fea12efb 98}
99
453844ab
QY
100/**
101 * Release label chunks from a client.
102 *
103 * Called on client disconnection or reconnection. It only releases chunks
104 * with empty keep value.
105 *
106 * @param proto Daemon protocol of client, to identify the owner
107 * @param instance Instance, to identify the owner
108 * @return Number of chunks released
109 */
4cebdb9b 110int release_daemon_label_chunks(struct zserv *client)
453844ab 111{
453844ab
QY
112 struct listnode *node;
113 struct label_manager_chunk *lmc;
114 int count = 0;
115 int ret;
116
8f86bb06 117 if (IS_ZEBRA_DEBUG_PACKET)
4cebdb9b
MS
118 zlog_debug("%s: Releasing chunks for client proto %s, instance %d, session %u",
119 __func__, zebra_route_string(client->proto),
120 client->instance, client->session_id);
e11d7c96 121
453844ab 122 for (ALL_LIST_ELEMENTS_RO(lbl_mgr.lc_list, node, lmc)) {
4cebdb9b
MS
123 if (lmc->proto == client->proto &&
124 lmc->instance == client->instance &&
125 lmc->session_id == client->session_id && lmc->keep == 0) {
453844ab 126 ret = release_label_chunk(lmc->proto, lmc->instance,
4cebdb9b 127 lmc->session_id,
453844ab
QY
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
140int lm_client_disconnect_cb(struct zserv *client)
141{
4cebdb9b 142 hook_call(lm_client_disconnect, client);
e11d7c96
EDP
143 return 0;
144}
145
146void lm_hooks_register(void)
147{
148 hook_register(lm_client_connect, label_manager_connect);
149 hook_register(lm_client_disconnect, label_manager_disconnect);
150 hook_register(lm_get_chunk, label_manager_get_chunk);
4cebdb9b 151 hook_register(lm_release_chunk, label_manager_release_label_chunk);
e11d7c96
EDP
152}
153void lm_hooks_unregister(void)
154{
155 hook_unregister(lm_client_connect, label_manager_connect);
156 hook_unregister(lm_client_disconnect, label_manager_disconnect);
157 hook_unregister(lm_get_chunk, label_manager_get_chunk);
4cebdb9b 158 hook_unregister(lm_release_chunk, label_manager_release_label_chunk);
e11d7c96
EDP
159}
160
fea12efb 161/**
162 * Init label manager (or proxy to an external one)
163 */
e11d7c96 164void label_manager_init(void)
fea12efb 165{
e11d7c96
EDP
166 lbl_mgr.lc_list = list_new();
167 lbl_mgr.lc_list->del = delete_label_chunk;
168 hook_register(zserv_client_close, lm_client_disconnect_cb);
1002497a 169
e11d7c96
EDP
170 /* register default hooks for the label manager actions */
171 lm_hooks_register();
453844ab 172
e11d7c96
EDP
173 /* notify any external module that we are done */
174 hook_call(lm_cbs_inited);
fea12efb 175}
176
0e3b6a92 177/* alloc and fill a label chunk */
507d2737
PR
178struct label_manager_chunk *
179create_label_chunk(uint8_t proto, unsigned short instance, uint32_t session_id,
180 uint8_t keep, uint32_t start, uint32_t end)
0e3b6a92
EDP
181{
182 /* alloc chunk, fill it and return it */
183 struct label_manager_chunk *lmc =
184 XCALLOC(MTYPE_LM_CHUNK, sizeof(struct label_manager_chunk));
185
186 lmc->start = start;
187 lmc->end = end;
188 lmc->proto = proto;
189 lmc->instance = instance;
4cebdb9b 190 lmc->session_id = session_id;
0e3b6a92
EDP
191 lmc->keep = keep;
192
193 return lmc;
194}
195
196/* attempt to get a specific label chunk */
e11d7c96 197static struct label_manager_chunk *
0e3b6a92 198assign_specific_label_chunk(uint8_t proto, unsigned short instance,
4cebdb9b
MS
199 uint32_t session_id, uint8_t keep, uint32_t size,
200 uint32_t base)
0e3b6a92
EDP
201{
202 struct label_manager_chunk *lmc;
203 struct listnode *node, *next = NULL;
204 struct listnode *first_node = NULL;
205 struct listnode *last_node = NULL;
206 struct listnode *insert_node = NULL;
207
208 /* precompute last label from base and size */
209 uint32_t end = base + size - 1;
210
211 /* sanities */
212 if ((base < MPLS_LABEL_UNRESERVED_MIN)
213 || (end > MPLS_LABEL_UNRESERVED_MAX)) {
214 zlog_err("Invalid LM request arguments: base: %u, size: %u",
215 base, size);
216 return NULL;
217 }
218
219 /* Scan the existing chunks to see if the requested range of labels
220 * falls inside any of such chunks */
221 for (ALL_LIST_ELEMENTS_RO(lbl_mgr.lc_list, node, lmc)) {
222
223 /* skip chunks for labels < base */
224 if (base > lmc->end)
225 continue;
226
227 /* requested range is not covered by any existing, free chunk.
228 * Therefore, need to insert a chunk */
229 if ((end < lmc->start) && !first_node) {
230 insert_node = node;
231 break;
232 }
233
234 if (!first_node)
235 first_node = node;
236
237 /* if chunk is used, cannot honor request */
238 if (lmc->proto != NO_PROTO)
239 return NULL;
240
7658c2e5 241 if (end <= lmc->end) {
0e3b6a92
EDP
242 last_node = node;
243 break;
244 }
245 }
246
247 /* insert chunk between existing chunks */
248 if (insert_node) {
4cebdb9b
MS
249 lmc = create_label_chunk(proto, instance, session_id, keep,
250 base, end);
0e3b6a92
EDP
251 listnode_add_before(lbl_mgr.lc_list, insert_node, lmc);
252 return lmc;
253 }
254
255 if (first_node) {
256 /* get node past the last one, if there */
257 if (last_node)
258 last_node = listnextnode(last_node);
259
260 /* delete node coming after the above chunk whose labels are
261 * included in the previous one */
262 for (node = first_node; node && (node != last_node);
263 node = next) {
7feb884d
DS
264 struct label_manager_chunk *death;
265
0e3b6a92 266 next = listnextnode(node);
7feb884d 267 death = listgetdata(node);
0e3b6a92 268 list_delete_node(lbl_mgr.lc_list, node);
7feb884d 269 delete_label_chunk(death);
0e3b6a92
EDP
270 }
271
4cebdb9b
MS
272 lmc = create_label_chunk(proto, instance, session_id, keep,
273 base, end);
0e3b6a92
EDP
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 */
4cebdb9b
MS
283 lmc = create_label_chunk(proto, instance, session_id, keep,
284 base, end);
0e3b6a92
EDP
285 listnode_add(lbl_mgr.lc_list, lmc);
286 return lmc;
287 }
288}
289
fea12efb 290/**
0e3b6a92 291 * Core function, assigns label chunks
fea12efb 292 *
293 * It first searches through the list to check if there's one available
294 * (previously released). Otherwise it creates and assigns a new one
295 *
296 * @param proto Daemon protocol of client, to identify the owner
297 * @param instance Instance, to identify the owner
298 * @param keep If set, avoid garbage collection
0e3b6a92
EDP
299 * @param size Size of the label chunk
300 * @param base Desired starting label of the chunk; if MPLS_LABEL_BASE_ANY it does not apply
301 * @return Pointer to the assigned label chunk, or NULL if the request could not be satisfied
fea12efb 302 */
507d2737
PR
303struct label_manager_chunk *
304assign_label_chunk(uint8_t proto, unsigned short instance, uint32_t session_id,
305 uint8_t keep, uint32_t size, uint32_t base)
fea12efb 306{
307 struct label_manager_chunk *lmc;
308 struct listnode *node;
3c844979 309 uint32_t prev_end = MPLS_LABEL_UNRESERVED_MIN;
0e3b6a92
EDP
310
311 /* handle chunks request with a specific base label */
312 if (base != MPLS_LABEL_BASE_ANY)
4cebdb9b
MS
313 return assign_specific_label_chunk(proto, instance, session_id,
314 keep, size, base);
0e3b6a92
EDP
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;
4cebdb9b 325 lmc->session_id = session_id;
fea12efb 326 lmc->keep = keep;
327 return lmc;
328 }
0e3b6a92
EDP
329 /* check if we hadve a "hole" behind us that we can squeeze into
330 */
18998228 331 if ((lmc->start > prev_end) && (lmc->start - prev_end > size)) {
4cebdb9b
MS
332 lmc = create_label_chunk(proto, instance, session_id,
333 keep, prev_end + 1,
334 prev_end + size);
0e3b6a92
EDP
335 listnode_add_before(lbl_mgr.lc_list, node, lmc);
336 return lmc;
337 }
338 prev_end = lmc->end;
fea12efb 339 }
340 /* otherwise create a new one */
0e3b6a92 341 uint32_t start_free;
fea12efb 342
343 if (list_isempty(lbl_mgr.lc_list))
0e3b6a92 344 start_free = MPLS_LABEL_UNRESERVED_MIN;
fea12efb 345 else
0e3b6a92 346 start_free = ((struct label_manager_chunk *)listgetdata(
d62a17ae 347 listtail(lbl_mgr.lc_list)))
348 ->end
349 + 1;
0e3b6a92
EDP
350
351 if (start_free > MPLS_LABEL_UNRESERVED_MAX - size + 1) {
e914ccbe 352 flog_err(EC_ZEBRA_LM_EXHAUSTED_LABELS,
0e3b6a92 353 "Reached max labels. Start: %u, size: %u", start_free,
1c50c1c0 354 size);
fea12efb 355 return NULL;
356 }
fea12efb 357
0e3b6a92 358 /* create chunk and link at tail */
4cebdb9b 359 lmc = create_label_chunk(proto, instance, session_id, keep, start_free,
0e3b6a92
EDP
360 start_free + size - 1);
361 listnode_add(lbl_mgr.lc_list, lmc);
fea12efb 362 return lmc;
363}
364
4cebdb9b
MS
365/**
366 * Release label chunks from a client.
367 *
368 * Called on client disconnection or reconnection. It only releases chunks
369 * with empty keep value.
370 *
371 * @param client Client zapi session
372 * @param start First label of the chunk
373 * @param end Last label of the chunk
374 * @return 0 on success
375 */
376static int label_manager_release_label_chunk(struct zserv *client,
377 uint32_t start, uint32_t end)
378{
379 return release_label_chunk(client->proto, client->instance,
380 client->session_id, start, end);
381}
382
fea12efb 383/**
0e3b6a92 384 * Core function, release no longer used label chunks
fea12efb 385 *
386 * @param proto Daemon protocol of client, to identify the owner
387 * @param instance Instance, to identify the owner
d3d9639d 388 * @param session_id Zclient session ID, to identify the zclient session
fea12efb 389 * @param start First label of the chunk
390 * @param end Last label of the chunk
391 * @return 0 on success, -1 otherwise
392 */
507d2737 393int release_label_chunk(uint8_t proto, unsigned short instance,
4cebdb9b 394 uint32_t session_id, uint32_t start, uint32_t end)
fea12efb 395{
396 struct listnode *node;
397 struct label_manager_chunk *lmc;
398 int ret = -1;
399
400 /* check that size matches */
8f86bb06
DS
401 if (IS_ZEBRA_DEBUG_PACKET)
402 zlog_debug("Releasing label chunk: %u - %u", start, end);
fea12efb 403 /* find chunk and disown */
404 for (ALL_LIST_ELEMENTS_RO(lbl_mgr.lc_list, node, lmc)) {
405 if (lmc->start != start)
406 continue;
407 if (lmc->end != end)
408 continue;
4cebdb9b
MS
409 if (lmc->proto != proto || lmc->instance != instance ||
410 lmc->session_id != session_id) {
e914ccbe 411 flog_err(EC_ZEBRA_LM_DAEMON_MISMATCH,
1c50c1c0 412 "%s: Daemon mismatch!!", __func__);
fea12efb 413 continue;
414 }
415 lmc->proto = NO_PROTO;
416 lmc->instance = 0;
4cebdb9b 417 lmc->session_id = 0;
fea12efb 418 lmc->keep = 0;
419 ret = 0;
420 break;
421 }
422 if (ret != 0)
e914ccbe 423 flog_err(EC_ZEBRA_LM_UNRELEASED_CHUNK,
1c50c1c0 424 "%s: Label chunk not released!!", __func__);
fea12efb 425
426 return ret;
427}
428
e11d7c96 429/* default functions to be called on hooks */
4cebdb9b 430static int label_manager_connect(struct zserv *client, vrf_id_t vrf_id)
e11d7c96
EDP
431{
432 /*
433 * Release previous labels of same protocol and instance.
434 * This is done in case it restarted from an unexpected shutdown.
435 */
4cebdb9b
MS
436 release_daemon_label_chunks(client);
437 return zsend_label_manager_connect_response(client, vrf_id, 0);
e11d7c96 438}
4cebdb9b 439static int label_manager_disconnect(struct zserv *client)
e11d7c96 440{
4cebdb9b 441 release_daemon_label_chunks(client);
e11d7c96
EDP
442 return 0;
443}
444static int label_manager_get_chunk(struct label_manager_chunk **lmc,
4cebdb9b
MS
445 struct zserv *client, uint8_t keep,
446 uint32_t size, uint32_t base,
e11d7c96
EDP
447 vrf_id_t vrf_id)
448{
4cebdb9b
MS
449 *lmc = assign_label_chunk(client->proto, client->instance,
450 client->session_id, keep, size, base);
451 return lm_get_chunk_response(*lmc, client, vrf_id);
e11d7c96
EDP
452}
453
454/* Respond to a connect request */
455int lm_client_connect_response(uint8_t proto, uint16_t instance,
4cebdb9b
MS
456 uint32_t session_id, vrf_id_t vrf_id,
457 uint8_t result)
e11d7c96 458{
4cebdb9b
MS
459 struct zserv *client = zserv_find_client_session(proto, instance,
460 session_id);
e11d7c96 461 if (!client) {
4cebdb9b
MS
462 zlog_err("%s: could not find client for daemon %s instance %u session %u",
463 __func__, zebra_route_string(proto), instance,
464 session_id);
e11d7c96
EDP
465 return 1;
466 }
467 return zsend_label_manager_connect_response(client, vrf_id, result);
468}
469
470/* Respond to a get_chunk request */
4cebdb9b
MS
471int lm_get_chunk_response(struct label_manager_chunk *lmc, struct zserv *client,
472 vrf_id_t vrf_id)
e11d7c96 473{
19358322
EDP
474 if (!lmc)
475 flog_err(EC_ZEBRA_LM_CANNOT_ASSIGN_CHUNK,
476 "Unable to assign Label Chunk to %s instance %u",
4cebdb9b 477 zebra_route_string(client->proto), client->instance);
19358322
EDP
478 else if (IS_ZEBRA_DEBUG_PACKET)
479 zlog_debug("Assigned Label Chunk %u - %u to %s instance %u",
4cebdb9b
MS
480 lmc->start, lmc->end,
481 zebra_route_string(client->proto), client->instance);
19358322 482
4cebdb9b 483 return zsend_assign_label_chunk_response(client, vrf_id, lmc);
e11d7c96 484}
fea12efb 485
4d762f26 486void label_manager_close(void)
fea12efb 487{
6a154c88 488 list_delete(&lbl_mgr.lc_list);
fea12efb 489}