devargs: fix null dereferencing on failure
[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 int
48 rte_eal_parse_devargs_str(const char *devargs_str,
49                         char **drvname, char **drvargs)
50 {
51         char *sep;
52
53         if ((devargs_str) == NULL || (drvname) == NULL || (drvargs == NULL))
54                 return -1;
55
56         *drvname = strdup(devargs_str);
57         if (drvname == NULL) {
58                 RTE_LOG(ERR, EAL,
59                         "cannot allocate temp memory for driver name\n");
60                 return -1;
61         }
62
63         /* set the first ',' to '\0' to split name and arguments */
64         sep = strchr(*drvname, ',');
65         if (sep != NULL) {
66                 sep[0] = '\0';
67                 *drvargs = strdup(sep + 1);
68         } else {
69                 *drvargs = strdup("");
70         }
71
72         if (*drvargs == NULL) {
73                 RTE_LOG(ERR, EAL,
74                         "cannot allocate temp memory for driver arguments\n");
75                 free(*drvname);
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                 RTE_LOG(ERR, EAL, "cannot allocate devargs\n");
93                 goto fail;
94         }
95         memset(devargs, 0, sizeof(*devargs));
96         devargs->type = devtype;
97
98         if (rte_eal_parse_devargs_str(devargs_str, &buf, &devargs->args))
99                 goto fail;
100
101         switch (devargs->type) {
102         case RTE_DEVTYPE_WHITELISTED_PCI:
103         case RTE_DEVTYPE_BLACKLISTED_PCI:
104                 /* try to parse pci identifier */
105                 if (eal_parse_pci_BDF(buf, &devargs->pci.addr) != 0 &&
106                     eal_parse_pci_DomBDF(buf, &devargs->pci.addr) != 0) {
107                         RTE_LOG(ERR, EAL, "invalid PCI identifier <%s>\n", buf);
108                         goto fail;
109                 }
110                 break;
111         case RTE_DEVTYPE_VIRTUAL:
112                 /* save driver name */
113                 ret = snprintf(devargs->virtual.drv_name,
114                                sizeof(devargs->virtual.drv_name), "%s", buf);
115                 if (ret < 0 || ret >= (int)sizeof(devargs->virtual.drv_name)) {
116                         RTE_LOG(ERR, EAL, "driver name too large: <%s>\n", buf);
117                         goto fail;
118                 }
119                 break;
120         }
121
122         free(buf);
123         TAILQ_INSERT_TAIL(&devargs_list, devargs, next);
124         return 0;
125
126 fail:
127         if (buf)
128                 free(buf);
129         if (devargs) {
130                 free(devargs->args);
131                 free(devargs);
132         }
133
134         return -1;
135 }
136
137 /* count the number of devices of a specified type */
138 unsigned int
139 rte_eal_devargs_type_count(enum rte_devtype devtype)
140 {
141         struct rte_devargs *devargs;
142         unsigned int count = 0;
143
144         TAILQ_FOREACH(devargs, &devargs_list, next) {
145                 if (devargs->type != devtype)
146                         continue;
147                 count++;
148         }
149         return count;
150 }
151
152 /* dump the user devices on the console */
153 void
154 rte_eal_devargs_dump(FILE *f)
155 {
156         struct rte_devargs *devargs;
157
158         fprintf(f, "User device white list:\n");
159         TAILQ_FOREACH(devargs, &devargs_list, next) {
160                 if (devargs->type == RTE_DEVTYPE_WHITELISTED_PCI)
161                         fprintf(f, "  PCI whitelist " PCI_PRI_FMT " %s\n",
162                                devargs->pci.addr.domain,
163                                devargs->pci.addr.bus,
164                                devargs->pci.addr.devid,
165                                devargs->pci.addr.function,
166                                devargs->args);
167                 else if (devargs->type == RTE_DEVTYPE_BLACKLISTED_PCI)
168                         fprintf(f, "  PCI blacklist " PCI_PRI_FMT " %s\n",
169                                devargs->pci.addr.domain,
170                                devargs->pci.addr.bus,
171                                devargs->pci.addr.devid,
172                                devargs->pci.addr.function,
173                                devargs->args);
174                 else if (devargs->type == RTE_DEVTYPE_VIRTUAL)
175                         fprintf(f, "  VIRTUAL %s %s\n",
176                                devargs->virtual.drv_name,
177                                devargs->args);
178                 else
179                         fprintf(f, "  UNKNOWN %s\n", devargs->args);
180         }
181 }