]> git.proxmox.com Git - mirror_frr.git/blob - zebra/label_manager.c
Merge pull request #2110 from msablic/pim_mtrace_group
[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 * Release label chunks from a client.
240 *
241 * Called on client disconnection or reconnection. It only releases chunks
242 * with empty keep value.
243 *
244 * @param proto Daemon protocol of client, to identify the owner
245 * @param instance Instance, to identify the owner
246 * @return Number of chunks released
247 */
248 int release_daemon_label_chunks(struct zserv *client)
249 {
250 uint8_t proto = client->proto;
251 uint16_t instance = client->instance;
252 struct listnode *node;
253 struct label_manager_chunk *lmc;
254 int count = 0;
255 int ret;
256
257 for (ALL_LIST_ELEMENTS_RO(lbl_mgr.lc_list, node, lmc)) {
258 if (lmc->proto == proto && lmc->instance == instance
259 && lmc->keep == 0) {
260 ret = release_label_chunk(lmc->proto, lmc->instance,
261 lmc->start, lmc->end);
262 if (ret == 0)
263 count++;
264 }
265 }
266
267 zlog_debug("%s: Released %d label chunks", __func__, count);
268
269 return count;
270 }
271
272 /**
273 * Init label manager (or proxy to an external one)
274 */
275 void label_manager_init(char *lm_zserv_path)
276 {
277 /* this is an actual label manager */
278 if (!lm_zserv_path) {
279 zlog_debug("Initializing own label manager");
280 lm_is_external = false;
281 lbl_mgr.lc_list = list_new();
282 lbl_mgr.lc_list->del = delete_label_chunk;
283 } else { /* it's acting just as a proxy */
284 zlog_debug("Initializing external label manager at %s",
285 lm_zserv_path);
286 lm_is_external = true;
287 lm_zclient_init(lm_zserv_path);
288 }
289
290 ibuf = stream_new(ZEBRA_MAX_PACKET_SIZ);
291 obuf = stream_new(ZEBRA_MAX_PACKET_SIZ);
292
293 hook_register(zapi_client_close, release_daemon_label_chunks);
294 }
295
296 /**
297 * Core function, assigns label cunks
298 *
299 * It first searches through the list to check if there's one available
300 * (previously released). Otherwise it creates and assigns a new one
301 *
302 * @param proto Daemon protocol of client, to identify the owner
303 * @param instance Instance, to identify the owner
304 * @param keep If set, avoid garbage collection
305 * @para size Size of the label chunk
306 * @return Pointer to the assigned label chunk
307 */
308 struct label_manager_chunk *assign_label_chunk(uint8_t proto,
309 unsigned short instance,
310 uint8_t keep, uint32_t size)
311 {
312 struct label_manager_chunk *lmc;
313 struct listnode *node;
314
315 /* first check if there's one available */
316 for (ALL_LIST_ELEMENTS_RO(lbl_mgr.lc_list, node, lmc)) {
317 if (lmc->proto == NO_PROTO
318 && lmc->end - lmc->start + 1 == size) {
319 lmc->proto = proto;
320 lmc->instance = instance;
321 lmc->keep = keep;
322 return lmc;
323 }
324 }
325 /* otherwise create a new one */
326 lmc = XCALLOC(MTYPE_LM_CHUNK, sizeof(struct label_manager_chunk));
327 if (!lmc)
328 return NULL;
329
330 if (list_isempty(lbl_mgr.lc_list))
331 lmc->start = MPLS_LABEL_UNRESERVED_MIN;
332 else
333 lmc->start = ((struct label_manager_chunk *)listgetdata(
334 listtail(lbl_mgr.lc_list)))
335 ->end
336 + 1;
337 if (lmc->start > MPLS_LABEL_UNRESERVED_MAX - size + 1) {
338 zlog_err("Reached max labels. Start: %u, size: %u", lmc->start,
339 size);
340 XFREE(MTYPE_LM_CHUNK, lmc);
341 return NULL;
342 }
343 lmc->end = lmc->start + size - 1;
344 lmc->proto = proto;
345 lmc->instance = instance;
346 lmc->keep = keep;
347 listnode_add(lbl_mgr.lc_list, lmc);
348
349 return lmc;
350 }
351
352 /**
353 * Core function, release no longer used label cunks
354 *
355 * @param proto Daemon protocol of client, to identify the owner
356 * @param instance Instance, to identify the owner
357 * @param start First label of the chunk
358 * @param end Last label of the chunk
359 * @return 0 on success, -1 otherwise
360 */
361 int release_label_chunk(uint8_t proto, unsigned short instance, uint32_t start,
362 uint32_t end)
363 {
364 struct listnode *node;
365 struct label_manager_chunk *lmc;
366 int ret = -1;
367
368 /* check that size matches */
369 zlog_debug("Releasing label chunk: %u - %u", start, end);
370 /* find chunk and disown */
371 for (ALL_LIST_ELEMENTS_RO(lbl_mgr.lc_list, node, lmc)) {
372 if (lmc->start != start)
373 continue;
374 if (lmc->end != end)
375 continue;
376 if (lmc->proto != proto || lmc->instance != instance) {
377 zlog_err("%s: Daemon mismatch!!", __func__);
378 continue;
379 }
380 lmc->proto = NO_PROTO;
381 lmc->instance = 0;
382 lmc->keep = 0;
383 ret = 0;
384 break;
385 }
386 if (ret != 0)
387 zlog_err("%s: Label chunk not released!!", __func__);
388
389 return ret;
390 }
391
392
393 void label_manager_close()
394 {
395 list_delete_and_null(&lbl_mgr.lc_list);
396 stream_free(ibuf);
397 stream_free(obuf);
398 }