]> git.proxmox.com Git - mirror_ovs.git/blob - lib/dpdk.c
dpif-netdev: Reorder elements in dp_netdev_port structure.
[mirror_ovs.git] / lib / dpdk.c
1 /*
2 * Copyright (c) 2014, 2015, 2016 Nicira, Inc.
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at:
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17 #include <config.h>
18 #include "dpdk.h"
19
20 #include <sys/types.h>
21 #include <sys/stat.h>
22 #include <getopt.h>
23
24 #include <rte_memzone.h>
25 #ifdef DPDK_PDUMP
26 #include <rte_mempool.h>
27 #include <rte_pdump.h>
28 #endif
29
30 #include "dirs.h"
31 #include "fatal-signal.h"
32 #include "netdev-dpdk.h"
33 #include "openvswitch/dynamic-string.h"
34 #include "openvswitch/vlog.h"
35 #include "smap.h"
36
37 VLOG_DEFINE_THIS_MODULE(dpdk);
38
39 static char *vhost_sock_dir = NULL; /* Location of vhost-user sockets */
40
41 static int
42 process_vhost_flags(char *flag, char *default_val, int size,
43 const struct smap *ovs_other_config,
44 char **new_val)
45 {
46 const char *val;
47 int changed = 0;
48
49 val = smap_get(ovs_other_config, flag);
50
51 /* Process the vhost-sock-dir flag if it is provided, otherwise resort to
52 * default value.
53 */
54 if (val && (strlen(val) <= size)) {
55 changed = 1;
56 *new_val = xstrdup(val);
57 VLOG_INFO("User-provided %s in use: %s", flag, *new_val);
58 } else {
59 VLOG_INFO("No %s provided - defaulting to %s", flag, default_val);
60 *new_val = default_val;
61 }
62
63 return changed;
64 }
65
66 static char **
67 grow_argv(char ***argv, size_t cur_siz, size_t grow_by)
68 {
69 return xrealloc(*argv, sizeof(char *) * (cur_siz + grow_by));
70 }
71
72 static void
73 dpdk_option_extend(char ***argv, int argc, const char *option,
74 const char *value)
75 {
76 char **newargv = grow_argv(argv, argc, 2);
77 *argv = newargv;
78 newargv[argc] = xstrdup(option);
79 newargv[argc+1] = xstrdup(value);
80 }
81
82 static char **
83 move_argv(char ***argv, size_t cur_size, char **src_argv, size_t src_argc)
84 {
85 char **newargv = grow_argv(argv, cur_size, src_argc);
86 while (src_argc--) {
87 newargv[cur_size+src_argc] = src_argv[src_argc];
88 src_argv[src_argc] = NULL;
89 }
90 return newargv;
91 }
92
93 static int
94 extra_dpdk_args(const char *ovs_extra_config, char ***argv, int argc)
95 {
96 int ret = argc;
97 char *release_tok = xstrdup(ovs_extra_config);
98 char *tok, *endptr = NULL;
99
100 for (tok = strtok_r(release_tok, " ", &endptr); tok != NULL;
101 tok = strtok_r(NULL, " ", &endptr)) {
102 char **newarg = grow_argv(argv, ret, 1);
103 *argv = newarg;
104 newarg[ret++] = xstrdup(tok);
105 }
106 free(release_tok);
107 return ret;
108 }
109
110 static bool
111 argv_contains(char **argv_haystack, const size_t argc_haystack,
112 const char *needle)
113 {
114 for (size_t i = 0; i < argc_haystack; ++i) {
115 if (!strcmp(argv_haystack[i], needle))
116 return true;
117 }
118 return false;
119 }
120
121 static int
122 construct_dpdk_options(const struct smap *ovs_other_config,
123 char ***argv, const int initial_size,
124 char **extra_args, const size_t extra_argc)
125 {
126 struct dpdk_options_map {
127 const char *ovs_configuration;
128 const char *dpdk_option;
129 bool default_enabled;
130 const char *default_value;
131 } opts[] = {
132 {"dpdk-lcore-mask", "-c", false, NULL},
133 {"dpdk-hugepage-dir", "--huge-dir", false, NULL},
134 };
135
136 int i, ret = initial_size;
137
138 /*First, construct from the flat-options (non-mutex)*/
139 for (i = 0; i < ARRAY_SIZE(opts); ++i) {
140 const char *lookup = smap_get(ovs_other_config,
141 opts[i].ovs_configuration);
142 if (!lookup && opts[i].default_enabled) {
143 lookup = opts[i].default_value;
144 }
145
146 if (lookup) {
147 if (!argv_contains(extra_args, extra_argc, opts[i].dpdk_option)) {
148 dpdk_option_extend(argv, ret, opts[i].dpdk_option, lookup);
149 ret += 2;
150 } else {
151 VLOG_WARN("Ignoring database defined option '%s' due to "
152 "dpdk_extras config", opts[i].dpdk_option);
153 }
154 }
155 }
156
157 return ret;
158 }
159
160 #define MAX_DPDK_EXCL_OPTS 10
161
162 static int
163 construct_dpdk_mutex_options(const struct smap *ovs_other_config,
164 char ***argv, const int initial_size,
165 char **extra_args, const size_t extra_argc)
166 {
167 struct dpdk_exclusive_options_map {
168 const char *category;
169 const char *ovs_dpdk_options[MAX_DPDK_EXCL_OPTS];
170 const char *eal_dpdk_options[MAX_DPDK_EXCL_OPTS];
171 const char *default_value;
172 int default_option;
173 } excl_opts[] = {
174 {"memory type",
175 {"dpdk-alloc-mem", "dpdk-socket-mem", NULL,},
176 {"-m", "--socket-mem", NULL,},
177 "1024,0", 1
178 },
179 };
180
181 int i, ret = initial_size;
182 for (i = 0; i < ARRAY_SIZE(excl_opts); ++i) {
183 int found_opts = 0, scan, found_pos = -1;
184 const char *found_value;
185 struct dpdk_exclusive_options_map *popt = &excl_opts[i];
186
187 for (scan = 0; scan < MAX_DPDK_EXCL_OPTS
188 && popt->ovs_dpdk_options[scan]; ++scan) {
189 const char *lookup = smap_get(ovs_other_config,
190 popt->ovs_dpdk_options[scan]);
191 if (lookup && strlen(lookup)) {
192 found_opts++;
193 found_pos = scan;
194 found_value = lookup;
195 }
196 }
197
198 if (!found_opts) {
199 if (popt->default_option) {
200 found_pos = popt->default_option;
201 found_value = popt->default_value;
202 } else {
203 continue;
204 }
205 }
206
207 if (found_opts > 1) {
208 VLOG_ERR("Multiple defined options for %s. Please check your"
209 " database settings and reconfigure if necessary.",
210 popt->category);
211 }
212
213 if (!argv_contains(extra_args, extra_argc,
214 popt->eal_dpdk_options[found_pos])) {
215 dpdk_option_extend(argv, ret, popt->eal_dpdk_options[found_pos],
216 found_value);
217 ret += 2;
218 } else {
219 VLOG_WARN("Ignoring database defined option '%s' due to "
220 "dpdk_extras config", popt->eal_dpdk_options[found_pos]);
221 }
222 }
223
224 return ret;
225 }
226
227 static int
228 get_dpdk_args(const struct smap *ovs_other_config, char ***argv,
229 int argc)
230 {
231 const char *extra_configuration;
232 char **extra_args = NULL;
233 int i;
234 size_t extra_argc = 0;
235
236 extra_configuration = smap_get(ovs_other_config, "dpdk-extra");
237 if (extra_configuration) {
238 extra_argc = extra_dpdk_args(extra_configuration, &extra_args, 0);
239 }
240
241 i = construct_dpdk_options(ovs_other_config, argv, argc, extra_args,
242 extra_argc);
243 i = construct_dpdk_mutex_options(ovs_other_config, argv, i, extra_args,
244 extra_argc);
245
246 if (extra_configuration) {
247 *argv = move_argv(argv, i, extra_args, extra_argc);
248 }
249
250 return i + extra_argc;
251 }
252
253 static char **dpdk_argv;
254 static int dpdk_argc;
255
256 static void
257 deferred_argv_release(void)
258 {
259 int result;
260 for (result = 0; result < dpdk_argc; ++result) {
261 free(dpdk_argv[result]);
262 }
263
264 free(dpdk_argv);
265 }
266
267 static void
268 dpdk_init__(const struct smap *ovs_other_config)
269 {
270 char **argv = NULL;
271 int result;
272 int argc, argc_tmp;
273 bool auto_determine = true;
274 int err = 0;
275 cpu_set_t cpuset;
276 char *sock_dir_subcomponent;
277
278 if (!smap_get_bool(ovs_other_config, "dpdk-init", false)) {
279 VLOG_INFO("DPDK Disabled - to change this requires a restart.\n");
280 return;
281 }
282
283 VLOG_INFO("DPDK Enabled, initializing");
284 if (process_vhost_flags("vhost-sock-dir", xstrdup(ovs_rundir()),
285 NAME_MAX, ovs_other_config,
286 &sock_dir_subcomponent)) {
287 struct stat s;
288 if (!strstr(sock_dir_subcomponent, "..")) {
289 vhost_sock_dir = xasprintf("%s/%s", ovs_rundir(),
290 sock_dir_subcomponent);
291
292 err = stat(vhost_sock_dir, &s);
293 if (err) {
294 VLOG_ERR("vhost-user sock directory '%s' does not exist.",
295 vhost_sock_dir);
296 }
297 } else {
298 vhost_sock_dir = xstrdup(ovs_rundir());
299 VLOG_ERR("vhost-user sock directory request '%s/%s' has invalid"
300 "characters '..' - using %s instead.",
301 ovs_rundir(), sock_dir_subcomponent, ovs_rundir());
302 }
303 free(sock_dir_subcomponent);
304 } else {
305 vhost_sock_dir = sock_dir_subcomponent;
306 }
307
308 argv = grow_argv(&argv, 0, 1);
309 argc = 1;
310 argv[0] = xstrdup(ovs_get_program_name());
311 argc_tmp = get_dpdk_args(ovs_other_config, &argv, argc);
312
313 while (argc_tmp != argc) {
314 if (!strcmp("-c", argv[argc]) || !strcmp("-l", argv[argc])) {
315 auto_determine = false;
316 break;
317 }
318 argc++;
319 }
320 argc = argc_tmp;
321
322 /**
323 * NOTE: This is an unsophisticated mechanism for determining the DPDK
324 * lcore for the DPDK Master.
325 */
326 if (auto_determine) {
327 int i;
328 /* Get the main thread affinity */
329 CPU_ZERO(&cpuset);
330 err = pthread_getaffinity_np(pthread_self(), sizeof(cpu_set_t),
331 &cpuset);
332 if (!err) {
333 for (i = 0; i < CPU_SETSIZE; i++) {
334 if (CPU_ISSET(i, &cpuset)) {
335 argv = grow_argv(&argv, argc, 2);
336 argv[argc++] = xstrdup("-c");
337 argv[argc++] = xasprintf("0x%08llX", (1ULL<<i));
338 i = CPU_SETSIZE;
339 }
340 }
341 } else {
342 VLOG_ERR("Thread getaffinity error %d. Using core 0x1", err);
343 /* User did not set dpdk-lcore-mask and unable to get current
344 * thread affintity - default to core 0x1 */
345 argv = grow_argv(&argv, argc, 2);
346 argv[argc++] = xstrdup("-c");
347 argv[argc++] = xasprintf("0x%X", 1);
348 }
349 }
350
351 argv = grow_argv(&argv, argc, 1);
352 argv[argc] = NULL;
353
354 optind = 1;
355
356 if (VLOG_IS_INFO_ENABLED()) {
357 struct ds eal_args;
358 int opt;
359 ds_init(&eal_args);
360 ds_put_cstr(&eal_args, "EAL ARGS:");
361 for (opt = 0; opt < argc; ++opt) {
362 ds_put_cstr(&eal_args, " ");
363 ds_put_cstr(&eal_args, argv[opt]);
364 }
365 VLOG_INFO("%s", ds_cstr_ro(&eal_args));
366 ds_destroy(&eal_args);
367 }
368
369 /* Make sure things are initialized ... */
370 result = rte_eal_init(argc, argv);
371 if (result < 0) {
372 ovs_abort(result, "Cannot init EAL");
373 }
374
375 /* Set the main thread affinity back to pre rte_eal_init() value */
376 if (auto_determine && !err) {
377 err = pthread_setaffinity_np(pthread_self(), sizeof(cpu_set_t),
378 &cpuset);
379 if (err) {
380 VLOG_ERR("Thread setaffinity error %d", err);
381 }
382 }
383
384 dpdk_argv = argv;
385 dpdk_argc = argc;
386
387 atexit(deferred_argv_release);
388
389 rte_memzone_dump(stdout);
390
391 /* We are called from the main thread here */
392 RTE_PER_LCORE(_lcore_id) = NON_PMD_CORE_ID;
393
394 #ifdef DPDK_PDUMP
395 VLOG_INFO("DPDK pdump packet capture enabled");
396 err = rte_pdump_init(ovs_rundir());
397 if (err) {
398 VLOG_INFO("Error initialising DPDK pdump");
399 rte_pdump_uninit();
400 } else {
401 char *server_socket_path;
402
403 server_socket_path = xasprintf("%s/%s", ovs_rundir(),
404 "pdump_server_socket");
405 fatal_signal_add_file_to_unlink(server_socket_path);
406 free(server_socket_path);
407 }
408 #endif
409
410 /* Finally, register the dpdk classes */
411 netdev_dpdk_register();
412 }
413
414 void
415 dpdk_init(const struct smap *ovs_other_config)
416 {
417 static struct ovsthread_once once = OVSTHREAD_ONCE_INITIALIZER;
418
419 if (ovs_other_config && ovsthread_once_start(&once)) {
420 dpdk_init__(ovs_other_config);
421 ovsthread_once_done(&once);
422 }
423 }
424
425 const char *
426 dpdk_get_vhost_sock_dir(void)
427 {
428 return vhost_sock_dir;
429 }
430
431 void
432 dpdk_set_lcore_id(unsigned cpu)
433 {
434 /* NON_PMD_CORE_ID is reserved for use by non pmd threads. */
435 ovs_assert(cpu != NON_PMD_CORE_ID);
436 RTE_PER_LCORE(_lcore_id) = cpu;
437 }