7c65dda078f33bc58abd6376008c7125e2142ac2
[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                 pci_dev = RTE_ETH_DEV_TO_PCI(&rte_eth_devices[i]);
65                 eth_pci_addr = &pci_dev->addr;
66
67                 if (pci_addr->bus == eth_pci_addr->bus &&
68                         pci_addr->devid == eth_pci_addr->devid &&
69                         pci_addr->domain == eth_pci_addr->domain &&
70                         pci_addr->function == eth_pci_addr->function)
71                         return i;
72         }
73         return -1;
74 }
75
76 static inline int
77 find_port_id_by_dev_name(const char *name)
78 {
79         unsigned i;
80
81         for (i = 0; i < rte_eth_dev_count(); i++) {
82                 if (rte_eth_devices[i].data == NULL)
83                         continue;
84
85                 if (strcmp(rte_eth_devices[i].device->name, name) == 0)
86                         return i;
87         }
88         return -1;
89 }
90
91 static inline int
92 pci_addr_cmp(const struct rte_device *dev, const void *_pci_addr)
93 {
94         struct rte_pci_device *pdev;
95         const struct rte_pci_addr *paddr = _pci_addr;
96
97         pdev = RTE_DEV_TO_PCI(*(struct rte_device **)(void *)&dev);
98         return rte_eal_compare_pci_addr(&pdev->addr, paddr);
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         struct rte_bus *pci_bus;
110         struct rte_device *dev;
111         int port_id;
112
113         pci_bus = rte_bus_find_by_name("pci");
114         if (pci_bus == NULL) {
115                 RTE_LOG(ERR, PMD, "unable to find PCI bus\n");
116                 return -1;
117         }
118
119         /* try parsing as pci address, physical devices */
120         if (pci_bus->parse(port_str, &dev_addr) == 0) {
121                 dev = pci_bus->find_device(NULL, pci_addr_cmp, &dev_addr);
122                 if (dev == NULL) {
123                         RTE_LOG(ERR, PMD, "unable to find PCI device\n");
124                         return -1;
125                 }
126                 port_id = find_port_id_by_pci_addr(&dev_addr);
127                 if (port_id < 0)
128                         return -1;
129         } else {
130                 /* try parsing as device name, virtual devices */
131                 port_id = find_port_id_by_dev_name(port_str);
132                 if (port_id < 0) {
133                         char *end;
134                         errno = 0;
135
136                         /* try parsing as port id */
137                         port_id = strtol(port_str, &end, 10);
138                         if (*end != 0 || errno != 0)
139                                 return -1;
140                 }
141         }
142
143         if (port_id < 0 || port_id > RTE_MAX_ETHPORTS) {
144                 RTE_BOND_LOG(ERR, "Slave port specified (%s) outside expected range",
145                                 port_str);
146                 return -1;
147         }
148         return port_id;
149 }
150
151 int
152 bond_ethdev_parse_slave_port_kvarg(const char *key,
153                 const char *value, void *extra_args)
154 {
155         struct bond_ethdev_slave_ports *slave_ports;
156
157         if (value == NULL || extra_args == NULL)
158                 return -1;
159
160         slave_ports = extra_args;
161
162         if (strcmp(key, PMD_BOND_SLAVE_PORT_KVARG) == 0) {
163                 int port_id = parse_port_id(value);
164                 if (port_id < 0) {
165                         RTE_BOND_LOG(ERR, "Invalid slave port value (%s) specified", value);
166                         return -1;
167                 } else
168                         slave_ports->slaves[slave_ports->slave_count++] =
169                                         (uint8_t)port_id;
170         }
171         return 0;
172 }
173
174 int
175 bond_ethdev_parse_slave_mode_kvarg(const char *key __rte_unused,
176                 const char *value, void *extra_args)
177 {
178         uint8_t *mode;
179         char *endptr;
180
181         if (value == NULL || extra_args == NULL)
182                 return -1;
183
184         mode = extra_args;
185
186         errno = 0;
187         *mode = strtol(value, &endptr, 10);
188         if (*endptr != 0 || errno != 0)
189                 return -1;
190
191         /* validate mode value */
192         switch (*mode) {
193         case BONDING_MODE_ROUND_ROBIN:
194         case BONDING_MODE_ACTIVE_BACKUP:
195         case BONDING_MODE_BALANCE:
196         case BONDING_MODE_BROADCAST:
197         case BONDING_MODE_8023AD:
198         case BONDING_MODE_TLB:
199         case BONDING_MODE_ALB:
200                 return 0;
201         default:
202                 RTE_BOND_LOG(ERR, "Invalid slave mode value (%s) specified", value);
203                 return -1;
204         }
205 }
206
207 int
208 bond_ethdev_parse_slave_agg_mode_kvarg(const char *key __rte_unused,
209                 const char *value, void *extra_args)
210 {
211         uint8_t *agg_mode;
212
213         if (value == NULL || extra_args == NULL)
214                 return -1;
215
216         agg_mode = extra_args;
217
218         errno = 0;
219         if (strncmp(value, "stable", 6) == 0)
220                 *agg_mode = AGG_STABLE;
221
222         if (strncmp(value, "bandwidth", 9) == 0)
223                 *agg_mode = AGG_BANDWIDTH;
224
225         if (strncmp(value, "count", 5) == 0)
226                 *agg_mode = AGG_COUNT;
227
228         switch (*agg_mode) {
229         case AGG_STABLE:
230         case AGG_BANDWIDTH:
231         case AGG_COUNT:
232                 return 0;
233         default:
234                 RTE_BOND_LOG(ERR, "Invalid agg mode value stable/bandwidth/count");
235                 return -1;
236         }
237 }
238
239 int
240 bond_ethdev_parse_socket_id_kvarg(const char *key __rte_unused,
241                 const char *value, void *extra_args)
242 {
243         int socket_id;
244         char *endptr;
245
246         if (value == NULL || extra_args == NULL)
247                 return -1;
248
249         errno = 0;
250         socket_id = (uint8_t)strtol(value, &endptr, 10);
251         if (*endptr != 0 || errno != 0)
252                 return -1;
253
254         /* validate socket id value */
255         if (socket_id >= 0) {
256                 *(uint8_t *)extra_args = (uint8_t)socket_id;
257                 return 0;
258         }
259         return -1;
260 }
261
262 int
263 bond_ethdev_parse_primary_slave_port_id_kvarg(const char *key __rte_unused,
264                 const char *value, void *extra_args)
265 {
266         int primary_slave_port_id;
267
268         if (value == NULL || extra_args == NULL)
269                 return -1;
270
271         primary_slave_port_id = parse_port_id(value);
272         if (primary_slave_port_id < 0)
273                 return -1;
274
275         *(uint8_t *)extra_args = (uint8_t)primary_slave_port_id;
276
277         return 0;
278 }
279
280 int
281 bond_ethdev_parse_balance_xmit_policy_kvarg(const char *key __rte_unused,
282                 const char *value, void *extra_args)
283 {
284         uint8_t *xmit_policy;
285
286         if (value == NULL || extra_args == NULL)
287                 return -1;
288
289         xmit_policy = extra_args;
290
291         if (strcmp(PMD_BOND_XMIT_POLICY_LAYER2_KVARG, value) == 0)
292                 *xmit_policy = BALANCE_XMIT_POLICY_LAYER2;
293         else if (strcmp(PMD_BOND_XMIT_POLICY_LAYER23_KVARG, value) == 0)
294                 *xmit_policy = BALANCE_XMIT_POLICY_LAYER23;
295         else if (strcmp(PMD_BOND_XMIT_POLICY_LAYER34_KVARG, value) == 0)
296                 *xmit_policy = BALANCE_XMIT_POLICY_LAYER34;
297         else
298                 return -1;
299
300         return 0;
301 }
302
303 int
304 bond_ethdev_parse_bond_mac_addr_kvarg(const char *key __rte_unused,
305                 const char *value, void *extra_args)
306 {
307         if (value == NULL || extra_args == NULL)
308                 return -1;
309
310         /* Parse MAC */
311         return cmdline_parse_etheraddr(NULL, value, extra_args,
312                 sizeof(struct ether_addr));
313 }
314
315 int
316 bond_ethdev_parse_time_ms_kvarg(const char *key __rte_unused,
317                 const char *value, void *extra_args)
318 {
319         uint32_t time_ms;
320         char *endptr;
321
322         if (value == NULL || extra_args == NULL)
323                 return -1;
324
325         errno = 0;
326         time_ms = (uint32_t)strtol(value, &endptr, 10);
327         if (*endptr != 0 || errno != 0)
328                 return -1;
329
330         *(uint32_t *)extra_args = time_ms;
331
332         return 0;
333 }