]>
Commit | Line | Data |
---|---|---|
f1c73d14 DL |
1 | /* |
2 | * CLI/command dummy handling tester | |
3 | * | |
4 | * Copyright (C) 2015 by David Lamparter, | |
5 | * for Open Source Routing / NetDEF, Inc. | |
6 | * | |
7 | * Quagga is free software; you can redistribute it and/or modify it | |
8 | * under the terms of the GNU General Public License as published by the | |
9 | * Free Software Foundation; either version 2, or (at your option) any | |
10 | * later version. | |
11 | * | |
12 | * Quagga is distributed in the hope that it will be useful, but | |
13 | * WITHOUT ANY WARRANTY; without even the implied warranty of | |
14 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU | |
15 | * General Public License for more details. | |
16 | * | |
17 | * You should have received a copy of the GNU General Public License along | |
18 | * with this program; see the file COPYING; if not, write to the Free Software | |
19 | * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA | |
20 | */ | |
21 | ||
22 | #include <zebra.h> | |
23 | ||
24 | #include "prefix.h" | |
25 | #include "vector.h" | |
26 | #include "vty.h" | |
27 | ||
28 | #include "ospf6d/ospf6_lsa.h" | |
29 | #include "ospf6d/ospf6_lsdb.h" | |
30 | ||
31 | #include "tests/lib/cli/common_cli.h" | |
a1286a32 | 32 | #include "tests/ospf6d/test_lsdb_clippy.c" |
f1c73d14 DL |
33 | |
34 | static struct ospf6_lsdb *lsdb; | |
35 | ||
36 | static struct ospf6_lsa **lsas = NULL; | |
37 | static size_t lsa_count = 0; | |
38 | ||
39 | static void lsa_check_resize(size_t len) | |
40 | { | |
0e9f8d2d QY |
41 | struct ospf6_lsa **templsas; |
42 | ||
f1c73d14 DL |
43 | if (lsa_count >= len) |
44 | return; | |
0e9f8d2d QY |
45 | templsas = realloc(lsas, len * sizeof(lsas[0])); |
46 | if (templsas) | |
47 | lsas = templsas; | |
48 | else | |
49 | return; | |
f1c73d14 DL |
50 | memset(lsas + lsa_count, 0, sizeof(lsas[0]) * (len - lsa_count)); |
51 | ||
52 | lsa_count = len; | |
53 | } | |
54 | ||
55 | DEFPY(lsa_set, lsa_set_cmd, | |
56 | "lsa set (0-999999)$idx {type (0-65535)|id A.B.C.D|adv A.B.C.D}", | |
57 | "LSA\n" | |
58 | "set\n" | |
59 | "LSA index in array\n" | |
60 | "OSPF6 type code\n" | |
61 | "OSPF6 type code\n" | |
62 | "LS-ID\n" | |
63 | "LS-ID\n" | |
64 | "Advertising router\n" | |
65 | "Advertising router\n") | |
66 | { | |
67 | struct ospf6_lsa_header hdr; | |
68 | memset(&hdr, 0, sizeof(hdr)); | |
69 | hdr.type = htons(type); | |
70 | hdr.id = id.s_addr; | |
71 | hdr.adv_router = adv.s_addr; | |
72 | ||
73 | lsa_check_resize(idx + 1); | |
74 | if (lsas[idx]) | |
75 | ospf6_lsa_unlock(lsas[idx]); | |
76 | lsas[idx] = ospf6_lsa_create_headeronly(&hdr); | |
77 | ospf6_lsa_lock(lsas[idx]); | |
78 | return CMD_SUCCESS; | |
79 | } | |
80 | ||
81 | DEFPY(lsa_drop, lsa_drop_cmd, | |
82 | "lsa drop (0-999999)$idx", | |
83 | "LSA\n" | |
84 | "drop reference\n" | |
85 | "LSA index in array\n") | |
86 | { | |
87 | if ((size_t)idx >= lsa_count) | |
88 | return CMD_SUCCESS; | |
89 | if (lsas[idx]->lock != 1) | |
53a37141 | 90 | vty_out(vty, "refcount at %u\n", lsas[idx]->lock); |
f1c73d14 DL |
91 | ospf6_lsa_unlock(lsas[idx]); |
92 | lsas[idx] = NULL; | |
93 | return CMD_SUCCESS; | |
94 | } | |
95 | ||
96 | ||
97 | DEFPY(lsdb_add, lsdb_add_cmd, | |
98 | "lsdb add (0-999999)$idx", | |
99 | "LSDB\n" | |
100 | "insert LSA into LSDB\n" | |
101 | "LSA index in array\n") | |
102 | { | |
103 | ospf6_lsdb_add(lsas[idx], lsdb); | |
104 | return CMD_SUCCESS; | |
105 | } | |
106 | ||
107 | DEFPY(lsdb_remove, lsdb_remove_cmd, | |
108 | "lsdb remove (0-999999)$idx", | |
109 | "LSDB\n" | |
110 | "remove LSA from LSDB\n" | |
111 | "LSA index in array\n") | |
112 | { | |
113 | ospf6_lsdb_remove(lsas[idx], lsdb); | |
114 | return CMD_SUCCESS; | |
115 | } | |
116 | ||
117 | static void lsa_show_oneline(struct vty *vty, struct ospf6_lsa *lsa) | |
118 | { | |
119 | char adv_router[64], id[64]; | |
120 | ||
121 | if (!lsa) { | |
53a37141 | 122 | vty_out(vty, "lsa = NULL\n"); |
f1c73d14 DL |
123 | return; |
124 | } | |
d62a17ae | 125 | inet_ntop(AF_INET, &lsa->header->id, id, sizeof(id)); |
126 | inet_ntop(AF_INET, &lsa->header->adv_router, adv_router, | |
127 | sizeof(adv_router)); | |
128 | vty_out(vty, "type %u adv %s id %s\n", ntohs(lsa->header->type), | |
129 | adv_router, id); | |
f1c73d14 DL |
130 | } |
131 | ||
132 | DEFPY(lsdb_walk, lsdb_walk_cmd, | |
133 | "lsdb walk", | |
134 | "LSDB\n" | |
135 | "walk entries\n") | |
136 | { | |
2e37407f DS |
137 | struct ospf6_lsa *lsa, *lsanext; |
138 | ||
f1c73d14 | 139 | unsigned cnt = 0; |
2e37407f | 140 | for (ALL_LSDB(lsdb, lsa, lsanext)) { |
f1c73d14 DL |
141 | lsa_show_oneline(vty, lsa); |
142 | cnt++; | |
143 | } | |
53a37141 | 144 | vty_out(vty, "%u entries.\n", cnt); |
f1c73d14 DL |
145 | return CMD_SUCCESS; |
146 | } | |
147 | ||
148 | DEFPY(lsdb_walk_type, lsdb_walk_type_cmd, | |
149 | "lsdb walk type (0-65535)", | |
150 | "LSDB\n" | |
151 | "walk entries\n" | |
152 | "entry type\n" | |
153 | "entry type\n") | |
154 | { | |
155 | struct ospf6_lsa *lsa; | |
156 | unsigned cnt = 0; | |
157 | type = htons(type); | |
158 | for (ALL_LSDB_TYPED(lsdb, type, lsa)) { | |
159 | lsa_show_oneline(vty, lsa); | |
160 | cnt++; | |
161 | } | |
53a37141 | 162 | vty_out(vty, "%u entries.\n", cnt); |
f1c73d14 DL |
163 | return CMD_SUCCESS; |
164 | } | |
165 | ||
166 | DEFPY(lsdb_walk_type_adv, lsdb_walk_type_adv_cmd, | |
167 | "lsdb walk type (0-65535) adv A.B.C.D", | |
168 | "LSDB\n" | |
169 | "walk entries\n" | |
170 | "entry type\n" | |
171 | "entry type\n" | |
172 | "advertising router ID\n" | |
173 | "advertising router ID\n") | |
174 | { | |
175 | struct ospf6_lsa *lsa; | |
176 | unsigned cnt = 0; | |
177 | type = htons(type); | |
178 | for (ALL_LSDB_TYPED_ADVRTR(lsdb, type, adv.s_addr, lsa)) { | |
179 | lsa_show_oneline(vty, lsa); | |
180 | cnt++; | |
181 | } | |
53a37141 | 182 | vty_out(vty, "%u entries.\n", cnt); |
f1c73d14 DL |
183 | return CMD_SUCCESS; |
184 | } | |
185 | ||
186 | DEFPY(lsdb_get, lsdb_get_cmd, | |
187 | "lsdb <get-next|get> type (0-65535) adv A.B.C.D id A.B.C.D", | |
188 | "LSDB\n" | |
189 | "get entry's successor\n" | |
190 | "entry type\n" | |
191 | "entry type\n" | |
192 | "advertising router ID\n" | |
193 | "advertising router ID\n" | |
194 | "LS-ID\n" | |
195 | "LS-ID\n") | |
196 | { | |
197 | struct ospf6_lsa *lsa; | |
198 | type = htons(type); | |
199 | if (!strcmp(argv[1]->text, "get-next")) | |
200 | lsa = ospf6_lsdb_lookup_next(type, id.s_addr, adv.s_addr, lsdb); | |
201 | else | |
202 | lsa = ospf6_lsdb_lookup(type, id.s_addr, adv.s_addr, lsdb); | |
203 | lsa_show_oneline(vty, lsa); | |
204 | return CMD_SUCCESS; | |
205 | } | |
206 | ||
207 | DEFPY(lsa_refcounts, lsa_refcounts_cmd, | |
208 | "lsa refcounts", | |
209 | "LSA\n" | |
210 | "show reference counts\n") | |
211 | { | |
212 | for (size_t i = 0; i < lsa_count; i++) | |
213 | if (lsas[i]) | |
53a37141 | 214 | vty_out(vty, "[%zu] %u\n", i, lsas[i]->lock); |
f1c73d14 DL |
215 | return CMD_SUCCESS; |
216 | } | |
217 | ||
218 | DEFPY(lsdb_create, lsdb_create_cmd, | |
219 | "lsdb create", | |
220 | "LSDB\n" | |
221 | "create LSDB\n") | |
222 | { | |
223 | if (lsdb) | |
224 | ospf6_lsdb_delete(lsdb); | |
225 | lsdb = ospf6_lsdb_create(NULL); | |
226 | return CMD_SUCCESS; | |
227 | } | |
228 | ||
229 | DEFPY(lsdb_delete, lsdb_delete_cmd, | |
230 | "lsdb delete", | |
231 | "LSDB\n" | |
232 | "delete LSDB\n") | |
233 | { | |
234 | ospf6_lsdb_delete(lsdb); | |
235 | lsdb = NULL; | |
236 | return CMD_SUCCESS; | |
237 | } | |
238 | ||
239 | ||
240 | struct zebra_privs_t ospf6d_privs; | |
241 | ||
242 | void test_init(int argc, char **argv) | |
243 | { | |
244 | ospf6_lsa_init(); | |
245 | ||
246 | install_element(ENABLE_NODE, &lsa_set_cmd); | |
247 | install_element(ENABLE_NODE, &lsa_refcounts_cmd); | |
248 | install_element(ENABLE_NODE, &lsa_drop_cmd); | |
249 | ||
250 | install_element(ENABLE_NODE, &lsdb_create_cmd); | |
251 | install_element(ENABLE_NODE, &lsdb_delete_cmd); | |
252 | ||
253 | install_element(ENABLE_NODE, &lsdb_add_cmd); | |
254 | install_element(ENABLE_NODE, &lsdb_remove_cmd); | |
255 | install_element(ENABLE_NODE, &lsdb_walk_cmd); | |
256 | install_element(ENABLE_NODE, &lsdb_walk_type_cmd); | |
257 | install_element(ENABLE_NODE, &lsdb_walk_type_adv_cmd); | |
258 | install_element(ENABLE_NODE, &lsdb_get_cmd); | |
259 | } |