]> git.proxmox.com Git - mirror_frr.git/blob - zebra/zebra_srv6_vty.c
Merge pull request #11784 from anlancs/fix/ospfd-cost-zebra-speed
[mirror_frr.git] / zebra / zebra_srv6_vty.c
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"
34 #include "zebra/zebra_router.h"
35 #include "zebra/zebra_vrf.h"
36 #include "zebra/zebra_srv6.h"
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
43 #ifndef VTYSH_EXTRACT_PL
44 #include "zebra/zebra_srv6_vty_clippy.c"
45 #endif
46
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
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
111 vty_json(vty, json);
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;
148 json_object *json_locator = NULL;
149
150 if (uj) {
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;
158 }
159
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));
179 }
180 }
181
182
183 return CMD_SUCCESS;
184 }
185
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
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
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 {
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);
251 vty->node = SRV6_LOC_NODE;
252 return CMD_SUCCESS;
253 }
254
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
272 DEFPY (locator_prefix,
273 locator_prefix_cmd,
274 "prefix X:X::X:X/M$prefix [func-bits (16-64)$func_bit_len]",
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);
281 struct srv6_locator_chunk *chunk = NULL;
282 struct listnode *node = NULL;
283
284 locator->prefix = *prefix;
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;
303 locator->function_bits_length = func_bit_len;
304 locator->argument_bits_length = 0;
305
306 if (list_isempty(locator->chunks)) {
307 chunk = srv6_locator_chunk_alloc();
308 chunk->prefix = *prefix;
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};
314
315 if (memcmp(&chunk->prefix.prefix, zero, 16) == 0) {
316 struct zserv *client;
317 struct listnode *client_node;
318
319 chunk->prefix = *prefix;
320 for (ALL_LIST_ELEMENTS_RO(zrouter.client_list,
321 client_node,
322 client)) {
323 struct srv6_locator *tmp;
324
325 if (client->proto != chunk->proto)
326 continue;
327
328 srv6_manager_get_locator_chunk_call(
329 &tmp, client,
330 locator->name,
331 VRF_DEFAULT);
332 }
333 }
334 }
335 }
336
337 zebra_srv6_locator_add(locator);
338 return CMD_SUCCESS;
339 }
340
341 static int zebra_sr_config(struct vty *vty)
342 {
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", str,
358 locator->prefix.prefixlen);
359 if (locator->function_bits_length)
360 vty_out(vty, " func-bits %u",
361 locator->function_bits_length);
362 vty_out(vty, "\n");
363 vty_out(vty, " exit\n");
364 vty_out(vty, " !\n");
365 }
366 vty_out(vty, " exit\n");
367 vty_out(vty, " !\n");
368 vty_out(vty, " exit\n");
369 vty_out(vty, " !\n");
370 vty_out(vty, "exit\n");
371 vty_out(vty, "!\n");
372 }
373 return 0;
374 }
375
376 void zebra_srv6_vty_init(void)
377 {
378 /* Install nodes and its default commands */
379 install_node(&sr_node);
380 install_node(&srv6_node);
381 install_node(&srv6_locs_node);
382 install_node(&srv6_loc_node);
383 install_default(SEGMENT_ROUTING_NODE);
384 install_default(SRV6_NODE);
385 install_default(SRV6_LOCS_NODE);
386 install_default(SRV6_LOC_NODE);
387
388 /* Command for change node */
389 install_element(CONFIG_NODE, &segment_routing_cmd);
390 install_element(SEGMENT_ROUTING_NODE, &srv6_cmd);
391 install_element(SEGMENT_ROUTING_NODE, &no_srv6_cmd);
392 install_element(SRV6_NODE, &srv6_locators_cmd);
393 install_element(SRV6_LOCS_NODE, &srv6_locator_cmd);
394 install_element(SRV6_LOCS_NODE, &no_srv6_locator_cmd);
395
396 /* Command for configuration */
397 install_element(SRV6_LOC_NODE, &locator_prefix_cmd);
398
399 /* Command for operation */
400 install_element(VIEW_NODE, &show_srv6_locator_cmd);
401 install_element(VIEW_NODE, &show_srv6_locator_detail_cmd);
402 }