]>
Commit | Line | Data |
---|---|---|
acddc0ed | 1 | // SPDX-License-Identifier: GPL-2.0-or-later |
52a7c25e RW |
2 | /* |
3 | * Copyright (C) 2020 NetDEF, Inc. | |
4 | * Renato Westphal | |
52a7c25e RW |
5 | */ |
6 | ||
7 | #include <zebra.h> | |
8 | ||
9 | #include "isisd/isisd.h" | |
10 | #include "isisd/isis_dynhn.h" | |
11 | #include "isisd/isis_mt.h" | |
12 | ||
13 | #include "test_common.h" | |
14 | ||
cd9d0537 | 15 | struct event_loop *master; |
52a7c25e RW |
16 | struct zebra_privs_t isisd_privs; |
17 | ||
18 | int isis_sock_init(struct isis_circuit *circuit) | |
19 | { | |
20 | return 0; | |
21 | } | |
22 | ||
23 | const struct isis_test_node * | |
24 | test_topology_find_node(const struct isis_topology *topology, | |
25 | const char *hostname, uint8_t pseudonode_id) | |
26 | { | |
27 | for (size_t i = 0; topology->nodes[i].hostname[0]; i++) | |
28 | if (strmatch(hostname, topology->nodes[i].hostname) | |
29 | && pseudonode_id == topology->nodes[i].pseudonode_id) | |
30 | return &topology->nodes[i]; | |
31 | ||
32 | return NULL; | |
33 | } | |
34 | ||
35 | const struct isis_topology * | |
36 | test_topology_find(struct isis_topology *test_topologies, uint16_t number) | |
37 | { | |
38 | for (size_t i = 0; test_topologies[i].number; i++) | |
39 | if (test_topologies[i].number == number) | |
40 | return &test_topologies[i]; | |
41 | ||
42 | return NULL; | |
43 | } | |
44 | ||
45 | static const struct isis_test_node * | |
46 | test_find_adjacency(const struct isis_test_node *tnode, const char *hostname) | |
47 | { | |
48 | for (size_t i = 0; tnode->adjacencies[i].hostname[0]; i++) { | |
49 | const struct isis_test_adj *tadj; | |
50 | ||
51 | tadj = &tnode->adjacencies[i]; | |
52 | if (strmatch(hostname, tadj->hostname)) | |
53 | return tnode; | |
54 | } | |
55 | ||
56 | return NULL; | |
57 | } | |
58 | ||
c40de294 RW |
59 | mpls_label_t test_topology_node_ldp_label(const struct isis_topology *topology, |
60 | struct in_addr router_id) | |
61 | { | |
62 | for (size_t i = 0; topology->nodes[i].hostname[0]; i++) { | |
63 | const struct isis_test_node *tnode = &topology->nodes[i]; | |
64 | struct in_addr node_router_id; | |
65 | ||
66 | if (!tnode->router_id) | |
67 | continue; | |
68 | ||
69 | (void)inet_pton(AF_INET, tnode->router_id, &node_router_id); | |
70 | if (IPV4_ADDR_SAME(&router_id, &node_router_id)) | |
71 | return (50000 + (i + 1) * 100); | |
72 | } | |
73 | ||
74 | return MPLS_INVALID_LABEL; | |
75 | } | |
76 | ||
52a7c25e RW |
77 | static struct isis_lsp *lsp_add(struct lspdb_head *lspdb, |
78 | struct isis_area *area, int level, | |
79 | const uint8_t *sysid, uint8_t pseudonode_id) | |
80 | { | |
81 | struct isis_lsp *lsp; | |
82 | uint8_t lspid[ISIS_SYS_ID_LEN + 2]; | |
83 | ||
84 | memcpy(lspid, sysid, ISIS_SYS_ID_LEN); | |
85 | LSP_PSEUDO_ID(lspid) = pseudonode_id; | |
86 | LSP_FRAGMENT(lspid) = 0; | |
87 | ||
88 | lsp = lsp_new(area, lspid, 6000, 1, 0, 0, NULL, level); | |
89 | lsp->tlvs = isis_alloc_tlvs(); | |
90 | lspdb_add(lspdb, lsp); | |
91 | ||
92 | return lsp; | |
93 | } | |
94 | ||
95 | static void lsp_add_ip_reach(struct isis_lsp *lsp, | |
96 | const struct isis_test_node *tnode, | |
97 | const char *prefix_str, uint32_t *next_sid_index) | |
98 | { | |
99 | struct prefix prefix; | |
100 | struct sr_prefix_cfg pcfg = {}; | |
0de7b433 | 101 | struct sr_prefix_cfg *pcfg_p[SR_ALGORITHM_COUNT] = {NULL}; |
52a7c25e RW |
102 | |
103 | if (str2prefix(prefix_str, &prefix) != 1) { | |
104 | zlog_debug("%s: invalid network: %s", __func__, prefix_str); | |
105 | return; | |
106 | } | |
107 | ||
108 | if (CHECK_FLAG(tnode->flags, F_ISIS_TEST_NODE_SR)) { | |
0de7b433 | 109 | pcfg_p[SR_ALGORITHM_SPF] = &pcfg; |
52a7c25e RW |
110 | |
111 | pcfg.sid = *next_sid_index; | |
112 | *next_sid_index = *next_sid_index + 1; | |
113 | pcfg.sid_type = SR_SID_VALUE_TYPE_INDEX; | |
ca74d663 | 114 | pcfg.node_sid = true; |
52a7c25e RW |
115 | pcfg.last_hop_behavior = SR_LAST_HOP_BEHAVIOR_PHP; |
116 | } | |
117 | ||
118 | if (prefix.family == AF_INET) | |
119 | isis_tlvs_add_extended_ip_reach(lsp->tlvs, | |
120 | (struct prefix_ipv4 *)&prefix, | |
121 | 10, false, pcfg_p); | |
122 | else | |
123 | isis_tlvs_add_ipv6_reach(lsp->tlvs, ISIS_MT_IPV6_UNICAST, | |
124 | (struct prefix_ipv6 *)&prefix, 10, | |
125 | false, pcfg_p); | |
126 | } | |
127 | ||
128 | static void lsp_add_reach(struct isis_lsp *lsp, | |
129 | const struct isis_test_node *tnode, | |
130 | const uint8_t *ne_id, uint8_t pseudonode_id, | |
131 | uint32_t metric, int family, mpls_label_t *next_label) | |
132 | { | |
133 | uint8_t nodeid[ISIS_SYS_ID_LEN + 1]; | |
134 | uint16_t mtid; | |
135 | struct isis_ext_subtlvs *ext = NULL; | |
136 | ||
137 | memcpy(nodeid, ne_id, ISIS_SYS_ID_LEN); | |
138 | LSP_PSEUDO_ID(nodeid) = pseudonode_id; | |
139 | ||
140 | if (CHECK_FLAG(tnode->flags, F_ISIS_TEST_NODE_SR)) { | |
141 | struct isis_adj_sid *adj_sid; | |
142 | ||
143 | adj_sid = XCALLOC(MTYPE_ISIS_SUBTLV, sizeof(*adj_sid)); | |
144 | adj_sid->family = family; | |
145 | SET_FLAG(adj_sid->flags, EXT_SUBTLV_LINK_ADJ_SID_VFLG); | |
146 | SET_FLAG(adj_sid->flags, EXT_SUBTLV_LINK_ADJ_SID_LFLG); | |
147 | if (family == AF_INET6) | |
148 | SET_FLAG(adj_sid->flags, EXT_SUBTLV_LINK_ADJ_SID_FFLG); | |
149 | adj_sid->weight = 0; | |
150 | adj_sid->sid = *next_label; | |
151 | *next_label = *next_label + 1; | |
152 | ||
153 | ext = isis_alloc_ext_subtlvs(); | |
154 | isis_tlvs_add_adj_sid(ext, adj_sid); | |
155 | } | |
156 | ||
157 | mtid = (family == AF_INET) ? ISIS_MT_IPV4_UNICAST | |
158 | : ISIS_MT_IPV6_UNICAST; | |
159 | ||
160 | isis_tlvs_add_extended_reach(lsp->tlvs, mtid, nodeid, metric, ext); | |
161 | } | |
162 | ||
163 | static void lsp_add_router_capability(struct isis_lsp *lsp, | |
164 | const struct isis_test_node *tnode) | |
165 | { | |
78774bbc | 166 | struct isis_router_cap *cap; |
52a7c25e RW |
167 | |
168 | if (!tnode->router_id) | |
169 | return; | |
170 | ||
78774bbc HS |
171 | cap = isis_tlvs_init_router_capability(lsp->tlvs); |
172 | ||
173 | if (inet_pton(AF_INET, tnode->router_id, &cap->router_id) != 1) { | |
52a7c25e RW |
174 | zlog_debug("%s: invalid router-id: %s", __func__, |
175 | tnode->router_id); | |
176 | return; | |
177 | } | |
178 | ||
179 | if (CHECK_FLAG(tnode->flags, F_ISIS_TEST_NODE_SR)) { | |
78774bbc | 180 | cap->srgb.flags = |
52a7c25e | 181 | ISIS_SUBTLV_SRGB_FLAG_I | ISIS_SUBTLV_SRGB_FLAG_V; |
78774bbc HS |
182 | cap->srgb.lower_bound = tnode->srgb.lower_bound |
183 | ? tnode->srgb.lower_bound | |
184 | : SRGB_DFTL_LOWER_BOUND; | |
185 | cap->srgb.range_size = tnode->srgb.range_size | |
186 | ? tnode->srgb.range_size | |
187 | : SRGB_DFTL_RANGE_SIZE; | |
188 | cap->algo[0] = SR_ALGORITHM_SPF; | |
189 | cap->algo[1] = SR_ALGORITHM_UNSET; | |
52a7c25e RW |
190 | } |
191 | ||
52a7c25e RW |
192 | } |
193 | ||
194 | static void lsp_add_mt_router_info(struct isis_lsp *lsp, | |
195 | const struct isis_test_node *tnode) | |
196 | { | |
197 | if (tnode->protocols.ipv4) | |
198 | isis_tlvs_add_mt_router_info(lsp->tlvs, ISIS_MT_IPV4_UNICAST, 0, | |
199 | false); | |
200 | if (tnode->protocols.ipv6) | |
201 | isis_tlvs_add_mt_router_info(lsp->tlvs, ISIS_MT_IPV6_UNICAST, 0, | |
202 | false); | |
203 | } | |
204 | ||
205 | static void lsp_add_protocols_supported(struct isis_lsp *lsp, | |
206 | const struct isis_test_node *tnode) | |
207 | { | |
208 | struct nlpids nlpids = {}; | |
209 | ||
210 | if (!tnode->protocols.ipv4 && !tnode->protocols.ipv6) | |
211 | return; | |
212 | ||
213 | if (tnode->protocols.ipv4) { | |
214 | nlpids.nlpids[nlpids.count] = NLPID_IP; | |
215 | nlpids.count++; | |
216 | } | |
217 | if (tnode->protocols.ipv6) { | |
218 | nlpids.nlpids[nlpids.count] = NLPID_IPV6; | |
219 | nlpids.count++; | |
220 | } | |
221 | isis_tlvs_set_protocols_supported(lsp->tlvs, &nlpids); | |
222 | } | |
223 | ||
224 | static int topology_load_node_level(const struct isis_topology *topology, | |
225 | const struct isis_test_node *tnode, | |
226 | size_t tnode_index, struct isis_area *area, | |
227 | struct lspdb_head *lspdb, int level) | |
228 | { | |
229 | struct isis_lsp *lsp; | |
230 | uint32_t next_sid_index = (tnode_index + 1) * 10; | |
231 | mpls_label_t next_label = 16; | |
232 | ||
233 | lsp = lsp_add(lspdb, area, level, tnode->sysid, tnode->pseudonode_id); | |
234 | lsp_add_mt_router_info(lsp, tnode); | |
235 | lsp_add_protocols_supported(lsp, tnode); | |
236 | lsp_add_router_capability(lsp, tnode); | |
237 | ||
238 | /* Add IP Reachability Information. */ | |
239 | for (size_t i = 0; tnode->networks[i]; i++) { | |
240 | if (i > MAX_NETWORKS) { | |
241 | zlog_debug( | |
242 | "%s: node has too many networks (maximum is %u)", | |
243 | __func__, MAX_NETWORKS); | |
244 | return -1; | |
245 | } | |
246 | lsp_add_ip_reach(lsp, tnode, tnode->networks[i], | |
247 | &next_sid_index); | |
248 | } | |
249 | ||
250 | /* Add IS Reachability Information. */ | |
251 | for (size_t i = 0; tnode->adjacencies[i].hostname[0]; i++) { | |
252 | const struct isis_test_adj *tadj; | |
253 | const struct isis_test_node *tadj_node; | |
254 | ||
255 | if (i > MAX_ADJACENCIES) { | |
256 | zlog_debug( | |
257 | "%s: node has too many adjacencies (maximum is %u)", | |
258 | __func__, MAX_ADJACENCIES); | |
259 | return -1; | |
260 | } | |
261 | ||
262 | tadj = &tnode->adjacencies[i]; | |
263 | tadj_node = test_topology_find_node(topology, tadj->hostname, | |
264 | tadj->pseudonode_id); | |
265 | if (!tadj_node) { | |
266 | zlog_debug( | |
267 | "%s: node \"%s\" has an adjacency with non-existing node \"%s\"", | |
268 | __func__, tnode->hostname, tadj->hostname); | |
269 | return -1; | |
270 | } | |
271 | if (!test_find_adjacency(tadj_node, tnode->hostname)) { | |
272 | zlog_debug( | |
273 | "%s: node \"%s\" has an one-way adjacency with node \"%s\"", | |
274 | __func__, tnode->hostname, tadj->hostname); | |
275 | return -1; | |
276 | } | |
277 | ||
278 | if (tnode->pseudonode_id || tadj_node->pseudonode_id | |
279 | || (tnode->protocols.ipv4 && tadj_node->protocols.ipv4)) | |
280 | lsp_add_reach(lsp, tnode, tadj_node->sysid, | |
281 | tadj_node->pseudonode_id, tadj->metric, | |
282 | AF_INET, &next_label); | |
283 | if (tadj_node->pseudonode_id | |
284 | || (tnode->protocols.ipv6 && tadj_node->protocols.ipv6)) | |
285 | lsp_add_reach(lsp, tnode, tadj_node->sysid, | |
286 | tadj_node->pseudonode_id, tadj->metric, | |
287 | AF_INET6, &next_label); | |
288 | } | |
289 | ||
290 | return 0; | |
291 | } | |
292 | ||
293 | static int topology_load_node(const struct isis_topology *topology, | |
294 | const struct isis_test_node *tnode, | |
295 | size_t tnode_index, struct isis_area *area, | |
296 | struct lspdb_head lspdb[]) | |
297 | { | |
298 | int ret; | |
299 | ||
240f48b3 IR |
300 | isis_dynhn_insert(area->isis, tnode->sysid, tnode->hostname, |
301 | tnode->level); | |
52a7c25e RW |
302 | |
303 | for (int level = IS_LEVEL_1; level <= IS_LEVEL_2; level++) { | |
304 | if ((tnode->level & level) == 0) | |
305 | continue; | |
306 | ||
307 | ret = topology_load_node_level(topology, tnode, tnode_index, | |
308 | area, &lspdb[level - 1], level); | |
309 | if (ret != 0) | |
310 | return ret; | |
311 | } | |
312 | ||
313 | return 0; | |
314 | } | |
315 | ||
316 | int test_topology_load(const struct isis_topology *topology, | |
317 | struct isis_area *area, struct lspdb_head lspdb[]) | |
318 | { | |
319 | for (int level = IS_LEVEL_1; level <= IS_LEVEL_2; level++) | |
320 | lsp_db_init(&lspdb[level - 1]); | |
321 | ||
322 | for (size_t i = 0; topology->nodes[i].hostname[0]; i++) { | |
323 | const struct isis_test_node *tnode = &topology->nodes[i]; | |
324 | int ret; | |
325 | ||
326 | if (i > MAX_NODES) { | |
327 | zlog_debug( | |
328 | "%s: topology has too many nodes (maximum is %u)", | |
329 | __func__, MAX_NODES); | |
330 | return -1; | |
331 | } | |
332 | ||
333 | ret = topology_load_node(topology, tnode, i, area, lspdb); | |
334 | if (ret != 0) | |
335 | return ret; | |
336 | } | |
337 | ||
338 | return 0; | |
339 | } |