Skip to content

Commit

Permalink
staticd : northbound backend code
Browse files Browse the repository at this point in the history
basic routing northbound callbacks
vrf northbound callbacks
staticd northbound callbacks
tag, distance, tableid is the property of route

Signed-off-by: VishalDhingra <vdhingra@vmware.com>
  • Loading branch information
vishaldhingra committed Mar 18, 2020
1 parent 3e31357 commit 14de7c2
Show file tree
Hide file tree
Showing 23 changed files with 1,449 additions and 438 deletions.
65 changes: 65 additions & 0 deletions lib/routing_nb.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
#include "northbound.h"
#include "libfrr.h"
#include "vrf.h"
#include "routing_nb.h"


static int lib_routing_cp_create(enum nb_event event,
const struct lyd_node *dnode,
union nb_resource *resource)
{
struct vrf *vrf;
const char *vrfname;

switch (event) {
case NB_EV_VALIDATE:
vrfname = yang_dnode_get_string(dnode, "./vrf");
vrf = vrf_lookup_by_name(vrfname);
if (!vrf) {
zlog_warn(
"vrf is not configured\n");
return NB_ERR_VALIDATION;
}

break;
case NB_EV_PREPARE:
case NB_EV_ABORT:
break;
case NB_EV_APPLY:
vrfname = yang_dnode_get_string(dnode, "./vrf");
vrf = vrf_lookup_by_name(vrfname);
nb_running_set_entry(dnode, vrf);
break;
};

return NB_OK;
}

static int lib_routing_cp_destroy(enum nb_event event,
const struct lyd_node *dnode)
{
struct vrf *vrf;

if (event != NB_EV_APPLY)
return NB_OK;

vrf = nb_running_unset_entry(dnode);
(void)vrf;
return NB_OK;
}

const struct frr_yang_module_info frr_routing_info = {
.name = "frr-routing",
.nodes = {
{
.xpath = FRR_ROUTING_XPATH,
.cbs = {
.create = lib_routing_cp_create,
.destroy = lib_routing_cp_destroy,
},
},
{
.xpath = NULL,
},
}
};
17 changes: 17 additions & 0 deletions lib/routing_nb.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
#ifndef _FRR_ROUTING_NB_H_
#define _FRR_ROUTING_NB_H_


#define FRR_ROUTING_XPATH \
"/frr-routing:routing/control-plane-protocols/control-plane-protocol"

#define FRR_ROUTING_KEY_XPATH \
"/frr-routing:routing/control-plane-protocols/control-plane-protocol[type='%s'][name='%s'][vrf='%s']"

extern const struct frr_yang_module_info frr_routing_info;

/* Mandatory callbacks. */



#endif /* _FRR_ROUTING_NB_H_ */
3 changes: 3 additions & 0 deletions lib/subdir.am
Original file line number Diff line number Diff line change
Expand Up @@ -103,6 +103,7 @@ lib_libfrr_la_SOURCES = \
lib/printf/printf-pos.c \
lib/printf/vfprintf.c \
lib/printf/glue.c \
lib/routing_nb.c \
# end

nodist_lib_libfrr_la_SOURCES = \
Expand All @@ -112,6 +113,7 @@ nodist_lib_libfrr_la_SOURCES = \
yang/frr-route-types.yang.c \
yang/frr-vrf.yang.c \
yang/frr-routing.yang.c \
yang/frr-nexthop.yang.c \
yang/ietf/ietf-routing-types.yang.c \
yang/frr-module-translator.yang.c \
# end
Expand Down Expand Up @@ -257,6 +259,7 @@ pkginclude_HEADERS += \
lib/zclient.h \
lib/zebra.h \
lib/pbr.h \
lib/routing_nb.h \
# end


Expand Down
202 changes: 184 additions & 18 deletions lib/vrf.c
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@
#include "privs.h"
#include "nexthop_group.h"
#include "lib_errors.h"
#include "northbound_cli.h"

/* default VRF ID value used when VRF backend is not NETNS */
#define VRF_DEFAULT_INTERNAL 0
Expand Down Expand Up @@ -714,10 +715,32 @@ DEFUN_NOSH (vrf,
"Select a VRF to configure\n"
"VRF's name\n")
{
char xpath_list[XPATH_MAXLEN];
int idx_name = 1;
int ret;
const char *vrfname = argv[idx_name]->arg;
struct vrf *vrfp;

snprintf(xpath_list, sizeof(xpath_list),
"/frr-vrf:lib/vrf[name='%s']",
vrfname);

nb_cli_enqueue_change(vty, xpath_list, NB_OP_CREATE, NULL);
ret = nb_cli_apply_changes(vty, xpath_list);

if (ret == CMD_SUCCESS) {
/*
* For backward compatibility with old commands we still need
* to use the qobj infrastructure. This can be removed once
* all vrf-level commands are converted to the new
* northbound model.
*/
vrfp = vrf_lookup_by_name(vrfname);
if (vrfp)
VTY_PUSH_CONTEXT(VRF_NODE, vrfp);
}

return vrf_handler_create(vty, vrfname, NULL);
return ret;
}

