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
26 #include <sys/types.h>
31 #include "lib/memory.h"
33 #include "lib/network.h"
34 #include "lib/stream.h"
35 #include "lib/zclient.h"
36 #include "lib/libfrr.h"
38 #include "label_manager.h"
40 #define CONNECTION_DELAY 5
42 struct label_manager lbl_mgr
;
44 extern struct zebra_privs_t zserv_privs
;
46 DEFINE_MGROUP(LBL_MGR
, "Label Manager");
47 DEFINE_MTYPE_STATIC(LBL_MGR
, LM_CHUNK
, "Label Manager Chunk");
49 /* In case this zebra daemon is not acting as label manager,
50 * it will be a proxy to relay messages to external label manager
51 * This zclient thus is to connect to it
53 static struct stream
*ibuf
;
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(struct zserv
*zserv
)
66 struct stream
*src
, *dst
;
78 ret
= zclient_read_header(src
, zclient
->sock
, &size
, &marker
, &version
,
80 if (ret
< 0 && errno
!= EAGAIN
) {
81 zlog_err("%s: Error reading Label Manager response: %s",
82 __func__
, strerror(errno
));
85 zlog_debug("%s: Label Manager response received, %d bytes", __func__
,
90 /* send response back */
91 stream_copy(dst
, src
);
92 ret
= writen(zserv
->sock
, src
->data
, stream_get_endp(src
));
94 zlog_err("%s: Error sending Label Manager response back: %s",
95 __func__
, strerror(errno
));
98 zlog_debug("%s: Label Manager response (%d bytes) sent back", __func__
,
104 static int lm_zclient_read(struct thread
*t
)
109 /* Get socket to zebra. */
110 zserv
= THREAD_ARG(t
);
111 zclient
->t_read
= NULL
;
113 /* read response and send it back */
114 ret
= relay_response_back(zserv
);
119 static int reply_error(int cmd
, struct zserv
*zserv
, vrf_id_t vrf_id
)
124 s
= stream_new(ZEBRA_MAX_PACKET_SIZ
);
126 zclient_create_header(s
, cmd
, vrf_id
);
131 /* Write packet size. */
132 stream_putw_at(s
, 0, stream_get_endp(s
));
134 ret
= writen(zserv
->sock
, s
->data
, stream_get_endp(s
));
140 * Receive a request to get or release a label chunk and forward it to external
143 * It's called from zserv in case it's not an actual label manager, but just a
146 * @param cmd Type of request (connect, get or release)
148 * @return 0 on success, -1 otherwise
150 int zread_relay_label_manager_request(int cmd
, struct zserv
*zserv
,
153 struct stream
*src
, *dst
;
156 if (zclient
->sock
< 0) {
158 "%s: Error relaying label chunk request: no zclient socket",
160 reply_error(cmd
, zserv
, vrf_id
);
164 /* in case there's any incoming message enqueued, read and forward it */
166 ret
= relay_response_back(zserv
);
168 /* Send request to external label manager */
172 stream_copy(dst
, src
);
174 ret
= writen(zclient
->sock
, dst
->data
, stream_get_endp(dst
));
176 zlog_err("%s: Error relaying label chunk request: %s", __func__
,
178 reply_error(cmd
, zserv
, vrf_id
);
181 zlog_debug("%s: Label chunk request relayed. %d bytes sent", __func__
,
184 /* Release label chunk has no response */
185 if (cmd
== ZEBRA_RELEASE_LABEL_CHUNK
)
188 /* make sure we listen to the response */
189 if (!zclient
->t_read
)
190 thread_add_read(zclient
->master
, lm_zclient_read
, zserv
,
191 zclient
->sock
, &zclient
->t_read
);
196 static int lm_zclient_connect(struct thread
*t
)
198 zclient
->t_connect
= NULL
;
200 if (zclient
->sock
>= 0)
203 if (zclient_socket_connect(zclient
) < 0) {
204 zlog_err("Error connecting synchronous zclient!");
205 thread_add_timer(zebrad
.master
, lm_zclient_connect
, zclient
,
206 CONNECTION_DELAY
, &zclient
->t_connect
);
210 /* make socket non-blocking */
211 if (set_nonblocking(zclient
->sock
) < 0)
212 zlog_warn("%s: set_nonblocking(%d) failed", __func__
,
219 * Function to initialize zclient in case this is not an actual
220 * label manager, but just a proxy to an external one.
222 * @param lm_zserv_path Path to zserv socket of external label manager
224 static void lm_zclient_init(char *lm_zserv_path
)
227 frr_zclient_addr(&zclient_addr
, &zclient_addr_len
,
230 /* Set default values. */
231 zclient
= zclient_new_notify(zebrad
.master
, &zclient_options_default
);
232 zclient
->privs
= &zserv_privs
;
234 zclient
->t_connect
= NULL
;
235 lm_zclient_connect(NULL
);
239 * Release label chunks from a client.
241 * Called on client disconnection or reconnection. It only releases chunks
242 * with empty keep value.
244 * @param proto Daemon protocol of client, to identify the owner
245 * @param instance Instance, to identify the owner
246 * @return Number of chunks released
248 int release_daemon_label_chunks(struct zserv
*client
)
250 uint8_t proto
= client
->proto
;
251 uint16_t instance
= client
->instance
;
252 struct listnode
*node
;
253 struct label_manager_chunk
*lmc
;
257 for (ALL_LIST_ELEMENTS_RO(lbl_mgr
.lc_list
, node
, lmc
)) {
258 if (lmc
->proto
== proto
&& lmc
->instance
== instance
260 ret
= release_label_chunk(lmc
->proto
, lmc
->instance
,
261 lmc
->start
, lmc
->end
);
267 zlog_debug("%s: Released %d label chunks", __func__
, count
);
273 * Init label manager (or proxy to an external one)
275 void label_manager_init(char *lm_zserv_path
)
277 /* this is an actual label manager */
278 if (!lm_zserv_path
) {
279 zlog_debug("Initializing own label manager");
280 lm_is_external
= false;
281 lbl_mgr
.lc_list
= list_new();
282 lbl_mgr
.lc_list
->del
= delete_label_chunk
;
283 } else { /* it's acting just as a proxy */
284 zlog_debug("Initializing external label manager at %s",
286 lm_is_external
= true;
287 lm_zclient_init(lm_zserv_path
);
290 ibuf
= stream_new(ZEBRA_MAX_PACKET_SIZ
);
291 obuf
= stream_new(ZEBRA_MAX_PACKET_SIZ
);
293 hook_register(zapi_client_close
, release_daemon_label_chunks
);
297 * Core function, assigns label cunks
299 * It first searches through the list to check if there's one available
300 * (previously released). Otherwise it creates and assigns a new one
302 * @param proto Daemon protocol of client, to identify the owner
303 * @param instance Instance, to identify the owner
304 * @param keep If set, avoid garbage collection
305 * @para size Size of the label chunk
306 * @return Pointer to the assigned label chunk
308 struct label_manager_chunk
*assign_label_chunk(uint8_t proto
,
309 unsigned short instance
,
310 uint8_t keep
, uint32_t size
)
312 struct label_manager_chunk
*lmc
;
313 struct listnode
*node
;
315 /* first check if there's one available */
316 for (ALL_LIST_ELEMENTS_RO(lbl_mgr
.lc_list
, node
, lmc
)) {
317 if (lmc
->proto
== NO_PROTO
318 && lmc
->end
- lmc
->start
+ 1 == size
) {
320 lmc
->instance
= instance
;
325 /* otherwise create a new one */
326 lmc
= XCALLOC(MTYPE_LM_CHUNK
, sizeof(struct label_manager_chunk
));
330 if (list_isempty(lbl_mgr
.lc_list
))
331 lmc
->start
= MPLS_LABEL_UNRESERVED_MIN
;
333 lmc
->start
= ((struct label_manager_chunk
*)listgetdata(
334 listtail(lbl_mgr
.lc_list
)))
337 if (lmc
->start
> MPLS_LABEL_UNRESERVED_MAX
- size
+ 1) {
338 zlog_err("Reached max labels. Start: %u, size: %u", lmc
->start
,
340 XFREE(MTYPE_LM_CHUNK
, lmc
);
343 lmc
->end
= lmc
->start
+ size
- 1;
345 lmc
->instance
= instance
;
347 listnode_add(lbl_mgr
.lc_list
, lmc
);
353 * Core function, release no longer used label cunks
355 * @param proto Daemon protocol of client, to identify the owner
356 * @param instance Instance, to identify the owner
357 * @param start First label of the chunk
358 * @param end Last label of the chunk
359 * @return 0 on success, -1 otherwise
361 int release_label_chunk(uint8_t proto
, unsigned short instance
, uint32_t start
,
364 struct listnode
*node
;
365 struct label_manager_chunk
*lmc
;
368 /* check that size matches */
369 zlog_debug("Releasing label chunk: %u - %u", start
, end
);
370 /* find chunk and disown */
371 for (ALL_LIST_ELEMENTS_RO(lbl_mgr
.lc_list
, node
, lmc
)) {
372 if (lmc
->start
!= start
)
376 if (lmc
->proto
!= proto
|| lmc
->instance
!= instance
) {
377 zlog_err("%s: Daemon mismatch!!", __func__
);
380 lmc
->proto
= NO_PROTO
;
387 zlog_err("%s: Label chunk not released!!", __func__
);
393 void label_manager_close()
395 list_delete_and_null(&lbl_mgr
.lc_list
);