#include "command.h"
#include "vty.h"
-#ifdef HAVE_NETNS
#ifndef CLONE_NEWNET
#define CLONE_NEWNET 0x40000000 /* New network namespace (lo, device, names sockets, etc) */
}
#endif /* HAVE_SETNS */
+#ifdef HAVE_NETNS
+
#define NS_DEFAULT_NAME "/proc/self/ns/net"
+static int have_netns_enabled = -1;
#else /* !HAVE_NETNS */
#endif /* HAVE_NETNS */
+static int have_netns(void)
+{
+#ifdef HAVE_NETNS
+ if (have_netns_enabled < 0)
+ {
+ int fd = open (NS_DEFAULT_NAME, O_RDONLY);
+
+ if (fd < 0)
+ have_netns_enabled = 0;
+ else
+ {
+ have_netns_enabled = 1;
+ close(fd);
+ }
+ }
+ return have_netns_enabled;
+#else
+ return 0;
+#endif
+}
+
struct ns
{
/* Identifier, same as the vector index */
static int
ns_is_enabled (struct ns *ns)
{
-#ifdef HAVE_NETNS
- return ns && ns->fd >= 0;
-#else
- return ns && ns->fd == -2 && ns->ns_id == NS_DEFAULT;
-#endif
+ if (have_netns())
+ return ns && ns->fd >= 0;
+ else
+ return ns && ns->fd == -2 && ns->ns_id == NS_DEFAULT;
}
/*
if (!ns_is_enabled (ns))
{
-#ifdef HAVE_NETNS
- ns->fd = open (ns->name, O_RDONLY);
-#else
- ns->fd = -2; /* Remember that ns_enable_hook has been called */
- errno = -ENOTSUP;
-#endif
+ if (have_netns()) {
+ ns->fd = open (ns->name, O_RDONLY);
+ } else {
+ ns->fd = -2; /* Remember that ns_enable_hook has been called */
+ errno = -ENOTSUP;
+ }
if (!ns_is_enabled (ns))
{
return 0;
}
-#ifdef HAVE_NETNS
- zlog_info ("NS %u is associated with NETNS %s.",
- ns->ns_id, ns->name);
-#endif
+ 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);
if (ns_master.ns_enable_hook)
if (ns_master.ns_disable_hook)
(*ns_master.ns_disable_hook) (ns->ns_id, &ns->info);
-#ifdef HAVE_NETNS
- close (ns->fd);
-#endif
+ if (have_netns())
+ close (ns->fd);
+
ns->fd = -1;
}
}
NS_BITMAP_FLAG (offset)) ? 1 : 0;
}
-#ifdef HAVE_NETNS
/*
* NS realization with NETNS
*/
return write;
}
-#endif /* HAVE_NETNS */
-
/* Initialize NS module. */
void
ns_init (void)
exit (1);
}
-#ifdef HAVE_NETNS
- /* Install NS commands. */
- install_node (&ns_node, ns_config_write);
- install_element (CONFIG_NODE, &ns_netns_cmd);
- install_element (CONFIG_NODE, &no_ns_netns_cmd);
-#endif
+ if (have_netns())
+ {
+ /* Install NS commands. */
+ install_node (&ns_node, ns_config_write);
+ install_element (CONFIG_NODE, &ns_netns_cmd);
+ install_element (CONFIG_NODE, &no_ns_netns_cmd);
+ }
}
/* Terminate NS module. */
return -1;
}
-#ifdef HAVE_NETNS
- ret = (ns_id != NS_DEFAULT) ? setns (ns->fd, CLONE_NEWNET) : 0;
- if (ret >= 0)
+ if (have_netns())
{
- ret = socket (domain, type, protocol);
- if (ns_id != NS_DEFAULT)
- setns (ns_lookup (NS_DEFAULT)->fd, CLONE_NEWNET);
+ ret = (ns_id != NS_DEFAULT) ? setns (ns->fd, CLONE_NEWNET) : 0;
+ if (ret >= 0)
+ {
+ ret = socket (domain, type, protocol);
+ if (ns_id != NS_DEFAULT)
+ setns (ns_lookup (NS_DEFAULT)->fd, CLONE_NEWNET);
+ }
}
-#else
- ret = socket (domain, type, protocol);
-#endif
+ else
+ ret = socket (domain, type, protocol);
return ret;
}
default:
if (strncmp (line, "interface", strlen ("interface")) == 0)
config = config_get (INTERFACE_NODE, line);
- else if (strncmp (line, "ns", strlen ("ns")) == 0)
+ else if (strncmp (line, "logical-router", strlen ("ns")) == 0)
config = config_get (NS_NODE, line);
else if (strncmp (line, "vrf", strlen ("vrf")) == 0)
config = config_get (VRF_NODE, line);