]> git.puffer.fish Git - matthieu/frr.git/commitdiff
lib: vrf northbound callbacks
authorChirag Shah <chirag@cumulusnetworks.com>
Wed, 11 Mar 2020 01:20:49 +0000 (18:20 -0700)
committerSantosh P K <sapk@vmware.com>
Thu, 16 Apr 2020 14:55:56 +0000 (07:55 -0700)
Signed-off-by: Chirag Shah <chirag@cumulusnetworks.com>
lib/vrf.c
lib/vrf.h
lib/yang.c
yang/frr-vrf.yang

index f642aa5609dbd1e89908f22d90960ee372d6d8ec..74103f42cd680ec812bb7603110409b4bd9651a9 100644 (file)
--- a/lib/vrf.c
+++ b/lib/vrf.c
@@ -36,6 +36,7 @@
 #include "privs.h"
 #include "nexthop_group.h"
 #include "lib_errors.h"
+#include "northbound.h"
 
 /* default VRF ID value used when VRF backend is not NETNS */
 #define VRF_DEFAULT_INTERNAL 0
@@ -1019,3 +1020,147 @@ vrf_id_t vrf_generate_id(void)
 
        return ++vrf_id_local;
 }
+
+/* ------- Northbound callbacks ------- */
+
+/*
+ * XPath: /frr-vrf:lib/vrf
+ */
+static int lib_vrf_create(enum nb_event event, const struct lyd_node *dnode,
+                         union nb_resource *resource)
+{
+       const char *vrfname;
+       struct vrf *vrfp;
+
+       vrfname = yang_dnode_get_string(dnode, "./name");
+
+       if (event != NB_EV_APPLY)
+               return NB_OK;
+
+       vrfp = vrf_get(VRF_UNKNOWN, vrfname);
+
+       nb_running_set_entry(dnode, vrfp);
+
+       return NB_OK;
+}
+
+static int lib_vrf_destroy(enum nb_event event, const struct lyd_node *dnode)
+{
+       struct vrf *vrfp;
+
+
+       switch (event) {
+       case NB_EV_VALIDATE:
+               vrfp = nb_running_get_entry(dnode, NULL, true);
+               if (CHECK_FLAG(vrfp->status, VRF_ACTIVE)) {
+                       zlog_debug("%s Only inactive VRFs can be deleted",
+                                  __func__);
+                       return NB_ERR_VALIDATION;
+               }
+               break;
+       case NB_EV_PREPARE:
+       case NB_EV_ABORT:
+               break;
+       case NB_EV_APPLY:
+               vrfp = nb_running_unset_entry(dnode);
+               /* Clear configured flag and invoke delete. */
+               UNSET_FLAG(vrfp->status, VRF_CONFIGURED);
+               vrf_delete(vrfp);
+               break;
+       }
+
+       return NB_OK;
+}
+
+static const void *lib_vrf_get_next(const void *parent_list_entry,
+                                   const void *list_entry)
+{
+       struct vrf *vrfp = (struct vrf *)list_entry;
+
+       if (list_entry == NULL) {
+               vrfp = RB_MIN(vrf_name_head, &vrfs_by_name);
+       } else {
+               vrfp = RB_NEXT(vrf_name_head, vrfp);
+       }
+
+       return vrfp;
+}
+
+static int lib_vrf_get_keys(const void *list_entry, struct yang_list_keys *keys)
+{
+       struct vrf *vrfp = (struct vrf *)list_entry;
+
+       keys->num = 1;
+       strlcpy(keys->key[0], vrfp->name, sizeof(keys->key[0]));
+
+       return NB_OK;
+}
+
+static const void *lib_vrf_lookup_entry(const void *parent_list_entry,
+                                       const struct yang_list_keys *keys)
+{
+       const char *vrfname = keys->key[0];
+
+       struct vrf *vrf = vrf_lookup_by_name(vrfname);
+
+       return vrf;
+}
+
+/*
+ * XPath: /frr-vrf:lib/vrf/id
+ */
+static struct yang_data *lib_vrf_state_id_get_elem(const char *xpath,
+                                                  const void *list_entry)
+{
+       struct vrf *vrfp = (struct vrf *)list_entry;
+
+       return yang_data_new_uint32(xpath, vrfp->vrf_id);
+}
+
+/*
+ * XPath: /frr-vrf:lib/vrf/active
+ */
+static struct yang_data *lib_vrf_state_active_get_elem(const char *xpath,
+                                                      const void *list_entry)
+{
+       struct vrf *vrfp = (struct vrf *)list_entry;
+
+       if (vrfp->status == VRF_ACTIVE)
+               return yang_data_new_bool(
+                       xpath, vrfp->status == VRF_ACTIVE ? true : false);
+
+       return NULL;
+}
+
+/* clang-format off */
+const struct frr_yang_module_info frr_vrf_info = {
+       .name = "frr-vrf",
+       .nodes = {
+               {
+                       .xpath = "/frr-vrf:lib/vrf",
+                       .cbs = {
+                               .create = lib_vrf_create,
+                               .destroy = lib_vrf_destroy,
+                               .get_next = lib_vrf_get_next,
+                               .get_keys = lib_vrf_get_keys,
+                               .lookup_entry = lib_vrf_lookup_entry,
+                       }
+               },
+               {
+                       .xpath = "/frr-vrf:lib/vrf/state/id",
+                       .cbs = {
+                               .get_elem = lib_vrf_state_id_get_elem,
+                       }
+               },
+               {
+                       .xpath = "/frr-vrf:lib/vrf/state/active",
+                       .cbs = {
+                               .get_elem = lib_vrf_state_active_get_elem,
+                       }
+               },
+               {
+                       .xpath = NULL,
+               },
+       }
+};
+
index 2dc26488373b2a5a1f711872300884266cb92593..83ed16b48e4e19678050236d555a45fec801e878 100644 (file)
--- a/lib/vrf.h
+++ b/lib/vrf.h
@@ -325,6 +325,8 @@ extern int vrf_enable(struct vrf *vrf);
 extern void vrf_delete(struct vrf *vrf);
 extern vrf_id_t vrf_generate_id(void);
 
