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