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