xref: /freebsd/sys/compat/linuxkpi/common/include/linux/netdevice.h (revision 105331f658e2f14fb4daa64dcd076c7369505295)
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 net_addr_assign_type {
79  	NET_ADDR_RANDOM,
80  };
81  
82  enum netdev_tx {
83  	NETDEV_TX_OK		= 0,
84  };
85  typedef	enum netdev_tx		netdev_tx_t;
86  
87  struct netdev_hw_addr {
88  	struct list_head	addr_list;
89  	uint8_t			addr[MAX_ADDR_LEN];
90  };
91  
92  struct netdev_hw_addr_list {
93  	struct list_head	addr_list;
94  	int			count;
95  };
96  
97  enum net_device_reg_state {
98  	NETREG_DUMMY		= 1,
99  	NETREG_REGISTERED,
100  };
101  
102  enum tc_setup_type {
103  	TC_SETUP_MAX_DUMMY,
104  };
105  
106  struct net_device_ops {
107  	int (*ndo_open)(struct net_device *);
108  	int (*ndo_stop)(struct net_device *);
109  	int (*ndo_set_mac_address)(struct net_device *,  void *);
110  	netdev_tx_t (*ndo_start_xmit)(struct sk_buff *, struct net_device *);
111  	void (*ndo_set_rx_mode)(struct net_device *);
112  };
113  
114  struct net_device {
115  	/* net_device fields seen publicly. */
116  	/* XXX can we later make some aliases to ifnet? */
117  	char				name[IFNAMSIZ];
118  	struct wireless_dev		*ieee80211_ptr;
119  	uint8_t				dev_addr[ETH_ALEN];
120  	struct netdev_hw_addr_list	mc;
121  	netdev_features_t		features;
122  	struct {
123  		unsigned long		multicast;
124  
125  		unsigned long		rx_bytes;
126  		unsigned long		rx_errors;
127  		unsigned long		rx_packets;
128  		unsigned long		tx_bytes;
129  		unsigned long		tx_dropped;
130  		unsigned long		tx_errors;
131  		unsigned long		tx_packets;
132  	} stats;
133  	enum net_addr_assign_type	addr_assign_type;
134  	enum net_device_reg_state	reg_state;
135  	const struct ethtool_ops	*ethtool_ops;
136  	const struct net_device_ops	*netdev_ops;
137  
138  	bool				needs_free_netdev;
139  	/* Not properly typed as-of now. */
140  	int	flags, type;
141  	int	name_assign_type, needed_headroom;
142  	int	threaded;
143  
144  	void (*priv_destructor)(struct net_device *);
145  
146  	/* net_device internal. */
147  	struct device			dev;
148  
149  	/*
150  	 * In case we delete the net_device we need to be able to clear all
151  	 * NAPI consumers.
152  	 */
153  	struct mtx			napi_mtx;
154  	TAILQ_HEAD(, napi_struct)	napi_head;
155  	struct taskqueue		*napi_tq;
156  
157  	/* Must stay last. */
158  	uint8_t				drv_priv[0] __aligned(CACHE_LINE_SIZE);
159  };
160  
161  #define	SET_NETDEV_DEV(_ndev, _dev)	(_ndev)->dev.parent = _dev;
162  
163  /* -------------------------------------------------------------------------- */
164  /* According to linux::ipoib_main.c. */
165  struct netdev_notifier_info {
166  	struct net_device	*dev;
167  	struct ifnet		*ifp;
168  };
169  
170  static inline struct net_device *
netdev_notifier_info_to_dev(struct netdev_notifier_info * ni)171  netdev_notifier_info_to_dev(struct netdev_notifier_info *ni)
172  {
173  	return (ni->dev);
174  }
175  
176  static inline struct ifnet *
netdev_notifier_info_to_ifp(struct netdev_notifier_info * ni)177  netdev_notifier_info_to_ifp(struct netdev_notifier_info *ni)
178  {
179  	return (ni->ifp);
180  }
181  
182  int	register_netdevice_notifier(struct notifier_block *);
183  int	register_inetaddr_notifier(struct notifier_block *);
184  int	unregister_netdevice_notifier(struct notifier_block *);
185  int	unregister_inetaddr_notifier(struct notifier_block *);
186  
187  /* -------------------------------------------------------------------------- */
188  
189  #define	NAPI_POLL_WEIGHT			64	/* budget */
190  
191  /*
192   * There are drivers directly testing napi state bits, so we need to publicly
193   * expose them.  If you ask me, those accesses should be hid behind an
194   * inline function and the bit flags not be directly exposed.
195   */
196  enum napi_state_bits {
197  	/*
198  	 * Official Linux flags encountered.
199  	 */
200  	NAPI_STATE_SCHED = 1,
201  
202  	/*
203  	 * Our internal versions (for now).
204  	 */
205  	/* Do not schedule new things while we are waiting to clear things. */
206  	LKPI_NAPI_FLAG_DISABLE_PENDING = 0,
207  	/* To synchronise that only one poll is ever running. */
208  	LKPI_NAPI_FLAG_IS_SCHEDULED = 1,
209  	/* If trying to schedule while poll is running. Need to re-schedule. */
210  	LKPI_NAPI_FLAG_LOST_RACE_TRY_AGAIN = 2,
211  	/* When shutting down forcefully prevent anything from running task/poll. */
212  	LKPI_NAPI_FLAG_SHUTDOWN = 3,
213  };
214  
215  struct napi_struct {
216  	TAILQ_ENTRY(napi_struct)	entry;
217  
218  	struct list_head	rx_list;
219  	struct net_device	*dev;
220  	int			(*poll)(struct napi_struct *, int);
221  	int			budget;
222  	int			rx_count;
223  
224  
225  	/*
226  	 * These flags mostly need to be checked/changed atomically
227  	 * (multiple together in some cases).
228  	 */
229  	volatile unsigned long	state;
230  
231  	/* FreeBSD internal. */
232  	/* Use task for now, so we can easily switch between direct and task. */
233  	struct task		napi_task;
234  };
235  
236  void linuxkpi_init_dummy_netdev(struct net_device *);
237  void linuxkpi_netif_napi_add(struct net_device *, struct napi_struct *,
238      int(*napi_poll)(struct napi_struct *, int));
239  void linuxkpi_netif_napi_del(struct napi_struct *);
240  bool linuxkpi_napi_schedule_prep(struct napi_struct *);
241  void linuxkpi___napi_schedule(struct napi_struct *);
242  bool linuxkpi_napi_schedule(struct napi_struct *);
243  void linuxkpi_napi_reschedule(struct napi_struct *);
244  bool linuxkpi_napi_complete_done(struct napi_struct *, int);
245  bool linuxkpi_napi_complete(struct napi_struct *);
246  void linuxkpi_napi_disable(struct napi_struct *);
247  void linuxkpi_napi_enable(struct napi_struct *);
248  void linuxkpi_napi_synchronize(struct napi_struct *);
249  
250  #define	init_dummy_netdev(_n)						\
251  	linuxkpi_init_dummy_netdev(_n)
252  #define	netif_napi_add(_nd, _ns, _p)					\
253  	linuxkpi_netif_napi_add(_nd, _ns, _p)
254  #define	netif_napi_del(_n)						\
255  	linuxkpi_netif_napi_del(_n)
256  #define	napi_schedule_prep(_n)						\
257  	linuxkpi_napi_schedule_prep(_n)
258  #define	__napi_schedule(_n)						\
259  	linuxkpi___napi_schedule(_n)
260  #define	napi_schedule(_n)						\
261  	linuxkpi_napi_schedule(_n)
262  #define	napi_reschedule(_n)						\
263  	linuxkpi_napi_reschedule(_n)
264  #define	napi_complete_done(_n, _r)					\
265  	linuxkpi_napi_complete_done(_n, _r)
266  #define	napi_complete(_n)						\
267  	linuxkpi_napi_complete(_n)
268  #define	napi_disable(_n)						\
269  	linuxkpi_napi_disable(_n)
270  #define	napi_enable(_n)							\
271  	linuxkpi_napi_enable(_n)
272  #define	napi_synchronize(_n)						\
273  	linuxkpi_napi_synchronize(_n)
274  
275  
276  static inline void
netif_napi_add_tx(struct net_device * dev,struct napi_struct * napi,int (* napi_poll)(struct napi_struct *,int))277  netif_napi_add_tx(struct net_device *dev, struct napi_struct *napi,
278      int(*napi_poll)(struct napi_struct *, int))
279  {
280  
281  	netif_napi_add(dev, napi, napi_poll);
282  }
283  
284  static inline bool
napi_is_scheduled(struct napi_struct * napi)285  napi_is_scheduled(struct napi_struct *napi)
286  {
287  
288  	return (test_bit(LKPI_NAPI_FLAG_IS_SCHEDULED, &napi->state));
289  }
290  
291  /* -------------------------------------------------------------------------- */
292  
293  static inline void
netdev_rss_key_fill(uint32_t * buf,size_t len)294  netdev_rss_key_fill(uint32_t *buf, size_t len)
295  {
296  
297  	/*
298  	 * Remembering from a previous life there was discussions on what is
299  	 * a good RSS hash key.  See end of rss_init() in net/rss_config.c.
300  	 * iwlwifi is looking for a 10byte "secret" so stay with random for now.
301  	 */
302  	get_random_bytes(buf, len);
303  }
304  
305  static inline int
netdev_hw_addr_list_count(struct netdev_hw_addr_list * list)306  netdev_hw_addr_list_count(struct netdev_hw_addr_list *list)
307  {
308  
309  	return (list->count);
310  }
311  
312  static inline int
netdev_mc_count(struct net_device * ndev)313  netdev_mc_count(struct net_device *ndev)
314  {
315  
316  	return (netdev_hw_addr_list_count(&ndev->mc));
317  }
318  
319  #define	netdev_hw_addr_list_for_each(_addr, _list)			\
320  	list_for_each_entry((_addr), &(_list)->addr_list, addr_list)
321  
322  #define	netdev_for_each_mc_addr(na, ndev)				\
323  	netdev_hw_addr_list_for_each(na, &(ndev)->mc)
324  
325  static __inline void
synchronize_net(void)326  synchronize_net(void)
327  {
328  
329  	/* We probably cannot do that unconditionally at some point anymore. */
330  	synchronize_rcu();
331  }
332  
333  static __inline void
netif_receive_skb_list(struct list_head * head)334  netif_receive_skb_list(struct list_head *head)
335  {
336  
337  	pr_debug("%s: TODO\n", __func__);
338  }
339  
340  static __inline int
napi_gro_receive(struct napi_struct * napi,struct sk_buff * skb)341  napi_gro_receive(struct napi_struct *napi, struct sk_buff *skb)
342  {
343  
344  	pr_debug("%s: TODO\n", __func__);
345  	return (-1);
346  }
347  
348  static __inline void
ether_setup(struct net_device * ndev)349  ether_setup(struct net_device *ndev)
350  {
351  
352  	pr_debug("%s: TODO\n", __func__);
353  }
354  
355  static __inline void
dev_net_set(struct net_device * ndev,void * p)356  dev_net_set(struct net_device *ndev, void *p)
357  {
358  
359  	pr_debug("%s: TODO\n", __func__);
360  }
361  
362  static __inline int
dev_set_threaded(struct net_device * ndev,bool threaded)363  dev_set_threaded(struct net_device *ndev, bool threaded)
364  {
365  
366  	pr_debug("%s: TODO\n", __func__);
367  	return (-ENODEV);
368  }
369  
370  /* -------------------------------------------------------------------------- */
371  
372  static __inline bool
netif_carrier_ok(struct net_device * ndev)373  netif_carrier_ok(struct net_device *ndev)
374  {
375  	pr_debug("%s: TODO\n", __func__);
376  	return (false);
377  }
378  
379  static __inline void
netif_carrier_off(struct net_device * ndev)380  netif_carrier_off(struct net_device *ndev)
381  {
382  	pr_debug("%s: TODO\n", __func__);
383  }
384  
385  static __inline void
netif_carrier_on(struct net_device * ndev)386  netif_carrier_on(struct net_device *ndev)
387  {
388  	pr_debug("%s: TODO\n", __func__);
389  }
390  
391  /* -------------------------------------------------------------------------- */
392  
393  static __inline bool
netif_queue_stopped(struct net_device * ndev)394  netif_queue_stopped(struct net_device *ndev)
395  {
396  	pr_debug("%s: TODO\n", __func__);
397  	return (false);
398  }
399  
400  static __inline void
netif_stop_queue(struct net_device * ndev)401  netif_stop_queue(struct net_device *ndev)
402  {
403  	pr_debug("%s: TODO\n", __func__);
404  }
405  
406  static __inline void
netif_wake_queue(struct net_device * ndev)407  netif_wake_queue(struct net_device *ndev)
408  {
409  	pr_debug("%s: TODO\n", __func__);
410  }
411  
412  /* -------------------------------------------------------------------------- */
413  
414  static __inline int
register_netdevice(struct net_device * ndev)415  register_netdevice(struct net_device *ndev)
416  {
417  
418  	/* assert rtnl_locked? */
419  	pr_debug("%s: TODO\n", __func__);
420  	return (0);
421  }
422  
423  static __inline int
register_netdev(struct net_device * ndev)424  register_netdev(struct net_device *ndev)
425  {
426  	int error;
427  
428  	/* lock */
429  	error = register_netdevice(ndev);
430  	/* unlock */
431  	pr_debug("%s: TODO\n", __func__);
432  	return (error);
433  }
434  
435  static __inline void
unregister_netdev(struct net_device * ndev)436  unregister_netdev(struct net_device *ndev)
437  {
438  	pr_debug("%s: TODO\n", __func__);
439  }
440  
441  static __inline void
unregister_netdevice(struct net_device * ndev)442  unregister_netdevice(struct net_device *ndev)
443  {
444  	pr_debug("%s: TODO\n", __func__);
445  }
446  
447  /* -------------------------------------------------------------------------- */
448  
449  static __inline void
netif_rx(struct sk_buff * skb)450  netif_rx(struct sk_buff *skb)
451  {
452  	pr_debug("%s: TODO\n", __func__);
453  }
454  
455  static __inline void
netif_rx_ni(struct sk_buff * skb)456  netif_rx_ni(struct sk_buff *skb)
457  {
458  	pr_debug("%s: TODO\n", __func__);
459  }
460  
461  /* -------------------------------------------------------------------------- */
462  
463  struct net_device *linuxkpi_alloc_netdev(size_t, const char *, uint32_t,
464      void(*)(struct net_device *));
465  void linuxkpi_free_netdev(struct net_device *);
466  
467  #define	alloc_netdev(_l, _n, _f, _func)						\
468  	linuxkpi_alloc_netdev(_l, _n, _f, _func)
469  #define	alloc_netdev_dummy(_l)							\
470  	linuxkpi_alloc_netdev(_l, "dummy", NET_NAME_UNKNOWN, NULL)
471  #define	free_netdev(_n)								\
472  	linuxkpi_free_netdev(_n)
473  
474  static inline void *
netdev_priv(const struct net_device * ndev)475  netdev_priv(const struct net_device *ndev)
476  {
477  
478  	return (__DECONST(void *, ndev->drv_priv));
479  }
480  
481  /* -------------------------------------------------------------------------- */
482  /* This is really rtnetlink and probably belongs elsewhere. */
483  
484  #define	rtnl_lock()		do { } while(0)
485  #define	rtnl_unlock()		do { } while(0)
486  #define	rcu_dereference_rtnl(x)	READ_ONCE(x)
487  
488  #endif	/* _LINUXKPI_LINUX_NETDEVICE_H */
489