From f93e3f696cee4b664b579c7de593d0cbe581aa66 Mon Sep 17 00:00:00 2001 From: Donald Sharp Date: Mon, 1 Feb 2016 09:38:33 -0800 Subject: [PATCH] lib: Allow vrf under the cli Add the infrastructure to allow the vrf keyword. Signed-off-by: Vipin Kumar Signed-off-by: Donald Sharp --- lib/command.c | 2 ++ lib/command.h | 1 + lib/if.c | 88 +++++++++++++++++++++++++++++++++++++++++++++++++-- lib/if.h | 5 +++ 4 files changed, 93 insertions(+), 3 deletions(-) diff --git a/lib/command.c b/lib/command.c index 5e8259dd39..ed337cbccf 100644 --- a/lib/command.c +++ b/lib/command.c @@ -2929,6 +2929,7 @@ DEFUN (config_exit, vty_config_unlock (vty); break; case INTERFACE_NODE: + case VRF_NODE: case ZEBRA_NODE: case BGP_NODE: case RIP_NODE: @@ -2980,6 +2981,7 @@ DEFUN (config_end, break; case CONFIG_NODE: case INTERFACE_NODE: + case VRF_NODE: case ZEBRA_NODE: case RIP_NODE: case RIPNG_NODE: diff --git a/lib/command.h b/lib/command.h index f9f4c7e397..c964b8a807 100644 --- a/lib/command.h +++ b/lib/command.h @@ -74,6 +74,7 @@ enum node_type KEYCHAIN_NODE, /* Key-chain node. */ KEYCHAIN_KEY_NODE, /* Key-chain key node. */ INTERFACE_NODE, /* Interface mode node. */ + VRF_NODE, /* VRF mode node. */ ZEBRA_NODE, /* zebra connection node. */ TABLE_NODE, /* rtm_table selection node. */ RIP_NODE, /* RIP protocol mode node. */ diff --git a/lib/if.c b/lib/if.c index d4bd918e95..6a54d242a0 100644 --- a/lib/if.c +++ b/lib/if.c @@ -154,6 +154,31 @@ if_create (const char *name, int namelen) return if_create_vrf (name, namelen, VRF_DEFAULT); } +/* Create new interface structure. */ +void +if_update_vrf (struct interface *ifp, const char *name, int namelen, vrf_id_t vrf_id) +{ + struct list *intf_list = vrf_iflist_get (vrf_id); + + /* remove interface from old master vrf list */ + if (vrf_iflist (ifp->vrf_id)) + listnode_delete (vrf_iflist (ifp->vrf_id), ifp); + + assert (name); + assert (namelen <= INTERFACE_NAMSIZ); /* Need space for '\0' at end. */ + strncpy (ifp->name, name, namelen); + ifp->name[namelen] = '\0'; + ifp->vrf_id = vrf_id; + if (if_lookup_by_name_vrf (ifp->name, vrf_id) == NULL) + listnode_add_sort (intf_list, ifp); + else + zlog_err("if_create(%s): corruption detected -- interface with this " + "name exists already in VRF %u!", ifp->name, vrf_id); + + return; +} + + /* Delete interface structure. */ void if_delete_retain (struct interface *ifp) @@ -682,8 +707,10 @@ DEFUN (interface, return CMD_WARNING; } +/*Pending: need proper vrf name based lookup/(possible creation of VRF) + Imagine forward reference of a vrf by name in this interface config */ if (argc > 1) - VTY_GET_INTEGER ("VRF ID", vrf_id, argv[1]); + VRF_GET_ID (vrf_id, argv[1]); #ifdef SUNOS_5 ifp = if_sunwzebra_get (argv[0], sl, vrf_id); @@ -716,7 +743,7 @@ DEFUN_NOSH (no_interface, vrf_id_t vrf_id = VRF_DEFAULT; if (argc > 1) - VTY_GET_INTEGER ("VRF ID", vrf_id, argv[1]); + VRF_GET_ID (vrf_id, argv[1]); ifp = if_lookup_by_name_vrf (argv[0], vrf_id); @@ -746,6 +773,61 @@ ALIAS (no_interface, "Interface's name\n" VRF_CMD_HELP_STR) +DEFUN (vrf, + vrf_cmd, + "vrf NAME", + "Select a VRF to configure\n" + "VRF's name\n") +{ + struct vrf *vrfp; + size_t sl; + + if ((sl = strlen(argv[0])) > VRF_NAMSIZ) + { + vty_out (vty, "%% VRF name %s is invalid: length exceeds " + "%d characters%s", + argv[0], VRF_NAMSIZ, VTY_NEWLINE); + return CMD_WARNING; + } + + vrfp = vrf_get_by_name_len (argv[0], sl); + + vty->index = vrfp; + vty->node = VRF_NODE; + + return CMD_SUCCESS; +} + +DEFUN_NOSH (no_vrf, + no_vrf_cmd, + "no vrf NAME", + NO_STR + "Delete a pseudo VRF's configuration\n" + "VRF's name\n") +{ + struct vrf *vrfp; + + vrfp = vrf_list_lookup_by_name (argv[0]); + + if (vrfp == NULL) + { + vty_out (vty, "%% VRF %s does not exist%s", argv[0], VTY_NEWLINE); + return CMD_WARNING; + } + + if (CHECK_FLAG (vrfp->status, ZEBRA_VRF_ACTIVE)) + { + vty_out (vty, "%% Only inactive VRFs can be deleted%s", + VTY_NEWLINE); + return CMD_WARNING; + } + + vrf_delete(vrfp); + + return CMD_SUCCESS; +} + + /* For debug purpose. */ DEFUN (show_address, show_address_cmd, @@ -761,7 +843,7 @@ DEFUN (show_address, vrf_id_t vrf_id = VRF_DEFAULT; if (argc > 0) - VTY_GET_INTEGER ("VRF ID", vrf_id, argv[0]); + VRF_GET_ID (vrf_id, argv[0]); for (ALL_LIST_ELEMENTS_RO (vrf_iflist (vrf_id), node, ifp)) { diff --git a/lib/if.h b/lib/if.h index f7a5a5ceef..b0f620352b 100644 --- a/lib/if.h +++ b/lib/if.h @@ -146,6 +146,7 @@ struct interface struct if_data stats; #endif /* HAVE_NET_RT_IFLIST */ + struct route_node *node; vrf_id_t vrf_id; }; @@ -266,6 +267,8 @@ extern struct interface *if_lookup_exact_address (void *matchaddr, int family); extern struct interface *if_lookup_address (void *matchaddr, int family); extern struct interface *if_lookup_prefix (struct prefix *prefix); +extern void if_update_vrf (struct interface *, const char *name, int namelen, + vrf_id_t vrf_id); extern struct interface *if_create_vrf (const char *name, int namelen, vrf_id_t vrf_id); extern struct interface *if_lookup_by_index_vrf (unsigned int, @@ -370,5 +373,7 @@ extern struct cmd_element no_interface_pseudo_cmd; extern struct cmd_element show_address_cmd; extern struct cmd_element show_address_vrf_cmd; extern struct cmd_element show_address_vrf_all_cmd; +extern struct cmd_element vrf_cmd; +extern struct cmd_element no_vrf_cmd; #endif /* _ZEBRA_IF_H */ -- 2.39.5