]>
Commit | Line | Data |
---|---|---|
fea12efb | 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 | * | |
896014f4 DL |
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 | |
fea12efb | 22 | */ |
23 | ||
43e52561 | 24 | #include <zebra.h> |
fea12efb | 25 | #include <stdio.h> |
26 | #include <string.h> | |
27 | #include <sys/types.h> | |
28 | ||
fea12efb | 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" | |
689f5a8c | 35 | #include "lib/libfrr.h" |
fea12efb | 36 | |
3801e764 DS |
37 | //#include "zebra/zserv.h" |
38 | #include "zebra/zebra_router.h" | |
43e52561 QY |
39 | #include "zebra/label_manager.h" |
40 | #include "zebra/zebra_errors.h" | |
fea12efb | 41 | |
42 | #define CONNECTION_DELAY 5 | |
43 | ||
44 | struct label_manager lbl_mgr; | |
45 | ||
7bfe377d | 46 | extern struct zebra_privs_t zserv_privs; |
47 | ||
fea12efb | 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 | */ | |
1002497a | 55 | static struct stream *obuf; |
fea12efb | 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 | ||
1cbba8ce | 64 | static int relay_response_back(void) |
5c7ef8dc | 65 | { |
66 | int ret = 0; | |
67 | struct stream *src, *dst; | |
d7c0a89a QY |
68 | uint16_t size = 0; |
69 | uint8_t marker; | |
70 | uint8_t version; | |
5c7ef8dc | 71 | vrf_id_t vrf_id; |
d7c0a89a | 72 | uint16_t resp_cmd; |
1cbba8ce FR |
73 | uint8_t proto; |
74 | const char *proto_str; | |
75 | unsigned short instance; | |
76 | struct zserv *zserv; | |
5c7ef8dc | 77 | |
1cbba8ce | 78 | /* input buffer with msg from label manager */ |
5c7ef8dc | 79 | src = zclient->ibuf; |
5c7ef8dc | 80 | |
81 | stream_reset(src); | |
82 | ||
1cbba8ce | 83 | /* parse header */ |
5c7ef8dc | 84 | ret = zclient_read_header(src, zclient->sock, &size, &marker, &version, |
d62a17ae | 85 | &vrf_id, &resp_cmd); |
5c7ef8dc | 86 | if (ret < 0 && errno != EAGAIN) { |
e914ccbe | 87 | flog_err(EC_ZEBRA_LM_RESPONSE, |
1c50c1c0 QY |
88 | "Error reading Label Manager response: %s", |
89 | strerror(errno)); | |
5c7ef8dc | 90 | return -1; |
91 | } | |
61eefcad A |
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 | ||
5c7ef8dc | 109 | if (size == 0) |
110 | return -1; | |
111 | ||
1cbba8ce FR |
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 */ | |
c0ea1ae7 | 121 | zserv = zserv_find_client(proto, instance); |
1cbba8ce | 122 | if (!zserv) { |
af4c2728 | 123 | flog_err( |
e914ccbe | 124 | EC_ZEBRA_LM_NO_SUCH_CLIENT, |
0313523d FR |
125 | "Error relaying LM response: can't find client %s, instance %u", |
126 | proto_str, instance); | |
1cbba8ce FR |
127 | return -1; |
128 | } | |
35cbe02a | 129 | zlog_debug("Found client to relay LM response to client %s instance %u", |
0313523d | 130 | proto_str, instance); |
1cbba8ce FR |
131 | |
132 | /* copy msg into output buffer */ | |
133 | dst = obuf; | |
5c7ef8dc | 134 | stream_copy(dst, src); |
1cbba8ce FR |
135 | |
136 | /* send response back */ | |
881999e6 | 137 | ret = writen(zserv->sock, dst->data, stream_get_endp(dst)); |
5c7ef8dc | 138 | if (ret <= 0) { |
e914ccbe | 139 | flog_err(EC_ZEBRA_LM_RELAY_FAILED, |
1c50c1c0 QY |
140 | "Error relaying LM response to %s instance %u: %s", |
141 | proto_str, instance, strerror(errno)); | |
5c7ef8dc | 142 | return -1; |
143 | } | |
0313523d FR |
144 | zlog_debug("Relayed LM response (%d bytes) to %s instance %u", ret, |
145 | proto_str, instance); | |
5c7ef8dc | 146 | |
147 | return 0; | |
148 | } | |
149 | ||
150 | static int lm_zclient_read(struct thread *t) | |
151 | { | |
5c7ef8dc | 152 | int ret; |
153 | ||
5c7ef8dc | 154 | zclient->t_read = NULL; |
155 | ||
156 | /* read response and send it back */ | |
1cbba8ce | 157 | ret = relay_response_back(); |
5c7ef8dc | 158 | |
98e9ab8b A |
159 | /* re-arm read */ |
160 | thread_add_read(zclient->master, lm_zclient_read, NULL, | |
161 | zclient->sock, &zclient->t_read); | |
5c7ef8dc | 162 | return ret; |
163 | } | |
164 | ||
d62a17ae | 165 | static int reply_error(int cmd, struct zserv *zserv, vrf_id_t vrf_id) |
5c7ef8dc | 166 | { |
1002497a | 167 | int ret; |
5c7ef8dc | 168 | struct stream *s; |
169 | ||
1002497a | 170 | s = stream_new(ZEBRA_MAX_PACKET_SIZ); |
5c7ef8dc | 171 | |
7cf15b25 | 172 | zclient_create_header(s, cmd, vrf_id); |
5c7ef8dc | 173 | |
1cbba8ce FR |
174 | /* proto */ |
175 | stream_putc(s, zserv->proto); | |
176 | /* instance */ | |
177 | stream_putw(s, zserv->instance); | |
5c7ef8dc | 178 | /* result */ |
d62a17ae | 179 | stream_putc(s, 1); |
5c7ef8dc | 180 | |
181 | /* Write packet size. */ | |
d62a17ae | 182 | stream_putw_at(s, 0, stream_get_endp(s)); |
5c7ef8dc | 183 | |
1002497a QY |
184 | ret = writen(zserv->sock, s->data, stream_get_endp(s)); |
185 | ||
186 | stream_free(s); | |
187 | return ret; | |
5c7ef8dc | 188 | } |
fea12efb | 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) | |
5c7ef8dc | 197 | * @param zserv |
fea12efb | 198 | * @return 0 on success, -1 otherwise |
199 | */ | |
d62a17ae | 200 | int zread_relay_label_manager_request(int cmd, struct zserv *zserv, |
0313523d | 201 | struct stream *msg, vrf_id_t vrf_id) |
fea12efb | 202 | { |
1cbba8ce | 203 | struct stream *dst; |
5c7ef8dc | 204 | int ret = 0; |
1cbba8ce FR |
205 | uint8_t proto; |
206 | const char *proto_str; | |
207 | unsigned short instance; | |
fea12efb | 208 | |
209 | if (zclient->sock < 0) { | |
e914ccbe | 210 | flog_err(EC_ZEBRA_LM_NO_SOCKET, |
1c50c1c0 | 211 | "Unable to relay LM request: no socket"); |
d62a17ae | 212 | reply_error(cmd, zserv, vrf_id); |
fea12efb | 213 | return -1; |
214 | } | |
5c7ef8dc | 215 | |
1cbba8ce FR |
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) { | |
e914ccbe | 232 | flog_warn(EC_ZEBRAING_LM_PROTO_MISMATCH, |
9df414fe | 233 | "Client proto(%u) != msg proto(%u)", zserv->proto, |
0313523d | 234 | proto); |
1cbba8ce FR |
235 | return -1; |
236 | } | |
237 | ||
238 | /* check & set client instance if unset */ | |
239 | if (zserv->instance && zserv->instance != instance) { | |
e914ccbe | 240 | flog_err(EC_ZEBRA_LM_BAD_INSTANCE, |
1c50c1c0 QY |
241 | "Client instance(%u) != msg instance(%u)", |
242 | zserv->instance, instance); | |
1cbba8ce FR |
243 | return -1; |
244 | } | |
245 | ||
246 | /* recall proto and instance */ | |
247 | zserv->instance = instance; | |
248 | zserv->proto = proto; | |
249 | ||
5c7ef8dc | 250 | /* in case there's any incoming message enqueued, read and forward it */ |
f533be73 | 251 | if (zserv->is_synchronous) |
252 | while (ret == 0) | |
253 | ret = relay_response_back(); | |
5c7ef8dc | 254 | |
1cbba8ce | 255 | /* get the msg buffer used toward the 'master' Label Manager */ |
fea12efb | 256 | dst = zclient->obuf; |
257 | ||
1cbba8ce FR |
258 | /* copy the message */ |
259 | stream_copy(dst, msg); | |
fea12efb | 260 | |
1cbba8ce | 261 | /* Send request to external label manager */ |
fea12efb | 262 | ret = writen(zclient->sock, dst->data, stream_get_endp(dst)); |
263 | if (ret <= 0) { | |
e914ccbe | 264 | flog_err(EC_ZEBRA_LM_RELAY_FAILED, |
1c50c1c0 QY |
265 | "Error relaying LM request from %s instance %u: %s", |
266 | proto_str, instance, strerror(errno)); | |
d62a17ae | 267 | reply_error(cmd, zserv, vrf_id); |
fea12efb | 268 | return -1; |
269 | } | |
0313523d FR |
270 | zlog_debug("Relayed LM request (%d bytes) from %s instance %u", ret, |
271 | proto_str, instance); | |
1cbba8ce | 272 | |
fea12efb | 273 | |
274 | /* Release label chunk has no response */ | |
275 | if (cmd == ZEBRA_RELEASE_LABEL_CHUNK) | |
276 | return 0; | |
277 | ||
5c7ef8dc | 278 | /* make sure we listen to the response */ |
279 | if (!zclient->t_read) | |
1cbba8ce | 280 | thread_add_read(zclient->master, lm_zclient_read, NULL, |
d62a17ae | 281 | zclient->sock, &zclient->t_read); |
fea12efb | 282 | |
283 | return 0; | |
284 | } | |
285 | ||
5c7ef8dc | 286 | static int lm_zclient_connect(struct thread *t) |
fea12efb | 287 | { |
288 | zclient->t_connect = NULL; | |
289 | ||
290 | if (zclient->sock >= 0) | |
291 | return 0; | |
292 | ||
293 | if (zclient_socket_connect(zclient) < 0) { | |
e914ccbe | 294 | flog_err(EC_ZEBRA_LM_CLIENT_CONNECTION_FAILED, |
1c50c1c0 | 295 | "Error connecting synchronous zclient!"); |
3801e764 | 296 | thread_add_timer(zrouter.master, lm_zclient_connect, zclient, |
ffa2c898 | 297 | CONNECTION_DELAY, &zclient->t_connect); |
fea12efb | 298 | return -1; |
299 | } | |
300 | ||
5c7ef8dc | 301 | /* make socket non-blocking */ |
9df414fe | 302 | (void)set_nonblocking(zclient->sock); |
5c7ef8dc | 303 | |
fea12efb | 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) | |
689f5a8c DL |
316 | frr_zclient_addr(&zclient_addr, &zclient_addr_len, |
317 | lm_zserv_path); | |
fea12efb | 318 | |
319 | /* Set default values. */ | |
3801e764 | 320 | zclient = zclient_new(zrouter.master, &zclient_options_default); |
7bfe377d | 321 | zclient->privs = &zserv_privs; |
fea12efb | 322 | zclient->sock = -1; |
323 | zclient->t_connect = NULL; | |
5c7ef8dc | 324 | lm_zclient_connect(NULL); |
fea12efb | 325 | } |
326 | ||
453844ab QY |
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 | ||
fea12efb | 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) { | |
35cbe02a | 368 | zlog_debug("Initializing internal label manager"); |
fea12efb | 369 | lm_is_external = false; |
370 | lbl_mgr.lc_list = list_new(); | |
371 | lbl_mgr.lc_list->del = delete_label_chunk; | |
d62a17ae | 372 | } else { /* it's acting just as a proxy */ |
fea12efb | 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 | } | |
1002497a | 378 | |
1002497a | 379 | obuf = stream_new(ZEBRA_MAX_PACKET_SIZ); |
453844ab | 380 | |
21ccc0cf | 381 | hook_register(zserv_client_close, release_daemon_label_chunks); |
fea12efb | 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 | */ | |
d7c0a89a QY |
396 | struct label_manager_chunk *assign_label_chunk(uint8_t proto, |
397 | unsigned short instance, | |
398 | uint8_t keep, uint32_t size) | |
fea12efb | 399 | { |
400 | struct label_manager_chunk *lmc; | |
401 | struct listnode *node; | |
402 | ||
fea12efb | 403 | /* first check if there's one available */ |
404 | for (ALL_LIST_ELEMENTS_RO(lbl_mgr.lc_list, node, lmc)) { | |
d62a17ae | 405 | if (lmc->proto == NO_PROTO |
406 | && lmc->end - lmc->start + 1 == size) { | |
fea12efb | 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)) | |
70e98a7f | 417 | lmc->start = MPLS_LABEL_UNRESERVED_MIN; |
fea12efb | 418 | else |
d62a17ae | 419 | lmc->start = ((struct label_manager_chunk *)listgetdata( |
420 | listtail(lbl_mgr.lc_list))) | |
421 | ->end | |
422 | + 1; | |
70e98a7f | 423 | if (lmc->start > MPLS_LABEL_UNRESERVED_MAX - size + 1) { |
e914ccbe | 424 | flog_err(EC_ZEBRA_LM_EXHAUSTED_LABELS, |
1c50c1c0 QY |
425 | "Reached max labels. Start: %u, size: %u", lmc->start, |
426 | size); | |
d62a17ae | 427 | XFREE(MTYPE_LM_CHUNK, lmc); |
fea12efb | 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 | */ | |
d7c0a89a | 448 | int release_label_chunk(uint8_t proto, unsigned short instance, uint32_t start, |
d62a17ae | 449 | uint32_t end) |
fea12efb | 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) { | |
e914ccbe | 464 | flog_err(EC_ZEBRA_LM_DAEMON_MISMATCH, |
1c50c1c0 | 465 | "%s: Daemon mismatch!!", __func__); |
fea12efb | 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) | |
e914ccbe | 475 | flog_err(EC_ZEBRA_LM_UNRELEASED_CHUNK, |
1c50c1c0 | 476 | "%s: Label chunk not released!!", __func__); |
fea12efb | 477 | |
478 | return ret; | |
479 | } | |
480 | ||
fea12efb | 481 | |
4d762f26 | 482 | void label_manager_close(void) |
fea12efb | 483 | { |
6a154c88 | 484 | list_delete(&lbl_mgr.lc_list); |
1002497a | 485 | stream_free(obuf); |
fea12efb | 486 | } |