2cf7a04eff2ea8427f3e37f03f69aaaccdc54a0a
[dpdk.git] / lib / librte_eal / windows / eal.c
1 /* SPDX-License-Identifier: BSD-3-Clause
2  * Copyright(c) 2019 Intel Corporation
3  */
4
5 #include <fcntl.h>
6 #include <io.h>
7 #include <share.h>
8 #include <sys/stat.h>
9
10 #include <rte_debug.h>
11 #include <rte_eal.h>
12 #include <eal_memcfg.h>
13 #include <rte_errno.h>
14 #include <rte_lcore.h>
15 #include <eal_thread.h>
16 #include <eal_internal_cfg.h>
17 #include <eal_filesystem.h>
18 #include <eal_options.h>
19 #include <eal_private.h>
20
21 #include "eal_windows.h"
22
23  /* Allow the application to print its usage message too if set */
24 static rte_usage_hook_t rte_application_usage_hook;
25
26 /* define fd variable here, because file needs to be kept open for the
27  * duration of the program, as we hold a write lock on it in the primary proc
28  */
29 static int mem_cfg_fd = -1;
30
31 /* early configuration structure, when memory config is not mmapped */
32 static struct rte_mem_config early_mem_config;
33
34 /* Address of global and public configuration */
35 static struct rte_config rte_config = {
36                 .mem_config = &early_mem_config,
37 };
38
39 /* internal configuration (per-core) */
40 struct lcore_config lcore_config[RTE_MAX_LCORE];
41
42 /* internal configuration */
43 struct internal_config internal_config;
44
45 /* platform-specific runtime dir */
46 static char runtime_dir[PATH_MAX];
47
48 const char *
49 rte_eal_get_runtime_dir(void)
50 {
51         return runtime_dir;
52 }
53
54 /* Return a pointer to the configuration structure */
55 struct rte_config *
56 rte_eal_get_configuration(void)
57 {
58         return &rte_config;
59 }
60
61 /* Detect if we are a primary or a secondary process */
62 enum rte_proc_type_t
63 eal_proc_type_detect(void)
64 {
65         enum rte_proc_type_t ptype = RTE_PROC_PRIMARY;
66         const char *pathname = eal_runtime_config_path();
67
68         /* if we can open the file but not get a write-lock we are a secondary
69          * process. NOTE: if we get a file handle back, we keep that open
70          * and don't close it to prevent a race condition between multiple opens
71          */
72         errno_t err = _sopen_s(&mem_cfg_fd, pathname,
73                 _O_RDWR, _SH_DENYNO, _S_IREAD | _S_IWRITE);
74         if (err == 0) {
75                 OVERLAPPED soverlapped = { 0 };
76                 soverlapped.Offset = sizeof(*rte_config.mem_config);
77                 soverlapped.OffsetHigh = 0;
78
79                 HANDLE hwinfilehandle = (HANDLE)_get_osfhandle(mem_cfg_fd);
80
81                 if (!LockFileEx(hwinfilehandle,
82                         LOCKFILE_EXCLUSIVE_LOCK | LOCKFILE_FAIL_IMMEDIATELY, 0,
83                         sizeof(*rte_config.mem_config), 0, &soverlapped))
84                         ptype = RTE_PROC_SECONDARY;
85         }
86
87         RTE_LOG(INFO, EAL, "Auto-detected process type: %s\n",
88                 ptype == RTE_PROC_PRIMARY ? "PRIMARY" : "SECONDARY");
89
90         return ptype;
91 }
92
93 /* display usage */
94 static void
95 eal_usage(const char *prgname)
96 {
97         printf("\nUsage: %s ", prgname);
98         eal_common_usage();
99         /* Allow the application to print its usage message too
100          * if hook is set
101          */
102         if (rte_application_usage_hook) {
103                 printf("===== Application Usage =====\n\n");
104                 rte_application_usage_hook(prgname);
105         }
106 }
107
108 /* Parse the arguments for --log-level only */
109 static void
110 eal_log_level_parse(int argc, char **argv)
111 {
112         int opt;
113         char **argvopt;
114         int option_index;
115
116         argvopt = argv;
117
118         eal_reset_internal_config(&internal_config);
119
120         while ((opt = getopt_long(argc, argvopt, eal_short_options,
121                 eal_long_options, &option_index)) != EOF) {
122
123                 int ret;
124
125                 /* getopt is not happy, stop right now */
126                 if (opt == '?')
127                         break;
128
129                 ret = (opt == OPT_LOG_LEVEL_NUM) ?
130                         eal_parse_common_option(opt, optarg,
131                                 &internal_config) : 0;
132
133                 /* common parser is not happy */
134                 if (ret < 0)
135                         break;
136         }
137
138         optind = 0; /* reset getopt lib */
139 }
140
141 /* Parse the argument given in the command line of the application */
142 __attribute__((optnone)) static int
143 eal_parse_args(int argc, char **argv)
144 {
145         int opt, ret;
146         char **argvopt;
147         int option_index;
148         char *prgname = argv[0];
149
150         argvopt = argv;
151
152         while ((opt = getopt_long(argc, argvopt, eal_short_options,
153                 eal_long_options, &option_index)) != EOF) {
154
155                 int ret;
156
157                 /* getopt is not happy, stop right now */
158                 if (opt == '?') {
159                         eal_usage(prgname);
160                         return -1;
161                 }
162
163                 ret = eal_parse_common_option(opt, optarg, &internal_config);
164                 /* common parser is not happy */
165                 if (ret < 0) {
166                         eal_usage(prgname);
167                         return -1;
168                 }
169                 /* common parser handled this option */
170                 if (ret == 0)
171                         continue;
172
173                 switch (opt) {
174                 case 'h':
175                         eal_usage(prgname);
176                         exit(EXIT_SUCCESS);
177                 default:
178                         if (opt < OPT_LONG_MIN_NUM && isprint(opt)) {
179                                 RTE_LOG(ERR, EAL, "Option %c is not supported "
180                                         "on Windows\n", opt);
181                         } else if (opt >= OPT_LONG_MIN_NUM &&
182                                 opt < OPT_LONG_MAX_NUM) {
183                                 RTE_LOG(ERR, EAL, "Option %s is not supported "
184                                         "on Windows\n",
185                                         eal_long_options[option_index].name);
186                         } else {
187                                 RTE_LOG(ERR, EAL, "Option %d is not supported "
188                                         "on Windows\n", opt);
189                         }
190                         eal_usage(prgname);
191                         return -1;
192                 }
193         }
194
195         if (eal_adjust_config(&internal_config) != 0)
196                 return -1;
197
198         /* sanity checks */
199         if (eal_check_common_options(&internal_config) != 0) {
200                 eal_usage(prgname);
201                 return -1;
202         }
203
204         if (optind >= 0)
205                 argv[optind - 1] = prgname;
206         ret = optind - 1;
207         optind = 0; /* reset getopt lib */
208         return ret;
209 }
210
211 static int
212 sync_func(void *arg __rte_unused)
213 {
214         return 0;
215 }
216
217 static void
218 rte_eal_init_alert(const char *msg)
219 {
220         fprintf(stderr, "EAL: FATAL: %s\n", msg);
221         RTE_LOG(ERR, EAL, "%s\n", msg);
222 }
223
224  /* Launch threads, called at application init(). */
225 int
226 rte_eal_init(int argc, char **argv)
227 {
228         int i, fctret;
229
230         eal_log_level_parse(argc, argv);
231
232         /* create a map of all processors in the system */
233         eal_create_cpu_map();
234
235         if (rte_eal_cpu_init() < 0) {
236                 rte_eal_init_alert("Cannot detect lcores.");
237                 rte_errno = ENOTSUP;
238                 return -1;
239         }
240
241         fctret = eal_parse_args(argc, argv);
242         if (fctret < 0)
243                 exit(1);
244
245         eal_thread_init_master(rte_config.master_lcore);
246
247         RTE_LCORE_FOREACH_SLAVE(i) {
248
249                 /*
250                  * create communication pipes between master thread
251                  * and children
252                  */
253                 if (_pipe(lcore_config[i].pipe_master2slave,
254                         sizeof(char), _O_BINARY) < 0)
255                         rte_panic("Cannot create pipe\n");
256                 if (_pipe(lcore_config[i].pipe_slave2master,
257                         sizeof(char), _O_BINARY) < 0)
258                         rte_panic("Cannot create pipe\n");
259
260                 lcore_config[i].state = WAIT;
261
262                 /* create a thread for each lcore */
263                 if (eal_thread_create(&lcore_config[i].thread_id) != 0)
264                         rte_panic("Cannot create thread\n");
265         }
266
267         /*
268          * Launch a dummy function on all slave lcores, so that master lcore
269          * knows they are all ready when this function returns.
270          */
271         rte_eal_mp_remote_launch(sync_func, NULL, SKIP_MASTER);
272         rte_eal_mp_wait_lcore();
273         return fctret;
274 }