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);
85 fs_eth_dev_create(struct rte_vdev_device *vdev)
87 struct rte_eth_dev *dev;
88 struct ether_addr *mac;
90 struct sub_device *sdev;
92 unsigned int socket_id;
98 socket_id = rte_socket_id();
99 INFO("Creating fail-safe device on NUMA socket %u", socket_id);
100 params = rte_vdev_device_args(vdev);
101 if (params == NULL) {
102 ERROR("This PMD requires sub-devices, none provided");
105 dev = rte_eth_vdev_allocate(vdev, sizeof(*priv));
107 ERROR("Unable to allocate rte_eth_dev");
112 dev->dev_ops = &failsafe_ops;
113 dev->data->mac_addrs = &PRIV(dev)->mac_addrs[0];
114 dev->data->dev_link = eth_link;
115 PRIV(dev)->nb_mac_addr = 1;
116 dev->rx_pkt_burst = (eth_rx_burst_t)&failsafe_rx_burst;
117 dev->tx_pkt_burst = (eth_tx_burst_t)&failsafe_tx_burst;
118 ret = fs_sub_device_alloc(dev, params);
120 ERROR("Could not allocate sub_devices");
123 ret = failsafe_args_parse(dev, params);
126 ret = failsafe_eal_init(dev);
129 mac = &dev->data->mac_addrs[0];
132 * If MAC address was provided as a parameter,
133 * apply to all probed slaves.
135 FOREACH_SUBDEV_STATE(sdev, i, dev, DEV_PROBED) {
136 ret = rte_eth_dev_default_mac_addr_set(PORT_ID(sdev),
139 ERROR("Failed to set default MAC address");
145 * Use the ether_addr from first probed
146 * device, either preferred or fallback.
148 FOREACH_SUBDEV(sdev, i, dev)
149 if (sdev->state >= DEV_PROBED) {
150 ether_addr_copy(Ð(sdev)->data->mac_addrs[0],
155 * If no device has been probed and no ether_addr
156 * has been provided on the command line, use a random
158 * It will be applied during future slave state syncs to
161 if (i == priv->subs_tail)
162 eth_random_addr(&mac->addr_bytes[0]);
164 INFO("MAC address is %02x:%02x:%02x:%02x:%02x:%02x",
165 mac->addr_bytes[0], mac->addr_bytes[1],
166 mac->addr_bytes[2], mac->addr_bytes[3],
167 mac->addr_bytes[4], mac->addr_bytes[5]);
170 failsafe_args_free(dev);
172 fs_sub_device_free(dev);
175 rte_eth_dev_release_port(dev);
180 fs_rte_eth_free(const char *name)
182 struct rte_eth_dev *dev;
185 dev = rte_eth_dev_allocated(name);
188 ret = failsafe_eal_uninit(dev);
190 ERROR("Error while uninitializing sub-EAL");
191 failsafe_args_free(dev);
192 fs_sub_device_free(dev);
194 rte_eth_dev_release_port(dev);
199 rte_pmd_failsafe_probe(struct rte_vdev_device *vdev)
203 name = rte_vdev_device_name(vdev);
204 INFO("Initializing " FAILSAFE_DRIVER_NAME " for %s",
206 return fs_eth_dev_create(vdev);
210 rte_pmd_failsafe_remove(struct rte_vdev_device *vdev)
214 name = rte_vdev_device_name(vdev);
215 INFO("Uninitializing " FAILSAFE_DRIVER_NAME " for %s", name);
216 return fs_rte_eth_free(name);
219 static struct rte_vdev_driver failsafe_drv = {
220 .probe = rte_pmd_failsafe_probe,
221 .remove = rte_pmd_failsafe_remove,
224 RTE_PMD_REGISTER_VDEV(net_failsafe, failsafe_drv);
225 RTE_PMD_REGISTER_PARAM_STRING(net_failsafe, PMD_FAILSAFE_PARAM_STRING);