diff options
author | Pierre Pfister <ppfister@cisco.com> | 2016-05-19 14:45:18 +0100 |
---|---|---|
committer | Pierre Pfister <ppfister@cisco.com> | 2016-05-19 14:45:18 +0100 |
commit | 83ddb4c4bf72b8317d8eda81dbfe3604c62b7f16 (patch) | |
tree | 5a55904ca3817b63225befb6165383ee08c635a1 /netlink | |
parent | 577cb66a026ecb1395bda1ff941462940420fe24 (diff) |
[Netlink] Add format function to get namespace path
Change-Id: I5a3e955696ea4b1b866f6217c375cfce8d722681
Signed-off-by: Pierre Pfister <ppfister@cisco.com>
Diffstat (limited to 'netlink')
-rw-r--r-- | netlink/librtnl/rtnl.c | 27 | ||||
-rw-r--r-- | netlink/librtnl/rtnl.h | 6 |
2 files changed, 19 insertions, 14 deletions
diff --git a/netlink/librtnl/rtnl.c b/netlink/librtnl/rtnl.c index 7f017a8..a0726c9 100644 --- a/netlink/librtnl/rtnl.c +++ b/netlink/librtnl/rtnl.c @@ -75,6 +75,18 @@ static vlib_node_registration_t rtnl_process_node; #define RTNL_BUFFSIZ 16384 #define RTNL_DUMP_TIMEOUT 1 +u8 *format_rtnl_nsname2path(u8 *s, va_list *args) +{ + char *nsname = va_arg(*args, char *); + if (!nsname || !strlen(nsname)) { + return format(s, "/proc/self/ns/net"); + } else if (strpbrk(nsname, "/") != NULL) { + return format(s, "%s", nsname); + } else { + return format((u8 *)0, "/var/run/netns/%s", nsname); + } +} + static_always_inline void rtnl_schedule_timeout(rtnl_ns_t *ns, f64 when) { @@ -177,13 +189,8 @@ int rtnl_exec_in_namespace(u32 stream_index, void *(*fn)(void *), void *arg, voi int rtnl_exec_in_namespace_by_name(char *nsname, void *(*fn)(void *), void *arg, void **ret) { - u8 *s; int fd; - if (nsname && strlen(nsname)) { - s = format(0, "/var/run/netns/%s", nsname); - } else { - s = format(0, "/proc/self/ns/net"); - } + u8 *s = format((u8 *)0, "%U", format_rtnl_nsname2path, nsname); if ((fd = open((char *)s, O_RDONLY)) < 0) { vec_free(s); @@ -539,13 +546,7 @@ rtnl_stream_open(rtnl_stream_t *template) rtnl_main_t *rm = &rtnl_main; rtnl_ns_t *ns; int fd; - u8 *s; - - if (strlen(template->name)) { - s = format(0, "/var/run/netns/%s", template->name); - } else { - s = format(0, "/proc/self/ns/net"); - } + u8 *s = format((u8 *)0, "%U", format_rtnl_nsname2path, template->name); if ((fd = open((char *)s, O_RDONLY)) < 0) { vec_free(s); diff --git a/netlink/librtnl/rtnl.h b/netlink/librtnl/rtnl.h index 4b4aeff..49f0f27 100644 --- a/netlink/librtnl/rtnl.h +++ b/netlink/librtnl/rtnl.h @@ -16,6 +16,8 @@ #ifndef RTNL_H_ #define RTNL_H_ +#include <vlib/vlib.h> + #include <linux/netlink.h> #include <vppinfra/clib.h> @@ -23,7 +25,7 @@ typedef enum { RTNL_ERR_UNKNOWN, } rtnl_error_t; -#define RTNL_NETNS_NAMELEN 31 +#define RTNL_NETNS_NAMELEN 128 /* * RTNL stream implements an RTNL overlay @@ -53,4 +55,6 @@ void rtnl_stream_close(u32 handle); int rtnl_exec_in_namespace(u32 handle, void *(*fn)(void *), void *arg, void **ret); int rtnl_exec_in_namespace_by_name(char *nsname, void *(*fn)(void *), void *arg, void **ret); +u8 *format_rtnl_nsname2path(u8 *s, va_list *args); + #endif |