summaryrefslogtreecommitdiffstats
path: root/zebra/table_manager.c
blob: 512508b79f96cf30f2940e534cd400f99c1a2791 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
// SPDX-License-Identifier: GPL-2.0-or-later
/* zebra table Manager for routing table identifier management
 * Copyright (C) 2018 6WIND
 */

#include "zebra.h"

#include <stdio.h>
#include <string.h>
#include <sys/types.h>

#include "lib/log.h"
#include "lib/memory.h"
#include "lib/table.h"
#include "lib/network.h"
#include "lib/stream.h"
#include "lib/zclient.h"
#include "lib/libfrr.h"
#include "lib/vrf.h"

#include "zebra/zserv.h"
#include "zebra/zebra_vrf.h"
#include "zebra/label_manager.h" /* for NO_PROTO */
#include "zebra/table_manager.h"
#include "zebra/zebra_errors.h"

/* routing table identifiers
 *
 */
#if !defined(GNU_LINUX)
/* BSD systems
 */
#else
/* Linux Systems
 */
#define RT_TABLE_ID_LOCAL                  255
#define RT_TABLE_ID_MAIN                   254
#define RT_TABLE_ID_DEFAULT                253
#define RT_TABLE_ID_COMPAT                 252
#define RT_TABLE_ID_UNSPEC                 0
#endif /* !def(GNU_LINUX) */
#define RT_TABLE_ID_UNRESERVED_MIN         1
#define RT_TABLE_ID_UNRESERVED_MAX         0xffffffff

DEFINE_MGROUP(TABLE_MGR, "Table Manager");
DEFINE_MTYPE_STATIC(TABLE_MGR, TM_CHUNK, "Table Manager Chunk");
DEFINE_MTYPE_STATIC(TABLE_MGR, TM_TABLE, "Table Manager Context");

static void delete_table_chunk(void *val)
{
	XFREE(MTYPE_TM_CHUNK, val);
}

/**
 * Init table manager
 */
void table_manager_enable(struct zebra_vrf *zvrf)
{

	if (zvrf->tbl_mgr)
		return;
	if (!vrf_is_backend_netns()
	    && strcmp(zvrf_name(zvrf), VRF_DEFAULT_NAME)) {
		struct zebra_vrf *def = zebra_vrf_lookup_by_id(VRF_DEFAULT);

		zvrf->tbl_mgr = def->tbl_mgr;
		return;
	}
	zvrf->tbl_mgr = XCALLOC(MTYPE_TM_TABLE, sizeof(struct table_manager));
	zvrf->tbl_mgr->lc_list = list_new();
	zvrf->tbl_mgr->lc_list->del = delete_table_chunk;
}

/**
 * Core function, assigns table chunks
 *
 * It first searches through the list to check if there's one available
 * (previously released). Otherwise it creates and assigns a new one
 *
 * @param proto Daemon protocol of client, to identify the owner
 * @param instance Instance, to identify the owner
 * @para size Size of the table chunk
 * @return Pointer to the assigned table chunk
 */
struct table_manager_chunk *assign_table_chunk(uint8_t proto, uint16_t instance,
					       uint32_t size,
					       struct zebra_vrf *zvrf)
{
	struct table_manager_chunk *tmc;
	struct listnode *node;
	uint32_t start;
	bool manual_conf = false;

	if (!zvrf)
		return NULL;

	/* first check if there's one available */
	for (ALL_LIST_ELEMENTS_RO(zvrf->tbl_mgr->lc_list, node, tmc)) {
		if (tmc->proto == NO_PROTO
		    && tmc->end - tmc->start + 1 == size) {
			tmc->proto = proto;
			tmc->instance = instance;
			return tmc;
		}
	}
	/* otherwise create a new one */
	tmc = XCALLOC(MTYPE_TM_CHUNK, sizeof(struct table_manager_chunk));

