From: Yasufumi Ogawa <ogawa.yasuf...@lab.ntt.co.jp>

In secondary_msl_create_walk(), it creates a file for fbarrays with its
PID for reserving unique name among secondary processes. However, it
does not work if secondary is run as app container because each of
containerized secondary has PID 1. To reserve unique name, use hostname
instead of PID because hostname is assigned as a short form of 64
digits full container ID in docker.

Cc: sta...@dpdk.org

Signed-off-by: Yasufumi Ogawa <ogawa.yasuf...@lab.ntt.co.jp>
---
 lib/librte_eal/linux/eal/eal_memalloc.c | 28 +++++++++++++++++++++++--
 1 file changed, 26 insertions(+), 2 deletions(-)

diff --git a/lib/librte_eal/linux/eal/eal_memalloc.c 
b/lib/librte_eal/linux/eal/eal_memalloc.c
index 1f6a7c18f..356b304a8 100644
--- a/lib/librte_eal/linux/eal/eal_memalloc.c
+++ b/lib/librte_eal/linux/eal/eal_memalloc.c
@@ -1366,6 +1366,7 @@ secondary_msl_create_walk(const struct rte_memseg_list 
*msl,
        struct rte_memseg_list *primary_msl, *local_msl;
        char name[PATH_MAX];
        int msl_idx, ret;
+       char proc_id[33] = { 0 };  /* 32bytes is enough if using hostname */
 
        if (msl->external)
                return 0;
@@ -1375,8 +1376,31 @@ secondary_msl_create_walk(const struct rte_memseg_list 
*msl,
        local_msl = &local_memsegs[msl_idx];
 
        /* create distinct fbarrays for each secondary */
-       snprintf(name, RTE_FBARRAY_NAME_LEN, "%s_%i",
-               primary_msl->memseg_arr.name, getpid());
+       /* If run secondary in a container, the name of fbarray file should
+        * not be decided with pid because getpid() always returns 1.
+        * In docker, hostname is assigned as a short form of full container
+        * ID. So use hostname as unique ID among containers instead.
+        */
+       if (getpid() == 1) {
+               FILE *hn_fp;
+               hn_fp = fopen("/etc/hostname", "r");
+               if (hn_fp == NULL) {
+                       RTE_LOG(ERR, EAL,
+                               "Cannot open '/etc/hostname' for secondary\n");
+                       return -1;
+               }
+
+               /* with docker, /etc/hostname just has one entry of hostname */
+               if (fscanf(hn_fp, "%32s", proc_id) == EOF) {
+                       fclose(hn_fp);
+                       return -1;
+               }
+               fclose(hn_fp);
+       } else
+               sprintf(proc_id, "%d", (int)getpid());
+
+       snprintf(name, RTE_FBARRAY_NAME_LEN, "%s_%s",
+                       primary_msl->memseg_arr.name, proc_id);
 
        ret = rte_fbarray_init(&local_msl->memseg_arr, name,
                primary_msl->memseg_arr.len,
-- 
2.17.1

Reply via email to