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 "rte_eth_bond.h"
11 #include "eth_bond_private.h"
13 const char *pmd_bond_init_valid_arguments[] = {
14 PMD_BOND_SLAVE_PORT_KVARG,
15 PMD_BOND_PRIMARY_SLAVE_KVARG,
17 PMD_BOND_XMIT_POLICY_KVARG,
18 PMD_BOND_SOCKET_ID_KVARG,
19 PMD_BOND_MAC_ADDR_KVARG,
20 PMD_BOND_AGG_MODE_KVARG,
26 find_port_id_by_pci_addr(const struct rte_pci_addr *pci_addr)
28 struct rte_pci_device *pci_dev;
29 struct rte_pci_addr *eth_pci_addr;
32 RTE_ETH_FOREACH_DEV(i) {
33 pci_dev = RTE_ETH_DEV_TO_PCI(&rte_eth_devices[i]);
34 eth_pci_addr = &pci_dev->addr;
36 if (pci_addr->bus == eth_pci_addr->bus &&
37 pci_addr->devid == eth_pci_addr->devid &&
38 pci_addr->domain == eth_pci_addr->domain &&
39 pci_addr->function == eth_pci_addr->function)
46 find_port_id_by_dev_name(const char *name)
50 RTE_ETH_FOREACH_DEV(i) {
51 if (rte_eth_devices[i].data == NULL)
54 if (strcmp(rte_eth_devices[i].device->name, name) == 0)
61 bond_pci_addr_cmp(const struct rte_device *dev, const void *_pci_addr)
63 const struct rte_pci_device *pdev = RTE_DEV_TO_PCI_CONST(dev);
64 const struct rte_pci_addr *paddr = _pci_addr;
66 return rte_pci_addr_cmp(&pdev->addr, paddr);
70 * Parses a port identifier string to a port id by pci address, then by name,
71 * and finally port id.
74 parse_port_id(const char *port_str)
76 struct rte_pci_addr dev_addr;
77 struct rte_bus *pci_bus;
78 struct rte_device *dev;
81 pci_bus = rte_bus_find_by_name("pci");
82 if (pci_bus == NULL) {
83 RTE_BOND_LOG(ERR, "unable to find PCI bus\n");
87 /* try parsing as pci address, physical devices */
88 if (pci_bus->parse(port_str, &dev_addr) == 0) {
89 dev = pci_bus->find_device(NULL, bond_pci_addr_cmp, &dev_addr);
91 RTE_BOND_LOG(ERR, "unable to find PCI device");
94 port_id = find_port_id_by_pci_addr(&dev_addr);
98 /* try parsing as device name, virtual devices */
99 port_id = find_port_id_by_dev_name(port_str);
104 /* try parsing as port id */
105 port_id = strtol(port_str, &end, 10);
106 if (*end != 0 || errno != 0)
111 if (port_id < 0 || port_id > RTE_MAX_ETHPORTS) {
112 RTE_BOND_LOG(ERR, "Slave port specified (%s) outside expected range",
120 bond_ethdev_parse_slave_port_kvarg(const char *key,
121 const char *value, void *extra_args)
123 struct bond_ethdev_slave_ports *slave_ports;
125 if (value == NULL || extra_args == NULL)
128 slave_ports = extra_args;
130 if (strcmp(key, PMD_BOND_SLAVE_PORT_KVARG) == 0) {
131 int port_id = parse_port_id(value);
133 RTE_BOND_LOG(ERR, "Invalid slave port value (%s) specified",
137 slave_ports->slaves[slave_ports->slave_count++] =
144 bond_ethdev_parse_slave_mode_kvarg(const char *key __rte_unused,
145 const char *value, void *extra_args)
150 if (value == NULL || extra_args == NULL)
156 *mode = strtol(value, &endptr, 10);
157 if (*endptr != 0 || errno != 0)
160 /* validate mode value */
162 case BONDING_MODE_ROUND_ROBIN:
163 case BONDING_MODE_ACTIVE_BACKUP:
164 case BONDING_MODE_BALANCE:
165 case BONDING_MODE_BROADCAST:
166 case BONDING_MODE_8023AD:
167 case BONDING_MODE_TLB:
168 case BONDING_MODE_ALB:
171 RTE_BOND_LOG(ERR, "Invalid slave mode value (%s) specified", value);
177 bond_ethdev_parse_slave_agg_mode_kvarg(const char *key __rte_unused,
178 const char *value, void *extra_args)
182 if (value == NULL || extra_args == NULL)
185 agg_mode = extra_args;
188 if (strncmp(value, "stable", 6) == 0)
189 *agg_mode = AGG_STABLE;
191 if (strncmp(value, "bandwidth", 9) == 0)
192 *agg_mode = AGG_BANDWIDTH;
194 if (strncmp(value, "count", 5) == 0)
195 *agg_mode = AGG_COUNT;
203 RTE_BOND_LOG(ERR, "Invalid agg mode value stable/bandwidth/count");
209 bond_ethdev_parse_socket_id_kvarg(const char *key __rte_unused,
210 const char *value, void *extra_args)
215 if (value == NULL || extra_args == NULL)
219 socket_id = (uint8_t)strtol(value, &endptr, 10);
220 if (*endptr != 0 || errno != 0)
223 /* validate socket id value */
224 if (socket_id >= 0) {
225 *(uint8_t *)extra_args = (uint8_t)socket_id;
232 bond_ethdev_parse_primary_slave_port_id_kvarg(const char *key __rte_unused,
233 const char *value, void *extra_args)
235 int primary_slave_port_id;
237 if (value == NULL || extra_args == NULL)
240 primary_slave_port_id = parse_port_id(value);
241 if (primary_slave_port_id < 0)
244 *(uint16_t *)extra_args = (uint16_t)primary_slave_port_id;
250 bond_ethdev_parse_balance_xmit_policy_kvarg(const char *key __rte_unused,
251 const char *value, void *extra_args)
253 uint8_t *xmit_policy;
255 if (value == NULL || extra_args == NULL)
258 xmit_policy = extra_args;
260 if (strcmp(PMD_BOND_XMIT_POLICY_LAYER2_KVARG, value) == 0)
261 *xmit_policy = BALANCE_XMIT_POLICY_LAYER2;
262 else if (strcmp(PMD_BOND_XMIT_POLICY_LAYER23_KVARG, value) == 0)
263 *xmit_policy = BALANCE_XMIT_POLICY_LAYER23;
264 else if (strcmp(PMD_BOND_XMIT_POLICY_LAYER34_KVARG, value) == 0)
265 *xmit_policy = BALANCE_XMIT_POLICY_LAYER34;
273 bond_ethdev_parse_bond_mac_addr_kvarg(const char *key __rte_unused,
274 const char *value, void *extra_args)
276 if (value == NULL || extra_args == NULL)
280 return rte_ether_unformat_addr(value, extra_args);
284 bond_ethdev_parse_time_ms_kvarg(const char *key __rte_unused,
285 const char *value, void *extra_args)
290 if (value == NULL || extra_args == NULL)
294 time_ms = (uint32_t)strtol(value, &endptr, 10);
295 if (*endptr != 0 || errno != 0)
298 *(uint32_t *)extra_args = time_ms;