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