bb634c62ef21cf1cc85af39f74bffa9bbf3e51e5
[deb_dpdk.git] / drivers / net / bonding / rte_eth_bond_args.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 #include <rte_devargs.h>
35 #include <rte_pci.h>
36 #include <rte_kvargs.h>
37
38 #include <cmdline_parse.h>
39 #include <cmdline_parse_etheraddr.h>
40
41 #include "rte_eth_bond.h"
42 #include "rte_eth_bond_private.h"
43
44 const char *pmd_bond_init_valid_arguments[] = {
45         PMD_BOND_SLAVE_PORT_KVARG,
46         PMD_BOND_PRIMARY_SLAVE_KVARG,
47         PMD_BOND_MODE_KVARG,
48         PMD_BOND_XMIT_POLICY_KVARG,
49         PMD_BOND_SOCKET_ID_KVARG,
50         PMD_BOND_MAC_ADDR_KVARG,
51         PMD_BOND_AGG_MODE_KVARG,
52         "driver",
53         NULL
54 };
55
56 static inline int
57 find_port_id_by_pci_addr(const struct rte_pci_addr *pci_addr)
58 {
59         struct rte_pci_device *pci_dev;
60         struct rte_pci_addr *eth_pci_addr;
61         unsigned i;
62
63         for (i = 0; i < rte_eth_dev_count(); i++) {
64
65                 /* Currently populated by rte_eth_copy_pci_info().
66                  *
67                  * TODO: Once the PCI bus has arrived we should have a better
68                  * way to test for being a PCI device or not.
69                  */
70                 if (rte_eth_devices[i].data->kdrv == RTE_KDRV_UNKNOWN ||
71                     rte_eth_devices[i].data->kdrv == RTE_KDRV_NONE)
72                         continue;
73
74                 pci_dev = RTE_ETH_DEV_TO_PCI(&rte_eth_devices[i]);
75                 eth_pci_addr = &pci_dev->addr;
76
77                 if (pci_addr->bus == eth_pci_addr->bus &&
78                         pci_addr->devid == eth_pci_addr->devid &&
79                         pci_addr->domain == eth_pci_addr->domain &&
80                         pci_addr->function == eth_pci_addr->function)
81                         return i;
82         }
83         return -1;
84 }
85
86 static inline int
87 find_port_id_by_dev_name(const char *name)
88 {
89         unsigned i;
90
91         for (i = 0; i < rte_eth_dev_count(); i++) {
92                 if (rte_eth_devices[i].data == NULL)
93                         continue;
94
95                 if (strcmp(rte_eth_devices[i].device->name, name) == 0)
96                         return i;
97         }
98         return -1;
99 }
100
101 /**
102  * Parses a port identifier string to a port id by pci address, then by name,
103  * and finally port id.
104  */
105 static inline int
106 parse_port_id(const char *port_str)
107 {
108         struct rte_pci_addr dev_addr;
109         int port_id;
110
111         /* try parsing as pci address, physical devices */
112         if (eal_parse_pci_DomBDF(port_str, &dev_addr) == 0) {
113                 port_id = find_port_id_by_pci_addr(&dev_addr);
114                 if (port_id < 0)
115                         return -1;
116         } else {
117                 /* try parsing as device name, virtual devices */
118                 port_id = find_port_id_by_dev_name(port_str);
119                 if (port_id < 0) {
120                         char *end;
121                         errno = 0;
122
123                         /* try parsing as port id */
124                         port_id = strtol(port_str, &end, 10);
125                         if (*end != 0 || errno != 0)
126                                 return -1;
127                 }
128         }
129
130         if (port_id < 0 || port_id > RTE_MAX_ETHPORTS) {
131                 RTE_BOND_LOG(ERR, "Slave port specified (%s) outside expected range",
132                                 port_str);
133                 return -1;
134         }
135         return port_id;
136 }
137
138 int
139 bond_ethdev_parse_slave_port_kvarg(const char *key,
140                 const char *value, void *extra_args)
141 {
142         struct bond_ethdev_slave_ports *slave_ports;
143
144         if (value == NULL || extra_args == NULL)
145                 return -1;
146
147         slave_ports = extra_args;
148
149         if (strcmp(key, PMD_BOND_SLAVE_PORT_KVARG) == 0) {
150                 int port_id = parse_port_id(value);
151                 if (port_id < 0) {
152                         RTE_BOND_LOG(ERR, "Invalid slave port value (%s) specified", value);
153                         return -1;
154                 } else
155                         slave_ports->slaves[slave_ports->slave_count++] =
156                                         (uint8_t)port_id;
157         }
158         return 0;
159 }
160
161 int
162 bond_ethdev_parse_slave_mode_kvarg(const char *key __rte_unused,
163                 const char *value, void *extra_args)
164 {
165         uint8_t *mode;
166         char *endptr;
167
168         if (value == NULL || extra_args == NULL)
169                 return -1;
170
171         mode = extra_args;
172
173         errno = 0;
174         *mode = strtol(value, &endptr, 10);
175         if (*endptr != 0 || errno != 0)
176                 return -1;
177
178         /* validate mode value */
179         switch (*mode) {
180         case BONDING_MODE_ROUND_ROBIN:
181         case BONDING_MODE_ACTIVE_BACKUP:
182         case BONDING_MODE_BALANCE:
183         case BONDING_MODE_BROADCAST:
184         case BONDING_MODE_8023AD:
185         case BONDING_MODE_TLB:
186         case BONDING_MODE_ALB:
187                 return 0;
188         default:
189                 RTE_BOND_LOG(ERR, "Invalid slave mode value (%s) specified", value);
190                 return -1;
191         }
192 }
193
194 int
195 bond_ethdev_parse_slave_agg_mode_kvarg(const char *key __rte_unused,
196                 const char *value, void *extra_args)
197 {
198         uint8_t *agg_mode;
199
200         if (value == NULL || extra_args == NULL)
201                 return -1;
202
203         agg_mode = extra_args;
204
205         errno = 0;
206         if (strncmp(value, "stable", 6) == 0)
207                 *agg_mode = AGG_STABLE;
208
209         if (strncmp(value, "bandwidth", 9) == 0)
210                 *agg_mode = AGG_BANDWIDTH;
211
212         if (strncmp(value, "count", 5) == 0)
213                 *agg_mode = AGG_COUNT;
214
215         switch (*agg_mode) {
216         case AGG_STABLE:
217         case AGG_BANDWIDTH:
218         case AGG_COUNT:
219                 return 0;
220         default:
221                 RTE_BOND_LOG(ERR, "Invalid agg mode value stable/bandwidth/count");
222                 return -1;
223         }
224 }
225
226 int
227 bond_ethdev_parse_socket_id_kvarg(const char *key __rte_unused,
228                 const char *value, void *extra_args)
229 {
230         int socket_id;
231         char *endptr;
232
233         if (value == NULL || extra_args == NULL)
234                 return -1;
235
236         errno = 0;
237         socket_id = (uint8_t)strtol(value, &endptr, 10);
238         if (*endptr != 0 || errno != 0)
239                 return -1;
240
241         /* validate socket id value */
242         if (socket_id >= 0) {
243                 *(uint8_t *)extra_args = (uint8_t)socket_id;
244                 return 0;
245         }
246         return -1;
247 }
248
249 int
250 bond_ethdev_parse_primary_slave_port_id_kvarg(const char *key __rte_unused,
251                 const char *value, void *extra_args)
252 {
253         int primary_slave_port_id;
254
255         if (value == NULL || extra_args == NULL)
256                 return -1;
257
258         primary_slave_port_id = parse_port_id(value);
259         if (primary_slave_port_id < 0)
260                 return -1;
261
262         *(uint8_t *)extra_args = (uint8_t)primary_slave_port_id;
263
264         return 0;
265 }
266
267 int
268 bond_ethdev_parse_balance_xmit_policy_kvarg(const char *key __rte_unused,
269                 const char *value, void *extra_args)
270 {
271         uint8_t *xmit_policy;
272
273         if (value == NULL || extra_args == NULL)
274                 return -1;
275
276         xmit_policy = extra_args;
277
278         if (strcmp(PMD_BOND_XMIT_POLICY_LAYER2_KVARG, value) == 0)
279                 *xmit_policy = BALANCE_XMIT_POLICY_LAYER2;
280         else if (strcmp(PMD_BOND_XMIT_POLICY_LAYER23_KVARG, value) == 0)
281                 *xmit_policy = BALANCE_XMIT_POLICY_LAYER23;
282         else if (strcmp(PMD_BOND_XMIT_POLICY_LAYER34_KVARG, value) == 0)
283                 *xmit_policy = BALANCE_XMIT_POLICY_LAYER34;
284         else
285                 return -1;
286
287         return 0;
288 }
289
290 int
291 bond_ethdev_parse_bond_mac_addr_kvarg(const char *key __rte_unused,
292                 const char *value, void *extra_args)
293 {
294         if (value == NULL || extra_args == NULL)
295                 return -1;
296
297         /* Parse MAC */
298         return cmdline_parse_etheraddr(NULL, value, extra_args,
299                 sizeof(struct ether_addr));
300 }
301
302 int
303 bond_ethdev_parse_time_ms_kvarg(const char *key __rte_unused,
304                 const char *value, void *extra_args)
305 {
306         uint32_t time_ms;
307         char *endptr;
308
309         if (value == NULL || extra_args == NULL)
310                 return -1;
311
312         errno = 0;
313         time_ms = (uint32_t)strtol(value, &endptr, 10);
314         if (*endptr != 0 || errno != 0)
315                 return -1;
316
317         *(uint32_t *)extra_args = time_ms;
318
319         return 0;
320 }