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