summaryrefslogtreecommitdiffstats
path: root/tests/isisd/test_common.c
blob: 5fa604c7499c2be8ff90f8d6d23d1fffc32a2212 (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
331
332
/*
 * Copyright (C) 2020  NetDEF, Inc.
 *                     Renato Westphal
 *
 * This program is free software; you can redistribute it and/or modify it
 * under the terms of the GNU General Public License as published by the Free
 * Software Foundation; either version 2 of the License, or (at your option)
 * any later version.
 *
 * This program is distributed in the hope that it will be useful, but WITHOUT
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License for
 * more details.
 *
 * You should have received a copy of the GNU General Public License along
 * with this program; see the file COPYING; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
 */

#include <zebra.h>

#include "isisd/isisd.h"
#include "isisd/isis_dynhn.h"
#include "isisd/isis_mt.h"

#include "test_common.h"

struct thread_master *master;
struct zebra_privs_t isisd_privs;

int isis_sock_init(struct isis_circuit *circuit)
{
	return 0;
}

const struct isis_test_node *
test_topology_find_node(const struct isis_topology *topology,
			const char *hostname, uint8_t pseudonode_id)
{
	for (size_t i = 0; topology->nodes[i].hostname[0]; i++)
		if (strmatch(hostname, topology->nodes[i].hostname)
		    && pseudonode_id == topology->nodes[i].pseudonode_id)
			return &topology->nodes[i];

	return NULL;
}

const struct isis_topology *
test_topology_find(struct isis_topology *test_topologies, uint16_t number)
{
	for (size_t i = 0; test_topologies[i].number; i++)
		if (test_topologies[i].number == number)
			return &test_topologies[i];

	return NULL;
}

static const struct isis_test_node *
test_find_adjacency(const struct isis_test_node *tnode, const char *hostname)
{
	for (size_t i = 0; tnode->adjacencies[i].hostname[0]; i++) {
		const struct isis_test_adj *tadj;

		tadj = &tnode->adjacencies[i];
		if (strmatch(hostname, tadj->hostname))
			return tnode;
	}

	return NULL;
}

static struct isis_lsp *lsp_add(struct lspdb_head *lspdb,
				struct isis_area *area, int level,
				const uint8_t *sysid, uint8_t pseudonode_id)
{
	struct isis_lsp *lsp;
	uint8_t lspid[ISIS_SYS_ID_LEN + 2];

	memcpy(lspid, sysid, ISIS_SYS_ID_LEN);
	LSP_PSEUDO_ID(lspid) = pseudonode_id;
	LSP_FRAGMENT(lspid) = 0;

	lsp = lsp_new(area, lspid, 6000, 1, 0, 0, NULL, level);
	lsp->tlvs = isis_alloc_tlvs();
	lspdb_add(lspdb, lsp);

	return lsp;
}

static void lsp_add_ip_reach(struct isis_lsp *lsp,
			     const struct isis_test_node *tnode,
			     const char *prefix_str, uint32_t *next_sid_index)
{
	struct prefix prefix;
	struct sr_prefix_cfg pcfg = {};
	struct sr_prefix_cfg *pcfg_p = NULL;

	if (str2prefix(prefix_str, &prefix) != 1) {
		zlog_debug("%s: invalid network: %s", __func__, prefix_str);
		return;
	}

	if (CHECK_FLAG(tnode->flags, F_ISIS_TEST_NODE_SR)) {
		pcfg_p = &pcfg;

		pcfg.sid = *next_sid_index;
		*next_sid_index = *next_sid_index + 1;
		pcfg.sid_type = SR_SID_VALUE_TYPE_INDEX;
		pcfg.node_sid = true;
		pcfg.last_hop_behavior = SR_LAST_HOP_BEHAVIOR_PHP;
	}

	if (prefix.family == AF_INET)
		isis_tlvs_add_extended_ip_reach(lsp->tlvs,
						(struct prefix_ipv4 *)&prefix,
						10, false, pcfg_p);
	else
		isis_tlvs_add_ipv6_reach(lsp->tlvs, ISIS_MT_IPV6_UNICAST,
					 (struct prefix_ipv6 *)&prefix, 10,
					 false, pcfg_p);
}

static void lsp_add_reach(struct isis_lsp *lsp,
			  const struct isis_test_node *tnode,
			  const uint8_t *ne_id, uint8_t pseudonode_id,
			  uint32_t metric, int family, mpls_label_t *next_label)
{
	uint8_t nodeid[ISIS_SYS_ID_LEN + 1];
	uint16_t mtid;
	struct isis_ext_subtlvs *ext = NULL;

	memcpy(nodeid, ne_id, ISIS_SYS_ID_LEN);
	LSP_PSEUDO_ID(nodeid) = pseudonode_id;

	if (CHECK_FLAG(tnode->flags, F_ISIS_TEST_NODE_SR)) {
		struct isis_adj_sid *adj_sid;

		adj_sid = XCALLOC(MTYPE_ISIS_SUBTLV, sizeof(*adj_sid));
		adj_sid->family = family;
		SET_FLAG(adj_sid->flags, EXT_SUBTLV_LINK_ADJ_SID_VFLG);
		SET_FLAG(adj_sid->flags, EXT_SUBTLV_LINK_ADJ_SID_LFLG);
		if (family == AF_INET6)
			SET_FLAG(adj_sid->flags, EXT_SUBTLV_LINK_ADJ_SID_FFLG);
		adj_sid->weight = 0;
		adj_sid->sid = *next_label;
		*next_label = *next_label + 1;

		ext = isis_alloc_ext_subtlvs();
		isis_tlvs_add_adj_sid(ext, adj_sid);
	}

	mtid = (family == AF_INET) ? ISIS_MT_IPV4_UNICAST
				   : ISIS_MT_IPV6_UNICAST;

	isis_tlvs_add_extended_reach(lsp->tlvs, mtid, nodeid, metric, ext);
}

static void lsp_add_router_capability(struct isis_lsp *lsp,
				      const struct isis_test_node *tnode)
{
	struct isis_router_cap cap = {};

	if (!tnode->router_id)
		return;

	if (inet_pton(AF_INET, tnode->router_id, &cap.router_id) != 1) {
		zlog_debug("%s: invalid router-id: %s", __func__,
			   tnode->router_id);
		return;
	}

	if (CHECK_FLAG(tnode->flags, F_ISIS_TEST_NODE_SR)) {
		cap.srgb.flags =
			ISIS_SUBTLV_SRGB_FLAG_I | ISIS_SUBTLV_SRGB_FLAG_V;
		cap.srgb.lower_bound = tnode->srgb.lower_bound
					       ? tnode->srgb.lower_bound
					       : SRGB_DFTL_LOWER_BOUND;
		cap.srgb.range_size = tnode->srgb.range_size
					      ? tnode->srgb.range_size
					      : SRGB_DFTL_RANGE_SIZE;
		cap.algo[0] = SR_ALGORITHM_SPF;
		cap.algo[1] = SR_ALGORITHM_UNSET;
	}

	isis_tlvs_set_router_capability(lsp->tlvs, &cap);
}

static void lsp_add_mt_router_info(struct isis_lsp *lsp,
				   const struct isis_test_node *tnode)
{
	if (tnode->protocols.ipv4)
		isis_tlvs_add_mt_router_info(lsp->tlvs, ISIS_MT_IPV4_UNICAST, 0,
					     false);
	if (tnode->protocols.ipv6)
		isis_tlvs_add_mt_router_info(lsp->tlvs, ISIS_MT_IPV6_UNICAST, 0,
					     false);
}

static void lsp_add_protocols_supported(struct isis_lsp *lsp,
					const struct isis_test_node *tnode)
{
	struct nlpids nlpids = {};

	if (!tnode->protocols.ipv4 && !tnode->protocols.ipv6)
		return;

	if (tnode->protocols.ipv4) {
		nlpids.nlpids[nlpids.count] = NLPID_IP;
		nlpids.count++;
	}
	if (tnode->protocols.ipv6) {
		nlpids.nlpids[nlpids.count] = NLPID_IPV6;
		nlpids.count++;
	}
	isis_tlvs_set_protocols_supported(lsp->tlvs, &nlpids);
}

static int topology_load_node_level(const struct isis_topology *topology,
				    const struct isis_test_node *tnode,
				    size_t tnode_index, struct isis_area *area,
				    struct lspdb_head *lspdb, int level)
{
	struct isis_lsp *lsp;
	uint32_t next_sid_index = (tnode_index + 1) * 10;
	mpls_label_t next_label = 16;

	lsp = lsp_add(lspdb, area, level, tnode->sysid, tnode->pseudonode_id);
	lsp_add_mt_router_info(lsp, tnode);
	lsp_add_protocols_supported(lsp, tnode);
	lsp_add_router_capability(lsp, tnode);

	/* Add IP Reachability Information. */
	for (size_t i = 0; tnode->networks[i]; i++) {
		if (i > MAX_NETWORKS) {
			zlog_debug(
				"%s: node has too many networks (maximum is %u)",
				__func__, MAX_NETWORKS);
			return -1;
		}
		lsp_add_ip_reach(lsp, tnode, tnode->networks[i],
				 &next_sid_index);
	}

	/* Add IS Reachability Information. */
	for (size_t i = 0; tnode->adjacencies[i].hostname[0]; i++) {
		const struct isis_test_adj *tadj;
		const struct isis_test_node *tadj_node;

		if (i > MAX_ADJACENCIES) {
			zlog_debug(
				"%s: node has too many adjacencies (maximum is %u)",
				__func__, MAX_ADJACENCIES);
			return -1;
		}

		tadj = &tnode->adjacencies[i];
		tadj_node = test_topology_find_node(topology, tadj->hostname,
						    tadj->pseudonode_id);
		if (!tadj_node) {
			zlog_debug(
				"%s: node \"%s\" has an adjacency with non-existing node \"%s\"",
				__func__, tnode->hostname, tadj->hostname);
			return -1;
		}
		if (!test_find_adjacency(tadj_node, tnode->hostname)) {
			zlog_debug(
				"%s: node \"%s\" has an one-way adjacency with node \"%s\"",
				__func__, tnode->hostname, tadj->hostname);
			return -1;
		}

		if (tnode->pseudonode_id || tadj_node->pseudonode_id
		    || (tnode->protocols.ipv4 && tadj_node->protocols.ipv4))
			lsp_add_reach(lsp, tnode, tadj_node->sysid,
				      tadj_node->pseudonode_id, tadj->metric,
				      AF_INET, &next_label);
		if (tadj_node->pseudonode_id
		    || (tnode->protocols.ipv6 && tadj_node->protocols.ipv6))
			lsp_add_reach(lsp, tnode, tadj_node->sysid,
				      tadj_node->pseudonode_id, tadj->metric,
				      AF_INET6, &next_label);
	}

	return 0;
}

static int topology_load_node(const struct isis_topology *topology,
			      const struct isis_test_node *tnode,
			      size_t tnode_index, struct isis_area *area,
			      struct lspdb_head lspdb[])
{
	int ret;

	isis_dynhn_insert(tnode->sysid, tnode->hostname, tnode->level);

	for (int level = IS_LEVEL_1; level <= IS_LEVEL_2; level++) {
		if ((tnode->level & level) == 0)
			continue;

		ret = topology_load_node_level(topology, tnode, tnode_index,
					       area, &lspdb[level - 1], level);
		if (ret != 0)
			return ret;
	}

	return 0;
}

int test_topology_load(const struct isis_topology *topology,
		       struct isis_area *area, struct lspdb_head lspdb[])
{
	for (int level = IS_LEVEL_1; level <= IS_LEVEL_2; level++)
		lsp_db_init(&lspdb[level - 1]);

	for (size_t i = 0; topology->nodes[i].hostname[0]; i++) {
		const struct isis_test_node *tnode = &topology->nodes[i];
		int ret;

		if (i > MAX_NODES) {
			zlog_debug(
				"%s: topology has too many nodes (maximum is %u)",
				__func__, MAX_NODES);
			return -1;
		}

		ret = topology_load_node(topology, tnode, i, area, lspdb);
		if (ret != 0)
			return ret;
	}

	return 0;
}