ethdev: support PF index in representor
[dpdk.git] / lib / librte_ethdev / rte_class_eth.c
1 /* SPDX-License-Identifier: BSD-3-Clause
2  * Copyright(c) 2018 GaĆ«tan Rivet
3  */
4
5 #include <string.h>
6
7 #include <rte_class.h>
8 #include <rte_compat.h>
9 #include <rte_errno.h>
10 #include <rte_kvargs.h>
11 #include <rte_log.h>
12
13 #include "rte_ethdev.h"
14 #include "rte_ethdev_core.h"
15 #include "ethdev_driver.h"
16 #include "ethdev_private.h"
17
18 enum eth_params {
19         RTE_ETH_PARAM_MAC,
20         RTE_ETH_PARAM_REPRESENTOR,
21         RTE_ETH_PARAM_MAX,
22 };
23
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,
28 };
29
30 struct eth_dev_match_arg {
31         struct rte_device *device;
32         struct rte_kvargs *kvlist;
33 };
34
35 #define eth_dev_match_arg(d, k) \
36         (&(const struct eth_dev_match_arg) { \
37                 .device = (d), \
38                 .kvlist = (k), \
39         })
40
41 static int
42 eth_mac_cmp(const char *key __rte_unused,
43                 const char *value, void *opaque)
44 {
45         struct rte_ether_addr mac;
46         const struct rte_eth_dev_data *data = opaque;
47         struct rte_eth_dev_info dev_info;
48         uint32_t index;
49
50         /* Parse devargs MAC address. */
51         if (rte_ether_unformat_addr(value, &mac) < 0)
52                 return -1; /* invalid devargs value */
53
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]))
58                         return 0;
59         return -1; /* no match */
60 }
61
62 static int
63 eth_representor_cmp(const char *key __rte_unused,
64                 const char *value, void *opaque)
65 {
66         int ret;
67         char *values;
68         const struct rte_eth_dev_data *data = opaque;
69         struct rte_eth_devargs representors;
70         uint16_t index;
71
72         if ((data->dev_flags & RTE_ETH_DEV_REPRESENTOR) == 0)
73                 return -1; /* not a representor port */
74
75         /* Parse devargs representor values. */
76         values = strdup(value);
77         if (values == NULL)
78                 return -1;
79         memset(&representors, 0, sizeof(representors));
80         ret = rte_eth_devargs_parse_representor_ports(values, &representors);
81         free(values);
82         if (ret != 0)
83                 return -1; /* invalid devargs value */
84
85         /* Return 0 if representor id is matching one of the values. */
86         for (index = 0; index < representors.nb_representor_ports; index++)
87                 if (data->representor_id ==
88                                 representors.representor_ports[index])
89                         return 0;
90         return -1; /* no match */
91 }
92
93 static int
94 eth_dev_match(const struct rte_eth_dev *edev,
95               const void *_arg)
96 {
97         int ret;
98         const struct eth_dev_match_arg *arg = _arg;
99         const struct rte_kvargs *kvlist = arg->kvlist;
100         unsigned int pair;
101
102         if (edev->state == RTE_ETH_DEV_UNUSED)
103                 return -1;
104         if (arg->device != NULL && arg->device != edev->device)
105                 return -1;
106
107         ret = rte_kvargs_process(kvlist,
108                         eth_params_keys[RTE_ETH_PARAM_MAC],
109                         eth_mac_cmp, edev->data);
110         if (ret != 0)
111                 return -1;
112
113         ret = rte_kvargs_process(kvlist,
114                         eth_params_keys[RTE_ETH_PARAM_REPRESENTOR],
115                         eth_representor_cmp, edev->data);
116         if (ret != 0)
117                 return -1;
118         /* search for representor key */
119         for (pair = 0; pair < kvlist->count; pair++) {
120                 ret = strcmp(kvlist->pairs[pair].key,
121                                 eth_params_keys[RTE_ETH_PARAM_REPRESENTOR]);
122                 if (ret == 0)
123                         break; /* there is a representor key */
124         }
125         /* if no representor key, default is to not match representor ports */
126         if (ret != 0)
127                 if ((edev->data->dev_flags & RTE_ETH_DEV_REPRESENTOR) != 0)
128                         return -1; /* do not match any representor */
129
130         return 0;
131 }
132
133 static void *
134 eth_dev_iterate(const void *start,
135                 const char *str,
136                 const struct rte_dev_iterator *it)
137 {
138         struct rte_kvargs *kvargs = NULL;
139         struct rte_eth_dev *edev = NULL;
140         const char * const *valid_keys = NULL;
141
142         if (str != NULL) {
143                 if (str[0] == '+') /* no validation of keys */
144                         str++;
145                 else
146                         valid_keys = eth_params_keys;
147                 kvargs = rte_kvargs_parse(str, valid_keys);
148                 if (kvargs == NULL) {
149                         RTE_LOG(ERR, EAL, "cannot parse argument list\n");
150                         rte_errno = EINVAL;
151                         return NULL;
152                 }
153         }
154         edev = eth_find_device(start, eth_dev_match,
155                                eth_dev_match_arg(it->device, kvargs));
156         rte_kvargs_free(kvargs);
157         return edev;
158 }
159
160 static struct rte_class rte_class_eth = {
161         .dev_iterate = eth_dev_iterate,
162 };
163
164 RTE_REGISTER_CLASS(eth, rte_class_eth);