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