update Intel copyright years to 2014
[dpdk.git] / lib / librte_eal / common / eal_common_whitelist.c
1 /*-
2  *   BSD LICENSE
3  * 
4  *   Copyright(c) 2010-2014 Intel Corporation. All rights reserved.
5  *   All rights reserved.
6  * 
7  *   Redistribution and use in source and binary forms, with or without
8  *   modification, are permitted provided that the following conditions
9  *   are met:
10  * 
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
16  *       distribution.
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.
20  * 
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.
32  */
33
34
35 /**
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.
41  */
42 #include <string.h>
43 #include <rte_string_fns.h>
44 #include <rte_log.h>
45 #include <rte_debug.h>
46 #include <rte_pci.h>
47 #include <ctype.h>
48 #ifdef RTE_LIBRTE_PMD_RING
49 #include <rte_eth_ring.h>
50 #endif
51 #ifdef RTE_LIBRTE_PMD_PCAP
52 #include <rte_eth_pcap.h>
53 #endif
54 #include "eal_private.h"
55
56 static char dev_list_str[4096];
57 static size_t dev_list_str_len = 0;
58
59 /*
60  * structure for storing a whitelist entry. Unlike for blacklists, we may
61  * in future use this for dummy NICs not backed by a physical device, e.g.
62  * backed by a file-system object instead, so we store the device path/addr
63  * as a string, rather than as a PCI Bus-Device-Function.
64  */
65 static struct whitelist_entry {
66         const char *device_str;
67         const char *device_params;
68 } whitelist[RTE_MAX_ETHPORTS] = { {NULL, NULL} };
69
70 static unsigned num_wl_entries = 0;
71
72 /* store a whitelist parameter for later parsing */
73 int
74 eal_dev_whitelist_add_entry(const char *entry)
75 {
76         dev_list_str_len += rte_snprintf(&dev_list_str[dev_list_str_len],
77                         sizeof(dev_list_str)-dev_list_str_len, "%s,", entry);
78         /* check for strings that won't fit (snprintf doesn't go beyond buffer) */
79         if (dev_list_str_len >= sizeof(dev_list_str)) {
80                 dev_list_str_len = sizeof(dev_list_str) - 1;
81                 return -1;
82         }
83
84         return 0;
85 }
86
87 /* check if a whitelist has been set up */
88 int
89 eal_dev_whitelist_exists(void)
90 {
91         return !!dev_list_str_len;
92 }
93
94 /* sanity checks a whitelist entry to ensure device is correct */
95 static int
96 is_valid_wl_entry(const char *device_str, size_t dev_buf_len)
97 {
98 #define NUM_PREFIXES (sizeof(non_pci_prefixes)/sizeof(non_pci_prefixes[0]))
99         static const char *non_pci_prefixes[] = {
100 #ifdef  RTE_LIBRTE_PMD_RING
101                         RTE_ETH_RING_PARAM_NAME,
102 #endif
103 #ifdef RTE_LIBRTE_PMD_PCAP
104                         RTE_ETH_PCAP_PARAM_NAME,
105 #endif
106                         "-nodev-" /* dummy value to prevent compiler warnings */
107         };
108         static uint8_t prefix_counts[NUM_PREFIXES] = {0};
109         char buf[16];
110         unsigned i;
111         struct rte_pci_addr pci_addr = { .domain = 0 };
112
113         if (eal_parse_pci_BDF(device_str, &pci_addr) == 0) {
114                 size_t n = rte_snprintf(buf, sizeof(buf), PCI_SHORT_PRI_FMT,
115                                 pci_addr.bus, pci_addr.devid, pci_addr.function);
116                 return (n == dev_buf_len) && (!strncmp(buf, device_str, dev_buf_len));
117         }
118         if (eal_parse_pci_DomBDF(device_str, &pci_addr) == 0) {
119                 size_t n = rte_snprintf(buf, sizeof(buf), PCI_PRI_FMT, pci_addr.domain,
120                                 pci_addr.bus, pci_addr.devid, pci_addr.function);
121                 return (n == dev_buf_len) && (!strncmp(buf, device_str, dev_buf_len));
122         }
123         for (i = 0; i < NUM_PREFIXES; i++) {
124                 size_t n = rte_snprintf(buf, sizeof(buf), "%s%u",
125                                 non_pci_prefixes[i], prefix_counts[i]);
126                 if ((n == dev_buf_len) && (!strncmp(buf, device_str, dev_buf_len))) {
127                         prefix_counts[i]++;
128                         return 1;
129                 }
130         }
131         return 0;
132 }
133
134 /*
135  * parse a whitelist string into a set of valid devices. To be called once
136  * all parameters have been added to the whitelist string.
137  */
138 int
139 eal_dev_whitelist_parse(void)
140 {
141         char *devs[RTE_MAX_ETHPORTS + 1] = { NULL };
142         int i, num_devs;
143         unsigned dev_name_len, j;
144
145         if (!eal_dev_whitelist_exists())
146                 return -1;
147
148         /* strip off any extra commas */
149         if (dev_list_str[dev_list_str_len - 1] == ',')
150                 dev_list_str[--dev_list_str_len] = '\0';
151
152         /* split on commas into separate device entries */
153         num_devs = rte_strsplit(dev_list_str, sizeof(dev_list_str), devs,
154                         RTE_MAX_ETHPORTS+1, ',');
155         if (num_devs > RTE_MAX_ETHPORTS) {
156                 RTE_LOG(ERR, EAL, "Error, too many devices specified. "
157                                 "[RTE_MAX_ETHPORTS = %u]\n", (unsigned)RTE_MAX_ETHPORTS);
158                 return -1;
159         }
160
161         size_t buf_len_rem = sizeof(dev_list_str); /* for tracking buffer length */
162         for (i = 0; i < num_devs; i++) {
163                 char *dev_n_params[2]; /* possibly split device name from params*/
164
165                 size_t curr_len = strnlen(devs[i], buf_len_rem);
166                 buf_len_rem-= (curr_len + 1);
167
168                 int split_res = rte_strsplit(devs[i], curr_len, dev_n_params, 2, ';');
169
170                 /* device names go lower case, i.e. '00:0A.0' wouldn't work
171                  * while '00:0a.0' would. */
172                 dev_name_len = strnlen(dev_n_params[0], curr_len);
173                 for (j = 0; j < dev_name_len; j++)
174                         dev_n_params[0][j] = (char)tolower((int)dev_n_params[0][j]);
175
176                 switch (split_res) {
177                 case 2:
178                         whitelist[i].device_params = dev_n_params[1]; /* fallthrough */
179                 case 1:
180                         whitelist[i].device_str = dev_n_params[0];
181                         break;
182                 default: /* should never ever occur */
183                         rte_panic("Fatal error parsing whitelist [--use-device] options\n");
184                 }
185
186                 if (!is_valid_wl_entry(whitelist[i].device_str,
187                                 strnlen(whitelist[i].device_str, curr_len))) {
188                         RTE_LOG(ERR, EAL, "Error parsing device identifier: '%s'\n",
189                                         whitelist[i].device_str);
190                         return -1;
191                 }
192         }
193         num_wl_entries = num_devs;
194         return 0;
195 }
196
197 /* check if a device is on the whitelist */
198 int
199 eal_dev_is_whitelisted(const char *device_str, const char **params)
200 {
201         unsigned i;
202         if (!eal_dev_whitelist_exists())
203                 return 0; /* return false if no whitelist set up */
204
205         for (i = 0; i < num_wl_entries; i++)
206                 if (strncmp(device_str, whitelist[i].device_str, 32) == 0) {
207                         if (params != NULL)
208                                 *params = whitelist[i].device_params;
209                         return 1;
210                 }
211
212         return 0;
213 }
214
215 /* clear the whole whitelist */
216 void
217 eal_dev_whitelist_clear(void)
218 {
219         dev_list_str[0] = '\0';
220         dev_list_str_len = num_wl_entries = 0;
221 }