+extern const struct frr_yang_module_info frr_vrf_info;
+
 #ifdef __cplusplus
 }
 #endif
index 0502d4952d5d1d0db2d3a2d6eb857baf2cd8bf85..dc2a3e9f9bac1ff16c63065098865fdb83e08120 100644 (file)
@@ -74,6 +74,7 @@ static const char *yang_module_imp_clb(const char *mod_name,
 
 static const char *const frr_native_modules[] = {
        "frr-interface",
+       "frr-vrf",
        "frr-ripd",
        "frr-ripngd",
        "frr-isisd",
index 40ead7e79c61a0ad5158fd9ed4948573cf7bddca..4924a86e89a104fe381248896f5e27bca40a7438 100644 (file)
@@ -16,11 +16,13 @@ module frr-vrf {
       "Initial revision.";
   }
 
-  /*
-   * Network namespace feature
-   */
-  feature netns {
-    description "Abstracts network namespace as VRF.";
+  typedef vrf-ref {
+    type leafref {
+      path "/frr-vrf:lib/frr-vrf:vrf/frr-vrf:name";
+      require-instance false;
+    }
+    description
+      "Reference to a VRF";
   }
 
   container lib {
@@ -36,40 +38,23 @@ module frr-vrf {
           "VRF name.";
       }
 
-      leaf id {
-        type uint32 {
-          range "0..4294967295";
-        }
-        config false;
-        description
-          "VRF Id.";
-      }
-
-      leaf active {
-        type boolean;
-        default "false";
+      container state {
         config false;
-        description
-          "VRF active in kernel.";
-      }
+        leaf id {
+          type uint32 {
+            range "0..4294967295";
+          }
+          description
+            "VRF Id.";
+        }
 
-      container netns {
-        if-feature "netns";
-        leaf name {
-          type string;
+        leaf active {
+          type boolean;
+          default "false";
           description
-            "Namespace name.";
+            "VRF active in kernel.";
         }
       }
     }
   }
-
-  typedef vrf-ref {
-    type leafref {
-      require-instance false;
-      path "/frr-vrf:lib/frr-vrf:vrf/frr-vrf:name";
-    }
-    description
-      "Reference to a VRF";
-  }
 }