]> git.proxmox.com Git - mirror_frr.git/blob - tests/ospf6d/test_lsdb.c
Merge pull request #12798 from donaldsharp/rib_match_multicast
[mirror_frr.git] / tests / ospf6d / test_lsdb.c
1 // SPDX-License-Identifier: GPL-2.0-or-later
2 /*
3 * CLI/command dummy handling tester
4 *
5 * Copyright (C) 2015 by David Lamparter,
6 * for Open Source Routing / NetDEF, Inc.
7 */
8
9 #include <zebra.h>
10
11 #include "prefix.h"
12 #include "vector.h"
13 #include "vty.h"
14
15 #include "ospf6d/ospf6_lsa.h"
16 #include "ospf6d/ospf6_lsdb.h"
17
18 #include "tests/lib/cli/common_cli.h"
19 #include "tests/ospf6d/test_lsdb_clippy.c"
20
21 static struct ospf6_lsdb *lsdb;
22
23 static struct ospf6_lsa **lsas = NULL;
24 static size_t lsa_count = 0;
25
26 static void lsa_check_resize(size_t len)
27 {
28 struct ospf6_lsa **templsas;
29
30 if (lsa_count >= len)
31 return;
32 templsas = realloc(lsas, len * sizeof(lsas[0]));
33 if (templsas)
34 lsas = templsas;
35 else
36 return;
37 memset(lsas + lsa_count, 0, sizeof(lsas[0]) * (len - lsa_count));
38
39 lsa_count = len;
40 }
41
42 DEFPY(lsa_set, lsa_set_cmd,
43 "lsa set (0-999999)$idx {type (0-65535)|id A.B.C.D|adv A.B.C.D}",
44 "LSA\n"
45 "set\n"
46 "LSA index in array\n"
47 "OSPF6 type code\n"
48 "OSPF6 type code\n"
49 "LS-ID\n"
50 "LS-ID\n"
51 "Advertising router\n"
52 "Advertising router\n")
53 {
54 struct ospf6_lsa_header hdr;
55 memset(&hdr, 0, sizeof(hdr));
56 hdr.type = htons(type);
57 hdr.id = id.s_addr;
58 hdr.adv_router = adv.s_addr;
59
60 lsa_check_resize(idx + 1);
61 if (lsas[idx])
62 ospf6_lsa_unlock(lsas[idx]);
63 lsas[idx] = ospf6_lsa_create_headeronly(&hdr);
64 ospf6_lsa_lock(lsas[idx]);
65 return CMD_SUCCESS;
66 }
67
68 DEFPY(lsa_drop, lsa_drop_cmd,
69 "lsa drop (0-999999)$idx",
70 "LSA\n"
71 "drop reference\n"
72 "LSA index in array\n")
73 {
74 if ((size_t)idx >= lsa_count)
75 return CMD_SUCCESS;
76 if (lsas[idx]->lock != 1)
77 vty_out(vty, "refcount at %u\n", lsas[idx]->lock);
78 ospf6_lsa_unlock(lsas[idx]);
79 lsas[idx] = NULL;
80 return CMD_SUCCESS;
81 }
82
83
84 DEFPY(lsdb_add, lsdb_add_cmd,
85 "lsdb add (0-999999)$idx",
86 "LSDB\n"
87 "insert LSA into LSDB\n"
88 "LSA index in array\n")
89 {
90 ospf6_lsdb_add(lsas[idx], lsdb);
91 return CMD_SUCCESS;
92 }
93
94 DEFPY(lsdb_remove, lsdb_remove_cmd,
95 "lsdb remove (0-999999)$idx",
96 "LSDB\n"
97 "remove LSA from LSDB\n"
98 "LSA index in array\n")
99 {
100 ospf6_lsdb_remove(lsas[idx], lsdb);
101 return CMD_SUCCESS;
102 }
103
104 static void lsa_show_oneline(struct vty *vty, struct ospf6_lsa *lsa)
105 {
106 char adv_router[64], id[64];
107
108 if (!lsa) {
109 vty_out(vty, "lsa = NULL\n");
110 return;
111 }
112 inet_ntop(AF_INET, &lsa->header->id, id, sizeof(id));
113 inet_ntop(AF_INET, &lsa->header->adv_router, adv_router,
114 sizeof(adv_router));
115 vty_out(vty, "type %u adv %s id %s\n", ntohs(lsa->header->type),
116 adv_router, id);
117 }
118
119 DEFPY(lsdb_walk, lsdb_walk_cmd,
120 "lsdb walk",
121 "LSDB\n"
122 "walk entries\n")
123 {
124 struct ospf6_lsa *lsa, *lsanext;
125
126 unsigned cnt = 0;
127 for (ALL_LSDB(lsdb, lsa, lsanext)) {
128 lsa_show_oneline(vty, lsa);
129 cnt++;
130 }
131 vty_out(vty, "%u entries.\n", cnt);
132 return CMD_SUCCESS;
133 }
134
135 DEFPY(lsdb_walk_type, lsdb_walk_type_cmd,
136 "lsdb walk type (0-65535)",
137 "LSDB\n"
138 "walk entries\n"
139 "entry type\n"
140 "entry type\n")
141 {
142 struct ospf6_lsa *lsa;
143 unsigned cnt = 0;
144 type = htons(type);
145 for (ALL_LSDB_TYPED(lsdb, type, lsa)) {
146 lsa_show_oneline(vty, lsa);
147 cnt++;
148 }
149 vty_out(vty, "%u entries.\n", cnt);
150 return CMD_SUCCESS;
151 }
152
153 DEFPY(lsdb_walk_type_adv, lsdb_walk_type_adv_cmd,
154 "lsdb walk type (0-65535) adv A.B.C.D",
155 "LSDB\n"
156 "walk entries\n"
157 "entry type\n"
158 "entry type\n"
159 "advertising router ID\n"
160 "advertising router ID\n")
161 {
162 struct ospf6_lsa *lsa;
163 unsigned cnt = 0;
164 type = htons(type);
165 for (ALL_LSDB_TYPED_ADVRTR(lsdb, type, adv.s_addr, lsa)) {
166 lsa_show_oneline(vty, lsa);
167 cnt++;
168 }
169 vty_out(vty, "%u entries.\n", cnt);
170 return CMD_SUCCESS;
171 }
172
173 DEFPY(lsdb_get, lsdb_get_cmd,
174 "lsdb <get-next|get> type (0-65535) adv A.B.C.D id A.B.C.D",
175 "LSDB\n"
176 "get entry's successor\n"
177 "entry type\n"
178 "entry type\n"
179 "advertising router ID\n"
180 "advertising router ID\n"
181 "LS-ID\n"
182 "LS-ID\n")
183 {
184 struct ospf6_lsa *lsa;
185 type = htons(type);
186 if (!strcmp(argv[1]->text, "get-next"))
187 lsa = ospf6_lsdb_lookup_next(type, id.s_addr, adv.s_addr, lsdb);
188 else
189 lsa = ospf6_lsdb_lookup(type, id.s_addr, adv.s_addr, lsdb);
190 lsa_show_oneline(vty, lsa);
191 return CMD_SUCCESS;
192 }
193
194 DEFPY(lsa_refcounts, lsa_refcounts_cmd,
195 "lsa refcounts",
196 "LSA\n"
197 "show reference counts\n")
198 {
199 for (size_t i = 0; i < lsa_count; i++)
200 if (lsas[i])
201 vty_out(vty, "[%zu] %u\n", i, lsas[i]->lock);
202 return CMD_SUCCESS;
203 }
204
205 DEFPY(lsdb_create, lsdb_create_cmd,
206 "lsdb create",
207 "LSDB\n"
208 "create LSDB\n")
209 {
210 if (lsdb)
211 ospf6_lsdb_delete(lsdb);
212 lsdb = ospf6_lsdb_create(NULL);
213 return CMD_SUCCESS;
214 }
215
216 DEFPY(lsdb_delete, lsdb_delete_cmd,
217 "lsdb delete",
218 "LSDB\n"
219 "delete LSDB\n")
220 {
221 ospf6_lsdb_delete(lsdb);
222 lsdb = NULL;
223 return CMD_SUCCESS;
224 }
225
226
227 struct zebra_privs_t ospf6d_privs;
228
229 void test_init(int argc, char **argv)
230 {
231 ospf6_lsa_init();
232
233 install_element(ENABLE_NODE, &lsa_set_cmd);
234 install_element(ENABLE_NODE, &lsa_refcounts_cmd);
235 install_element(ENABLE_NODE, &lsa_drop_cmd);
236
237 install_element(ENABLE_NODE, &lsdb_create_cmd);
238 install_element(ENABLE_NODE, &lsdb_delete_cmd);
239
240 install_element(ENABLE_NODE, &lsdb_add_cmd);
241 install_element(ENABLE_NODE, &lsdb_remove_cmd);
242 install_element(ENABLE_NODE, &lsdb_walk_cmd);
243 install_element(ENABLE_NODE, &lsdb_walk_type_cmd);
244 install_element(ENABLE_NODE, &lsdb_walk_type_adv_cmd);
245 install_element(ENABLE_NODE, &lsdb_get_cmd);
246 }