DEFUN (no_vrf,
Expand All @@ -727,27 +750,18 @@ DEFUN (no_vrf,
"Delete a pseudo VRF's configuration\n"
"VRF's name\n")
{
char xpath_list[XPATH_MAXLEN];
const char *vrfname = argv[2]->arg;
int ret;

struct vrf *vrfp;

vrfp = vrf_lookup_by_name(vrfname);
snprintf(xpath_list, sizeof(xpath_list),
"/frr-vrf:lib/vrf[name='%s']",
vrfname);

if (vrfp == NULL) {
vty_out(vty, "%% VRF %s does not exist\n", vrfname);
return CMD_WARNING_CONFIG_FAILED;
}
nb_cli_enqueue_change(vty, ".", NB_OP_DESTROY, NULL);
ret = nb_cli_apply_changes(vty, xpath_list);

if (CHECK_FLAG(vrfp->status, VRF_ACTIVE)) {
vty_out(vty, "%% Only inactive VRFs can be deleted\n");
return CMD_WARNING_CONFIG_FAILED;
}

/* Clear configured flag and invoke delete. */
UNSET_FLAG(vrfp->status, VRF_CONFIGURED);
vrf_delete(vrfp);

return CMD_SUCCESS;
return ret;
}


Expand Down Expand Up @@ -1012,3 +1026,155 @@ 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;

if (event != NB_EV_APPLY)
return NB_OK;

vrfname = yang_dnode_get_string(dnode, "./name");

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_warn(
"Only inactive VRFs can be deleted\n");
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;
}

/*
* XPath: /frr-vrf:lib/vrf
*/
static const void *lib_vrf_get_next(const void *parent_list_entry,
const void *list_entry)
{
return NULL;
}

static int lib_vrf_get_keys(const void *list_entry,
struct yang_list_keys *keys)
{
return NB_OK;
}

static const void *lib_vrf_lookup_entry(const void *parent_list_entry,
const struct yang_list_keys *keys)
{

return NULL;
}


/*
* XPath: /frr-vrf:lib/vrf/id
*/
struct yang_data * lib_vrf_id_get_elem(const char *xpath,
const void *list_entry)
{
return NULL;
}

/*
* XPath: /frr-vrf:lib/vrf/active
*/
struct yang_data * lib_vrf_active_get_elem(const char *xpath,
const void *list_entry)
{
return NULL;
}

/*
* XPath: /frr-vrf:lib/vrf/netns/name
*/
static int lib_vrf_netns_name_modify(enum nb_event event,
const struct lyd_node *dnode,
union nb_resource *resource)
{
return NB_OK;
}

static int lib_vrf_netns_name_destroy(enum nb_event event,
const struct lyd_node *dnode)
{
return NB_OK;
}

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/id",
.cbs = {
.get_elem = lib_vrf_id_get_elem,
},
},
{
.xpath = "/frr-vrf:lib/vrf/active",
.cbs = {
.get_elem = lib_vrf_active_get_elem,
},
},
{
.xpath = "/frr-vrf:lib/vrf/netns/name",
.cbs = {
.modify = lib_vrf_netns_name_modify,
.destroy = lib_vrf_netns_name_destroy,
},
},
{
.xpath = NULL,
},
}
};
8 changes: 8 additions & 0 deletions lib/vrf.h
Original file line number Diff line number Diff line change
Expand Up @@ -321,6 +321,14 @@ extern void vrf_disable(struct vrf *vrf);
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;

/* ------- Northbound callbacks ------- */

struct yang_data * lib_vrf_id_get_elem(const char *xpath,
const void *list_entry);
struct yang_data * lib_vrf_active_get_elem(const char *xpath,
const void *list_entry);

#ifdef __cplusplus
}
Expand Down
21 changes: 21 additions & 0 deletions lib/yang.c
Original file line number Diff line number Diff line change
Expand Up @@ -688,3 +688,24 @@ void yang_terminate(void)

ly_ctx_destroy(ly_native_ctx, NULL);
}

const struct lyd_node *yang_dnode_get_pleaf(const struct lyd_node *dnode,
const char *lname)
{
const struct lyd_node *orig_dnode = dnode;

while (orig_dnode) {
switch (orig_dnode->schema->nodetype) {
case LYS_LIST:
if(!strcmp(orig_dnode->schema->name, lname))
return orig_dnode;
break;
default:
break;
}

orig_dnode = orig_dnode->parent;
}

return NULL;
}
Loading

0 comments on commit 14de7c2

Please sign in to comment.