]> git.proxmox.com Git - mirror_frr.git/blame - zebra/label_manager.c
Merge branch 'master' into stylechecker
[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
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
42struct label_manager lbl_mgr;
43
7bfe377d 44extern struct zebra_privs_t zserv_privs;
45
fea12efb 46DEFINE_MGROUP(LBL_MGR, "Label Manager");
47DEFINE_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
QY
53static struct stream *ibuf;
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
5c7ef8dc 63static int relay_response_back(struct zserv *zserv)
64{
65 int ret = 0;
66 struct stream *src, *dst;
67 u_int16_t size = 0;
68 u_char marker;
69 u_char version;
70 vrf_id_t vrf_id;
71 u_int16_t resp_cmd;
72
73 src = zclient->ibuf;
1002497a 74 dst = obuf;
5c7ef8dc 75
76 stream_reset(src);
77
78 ret = zclient_read_header(src, zclient->sock, &size, &marker, &version,
d62a17ae 79 &vrf_id, &resp_cmd);
5c7ef8dc 80 if (ret < 0 && errno != EAGAIN) {
d62a17ae 81 zlog_err("%s: Error reading Label Manager response: %s",
82 __func__, strerror(errno));
5c7ef8dc 83 return -1;
84 }
85 zlog_debug("%s: Label Manager response received, %d bytes", __func__,
d62a17ae 86 size);
5c7ef8dc 87 if (size == 0)
88 return -1;
89
90 /* send response back */
91 stream_copy(dst, src);
1002497a 92 ret = writen(zserv->sock, src->data, stream_get_endp(src));
5c7ef8dc 93 if (ret <= 0) {
94 zlog_err("%s: Error sending Label Manager response back: %s",
d62a17ae 95 __func__, strerror(errno));
5c7ef8dc 96 return -1;
97 }
98 zlog_debug("%s: Label Manager response (%d bytes) sent back", __func__,
d62a17ae 99 ret);
5c7ef8dc 100
101 return 0;
102}
103
104static int lm_zclient_read(struct thread *t)
105{
106 struct zserv *zserv;
107 int ret;
108
109 /* Get socket to zebra. */
110 zserv = THREAD_ARG(t);
111 zclient->t_read = NULL;
112
113 /* read response and send it back */
114 ret = relay_response_back(zserv);
115
116 return ret;
117}
118
d62a17ae 119static int reply_error(int cmd, struct zserv *zserv, vrf_id_t vrf_id)
5c7ef8dc 120{
1002497a 121 int ret;
5c7ef8dc 122 struct stream *s;
123
1002497a 124 s = stream_new(ZEBRA_MAX_PACKET_SIZ);
5c7ef8dc 125
7cf15b25 126 zclient_create_header(s, cmd, vrf_id);
5c7ef8dc 127
128 /* result */
d62a17ae 129 stream_putc(s, 1);
5c7ef8dc 130
131 /* Write packet size. */
d62a17ae 132 stream_putw_at(s, 0, stream_get_endp(s));
5c7ef8dc 133
1002497a
QY
134 ret = writen(zserv->sock, s->data, stream_get_endp(s));
135
136 stream_free(s);
137 return ret;
5c7ef8dc 138}
fea12efb 139/**
140 * Receive a request to get or release a label chunk and forward it to external
141 * label manager.
142 *
143 * It's called from zserv in case it's not an actual label manager, but just a
144 * proxy.
145 *
146 * @param cmd Type of request (connect, get or release)
5c7ef8dc 147 * @param zserv
fea12efb 148 * @return 0 on success, -1 otherwise
149 */
d62a17ae 150int zread_relay_label_manager_request(int cmd, struct zserv *zserv,
151 vrf_id_t vrf_id)
fea12efb 152{
153 struct stream *src, *dst;
5c7ef8dc 154 int ret = 0;
fea12efb 155
156 if (zclient->sock < 0) {
d62a17ae 157 zlog_err(
158 "%s: Error relaying label chunk request: no zclient socket",
159 __func__);
160 reply_error(cmd, zserv, vrf_id);
fea12efb 161 return -1;
162 }
5c7ef8dc 163
164 /* in case there's any incoming message enqueued, read and forward it */
165 while (ret == 0)
166 ret = relay_response_back(zserv);
167
fea12efb 168 /* Send request to external label manager */
1002497a 169 src = ibuf;
fea12efb 170 dst = zclient->obuf;
171
172 stream_copy(dst, src);
173
174 ret = writen(zclient->sock, dst->data, stream_get_endp(dst));
175 if (ret <= 0) {
176 zlog_err("%s: Error relaying label chunk request: %s", __func__,
177 strerror(errno));
d62a17ae 178 reply_error(cmd, zserv, vrf_id);
fea12efb 179 return -1;
180 }
181 zlog_debug("%s: Label chunk request relayed. %d bytes sent", __func__,
182 ret);
183
184 /* Release label chunk has no response */
185 if (cmd == ZEBRA_RELEASE_LABEL_CHUNK)
186 return 0;
187
5c7ef8dc 188 /* make sure we listen to the response */
189 if (!zclient->t_read)
0e20096f 190 thread_add_read(zclient->master, lm_zclient_read, zserv,
d62a17ae 191 zclient->sock, &zclient->t_read);
fea12efb 192
193 return 0;
194}
195
5c7ef8dc 196static int lm_zclient_connect(struct thread *t)
fea12efb 197{
198 zclient->t_connect = NULL;
199
200 if (zclient->sock >= 0)
201 return 0;
202
203 if (zclient_socket_connect(zclient) < 0) {
204 zlog_err("Error connecting synchronous zclient!");
fa84d193 205 thread_add_timer(zebrad.master, lm_zclient_connect, zclient,
ffa2c898 206 CONNECTION_DELAY, &zclient->t_connect);
fea12efb 207 return -1;
208 }
209
5c7ef8dc 210 /* make socket non-blocking */
211 if (set_nonblocking(zclient->sock) < 0)
d62a17ae 212 zlog_warn("%s: set_nonblocking(%d) failed", __func__,
213 zclient->sock);
5c7ef8dc 214
fea12efb 215 return 0;
216}
217
218/**
219 * Function to initialize zclient in case this is not an actual
220 * label manager, but just a proxy to an external one.
221 *
222 * @param lm_zserv_path Path to zserv socket of external label manager
223 */
224static void lm_zclient_init(char *lm_zserv_path)
225{
226 if (lm_zserv_path)
689f5a8c
DL
227 frr_zclient_addr(&zclient_addr, &zclient_addr_len,
228 lm_zserv_path);
fea12efb 229
230 /* Set default values. */
e1a1880d 231 zclient = zclient_new_notify(zebrad.master, &zclient_options_default);
7bfe377d 232 zclient->privs = &zserv_privs;
fea12efb 233 zclient->sock = -1;
234 zclient->t_connect = NULL;
5c7ef8dc 235 lm_zclient_connect(NULL);
fea12efb 236}
237
238/**
239 * Init label manager (or proxy to an external one)
240 */
241void label_manager_init(char *lm_zserv_path)
242{
243 /* this is an actual label manager */
244 if (!lm_zserv_path) {
245 zlog_debug("Initializing own label manager");
246 lm_is_external = false;
247 lbl_mgr.lc_list = list_new();
248 lbl_mgr.lc_list->del = delete_label_chunk;
d62a17ae 249 } else { /* it's acting just as a proxy */
fea12efb 250 zlog_debug("Initializing external label manager at %s",
251 lm_zserv_path);
252 lm_is_external = true;
253 lm_zclient_init(lm_zserv_path);
254 }
1002497a
QY
255
256 ibuf = stream_new(ZEBRA_MAX_PACKET_SIZ);
257 obuf = stream_new(ZEBRA_MAX_PACKET_SIZ);
fea12efb 258}
259
260/**
261 * Core function, assigns label cunks
262 *
263 * It first searches through the list to check if there's one available
264 * (previously released). Otherwise it creates and assigns a new one
265 *
266 * @param proto Daemon protocol of client, to identify the owner
267 * @param instance Instance, to identify the owner
268 * @param keep If set, avoid garbage collection
269 * @para size Size of the label chunk
270 * @return Pointer to the assigned label chunk
271 */
272struct label_manager_chunk *assign_label_chunk(u_char proto, u_short instance,
273 u_char keep, uint32_t size)
274{
275 struct label_manager_chunk *lmc;
276 struct listnode *node;
277
fea12efb 278 /* first check if there's one available */
279 for (ALL_LIST_ELEMENTS_RO(lbl_mgr.lc_list, node, lmc)) {
d62a17ae 280 if (lmc->proto == NO_PROTO
281 && lmc->end - lmc->start + 1 == size) {
fea12efb 282 lmc->proto = proto;
283 lmc->instance = instance;
284 lmc->keep = keep;
285 return lmc;
286 }
287 }
288 /* otherwise create a new one */
289 lmc = XCALLOC(MTYPE_LM_CHUNK, sizeof(struct label_manager_chunk));
66749b59 290 if (!lmc)
291 return NULL;
fea12efb 292
293 if (list_isempty(lbl_mgr.lc_list))
70e98a7f 294 lmc->start = MPLS_LABEL_UNRESERVED_MIN;
fea12efb 295 else
d62a17ae 296 lmc->start = ((struct label_manager_chunk *)listgetdata(
297 listtail(lbl_mgr.lc_list)))
298 ->end
299 + 1;
70e98a7f 300 if (lmc->start > MPLS_LABEL_UNRESERVED_MAX - size + 1) {
fea12efb 301 zlog_err("Reached max labels. Start: %u, size: %u", lmc->start,
302 size);
d62a17ae 303 XFREE(MTYPE_LM_CHUNK, lmc);
fea12efb 304 return NULL;
305 }
306 lmc->end = lmc->start + size - 1;
307 lmc->proto = proto;
308 lmc->instance = instance;
309 lmc->keep = keep;
310 listnode_add(lbl_mgr.lc_list, lmc);
311
312 return lmc;
313}
314
315/**
316 * Core function, release no longer used label cunks
317 *
318 * @param proto Daemon protocol of client, to identify the owner
319 * @param instance Instance, to identify the owner
320 * @param start First label of the chunk
321 * @param end Last label of the chunk
322 * @return 0 on success, -1 otherwise
323 */
d62a17ae 324int release_label_chunk(u_char proto, u_short instance, uint32_t start,
325 uint32_t end)
fea12efb 326{
327 struct listnode *node;
328 struct label_manager_chunk *lmc;
329 int ret = -1;
330
331 /* check that size matches */
332 zlog_debug("Releasing label chunk: %u - %u", start, end);
333 /* find chunk and disown */
334 for (ALL_LIST_ELEMENTS_RO(lbl_mgr.lc_list, node, lmc)) {
335 if (lmc->start != start)
336 continue;
337 if (lmc->end != end)
338 continue;
339 if (lmc->proto != proto || lmc->instance != instance) {
340 zlog_err("%s: Daemon mismatch!!", __func__);
341 continue;
342 }
343 lmc->proto = NO_PROTO;
344 lmc->instance = 0;
345 lmc->keep = 0;
346 ret = 0;
347 break;
348 }
349 if (ret != 0)
350 zlog_err("%s: Label chunk not released!!", __func__);
351
352 return ret;
353}
354
355/**
356 * Release label chunks from a client.
357 *
358 * Called on client disconnection or reconnection. It only releases chunks
359 * with empty keep value.
360 *
361 * @param proto Daemon protocol of client, to identify the owner
362 * @param instance Instance, to identify the owner
363 * @return Number of chunks released
364 */
365int release_daemon_chunks(u_char proto, u_short instance)
366{
367 struct listnode *node;
368 struct label_manager_chunk *lmc;
369 int count = 0;
370 int ret;
371
372 for (ALL_LIST_ELEMENTS_RO(lbl_mgr.lc_list, node, lmc)) {
373 if (lmc->proto == proto && lmc->instance == instance
374 && lmc->keep == 0) {
d62a17ae 375 ret = release_label_chunk(lmc->proto, lmc->instance,
376 lmc->start, lmc->end);
fea12efb 377 if (ret == 0)
378 count++;
379 }
380 }
381
382 zlog_debug("%s: Released %d label chunks", __func__, count);
383
384 return count;
385}
386
387void label_manager_close()
388{
affe9e99 389 list_delete_and_null(&lbl_mgr.lc_list);
1002497a
QY
390 stream_free(ibuf);
391 stream_free(obuf);
fea12efb 392}