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