eal/windows: detect process type
[dpdk.git] / lib / librte_eal / windows / eal / eal.c
1 /* SPDX-License-Identifier: BSD-3-Clause
2  * Copyright(c) 2019 Intel Corporation
3  */
4
5 #include <sys/stat.h>
6 #include <io.h>
7 #include <fcntl.h>
8 #include <rte_debug.h>
9 #include <rte_eal.h>
10 #include <eal_memcfg.h>
11 #include <rte_errno.h>
12 #include <rte_lcore.h>
13 #include <eal_thread.h>
14 #include <eal_internal_cfg.h>
15 #include <eal_filesystem.h>
16 #include <eal_options.h>
17 #include <eal_private.h>
18
19 /* define fd variable here, because file needs to be kept open for the
20  * duration of the program, as we hold a write lock on it in the primary proc
21  */
22 static int mem_cfg_fd = -1;
23
24 /* early configuration structure, when memory config is not mmapped */
25 static struct rte_mem_config early_mem_config;
26
27 /* Address of global and public configuration */
28 static struct rte_config rte_config = {
29                 .mem_config = &early_mem_config,
30 };
31
32 /* internal configuration (per-core) */
33 struct lcore_config lcore_config[RTE_MAX_LCORE];
34
35 /* internal configuration */
36 struct internal_config internal_config;
37
38 /* platform-specific runtime dir */
39 static char runtime_dir[PATH_MAX];
40
41 const char *
42 rte_eal_get_runtime_dir(void)
43 {
44         return runtime_dir;
45 }
46
47 /* Return a pointer to the configuration structure */
48 struct rte_config *
49 rte_eal_get_configuration(void)
50 {
51         return &rte_config;
52 }
53
54 /* Detect if we are a primary or a secondary process */
55 enum rte_proc_type_t
56 eal_proc_type_detect(void)
57 {
58         enum rte_proc_type_t ptype = RTE_PROC_PRIMARY;
59         const char *pathname = eal_runtime_config_path();
60
61         /* if we can open the file but not get a write-lock we are a secondary
62          * process. NOTE: if we get a file handle back, we keep that open
63          * and don't close it to prevent a race condition between multiple opens
64          */
65         errno_t err = _sopen_s(&mem_cfg_fd, pathname,
66                 _O_RDWR, _SH_DENYNO, _S_IREAD | _S_IWRITE);
67         if (err == 0) {
68                 OVERLAPPED soverlapped = { 0 };
69                 soverlapped.Offset = sizeof(*rte_config.mem_config);
70                 soverlapped.OffsetHigh = 0;
71
72                 HANDLE hwinfilehandle = (HANDLE)_get_osfhandle(mem_cfg_fd);
73
74                 if (!LockFileEx(hwinfilehandle,
75                         LOCKFILE_EXCLUSIVE_LOCK | LOCKFILE_FAIL_IMMEDIATELY, 0,
76                         sizeof(*rte_config.mem_config), 0, &soverlapped))
77                         ptype = RTE_PROC_SECONDARY;
78         }
79
80         RTE_LOG(INFO, EAL, "Auto-detected process type: %s\n",
81                 ptype == RTE_PROC_PRIMARY ? "PRIMARY" : "SECONDARY");
82
83         return ptype;
84 }
85
86 static int
87 sync_func(void *arg __rte_unused)
88 {
89         return 0;
90 }
91
92 static void
93 rte_eal_init_alert(const char *msg)
94 {
95         fprintf(stderr, "EAL: FATAL: %s\n", msg);
96         RTE_LOG(ERR, EAL, "%s\n", msg);
97 }
98
99  /* Launch threads, called at application init(). */
100 int
101 rte_eal_init(int argc __rte_unused, char **argv __rte_unused)
102 {
103         int i;
104
105         /* create a map of all processors in the system */
106         eal_create_cpu_map();
107
108         if (rte_eal_cpu_init() < 0) {
109                 rte_eal_init_alert("Cannot detect lcores.");
110                 rte_errno = ENOTSUP;
111                 return -1;
112         }
113
114         eal_thread_init_master(rte_config.master_lcore);
115
116         RTE_LCORE_FOREACH_SLAVE(i) {
117
118                 /*
119                  * create communication pipes between master thread
120                  * and children
121                  */
122                 if (_pipe(lcore_config[i].pipe_master2slave,
123                         sizeof(char), _O_BINARY) < 0)
124                         rte_panic("Cannot create pipe\n");
125                 if (_pipe(lcore_config[i].pipe_slave2master,
126                         sizeof(char), _O_BINARY) < 0)
127                         rte_panic("Cannot create pipe\n");
128
129                 lcore_config[i].state = WAIT;
130
131                 /* create a thread for each lcore */
132                 if (eal_thread_create(&lcore_config[i].thread_id) != 0)
133                         rte_panic("Cannot create thread\n");
134         }
135
136         /*
137          * Launch a dummy function on all slave lcores, so that master lcore
138          * knows they are all ready when this function returns.
139          */
140         rte_eal_mp_remote_launch(sync_func, NULL, SKIP_MASTER);
141         rte_eal_mp_wait_lcore();
142         return 0;
143 }