	if (zvrf->tbl_mgr->start || zvrf->tbl_mgr->end)
		manual_conf = true;
	/* table RT IDs range are [1;252] and [256;0xffffffff]
	 * - check if the requested range can be within the first range,
	 * otherwise elect second one
	 * - TODO : vrf-lites have their own table identifier.
	 * In that case, table_id should be removed from the table range.
	 */
	if (list_isempty(zvrf->tbl_mgr->lc_list)) {
		if (!manual_conf)
			start = RT_TABLE_ID_UNRESERVED_MIN;
		else
			start = zvrf->tbl_mgr->start;
	} else
		start = ((struct table_manager_chunk *)listgetdata(
				 listtail(zvrf->tbl_mgr->lc_list)))
				->end
			+ 1;

	if (!manual_conf) {

#if !defined(GNU_LINUX)
/* BSD systems
 */
#else
/* Linux Systems
 */
		/* if not enough room space between MIN and COMPAT,
		 * then begin after LOCAL
		 */
		if (start < RT_TABLE_ID_COMPAT
		    && (size > RT_TABLE_ID_COMPAT - RT_TABLE_ID_UNRESERVED_MIN))
			start = RT_TABLE_ID_LOCAL + 1;
#endif /* !def(GNU_LINUX) */
		tmc->start = start;
		if (RT_TABLE_ID_UNRESERVED_MAX - size + 1 < start) {
			flog_err(EC_ZEBRA_TM_EXHAUSTED_IDS,
				 "Reached max table id. Start/Size %u/%u",
				 start, size);
			XFREE(MTYPE_TM_CHUNK, tmc);
			return NULL;
		}
	} else {
		tmc->start = start;
		if (zvrf->tbl_mgr->end - size + 1 < start) {
			flog_err(EC_ZEBRA_TM_EXHAUSTED_IDS,
				 "Reached max table id. Start/Size %u/%u",
				 start, size);
			XFREE(MTYPE_TM_CHUNK, tmc);
			return NULL;
		}
	}
	tmc->end = tmc->start + size - 1;
	tmc->proto = proto;
	tmc->instance = instance;
	listnode_add(zvrf->tbl_mgr->lc_list, tmc);

	return tmc;
}

/**
 * Core function, release no longer used table chunks
 *
 * @param proto Daemon protocol of client, to identify the owner
 * @param instance Instance, to identify the owner
 * @param start First table RT ID of the chunk
 * @param end Last table RT ID of the chunk
 * @return 0 on success, -1 otherwise
 */
int release_table_chunk(uint8_t proto, uint16_t instance, uint32_t start,
			uint32_t end, struct zebra_vrf *zvrf)
{
	struct listnode *node;
	struct table_manager_chunk *tmc;
	int ret = -1;
	struct table_manager *tbl_mgr;

	if (!zvrf)
		return -1;

	tbl_mgr = zvrf->tbl_mgr;
	if (!tbl_mgr)
		return ret;
	/* check that size matches */
	zlog_debug("Releasing table chunk: %u - %u", start, end);
	/* find chunk and disown */
	for (ALL_LIST_ELEMENTS_RO(tbl_mgr->lc_list, node, tmc)) {
		if (tmc->start != start)
			continue;
		if (tmc->end != end)
			continue;
		if (tmc->proto != proto || tmc->instance != instance) {
			flog_err(EC_ZEBRA_TM_DAEMON_MISMATCH,
				 "%s: Daemon mismatch!!", __func__);
			continue;
		}
		tmc->proto = NO_PROTO;
		tmc->instance = 0;
		ret = 0;
		break;
	}
	if (ret != 0)
		flog_err(EC_ZEBRA_TM_UNRELEASED_CHUNK,
			 "%s: Table chunk not released!!", __func__);

	return ret;
}

/**
 * Release table chunks from a client.
 *
 * Called on client disconnection or reconnection. It only releases chunks
 * with empty keep value.
 *
 * @param client the client to release chunks from
 * @return Number of chunks released
 */
