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
);
390 * Core function, assigns label cunks
392 * It first searches through the list to check if there's one available
393 * (previously released). Otherwise it creates and assigns a new one
395 * @param proto Daemon protocol of client, to identify the owner
396 * @param instance Instance, to identify the owner
397 * @param keep If set, avoid garbage collection
398 * @para size Size of the label chunk
399 * @return Pointer to the assigned label chunk
401 struct label_manager_chunk
*assign_label_chunk(uint8_t proto
,
402 unsigned short instance
,
403 uint8_t keep
, uint32_t size
)
405 struct label_manager_chunk
*lmc
;
406 struct listnode
*node
;
408 /* first check if there's one available */
409 for (ALL_LIST_ELEMENTS_RO(lbl_mgr
.lc_list
, node
, lmc
)) {
410 if (lmc
->proto
== NO_PROTO
411 && lmc
->end
- lmc
->start
+ 1 == size
) {
413 lmc
->instance
= instance
;
418 /* otherwise create a new one */
419 lmc
= XCALLOC(MTYPE_LM_CHUNK
, sizeof(struct label_manager_chunk
));
421 if (list_isempty(lbl_mgr
.lc_list
))
422 lmc
->start
= MPLS_LABEL_UNRESERVED_MIN
;
424 lmc
->start
= ((struct label_manager_chunk
*)listgetdata(
425 listtail(lbl_mgr
.lc_list
)))
428 if (lmc
->start
> MPLS_LABEL_UNRESERVED_MAX
- size
+ 1) {
429 flog_err(EC_ZEBRA_LM_EXHAUSTED_LABELS
,
430 "Reached max labels. Start: %u, size: %u", lmc
->start
,
432 XFREE(MTYPE_LM_CHUNK
, lmc
);
435 lmc
->end
= lmc
->start
+ size
- 1;
437 lmc
->instance
= instance
;
439 listnode_add(lbl_mgr
.lc_list
, lmc
);
445 * Core function, release no longer used label cunks
447 * @param proto Daemon protocol of client, to identify the owner
448 * @param instance Instance, to identify the owner
449 * @param start First label of the chunk
450 * @param end Last label of the chunk
451 * @return 0 on success, -1 otherwise
453 int release_label_chunk(uint8_t proto
, unsigned short instance
, uint32_t start
,
456 struct listnode
*node
;
457 struct label_manager_chunk
*lmc
;
460 /* check that size matches */
461 zlog_debug("Releasing label chunk: %u - %u", start
, end
);
462 /* find chunk and disown */
463 for (ALL_LIST_ELEMENTS_RO(lbl_mgr
.lc_list
, node
, lmc
)) {
464 if (lmc
->start
!= start
)
468 if (lmc
->proto
!= proto
|| lmc
->instance
!= instance
) {
469 flog_err(EC_ZEBRA_LM_DAEMON_MISMATCH
,
470 "%s: Daemon mismatch!!", __func__
);
473 lmc
->proto
= NO_PROTO
;
480 flog_err(EC_ZEBRA_LM_UNRELEASED_CHUNK
,
481 "%s: Label chunk not released!!", __func__
);
487 void label_manager_close(void)
489 list_delete(&lbl_mgr
.lc_list
);