]> git.proxmox.com Git - mirror_frr.git/blob - zebra/label_manager.c
lib, zebra: support label chunk requests for SRGB
[mirror_frr.git] / zebra / label_manager.c
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 *
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
22 */
23
24 #include <zebra.h>
25 #include <stdio.h>
26 #include <string.h>
27 #include <sys/types.h>
28
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"
35 #include "lib/libfrr.h"
36
37 //#include "zebra/zserv.h"
38 #include "zebra/zebra_router.h"
39 #include "zebra/label_manager.h"
40 #include "zebra/zebra_errors.h"
41
42 #define CONNECTION_DELAY 5
43
44 struct label_manager lbl_mgr;
45
46 extern struct zebra_privs_t zserv_privs;
47
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 */
55 static struct stream *obuf;
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
64 static int relay_response_back(void)
65 {
66 int ret = 0;
67 struct stream *src, *dst;
68 uint16_t size = 0;
69 uint8_t marker;
70 uint8_t version;
71 vrf_id_t vrf_id;
72 uint16_t resp_cmd;
73 uint8_t proto;
74 const char *proto_str;
75 unsigned short instance;
76 struct zserv *zserv;
77
78 /* sanity */
79 if (!zclient || zclient->sock < 0)
80 return -1;
81
82 /* input buffer with msg from label manager */
83 src = zclient->ibuf;
84
85 stream_reset(src);
86
87 /* parse header */
88 ret = zclient_read_header(src, zclient->sock, &size, &marker, &version,
89 &vrf_id, &resp_cmd);
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));
95 return -1;
96 }
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
114 if (size == 0)
115 return -1;
116
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 */
126 zserv = zserv_find_client(proto, instance);
127 if (!zserv) {
128 flog_err(
129 EC_ZEBRA_LM_NO_SUCH_CLIENT,
130 "Error relaying LM response: can't find client %s, instance %u",
131 proto_str, instance);
132 return -1;
133 }
134 zlog_debug("Found client to relay LM response to client %s instance %u",
135 proto_str, instance);
136
137 /* copy msg into output buffer */
138 dst = obuf;
139 stream_copy(dst, src);
140
141 /* send response back */
142 ret = writen(zserv->sock, dst->data, stream_get_endp(dst));
143 if (ret <= 0) {
144 flog_err(EC_ZEBRA_LM_RELAY_FAILED,
145 "Error relaying LM response to %s instance %u: %s",
146 proto_str, instance, strerror(errno));
147 return -1;
148 }
149 zlog_debug("Relayed LM response (%d bytes) to %s instance %u", ret,
150 proto_str, instance);
151
152 return 0;
153 }
154
155 static int lm_zclient_read(struct thread *t)
156 {
157 int ret;
158
159 zclient->t_read = NULL;
160
161 /* read response and send it back */
162 ret = relay_response_back();
163
164 /* re-arm read */
165 thread_add_read(zclient->master, lm_zclient_read, NULL,
166 zclient->sock, &zclient->t_read);
167 return ret;
168 }
169
170 static int reply_error(int cmd, struct zserv *zserv, vrf_id_t vrf_id)
171 {
172 int ret;
173 struct stream *s;
174
175 s = stream_new(ZEBRA_MAX_PACKET_SIZ);
176
177 zclient_create_header(s, cmd, vrf_id);
178
179 /* proto */
180 stream_putc(s, zserv->proto);
181 /* instance */
182 stream_putw(s, zserv->instance);
183 /* result */
184 stream_putc(s, 1);
185
186 /* Write packet size. */
187 stream_putw_at(s, 0, stream_get_endp(s));
188
189 ret = writen(zserv->sock, s->data, stream_get_endp(s));
190
191 stream_free(s);
192 return ret;
193 }
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)
202 * @param zserv
203 * @return 0 on success, -1 otherwise
204 */
205 int zread_relay_label_manager_request(int cmd, struct zserv *zserv,
206 struct stream *msg, vrf_id_t vrf_id)
207 {
208 struct stream *dst;
209 int ret = 0;
210 uint8_t proto;
211 const char *proto_str;
212 unsigned short instance;
213
214 if (zclient->sock < 0) {
215 flog_err(EC_ZEBRA_LM_NO_SOCKET,
216 "Unable to relay LM request: no socket");
217 reply_error(cmd, zserv, vrf_id);
218 return -1;
219 }
220
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) {
237 flog_warn(EC_ZEBRAING_LM_PROTO_MISMATCH,
238 "Client proto(%u) != msg proto(%u)", zserv->proto,
239 proto);
240 return -1;
241 }
242
243 /* check & set client instance if unset */
244 if (zserv->instance && zserv->instance != instance) {
245 flog_err(EC_ZEBRA_LM_BAD_INSTANCE,
246 "Client instance(%u) != msg instance(%u)",
247 zserv->instance, instance);
248 return -1;
249 }
250
251 /* recall proto and instance */
252 zserv->instance = instance;
253 zserv->proto = proto;
254
255 /* in case there's any incoming message enqueued, read and forward it */
256 if (zserv->is_synchronous)
257 while (ret == 0)
258 ret = relay_response_back();
259
260 /* get the msg buffer used toward the 'master' Label Manager */
261 dst = zclient->obuf;
262
263 /* copy the message */
264 stream_copy(dst, msg);
265
266 /* Send request to external label manager */
267 ret = writen(zclient->sock, dst->data, stream_get_endp(dst));
268 if (ret <= 0) {
269 flog_err(EC_ZEBRA_LM_RELAY_FAILED,
270 "Error relaying LM request from %s instance %u: %s",
271 proto_str, instance, strerror(errno));
272 reply_error(cmd, zserv, vrf_id);
273 return -1;
274 }
275 zlog_debug("Relayed LM request (%d bytes) from %s instance %u", ret,
276 proto_str, instance);
277
278
279 /* Release label chunk has no response */
280 if (cmd == ZEBRA_RELEASE_LABEL_CHUNK)
281 return 0;
282
283 /* make sure we listen to the response */
284 if (!zclient->t_read)
285 thread_add_read(zclient->master, lm_zclient_read, NULL,
286 zclient->sock, &zclient->t_read);
287
288 return 0;
289 }
290
291 static int lm_zclient_connect(struct thread *t)
292 {
293 zclient->t_connect = NULL;
294
295 if (zclient->sock >= 0)
296 return 0;
297
298 if (zclient_socket_connect(zclient) < 0) {
299 flog_err(EC_ZEBRA_LM_CLIENT_CONNECTION_FAILED,
300 "Error connecting synchronous zclient!");
301 thread_add_timer(zrouter.master, lm_zclient_connect, zclient,
302 CONNECTION_DELAY, &zclient->t_connect);
303 return -1;
304 }
305
306 /* make socket non-blocking */
307 (void)set_nonblocking(zclient->sock);
308
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)
321 frr_zclient_addr(&zclient_addr, &zclient_addr_len,
322 lm_zserv_path);
323
324 /* Set default values. */
325 zclient = zclient_new(zrouter.master, &zclient_options_default);
326 zclient->privs = &zserv_privs;
327 zclient->sock = -1;
328 zclient->t_connect = NULL;
329 lm_zclient_connect(NULL);
330 }
331
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
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) {
373 zlog_debug("Initializing internal label manager");
374 lm_is_external = false;
375 lbl_mgr.lc_list = list_new();
376 lbl_mgr.lc_list->del = delete_label_chunk;
377 } else { /* it's acting just as a proxy */
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 }
383
384 obuf = stream_new(ZEBRA_MAX_PACKET_SIZ);
385
386 hook_register(zserv_client_close, release_daemon_label_chunks);
387 }
388
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
493 /**
494 * Core function, assigns label chunks
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
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
505 */
506 struct label_manager_chunk *assign_label_chunk(uint8_t proto,
507 unsigned short instance,
508 uint8_t keep, uint32_t size,
509 uint32_t base)
510 {
511 struct label_manager_chunk *lmc;
512 struct listnode *node;
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);
522
523 /* first check if there's one available */
524 for (ALL_LIST_ELEMENTS_RO(lbl_mgr.lc_list, node, lmc)) {
525 if (lmc->proto == NO_PROTO
526 && lmc->end - lmc->start + 1 == size) {
527 lmc->proto = proto;
528 lmc->instance = instance;
529 lmc->keep = keep;
530 return lmc;
531 }
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;
542 }
543 /* otherwise create a new one */
544 uint32_t start_free;
545
546 if (list_isempty(lbl_mgr.lc_list))
547 start_free = MPLS_LABEL_UNRESERVED_MIN;
548 else
549 start_free = ((struct label_manager_chunk *)listgetdata(
550 listtail(lbl_mgr.lc_list)))
551 ->end
552 + 1;
553
554 if (start_free > MPLS_LABEL_UNRESERVED_MAX - size + 1) {
555 flog_err(EC_ZEBRA_LM_EXHAUSTED_LABELS,
556 "Reached max labels. Start: %u, size: %u", start_free,
557 size);
558 return NULL;
559 }
560
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);
565 return lmc;
566 }
567
568 /**
569 * Core function, release no longer used label chunks
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 */
577 int release_label_chunk(uint8_t proto, unsigned short instance, uint32_t start,
578 uint32_t end)
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) {
593 flog_err(EC_ZEBRA_LM_DAEMON_MISMATCH,
594 "%s: Daemon mismatch!!", __func__);
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)
604 flog_err(EC_ZEBRA_LM_UNRELEASED_CHUNK,
605 "%s: Label chunk not released!!", __func__);
606
607 return ret;
608 }
609
610
611 void label_manager_close(void)
612 {
613 list_delete(&lbl_mgr.lc_list);
614 stream_free(obuf);
615 }