Skip to content

Commit

Permalink
lib: externalise vrf and ns creation
Browse files Browse the repository at this point in the history
In addition to have the possibility to create from vty vrf based on a
netns backend, the API will be made accessible from external, especially
for zebra that will handle the netns discovery part. This commit is
externalising following functions:
- netns_pathname
- ns_handler_create
- vrf_handler_create

Also, the VRF initialisation case when under NETNS backend is changed,
since the NS identifier may not be known at the configuration time,but
may be known later, under discovery process.

Signed-off-by: Philippe Guibert <philippe.guibert@6wind.com>
  • Loading branch information
pguibert6WIND committed Feb 27, 2018
1 parent fbb65ff commit 697d3ec
Show file tree
Hide file tree
Showing 4 changed files with 140 additions and 41 deletions.
105 changes: 79 additions & 26 deletions lib/ns.c
Expand Up @@ -236,6 +236,7 @@ static int ns_is_enabled(struct ns *ns)
*/
static int ns_enable(struct ns *ns)
{
int vrf_on = 0;

if (!ns_is_enabled(ns)) {
if (have_netns()) {
Expand All @@ -252,13 +253,26 @@ static int ns_enable(struct ns *ns)
return 0;
}

/* Non default NS. leave */
if (ns->ns_id == NS_UNKNOWN) {
zlog_err("Can not enable NS %s %u: Invalid NSID",
ns->name, ns->ns_id);
return 0;
}
vrf_on = vrf_update_vrf_id((vrf_id_t)ns->ns_id,
(struct vrf *)ns->vrf_ctxt);
if (have_netns())
zlog_info("NS %u is associated with NETNS %s.",
ns->ns_id, ns->name);

zlog_info("NS %u is enabled.", ns->ns_id);
/* zebra first receives NS enable event,
* then VRF enable event
*/
if (ns_master.ns_enable_hook)
(*ns_master.ns_enable_hook)(ns->ns_id, &ns->info);
if (vrf_on == 1)
vrf_enable((struct vrf *)ns->vrf_ctxt);
}

return 1;
Expand Down Expand Up @@ -310,7 +324,7 @@ void ns_add_hook(int type, int (*func)(ns_id_t, void **))
* NS realization with NETNS
*/

static char *ns_netns_pathname(struct vty *vty, const char *name)
char *ns_netns_pathname(struct vty *vty, const char *name)
{
static char pathname[PATH_MAX];
char *result;
Expand All @@ -325,7 +339,9 @@ static char *ns_netns_pathname(struct vty *vty, const char *name)
}

if (!result) {
vty_out(vty, "Invalid pathname: %s\n", safe_strerror(errno));
if (vty)
vty_out(vty, "Invalid pathname: %s\n",
safe_strerror(errno));
return NULL;
}
return pathname;
Expand Down Expand Up @@ -413,59 +429,73 @@ DEFUN (no_ns_logicalrouter,
return CMD_SUCCESS;
}

DEFUN_NOSH (ns_netns,
ns_netns_cmd,
"netns NAME",
"Attach VRF to a Namespace\n"
"The file name in " NS_RUN_DIR ", or a full pathname\n")
int ns_handler_create(struct vty *vty, struct vrf *vrf,
char *pathname, ns_id_t ns_id)
{
int idx_name = 1;
struct ns *ns = NULL;
char *pathname = ns_netns_pathname(vty, argv[idx_name]->arg);

VTY_DECLVAR_CONTEXT(vrf, vrf);

if (!pathname)
return CMD_WARNING_CONFIG_FAILED;

if (!vrf)
return CMD_WARNING_CONFIG_FAILED;
if (vrf->vrf_id != VRF_UNKNOWN && vrf->ns_ctxt == NULL) {
vty_out(vty, "VRF %u is already configured with VRF %s\n",
vrf->vrf_id, vrf->name);
if (vty)
vty_out(vty,
"VRF %u is already configured with VRF %s\n",
vrf->vrf_id, vrf->name);
else
zlog_warn("VRF %u is already configured with VRF %s\n",
vrf->vrf_id, vrf->name);
return CMD_WARNING_CONFIG_FAILED;
}
if (vrf->ns_ctxt != NULL) {
ns = (struct ns *) vrf->ns_ctxt;
if (ns && 0 != strcmp(ns->name, pathname)) {
vty_out(vty, "VRF %u is already configured"
" with NETNS %s\n",
vrf->vrf_id, ns->name);
if (vty)
vty_out(vty,
"VRF %u is already configured"
" with NETNS %s\n",
vrf->vrf_id, ns->name);
else
zlog_warn("VRF %u is already configured with NETNS %s",
vrf->vrf_id, ns->name);
return CMD_WARNING_CONFIG_FAILED;
}
}
ns = ns_lookup_name(pathname);
if (ns && ns->vrf_ctxt) {
struct vrf *vrf2 = (struct vrf *)ns->vrf_ctxt;

vty_out(vty, "NS %s is already configured"
" with VRF %u(%s)\n",
ns->name, vrf2->vrf_id, vrf2->name);
if (vty)
vty_out(vty, "NS %s is already configured"
" with VRF %u(%s)\n",
ns->name, vrf2->vrf_id, vrf2->name);
else
zlog_warn("NS %s is already configured with VRF %u(%s)",
ns->name, vrf2->vrf_id, vrf2->name);
return CMD_WARNING_CONFIG_FAILED;
} else if (!ns)
ns = ns_get_by_name(pathname);

if (ns_id != ns->ns_id) {
RB_REMOVE(ns_head, &ns_tree, ns);
ns->ns_id = ns_id;
RB_INSERT(ns_head, &ns_tree, ns);
}
ns->vrf_ctxt = (void *)vrf;
vrf->ns_ctxt = (void *)ns;
if (!ns_enable(ns)) {
vty_out(vty, "Can not associate NS %u with NETNS %s\n",
ns->ns_id, ns->name);
if (vty)
vty_out(vty, "Can not associate NS %u with NETNS %s\n",
ns->ns_id, ns->name);
else
zlog_warn("Can not associate NS %u with NETNS %s",
ns->ns_id, ns->name);
return CMD_WARNING_CONFIG_FAILED;
}

vrf->ns_ctxt = (void *)ns;
ns->vrf_ctxt = (void *)vrf;
return CMD_SUCCESS;
}


static int ns_logicalrouter_config_write(struct vty *vty)
{
struct ns *ns;
Expand All @@ -481,6 +511,22 @@ static int ns_logicalrouter_config_write(struct vty *vty)
return write;
}

DEFUN_NOSH (ns_netns,
ns_netns_cmd,
"netns NAME",
"Attach VRF to a Namespace\n"
"The file name in " NS_RUN_DIR ", or a full pathname\n")
{
int idx_name = 1;
char *pathname = ns_netns_pathname(vty, argv[idx_name]->arg);

VTY_DECLVAR_CONTEXT(vrf, vrf);

if (!pathname)
return CMD_WARNING_CONFIG_FAILED;
return ns_handler_create(vty, vrf, pathname, NS_UNKNOWN);
}

DEFUN (no_ns_netns,
no_ns_netns_cmd,
"no netns [NAME]",
Expand All @@ -505,6 +551,13 @@ DEFUN (no_ns_netns,
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
*/
RB_REMOVE(ns_head, &ns_tree, ns);
ns->ns_id = NS_UNKNOWN;
RB_INSERT(ns_head, &ns_tree, ns);
ns_delete(ns);
vrf->ns_ctxt = NULL;
return CMD_SUCCESS;
Expand Down
5 changes: 5 additions & 0 deletions lib/ns.h
Expand Up @@ -24,6 +24,8 @@

#include "openbsd-tree.h"
#include "linklist.h"
#include "vty.h"
#include "vrf.h"

typedef u_int32_t ns_id_t;

Expand Down Expand Up @@ -93,5 +95,8 @@ extern void ns_terminate(void);
/* Create a socket serving for the given NS */
extern int ns_socket(int, int, int, ns_id_t);
extern void ns_cmd_init(void);
extern int ns_handler_create(struct vty *vty, struct vrf *vrf,
char *pathname, ns_id_t ns_id);
extern char *ns_netns_pathname(struct vty *vty, const char *name);

#endif /*_ZEBRA_NS_H*/
61 changes: 46 additions & 15 deletions lib/vrf.c
Expand Up @@ -64,7 +64,6 @@ struct vrf_master {
};

static int vrf_is_enabled(struct vrf *vrf);
static void vrf_disable(struct vrf *vrf);

/* VRF list existance check by name. */
struct vrf *vrf_lookup_by_name(const char *name)
Expand All @@ -84,6 +83,25 @@ static int vrf_name_compare(const struct vrf *a, const struct vrf *b)
return strcmp(a->name, b->name);
}

/* return 1 if vrf can be enabled */
int vrf_update_vrf_id(vrf_id_t vrf_id, struct vrf *vrf)
{
vrf_id_t old_vrf_id;

if (!vrf)
return 0;
old_vrf_id = vrf->vrf_id;
if (vrf_id == vrf->vrf_id)
return 0;
if (vrf->vrf_id != VRF_UNKNOWN)
RB_REMOVE(vrf_id_head, &vrfs_by_id, vrf);
vrf->vrf_id = vrf_id;
RB_INSERT(vrf_id_head, &vrfs_by_id, vrf);
if (old_vrf_id == VRF_UNKNOWN)
return 1;
return 0;
}

/* Get a VRF. If not found, create one.
* Arg:
* name - The name of the vrf. May be NULL if unknown.
Expand Down Expand Up @@ -223,7 +241,7 @@ int vrf_enable(struct vrf *vrf)
* The VRF_DELETE_HOOK callback will be called to inform
* that they must release the resources in the VRF.
*/
static void vrf_disable(struct vrf *vrf)
void vrf_disable(struct vrf *vrf)
{
if (!vrf_is_enabled(vrf))
return;
Expand Down Expand Up @@ -465,32 +483,45 @@ void vrf_configure_backend(int vrf_backend_netns)
vrf_backend = vrf_backend_netns;
}

/* vrf CLI commands */
DEFUN_NOSH (vrf,
vrf_cmd,
"vrf NAME",
"Select a VRF to configure\n"
"VRF's name\n")
int vrf_handler_create(struct vty *vty, const char *vrfname, struct vrf **vrf)
{
int idx_name = 1;
const char *vrfname = argv[idx_name]->arg;
struct vrf *vrfp;

if (strlen(vrfname) > VRF_NAMSIZ) {
vty_out(vty,
"%% VRF name %s is invalid: length exceeds "
"%d characters\n",
vrfname, VRF_NAMSIZ);
if (vty)
vty_out(vty,
"%% VRF name %s invalid: length exceeds %d bytes\n",
vrfname, VRF_NAMSIZ);
else
zlog_warn(
"%% VRF name %s invalid: length exceeds %d bytes\n",
vrfname, VRF_NAMSIZ);
return CMD_WARNING_CONFIG_FAILED;
}

vrfp = vrf_get(VRF_UNKNOWN, vrfname);

VTY_PUSH_CONTEXT(VRF_NODE, vrfp);
if (vty)
VTY_PUSH_CONTEXT(VRF_NODE, vrfp);

if (vrf)
*vrf = vrfp;
return CMD_SUCCESS;
}

/* vrf CLI commands */
DEFUN_NOSH (vrf,
vrf_cmd,
"vrf NAME",
"Select a VRF to configure\n"
"VRF's name\n")
{
int idx_name = 1;
const char *vrfname = argv[idx_name]->arg;

return vrf_handler_create(vty, vrfname, NULL);
}

DEFUN_NOSH (no_vrf,
no_vrf_cmd,
"no vrf NAME",
Expand Down
10 changes: 10 additions & 0 deletions lib/vrf.h
Expand Up @@ -212,6 +212,16 @@ extern int vrf_socket(int, int, int, vrf_id_t);
extern void vrf_configure_backend(int vrf_backend_netns);
extern int vrf_get_backend(void);
extern int vrf_is_backend_netns(void);
extern int vrf_handler_create(struct vty *vty,
const char *name,
struct vrf **vrf);

/* used by NS when vrf backend is NS.
* Notify a change in the VRF ID of the VRF
*/
extern int vrf_update_vrf_id(vrf_id_t vrf_id, struct vrf *vrf);
extern void vrf_disable(struct vrf *vrf);
extern int vrf_enable(struct vrf *vrf);

/*
* VRF Debugging
Expand Down

0 comments on commit 697d3ec

Please sign in to comment.