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