1 /* SPDX-License-Identifier: BSD-3-Clause
2 * Copyright(c) 2010-2014 Intel Corporation
5 #include <rte_devargs.h>
7 #include <rte_bus_pci.h>
8 #include <rte_kvargs.h>
10 #include <cmdline_parse.h>
11 #include <cmdline_parse_etheraddr.h>
13 #include "rte_eth_bond.h"
14 #include "rte_eth_bond_private.h"
16 const char *pmd_bond_init_valid_arguments[] = {
17 PMD_BOND_SLAVE_PORT_KVARG,
18 PMD_BOND_PRIMARY_SLAVE_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,
29 find_port_id_by_pci_addr(const struct rte_pci_addr *pci_addr)
31 struct rte_pci_device *pci_dev;
32 struct rte_pci_addr *eth_pci_addr;
35 RTE_ETH_FOREACH_DEV(i) {
36 pci_dev = RTE_ETH_DEV_TO_PCI(&rte_eth_devices[i]);
37 eth_pci_addr = &pci_dev->addr;
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)
49 find_port_id_by_dev_name(const char *name)
53 RTE_ETH_FOREACH_DEV(i) {
54 if (rte_eth_devices[i].data == NULL)
57 if (strcmp(rte_eth_devices[i].device->name, name) == 0)
64 bond_pci_addr_cmp(const struct rte_device *dev, const void *_pci_addr)
66 struct rte_pci_device *pdev;
67 const struct rte_pci_addr *paddr = _pci_addr;
69 pdev = RTE_DEV_TO_PCI(*(struct rte_device **)(void *)&dev);
70 return rte_eal_compare_pci_addr(&pdev->addr, paddr);
74 * Parses a port identifier string to a port id by pci address, then by name,
75 * and finally port id.
78 parse_port_id(const char *port_str)
80 struct rte_pci_addr dev_addr;
81 struct rte_bus *pci_bus;
82 struct rte_device *dev;
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");
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);
95 RTE_BOND_LOG(ERR, "unable to find PCI device");
98 port_id = find_port_id_by_pci_addr(&dev_addr);
102 /* try parsing as device name, virtual devices */
103 port_id = find_port_id_by_dev_name(port_str);
108 /* try parsing as port id */
109 port_id = strtol(port_str, &end, 10);
110 if (*end != 0 || errno != 0)
115 if (port_id < 0 || port_id > RTE_MAX_ETHPORTS) {
116 RTE_BOND_LOG(ERR, "Slave port specified (%s) outside expected range",
124 bond_ethdev_parse_slave_port_kvarg(const char *key,
125 const char *value, void *extra_args)
127 struct bond_ethdev_slave_ports *slave_ports;
129 if (value == NULL || extra_args == NULL)
132 slave_ports = extra_args;
134 if (strcmp(key, PMD_BOND_SLAVE_PORT_KVARG) == 0) {
135 int port_id = parse_port_id(value);
137 RTE_BOND_LOG(ERR, "Invalid slave port value (%s) specified",
141 slave_ports->slaves[slave_ports->slave_count++] =
148 bond_ethdev_parse_slave_mode_kvarg(const char *key __rte_unused,
149 const char *value, void *extra_args)
154 if (value == NULL || extra_args == NULL)
160 *mode = strtol(value, &endptr, 10);
161 if (*endptr != 0 || errno != 0)
164 /* validate mode value */
166 case BONDING_MODE_ROUND_ROBIN:
167 case BONDING_MODE_ACTIVE_BACKUP:
168 case BONDING_MODE_BALANCE:
169 case BONDING_MODE_BROADCAST:
170 case BONDING_MODE_8023AD:
171 case BONDING_MODE_TLB:
172 case BONDING_MODE_ALB:
175 RTE_BOND_LOG(ERR, "Invalid slave mode value (%s) specified", value);
181 bond_ethdev_parse_slave_agg_mode_kvarg(const char *key __rte_unused,
182 const char *value, void *extra_args)
186 if (value == NULL || extra_args == NULL)
189 agg_mode = extra_args;
192 if (strncmp(value, "stable", 6) == 0)
193 *agg_mode = AGG_STABLE;
195 if (strncmp(value, "bandwidth", 9) == 0)
196 *agg_mode = AGG_BANDWIDTH;
198 if (strncmp(value, "count", 5) == 0)
199 *agg_mode = AGG_COUNT;
207 RTE_BOND_LOG(ERR, "Invalid agg mode value stable/bandwidth/count");
213 bond_ethdev_parse_socket_id_kvarg(const char *key __rte_unused,
214 const char *value, void *extra_args)
219 if (value == NULL || extra_args == NULL)
223 socket_id = (uint8_t)strtol(value, &endptr, 10);
224 if (*endptr != 0 || errno != 0)
227 /* validate socket id value */
228 if (socket_id >= 0) {
229 *(uint8_t *)extra_args = (uint8_t)socket_id;
236 bond_ethdev_parse_primary_slave_port_id_kvarg(const char *key __rte_unused,
237 const char *value, void *extra_args)
239 int primary_slave_port_id;
241 if (value == NULL || extra_args == NULL)
244 primary_slave_port_id = parse_port_id(value);
245 if (primary_slave_port_id < 0)
248 *(uint16_t *)extra_args = (uint16_t)primary_slave_port_id;
254 bond_ethdev_parse_balance_xmit_policy_kvarg(const char *key __rte_unused,
255 const char *value, void *extra_args)
257 uint8_t *xmit_policy;
259 if (value == NULL || extra_args == NULL)
262 xmit_policy = extra_args;
264 if (strcmp(PMD_BOND_XMIT_POLICY_LAYER2_KVARG, value) == 0)
265 *xmit_policy = BALANCE_XMIT_POLICY_LAYER2;
266 else if (strcmp(PMD_BOND_XMIT_POLICY_LAYER23_KVARG, value) == 0)
267 *xmit_policy = BALANCE_XMIT_POLICY_LAYER23;
268 else if (strcmp(PMD_BOND_XMIT_POLICY_LAYER34_KVARG, value) == 0)
269 *xmit_policy = BALANCE_XMIT_POLICY_LAYER34;
277 bond_ethdev_parse_bond_mac_addr_kvarg(const char *key __rte_unused,
278 const char *value, void *extra_args)
280 if (value == NULL || extra_args == NULL)
284 return cmdline_parse_etheraddr(NULL, value, extra_args,
285 sizeof(struct ether_addr));
289 bond_ethdev_parse_time_ms_kvarg(const char *key __rte_unused,
290 const char *value, void *extra_args)
295 if (value == NULL || extra_args == NULL)
299 time_ms = (uint32_t)strtol(value, &endptr, 10);
300 if (*endptr != 0 || errno != 0)
303 *(uint32_t *)extra_args = time_ms;