]> git.proxmox.com Git - mirror_frr.git/blobdiff - pbrd/pbr_vty.c
Merge pull request #4765 from opensourcerouting/defaults-v2
[mirror_frr.git] / pbrd / pbr_vty.c
index 5e7addc9d2b0e10bde2cb014a677d3ecafd22d4c..53248f5aaf4c7cba672b0d0748927a881df74fb2 100644 (file)
@@ -127,16 +127,19 @@ DEFPY(pbr_map_match_src, pbr_map_match_src_cmd,
        pbrms->family = prefix->family;
 
        if (!no) {
-               if (prefix_same(pbrms->src, prefix))
-                       return CMD_SUCCESS;
+               if (pbrms->src) {
+                       if (prefix_same(pbrms->src, prefix))
+                               return CMD_SUCCESS;
+
+                       vty_out(vty,
+                               "A `match src-ip XX` command already exists, please remove that first\n");
+                       return CMD_WARNING_CONFIG_FAILED;
+               }
 
-               if (!pbrms->src)
-                       pbrms->src = prefix_new();
+               pbrms->src = prefix_new();
                prefix_copy(pbrms->src, prefix);
-       } else {
-               prefix_free(pbrms->src);
-               pbrms->src = 0;
-       }
+       } else
+               prefix_free(&pbrms->src);
 
        pbr_map_check(pbrms);
 
