The eswitch control message socket was created at a fixed path in
/tmp, which is world-writable and shared across DPDK instances.

Build the path under the EAL runtime directory (e.g. /run/dpdk/<prefix>)
instead, so it follows the configured prefix and is isolated per
instance.

Add cnxk_eswitch_ctrl_msg_sock_addr() to fill the sockaddr_un from the
runtime dir and use it on both the server (bind) and client (connect)
paths.

Signed-off-by: Stephen Hemminger <[email protected]>
---
 drivers/net/cnxk/cnxk_eswitch.c | 29 +++++++++++++++++++++++++----
 drivers/net/cnxk/cnxk_eswitch.h |  3 ++-
 drivers/net/cnxk/cnxk_rep_msg.c | 26 ++++++++++++--------------
 3 files changed, 39 insertions(+), 19 deletions(-)

diff --git a/drivers/net/cnxk/cnxk_eswitch.c b/drivers/net/cnxk/cnxk_eswitch.c
index 7e717a2fbf..0a6482462c 100644
--- a/drivers/net/cnxk/cnxk_eswitch.c
+++ b/drivers/net/cnxk/cnxk_eswitch.c
@@ -9,6 +9,24 @@
 
 #define CNXK_NIX_DEF_SQ_COUNT 512
 
+/* Build the control message unix socket address under the EAL runtime
+ * directory (e.g. /run/dpdk/<prefix>) rather than a fixed path in /tmp.
+ */
+int
+cnxk_eswitch_ctrl_msg_sock_addr(struct sockaddr_un *un)
+{
+       int ret;
+
+       memset(un, 0, sizeof(*un));
+       un->sun_family = AF_UNIX;
+       ret = snprintf(un->sun_path, sizeof(un->sun_path), "%s/%s",
+                      rte_eal_get_runtime_dir(), 
CNXK_ESWITCH_CTRL_MSG_SOCK_NAME);
+       if (ret < 0 || (size_t)ret >= sizeof(un->sun_path))
+               return -ENAMETOOLONG;
+
+       return 0;
+}
+
 int
 cnxk_eswitch_representor_id(struct cnxk_eswitch_dev *eswitch_dev, uint16_t 
hw_func,
                            uint16_t *rep_id)
@@ -122,14 +140,17 @@ cnxk_eswitch_dev_remove(struct rte_pci_device *pci_dev)
                                        plt_err("Failed to open socket. err 
%d", -errno);
                                        return -errno;
                                }
-                               sun.sun_family = AF_UNIX;
+                               rc = cnxk_eswitch_ctrl_msg_sock_addr(&sun);
+                               if (rc) {
+                                       plt_err("Control message socket path 
too long");
+                                       close(sock_fd);
+                                       return rc;
+                               }
                                sunlen = sizeof(struct sockaddr_un);
-                               strncpy(sun.sun_path, 
CNXK_ESWITCH_CTRL_MSG_SOCK_PATH,
-                                       sizeof(sun.sun_path) - 1);
 
                                if (connect(sock_fd, (struct sockaddr *)&sun, 
sunlen) < 0) {
                                        plt_err("Failed to connect socket: %s, 
err %d",
-                                               
CNXK_ESWITCH_CTRL_MSG_SOCK_PATH, errno);
+                                               sun.sun_path, errno);
                                        close(sock_fd);
                                        return -errno;
                                }
diff --git a/drivers/net/cnxk/cnxk_eswitch.h b/drivers/net/cnxk/cnxk_eswitch.h
index 0275e760fb..b35a615edd 100644
--- a/drivers/net/cnxk/cnxk_eswitch.h
+++ b/drivers/net/cnxk/cnxk_eswitch.h
@@ -12,7 +12,7 @@
 
 #include "cn10k_tx.h"
 
-#define CNXK_ESWITCH_CTRL_MSG_SOCK_PATH "/tmp/cxk_rep_ctrl_msg_sock"
+#define CNXK_ESWITCH_CTRL_MSG_SOCK_NAME "cnxk_ctrl_msg_sock"
 #define CNXK_ESWITCH_VLAN_TPID         ROC_ESWITCH_VLAN_TPID
 #define CNXK_REP_ESWITCH_DEV_MZ                "cnxk_eswitch_dev"
 #define CNXK_ESWITCH_MAX_TXQ           256
@@ -179,6 +179,7 @@ cnxk_eswitch_pmd_priv(void)
 }
 
 /* HW Resources */
+int cnxk_eswitch_ctrl_msg_sock_addr(struct sockaddr_un *un);
 int cnxk_eswitch_nix_rsrc_start(struct cnxk_eswitch_dev *eswitch_dev);
 int cnxk_eswitch_representor_id(struct cnxk_eswitch_dev *eswitch_dev, uint16_t 
hw_func,
                                uint16_t *rep_id);
diff --git a/drivers/net/cnxk/cnxk_rep_msg.c b/drivers/net/cnxk/cnxk_rep_msg.c
index a222e2b5cd..9662213139 100644
--- a/drivers/net/cnxk/cnxk_rep_msg.c
+++ b/drivers/net/cnxk/cnxk_rep_msg.c
@@ -14,8 +14,11 @@
 static void
 close_socket(int fd)
 {
+       struct sockaddr_un un;
+
        close(fd);
-       unlink(CNXK_ESWITCH_CTRL_MSG_SOCK_PATH);
+       if (cnxk_eswitch_ctrl_msg_sock_addr(&un) == 0)
+               unlink(un.sun_path);
 }
 
 static int
@@ -113,6 +116,7 @@ open_socket_ctrl_channel(void)
 {
        struct sockaddr_un un;
        int sock_fd;
+       int ret;
 
        sock_fd = socket(AF_UNIX, SOCK_STREAM, 0);
        if (sock_fd < 0) {
@@ -120,26 +124,20 @@ open_socket_ctrl_channel(void)
                return -1;
        }
 
-       /* Set unix socket path and bind */
-       memset(&un, 0, sizeof(un));
-       un.sun_family = AF_UNIX;
-
-       if (strlen(CNXK_ESWITCH_CTRL_MSG_SOCK_PATH) > sizeof(un.sun_path) - 1) {
-               plt_err("Server socket path too long: %s", 
CNXK_ESWITCH_CTRL_MSG_SOCK_PATH);
+       /* Set unix socket path under the runtime dir and bind */
+       ret = cnxk_eswitch_ctrl_msg_sock_addr(&un);
+       if (ret) {
+               plt_err("Server socket path too long");
                close(sock_fd);
-               return -E2BIG;
+               return ret;
        }
 
-       if (remove(CNXK_ESWITCH_CTRL_MSG_SOCK_PATH) == -1 && errno != ENOENT) {
-               plt_err("remove-%s", CNXK_ESWITCH_CTRL_MSG_SOCK_PATH);
+       if (remove(un.sun_path) == -1 && errno != ENOENT) {
+               plt_err("remove-%s", un.sun_path);
                close(sock_fd);
                return -errno;
        }
 
-       memset(&un, 0, sizeof(struct sockaddr_un));
-       un.sun_family = AF_UNIX;
-       strncpy(un.sun_path, CNXK_ESWITCH_CTRL_MSG_SOCK_PATH, 
sizeof(un.sun_path) - 1);
-
        if (bind(sock_fd, (struct sockaddr *)&un, sizeof(un)) < 0) {
                plt_err("Failed to bind %s: %s", un.sun_path, strerror(errno));
                close(sock_fd);
-- 
2.53.0

Reply via email to