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
20 * along with FRR; see the file COPYING. If not, write to the Free
21 * Software Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA
27 #include <sys/types.h>
32 #include "lib/memory.h"
34 #include "lib/network.h"
35 #include "lib/stream.h"
36 #include "lib/zclient.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
);
60 * Receive a request to get or release a label chunk and forward it to external
63 * It's called from zserv in case it's not an actual label manager, but just a
66 * @param cmd Type of request (connect, get or release)
67 * @param src Input buffer from zserv
68 * @return 0 on success, -1 otherwise
70 int zread_relay_label_manager_request(int cmd
, struct zserv
*zserv
)
72 struct stream
*src
, *dst
;
75 if (zclient
->sock
< 0) {
76 zlog_err("%s: Error relaying label chunk request: no zclient socket",
80 /* Send request to external label manager */
84 stream_copy(dst
, src
);
86 ret
= writen(zclient
->sock
, dst
->data
, stream_get_endp(dst
));
88 zlog_err("%s: Error relaying label chunk request: %s", __func__
,
92 zlog_debug("%s: Label chunk request relayed. %d bytes sent", __func__
,
95 /* Release label chunk has no response */
96 if (cmd
== ZEBRA_RELEASE_LABEL_CHUNK
)
110 ret
= zclient_read_header(src
, zclient
->sock
, &size
, &marker
, &version
,
113 zlog_err("%s: Error reading label chunk response: %s", __func__
,
117 zlog_debug("%s: Label chunk response received, %d bytes", __func__
,
120 /* send response back */
121 stream_copy(dst
, src
);
122 stream_copy(zserv
->obuf
, zclient
->ibuf
);
123 ret
= writen(zserv
->sock
, dst
->data
, stream_get_endp(dst
));
125 zlog_err("%s: Error sending label chunk response back: %s",
126 __func__
, strerror(errno
));
129 zlog_debug("%s: Label chunk response (%d bytes) sent back", __func__
,
135 static int zclient_connect(struct thread
*t
)
137 zclient
->t_connect
= NULL
;
139 if (zclient
->sock
>= 0)
142 if (zclient_socket_connect(zclient
) < 0) {
143 zlog_err("Error connecting synchronous zclient!");
144 THREAD_TIMER_ON(zebrad
.master
, zclient
->t_connect
,
146 zclient
, CONNECTION_DELAY
);
154 * Function to initialize zclient in case this is not an actual
155 * label manager, but just a proxy to an external one.
157 * @param lm_zserv_path Path to zserv socket of external label manager
159 static void lm_zclient_init(char *lm_zserv_path
)
162 zclient_serv_path_set(lm_zserv_path
);
164 /* Set default values. */
165 zclient
= zclient_new(zebrad
.master
);
167 zclient
->t_connect
= NULL
;
168 zclient_connect (NULL
);
172 * Init label manager (or proxy to an external one)
174 void label_manager_init(char *lm_zserv_path
)
176 /* this is an actual label manager */
177 if (!lm_zserv_path
) {
178 zlog_debug("Initializing own label manager");
179 lm_is_external
= false;
180 lbl_mgr
.lc_list
= list_new();
181 lbl_mgr
.lc_list
->del
= delete_label_chunk
;
182 } else { /* it's acting just as a proxy */
183 zlog_debug("Initializing external label manager at %s",
185 lm_is_external
= true;
186 lm_zclient_init(lm_zserv_path
);
191 * Core function, assigns label cunks
193 * It first searches through the list to check if there's one available
194 * (previously released). Otherwise it creates and assigns a new one
196 * @param proto Daemon protocol of client, to identify the owner
197 * @param instance Instance, to identify the owner
198 * @param keep If set, avoid garbage collection
199 * @para size Size of the label chunk
200 * @return Pointer to the assigned label chunk
202 struct label_manager_chunk
*assign_label_chunk(u_char proto
, u_short instance
,
203 u_char keep
, uint32_t size
)
205 struct label_manager_chunk
*lmc
;
206 struct listnode
*node
;
208 node
= lbl_mgr
.lc_list
->head
;
209 /* first check if there's one available */
210 for (ALL_LIST_ELEMENTS_RO(lbl_mgr
.lc_list
, node
, lmc
)) {
211 if (lmc
->proto
== NO_PROTO
&& lmc
->end
- lmc
->start
+ 1 == size
) {
213 lmc
->instance
= instance
;
218 /* otherwise create a new one */
219 lmc
= XCALLOC(MTYPE_LM_CHUNK
, sizeof(struct label_manager_chunk
));
223 if (list_isempty(lbl_mgr
.lc_list
))
224 lmc
->start
= MPLS_MIN_UNRESERVED_LABEL
;
226 lmc
->start
= ((struct label_manager_chunk
*)
227 listgetdata(listtail(lbl_mgr
.lc_list
)))->end
+ 1;
228 if (lmc
->start
> MPLS_MAX_UNRESERVED_LABEL
- size
+ 1) {
229 zlog_err("Reached max labels. Start: %u, size: %u", lmc
->start
,
233 lmc
->end
= lmc
->start
+ size
- 1;
235 lmc
->instance
= instance
;
237 listnode_add(lbl_mgr
.lc_list
, lmc
);
243 * Core function, release no longer used label cunks
245 * @param proto Daemon protocol of client, to identify the owner
246 * @param instance Instance, to identify the owner
247 * @param start First label of the chunk
248 * @param end Last label of the chunk
249 * @return 0 on success, -1 otherwise
252 release_label_chunk(u_char proto
, u_short instance
, uint32_t start
,
255 struct listnode
*node
;
256 struct label_manager_chunk
*lmc
;
259 /* check that size matches */
260 zlog_debug("Releasing label chunk: %u - %u", start
, end
);
261 /* find chunk and disown */
262 for (ALL_LIST_ELEMENTS_RO(lbl_mgr
.lc_list
, node
, lmc
)) {
263 if (lmc
->start
!= start
)
267 if (lmc
->proto
!= proto
|| lmc
->instance
!= instance
) {
268 zlog_err("%s: Daemon mismatch!!", __func__
);
271 lmc
->proto
= NO_PROTO
;
278 zlog_err("%s: Label chunk not released!!", __func__
);
284 * Release label chunks from a client.
286 * Called on client disconnection or reconnection. It only releases chunks
287 * with empty keep value.
289 * @param proto Daemon protocol of client, to identify the owner
290 * @param instance Instance, to identify the owner
291 * @return Number of chunks released
293 int release_daemon_chunks(u_char proto
, u_short instance
)
295 struct listnode
*node
;
296 struct label_manager_chunk
*lmc
;
300 for (ALL_LIST_ELEMENTS_RO(lbl_mgr
.lc_list
, node
, lmc
)) {
301 if (lmc
->proto
== proto
&& lmc
->instance
== instance
304 release_label_chunk(lmc
->proto
, lmc
->instance
,
305 lmc
->start
, lmc
->end
);
311 zlog_debug("%s: Released %d label chunks", __func__
, count
);
316 void label_manager_close()
318 list_delete(lbl_mgr
.lc_list
);