From 4cef6de5915d22508c3e79335fbbe226f47ad0f5 Mon Sep 17 00:00:00 2001 From: Nathan Skrzypczak Date: Mon, 19 Jul 2021 18:21:43 +0200 Subject: vppinfra: add abstract socket & netns fns * Add clib_socket_init support for abstract sockets if name starts with an '@' * Add clib_socket_init_netns to open socket in netns * Add clib_netns_open Type: feature Change-Id: I89637ad657c702ec38ddecb5c03a1673d0dfb104 Signed-off-by: Nathan Skrzypczak --- src/plugins/linux-cp/lcp_interface.c | 33 ++++++++++----------------------- 1 file changed, 10 insertions(+), 23 deletions(-) (limited to 'src/plugins/linux-cp') diff --git a/src/plugins/linux-cp/lcp_interface.c b/src/plugins/linux-cp/lcp_interface.c index 3fc91179cd1..da409619746 100644 --- a/src/plugins/linux-cp/lcp_interface.c +++ b/src/plugins/linux-cp/lcp_interface.c @@ -14,7 +14,6 @@ */ #define _GNU_SOURCE -#include #include #include #include @@ -26,6 +25,8 @@ #include #include +#include + #include #include #include @@ -614,17 +615,6 @@ lcp_validate_if_name (u8 *name) return 1; } -static int -lcp_itf_get_ns_fd (char *ns_name) -{ - char ns_path[256] = "/proc/self/ns/net"; - - if (ns_name) - snprintf (ns_path, sizeof (ns_path) - 1, "/var/run/netns/%s", ns_name); - - return open (ns_path, O_RDONLY); -} - static void lcp_itf_set_vif_link_state (u32 vif_index, u8 up, u8 *ns) { @@ -634,13 +624,10 @@ lcp_itf_set_vif_link_state (u32 vif_index, u8 up, u8 *ns) if (ns) { - u8 *ns_path = 0; - - curr_ns_fd = open ("/proc/self/ns/net", O_RDONLY); - ns_path = format (0, "/var/run/netns/%s%c", (char *) ns, 0); - vif_ns_fd = open ((char *) ns_path, O_RDONLY); + curr_ns_fd = clib_netns_open (NULL /* self */); + vif_ns_fd = clib_netns_open (ns); if (vif_ns_fd != -1) - setns (vif_ns_fd, CLONE_NEWNET); + clib_setns (vif_ns_fd); } vnet_netlink_set_link_state (vif_index, up); @@ -650,7 +637,7 @@ lcp_itf_set_vif_link_state (u32 vif_index, u8 up, u8 *ns) if (curr_ns_fd != -1) { - setns (curr_ns_fd, CLONE_NEWNET); + clib_setns (curr_ns_fd); close (curr_ns_fd); } } @@ -706,12 +693,12 @@ lcp_itf_pair_create (u32 phy_sw_if_index, u8 *host_if_name, if (ns && ns[0] != 0) { - orig_ns_fd = lcp_itf_get_ns_fd (NULL); - ns_fd = lcp_itf_get_ns_fd ((char *) ns); + orig_ns_fd = clib_netns_open (NULL /* self */); + ns_fd = clib_netns_open (ns); if (orig_ns_fd == -1 || ns_fd == -1) goto socket_close; - setns (ns_fd, CLONE_NEWNET); + clib_setns (ns_fd); } vif_index = if_nametoindex ((const char *) host_if_name); @@ -745,7 +732,7 @@ lcp_itf_pair_create (u32 phy_sw_if_index, u8 *host_if_name, socket_close: if (orig_ns_fd != -1) { - setns (orig_ns_fd, CLONE_NEWNET); + clib_setns (orig_ns_fd); close (orig_ns_fd); } if (ns_fd != -1) -- cgit 1.2.3-korg