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/label_manager.h"
39 #include "zebra/zebra_errors.h"
41 #define CONNECTION_DELAY 5
43 struct label_manager lbl_mgr
;
45 extern struct zebra_privs_t zserv_privs
;
47 DEFINE_MGROUP(LBL_MGR
, "Label Manager");
48 DEFINE_MTYPE_STATIC(LBL_MGR
, LM_CHUNK
, "Label Manager Chunk");
50 /* In case this zebra daemon is not acting as label manager,
51 * it will be a proxy to relay messages to external label manager
52 * This zclient thus is to connect to it
54 static struct stream
*obuf
;
55 static struct zclient
*zclient
;
58 static void delete_label_chunk(void *val
)
60 XFREE(MTYPE_LM_CHUNK
, val
);
63 static int relay_response_back(void)
66 struct stream
*src
, *dst
;
73 const char *proto_str
;
74 unsigned short instance
;
77 /* input buffer with msg from label manager */
83 ret
= zclient_read_header(src
, zclient
->sock
, &size
, &marker
, &version
,
85 if (ret
< 0 && errno
!= EAGAIN
) {
86 flog_err(EC_ZEBRA_LM_RESPONSE
,
87 "Error reading Label Manager response: %s",
92 /* do not relay a msg that has nothing to do with LM */
94 case ZEBRA_LABEL_MANAGER_CONNECT
:
95 case ZEBRA_LABEL_MANAGER_CONNECT_ASYNC
: /* should not be seen */
96 case ZEBRA_GET_LABEL_CHUNK
:
97 case ZEBRA_RELEASE_LABEL_CHUNK
:
100 zlog_debug("Not relaying '%s' response (size %d) from LM",
101 zserv_command_string(resp_cmd
), size
);
105 zlog_debug("Received '%s' response (size %d) from LM",
106 zserv_command_string(resp_cmd
), size
);
111 /* Get the 'proto' field of the message */
112 proto
= stream_getc(src
);
114 /* Get the 'instance' field of the message */
115 instance
= stream_getw(src
);
117 proto_str
= zebra_route_string(proto
);
119 /* lookup the client to relay the msg to */
120 zserv
= zserv_find_client(proto
, instance
);
123 EC_ZEBRA_LM_NO_SUCH_CLIENT
,
124 "Error relaying LM response: can't find client %s, instance %u",
125 proto_str
, instance
);
128 zlog_debug("Found client to relay LM response to client %s instance %u",
129 proto_str
, instance
);
131 /* copy msg into output buffer */
133 stream_copy(dst
, src
);
135 /* send response back */
136 ret
= writen(zserv
->sock
, dst
->data
, stream_get_endp(dst
));
138 flog_err(EC_ZEBRA_LM_RELAY_FAILED
,
139 "Error relaying LM response to %s instance %u: %s",
140 proto_str
, instance
, strerror(errno
));
143 zlog_debug("Relayed LM response (%d bytes) to %s instance %u", ret
,
144 proto_str
, instance
);
149 static int lm_zclient_read(struct thread
*t
)
153 zclient
->t_read
= NULL
;
155 /* read response and send it back */
156 ret
= relay_response_back();
158 /* on error, schedule another read */
160 if (!zclient
->t_read
)
161 thread_add_read(zclient
->master
, lm_zclient_read
, NULL
,
162 zclient
->sock
, &zclient
->t_read
);
166 static int reply_error(int cmd
, struct zserv
*zserv
, vrf_id_t vrf_id
)
171 s
= stream_new(ZEBRA_MAX_PACKET_SIZ
);
173 zclient_create_header(s
, cmd
, vrf_id
);
176 stream_putc(s
, zserv
->proto
);
178 stream_putw(s
, zserv
->instance
);
182 /* Write packet size. */
183 stream_putw_at(s
, 0, stream_get_endp(s
));
185 ret
= writen(zserv
->sock
, s
->data
, stream_get_endp(s
));
191 * Receive a request to get or release a label chunk and forward it to external
194 * It's called from zserv in case it's not an actual label manager, but just a
197 * @param cmd Type of request (connect, get or release)
199 * @return 0 on success, -1 otherwise
201 int zread_relay_label_manager_request(int cmd
, struct zserv
*zserv
,
202 struct stream
*msg
, vrf_id_t vrf_id
)
207 const char *proto_str
;
208 unsigned short instance
;
210 if (zclient
->sock
< 0) {
211 flog_err(EC_ZEBRA_LM_NO_SOCKET
,
212 "Unable to relay LM request: no socket");
213 reply_error(cmd
, zserv
, vrf_id
);
217 /* peek msg to get proto and instance id. This zebra, which acts as
218 * a proxy needs to have such values for each client in order to
219 * relay responses back to it.
222 /* Get the 'proto' field of incoming msg */
223 proto
= stream_getc(msg
);
225 /* Get the 'instance' field of incoming msg */
226 instance
= stream_getw(msg
);
228 /* stringify proto */
229 proto_str
= zebra_route_string(proto
);
231 /* check & set client proto if unset */
232 if (zserv
->proto
&& zserv
->proto
!= proto
) {
233 flog_warn(EC_ZEBRAING_LM_PROTO_MISMATCH
,
234 "Client proto(%u) != msg proto(%u)", zserv
->proto
,
239 /* check & set client instance if unset */
240 if (zserv
->instance
&& zserv
->instance
!= instance
) {
241 flog_err(EC_ZEBRA_LM_BAD_INSTANCE
,
242 "Client instance(%u) != msg instance(%u)",
243 zserv
->instance
, instance
);
247 /* recall proto and instance */
248 zserv
->instance
= instance
;
249 zserv
->proto
= proto
;
251 /* in case there's any incoming message enqueued, read and forward it */
252 if (zserv
->is_synchronous
)
254 ret
= relay_response_back();
256 /* get the msg buffer used toward the 'master' Label Manager */
259 /* copy the message */
260 stream_copy(dst
, msg
);
262 /* Send request to external label manager */
263 ret
= writen(zclient
->sock
, dst
->data
, stream_get_endp(dst
));
265 flog_err(EC_ZEBRA_LM_RELAY_FAILED
,
266 "Error relaying LM request from %s instance %u: %s",
267 proto_str
, instance
, strerror(errno
));
268 reply_error(cmd
, zserv
, vrf_id
);
271 zlog_debug("Relayed LM request (%d bytes) from %s instance %u", ret
,
272 proto_str
, instance
);
275 /* Release label chunk has no response */
276 if (cmd
== ZEBRA_RELEASE_LABEL_CHUNK
)
279 /* make sure we listen to the response */
280 if (!zclient
->t_read
)
281 thread_add_read(zclient
->master
, lm_zclient_read
, NULL
,
282 zclient
->sock
, &zclient
->t_read
);
287 static int lm_zclient_connect(struct thread
*t
)
289 zclient
->t_connect
= NULL
;
291 if (zclient
->sock
>= 0)
294 if (zclient_socket_connect(zclient
) < 0) {
295 flog_err(EC_ZEBRA_LM_CLIENT_CONNECTION_FAILED
,
296 "Error connecting synchronous zclient!");
297 thread_add_timer(zebrad
.master
, lm_zclient_connect
, zclient
,
298 CONNECTION_DELAY
, &zclient
->t_connect
);
302 /* make socket non-blocking */
303 (void)set_nonblocking(zclient
->sock
);
309 * Function to initialize zclient in case this is not an actual
310 * label manager, but just a proxy to an external one.
312 * @param lm_zserv_path Path to zserv socket of external label manager
314 static void lm_zclient_init(char *lm_zserv_path
)
317 frr_zclient_addr(&zclient_addr
, &zclient_addr_len
,
320 /* Set default values. */
321 zclient
= zclient_new(zebrad
.master
, &zclient_options_default
);
322 zclient
->privs
= &zserv_privs
;
324 zclient
->t_connect
= NULL
;
325 lm_zclient_connect(NULL
);
329 * Release label chunks from a client.
331 * Called on client disconnection or reconnection. It only releases chunks
332 * with empty keep value.
334 * @param proto Daemon protocol of client, to identify the owner
335 * @param instance Instance, to identify the owner
336 * @return Number of chunks released
338 int release_daemon_label_chunks(struct zserv
*client
)
340 uint8_t proto
= client
->proto
;
341 uint16_t instance
= client
->instance
;
342 struct listnode
*node
;
343 struct label_manager_chunk
*lmc
;
347 for (ALL_LIST_ELEMENTS_RO(lbl_mgr
.lc_list
, node
, lmc
)) {
348 if (lmc
->proto
== proto
&& lmc
->instance
== instance
350 ret
= release_label_chunk(lmc
->proto
, lmc
->instance
,
351 lmc
->start
, lmc
->end
);
357 zlog_debug("%s: Released %d label chunks", __func__
, count
);
363 * Init label manager (or proxy to an external one)
365 void label_manager_init(char *lm_zserv_path
)
367 /* this is an actual label manager */
368 if (!lm_zserv_path
) {
369 zlog_debug("Initializing internal label manager");
370 lm_is_external
= false;
371 lbl_mgr
.lc_list
= list_new();
372 lbl_mgr
.lc_list
->del
= delete_label_chunk
;
373 } else { /* it's acting just as a proxy */
374 zlog_debug("Initializing external label manager at %s",
376 lm_is_external
= true;
377 lm_zclient_init(lm_zserv_path
);
380 obuf
= stream_new(ZEBRA_MAX_PACKET_SIZ
);
382 hook_register(zserv_client_close
, release_daemon_label_chunks
);
386 * Core function, assigns label cunks
388 * It first searches through the list to check if there's one available
389 * (previously released). Otherwise it creates and assigns a new one
391 * @param proto Daemon protocol of client, to identify the owner
392 * @param instance Instance, to identify the owner
393 * @param keep If set, avoid garbage collection
394 * @para size Size of the label chunk
395 * @return Pointer to the assigned label chunk
397 struct label_manager_chunk
*assign_label_chunk(uint8_t proto
,
398 unsigned short instance
,
399 uint8_t keep
, uint32_t size
)
401 struct label_manager_chunk
*lmc
;
402 struct listnode
*node
;
404 /* first check if there's one available */
405 for (ALL_LIST_ELEMENTS_RO(lbl_mgr
.lc_list
, node
, lmc
)) {
406 if (lmc
->proto
== NO_PROTO
407 && lmc
->end
- lmc
->start
+ 1 == size
) {
409 lmc
->instance
= instance
;
414 /* otherwise create a new one */
415 lmc
= XCALLOC(MTYPE_LM_CHUNK
, sizeof(struct label_manager_chunk
));
417 if (list_isempty(lbl_mgr
.lc_list
))
418 lmc
->start
= MPLS_LABEL_UNRESERVED_MIN
;
420 lmc
->start
= ((struct label_manager_chunk
*)listgetdata(
421 listtail(lbl_mgr
.lc_list
)))
424 if (lmc
->start
> MPLS_LABEL_UNRESERVED_MAX
- size
+ 1) {
425 flog_err(EC_ZEBRA_LM_EXHAUSTED_LABELS
,
426 "Reached max labels. Start: %u, size: %u", lmc
->start
,
428 XFREE(MTYPE_LM_CHUNK
, lmc
);
431 lmc
->end
= lmc
->start
+ size
- 1;
433 lmc
->instance
= instance
;
435 listnode_add(lbl_mgr
.lc_list
, lmc
);
441 * Core function, release no longer used label cunks
443 * @param proto Daemon protocol of client, to identify the owner
444 * @param instance Instance, to identify the owner
445 * @param start First label of the chunk
446 * @param end Last label of the chunk
447 * @return 0 on success, -1 otherwise
449 int release_label_chunk(uint8_t proto
, unsigned short instance
, uint32_t start
,
452 struct listnode
*node
;
453 struct label_manager_chunk
*lmc
;
456 /* check that size matches */
457 zlog_debug("Releasing label chunk: %u - %u", start
, end
);
458 /* find chunk and disown */
459 for (ALL_LIST_ELEMENTS_RO(lbl_mgr
.lc_list
, node
, lmc
)) {
460 if (lmc
->start
!= start
)
464 if (lmc
->proto
!= proto
|| lmc
->instance
!= instance
) {
465 flog_err(EC_ZEBRA_LM_DAEMON_MISMATCH
,
466 "%s: Daemon mismatch!!", __func__
);
469 lmc
->proto
= NO_PROTO
;
476 flog_err(EC_ZEBRA_LM_UNRELEASED_CHUNK
,
477 "%s: Label chunk not released!!", __func__
);
483 void label_manager_close()
485 list_delete(&lbl_mgr
.lc_list
);