mem: get hugepages config
[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 #ifdef RTE_LIBRTE_PMD_XENVIRT
55 #include <rte_eth_xenvirt.h>
56 #endif
57 #include "eal_private.h"
58
59 static char dev_list_str[4096];
60 static size_t dev_list_str_len = 0;
61
62 /*
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.
67  */
68 static struct whitelist_entry {
69         const char *device_str;
70         const char *device_params;
71 } whitelist[RTE_MAX_ETHPORTS] = { {NULL, NULL} };
72
73 static unsigned num_wl_entries = 0;
74
75 /* store a whitelist parameter for later parsing */
76 int
77 eal_dev_whitelist_add_entry(const char *entry)
78 {
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;
84                 return -1;
85         }
86
87         return 0;
88 }
89
90 /* check if a whitelist has been set up */
91 int
92 eal_dev_whitelist_exists(void)
93 {
94         return !!dev_list_str_len;
95 }
96
97 /* sanity checks a whitelist entry to ensure device is correct */
98 static int
99 is_valid_wl_entry(const char *device_str, size_t dev_buf_len)
100 {
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,
105 #endif
106 #ifdef RTE_LIBRTE_PMD_PCAP
107                         RTE_ETH_PCAP_PARAM_NAME,
108 #endif
109 #ifdef RTE_LIBRTE_PMD_XENVIRT
110                         RTE_ETH_XENVIRT_PARAM_NAME,
111 #endif
112                         "-nodev-" /* dummy value to prevent compiler warnings */
113         };
114         static uint8_t prefix_counts[NUM_PREFIXES] = {0};
115         char buf[16];
116         unsigned i;
117         struct rte_pci_addr pci_addr = { .domain = 0 };
118
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));
123         }
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));
128         }
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))) {
133                         prefix_counts[i]++;
134                         return 1;
135                 }
136         }
137         return 0;
138 }
139
140 /*
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.
143  */
144 int
145 eal_dev_whitelist_parse(void)
146 {
147         char *devs[RTE_MAX_ETHPORTS + 1] = { NULL };
148         int i, num_devs;
149         unsigned dev_name_len, j;
150
151         if (!eal_dev_whitelist_exists())
152                 return -1;
153
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';
157
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);
164                 return -1;
165         }
166
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*/
170
171                 size_t curr_len = strnlen(devs[i], buf_len_rem);
172                 buf_len_rem-= (curr_len + 1);
173
174                 int split_res = rte_strsplit(devs[i], curr_len, dev_n_params, 2, ';');
175
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]);
181
182                 switch (split_res) {
183                 case 2:
184                         whitelist[i].device_params = dev_n_params[1]; /* fallthrough */
185                 case 1:
186                         whitelist[i].device_str = dev_n_params[0];
187                         break;
188                 default: /* should never ever occur */
189                         rte_panic("Fatal error parsing whitelist [--use-device] options\n");
190                 }
191
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);
196                         return -1;
197                 }
198         }
199         num_wl_entries = num_devs;
200         return 0;
201 }
202
203 /* check if a device is on the whitelist */
204 int
205 eal_dev_is_whitelisted(const char *device_str, const char **params)
206 {
207         unsigned i;
208         if (!eal_dev_whitelist_exists())
209                 return 0; /* return false if no whitelist set up */
210
211         for (i = 0; i < num_wl_entries; i++)
212                 if (strncmp(device_str, whitelist[i].device_str, 32) == 0) {
213                         if (params != NULL)
214                                 *params = whitelist[i].device_params;
215                         return 1;
216                 }
217
218         return 0;
219 }
220
221 /* clear the whole whitelist */
222 void
223 eal_dev_whitelist_clear(void)
224 {
225         dev_list_str[0] = '\0';
226         dev_list_str_len = num_wl_entries = 0;
227 }