4 * Copyright 2017 6WIND S.A.
5 * Copyright 2017 Mellanox.
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
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
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.
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.
37 #include <rte_flow_driver.h>
39 #include "failsafe_private.h"
41 /** Print a message out of a flow error. */
43 fs_flow_complain(struct rte_flow_error *error)
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",
63 if ((unsigned int)error->type >= RTE_DIM(errstrlist) ||
64 !errstrlist[error->type])
65 errstr = "unknown type";
67 errstr = errstrlist[error->type];
68 ERROR("Caught error type %d (%s): %s%s\n",
70 error->cause ? (snprintf(buf, sizeof(buf), "cause: %p, ",
71 error->cause), buf) : "",
72 error->message ? error->message : "(no stated reason)");
77 fs_eth_dev_conf_apply(struct rte_eth_dev *dev,
78 struct sub_device *sdev)
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;
90 for (i = 0; i < dev->data->nb_rx_queues; i++) {
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);
98 ERROR("rx_queue_setup failed");
103 for (i = 0; i < dev->data->nb_tx_queues; i++) {
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,
111 ERROR("tx_queue_setup failed");
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));
122 ret = rte_eth_dev_set_link_down(PORT_ID(sdev));
124 ERROR("Failed to apply link_status");
128 DEBUG("link_status already set");
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));
136 rte_eth_promiscuous_disable(PORT_ID(sdev));
138 DEBUG("promiscuous already set");
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));
146 rte_eth_allmulticast_disable(PORT_ID(sdev));
148 DEBUG("all_multicast already set");
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);
155 ERROR("Failed to apply MTU");
159 DEBUG("MTU already set");
162 DEBUG("Configuring default MAC address");
163 ret = rte_eth_dev_default_mac_addr_set(PORT_ID(sdev),
164 &dev->data->mac_addrs[0]);
166 ERROR("Setting default MAC address failed");
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;
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]);
180 char ea_fmt[ETHER_ADDR_FMT_SIZE];
182 ether_format_addr(ea_fmt, ETHER_ADDR_FMT_SIZE, ea);
183 ERROR("Adding MAC address %s failed", ea_fmt);
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))) {
195 DEBUG("Configuring VLAN filter");
196 for (i = 0; i < RTE_DIM(vfc1->ids); i++) {
197 if (vfc1->ids[i] == 0)
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++)
208 ret = rte_eth_dev_vlan_filter(
209 PORT_ID(sdev), vlan_id, 1);
211 ERROR("Failed to apply VLAN filter %hu",
218 DEBUG("VLAN filter already set");
221 if (TAILQ_EMPTY(&PRIV(dev)->flow_list)) {
222 DEBUG("rte_flow already set");
224 DEBUG("Resetting rte_flow configuration");
225 ret = rte_flow_flush(PORT_ID(sdev), &ferror);
227 fs_flow_complain(&ferror);
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),
246 fs_flow_complain(&ferror);
254 fs_dev_remove(struct sub_device *sdev)
260 switch (sdev->state) {
262 rte_eth_dev_stop(PORT_ID(sdev));
263 sdev->state = DEV_ACTIVE;
266 rte_eth_dev_close(PORT_ID(sdev));
267 sdev->state = DEV_PROBED;
270 ret = rte_eal_hotplug_remove(sdev->bus->name,
273 ERROR("Bus detach failed for sub_device %u",
276 ETH(sdev)->state = RTE_ETH_DEV_UNUSED;
278 sdev->state = DEV_PARSED;
282 sdev->state = DEV_UNDEFINED;
286 failsafe_hotplug_alarm_install(sdev->fs_dev);
290 fs_rxtx_clean(struct sub_device *sdev)
294 for (i = 0; i < ETH(sdev)->data->nb_rx_queues; i++)
295 if (FS_ATOMIC_RX(sdev, i))
297 for (i = 0; i < ETH(sdev)->data->nb_tx_queues; i++)
298 if (FS_ATOMIC_TX(sdev, i))
304 failsafe_dev_remove(struct rte_eth_dev *dev)
306 struct sub_device *sdev;
309 FOREACH_SUBDEV_STATE(sdev, i, dev, DEV_ACTIVE)
310 if (sdev->remove && fs_rxtx_clean(sdev))
315 failsafe_eth_dev_state_sync(struct rte_eth_dev *dev)
317 struct sub_device *sdev;
322 if (PRIV(dev)->state < DEV_PARSED)
325 ret = failsafe_args_parse_subs(dev);
329 if (PRIV(dev)->state < DEV_PROBED)
331 ret = failsafe_eal_init(dev);
334 if (PRIV(dev)->state < DEV_ACTIVE)
337 FOREACH_SUBDEV(sdev, i, dev)
338 if (sdev->state == DEV_PROBED)
339 inactive |= UINT32_C(1) << i;
340 ret = dev->dev_ops->dev_configure(dev);
343 FOREACH_SUBDEV(sdev, i, dev) {
344 if (inactive & (UINT32_C(1) << i)) {
345 ret = fs_eth_dev_conf_apply(dev, sdev);
347 ERROR("Could not apply configuration to sub_device %d",
354 * If new devices have been configured, check if
355 * the link state has changed.
358 dev->dev_ops->link_update(dev, 1);
359 if (PRIV(dev)->state < DEV_STARTED)
361 ret = dev->dev_ops->dev_start(dev);
366 FOREACH_SUBDEV(sdev, i, dev)
367 if (sdev->state != PRIV(dev)->state)
373 failsafe_eth_rmv_event_callback(uint8_t port_id __rte_unused,
374 enum rte_eth_event_type event __rte_unused,
375 void *cb_arg, void *out __rte_unused)
377 struct sub_device *sdev = cb_arg;
379 /* Switch as soon as possible tx_dev. */
380 fs_switch_dev(sdev->fs_dev, sdev);
381 /* Use safe bursts in any case. */
382 set_burst_fn(sdev->fs_dev, 1);
384 * Async removal, the sub-PMD will try to unregister
385 * the callback at the source of the current thread context.