]> git.proxmox.com Git - mirror_frr.git/blob - zebra/label_manager.c
zebra, lib: Fix SA warning and formatting.
[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 #include "lib/libfrr.h"
37
38 #include "label_manager.h"
39
40 #define CONNECTION_DELAY 5
41
42 struct label_manager lbl_mgr;
43
44 extern struct zebra_privs_t zserv_privs;
45
46 DEFINE_MGROUP(LBL_MGR, "Label Manager");
47 DEFINE_MTYPE_STATIC(LBL_MGR, LM_CHUNK, "Label Manager Chunk");
48
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
52 */
53 static struct stream *obuf;
54 static struct zclient *zclient;
55 bool lm_is_external;
56
57 static void delete_label_chunk(void *val)
58 {
59 XFREE(MTYPE_LM_CHUNK, val);
60 }
61
62 static int relay_response_back(void)
63 {
64 int ret = 0;
65 struct stream *src, *dst;
66 uint16_t size = 0;
67 uint8_t marker;
68 uint8_t version;
69 vrf_id_t vrf_id;
70 uint16_t resp_cmd;
71 uint8_t proto;
72 const char *proto_str;
73 unsigned short instance;
74 struct zserv *zserv;
75
76 /* input buffer with msg from label manager */
77 src = zclient->ibuf;
78
79 stream_reset(src);
80
81 /* parse header */
82 ret = zclient_read_header(src, zclient->sock, &size, &marker, &version,
83 &vrf_id, &resp_cmd);
84 if (ret < 0 && errno != EAGAIN) {
85 zlog_err("%s: Error reading Label Manager response: %s",
86 __func__, strerror(errno));
87 return -1;
88 }
89 zlog_debug("Label Manager response received, %d bytes", size);
90 if (size == 0)
91 return -1;
92
93 /* Get the 'proto' field of the message */
94 proto = stream_getc(src);
95
96 /* Get the 'instance' field of the message */
97 instance = stream_getw(src);
98
99 proto_str = zebra_route_string(proto);
100
101 /* lookup the client to relay the msg to */
102 zserv = zebra_find_client(proto, instance);
103 if (!zserv) {
104 zlog_err("Error relaying LM response: can't find client %s, instance %u",
105 proto_str, instance);
106 return -1;
107 }
108 zlog_debug("Found client to relay LM response to client %s instance %u",
109 proto_str, instance);
110
111 /* copy msg into output buffer */
112 dst = obuf;
113 stream_copy(dst, src);
114
115 /* send response back */
116 ret = writen(zserv->sock, dst->data, stream_get_endp(dst));
117 if (ret <= 0) {
118 zlog_err("Error relaying LM response to %s instance %u: %s",
119 proto_str, instance, strerror(errno));
120 return -1;
121 }
122 zlog_debug("Relayed LM response (%d bytes) to %s instance %u",
123 ret, proto_str, instance);
124
125 return 0;
126 }
127
128 static int lm_zclient_read(struct thread *t)
129 {
130 int ret;
131
132 zclient->t_read = NULL;
133
134 /* read response and send it back */
135 ret = relay_response_back();
136
137 return ret;
138 }
139
140 static int reply_error(int cmd, struct zserv *zserv, vrf_id_t vrf_id)
141 {
142 int ret;
143 struct stream *s;
144
145 s = stream_new(ZEBRA_MAX_PACKET_SIZ);
146
147 zclient_create_header(s, cmd, vrf_id);
148
149 /* proto */
150 stream_putc(s, zserv->proto);
151 /* instance */
152 stream_putw(s, zserv->instance);
153 /* result */
154 stream_putc(s, 1);
155
156 /* Write packet size. */
157 stream_putw_at(s, 0, stream_get_endp(s));
158
159 ret = writen(zserv->sock, s->data, stream_get_endp(s));
160
161 stream_free(s);
162 return ret;
163 }
164 /**
165 * Receive a request to get or release a label chunk and forward it to external
166 * label manager.
167 *
168 * It's called from zserv in case it's not an actual label manager, but just a
169 * proxy.
170 *
171 * @param cmd Type of request (connect, get or release)
172 * @param zserv
173 * @return 0 on success, -1 otherwise
174 */
175 int zread_relay_label_manager_request(int cmd, struct zserv *zserv,
176 struct stream *msg, vrf_id_t vrf_id)
177 {
178 struct stream *dst;
179 int ret = 0;
180 uint8_t proto;
181 const char *proto_str;
182 unsigned short instance;
183
184 if (zclient->sock < 0) {
185 zlog_err("Unable to relay LM request: no socket");
186 reply_error(cmd, zserv, vrf_id);
187 return -1;
188 }
189
190 /* peek msg to get proto and instance id. This zebra, which acts as
191 * a proxy needs to have such values for each client in order to
192 * relay responses back to it.
193 */
194
195 /* Get the 'proto' field of incoming msg */
196 proto = stream_getc(msg);
197
198 /* Get the 'instance' field of incoming msg */
199 instance = stream_getw(msg);
200
201 /* stringify proto */
202 proto_str = zebra_route_string(proto);
203
204 /* check & set client proto if unset */
205 if (zserv->proto && zserv->proto != proto) {
206 zlog_warn("Client proto(%u) != msg proto(%u)",
207 zserv->proto, proto);
208 return -1;
209 }
210
211 /* check & set client instance if unset */
212 if (zserv->instance && zserv->instance != instance) {
213 zlog_err("Client instance(%u) != msg instance(%u)",
214 zserv->instance, instance);
215 return -1;
216 }
217
218 /* recall proto and instance */
219 zserv->instance = instance;
220 zserv->proto = proto;
221
222 /* in case there's any incoming message enqueued, read and forward it */
223 while (ret == 0)
224 ret = relay_response_back();
225
226 /* get the msg buffer used toward the 'master' Label Manager */
227 dst = zclient->obuf;
228
229 /* copy the message */
230 stream_copy(dst, msg);
231
232 /* Send request to external label manager */
233 ret = writen(zclient->sock, dst->data, stream_get_endp(dst));
234 if (ret <= 0) {
235 zlog_err("Error relaying LM request from %s instance %u: %s",
236 proto_str, instance, strerror(errno));
237 reply_error(cmd, zserv, vrf_id);
238 return -1;
239 }
240 zlog_debug("Relayed LM request (%d bytes) from %s instance %u",
241 ret, proto_str, instance);
242
243
244 /* Release label chunk has no response */
245 if (cmd == ZEBRA_RELEASE_LABEL_CHUNK)
246 return 0;
247
248 /* make sure we listen to the response */
249 if (!zclient->t_read)
250 thread_add_read(zclient->master, lm_zclient_read, NULL,
251 zclient->sock, &zclient->t_read);
252
253 return 0;
254 }
255
256 static int lm_zclient_connect(struct thread *t)
257 {
258 zclient->t_connect = NULL;
259
260 if (zclient->sock >= 0)
261 return 0;
262
263 if (zclient_socket_connect(zclient) < 0) {
264 zlog_err("Error connecting synchronous zclient!");
265 thread_add_timer(zebrad.master, lm_zclient_connect, zclient,
266 CONNECTION_DELAY, &zclient->t_connect);
267 return -1;
268 }
269
270 /* make socket non-blocking */
271 if (set_nonblocking(zclient->sock) < 0)
272 zlog_warn("%s: set_nonblocking(%d) failed", __func__,
273 zclient->sock);
274
275 return 0;
276 }
277
278 /**
279 * Function to initialize zclient in case this is not an actual
280 * label manager, but just a proxy to an external one.
281 *
282 * @param lm_zserv_path Path to zserv socket of external label manager
283 */
284 static void lm_zclient_init(char *lm_zserv_path)
285 {
286 if (lm_zserv_path)
287 frr_zclient_addr(&zclient_addr, &zclient_addr_len,
288 lm_zserv_path);
289
290 /* Set default values. */
291 zclient = zclient_new_notify(zebrad.master, &zclient_options_default);
292 zclient->privs = &zserv_privs;
293 zclient->sock = -1;
294 zclient->t_connect = NULL;
295 lm_zclient_connect(NULL);
296 }
297
298 /**
299 * Release label chunks from a client.
300 *
301 * Called on client disconnection or reconnection. It only releases chunks
302 * with empty keep value.
303 *
304 * @param proto Daemon protocol of client, to identify the owner
305 * @param instance Instance, to identify the owner
306 * @return Number of chunks released
307 */
308 int release_daemon_label_chunks(struct zserv *client)
309 {
310 uint8_t proto = client->proto;
311 uint16_t instance = client->instance;
312 struct listnode *node;
313 struct label_manager_chunk *lmc;
314 int count = 0;
315 int ret;
316
317 for (ALL_LIST_ELEMENTS_RO(lbl_mgr.lc_list, node, lmc)) {
318 if (lmc->proto == proto && lmc->instance == instance
319 && lmc->keep == 0) {
320 ret = release_label_chunk(lmc->proto, lmc->instance,
321 lmc->start, lmc->end);
322 if (ret == 0)
323 count++;
324 }
325 }
326
327 zlog_debug("%s: Released %d label chunks", __func__, count);
328
329 return count;
330 }
331
332 /**
333 * Init label manager (or proxy to an external one)
334 */
335 void label_manager_init(char *lm_zserv_path)
336 {
337 /* this is an actual label manager */
338 if (!lm_zserv_path) {
339 zlog_debug("Initializing internal label manager");
340 lm_is_external = false;
341 lbl_mgr.lc_list = list_new();
342 lbl_mgr.lc_list->del = delete_label_chunk;
343 } else { /* it's acting just as a proxy */
344 zlog_debug("Initializing external label manager at %s",
345 lm_zserv_path);
346 lm_is_external = true;
347 lm_zclient_init(lm_zserv_path);
348 }
349
350 obuf = stream_new(ZEBRA_MAX_PACKET_SIZ);
351
352 hook_register(zapi_client_close, release_daemon_label_chunks);
353 }
354
355 /**
356 * Core function, assigns label cunks
357 *
358 * It first searches through the list to check if there's one available
359 * (previously released). Otherwise it creates and assigns a new one
360 *
361 * @param proto Daemon protocol of client, to identify the owner
362 * @param instance Instance, to identify the owner
363 * @param keep If set, avoid garbage collection
364 * @para size Size of the label chunk
365 * @return Pointer to the assigned label chunk
366 */
367 struct label_manager_chunk *assign_label_chunk(uint8_t proto,
368 unsigned short instance,
369 uint8_t keep, uint32_t size)
370 {
371 struct label_manager_chunk *lmc;
372 struct listnode *node;
373
374 /* first check if there's one available */
375 for (ALL_LIST_ELEMENTS_RO(lbl_mgr.lc_list, node, lmc)) {
376 if (lmc->proto == NO_PROTO
377 && lmc->end - lmc->start + 1 == size) {
378 lmc->proto = proto;
379 lmc->instance = instance;
380 lmc->keep = keep;
381 return lmc;
382 }
383 }
384 /* otherwise create a new one */
385 lmc = XCALLOC(MTYPE_LM_CHUNK, sizeof(struct label_manager_chunk));
386 if (!lmc)
387 return NULL;
388
389 if (list_isempty(lbl_mgr.lc_list))
390 lmc->start = MPLS_LABEL_UNRESERVED_MIN;
391 else
392 lmc->start = ((struct label_manager_chunk *)listgetdata(
393 listtail(lbl_mgr.lc_list)))
394 ->end
395 + 1;
396 if (lmc->start > MPLS_LABEL_UNRESERVED_MAX - size + 1) {
397 zlog_err("Reached max labels. Start: %u, size: %u", lmc->start,
398 size);
399 XFREE(MTYPE_LM_CHUNK, lmc);
400 return NULL;
401 }
402 lmc->end = lmc->start + size - 1;
403 lmc->proto = proto;
404 lmc->instance = instance;
405 lmc->keep = keep;
406 listnode_add(lbl_mgr.lc_list, lmc);
407
408 return lmc;
409 }
410
411 /**
412 * Core function, release no longer used label cunks
413 *
414 * @param proto Daemon protocol of client, to identify the owner
415 * @param instance Instance, to identify the owner
416 * @param start First label of the chunk
417 * @param end Last label of the chunk
418 * @return 0 on success, -1 otherwise
419 */
420 int release_label_chunk(uint8_t proto, unsigned short instance, uint32_t start,
421 uint32_t end)
422 {
423 struct listnode *node;
424 struct label_manager_chunk *lmc;
425 int ret = -1;
426
427 /* check that size matches */
428 zlog_debug("Releasing label chunk: %u - %u", start, end);
429 /* find chunk and disown */
430 for (ALL_LIST_ELEMENTS_RO(lbl_mgr.lc_list, node, lmc)) {
431 if (lmc->start != start)
432 continue;
433 if (lmc->end != end)
434 continue;
435 if (lmc->proto != proto || lmc->instance != instance) {
436 zlog_err("%s: Daemon mismatch!!", __func__);
437 continue;
438 }
439 lmc->proto = NO_PROTO;
440 lmc->instance = 0;
441 lmc->keep = 0;
442 ret = 0;
443 break;
444 }
445 if (ret != 0)
446 zlog_err("%s: Label chunk not released!!", __func__);
447
448 return ret;
449 }
450
451
452 void label_manager_close()
453 {
454 list_delete_and_null(&lbl_mgr.lc_list);
455 stream_free(obuf);
456 }