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