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.
34 #include <rte_alarm.h>
35 #include <rte_malloc.h>
36 #include <rte_ethdev.h>
37 #include <rte_ethdev_vdev.h>
38 #include <rte_devargs.h>
39 #include <rte_kvargs.h>
42 #include "failsafe_private.h"
44 const char pmd_failsafe_driver_name[] = FAILSAFE_DRIVER_NAME;
45 static const struct rte_eth_link eth_link = {
46 .link_speed = ETH_SPEED_NUM_10G,
47 .link_duplex = ETH_LINK_FULL_DUPLEX,
48 .link_status = ETH_LINK_UP,
49 .link_autoneg = ETH_LINK_SPEED_AUTONEG,
53 fs_sub_device_alloc(struct rte_eth_dev *dev,
59 ret = failsafe_args_count_subdevice(dev, params);
62 if (PRIV(dev)->subs_tail > FAILSAFE_MAX_ETHPORTS) {
63 ERROR("Cannot allocate more than %d ports",
64 FAILSAFE_MAX_ETHPORTS);
67 nb_subs = PRIV(dev)->subs_tail;
68 PRIV(dev)->subs = rte_zmalloc(NULL,
69 sizeof(struct sub_device) * nb_subs,
71 if (PRIV(dev)->subs == NULL) {
72 ERROR("Could not allocate sub_devices");
79 fs_sub_device_free(struct rte_eth_dev *dev)
81 rte_free(PRIV(dev)->subs);
84 static void fs_hotplug_alarm(void *arg);
87 failsafe_hotplug_alarm_install(struct rte_eth_dev *dev)
93 if (PRIV(dev)->pending_alarm)
95 ret = rte_eal_alarm_set(hotplug_poll * 1000,
99 ERROR("Could not set up plug-in event detection");
102 PRIV(dev)->pending_alarm = 1;
107 failsafe_hotplug_alarm_cancel(struct rte_eth_dev *dev)
111 if (PRIV(dev)->pending_alarm) {
113 rte_eal_alarm_cancel(fs_hotplug_alarm, dev);
115 ERROR("rte_eal_alarm_cancel failed (errno: %s)",
116 strerror(rte_errno));
119 PRIV(dev)->pending_alarm = 0;
126 fs_hotplug_alarm(void *arg)
128 struct rte_eth_dev *dev = arg;
129 struct sub_device *sdev;
133 if (!PRIV(dev)->pending_alarm)
135 PRIV(dev)->pending_alarm = 0;
136 FOREACH_SUBDEV(sdev, i, dev)
137 if (sdev->state != PRIV(dev)->state)
139 /* if we have non-probed device */
140 if (i != PRIV(dev)->subs_tail) {
141 ret = failsafe_eth_dev_state_sync(dev);
143 ERROR("Unable to synchronize sub_device state");
145 failsafe_dev_remove(dev);
146 ret = failsafe_hotplug_alarm_install(dev);
148 ERROR("Unable to set up next alarm");
152 fs_eth_dev_create(struct rte_vdev_device *vdev)
154 struct rte_eth_dev *dev;
155 struct ether_addr *mac;
156 struct fs_priv *priv;
157 struct sub_device *sdev;
159 unsigned int socket_id;
165 socket_id = rte_socket_id();
166 INFO("Creating fail-safe device on NUMA socket %u", socket_id);
167 params = rte_vdev_device_args(vdev);
168 if (params == NULL) {
169 ERROR("This PMD requires sub-devices, none provided");
172 dev = rte_eth_vdev_allocate(vdev, sizeof(*priv));
174 ERROR("Unable to allocate rte_eth_dev");
179 dev->dev_ops = &failsafe_ops;
180 dev->data->mac_addrs = &PRIV(dev)->mac_addrs[0];
181 dev->data->dev_link = eth_link;
182 PRIV(dev)->nb_mac_addr = 1;
183 TAILQ_INIT(&PRIV(dev)->flow_list);
184 dev->rx_pkt_burst = (eth_rx_burst_t)&failsafe_rx_burst;
185 dev->tx_pkt_burst = (eth_tx_burst_t)&failsafe_tx_burst;
186 ret = fs_sub_device_alloc(dev, params);
188 ERROR("Could not allocate sub_devices");
191 ret = failsafe_args_parse(dev, params);
194 ret = failsafe_eal_init(dev);
197 ret = failsafe_hotplug_alarm_install(dev);
199 ERROR("Could not set up plug-in event detection");
202 mac = &dev->data->mac_addrs[0];
205 * If MAC address was provided as a parameter,
206 * apply to all probed slaves.
208 FOREACH_SUBDEV_STATE(sdev, i, dev, DEV_PROBED) {
209 ret = rte_eth_dev_default_mac_addr_set(PORT_ID(sdev),
212 ERROR("Failed to set default MAC address");
218 * Use the ether_addr from first probed
219 * device, either preferred or fallback.
221 FOREACH_SUBDEV(sdev, i, dev)
222 if (sdev->state >= DEV_PROBED) {
223 ether_addr_copy(Ð(sdev)->data->mac_addrs[0],
228 * If no device has been probed and no ether_addr
229 * has been provided on the command line, use a random
231 * It will be applied during future slave state syncs to
234 if (i == priv->subs_tail)
235 eth_random_addr(&mac->addr_bytes[0]);
237 INFO("MAC address is %02x:%02x:%02x:%02x:%02x:%02x",
238 mac->addr_bytes[0], mac->addr_bytes[1],
239 mac->addr_bytes[2], mac->addr_bytes[3],
240 mac->addr_bytes[4], mac->addr_bytes[5]);
241 dev->data->dev_flags |= RTE_ETH_DEV_INTR_LSC;
244 failsafe_args_free(dev);
246 fs_sub_device_free(dev);
249 rte_eth_dev_release_port(dev);
254 fs_rte_eth_free(const char *name)
256 struct rte_eth_dev *dev;
259 dev = rte_eth_dev_allocated(name);
262 ret = failsafe_eal_uninit(dev);
264 ERROR("Error while uninitializing sub-EAL");
265 failsafe_args_free(dev);
266 fs_sub_device_free(dev);
268 rte_eth_dev_release_port(dev);
273 rte_pmd_failsafe_probe(struct rte_vdev_device *vdev)
277 name = rte_vdev_device_name(vdev);
278 INFO("Initializing " FAILSAFE_DRIVER_NAME " for %s",
280 return fs_eth_dev_create(vdev);
284 rte_pmd_failsafe_remove(struct rte_vdev_device *vdev)
288 name = rte_vdev_device_name(vdev);
289 INFO("Uninitializing " FAILSAFE_DRIVER_NAME " for %s", name);
290 return fs_rte_eth_free(name);
293 static struct rte_vdev_driver failsafe_drv = {
294 .probe = rte_pmd_failsafe_probe,
295 .remove = rte_pmd_failsafe_remove,
298 RTE_PMD_REGISTER_VDEV(net_failsafe, failsafe_drv);
299 RTE_PMD_REGISTER_PARAM_STRING(net_failsafe, PMD_FAILSAFE_PARAM_STRING);