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