net/failsafe: register slaves Rx interrupts
[dpdk.git] / drivers / net / failsafe / failsafe_private.h
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 #ifndef _RTE_ETH_FAILSAFE_PRIVATE_H_
35 #define _RTE_ETH_FAILSAFE_PRIVATE_H_
36
37 #include <sys/queue.h>
38
39 #include <rte_atomic.h>
40 #include <rte_dev.h>
41 #include <rte_ethdev_driver.h>
42 #include <rte_devargs.h>
43 #include <rte_interrupts.h>
44
45 #define FAILSAFE_DRIVER_NAME "Fail-safe PMD"
46
47 #define PMD_FAILSAFE_MAC_KVARG "mac"
48 #define PMD_FAILSAFE_HOTPLUG_POLL_KVARG "hotplug_poll"
49 #define PMD_FAILSAFE_PARAM_STRING       \
50         "dev(<ifc>),"                   \
51         "exec(<shell command>),"        \
52         "fd(<fd number>),"              \
53         "mac=mac_addr,"                 \
54         "hotplug_poll=u64"              \
55         ""
56
57 #define FAILSAFE_HOTPLUG_DEFAULT_TIMEOUT_MS 2000
58
59 #define FAILSAFE_MAX_ETHPORTS 2
60 #define FAILSAFE_MAX_ETHADDR 128
61
62 #define DEVARGS_MAXLEN 4096
63
64 /* TYPES */
65
66 struct rx_proxy {
67         /* epoll file descriptor */
68         int efd;
69         /* event vector to be used by epoll */
70         struct rte_epoll_event *evec;
71 };
72
73 struct rxq {
74         struct fs_priv *priv;
75         uint16_t qid;
76         /* next sub_device to poll */
77         struct sub_device *sdev;
78         unsigned int socket_id;
79         int event_fd;
80         unsigned int enable_events:1;
81         struct rte_eth_rxq_info info;
82         rte_atomic64_t refcnt[];
83 };
84
85 struct txq {
86         struct fs_priv *priv;
87         uint16_t qid;
88         unsigned int socket_id;
89         struct rte_eth_txq_info info;
90         rte_atomic64_t refcnt[];
91 };
92
93 struct rte_flow {
94         TAILQ_ENTRY(rte_flow) next;
95         /* sub_flows */
96         struct rte_flow *flows[FAILSAFE_MAX_ETHPORTS];
97         /* flow description for synchronization */
98         struct rte_flow_desc *fd;
99 };
100
101 enum dev_state {
102         DEV_UNDEFINED,
103         DEV_PARSED,
104         DEV_PROBED,
105         DEV_ACTIVE,
106         DEV_STARTED,
107 };
108
109 struct fs_stats {
110         struct rte_eth_stats stats;
111         uint64_t timestamp;
112 };
113
114 struct sub_device {
115         /* Exhaustive DPDK device description */
116         struct sub_device *next;
117         struct rte_devargs devargs;
118         struct rte_bus *bus;
119         struct rte_device *dev;
120         struct rte_eth_dev *edev;
121         uint8_t sid;
122         /* Device state machine */
123         enum dev_state state;
124         /* Last stats snapshot passed to user */
125         struct fs_stats stats_snapshot;
126         /* Some device are defined as a command line */
127         char *cmdline;
128         /* Others are retrieved through a file descriptor */
129         char *fd_str;
130         /* fail-safe device backreference */
131         struct rte_eth_dev *fs_dev;
132         /* flag calling for recollection */
133         volatile unsigned int remove:1;
134         /* flow isolation state */
135         int flow_isolated:1;
136 };
137
138 struct fs_priv {
139         struct rte_eth_dev *dev;
140         /*
141          * Set of sub_devices.
142          * subs[0] is the preferred device
143          * any other is just another slave
144          */
145         struct sub_device *subs;
146         uint8_t subs_head; /* if head == tail, no subs */
147         uint8_t subs_tail; /* first invalid */
148         uint8_t subs_tx; /* current emitting device */
149         uint8_t current_probed;
150         /* flow mapping */
151         TAILQ_HEAD(sub_flows, rte_flow) flow_list;
152         /* current number of mac_addr slots allocated. */
153         uint32_t nb_mac_addr;
154         struct ether_addr mac_addrs[FAILSAFE_MAX_ETHADDR];
155         uint32_t mac_addr_pool[FAILSAFE_MAX_ETHADDR];
156         /* current capabilities */
157         struct rte_eth_dev_info infos;
158         struct rte_intr_handle intr_handle; /* Port interrupt handle. */
159         /*
160          * Fail-safe state machine.
161          * This level will be tracking state of the EAL and eth
162          * layer at large as defined by the user application.
163          * It will then steer the sub_devices toward the same
164          * synchronized state.
165          */
166         enum dev_state state;
167         struct rte_eth_stats stats_accumulator;
168         /*
169          * Rx interrupts/events proxy.
170          * The PMD issues Rx events to the EAL on behalf of its subdevices,
171          * it does that by registering an event-fd for each of its queues with
172          * the EAL.
173          */
174         struct rx_proxy rxp;
175         unsigned int pending_alarm:1; /* An alarm is pending */
176         /* flow isolation state */
177         int flow_isolated:1;
178 };
179
180 /* FAILSAFE_INTR */
181
182 int failsafe_rx_intr_install(struct rte_eth_dev *dev);
183 void failsafe_rx_intr_uninstall(struct rte_eth_dev *dev);
184 int failsafe_rx_intr_install_subdevice(struct sub_device *sdev);
185 void failsafe_rx_intr_uninstall_subdevice(struct sub_device *sdev);
186
187 /* MISC */
188
189 int failsafe_hotplug_alarm_install(struct rte_eth_dev *dev);
190 int failsafe_hotplug_alarm_cancel(struct rte_eth_dev *dev);
191
192 /* RX / TX */
193
194 void set_burst_fn(struct rte_eth_dev *dev, int force_safe);
195
196 uint16_t failsafe_rx_burst(void *rxq,
197                 struct rte_mbuf **rx_pkts, uint16_t nb_pkts);
198 uint16_t failsafe_tx_burst(void *txq,
199                 struct rte_mbuf **tx_pkts, uint16_t nb_pkts);
200
201 uint16_t failsafe_rx_burst_fast(void *rxq,
202                 struct rte_mbuf **rx_pkts, uint16_t nb_pkts);
203 uint16_t failsafe_tx_burst_fast(void *txq,
204                 struct rte_mbuf **tx_pkts, uint16_t nb_pkts);
205
206 /* ARGS */
207
208 int failsafe_args_parse(struct rte_eth_dev *dev, const char *params);
209 void failsafe_args_free(struct rte_eth_dev *dev);
210 int failsafe_args_count_subdevice(struct rte_eth_dev *dev, const char *params);
211 int failsafe_args_parse_subs(struct rte_eth_dev *dev);
212
213 /* EAL */
214
215 int failsafe_eal_init(struct rte_eth_dev *dev);
216 int failsafe_eal_uninit(struct rte_eth_dev *dev);
217
218 /* ETH_DEV */
219
220 int failsafe_eth_dev_state_sync(struct rte_eth_dev *dev);
221 void failsafe_dev_remove(struct rte_eth_dev *dev);
222 void failsafe_stats_increment(struct rte_eth_stats *to,
223                                 struct rte_eth_stats *from);
224 int failsafe_eth_rmv_event_callback(uint16_t port_id,
225                                     enum rte_eth_event_type type,
226                                     void *arg, void *out);
227 int failsafe_eth_lsc_event_callback(uint16_t port_id,
228                                     enum rte_eth_event_type event,
229                                     void *cb_arg, void *out);
230
231 /* GLOBALS */
232
233 extern const char pmd_failsafe_driver_name[];
234 extern const struct eth_dev_ops failsafe_ops;
235 extern const struct rte_flow_ops fs_flow_ops;
236 extern uint64_t hotplug_poll;
237 extern int mac_from_arg;
238
239 /* HELPERS */
240
241 /* dev: (struct rte_eth_dev *) fail-safe device */
242 #define PRIV(dev) \
243         ((struct fs_priv *)(dev)->data->dev_private)
244
245 /* sdev: (struct sub_device *) */
246 #define ETH(sdev) \
247         ((sdev)->edev)
248
249 /* sdev: (struct sub_device *) */
250 #define PORT_ID(sdev) \
251         (ETH(sdev)->data->port_id)
252
253 /* sdev: (struct sub_device *) */
254 #define SUB_ID(sdev) \
255         ((sdev)->sid)
256
257 /**
258  * Stateful iterator construct over fail-safe sub-devices:
259  * s:     (struct sub_device *), iterator
260  * i:     (uint8_t), increment
261  * dev:   (struct rte_eth_dev *), fail-safe ethdev
262  * state: (enum dev_state), minimum acceptable device state
263  */
264 #define FOREACH_SUBDEV_STATE(s, i, dev, state)          \
265         for (s = fs_find_next((dev), 0, state, &i);     \
266              s != NULL;                                 \
267              s = fs_find_next((dev), i + 1, state, &i))
268
269 /**
270  * Iterator construct over fail-safe sub-devices:
271  * s:   (struct sub_device *), iterator
272  * i:   (uint8_t), increment
273  * dev: (struct rte_eth_dev *), fail-safe ethdev
274  */
275 #define FOREACH_SUBDEV(s, i, dev)                       \
276         FOREACH_SUBDEV_STATE(s, i, dev, DEV_UNDEFINED)
277
278 /* dev: (struct rte_eth_dev *) fail-safe device */
279 #define PREFERRED_SUBDEV(dev) \
280         (&PRIV(dev)->subs[0])
281
282 /* dev: (struct rte_eth_dev *) fail-safe device */
283 #define TX_SUBDEV(dev)                                                    \
284         (PRIV(dev)->subs_tx >= PRIV(dev)->subs_tail                ? NULL \
285          : (PRIV(dev)->subs[PRIV(dev)->subs_tx].state < DEV_PROBED ? NULL \
286          : &PRIV(dev)->subs[PRIV(dev)->subs_tx]))
287
288 /**
289  * s:   (struct sub_device *)
290  * ops: (struct eth_dev_ops) member
291  */
292 #define SUBOPS(s, ops) \
293         (ETH(s)->dev_ops->ops)
294
295 /**
296  * Atomic guard
297  */
298
299 /**
300  * a: (rte_atomic64_t)
301  */
302 #define FS_ATOMIC_P(a) \
303         rte_atomic64_set(&(a), 1)
304
305 /**
306  * a: (rte_atomic64_t)
307  */
308 #define FS_ATOMIC_V(a) \
309         rte_atomic64_set(&(a), 0)
310
311 /**
312  * s: (struct sub_device *)
313  * i: uint16_t qid
314  */
315 #define FS_ATOMIC_RX(s, i) \
316         rte_atomic64_read( \
317          &((struct rxq *)((s)->fs_dev->data->rx_queues[i]))->refcnt[(s)->sid] \
318         )
319 /**
320  * s: (struct sub_device *)
321  * i: uint16_t qid
322  */
323 #define FS_ATOMIC_TX(s, i) \
324         rte_atomic64_read( \
325          &((struct txq *)((s)->fs_dev->data->tx_queues[i]))->refcnt[(s)->sid] \
326         )
327
328 #define LOG__(level, m, ...) \
329         RTE_LOG(level, PMD, "net_failsafe: " m "%c", __VA_ARGS__)
330 #define LOG_(level, ...) LOG__(level, __VA_ARGS__, '\n')
331 #define DEBUG(...) LOG_(DEBUG, __VA_ARGS__)
332 #define INFO(...) LOG_(INFO, __VA_ARGS__)
333 #define WARN(...) LOG_(WARNING, __VA_ARGS__)
334 #define ERROR(...) LOG_(ERR, __VA_ARGS__)
335
336 /* inlined functions */
337
338 static inline struct sub_device *
339 fs_find_next(struct rte_eth_dev *dev,
340              uint8_t sid,
341              enum dev_state min_state,
342              uint8_t *sid_out)
343 {
344         struct sub_device *subs;
345         uint8_t tail;
346
347         subs = PRIV(dev)->subs;
348         tail = PRIV(dev)->subs_tail;
349         while (sid < tail) {
350                 if (subs[sid].state >= min_state)
351                         break;
352                 sid++;
353         }
354         *sid_out = sid;
355         if (sid >= tail)
356                 return NULL;
357         return &subs[sid];
358 }
359
360 /*
361  * Switch emitting device.
362  * If banned is set, banned must not be considered for
363  * the role of emitting device.
364  */
365 static inline void
366 fs_switch_dev(struct rte_eth_dev *dev,
367               struct sub_device *banned)
368 {
369         struct sub_device *txd;
370         enum dev_state req_state;
371
372         req_state = PRIV(dev)->state;
373         txd = TX_SUBDEV(dev);
374         if (PREFERRED_SUBDEV(dev)->state >= req_state &&
375             PREFERRED_SUBDEV(dev) != banned) {
376                 if (txd != PREFERRED_SUBDEV(dev) &&
377                     (txd == NULL ||
378                      (req_state == DEV_STARTED) ||
379                      (txd && txd->state < DEV_STARTED))) {
380                         DEBUG("Switching tx_dev to preferred sub_device");
381                         PRIV(dev)->subs_tx = 0;
382                 }
383         } else if ((txd && txd->state < req_state) ||
384                    txd == NULL ||
385                    txd == banned) {
386                 struct sub_device *sdev = NULL;
387                 uint8_t i;
388
389                 /* Using acceptable device */
390                 FOREACH_SUBDEV_STATE(sdev, i, dev, req_state) {
391                         if (sdev == banned)
392                                 continue;
393                         DEBUG("Switching tx_dev to sub_device %d",
394                               i);
395                         PRIV(dev)->subs_tx = i;
396                         break;
397                 }
398                 if (i >= PRIV(dev)->subs_tail || sdev == NULL) {
399                         DEBUG("No device ready, deactivating tx_dev");
400                         PRIV(dev)->subs_tx = PRIV(dev)->subs_tail;
401                 }
402         } else {
403                 return;
404         }
405         set_burst_fn(dev, 0);
406         rte_wmb();
407 }
408
409 /*
410  * Adjust error value and rte_errno to the fail-safe actual error value.
411  */
412 static inline int
413 fs_err(struct sub_device *sdev, int err)
414 {
415         /* A device removal shouldn't be reported as an error. */
416         if (sdev->remove == 1 || err == -EIO)
417                 return rte_errno = 0;
418         return err;
419 }
420 #endif /* _RTE_ETH_FAILSAFE_PRIVATE_H_ */