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