4 * Copyright(c) 2010-2014 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 #ifdef RTE_LIBRTE_PMD_PCAP
52 #include <rte_eth_pcap.h>
54 #ifdef RTE_LIBRTE_PMD_XENVIRT
55 #include <rte_eth_xenvirt.h>
57 #include "eal_private.h"
59 static char dev_list_str[4096];
60 static size_t dev_list_str_len = 0;
63 * structure for storing a whitelist entry. Unlike for blacklists, we may
64 * in future use this for dummy NICs not backed by a physical device, e.g.
65 * backed by a file-system object instead, so we store the device path/addr
66 * as a string, rather than as a PCI Bus-Device-Function.
68 static struct whitelist_entry {
69 const char *device_str;
70 const char *device_params;
71 } whitelist[RTE_MAX_ETHPORTS] = { {NULL, NULL} };
73 static unsigned num_wl_entries = 0;
75 /* store a whitelist parameter for later parsing */
77 eal_dev_whitelist_add_entry(const char *entry)
79 dev_list_str_len += rte_snprintf(&dev_list_str[dev_list_str_len],
80 sizeof(dev_list_str)-dev_list_str_len, "%s,", entry);
81 /* check for strings that won't fit (snprintf doesn't go beyond buffer) */
82 if (dev_list_str_len >= sizeof(dev_list_str)) {
83 dev_list_str_len = sizeof(dev_list_str) - 1;
90 /* check if a whitelist has been set up */
92 eal_dev_whitelist_exists(void)
94 return !!dev_list_str_len;
97 /* sanity checks a whitelist entry to ensure device is correct */
99 is_valid_wl_entry(const char *device_str, size_t dev_buf_len)
101 #define NUM_PREFIXES (sizeof(non_pci_prefixes)/sizeof(non_pci_prefixes[0]))
102 static const char *non_pci_prefixes[] = {
103 #ifdef RTE_LIBRTE_PMD_RING
104 RTE_ETH_RING_PARAM_NAME,
106 #ifdef RTE_LIBRTE_PMD_PCAP
107 RTE_ETH_PCAP_PARAM_NAME,
109 #ifdef RTE_LIBRTE_PMD_XENVIRT
110 RTE_ETH_XENVIRT_PARAM_NAME,
112 "-nodev-" /* dummy value to prevent compiler warnings */
114 static uint8_t prefix_counts[NUM_PREFIXES] = {0};
117 struct rte_pci_addr pci_addr = { .domain = 0 };
119 if (eal_parse_pci_BDF(device_str, &pci_addr) == 0) {
120 size_t n = rte_snprintf(buf, sizeof(buf), PCI_SHORT_PRI_FMT,
121 pci_addr.bus, pci_addr.devid, pci_addr.function);
122 return (n == dev_buf_len) && (!strncmp(buf, device_str, dev_buf_len));
124 if (eal_parse_pci_DomBDF(device_str, &pci_addr) == 0) {
125 size_t n = rte_snprintf(buf, sizeof(buf), PCI_PRI_FMT, pci_addr.domain,
126 pci_addr.bus, pci_addr.devid, pci_addr.function);
127 return (n == dev_buf_len) && (!strncmp(buf, device_str, dev_buf_len));
129 for (i = 0; i < NUM_PREFIXES; i++) {
130 size_t n = rte_snprintf(buf, sizeof(buf), "%s%u",
131 non_pci_prefixes[i], prefix_counts[i]);
132 if ((n == dev_buf_len) && (!strncmp(buf, device_str, dev_buf_len))) {
141 * parse a whitelist string into a set of valid devices. To be called once
142 * all parameters have been added to the whitelist string.
145 eal_dev_whitelist_parse(void)
147 char *devs[RTE_MAX_ETHPORTS + 1] = { NULL };
149 unsigned dev_name_len, j;
151 if (!eal_dev_whitelist_exists())
154 /* strip off any extra commas */
155 if (dev_list_str[dev_list_str_len - 1] == ',')
156 dev_list_str[--dev_list_str_len] = '\0';
158 /* split on commas into separate device entries */
159 num_devs = rte_strsplit(dev_list_str, sizeof(dev_list_str), devs,
160 RTE_MAX_ETHPORTS+1, ',');
161 if (num_devs > RTE_MAX_ETHPORTS) {
162 RTE_LOG(ERR, EAL, "Error, too many devices specified. "
163 "[RTE_MAX_ETHPORTS = %u]\n", (unsigned)RTE_MAX_ETHPORTS);
167 size_t buf_len_rem = sizeof(dev_list_str); /* for tracking buffer length */
168 for (i = 0; i < num_devs; i++) {
169 char *dev_n_params[2]; /* possibly split device name from params*/
171 size_t curr_len = strnlen(devs[i], buf_len_rem);
172 buf_len_rem-= (curr_len + 1);
174 int split_res = rte_strsplit(devs[i], curr_len, dev_n_params, 2, ';');
176 /* device names go lower case, i.e. '00:0A.0' wouldn't work
177 * while '00:0a.0' would. */
178 dev_name_len = strnlen(dev_n_params[0], curr_len);
179 for (j = 0; j < dev_name_len; j++)
180 dev_n_params[0][j] = (char)tolower((int)dev_n_params[0][j]);
184 whitelist[i].device_params = dev_n_params[1]; /* fallthrough */
186 whitelist[i].device_str = dev_n_params[0];
188 default: /* should never ever occur */
189 rte_panic("Fatal error parsing whitelist [--use-device] options\n");
192 if (!is_valid_wl_entry(whitelist[i].device_str,
193 strnlen(whitelist[i].device_str, curr_len))) {
194 RTE_LOG(ERR, EAL, "Error parsing device identifier: '%s'\n",
195 whitelist[i].device_str);
199 num_wl_entries = num_devs;
203 /* check if a device is on the whitelist */
205 eal_dev_is_whitelisted(const char *device_str, const char **params)
208 if (!eal_dev_whitelist_exists())
209 return 0; /* return false if no whitelist set up */
211 for (i = 0; i < num_wl_entries; i++)
212 if (strncmp(device_str, whitelist[i].device_str, 32) == 0) {
214 *params = whitelist[i].device_params;
221 /* clear the whole whitelist */
223 eal_dev_whitelist_clear(void)
225 dev_list_str[0] = '\0';
226 dev_list_str_len = num_wl_entries = 0;