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