]> git.proxmox.com Git - mirror_frr.git/blob - zebra/label_manager.c
Merge pull request #1927 from pguibert6WIND/issue_1926
[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 <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"
36 #include "lib/libfrr.h"
37
38 #include "label_manager.h"
39
40 #define CONNECTION_DELAY 5
41
42 struct label_manager lbl_mgr;
43
44 extern struct zebra_privs_t zserv_privs;
45
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 */
53 static struct stream *ibuf;
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(struct zserv *zserv)
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
73 src = zclient->ibuf;
74 dst = obuf;
75
76 stream_reset(src);
77
78 ret = zclient_read_header(src, zclient->sock, &size, &marker, &version,
79 &vrf_id, &resp_cmd);
80 if (ret < 0 && errno != EAGAIN) {
81 zlog_err("%s: Error reading Label Manager response: %s",
82 __func__, strerror(errno));
83 return -1;
84 }
85 zlog_debug("%s: Label Manager response received, %d bytes", __func__,
86 size);
87 if (size == 0)
88 return -1;
89
90 /* send response back */
91 stream_copy(dst, src);
92 ret = writen(zserv->sock, src->data, stream_get_endp(src));
93 if (ret <= 0) {
94 zlog_err("%s: Error sending Label Manager response back: %s",
95 __func__, strerror(errno));
96 return -1;
97 }
98 zlog_debug("%s: Label Manager response (%d bytes) sent back", __func__,
99 ret);
100
101 return 0;
102 }
103
104 static 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
119 static int reply_error(int cmd, struct zserv *zserv, vrf_id_t vrf_id)
120 {
121 int ret;
122 struct stream *s;
123
124 s = stream_new(ZEBRA_MAX_PACKET_SIZ);
125
126 zclient_create_header(s, cmd, vrf_id);
127
128 /* result */
129 stream_putc(s, 1);
130
131 /* Write packet size. */
132 stream_putw_at(s, 0, stream_get_endp(s));
133
134 ret = writen(zserv->sock, s->data, stream_get_endp(s));
135
136 stream_free(s);
137 return ret;
138 }
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)
147 * @param zserv
148 * @return 0 on success, -1 otherwise
149 */
150 int zread_relay_label_manager_request(int cmd, struct zserv *zserv,
151 vrf_id_t vrf_id)
152 {
153 struct stream *src, *dst;
154 int ret = 0;
155
156 if (zclient->sock < 0) {
157 zlog_err(
158 "%s: Error relaying label chunk request: no zclient socket",
159 __func__);
160 reply_error(cmd, zserv, vrf_id);
161 return -1;
162 }
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
168 /* Send request to external label manager */
169 src = ibuf;
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));
178 reply_error(cmd, zserv, vrf_id);
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
188 /* make sure we listen to the response */
189 if (!zclient->t_read)
190 thread_add_read(zclient->master, lm_zclient_read, zserv,
191 zclient->sock, &zclient->t_read);
192
193 return 0;
194 }
195
196 static int lm_zclient_connect(struct thread *t)
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!");
205 thread_add_timer(zebrad.master, lm_zclient_connect, zclient,
206 CONNECTION_DELAY, &zclient->t_connect);
207 return -1;
208 }
209
210 /* make socket non-blocking */
211 if (set_nonblocking(zclient->sock) < 0)
212 zlog_warn("%s: set_nonblocking(%d) failed", __func__,
213 zclient->sock);
214
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 */
224 static void lm_zclient_init(char *lm_zserv_path)
225 {
226 if (lm_zserv_path)
227 frr_zclient_addr(&zclient_addr, &zclient_addr_len,
228 lm_zserv_path);
229
230 /* Set default values. */
231 zclient = zclient_new_notify(zebrad.master, &zclient_options_default);
232 zclient->privs = &zserv_privs;
233 zclient->sock = -1;
234 zclient->t_connect = NULL;
235 lm_zclient_connect(NULL);
236 }
237
238 /**
239 * Init label manager (or proxy to an external one)
240 */
241 void 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;
249 } else { /* it's acting just as a proxy */
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 }
255
256 ibuf = stream_new(ZEBRA_MAX_PACKET_SIZ);
257 obuf = stream_new(ZEBRA_MAX_PACKET_SIZ);
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 */
272 struct label_manager_chunk *assign_label_chunk(uint8_t proto,
273 unsigned short instance,
274 uint8_t keep, uint32_t size)
275 {
276 struct label_manager_chunk *lmc;
277 struct listnode *node;
278
279 /* first check if there's one available */
280 for (ALL_LIST_ELEMENTS_RO(lbl_mgr.lc_list, node, lmc)) {
281 if (lmc->proto == NO_PROTO
282 && lmc->end - lmc->start + 1 == size) {
283 lmc->proto = proto;
284 lmc->instance = instance;
285 lmc->keep = keep;
286 return lmc;
287 }
288 }
289 /* otherwise create a new one */
290 lmc = XCALLOC(MTYPE_LM_CHUNK, sizeof(struct label_manager_chunk));
291 if (!lmc)
292 return NULL;
293
294 if (list_isempty(lbl_mgr.lc_list))
295 lmc->start = MPLS_LABEL_UNRESERVED_MIN;
296 else
297 lmc->start = ((struct label_manager_chunk *)listgetdata(
298 listtail(lbl_mgr.lc_list)))
299 ->end
300 + 1;
301 if (lmc->start > MPLS_LABEL_UNRESERVED_MAX - size + 1) {
302 zlog_err("Reached max labels. Start: %u, size: %u", lmc->start,
303 size);
304 XFREE(MTYPE_LM_CHUNK, lmc);
305 return NULL;
306 }
307 lmc->end = lmc->start + size - 1;
308 lmc->proto = proto;
309 lmc->instance = instance;
310 lmc->keep = keep;
311 listnode_add(lbl_mgr.lc_list, lmc);
312
313 return lmc;
314 }
315
316 /**
317 * Core function, release no longer used label cunks
318 *
319 * @param proto Daemon protocol of client, to identify the owner
320 * @param instance Instance, to identify the owner
321 * @param start First label of the chunk
322 * @param end Last label of the chunk
323 * @return 0 on success, -1 otherwise
324 */
325 int release_label_chunk(uint8_t proto, unsigned short instance, uint32_t start,
326 uint32_t end)
327 {
328 struct listnode *node;
329 struct label_manager_chunk *lmc;
330 int ret = -1;
331
332 /* check that size matches */
333 zlog_debug("Releasing label chunk: %u - %u", start, end);
334 /* find chunk and disown */
335 for (ALL_LIST_ELEMENTS_RO(lbl_mgr.lc_list, node, lmc)) {
336 if (lmc->start != start)
337 continue;
338 if (lmc->end != end)
339 continue;
340 if (lmc->proto != proto || lmc->instance != instance) {
341 zlog_err("%s: Daemon mismatch!!", __func__);
342 continue;
343 }
344 lmc->proto = NO_PROTO;
345 lmc->instance = 0;
346 lmc->keep = 0;
347 ret = 0;
348 break;
349 }
350 if (ret != 0)
351 zlog_err("%s: Label chunk not released!!", __func__);
352
353 return ret;
354 }
355
356 /**
357 * Release label chunks from a client.
358 *
359 * Called on client disconnection or reconnection. It only releases chunks
360 * with empty keep value.
361 *
362 * @param proto Daemon protocol of client, to identify the owner
363 * @param instance Instance, to identify the owner
364 * @return Number of chunks released
365 */
366 int release_daemon_label_chunks(uint8_t proto, unsigned short instance)
367 {
368 struct listnode *node;
369 struct label_manager_chunk *lmc;
370 int count = 0;
371 int ret;
372
373 for (ALL_LIST_ELEMENTS_RO(lbl_mgr.lc_list, node, lmc)) {
374 if (lmc->proto == proto && lmc->instance == instance
375 && lmc->keep == 0) {
376 ret = release_label_chunk(lmc->proto, lmc->instance,
377 lmc->start, lmc->end);
378 if (ret == 0)
379 count++;
380 }
381 }
382
383 zlog_debug("%s: Released %d label chunks", __func__, count);
384
385 return count;
386 }
387
388 void label_manager_close()
389 {
390 list_delete_and_null(&lbl_mgr.lc_list);
391 stream_free(ibuf);
392 stream_free(obuf);
393 }