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