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 DEFINE_MGROUP(LBL_MGR
, "Label Manager");
45 DEFINE_MTYPE_STATIC(LBL_MGR
, LM_CHUNK
, "Label Manager Chunk");
47 /* In case this zebra daemon is not acting as label manager,
48 * it will be a proxy to relay messages to external label manager
49 * This zclient thus is to connect to it
51 static struct zclient
*zclient
;
54 static void delete_label_chunk(void *val
)
56 XFREE(MTYPE_LM_CHUNK
, val
);
59 static int relay_response_back(struct zserv
*zserv
)
62 struct stream
*src
, *dst
;
74 ret
= zclient_read_header(src
, zclient
->sock
, &size
, &marker
, &version
,
76 if (ret
< 0 && errno
!= EAGAIN
) {
77 zlog_err("%s: Error reading Label Manager response: %s",
78 __func__
, strerror(errno
));
81 zlog_debug("%s: Label Manager response received, %d bytes", __func__
,
86 /* send response back */
87 stream_copy(dst
, src
);
88 ret
= writen(zserv
->sock
, dst
->data
, stream_get_endp(dst
));
90 zlog_err("%s: Error sending Label Manager response back: %s",
91 __func__
, strerror(errno
));
94 zlog_debug("%s: Label Manager response (%d bytes) sent back", __func__
,
100 static int lm_zclient_read(struct thread
*t
)
105 /* Get socket to zebra. */
106 zserv
= THREAD_ARG(t
);
107 zclient
->t_read
= NULL
;
109 /* read response and send it back */
110 ret
= relay_response_back(zserv
);
115 static int reply_error(int cmd
, struct zserv
*zserv
, vrf_id_t vrf_id
)
122 zserv_create_header(s
, cmd
, vrf_id
);
127 /* Write packet size. */
128 stream_putw_at(s
, 0, stream_get_endp(s
));
130 return writen(zserv
->sock
, s
->data
, stream_get_endp(s
));
133 * Receive a request to get or release a label chunk and forward it to external
136 * It's called from zserv in case it's not an actual label manager, but just a
139 * @param cmd Type of request (connect, get or release)
141 * @return 0 on success, -1 otherwise
143 int zread_relay_label_manager_request(int cmd
, struct zserv
*zserv
,
146 struct stream
*src
, *dst
;
149 if (zclient
->sock
< 0) {
151 "%s: Error relaying label chunk request: no zclient socket",
153 reply_error(cmd
, zserv
, vrf_id
);
157 /* in case there's any incoming message enqueued, read and forward it */
159 ret
= relay_response_back(zserv
);
161 /* Send request to external label manager */
165 stream_copy(dst
, src
);
167 ret
= writen(zclient
->sock
, dst
->data
, stream_get_endp(dst
));
169 zlog_err("%s: Error relaying label chunk request: %s", __func__
,
171 reply_error(cmd
, zserv
, vrf_id
);
174 zlog_debug("%s: Label chunk request relayed. %d bytes sent", __func__
,
177 /* Release label chunk has no response */
178 if (cmd
== ZEBRA_RELEASE_LABEL_CHUNK
)
181 /* make sure we listen to the response */
182 if (!zclient
->t_read
)
183 thread_add_read(zclient
->master
, lm_zclient_read
, zserv
,
184 zclient
->sock
, &zclient
->t_read
);
189 static int lm_zclient_connect(struct thread
*t
)
191 zclient
->t_connect
= NULL
;
193 if (zclient
->sock
>= 0)
196 if (zclient_socket_connect(zclient
) < 0) {
197 zlog_err("Error connecting synchronous zclient!");
198 thread_add_timer(zebrad
.master
, lm_zclient_connect
, zclient
,
199 CONNECTION_DELAY
, &zclient
->t_connect
);
203 /* make socket non-blocking */
204 if (set_nonblocking(zclient
->sock
) < 0)
205 zlog_warn("%s: set_nonblocking(%d) failed", __func__
,
212 * Function to initialize zclient in case this is not an actual
213 * label manager, but just a proxy to an external one.
215 * @param lm_zserv_path Path to zserv socket of external label manager
217 static void lm_zclient_init(char *lm_zserv_path
)
220 frr_zclient_addr(&zclient_addr
, &zclient_addr_len
,
223 /* Set default values. */
224 zclient
= zclient_new_notify(zebrad
.master
, &zclient_options_default
);
226 zclient
->t_connect
= NULL
;
227 lm_zclient_connect(NULL
);
231 * Init label manager (or proxy to an external one)
233 void label_manager_init(char *lm_zserv_path
)
235 /* this is an actual label manager */
236 if (!lm_zserv_path
) {
237 zlog_debug("Initializing own label manager");
238 lm_is_external
= false;
239 lbl_mgr
.lc_list
= list_new();
240 lbl_mgr
.lc_list
->del
= delete_label_chunk
;
241 } else { /* it's acting just as a proxy */
242 zlog_debug("Initializing external label manager at %s",
244 lm_is_external
= true;
245 lm_zclient_init(lm_zserv_path
);
250 * Core function, assigns label cunks
252 * It first searches through the list to check if there's one available
253 * (previously released). Otherwise it creates and assigns a new one
255 * @param proto Daemon protocol of client, to identify the owner
256 * @param instance Instance, to identify the owner
257 * @param keep If set, avoid garbage collection
258 * @para size Size of the label chunk
259 * @return Pointer to the assigned label chunk
261 struct label_manager_chunk
*assign_label_chunk(u_char proto
, u_short instance
,
262 u_char keep
, uint32_t size
)
264 struct label_manager_chunk
*lmc
;
265 struct listnode
*node
;
267 /* first check if there's one available */
268 for (ALL_LIST_ELEMENTS_RO(lbl_mgr
.lc_list
, node
, lmc
)) {
269 if (lmc
->proto
== NO_PROTO
270 && lmc
->end
- lmc
->start
+ 1 == size
) {
272 lmc
->instance
= instance
;
277 /* otherwise create a new one */
278 lmc
= XCALLOC(MTYPE_LM_CHUNK
, sizeof(struct label_manager_chunk
));
282 if (list_isempty(lbl_mgr
.lc_list
))
283 lmc
->start
= MPLS_MIN_UNRESERVED_LABEL
;
285 lmc
->start
= ((struct label_manager_chunk
*)listgetdata(
286 listtail(lbl_mgr
.lc_list
)))
289 if (lmc
->start
> MPLS_MAX_UNRESERVED_LABEL
- size
+ 1) {
290 zlog_err("Reached max labels. Start: %u, size: %u", lmc
->start
,
292 XFREE(MTYPE_LM_CHUNK
, lmc
);
295 lmc
->end
= lmc
->start
+ size
- 1;
297 lmc
->instance
= instance
;
299 listnode_add(lbl_mgr
.lc_list
, lmc
);
305 * Core function, release no longer used label cunks
307 * @param proto Daemon protocol of client, to identify the owner
308 * @param instance Instance, to identify the owner
309 * @param start First label of the chunk
310 * @param end Last label of the chunk
311 * @return 0 on success, -1 otherwise
313 int release_label_chunk(u_char proto
, u_short instance
, uint32_t start
,
316 struct listnode
*node
;
317 struct label_manager_chunk
*lmc
;
320 /* check that size matches */
321 zlog_debug("Releasing label chunk: %u - %u", start
, end
);
322 /* find chunk and disown */
323 for (ALL_LIST_ELEMENTS_RO(lbl_mgr
.lc_list
, node
, lmc
)) {
324 if (lmc
->start
!= start
)
328 if (lmc
->proto
!= proto
|| lmc
->instance
!= instance
) {
329 zlog_err("%s: Daemon mismatch!!", __func__
);
332 lmc
->proto
= NO_PROTO
;
339 zlog_err("%s: Label chunk not released!!", __func__
);
345 * Release label chunks from a client.
347 * Called on client disconnection or reconnection. It only releases chunks
348 * with empty keep value.
350 * @param proto Daemon protocol of client, to identify the owner
351 * @param instance Instance, to identify the owner
352 * @return Number of chunks released
354 int release_daemon_chunks(u_char proto
, u_short instance
)
356 struct listnode
*node
;
357 struct label_manager_chunk
*lmc
;
361 for (ALL_LIST_ELEMENTS_RO(lbl_mgr
.lc_list
, node
, lmc
)) {
362 if (lmc
->proto
== proto
&& lmc
->instance
== instance
364 ret
= release_label_chunk(lmc
->proto
, lmc
->instance
,
365 lmc
->start
, lmc
->end
);
371 zlog_debug("%s: Released %d label chunks", __func__
, count
);
376 void label_manager_close()
378 list_delete_and_null(&lbl_mgr
.lc_list
);