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