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