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();
159 /* on error, schedule another read */
161 if (!zclient
->t_read
)
162 thread_add_read(zclient
->master
, lm_zclient_read
, NULL
,
163 zclient
->sock
, &zclient
->t_read
);
167 static int reply_error(int cmd
, struct zserv
*zserv
, vrf_id_t vrf_id
)
172 s
= stream_new(ZEBRA_MAX_PACKET_SIZ
);
174 zclient_create_header(s
, cmd
, vrf_id
);
177 stream_putc(s
, zserv
->proto
);
179 stream_putw(s
, zserv
->instance
);
183 /* Write packet size. */
184 stream_putw_at(s
, 0, stream_get_endp(s
));
186 ret
= writen(zserv
->sock
, s
->data
, stream_get_endp(s
));
192 * Receive a request to get or release a label chunk and forward it to external
195 * It's called from zserv in case it's not an actual label manager, but just a
198 * @param cmd Type of request (connect, get or release)
200 * @return 0 on success, -1 otherwise
202 int zread_relay_label_manager_request(int cmd
, struct zserv
*zserv
,
203 struct stream
*msg
, vrf_id_t vrf_id
)
208 const char *proto_str
;
209 unsigned short instance
;
211 if (zclient
->sock
< 0) {
212 flog_err(EC_ZEBRA_LM_NO_SOCKET
,
213 "Unable to relay LM request: no socket");
214 reply_error(cmd
, zserv
, vrf_id
);
218 /* peek msg to get proto and instance id. This zebra, which acts as
219 * a proxy needs to have such values for each client in order to
220 * relay responses back to it.
223 /* Get the 'proto' field of incoming msg */
224 proto
= stream_getc(msg
);
226 /* Get the 'instance' field of incoming msg */
227 instance
= stream_getw(msg
);
229 /* stringify proto */
230 proto_str
= zebra_route_string(proto
);
232 /* check & set client proto if unset */
233 if (zserv
->proto
&& zserv
->proto
!= proto
) {
234 flog_warn(EC_ZEBRAING_LM_PROTO_MISMATCH
,
235 "Client proto(%u) != msg proto(%u)", zserv
->proto
,
240 /* check & set client instance if unset */
241 if (zserv
->instance
&& zserv
->instance
!= instance
) {
242 flog_err(EC_ZEBRA_LM_BAD_INSTANCE
,
243 "Client instance(%u) != msg instance(%u)",
244 zserv
->instance
, instance
);
248 /* recall proto and instance */
249 zserv
->instance
= instance
;
250 zserv
->proto
= proto
;
252 /* in case there's any incoming message enqueued, read and forward it */
253 if (zserv
->is_synchronous
)
255 ret
= relay_response_back();
257 /* get the msg buffer used toward the 'master' Label Manager */
260 /* copy the message */
261 stream_copy(dst
, msg
);
263 /* Send request to external label manager */
264 ret
= writen(zclient
->sock
, dst
->data
, stream_get_endp(dst
));
266 flog_err(EC_ZEBRA_LM_RELAY_FAILED
,
267 "Error relaying LM request from %s instance %u: %s",
268 proto_str
, instance
, strerror(errno
));
269 reply_error(cmd
, zserv
, vrf_id
);
272 zlog_debug("Relayed LM request (%d bytes) from %s instance %u", ret
,
273 proto_str
, instance
);
276 /* Release label chunk has no response */
277 if (cmd
== ZEBRA_RELEASE_LABEL_CHUNK
)
280 /* make sure we listen to the response */
281 if (!zclient
->t_read
)
282 thread_add_read(zclient
->master
, lm_zclient_read
, NULL
,
283 zclient
->sock
, &zclient
->t_read
);
288 static int lm_zclient_connect(struct thread
*t
)
290 zclient
->t_connect
= NULL
;
292 if (zclient
->sock
>= 0)
295 if (zclient_socket_connect(zclient
) < 0) {
296 flog_err(EC_ZEBRA_LM_CLIENT_CONNECTION_FAILED
,
297 "Error connecting synchronous zclient!");
298 thread_add_timer(zrouter
.master
, lm_zclient_connect
, zclient
,
299 CONNECTION_DELAY
, &zclient
->t_connect
);
303 /* make socket non-blocking */
304 (void)set_nonblocking(zclient
->sock
);
310 * Function to initialize zclient in case this is not an actual
311 * label manager, but just a proxy to an external one.
313 * @param lm_zserv_path Path to zserv socket of external label manager
315 static void lm_zclient_init(char *lm_zserv_path
)
318 frr_zclient_addr(&zclient_addr
, &zclient_addr_len
,
321 /* Set default values. */
322 zclient
= zclient_new(zrouter
.master
, &zclient_options_default
);
323 zclient
->privs
= &zserv_privs
;
325 zclient
->t_connect
= NULL
;
326 lm_zclient_connect(NULL
);
330 * Release label chunks from a client.
332 * Called on client disconnection or reconnection. It only releases chunks
333 * with empty keep value.
335 * @param proto Daemon protocol of client, to identify the owner
336 * @param instance Instance, to identify the owner
337 * @return Number of chunks released
339 int release_daemon_label_chunks(struct zserv
*client
)
341 uint8_t proto
= client
->proto
;
342 uint16_t instance
= client
->instance
;
343 struct listnode
*node
;
344 struct label_manager_chunk
*lmc
;
348 for (ALL_LIST_ELEMENTS_RO(lbl_mgr
.lc_list
, node
, lmc
)) {
349 if (lmc
->proto
== proto
&& lmc
->instance
== instance
351 ret
= release_label_chunk(lmc
->proto
, lmc
->instance
,
352 lmc
->start
, lmc
->end
);
358 zlog_debug("%s: Released %d label chunks", __func__
, count
);
364 * Init label manager (or proxy to an external one)
366 void label_manager_init(char *lm_zserv_path
)
368 /* this is an actual label manager */
369 if (!lm_zserv_path
) {
370 zlog_debug("Initializing internal label manager");
371 lm_is_external
= false;
372 lbl_mgr
.lc_list
= list_new();
373 lbl_mgr
.lc_list
->del
= delete_label_chunk
;
374 } else { /* it's acting just as a proxy */
375 zlog_debug("Initializing external label manager at %s",
377 lm_is_external
= true;
378 lm_zclient_init(lm_zserv_path
);
381 obuf
= stream_new(ZEBRA_MAX_PACKET_SIZ
);
383 hook_register(zserv_client_close
, release_daemon_label_chunks
);
387 * Core function, assigns label cunks
389 * It first searches through the list to check if there's one available
390 * (previously released). Otherwise it creates and assigns a new one
392 * @param proto Daemon protocol of client, to identify the owner
393 * @param instance Instance, to identify the owner
394 * @param keep If set, avoid garbage collection
395 * @para size Size of the label chunk
396 * @return Pointer to the assigned label chunk
398 struct label_manager_chunk
*assign_label_chunk(uint8_t proto
,
399 unsigned short instance
,
400 uint8_t keep
, uint32_t size
)
402 struct label_manager_chunk
*lmc
;
403 struct listnode
*node
;
405 /* first check if there's one available */
406 for (ALL_LIST_ELEMENTS_RO(lbl_mgr
.lc_list
, node
, lmc
)) {
407 if (lmc
->proto
== NO_PROTO
408 && lmc
->end
- lmc
->start
+ 1 == size
) {
410 lmc
->instance
= instance
;
415 /* otherwise create a new one */
416 lmc
= XCALLOC(MTYPE_LM_CHUNK
, sizeof(struct label_manager_chunk
));
418 if (list_isempty(lbl_mgr
.lc_list
))
419 lmc
->start
= MPLS_LABEL_UNRESERVED_MIN
;
421 lmc
->start
= ((struct label_manager_chunk
*)listgetdata(
422 listtail(lbl_mgr
.lc_list
)))
425 if (lmc
->start
> MPLS_LABEL_UNRESERVED_MAX
- size
+ 1) {
426 flog_err(EC_ZEBRA_LM_EXHAUSTED_LABELS
,
427 "Reached max labels. Start: %u, size: %u", lmc
->start
,
429 XFREE(MTYPE_LM_CHUNK
, lmc
);
432 lmc
->end
= lmc
->start
+ size
- 1;
434 lmc
->instance
= instance
;
436 listnode_add(lbl_mgr
.lc_list
, lmc
);
442 * Core function, release no longer used label cunks
444 * @param proto Daemon protocol of client, to identify the owner
445 * @param instance Instance, to identify the owner
446 * @param start First label of the chunk
447 * @param end Last label of the chunk
448 * @return 0 on success, -1 otherwise
450 int release_label_chunk(uint8_t proto
, unsigned short instance
, uint32_t start
,
453 struct listnode
*node
;
454 struct label_manager_chunk
*lmc
;
457 /* check that size matches */
458 zlog_debug("Releasing label chunk: %u - %u", start
, end
);
459 /* find chunk and disown */
460 for (ALL_LIST_ELEMENTS_RO(lbl_mgr
.lc_list
, node
, lmc
)) {
461 if (lmc
->start
!= start
)
465 if (lmc
->proto
!= proto
|| lmc
->instance
!= instance
) {
466 flog_err(EC_ZEBRA_LM_DAEMON_MISMATCH
,
467 "%s: Daemon mismatch!!", __func__
);
470 lmc
->proto
= NO_PROTO
;
477 flog_err(EC_ZEBRA_LM_UNRELEASED_CHUNK
,
478 "%s: Label chunk not released!!", __func__
);
484 void label_manager_close(void)
486 list_delete(&lbl_mgr
.lc_list
);