]> git.proxmox.com Git - mirror_ovs.git/blame - lib/dpdk.c
dpdk: Limit DPDK memory usage.
[mirror_ovs.git] / lib / dpdk.c
CommitLineData
01961bbd 1/*
5575908b 2 * Copyright (c) 2014, 2015, 2016, 2017 Nicira, Inc.
01961bbd
DDP
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
736ca516 20#include <stdio.h>
01961bbd
DDP
21#include <sys/types.h>
22#include <sys/stat.h>
23#include <getopt.h>
24
d7e2509e 25#include <rte_errno.h>
736ca516 26#include <rte_log.h>
01961bbd 27#include <rte_memzone.h>
40c23a57 28#include <rte_version.h>
a0cbc627
CL
29#ifdef DPDK_PDUMP
30#include <rte_mempool.h>
31#include <rte_pdump.h>
32#endif
01961bbd
DDP
33
34#include "dirs.h"
a0cbc627 35#include "fatal-signal.h"
01961bbd
DDP
36#include "netdev-dpdk.h"
37#include "openvswitch/dynamic-string.h"
38#include "openvswitch/vlog.h"
7189d54c 39#include "ovs-numa.h"
01961bbd 40#include "smap.h"
68c00e3e 41#include "svec.h"
3e52fa56 42#include "vswitch-idl.h"
01961bbd
DDP
43
44VLOG_DEFINE_THIS_MODULE(dpdk);
45
736ca516
IM
46static FILE *log_stream = NULL; /* Stream for DPDK log redirection */
47
01961bbd 48static char *vhost_sock_dir = NULL; /* Location of vhost-user sockets */
a14d1cc8 49static bool vhost_iommu_enabled = false; /* Status of vHost IOMMU support */
3e52fa56
AC
50static bool dpdk_initialized = false; /* Indicates successful initialization
51 * of DPDK. */
43307ad0 52static bool per_port_memory = false; /* Status of per port memory support */
01961bbd
DDP
53
54static int
6c4f08e2 55process_vhost_flags(char *flag, const char *default_val, int size,
01961bbd
DDP
56 const struct smap *ovs_other_config,
57 char **new_val)
58{
59 const char *val;
60 int changed = 0;
61
62 val = smap_get(ovs_other_config, flag);
63
64 /* Process the vhost-sock-dir flag if it is provided, otherwise resort to
65 * default value.
66 */
67 if (val && (strlen(val) <= size)) {
68 changed = 1;
69 *new_val = xstrdup(val);
70 VLOG_INFO("User-provided %s in use: %s", flag, *new_val);
71 } else {
72 VLOG_INFO("No %s provided - defaulting to %s", flag, default_val);
6c4f08e2 73 *new_val = xstrdup(default_val);
01961bbd
DDP
74 }
75
76 return changed;
77}
78
01961bbd 79static bool
68c00e3e 80args_contains(const struct svec *args, const char *value)
01961bbd 81{
68c00e3e
IM
82 const char *arg;
83 size_t i;
84
85 /* We can't just use 'svec_contains' because args are not sorted. */
86 SVEC_FOR_EACH (i, arg, args) {
87 if (!strcmp(arg, value)) {
01961bbd 88 return true;
68c00e3e 89 }
01961bbd
DDP
90 }
91 return false;
92}
93
68c00e3e
IM
94static void
95construct_dpdk_options(const struct smap *ovs_other_config, struct svec *args)
01961bbd
DDP
96{
97 struct dpdk_options_map {
98 const char *ovs_configuration;
99 const char *dpdk_option;
100 bool default_enabled;
101 const char *default_value;
102 } opts[] = {
8411b6cc
IM
103 {"dpdk-lcore-mask", "-c", false, NULL},
104 {"dpdk-hugepage-dir", "--huge-dir", false, NULL},
105 {"dpdk-socket-limit", "--socket-limit", false, NULL},
01961bbd
DDP
106 };
107
68c00e3e 108 int i;
01961bbd
DDP
109
110 /*First, construct from the flat-options (non-mutex)*/
111 for (i = 0; i < ARRAY_SIZE(opts); ++i) {
68c00e3e
IM
112 const char *value = smap_get(ovs_other_config,
113 opts[i].ovs_configuration);
114 if (!value && opts[i].default_enabled) {
115 value = opts[i].default_value;
01961bbd
DDP
116 }
117
68c00e3e
IM
118 if (value) {
119 if (!args_contains(args, opts[i].dpdk_option)) {
120 svec_add(args, opts[i].dpdk_option);
121 svec_add(args, value);
01961bbd
DDP
122 } else {
123 VLOG_WARN("Ignoring database defined option '%s' due to "
68c00e3e 124 "dpdk-extra config", opts[i].dpdk_option);
01961bbd
DDP
125 }
126 }
127 }
01961bbd
DDP
128}
129
7189d54c
MR
130static char *
131construct_dpdk_socket_mem(void)
132{
7189d54c 133 const char *def_value = "1024";
9c68ca34
IM
134 int numa, numa_nodes = ovs_numa_get_n_numas();
135 struct ds dpdk_socket_mem = DS_EMPTY_INITIALIZER;
7189d54c
MR
136
137 if (numa_nodes == 0 || numa_nodes == OVS_NUMA_UNSPEC) {
138 numa_nodes = 1;
139 }
7189d54c 140
9c68ca34 141 ds_put_cstr(&dpdk_socket_mem, def_value);
7189d54c 142 for (numa = 1; numa < numa_nodes; ++numa) {
9c68ca34 143 ds_put_format(&dpdk_socket_mem, ",%s", def_value);
7189d54c
MR
144 }
145
9c68ca34 146 return ds_cstr(&dpdk_socket_mem);
7189d54c
MR
147}
148
01961bbd
DDP
149#define MAX_DPDK_EXCL_OPTS 10
150
68c00e3e 151static void
01961bbd 152construct_dpdk_mutex_options(const struct smap *ovs_other_config,
68c00e3e 153 struct svec *args)
01961bbd 154{
7189d54c 155 char *default_dpdk_socket_mem = construct_dpdk_socket_mem();
68c00e3e 156
01961bbd
DDP
157 struct dpdk_exclusive_options_map {
158 const char *category;
159 const char *ovs_dpdk_options[MAX_DPDK_EXCL_OPTS];
160 const char *eal_dpdk_options[MAX_DPDK_EXCL_OPTS];
161 const char *default_value;
162 int default_option;
163 } excl_opts[] = {
164 {"memory type",
165 {"dpdk-alloc-mem", "dpdk-socket-mem", NULL,},
166 {"-m", "--socket-mem", NULL,},
7189d54c 167 default_dpdk_socket_mem, 1
01961bbd
DDP
168 },
169 };
170
68c00e3e 171 int i;
01961bbd
DDP
172 for (i = 0; i < ARRAY_SIZE(excl_opts); ++i) {
173 int found_opts = 0, scan, found_pos = -1;
174 const char *found_value;
175 struct dpdk_exclusive_options_map *popt = &excl_opts[i];
176
177 for (scan = 0; scan < MAX_DPDK_EXCL_OPTS
178 && popt->ovs_dpdk_options[scan]; ++scan) {
68c00e3e
IM
179 const char *value = smap_get(ovs_other_config,
180 popt->ovs_dpdk_options[scan]);
181 if (value && strlen(value)) {
01961bbd
DDP
182 found_opts++;
183 found_pos = scan;
68c00e3e 184 found_value = value;
01961bbd
DDP
185 }
186 }
187
188 if (!found_opts) {
189 if (popt->default_option) {
190 found_pos = popt->default_option;
191 found_value = popt->default_value;
192 } else {
193 continue;
194 }
195 }
196
197 if (found_opts > 1) {
198 VLOG_ERR("Multiple defined options for %s. Please check your"
199 " database settings and reconfigure if necessary.",
200 popt->category);
201 }
202
68c00e3e
IM
203 if (!args_contains(args, popt->eal_dpdk_options[found_pos])) {
204 svec_add(args, popt->eal_dpdk_options[found_pos]);
205 svec_add(args, found_value);
01961bbd
DDP
206 } else {
207 VLOG_WARN("Ignoring database defined option '%s' due to "
68c00e3e 208 "dpdk-extra config", popt->eal_dpdk_options[found_pos]);
01961bbd
DDP
209 }
210 }
211
7189d54c 212 free(default_dpdk_socket_mem);
01961bbd
DDP
213}
214
68c00e3e
IM
215static void
216construct_dpdk_args(const struct smap *ovs_other_config, struct svec *args)
01961bbd 217{
68c00e3e 218 const char *extra_configuration = smap_get(ovs_other_config, "dpdk-extra");
01961bbd 219
01961bbd 220 if (extra_configuration) {
68c00e3e 221 svec_parse_words(args, extra_configuration);
01961bbd
DDP
222 }
223
68c00e3e
IM
224 construct_dpdk_options(ovs_other_config, args);
225 construct_dpdk_mutex_options(ovs_other_config, args);
01961bbd
DDP
226}
227
736ca516
IM
228static ssize_t
229dpdk_log_write(void *c OVS_UNUSED, const char *buf, size_t size)
230{
231 char *str = xmemdup0(buf, size);
9fd38f68
IM
232 static struct vlog_rate_limit rl = VLOG_RATE_LIMIT_INIT(600, 600);
233 static struct vlog_rate_limit dbg_rl = VLOG_RATE_LIMIT_INIT(600, 600);
736ca516
IM
234
235 switch (rte_log_cur_msg_loglevel()) {
236 case RTE_LOG_DEBUG:
9fd38f68 237 VLOG_DBG_RL(&dbg_rl, "%s", str);
736ca516
IM
238 break;
239 case RTE_LOG_INFO:
240 case RTE_LOG_NOTICE:
9fd38f68 241 VLOG_INFO_RL(&rl, "%s", str);
736ca516
IM
242 break;
243 case RTE_LOG_WARNING:
9fd38f68 244 VLOG_WARN_RL(&rl, "%s", str);
736ca516
IM
245 break;
246 case RTE_LOG_ERR:
9fd38f68 247 VLOG_ERR_RL(&rl, "%s", str);
736ca516
IM
248 break;
249 case RTE_LOG_CRIT:
250 case RTE_LOG_ALERT:
251 case RTE_LOG_EMERG:
252 VLOG_EMER("%s", str);
253 break;
254 default:
255 OVS_NOT_REACHED();
256 }
257
258 free(str);
259 return size;
260}
261
262static cookie_io_functions_t dpdk_log_func = {
263 .write = dpdk_log_write,
264};
265
d7e2509e 266static bool
01961bbd
DDP
267dpdk_init__(const struct smap *ovs_other_config)
268{
68c00e3e
IM
269 char *sock_dir_subcomponent;
270 char **argv = NULL;
01961bbd 271 int result;
01961bbd
DDP
272 bool auto_determine = true;
273 int err = 0;
274 cpu_set_t cpuset;
68c00e3e 275 struct svec args = SVEC_EMPTY_INITIALIZER;
01961bbd 276
736ca516
IM
277 log_stream = fopencookie(NULL, "w+", dpdk_log_func);
278 if (log_stream == NULL) {
279 VLOG_ERR("Can't redirect DPDK log: %s.", ovs_strerror(errno));
280 } else {
281 setbuf(log_stream, NULL);
282 rte_openlog_stream(log_stream);
283 }
284
6c4f08e2 285 if (process_vhost_flags("vhost-sock-dir", ovs_rundir(),
01961bbd
DDP
286 NAME_MAX, ovs_other_config,
287 &sock_dir_subcomponent)) {
288 struct stat s;
289 if (!strstr(sock_dir_subcomponent, "..")) {
290 vhost_sock_dir = xasprintf("%s/%s", ovs_rundir(),
291 sock_dir_subcomponent);
292
293 err = stat(vhost_sock_dir, &s);
294 if (err) {
295 VLOG_ERR("vhost-user sock directory '%s' does not exist.",
296 vhost_sock_dir);
297 }
298 } else {
299 vhost_sock_dir = xstrdup(ovs_rundir());
300 VLOG_ERR("vhost-user sock directory request '%s/%s' has invalid"
301 "characters '..' - using %s instead.",
302 ovs_rundir(), sock_dir_subcomponent, ovs_rundir());
303 }
304 free(sock_dir_subcomponent);
305 } else {
306 vhost_sock_dir = sock_dir_subcomponent;
307 }
308
a14d1cc8
MK
309 vhost_iommu_enabled = smap_get_bool(ovs_other_config,
310 "vhost-iommu-support", false);
311 VLOG_INFO("IOMMU support for vhost-user-client %s.",
312 vhost_iommu_enabled ? "enabled" : "disabled");
313
43307ad0
IS
314 per_port_memory = smap_get_bool(ovs_other_config,
315 "per-port-memory", false);
316 VLOG_INFO("Per port memory for DPDK devices %s.",
317 per_port_memory ? "enabled" : "disabled");
318
68c00e3e
IM
319 svec_add(&args, ovs_get_program_name());
320 construct_dpdk_args(ovs_other_config, &args);
01961bbd 321
8411b6cc
IM
322 if (!args_contains(&args, "--legacy-mem")
323 && !args_contains(&args, "--socket-limit")) {
324 const char *arg;
325 size_t i;
326
327 SVEC_FOR_EACH (i, arg, &args) {
328 if (!strcmp(arg, "--socket-mem")) {
329 break;
330 }
331 }
332 if (i < args.n - 1) {
333 svec_add(&args, "--socket-limit");
334 svec_add(&args, args.names[i + 1]);
335 }
336 }
337
68c00e3e
IM
338 if (args_contains(&args, "-c") || args_contains(&args, "-l")) {
339 auto_determine = false;
01961bbd 340 }
01961bbd
DDP
341
342 /**
343 * NOTE: This is an unsophisticated mechanism for determining the DPDK
344 * lcore for the DPDK Master.
345 */
346 if (auto_determine) {
68c00e3e
IM
347 int cpu = 0;
348
01961bbd
DDP
349 /* Get the main thread affinity */
350 CPU_ZERO(&cpuset);
351 err = pthread_getaffinity_np(pthread_self(), sizeof(cpu_set_t),
352 &cpuset);
353 if (!err) {
68c00e3e
IM
354 for (cpu = 0; cpu < CPU_SETSIZE; cpu++) {
355 if (CPU_ISSET(cpu, &cpuset)) {
356 break;
01961bbd
DDP
357 }
358 }
359 } else {
01961bbd 360 /* User did not set dpdk-lcore-mask and unable to get current
68c00e3e
IM
361 * thread affintity - default to core #0 */
362 VLOG_ERR("Thread getaffinity error %d. Using core #0", err);
01961bbd 363 }
68c00e3e
IM
364 svec_add(&args, "-l");
365 svec_add_nocopy(&args, xasprintf("%d", cpu));
01961bbd
DDP
366 }
367
68c00e3e 368 svec_terminate(&args);
01961bbd
DDP
369
370 optind = 1;
371
372 if (VLOG_IS_INFO_ENABLED()) {
68c00e3e
IM
373 struct ds eal_args = DS_EMPTY_INITIALIZER;
374 char *joined_args = svec_join(&args, " ", ".");
375
376 ds_put_format(&eal_args, "EAL ARGS: %s", joined_args);
01961bbd
DDP
377 VLOG_INFO("%s", ds_cstr_ro(&eal_args));
378 ds_destroy(&eal_args);
68c00e3e 379 free(joined_args);
01961bbd
DDP
380 }
381
68c00e3e
IM
382 /* Copy because 'rte_eal_init' will change the argv, i.e. it will remove
383 * some arguments from it. '+1' to copy the terminating NULL. */
384 argv = xmemdup(args.names, (args.n + 1) * sizeof args.names[0]);
fe11b9e0 385
01961bbd 386 /* Make sure things are initialized ... */
68c00e3e
IM
387 result = rte_eal_init(args.n, argv);
388
389 free(argv);
390 svec_destroy(&args);
01961bbd
DDP
391
392 /* Set the main thread affinity back to pre rte_eal_init() value */
393 if (auto_determine && !err) {
394 err = pthread_setaffinity_np(pthread_self(), sizeof(cpu_set_t),
395 &cpuset);
396 if (err) {
397 VLOG_ERR("Thread setaffinity error %d", err);
398 }
399 }
400
d7e2509e
AC
401 if (result < 0) {
402 VLOG_EMER("Unable to initialize DPDK: %s", ovs_strerror(rte_errno));
403 return false;
404 }
405
01961bbd
DDP
406 rte_memzone_dump(stdout);
407
408 /* We are called from the main thread here */
409 RTE_PER_LCORE(_lcore_id) = NON_PMD_CORE_ID;
410
411#ifdef DPDK_PDUMP
412 VLOG_INFO("DPDK pdump packet capture enabled");
413 err = rte_pdump_init(ovs_rundir());
414 if (err) {
415 VLOG_INFO("Error initialising DPDK pdump");
416 rte_pdump_uninit();
417 } else {
418 char *server_socket_path;
419
420 server_socket_path = xasprintf("%s/%s", ovs_rundir(),
421 "pdump_server_socket");
422 fatal_signal_add_file_to_unlink(server_socket_path);
423 free(server_socket_path);
424 }
425#endif
426
427 /* Finally, register the dpdk classes */
428 netdev_dpdk_register();
d7e2509e 429 return true;
01961bbd
DDP
430}
431
432void
433dpdk_init(const struct smap *ovs_other_config)
434{
ec2b0701
DDP
435 static bool enabled = false;
436
437 if (enabled || !ovs_other_config) {
438 return;
439 }
440
3e52fa56
AC
441 const char *dpdk_init_val = smap_get_def(ovs_other_config, "dpdk-init",
442 "false");
443
444 bool try_only = !strcmp(dpdk_init_val, "try");
445 if (!strcmp(dpdk_init_val, "true") || try_only) {
ec2b0701 446 static struct ovsthread_once once_enable = OVSTHREAD_ONCE_INITIALIZER;
01961bbd 447
ec2b0701 448 if (ovsthread_once_start(&once_enable)) {
40c23a57 449 VLOG_INFO("Using %s", rte_version());
ec2b0701 450 VLOG_INFO("DPDK Enabled - initializing...");
d7e2509e
AC
451 enabled = dpdk_init__(ovs_other_config);
452 if (enabled) {
453 VLOG_INFO("DPDK Enabled - initialized");
3e52fa56 454 } else if (!try_only) {
d7e2509e
AC
455 ovs_abort(rte_errno, "Cannot init EAL");
456 }
ec2b0701 457 ovsthread_once_done(&once_enable);
d7e2509e
AC
458 } else {
459 VLOG_ERR_ONCE("DPDK Initialization Failed.");
ec2b0701
DDP
460 }
461 } else {
5575908b 462 VLOG_INFO_ONCE("DPDK Disabled - Use other_config:dpdk-init to enable");
01961bbd 463 }
3e52fa56 464 dpdk_initialized = enabled;
01961bbd
DDP
465}
466
467const char *
468dpdk_get_vhost_sock_dir(void)
469{
470 return vhost_sock_dir;
471}
472
a14d1cc8
MK
473bool
474dpdk_vhost_iommu_enabled(void)
475{
476 return vhost_iommu_enabled;
477}
478
43307ad0
IS
479bool
480dpdk_per_port_memory(void)
481{
482 return per_port_memory;
483}
484
01961bbd
DDP
485void
486dpdk_set_lcore_id(unsigned cpu)
487{
488 /* NON_PMD_CORE_ID is reserved for use by non pmd threads. */
489 ovs_assert(cpu != NON_PMD_CORE_ID);
490 RTE_PER_LCORE(_lcore_id) = cpu;
491}
40c23a57
MC
492
493void
494print_dpdk_version(void)
495{
496 puts(rte_version());
497}
3e52fa56
AC
498
499void
500dpdk_status(const struct ovsrec_open_vswitch *cfg)
501{
502 if (cfg) {
503 ovsrec_open_vswitch_set_dpdk_initialized(cfg, dpdk_initialized);
504 ovsrec_open_vswitch_set_dpdk_version(cfg, rte_version());
505 }
506}