net/failsafe: add fail-safe PMD
[dpdk.git] / drivers / net / failsafe / failsafe.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 <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>
40 #include <rte_vdev.h>
41
42 #include "failsafe_private.h"
43
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,
50 };
51
52 static int
53 fs_sub_device_alloc(struct rte_eth_dev *dev,
54                 const char *params)
55 {
56         uint8_t nb_subs;
57         int ret;
58
59         ret = failsafe_args_count_subdevice(dev, params);
60         if (ret)
61                 return ret;
62         if (PRIV(dev)->subs_tail > FAILSAFE_MAX_ETHPORTS) {
63                 ERROR("Cannot allocate more than %d ports",
64                         FAILSAFE_MAX_ETHPORTS);
65                 return -ENOSPC;
66         }
67         nb_subs = PRIV(dev)->subs_tail;
68         PRIV(dev)->subs = rte_zmalloc(NULL,
69                         sizeof(struct sub_device) * nb_subs,
70                         RTE_CACHE_LINE_SIZE);
71         if (PRIV(dev)->subs == NULL) {
72                 ERROR("Could not allocate sub_devices");
73                 return -ENOMEM;
74         }
75         return 0;
76 }
77
78 static void
79 fs_sub_device_free(struct rte_eth_dev *dev)
80 {
81         rte_free(PRIV(dev)->subs);
82 }
83
84 static int
85 fs_eth_dev_create(struct rte_vdev_device *vdev)
86 {
87         struct rte_eth_dev *dev;
88         struct ether_addr *mac;
89         struct fs_priv *priv;
90         struct sub_device *sdev;
91         const char *params;
92         unsigned int socket_id;
93         uint8_t i;
94         int ret;
95
96         dev = NULL;
97         priv = NULL;
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");
103                 return -1;
104         }
105         dev = rte_eth_vdev_allocate(vdev, sizeof(*priv));
106         if (dev == NULL) {
107                 ERROR("Unable to allocate rte_eth_dev");
108                 return -1;
109         }
110         priv = PRIV(dev);
111         priv->dev = 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);
119         if (ret) {
120                 ERROR("Could not allocate sub_devices");
121                 goto free_dev;
122         }
123         ret = failsafe_args_parse(dev, params);
124         if (ret)
125                 goto free_subs;
126         ret = failsafe_eal_init(dev);
127         if (ret)
128                 goto free_args;
129         mac = &dev->data->mac_addrs[0];
130         if (mac_from_arg) {
131                 /*
132                  * If MAC address was provided as a parameter,
133                  * apply to all probed slaves.
134                  */
135                 FOREACH_SUBDEV_STATE(sdev, i, dev, DEV_PROBED) {
136                         ret = rte_eth_dev_default_mac_addr_set(PORT_ID(sdev),
137                                                                mac);
138                         if (ret) {
139                                 ERROR("Failed to set default MAC address");
140                                 goto free_args;
141                         }
142                 }
143         } else {
144                 /*
145                  * Use the ether_addr from first probed
146                  * device, either preferred or fallback.
147                  */
148                 FOREACH_SUBDEV(sdev, i, dev)
149                         if (sdev->state >= DEV_PROBED) {
150                                 ether_addr_copy(&ETH(sdev)->data->mac_addrs[0],
151                                                 mac);
152                                 break;
153                         }
154                 /*
155                  * If no device has been probed and no ether_addr
156                  * has been provided on the command line, use a random
157                  * valid one.
158                  * It will be applied during future slave state syncs to
159                  * probed slaves.
160                  */
161                 if (i == priv->subs_tail)
162                         eth_random_addr(&mac->addr_bytes[0]);
163         }
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]);
168         return 0;
169 free_args:
170         failsafe_args_free(dev);
171 free_subs:
172         fs_sub_device_free(dev);
173 free_dev:
174         rte_free(PRIV(dev));
175         rte_eth_dev_release_port(dev);
176         return -1;
177 }
178
179 static int
180 fs_rte_eth_free(const char *name)
181 {
182         struct rte_eth_dev *dev;
183         int ret;
184
185         dev = rte_eth_dev_allocated(name);
186         if (dev == NULL)
187                 return -ENODEV;
188         ret = failsafe_eal_uninit(dev);
189         if (ret)
190                 ERROR("Error while uninitializing sub-EAL");
191         failsafe_args_free(dev);
192         fs_sub_device_free(dev);
193         rte_free(PRIV(dev));
194         rte_eth_dev_release_port(dev);
195         return ret;
196 }
197
198 static int
199 rte_pmd_failsafe_probe(struct rte_vdev_device *vdev)
200 {
201         const char *name;
202
203         name = rte_vdev_device_name(vdev);
204         INFO("Initializing " FAILSAFE_DRIVER_NAME " for %s",
205                         name);
206         return fs_eth_dev_create(vdev);
207 }
208
209 static int
210 rte_pmd_failsafe_remove(struct rte_vdev_device *vdev)
211 {
212         const char *name;
213
214         name = rte_vdev_device_name(vdev);
215         INFO("Uninitializing " FAILSAFE_DRIVER_NAME " for %s", name);
216         return fs_rte_eth_free(name);
217 }
218
219 static struct rte_vdev_driver failsafe_drv = {
220         .probe = rte_pmd_failsafe_probe,
221         .remove = rte_pmd_failsafe_remove,
222 };
223
224 RTE_PMD_REGISTER_VDEV(net_failsafe, failsafe_drv);
225 RTE_PMD_REGISTER_PARAM_STRING(net_failsafe, PMD_FAILSAFE_PARAM_STRING);