]> git.proxmox.com Git - mirror_frr.git/blob - pbrd/pbr_vty.c
pbrd, lib: move PBR_STR def. to lib
[mirror_frr.git] / pbrd / pbr_vty.c
1 /*
2 * PBR - vty code
3 * Copyright (C) 2018 Cumulus Networks, Inc.
4 * Donald Sharp
5 *
6 * FRR is free software; you can redistribute it and/or modify it
7 * under the terms of the GNU General Public License as published by the
8 * Free Software Foundation; either version 2, or (at your option) any
9 * later version.
10 *
11 * FRR is distributed in the hope that it will be useful, but
12 * WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * General Public License for more details.
15 *
16 * You should have received a copy of the GNU General Public License along
17 * with this program; see the file COPYING; if not, write to the Free Software
18 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
19 */
20 #include <zebra.h>
21
22 #include "vty.h"
23 #include "command.h"
24 #include "prefix.h"
25 #include "vrf.h"
26 #include "nexthop.h"
27 #include "nexthop_group.h"
28 #include "log.h"
29 #include "debug.h"
30 #include "pbr.h"
31
32 #include "pbrd/pbr_nht.h"
33 #include "pbrd/pbr_map.h"
34 #include "pbrd/pbr_zebra.h"
35 #include "pbrd/pbr_vty.h"
36 #include "pbrd/pbr_debug.h"
37 #ifndef VTYSH_EXTRACT_PL
38 #include "pbrd/pbr_vty_clippy.c"
39 #endif
40
41 DEFUN_NOSH(pbr_map, pbr_map_cmd, "pbr-map WORD seq (1-700)",
42 "Create pbr-map or enter pbr-map command mode\n"
43 "The name of the PBR MAP\n"
44 "Sequence to insert in existing pbr-map entry\n"
45 "Sequence number\n")
46 {
47 const char *pbrm_name = argv[1]->arg;
48 uint32_t seqno = atoi(argv[3]->arg);
49 struct pbr_map_sequence *pbrms;
50
51 pbrms = pbrms_get(pbrm_name, seqno);
52 VTY_PUSH_CONTEXT(PBRMAP_NODE, pbrms);
53
54 return CMD_SUCCESS;
55 }
56
57 DEFUN_NOSH(no_pbr_map, no_pbr_map_cmd, "no pbr-map WORD [seq (1-700)]",
58 NO_STR
59 "Delete pbr-map\n"
60 "The name of the PBR MAP\n"
61 "Sequence to delete from existing pbr-map entry\n"
62 "Sequence number\n")
63 {
64 const char *pbrm_name = argv[2]->arg;
65 uint32_t seqno = 0;
66 struct pbr_map *pbrm = pbrm_find(pbrm_name);
67 struct pbr_map_sequence *pbrms;
68 struct listnode *node, *next_node;
69
70 if (argc > 3)
71 seqno = atoi(argv[4]->arg);
72
73 if (!pbrm) {
74 vty_out(vty, "pbr-map %s not found\n", pbrm_name);
75 return CMD_SUCCESS;
76 }
77
78 for (ALL_LIST_ELEMENTS(pbrm->seqnumbers, node, next_node, pbrms)) {
79 if (seqno && pbrms->seqno != seqno)
80 continue;
81
82 pbr_map_delete(pbrms);
83 }
84
85 return CMD_SUCCESS;
86 }
87
88 DEFPY(pbr_set_table_range,
89 pbr_set_table_range_cmd,
90 "[no] pbr table range (10000-4294966272)$lb (10000-4294966272)$ub",
91 NO_STR
92 PBR_STR
93 "Set table ID range\n"
94 "Set table ID range\n"
95 "Lower bound for table ID range\n"
96 "Upper bound for table ID range\n")
97 {
98 /* upper bound is 2^32 - 2^10 */
99 int ret = CMD_WARNING;
100
101 /* validate given bounds */
102 if (lb > ub)
103 vty_out(vty, "%% Lower bound must be less than upper bound\n");
104 else if (ub - lb < 10)
105 vty_out(vty, "%% Range breadth must be at least 10\n");
106 else {
107 ret = CMD_SUCCESS;
108 pbr_nht_set_tableid_range((uint32_t) lb, (uint32_t) ub);
109 }
110
111 return ret;
112 }
113
114
115 DEFPY(pbr_map_match_src, pbr_map_match_src_cmd,
116 "[no] match src-ip <A.B.C.D/M|X:X::X:X/M>$prefix",
117 NO_STR
118 "Match the rest of the command\n"
119 "Choose the src ip or ipv6 prefix to use\n"
120 "v4 Prefix\n"
121 "v6 Prefix\n")
122 {
123 struct pbr_map_sequence *pbrms = VTY_GET_CONTEXT(pbr_map_sequence);
124
125 pbrms->family = prefix->family;
126
127 if (!no) {
128 if (prefix_same(pbrms->src, prefix))
129 return CMD_SUCCESS;
130
131 if (!pbrms->src)
132 pbrms->src = prefix_new();
133 prefix_copy(pbrms->src, prefix);
134 } else {
135 prefix_free(pbrms->src);
136 pbrms->src = 0;
137 }
138
139 pbr_map_check(pbrms);
140
141 return CMD_SUCCESS;
142 }
143
144 DEFPY(pbr_map_match_dst, pbr_map_match_dst_cmd,
145 "[no] match dst-ip <A.B.C.D/M|X:X::X:X/M>$prefix",
146 NO_STR
147 "Match the rest of the command\n"
148 "Choose the src ip or ipv6 prefix to use\n"
149 "v4 Prefix\n"
150 "v6 Prefix\n")
151 {
152 struct pbr_map_sequence *pbrms = VTY_GET_CONTEXT(pbr_map_sequence);
153
154 pbrms->family = prefix->family;
155
156 if (!no) {
157 if (prefix_same(pbrms->dst, prefix))
158 return CMD_SUCCESS;
159
160 if (!pbrms->dst)
161 pbrms->dst = prefix_new();
162 prefix_copy(pbrms->dst, prefix);
163 } else {
164 prefix_free(pbrms->dst);
165 pbrms->dst = NULL;
166 }
167
168 pbr_map_check(pbrms);
169
170 return CMD_SUCCESS;
171 }
172
173 DEFPY(pbr_map_nexthop_group, pbr_map_nexthop_group_cmd,
174 "[no] set nexthop-group NAME$name",
175 NO_STR
176 "Set for the PBR-MAP\n"
177 "nexthop-group to use\n"
178 "The name of the nexthop-group\n")
179 {
180 struct pbr_map_sequence *pbrms = VTY_GET_CONTEXT(pbr_map_sequence);
181 struct nexthop_group_cmd *nhgc;
182
183 if (pbrms->nhg) {
184 vty_out(vty,
185 "A `set nexthop XX` command already exists, please remove that first\n");
186 return CMD_WARNING_CONFIG_FAILED;
187 }
188
189 nhgc = nhgc_find(name);
190 if (!nhgc) {
191 vty_out(vty, "Specified nexthop-group %s does not exist\n",
192 name);
193 vty_out(vty, "PBR-MAP will not be applied until it is created\n");
194 }
195
196 if (no) {
197 if (pbrms->nhgrp_name && strcmp(name, pbrms->nhgrp_name) == 0)
198 pbr_map_delete_nexthop_group(pbrms);
199 else {
200 vty_out(vty,
201 "Nexthop Group specified: %s does not exist to remove",
202 name);
203 return CMD_WARNING_CONFIG_FAILED;
204 }
205 } else {
206 if (pbrms->nhgrp_name) {
207 if (strcmp(name, pbrms->nhgrp_name) != 0) {
208 vty_out(vty,
209 "Please delete current nexthop group before modifying current one");
210 return CMD_WARNING_CONFIG_FAILED;
211 }
212
213 return CMD_SUCCESS;
214 }
215 pbrms->nhgrp_name = XSTRDUP(MTYPE_TMP, name);
216 pbr_map_check(pbrms);
217 }
218
219 return CMD_SUCCESS;
220 }
221
222 DEFPY(pbr_map_nexthop, pbr_map_nexthop_cmd,
223 "[no] set nexthop <A.B.C.D|X:X::X:X>$addr [INTERFACE]$intf [nexthop-vrf NAME$name]",
224 NO_STR
225 "Set for the PBR-MAP\n"
226 "Specify one of the nexthops in this map\n"
227 "v4 Address\n"
228 "v6 Address\n"
229 "Interface to use\n"
230 "If the nexthop is in a different vrf tell us\n"
231 "The nexthop-vrf Name\n")
232 {
233 struct pbr_map_sequence *pbrms = VTY_GET_CONTEXT(pbr_map_sequence);
234 struct vrf *vrf;
235 struct nexthop nhop;
236 struct nexthop *nh;
237
238 if (pbrms->nhgrp_name) {
239 vty_out(vty,
240 "Please unconfigure the nexthop group before adding an individual nexthop");
241 return CMD_WARNING_CONFIG_FAILED;
242 }
243
244 if (name)
245 vrf = vrf_lookup_by_name(name);
246 else
247 vrf = vrf_lookup_by_id(VRF_DEFAULT);
248
249 if (!vrf) {
250 vty_out(vty, "Specified: %s is non-existent\n", name);
251 return CMD_WARNING_CONFIG_FAILED;
252 }
253
254 memset(&nhop, 0, sizeof(nhop));
255 nhop.vrf_id = vrf->vrf_id;
256
257 /*
258 * Make SA happy. CLIPPY is not going to give us a NULL
259 * addr.
260 */
261 assert(addr);
262 if (addr->sa.sa_family == AF_INET) {
263 nhop.gate.ipv4.s_addr = addr->sin.sin_addr.s_addr;
264 if (intf) {
265 nhop.type = NEXTHOP_TYPE_IPV4_IFINDEX;
266 nhop.ifindex = ifname2ifindex(intf, vrf->vrf_id);
267 if (nhop.ifindex == IFINDEX_INTERNAL) {
268 vty_out(vty,
269 "Specified Intf %s does not exist in vrf: %s\n",
270 intf, vrf->name);
271 return CMD_WARNING_CONFIG_FAILED;
272 }
273 } else
274 nhop.type = NEXTHOP_TYPE_IPV4;
275 } else {
276 memcpy(&nhop.gate.ipv6, &addr->sin6.sin6_addr, 16);
277 if (intf) {
278 nhop.type = NEXTHOP_TYPE_IPV6_IFINDEX;
279 nhop.ifindex = ifname2ifindex(intf, vrf->vrf_id);
280 if (nhop.ifindex == IFINDEX_INTERNAL) {
281 vty_out(vty,
282 "Specified Intf %s does not exist in vrf: %s\n",
283 intf, vrf->name);
284 return CMD_WARNING_CONFIG_FAILED;
285 }
286 } else {
287 if (IN6_IS_ADDR_LINKLOCAL(&nhop.gate.ipv6)) {
288 vty_out(vty,
289 "Specified a v6 LL with no interface, rejecting\n");
290 return CMD_WARNING_CONFIG_FAILED;
291 }
292 nhop.type = NEXTHOP_TYPE_IPV6;
293 }
294 }
295
296 if (pbrms->nhg)
297 nh = nexthop_exists(pbrms->nhg, &nhop);
298 else {
299 char buf[PBR_NHC_NAMELEN];
300
301 if (no) {
302 vty_out(vty, "No nexthops to delete");
303 return CMD_WARNING_CONFIG_FAILED;
304 }
305
306 pbrms->nhg = nexthop_group_new();
307 pbrms->internal_nhg_name =
308 XSTRDUP(MTYPE_TMP,
309 pbr_nht_nexthop_make_name(pbrms->parent->name,
310 PBR_NHC_NAMELEN,
311 pbrms->seqno,
312 buf));
313 nh = NULL;
314 }
315
316 if (no) {
317 if (nh)
318 pbr_nht_delete_individual_nexthop(pbrms);
319 } else if (!nh) {
320
321 if (pbrms->nhg->nexthop) {
322 vty_out(vty,
323 "If you would like more than one nexthop please use nexthop-groups");
324 return CMD_WARNING_CONFIG_FAILED;
325 }
326
327 /* must be adding new nexthop since !no and !nexthop_exists */
328 nh = nexthop_new();
329
330 memcpy(nh, &nhop, sizeof(nhop));
331 nexthop_add(&pbrms->nhg->nexthop, nh);
332
333 pbr_nht_add_individual_nexthop(pbrms);
334 pbr_map_check(pbrms);
335 }
336
337 return CMD_SUCCESS;
338 }
339
340 DEFPY (pbr_policy,
341 pbr_policy_cmd,
342 "[no] pbr-policy NAME$mapname",
343 NO_STR
344 "Policy to use\n"
345 "Name of the pbr-map to apply\n")
346 {
347 VTY_DECLVAR_CONTEXT(interface, ifp);
348 struct pbr_map *pbrm, *old_pbrm;
349 struct pbr_interface *pbr_ifp = ifp->info;
350
351 pbrm = pbrm_find(mapname);
352
353 if (!pbr_ifp) {
354 /* we don't want one and we don't have one, so... */
355 if (no)
356 return CMD_SUCCESS;
357
358 /* Some one could have fat fingered the interface name */
359 pbr_ifp = pbr_if_new(ifp);
360 }
361
362 if (no) {
363 if (strcmp(pbr_ifp->mapname, mapname) == 0) {
364 pbr_ifp->mapname[0] = '\0';
365 if (pbrm)
366 pbr_map_interface_delete(pbrm, ifp);
367 }
368 } else {
369 if (strcmp(pbr_ifp->mapname, "") != 0) {
370 old_pbrm = pbrm_find(pbr_ifp->mapname);
371 if (old_pbrm)
372 pbr_map_interface_delete(old_pbrm, ifp);
373 }
374 snprintf(pbr_ifp->mapname, sizeof(pbr_ifp->mapname),
375 "%s", mapname);
376 if (pbrm)
377 pbr_map_add_interface(pbrm, ifp);
378 }
379
380 return CMD_SUCCESS;
381 }
382
383 DEFPY (show_pbr,
384 show_pbr_cmd,
385 "show pbr",
386 SHOW_STR
387 PBR_STR)
388 {
389 pbr_nht_write_table_range(vty);
390 pbr_nht_write_rule_range(vty);
391
392 return CMD_SUCCESS;
393 }
394
395 DEFPY (show_pbr_map,
396 show_pbr_map_cmd,
397 "show pbr map [NAME$name] [detail$detail]",
398 SHOW_STR
399 PBR_STR
400 "PBR Map\n"
401 "PBR Map Name\n"
402 "Detailed information\n")
403 {
404 struct pbr_map_sequence *pbrms;
405 struct pbr_map *pbrm;
406 struct listnode *node;
407 char buf[PREFIX_STRLEN];
408 char rbuf[64];
409
410 RB_FOREACH (pbrm, pbr_map_entry_head, &pbr_maps) {
411 if (name && strcmp(name, pbrm->name) != 0)
412 continue;
413
414 vty_out(vty, " pbr-map %s valid: %d\n", pbrm->name,
415 pbrm->valid);
416
417 for (ALL_LIST_ELEMENTS_RO(pbrm->seqnumbers, node, pbrms)) {
418 if (pbrms->reason)
419 pbr_map_reason_string(pbrms->reason, rbuf,
420 sizeof(rbuf));
421 vty_out(vty,
422 " Seq: %u rule: %u Installed: %" PRIu64 "(%u) Reason: %s\n",
423 pbrms->seqno, pbrms->ruleno, pbrms->installed,
424 pbrms->unique, pbrms->reason ? rbuf : "Valid");
425
426 if (pbrms->src)
427 vty_out(vty, "\tSRC Match: %s\n",
428 prefix2str(pbrms->src, buf,
429 sizeof(buf)));
430 if (pbrms->dst)
431 vty_out(vty, "\tDST Match: %s\n",
432 prefix2str(pbrms->dst, buf,
433 sizeof(buf)));
434
435 if (pbrms->nhgrp_name) {
436 vty_out(vty,
437 "\tNexthop-Group: %s(%u) Installed: %u(%d)\n",
438 pbrms->nhgrp_name,
439 pbr_nht_get_table(pbrms->nhgrp_name),
440 pbrms->nhs_installed,
441 pbr_nht_get_installed(
442 pbrms->nhgrp_name));
443 } else if (pbrms->nhg) {
444 vty_out(vty, " ");
445 nexthop_group_write_nexthop(
446 vty, pbrms->nhg->nexthop);
447 vty_out(vty,
448 "\tInstalled: %u(%d) Tableid: %d\n",
449 pbrms->nhs_installed,
450 pbr_nht_get_installed(
451 pbrms->internal_nhg_name),
452 pbr_nht_get_table(
453 pbrms->internal_nhg_name));
454 } else {
455 vty_out(vty,
456 "\tNexthop-Group: Unknown Installed: 0(0)\n");
457 }
458 }
459 }
460 return CMD_SUCCESS;
461 }
462
463 DEFPY(show_pbr_nexthop_group,
464 show_pbr_nexthop_group_cmd,
465 "show pbr nexthop-groups [WORD$word]",
466 SHOW_STR
467 PBR_STR
468 "Nexthop Groups\n"
469 "Optional Name of the nexthop group\n")
470 {
471 pbr_nht_show_nexthop_group(vty, word);
472
473 return CMD_SUCCESS;
474 }
475
476 DEFPY (show_pbr_interface,
477 show_pbr_interface_cmd,
478 "show pbr interface [NAME$name]",
479 SHOW_STR
480 PBR_STR
481 "PBR Interface\n"
482 "PBR Interface Name\n")
483 {
484 struct interface *ifp;
485 struct vrf *vrf;
486 struct pbr_interface *pbr_ifp;
487
488 RB_FOREACH(vrf, vrf_name_head, &vrfs_by_name) {
489 FOR_ALL_INTERFACES(vrf, ifp) {
490 struct pbr_map *pbrm;
491
492 if (!ifp->info)
493 continue;
494
495 if (name && strcmp(ifp->name, name) != 0)
496 continue;
497
498 pbr_ifp = ifp->info;
499
500 if (strcmp(pbr_ifp->mapname, "") == 0)
501 continue;
502
503 pbrm = pbrm_find(pbr_ifp->mapname);
504 vty_out(vty, " %s(%d) with pbr-policy %s", ifp->name,
505 ifp->ifindex, pbr_ifp->mapname);
506 if (!pbrm)
507 vty_out(vty, " (map doesn't exist)");
508 vty_out(vty, "\n");
509 }
510 }
511
512 return CMD_SUCCESS;
513 }
514
515 /* PBR debugging CLI ------------------------------------------------------- */
516
517 static struct cmd_node debug_node = {DEBUG_NODE, "", 1};
518
519 DEFPY(debug_pbr,
520 debug_pbr_cmd,
521 "[no] debug pbr [{map$map|zebra$zebra|nht$nht|events$events}]",
522 NO_STR
523 DEBUG_STR
524 PBR_STR
525 "Policy maps\n"
526 "PBRD <-> Zebra communications\n"
527 "Nexthop tracking\n"
528 "Events\n")
529 {
530 uint32_t mode = DEBUG_NODE2MODE(vty->node);
531
532 if (map)
533 DEBUG_MODE_SET(&pbr_dbg_map, mode, !no);
534 if (zebra)
535 DEBUG_MODE_SET(&pbr_dbg_zebra, mode, !no);
536 if (nht)
537 DEBUG_MODE_SET(&pbr_dbg_nht, mode, !no);
538 if (events)
539 DEBUG_MODE_SET(&pbr_dbg_event, mode, !no);
540
541 /* no specific debug --> act on all of them */
542 if (strmatch(argv[argc - 1]->text, "pbr"))
543 pbr_debug_set_all(mode, !no);
544
545 return CMD_SUCCESS;
546 }
547
548 DEFUN_NOSH(show_debugging_pbr,
549 show_debugging_pbr_cmd,
550 "show debugging [pbr]",
551 SHOW_STR
552 DEBUG_STR
553 PBR_STR)
554 {
555 vty_out(vty, "PBR debugging status:\n");
556
557 pbr_debug_config_write_helper(vty, false);
558
559 return CMD_SUCCESS;
560 }
561
562 /* ------------------------------------------------------------------------- */
563
564
565 static struct cmd_node interface_node = {
566 INTERFACE_NODE, "%s(config-if)# ", 1 /* vtysh ? yes */
567 };
568
569 static int pbr_interface_config_write(struct vty *vty)
570 {
571 struct interface *ifp;
572 struct vrf *vrf;
573
574 RB_FOREACH (vrf, vrf_name_head, &vrfs_by_name) {
575 FOR_ALL_INTERFACES (vrf, ifp) {
576 if (vrf->vrf_id == VRF_DEFAULT)
577 vty_frame(vty, "interface %s\n", ifp->name);
578 else
579 vty_frame(vty, "interface %s vrf %s\n",
580 ifp->name, vrf->name);
581
582 pbr_map_write_interfaces(vty, ifp);
583
584 vty_endframe(vty, "!\n");
585 }
586 }
587
588 return 1;
589 }
590
591 /* PBR map node structure. */
592 static struct cmd_node pbr_map_node = {PBRMAP_NODE, "%s(config-pbr-map)# ", 1};
593
594 static int pbr_vty_map_config_write_sequence(struct vty *vty,
595 struct pbr_map *pbrm,
596 struct pbr_map_sequence *pbrms)
597 {
598 char buff[PREFIX_STRLEN];
599
600 vty_out(vty, "pbr-map %s seq %u\n", pbrm->name, pbrms->seqno);
601
602 if (pbrms->src)
603 vty_out(vty, " match src-ip %s\n",
604 prefix2str(pbrms->src, buff, sizeof(buff)));
605
606 if (pbrms->dst)
607 vty_out(vty, " match dst-ip %s\n",
608 prefix2str(pbrms->dst, buff, sizeof(buff)));
609
610 if (pbrms->nhgrp_name)
611 vty_out(vty, " set nexthop-group %s\n", pbrms->nhgrp_name);
612
613 if (pbrms->nhg) {
614 vty_out(vty, " set ");
615 nexthop_group_write_nexthop(vty, pbrms->nhg->nexthop);
616 }
617
618 vty_out(vty, "!\n");
619 return 1;
620 }
621
622 static int pbr_vty_map_config_write(struct vty *vty)
623 {
624 struct pbr_map *pbrm;
625
626 pbr_nht_write_table_range(vty);
627 pbr_nht_write_rule_range(vty);
628
629 RB_FOREACH(pbrm, pbr_map_entry_head, &pbr_maps) {
630 struct pbr_map_sequence *pbrms;
631 struct listnode *node;
632
633 for (ALL_LIST_ELEMENTS_RO(pbrm->seqnumbers, node, pbrms))
634 pbr_vty_map_config_write_sequence(vty, pbrm, pbrms);
635 }
636
637 return 1;
638 }
639
640 void pbr_vty_init(void)
641 {
642 install_node(&interface_node,
643 pbr_interface_config_write);
644 if_cmd_init();
645
646 install_node(&pbr_map_node,
647 pbr_vty_map_config_write);
648
649 /* debug */
650 install_node(&debug_node, pbr_debug_config_write);
651 install_element(VIEW_NODE, &debug_pbr_cmd);
652 install_element(CONFIG_NODE, &debug_pbr_cmd);
653 install_element(VIEW_NODE, &show_debugging_pbr_cmd);
654
655 install_default(PBRMAP_NODE);
656
657 install_element(CONFIG_NODE, &pbr_map_cmd);
658 install_element(CONFIG_NODE, &no_pbr_map_cmd);
659 install_element(CONFIG_NODE, &pbr_set_table_range_cmd);
660 install_element(INTERFACE_NODE, &pbr_policy_cmd);
661 install_element(PBRMAP_NODE, &pbr_map_match_src_cmd);
662 install_element(PBRMAP_NODE, &pbr_map_match_dst_cmd);
663 install_element(PBRMAP_NODE, &pbr_map_nexthop_group_cmd);
664 install_element(PBRMAP_NODE, &pbr_map_nexthop_cmd);
665 install_element(VIEW_NODE, &show_pbr_cmd);
666 install_element(VIEW_NODE, &show_pbr_map_cmd);
667 install_element(VIEW_NODE, &show_pbr_interface_cmd);
668 install_element(VIEW_NODE, &show_pbr_nexthop_group_cmd);
669 }