#include <rte_memzone.h>
#include <rte_tailq.h>
#include <rte_eal.h>
+#include <rte_string_fns.h>
+#include <rte_common.h>
#include "eal_private.h"
return -1;
}
+/*
+ * Check if a device is ok to use according to whitelist rules.
+ */
+static int
+pcidev_is_whitelisted(struct rte_pci_device *dev)
+{
+ char buf[16];
+ if (dev->addr.domain == 0) {
+ rte_snprintf(buf, sizeof(buf), PCI_SHORT_PRI_FMT, dev->addr.bus,
+ dev->addr.devid, dev->addr.function);
+ if (eal_dev_is_whitelisted(buf, NULL))
+ return 1;
+ }
+ rte_snprintf(buf, sizeof(buf), PCI_PRI_FMT, dev->addr.domain,dev->addr.bus,
+ dev->addr.devid, dev->addr.function);
+ return eal_dev_is_whitelisted(buf, NULL);
+}
+
/*
* Scan the content of the PCI bus, and call the devinit() function for
* all registered drivers that have a matching entry in its id_table
struct rte_pci_device *dev = NULL;
TAILQ_FOREACH(dev, &device_list, next)
- pci_probe_all_drivers(dev);
+ if (!eal_dev_whitelist_exists())
+ pci_probe_all_drivers(dev);
+ else if (pcidev_is_whitelisted(dev) && pci_probe_all_drivers(dev) < 0 )
+ rte_exit(EXIT_FAILURE, "Requested device " PCI_PRI_FMT
+ " cannot be used\n", dev->addr.domain,dev->addr.bus,
+ dev->addr.devid, dev->addr.function);
+
return 0;
}
--- /dev/null
+/*-
+ * BSD LICENSE
+ *
+ * Copyright(c) 2010-2013 Intel Corporation. All rights reserved.
+ * All rights reserved.
+ *
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions
+ * are met:
+ *
+ * * Redistributions of source code must retain the above copyright
+ * notice, this list of conditions and the following disclaimer.
+ * * Redistributions in binary form must reproduce the above copyright
+ * notice, this list of conditions and the following disclaimer in
+ * the documentation and/or other materials provided with the
+ * distribution.
+ * * Neither the name of Intel Corporation nor the names of its
+ * contributors may be used to endorse or promote products derived
+ * from this software without specific prior written permission.
+ *
+ * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
+ * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
+ * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
+ * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
+ * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
+ * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
+ * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
+ * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
+ * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
+ * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
+ * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
+ */
+
+
+/**
+ * This file provides functions for managing a whitelist of devices. An EAL
+ * command-line parameter should be used for specifying what devices to
+ * whitelist, and the functions here should be called in handling that
+ * parameter. Then when scanning the PCI bus, the is_whitelisted() function
+ * can be used to query the previously set up whitelist.
+ */
+#include <string.h>
+#include <rte_string_fns.h>
+#include <rte_log.h>
+#include <rte_debug.h>
+#include <rte_pci.h>
+#include <ctype.h>
+#include "eal_private.h"
+
+static char dev_list_str[4096];
+static size_t dev_list_str_len = 0;
+
+/*
+ * structure for storing a whitelist entry. Unlike for blacklists, we may
+ * in future use this for dummy NICs not backed by a physical device, e.g.
+ * backed by a file-system object instead, so we store the device path/addr
+ * as a string, rather than as a PCI Bus-Device-Function.
+ */
+static struct whitelist_entry {
+ const char *device_str;
+ const char *device_params;
+} whitelist[RTE_MAX_ETHPORTS] = { {NULL, NULL} };
+
+static unsigned num_wl_entries = 0;
+
+/* store a whitelist parameter for later parsing */
+int
+eal_dev_whitelist_add_entry(const char *entry)
+{
+ dev_list_str_len += rte_snprintf(&dev_list_str[dev_list_str_len],
+ sizeof(dev_list_str)-dev_list_str_len, "%s,", entry);
+ /* check for strings that won't fit (snprintf doesn't go beyond buffer) */
+ if (dev_list_str_len >= sizeof(dev_list_str)) {
+ dev_list_str_len = sizeof(dev_list_str) - 1;
+ return -1;
+ }
+
+ return 0;
+}
+
+/* check if a whitelist has been set up */
+int
+eal_dev_whitelist_exists(void)
+{
+ return !!dev_list_str_len;
+}
+
+/* sanity checks a whitelist entry to ensure device is correct */
+static int
+is_valid_wl_entry(const char *device_str, size_t dev_buf_len)
+{
+ char buf[16];
+ unsigned i;
+ struct rte_pci_addr pci_addr = { .domain = 0 };
+
+ if (eal_parse_pci_BDF(device_str, &pci_addr) == 0) {
+ size_t n = rte_snprintf(buf, sizeof(buf), PCI_SHORT_PRI_FMT,
+ pci_addr.bus, pci_addr.devid, pci_addr.function);
+ return (n == dev_buf_len) && (!strncmp(buf, device_str, dev_buf_len));
+ }
+ if (eal_parse_pci_DomBDF(device_str, &pci_addr) == 0) {
+ size_t n = rte_snprintf(buf, sizeof(buf), PCI_PRI_FMT, pci_addr.domain,
+ pci_addr.bus, pci_addr.devid, pci_addr.function);
+ return (n == dev_buf_len) && (!strncmp(buf, device_str, dev_buf_len));
+ }
+ return 0;
+}
+
+/*
+ * parse a whitelist string into a set of valid devices. To be called once
+ * all parameters have been added to the whitelist string.
+ */
+int
+eal_dev_whitelist_parse(void)
+{
+ char *devs[RTE_MAX_ETHPORTS + 1] = { NULL };
+ int i, num_devs;
+ unsigned dev_name_len, j;
+
+ if (!eal_dev_whitelist_exists())
+ return -1;
+
+ /* strip off any extra commas */
+ if (dev_list_str[dev_list_str_len - 1] == ',')
+ dev_list_str[--dev_list_str_len] = '\0';
+
+ /* split on commas into separate device entries */
+ num_devs = rte_strsplit(dev_list_str, sizeof(dev_list_str), devs,
+ RTE_MAX_ETHPORTS+1, ',');
+ if (num_devs > RTE_MAX_ETHPORTS) {
+ RTE_LOG(ERR, EAL, "Error, too many devices specified. "
+ "[RTE_MAX_ETHPORTS = %u]\n", (unsigned)RTE_MAX_ETHPORTS);
+ return -1;
+ }
+
+ size_t buf_len_rem = sizeof(dev_list_str); /* for tracking buffer length */
+ for (i = 0; i < num_devs; i++) {
+ char *dev_n_params[2]; /* possibly split device name from params*/
+
+ size_t curr_len = strnlen(devs[i], buf_len_rem);
+ buf_len_rem-= (curr_len + 1);
+
+ int split_res = rte_strsplit(devs[i], curr_len, dev_n_params, 2, ';');
+
+ /* device names go lower case, i.e. '00:0A.0' wouldn't work
+ * while '00:0a.0' would. */
+ dev_name_len = strnlen(dev_n_params[0], curr_len);
+ for (j = 0; j < dev_name_len; j++)
+ dev_n_params[0][j] = (char)tolower((int)dev_n_params[0][j]);
+
+ switch (split_res) {
+ case 2:
+ whitelist[i].device_params = dev_n_params[1]; /* fallthrough */
+ case 1:
+ whitelist[i].device_str = dev_n_params[0];
+ break;
+ default: /* should never ever occur */
+ rte_panic("Fatal error parsing whitelist [--use-device] options\n");
+ }
+
+ if (!is_valid_wl_entry(whitelist[i].device_str,
+ strnlen(whitelist[i].device_str, curr_len))) {
+ RTE_LOG(ERR, EAL, "Error parsing device identifier: '%s'\n",
+ whitelist[i].device_str);
+ return -1;
+ }
+ }
+ num_wl_entries = num_devs;
+ return 0;
+}
+
+/* check if a device is on the whitelist */
+int
+eal_dev_is_whitelisted(const char *device_str, const char **params)
+{
+ unsigned i;
+ if (!eal_dev_whitelist_exists())
+ return 0; /* return false if no whitelist set up */
+
+ for (i = 0; i < num_wl_entries; i++)
+ if (strncmp(device_str, whitelist[i].device_str, 32) == 0) {
+ if (params != NULL)
+ *params = whitelist[i].device_params;
+ return 1;
+ }
+
+ return 0;
+}
+
+/* clear the whole whitelist */
+void
+eal_dev_whitelist_clear(void)
+{
+ dev_list_str[0] = '\0';
+ dev_list_str_len = num_wl_entries = 0;
+}
#define OPT_NO_HUGE "no-huge"
#define OPT_FILE_PREFIX "file-prefix"
#define OPT_SOCKET_MEM "socket-mem"
+#define OPT_USE_DEVICE "use-device"
#define OPT_SYSLOG "syslog"
#define RTE_EAL_BLACKLIST_SIZE 0x100
" --"OPT_HUGE_DIR" : directory where hugetlbfs is mounted\n"
" --"OPT_PROC_TYPE" : type of this process\n"
" --"OPT_FILE_PREFIX": prefix for hugepage filenames\n"
+ " --"OPT_USE_DEVICE": use the specified ethernet device(s) only."
+ "Use comma-separate <[domain:]bus:devid.func> values.\n"
+ " [NOTE: Cannot be used with -b option]\n"
" --"OPT_VMWARE_TSC_MAP": use VMware TSC map instead of "
"native RDTSC\n"
"\nEAL options for DEBUG use only:\n"
{OPT_PROC_TYPE, 1, 0, 0},
{OPT_FILE_PREFIX, 1, 0, 0},
{OPT_SOCKET_MEM, 1, 0, 0},
+ {OPT_USE_DEVICE, 1, 0, 0},
{OPT_SYSLOG, 1, NULL, 0},
{0, 0, 0, 0}
};
return -1;
}
}
+ else if (!strcmp(lgopts[option_index].name, OPT_USE_DEVICE)) {
+ eal_dev_whitelist_add_entry(optarg);
+ }
else if (!strcmp(lgopts[option_index].name, OPT_SYSLOG)) {
if (eal_parse_syslog(optarg) < 0) {
RTE_LOG(ERR, EAL, "invalid parameters for --"
return -1;
}
- if (blacklist_index > 0)
+ /* if no blacklist, parse a whitelist */
+ if (blacklist_index > 0) {
+ if (eal_dev_whitelist_exists()) {
+ RTE_LOG(ERR, EAL, "Error: blacklist [-b] and whitelist "
+ "[--use-device] options cannot be used at the same time\n");
+ eal_usage(prgname);
+ return -1;
+ }
rte_eal_pci_set_blacklist(eal_dev_blacklist, blacklist_index);
+ } else {
+ if (eal_dev_whitelist_exists() && eal_dev_whitelist_parse() < 0) {
+ RTE_LOG(ERR,EAL, "Error parsing whitelist[--use-device] options\n");
+ return -1;
+ }
+ }
if (optind >= 0)
argv[optind-1] = prgname;