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