]> git.proxmox.com Git - mirror_frr.git/blob - zebra/label_manager.c
Merge remote-tracking branch 'origin/master' into babel
[mirror_frr.git] / zebra / label_manager.c
1 /*
2 * Label Manager for FRR
3 *
4 * Copyright (C) 2017 by Bingen Eguzkitza,
5 * Volta Networks Inc.
6 *
7 * This file is part of FreeRangeRouting (FRR)
8 *
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
12 * later version.
13 *
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.
18 *
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
22 */
23
24 #include <stdio.h>
25 #include <string.h>
26 #include <sys/types.h>
27
28 #include "zebra.h"
29 #include "zserv.h"
30 #include "lib/log.h"
31 #include "lib/memory.h"
32 #include "lib/mpls.h"
33 #include "lib/network.h"
34 #include "lib/stream.h"
35 #include "lib/zclient.h"
36
37 #include "label_manager.h"
38
39 #define CONNECTION_DELAY 5
40
41 struct label_manager lbl_mgr;
42
43 DEFINE_MGROUP(LBL_MGR, "Label Manager");
44 DEFINE_MTYPE_STATIC(LBL_MGR, LM_CHUNK, "Label Manager Chunk");
45
46 /* In case this zebra daemon is not acting as label manager,
47 * it will be a proxy to relay messages to external label manager
48 * This zclient thus is to connect to it
49 */
50 static struct zclient *zclient;
51 bool lm_is_external;
52
53 static void delete_label_chunk(void *val)
54 {
55 XFREE(MTYPE_LM_CHUNK, val);
56 }
57
58 static int relay_response_back(struct zserv *zserv)
59 {
60 int ret = 0;
61 struct stream *src, *dst;
62 u_int16_t size = 0;
63 u_char marker;
64 u_char version;
65 vrf_id_t vrf_id;
66 u_int16_t resp_cmd;
67
68 src = zclient->ibuf;
69 dst = zserv->obuf;
70
71 stream_reset(src);
72
73 ret = zclient_read_header(src, zclient->sock, &size, &marker, &version,
74 &vrf_id, &resp_cmd);
75 if (ret < 0 && errno != EAGAIN) {
76 zlog_err("%s: Error reading Label Manager response: %s", __func__,
77 strerror(errno));
78 return -1;
79 }
80 zlog_debug("%s: Label Manager response received, %d bytes", __func__,
81 size);
82 if (size == 0)
83 return -1;
84
85 /* send response back */
86 stream_copy(dst, src);
87 ret = writen(zserv->sock, dst->data, stream_get_endp(dst));
88 if (ret <= 0) {
89 zlog_err("%s: Error sending Label Manager response back: %s",
90 __func__, strerror(errno));
91 return -1;
92 }
93 zlog_debug("%s: Label Manager response (%d bytes) sent back", __func__,
94 ret);
95
96 return 0;
97 }
98
99 static int lm_zclient_read(struct thread *t)
100 {
101 struct zserv *zserv;
102 int ret;
103
104 /* Get socket to zebra. */
105 zserv = THREAD_ARG(t);
106 zclient->t_read = NULL;
107
108 /* read response and send it back */
109 ret = relay_response_back(zserv);
110
111 return ret;
112 }
113
114 static int reply_error (int cmd, struct zserv *zserv, vrf_id_t vrf_id)
115 {
116 struct stream *s;
117
118 s = zserv->obuf;
119 stream_reset (s);
120
121 zserv_create_header (s, cmd, vrf_id);
122
123 /* result */
124 stream_putc (s, 1);
125
126 /* Write packet size. */
127 stream_putw_at (s, 0, stream_get_endp (s));
128
129 return writen (zserv->sock, s->data, stream_get_endp (s));
130
131 }
132 /**
133 * Receive a request to get or release a label chunk and forward it to external
134 * label manager.
135 *
136 * It's called from zserv in case it's not an actual label manager, but just a
137 * proxy.
138 *
139 * @param cmd Type of request (connect, get or release)
140 * @param zserv
141 * @return 0 on success, -1 otherwise
142 */
143 int zread_relay_label_manager_request(int cmd, struct zserv *zserv, vrf_id_t vrf_id)
144 {
145 struct stream *src, *dst;
146 int ret = 0;
147
148 if (zclient->sock < 0) {
149 zlog_err("%s: Error relaying label chunk request: no zclient socket",
150 __func__);
151 reply_error (cmd, zserv, vrf_id);
152 return -1;
153 }
154
155 /* in case there's any incoming message enqueued, read and forward it */
156 while (ret == 0)
157 ret = relay_response_back(zserv);
158
159 /* Send request to external label manager */
160 src = zserv->ibuf;
161 dst = zclient->obuf;
162
163 stream_copy(dst, src);
164
165 ret = writen(zclient->sock, dst->data, stream_get_endp(dst));
166 if (ret <= 0) {
167 zlog_err("%s: Error relaying label chunk request: %s", __func__,
168 strerror(errno));
169 reply_error (cmd, zserv, vrf_id);
170 return -1;
171 }
172 zlog_debug("%s: Label chunk request relayed. %d bytes sent", __func__,
173 ret);
174
175 /* Release label chunk has no response */
176 if (cmd == ZEBRA_RELEASE_LABEL_CHUNK)
177 return 0;
178
179 /* make sure we listen to the response */
180 if (!zclient->t_read)
181 thread_add_read(zclient->master, lm_zclient_read, zserv,
182 zclient->sock, &zclient->t_read);
183
184 return 0;
185 }
186
187 static int lm_zclient_connect(struct thread *t)
188 {
189 zclient->t_connect = NULL;
190
191 if (zclient->sock >= 0)
192 return 0;
193
194 if (zclient_socket_connect(zclient) < 0) {
195 zlog_err("Error connecting synchronous zclient!");
196 thread_add_timer(zebrad.master, lm_zclient_connect, zclient,
197 CONNECTION_DELAY, &zclient->t_connect);
198 return -1;
199 }
200
201 /* make socket non-blocking */
202 if (set_nonblocking(zclient->sock) < 0)
203 zlog_warn("%s: set_nonblocking(%d) failed", __func__, zclient->sock);
204
205 return 0;
206 }
207
208 /**
209 * Function to initialize zclient in case this is not an actual
210 * label manager, but just a proxy to an external one.
211 *
212 * @param lm_zserv_path Path to zserv socket of external label manager
213 */
214 static void lm_zclient_init(char *lm_zserv_path)
215 {
216 if (lm_zserv_path)
217 zclient_serv_path_set(lm_zserv_path);
218
219 /* Set default values. */
220 zclient = zclient_new(zebrad.master);
221 zclient->sock = -1;
222 zclient->t_connect = NULL;
223 lm_zclient_connect(NULL);
224 }
225
226 /**
227 * Init label manager (or proxy to an external one)
228 */
229 void label_manager_init(char *lm_zserv_path)
230 {
231 /* this is an actual label manager */
232 if (!lm_zserv_path) {
233 zlog_debug("Initializing own label manager");
234 lm_is_external = false;
235 lbl_mgr.lc_list = list_new();
236 lbl_mgr.lc_list->del = delete_label_chunk;
237 } else { /* it's acting just as a proxy */
238 zlog_debug("Initializing external label manager at %s",
239 lm_zserv_path);
240 lm_is_external = true;
241 lm_zclient_init(lm_zserv_path);
242 }
243 }
244
245 /**
246 * Core function, assigns label cunks
247 *
248 * It first searches through the list to check if there's one available
249 * (previously released). Otherwise it creates and assigns a new one
250 *
251 * @param proto Daemon protocol of client, to identify the owner
252 * @param instance Instance, to identify the owner
253 * @param keep If set, avoid garbage collection
254 * @para size Size of the label chunk
255 * @return Pointer to the assigned label chunk
256 */
257 struct label_manager_chunk *assign_label_chunk(u_char proto, u_short instance,
258 u_char keep, uint32_t size)
259 {
260 struct label_manager_chunk *lmc;
261 struct listnode *node;
262
263 /* first check if there's one available */
264 for (ALL_LIST_ELEMENTS_RO(lbl_mgr.lc_list, node, lmc)) {
265 if (lmc->proto == NO_PROTO && lmc->end - lmc->start + 1 == size) {
266 lmc->proto = proto;
267 lmc->instance = instance;
268 lmc->keep = keep;
269 return lmc;
270 }
271 }
272 /* otherwise create a new one */
273 lmc = XCALLOC(MTYPE_LM_CHUNK, sizeof(struct label_manager_chunk));
274 if (!lmc)
275 return NULL;
276
277 if (list_isempty(lbl_mgr.lc_list))
278 lmc->start = MPLS_MIN_UNRESERVED_LABEL;
279 else
280 lmc->start = ((struct label_manager_chunk *)
281 listgetdata(listtail(lbl_mgr.lc_list)))->end + 1;
282 if (lmc->start > MPLS_MAX_UNRESERVED_LABEL - size + 1) {
283 zlog_err("Reached max labels. Start: %u, size: %u", lmc->start,
284 size);
285 XFREE(MTYPE_LM_CHUNK, lmc);
286 return NULL;
287 }
288 lmc->end = lmc->start + size - 1;
289 lmc->proto = proto;
290 lmc->instance = instance;
291 lmc->keep = keep;
292 listnode_add(lbl_mgr.lc_list, lmc);
293
294 return lmc;
295 }
296
297 /**
298 * Core function, release no longer used label cunks
299 *
300 * @param proto Daemon protocol of client, to identify the owner
301 * @param instance Instance, to identify the owner
302 * @param start First label of the chunk
303 * @param end Last label of the chunk
304 * @return 0 on success, -1 otherwise
305 */
306 int
307 release_label_chunk(u_char proto, u_short instance, uint32_t start,
308 uint32_t end)
309 {
310 struct listnode *node;
311 struct label_manager_chunk *lmc;
312 int ret = -1;
313
314 /* check that size matches */
315 zlog_debug("Releasing label chunk: %u - %u", start, end);
316 /* find chunk and disown */
317 for (ALL_LIST_ELEMENTS_RO(lbl_mgr.lc_list, node, lmc)) {
318 if (lmc->start != start)
319 continue;
320 if (lmc->end != end)
321 continue;
322 if (lmc->proto != proto || lmc->instance != instance) {
323 zlog_err("%s: Daemon mismatch!!", __func__);
324 continue;
325 }
326 lmc->proto = NO_PROTO;
327 lmc->instance = 0;
328 lmc->keep = 0;
329 ret = 0;
330 break;
331 }
332 if (ret != 0)
333 zlog_err("%s: Label chunk not released!!", __func__);
334
335 return ret;
336 }
337
338 /**
339 * Release label chunks from a client.
340 *
341 * Called on client disconnection or reconnection. It only releases chunks
342 * with empty keep value.
343 *
344 * @param proto Daemon protocol of client, to identify the owner
345 * @param instance Instance, to identify the owner
346 * @return Number of chunks released
347 */
348 int release_daemon_chunks(u_char proto, u_short instance)
349 {
350 struct listnode *node;
351 struct label_manager_chunk *lmc;
352 int count = 0;
353 int ret;
354
355 for (ALL_LIST_ELEMENTS_RO(lbl_mgr.lc_list, node, lmc)) {
356 if (lmc->proto == proto && lmc->instance == instance
357 && lmc->keep == 0) {
358 ret =
359 release_label_chunk(lmc->proto, lmc->instance,
360 lmc->start, lmc->end);
361 if (ret == 0)
362 count++;
363 }
364 }
365
366 zlog_debug("%s: Released %d label chunks", __func__, count);
367
368 return count;
369 }
370
371 void label_manager_close()
372 {
373 list_delete(lbl_mgr.lc_list);
374 }