Imported Upstream version 16.11.1
[deb_dpdk.git] / lib / librte_eal / common / eal_common_devargs.c
1 /*-
2  *   BSD LICENSE
3  *
4  *   Copyright 2014 6WIND S.A.
5  *
6  *   Redistribution and use in source and binary forms, with or without
7  *   modification, are permitted provided that the following conditions
8  *   are met:
9  *
10  *     * Redistributions of source code must retain the above copyright
11  *       notice, this list of conditions and the following disclaimer.
12  *     * Redistributions in binary form must reproduce the above copyright
13  *       notice, this list of conditions and the following disclaimer in
14  *       the documentation and/or other materials provided with the
15  *       distribution.
16  *     * Neither the name of 6WIND S.A nor the names of its contributors
17  *       may be used to endorse or promote products derived from this
18  *       software without specific prior written permission.
19  *
20  *   THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
21  *   "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
22  *   LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
23  *   A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
24  *   OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
25  *   SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
26  *   LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
27  *   DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
28  *   THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
29  *   (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
30  *   OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
31  */
32
33 /* This file manages the list of devices and their arguments, as given
34  * by the user at startup
35  *
36  * Code here should not call rte_log since the EAL environment
37  * may not be initialized.
38  */
39
40 #include <stdio.h>
41 #include <string.h>
42
43 #include <rte_pci.h>
44 #include <rte_devargs.h>
45 #include "eal_private.h"
46
47 /** Global list of user devices */
48 struct rte_devargs_list devargs_list =
49         TAILQ_HEAD_INITIALIZER(devargs_list);
50
51 int
52 rte_eal_parse_devargs_str(const char *devargs_str,
53                         char **drvname, char **drvargs)
54 {
55         char *sep;
56
57         if ((devargs_str) == NULL || (drvname) == NULL || (drvargs == NULL))
58                 return -1;
59
60         *drvname = strdup(devargs_str);
61         if (*drvname == NULL)
62                 return -1;
63
64         /* set the first ',' to '\0' to split name and arguments */
65         sep = strchr(*drvname, ',');
66         if (sep != NULL) {
67                 sep[0] = '\0';
68                 *drvargs = strdup(sep + 1);
69         } else {
70                 *drvargs = strdup("");
71         }
72
73         if (*drvargs == NULL) {
74                 free(*drvname);
75                 *drvname = NULL;
76                 return -1;
77         }
78         return 0;
79 }
80
81 /* store a whitelist parameter for later parsing */
82 int
83 rte_eal_devargs_add(enum rte_devtype devtype, const char *devargs_str)
84 {
85         struct rte_devargs *devargs = NULL;
86         char *buf = NULL;
87         int ret;
88
89         /* use malloc instead of rte_malloc as it's called early at init */
90         devargs = malloc(sizeof(*devargs));
91         if (devargs == NULL)
92                 goto fail;
93
94         memset(devargs, 0, sizeof(*devargs));
95         devargs->type = devtype;
96
97         if (rte_eal_parse_devargs_str(devargs_str, &buf, &devargs->args))
98                 goto fail;
99
100         switch (devargs->type) {
101         case RTE_DEVTYPE_WHITELISTED_PCI:
102         case RTE_DEVTYPE_BLACKLISTED_PCI:
103                 /* try to parse pci identifier */
104                 if (eal_parse_pci_BDF(buf, &devargs->pci.addr) != 0 &&
105                     eal_parse_pci_DomBDF(buf, &devargs->pci.addr) != 0)
106                         goto fail;
107
108                 break;
109         case RTE_DEVTYPE_VIRTUAL:
110                 /* save driver name */
111                 ret = snprintf(devargs->virt.drv_name,
112                                sizeof(devargs->virt.drv_name), "%s", buf);
113                 if (ret < 0 || ret >= (int)sizeof(devargs->virt.drv_name))
114                         goto fail;
115
116                 break;
117         }
118
119         free(buf);
120         TAILQ_INSERT_TAIL(&devargs_list, devargs, next);
121         return 0;
122
123 fail:
124         free(buf);
125         if (devargs) {
126                 free(devargs->args);
127                 free(devargs);
128         }
129
130         return -1;
131 }
132
133 /* count the number of devices of a specified type */
134 unsigned int
135 rte_eal_devargs_type_count(enum rte_devtype devtype)
136 {
137         struct rte_devargs *devargs;
138         unsigned int count = 0;
139
140         TAILQ_FOREACH(devargs, &devargs_list, next) {
141                 if (devargs->type != devtype)
142                         continue;
143                 count++;
144         }
145         return count;
146 }
147
148 /* dump the user devices on the console */
149 void
150 rte_eal_devargs_dump(FILE *f)
151 {
152         struct rte_devargs *devargs;
153
154         fprintf(f, "User device white list:\n");
155         TAILQ_FOREACH(devargs, &devargs_list, next) {
156                 if (devargs->type == RTE_DEVTYPE_WHITELISTED_PCI)
157                         fprintf(f, "  PCI whitelist " PCI_PRI_FMT " %s\n",
158                                devargs->pci.addr.domain,
159                                devargs->pci.addr.bus,
160                                devargs->pci.addr.devid,
161                                devargs->pci.addr.function,
162                                devargs->args);
163                 else if (devargs->type == RTE_DEVTYPE_BLACKLISTED_PCI)
164                         fprintf(f, "  PCI blacklist " PCI_PRI_FMT " %s\n",
165                                devargs->pci.addr.domain,
166                                devargs->pci.addr.bus,
167                                devargs->pci.addr.devid,
168                                devargs->pci.addr.function,
169                                devargs->args);
170                 else if (devargs->type == RTE_DEVTYPE_VIRTUAL)
171                         fprintf(f, "  VIRTUAL %s %s\n",
172                                devargs->virt.drv_name,
173                                devargs->args);
174                 else
175                         fprintf(f, "  UNKNOWN %s\n", devargs->args);
176         }
177 }