]> git.proxmox.com Git - mirror_frr.git/blame - zebra/label_manager.c
bgpd: fixup bgpd: allow VPN next hop to be different AFI than NLRI next
[mirror_frr.git] / zebra / label_manager.c
CommitLineData
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
42struct label_manager lbl_mgr;
43
44DEFINE_MGROUP(LBL_MGR, "Label Manager");
45DEFINE_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 */
51static struct zclient *zclient;
52bool lm_is_external;
53
54static void delete_label_chunk(void *val)
55{
56 XFREE(MTYPE_LM_CHUNK, val);
57}
58
59/**
60 * Receive a request to get or release a label chunk and forward it to external
61 * label manager.
62 *
63 * It's called from zserv in case it's not an actual label manager, but just a
64 * proxy.
65 *
66 * @param cmd Type of request (connect, get or release)
67 * @param src Input buffer from zserv
68 * @return 0 on success, -1 otherwise
69 */
70int zread_relay_label_manager_request(int cmd, struct zserv *zserv)
71{
72 struct stream *src, *dst;
73 int ret;
74
75 if (zclient->sock < 0) {
76 zlog_err("%s: Error relaying label chunk request: no zclient socket",
77 __func__);
78 return -1;
79 }
80 /* Send request to external label manager */
81 src = zserv->ibuf;
82 dst = zclient->obuf;
83
84 stream_copy(dst, src);
85
86 ret = writen(zclient->sock, dst->data, stream_get_endp(dst));
87 if (ret <= 0) {
88 zlog_err("%s: Error relaying label chunk request: %s", __func__,
89 strerror(errno));
90 return -1;
91 }
92 zlog_debug("%s: Label chunk request relayed. %d bytes sent", __func__,
93 ret);
94
95 /* Release label chunk has no response */
96 if (cmd == ZEBRA_RELEASE_LABEL_CHUNK)
97 return 0;
98
99 /* read response */
100 src = zclient->ibuf;
101 dst = zserv->obuf;
102
103 stream_reset(src);
104
105 u_int16_t size;
106 u_char marker;
107 u_char version;
108 vrf_id_t vrf_id;
109 u_int16_t resp_cmd;
110 ret = zclient_read_header(src, zclient->sock, &size, &marker, &version,
111 &vrf_id, &resp_cmd);
112 if (ret < 0) {
113 zlog_err("%s: Error reading label chunk response: %s", __func__,
114 strerror(errno));
115 return -1;
116 }
117 zlog_debug("%s: Label chunk response received, %d bytes", __func__,
118 size);
119
120 /* send response back */
121 stream_copy(dst, src);
122 stream_copy(zserv->obuf, zclient->ibuf);
123 ret = writen(zserv->sock, dst->data, stream_get_endp(dst));
124 if (ret <= 0) {
125 zlog_err("%s: Error sending label chunk response back: %s",
126 __func__, strerror(errno));
127 return -1;
128 }
129 zlog_debug("%s: Label chunk response (%d bytes) sent back", __func__,
130 ret);
131
132 return 0;
133}
134
135static int zclient_connect(struct thread *t)
136{
137 zclient->t_connect = NULL;
138
139 if (zclient->sock >= 0)
140 return 0;
141
142 if (zclient_socket_connect(zclient) < 0) {
143 zlog_err("Error connecting synchronous zclient!");
144 THREAD_TIMER_ON(zebrad.master, zclient->t_connect,
145 zclient_connect,
146 zclient, CONNECTION_DELAY);
147 return -1;
148 }
149
150 return 0;
151}
152
153/**
154 * Function to initialize zclient in case this is not an actual
155 * label manager, but just a proxy to an external one.
156 *
157 * @param lm_zserv_path Path to zserv socket of external label manager
158 */
159static void lm_zclient_init(char *lm_zserv_path)
160{
161 if (lm_zserv_path)
162 zclient_serv_path_set(lm_zserv_path);
163
164 /* Set default values. */
165 zclient = zclient_new(zebrad.master);
166 zclient->sock = -1;
167 zclient->t_connect = NULL;
168 zclient_connect (NULL);
169}
170
171/**
172 * Init label manager (or proxy to an external one)
173 */
174void label_manager_init(char *lm_zserv_path)
175{
176 /* this is an actual label manager */
177 if (!lm_zserv_path) {
178 zlog_debug("Initializing own label manager");
179 lm_is_external = false;
180 lbl_mgr.lc_list = list_new();
181 lbl_mgr.lc_list->del = delete_label_chunk;
182 } else { /* it's acting just as a proxy */
183 zlog_debug("Initializing external label manager at %s",
184 lm_zserv_path);
185 lm_is_external = true;
186 lm_zclient_init(lm_zserv_path);
187 }
188}
189
190/**
191 * Core function, assigns label cunks
192 *
193 * It first searches through the list to check if there's one available
194 * (previously released). Otherwise it creates and assigns a new one
195 *
196 * @param proto Daemon protocol of client, to identify the owner
197 * @param instance Instance, to identify the owner
198 * @param keep If set, avoid garbage collection
199 * @para size Size of the label chunk
200 * @return Pointer to the assigned label chunk
201 */
202struct label_manager_chunk *assign_label_chunk(u_char proto, u_short instance,
203 u_char keep, uint32_t size)
204{
205 struct label_manager_chunk *lmc;
206 struct listnode *node;
207
fea12efb 208 /* first check if there's one available */
209 for (ALL_LIST_ELEMENTS_RO(lbl_mgr.lc_list, node, lmc)) {
210 if (lmc->proto == NO_PROTO && lmc->end - lmc->start + 1 == size) {
211 lmc->proto = proto;
212 lmc->instance = instance;
213 lmc->keep = keep;
214 return lmc;
215 }
216 }
217 /* otherwise create a new one */
218 lmc = XCALLOC(MTYPE_LM_CHUNK, sizeof(struct label_manager_chunk));
66749b59 219 if (!lmc)
220 return NULL;
fea12efb 221
222 if (list_isempty(lbl_mgr.lc_list))
223 lmc->start = MPLS_MIN_UNRESERVED_LABEL;
224 else
225 lmc->start = ((struct label_manager_chunk *)
226 listgetdata(listtail(lbl_mgr.lc_list)))->end + 1;
227 if (lmc->start > MPLS_MAX_UNRESERVED_LABEL - size + 1) {
228 zlog_err("Reached max labels. Start: %u, size: %u", lmc->start,
229 size);
1c72f1a0 230 XFREE(MTYPE_LM_CHUNK, lmc);
fea12efb 231 return NULL;
232 }
233 lmc->end = lmc->start + size - 1;
234 lmc->proto = proto;
235 lmc->instance = instance;
236 lmc->keep = keep;
237 listnode_add(lbl_mgr.lc_list, lmc);
238
239 return lmc;
240}
241
242/**
243 * Core function, release no longer used label cunks
244 *
245 * @param proto Daemon protocol of client, to identify the owner
246 * @param instance Instance, to identify the owner
247 * @param start First label of the chunk
248 * @param end Last label of the chunk
249 * @return 0 on success, -1 otherwise
250 */
251int
252release_label_chunk(u_char proto, u_short instance, uint32_t start,
253 uint32_t end)
254{
255 struct listnode *node;
256 struct label_manager_chunk *lmc;
257 int ret = -1;
258
259 /* check that size matches */
260 zlog_debug("Releasing label chunk: %u - %u", start, end);
261 /* find chunk and disown */
262 for (ALL_LIST_ELEMENTS_RO(lbl_mgr.lc_list, node, lmc)) {
263 if (lmc->start != start)
264 continue;
265 if (lmc->end != end)
266 continue;
267 if (lmc->proto != proto || lmc->instance != instance) {
268 zlog_err("%s: Daemon mismatch!!", __func__);
269 continue;
270 }
271 lmc->proto = NO_PROTO;
272 lmc->instance = 0;
273 lmc->keep = 0;
274 ret = 0;
275 break;
276 }
277 if (ret != 0)
278 zlog_err("%s: Label chunk not released!!", __func__);
279
280 return ret;
281}
282
283/**
284 * Release label chunks from a client.
285 *
286 * Called on client disconnection or reconnection. It only releases chunks
287 * with empty keep value.
288 *
289 * @param proto Daemon protocol of client, to identify the owner
290 * @param instance Instance, to identify the owner
291 * @return Number of chunks released
292 */
293int release_daemon_chunks(u_char proto, u_short instance)
294{
295 struct listnode *node;
296 struct label_manager_chunk *lmc;
297 int count = 0;
298 int ret;
299
300 for (ALL_LIST_ELEMENTS_RO(lbl_mgr.lc_list, node, lmc)) {
301 if (lmc->proto == proto && lmc->instance == instance
302 && lmc->keep == 0) {
303 ret =
304 release_label_chunk(lmc->proto, lmc->instance,
305 lmc->start, lmc->end);
306 if (ret == 0)
307 count++;
308 }
309 }
310
311 zlog_debug("%s: Released %d label chunks", __func__, count);
312
313 return count;
314}
315
316void label_manager_close()
317{
318 list_delete(lbl_mgr.lc_list);
319}