]>
Commit | Line | Data |
---|---|---|
6c0a7c09 HS |
1 | /* |
2 | * Zebra SRv6 VTY functions | |
3 | * Copyright (C) 2020 Hiroki Shirokura, LINE Corporation | |
4 | * | |
5 | * This program is free software; you can redistribute it and/or modify it | |
6 | * under the terms of the GNU General Public License as published by the Free | |
7 | * Software Foundation; either version 2 of the License, or (at your option) | |
8 | * any later version. | |
9 | * | |
10 | * This program is distributed in the hope that it will be useful, but WITHOUT | |
11 | * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or | |
12 | * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for | |
13 | * more details. | |
14 | * | |
15 | * You should have received a copy of the GNU General Public License along | |
16 | * with this program; see the file COPYING; if not, write to the Free Software | |
17 | * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA | |
18 | */ | |
19 | ||
20 | #include <zebra.h> | |
21 | ||
22 | #include "memory.h" | |
23 | #include "if.h" | |
24 | #include "prefix.h" | |
25 | #include "command.h" | |
26 | #include "table.h" | |
27 | #include "rib.h" | |
28 | #include "nexthop.h" | |
29 | #include "vrf.h" | |
30 | #include "srv6.h" | |
31 | #include "lib/json.h" | |
32 | ||
33 | #include "zebra/zserv.h" | |
00978977 | 34 | #include "zebra/zebra_router.h" |
6c0a7c09 | 35 | #include "zebra/zebra_vrf.h" |
00978977 | 36 | #include "zebra/zebra_srv6.h" |
6c0a7c09 HS |
37 | #include "zebra/zebra_srv6_vty.h" |
38 | #include "zebra/zebra_rnh.h" | |
39 | #include "zebra/redistribute.h" | |
40 | #include "zebra/zebra_routemap.h" | |
41 | #include "zebra/zebra_dplane.h" | |
42 | ||
daedb8b3 HS |
43 | #ifndef VTYSH_EXTRACT_PL |
44 | #include "zebra/zebra_srv6_vty_clippy.c" | |
45 | #endif | |
46 | ||
6c0a7c09 HS |
47 | static int zebra_sr_config(struct vty *vty); |
48 | ||
49 | static struct cmd_node sr_node = { | |
50 | .name = "sr", | |
51 | .node = SEGMENT_ROUTING_NODE, | |
52 | .parent_node = CONFIG_NODE, | |
53 | .prompt = "%s(config-sr)# ", | |
54 | .config_write = zebra_sr_config, | |
55 | }; | |
56 | ||
57 | static struct cmd_node srv6_node = { | |
58 | .name = "srv6", | |
59 | .node = SRV6_NODE, | |
60 | .parent_node = SEGMENT_ROUTING_NODE, | |
61 | .prompt = "%s(config-srv6)# ", | |
62 | ||
63 | }; | |
64 | ||
65 | static struct cmd_node srv6_locs_node = { | |
66 | .name = "srv6-locators", | |
67 | .node = SRV6_LOCS_NODE, | |
68 | .parent_node = SRV6_NODE, | |
69 | .prompt = "%s(config-srv6-locators)# ", | |
70 | }; | |
71 | ||
72 | static struct cmd_node srv6_loc_node = { | |
73 | .name = "srv6-locator", | |
74 | .node = SRV6_LOC_NODE, | |
75 | .parent_node = SRV6_LOCS_NODE, | |
76 | .prompt = "%s(config-srv6-locator)# " | |
77 | }; | |
78 | ||
00978977 HS |
79 | DEFUN (show_srv6_locator, |
80 | show_srv6_locator_cmd, | |
81 | "show segment-routing srv6 locator [json]", | |
82 | SHOW_STR | |
83 | "Segment Routing\n" | |
84 | "Segment Routing SRv6\n" | |
85 | "Locator Information\n" | |
86 | JSON_STR) | |
87 | { | |
88 | const bool uj = use_json(argc, argv); | |
89 | struct zebra_srv6 *srv6 = zebra_srv6_get_default(); | |
90 | struct srv6_locator *locator; | |
91 | struct listnode *node; | |
92 | char str[256]; | |
93 | int id; | |
94 | json_object *json = NULL; | |
95 | json_object *json_locators = NULL; | |
96 | json_object *json_locator = NULL; | |
97 | ||
98 | if (uj) { | |
99 | json = json_object_new_object(); | |
100 | json_locators = json_object_new_array(); | |
101 | json_object_object_add(json, "locators", json_locators); | |
102 | ||
103 | for (ALL_LIST_ELEMENTS_RO(srv6->locators, node, locator)) { | |
104 | json_locator = srv6_locator_json(locator); | |
105 | if (!json_locator) | |
106 | continue; | |
107 | json_object_array_add(json_locators, json_locator); | |
108 | ||
109 | } | |
110 | ||
962af8a8 | 111 | vty_json(vty, json); |
00978977 HS |
112 | } else { |
113 | vty_out(vty, "Locator:\n"); | |
114 | vty_out(vty, "Name ID Prefix Status\n"); | |
115 | vty_out(vty, "-------------------- ------- ------------------------ -------\n"); | |
116 | ||
117 | id = 1; | |
118 | for (ALL_LIST_ELEMENTS_RO(srv6->locators, node, locator)) { | |
119 | prefix2str(&locator->prefix, str, sizeof(str)); | |
120 | vty_out(vty, "%-20s %7d %-24s %s\n", | |
121 | locator->name, id, str, | |
122 | locator->status_up ? "Up" : "Down"); | |
123 | ++id; | |
124 | } | |
125 | vty_out(vty, "\n"); | |
126 | } | |
127 | ||
128 | return CMD_SUCCESS; | |
129 | } | |
130 | ||
131 | DEFUN (show_srv6_locator_detail, | |
132 | show_srv6_locator_detail_cmd, | |
133 | "show segment-routing srv6 locator NAME detail [json]", | |
134 | SHOW_STR | |
135 | "Segment Routing\n" | |
136 | "Segment Routing SRv6\n" | |
137 | "Locator Information\n" | |
138 | "Locator Name\n" | |
139 | "Detailed information\n" | |
140 | JSON_STR) | |
141 | { | |
142 | const bool uj = use_json(argc, argv); | |
143 | struct zebra_srv6 *srv6 = zebra_srv6_get_default(); | |
144 | struct srv6_locator *locator; | |
145 | struct listnode *node; | |
146 | char str[256]; | |
147 | const char *locator_name = argv[4]->arg; | |
559f4b2f | 148 | json_object *json_locator = NULL; |
00978977 HS |
149 | |
150 | if (uj) { | |
559f4b2f YS |
151 | locator = zebra_srv6_locator_lookup(locator_name); |
152 | if (!locator) | |
153 | return CMD_WARNING; | |
154 | ||
155 | json_locator = srv6_locator_detailed_json(locator); | |
156 | vty_json(vty, json_locator); | |
157 | return CMD_SUCCESS; | |
4df9d859 | 158 | } |
00978977 | 159 | |
4df9d859 HS |
160 | for (ALL_LIST_ELEMENTS_RO(srv6->locators, node, locator)) { |
161 | struct listnode *node; | |
162 | struct srv6_locator_chunk *chunk; | |
163 | ||
164 | if (strcmp(locator->name, locator_name) != 0) | |
165 | continue; | |
166 | ||
167 | prefix2str(&locator->prefix, str, sizeof(str)); | |
168 | vty_out(vty, "Name: %s\n", locator->name); | |
169 | vty_out(vty, "Prefix: %s\n", str); | |
170 | vty_out(vty, "Function-Bit-Len: %u\n", | |
171 | locator->function_bits_length); | |
172 | ||
173 | vty_out(vty, "Chunks:\n"); | |
174 | for (ALL_LIST_ELEMENTS_RO((struct list *)locator->chunks, node, | |
175 | chunk)) { | |
176 | prefix2str(&chunk->prefix, str, sizeof(str)); | |
177 | vty_out(vty, "- prefix: %s, owner: %s\n", str, | |
178 | zebra_route_string(chunk->proto)); | |
00978977 | 179 | } |
00978977 HS |
180 | } |
181 | ||
4df9d859 | 182 | |
00978977 HS |
183 | return CMD_SUCCESS; |
184 | } | |
185 | ||
6c0a7c09 HS |
186 | DEFUN_NOSH (segment_routing, |
187 | segment_routing_cmd, | |
188 | "segment-routing", | |
189 | "Segment Routing\n") | |
190 | { | |
191 | vty->node = SEGMENT_ROUTING_NODE; | |
192 | return CMD_SUCCESS; | |
193 | } | |
194 | ||
195 | DEFUN_NOSH (srv6, | |
196 | srv6_cmd, | |
197 | "srv6", | |
198 | "Segment Routing SRv6\n") | |
199 | { | |
200 | vty->node = SRV6_NODE; | |
201 | return CMD_SUCCESS; | |
202 | } | |
203 | ||
0a735cd5 HS |
204 | DEFUN (no_srv6, |
205 | no_srv6_cmd, | |
206 | "no srv6", | |
207 | NO_STR | |
208 | "Segment Routing SRv6\n") | |
209 | { | |
210 | struct zebra_srv6 *srv6 = zebra_srv6_get_default(); | |
211 | struct srv6_locator *locator; | |
212 | struct listnode *node, *nnode; | |
213 | ||
214 | for (ALL_LIST_ELEMENTS(srv6->locators, node, nnode, locator)) | |
215 | zebra_srv6_locator_delete(locator); | |
216 | return CMD_SUCCESS; | |
217 | } | |
218 | ||
6c0a7c09 HS |
219 | DEFUN_NOSH (srv6_locators, |
220 | srv6_locators_cmd, | |
221 | "locators", | |
222 | "Segment Routing SRv6 locators\n") | |
223 | { | |
224 | vty->node = SRV6_LOCS_NODE; | |
225 | return CMD_SUCCESS; | |
226 | } | |
227 | ||
228 | DEFUN_NOSH (srv6_locator, | |
229 | srv6_locator_cmd, | |
230 | "locator WORD", | |
231 | "Segment Routing SRv6 locator\n" | |
232 | "Specify locator-name\n") | |
233 | { | |
00978977 HS |
234 | struct srv6_locator *locator = NULL; |
235 | ||
236 | locator = zebra_srv6_locator_lookup(argv[1]->arg); | |
237 | if (locator) { | |
238 | VTY_PUSH_CONTEXT(SRV6_LOC_NODE, locator); | |
239 | locator->status_up = true; | |
240 | return CMD_SUCCESS; | |
241 | } | |
242 | ||
243 | locator = srv6_locator_alloc(argv[1]->arg); | |
244 | if (!locator) { | |
245 | vty_out(vty, "%% Alloc failed\n"); | |
246 | return CMD_WARNING_CONFIG_FAILED; | |
247 | } | |
248 | locator->status_up = true; | |
249 | ||
250 | VTY_PUSH_CONTEXT(SRV6_LOC_NODE, locator); | |
6c0a7c09 HS |
251 | vty->node = SRV6_LOC_NODE; |
252 | return CMD_SUCCESS; | |
253 | } | |
254 | ||
0a735cd5 HS |
255 | DEFUN (no_srv6_locator, |
256 | no_srv6_locator_cmd, | |
257 | "no locator WORD", | |
258 | NO_STR | |
259 | "Segment Routing SRv6 locator\n" | |
260 | "Specify locator-name\n") | |
261 | { | |
262 | struct srv6_locator *locator = zebra_srv6_locator_lookup(argv[2]->arg); | |
263 | if (!locator) { | |
264 | vty_out(vty, "%% Can't find SRv6 locator\n"); | |
265 | return CMD_WARNING_CONFIG_FAILED; | |
266 | } | |
267 | ||
268 | zebra_srv6_locator_delete(locator); | |
269 | return CMD_SUCCESS; | |
270 | } | |
271 | ||
daedb8b3 | 272 | DEFPY (locator_prefix, |
00978977 | 273 | locator_prefix_cmd, |
daedb8b3 | 274 | "prefix X:X::X:X/M$prefix [func-bits (16-64)$func_bit_len]", |
00978977 HS |
275 | "Configure SRv6 locator prefix\n" |
276 | "Specify SRv6 locator prefix\n" | |
277 | "Configure SRv6 locator function length in bits\n" | |
278 | "Specify SRv6 locator function length in bits\n") | |
279 | { | |
280 | VTY_DECLVAR_CONTEXT(srv6_locator, locator); | |
00978977 HS |
281 | struct srv6_locator_chunk *chunk = NULL; |
282 | struct listnode *node = NULL; | |
00978977 | 283 | |
daedb8b3 | 284 | locator->prefix = *prefix; |
ac6a9479 HS |
285 | |
286 | /* | |
287 | * TODO(slankdev): please support variable node-bit-length. | |
288 | * In draft-ietf-bess-srv6-services-05#section-3.2.1. | |
289 | * Locator block length and Locator node length are defined. | |
290 | * Which are defined as "locator-len == block-len + node-len". | |
291 | * In current implementation, node bits length is hardcoded as 24. | |
292 | * It should be supported various val. | |
293 | * | |
294 | * Cisco IOS-XR support only following pattern. | |
295 | * (1) Teh locator length should be 64-bits long. | |
296 | * (2) The SID block portion (MSBs) cannot exceed 40 bits. | |
297 | * If this value is less than 40 bits, | |
298 | * user should use a pattern of zeros as a filler. | |
299 | * (3) The Node Id portion (LSBs) cannot exceed 24 bits. | |
300 | */ | |
301 | locator->block_bits_length = prefix->prefixlen - 24; | |
302 | locator->node_bits_length = 24; | |
daedb8b3 | 303 | locator->function_bits_length = func_bit_len; |
ac6a9479 | 304 | locator->argument_bits_length = 0; |
00978977 HS |
305 | |
306 | if (list_isempty(locator->chunks)) { | |
307 | chunk = srv6_locator_chunk_alloc(); | |
daedb8b3 | 308 | chunk->prefix = *prefix; |
00978977 HS |
309 | chunk->proto = 0; |
310 | listnode_add(locator->chunks, chunk); | |
311 | } else { | |
312 | for (ALL_LIST_ELEMENTS_RO(locator->chunks, node, chunk)) { | |
313 | uint8_t zero[16] = {0}; | |
4df9d859 | 314 | |
00978977 HS |
315 | if (memcmp(&chunk->prefix.prefix, zero, 16) == 0) { |
316 | struct zserv *client; | |
317 | struct listnode *client_node; | |
4df9d859 | 318 | |
daedb8b3 | 319 | chunk->prefix = *prefix; |
4df9d859 HS |
320 | for (ALL_LIST_ELEMENTS_RO(zrouter.client_list, |
321 | client_node, | |
322 | client)) { | |
323 | struct srv6_locator *tmp; | |
324 | ||
00978977 HS |
325 | if (client->proto != chunk->proto) |
326 | continue; | |
f29aed74 | 327 | |
00978977 | 328 | srv6_manager_get_locator_chunk_call( |
4df9d859 HS |
329 | &tmp, client, |
330 | locator->name, | |
331 | VRF_DEFAULT); | |
00978977 HS |
332 | } |
333 | } | |
334 | } | |
335 | } | |
336 | ||
337 | zebra_srv6_locator_add(locator); | |
338 | return CMD_SUCCESS; | |
339 | } | |
340 | ||
6c0a7c09 HS |
341 | static int zebra_sr_config(struct vty *vty) |
342 | { | |
00978977 HS |
343 | struct zebra_srv6 *srv6 = zebra_srv6_get_default(); |
344 | struct listnode *node; | |
345 | struct srv6_locator *locator; | |
346 | char str[256]; | |
347 | ||
348 | vty_out(vty, "!\n"); | |
349 | if (zebra_srv6_is_enable()) { | |
350 | vty_out(vty, "segment-routing\n"); | |
351 | vty_out(vty, " srv6\n"); | |
352 | vty_out(vty, " locators\n"); | |
353 | for (ALL_LIST_ELEMENTS_RO(srv6->locators, node, locator)) { | |
354 | inet_ntop(AF_INET6, &locator->prefix.prefix, | |
355 | str, sizeof(str)); | |
356 | vty_out(vty, " locator %s\n", locator->name); | |
357 | vty_out(vty, " prefix %s/%u\n", str, | |
358 | locator->prefix.prefixlen); | |
07679ad9 | 359 | vty_out(vty, " exit\n"); |
00978977 HS |
360 | vty_out(vty, " !\n"); |
361 | } | |
07679ad9 | 362 | vty_out(vty, " exit\n"); |
00978977 | 363 | vty_out(vty, " !\n"); |
07679ad9 | 364 | vty_out(vty, " exit\n"); |
00978977 | 365 | vty_out(vty, " !\n"); |
07679ad9 | 366 | vty_out(vty, "exit\n"); |
00978977 HS |
367 | vty_out(vty, "!\n"); |
368 | } | |
6c0a7c09 HS |
369 | return 0; |
370 | } | |
371 | ||
372 | void zebra_srv6_vty_init(void) | |
373 | { | |
374 | /* Install nodes and its default commands */ | |
375 | install_node(&sr_node); | |
376 | install_node(&srv6_node); | |
377 | install_node(&srv6_locs_node); | |
378 | install_node(&srv6_loc_node); | |
379 | install_default(SEGMENT_ROUTING_NODE); | |
380 | install_default(SRV6_NODE); | |
381 | install_default(SRV6_LOCS_NODE); | |
382 | install_default(SRV6_LOC_NODE); | |
383 | ||
384 | /* Command for change node */ | |
385 | install_element(CONFIG_NODE, &segment_routing_cmd); | |
386 | install_element(SEGMENT_ROUTING_NODE, &srv6_cmd); | |
0a735cd5 | 387 | install_element(SEGMENT_ROUTING_NODE, &no_srv6_cmd); |
6c0a7c09 HS |
388 | install_element(SRV6_NODE, &srv6_locators_cmd); |
389 | install_element(SRV6_LOCS_NODE, &srv6_locator_cmd); | |
0a735cd5 | 390 | install_element(SRV6_LOCS_NODE, &no_srv6_locator_cmd); |
00978977 HS |
391 | |
392 | /* Command for configuration */ | |
393 | install_element(SRV6_LOC_NODE, &locator_prefix_cmd); | |
394 | ||
395 | /* Command for operation */ | |
396 | install_element(VIEW_NODE, &show_srv6_locator_cmd); | |
397 | install_element(VIEW_NODE, &show_srv6_locator_detail_cmd); | |
6c0a7c09 | 398 | } |