@@ -147,7 +150,7 @@ DEFPY(pbr_map_match_dst, pbr_map_match_dst_cmd,
        "[no] match dst-ip <A.B.C.D/M|X:X::X:X/M>$prefix",
        NO_STR
        "Match the rest of the command\n"
-       "Choose the src ip or ipv6 prefix to use\n"
+       "Choose the dst ip or ipv6 prefix to use\n"
        "v4 Prefix\n"
        "v6 Prefix\n")
 {
@@ -156,16 +159,19 @@ DEFPY(pbr_map_match_dst, pbr_map_match_dst_cmd,
        pbrms->family = prefix->family;
 
        if (!no) {
-               if (prefix_same(pbrms->dst, prefix))
-                       return CMD_SUCCESS;
+               if (pbrms->dst) {
+                       if (prefix_same(pbrms->dst, prefix))
+                               return CMD_SUCCESS;
+
+                       vty_out(vty,
+                               "A `match dst-ip XX` command already exists, please remove that first\n");
+                       return CMD_WARNING_CONFIG_FAILED;
+               }
 
-               if (!pbrms->dst)
-                       pbrms->dst = prefix_new();
+               pbrms->dst = prefix_new();
                prefix_copy(pbrms->dst, prefix);
-       } else {
-               prefix_free(pbrms->dst);
-               pbrms->dst = NULL;
-       }
+       } else
+               prefix_free(&pbrms->dst);
 
        pbr_map_check(pbrms);
 
@@ -187,24 +193,33 @@ DEFPY(pbr_map_match_mark, pbr_map_match_mark_cmd,
 #endif
 
        if (!no) {
-               if (pbrms->mark == (uint32_t) mark)
-                       return CMD_SUCCESS;
-               pbrms->mark = (uint32_t) mark;
-       } else {
+               if (pbrms->mark) {
+                       if (pbrms->mark == (uint32_t)mark)
+                               return CMD_SUCCESS;
+
+                       vty_out(vty,
+                               "A `match mark XX` command already exists, please remove that first\n");
+                       return CMD_WARNING_CONFIG_FAILED;
+               }
+
+               pbrms->mark = (uint32_t)mark;
+       } else
                pbrms->mark = 0;
-       }
 
        pbr_map_check(pbrms);
 
        return CMD_SUCCESS;
- }
+}
+
+#define SET_VRF_EXISTS_STR                                                     \
+       "A `set vrf XX` command already exists, please remove that first\n"
 
 DEFPY(pbr_map_nexthop_group, pbr_map_nexthop_group_cmd,
-       "[no] set nexthop-group NHGNAME$name",
-       NO_STR
-       "Set for the PBR-MAP\n"
-       "nexthop-group to use\n"
-       "The name of the nexthop-group\n")
+      "[no] set nexthop-group NHGNAME$name",
+      NO_STR
+      "Set for the PBR-MAP\n"
+      "nexthop-group to use\n"
+      "The name of the nexthop-group\n")
 {
        struct pbr_map_sequence *pbrms = VTY_GET_CONTEXT(pbr_map_sequence);
        struct nexthop_group_cmd *nhgc;
@@ -215,19 +230,25 @@ DEFPY(pbr_map_nexthop_group, pbr_map_nexthop_group_cmd,
                return CMD_WARNING_CONFIG_FAILED;
        }
 
+       if (pbrms->vrf_lookup || pbrms->vrf_unchanged) {
+               vty_out(vty, SET_VRF_EXISTS_STR);
+               return CMD_WARNING_CONFIG_FAILED;
+       }
+
        nhgc = nhgc_find(name);
        if (!nhgc) {
                vty_out(vty, "Specified nexthop-group %s does not exist\n",
                        name);
-               vty_out(vty, "PBR-MAP will not be applied until it is created\n");
+               vty_out(vty,
+                       "PBR-MAP will not be applied until it is created\n");
        }
 
        if (no) {
                if (pbrms->nhgrp_name && strcmp(name, pbrms->nhgrp_name) == 0)
-                       pbr_map_delete_nexthop_group(pbrms);
+                       pbr_map_delete_nexthops(pbrms);
                else {
                        vty_out(vty,
-                               "Nexthop Group specified: %s does not exist to remove",
+                               "Nexthop Group specified: %s does not exist to remove\n",
                                name);
                        return CMD_WARNING_CONFIG_FAILED;
                }
@@ -235,7 +256,7 @@ DEFPY(pbr_map_nexthop_group, pbr_map_nexthop_group_cmd,
                if (pbrms->nhgrp_name) {
                        if (strcmp(name, pbrms->nhgrp_name) != 0) {
                                vty_out(vty,
-                                       "Please delete current nexthop group before modifying current one");
+                                       "Please delete current nexthop group before modifying current one\n");
                                return CMD_WARNING_CONFIG_FAILED;
                        }
 
@@ -254,7 +275,7 @@ DEFPY(pbr_map_nexthop, pbr_map_nexthop_cmd,
          <A.B.C.D|X:X::X:X>$addr [INTERFACE$intf]\
          |INTERFACE$intf\
        >\
-        [nexthop-vrf NAME$name]",
+        [nexthop-vrf NAME$vrf_name]",
       NO_STR
       "Set for the PBR-MAP\n"
       "Specify one of the nexthops in this map\n"
@@ -272,17 +293,22 @@ DEFPY(pbr_map_nexthop, pbr_map_nexthop_cmd,
 
        if (pbrms->nhgrp_name) {
                vty_out(vty,
-                       "Please unconfigure the nexthop group before adding an individual nexthop");
+                       "Please unconfigure the nexthop group before adding an individual nexthop\n");
+               return CMD_WARNING_CONFIG_FAILED;
+       }
+
+       if (pbrms->vrf_lookup || pbrms->vrf_unchanged) {
+               vty_out(vty, SET_VRF_EXISTS_STR);
                return CMD_WARNING_CONFIG_FAILED;
        }
 
-       if (name)
-               vrf = vrf_lookup_by_name(name);
+       if (vrf_name)
+               vrf = vrf_lookup_by_name(vrf_name);
        else
                vrf = vrf_lookup_by_id(VRF_DEFAULT);
 
        if (!vrf) {
-               vty_out(vty, "Specified: %s is non-existent\n", name);
+               vty_out(vty, "Specified: %s is non-existent\n", vrf_name);
                return CMD_WARNING_CONFIG_FAILED;
        }
 
@@ -349,7 +375,7 @@ DEFPY(pbr_map_nexthop, pbr_map_nexthop_cmd,
 
                if (pbrms->nhg->nexthop) {
                        vty_out(vty,
-                               "If you would like more than one nexthop please use nexthop-groups");
+                               "If you would like more than one nexthop please use nexthop-groups\n");
                        return CMD_WARNING_CONFIG_FAILED;
                }
 
@@ -363,7 +389,9 @@ DEFPY(pbr_map_nexthop, pbr_map_nexthop_cmd,
                pbr_map_check(pbrms);
        }
 
-       if (nhop.type == NEXTHOP_TYPE_IFINDEX) {
+       if (nhop.type == NEXTHOP_TYPE_IFINDEX
+           || (nhop.type == NEXTHOP_TYPE_IPV6_IFINDEX
+               && IN6_IS_ADDR_LINKLOCAL(&nhop.gate.ipv6))) {
                struct interface *ifp;
 
                ifp = if_lookup_by_index(nhop.ifindex, nhop.vrf_id);
@@ -374,6 +402,61 @@ DEFPY(pbr_map_nexthop, pbr_map_nexthop_cmd,
        return CMD_SUCCESS;
 }
 
+DEFPY(pbr_map_vrf, pbr_map_vrf_cmd,
+      "[no] set vrf <NAME$vrf_name|unchanged>",
+      NO_STR
+      "Set for the PBR-MAP\n"
+      "Specify the VRF for this map\n"
+      "The VRF Name\n"
+      "Use the interface's VRF for lookup\n")
+{
+       struct pbr_map_sequence *pbrms = VTY_GET_CONTEXT(pbr_map_sequence);
+       int ret = CMD_SUCCESS;
+
+       if (no) {
+               pbr_map_delete_vrf(pbrms);
+
+               /* Reset all data */
+               pbrms->nhs_installed = false;
+               pbrms->vrf_name[0] = '\0';
+               pbrms->vrf_lookup = false;
+               pbrms->vrf_unchanged = false;
+
+               goto done;
+       }
+
+       if (pbrms->nhgrp_name || pbrms->nhg) {
+               vty_out(vty,
+                       "A `set nexthop/nexthop-group XX` command already exits, please remove that first\n");
+               ret = CMD_WARNING_CONFIG_FAILED;
+               goto done;
+       }
+
+       if (pbrms->vrf_lookup || pbrms->vrf_unchanged) {
+               vty_out(vty, SET_VRF_EXISTS_STR);
+               ret = CMD_WARNING_CONFIG_FAILED;
+               goto done;
+       }
+
+       if (vrf_name) {
+               if (!pbr_vrf_lookup_by_name(vrf_name)) {
+                       vty_out(vty, "Specified: %s is non-existent\n",
+                               vrf_name);
+                       ret = CMD_WARNING_CONFIG_FAILED;
+                       goto done;
+               }
+
+               pbrms->vrf_lookup = true;
+               strlcpy(pbrms->vrf_name, vrf_name, sizeof(pbrms->vrf_name));
+       } else
+               pbrms->vrf_unchanged = true;
+
+       pbr_map_check(pbrms);
+
+done:
+       return ret;
+}
+
 DEFPY (pbr_policy,
        pbr_policy_cmd,
        "[no] pbr-policy PBRMAP$mapname",
@@ -502,6 +585,12 @@ DEFPY (show_pbr_map,
                                                pbrms->internal_nhg_name),
                                        pbr_nht_get_table(
                                                pbrms->internal_nhg_name));
+                       } else if (pbrms->vrf_unchanged) {
+                               vty_out(vty,
+                                       "\tVRF Unchanged (use interface vrf)\n");
+                       } else if (pbrms->vrf_lookup) {
+                               vty_out(vty, "\tVRF Lookup: %s\n",
+                                       pbrms->vrf_name);
                        } else {
                                vty_out(vty,
                                        "\tNexthop-Group: Unknown Installed: 0(0)\n");
@@ -664,6 +753,12 @@ static int pbr_vty_map_config_write_sequence(struct vty *vty,
        if (pbrms->mark)
                vty_out(vty, " match mark %u\n", pbrms->mark);
 
+       if (pbrms->vrf_unchanged)
+               vty_out(vty, " set vrf unchanged\n");
+
+       if (pbrms->vrf_lookup)
+               vty_out(vty, " set vrf %s\n", pbrms->vrf_name);
+
        if (pbrms->nhgrp_name)
                vty_out(vty, " set nexthop-group %s\n", pbrms->nhgrp_name);
 
@@ -739,6 +834,7 @@ void pbr_vty_init(void)
        install_element(PBRMAP_NODE, &pbr_map_match_mark_cmd);
        install_element(PBRMAP_NODE, &pbr_map_nexthop_group_cmd);
        install_element(PBRMAP_NODE, &pbr_map_nexthop_cmd);
+       install_element(PBRMAP_NODE, &pbr_map_vrf_cmd);
        install_element(VIEW_NODE, &show_pbr_cmd);
        install_element(VIEW_NODE, &show_pbr_map_cmd);
        install_element(VIEW_NODE, &show_pbr_interface_cmd);