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