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(ZEBRA_ERR_LM_RESPONSE
,
87 "Error reading Label Manager response: %s",
91 zlog_debug("Label Manager response received, %d bytes", size
);
95 /* Get the 'proto' field of the message */
96 proto
= stream_getc(src
);
98 /* Get the 'instance' field of the message */
99 instance
= stream_getw(src
);
101 proto_str
= zebra_route_string(proto
);
103 /* lookup the client to relay the msg to */
104 zserv
= zserv_find_client(proto
, instance
);
107 ZEBRA_ERR_LM_NO_SUCH_CLIENT
,
108 "Error relaying LM response: can't find client %s, instance %u",
109 proto_str
, instance
);
112 zlog_debug("Found client to relay LM response to client %s instance %u",
113 proto_str
, instance
);
115 /* copy msg into output buffer */
117 stream_copy(dst
, src
);
119 /* send response back */
120 ret
= writen(zserv
->sock
, dst
->data
, stream_get_endp(dst
));
122 flog_err(ZEBRA_ERR_LM_RELAY_FAILED
,
123 "Error relaying LM response to %s instance %u: %s",
124 proto_str
, instance
, strerror(errno
));
127 zlog_debug("Relayed LM response (%d bytes) to %s instance %u", ret
,
128 proto_str
, instance
);
133 static int lm_zclient_read(struct thread
*t
)
137 zclient
->t_read
= NULL
;
139 /* read response and send it back */
140 ret
= relay_response_back();
145 static int reply_error(int cmd
, struct zserv
*zserv
, vrf_id_t vrf_id
)
150 s
= stream_new(ZEBRA_MAX_PACKET_SIZ
);
152 zclient_create_header(s
, cmd
, vrf_id
);
155 stream_putc(s
, zserv
->proto
);
157 stream_putw(s
, zserv
->instance
);
161 /* Write packet size. */
162 stream_putw_at(s
, 0, stream_get_endp(s
));
164 ret
= writen(zserv
->sock
, s
->data
, stream_get_endp(s
));
170 * Receive a request to get or release a label chunk and forward it to external
173 * It's called from zserv in case it's not an actual label manager, but just a
176 * @param cmd Type of request (connect, get or release)
178 * @return 0 on success, -1 otherwise
180 int zread_relay_label_manager_request(int cmd
, struct zserv
*zserv
,
181 struct stream
*msg
, vrf_id_t vrf_id
)
186 const char *proto_str
;
187 unsigned short instance
;
189 if (zclient
->sock
< 0) {
190 flog_err(ZEBRA_ERR_LM_NO_SOCKET
,
191 "Unable to relay LM request: no socket");
192 reply_error(cmd
, zserv
, vrf_id
);
196 /* peek msg to get proto and instance id. This zebra, which acts as
197 * a proxy needs to have such values for each client in order to
198 * relay responses back to it.
201 /* Get the 'proto' field of incoming msg */
202 proto
= stream_getc(msg
);
204 /* Get the 'instance' field of incoming msg */
205 instance
= stream_getw(msg
);
207 /* stringify proto */
208 proto_str
= zebra_route_string(proto
);
210 /* check & set client proto if unset */
211 if (zserv
->proto
&& zserv
->proto
!= proto
) {
212 zlog_warn("Client proto(%u) != msg proto(%u)", zserv
->proto
,
217 /* check & set client instance if unset */
218 if (zserv
->instance
&& zserv
->instance
!= instance
) {
219 flog_err(ZEBRA_ERR_LM_BAD_INSTANCE
,
220 "Client instance(%u) != msg instance(%u)",
221 zserv
->instance
, instance
);
225 /* recall proto and instance */
226 zserv
->instance
= instance
;
227 zserv
->proto
= proto
;
229 /* in case there's any incoming message enqueued, read and forward it */
231 ret
= relay_response_back();
233 /* get the msg buffer used toward the 'master' Label Manager */
236 /* copy the message */
237 stream_copy(dst
, msg
);
239 /* Send request to external label manager */
240 ret
= writen(zclient
->sock
, dst
->data
, stream_get_endp(dst
));
242 flog_err(ZEBRA_ERR_LM_RELAY_FAILED
,
243 "Error relaying LM request from %s instance %u: %s",
244 proto_str
, instance
, strerror(errno
));
245 reply_error(cmd
, zserv
, vrf_id
);
248 zlog_debug("Relayed LM request (%d bytes) from %s instance %u", ret
,
249 proto_str
, instance
);
252 /* Release label chunk has no response */
253 if (cmd
== ZEBRA_RELEASE_LABEL_CHUNK
)
256 /* make sure we listen to the response */
257 if (!zclient
->t_read
)
258 thread_add_read(zclient
->master
, lm_zclient_read
, NULL
,
259 zclient
->sock
, &zclient
->t_read
);
264 static int lm_zclient_connect(struct thread
*t
)
266 zclient
->t_connect
= NULL
;
268 if (zclient
->sock
>= 0)
271 if (zclient_socket_connect(zclient
) < 0) {
272 flog_err(ZEBRA_ERR_LM_CLIENT_CONNECTION_FAILED
,
273 "Error connecting synchronous zclient!");
274 thread_add_timer(zebrad
.master
, lm_zclient_connect
, zclient
,
275 CONNECTION_DELAY
, &zclient
->t_connect
);
279 /* make socket non-blocking */
280 if (set_nonblocking(zclient
->sock
) < 0)
281 zlog_warn("%s: set_nonblocking(%d) failed", __func__
,
288 * Function to initialize zclient in case this is not an actual
289 * label manager, but just a proxy to an external one.
291 * @param lm_zserv_path Path to zserv socket of external label manager
293 static void lm_zclient_init(char *lm_zserv_path
)
296 frr_zclient_addr(&zclient_addr
, &zclient_addr_len
,
299 /* Set default values. */
300 zclient
= zclient_new_notify(zebrad
.master
, &zclient_options_default
);
301 zclient
->privs
= &zserv_privs
;
303 zclient
->t_connect
= NULL
;
304 lm_zclient_connect(NULL
);
308 * Release label chunks from a client.
310 * Called on client disconnection or reconnection. It only releases chunks
311 * with empty keep value.
313 * @param proto Daemon protocol of client, to identify the owner
314 * @param instance Instance, to identify the owner
315 * @return Number of chunks released
317 int release_daemon_label_chunks(struct zserv
*client
)
319 uint8_t proto
= client
->proto
;
320 uint16_t instance
= client
->instance
;
321 struct listnode
*node
;
322 struct label_manager_chunk
*lmc
;
326 for (ALL_LIST_ELEMENTS_RO(lbl_mgr
.lc_list
, node
, lmc
)) {
327 if (lmc
->proto
== proto
&& lmc
->instance
== instance
329 ret
= release_label_chunk(lmc
->proto
, lmc
->instance
,
330 lmc
->start
, lmc
->end
);
336 zlog_debug("%s: Released %d label chunks", __func__
, count
);
342 * Init label manager (or proxy to an external one)
344 void label_manager_init(char *lm_zserv_path
)
346 /* this is an actual label manager */
347 if (!lm_zserv_path
) {
348 zlog_debug("Initializing internal label manager");
349 lm_is_external
= false;
350 lbl_mgr
.lc_list
= list_new();
351 lbl_mgr
.lc_list
->del
= delete_label_chunk
;
352 } else { /* it's acting just as a proxy */
353 zlog_debug("Initializing external label manager at %s",
355 lm_is_external
= true;
356 lm_zclient_init(lm_zserv_path
);
359 obuf
= stream_new(ZEBRA_MAX_PACKET_SIZ
);
361 hook_register(zserv_client_close
, release_daemon_label_chunks
);
365 * Core function, assigns label cunks
367 * It first searches through the list to check if there's one available
368 * (previously released). Otherwise it creates and assigns a new one
370 * @param proto Daemon protocol of client, to identify the owner
371 * @param instance Instance, to identify the owner
372 * @param keep If set, avoid garbage collection
373 * @para size Size of the label chunk
374 * @return Pointer to the assigned label chunk
376 struct label_manager_chunk
*assign_label_chunk(uint8_t proto
,
377 unsigned short instance
,
378 uint8_t keep
, uint32_t size
)
380 struct label_manager_chunk
*lmc
;
381 struct listnode
*node
;
383 /* first check if there's one available */
384 for (ALL_LIST_ELEMENTS_RO(lbl_mgr
.lc_list
, node
, lmc
)) {
385 if (lmc
->proto
== NO_PROTO
386 && lmc
->end
- lmc
->start
+ 1 == size
) {
388 lmc
->instance
= instance
;
393 /* otherwise create a new one */
394 lmc
= XCALLOC(MTYPE_LM_CHUNK
, sizeof(struct label_manager_chunk
));
396 if (list_isempty(lbl_mgr
.lc_list
))
397 lmc
->start
= MPLS_LABEL_UNRESERVED_MIN
;
399 lmc
->start
= ((struct label_manager_chunk
*)listgetdata(
400 listtail(lbl_mgr
.lc_list
)))
403 if (lmc
->start
> MPLS_LABEL_UNRESERVED_MAX
- size
+ 1) {
404 flog_err(ZEBRA_ERR_LM_EXHAUSTED_LABELS
,
405 "Reached max labels. Start: %u, size: %u", lmc
->start
,
407 XFREE(MTYPE_LM_CHUNK
, lmc
);
410 lmc
->end
= lmc
->start
+ size
- 1;
412 lmc
->instance
= instance
;
414 listnode_add(lbl_mgr
.lc_list
, lmc
);
420 * Core function, release no longer used label cunks
422 * @param proto Daemon protocol of client, to identify the owner
423 * @param instance Instance, to identify the owner
424 * @param start First label of the chunk
425 * @param end Last label of the chunk
426 * @return 0 on success, -1 otherwise
428 int release_label_chunk(uint8_t proto
, unsigned short instance
, uint32_t start
,
431 struct listnode
*node
;
432 struct label_manager_chunk
*lmc
;
435 /* check that size matches */
436 zlog_debug("Releasing label chunk: %u - %u", start
, end
);
437 /* find chunk and disown */
438 for (ALL_LIST_ELEMENTS_RO(lbl_mgr
.lc_list
, node
, lmc
)) {
439 if (lmc
->start
!= start
)
443 if (lmc
->proto
!= proto
|| lmc
->instance
!= instance
) {
444 flog_err(ZEBRA_ERR_LM_DAEMON_MISMATCH
,
445 "%s: Daemon mismatch!!", __func__
);
448 lmc
->proto
= NO_PROTO
;
455 flog_err(ZEBRA_ERR_LM_UNRELEASED_CHUNK
,
456 "%s: Label chunk not released!!", __func__
);
462 void label_manager_close()
464 list_delete_and_null(&lbl_mgr
.lc_list
);