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