8c9b31ace7fcb78d96c8c6444a7bda725cfc1667
[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 #include <string.h>
37
38 #include <rte_log.h>
39 #include <rte_pci.h>
40 #include <rte_devargs.h>
41 #include "eal_private.h"
42
43 /** Global list of user devices */
44 struct rte_devargs_list devargs_list =
45         TAILQ_HEAD_INITIALIZER(devargs_list);
46
47 /* store a whitelist parameter for later parsing */
48 int
49 rte_eal_devargs_add(enum rte_devtype devtype, const char *devargs_str)
50 {
51         struct rte_devargs *devargs = NULL;
52         char buf[RTE_DEVARGS_LEN];
53         char *sep;
54         int ret;
55
56         ret = snprintf(buf, sizeof(buf), "%s", devargs_str);
57         if (ret < 0 || ret >= (int)sizeof(buf)) {
58                 RTE_LOG(ERR, EAL, "user device args too large: <%s>\n",
59                         devargs_str);
60                 goto fail;
61         }
62
63         /* use malloc instead of rte_malloc as it's called early at init */
64         devargs = malloc(sizeof(*devargs));
65         if (devargs == NULL) {
66                 RTE_LOG(ERR, EAL, "cannot allocate devargs\n");
67                 goto fail;
68         }
69         memset(devargs, 0, sizeof(*devargs));
70         devargs->type = devtype;
71
72         /* set the first ',' to '\0' to split name and arguments */
73         sep = strchr(buf, ',');
74         if (sep != NULL) {
75                 sep[0] = '\0';
76                 snprintf(devargs->args, sizeof(devargs->args), "%s", sep + 1);
77         }
78
79         switch (devargs->type) {
80         case RTE_DEVTYPE_WHITELISTED_PCI:
81         case RTE_DEVTYPE_BLACKLISTED_PCI:
82                 /* try to parse pci identifier */
83                 if (eal_parse_pci_BDF(buf, &devargs->pci.addr) != 0 &&
84                     eal_parse_pci_DomBDF(buf, &devargs->pci.addr) != 0) {
85                         RTE_LOG(ERR, EAL, "invalid PCI identifier <%s>\n", buf);
86                         goto fail;
87                 }
88                 break;
89         case RTE_DEVTYPE_VIRTUAL:
90                 /* save driver name */
91                 ret = snprintf(devargs->virtual.drv_name,
92                                sizeof(devargs->virtual.drv_name), "%s", buf);
93                 if (ret < 0 || ret >= (int)sizeof(devargs->virtual.drv_name)) {
94                         RTE_LOG(ERR, EAL, "driver name too large: <%s>\n", buf);
95                         goto fail;
96                 }
97                 break;
98         }
99
100         TAILQ_INSERT_TAIL(&devargs_list, devargs, next);
101         return 0;
102
103 fail:
104         if (devargs)
105                 free(devargs);
106         return -1;
107 }
108
109 /* count the number of devices of a specified type */
110 unsigned int
111 rte_eal_devargs_type_count(enum rte_devtype devtype)
112 {
113         struct rte_devargs *devargs;
114         unsigned int count = 0;
115
116         TAILQ_FOREACH(devargs, &devargs_list, next) {
117                 if (devargs->type != devtype)
118                         continue;
119                 count++;
120         }
121         return count;
122 }
123
124 /* dump the user devices on the console */
125 void
126 rte_eal_devargs_dump(FILE *f)
127 {
128         struct rte_devargs *devargs;
129
130         fprintf(f, "User device white list:\n");
131         TAILQ_FOREACH(devargs, &devargs_list, next) {
132                 if (devargs->type == RTE_DEVTYPE_WHITELISTED_PCI)
133                         fprintf(f, "  PCI whitelist " PCI_PRI_FMT " %s\n",
134                                devargs->pci.addr.domain,
135                                devargs->pci.addr.bus,
136                                devargs->pci.addr.devid,
137                                devargs->pci.addr.function,
138                                devargs->args);
139                 else if (devargs->type == RTE_DEVTYPE_BLACKLISTED_PCI)
140                         fprintf(f, "  PCI blacklist " PCI_PRI_FMT " %s\n",
141                                devargs->pci.addr.domain,
142                                devargs->pci.addr.bus,
143                                devargs->pci.addr.devid,
144                                devargs->pci.addr.function,
145                                devargs->args);
146                 else if (devargs->type == RTE_DEVTYPE_VIRTUAL)
147                         fprintf(f, "  VIRTUAL %s %s\n",
148                                devargs->virtual.drv_name,
149                                devargs->args);
150                 else
151                         fprintf(f, "  UNKNOWN %s\n", devargs->args);
152         }
153 }