4 * Copyright(c) 2010-2013 Intel Corporation. All rights reserved.
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
11 * * Redistributions of source code must retain the above copyright
12 * notice, this list of conditions and the following disclaimer.
13 * * Redistributions in binary form must reproduce the above copyright
14 * notice, this list of conditions and the following disclaimer in
15 * the documentation and/or other materials provided with the
17 * * Neither the name of Intel Corporation nor the names of its
18 * contributors may be used to endorse or promote products derived
19 * from this software without specific prior written permission.
21 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
22 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
23 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
24 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
25 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
26 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
27 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
28 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
29 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
30 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
31 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
36 * This file provides functions for managing a whitelist of devices. An EAL
37 * command-line parameter should be used for specifying what devices to
38 * whitelist, and the functions here should be called in handling that
39 * parameter. Then when scanning the PCI bus, the is_whitelisted() function
40 * can be used to query the previously set up whitelist.
43 #include <rte_string_fns.h>
45 #include <rte_debug.h>
48 #ifdef RTE_LIBRTE_PMD_RING
49 #include <rte_eth_ring.h>
51 #include "eal_private.h"
53 static char dev_list_str[4096];
54 static size_t dev_list_str_len = 0;
57 * structure for storing a whitelist entry. Unlike for blacklists, we may
58 * in future use this for dummy NICs not backed by a physical device, e.g.
59 * backed by a file-system object instead, so we store the device path/addr
60 * as a string, rather than as a PCI Bus-Device-Function.
62 static struct whitelist_entry {
63 const char *device_str;
64 const char *device_params;
65 } whitelist[RTE_MAX_ETHPORTS] = { {NULL, NULL} };
67 static unsigned num_wl_entries = 0;
69 /* store a whitelist parameter for later parsing */
71 eal_dev_whitelist_add_entry(const char *entry)
73 dev_list_str_len += rte_snprintf(&dev_list_str[dev_list_str_len],
74 sizeof(dev_list_str)-dev_list_str_len, "%s,", entry);
75 /* check for strings that won't fit (snprintf doesn't go beyond buffer) */
76 if (dev_list_str_len >= sizeof(dev_list_str)) {
77 dev_list_str_len = sizeof(dev_list_str) - 1;
84 /* check if a whitelist has been set up */
86 eal_dev_whitelist_exists(void)
88 return !!dev_list_str_len;
91 /* sanity checks a whitelist entry to ensure device is correct */
93 is_valid_wl_entry(const char *device_str, size_t dev_buf_len)
95 #define NUM_PREFIXES (sizeof(non_pci_prefixes)/sizeof(non_pci_prefixes[0]))
96 static const char *non_pci_prefixes[] = {
97 #ifdef RTE_LIBRTE_PMD_RING
98 RTE_ETH_RING_PARAM_NAME,
100 "-nodev-" /* dummy value to prevent compiler warnings */
102 static uint8_t prefix_counts[NUM_PREFIXES] = {0};
105 struct rte_pci_addr pci_addr = { .domain = 0 };
107 if (eal_parse_pci_BDF(device_str, &pci_addr) == 0) {
108 size_t n = rte_snprintf(buf, sizeof(buf), PCI_SHORT_PRI_FMT,
109 pci_addr.bus, pci_addr.devid, pci_addr.function);
110 return (n == dev_buf_len) && (!strncmp(buf, device_str, dev_buf_len));
112 if (eal_parse_pci_DomBDF(device_str, &pci_addr) == 0) {
113 size_t n = rte_snprintf(buf, sizeof(buf), PCI_PRI_FMT, pci_addr.domain,
114 pci_addr.bus, pci_addr.devid, pci_addr.function);
115 return (n == dev_buf_len) && (!strncmp(buf, device_str, dev_buf_len));
117 for (i = 0; i < NUM_PREFIXES; i++) {
118 size_t n = rte_snprintf(buf, sizeof(buf), "%s%u",
119 non_pci_prefixes[i], prefix_counts[i]);
120 if ((n == dev_buf_len) && (!strncmp(buf, device_str, dev_buf_len))) {
129 * parse a whitelist string into a set of valid devices. To be called once
130 * all parameters have been added to the whitelist string.
133 eal_dev_whitelist_parse(void)
135 char *devs[RTE_MAX_ETHPORTS + 1] = { NULL };
137 unsigned dev_name_len, j;
139 if (!eal_dev_whitelist_exists())
142 /* strip off any extra commas */
143 if (dev_list_str[dev_list_str_len - 1] == ',')
144 dev_list_str[--dev_list_str_len] = '\0';
146 /* split on commas into separate device entries */
147 num_devs = rte_strsplit(dev_list_str, sizeof(dev_list_str), devs,
148 RTE_MAX_ETHPORTS+1, ',');
149 if (num_devs > RTE_MAX_ETHPORTS) {
150 RTE_LOG(ERR, EAL, "Error, too many devices specified. "
151 "[RTE_MAX_ETHPORTS = %u]\n", (unsigned)RTE_MAX_ETHPORTS);
155 size_t buf_len_rem = sizeof(dev_list_str); /* for tracking buffer length */
156 for (i = 0; i < num_devs; i++) {
157 char *dev_n_params[2]; /* possibly split device name from params*/
159 size_t curr_len = strnlen(devs[i], buf_len_rem);
160 buf_len_rem-= (curr_len + 1);
162 int split_res = rte_strsplit(devs[i], curr_len, dev_n_params, 2, ';');
164 /* device names go lower case, i.e. '00:0A.0' wouldn't work
165 * while '00:0a.0' would. */
166 dev_name_len = strnlen(dev_n_params[0], curr_len);
167 for (j = 0; j < dev_name_len; j++)
168 dev_n_params[0][j] = (char)tolower((int)dev_n_params[0][j]);
172 whitelist[i].device_params = dev_n_params[1]; /* fallthrough */
174 whitelist[i].device_str = dev_n_params[0];
176 default: /* should never ever occur */
177 rte_panic("Fatal error parsing whitelist [--use-device] options\n");
180 if (!is_valid_wl_entry(whitelist[i].device_str,
181 strnlen(whitelist[i].device_str, curr_len))) {
182 RTE_LOG(ERR, EAL, "Error parsing device identifier: '%s'\n",
183 whitelist[i].device_str);
187 num_wl_entries = num_devs;
191 /* check if a device is on the whitelist */
193 eal_dev_is_whitelisted(const char *device_str, const char **params)
196 if (!eal_dev_whitelist_exists())
197 return 0; /* return false if no whitelist set up */
199 for (i = 0; i < num_wl_entries; i++)
200 if (strncmp(device_str, whitelist[i].device_str, 32) == 0) {
202 *params = whitelist[i].device_params;
209 /* clear the whole whitelist */
211 eal_dev_whitelist_clear(void)
213 dev_list_str[0] = '\0';
214 dev_list_str_len = num_wl_entries = 0;