0361cf434698d622348edc3e4c86a24b250ed576
[deb_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.h>
42 #include <rte_devargs.h>
43
44 #define FAILSAFE_DRIVER_NAME "Fail-safe PMD"
45
46 #define PMD_FAILSAFE_MAC_KVARG "mac"
47 #define PMD_FAILSAFE_HOTPLUG_POLL_KVARG "hotplug_poll"
48 #define PMD_FAILSAFE_PARAM_STRING       \
49         "dev(<ifc>),"                   \
50         "exec(<shell command>),"        \
51         "mac=mac_addr,"                 \
52         "hotplug_poll=u64"              \
53         ""
54
55 #define FAILSAFE_HOTPLUG_DEFAULT_TIMEOUT_MS 2000
56
57 #define FAILSAFE_MAX_ETHPORTS 2
58 #define FAILSAFE_MAX_ETHADDR 128
59
60 /* TYPES */
61
62 struct rxq {
63         struct fs_priv *priv;
64         uint16_t qid;
65         /* id of last sub_device polled */
66         uint8_t last_polled;
67         unsigned int socket_id;
68         struct rte_eth_rxq_info info;
69         rte_atomic64_t refcnt[];
70 };
71
72 struct txq {
73         struct fs_priv *priv;
74         uint16_t qid;
75         unsigned int socket_id;
76         struct rte_eth_txq_info info;
77         rte_atomic64_t refcnt[];
78 };
79
80 struct rte_flow {
81         TAILQ_ENTRY(rte_flow) next;
82         /* sub_flows */
83         struct rte_flow *flows[FAILSAFE_MAX_ETHPORTS];
84         /* flow description for synchronization */
85         struct rte_flow_desc *fd;
86 };
87
88 enum dev_state {
89         DEV_UNDEFINED,
90         DEV_PARSED,
91         DEV_PROBED,
92         DEV_ACTIVE,
93         DEV_STARTED,
94 };
95
96 struct sub_device {
97         /* Exhaustive DPDK device description */
98         struct rte_devargs devargs;
99         struct rte_bus *bus;
100         struct rte_device *dev;
101         struct rte_eth_dev *edev;
102         uint8_t sid;
103         /* Device state machine */
104         enum dev_state state;
105         /* Some device are defined as a command line */
106         char *cmdline;
107         /* fail-safe device backreference */
108         struct rte_eth_dev *fs_dev;
109         /* flag calling for recollection */
110         volatile unsigned int remove:1;
111         /* flow isolation state */
112         int flow_isolated:1;
113 };
114
115 struct fs_priv {
116         struct rte_eth_dev *dev;
117         /*
118          * Set of sub_devices.
119          * subs[0] is the preferred device
120          * any other is just another slave
121          */
122         struct sub_device *subs;
123         uint8_t subs_head; /* if head == tail, no subs */
124         uint8_t subs_tail; /* first invalid */
125         uint8_t subs_tx; /* current emitting device */
126         uint8_t current_probed;
127         /* flow mapping */
128         TAILQ_HEAD(sub_flows, rte_flow) flow_list;
129         /* current number of mac_addr slots allocated. */
130         uint32_t nb_mac_addr;
131         struct ether_addr mac_addrs[FAILSAFE_MAX_ETHADDR];
132         uint32_t mac_addr_pool[FAILSAFE_MAX_ETHADDR];
133         /* current capabilities */
134         struct rte_eth_dev_info infos;
135         /*
136          * Fail-safe state machine.
137          * This level will be tracking state of the EAL and eth
138          * layer at large as defined by the user application.
139          * It will then steer the sub_devices toward the same
140          * synchronized state.
141          */
142         enum dev_state state;
143         unsigned int pending_alarm:1; /* An alarm is pending */
144         /* flow isolation state */
145         int flow_isolated:1;
146 };
147
148 /* MISC */
149
150 int failsafe_hotplug_alarm_install(struct rte_eth_dev *dev);
151 int failsafe_hotplug_alarm_cancel(struct rte_eth_dev *dev);
152
153 /* RX / TX */
154
155 void set_burst_fn(struct rte_eth_dev *dev, int force_safe);
156
157 uint16_t failsafe_rx_burst(void *rxq,
158                 struct rte_mbuf **rx_pkts, uint16_t nb_pkts);
159 uint16_t failsafe_tx_burst(void *txq,
160                 struct rte_mbuf **tx_pkts, uint16_t nb_pkts);
161
162 uint16_t failsafe_rx_burst_fast(void *rxq,
163                 struct rte_mbuf **rx_pkts, uint16_t nb_pkts);
164 uint16_t failsafe_tx_burst_fast(void *txq,
165                 struct rte_mbuf **tx_pkts, uint16_t nb_pkts);
166
167 /* ARGS */
168
169 int failsafe_args_parse(struct rte_eth_dev *dev, const char *params);
170 void failsafe_args_free(struct rte_eth_dev *dev);
171 int failsafe_args_count_subdevice(struct rte_eth_dev *dev, const char *params);
172 int failsafe_args_parse_subs(struct rte_eth_dev *dev);
173
174 /* EAL */
175
176 int failsafe_eal_init(struct rte_eth_dev *dev);
177 int failsafe_eal_uninit(struct rte_eth_dev *dev);
178
179 /* ETH_DEV */
180
181 int failsafe_eth_dev_state_sync(struct rte_eth_dev *dev);
182 void failsafe_dev_remove(struct rte_eth_dev *dev);
183 int failsafe_eth_rmv_event_callback(uint8_t port_id,
184                                     enum rte_eth_event_type type,
185                                     void *arg, void *out);
186 int failsafe_eth_lsc_event_callback(uint8_t port_id,
187                                     enum rte_eth_event_type event,
188                                     void *cb_arg, void *out);
189
190 /* GLOBALS */
191
192 extern const char pmd_failsafe_driver_name[];
193 extern const struct eth_dev_ops failsafe_ops;
194 extern const struct rte_flow_ops fs_flow_ops;
195 extern uint64_t hotplug_poll;
196 extern int mac_from_arg;
197
198 /* HELPERS */
199
200 /* dev: (struct rte_eth_dev *) fail-safe device */
201 #define PRIV(dev) \
202         ((struct fs_priv *)(dev)->data->dev_private)
203
204 /* sdev: (struct sub_device *) */
205 #define ETH(sdev) \
206         ((sdev)->edev)
207
208 /* sdev: (struct sub_device *) */
209 #define PORT_ID(sdev) \
210         (ETH(sdev)->data->port_id)
211
212 /* sdev: (struct sub_device *) */
213 #define SUB_ID(sdev) \
214         ((sdev)->sid)
215
216 /**
217  * Stateful iterator construct over fail-safe sub-devices:
218  * s:     (struct sub_device *), iterator
219  * i:     (uint8_t), increment
220  * dev:   (struct rte_eth_dev *), fail-safe ethdev
221  * state: (enum dev_state), minimum acceptable device state
222  */
223 #define FOREACH_SUBDEV_STATE(s, i, dev, state)                          \
224         for (i = fs_find_next((dev), 0, state);                         \
225              i < PRIV(dev)->subs_tail && (s = &PRIV(dev)->subs[i]);     \
226              i = fs_find_next((dev), i + 1, state))
227
228 /**
229  * Iterator construct over fail-safe sub-devices:
230  * s:   (struct sub_device *), iterator
231  * i:   (uint8_t), increment
232  * dev: (struct rte_eth_dev *), fail-safe ethdev
233  */
234 #define FOREACH_SUBDEV(s, i, dev)                       \
235         FOREACH_SUBDEV_STATE(s, i, dev, DEV_UNDEFINED)
236
237 /* dev: (struct rte_eth_dev *) fail-safe device */
238 #define PREFERRED_SUBDEV(dev) \
239         (&PRIV(dev)->subs[0])
240
241 /* dev: (struct rte_eth_dev *) fail-safe device */
242 #define TX_SUBDEV(dev)                                                    \
243         (PRIV(dev)->subs_tx >= PRIV(dev)->subs_tail                ? NULL \
244          : (PRIV(dev)->subs[PRIV(dev)->subs_tx].state < DEV_PROBED ? NULL \
245          : &PRIV(dev)->subs[PRIV(dev)->subs_tx]))
246
247 /**
248  * s:   (struct sub_device *)
249  * ops: (struct eth_dev_ops) member
250  */
251 #define SUBOPS(s, ops) \
252         (ETH(s)->dev_ops->ops)
253
254 /**
255  * Atomic guard
256  */
257
258 /**
259  * a: (rte_atomic64_t)
260  */
261 #define FS_ATOMIC_P(a) \
262         rte_atomic64_add(&(a), 1)
263
264 /**
265  * a: (rte_atomic64_t)
266  */
267 #define FS_ATOMIC_V(a) \
268         rte_atomic64_sub(&(a), 1)
269
270 /**
271  * s: (struct sub_device *)
272  * i: uint16_t qid
273  */
274 #define FS_ATOMIC_RX(s, i) \
275         rte_atomic64_read( \
276          &((struct rxq *)((s)->fs_dev->data->rx_queues[i]))->refcnt[(s)->sid] \
277         )
278 /**
279  * s: (struct sub_device *)
280  * i: uint16_t qid
281  */
282 #define FS_ATOMIC_TX(s, i) \
283         rte_atomic64_read( \
284          &((struct txq *)((s)->fs_dev->data->tx_queues[i]))->refcnt[(s)->sid] \
285         )
286
287 #define LOG__(level, m, ...) \
288         RTE_LOG(level, PMD, "net_failsafe: " m "%c", __VA_ARGS__)
289 #define LOG_(level, ...) LOG__(level, __VA_ARGS__, '\n')
290 #define DEBUG(...) LOG_(DEBUG, __VA_ARGS__)
291 #define INFO(...) LOG_(INFO, __VA_ARGS__)
292 #define WARN(...) LOG_(WARNING, __VA_ARGS__)
293 #define ERROR(...) LOG_(ERR, __VA_ARGS__)
294
295 /* inlined functions */
296
297 static inline uint8_t
298 fs_find_next(struct rte_eth_dev *dev, uint8_t sid,
299                 enum dev_state min_state)
300 {
301         while (sid < PRIV(dev)->subs_tail) {
302                 if (PRIV(dev)->subs[sid].state >= min_state)
303                         break;
304                 sid++;
305         }
306         if (sid >= PRIV(dev)->subs_tail)
307                 return PRIV(dev)->subs_tail;
308         return sid;
309 }
310
311 /*
312  * Switch emitting device.
313  * If banned is set, banned must not be considered for
314  * the role of emitting device.
315  */
316 static inline void
317 fs_switch_dev(struct rte_eth_dev *dev,
318               struct sub_device *banned)
319 {
320         struct sub_device *txd;
321         enum dev_state req_state;
322
323         req_state = PRIV(dev)->state;
324         txd = TX_SUBDEV(dev);
325         if (PREFERRED_SUBDEV(dev)->state >= req_state &&
326             PREFERRED_SUBDEV(dev) != banned) {
327                 if (txd != PREFERRED_SUBDEV(dev) &&
328                     (txd == NULL ||
329                      (req_state == DEV_STARTED) ||
330                      (txd && txd->state < DEV_STARTED))) {
331                         DEBUG("Switching tx_dev to preferred sub_device");
332                         PRIV(dev)->subs_tx = 0;
333                 }
334         } else if ((txd && txd->state < req_state) ||
335                    txd == NULL ||
336                    txd == banned) {
337                 struct sub_device *sdev;
338                 uint8_t i;
339
340                 /* Using acceptable device */
341                 FOREACH_SUBDEV_STATE(sdev, i, dev, req_state) {
342                         if (sdev == banned)
343                                 continue;
344                         DEBUG("Switching tx_dev to sub_device %d",
345                               i);
346                         PRIV(dev)->subs_tx = i;
347                         break;
348                 }
349         } else if (txd && txd->state < req_state) {
350                 DEBUG("No device ready, deactivating tx_dev");
351                 PRIV(dev)->subs_tx = PRIV(dev)->subs_tail;
352         } else {
353                 return;
354         }
355         set_burst_fn(dev, 0);
356         rte_wmb();
357 }
358
359 #endif /* _RTE_ETH_FAILSAFE_PRIVATE_H_ */