]>
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" | |
fea12efb | 41 | |
42 | #define CONNECTION_DELAY 5 | |
43 | ||
44 | struct label_manager lbl_mgr; | |
45 | ||
7bfe377d | 46 | extern struct zebra_privs_t zserv_privs; |
47 | ||
fea12efb | 48 | DEFINE_MGROUP(LBL_MGR, "Label Manager"); |
49 | DEFINE_MTYPE_STATIC(LBL_MGR, LM_CHUNK, "Label Manager Chunk"); | |
50 | ||
51 | /* In case this zebra daemon is not acting as label manager, | |
52 | * it will be a proxy to relay messages to external label manager | |
53 | * This zclient thus is to connect to it | |
54 | */ | |
1002497a | 55 | static struct stream *obuf; |
fea12efb | 56 | static struct zclient *zclient; |
57 | bool lm_is_external; | |
58 | ||
59 | static void delete_label_chunk(void *val) | |
60 | { | |
61 | XFREE(MTYPE_LM_CHUNK, val); | |
62 | } | |
63 | ||
1cbba8ce | 64 | static int relay_response_back(void) |
5c7ef8dc | 65 | { |
66 | int ret = 0; | |
67 | struct stream *src, *dst; | |
d7c0a89a QY |
68 | uint16_t size = 0; |
69 | uint8_t marker; | |
70 | uint8_t version; | |
5c7ef8dc | 71 | vrf_id_t vrf_id; |
d7c0a89a | 72 | uint16_t resp_cmd; |
1cbba8ce FR |
73 | uint8_t proto; |
74 | const char *proto_str; | |
75 | unsigned short instance; | |
76 | struct zserv *zserv; | |
5c7ef8dc | 77 | |
614a0f86 EDP |
78 | /* sanity */ |
79 | if (!zclient || zclient->sock < 0) | |
80 | return -1; | |
81 | ||
1cbba8ce | 82 | /* input buffer with msg from label manager */ |
5c7ef8dc | 83 | src = zclient->ibuf; |
5c7ef8dc | 84 | |
85 | stream_reset(src); | |
86 | ||
1cbba8ce | 87 | /* parse header */ |
5c7ef8dc | 88 | ret = zclient_read_header(src, zclient->sock, &size, &marker, &version, |
d62a17ae | 89 | &vrf_id, &resp_cmd); |
614a0f86 EDP |
90 | if (ret < 0) { |
91 | if (errno != EAGAIN) | |
92 | flog_err(EC_ZEBRA_LM_RESPONSE, | |
93 | "Error reading Label Manager response: %s", | |
94 | strerror(errno)); | |
5c7ef8dc | 95 | return -1; |
96 | } | |
61eefcad A |
97 | |
98 | /* do not relay a msg that has nothing to do with LM */ | |
99 | switch (resp_cmd) { | |
100 | case ZEBRA_LABEL_MANAGER_CONNECT: | |
101 | case ZEBRA_LABEL_MANAGER_CONNECT_ASYNC: /* should not be seen */ | |
102 | case ZEBRA_GET_LABEL_CHUNK: | |
103 | case ZEBRA_RELEASE_LABEL_CHUNK: | |
104 | break; | |
105 | default: | |
106 | zlog_debug("Not relaying '%s' response (size %d) from LM", | |
107 | zserv_command_string(resp_cmd), size); | |
108 | return -1; | |
109 | } | |
110 | ||
111 | zlog_debug("Received '%s' response (size %d) from LM", | |
112 | zserv_command_string(resp_cmd), size); | |
113 | ||
5c7ef8dc | 114 | if (size == 0) |
115 | return -1; | |
116 | ||
1cbba8ce FR |
117 | /* Get the 'proto' field of the message */ |
118 | proto = stream_getc(src); | |
119 | ||
120 | /* Get the 'instance' field of the message */ | |
121 | instance = stream_getw(src); | |
122 | ||
123 | proto_str = zebra_route_string(proto); | |
124 | ||
125 | /* lookup the client to relay the msg to */ | |
c0ea1ae7 | 126 | zserv = zserv_find_client(proto, instance); |
1cbba8ce | 127 | if (!zserv) { |
af4c2728 | 128 | flog_err( |
e914ccbe | 129 | EC_ZEBRA_LM_NO_SUCH_CLIENT, |
0313523d FR |
130 | "Error relaying LM response: can't find client %s, instance %u", |
131 | proto_str, instance); | |
1cbba8ce FR |
132 | return -1; |
133 | } | |
35cbe02a | 134 | zlog_debug("Found client to relay LM response to client %s instance %u", |
0313523d | 135 | proto_str, instance); |
1cbba8ce FR |
136 | |
137 | /* copy msg into output buffer */ | |
138 | dst = obuf; | |
5c7ef8dc | 139 | stream_copy(dst, src); |
1cbba8ce FR |
140 | |
141 | /* send response back */ | |
881999e6 | 142 | ret = writen(zserv->sock, dst->data, stream_get_endp(dst)); |
5c7ef8dc | 143 | if (ret <= 0) { |
e914ccbe | 144 | flog_err(EC_ZEBRA_LM_RELAY_FAILED, |
1c50c1c0 QY |
145 | "Error relaying LM response to %s instance %u: %s", |
146 | proto_str, instance, strerror(errno)); | |
5c7ef8dc | 147 | return -1; |
148 | } | |
0313523d FR |
149 | zlog_debug("Relayed LM response (%d bytes) to %s instance %u", ret, |
150 | proto_str, instance); | |
5c7ef8dc | 151 | |
152 | return 0; | |
153 | } | |
154 | ||
155 | static int lm_zclient_read(struct thread *t) | |
156 | { | |
5c7ef8dc | 157 | int ret; |
158 | ||
5c7ef8dc | 159 | zclient->t_read = NULL; |
160 | ||
161 | /* read response and send it back */ | |
1cbba8ce | 162 | ret = relay_response_back(); |
5c7ef8dc | 163 | |
98e9ab8b A |
164 | /* re-arm read */ |
165 | thread_add_read(zclient->master, lm_zclient_read, NULL, | |
166 | zclient->sock, &zclient->t_read); | |
5c7ef8dc | 167 | return ret; |
168 | } | |
169 | ||
d62a17ae | 170 | static int reply_error(int cmd, struct zserv *zserv, vrf_id_t vrf_id) |
5c7ef8dc | 171 | { |
1002497a | 172 | int ret; |
5c7ef8dc | 173 | struct stream *s; |
174 | ||
1002497a | 175 | s = stream_new(ZEBRA_MAX_PACKET_SIZ); |
5c7ef8dc | 176 | |
7cf15b25 | 177 | zclient_create_header(s, cmd, vrf_id); |
5c7ef8dc | 178 | |
1cbba8ce FR |
179 | /* proto */ |
180 | stream_putc(s, zserv->proto); | |
181 | /* instance */ | |
182 | stream_putw(s, zserv->instance); | |
5c7ef8dc | 183 | /* result */ |
d62a17ae | 184 | stream_putc(s, 1); |
5c7ef8dc | 185 | |
186 | /* Write packet size. */ | |
d62a17ae | 187 | stream_putw_at(s, 0, stream_get_endp(s)); |
5c7ef8dc | 188 | |
1002497a QY |
189 | ret = writen(zserv->sock, s->data, stream_get_endp(s)); |
190 | ||
191 | stream_free(s); | |
192 | return ret; | |
5c7ef8dc | 193 | } |
fea12efb | 194 | /** |
195 | * Receive a request to get or release a label chunk and forward it to external | |
196 | * label manager. | |
197 | * | |
198 | * It's called from zserv in case it's not an actual label manager, but just a | |
199 | * proxy. | |
200 | * | |
201 | * @param cmd Type of request (connect, get or release) | |
5c7ef8dc | 202 | * @param zserv |
fea12efb | 203 | * @return 0 on success, -1 otherwise |
204 | */ | |
d62a17ae | 205 | int zread_relay_label_manager_request(int cmd, struct zserv *zserv, |
0313523d | 206 | struct stream *msg, vrf_id_t vrf_id) |
fea12efb | 207 | { |
1cbba8ce | 208 | struct stream *dst; |
5c7ef8dc | 209 | int ret = 0; |
1cbba8ce FR |
210 | uint8_t proto; |
211 | const char *proto_str; | |
212 | unsigned short instance; | |
fea12efb | 213 | |
214 | if (zclient->sock < 0) { | |
e914ccbe | 215 | flog_err(EC_ZEBRA_LM_NO_SOCKET, |
1c50c1c0 | 216 | "Unable to relay LM request: no socket"); |
d62a17ae | 217 | reply_error(cmd, zserv, vrf_id); |
fea12efb | 218 | return -1; |
219 | } | |
5c7ef8dc | 220 | |
1cbba8ce FR |
221 | /* peek msg to get proto and instance id. This zebra, which acts as |
222 | * a proxy needs to have such values for each client in order to | |
223 | * relay responses back to it. | |
224 | */ | |
225 | ||
226 | /* Get the 'proto' field of incoming msg */ | |
227 | proto = stream_getc(msg); | |
228 | ||
229 | /* Get the 'instance' field of incoming msg */ | |
230 | instance = stream_getw(msg); | |
231 | ||
232 | /* stringify proto */ | |
233 | proto_str = zebra_route_string(proto); | |
234 | ||
235 | /* check & set client proto if unset */ | |
236 | if (zserv->proto && zserv->proto != proto) { | |
e914ccbe | 237 | flog_warn(EC_ZEBRAING_LM_PROTO_MISMATCH, |
9df414fe | 238 | "Client proto(%u) != msg proto(%u)", zserv->proto, |
0313523d | 239 | proto); |
1cbba8ce FR |
240 | return -1; |
241 | } | |
242 | ||
243 | /* check & set client instance if unset */ | |
244 | if (zserv->instance && zserv->instance != instance) { | |
e914ccbe | 245 | flog_err(EC_ZEBRA_LM_BAD_INSTANCE, |
1c50c1c0 QY |
246 | "Client instance(%u) != msg instance(%u)", |
247 | zserv->instance, instance); | |
1cbba8ce FR |
248 | return -1; |
249 | } | |
250 | ||
251 | /* recall proto and instance */ | |
252 | zserv->instance = instance; | |
253 | zserv->proto = proto; | |
254 | ||
5c7ef8dc | 255 | /* in case there's any incoming message enqueued, read and forward it */ |
f533be73 | 256 | if (zserv->is_synchronous) |
257 | while (ret == 0) | |
258 | ret = relay_response_back(); | |
5c7ef8dc | 259 | |
1cbba8ce | 260 | /* get the msg buffer used toward the 'master' Label Manager */ |
fea12efb | 261 | dst = zclient->obuf; |
262 | ||
1cbba8ce FR |
263 | /* copy the message */ |
264 | stream_copy(dst, msg); | |
fea12efb | 265 | |
1cbba8ce | 266 | /* Send request to external label manager */ |
fea12efb | 267 | ret = writen(zclient->sock, dst->data, stream_get_endp(dst)); |
268 | if (ret <= 0) { | |
e914ccbe | 269 | flog_err(EC_ZEBRA_LM_RELAY_FAILED, |
1c50c1c0 QY |
270 | "Error relaying LM request from %s instance %u: %s", |
271 | proto_str, instance, strerror(errno)); | |
d62a17ae | 272 | reply_error(cmd, zserv, vrf_id); |
fea12efb | 273 | return -1; |
274 | } | |
0313523d FR |
275 | zlog_debug("Relayed LM request (%d bytes) from %s instance %u", ret, |
276 | proto_str, instance); | |
1cbba8ce | 277 | |
fea12efb | 278 | |
279 | /* Release label chunk has no response */ | |
280 | if (cmd == ZEBRA_RELEASE_LABEL_CHUNK) | |
281 | return 0; | |
282 | ||
5c7ef8dc | 283 | /* make sure we listen to the response */ |
284 | if (!zclient->t_read) | |
1cbba8ce | 285 | thread_add_read(zclient->master, lm_zclient_read, NULL, |
d62a17ae | 286 | zclient->sock, &zclient->t_read); |
fea12efb | 287 | |
288 | return 0; | |
289 | } | |
290 | ||
5c7ef8dc | 291 | static int lm_zclient_connect(struct thread *t) |
fea12efb | 292 | { |
293 | zclient->t_connect = NULL; | |
294 | ||
295 | if (zclient->sock >= 0) | |
296 | return 0; | |
297 | ||
298 | if (zclient_socket_connect(zclient) < 0) { | |
e914ccbe | 299 | flog_err(EC_ZEBRA_LM_CLIENT_CONNECTION_FAILED, |
1c50c1c0 | 300 | "Error connecting synchronous zclient!"); |
3801e764 | 301 | thread_add_timer(zrouter.master, lm_zclient_connect, zclient, |
ffa2c898 | 302 | CONNECTION_DELAY, &zclient->t_connect); |
fea12efb | 303 | return -1; |
304 | } | |
305 | ||
5c7ef8dc | 306 | /* make socket non-blocking */ |
9df414fe | 307 | (void)set_nonblocking(zclient->sock); |
5c7ef8dc | 308 | |
fea12efb | 309 | return 0; |
310 | } | |
311 | ||
312 | /** | |
313 | * Function to initialize zclient in case this is not an actual | |
314 | * label manager, but just a proxy to an external one. | |
315 | * | |
316 | * @param lm_zserv_path Path to zserv socket of external label manager | |
317 | */ | |
318 | static void lm_zclient_init(char *lm_zserv_path) | |
319 | { | |
320 | if (lm_zserv_path) | |
689f5a8c DL |
321 | frr_zclient_addr(&zclient_addr, &zclient_addr_len, |
322 | lm_zserv_path); | |
fea12efb | 323 | |
324 | /* Set default values. */ | |
3801e764 | 325 | zclient = zclient_new(zrouter.master, &zclient_options_default); |
7bfe377d | 326 | zclient->privs = &zserv_privs; |
fea12efb | 327 | zclient->sock = -1; |
328 | zclient->t_connect = NULL; | |
5c7ef8dc | 329 | lm_zclient_connect(NULL); |
fea12efb | 330 | } |
331 | ||
453844ab QY |
332 | /** |
333 | * Release label chunks from a client. | |
334 | * | |
335 | * Called on client disconnection or reconnection. It only releases chunks | |
336 | * with empty keep value. | |
337 | * | |
338 | * @param proto Daemon protocol of client, to identify the owner | |
339 | * @param instance Instance, to identify the owner | |
340 | * @return Number of chunks released | |
341 | */ | |
342 | int release_daemon_label_chunks(struct zserv *client) | |
343 | { | |
344 | uint8_t proto = client->proto; | |
345 | uint16_t instance = client->instance; | |
346 | struct listnode *node; | |
347 | struct label_manager_chunk *lmc; | |
348 | int count = 0; | |
349 | int ret; | |
350 | ||
351 | for (ALL_LIST_ELEMENTS_RO(lbl_mgr.lc_list, node, lmc)) { | |
352 | if (lmc->proto == proto && lmc->instance == instance | |
353 | && lmc->keep == 0) { | |
354 | ret = release_label_chunk(lmc->proto, lmc->instance, | |
355 | lmc->start, lmc->end); | |
356 | if (ret == 0) | |
357 | count++; | |
358 | } | |
359 | } | |
360 | ||
361 | zlog_debug("%s: Released %d label chunks", __func__, count); | |
362 | ||
363 | return count; | |
364 | } | |
365 | ||
fea12efb | 366 | /** |
367 | * Init label manager (or proxy to an external one) | |
368 | */ | |
369 | void label_manager_init(char *lm_zserv_path) | |
370 | { | |
371 | /* this is an actual label manager */ | |
372 | if (!lm_zserv_path) { | |
35cbe02a | 373 | zlog_debug("Initializing internal label manager"); |
fea12efb | 374 | lm_is_external = false; |
375 | lbl_mgr.lc_list = list_new(); | |
376 | lbl_mgr.lc_list->del = delete_label_chunk; | |
d62a17ae | 377 | } else { /* it's acting just as a proxy */ |
fea12efb | 378 | zlog_debug("Initializing external label manager at %s", |
379 | lm_zserv_path); | |
380 | lm_is_external = true; | |
381 | lm_zclient_init(lm_zserv_path); | |
382 | } | |
1002497a | 383 | |
1002497a | 384 | obuf = stream_new(ZEBRA_MAX_PACKET_SIZ); |
453844ab | 385 | |
21ccc0cf | 386 | hook_register(zserv_client_close, release_daemon_label_chunks); |
fea12efb | 387 | } |
388 | ||
0e3b6a92 EDP |
389 | /* alloc and fill a label chunk */ |
390 | static struct label_manager_chunk * | |
391 | create_label_chunk(uint8_t proto, unsigned short instance, uint8_t keep, | |
392 | uint32_t start, uint32_t end) | |
393 | { | |
394 | /* alloc chunk, fill it and return it */ | |
395 | struct label_manager_chunk *lmc = | |
396 | XCALLOC(MTYPE_LM_CHUNK, sizeof(struct label_manager_chunk)); | |
397 | ||
398 | lmc->start = start; | |
399 | lmc->end = end; | |
400 | lmc->proto = proto; | |
401 | lmc->instance = instance; | |
402 | lmc->keep = keep; | |
403 | ||
404 | return lmc; | |
405 | } | |
406 | ||
407 | /* attempt to get a specific label chunk */ | |
408 | struct label_manager_chunk * | |
409 | assign_specific_label_chunk(uint8_t proto, unsigned short instance, | |
410 | uint8_t keep, uint32_t size, uint32_t base) | |
411 | { | |
412 | struct label_manager_chunk *lmc; | |
413 | struct listnode *node, *next = NULL; | |
414 | struct listnode *first_node = NULL; | |
415 | struct listnode *last_node = NULL; | |
416 | struct listnode *insert_node = NULL; | |
417 | ||
418 | /* precompute last label from base and size */ | |
419 | uint32_t end = base + size - 1; | |
420 | ||
421 | /* sanities */ | |
422 | if ((base < MPLS_LABEL_UNRESERVED_MIN) | |
423 | || (end > MPLS_LABEL_UNRESERVED_MAX)) { | |
424 | zlog_err("Invalid LM request arguments: base: %u, size: %u", | |
425 | base, size); | |
426 | return NULL; | |
427 | } | |
428 | ||
429 | /* Scan the existing chunks to see if the requested range of labels | |
430 | * falls inside any of such chunks */ | |
431 | for (ALL_LIST_ELEMENTS_RO(lbl_mgr.lc_list, node, lmc)) { | |
432 | ||
433 | /* skip chunks for labels < base */ | |
434 | if (base > lmc->end) | |
435 | continue; | |
436 | ||
437 | /* requested range is not covered by any existing, free chunk. | |
438 | * Therefore, need to insert a chunk */ | |
439 | if ((end < lmc->start) && !first_node) { | |
440 | insert_node = node; | |
441 | break; | |
442 | } | |
443 | ||
444 | if (!first_node) | |
445 | first_node = node; | |
446 | ||
447 | /* if chunk is used, cannot honor request */ | |
448 | if (lmc->proto != NO_PROTO) | |
449 | return NULL; | |
450 | ||
451 | if (end < lmc->end) { | |
452 | last_node = node; | |
453 | break; | |
454 | } | |
455 | } | |
456 | ||
457 | /* insert chunk between existing chunks */ | |
458 | if (insert_node) { | |
459 | lmc = create_label_chunk(proto, instance, keep, base, end); | |
460 | listnode_add_before(lbl_mgr.lc_list, insert_node, lmc); | |
461 | return lmc; | |
462 | } | |
463 | ||
464 | if (first_node) { | |
465 | /* get node past the last one, if there */ | |
466 | if (last_node) | |
467 | last_node = listnextnode(last_node); | |
468 | ||
469 | /* delete node coming after the above chunk whose labels are | |
470 | * included in the previous one */ | |
471 | for (node = first_node; node && (node != last_node); | |
472 | node = next) { | |
473 | next = listnextnode(node); | |
474 | list_delete_node(lbl_mgr.lc_list, node); | |
475 | } | |
476 | ||
477 | lmc = create_label_chunk(proto, instance, keep, base, end); | |
478 | if (last_node) | |
479 | listnode_add_before(lbl_mgr.lc_list, last_node, lmc); | |
480 | else | |
481 | listnode_add(lbl_mgr.lc_list, lmc); | |
482 | ||
483 | return lmc; | |
484 | } else { | |
485 | /* create a new chunk past all the existing ones and link at | |
486 | * tail */ | |
487 | lmc = create_label_chunk(proto, instance, keep, base, end); | |
488 | listnode_add(lbl_mgr.lc_list, lmc); | |
489 | return lmc; | |
490 | } | |
491 | } | |
492 | ||
fea12efb | 493 | /** |
0e3b6a92 | 494 | * Core function, assigns label chunks |
fea12efb | 495 | * |
496 | * It first searches through the list to check if there's one available | |
497 | * (previously released). Otherwise it creates and assigns a new one | |
498 | * | |
499 | * @param proto Daemon protocol of client, to identify the owner | |
500 | * @param instance Instance, to identify the owner | |
501 | * @param keep If set, avoid garbage collection | |
0e3b6a92 EDP |
502 | * @param size Size of the label chunk |
503 | * @param base Desired starting label of the chunk; if MPLS_LABEL_BASE_ANY it does not apply | |
504 | * @return Pointer to the assigned label chunk, or NULL if the request could not be satisfied | |
fea12efb | 505 | */ |
d7c0a89a QY |
506 | struct label_manager_chunk *assign_label_chunk(uint8_t proto, |
507 | unsigned short instance, | |
0e3b6a92 EDP |
508 | uint8_t keep, uint32_t size, |
509 | uint32_t base) | |
fea12efb | 510 | { |
511 | struct label_manager_chunk *lmc; | |
512 | struct listnode *node; | |
0e3b6a92 EDP |
513 | uint32_t prev_end = 0; |
514 | ||
515 | /* handle chunks request with a specific base label */ | |
516 | if (base != MPLS_LABEL_BASE_ANY) | |
517 | return assign_specific_label_chunk(proto, instance, keep, size, | |
518 | base); | |
519 | ||
520 | /* appease scan-build, who gets confused by the use of macros */ | |
521 | assert(lbl_mgr.lc_list); | |
fea12efb | 522 | |
fea12efb | 523 | /* first check if there's one available */ |
524 | for (ALL_LIST_ELEMENTS_RO(lbl_mgr.lc_list, node, lmc)) { | |
d62a17ae | 525 | if (lmc->proto == NO_PROTO |
526 | && lmc->end - lmc->start + 1 == size) { | |
fea12efb | 527 | lmc->proto = proto; |
528 | lmc->instance = instance; | |
529 | lmc->keep = keep; | |
530 | return lmc; | |
531 | } | |
0e3b6a92 EDP |
532 | /* check if we hadve a "hole" behind us that we can squeeze into |
533 | */ | |
534 | if ((lmc->start > prev_end) | |
535 | && (lmc->start - prev_end >= size)) { | |
536 | lmc = create_label_chunk(proto, instance, keep, | |
537 | prev_end + 1, prev_end + size); | |
538 | listnode_add_before(lbl_mgr.lc_list, node, lmc); | |
539 | return lmc; | |
540 | } | |
541 | prev_end = lmc->end; | |
fea12efb | 542 | } |
543 | /* otherwise create a new one */ | |
0e3b6a92 | 544 | uint32_t start_free; |
fea12efb | 545 | |
546 | if (list_isempty(lbl_mgr.lc_list)) | |
0e3b6a92 | 547 | start_free = MPLS_LABEL_UNRESERVED_MIN; |
fea12efb | 548 | else |
0e3b6a92 | 549 | start_free = ((struct label_manager_chunk *)listgetdata( |
d62a17ae | 550 | listtail(lbl_mgr.lc_list))) |
551 | ->end | |
552 | + 1; | |
0e3b6a92 EDP |
553 | |
554 | if (start_free > MPLS_LABEL_UNRESERVED_MAX - size + 1) { | |
e914ccbe | 555 | flog_err(EC_ZEBRA_LM_EXHAUSTED_LABELS, |
0e3b6a92 | 556 | "Reached max labels. Start: %u, size: %u", start_free, |
1c50c1c0 | 557 | size); |
fea12efb | 558 | return NULL; |
559 | } | |
fea12efb | 560 | |
0e3b6a92 EDP |
561 | /* create chunk and link at tail */ |
562 | lmc = create_label_chunk(proto, instance, keep, start_free, | |
563 | start_free + size - 1); | |
564 | listnode_add(lbl_mgr.lc_list, lmc); | |
fea12efb | 565 | return lmc; |
566 | } | |
567 | ||
568 | /** | |
0e3b6a92 | 569 | * Core function, release no longer used label chunks |
fea12efb | 570 | * |
571 | * @param proto Daemon protocol of client, to identify the owner | |
572 | * @param instance Instance, to identify the owner | |
573 | * @param start First label of the chunk | |
574 | * @param end Last label of the chunk | |
575 | * @return 0 on success, -1 otherwise | |
576 | */ | |
d7c0a89a | 577 | int release_label_chunk(uint8_t proto, unsigned short instance, uint32_t start, |
d62a17ae | 578 | uint32_t end) |
fea12efb | 579 | { |
580 | struct listnode *node; | |
581 | struct label_manager_chunk *lmc; | |
582 | int ret = -1; | |
583 | ||
584 | /* check that size matches */ | |
585 | zlog_debug("Releasing label chunk: %u - %u", start, end); | |
586 | /* find chunk and disown */ | |
587 | for (ALL_LIST_ELEMENTS_RO(lbl_mgr.lc_list, node, lmc)) { | |
588 | if (lmc->start != start) | |
589 | continue; | |
590 | if (lmc->end != end) | |
591 | continue; | |
592 | if (lmc->proto != proto || lmc->instance != instance) { | |
e914ccbe | 593 | flog_err(EC_ZEBRA_LM_DAEMON_MISMATCH, |
1c50c1c0 | 594 | "%s: Daemon mismatch!!", __func__); |
fea12efb | 595 | continue; |
596 | } | |
597 | lmc->proto = NO_PROTO; | |
598 | lmc->instance = 0; | |
599 | lmc->keep = 0; | |
600 | ret = 0; | |
601 | break; | |
602 | } | |
603 | if (ret != 0) | |
e914ccbe | 604 | flog_err(EC_ZEBRA_LM_UNRELEASED_CHUNK, |
1c50c1c0 | 605 | "%s: Label chunk not released!!", __func__); |
fea12efb | 606 | |
607 | return ret; | |
608 | } | |
609 | ||
fea12efb | 610 | |
4d762f26 | 611 | void label_manager_close(void) |
fea12efb | 612 | { |
6a154c88 | 613 | list_delete(&lbl_mgr.lc_list); |
1002497a | 614 | stream_free(obuf); |
fea12efb | 615 | } |