1 /* SPDX-License-Identifier: BSD-3-Clause
2 * Copyright(c) 2018 Gaƫtan Rivet
8 #include <rte_compat.h>
10 #include <rte_kvargs.h>
13 #include "rte_ethdev.h"
14 #include "rte_ethdev_core.h"
15 #include "ethdev_driver.h"
16 #include "ethdev_private.h"
20 RTE_ETH_PARAM_REPRESENTOR,
24 static const char * const eth_params_keys[] = {
25 [RTE_ETH_PARAM_MAC] = "mac",
26 [RTE_ETH_PARAM_REPRESENTOR] = "representor",
27 [RTE_ETH_PARAM_MAX] = NULL,
30 struct eth_dev_match_arg {
31 struct rte_device *device;
32 struct rte_kvargs *kvlist;
35 #define eth_dev_match_arg(d, k) \
36 (&(const struct eth_dev_match_arg) { \
42 eth_mac_cmp(const char *key __rte_unused,
43 const char *value, void *opaque)
45 struct rte_ether_addr mac;
46 const struct rte_eth_dev_data *data = opaque;
47 struct rte_eth_dev_info dev_info;
50 /* Parse devargs MAC address. */
51 if (rte_ether_unformat_addr(value, &mac) < 0)
52 return -1; /* invalid devargs value */
54 /* Return 0 if devargs MAC is matching one of the device MACs. */
55 rte_eth_dev_info_get(data->port_id, &dev_info);
56 for (index = 0; index < dev_info.max_mac_addrs; index++)
57 if (rte_is_same_ether_addr(&mac, &data->mac_addrs[index]))
59 return -1; /* no match */
63 eth_representor_cmp(const char *key __rte_unused,
64 const char *value, void *opaque)
68 const struct rte_eth_dev *edev = opaque;
69 const struct rte_eth_dev_data *data = edev->data;
70 struct rte_eth_devargs eth_da;
71 uint16_t id, nc, np, nf, i, c, p, f;
73 if ((data->dev_flags & RTE_ETH_DEV_REPRESENTOR) == 0)
74 return -1; /* not a representor port */
76 /* Parse devargs representor values. */
77 values = strdup(value);
80 memset(ð_da, 0, sizeof(eth_da));
81 ret = rte_eth_devargs_parse_representor_ports(values, ð_da);
84 return -1; /* invalid devargs value */
86 if (eth_da.nb_mh_controllers == 0 && eth_da.nb_ports == 0 &&
87 eth_da.nb_representor_ports == 0)
89 nc = eth_da.nb_mh_controllers > 0 ? eth_da.nb_mh_controllers : 1;
90 np = eth_da.nb_ports > 0 ? eth_da.nb_ports : 1;
91 nf = eth_da.nb_representor_ports > 0 ? eth_da.nb_representor_ports : 1;
93 /* Return 0 if representor id is matching one of the values. */
94 for (i = 0; i < nc * np * nf; ++i) {
98 if (rte_eth_representor_id_get(edev,
100 eth_da.nb_mh_controllers == 0 ? -1 :
101 eth_da.mh_controllers[c],
102 eth_da.nb_ports == 0 ? -1 : eth_da.ports[p],
103 eth_da.nb_representor_ports == 0 ? -1 :
104 eth_da.representor_ports[f],
107 if (data->representor_id == id)
110 return -1; /* no match */
114 eth_dev_match(const struct rte_eth_dev *edev,
118 const struct eth_dev_match_arg *arg = _arg;
119 const struct rte_kvargs *kvlist = arg->kvlist;
122 if (edev->state == RTE_ETH_DEV_UNUSED)
124 if (arg->device != NULL && arg->device != edev->device)
127 ret = rte_kvargs_process(kvlist,
128 eth_params_keys[RTE_ETH_PARAM_MAC],
129 eth_mac_cmp, edev->data);
133 ret = rte_kvargs_process(kvlist,
134 eth_params_keys[RTE_ETH_PARAM_REPRESENTOR],
135 eth_representor_cmp, (void *)(uintptr_t)edev);
138 /* search for representor key */
139 for (pair = 0; pair < kvlist->count; pair++) {
140 ret = strcmp(kvlist->pairs[pair].key,
141 eth_params_keys[RTE_ETH_PARAM_REPRESENTOR]);
143 break; /* there is a representor key */
145 /* if no representor key, default is to not match representor ports */
147 if ((edev->data->dev_flags & RTE_ETH_DEV_REPRESENTOR) != 0)
148 return -1; /* do not match any representor */
154 eth_dev_iterate(const void *start,
156 const struct rte_dev_iterator *it)
158 struct rte_kvargs *kvargs = NULL;
159 struct rte_eth_dev *edev = NULL;
160 const char * const *valid_keys = NULL;
163 if (str[0] == '+') /* no validation of keys */
166 valid_keys = eth_params_keys;
167 kvargs = rte_kvargs_parse(str, valid_keys);
168 if (kvargs == NULL) {
169 RTE_LOG(ERR, EAL, "cannot parse argument list\n");
174 edev = eth_find_device(start, eth_dev_match,
175 eth_dev_match_arg(it->device, kvargs));
176 rte_kvargs_free(kvargs);
180 static struct rte_class rte_class_eth = {
181 .dev_iterate = eth_dev_iterate,
184 RTE_REGISTER_CLASS(eth, rte_class_eth);