2958207a6976453de357b31026428248d5595d1b
[dpdk.git] / drivers / net / failsafe / failsafe_ether.c
1 /*-
2  *   BSD LICENSE
3  *
4  *   Copyright 2017 6WIND S.A.
5  *   Copyright 2017 Mellanox.
6  *
7  *   Redistribution and use in source and binary forms, with or without
8  *   modification, are permitted provided that the following conditions
9  *   are met:
10  *
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
16  *       distribution.
17  *     * Neither the name of 6WIND S.A. 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.
20  *
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.
32  */
33
34 #include <unistd.h>
35
36 #include <rte_flow.h>
37 #include <rte_flow_driver.h>
38
39 #include "failsafe_private.h"
40
41 /** Print a message out of a flow error. */
42 static int
43 fs_flow_complain(struct rte_flow_error *error)
44 {
45         static const char *const errstrlist[] = {
46                 [RTE_FLOW_ERROR_TYPE_NONE] = "no error",
47                 [RTE_FLOW_ERROR_TYPE_UNSPECIFIED] = "cause unspecified",
48                 [RTE_FLOW_ERROR_TYPE_HANDLE] = "flow rule (handle)",
49                 [RTE_FLOW_ERROR_TYPE_ATTR_GROUP] = "group field",
50                 [RTE_FLOW_ERROR_TYPE_ATTR_PRIORITY] = "priority field",
51                 [RTE_FLOW_ERROR_TYPE_ATTR_INGRESS] = "ingress field",
52                 [RTE_FLOW_ERROR_TYPE_ATTR_EGRESS] = "egress field",
53                 [RTE_FLOW_ERROR_TYPE_ATTR] = "attributes structure",
54                 [RTE_FLOW_ERROR_TYPE_ITEM_NUM] = "pattern length",
55                 [RTE_FLOW_ERROR_TYPE_ITEM] = "specific pattern item",
56                 [RTE_FLOW_ERROR_TYPE_ACTION_NUM] = "number of actions",
57                 [RTE_FLOW_ERROR_TYPE_ACTION] = "specific action",
58         };
59         const char *errstr;
60         char buf[32];
61         int err = rte_errno;
62
63         if ((unsigned int)error->type >= RTE_DIM(errstrlist) ||
64                         !errstrlist[error->type])
65                 errstr = "unknown type";
66         else
67                 errstr = errstrlist[error->type];
68         ERROR("Caught error type %d (%s): %s%s\n",
69                 error->type, errstr,
70                 error->cause ? (snprintf(buf, sizeof(buf), "cause: %p, ",
71                                 error->cause), buf) : "",
72                 error->message ? error->message : "(no stated reason)");
73         return -err;
74 }
75
76 static int
77 fs_eth_dev_conf_apply(struct rte_eth_dev *dev,
78                 struct sub_device *sdev)
79 {
80         struct rte_eth_dev *edev;
81         struct rte_vlan_filter_conf *vfc1;
82         struct rte_vlan_filter_conf *vfc2;
83         struct rte_flow *flow;
84         struct rte_flow_error ferror;
85         uint32_t i;
86         int ret;
87
88         edev = ETH(sdev);
89         /* RX queue setup */
90         for (i = 0; i < dev->data->nb_rx_queues; i++) {
91                 struct rxq *rxq;
92
93                 rxq = dev->data->rx_queues[i];
94                 ret = rte_eth_rx_queue_setup(PORT_ID(sdev), i,
95                                 rxq->info.nb_desc, rxq->socket_id,
96                                 &rxq->info.conf, rxq->info.mp);
97                 if (ret) {
98                         ERROR("rx_queue_setup failed");
99                         return ret;
100                 }
101         }
102         /* TX queue setup */
103         for (i = 0; i < dev->data->nb_tx_queues; i++) {
104                 struct txq *txq;
105
106                 txq = dev->data->tx_queues[i];
107                 ret = rte_eth_tx_queue_setup(PORT_ID(sdev), i,
108                                 txq->info.nb_desc, txq->socket_id,
109                                 &txq->info.conf);
110                 if (ret) {
111                         ERROR("tx_queue_setup failed");
112                         return ret;
113                 }
114         }
115         /* dev_link.link_status */
116         if (dev->data->dev_link.link_status !=
117             edev->data->dev_link.link_status) {
118                 DEBUG("Configuring link_status");
119                 if (dev->data->dev_link.link_status)
120                         ret = rte_eth_dev_set_link_up(PORT_ID(sdev));
121                 else
122                         ret = rte_eth_dev_set_link_down(PORT_ID(sdev));
123                 if (ret) {
124                         ERROR("Failed to apply link_status");
125                         return ret;
126                 }
127         } else {
128                 DEBUG("link_status already set");
129         }
130         /* promiscuous */
131         if (dev->data->promiscuous != edev->data->promiscuous) {
132                 DEBUG("Configuring promiscuous");
133                 if (dev->data->promiscuous)
134                         rte_eth_promiscuous_enable(PORT_ID(sdev));
135                 else
136                         rte_eth_promiscuous_disable(PORT_ID(sdev));
137         } else {
138                 DEBUG("promiscuous already set");
139         }
140         /* all_multicast */
141         if (dev->data->all_multicast != edev->data->all_multicast) {
142                 DEBUG("Configuring all_multicast");
143                 if (dev->data->all_multicast)
144                         rte_eth_allmulticast_enable(PORT_ID(sdev));
145                 else
146                         rte_eth_allmulticast_disable(PORT_ID(sdev));
147         } else {
148                 DEBUG("all_multicast already set");
149         }
150         /* MTU */
151         if (dev->data->mtu != edev->data->mtu) {
152                 DEBUG("Configuring MTU");
153                 ret = rte_eth_dev_set_mtu(PORT_ID(sdev), dev->data->mtu);
154                 if (ret) {
155                         ERROR("Failed to apply MTU");
156                         return ret;
157                 }
158         } else {
159                 DEBUG("MTU already set");
160         }
161         /* default MAC */
162         DEBUG("Configuring default MAC address");
163         ret = rte_eth_dev_default_mac_addr_set(PORT_ID(sdev),
164                         &dev->data->mac_addrs[0]);
165         if (ret) {
166                 ERROR("Setting default MAC address failed");
167                 return ret;
168         }
169         /* additional MAC */
170         if (PRIV(dev)->nb_mac_addr > 1)
171                 DEBUG("Configure additional MAC address%s",
172                         (PRIV(dev)->nb_mac_addr > 2 ? "es" : ""));
173         for (i = 1; i < PRIV(dev)->nb_mac_addr; i++) {
174                 struct ether_addr *ea;
175
176                 ea = &dev->data->mac_addrs[i];
177                 ret = rte_eth_dev_mac_addr_add(PORT_ID(sdev), ea,
178                                 PRIV(dev)->mac_addr_pool[i]);
179                 if (ret) {
180                         char ea_fmt[ETHER_ADDR_FMT_SIZE];
181
182                         ether_format_addr(ea_fmt, ETHER_ADDR_FMT_SIZE, ea);
183                         ERROR("Adding MAC address %s failed", ea_fmt);
184                 }
185         }
186         /* VLAN filter */
187         vfc1 = &dev->data->vlan_filter_conf;
188         vfc2 = &edev->data->vlan_filter_conf;
189         if (memcmp(vfc1, vfc2, sizeof(struct rte_vlan_filter_conf))) {
190                 uint64_t vbit;
191                 uint64_t ids;
192                 size_t i;
193                 uint16_t vlan_id;
194
195                 DEBUG("Configuring VLAN filter");
196                 for (i = 0; i < RTE_DIM(vfc1->ids); i++) {
197                         if (vfc1->ids[i] == 0)
198                                 continue;
199                         ids = vfc1->ids[i];
200                         while (ids) {
201                                 vlan_id = 64 * i;
202                                 /* count trailing zeroes */
203                                 vbit = ~ids & (ids - 1);
204                                 /* clear least significant bit set */
205                                 ids ^= (ids ^ (ids - 1)) ^ vbit;
206                                 for (; vbit; vlan_id++)
207                                         vbit >>= 1;
208                                 ret = rte_eth_dev_vlan_filter(
209                                         PORT_ID(sdev), vlan_id, 1);
210                                 if (ret) {
211                                         ERROR("Failed to apply VLAN filter %hu",
212                                                 vlan_id);
213                                         return ret;
214                                 }
215                         }
216                 }
217         } else {
218                 DEBUG("VLAN filter already set");
219         }
220         /* rte_flow */
221         if (TAILQ_EMPTY(&PRIV(dev)->flow_list)) {
222                 DEBUG("rte_flow already set");
223         } else {
224                 DEBUG("Resetting rte_flow configuration");
225                 ret = rte_flow_flush(PORT_ID(sdev), &ferror);
226                 if (ret) {
227                         fs_flow_complain(&ferror);
228                         return ret;
229                 }
230                 i = 0;
231                 rte_errno = 0;
232                 DEBUG("Configuring rte_flow");
233                 TAILQ_FOREACH(flow, &PRIV(dev)->flow_list, next) {
234                         DEBUG("Creating flow #%" PRIu32, i++);
235                         flow->flows[SUB_ID(sdev)] =
236                                 rte_flow_create(PORT_ID(sdev),
237                                                 &flow->fd->attr,
238                                                 flow->fd->items,
239                                                 flow->fd->actions,
240                                                 &ferror);
241                         ret = rte_errno;
242                         if (ret)
243                                 break;
244                 }
245                 if (ret) {
246                         fs_flow_complain(&ferror);
247                         return ret;
248                 }
249         }
250         return 0;
251 }
252
253 int
254 failsafe_eth_dev_state_sync(struct rte_eth_dev *dev)
255 {
256         struct sub_device *sdev;
257         uint32_t inactive;
258         int ret;
259         uint8_t i;
260
261         if (PRIV(dev)->state < DEV_PARSED)
262                 return 0;
263
264         ret = failsafe_args_parse_subs(dev);
265         if (ret)
266                 return ret;
267
268         if (PRIV(dev)->state < DEV_PROBED)
269                 return 0;
270         ret = failsafe_eal_init(dev);
271         if (ret)
272                 return ret;
273         if (PRIV(dev)->state < DEV_ACTIVE)
274                 return 0;
275         inactive = 0;
276         FOREACH_SUBDEV(sdev, i, dev)
277                 if (sdev->state == DEV_PROBED)
278                         inactive |= UINT32_C(1) << i;
279         ret = dev->dev_ops->dev_configure(dev);
280         if (ret)
281                 return ret;
282         FOREACH_SUBDEV(sdev, i, dev) {
283                 if (inactive & (UINT32_C(1) << i)) {
284                         ret = fs_eth_dev_conf_apply(dev, sdev);
285                         if (ret) {
286                                 ERROR("Could not apply configuration to sub_device %d",
287                                       i);
288                                 /* TODO: disable device */
289                                 return ret;
290                         }
291                 }
292         }
293         /*
294          * If new devices have been configured, check if
295          * the link state has changed.
296          */
297         if (inactive)
298                 dev->dev_ops->link_update(dev, 1);
299         if (PRIV(dev)->state < DEV_STARTED)
300                 return 0;
301         ret = dev->dev_ops->dev_start(dev);
302         if (ret)
303                 return ret;
304         return 0;
305 }