int release_daemon_table_chunks(struct zserv *client)
{
	uint8_t proto = client->proto;
	uint16_t instance = client->instance;
	struct listnode *node;
	struct table_manager_chunk *tmc;
	int count = 0;
	int ret;
	struct vrf *vrf;
	struct zebra_vrf *zvrf;

	RB_FOREACH (vrf, vrf_name_head, &vrfs_by_name) {
		zvrf = vrf->info;

		if (!zvrf)
			continue;
		if (!vrf_is_backend_netns() && vrf->vrf_id != VRF_DEFAULT)
			continue;
		for (ALL_LIST_ELEMENTS_RO(zvrf->tbl_mgr->lc_list, node, tmc)) {
			if (tmc->proto == proto && tmc->instance == instance) {
				ret = release_table_chunk(
					tmc->proto, tmc->instance, tmc->start,
					tmc->end, zvrf);
				if (ret == 0)
					count++;
			}
		}
	}
	zlog_debug("%s: Released %d table chunks", __func__, count);

	return count;
}

static void table_range_add(struct zebra_vrf *zvrf, uint32_t start,
			    uint32_t end)
{
	if (!zvrf->tbl_mgr)
		return;
	zvrf->tbl_mgr->start = start;
	zvrf->tbl_mgr->end = end;
}

void table_manager_disable(struct zebra_vrf *zvrf)
{
	if (!zvrf->tbl_mgr)
		return;
	if (!vrf_is_backend_netns()
	    && strcmp(zvrf_name(zvrf), VRF_DEFAULT_NAME)) {
		zvrf->tbl_mgr = NULL;
		return;
	}
	list_delete(&zvrf->tbl_mgr->lc_list);
	XFREE(MTYPE_TM_TABLE, zvrf->tbl_mgr);
	zvrf->tbl_mgr = NULL;
}

int table_manager_range(struct vty *vty, bool add, struct zebra_vrf *zvrf,
			const char *start_table_str, const char *end_table_str)
{
	uint32_t start;
	uint32_t end;

	if (add) {
		if (!start_table_str || !end_table_str) {
			vty_out(vty, "%% Labels not specified\n");
			return CMD_WARNING_CONFIG_FAILED;
		}
		start = atoi(start_table_str);
		end = atoi(end_table_str);
		if (end < start) {
			vty_out(vty, "%% End table is less than Start table\n");
			return CMD_WARNING_CONFIG_FAILED;
		}

#if !defined(GNU_LINUX)
/* BSD systems
 */
#else
		/* Linux Systems
		 */
		if ((start >= RT_TABLE_ID_COMPAT && start <= RT_TABLE_ID_LOCAL)
		    || (end >= RT_TABLE_ID_COMPAT
			&& end <= RT_TABLE_ID_LOCAL)) {
			vty_out(vty, "%% Values forbidden in range [%u;%u]\n",
				RT_TABLE_ID_COMPAT, RT_TABLE_ID_LOCAL);
			return CMD_WARNING_CONFIG_FAILED;
		}
		if (start < RT_TABLE_ID_COMPAT && end > RT_TABLE_ID_LOCAL) {
			vty_out(vty,
				"%% Range overlaps range [%u;%u] forbidden\n",
				RT_TABLE_ID_COMPAT, RT_TABLE_ID_LOCAL);
			return CMD_WARNING_CONFIG_FAILED;
		}
#endif
		if (zvrf->tbl_mgr
		    && ((zvrf->tbl_mgr->start && zvrf->tbl_mgr->start != start)
			|| (zvrf->tbl_mgr->end && zvrf->tbl_mgr->end != end))) {
			vty_out(vty,
				"%% New range will be taken into account at restart\n");
		}
		table_range_add(zvrf, start, end);
	} else
		table_range_add(zvrf, 0, 0);
	return CMD_SUCCESS;
}