2 * Label Manager for FRR
4 * Copyright (C) 2017 by Bingen Eguzkitza,
7 * This file is part of FreeRangeRouting (FRR)
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
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.
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
27 #include <sys/types.h>
30 #include "lib/memory.h"
32 #include "lib/network.h"
33 #include "lib/stream.h"
34 #include "lib/zclient.h"
35 #include "lib/libfrr.h"
37 //#include "zebra/zserv.h"
38 #include "zebra/zebra_router.h"
39 #include "zebra/label_manager.h"
40 #include "zebra/zebra_errors.h"
42 #define CONNECTION_DELAY 5
44 struct label_manager lbl_mgr
;
46 extern struct zebra_privs_t zserv_privs
;
48 DEFINE_MGROUP(LBL_MGR
, "Label Manager");
49 DEFINE_MTYPE_STATIC(LBL_MGR
, LM_CHUNK
, "Label Manager Chunk");
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
55 static struct stream
*obuf
;
56 static struct zclient
*zclient
;
59 static void delete_label_chunk(void *val
)
61 XFREE(MTYPE_LM_CHUNK
, val
);
64 static int relay_response_back(void)
67 struct stream
*src
, *dst
;
74 const char *proto_str
;
75 unsigned short instance
;
79 if (!zclient
|| zclient
->sock
< 0)
82 /* input buffer with msg from label manager */
88 ret
= zclient_read_header(src
, zclient
->sock
, &size
, &marker
, &version
,
92 flog_err(EC_ZEBRA_LM_RESPONSE
,
93 "Error reading Label Manager response: %s",
98 /* do not relay a msg that has nothing to do with LM */
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
:
106 zlog_debug("Not relaying '%s' response (size %d) from LM",
107 zserv_command_string(resp_cmd
), size
);
111 zlog_debug("Received '%s' response (size %d) from LM",
112 zserv_command_string(resp_cmd
), size
);
117 /* Get the 'proto' field of the message */
118 proto
= stream_getc(src
);
120 /* Get the 'instance' field of the message */
121 instance
= stream_getw(src
);
123 proto_str
= zebra_route_string(proto
);
125 /* lookup the client to relay the msg to */
126 zserv
= zserv_find_client(proto
, instance
);
129 EC_ZEBRA_LM_NO_SUCH_CLIENT
,
130 "Error relaying LM response: can't find client %s, instance %u",
131 proto_str
, instance
);
134 zlog_debug("Found client to relay LM response to client %s instance %u",
135 proto_str
, instance
);
137 /* copy msg into output buffer */
139 stream_copy(dst
, src
);
141 /* send response back */
142 ret
= writen(zserv
->sock
, dst
->data
, stream_get_endp(dst
));
144 flog_err(EC_ZEBRA_LM_RELAY_FAILED
,
145 "Error relaying LM response to %s instance %u: %s",
146 proto_str
, instance
, strerror(errno
));
149 zlog_debug("Relayed LM response (%d bytes) to %s instance %u", ret
,
150 proto_str
, instance
);
155 static int lm_zclient_read(struct thread
*t
)
159 zclient
->t_read
= NULL
;
161 /* read response and send it back */
162 ret
= relay_response_back();
165 thread_add_read(zclient
->master
, lm_zclient_read
, NULL
,
166 zclient
->sock
, &zclient
->t_read
);
170 static int reply_error(int cmd
, struct zserv
*zserv
, vrf_id_t vrf_id
)
175 s
= stream_new(ZEBRA_MAX_PACKET_SIZ
);
177 zclient_create_header(s
, cmd
, vrf_id
);
180 stream_putc(s
, zserv
->proto
);
182 stream_putw(s
, zserv
->instance
);
186 /* Write packet size. */
187 stream_putw_at(s
, 0, stream_get_endp(s
));
189 ret
= writen(zserv
->sock
, s
->data
, stream_get_endp(s
));
195 * Receive a request to get or release a label chunk and forward it to external
198 * It's called from zserv in case it's not an actual label manager, but just a
201 * @param cmd Type of request (connect, get or release)
203 * @return 0 on success, -1 otherwise
205 int zread_relay_label_manager_request(int cmd
, struct zserv
*zserv
,
206 struct stream
*msg
, vrf_id_t vrf_id
)
211 const char *proto_str
;
212 unsigned short instance
;
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
);
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.
226 /* Get the 'proto' field of incoming msg */
227 proto
= stream_getc(msg
);
229 /* Get the 'instance' field of incoming msg */
230 instance
= stream_getw(msg
);
232 /* stringify proto */
233 proto_str
= zebra_route_string(proto
);
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
,
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
);
251 /* recall proto and instance */
252 zserv
->instance
= instance
;
253 zserv
->proto
= proto
;
255 /* in case there's any incoming message enqueued, read and forward it */
256 if (zserv
->is_synchronous
)
258 ret
= relay_response_back();
260 /* get the msg buffer used toward the 'master' Label Manager */
263 /* copy the message */
264 stream_copy(dst
, msg
);
266 /* Send request to external label manager */
267 ret
= writen(zclient
->sock
, dst
->data
, stream_get_endp(dst
));
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
);
275 zlog_debug("Relayed LM request (%d bytes) from %s instance %u", ret
,
276 proto_str
, instance
);
279 /* Release label chunk has no response */
280 if (cmd
== ZEBRA_RELEASE_LABEL_CHUNK
)
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
);
291 static int lm_zclient_connect(struct thread
*t
)
293 zclient
->t_connect
= NULL
;
295 if (zclient
->sock
>= 0)
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
);
306 /* make socket non-blocking */
307 (void)set_nonblocking(zclient
->sock
);
313 * Function to initialize zclient in case this is not an actual
314 * label manager, but just a proxy to an external one.
316 * @param lm_zserv_path Path to zserv socket of external label manager
318 static void lm_zclient_init(char *lm_zserv_path
)
321 frr_zclient_addr(&zclient_addr
, &zclient_addr_len
,
324 /* Set default values. */
325 zclient
= zclient_new(zrouter
.master
, &zclient_options_default
);
326 zclient
->privs
= &zserv_privs
;
328 zclient
->t_connect
= NULL
;
329 lm_zclient_connect(NULL
);
333 * Release label chunks from a client.
335 * Called on client disconnection or reconnection. It only releases chunks
336 * with empty keep value.
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
342 int release_daemon_label_chunks(struct zserv
*client
)
344 uint8_t proto
= client
->proto
;
345 uint16_t instance
= client
->instance
;
346 struct listnode
*node
;
347 struct label_manager_chunk
*lmc
;
351 for (ALL_LIST_ELEMENTS_RO(lbl_mgr
.lc_list
, node
, lmc
)) {
352 if (lmc
->proto
== proto
&& lmc
->instance
== instance
354 ret
= release_label_chunk(lmc
->proto
, lmc
->instance
,
355 lmc
->start
, lmc
->end
);
361 zlog_debug("%s: Released %d label chunks", __func__
, count
);
367 * Init label manager (or proxy to an external one)
369 void label_manager_init(char *lm_zserv_path
)
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",
380 lm_is_external
= true;
381 lm_zclient_init(lm_zserv_path
);
384 obuf
= stream_new(ZEBRA_MAX_PACKET_SIZ
);
386 hook_register(zserv_client_close
, release_daemon_label_chunks
);
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
)
394 /* alloc chunk, fill it and return it */
395 struct label_manager_chunk
*lmc
=
396 XCALLOC(MTYPE_LM_CHUNK
, sizeof(struct label_manager_chunk
));
401 lmc
->instance
= instance
;
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
)
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
;
418 /* precompute last label from base and size */
419 uint32_t end
= base
+ size
- 1;
422 if ((base
< MPLS_LABEL_UNRESERVED_MIN
)
423 || (end
> MPLS_LABEL_UNRESERVED_MAX
)) {
424 zlog_err("Invalid LM request arguments: base: %u, size: %u",
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
)) {
433 /* skip chunks for labels < base */
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
) {
447 /* if chunk is used, cannot honor request */
448 if (lmc
->proto
!= NO_PROTO
)
451 if (end
< lmc
->end
) {
457 /* insert chunk between existing chunks */
459 lmc
= create_label_chunk(proto
, instance
, keep
, base
, end
);
460 listnode_add_before(lbl_mgr
.lc_list
, insert_node
, lmc
);
465 /* get node past the last one, if there */
467 last_node
= listnextnode(last_node
);
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
);
473 next
= listnextnode(node
);
474 list_delete_node(lbl_mgr
.lc_list
, node
);
477 lmc
= create_label_chunk(proto
, instance
, keep
, base
, end
);
479 listnode_add_before(lbl_mgr
.lc_list
, last_node
, lmc
);
481 listnode_add(lbl_mgr
.lc_list
, lmc
);
485 /* create a new chunk past all the existing ones and link at
487 lmc
= create_label_chunk(proto
, instance
, keep
, base
, end
);
488 listnode_add(lbl_mgr
.lc_list
, lmc
);
494 * Core function, assigns label chunks
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
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
506 struct label_manager_chunk
*assign_label_chunk(uint8_t proto
,
507 unsigned short instance
,
508 uint8_t keep
, uint32_t size
,
511 struct label_manager_chunk
*lmc
;
512 struct listnode
*node
;
513 uint32_t prev_end
= 0;
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
,
520 /* appease scan-build, who gets confused by the use of macros */
521 assert(lbl_mgr
.lc_list
);
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
) {
528 lmc
->instance
= instance
;
532 /* check if we hadve a "hole" behind us that we can squeeze into
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
);
543 /* otherwise create a new one */
546 if (list_isempty(lbl_mgr
.lc_list
))
547 start_free
= MPLS_LABEL_UNRESERVED_MIN
;
549 start_free
= ((struct label_manager_chunk
*)listgetdata(
550 listtail(lbl_mgr
.lc_list
)))
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
,
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
);
569 * Core function, release no longer used label chunks
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
577 int release_label_chunk(uint8_t proto
, unsigned short instance
, uint32_t start
,
580 struct listnode
*node
;
581 struct label_manager_chunk
*lmc
;
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
)
592 if (lmc
->proto
!= proto
|| lmc
->instance
!= instance
) {
593 flog_err(EC_ZEBRA_LM_DAEMON_MISMATCH
,
594 "%s: Daemon mismatch!!", __func__
);
597 lmc
->proto
= NO_PROTO
;
604 flog_err(EC_ZEBRA_LM_UNRELEASED_CHUNK
,
605 "%s: Label chunk not released!!", __func__
);
611 void label_manager_close(void)
613 list_delete(&lbl_mgr
.lc_list
);