netpoll.h 3.7 KB
Newer Older
L
Linus Torvalds 已提交
1 2 3 4 5 6 7 8 9 10 11
/*
 * Common code for low-level network console, dump, and debugger code
 *
 * Derived from netconsole, kgdb-over-ethernet, and netdump patches
 */

#ifndef _LINUX_NETPOLL_H
#define _LINUX_NETPOLL_H

#include <linux/netdevice.h>
#include <linux/interrupt.h>
12
#include <linux/rcupdate.h>
L
Linus Torvalds 已提交
13 14
#include <linux/list.h>

C
Cong Wang 已提交
15 16 17 18 19 20 21 22
union inet_addr {
	__u32		all[4];
	__be32		ip;
	__be32		ip6[4];
	struct in_addr	in;
	struct in6_addr	in6;
};

L
Linus Torvalds 已提交
23 24
struct netpoll {
	struct net_device *dev;
S
Stephen Hemminger 已提交
25 26
	char dev_name[IFNAMSIZ];
	const char *name;
L
Linus Torvalds 已提交
27
	void (*rx_hook)(struct netpoll *, int, char *, int);
S
Stephen Hemminger 已提交
28

C
Cong Wang 已提交
29 30
	union inet_addr local_ip, remote_ip;
	bool ipv6;
L
Linus Torvalds 已提交
31
	u16 local_port, remote_port;
32
	u8 remote_mac[ETH_ALEN];
33 34

	struct list_head rx; /* rx_np list element */
35
	struct rcu_head rcu;
36 37 38
};

struct netpoll_info {
S
Stephen Hemminger 已提交
39
	atomic_t refcnt;
40

41
	int rx_flags;
42
	spinlock_t rx_lock;
43 44
	struct list_head rx_np; /* netpolls that registered an rx_hook */

C
Cong Wang 已提交
45
	struct sk_buff_head neigh_tx; /* list of neigh requests to reply to */
S
Stephen Hemminger 已提交
46
	struct sk_buff_head txq;
47

48
	struct delayed_work tx_work;
49 50

	struct netpoll *netpoll;
51
	struct rcu_head rcu;
L
Linus Torvalds 已提交
52 53 54
};

void netpoll_send_udp(struct netpoll *np, const char *msg, int len);
55
void netpoll_print_options(struct netpoll *np);
L
Linus Torvalds 已提交
56
int netpoll_parse_options(struct netpoll *np, char *opt);
57
int __netpoll_setup(struct netpoll *np, struct net_device *ndev, gfp_t gfp);
L
Linus Torvalds 已提交
58 59 60
int netpoll_setup(struct netpoll *np);
int netpoll_trap(void);
void netpoll_set_trap(int trap);
61
void __netpoll_cleanup(struct netpoll *np);
62
void __netpoll_free_rcu(struct netpoll *np);
L
Linus Torvalds 已提交
63
void netpoll_cleanup(struct netpoll *np);
64
int __netpoll_rx(struct sk_buff *skb, struct netpoll_info *npinfo);
65 66 67 68
void netpoll_send_skb_on_dev(struct netpoll *np, struct sk_buff *skb,
			     struct net_device *dev);
static inline void netpoll_send_skb(struct netpoll *np, struct sk_buff *skb)
{
69 70
	unsigned long flags;
	local_irq_save(flags);
71
	netpoll_send_skb_on_dev(np, skb, np->dev);
72
	local_irq_restore(flags);
73 74
}

S
Stephen Hemminger 已提交
75

L
Linus Torvalds 已提交
76 77

#ifdef CONFIG_NETPOLL
78
static inline bool netpoll_rx_on(struct sk_buff *skb)
79 80 81 82 83 84
{
	struct netpoll_info *npinfo = rcu_dereference_bh(skb->dev->npinfo);

	return npinfo && (!list_empty(&npinfo->rx_np) || npinfo->rx_flags);
}

85
static inline bool netpoll_rx(struct sk_buff *skb)
L
Linus Torvalds 已提交
86
{
H
Herbert Xu 已提交
87
	struct netpoll_info *npinfo;
88
	unsigned long flags;
89
	bool ret = false;
90

91
	local_irq_save(flags);
H
Herbert Xu 已提交
92

93
	if (!netpoll_rx_on(skb))
H
Herbert Xu 已提交
94
		goto out;
95

96
	npinfo = rcu_dereference_bh(skb->dev->npinfo);
97
	spin_lock(&npinfo->rx_lock);
98
	/* check rx_flags again with the lock held */
99
	if (npinfo->rx_flags && __netpoll_rx(skb, npinfo))
100
		ret = true;
101
	spin_unlock(&npinfo->rx_lock);
102

H
Herbert Xu 已提交
103
out:
104
	local_irq_restore(flags);
105
	return ret;
L
Linus Torvalds 已提交
106 107
}

108
static inline int netpoll_receive_skb(struct sk_buff *skb)
L
Linus Torvalds 已提交
109
{
110 111 112 113 114 115 116 117 118 119 120 121 122
	if (!list_empty(&skb->dev->napi_list))
		return netpoll_rx(skb);
	return 0;
}

static inline void *netpoll_poll_lock(struct napi_struct *napi)
{
	struct net_device *dev = napi->dev;

	if (dev && dev->npinfo) {
		spin_lock(&napi->poll_lock);
		napi->poll_owner = smp_processor_id();
		return napi;
L
Linus Torvalds 已提交
123
	}
124
	return NULL;
L
Linus Torvalds 已提交
125 126
}

127
static inline void netpoll_poll_unlock(void *have)
L
Linus Torvalds 已提交
128
{
129
	struct napi_struct *napi = have;
130

131 132 133
	if (napi) {
		napi->poll_owner = -1;
		spin_unlock(&napi->poll_lock);
L
Linus Torvalds 已提交
134 135 136
	}
}

137
static inline bool netpoll_tx_running(struct net_device *dev)
H
Herbert Xu 已提交
138 139 140 141
{
	return irqs_disabled();
}

L
Linus Torvalds 已提交
142
#else
143
static inline bool netpoll_rx(struct sk_buff *skb)
144
{
145
	return false;
146
}
147
static inline bool netpoll_rx_on(struct sk_buff *skb)
148
{
149
	return false;
150
}
151 152 153 154 155 156 157 158 159 160 161 162 163 164
static inline int netpoll_receive_skb(struct sk_buff *skb)
{
	return 0;
}
static inline void *netpoll_poll_lock(struct napi_struct *napi)
{
	return NULL;
}
static inline void netpoll_poll_unlock(void *have)
{
}
static inline void netpoll_netdev_init(struct net_device *dev)
{
}
165
static inline bool netpoll_tx_running(struct net_device *dev)
H
Herbert Xu 已提交
166
{
167
	return false;
H
Herbert Xu 已提交
168
}
L
Linus Torvalds 已提交
169 170 171
#endif

#endif