]> git.proxmox.com Git - mirror_frr.git/blobdiff - zebra/zebra_vrf.c
lib, zebra: move vrf netns commands from lib to zebra
[mirror_frr.git] / zebra / zebra_vrf.c
index b42923640f27e684b1d6ae569a35e494f06de31f..a2a671e9574685e8cc33cfe4c96e2b092349c419 100644 (file)
@@ -41,6 +41,9 @@
 #include "zebra/zebra_vxlan.h"
 #include "zebra/zebra_netns_notify.h"
 #include "zebra/zebra_routemap.h"
+#ifndef VTYSH_EXTRACT_PL
+#include "zebra/zebra_vrf_clippy.c"
+#endif
 
 static void zebra_vrf_table_create(struct zebra_vrf *zvrf, afi_t afi,
                                   safi_t safi);
@@ -103,9 +106,7 @@ static int zebra_vrf_new(struct vrf *vrf)
        if (IS_ZEBRA_DEBUG_EVENT)
                zlog_debug("VRF %s created, id %u", vrf->name, vrf->vrf_id);
 
-       zvrf = zebra_vrf_alloc();
-       vrf->info = zvrf;
-       zvrf->vrf = vrf;
+       zvrf = zebra_vrf_alloc(vrf);
        if (!vrf_is_backend_netns())
                zvrf->zns = zebra_ns_lookup(NS_DEFAULT);
 
@@ -133,7 +134,7 @@ static int zebra_vrf_enable(struct vrf *vrf)
        else
                zvrf->zns = zebra_ns_lookup(NS_DEFAULT);
 #if defined(HAVE_RTADV)
-       rtadv_init(zvrf);
+       rtadv_vrf_init(zvrf);
 #endif
 
        /* Inform clients that the VRF is now active. This is an
@@ -338,20 +339,6 @@ static int zebra_vrf_update(struct vrf *vrf)
        return 0;
 }
 
-
-/* Return if this VRF has any FRR configuration or not.
- * IMPORTANT: This function needs to be updated when additional configuration
- * is added for a VRF.
- */
-int zebra_vrf_has_config(struct zebra_vrf *zvrf)
-{
-       /* EVPN L3-VNI? */
-       if (zvrf->l3vni)
-               return 1;
-
-       return 0;
-}
-
 /* Lookup the routing table in a VRF based on both VRF-Id and table-id.
  * NOTE: Table-id is relevant on two modes:
  * - case VRF backend is default : on default VRF only
@@ -441,12 +428,15 @@ static void zebra_vrf_table_create(struct zebra_vrf *zvrf, afi_t afi,
 }
 
 /* Allocate new zebra VRF. */
-struct zebra_vrf *zebra_vrf_alloc(void)
+struct zebra_vrf *zebra_vrf_alloc(struct vrf *vrf)
 {
        struct zebra_vrf *zvrf;
 
        zvrf = XCALLOC(MTYPE_ZEBRA_VRF, sizeof(struct zebra_vrf));
 
+       zvrf->vrf = vrf;
+       vrf->info = zvrf;
+
        zebra_vxlan_init_tables(zvrf);
        zebra_mpls_init_tables(zvrf);
        zebra_pw_init(zvrf);
@@ -541,6 +531,63 @@ static int vrf_config_write(struct vty *vty)
        return 0;
 }
 
+DEFPY (vrf_netns,
+       vrf_netns_cmd,
+       "netns NAME$netns_name",
+       "Attach VRF to a Namespace\n"
+       "The file name in " NS_RUN_DIR ", or a full pathname\n")
+{
+       char *pathname = ns_netns_pathname(vty, netns_name);
+       int ret;
+
+       VTY_DECLVAR_CONTEXT(vrf, vrf);
+
+       if (!pathname)
+               return CMD_WARNING_CONFIG_FAILED;
+
+       frr_with_privs(&zserv_privs) {
+               ret = vrf_netns_handler_create(vty, vrf, pathname,
+                                              NS_UNKNOWN,
+                                              NS_UNKNOWN,
+                                              NS_UNKNOWN);
+       }
+
+       return ret;
+}
+
+DEFUN (no_vrf_netns,
+       no_vrf_netns_cmd,
+       "no netns [NAME]",
+       NO_STR
+       "Detach VRF from a Namespace\n"
+       "The file name in " NS_RUN_DIR ", or a full pathname\n")
+{
+       struct ns *ns = NULL;
+
+       VTY_DECLVAR_CONTEXT(vrf, vrf);
+
+       if (!vrf_is_backend_netns()) {
+               vty_out(vty, "VRF backend is not Netns. Aborting\n");
+               return CMD_WARNING_CONFIG_FAILED;
+       }
+       if (!vrf->ns_ctxt) {
+               vty_out(vty, "VRF %s(%u) is not configured with NetNS\n",
+                       vrf->name, vrf->vrf_id);
+               return CMD_WARNING_CONFIG_FAILED;
+       }
+
+       ns = (struct ns *)vrf->ns_ctxt;
+
+       ns->vrf_ctxt = NULL;
+       vrf_disable(vrf);
+       /* vrf ID from VRF is necessary for Zebra
+        * so that propagate to other clients is done
+        */
+       ns_delete(ns);
+       vrf->ns_ctxt = NULL;
+       return CMD_SUCCESS;
+}
+
 /* Zebra VRF initialization. */
 void zebra_vrf_init(void)
 {
@@ -548,4 +595,10 @@ void zebra_vrf_init(void)
                 zebra_vrf_delete, zebra_vrf_update);
 
        vrf_cmd_init(vrf_config_write, &zserv_privs);
+
+       if (vrf_is_backend_netns() && ns_have_netns()) {
+               /* Install NS commands. */
+               install_element(VRF_NODE, &vrf_netns_cmd);
+               install_element(VRF_NODE, &no_vrf_netns_cmd);
+       }
 }