xref: /linux/include/linux/netpoll.h (revision 91ec2035134982b98fab0609a9fd8480e8217dc1)
1 /* SPDX-License-Identifier: GPL-2.0 */
2 /*
3  * Common code for low-level network console, dump, and debugger code
4  *
5  * Derived from netconsole, kgdb-over-ethernet, and netdump patches
6  */
7 
8 #ifndef _LINUX_NETPOLL_H
9 #define _LINUX_NETPOLL_H
10 
11 #include <linux/netdevice.h>
12 #include <linux/interrupt.h>
13 #include <linux/rcupdate.h>
14 #include <linux/list.h>
15 #include <linux/refcount.h>
16 #include <linux/ip.h>
17 #include <linux/udp.h>
18 
19 union inet_addr {
20 	__be32		ip;
21 	struct in6_addr	in6;
22 };
23 
24 struct netpoll {
25 	struct net_device *dev;
26 	netdevice_tracker dev_tracker;
27 	/*
28 	 * Either dev_name or dev_mac can be used to specify the local
29 	 * interface - dev_name is used if it is a nonempty string, else
30 	 * dev_mac is used.
31 	 */
32 	char dev_name[IFNAMSIZ];
33 	u8 dev_mac[ETH_ALEN];
34 	const char *name;
35 };
36 
37 #define np_info(np, fmt, ...)				\
38 	pr_info("%s: " fmt, np->name, ##__VA_ARGS__)
39 #define np_err(np, fmt, ...)				\
40 	pr_err("%s: " fmt, np->name, ##__VA_ARGS__)
41 #define np_notice(np, fmt, ...)				\
42 	pr_notice("%s: " fmt, np->name, ##__VA_ARGS__)
43 
44 struct netpoll_info {
45 	refcount_t refcnt;
46 
47 	struct semaphore dev_lock;
48 
49 	struct sk_buff_head txq;
50 
51 	struct delayed_work tx_work;
52 
53 	struct rcu_head rcu;
54 };
55 
56 #ifdef CONFIG_NETPOLL
57 void netpoll_poll_dev(struct net_device *dev);
58 void netpoll_poll_disable(struct net_device *dev);
59 void netpoll_poll_enable(struct net_device *dev);
60 #else
netpoll_poll_disable(struct net_device * dev)61 static inline void netpoll_poll_disable(struct net_device *dev) { return; }
netpoll_poll_enable(struct net_device * dev)62 static inline void netpoll_poll_enable(struct net_device *dev) { return; }
63 #endif
64 
65 int __netpoll_setup(struct netpoll *np, struct net_device *ndev);
66 void __netpoll_free(struct netpoll *np);
67 void netpoll_cleanup(struct netpoll *np);
68 void do_netpoll_cleanup(struct netpoll *np);
69 netdev_tx_t netpoll_send_skb(struct netpoll *np, struct sk_buff *skb);
70 void netpoll_zap_completion_queue(void);
71 unsigned int netpoll_get_carrier_timeout(void);
72 
73 #ifdef CONFIG_NETPOLL
netpoll_poll_lock(struct napi_struct * napi)74 static inline void *netpoll_poll_lock(struct napi_struct *napi)
75 {
76 	struct net_device *dev = napi->dev;
77 
78 	if (dev && rcu_access_pointer(dev->npinfo)) {
79 		int owner = smp_processor_id();
80 
81 		while (cmpxchg(&napi->poll_owner, -1, owner) != -1)
82 			cpu_relax();
83 
84 		return napi;
85 	}
86 	return NULL;
87 }
88 
netpoll_poll_unlock(void * have)89 static inline void netpoll_poll_unlock(void *have)
90 {
91 	struct napi_struct *napi = have;
92 
93 	if (napi)
94 		smp_store_release(&napi->poll_owner, -1);
95 }
96 
netpoll_tx_running(struct net_device * dev)97 static inline bool netpoll_tx_running(struct net_device *dev)
98 {
99 	return irqs_disabled();
100 }
101 
102 #else
netpoll_poll_lock(struct napi_struct * napi)103 static inline void *netpoll_poll_lock(struct napi_struct *napi)
104 {
105 	return NULL;
106 }
netpoll_poll_unlock(void * have)107 static inline void netpoll_poll_unlock(void *have)
108 {
109 }
netpoll_tx_running(struct net_device * dev)110 static inline bool netpoll_tx_running(struct net_device *dev)
111 {
112 	return false;
113 }
114 #endif
115 
116 #endif
117