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