Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1 | /* |
| 2 | * Common framework for low-level network console, dump, and debugger code |
| 3 | * |
| 4 | * Sep 8 2003 Matt Mackall <mpm@selenic.com> |
| 5 | * |
| 6 | * based on the netconsole code from: |
| 7 | * |
| 8 | * Copyright (C) 2001 Ingo Molnar <mingo@redhat.com> |
| 9 | * Copyright (C) 2002 Red Hat, Inc. |
| 10 | */ |
| 11 | |
Joe Perches | e6ec26935 | 2012-01-29 15:50:43 +0000 | [diff] [blame] | 12 | #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt |
| 13 | |
Anton Vorontsov | bff3877 | 2009-07-08 11:10:56 -0700 | [diff] [blame] | 14 | #include <linux/moduleparam.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 15 | #include <linux/netdevice.h> |
| 16 | #include <linux/etherdevice.h> |
| 17 | #include <linux/string.h> |
Arnaldo Carvalho de Melo | 14c8502 | 2005-12-27 02:43:12 -0200 | [diff] [blame] | 18 | #include <linux/if_arp.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 19 | #include <linux/inetdevice.h> |
| 20 | #include <linux/inet.h> |
| 21 | #include <linux/interrupt.h> |
| 22 | #include <linux/netpoll.h> |
| 23 | #include <linux/sched.h> |
| 24 | #include <linux/delay.h> |
| 25 | #include <linux/rcupdate.h> |
| 26 | #include <linux/workqueue.h> |
Tejun Heo | 5a0e3ad | 2010-03-24 17:04:11 +0900 | [diff] [blame] | 27 | #include <linux/slab.h> |
Paul Gortmaker | bc3b2d7 | 2011-07-15 11:47:34 -0400 | [diff] [blame] | 28 | #include <linux/export.h> |
Amerigo Wang | 689971b | 2012-08-10 01:24:49 +0000 | [diff] [blame] | 29 | #include <linux/if_vlan.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 30 | #include <net/tcp.h> |
| 31 | #include <net/udp.h> |
Cong Wang | b3d936f | 2013-01-07 20:52:41 +0000 | [diff] [blame] | 32 | #include <net/addrconf.h> |
| 33 | #include <net/ndisc.h> |
| 34 | #include <net/ip6_checksum.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 35 | #include <asm/unaligned.h> |
David S. Miller | 9cbc1cb | 2009-06-15 03:02:23 -0700 | [diff] [blame] | 36 | #include <trace/events/napi.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 37 | |
| 38 | /* |
| 39 | * We maintain a small pool of fully-sized skbs, to make sure the |
| 40 | * message gets out even in extreme OOM situations. |
| 41 | */ |
| 42 | |
| 43 | #define MAX_UDP_CHUNK 1460 |
| 44 | #define MAX_SKBS 32 |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 45 | |
Stephen Hemminger | a1bcfac | 2006-11-14 10:43:58 -0800 | [diff] [blame] | 46 | static struct sk_buff_head skb_pool; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 47 | |
| 48 | static atomic_t trapped; |
| 49 | |
Stephen Hemminger | 2bdfe0b | 2006-10-26 15:46:54 -0700 | [diff] [blame] | 50 | #define USEC_PER_POLL 50 |
David S. Miller | d9452e9 | 2008-03-04 12:28:49 -0800 | [diff] [blame] | 51 | #define NETPOLL_RX_ENABLED 1 |
| 52 | #define NETPOLL_RX_DROP 2 |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 53 | |
Joe Perches | 6f70624 | 2012-01-29 15:50:44 +0000 | [diff] [blame] | 54 | #define MAX_SKB_SIZE \ |
| 55 | (sizeof(struct ethhdr) + \ |
| 56 | sizeof(struct iphdr) + \ |
| 57 | sizeof(struct udphdr) + \ |
| 58 | MAX_UDP_CHUNK) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 59 | |
David S. Miller | 3578b0c | 2010-08-03 00:24:04 -0700 | [diff] [blame] | 60 | static void zap_completion_queue(void); |
Cong Wang | b7394d2 | 2013-01-07 20:52:39 +0000 | [diff] [blame] | 61 | static void netpoll_neigh_reply(struct sk_buff *skb, struct netpoll_info *npinfo); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 62 | |
Anton Vorontsov | bff3877 | 2009-07-08 11:10:56 -0700 | [diff] [blame] | 63 | static unsigned int carrier_timeout = 4; |
| 64 | module_param(carrier_timeout, uint, 0644); |
| 65 | |
Joe Perches | e6ec26935 | 2012-01-29 15:50:43 +0000 | [diff] [blame] | 66 | #define np_info(np, fmt, ...) \ |
| 67 | pr_info("%s: " fmt, np->name, ##__VA_ARGS__) |
| 68 | #define np_err(np, fmt, ...) \ |
| 69 | pr_err("%s: " fmt, np->name, ##__VA_ARGS__) |
| 70 | #define np_notice(np, fmt, ...) \ |
| 71 | pr_notice("%s: " fmt, np->name, ##__VA_ARGS__) |
| 72 | |
David Howells | c402895 | 2006-11-22 14:57:56 +0000 | [diff] [blame] | 73 | static void queue_process(struct work_struct *work) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 74 | { |
David Howells | 4c1ac1b | 2006-12-05 14:37:56 +0000 | [diff] [blame] | 75 | struct netpoll_info *npinfo = |
| 76 | container_of(work, struct netpoll_info, tx_work.work); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 77 | struct sk_buff *skb; |
Ingo Molnar | 3640543 | 2006-12-12 17:20:42 +0100 | [diff] [blame] | 78 | unsigned long flags; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 79 | |
Stephen Hemminger | 6c43ff1 | 2006-10-26 15:46:53 -0700 | [diff] [blame] | 80 | while ((skb = skb_dequeue(&npinfo->txq))) { |
| 81 | struct net_device *dev = skb->dev; |
Stephen Hemminger | 0082982 | 2008-11-20 20:14:53 -0800 | [diff] [blame] | 82 | const struct net_device_ops *ops = dev->netdev_ops; |
David S. Miller | fd2ea0a | 2008-07-17 01:56:23 -0700 | [diff] [blame] | 83 | struct netdev_queue *txq; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 84 | |
Stephen Hemminger | 6c43ff1 | 2006-10-26 15:46:53 -0700 | [diff] [blame] | 85 | if (!netif_device_present(dev) || !netif_running(dev)) { |
| 86 | __kfree_skb(skb); |
| 87 | continue; |
| 88 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 89 | |
David S. Miller | fd2ea0a | 2008-07-17 01:56:23 -0700 | [diff] [blame] | 90 | txq = netdev_get_tx_queue(dev, skb_get_queue_mapping(skb)); |
| 91 | |
Ingo Molnar | 3640543 | 2006-12-12 17:20:42 +0100 | [diff] [blame] | 92 | local_irq_save(flags); |
David S. Miller | fd2ea0a | 2008-07-17 01:56:23 -0700 | [diff] [blame] | 93 | __netif_tx_lock(txq, smp_processor_id()); |
Tom Herbert | 73466498 | 2011-11-28 16:32:44 +0000 | [diff] [blame] | 94 | if (netif_xmit_frozen_or_stopped(txq) || |
Stephen Hemminger | 0082982 | 2008-11-20 20:14:53 -0800 | [diff] [blame] | 95 | ops->ndo_start_xmit(skb, dev) != NETDEV_TX_OK) { |
Stephen Hemminger | 6c43ff1 | 2006-10-26 15:46:53 -0700 | [diff] [blame] | 96 | skb_queue_head(&npinfo->txq, skb); |
David S. Miller | fd2ea0a | 2008-07-17 01:56:23 -0700 | [diff] [blame] | 97 | __netif_tx_unlock(txq); |
Ingo Molnar | 3640543 | 2006-12-12 17:20:42 +0100 | [diff] [blame] | 98 | local_irq_restore(flags); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 99 | |
Jarek Poplawski | 25442ca | 2007-07-05 17:42:44 -0700 | [diff] [blame] | 100 | schedule_delayed_work(&npinfo->tx_work, HZ/10); |
Stephen Hemminger | 6c43ff1 | 2006-10-26 15:46:53 -0700 | [diff] [blame] | 101 | return; |
| 102 | } |
David S. Miller | fd2ea0a | 2008-07-17 01:56:23 -0700 | [diff] [blame] | 103 | __netif_tx_unlock(txq); |
Ingo Molnar | 3640543 | 2006-12-12 17:20:42 +0100 | [diff] [blame] | 104 | local_irq_restore(flags); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 105 | } |
| 106 | } |
| 107 | |
Al Viro | b51655b | 2006-11-14 21:40:42 -0800 | [diff] [blame] | 108 | static __sum16 checksum_udp(struct sk_buff *skb, struct udphdr *uh, |
| 109 | unsigned short ulen, __be32 saddr, __be32 daddr) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 110 | { |
Al Viro | d6f5493c | 2006-11-14 21:26:08 -0800 | [diff] [blame] | 111 | __wsum psum; |
Herbert Xu | fb286bb | 2005-11-10 13:01:24 -0800 | [diff] [blame] | 112 | |
Herbert Xu | 6047637 | 2007-04-09 11:59:39 -0700 | [diff] [blame] | 113 | if (uh->check == 0 || skb_csum_unnecessary(skb)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 114 | return 0; |
| 115 | |
Herbert Xu | fb286bb | 2005-11-10 13:01:24 -0800 | [diff] [blame] | 116 | psum = csum_tcpudp_nofold(saddr, daddr, ulen, IPPROTO_UDP, 0); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 117 | |
Patrick McHardy | 84fa793 | 2006-08-29 16:44:56 -0700 | [diff] [blame] | 118 | if (skb->ip_summed == CHECKSUM_COMPLETE && |
Al Viro | d3bc23e | 2006-11-14 21:24:49 -0800 | [diff] [blame] | 119 | !csum_fold(csum_add(psum, skb->csum))) |
Herbert Xu | fb286bb | 2005-11-10 13:01:24 -0800 | [diff] [blame] | 120 | return 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 121 | |
Herbert Xu | fb286bb | 2005-11-10 13:01:24 -0800 | [diff] [blame] | 122 | skb->csum = psum; |
| 123 | |
| 124 | return __skb_checksum_complete(skb); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 125 | } |
| 126 | |
| 127 | /* |
| 128 | * Check whether delayed processing was scheduled for our NIC. If so, |
| 129 | * we attempt to grab the poll lock and use ->poll() to pump the card. |
| 130 | * If this fails, either we've recursed in ->poll() or it's already |
| 131 | * running on another CPU. |
| 132 | * |
| 133 | * Note: we don't mask interrupts with this lock because we're using |
| 134 | * trylock here and interrupts are already disabled in the softirq |
| 135 | * case. Further, we test the poll_owner to avoid recursion on UP |
| 136 | * systems where the lock doesn't exist. |
| 137 | * |
| 138 | * In cases where there is bi-directional communications, reading only |
| 139 | * one message at a time can lead to packets being dropped by the |
| 140 | * network adapter, forcing superfluous retries and possibly timeouts. |
| 141 | * Thus, we set our budget to greater than 1. |
| 142 | */ |
David S. Miller | 0a7606c | 2007-10-29 21:28:47 -0700 | [diff] [blame] | 143 | static int poll_one_napi(struct netpoll_info *npinfo, |
| 144 | struct napi_struct *napi, int budget) |
| 145 | { |
| 146 | int work; |
| 147 | |
| 148 | /* net_rx_action's ->poll() invocations and our's are |
| 149 | * synchronized by this test which is only made while |
| 150 | * holding the napi->poll_lock. |
| 151 | */ |
| 152 | if (!test_bit(NAPI_STATE_SCHED, &napi->state)) |
| 153 | return budget; |
| 154 | |
David S. Miller | d9452e9 | 2008-03-04 12:28:49 -0800 | [diff] [blame] | 155 | npinfo->rx_flags |= NETPOLL_RX_DROP; |
David S. Miller | 0a7606c | 2007-10-29 21:28:47 -0700 | [diff] [blame] | 156 | atomic_inc(&trapped); |
Neil Horman | 7b363e4 | 2008-12-09 23:22:26 -0800 | [diff] [blame] | 157 | set_bit(NAPI_STATE_NPSVC, &napi->state); |
David S. Miller | 0a7606c | 2007-10-29 21:28:47 -0700 | [diff] [blame] | 158 | |
| 159 | work = napi->poll(napi, budget); |
David S. Miller | 7d18f11 | 2009-05-21 23:30:09 -0700 | [diff] [blame] | 160 | trace_napi_poll(napi); |
David S. Miller | 0a7606c | 2007-10-29 21:28:47 -0700 | [diff] [blame] | 161 | |
Neil Horman | 7b363e4 | 2008-12-09 23:22:26 -0800 | [diff] [blame] | 162 | clear_bit(NAPI_STATE_NPSVC, &napi->state); |
David S. Miller | 0a7606c | 2007-10-29 21:28:47 -0700 | [diff] [blame] | 163 | atomic_dec(&trapped); |
David S. Miller | d9452e9 | 2008-03-04 12:28:49 -0800 | [diff] [blame] | 164 | npinfo->rx_flags &= ~NETPOLL_RX_DROP; |
David S. Miller | 0a7606c | 2007-10-29 21:28:47 -0700 | [diff] [blame] | 165 | |
| 166 | return budget - work; |
| 167 | } |
| 168 | |
Stephen Hemminger | 5106930 | 2007-11-19 19:18:11 -0800 | [diff] [blame] | 169 | static void poll_napi(struct net_device *dev) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 170 | { |
Stephen Hemminger | bea3348 | 2007-10-03 16:41:36 -0700 | [diff] [blame] | 171 | struct napi_struct *napi; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 172 | int budget = 16; |
| 173 | |
Neil Horman | f13d493 | 2010-10-19 07:04:26 +0000 | [diff] [blame] | 174 | list_for_each_entry(napi, &dev->napi_list, dev_list) { |
David S. Miller | 0a7606c | 2007-10-29 21:28:47 -0700 | [diff] [blame] | 175 | if (napi->poll_owner != smp_processor_id() && |
Stephen Hemminger | bea3348 | 2007-10-03 16:41:36 -0700 | [diff] [blame] | 176 | spin_trylock(&napi->poll_lock)) { |
Amerigo Wang | 2899656 | 2012-08-10 01:24:42 +0000 | [diff] [blame] | 177 | budget = poll_one_napi(rcu_dereference_bh(dev->npinfo), |
| 178 | napi, budget); |
Stephen Hemminger | bea3348 | 2007-10-03 16:41:36 -0700 | [diff] [blame] | 179 | spin_unlock(&napi->poll_lock); |
David S. Miller | 0a7606c | 2007-10-29 21:28:47 -0700 | [diff] [blame] | 180 | |
Amerigo Wang | 072a9c4 | 2012-08-24 21:41:11 +0000 | [diff] [blame] | 181 | if (!budget) |
David S. Miller | 0a7606c | 2007-10-29 21:28:47 -0700 | [diff] [blame] | 182 | break; |
Stephen Hemminger | bea3348 | 2007-10-03 16:41:36 -0700 | [diff] [blame] | 183 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 184 | } |
| 185 | } |
| 186 | |
Cong Wang | b7394d2 | 2013-01-07 20:52:39 +0000 | [diff] [blame] | 187 | static void service_neigh_queue(struct netpoll_info *npi) |
Neil Horman | 068c6e9 | 2006-06-26 00:04:27 -0700 | [diff] [blame] | 188 | { |
Stephen Hemminger | 5106930 | 2007-11-19 19:18:11 -0800 | [diff] [blame] | 189 | if (npi) { |
| 190 | struct sk_buff *skb; |
Neil Horman | 068c6e9 | 2006-06-26 00:04:27 -0700 | [diff] [blame] | 191 | |
Cong Wang | b7394d2 | 2013-01-07 20:52:39 +0000 | [diff] [blame] | 192 | while ((skb = skb_dequeue(&npi->neigh_tx))) |
| 193 | netpoll_neigh_reply(skb, npi); |
Neil Horman | 068c6e9 | 2006-06-26 00:04:27 -0700 | [diff] [blame] | 194 | } |
Neil Horman | 068c6e9 | 2006-06-26 00:04:27 -0700 | [diff] [blame] | 195 | } |
| 196 | |
Joe Perches | 234b921 | 2011-06-30 15:08:57 +0000 | [diff] [blame] | 197 | static void netpoll_poll_dev(struct net_device *dev) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 198 | { |
Pavel Emelyanov | 5e39273 | 2009-05-11 00:36:35 +0000 | [diff] [blame] | 199 | const struct net_device_ops *ops; |
Amerigo Wang | 2899656 | 2012-08-10 01:24:42 +0000 | [diff] [blame] | 200 | struct netpoll_info *ni = rcu_dereference_bh(dev->npinfo); |
Stephen Hemminger | 5106930 | 2007-11-19 19:18:11 -0800 | [diff] [blame] | 201 | |
Pavel Emelyanov | 5e39273 | 2009-05-11 00:36:35 +0000 | [diff] [blame] | 202 | if (!dev || !netif_running(dev)) |
| 203 | return; |
| 204 | |
| 205 | ops = dev->netdev_ops; |
| 206 | if (!ops->ndo_poll_controller) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 207 | return; |
| 208 | |
| 209 | /* Process pending work on NIC */ |
Stephen Hemminger | d314774 | 2008-11-19 21:32:24 -0800 | [diff] [blame] | 210 | ops->ndo_poll_controller(dev); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 211 | |
Stephen Hemminger | 5106930 | 2007-11-19 19:18:11 -0800 | [diff] [blame] | 212 | poll_napi(dev); |
| 213 | |
Eric Dumazet | 58e05f3 | 2012-02-14 10:11:59 +0000 | [diff] [blame] | 214 | if (dev->flags & IFF_SLAVE) { |
Amerigo Wang | 2899656 | 2012-08-10 01:24:42 +0000 | [diff] [blame] | 215 | if (ni) { |
Jiri Pirko | 49bd8fb | 2013-01-03 22:48:55 +0000 | [diff] [blame] | 216 | struct net_device *bond_dev; |
Amerigo Wang | 5a698af | 2011-02-17 23:43:34 +0000 | [diff] [blame] | 217 | struct sk_buff *skb; |
Jiri Pirko | 49bd8fb | 2013-01-03 22:48:55 +0000 | [diff] [blame] | 218 | struct netpoll_info *bond_ni; |
| 219 | |
| 220 | bond_dev = netdev_master_upper_dev_get_rcu(dev); |
| 221 | bond_ni = rcu_dereference_bh(bond_dev->npinfo); |
Cong Wang | b7394d2 | 2013-01-07 20:52:39 +0000 | [diff] [blame] | 222 | while ((skb = skb_dequeue(&ni->neigh_tx))) { |
Amerigo Wang | 5a698af | 2011-02-17 23:43:34 +0000 | [diff] [blame] | 223 | skb->dev = bond_dev; |
Cong Wang | b7394d2 | 2013-01-07 20:52:39 +0000 | [diff] [blame] | 224 | skb_queue_tail(&bond_ni->neigh_tx, skb); |
Amerigo Wang | 5a698af | 2011-02-17 23:43:34 +0000 | [diff] [blame] | 225 | } |
| 226 | } |
| 227 | } |
| 228 | |
Cong Wang | b7394d2 | 2013-01-07 20:52:39 +0000 | [diff] [blame] | 229 | service_neigh_queue(ni); |
Neil Horman | 068c6e9 | 2006-06-26 00:04:27 -0700 | [diff] [blame] | 230 | |
David S. Miller | 3578b0c | 2010-08-03 00:24:04 -0700 | [diff] [blame] | 231 | zap_completion_queue(); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 232 | } |
| 233 | |
| 234 | static void refill_skbs(void) |
| 235 | { |
| 236 | struct sk_buff *skb; |
| 237 | unsigned long flags; |
| 238 | |
Stephen Hemminger | a1bcfac | 2006-11-14 10:43:58 -0800 | [diff] [blame] | 239 | spin_lock_irqsave(&skb_pool.lock, flags); |
| 240 | while (skb_pool.qlen < MAX_SKBS) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 241 | skb = alloc_skb(MAX_SKB_SIZE, GFP_ATOMIC); |
| 242 | if (!skb) |
| 243 | break; |
| 244 | |
Stephen Hemminger | a1bcfac | 2006-11-14 10:43:58 -0800 | [diff] [blame] | 245 | __skb_queue_tail(&skb_pool, skb); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 246 | } |
Stephen Hemminger | a1bcfac | 2006-11-14 10:43:58 -0800 | [diff] [blame] | 247 | spin_unlock_irqrestore(&skb_pool.lock, flags); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 248 | } |
| 249 | |
David S. Miller | 3578b0c | 2010-08-03 00:24:04 -0700 | [diff] [blame] | 250 | static void zap_completion_queue(void) |
| 251 | { |
| 252 | unsigned long flags; |
| 253 | struct softnet_data *sd = &get_cpu_var(softnet_data); |
| 254 | |
| 255 | if (sd->completion_queue) { |
| 256 | struct sk_buff *clist; |
| 257 | |
| 258 | local_irq_save(flags); |
| 259 | clist = sd->completion_queue; |
| 260 | sd->completion_queue = NULL; |
| 261 | local_irq_restore(flags); |
| 262 | |
| 263 | while (clist != NULL) { |
| 264 | struct sk_buff *skb = clist; |
| 265 | clist = clist->next; |
| 266 | if (skb->destructor) { |
| 267 | atomic_inc(&skb->users); |
| 268 | dev_kfree_skb_any(skb); /* put this one back */ |
| 269 | } else { |
| 270 | __kfree_skb(skb); |
| 271 | } |
| 272 | } |
| 273 | } |
| 274 | |
| 275 | put_cpu_var(softnet_data); |
| 276 | } |
| 277 | |
Stephen Hemminger | a1bcfac | 2006-11-14 10:43:58 -0800 | [diff] [blame] | 278 | static struct sk_buff *find_skb(struct netpoll *np, int len, int reserve) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 279 | { |
Stephen Hemminger | a1bcfac | 2006-11-14 10:43:58 -0800 | [diff] [blame] | 280 | int count = 0; |
| 281 | struct sk_buff *skb; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 282 | |
David S. Miller | 3578b0c | 2010-08-03 00:24:04 -0700 | [diff] [blame] | 283 | zap_completion_queue(); |
Stephen Hemminger | a1bcfac | 2006-11-14 10:43:58 -0800 | [diff] [blame] | 284 | refill_skbs(); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 285 | repeat: |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 286 | |
| 287 | skb = alloc_skb(len, GFP_ATOMIC); |
Stephen Hemminger | a1bcfac | 2006-11-14 10:43:58 -0800 | [diff] [blame] | 288 | if (!skb) |
| 289 | skb = skb_dequeue(&skb_pool); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 290 | |
| 291 | if (!skb) { |
Stephen Hemminger | a1bcfac | 2006-11-14 10:43:58 -0800 | [diff] [blame] | 292 | if (++count < 10) { |
Joe Perches | 2a49e00 | 2011-06-30 15:08:58 +0000 | [diff] [blame] | 293 | netpoll_poll_dev(np->dev); |
Stephen Hemminger | a1bcfac | 2006-11-14 10:43:58 -0800 | [diff] [blame] | 294 | goto repeat; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 295 | } |
Stephen Hemminger | a1bcfac | 2006-11-14 10:43:58 -0800 | [diff] [blame] | 296 | return NULL; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 297 | } |
| 298 | |
| 299 | atomic_set(&skb->users, 1); |
| 300 | skb_reserve(skb, reserve); |
| 301 | return skb; |
| 302 | } |
| 303 | |
Stephen Hemminger | bea3348 | 2007-10-03 16:41:36 -0700 | [diff] [blame] | 304 | static int netpoll_owner_active(struct net_device *dev) |
| 305 | { |
| 306 | struct napi_struct *napi; |
| 307 | |
| 308 | list_for_each_entry(napi, &dev->napi_list, dev_list) { |
| 309 | if (napi->poll_owner == smp_processor_id()) |
| 310 | return 1; |
| 311 | } |
| 312 | return 0; |
| 313 | } |
| 314 | |
Amerigo Wang | 2899656 | 2012-08-10 01:24:42 +0000 | [diff] [blame] | 315 | /* call with IRQ disabled */ |
Neil Horman | c2355e1 | 2010-10-13 16:01:49 +0000 | [diff] [blame] | 316 | void netpoll_send_skb_on_dev(struct netpoll *np, struct sk_buff *skb, |
| 317 | struct net_device *dev) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 318 | { |
Stephen Hemminger | 2bdfe0b | 2006-10-26 15:46:54 -0700 | [diff] [blame] | 319 | int status = NETDEV_TX_BUSY; |
| 320 | unsigned long tries; |
Stephen Hemminger | 0082982 | 2008-11-20 20:14:53 -0800 | [diff] [blame] | 321 | const struct net_device_ops *ops = dev->netdev_ops; |
Herbert Xu | de85d99 | 2010-06-10 16:12:44 +0000 | [diff] [blame] | 322 | /* It is up to the caller to keep npinfo alive. */ |
Amerigo Wang | 2899656 | 2012-08-10 01:24:42 +0000 | [diff] [blame] | 323 | struct netpoll_info *npinfo; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 324 | |
Amerigo Wang | 2899656 | 2012-08-10 01:24:42 +0000 | [diff] [blame] | 325 | WARN_ON_ONCE(!irqs_disabled()); |
| 326 | |
| 327 | npinfo = rcu_dereference_bh(np->dev->npinfo); |
YOSHIFUJI Hideaki | 4ec93ed | 2007-02-09 23:24:36 +0900 | [diff] [blame] | 328 | if (!npinfo || !netif_running(dev) || !netif_device_present(dev)) { |
| 329 | __kfree_skb(skb); |
| 330 | return; |
| 331 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 332 | |
Stephen Hemminger | 2bdfe0b | 2006-10-26 15:46:54 -0700 | [diff] [blame] | 333 | /* don't get messages out of order, and no recursion */ |
Stephen Hemminger | bea3348 | 2007-10-03 16:41:36 -0700 | [diff] [blame] | 334 | if (skb_queue_len(&npinfo->txq) == 0 && !netpoll_owner_active(dev)) { |
David S. Miller | fd2ea0a | 2008-07-17 01:56:23 -0700 | [diff] [blame] | 335 | struct netdev_queue *txq; |
Andrew Morton | a49f99f | 2006-12-11 17:24:46 -0800 | [diff] [blame] | 336 | |
Amerigo Wang | 8c4c49d | 2012-09-17 20:16:31 +0000 | [diff] [blame] | 337 | txq = netdev_pick_tx(dev, skb); |
David S. Miller | fd2ea0a | 2008-07-17 01:56:23 -0700 | [diff] [blame] | 338 | |
Stephen Hemminger | 0db3dc7 | 2007-06-27 00:39:42 -0700 | [diff] [blame] | 339 | /* try until next clock tick */ |
| 340 | for (tries = jiffies_to_usecs(1)/USEC_PER_POLL; |
| 341 | tries > 0; --tries) { |
David S. Miller | fd2ea0a | 2008-07-17 01:56:23 -0700 | [diff] [blame] | 342 | if (__netif_tx_trylock(txq)) { |
Tom Herbert | 73466498 | 2011-11-28 16:32:44 +0000 | [diff] [blame] | 343 | if (!netif_xmit_stopped(txq)) { |
Amerigo Wang | 689971b | 2012-08-10 01:24:49 +0000 | [diff] [blame] | 344 | if (vlan_tx_tag_present(skb) && |
| 345 | !(netif_skb_features(skb) & NETIF_F_HW_VLAN_TX)) { |
| 346 | skb = __vlan_put_tag(skb, vlan_tx_tag_get(skb)); |
| 347 | if (unlikely(!skb)) |
| 348 | break; |
| 349 | skb->vlan_tci = 0; |
| 350 | } |
| 351 | |
Stephen Hemminger | 0082982 | 2008-11-20 20:14:53 -0800 | [diff] [blame] | 352 | status = ops->ndo_start_xmit(skb, dev); |
Eric Dumazet | 08baf56 | 2009-05-25 22:58:01 -0700 | [diff] [blame] | 353 | if (status == NETDEV_TX_OK) |
| 354 | txq_trans_update(txq); |
| 355 | } |
David S. Miller | fd2ea0a | 2008-07-17 01:56:23 -0700 | [diff] [blame] | 356 | __netif_tx_unlock(txq); |
Matt Mackall | f0d3459 | 2005-08-11 19:25:11 -0700 | [diff] [blame] | 357 | |
Andrew Morton | e37b8d9 | 2006-12-09 14:01:49 -0800 | [diff] [blame] | 358 | if (status == NETDEV_TX_OK) |
| 359 | break; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 360 | |
Andrew Morton | e37b8d9 | 2006-12-09 14:01:49 -0800 | [diff] [blame] | 361 | } |
Stephen Hemminger | 0db3dc7 | 2007-06-27 00:39:42 -0700 | [diff] [blame] | 362 | |
| 363 | /* tickle device maybe there is some cleanup */ |
Joe Perches | 2a49e00 | 2011-06-30 15:08:58 +0000 | [diff] [blame] | 364 | netpoll_poll_dev(np->dev); |
Stephen Hemminger | 0db3dc7 | 2007-06-27 00:39:42 -0700 | [diff] [blame] | 365 | |
| 366 | udelay(USEC_PER_POLL); |
Matt Mackall | 0db1d6f | 2005-08-11 19:25:54 -0700 | [diff] [blame] | 367 | } |
Dongdong Deng | 79b1bee | 2009-08-21 03:33:36 +0000 | [diff] [blame] | 368 | |
| 369 | WARN_ONCE(!irqs_disabled(), |
Amerigo Wang | 2899656 | 2012-08-10 01:24:42 +0000 | [diff] [blame] | 370 | "netpoll_send_skb_on_dev(): %s enabled interrupts in poll (%pF)\n", |
Dongdong Deng | 79b1bee | 2009-08-21 03:33:36 +0000 | [diff] [blame] | 371 | dev->name, ops->ndo_start_xmit); |
| 372 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 373 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 374 | |
Stephen Hemminger | 2bdfe0b | 2006-10-26 15:46:54 -0700 | [diff] [blame] | 375 | if (status != NETDEV_TX_OK) { |
Stephen Hemminger | 5de4a47 | 2006-10-26 15:46:55 -0700 | [diff] [blame] | 376 | skb_queue_tail(&npinfo->txq, skb); |
David Howells | 4c1ac1b | 2006-12-05 14:37:56 +0000 | [diff] [blame] | 377 | schedule_delayed_work(&npinfo->tx_work,0); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 378 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 379 | } |
Neil Horman | c2355e1 | 2010-10-13 16:01:49 +0000 | [diff] [blame] | 380 | EXPORT_SYMBOL(netpoll_send_skb_on_dev); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 381 | |
| 382 | void netpoll_send_udp(struct netpoll *np, const char *msg, int len) |
| 383 | { |
Eric Dumazet | 954fba0 | 2012-06-12 19:30:21 +0000 | [diff] [blame] | 384 | int total_len, ip_len, udp_len; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 385 | struct sk_buff *skb; |
| 386 | struct udphdr *udph; |
| 387 | struct iphdr *iph; |
| 388 | struct ethhdr *eth; |
Eric Dumazet | ee13040 | 2012-08-24 01:47:26 +0000 | [diff] [blame] | 389 | static atomic_t ip_ident; |
Cong Wang | b3d936f | 2013-01-07 20:52:41 +0000 | [diff] [blame] | 390 | struct ipv6hdr *ip6h; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 391 | |
| 392 | udp_len = len + sizeof(*udph); |
Cong Wang | b3d936f | 2013-01-07 20:52:41 +0000 | [diff] [blame] | 393 | if (np->ipv6) |
| 394 | ip_len = udp_len + sizeof(*ip6h); |
| 395 | else |
Cong Wang | b7394d2 | 2013-01-07 20:52:39 +0000 | [diff] [blame] | 396 | ip_len = udp_len + sizeof(*iph); |
| 397 | |
Eric Dumazet | 954fba0 | 2012-06-12 19:30:21 +0000 | [diff] [blame] | 398 | total_len = ip_len + LL_RESERVED_SPACE(np->dev); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 399 | |
Eric Dumazet | 954fba0 | 2012-06-12 19:30:21 +0000 | [diff] [blame] | 400 | skb = find_skb(np, total_len + np->dev->needed_tailroom, |
| 401 | total_len - len); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 402 | if (!skb) |
| 403 | return; |
| 404 | |
Arnaldo Carvalho de Melo | 27d7ff4 | 2007-03-31 11:55:19 -0300 | [diff] [blame] | 405 | skb_copy_to_linear_data(skb, msg, len); |
Eric Dumazet | 954fba0 | 2012-06-12 19:30:21 +0000 | [diff] [blame] | 406 | skb_put(skb, len); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 407 | |
Arnaldo Carvalho de Melo | 4bedb45 | 2007-03-13 14:28:48 -0300 | [diff] [blame] | 408 | skb_push(skb, sizeof(*udph)); |
| 409 | skb_reset_transport_header(skb); |
| 410 | udph = udp_hdr(skb); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 411 | udph->source = htons(np->local_port); |
| 412 | udph->dest = htons(np->remote_port); |
| 413 | udph->len = htons(udp_len); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 414 | |
Cong Wang | b3d936f | 2013-01-07 20:52:41 +0000 | [diff] [blame] | 415 | if (np->ipv6) { |
| 416 | udph->check = 0; |
| 417 | udph->check = csum_ipv6_magic(&np->local_ip.in6, |
| 418 | &np->remote_ip.in6, |
| 419 | udp_len, IPPROTO_UDP, |
| 420 | csum_partial(udph, udp_len, 0)); |
| 421 | if (udph->check == 0) |
| 422 | udph->check = CSUM_MANGLED_0; |
| 423 | |
| 424 | skb_push(skb, sizeof(*ip6h)); |
| 425 | skb_reset_network_header(skb); |
| 426 | ip6h = ipv6_hdr(skb); |
| 427 | |
| 428 | /* ip6h->version = 6; ip6h->priority = 0; */ |
| 429 | put_unaligned(0x60, (unsigned char *)ip6h); |
| 430 | ip6h->flow_lbl[0] = 0; |
| 431 | ip6h->flow_lbl[1] = 0; |
| 432 | ip6h->flow_lbl[2] = 0; |
| 433 | |
| 434 | ip6h->payload_len = htons(sizeof(struct udphdr) + len); |
| 435 | ip6h->nexthdr = IPPROTO_UDP; |
| 436 | ip6h->hop_limit = 32; |
| 437 | ip6h->saddr = np->local_ip.in6; |
| 438 | ip6h->daddr = np->remote_ip.in6; |
| 439 | |
| 440 | eth = (struct ethhdr *) skb_push(skb, ETH_HLEN); |
| 441 | skb_reset_mac_header(skb); |
| 442 | skb->protocol = eth->h_proto = htons(ETH_P_IPV6); |
| 443 | } else { |
Cong Wang | b7394d2 | 2013-01-07 20:52:39 +0000 | [diff] [blame] | 444 | udph->check = 0; |
| 445 | udph->check = csum_tcpudp_magic(np->local_ip.ip, |
| 446 | np->remote_ip.ip, |
| 447 | udp_len, IPPROTO_UDP, |
| 448 | csum_partial(udph, udp_len, 0)); |
| 449 | if (udph->check == 0) |
| 450 | udph->check = CSUM_MANGLED_0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 451 | |
Cong Wang | b7394d2 | 2013-01-07 20:52:39 +0000 | [diff] [blame] | 452 | skb_push(skb, sizeof(*iph)); |
| 453 | skb_reset_network_header(skb); |
| 454 | iph = ip_hdr(skb); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 455 | |
Cong Wang | b7394d2 | 2013-01-07 20:52:39 +0000 | [diff] [blame] | 456 | /* iph->version = 4; iph->ihl = 5; */ |
| 457 | put_unaligned(0x45, (unsigned char *)iph); |
| 458 | iph->tos = 0; |
| 459 | put_unaligned(htons(ip_len), &(iph->tot_len)); |
| 460 | iph->id = htons(atomic_inc_return(&ip_ident)); |
| 461 | iph->frag_off = 0; |
| 462 | iph->ttl = 64; |
| 463 | iph->protocol = IPPROTO_UDP; |
| 464 | iph->check = 0; |
| 465 | put_unaligned(np->local_ip.ip, &(iph->saddr)); |
| 466 | put_unaligned(np->remote_ip.ip, &(iph->daddr)); |
| 467 | iph->check = ip_fast_csum((unsigned char *)iph, iph->ihl); |
| 468 | |
| 469 | eth = (struct ethhdr *) skb_push(skb, ETH_HLEN); |
| 470 | skb_reset_mac_header(skb); |
| 471 | skb->protocol = eth->h_proto = htons(ETH_P_IP); |
| 472 | } |
| 473 | |
Stephen Hemminger | 0953864 | 2007-11-19 19:23:29 -0800 | [diff] [blame] | 474 | memcpy(eth->h_source, np->dev->dev_addr, ETH_ALEN); |
| 475 | memcpy(eth->h_dest, np->remote_mac, ETH_ALEN); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 476 | |
| 477 | skb->dev = np->dev; |
| 478 | |
| 479 | netpoll_send_skb(np, skb); |
| 480 | } |
Eric Dumazet | 9e34a5b | 2010-07-09 21:22:04 +0000 | [diff] [blame] | 481 | EXPORT_SYMBOL(netpoll_send_udp); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 482 | |
Cong Wang | b7394d2 | 2013-01-07 20:52:39 +0000 | [diff] [blame] | 483 | static void netpoll_neigh_reply(struct sk_buff *skb, struct netpoll_info *npinfo) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 484 | { |
Cong Wang | b3d936f | 2013-01-07 20:52:41 +0000 | [diff] [blame] | 485 | int size, type = ARPOP_REPLY; |
Al Viro | 252e3346 | 2006-11-14 20:48:11 -0800 | [diff] [blame] | 486 | __be32 sip, tip; |
Neil Horman | 47bbec0 | 2006-12-08 00:05:55 -0800 | [diff] [blame] | 487 | unsigned char *sha; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 488 | struct sk_buff *send_skb; |
Daniel Borkmann | 508e14b | 2010-01-12 14:27:30 +0000 | [diff] [blame] | 489 | struct netpoll *np, *tmp; |
| 490 | unsigned long flags; |
Herbert Xu | ae64194 | 2011-11-18 02:20:04 +0000 | [diff] [blame] | 491 | int hlen, tlen; |
Cong Wang | b7394d2 | 2013-01-07 20:52:39 +0000 | [diff] [blame] | 492 | int hits = 0, proto; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 493 | |
Daniel Borkmann | 508e14b | 2010-01-12 14:27:30 +0000 | [diff] [blame] | 494 | if (list_empty(&npinfo->rx_np)) |
| 495 | return; |
| 496 | |
| 497 | /* Before checking the packet, we do some early |
| 498 | inspection whether this is interesting at all */ |
| 499 | spin_lock_irqsave(&npinfo->rx_lock, flags); |
| 500 | list_for_each_entry_safe(np, tmp, &npinfo->rx_np, rx) { |
| 501 | if (np->dev == skb->dev) |
| 502 | hits++; |
| 503 | } |
| 504 | spin_unlock_irqrestore(&npinfo->rx_lock, flags); |
| 505 | |
| 506 | /* No netpoll struct is using this dev */ |
| 507 | if (!hits) |
Jeff Moyer | 115c1d6 | 2005-06-22 22:05:31 -0700 | [diff] [blame] | 508 | return; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 509 | |
Cong Wang | b7394d2 | 2013-01-07 20:52:39 +0000 | [diff] [blame] | 510 | proto = ntohs(eth_hdr(skb)->h_proto); |
| 511 | if (proto == ETH_P_IP) { |
Cong Wang | b3d936f | 2013-01-07 20:52:41 +0000 | [diff] [blame] | 512 | struct arphdr *arp; |
| 513 | unsigned char *arp_ptr; |
Cong Wang | b7394d2 | 2013-01-07 20:52:39 +0000 | [diff] [blame] | 514 | /* No arp on this interface */ |
| 515 | if (skb->dev->flags & IFF_NOARP) |
| 516 | return; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 517 | |
Cong Wang | b7394d2 | 2013-01-07 20:52:39 +0000 | [diff] [blame] | 518 | if (!pskb_may_pull(skb, arp_hdr_len(skb->dev))) |
| 519 | return; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 520 | |
Cong Wang | b7394d2 | 2013-01-07 20:52:39 +0000 | [diff] [blame] | 521 | skb_reset_network_header(skb); |
| 522 | skb_reset_transport_header(skb); |
| 523 | arp = arp_hdr(skb); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 524 | |
Cong Wang | b7394d2 | 2013-01-07 20:52:39 +0000 | [diff] [blame] | 525 | if ((arp->ar_hrd != htons(ARPHRD_ETHER) && |
| 526 | arp->ar_hrd != htons(ARPHRD_IEEE802)) || |
| 527 | arp->ar_pro != htons(ETH_P_IP) || |
| 528 | arp->ar_op != htons(ARPOP_REQUEST)) |
| 529 | return; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 530 | |
Cong Wang | b7394d2 | 2013-01-07 20:52:39 +0000 | [diff] [blame] | 531 | arp_ptr = (unsigned char *)(arp+1); |
| 532 | /* save the location of the src hw addr */ |
| 533 | sha = arp_ptr; |
| 534 | arp_ptr += skb->dev->addr_len; |
| 535 | memcpy(&sip, arp_ptr, 4); |
Daniel Borkmann | 508e14b | 2010-01-12 14:27:30 +0000 | [diff] [blame] | 536 | arp_ptr += 4; |
Cong Wang | b7394d2 | 2013-01-07 20:52:39 +0000 | [diff] [blame] | 537 | /* If we actually cared about dst hw addr, |
| 538 | it would get copied here */ |
| 539 | arp_ptr += skb->dev->addr_len; |
| 540 | memcpy(&tip, arp_ptr, 4); |
Daniel Borkmann | 508e14b | 2010-01-12 14:27:30 +0000 | [diff] [blame] | 541 | |
Cong Wang | b7394d2 | 2013-01-07 20:52:39 +0000 | [diff] [blame] | 542 | /* Should we ignore arp? */ |
| 543 | if (ipv4_is_loopback(tip) || ipv4_is_multicast(tip)) |
| 544 | return; |
Daniel Borkmann | 508e14b | 2010-01-12 14:27:30 +0000 | [diff] [blame] | 545 | |
Cong Wang | b7394d2 | 2013-01-07 20:52:39 +0000 | [diff] [blame] | 546 | size = arp_hdr_len(skb->dev); |
| 547 | |
| 548 | spin_lock_irqsave(&npinfo->rx_lock, flags); |
| 549 | list_for_each_entry_safe(np, tmp, &npinfo->rx_np, rx) { |
| 550 | if (tip != np->local_ip.ip) |
| 551 | continue; |
| 552 | |
| 553 | hlen = LL_RESERVED_SPACE(np->dev); |
| 554 | tlen = np->dev->needed_tailroom; |
| 555 | send_skb = find_skb(np, size + hlen + tlen, hlen); |
| 556 | if (!send_skb) |
| 557 | continue; |
| 558 | |
| 559 | skb_reset_network_header(send_skb); |
| 560 | arp = (struct arphdr *) skb_put(send_skb, size); |
| 561 | send_skb->dev = skb->dev; |
| 562 | send_skb->protocol = htons(ETH_P_ARP); |
| 563 | |
| 564 | /* Fill the device header for the ARP frame */ |
Cong Wang | b3d936f | 2013-01-07 20:52:41 +0000 | [diff] [blame] | 565 | if (dev_hard_header(send_skb, skb->dev, ETH_P_ARP, |
Cong Wang | b7394d2 | 2013-01-07 20:52:39 +0000 | [diff] [blame] | 566 | sha, np->dev->dev_addr, |
| 567 | send_skb->len) < 0) { |
| 568 | kfree_skb(send_skb); |
| 569 | continue; |
| 570 | } |
| 571 | |
| 572 | /* |
| 573 | * Fill out the arp protocol part. |
| 574 | * |
| 575 | * we only support ethernet device type, |
| 576 | * which (according to RFC 1390) should |
| 577 | * always equal 1 (Ethernet). |
| 578 | */ |
| 579 | |
| 580 | arp->ar_hrd = htons(np->dev->type); |
| 581 | arp->ar_pro = htons(ETH_P_IP); |
| 582 | arp->ar_hln = np->dev->addr_len; |
| 583 | arp->ar_pln = 4; |
| 584 | arp->ar_op = htons(type); |
| 585 | |
| 586 | arp_ptr = (unsigned char *)(arp + 1); |
| 587 | memcpy(arp_ptr, np->dev->dev_addr, np->dev->addr_len); |
| 588 | arp_ptr += np->dev->addr_len; |
| 589 | memcpy(arp_ptr, &tip, 4); |
| 590 | arp_ptr += 4; |
| 591 | memcpy(arp_ptr, sha, np->dev->addr_len); |
| 592 | arp_ptr += np->dev->addr_len; |
| 593 | memcpy(arp_ptr, &sip, 4); |
| 594 | |
| 595 | netpoll_send_skb(np, send_skb); |
| 596 | |
| 597 | /* If there are several rx_hooks for the same address, |
| 598 | we're fine by sending a single reply */ |
| 599 | break; |
| 600 | } |
| 601 | spin_unlock_irqrestore(&npinfo->rx_lock, flags); |
Cong Wang | b3d936f | 2013-01-07 20:52:41 +0000 | [diff] [blame] | 602 | } else if( proto == ETH_P_IPV6) { |
| 603 | #if IS_ENABLED(CONFIG_IPV6) |
| 604 | struct nd_msg *msg; |
| 605 | u8 *lladdr = NULL; |
| 606 | struct ipv6hdr *hdr; |
| 607 | struct icmp6hdr *icmp6h; |
| 608 | const struct in6_addr *saddr; |
| 609 | const struct in6_addr *daddr; |
| 610 | struct inet6_dev *in6_dev = NULL; |
| 611 | struct in6_addr *target; |
| 612 | |
| 613 | in6_dev = in6_dev_get(skb->dev); |
| 614 | if (!in6_dev || !in6_dev->cnf.accept_ra) |
| 615 | return; |
| 616 | |
| 617 | if (!pskb_may_pull(skb, skb->len)) |
| 618 | return; |
| 619 | |
| 620 | msg = (struct nd_msg *)skb_transport_header(skb); |
| 621 | |
| 622 | __skb_push(skb, skb->data - skb_transport_header(skb)); |
| 623 | |
| 624 | if (ipv6_hdr(skb)->hop_limit != 255) |
| 625 | return; |
| 626 | if (msg->icmph.icmp6_code != 0) |
| 627 | return; |
| 628 | if (msg->icmph.icmp6_type != NDISC_NEIGHBOUR_SOLICITATION) |
| 629 | return; |
| 630 | |
| 631 | saddr = &ipv6_hdr(skb)->saddr; |
| 632 | daddr = &ipv6_hdr(skb)->daddr; |
| 633 | |
| 634 | size = sizeof(struct icmp6hdr) + sizeof(struct in6_addr); |
| 635 | |
| 636 | spin_lock_irqsave(&npinfo->rx_lock, flags); |
| 637 | list_for_each_entry_safe(np, tmp, &npinfo->rx_np, rx) { |
| 638 | if (memcmp(daddr, &np->local_ip, sizeof(*daddr))) |
| 639 | continue; |
| 640 | |
| 641 | hlen = LL_RESERVED_SPACE(np->dev); |
| 642 | tlen = np->dev->needed_tailroom; |
| 643 | send_skb = find_skb(np, size + hlen + tlen, hlen); |
| 644 | if (!send_skb) |
| 645 | continue; |
| 646 | |
| 647 | send_skb->protocol = htons(ETH_P_IPV6); |
| 648 | send_skb->dev = skb->dev; |
| 649 | |
| 650 | skb_reset_network_header(send_skb); |
| 651 | skb_put(send_skb, sizeof(struct ipv6hdr)); |
| 652 | hdr = ipv6_hdr(send_skb); |
| 653 | |
| 654 | *(__be32*)hdr = htonl(0x60000000); |
| 655 | |
| 656 | hdr->payload_len = htons(size); |
| 657 | hdr->nexthdr = IPPROTO_ICMPV6; |
| 658 | hdr->hop_limit = 255; |
| 659 | hdr->saddr = *saddr; |
| 660 | hdr->daddr = *daddr; |
| 661 | |
| 662 | send_skb->transport_header = send_skb->tail; |
| 663 | skb_put(send_skb, size); |
| 664 | |
| 665 | icmp6h = (struct icmp6hdr *)skb_transport_header(skb); |
| 666 | icmp6h->icmp6_type = NDISC_NEIGHBOUR_ADVERTISEMENT; |
| 667 | icmp6h->icmp6_router = 0; |
| 668 | icmp6h->icmp6_solicited = 1; |
| 669 | target = (struct in6_addr *)skb_transport_header(send_skb) + sizeof(struct icmp6hdr); |
| 670 | *target = msg->target; |
| 671 | icmp6h->icmp6_cksum = csum_ipv6_magic(saddr, daddr, size, |
| 672 | IPPROTO_ICMPV6, |
| 673 | csum_partial(icmp6h, |
| 674 | size, 0)); |
| 675 | |
| 676 | if (dev_hard_header(send_skb, skb->dev, ETH_P_IPV6, |
| 677 | lladdr, np->dev->dev_addr, |
| 678 | send_skb->len) < 0) { |
| 679 | kfree_skb(send_skb); |
| 680 | continue; |
| 681 | } |
| 682 | |
| 683 | netpoll_send_skb(np, send_skb); |
| 684 | |
| 685 | /* If there are several rx_hooks for the same address, |
| 686 | we're fine by sending a single reply */ |
| 687 | break; |
| 688 | } |
| 689 | spin_unlock_irqrestore(&npinfo->rx_lock, flags); |
| 690 | #endif |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 691 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 692 | } |
| 693 | |
Cong Wang | b3d936f | 2013-01-07 20:52:41 +0000 | [diff] [blame] | 694 | static bool pkt_is_ns(struct sk_buff *skb) |
| 695 | { |
| 696 | struct nd_msg *msg; |
| 697 | struct ipv6hdr *hdr; |
| 698 | |
| 699 | if (skb->protocol != htons(ETH_P_ARP)) |
| 700 | return false; |
| 701 | if (!pskb_may_pull(skb, sizeof(struct ipv6hdr) + sizeof(struct nd_msg))) |
| 702 | return false; |
| 703 | |
| 704 | msg = (struct nd_msg *)skb_transport_header(skb); |
| 705 | __skb_push(skb, skb->data - skb_transport_header(skb)); |
| 706 | hdr = ipv6_hdr(skb); |
| 707 | |
| 708 | if (hdr->nexthdr != IPPROTO_ICMPV6) |
| 709 | return false; |
| 710 | if (hdr->hop_limit != 255) |
| 711 | return false; |
| 712 | if (msg->icmph.icmp6_code != 0) |
| 713 | return false; |
| 714 | if (msg->icmph.icmp6_type != NDISC_NEIGHBOUR_SOLICITATION) |
| 715 | return false; |
| 716 | |
| 717 | return true; |
| 718 | } |
| 719 | |
Amerigo Wang | 57c5d46 | 2012-08-10 01:24:40 +0000 | [diff] [blame] | 720 | int __netpoll_rx(struct sk_buff *skb, struct netpoll_info *npinfo) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 721 | { |
| 722 | int proto, len, ulen; |
Daniel Borkmann | 508e14b | 2010-01-12 14:27:30 +0000 | [diff] [blame] | 723 | int hits = 0; |
Eric Dumazet | b71d1d4 | 2011-04-22 04:53:02 +0000 | [diff] [blame] | 724 | const struct iphdr *iph; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 725 | struct udphdr *uh; |
Daniel Borkmann | 508e14b | 2010-01-12 14:27:30 +0000 | [diff] [blame] | 726 | struct netpoll *np, *tmp; |
Neil Horman | 068c6e9 | 2006-06-26 00:04:27 -0700 | [diff] [blame] | 727 | |
Daniel Borkmann | 508e14b | 2010-01-12 14:27:30 +0000 | [diff] [blame] | 728 | if (list_empty(&npinfo->rx_np)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 729 | goto out; |
Daniel Borkmann | 508e14b | 2010-01-12 14:27:30 +0000 | [diff] [blame] | 730 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 731 | if (skb->dev->type != ARPHRD_ETHER) |
| 732 | goto out; |
| 733 | |
David S. Miller | d9452e9 | 2008-03-04 12:28:49 -0800 | [diff] [blame] | 734 | /* check if netpoll clients need ARP */ |
Cong Wang | b3d936f | 2013-01-07 20:52:41 +0000 | [diff] [blame] | 735 | if (skb->protocol == htons(ETH_P_ARP) && atomic_read(&trapped)) { |
| 736 | skb_queue_tail(&npinfo->neigh_tx, skb); |
| 737 | return 1; |
| 738 | } else if (pkt_is_ns(skb) && atomic_read(&trapped)) { |
Cong Wang | b7394d2 | 2013-01-07 20:52:39 +0000 | [diff] [blame] | 739 | skb_queue_tail(&npinfo->neigh_tx, skb); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 740 | return 1; |
| 741 | } |
| 742 | |
Amerigo Wang | 689971b | 2012-08-10 01:24:49 +0000 | [diff] [blame] | 743 | if (skb->protocol == cpu_to_be16(ETH_P_8021Q)) { |
| 744 | skb = vlan_untag(skb); |
| 745 | if (unlikely(!skb)) |
| 746 | goto out; |
| 747 | } |
| 748 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 749 | proto = ntohs(eth_hdr(skb)->h_proto); |
Cong Wang | b7394d2 | 2013-01-07 20:52:39 +0000 | [diff] [blame] | 750 | if (proto != ETH_P_IP && proto != ETH_P_IPV6) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 751 | goto out; |
| 752 | if (skb->pkt_type == PACKET_OTHERHOST) |
| 753 | goto out; |
| 754 | if (skb_shared(skb)) |
| 755 | goto out; |
| 756 | |
Cong Wang | b7394d2 | 2013-01-07 20:52:39 +0000 | [diff] [blame] | 757 | if (proto == ETH_P_IP) { |
| 758 | if (!pskb_may_pull(skb, sizeof(struct iphdr))) |
| 759 | goto out; |
| 760 | iph = (struct iphdr *)skb->data; |
| 761 | if (iph->ihl < 5 || iph->version != 4) |
| 762 | goto out; |
| 763 | if (!pskb_may_pull(skb, iph->ihl*4)) |
| 764 | goto out; |
| 765 | iph = (struct iphdr *)skb->data; |
| 766 | if (ip_fast_csum((u8 *)iph, iph->ihl) != 0) |
| 767 | goto out; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 768 | |
Cong Wang | b7394d2 | 2013-01-07 20:52:39 +0000 | [diff] [blame] | 769 | len = ntohs(iph->tot_len); |
| 770 | if (skb->len < len || len < iph->ihl*4) |
| 771 | goto out; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 772 | |
Cong Wang | b7394d2 | 2013-01-07 20:52:39 +0000 | [diff] [blame] | 773 | /* |
| 774 | * Our transport medium may have padded the buffer out. |
| 775 | * Now We trim to the true length of the frame. |
| 776 | */ |
| 777 | if (pskb_trim_rcsum(skb, len)) |
| 778 | goto out; |
Aubrey.Li | 5e7d7fa | 2007-04-17 12:40:20 -0700 | [diff] [blame] | 779 | |
Cong Wang | b7394d2 | 2013-01-07 20:52:39 +0000 | [diff] [blame] | 780 | iph = (struct iphdr *)skb->data; |
| 781 | if (iph->protocol != IPPROTO_UDP) |
| 782 | goto out; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 783 | |
Cong Wang | b7394d2 | 2013-01-07 20:52:39 +0000 | [diff] [blame] | 784 | len -= iph->ihl*4; |
| 785 | uh = (struct udphdr *)(((char *)iph) + iph->ihl*4); |
| 786 | ulen = ntohs(uh->len); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 787 | |
Cong Wang | b7394d2 | 2013-01-07 20:52:39 +0000 | [diff] [blame] | 788 | if (ulen != len) |
| 789 | goto out; |
| 790 | if (checksum_udp(skb, uh, ulen, iph->saddr, iph->daddr)) |
| 791 | goto out; |
| 792 | list_for_each_entry_safe(np, tmp, &npinfo->rx_np, rx) { |
| 793 | if (np->local_ip.ip && np->local_ip.ip != iph->daddr) |
| 794 | continue; |
| 795 | if (np->remote_ip.ip && np->remote_ip.ip != iph->saddr) |
| 796 | continue; |
| 797 | if (np->local_port && np->local_port != ntohs(uh->dest)) |
| 798 | continue; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 799 | |
Cong Wang | b7394d2 | 2013-01-07 20:52:39 +0000 | [diff] [blame] | 800 | np->rx_hook(np, ntohs(uh->source), |
| 801 | (char *)(uh+1), |
| 802 | ulen - sizeof(struct udphdr)); |
| 803 | hits++; |
| 804 | } |
Cong Wang | b3d936f | 2013-01-07 20:52:41 +0000 | [diff] [blame] | 805 | } else { |
| 806 | #if IS_ENABLED(CONFIG_IPV6) |
| 807 | const struct ipv6hdr *ip6h; |
| 808 | |
| 809 | if (!pskb_may_pull(skb, sizeof(struct ipv6hdr))) |
| 810 | goto out; |
| 811 | ip6h = (struct ipv6hdr *)skb->data; |
| 812 | if (ip6h->version != 6) |
| 813 | goto out; |
| 814 | len = ntohs(ip6h->payload_len); |
| 815 | if (!len) |
| 816 | goto out; |
| 817 | if (len + sizeof(struct ipv6hdr) > skb->len) |
| 818 | goto out; |
| 819 | if (pskb_trim_rcsum(skb, len + sizeof(struct ipv6hdr))) |
| 820 | goto out; |
| 821 | ip6h = ipv6_hdr(skb); |
| 822 | if (!pskb_may_pull(skb, sizeof(struct udphdr))) |
| 823 | goto out; |
| 824 | uh = udp_hdr(skb); |
| 825 | ulen = ntohs(uh->len); |
| 826 | if (ulen != skb->len) |
| 827 | goto out; |
| 828 | if (udp6_csum_init(skb, uh, IPPROTO_UDP)) |
| 829 | goto out; |
| 830 | list_for_each_entry_safe(np, tmp, &npinfo->rx_np, rx) { |
| 831 | if (memcmp(&np->local_ip.in6, &ip6h->daddr, sizeof(struct in6_addr)) != 0) |
| 832 | continue; |
| 833 | if (memcmp(&np->remote_ip.in6, &ip6h->saddr, sizeof(struct in6_addr)) != 0) |
| 834 | continue; |
| 835 | if (np->local_port && np->local_port != ntohs(uh->dest)) |
| 836 | continue; |
| 837 | |
| 838 | np->rx_hook(np, ntohs(uh->source), |
| 839 | (char *)(uh+1), |
| 840 | ulen - sizeof(struct udphdr)); |
| 841 | hits++; |
| 842 | } |
| 843 | #endif |
Daniel Borkmann | 508e14b | 2010-01-12 14:27:30 +0000 | [diff] [blame] | 844 | } |
| 845 | |
| 846 | if (!hits) |
| 847 | goto out; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 848 | |
| 849 | kfree_skb(skb); |
| 850 | return 1; |
| 851 | |
| 852 | out: |
| 853 | if (atomic_read(&trapped)) { |
| 854 | kfree_skb(skb); |
| 855 | return 1; |
| 856 | } |
| 857 | |
| 858 | return 0; |
| 859 | } |
| 860 | |
Satyam Sharma | 0bcc181 | 2007-08-10 15:35:05 -0700 | [diff] [blame] | 861 | void netpoll_print_options(struct netpoll *np) |
| 862 | { |
Joe Perches | e6ec26935 | 2012-01-29 15:50:43 +0000 | [diff] [blame] | 863 | np_info(np, "local port %d\n", np->local_port); |
Cong Wang | b3d936f | 2013-01-07 20:52:41 +0000 | [diff] [blame] | 864 | if (np->ipv6) |
| 865 | np_info(np, "local IPv6 address %pI6c\n", &np->local_ip.in6); |
| 866 | else |
Cong Wang | b7394d2 | 2013-01-07 20:52:39 +0000 | [diff] [blame] | 867 | np_info(np, "local IPv4 address %pI4\n", &np->local_ip.ip); |
Joe Perches | e6ec26935 | 2012-01-29 15:50:43 +0000 | [diff] [blame] | 868 | np_info(np, "interface '%s'\n", np->dev_name); |
| 869 | np_info(np, "remote port %d\n", np->remote_port); |
Cong Wang | b3d936f | 2013-01-07 20:52:41 +0000 | [diff] [blame] | 870 | if (np->ipv6) |
| 871 | np_info(np, "remote IPv6 address %pI6c\n", &np->remote_ip.in6); |
| 872 | else |
Cong Wang | b7394d2 | 2013-01-07 20:52:39 +0000 | [diff] [blame] | 873 | np_info(np, "remote IPv4 address %pI4\n", &np->remote_ip.ip); |
Joe Perches | e6ec26935 | 2012-01-29 15:50:43 +0000 | [diff] [blame] | 874 | np_info(np, "remote ethernet address %pM\n", np->remote_mac); |
Satyam Sharma | 0bcc181 | 2007-08-10 15:35:05 -0700 | [diff] [blame] | 875 | } |
Eric Dumazet | 9e34a5b | 2010-07-09 21:22:04 +0000 | [diff] [blame] | 876 | EXPORT_SYMBOL(netpoll_print_options); |
Satyam Sharma | 0bcc181 | 2007-08-10 15:35:05 -0700 | [diff] [blame] | 877 | |
Cong Wang | b7394d2 | 2013-01-07 20:52:39 +0000 | [diff] [blame] | 878 | static int netpoll_parse_ip_addr(const char *str, union inet_addr *addr) |
| 879 | { |
| 880 | const char *end; |
| 881 | |
| 882 | if (!strchr(str, ':') && |
| 883 | in4_pton(str, -1, (void *)addr, -1, &end) > 0) { |
| 884 | if (!*end) |
| 885 | return 0; |
| 886 | } |
| 887 | if (in6_pton(str, -1, addr->in6.s6_addr, -1, &end) > 0) { |
| 888 | #if IS_ENABLED(CONFIG_IPV6) |
| 889 | if (!*end) |
| 890 | return 1; |
| 891 | #else |
| 892 | return -1; |
| 893 | #endif |
| 894 | } |
| 895 | return -1; |
| 896 | } |
| 897 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 898 | int netpoll_parse_options(struct netpoll *np, char *opt) |
| 899 | { |
| 900 | char *cur=opt, *delim; |
Cong Wang | b7394d2 | 2013-01-07 20:52:39 +0000 | [diff] [blame] | 901 | int ipv6; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 902 | |
David S. Miller | c68b907 | 2006-11-14 20:40:49 -0800 | [diff] [blame] | 903 | if (*cur != '@') { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 904 | if ((delim = strchr(cur, '@')) == NULL) |
| 905 | goto parse_failed; |
David S. Miller | c68b907 | 2006-11-14 20:40:49 -0800 | [diff] [blame] | 906 | *delim = 0; |
Abhijit Pawar | 4b5511e | 2012-12-09 23:12:28 +0000 | [diff] [blame] | 907 | if (kstrtou16(cur, 10, &np->local_port)) |
| 908 | goto parse_failed; |
David S. Miller | c68b907 | 2006-11-14 20:40:49 -0800 | [diff] [blame] | 909 | cur = delim; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 910 | } |
| 911 | cur++; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 912 | |
David S. Miller | c68b907 | 2006-11-14 20:40:49 -0800 | [diff] [blame] | 913 | if (*cur != '/') { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 914 | if ((delim = strchr(cur, '/')) == NULL) |
| 915 | goto parse_failed; |
David S. Miller | c68b907 | 2006-11-14 20:40:49 -0800 | [diff] [blame] | 916 | *delim = 0; |
Cong Wang | b7394d2 | 2013-01-07 20:52:39 +0000 | [diff] [blame] | 917 | ipv6 = netpoll_parse_ip_addr(cur, &np->local_ip); |
| 918 | if (ipv6 < 0) |
| 919 | goto parse_failed; |
| 920 | else |
| 921 | np->ipv6 = (bool)ipv6; |
David S. Miller | c68b907 | 2006-11-14 20:40:49 -0800 | [diff] [blame] | 922 | cur = delim; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 923 | } |
| 924 | cur++; |
| 925 | |
David S. Miller | c68b907 | 2006-11-14 20:40:49 -0800 | [diff] [blame] | 926 | if (*cur != ',') { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 927 | /* parse out dev name */ |
| 928 | if ((delim = strchr(cur, ',')) == NULL) |
| 929 | goto parse_failed; |
David S. Miller | c68b907 | 2006-11-14 20:40:49 -0800 | [diff] [blame] | 930 | *delim = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 931 | strlcpy(np->dev_name, cur, sizeof(np->dev_name)); |
David S. Miller | c68b907 | 2006-11-14 20:40:49 -0800 | [diff] [blame] | 932 | cur = delim; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 933 | } |
| 934 | cur++; |
| 935 | |
David S. Miller | c68b907 | 2006-11-14 20:40:49 -0800 | [diff] [blame] | 936 | if (*cur != '@') { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 937 | /* dst port */ |
| 938 | if ((delim = strchr(cur, '@')) == NULL) |
| 939 | goto parse_failed; |
David S. Miller | c68b907 | 2006-11-14 20:40:49 -0800 | [diff] [blame] | 940 | *delim = 0; |
Amerigo Wang | 5fc05f8 | 2010-03-21 22:59:58 +0000 | [diff] [blame] | 941 | if (*cur == ' ' || *cur == '\t') |
Joe Perches | e6ec26935 | 2012-01-29 15:50:43 +0000 | [diff] [blame] | 942 | np_info(np, "warning: whitespace is not allowed\n"); |
Abhijit Pawar | 4b5511e | 2012-12-09 23:12:28 +0000 | [diff] [blame] | 943 | if (kstrtou16(cur, 10, &np->remote_port)) |
| 944 | goto parse_failed; |
David S. Miller | c68b907 | 2006-11-14 20:40:49 -0800 | [diff] [blame] | 945 | cur = delim; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 946 | } |
| 947 | cur++; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 948 | |
| 949 | /* dst ip */ |
| 950 | if ((delim = strchr(cur, '/')) == NULL) |
| 951 | goto parse_failed; |
David S. Miller | c68b907 | 2006-11-14 20:40:49 -0800 | [diff] [blame] | 952 | *delim = 0; |
Cong Wang | b7394d2 | 2013-01-07 20:52:39 +0000 | [diff] [blame] | 953 | ipv6 = netpoll_parse_ip_addr(cur, &np->remote_ip); |
| 954 | if (ipv6 < 0) |
| 955 | goto parse_failed; |
| 956 | else if (np->ipv6 != (bool)ipv6) |
| 957 | goto parse_failed; |
| 958 | else |
| 959 | np->ipv6 = (bool)ipv6; |
David S. Miller | c68b907 | 2006-11-14 20:40:49 -0800 | [diff] [blame] | 960 | cur = delim + 1; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 961 | |
David S. Miller | c68b907 | 2006-11-14 20:40:49 -0800 | [diff] [blame] | 962 | if (*cur != 0) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 963 | /* MAC address */ |
Alexey Dobriyan | 4940fc8 | 2011-05-07 23:00:07 +0000 | [diff] [blame] | 964 | if (!mac_pton(cur, np->remote_mac)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 965 | goto parse_failed; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 966 | } |
| 967 | |
Satyam Sharma | 0bcc181 | 2007-08-10 15:35:05 -0700 | [diff] [blame] | 968 | netpoll_print_options(np); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 969 | |
| 970 | return 0; |
| 971 | |
| 972 | parse_failed: |
Joe Perches | e6ec26935 | 2012-01-29 15:50:43 +0000 | [diff] [blame] | 973 | np_info(np, "couldn't parse config at '%s'!\n", cur); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 974 | return -1; |
| 975 | } |
Eric Dumazet | 9e34a5b | 2010-07-09 21:22:04 +0000 | [diff] [blame] | 976 | EXPORT_SYMBOL(netpoll_parse_options); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 977 | |
Amerigo Wang | 47be03a2 | 2012-08-10 01:24:37 +0000 | [diff] [blame] | 978 | int __netpoll_setup(struct netpoll *np, struct net_device *ndev, gfp_t gfp) |
Herbert Xu | 8fdd95e | 2010-06-10 16:12:48 +0000 | [diff] [blame] | 979 | { |
Herbert Xu | 8fdd95e | 2010-06-10 16:12:48 +0000 | [diff] [blame] | 980 | struct netpoll_info *npinfo; |
| 981 | const struct net_device_ops *ops; |
| 982 | unsigned long flags; |
| 983 | int err; |
| 984 | |
Jiri Pirko | 30fdd8a0 | 2012-07-17 05:22:35 +0000 | [diff] [blame] | 985 | np->dev = ndev; |
| 986 | strlcpy(np->dev_name, ndev->name, IFNAMSIZ); |
| 987 | |
Herbert Xu | 8fdd95e | 2010-06-10 16:12:48 +0000 | [diff] [blame] | 988 | if ((ndev->priv_flags & IFF_DISABLE_NETPOLL) || |
| 989 | !ndev->netdev_ops->ndo_poll_controller) { |
Joe Perches | e6ec26935 | 2012-01-29 15:50:43 +0000 | [diff] [blame] | 990 | np_err(np, "%s doesn't support polling, aborting\n", |
| 991 | np->dev_name); |
Herbert Xu | 8fdd95e | 2010-06-10 16:12:48 +0000 | [diff] [blame] | 992 | err = -ENOTSUPP; |
| 993 | goto out; |
| 994 | } |
| 995 | |
| 996 | if (!ndev->npinfo) { |
Amerigo Wang | 47be03a2 | 2012-08-10 01:24:37 +0000 | [diff] [blame] | 997 | npinfo = kmalloc(sizeof(*npinfo), gfp); |
Herbert Xu | 8fdd95e | 2010-06-10 16:12:48 +0000 | [diff] [blame] | 998 | if (!npinfo) { |
| 999 | err = -ENOMEM; |
| 1000 | goto out; |
| 1001 | } |
| 1002 | |
| 1003 | npinfo->rx_flags = 0; |
| 1004 | INIT_LIST_HEAD(&npinfo->rx_np); |
| 1005 | |
| 1006 | spin_lock_init(&npinfo->rx_lock); |
Cong Wang | b7394d2 | 2013-01-07 20:52:39 +0000 | [diff] [blame] | 1007 | skb_queue_head_init(&npinfo->neigh_tx); |
Herbert Xu | 8fdd95e | 2010-06-10 16:12:48 +0000 | [diff] [blame] | 1008 | skb_queue_head_init(&npinfo->txq); |
| 1009 | INIT_DELAYED_WORK(&npinfo->tx_work, queue_process); |
| 1010 | |
| 1011 | atomic_set(&npinfo->refcnt, 1); |
| 1012 | |
| 1013 | ops = np->dev->netdev_ops; |
| 1014 | if (ops->ndo_netpoll_setup) { |
Amerigo Wang | 47be03a2 | 2012-08-10 01:24:37 +0000 | [diff] [blame] | 1015 | err = ops->ndo_netpoll_setup(ndev, npinfo, gfp); |
Herbert Xu | 8fdd95e | 2010-06-10 16:12:48 +0000 | [diff] [blame] | 1016 | if (err) |
| 1017 | goto free_npinfo; |
| 1018 | } |
| 1019 | } else { |
| 1020 | npinfo = ndev->npinfo; |
| 1021 | atomic_inc(&npinfo->refcnt); |
| 1022 | } |
| 1023 | |
| 1024 | npinfo->netpoll = np; |
| 1025 | |
| 1026 | if (np->rx_hook) { |
| 1027 | spin_lock_irqsave(&npinfo->rx_lock, flags); |
| 1028 | npinfo->rx_flags |= NETPOLL_RX_ENABLED; |
| 1029 | list_add_tail(&np->rx, &npinfo->rx_np); |
| 1030 | spin_unlock_irqrestore(&npinfo->rx_lock, flags); |
| 1031 | } |
| 1032 | |
| 1033 | /* last thing to do is link it to the net device structure */ |
Eric Dumazet | cf778b0 | 2012-01-12 04:41:32 +0000 | [diff] [blame] | 1034 | rcu_assign_pointer(ndev->npinfo, npinfo); |
Herbert Xu | 8fdd95e | 2010-06-10 16:12:48 +0000 | [diff] [blame] | 1035 | |
| 1036 | return 0; |
| 1037 | |
| 1038 | free_npinfo: |
| 1039 | kfree(npinfo); |
| 1040 | out: |
| 1041 | return err; |
| 1042 | } |
| 1043 | EXPORT_SYMBOL_GPL(__netpoll_setup); |
| 1044 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1045 | int netpoll_setup(struct netpoll *np) |
| 1046 | { |
| 1047 | struct net_device *ndev = NULL; |
| 1048 | struct in_device *in_dev; |
Stephen Hemminger | b41848b | 2006-10-26 15:46:52 -0700 | [diff] [blame] | 1049 | int err; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1050 | |
Cong Wang | f92d318 | 2013-01-14 23:34:06 +0000 | [diff] [blame^] | 1051 | rtnl_lock(); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1052 | if (np->dev_name) |
Cong Wang | f92d318 | 2013-01-14 23:34:06 +0000 | [diff] [blame^] | 1053 | ndev = __dev_get_by_name(&init_net, np->dev_name); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1054 | if (!ndev) { |
Joe Perches | e6ec26935 | 2012-01-29 15:50:43 +0000 | [diff] [blame] | 1055 | np_err(np, "%s doesn't exist, aborting\n", np->dev_name); |
Cong Wang | f92d318 | 2013-01-14 23:34:06 +0000 | [diff] [blame^] | 1056 | err = -ENODEV; |
| 1057 | goto unlock; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1058 | } |
| 1059 | |
Jiri Pirko | 49bd8fb | 2013-01-03 22:48:55 +0000 | [diff] [blame] | 1060 | if (netdev_master_upper_dev_get(ndev)) { |
Joe Perches | e6ec26935 | 2012-01-29 15:50:43 +0000 | [diff] [blame] | 1061 | np_err(np, "%s is a slave device, aborting\n", np->dev_name); |
Dan Carpenter | 83fe32d | 2011-06-11 18:55:22 -0700 | [diff] [blame] | 1062 | err = -EBUSY; |
| 1063 | goto put; |
WANG Cong | 0c1ad04 | 2011-06-09 00:28:13 -0700 | [diff] [blame] | 1064 | } |
| 1065 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1066 | if (!netif_running(ndev)) { |
| 1067 | unsigned long atmost, atleast; |
| 1068 | |
Joe Perches | e6ec26935 | 2012-01-29 15:50:43 +0000 | [diff] [blame] | 1069 | np_info(np, "device %s not up yet, forcing it\n", np->dev_name); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1070 | |
Stephen Hemminger | b41848b | 2006-10-26 15:46:52 -0700 | [diff] [blame] | 1071 | err = dev_open(ndev); |
Stephen Hemminger | b41848b | 2006-10-26 15:46:52 -0700 | [diff] [blame] | 1072 | |
| 1073 | if (err) { |
Joe Perches | e6ec26935 | 2012-01-29 15:50:43 +0000 | [diff] [blame] | 1074 | np_err(np, "failed to open %s\n", ndev->name); |
Herbert Xu | dbaa154 | 2010-06-10 16:12:46 +0000 | [diff] [blame] | 1075 | goto put; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1076 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1077 | |
Cong Wang | f92d318 | 2013-01-14 23:34:06 +0000 | [diff] [blame^] | 1078 | rtnl_unlock(); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1079 | atleast = jiffies + HZ/10; |
Anton Vorontsov | bff3877 | 2009-07-08 11:10:56 -0700 | [diff] [blame] | 1080 | atmost = jiffies + carrier_timeout * HZ; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1081 | while (!netif_carrier_ok(ndev)) { |
| 1082 | if (time_after(jiffies, atmost)) { |
Joe Perches | e6ec26935 | 2012-01-29 15:50:43 +0000 | [diff] [blame] | 1083 | np_notice(np, "timeout waiting for carrier\n"); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1084 | break; |
| 1085 | } |
Anton Vorontsov | 1b614fb | 2009-07-08 20:09:44 -0700 | [diff] [blame] | 1086 | msleep(1); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1087 | } |
| 1088 | |
| 1089 | /* If carrier appears to come up instantly, we don't |
| 1090 | * trust it and pause so that we don't pump all our |
| 1091 | * queued console messages into the bitbucket. |
| 1092 | */ |
| 1093 | |
| 1094 | if (time_before(jiffies, atleast)) { |
Joe Perches | e6ec26935 | 2012-01-29 15:50:43 +0000 | [diff] [blame] | 1095 | np_notice(np, "carrier detect appears untrustworthy, waiting 4 seconds\n"); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1096 | msleep(4000); |
| 1097 | } |
Cong Wang | f92d318 | 2013-01-14 23:34:06 +0000 | [diff] [blame^] | 1098 | rtnl_lock(); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1099 | } |
| 1100 | |
Cong Wang | b7394d2 | 2013-01-07 20:52:39 +0000 | [diff] [blame] | 1101 | if (!np->local_ip.ip) { |
| 1102 | if (!np->ipv6) { |
Cong Wang | f92d318 | 2013-01-14 23:34:06 +0000 | [diff] [blame^] | 1103 | in_dev = __in_dev_get_rtnl(ndev); |
Cong Wang | b7394d2 | 2013-01-07 20:52:39 +0000 | [diff] [blame] | 1104 | |
| 1105 | if (!in_dev || !in_dev->ifa_list) { |
Cong Wang | b7394d2 | 2013-01-07 20:52:39 +0000 | [diff] [blame] | 1106 | np_err(np, "no IP address for %s, aborting\n", |
| 1107 | np->dev_name); |
| 1108 | err = -EDESTADDRREQ; |
| 1109 | goto put; |
| 1110 | } |
| 1111 | |
| 1112 | np->local_ip.ip = in_dev->ifa_list->ifa_local; |
Cong Wang | b7394d2 | 2013-01-07 20:52:39 +0000 | [diff] [blame] | 1113 | np_info(np, "local IP %pI4\n", &np->local_ip.ip); |
Cong Wang | b3d936f | 2013-01-07 20:52:41 +0000 | [diff] [blame] | 1114 | } else { |
| 1115 | #if IS_ENABLED(CONFIG_IPV6) |
| 1116 | struct inet6_dev *idev; |
| 1117 | |
| 1118 | err = -EDESTADDRREQ; |
Cong Wang | b3d936f | 2013-01-07 20:52:41 +0000 | [diff] [blame] | 1119 | idev = __in6_dev_get(ndev); |
| 1120 | if (idev) { |
| 1121 | struct inet6_ifaddr *ifp; |
| 1122 | |
| 1123 | read_lock_bh(&idev->lock); |
| 1124 | list_for_each_entry(ifp, &idev->addr_list, if_list) { |
| 1125 | if (ipv6_addr_type(&ifp->addr) & IPV6_ADDR_LINKLOCAL) |
| 1126 | continue; |
| 1127 | np->local_ip.in6 = ifp->addr; |
| 1128 | err = 0; |
| 1129 | break; |
| 1130 | } |
| 1131 | read_unlock_bh(&idev->lock); |
| 1132 | } |
Cong Wang | b3d936f | 2013-01-07 20:52:41 +0000 | [diff] [blame] | 1133 | if (err) { |
| 1134 | np_err(np, "no IPv6 address for %s, aborting\n", |
| 1135 | np->dev_name); |
| 1136 | goto put; |
| 1137 | } else |
| 1138 | np_info(np, "local IPv6 %pI6c\n", &np->local_ip.in6); |
| 1139 | #else |
| 1140 | np_err(np, "IPv6 is not supported %s, aborting\n", |
| 1141 | np->dev_name); |
| 1142 | goto put; |
| 1143 | #endif |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1144 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1145 | } |
| 1146 | |
Herbert Xu | dbaa154 | 2010-06-10 16:12:46 +0000 | [diff] [blame] | 1147 | /* fill up the skb queue */ |
| 1148 | refill_skbs(); |
| 1149 | |
Amerigo Wang | 47be03a2 | 2012-08-10 01:24:37 +0000 | [diff] [blame] | 1150 | err = __netpoll_setup(np, ndev, GFP_KERNEL); |
Herbert Xu | 8fdd95e | 2010-06-10 16:12:48 +0000 | [diff] [blame] | 1151 | if (err) |
| 1152 | goto put; |
| 1153 | |
Cong Wang | f92d318 | 2013-01-14 23:34:06 +0000 | [diff] [blame^] | 1154 | rtnl_unlock(); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1155 | return 0; |
| 1156 | |
Jiri Slaby | 21edbb2 | 2010-03-16 05:29:54 +0000 | [diff] [blame] | 1157 | put: |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1158 | dev_put(ndev); |
Cong Wang | f92d318 | 2013-01-14 23:34:06 +0000 | [diff] [blame^] | 1159 | unlock: |
| 1160 | rtnl_unlock(); |
Stephen Hemminger | b41848b | 2006-10-26 15:46:52 -0700 | [diff] [blame] | 1161 | return err; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1162 | } |
Eric Dumazet | 9e34a5b | 2010-07-09 21:22:04 +0000 | [diff] [blame] | 1163 | EXPORT_SYMBOL(netpoll_setup); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1164 | |
David S. Miller | c68b907 | 2006-11-14 20:40:49 -0800 | [diff] [blame] | 1165 | static int __init netpoll_init(void) |
| 1166 | { |
Stephen Hemminger | a1bcfac | 2006-11-14 10:43:58 -0800 | [diff] [blame] | 1167 | skb_queue_head_init(&skb_pool); |
| 1168 | return 0; |
| 1169 | } |
| 1170 | core_initcall(netpoll_init); |
| 1171 | |
Amerigo Wang | 38e6bc1 | 2012-08-10 01:24:38 +0000 | [diff] [blame] | 1172 | static void rcu_cleanup_netpoll_info(struct rcu_head *rcu_head) |
| 1173 | { |
| 1174 | struct netpoll_info *npinfo = |
| 1175 | container_of(rcu_head, struct netpoll_info, rcu); |
| 1176 | |
Cong Wang | b7394d2 | 2013-01-07 20:52:39 +0000 | [diff] [blame] | 1177 | skb_queue_purge(&npinfo->neigh_tx); |
Amerigo Wang | 38e6bc1 | 2012-08-10 01:24:38 +0000 | [diff] [blame] | 1178 | skb_queue_purge(&npinfo->txq); |
| 1179 | |
| 1180 | /* we can't call cancel_delayed_work_sync here, as we are in softirq */ |
| 1181 | cancel_delayed_work(&npinfo->tx_work); |
| 1182 | |
| 1183 | /* clean after last, unfinished work */ |
| 1184 | __skb_queue_purge(&npinfo->txq); |
| 1185 | /* now cancel it again */ |
| 1186 | cancel_delayed_work(&npinfo->tx_work); |
| 1187 | kfree(npinfo); |
| 1188 | } |
| 1189 | |
Herbert Xu | 8fdd95e | 2010-06-10 16:12:48 +0000 | [diff] [blame] | 1190 | void __netpoll_cleanup(struct netpoll *np) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1191 | { |
Jeff Moyer | fbeec2e | 2005-06-22 22:05:59 -0700 | [diff] [blame] | 1192 | struct netpoll_info *npinfo; |
| 1193 | unsigned long flags; |
| 1194 | |
Herbert Xu | 8fdd95e | 2010-06-10 16:12:48 +0000 | [diff] [blame] | 1195 | npinfo = np->dev->npinfo; |
| 1196 | if (!npinfo) |
Herbert Xu | dbaa154 | 2010-06-10 16:12:46 +0000 | [diff] [blame] | 1197 | return; |
Stephen Hemminger | 93ec2c7 | 2006-10-26 15:46:50 -0700 | [diff] [blame] | 1198 | |
Herbert Xu | 8fdd95e | 2010-06-10 16:12:48 +0000 | [diff] [blame] | 1199 | if (!list_empty(&npinfo->rx_np)) { |
| 1200 | spin_lock_irqsave(&npinfo->rx_lock, flags); |
| 1201 | list_del(&np->rx); |
| 1202 | if (list_empty(&npinfo->rx_np)) |
| 1203 | npinfo->rx_flags &= ~NETPOLL_RX_ENABLED; |
| 1204 | spin_unlock_irqrestore(&npinfo->rx_lock, flags); |
Jeff Moyer | 115c1d6 | 2005-06-22 22:05:31 -0700 | [diff] [blame] | 1205 | } |
Herbert Xu | dbaa154 | 2010-06-10 16:12:46 +0000 | [diff] [blame] | 1206 | |
Herbert Xu | 8fdd95e | 2010-06-10 16:12:48 +0000 | [diff] [blame] | 1207 | if (atomic_dec_and_test(&npinfo->refcnt)) { |
| 1208 | const struct net_device_ops *ops; |
| 1209 | |
| 1210 | ops = np->dev->netdev_ops; |
| 1211 | if (ops->ndo_netpoll_cleanup) |
| 1212 | ops->ndo_netpoll_cleanup(np->dev); |
| 1213 | |
Stephen Hemminger | a9b3cd7 | 2011-08-01 16:19:00 +0000 | [diff] [blame] | 1214 | RCU_INIT_POINTER(np->dev->npinfo, NULL); |
Amerigo Wang | 38e6bc1 | 2012-08-10 01:24:38 +0000 | [diff] [blame] | 1215 | call_rcu_bh(&npinfo->rcu, rcu_cleanup_netpoll_info); |
Herbert Xu | dbaa154 | 2010-06-10 16:12:46 +0000 | [diff] [blame] | 1216 | } |
Herbert Xu | 8fdd95e | 2010-06-10 16:12:48 +0000 | [diff] [blame] | 1217 | } |
| 1218 | EXPORT_SYMBOL_GPL(__netpoll_cleanup); |
| 1219 | |
Amerigo Wang | 38e6bc1 | 2012-08-10 01:24:38 +0000 | [diff] [blame] | 1220 | static void rcu_cleanup_netpoll(struct rcu_head *rcu_head) |
| 1221 | { |
| 1222 | struct netpoll *np = container_of(rcu_head, struct netpoll, rcu); |
| 1223 | |
| 1224 | __netpoll_cleanup(np); |
| 1225 | kfree(np); |
| 1226 | } |
| 1227 | |
| 1228 | void __netpoll_free_rcu(struct netpoll *np) |
| 1229 | { |
| 1230 | call_rcu_bh(&np->rcu, rcu_cleanup_netpoll); |
| 1231 | } |
| 1232 | EXPORT_SYMBOL_GPL(__netpoll_free_rcu); |
| 1233 | |
Herbert Xu | 8fdd95e | 2010-06-10 16:12:48 +0000 | [diff] [blame] | 1234 | void netpoll_cleanup(struct netpoll *np) |
| 1235 | { |
| 1236 | if (!np->dev) |
| 1237 | return; |
| 1238 | |
| 1239 | rtnl_lock(); |
| 1240 | __netpoll_cleanup(np); |
| 1241 | rtnl_unlock(); |
Herbert Xu | dbaa154 | 2010-06-10 16:12:46 +0000 | [diff] [blame] | 1242 | |
| 1243 | dev_put(np->dev); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1244 | np->dev = NULL; |
| 1245 | } |
Eric Dumazet | 9e34a5b | 2010-07-09 21:22:04 +0000 | [diff] [blame] | 1246 | EXPORT_SYMBOL(netpoll_cleanup); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1247 | |
| 1248 | int netpoll_trap(void) |
| 1249 | { |
| 1250 | return atomic_read(&trapped); |
| 1251 | } |
Eric Dumazet | 9e34a5b | 2010-07-09 21:22:04 +0000 | [diff] [blame] | 1252 | EXPORT_SYMBOL(netpoll_trap); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1253 | |
| 1254 | void netpoll_set_trap(int trap) |
| 1255 | { |
| 1256 | if (trap) |
| 1257 | atomic_inc(&trapped); |
| 1258 | else |
| 1259 | atomic_dec(&trapped); |
| 1260 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1261 | EXPORT_SYMBOL(netpoll_set_trap); |