blob: 66d5379c305e60d17dcde8b60d286f0b57b23a16 [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
2 * Common code for low-level network console, dump, and debugger code
3 *
4 * Derived from netconsole, kgdb-over-ethernet, and netdump patches
5 */
6
7#ifndef _LINUX_NETPOLL_H
8#define _LINUX_NETPOLL_H
9
10#include <linux/netdevice.h>
11#include <linux/interrupt.h>
Matt Mackall53fb95d2005-08-11 19:27:43 -070012#include <linux/rcupdate.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070013#include <linux/list.h>
14
Linus Torvalds1da177e2005-04-16 15:20:36 -070015struct netpoll {
16 struct net_device *dev;
Stephen Hemmingerbf6bce72006-10-26 15:46:56 -070017 char dev_name[IFNAMSIZ];
18 const char *name;
Linus Torvalds1da177e2005-04-16 15:20:36 -070019 void (*rx_hook)(struct netpoll *, int, char *, int);
Stephen Hemminger5de4a472006-10-26 15:46:55 -070020
Harvey Harrisone7557af2009-03-28 15:38:31 +000021 __be32 local_ip, remote_ip;
Linus Torvalds1da177e2005-04-16 15:20:36 -070022 u16 local_port, remote_port;
Stephen Hemminger09538642007-11-19 19:23:29 -080023 u8 remote_mac[ETH_ALEN];
Daniel Borkmann508e14b2010-01-12 14:27:30 +000024
25 struct list_head rx; /* rx_np list element */
Amerigo Wang38e6bc12012-08-10 01:24:38 +000026 struct rcu_head rcu;
Jeff Moyer115c1d62005-06-22 22:05:31 -070027};
28
29struct netpoll_info {
Stephen Hemminger93ec2c72006-10-26 15:46:50 -070030 atomic_t refcnt;
Daniel Borkmann508e14b2010-01-12 14:27:30 +000031
David S. Millerd9452e92008-03-04 12:28:49 -080032 int rx_flags;
Jeff Moyerfbeec2e2005-06-22 22:05:59 -070033 spinlock_t rx_lock;
Daniel Borkmann508e14b2010-01-12 14:27:30 +000034 struct list_head rx_np; /* netpolls that registered an rx_hook */
35
Neil Horman068c6e92006-06-26 00:04:27 -070036 struct sk_buff_head arp_tx; /* list of arp requests to reply to */
Stephen Hemmingerb6cd27e2006-10-26 15:46:51 -070037 struct sk_buff_head txq;
Daniel Borkmann508e14b2010-01-12 14:27:30 +000038
David Howells6d5aefb2006-12-05 19:36:26 +000039 struct delayed_work tx_work;
WANG Cong0e34e932010-05-06 00:47:21 -070040
41 struct netpoll *netpoll;
Amerigo Wang38e6bc12012-08-10 01:24:38 +000042 struct rcu_head rcu;
Linus Torvalds1da177e2005-04-16 15:20:36 -070043};
44
Linus Torvalds1da177e2005-04-16 15:20:36 -070045void netpoll_send_udp(struct netpoll *np, const char *msg, int len);
Satyam Sharma0bcc1812007-08-10 15:35:05 -070046void netpoll_print_options(struct netpoll *np);
Linus Torvalds1da177e2005-04-16 15:20:36 -070047int netpoll_parse_options(struct netpoll *np, char *opt);
Amerigo Wang47be03a22012-08-10 01:24:37 +000048int __netpoll_setup(struct netpoll *np, struct net_device *ndev, gfp_t gfp);
Linus Torvalds1da177e2005-04-16 15:20:36 -070049int netpoll_setup(struct netpoll *np);
50int netpoll_trap(void);
51void netpoll_set_trap(int trap);
Herbert Xu8fdd95e2010-06-10 16:12:48 +000052void __netpoll_cleanup(struct netpoll *np);
Amerigo Wang38e6bc12012-08-10 01:24:38 +000053void __netpoll_free_rcu(struct netpoll *np);
Linus Torvalds1da177e2005-04-16 15:20:36 -070054void netpoll_cleanup(struct netpoll *np);
Amerigo Wang57c5d462012-08-10 01:24:40 +000055int __netpoll_rx(struct sk_buff *skb, struct netpoll_info *npinfo);
Neil Hormanc2355e12010-10-13 16:01:49 +000056void netpoll_send_skb_on_dev(struct netpoll *np, struct sk_buff *skb,
57 struct net_device *dev);
58static inline void netpoll_send_skb(struct netpoll *np, struct sk_buff *skb)
59{
Amerigo Wang28996562012-08-10 01:24:42 +000060 unsigned long flags;
61 local_irq_save(flags);
Neil Hormanc2355e12010-10-13 16:01:49 +000062 netpoll_send_skb_on_dev(np, skb, np->dev);
Amerigo Wang28996562012-08-10 01:24:42 +000063 local_irq_restore(flags);
Neil Hormanc2355e12010-10-13 16:01:49 +000064}
65
Stephen Hemminger5de4a472006-10-26 15:46:55 -070066
Linus Torvalds1da177e2005-04-16 15:20:36 -070067
68#ifdef CONFIG_NETPOLL
Amerigo Wang77ab8a52012-08-10 01:24:46 +000069static inline bool netpoll_rx_on(struct sk_buff *skb)
Amerigo Wang91fe4a42012-08-10 01:24:41 +000070{
71 struct netpoll_info *npinfo = rcu_dereference_bh(skb->dev->npinfo);
72
73 return npinfo && (!list_empty(&npinfo->rx_np) || npinfo->rx_flags);
74}
75
David S. Millerffb27362010-05-06 01:20:10 -070076static inline bool netpoll_rx(struct sk_buff *skb)
Linus Torvalds1da177e2005-04-16 15:20:36 -070077{
Herbert Xude85d992010-06-10 16:12:44 +000078 struct netpoll_info *npinfo;
Jeff Moyerfbeec2e2005-06-22 22:05:59 -070079 unsigned long flags;
David S. Millerffb27362010-05-06 01:20:10 -070080 bool ret = false;
Jeff Moyer115c1d62005-06-22 22:05:31 -070081
Herbert Xuf0f9dea2010-09-17 16:55:03 -070082 local_irq_save(flags);
Herbert Xude85d992010-06-10 16:12:44 +000083
Amerigo Wang91fe4a42012-08-10 01:24:41 +000084 if (!netpoll_rx_on(skb))
Herbert Xude85d992010-06-10 16:12:44 +000085 goto out;
Jeff Moyer115c1d62005-06-22 22:05:31 -070086
Amerigo Wang91fe4a42012-08-10 01:24:41 +000087 npinfo = rcu_dereference_bh(skb->dev->npinfo);
Herbert Xuf0f9dea2010-09-17 16:55:03 -070088 spin_lock(&npinfo->rx_lock);
David S. Millerd9452e92008-03-04 12:28:49 -080089 /* check rx_flags again with the lock held */
Amerigo Wang57c5d462012-08-10 01:24:40 +000090 if (npinfo->rx_flags && __netpoll_rx(skb, npinfo))
David S. Millerffb27362010-05-06 01:20:10 -070091 ret = true;
Herbert Xuf0f9dea2010-09-17 16:55:03 -070092 spin_unlock(&npinfo->rx_lock);
Jeff Moyerfbeec2e2005-06-22 22:05:59 -070093
Herbert Xude85d992010-06-10 16:12:44 +000094out:
Herbert Xuf0f9dea2010-09-17 16:55:03 -070095 local_irq_restore(flags);
Jeff Moyerfbeec2e2005-06-22 22:05:59 -070096 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -070097}
98
Stephen Hemmingerbea33482007-10-03 16:41:36 -070099static inline int netpoll_receive_skb(struct sk_buff *skb)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700100{
Stephen Hemmingerbea33482007-10-03 16:41:36 -0700101 if (!list_empty(&skb->dev->napi_list))
102 return netpoll_rx(skb);
103 return 0;
104}
105
106static inline void *netpoll_poll_lock(struct napi_struct *napi)
107{
108 struct net_device *dev = napi->dev;
109
Stephen Hemmingerbea33482007-10-03 16:41:36 -0700110 if (dev && dev->npinfo) {
111 spin_lock(&napi->poll_lock);
112 napi->poll_owner = smp_processor_id();
113 return napi;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700114 }
Matt Mackall53fb95d2005-08-11 19:27:43 -0700115 return NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700116}
117
Matt Mackall53fb95d2005-08-11 19:27:43 -0700118static inline void netpoll_poll_unlock(void *have)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700119{
Stephen Hemmingerbea33482007-10-03 16:41:36 -0700120 struct napi_struct *napi = have;
Matt Mackall53fb95d2005-08-11 19:27:43 -0700121
Stephen Hemmingerbea33482007-10-03 16:41:36 -0700122 if (napi) {
123 napi->poll_owner = -1;
124 spin_unlock(&napi->poll_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700125 }
126}
127
Amerigo Wang77ab8a52012-08-10 01:24:46 +0000128static inline bool netpoll_tx_running(struct net_device *dev)
Herbert Xuc18370f2010-06-10 16:12:49 +0000129{
130 return irqs_disabled();
131}
132
Linus Torvalds1da177e2005-04-16 15:20:36 -0700133#else
John W. Linville969a6e52010-08-10 16:24:41 -0700134static inline bool netpoll_rx(struct sk_buff *skb)
Stephen Hemmingerbea33482007-10-03 16:41:36 -0700135{
Amerigo Wang77ab8a52012-08-10 01:24:46 +0000136 return false;
Stephen Hemmingerbea33482007-10-03 16:41:36 -0700137}
Amerigo Wang77ab8a52012-08-10 01:24:46 +0000138static inline bool netpoll_rx_on(struct sk_buff *skb)
Herbert Xud1c76af2009-03-16 10:50:02 -0700139{
Amerigo Wang77ab8a52012-08-10 01:24:46 +0000140 return false;
Herbert Xud1c76af2009-03-16 10:50:02 -0700141}
Stephen Hemmingerbea33482007-10-03 16:41:36 -0700142static inline int netpoll_receive_skb(struct sk_buff *skb)
143{
144 return 0;
145}
146static inline void *netpoll_poll_lock(struct napi_struct *napi)
147{
148 return NULL;
149}
150static inline void netpoll_poll_unlock(void *have)
151{
152}
153static inline void netpoll_netdev_init(struct net_device *dev)
154{
155}
Amerigo Wang77ab8a52012-08-10 01:24:46 +0000156static inline bool netpoll_tx_running(struct net_device *dev)
Herbert Xuc18370f2010-06-10 16:12:49 +0000157{
Amerigo Wang77ab8a52012-08-10 01:24:46 +0000158 return false;
Herbert Xuc18370f2010-06-10 16:12:49 +0000159}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700160#endif
161
162#endif