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