1 /*-
2 * Copyright (c) 2010 Isilon Systems, Inc.
3 * Copyright (c) 2010 iX Systems, Inc.
4 * Copyright (c) 2010 Panasas, Inc.
5 * Copyright (c) 2013-2019 Mellanox Technologies, Ltd.
6 * All rights reserved.
7 * Copyright (c) 2020-2021 The FreeBSD Foundation
8 * Copyright (c) 2020-2022 Bjoern A. Zeeb
9 *
10 * Portions of this software were developed by Björn Zeeb
11 * under sponsorship from the FreeBSD Foundation.
12 *
13 * Redistribution and use in source and binary forms, with or without
14 * modification, are permitted provided that the following conditions
15 * are met:
16 * 1. Redistributions of source code must retain the above copyright
17 * notice unmodified, this list of conditions, and the following
18 * disclaimer.
19 * 2. Redistributions in binary form must reproduce the above copyright
20 * notice, this list of conditions and the following disclaimer in the
21 * documentation and/or other materials provided with the distribution.
22 *
23 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
24 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
25 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
26 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
27 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
28 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
29 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
30 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
31 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
32 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
33 */
34 #ifndef _LINUXKPI_LINUX_NETDEVICE_H
35 #define _LINUXKPI_LINUX_NETDEVICE_H
36
37 #include <linux/types.h>
38 #include <linux/netdev_features.h>
39
40 #include <sys/param.h>
41 #include <sys/kernel.h>
42 #include <sys/lock.h>
43 #include <sys/mutex.h>
44 #include <sys/malloc.h>
45 #include <sys/queue.h>
46 #include <sys/socket.h>
47 #include <sys/taskqueue.h>
48
49 #include <net/if_types.h>
50 #include <net/if.h>
51 #include <net/if_var.h>
52 #include <net/if_dl.h>
53
54 #include <linux/kernel.h>
55 #include <linux/bitops.h>
56 #include <linux/list.h>
57 #include <linux/device.h>
58 #include <linux/net.h>
59 #include <linux/if_ether.h>
60 #include <linux/notifier.h>
61 #include <linux/random.h>
62 #include <linux/rcupdate.h>
63
64 #ifdef VIMAGE
65 #define init_net *vnet0
66 #else
67 #define init_net *((struct vnet *)0)
68 #endif
69
70 struct sk_buff;
71 struct net_device;
72 struct wireless_dev; /* net/cfg80211.h */
73
74 #define MAX_ADDR_LEN 20
75
76 #define NET_NAME_UNKNOWN 0
77
78 enum netdev_tx {
79 NETDEV_TX_OK = 0,
80 };
81 typedef enum netdev_tx netdev_tx_t;
82
83 struct netdev_hw_addr {
84 struct list_head addr_list;
85 uint8_t addr[MAX_ADDR_LEN];
86 };
87
88 struct netdev_hw_addr_list {
89 struct list_head addr_list;
90 int count;
91 };
92
93 enum net_device_reg_state {
94 NETREG_DUMMY = 1,
95 NETREG_REGISTERED,
96 };
97
98 struct net_device_ops {
99 int (*ndo_open)(struct net_device *);
100 int (*ndo_stop)(struct net_device *);
101 int (*ndo_set_mac_address)(struct net_device *, void *);
102 netdev_tx_t (*ndo_start_xmit)(struct sk_buff *, struct net_device *);
103 void (*ndo_set_rx_mode)(struct net_device *);
104 };
105
106 struct net_device {
107 /* BSD specific for compat. */
108 struct ifnet bsdifp;
109
110 /* net_device fields seen publicly. */
111 /* XXX can we later make some aliases to ifnet? */
112 char name[IFNAMSIZ];
113 struct wireless_dev *ieee80211_ptr;
114 uint8_t dev_addr[ETH_ALEN];
115 struct netdev_hw_addr_list mc;
116 netdev_features_t features;
117 struct {
118 unsigned long multicast;
119
120 unsigned long rx_bytes;
121 unsigned long rx_errors;
122 unsigned long rx_packets;
123 unsigned long tx_bytes;
124 unsigned long tx_dropped;
125 unsigned long tx_errors;
126 unsigned long tx_packets;
127 } stats;
128 enum net_device_reg_state reg_state;
129 const struct ethtool_ops *ethtool_ops;
130 const struct net_device_ops *netdev_ops;
131
132 bool needs_free_netdev;
133 /* Not properly typed as-of now. */
134 int flags, type;
135 int name_assign_type, needed_headroom;
136 int threaded;
137
138 void (*priv_destructor)(struct net_device *);
139
140 /* net_device internal. */
141 struct device dev;
142
143 /*
144 * In case we delete the net_device we need to be able to clear all
145 * NAPI consumers.
146 */
147 struct mtx napi_mtx;
148 TAILQ_HEAD(, napi_struct) napi_head;
149 struct taskqueue *napi_tq;
150
151 /* Must stay last. */
152 uint8_t drv_priv[0] __aligned(CACHE_LINE_SIZE);
153 };
154
155 #define SET_NETDEV_DEV(_ndev, _dev) (_ndev)->dev.parent = _dev;
156
157 /* -------------------------------------------------------------------------- */
158 /* According to linux::ipoib_main.c. */
159 struct netdev_notifier_info {
160 struct net_device *dev;
161 struct ifnet *ifp;
162 };
163
164 static inline struct net_device *
netdev_notifier_info_to_dev(struct netdev_notifier_info * ni)165 netdev_notifier_info_to_dev(struct netdev_notifier_info *ni)
166 {
167 return (ni->dev);
168 }
169
170 static inline struct ifnet *
netdev_notifier_info_to_ifp(struct netdev_notifier_info * ni)171 netdev_notifier_info_to_ifp(struct netdev_notifier_info *ni)
172 {
173 return (ni->ifp);
174 }
175
176 int register_netdevice_notifier(struct notifier_block *);
177 int register_inetaddr_notifier(struct notifier_block *);
178 int unregister_netdevice_notifier(struct notifier_block *);
179 int unregister_inetaddr_notifier(struct notifier_block *);
180
181 /* -------------------------------------------------------------------------- */
182
183 #define NAPI_POLL_WEIGHT 64 /* budget */
184
185 /*
186 * There are drivers directly testing napi state bits, so we need to publicly
187 * expose them. If you ask me, those accesses should be hid behind an
188 * inline function and the bit flags not be directly exposed.
189 */
190 enum napi_state_bits {
191 /*
192 * Official Linux flags encountered.
193 */
194 NAPI_STATE_SCHED = 1,
195
196 /*
197 * Our internal versions (for now).
198 */
199 /* Do not schedule new things while we are waiting to clear things. */
200 LKPI_NAPI_FLAG_DISABLE_PENDING = 0,
201 /* To synchronise that only one poll is ever running. */
202 LKPI_NAPI_FLAG_IS_SCHEDULED = 1,
203 /* If trying to schedule while poll is running. Need to re-schedule. */
204 LKPI_NAPI_FLAG_LOST_RACE_TRY_AGAIN = 2,
205 /* When shutting down forcefully prevent anything from running task/poll. */
206 LKPI_NAPI_FLAG_SHUTDOWN = 3,
207 };
208
209 struct napi_struct {
210 TAILQ_ENTRY(napi_struct) entry;
211
212 struct list_head rx_list;
213 struct net_device *dev;
214 int (*poll)(struct napi_struct *, int);
215 int budget;
216 int rx_count;
217
218
219 /*
220 * These flags mostly need to be checked/changed atomically
221 * (multiple together in some cases).
222 */
223 volatile unsigned long state;
224
225 /* FreeBSD internal. */
226 /* Use task for now, so we can easily switch between direct and task. */
227 struct task napi_task;
228 };
229
230 void linuxkpi_init_dummy_netdev(struct net_device *);
231 void linuxkpi_netif_napi_add(struct net_device *, struct napi_struct *,
232 int(*napi_poll)(struct napi_struct *, int));
233 void linuxkpi_netif_napi_del(struct napi_struct *);
234 bool linuxkpi_napi_schedule_prep(struct napi_struct *);
235 void linuxkpi___napi_schedule(struct napi_struct *);
236 bool linuxkpi_napi_schedule(struct napi_struct *);
237 void linuxkpi_napi_reschedule(struct napi_struct *);
238 bool linuxkpi_napi_complete_done(struct napi_struct *, int);
239 bool linuxkpi_napi_complete(struct napi_struct *);
240 void linuxkpi_napi_disable(struct napi_struct *);
241 void linuxkpi_napi_enable(struct napi_struct *);
242 void linuxkpi_napi_synchronize(struct napi_struct *);
243
244 #define init_dummy_netdev(_n) \
245 linuxkpi_init_dummy_netdev(_n)
246 #define netif_napi_add(_nd, _ns, _p) \
247 linuxkpi_netif_napi_add(_nd, _ns, _p)
248 #define netif_napi_del(_n) \
249 linuxkpi_netif_napi_del(_n)
250 #define napi_schedule_prep(_n) \
251 linuxkpi_napi_schedule_prep(_n)
252 #define __napi_schedule(_n) \
253 linuxkpi___napi_schedule(_n)
254 #define napi_schedule(_n) \
255 linuxkpi_napi_schedule(_n)
256 #define napi_reschedule(_n) \
257 linuxkpi_napi_reschedule(_n)
258 #define napi_complete_done(_n, _r) \
259 linuxkpi_napi_complete_done(_n, _r)
260 #define napi_complete(_n) \
261 linuxkpi_napi_complete(_n)
262 #define napi_disable(_n) \
263 linuxkpi_napi_disable(_n)
264 #define napi_enable(_n) \
265 linuxkpi_napi_enable(_n)
266 #define napi_synchronize(_n) \
267 linuxkpi_napi_synchronize(_n)
268
269
270 static inline void
netif_napi_add_tx(struct net_device * dev,struct napi_struct * napi,int (* napi_poll)(struct napi_struct *,int))271 netif_napi_add_tx(struct net_device *dev, struct napi_struct *napi,
272 int(*napi_poll)(struct napi_struct *, int))
273 {
274
275 netif_napi_add(dev, napi, napi_poll);
276 }
277
278 static inline bool
napi_is_scheduled(struct napi_struct * napi)279 napi_is_scheduled(struct napi_struct *napi)
280 {
281
282 return (test_bit(LKPI_NAPI_FLAG_IS_SCHEDULED, &napi->state));
283 }
284
285 /* -------------------------------------------------------------------------- */
286
287 static inline void
netdev_rss_key_fill(uint32_t * buf,size_t len)288 netdev_rss_key_fill(uint32_t *buf, size_t len)
289 {
290
291 /*
292 * Remembering from a previous life there was discussions on what is
293 * a good RSS hash key. See end of rss_init() in net/rss_config.c.
294 * iwlwifi is looking for a 10byte "secret" so stay with random for now.
295 */
296 get_random_bytes(buf, len);
297 }
298
299 static inline int
netdev_hw_addr_list_count(struct netdev_hw_addr_list * list)300 netdev_hw_addr_list_count(struct netdev_hw_addr_list *list)
301 {
302
303 return (list->count);
304 }
305
306 static inline int
netdev_mc_count(struct net_device * ndev)307 netdev_mc_count(struct net_device *ndev)
308 {
309
310 return (netdev_hw_addr_list_count(&ndev->mc));
311 }
312
313 #define netdev_hw_addr_list_for_each(_addr, _list) \
314 list_for_each_entry((_addr), &(_list)->addr_list, addr_list)
315
316 #define netdev_for_each_mc_addr(na, ndev) \
317 netdev_hw_addr_list_for_each(na, &(ndev)->mc)
318
319 static __inline void
synchronize_net(void)320 synchronize_net(void)
321 {
322
323 /* We probably cannot do that unconditionally at some point anymore. */
324 synchronize_rcu();
325 }
326
327 static __inline void
netif_receive_skb_list(struct list_head * head)328 netif_receive_skb_list(struct list_head *head)
329 {
330
331 pr_debug("%s: TODO\n", __func__);
332 }
333
334 static __inline int
napi_gro_receive(struct napi_struct * napi,struct sk_buff * skb)335 napi_gro_receive(struct napi_struct *napi, struct sk_buff *skb)
336 {
337
338 pr_debug("%s: TODO\n", __func__);
339 return (-1);
340 }
341
342 static __inline void
ether_setup(struct net_device * ndev)343 ether_setup(struct net_device *ndev)
344 {
345
346 pr_debug("%s: TODO\n", __func__);
347 }
348
349 static __inline void
dev_net_set(struct net_device * ndev,void * p)350 dev_net_set(struct net_device *ndev, void *p)
351 {
352
353 pr_debug("%s: TODO\n", __func__);
354 }
355
356 static __inline int
dev_set_threaded(struct net_device * ndev,bool threaded)357 dev_set_threaded(struct net_device *ndev, bool threaded)
358 {
359
360 pr_debug("%s: TODO\n", __func__);
361 return (-ENODEV);
362 }
363
364 /* -------------------------------------------------------------------------- */
365
366 static __inline bool
netif_carrier_ok(struct net_device * ndev)367 netif_carrier_ok(struct net_device *ndev)
368 {
369 pr_debug("%s: TODO\n", __func__);
370 return (false);
371 }
372
373 static __inline void
netif_carrier_off(struct net_device * ndev)374 netif_carrier_off(struct net_device *ndev)
375 {
376 pr_debug("%s: TODO\n", __func__);
377 }
378
379 static __inline void
netif_carrier_on(struct net_device * ndev)380 netif_carrier_on(struct net_device *ndev)
381 {
382 pr_debug("%s: TODO\n", __func__);
383 }
384
385 /* -------------------------------------------------------------------------- */
386
387 static __inline bool
netif_queue_stopped(struct net_device * ndev)388 netif_queue_stopped(struct net_device *ndev)
389 {
390 pr_debug("%s: TODO\n", __func__);
391 return (false);
392 }
393
394 static __inline void
netif_stop_queue(struct net_device * ndev)395 netif_stop_queue(struct net_device *ndev)
396 {
397 pr_debug("%s: TODO\n", __func__);
398 }
399
400 static __inline void
netif_wake_queue(struct net_device * ndev)401 netif_wake_queue(struct net_device *ndev)
402 {
403 pr_debug("%s: TODO\n", __func__);
404 }
405
406 /* -------------------------------------------------------------------------- */
407
408 static __inline int
register_netdevice(struct net_device * ndev)409 register_netdevice(struct net_device *ndev)
410 {
411
412 /* assert rtnl_locked? */
413 pr_debug("%s: TODO\n", __func__);
414 return (0);
415 }
416
417 static __inline int
register_netdev(struct net_device * ndev)418 register_netdev(struct net_device *ndev)
419 {
420 int error;
421
422 /* lock */
423 error = register_netdevice(ndev);
424 /* unlock */
425 pr_debug("%s: TODO\n", __func__);
426 return (error);
427 }
428
429 static __inline void
unregister_netdev(struct net_device * ndev)430 unregister_netdev(struct net_device *ndev)
431 {
432 pr_debug("%s: TODO\n", __func__);
433 }
434
435 static __inline void
unregister_netdevice(struct net_device * ndev)436 unregister_netdevice(struct net_device *ndev)
437 {
438 pr_debug("%s: TODO\n", __func__);
439 }
440
441 /* -------------------------------------------------------------------------- */
442
443 static __inline void
netif_rx(struct sk_buff * skb)444 netif_rx(struct sk_buff *skb)
445 {
446 pr_debug("%s: TODO\n", __func__);
447 }
448
449 static __inline void
netif_rx_ni(struct sk_buff * skb)450 netif_rx_ni(struct sk_buff *skb)
451 {
452 pr_debug("%s: TODO\n", __func__);
453 }
454
455 /* -------------------------------------------------------------------------- */
456
457 struct net_device *linuxkpi_alloc_netdev(size_t, const char *, uint32_t,
458 void(*)(struct net_device *));
459 void linuxkpi_free_netdev(struct net_device *);
460
461 #define alloc_netdev(_l, _n, _f, _func) \
462 linuxkpi_alloc_netdev(_l, _n, _f, _func)
463 #define free_netdev(_n) \
464 linuxkpi_free_netdev(_n)
465
466 static inline void *
netdev_priv(const struct net_device * ndev)467 netdev_priv(const struct net_device *ndev)
468 {
469
470 return (__DECONST(void *, ndev->drv_priv));
471 }
472
473 /* -------------------------------------------------------------------------- */
474 /* This is really rtnetlink and probably belongs elsewhere. */
475
476 #define rtnl_lock() do { } while(0)
477 #define rtnl_unlock() do { } while(0)
478 #define rcu_dereference_rtnl(x) READ_ONCE(x)
479
480 #endif /* _LINUXKPI_LINUX_NETDEVICE_H */
481