4 * Copyright(c) 2010-2014 Intel Corporation. All rights reserved.
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
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
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.
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.
34 #include <rte_devargs.h>
36 #include <rte_bus_pci.h>
37 #include <rte_kvargs.h>
39 #include <cmdline_parse.h>
40 #include <cmdline_parse_etheraddr.h>
42 #include "rte_eth_bond.h"
43 #include "rte_eth_bond_private.h"
45 const char *pmd_bond_init_valid_arguments[] = {
46 PMD_BOND_SLAVE_PORT_KVARG,
47 PMD_BOND_PRIMARY_SLAVE_KVARG,
49 PMD_BOND_XMIT_POLICY_KVARG,
50 PMD_BOND_SOCKET_ID_KVARG,
51 PMD_BOND_MAC_ADDR_KVARG,
52 PMD_BOND_AGG_MODE_KVARG,
58 find_port_id_by_pci_addr(const struct rte_pci_addr *pci_addr)
60 struct rte_pci_device *pci_dev;
61 struct rte_pci_addr *eth_pci_addr;
64 for (i = 0; i < rte_eth_dev_count(); i++) {
65 pci_dev = RTE_ETH_DEV_TO_PCI(&rte_eth_devices[i]);
66 eth_pci_addr = &pci_dev->addr;
68 if (pci_addr->bus == eth_pci_addr->bus &&
69 pci_addr->devid == eth_pci_addr->devid &&
70 pci_addr->domain == eth_pci_addr->domain &&
71 pci_addr->function == eth_pci_addr->function)
78 find_port_id_by_dev_name(const char *name)
82 for (i = 0; i < rte_eth_dev_count(); i++) {
83 if (rte_eth_devices[i].data == NULL)
86 if (strcmp(rte_eth_devices[i].device->name, name) == 0)
93 bond_pci_addr_cmp(const struct rte_device *dev, const void *_pci_addr)
95 struct rte_pci_device *pdev;
96 const struct rte_pci_addr *paddr = _pci_addr;
98 pdev = RTE_DEV_TO_PCI(*(struct rte_device **)(void *)&dev);
99 return rte_eal_compare_pci_addr(&pdev->addr, paddr);
103 * Parses a port identifier string to a port id by pci address, then by name,
104 * and finally port id.
107 parse_port_id(const char *port_str)
109 struct rte_pci_addr dev_addr;
110 struct rte_bus *pci_bus;
111 struct rte_device *dev;
114 pci_bus = rte_bus_find_by_name("pci");
115 if (pci_bus == NULL) {
116 RTE_LOG(ERR, PMD, "unable to find PCI bus\n");
120 /* try parsing as pci address, physical devices */
121 if (pci_bus->parse(port_str, &dev_addr) == 0) {
122 dev = pci_bus->find_device(NULL, bond_pci_addr_cmp, &dev_addr);
124 RTE_LOG(ERR, PMD, "unable to find PCI device\n");
127 port_id = find_port_id_by_pci_addr(&dev_addr);
131 /* try parsing as device name, virtual devices */
132 port_id = find_port_id_by_dev_name(port_str);
137 /* try parsing as port id */
138 port_id = strtol(port_str, &end, 10);
139 if (*end != 0 || errno != 0)
144 if (port_id < 0 || port_id > RTE_MAX_ETHPORTS) {
145 RTE_BOND_LOG(ERR, "Slave port specified (%s) outside expected range",
153 bond_ethdev_parse_slave_port_kvarg(const char *key,
154 const char *value, void *extra_args)
156 struct bond_ethdev_slave_ports *slave_ports;
158 if (value == NULL || extra_args == NULL)
161 slave_ports = extra_args;
163 if (strcmp(key, PMD_BOND_SLAVE_PORT_KVARG) == 0) {
164 int port_id = parse_port_id(value);
166 RTE_BOND_LOG(ERR, "Invalid slave port value (%s) specified", value);
169 slave_ports->slaves[slave_ports->slave_count++] =
176 bond_ethdev_parse_slave_mode_kvarg(const char *key __rte_unused,
177 const char *value, void *extra_args)
182 if (value == NULL || extra_args == NULL)
188 *mode = strtol(value, &endptr, 10);
189 if (*endptr != 0 || errno != 0)
192 /* validate mode value */
194 case BONDING_MODE_ROUND_ROBIN:
195 case BONDING_MODE_ACTIVE_BACKUP:
196 case BONDING_MODE_BALANCE:
197 case BONDING_MODE_BROADCAST:
198 case BONDING_MODE_8023AD:
199 case BONDING_MODE_TLB:
200 case BONDING_MODE_ALB:
203 RTE_BOND_LOG(ERR, "Invalid slave mode value (%s) specified", value);
209 bond_ethdev_parse_slave_agg_mode_kvarg(const char *key __rte_unused,
210 const char *value, void *extra_args)
214 if (value == NULL || extra_args == NULL)
217 agg_mode = extra_args;
220 if (strncmp(value, "stable", 6) == 0)
221 *agg_mode = AGG_STABLE;
223 if (strncmp(value, "bandwidth", 9) == 0)
224 *agg_mode = AGG_BANDWIDTH;
226 if (strncmp(value, "count", 5) == 0)
227 *agg_mode = AGG_COUNT;
235 RTE_BOND_LOG(ERR, "Invalid agg mode value stable/bandwidth/count");
241 bond_ethdev_parse_socket_id_kvarg(const char *key __rte_unused,
242 const char *value, void *extra_args)
247 if (value == NULL || extra_args == NULL)
251 socket_id = (uint8_t)strtol(value, &endptr, 10);
252 if (*endptr != 0 || errno != 0)
255 /* validate socket id value */
256 if (socket_id >= 0) {
257 *(uint8_t *)extra_args = (uint8_t)socket_id;
264 bond_ethdev_parse_primary_slave_port_id_kvarg(const char *key __rte_unused,
265 const char *value, void *extra_args)
267 int primary_slave_port_id;
269 if (value == NULL || extra_args == NULL)
272 primary_slave_port_id = parse_port_id(value);
273 if (primary_slave_port_id < 0)
276 *(uint8_t *)extra_args = (uint8_t)primary_slave_port_id;
282 bond_ethdev_parse_balance_xmit_policy_kvarg(const char *key __rte_unused,
283 const char *value, void *extra_args)
285 uint8_t *xmit_policy;
287 if (value == NULL || extra_args == NULL)
290 xmit_policy = extra_args;
292 if (strcmp(PMD_BOND_XMIT_POLICY_LAYER2_KVARG, value) == 0)
293 *xmit_policy = BALANCE_XMIT_POLICY_LAYER2;
294 else if (strcmp(PMD_BOND_XMIT_POLICY_LAYER23_KVARG, value) == 0)
295 *xmit_policy = BALANCE_XMIT_POLICY_LAYER23;
296 else if (strcmp(PMD_BOND_XMIT_POLICY_LAYER34_KVARG, value) == 0)
297 *xmit_policy = BALANCE_XMIT_POLICY_LAYER34;
305 bond_ethdev_parse_bond_mac_addr_kvarg(const char *key __rte_unused,
306 const char *value, void *extra_args)
308 if (value == NULL || extra_args == NULL)
312 return cmdline_parse_etheraddr(NULL, value, extra_args,
313 sizeof(struct ether_addr));
317 bond_ethdev_parse_time_ms_kvarg(const char *key __rte_unused,
318 const char *value, void *extra_args)
323 if (value == NULL || extra_args == NULL)
327 time_ms = (uint32_t)strtol(value, &endptr, 10);
328 if (*endptr != 0 || errno != 0)
331 *(uint32_t *)extra_args = time_ms;