diff options
| author | Philippe Guibert <philippe.guibert@6wind.com> | 2018-01-16 13:59:58 +0100 |
|---|---|---|
| committer | Philippe Guibert <philippe.guibert@6wind.com> | 2018-02-27 11:11:24 +0100 |
| commit | ec31f30d28b65a4820a44ac658e677840ed6f88b (patch) | |
| tree | 12d7828e18031db13e71f2fa0d5f8070246fb966 /lib/ns.c | |
| parent | e27dec3cf15d627cc26c07f40ded138b0bcc6a0a (diff) | |
zebra: upon startup, a NSID is assigned to default netns
when the netns backend is selected for VRF, the default VRF is being
assigned a NSID. This avoids the need to handle the case where if the
incoming NSID was 0 for a non default VRF, then a specific handling had
to be done to keep 0 value for default VRF.
In most cases, as the first NETNS to get a NSID will be the default VRF,
most probably the default VRF will be assigned to 0, while the other
ones will have their value incremented. On some cases, where the NSID is
already assigned for NETNS, including default VRF, then the default VRF
value will be the one derived from the NSID of default VRF, thus keeping
consistency between VRF IDs and NETNS IDs.
Default NS is attempted to be created. Actually, some VMs may have the
netns feature, but the NS initialisation fails because that folder is
not present.
Signed-off-by: Philippe Guibert <philippe.guibert@6wind.com>
Diffstat (limited to 'lib/ns.c')
| -rw-r--r-- | lib/ns.c | 35 |
1 files changed, 22 insertions, 13 deletions
@@ -51,6 +51,7 @@ RB_GENERATE(ns_head, ns, entry, ns_compare) struct ns_head ns_tree = RB_INITIALIZER(&ns_tree); +static struct ns *default_ns; static int ns_current_ns_fd; static int ns_default_ns_fd; @@ -71,16 +72,12 @@ static inline int setns(int fd, int nstype) #endif /* HAVE_SETNS */ #ifdef HAVE_NETNS - -#define NS_DEFAULT_NAME "/proc/self/ns/net" static int have_netns_enabled = -1; - -#else /* !HAVE_NETNS */ - -#define NS_DEFAULT_NAME "Default-logical-router" - #endif /* HAVE_NETNS */ +/* default NS ID value used when VRF backend is not NETNS */ +#define NS_DEFAULT_INTERNAL 0 + static int have_netns(void) { #ifdef HAVE_NETNS @@ -625,24 +622,28 @@ void ns_init(void) } #endif /* HAVE_NETNS */ ns_default_ns_fd = -1; + default_ns = NULL; } /* Initialize NS module. */ -void ns_init_zebra(void) +void ns_init_zebra(ns_id_t default_ns_id) { - struct ns *default_ns; + int fd; ns_init(); - /* The default NS always exists. */ - default_ns = ns_get(NS_DEFAULT); - ns_current_ns_fd = -1; + default_ns = ns_get(default_ns_id); if (!default_ns) { zlog_err("ns_init: failed to create the default NS!"); exit(1); } - + if (have_netns()) { + fd = open(NS_DEFAULT_NAME, O_RDONLY); + default_ns->fd = fd; + } + ns_current_ns_fd = -1; /* Set the default NS name. */ default_ns->name = XSTRDUP(MTYPE_NS_NAME, NS_DEFAULT_NAME); + zlog_info("ns_init: default NSID is %u", default_ns->ns_id); /* Enable the default NS. */ if (!ns_enable(default_ns)) { @@ -739,3 +740,11 @@ int ns_socket(int domain, int type, int protocol, ns_id_t ns_id) return ret; } + +ns_id_t ns_get_default_id(void) +{ + if (default_ns) + return default_ns->ns_id; + return NS_UNKNOWN; +} + |
