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
;
78 /* input buffer with msg from label manager */
84 ret
= zclient_read_header(src
, zclient
->sock
, &size
, &marker
, &version
,
86 if (ret
< 0 && errno
!= EAGAIN
) {
87 flog_err(EC_ZEBRA_LM_RESPONSE
,
88 "Error reading Label Manager response: %s",
93 /* do not relay a msg that has nothing to do with LM */
95 case ZEBRA_LABEL_MANAGER_CONNECT
:
96 case ZEBRA_LABEL_MANAGER_CONNECT_ASYNC
: /* should not be seen */
97 case ZEBRA_GET_LABEL_CHUNK
:
98 case ZEBRA_RELEASE_LABEL_CHUNK
:
101 zlog_debug("Not relaying '%s' response (size %d) from LM",
102 zserv_command_string(resp_cmd
), size
);
106 zlog_debug("Received '%s' response (size %d) from LM",
107 zserv_command_string(resp_cmd
), size
);
112 /* Get the 'proto' field of the message */
113 proto
= stream_getc(src
);
115 /* Get the 'instance' field of the message */
116 instance
= stream_getw(src
);
118 proto_str
= zebra_route_string(proto
);
120 /* lookup the client to relay the msg to */
121 zserv
= zserv_find_client(proto
, instance
);
124 EC_ZEBRA_LM_NO_SUCH_CLIENT
,
125 "Error relaying LM response: can't find client %s, instance %u",
126 proto_str
, instance
);
129 zlog_debug("Found client to relay LM response to client %s instance %u",
130 proto_str
, instance
);
132 /* copy msg into output buffer */
134 stream_copy(dst
, src
);
136 /* send response back */
137 ret
= writen(zserv
->sock
, dst
->data
, stream_get_endp(dst
));
139 flog_err(EC_ZEBRA_LM_RELAY_FAILED
,
140 "Error relaying LM response to %s instance %u: %s",
141 proto_str
, instance
, strerror(errno
));
144 zlog_debug("Relayed LM response (%d bytes) to %s instance %u", ret
,
145 proto_str
, instance
);
150 static int lm_zclient_read(struct thread
*t
)
154 zclient
->t_read
= NULL
;
156 /* read response and send it back */
157 ret
= relay_response_back();
160 thread_add_read(zclient
->master
, lm_zclient_read
, NULL
,
161 zclient
->sock
, &zclient
->t_read
);
165 static int reply_error(int cmd
, struct zserv
*zserv
, vrf_id_t vrf_id
)
170 s
= stream_new(ZEBRA_MAX_PACKET_SIZ
);
172 zclient_create_header(s
, cmd
, vrf_id
);
175 stream_putc(s
, zserv
->proto
);
177 stream_putw(s
, zserv
->instance
);
181 /* Write packet size. */
182 stream_putw_at(s
, 0, stream_get_endp(s
));
184 ret
= writen(zserv
->sock
, s
->data
, stream_get_endp(s
));
190 * Receive a request to get or release a label chunk and forward it to external
193 * It's called from zserv in case it's not an actual label manager, but just a
196 * @param cmd Type of request (connect, get or release)
198 * @return 0 on success, -1 otherwise
200 int zread_relay_label_manager_request(int cmd
, struct zserv
*zserv
,
201 struct stream
*msg
, vrf_id_t vrf_id
)
206 const char *proto_str
;
207 unsigned short instance
;
209 if (zclient
->sock
< 0) {
210 flog_err(EC_ZEBRA_LM_NO_SOCKET
,
211 "Unable to relay LM request: no socket");
212 reply_error(cmd
, zserv
, vrf_id
);
216 /* peek msg to get proto and instance id. This zebra, which acts as
217 * a proxy needs to have such values for each client in order to
218 * relay responses back to it.
221 /* Get the 'proto' field of incoming msg */
222 proto
= stream_getc(msg
);
224 /* Get the 'instance' field of incoming msg */
225 instance
= stream_getw(msg
);
227 /* stringify proto */
228 proto_str
= zebra_route_string(proto
);
230 /* check & set client proto if unset */
231 if (zserv
->proto
&& zserv
->proto
!= proto
) {
232 flog_warn(EC_ZEBRAING_LM_PROTO_MISMATCH
,
233 "Client proto(%u) != msg proto(%u)", zserv
->proto
,
238 /* check & set client instance if unset */
239 if (zserv
->instance
&& zserv
->instance
!= instance
) {
240 flog_err(EC_ZEBRA_LM_BAD_INSTANCE
,
241 "Client instance(%u) != msg instance(%u)",
242 zserv
->instance
, instance
);
246 /* recall proto and instance */
247 zserv
->instance
= instance
;
248 zserv
->proto
= proto
;
250 /* in case there's any incoming message enqueued, read and forward it */
251 if (zserv
->is_synchronous
)
253 ret
= relay_response_back();
255 /* get the msg buffer used toward the 'master' Label Manager */
258 /* copy the message */
259 stream_copy(dst
, msg
);
261 /* Send request to external label manager */
262 ret
= writen(zclient
->sock
, dst
->data
, stream_get_endp(dst
));
264 flog_err(EC_ZEBRA_LM_RELAY_FAILED
,
265 "Error relaying LM request from %s instance %u: %s",
266 proto_str
, instance
, strerror(errno
));
267 reply_error(cmd
, zserv
, vrf_id
);
270 zlog_debug("Relayed LM request (%d bytes) from %s instance %u", ret
,
271 proto_str
, instance
);
274 /* Release label chunk has no response */
275 if (cmd
== ZEBRA_RELEASE_LABEL_CHUNK
)
278 /* make sure we listen to the response */
279 if (!zclient
->t_read
)
280 thread_add_read(zclient
->master
, lm_zclient_read
, NULL
,
281 zclient
->sock
, &zclient
->t_read
);
286 static int lm_zclient_connect(struct thread
*t
)
288 zclient
->t_connect
= NULL
;
290 if (zclient
->sock
>= 0)
293 if (zclient_socket_connect(zclient
) < 0) {
294 flog_err(EC_ZEBRA_LM_CLIENT_CONNECTION_FAILED
,
295 "Error connecting synchronous zclient!");
296 thread_add_timer(zrouter
.master
, lm_zclient_connect
, zclient
,
297 CONNECTION_DELAY
, &zclient
->t_connect
);
301 /* make socket non-blocking */
302 (void)set_nonblocking(zclient
->sock
);
308 * Function to initialize zclient in case this is not an actual
309 * label manager, but just a proxy to an external one.
311 * @param lm_zserv_path Path to zserv socket of external label manager
313 static void lm_zclient_init(char *lm_zserv_path
)
316 frr_zclient_addr(&zclient_addr
, &zclient_addr_len
,
319 /* Set default values. */
320 zclient
= zclient_new(zrouter
.master
, &zclient_options_default
);
321 zclient
->privs
= &zserv_privs
;
323 zclient
->t_connect
= NULL
;
324 lm_zclient_connect(NULL
);
328 * Release label chunks from a client.
330 * Called on client disconnection or reconnection. It only releases chunks
331 * with empty keep value.
333 * @param proto Daemon protocol of client, to identify the owner
334 * @param instance Instance, to identify the owner
335 * @return Number of chunks released
337 int release_daemon_label_chunks(struct zserv
*client
)
339 uint8_t proto
= client
->proto
;
340 uint16_t instance
= client
->instance
;
341 struct listnode
*node
;
342 struct label_manager_chunk
*lmc
;
346 for (ALL_LIST_ELEMENTS_RO(lbl_mgr
.lc_list
, node
, lmc
)) {
347 if (lmc
->proto
== proto
&& lmc
->instance
== instance
349 ret
= release_label_chunk(lmc
->proto
, lmc
->instance
,
350 lmc
->start
, lmc
->end
);
356 zlog_debug("%s: Released %d label chunks", __func__
, count
);
362 * Init label manager (or proxy to an external one)
364 void label_manager_init(char *lm_zserv_path
)
366 /* this is an actual label manager */
367 if (!lm_zserv_path
) {
368 zlog_debug("Initializing internal label manager");
369 lm_is_external
= false;
370 lbl_mgr
.lc_list
= list_new();
371 lbl_mgr
.lc_list
->del
= delete_label_chunk
;
372 } else { /* it's acting just as a proxy */
373 zlog_debug("Initializing external label manager at %s",
375 lm_is_external
= true;
376 lm_zclient_init(lm_zserv_path
);
379 obuf
= stream_new(ZEBRA_MAX_PACKET_SIZ
);
381 hook_register(zserv_client_close
, release_daemon_label_chunks
);
385 * Core function, assigns label cunks
387 * It first searches through the list to check if there's one available
388 * (previously released). Otherwise it creates and assigns a new one
390 * @param proto Daemon protocol of client, to identify the owner
391 * @param instance Instance, to identify the owner
392 * @param keep If set, avoid garbage collection
393 * @para size Size of the label chunk
394 * @return Pointer to the assigned label chunk
396 struct label_manager_chunk
*assign_label_chunk(uint8_t proto
,
397 unsigned short instance
,
398 uint8_t keep
, uint32_t size
)
400 struct label_manager_chunk
*lmc
;
401 struct listnode
*node
;
403 /* first check if there's one available */
404 for (ALL_LIST_ELEMENTS_RO(lbl_mgr
.lc_list
, node
, lmc
)) {
405 if (lmc
->proto
== NO_PROTO
406 && lmc
->end
- lmc
->start
+ 1 == size
) {
408 lmc
->instance
= instance
;
413 /* otherwise create a new one */
414 lmc
= XCALLOC(MTYPE_LM_CHUNK
, sizeof(struct label_manager_chunk
));
416 if (list_isempty(lbl_mgr
.lc_list
))
417 lmc
->start
= MPLS_LABEL_UNRESERVED_MIN
;
419 lmc
->start
= ((struct label_manager_chunk
*)listgetdata(
420 listtail(lbl_mgr
.lc_list
)))
423 if (lmc
->start
> MPLS_LABEL_UNRESERVED_MAX
- size
+ 1) {
424 flog_err(EC_ZEBRA_LM_EXHAUSTED_LABELS
,
425 "Reached max labels. Start: %u, size: %u", lmc
->start
,
427 XFREE(MTYPE_LM_CHUNK
, lmc
);
430 lmc
->end
= lmc
->start
+ size
- 1;
432 lmc
->instance
= instance
;
434 listnode_add(lbl_mgr
.lc_list
, lmc
);
440 * Core function, release no longer used label cunks
442 * @param proto Daemon protocol of client, to identify the owner
443 * @param instance Instance, to identify the owner
444 * @param start First label of the chunk
445 * @param end Last label of the chunk
446 * @return 0 on success, -1 otherwise
448 int release_label_chunk(uint8_t proto
, unsigned short instance
, uint32_t start
,
451 struct listnode
*node
;
452 struct label_manager_chunk
*lmc
;
455 /* check that size matches */
456 zlog_debug("Releasing label chunk: %u - %u", start
, end
);
457 /* find chunk and disown */
458 for (ALL_LIST_ELEMENTS_RO(lbl_mgr
.lc_list
, node
, lmc
)) {
459 if (lmc
->start
!= start
)
463 if (lmc
->proto
!= proto
|| lmc
->instance
!= instance
) {
464 flog_err(EC_ZEBRA_LM_DAEMON_MISMATCH
,
465 "%s: Daemon mismatch!!", __func__
);
468 lmc
->proto
= NO_PROTO
;
475 flog_err(EC_ZEBRA_LM_UNRELEASED_CHUNK
,
476 "%s: Label chunk not released!!", __func__
);
482 void label_manager_close(void)
484 list_delete(&lbl_mgr
.lc_list
);