]> git.proxmox.com Git - mirror_frr.git/blame - zebra/table_manager.c
Merge pull request #10074 from opensourcerouting/assorted-20211116
[mirror_frr.git] / zebra / table_manager.c
CommitLineData
50261279
PG
1/* zebra table Manager for routing table identifier management
2 * Copyright (C) 2018 6WIND
3 *
4 * This program is free software; you can redistribute it and/or modify it
5 * under the terms of the GNU General Public License as published by the Free
6 * Software Foundation; either version 2 of the License, or (at your option)
7 * any later version.
8 *
9 * This program is distributed in the hope that it will be useful, but WITHOUT
10 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
11 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
12 * more details.
13 *
14 * You should have received a copy of the GNU General Public License along
15 * with this program; see the file COPYING; if not, write to the Free Software
16 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
17 */
18
453844ab
QY
19#include "zebra.h"
20
50261279
PG
21#include <stdio.h>
22#include <string.h>
23#include <sys/types.h>
24
50261279
PG
25#include "lib/log.h"
26#include "lib/memory.h"
27#include "lib/table.h"
28#include "lib/network.h"
29#include "lib/stream.h"
30#include "lib/zclient.h"
31#include "lib/libfrr.h"
32#include "lib/vrf.h"
33
453844ab
QY
34#include "zebra/zserv.h"
35#include "zebra/zebra_vrf.h"
36#include "zebra/label_manager.h" /* for NO_PROTO */
37#include "zebra/table_manager.h"
43e52561 38#include "zebra/zebra_errors.h"
50261279
PG
39
40/* routing table identifiers
41 *
42 */
cae8bc96 43#if !defined(GNU_LINUX)
50261279
PG
44/* BSD systems
45 */
46#else
47/* Linux Systems
48 */
49#define RT_TABLE_ID_LOCAL 255
50#define RT_TABLE_ID_MAIN 254
51#define RT_TABLE_ID_DEFAULT 253
52#define RT_TABLE_ID_COMPAT 252
53#define RT_TABLE_ID_UNSPEC 0
cae8bc96 54#endif /* !def(GNU_LINUX) */
50261279
PG
55#define RT_TABLE_ID_UNRESERVED_MIN 1
56#define RT_TABLE_ID_UNRESERVED_MAX 0xffffffff
57
50261279
PG
58DEFINE_MGROUP(TABLE_MGR, "Table Manager");
59DEFINE_MTYPE_STATIC(TABLE_MGR, TM_CHUNK, "Table Manager Chunk");
42d4b30e 60DEFINE_MTYPE_STATIC(TABLE_MGR, TM_TABLE, "Table Manager Context");
50261279
PG
61
62static void delete_table_chunk(void *val)
63{
64 XFREE(MTYPE_TM_CHUNK, val);
65}
66
67/**
68 * Init table manager
69 */
42d4b30e 70void table_manager_enable(struct zebra_vrf *zvrf)
50261279 71{
42d4b30e
PG
72
73 if (zvrf->tbl_mgr)
50261279 74 return;
9742796f
IR
75 if (!vrf_is_backend_netns()
76 && strcmp(zvrf_name(zvrf), VRF_DEFAULT_NAME)) {
42d4b30e
PG
77 struct zebra_vrf *def = zebra_vrf_lookup_by_id(VRF_DEFAULT);
78
79 if (def)
80 zvrf->tbl_mgr = def->tbl_mgr;
81 return;
82 }
83 zvrf->tbl_mgr = XCALLOC(MTYPE_TM_TABLE, sizeof(struct table_manager));
84 zvrf->tbl_mgr->lc_list = list_new();
85 zvrf->tbl_mgr->lc_list->del = delete_table_chunk;
50261279
PG
86}
87
88/**
89 * Core function, assigns table chunks
90 *
91 * It first searches through the list to check if there's one available
92 * (previously released). Otherwise it creates and assigns a new one
93 *
94 * @param proto Daemon protocol of client, to identify the owner
95 * @param instance Instance, to identify the owner
96 * @para size Size of the table chunk
97 * @return Pointer to the assigned table chunk
98 */
99struct table_manager_chunk *assign_table_chunk(uint8_t proto, uint16_t instance,
42d4b30e
PG
100 uint32_t size,
101 struct zebra_vrf *zvrf)
50261279
PG
102{
103 struct table_manager_chunk *tmc;
104 struct listnode *node;
105 uint32_t start;
42d4b30e
PG
106 bool manual_conf = false;
107
108 if (!zvrf)
109 return NULL;
50261279
PG
110
111 /* first check if there's one available */
42d4b30e 112 for (ALL_LIST_ELEMENTS_RO(zvrf->tbl_mgr->lc_list, node, tmc)) {
50261279
PG
113 if (tmc->proto == NO_PROTO
114 && tmc->end - tmc->start + 1 == size) {
115 tmc->proto = proto;
116 tmc->instance = instance;
117 return tmc;
118 }
119 }
120 /* otherwise create a new one */
121 tmc = XCALLOC(MTYPE_TM_CHUNK, sizeof(struct table_manager_chunk));
122 if (!tmc)
123 return NULL;
124
42d4b30e
PG
125 if (zvrf->tbl_mgr->start || zvrf->tbl_mgr->end)
126 manual_conf = true;
50261279
PG
127 /* table RT IDs range are [1;252] and [256;0xffffffff]
128 * - check if the requested range can be within the first range,
129 * otherwise elect second one
130 * - TODO : vrf-lites have their own table identifier.
131 * In that case, table_id should be removed from the table range.
132 */
42d4b30e
PG
133 if (list_isempty(zvrf->tbl_mgr->lc_list)) {
134 if (!manual_conf)
135 start = RT_TABLE_ID_UNRESERVED_MIN;
136 else
137 start = zvrf->tbl_mgr->start;
138 } else
50261279 139 start = ((struct table_manager_chunk *)listgetdata(
42d4b30e
PG
140 listtail(zvrf->tbl_mgr->lc_list)))
141 ->end
142 + 1;
143
144 if (!manual_conf) {
50261279 145
cae8bc96 146#if !defined(GNU_LINUX)
50261279
PG
147/* BSD systems
148 */
149#else
150/* Linux Systems
151 */
42d4b30e
PG
152 /* if not enough room space between MIN and COMPAT,
153 * then begin after LOCAL
154 */
155 if (start < RT_TABLE_ID_COMPAT
156 && (size > RT_TABLE_ID_COMPAT - RT_TABLE_ID_UNRESERVED_MIN))
157 start = RT_TABLE_ID_LOCAL + 1;
cae8bc96 158#endif /* !def(GNU_LINUX) */
42d4b30e
PG
159 tmc->start = start;
160 if (RT_TABLE_ID_UNRESERVED_MAX - size + 1 < start) {
161 flog_err(EC_ZEBRA_TM_EXHAUSTED_IDS,
162 "Reached max table id. Start/Size %u/%u",
163 start, size);
164 XFREE(MTYPE_TM_CHUNK, tmc);
165 return NULL;
166 }
167 } else {
168 tmc->start = start;
169 if (zvrf->tbl_mgr->end - size + 1 < start) {
170 flog_err(EC_ZEBRA_TM_EXHAUSTED_IDS,
171 "Reached max table id. Start/Size %u/%u",
172 start, size);
173 XFREE(MTYPE_TM_CHUNK, tmc);
174 return NULL;
175 }
50261279
PG
176 }
177 tmc->end = tmc->start + size - 1;
178 tmc->proto = proto;
179 tmc->instance = instance;
42d4b30e 180 listnode_add(zvrf->tbl_mgr->lc_list, tmc);
50261279
PG
181
182 return tmc;
183}
184
185/**
186 * Core function, release no longer used table chunks
187 *
188 * @param proto Daemon protocol of client, to identify the owner
189 * @param instance Instance, to identify the owner
190 * @param start First table RT ID of the chunk
191 * @param end Last table RT ID of the chunk
192 * @return 0 on success, -1 otherwise
193 */
194int release_table_chunk(uint8_t proto, uint16_t instance, uint32_t start,
42d4b30e 195 uint32_t end, struct zebra_vrf *zvrf)
50261279
PG
196{
197 struct listnode *node;
198 struct table_manager_chunk *tmc;
199 int ret = -1;
42d4b30e
PG
200 struct table_manager *tbl_mgr;
201
202 if (!zvrf)
203 return -1;
50261279 204
42d4b30e
PG
205 tbl_mgr = zvrf->tbl_mgr;
206 if (!tbl_mgr)
207 return ret;
50261279
PG
208 /* check that size matches */
209 zlog_debug("Releasing table chunk: %u - %u", start, end);
210 /* find chunk and disown */
42d4b30e 211 for (ALL_LIST_ELEMENTS_RO(tbl_mgr->lc_list, node, tmc)) {
50261279
PG
212 if (tmc->start != start)
213 continue;
214 if (tmc->end != end)
215 continue;
216 if (tmc->proto != proto || tmc->instance != instance) {
e914ccbe 217 flog_err(EC_ZEBRA_TM_DAEMON_MISMATCH,
1c50c1c0 218 "%s: Daemon mismatch!!", __func__);
50261279
PG
219 continue;
220 }
221 tmc->proto = NO_PROTO;
222 tmc->instance = 0;
223 ret = 0;
224 break;
225 }
226 if (ret != 0)
e914ccbe 227 flog_err(EC_ZEBRA_TM_UNRELEASED_CHUNK,
1c50c1c0 228 "%s: Table chunk not released!!", __func__);
50261279
PG
229
230 return ret;
231}
232
233/**
234 * Release table chunks from a client.
235 *
236 * Called on client disconnection or reconnection. It only releases chunks
237 * with empty keep value.
238 *
453844ab 239 * @param client the client to release chunks from
50261279
PG
240 * @return Number of chunks released
241 */
453844ab 242int release_daemon_table_chunks(struct zserv *client)
50261279 243{
453844ab
QY
244 uint8_t proto = client->proto;
245 uint16_t instance = client->instance;
50261279
PG
246 struct listnode *node;
247 struct table_manager_chunk *tmc;
248 int count = 0;
249 int ret;
42d4b30e
PG
250 struct vrf *vrf;
251 struct zebra_vrf *zvrf;
50261279 252
42d4b30e
PG
253 RB_FOREACH (vrf, vrf_name_head, &vrfs_by_name) {
254 zvrf = vrf->info;
255
256 if (!zvrf)
257 continue;
258 if (!vrf_is_backend_netns() && vrf->vrf_id != VRF_DEFAULT)
259 continue;
260 for (ALL_LIST_ELEMENTS_RO(zvrf->tbl_mgr->lc_list, node, tmc)) {
261 if (tmc->proto == proto && tmc->instance == instance) {
262 ret = release_table_chunk(
263 tmc->proto, tmc->instance, tmc->start,
264 tmc->end, zvrf);
265 if (ret == 0)
266 count++;
267 }
50261279
PG
268 }
269 }
50261279
PG
270 zlog_debug("%s: Released %d table chunks", __func__, count);
271
272 return count;
273}
274
42d4b30e
PG
275static void table_range_add(struct zebra_vrf *zvrf, uint32_t start,
276 uint32_t end)
277{
278 if (!zvrf->tbl_mgr)
279 return;
280 zvrf->tbl_mgr->start = start;
281 zvrf->tbl_mgr->end = end;
282}
283
284void table_manager_disable(struct zebra_vrf *zvrf)
50261279 285{
42d4b30e
PG
286 if (!zvrf->tbl_mgr)
287 return;
9742796f
IR
288 if (!vrf_is_backend_netns()
289 && strcmp(zvrf_name(zvrf), VRF_DEFAULT_NAME)) {
42d4b30e 290 zvrf->tbl_mgr = NULL;
50261279 291 return;
42d4b30e
PG
292 }
293 list_delete(&zvrf->tbl_mgr->lc_list);
294 XFREE(MTYPE_TM_TABLE, zvrf->tbl_mgr);
295 zvrf->tbl_mgr = NULL;
296}
297
298int table_manager_range(struct vty *vty, bool add, struct zebra_vrf *zvrf,
299 const char *start_table_str, const char *end_table_str)
300{
301 uint32_t start;
302 uint32_t end;
303
304 if (add) {
305 if (!start_table_str || !end_table_str) {
306 vty_out(vty, "%% Labels not specified\n");
307 return CMD_WARNING_CONFIG_FAILED;
308 }
309 start = atoi(start_table_str);
310 end = atoi(end_table_str);
311 if (end < start) {
312 vty_out(vty, "%% End table is less than Start table\n");
313 return CMD_WARNING_CONFIG_FAILED;
314 }
315
316#if !defined(GNU_LINUX)
317/* BSD systems
318 */
319#else
320 /* Linux Systems
321 */
322 if ((start >= RT_TABLE_ID_COMPAT && start <= RT_TABLE_ID_LOCAL)
323 || (end >= RT_TABLE_ID_COMPAT
324 && end <= RT_TABLE_ID_LOCAL)) {
325 vty_out(vty, "%% Values forbidden in range [%u;%u]\n",
326 RT_TABLE_ID_COMPAT, RT_TABLE_ID_LOCAL);
327 return CMD_WARNING_CONFIG_FAILED;
328 }
329 if (start < RT_TABLE_ID_COMPAT && end > RT_TABLE_ID_LOCAL) {
330 vty_out(vty,
331 "%% Range overlaps range [%u;%u] forbidden\n",
332 RT_TABLE_ID_COMPAT, RT_TABLE_ID_LOCAL);
333 return CMD_WARNING_CONFIG_FAILED;
334 }
335#endif
336 if (zvrf->tbl_mgr
337 && ((zvrf->tbl_mgr->start && zvrf->tbl_mgr->start != start)
338 || (zvrf->tbl_mgr->end && zvrf->tbl_mgr->end != end))) {
339 vty_out(vty,
340 "%% New range will be taken into account at restart\n");
341 }
342 table_range_add(zvrf, start, end);
343 } else
344 table_range_add(zvrf, 0, 0);
345 return CMD_SUCCESS;
50261279 346}