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