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 zclient
*zclient
;
56 static void delete_label_chunk(void *val
)
58 XFREE(MTYPE_LM_CHUNK
, val
);
61 static int relay_response_back(struct zserv
*zserv
)
64 struct stream
*src
, *dst
;
76 ret
= zclient_read_header(src
, zclient
->sock
, &size
, &marker
, &version
,
78 if (ret
< 0 && errno
!= EAGAIN
) {
79 zlog_err("%s: Error reading Label Manager response: %s",
80 __func__
, strerror(errno
));
83 zlog_debug("%s: Label Manager response received, %d bytes", __func__
,
88 /* send response back */
89 stream_copy(dst
, src
);
90 ret
= writen(zserv
->sock
, dst
->data
, stream_get_endp(dst
));
92 zlog_err("%s: Error sending Label Manager response back: %s",
93 __func__
, strerror(errno
));
96 zlog_debug("%s: Label Manager response (%d bytes) sent back", __func__
,
102 static int lm_zclient_read(struct thread
*t
)
107 /* Get socket to zebra. */
108 zserv
= THREAD_ARG(t
);
109 zclient
->t_read
= NULL
;
111 /* read response and send it back */
112 ret
= relay_response_back(zserv
);
117 static int reply_error(int cmd
, struct zserv
*zserv
, vrf_id_t vrf_id
)
124 zclient_create_header(s
, cmd
, vrf_id
);
129 /* Write packet size. */
130 stream_putw_at(s
, 0, stream_get_endp(s
));
132 return writen(zserv
->sock
, s
->data
, stream_get_endp(s
));
135 * Receive a request to get or release a label chunk and forward it to external
138 * It's called from zserv in case it's not an actual label manager, but just a
141 * @param cmd Type of request (connect, get or release)
143 * @return 0 on success, -1 otherwise
145 int zread_relay_label_manager_request(int cmd
, struct zserv
*zserv
,
148 struct stream
*src
, *dst
;
151 if (zclient
->sock
< 0) {
153 "%s: Error relaying label chunk request: no zclient socket",
155 reply_error(cmd
, zserv
, vrf_id
);
159 /* in case there's any incoming message enqueued, read and forward it */
161 ret
= relay_response_back(zserv
);
163 /* Send request to external label manager */
167 stream_copy(dst
, src
);
169 ret
= writen(zclient
->sock
, dst
->data
, stream_get_endp(dst
));
171 zlog_err("%s: Error relaying label chunk request: %s", __func__
,
173 reply_error(cmd
, zserv
, vrf_id
);
176 zlog_debug("%s: Label chunk request relayed. %d bytes sent", __func__
,
179 /* Release label chunk has no response */
180 if (cmd
== ZEBRA_RELEASE_LABEL_CHUNK
)
183 /* make sure we listen to the response */
184 if (!zclient
->t_read
)
185 thread_add_read(zclient
->master
, lm_zclient_read
, zserv
,
186 zclient
->sock
, &zclient
->t_read
);
191 static int lm_zclient_connect(struct thread
*t
)
193 zclient
->t_connect
= NULL
;
195 if (zclient
->sock
>= 0)
198 if (zclient_socket_connect(zclient
) < 0) {
199 zlog_err("Error connecting synchronous zclient!");
200 thread_add_timer(zebrad
.master
, lm_zclient_connect
, zclient
,
201 CONNECTION_DELAY
, &zclient
->t_connect
);
205 /* make socket non-blocking */
206 if (set_nonblocking(zclient
->sock
) < 0)
207 zlog_warn("%s: set_nonblocking(%d) failed", __func__
,
214 * Function to initialize zclient in case this is not an actual
215 * label manager, but just a proxy to an external one.
217 * @param lm_zserv_path Path to zserv socket of external label manager
219 static void lm_zclient_init(char *lm_zserv_path
)
222 frr_zclient_addr(&zclient_addr
, &zclient_addr_len
,
225 /* Set default values. */
226 zclient
= zclient_new_notify(zebrad
.master
, &zclient_options_default
);
227 zclient
->privs
= &zserv_privs
;
229 zclient
->t_connect
= NULL
;
230 lm_zclient_connect(NULL
);
234 * Init label manager (or proxy to an external one)
236 void label_manager_init(char *lm_zserv_path
)
238 /* this is an actual label manager */
239 if (!lm_zserv_path
) {
240 zlog_debug("Initializing own label manager");
241 lm_is_external
= false;
242 lbl_mgr
.lc_list
= list_new();
243 lbl_mgr
.lc_list
->del
= delete_label_chunk
;
244 } else { /* it's acting just as a proxy */
245 zlog_debug("Initializing external label manager at %s",
247 lm_is_external
= true;
248 lm_zclient_init(lm_zserv_path
);
253 * Core function, assigns label cunks
255 * It first searches through the list to check if there's one available
256 * (previously released). Otherwise it creates and assigns a new one
258 * @param proto Daemon protocol of client, to identify the owner
259 * @param instance Instance, to identify the owner
260 * @param keep If set, avoid garbage collection
261 * @para size Size of the label chunk
262 * @return Pointer to the assigned label chunk
264 struct label_manager_chunk
*assign_label_chunk(u_char proto
, u_short instance
,
265 u_char keep
, uint32_t size
)
267 struct label_manager_chunk
*lmc
;
268 struct listnode
*node
;
270 /* first check if there's one available */
271 for (ALL_LIST_ELEMENTS_RO(lbl_mgr
.lc_list
, node
, lmc
)) {
272 if (lmc
->proto
== NO_PROTO
273 && lmc
->end
- lmc
->start
+ 1 == size
) {
275 lmc
->instance
= instance
;
280 /* otherwise create a new one */
281 lmc
= XCALLOC(MTYPE_LM_CHUNK
, sizeof(struct label_manager_chunk
));
285 if (list_isempty(lbl_mgr
.lc_list
))
286 lmc
->start
= MPLS_LABEL_UNRESERVED_MIN
;
288 lmc
->start
= ((struct label_manager_chunk
*)listgetdata(
289 listtail(lbl_mgr
.lc_list
)))
292 if (lmc
->start
> MPLS_LABEL_UNRESERVED_MAX
- size
+ 1) {
293 zlog_err("Reached max labels. Start: %u, size: %u", lmc
->start
,
295 XFREE(MTYPE_LM_CHUNK
, lmc
);
298 lmc
->end
= lmc
->start
+ size
- 1;
300 lmc
->instance
= instance
;
302 listnode_add(lbl_mgr
.lc_list
, lmc
);
308 * Core function, release no longer used label cunks
310 * @param proto Daemon protocol of client, to identify the owner
311 * @param instance Instance, to identify the owner
312 * @param start First label of the chunk
313 * @param end Last label of the chunk
314 * @return 0 on success, -1 otherwise
316 int release_label_chunk(u_char proto
, u_short instance
, uint32_t start
,
319 struct listnode
*node
;
320 struct label_manager_chunk
*lmc
;
323 /* check that size matches */
324 zlog_debug("Releasing label chunk: %u - %u", start
, end
);
325 /* find chunk and disown */
326 for (ALL_LIST_ELEMENTS_RO(lbl_mgr
.lc_list
, node
, lmc
)) {
327 if (lmc
->start
!= start
)
331 if (lmc
->proto
!= proto
|| lmc
->instance
!= instance
) {
332 zlog_err("%s: Daemon mismatch!!", __func__
);
335 lmc
->proto
= NO_PROTO
;
342 zlog_err("%s: Label chunk not released!!", __func__
);
348 * Release label chunks from a client.
350 * Called on client disconnection or reconnection. It only releases chunks
351 * with empty keep value.
353 * @param proto Daemon protocol of client, to identify the owner
354 * @param instance Instance, to identify the owner
355 * @return Number of chunks released
357 int release_daemon_chunks(u_char proto
, u_short instance
)
359 struct listnode
*node
;
360 struct label_manager_chunk
*lmc
;
364 for (ALL_LIST_ELEMENTS_RO(lbl_mgr
.lc_list
, node
, lmc
)) {
365 if (lmc
->proto
== proto
&& lmc
->instance
== instance
367 ret
= release_label_chunk(lmc
->proto
, lmc
->instance
,
368 lmc
->start
, lmc
->end
);
374 zlog_debug("%s: Released %d label chunks", __func__
, count
);
379 void label_manager_close()
381 list_delete_and_null(&lbl_mgr
.lc_list
);