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 | |
| 12 | #include <linux/smp_lock.h> |
| 13 | #include <linux/netdevice.h> |
| 14 | #include <linux/etherdevice.h> |
| 15 | #include <linux/string.h> |
Arnaldo Carvalho de Melo | 14c8502 | 2005-12-27 02:43:12 -0200 | [diff] [blame] | 16 | #include <linux/if_arp.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 17 | #include <linux/inetdevice.h> |
| 18 | #include <linux/inet.h> |
| 19 | #include <linux/interrupt.h> |
| 20 | #include <linux/netpoll.h> |
| 21 | #include <linux/sched.h> |
| 22 | #include <linux/delay.h> |
| 23 | #include <linux/rcupdate.h> |
| 24 | #include <linux/workqueue.h> |
| 25 | #include <net/tcp.h> |
| 26 | #include <net/udp.h> |
| 27 | #include <asm/unaligned.h> |
| 28 | |
| 29 | /* |
| 30 | * We maintain a small pool of fully-sized skbs, to make sure the |
| 31 | * message gets out even in extreme OOM situations. |
| 32 | */ |
| 33 | |
| 34 | #define MAX_UDP_CHUNK 1460 |
| 35 | #define MAX_SKBS 32 |
| 36 | #define MAX_QUEUE_DEPTH (MAX_SKBS / 2) |
| 37 | |
Stephen Hemminger | a1bcfac | 2006-11-14 10:43:58 -0800 | [diff] [blame] | 38 | static struct sk_buff_head skb_pool; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 39 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 40 | static atomic_t trapped; |
| 41 | |
Stephen Hemminger | 2bdfe0b | 2006-10-26 15:46:54 -0700 | [diff] [blame] | 42 | #define USEC_PER_POLL 50 |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 43 | #define NETPOLL_RX_ENABLED 1 |
| 44 | #define NETPOLL_RX_DROP 2 |
| 45 | |
| 46 | #define MAX_SKB_SIZE \ |
| 47 | (MAX_UDP_CHUNK + sizeof(struct udphdr) + \ |
| 48 | sizeof(struct iphdr) + sizeof(struct ethhdr)) |
| 49 | |
| 50 | static void zap_completion_queue(void); |
Neil Horman | 068c6e9 | 2006-06-26 00:04:27 -0700 | [diff] [blame] | 51 | static void arp_reply(struct sk_buff *skb); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 52 | |
| 53 | static void queue_process(void *p) |
| 54 | { |
Stephen Hemminger | b6cd27e | 2006-10-26 15:46:51 -0700 | [diff] [blame] | 55 | struct netpoll_info *npinfo = p; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 56 | struct sk_buff *skb; |
| 57 | |
Stephen Hemminger | 6c43ff1 | 2006-10-26 15:46:53 -0700 | [diff] [blame] | 58 | while ((skb = skb_dequeue(&npinfo->txq))) { |
| 59 | struct net_device *dev = skb->dev; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 60 | |
Stephen Hemminger | 6c43ff1 | 2006-10-26 15:46:53 -0700 | [diff] [blame] | 61 | if (!netif_device_present(dev) || !netif_running(dev)) { |
| 62 | __kfree_skb(skb); |
| 63 | continue; |
| 64 | } |
| 65 | |
| 66 | netif_tx_lock_bh(dev); |
| 67 | if (netif_queue_stopped(dev) || |
| 68 | dev->hard_start_xmit(skb, dev) != NETDEV_TX_OK) { |
| 69 | skb_queue_head(&npinfo->txq, skb); |
| 70 | netif_tx_unlock_bh(dev); |
| 71 | |
| 72 | schedule_delayed_work(&npinfo->tx_work, HZ/10); |
| 73 | return; |
| 74 | } |
Stephen Hemminger | 2bdfe0b | 2006-10-26 15:46:54 -0700 | [diff] [blame] | 75 | |
Stephen Hemminger | 6c43ff1 | 2006-10-26 15:46:53 -0700 | [diff] [blame] | 76 | netif_tx_unlock_bh(dev); |
| 77 | } |
Stephen Hemminger | b6cd27e | 2006-10-26 15:46:51 -0700 | [diff] [blame] | 78 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 79 | |
Al Viro | b51655b | 2006-11-14 21:40:42 -0800 | [diff] [blame] | 80 | static __sum16 checksum_udp(struct sk_buff *skb, struct udphdr *uh, |
| 81 | unsigned short ulen, __be32 saddr, __be32 daddr) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 82 | { |
Al Viro | d6f5493c | 2006-11-14 21:26:08 -0800 | [diff] [blame] | 83 | __wsum psum; |
Herbert Xu | fb286bb | 2005-11-10 13:01:24 -0800 | [diff] [blame] | 84 | |
| 85 | if (uh->check == 0 || skb->ip_summed == CHECKSUM_UNNECESSARY) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 86 | return 0; |
| 87 | |
Herbert Xu | fb286bb | 2005-11-10 13:01:24 -0800 | [diff] [blame] | 88 | psum = csum_tcpudp_nofold(saddr, daddr, ulen, IPPROTO_UDP, 0); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 89 | |
Patrick McHardy | 84fa793 | 2006-08-29 16:44:56 -0700 | [diff] [blame] | 90 | if (skb->ip_summed == CHECKSUM_COMPLETE && |
Al Viro | d3bc23e | 2006-11-14 21:24:49 -0800 | [diff] [blame] | 91 | !csum_fold(csum_add(psum, skb->csum))) |
Herbert Xu | fb286bb | 2005-11-10 13:01:24 -0800 | [diff] [blame] | 92 | return 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 93 | |
Herbert Xu | fb286bb | 2005-11-10 13:01:24 -0800 | [diff] [blame] | 94 | skb->csum = psum; |
| 95 | |
| 96 | return __skb_checksum_complete(skb); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 97 | } |
| 98 | |
| 99 | /* |
| 100 | * Check whether delayed processing was scheduled for our NIC. If so, |
| 101 | * we attempt to grab the poll lock and use ->poll() to pump the card. |
| 102 | * If this fails, either we've recursed in ->poll() or it's already |
| 103 | * running on another CPU. |
| 104 | * |
| 105 | * Note: we don't mask interrupts with this lock because we're using |
| 106 | * trylock here and interrupts are already disabled in the softirq |
| 107 | * case. Further, we test the poll_owner to avoid recursion on UP |
| 108 | * systems where the lock doesn't exist. |
| 109 | * |
| 110 | * In cases where there is bi-directional communications, reading only |
| 111 | * one message at a time can lead to packets being dropped by the |
| 112 | * network adapter, forcing superfluous retries and possibly timeouts. |
| 113 | * Thus, we set our budget to greater than 1. |
| 114 | */ |
| 115 | static void poll_napi(struct netpoll *np) |
| 116 | { |
Jeff Moyer | 115c1d6 | 2005-06-22 22:05:31 -0700 | [diff] [blame] | 117 | struct netpoll_info *npinfo = np->dev->npinfo; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 118 | int budget = 16; |
| 119 | |
| 120 | if (test_bit(__LINK_STATE_RX_SCHED, &np->dev->state) && |
Jeff Moyer | 115c1d6 | 2005-06-22 22:05:31 -0700 | [diff] [blame] | 121 | npinfo->poll_owner != smp_processor_id() && |
| 122 | spin_trylock(&npinfo->poll_lock)) { |
| 123 | npinfo->rx_flags |= NETPOLL_RX_DROP; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 124 | atomic_inc(&trapped); |
| 125 | |
| 126 | np->dev->poll(np->dev, &budget); |
| 127 | |
| 128 | atomic_dec(&trapped); |
Jeff Moyer | 115c1d6 | 2005-06-22 22:05:31 -0700 | [diff] [blame] | 129 | npinfo->rx_flags &= ~NETPOLL_RX_DROP; |
| 130 | spin_unlock(&npinfo->poll_lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 131 | } |
| 132 | } |
| 133 | |
Neil Horman | 068c6e9 | 2006-06-26 00:04:27 -0700 | [diff] [blame] | 134 | static void service_arp_queue(struct netpoll_info *npi) |
| 135 | { |
| 136 | struct sk_buff *skb; |
| 137 | |
| 138 | if (unlikely(!npi)) |
| 139 | return; |
| 140 | |
| 141 | skb = skb_dequeue(&npi->arp_tx); |
| 142 | |
| 143 | while (skb != NULL) { |
| 144 | arp_reply(skb); |
| 145 | skb = skb_dequeue(&npi->arp_tx); |
| 146 | } |
Neil Horman | 068c6e9 | 2006-06-26 00:04:27 -0700 | [diff] [blame] | 147 | } |
| 148 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 149 | void netpoll_poll(struct netpoll *np) |
| 150 | { |
David S. Miller | c68b907 | 2006-11-14 20:40:49 -0800 | [diff] [blame] | 151 | if (!np->dev || !netif_running(np->dev) || !np->dev->poll_controller) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 152 | return; |
| 153 | |
| 154 | /* Process pending work on NIC */ |
| 155 | np->dev->poll_controller(np->dev); |
| 156 | if (np->dev->poll) |
| 157 | poll_napi(np); |
| 158 | |
Neil Horman | 068c6e9 | 2006-06-26 00:04:27 -0700 | [diff] [blame] | 159 | service_arp_queue(np->dev->npinfo); |
| 160 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 161 | zap_completion_queue(); |
| 162 | } |
| 163 | |
| 164 | static void refill_skbs(void) |
| 165 | { |
| 166 | struct sk_buff *skb; |
| 167 | unsigned long flags; |
| 168 | |
Stephen Hemminger | a1bcfac | 2006-11-14 10:43:58 -0800 | [diff] [blame] | 169 | spin_lock_irqsave(&skb_pool.lock, flags); |
| 170 | while (skb_pool.qlen < MAX_SKBS) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 171 | skb = alloc_skb(MAX_SKB_SIZE, GFP_ATOMIC); |
| 172 | if (!skb) |
| 173 | break; |
| 174 | |
Stephen Hemminger | a1bcfac | 2006-11-14 10:43:58 -0800 | [diff] [blame] | 175 | __skb_queue_tail(&skb_pool, skb); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 176 | } |
Stephen Hemminger | a1bcfac | 2006-11-14 10:43:58 -0800 | [diff] [blame] | 177 | spin_unlock_irqrestore(&skb_pool.lock, flags); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 178 | } |
| 179 | |
| 180 | static void zap_completion_queue(void) |
| 181 | { |
| 182 | unsigned long flags; |
| 183 | struct softnet_data *sd = &get_cpu_var(softnet_data); |
| 184 | |
| 185 | if (sd->completion_queue) { |
| 186 | struct sk_buff *clist; |
| 187 | |
| 188 | local_irq_save(flags); |
| 189 | clist = sd->completion_queue; |
| 190 | sd->completion_queue = NULL; |
| 191 | local_irq_restore(flags); |
| 192 | |
| 193 | while (clist != NULL) { |
| 194 | struct sk_buff *skb = clist; |
| 195 | clist = clist->next; |
David S. Miller | c68b907 | 2006-11-14 20:40:49 -0800 | [diff] [blame] | 196 | if (skb->destructor) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 197 | dev_kfree_skb_any(skb); /* put this one back */ |
| 198 | else |
| 199 | __kfree_skb(skb); |
| 200 | } |
| 201 | } |
| 202 | |
| 203 | put_cpu_var(softnet_data); |
| 204 | } |
| 205 | |
Stephen Hemminger | a1bcfac | 2006-11-14 10:43:58 -0800 | [diff] [blame] | 206 | 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] | 207 | { |
Stephen Hemminger | a1bcfac | 2006-11-14 10:43:58 -0800 | [diff] [blame] | 208 | int count = 0; |
| 209 | struct sk_buff *skb; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 210 | |
| 211 | zap_completion_queue(); |
Stephen Hemminger | a1bcfac | 2006-11-14 10:43:58 -0800 | [diff] [blame] | 212 | refill_skbs(); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 213 | repeat: |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 214 | |
| 215 | skb = alloc_skb(len, GFP_ATOMIC); |
Stephen Hemminger | a1bcfac | 2006-11-14 10:43:58 -0800 | [diff] [blame] | 216 | if (!skb) |
| 217 | skb = skb_dequeue(&skb_pool); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 218 | |
David S. Miller | c68b907 | 2006-11-14 20:40:49 -0800 | [diff] [blame] | 219 | if (!skb) { |
Stephen Hemminger | a1bcfac | 2006-11-14 10:43:58 -0800 | [diff] [blame] | 220 | if (++count < 10) { |
| 221 | netpoll_poll(np); |
| 222 | goto repeat; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 223 | } |
Stephen Hemminger | a1bcfac | 2006-11-14 10:43:58 -0800 | [diff] [blame] | 224 | return NULL; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 225 | } |
| 226 | |
| 227 | atomic_set(&skb->users, 1); |
| 228 | skb_reserve(skb, reserve); |
| 229 | return skb; |
| 230 | } |
| 231 | |
| 232 | static void netpoll_send_skb(struct netpoll *np, struct sk_buff *skb) |
| 233 | { |
Stephen Hemminger | 2bdfe0b | 2006-10-26 15:46:54 -0700 | [diff] [blame] | 234 | int status = NETDEV_TX_BUSY; |
| 235 | unsigned long tries; |
| 236 | struct net_device *dev = np->dev; |
| 237 | struct netpoll_info *npinfo = np->dev->npinfo; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 238 | |
Stephen Hemminger | 2bdfe0b | 2006-10-26 15:46:54 -0700 | [diff] [blame] | 239 | if (!npinfo || !netif_running(dev) || !netif_device_present(dev)) { |
| 240 | __kfree_skb(skb); |
| 241 | return; |
| 242 | } |
| 243 | |
| 244 | /* don't get messages out of order, and no recursion */ |
David S. Miller | c68b907 | 2006-11-14 20:40:49 -0800 | [diff] [blame] | 245 | if (skb_queue_len(&npinfo->txq) == 0 && |
| 246 | npinfo->poll_owner != smp_processor_id() && |
| 247 | netif_tx_trylock(dev)) { |
Stephen Hemminger | 2bdfe0b | 2006-10-26 15:46:54 -0700 | [diff] [blame] | 248 | /* try until next clock tick */ |
David S. Miller | c68b907 | 2006-11-14 20:40:49 -0800 | [diff] [blame] | 249 | for (tries = jiffies_to_usecs(1)/USEC_PER_POLL; tries > 0; --tries) { |
Stephen Hemminger | 2bdfe0b | 2006-10-26 15:46:54 -0700 | [diff] [blame] | 250 | if (!netif_queue_stopped(dev)) |
| 251 | status = dev->hard_start_xmit(skb, dev); |
| 252 | |
| 253 | if (status == NETDEV_TX_OK) |
| 254 | break; |
| 255 | |
| 256 | /* tickle device maybe there is some cleanup */ |
| 257 | netpoll_poll(np); |
| 258 | |
| 259 | udelay(USEC_PER_POLL); |
| 260 | } |
| 261 | netif_tx_unlock(dev); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 262 | } |
| 263 | |
Stephen Hemminger | 2bdfe0b | 2006-10-26 15:46:54 -0700 | [diff] [blame] | 264 | if (status != NETDEV_TX_OK) { |
Stephen Hemminger | 5de4a47 | 2006-10-26 15:46:55 -0700 | [diff] [blame] | 265 | skb_queue_tail(&npinfo->txq, skb); |
| 266 | schedule_work(&npinfo->tx_work); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 267 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 268 | } |
| 269 | |
| 270 | void netpoll_send_udp(struct netpoll *np, const char *msg, int len) |
| 271 | { |
| 272 | int total_len, eth_len, ip_len, udp_len; |
| 273 | struct sk_buff *skb; |
| 274 | struct udphdr *udph; |
| 275 | struct iphdr *iph; |
| 276 | struct ethhdr *eth; |
| 277 | |
| 278 | udp_len = len + sizeof(*udph); |
| 279 | ip_len = eth_len = udp_len + sizeof(*iph); |
| 280 | total_len = eth_len + ETH_HLEN + NET_IP_ALIGN; |
| 281 | |
| 282 | skb = find_skb(np, total_len, total_len - len); |
| 283 | if (!skb) |
| 284 | return; |
| 285 | |
| 286 | memcpy(skb->data, msg, len); |
| 287 | skb->len += len; |
| 288 | |
Stephen Hemminger | 206daaf | 2006-10-19 23:58:23 -0700 | [diff] [blame] | 289 | skb->h.uh = udph = (struct udphdr *) skb_push(skb, sizeof(*udph)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 290 | udph->source = htons(np->local_port); |
| 291 | udph->dest = htons(np->remote_port); |
| 292 | udph->len = htons(udp_len); |
| 293 | udph->check = 0; |
Chris Lalancette | 8e365ee | 2006-11-07 14:56:19 -0800 | [diff] [blame] | 294 | udph->check = csum_tcpudp_magic(htonl(np->local_ip), |
| 295 | htonl(np->remote_ip), |
| 296 | udp_len, IPPROTO_UDP, |
| 297 | csum_partial((unsigned char *)udph, udp_len, 0)); |
| 298 | if (udph->check == 0) |
| 299 | udph->check = -1; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 300 | |
Stephen Hemminger | 206daaf | 2006-10-19 23:58:23 -0700 | [diff] [blame] | 301 | skb->nh.iph = iph = (struct iphdr *)skb_push(skb, sizeof(*iph)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 302 | |
| 303 | /* iph->version = 4; iph->ihl = 5; */ |
| 304 | put_unaligned(0x45, (unsigned char *)iph); |
| 305 | iph->tos = 0; |
| 306 | put_unaligned(htons(ip_len), &(iph->tot_len)); |
| 307 | iph->id = 0; |
| 308 | iph->frag_off = 0; |
| 309 | iph->ttl = 64; |
| 310 | iph->protocol = IPPROTO_UDP; |
| 311 | iph->check = 0; |
| 312 | put_unaligned(htonl(np->local_ip), &(iph->saddr)); |
| 313 | put_unaligned(htonl(np->remote_ip), &(iph->daddr)); |
| 314 | iph->check = ip_fast_csum((unsigned char *)iph, iph->ihl); |
| 315 | |
| 316 | eth = (struct ethhdr *) skb_push(skb, ETH_HLEN); |
Stephen Hemminger | 206daaf | 2006-10-19 23:58:23 -0700 | [diff] [blame] | 317 | skb->mac.raw = skb->data; |
| 318 | skb->protocol = eth->h_proto = htons(ETH_P_IP); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 319 | memcpy(eth->h_source, np->local_mac, 6); |
| 320 | memcpy(eth->h_dest, np->remote_mac, 6); |
| 321 | |
| 322 | skb->dev = np->dev; |
| 323 | |
| 324 | netpoll_send_skb(np, skb); |
| 325 | } |
| 326 | |
| 327 | static void arp_reply(struct sk_buff *skb) |
| 328 | { |
Jeff Moyer | 115c1d6 | 2005-06-22 22:05:31 -0700 | [diff] [blame] | 329 | struct netpoll_info *npinfo = skb->dev->npinfo; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 330 | struct arphdr *arp; |
| 331 | unsigned char *arp_ptr; |
| 332 | int size, type = ARPOP_REPLY, ptype = ETH_P_ARP; |
Al Viro | 252e3346 | 2006-11-14 20:48:11 -0800 | [diff] [blame] | 333 | __be32 sip, tip; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 334 | struct sk_buff *send_skb; |
Jeff Moyer | 115c1d6 | 2005-06-22 22:05:31 -0700 | [diff] [blame] | 335 | struct netpoll *np = NULL; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 336 | |
Jeff Moyer | fbeec2e | 2005-06-22 22:05:59 -0700 | [diff] [blame] | 337 | if (npinfo->rx_np && npinfo->rx_np->dev == skb->dev) |
| 338 | np = npinfo->rx_np; |
Jeff Moyer | 115c1d6 | 2005-06-22 22:05:31 -0700 | [diff] [blame] | 339 | if (!np) |
| 340 | return; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 341 | |
| 342 | /* No arp on this interface */ |
| 343 | if (skb->dev->flags & IFF_NOARP) |
| 344 | return; |
| 345 | |
| 346 | if (!pskb_may_pull(skb, (sizeof(struct arphdr) + |
| 347 | (2 * skb->dev->addr_len) + |
| 348 | (2 * sizeof(u32))))) |
| 349 | return; |
| 350 | |
| 351 | skb->h.raw = skb->nh.raw = skb->data; |
| 352 | arp = skb->nh.arph; |
| 353 | |
| 354 | if ((arp->ar_hrd != htons(ARPHRD_ETHER) && |
| 355 | arp->ar_hrd != htons(ARPHRD_IEEE802)) || |
| 356 | arp->ar_pro != htons(ETH_P_IP) || |
| 357 | arp->ar_op != htons(ARPOP_REQUEST)) |
| 358 | return; |
| 359 | |
| 360 | arp_ptr = (unsigned char *)(arp+1) + skb->dev->addr_len; |
| 361 | memcpy(&sip, arp_ptr, 4); |
| 362 | arp_ptr += 4 + skb->dev->addr_len; |
| 363 | memcpy(&tip, arp_ptr, 4); |
| 364 | |
| 365 | /* Should we ignore arp? */ |
| 366 | if (tip != htonl(np->local_ip) || LOOPBACK(tip) || MULTICAST(tip)) |
| 367 | return; |
| 368 | |
| 369 | size = sizeof(struct arphdr) + 2 * (skb->dev->addr_len + 4); |
| 370 | send_skb = find_skb(np, size + LL_RESERVED_SPACE(np->dev), |
| 371 | LL_RESERVED_SPACE(np->dev)); |
| 372 | |
| 373 | if (!send_skb) |
| 374 | return; |
| 375 | |
| 376 | send_skb->nh.raw = send_skb->data; |
| 377 | arp = (struct arphdr *) skb_put(send_skb, size); |
| 378 | send_skb->dev = skb->dev; |
| 379 | send_skb->protocol = htons(ETH_P_ARP); |
| 380 | |
| 381 | /* Fill the device header for the ARP frame */ |
| 382 | |
| 383 | if (np->dev->hard_header && |
| 384 | np->dev->hard_header(send_skb, skb->dev, ptype, |
David S. Miller | c68b907 | 2006-11-14 20:40:49 -0800 | [diff] [blame] | 385 | np->remote_mac, np->local_mac, |
| 386 | send_skb->len) < 0) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 387 | kfree_skb(send_skb); |
| 388 | return; |
| 389 | } |
| 390 | |
| 391 | /* |
| 392 | * Fill out the arp protocol part. |
| 393 | * |
| 394 | * we only support ethernet device type, |
| 395 | * which (according to RFC 1390) should always equal 1 (Ethernet). |
| 396 | */ |
| 397 | |
| 398 | arp->ar_hrd = htons(np->dev->type); |
| 399 | arp->ar_pro = htons(ETH_P_IP); |
| 400 | arp->ar_hln = np->dev->addr_len; |
| 401 | arp->ar_pln = 4; |
| 402 | arp->ar_op = htons(type); |
| 403 | |
| 404 | arp_ptr=(unsigned char *)(arp + 1); |
| 405 | memcpy(arp_ptr, np->dev->dev_addr, np->dev->addr_len); |
| 406 | arp_ptr += np->dev->addr_len; |
| 407 | memcpy(arp_ptr, &tip, 4); |
| 408 | arp_ptr += 4; |
| 409 | memcpy(arp_ptr, np->remote_mac, np->dev->addr_len); |
| 410 | arp_ptr += np->dev->addr_len; |
| 411 | memcpy(arp_ptr, &sip, 4); |
| 412 | |
| 413 | netpoll_send_skb(np, send_skb); |
| 414 | } |
| 415 | |
| 416 | int __netpoll_rx(struct sk_buff *skb) |
| 417 | { |
| 418 | int proto, len, ulen; |
| 419 | struct iphdr *iph; |
| 420 | struct udphdr *uh; |
Neil Horman | 068c6e9 | 2006-06-26 00:04:27 -0700 | [diff] [blame] | 421 | struct netpoll_info *npi = skb->dev->npinfo; |
| 422 | struct netpoll *np = npi->rx_np; |
| 423 | |
Jeff Moyer | fbeec2e | 2005-06-22 22:05:59 -0700 | [diff] [blame] | 424 | if (!np) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 425 | goto out; |
| 426 | if (skb->dev->type != ARPHRD_ETHER) |
| 427 | goto out; |
| 428 | |
| 429 | /* check if netpoll clients need ARP */ |
| 430 | if (skb->protocol == __constant_htons(ETH_P_ARP) && |
| 431 | atomic_read(&trapped)) { |
Neil Horman | 068c6e9 | 2006-06-26 00:04:27 -0700 | [diff] [blame] | 432 | skb_queue_tail(&npi->arp_tx, skb); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 433 | return 1; |
| 434 | } |
| 435 | |
| 436 | proto = ntohs(eth_hdr(skb)->h_proto); |
| 437 | if (proto != ETH_P_IP) |
| 438 | goto out; |
| 439 | if (skb->pkt_type == PACKET_OTHERHOST) |
| 440 | goto out; |
| 441 | if (skb_shared(skb)) |
| 442 | goto out; |
| 443 | |
| 444 | iph = (struct iphdr *)skb->data; |
| 445 | if (!pskb_may_pull(skb, sizeof(struct iphdr))) |
| 446 | goto out; |
| 447 | if (iph->ihl < 5 || iph->version != 4) |
| 448 | goto out; |
| 449 | if (!pskb_may_pull(skb, iph->ihl*4)) |
| 450 | goto out; |
| 451 | if (ip_fast_csum((u8 *)iph, iph->ihl) != 0) |
| 452 | goto out; |
| 453 | |
| 454 | len = ntohs(iph->tot_len); |
| 455 | if (skb->len < len || len < iph->ihl*4) |
| 456 | goto out; |
| 457 | |
| 458 | if (iph->protocol != IPPROTO_UDP) |
| 459 | goto out; |
| 460 | |
| 461 | len -= iph->ihl*4; |
| 462 | uh = (struct udphdr *)(((char *)iph) + iph->ihl*4); |
| 463 | ulen = ntohs(uh->len); |
| 464 | |
| 465 | if (ulen != len) |
| 466 | goto out; |
Herbert Xu | fb286bb | 2005-11-10 13:01:24 -0800 | [diff] [blame] | 467 | if (checksum_udp(skb, uh, ulen, iph->saddr, iph->daddr)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 468 | goto out; |
| 469 | if (np->local_ip && np->local_ip != ntohl(iph->daddr)) |
| 470 | goto out; |
| 471 | if (np->remote_ip && np->remote_ip != ntohl(iph->saddr)) |
| 472 | goto out; |
| 473 | if (np->local_port && np->local_port != ntohs(uh->dest)) |
| 474 | goto out; |
| 475 | |
| 476 | np->rx_hook(np, ntohs(uh->source), |
| 477 | (char *)(uh+1), |
| 478 | ulen - sizeof(struct udphdr)); |
| 479 | |
| 480 | kfree_skb(skb); |
| 481 | return 1; |
| 482 | |
| 483 | out: |
| 484 | if (atomic_read(&trapped)) { |
| 485 | kfree_skb(skb); |
| 486 | return 1; |
| 487 | } |
| 488 | |
| 489 | return 0; |
| 490 | } |
| 491 | |
| 492 | int netpoll_parse_options(struct netpoll *np, char *opt) |
| 493 | { |
| 494 | char *cur=opt, *delim; |
| 495 | |
David S. Miller | c68b907 | 2006-11-14 20:40:49 -0800 | [diff] [blame] | 496 | if (*cur != '@') { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 497 | if ((delim = strchr(cur, '@')) == NULL) |
| 498 | goto parse_failed; |
David S. Miller | c68b907 | 2006-11-14 20:40:49 -0800 | [diff] [blame] | 499 | *delim = 0; |
| 500 | np->local_port = simple_strtol(cur, NULL, 10); |
| 501 | cur = delim; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 502 | } |
| 503 | cur++; |
| 504 | printk(KERN_INFO "%s: local port %d\n", np->name, np->local_port); |
| 505 | |
David S. Miller | c68b907 | 2006-11-14 20:40:49 -0800 | [diff] [blame] | 506 | if (*cur != '/') { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 507 | if ((delim = strchr(cur, '/')) == NULL) |
| 508 | goto parse_failed; |
David S. Miller | c68b907 | 2006-11-14 20:40:49 -0800 | [diff] [blame] | 509 | *delim = 0; |
| 510 | np->local_ip = ntohl(in_aton(cur)); |
| 511 | cur = delim; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 512 | |
| 513 | printk(KERN_INFO "%s: local IP %d.%d.%d.%d\n", |
| 514 | np->name, HIPQUAD(np->local_ip)); |
| 515 | } |
| 516 | cur++; |
| 517 | |
David S. Miller | c68b907 | 2006-11-14 20:40:49 -0800 | [diff] [blame] | 518 | if (*cur != ',') { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 519 | /* parse out dev name */ |
| 520 | if ((delim = strchr(cur, ',')) == NULL) |
| 521 | goto parse_failed; |
David S. Miller | c68b907 | 2006-11-14 20:40:49 -0800 | [diff] [blame] | 522 | *delim = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 523 | strlcpy(np->dev_name, cur, sizeof(np->dev_name)); |
David S. Miller | c68b907 | 2006-11-14 20:40:49 -0800 | [diff] [blame] | 524 | cur = delim; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 525 | } |
| 526 | cur++; |
| 527 | |
| 528 | printk(KERN_INFO "%s: interface %s\n", np->name, np->dev_name); |
| 529 | |
David S. Miller | c68b907 | 2006-11-14 20:40:49 -0800 | [diff] [blame] | 530 | if (*cur != '@') { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 531 | /* dst port */ |
| 532 | if ((delim = strchr(cur, '@')) == NULL) |
| 533 | goto parse_failed; |
David S. Miller | c68b907 | 2006-11-14 20:40:49 -0800 | [diff] [blame] | 534 | *delim = 0; |
| 535 | np->remote_port = simple_strtol(cur, NULL, 10); |
| 536 | cur = delim; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 537 | } |
| 538 | cur++; |
| 539 | printk(KERN_INFO "%s: remote port %d\n", np->name, np->remote_port); |
| 540 | |
| 541 | /* dst ip */ |
| 542 | if ((delim = strchr(cur, '/')) == NULL) |
| 543 | goto parse_failed; |
David S. Miller | c68b907 | 2006-11-14 20:40:49 -0800 | [diff] [blame] | 544 | *delim = 0; |
| 545 | np->remote_ip = ntohl(in_aton(cur)); |
| 546 | cur = delim + 1; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 547 | |
| 548 | printk(KERN_INFO "%s: remote IP %d.%d.%d.%d\n", |
David S. Miller | c68b907 | 2006-11-14 20:40:49 -0800 | [diff] [blame] | 549 | np->name, HIPQUAD(np->remote_ip)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 550 | |
David S. Miller | c68b907 | 2006-11-14 20:40:49 -0800 | [diff] [blame] | 551 | if (*cur != 0) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 552 | /* MAC address */ |
| 553 | if ((delim = strchr(cur, ':')) == NULL) |
| 554 | goto parse_failed; |
David S. Miller | c68b907 | 2006-11-14 20:40:49 -0800 | [diff] [blame] | 555 | *delim = 0; |
| 556 | np->remote_mac[0] = simple_strtol(cur, NULL, 16); |
| 557 | cur = delim + 1; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 558 | if ((delim = strchr(cur, ':')) == NULL) |
| 559 | goto parse_failed; |
David S. Miller | c68b907 | 2006-11-14 20:40:49 -0800 | [diff] [blame] | 560 | *delim = 0; |
| 561 | np->remote_mac[1] = simple_strtol(cur, NULL, 16); |
| 562 | cur = delim + 1; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 563 | if ((delim = strchr(cur, ':')) == NULL) |
| 564 | goto parse_failed; |
David S. Miller | c68b907 | 2006-11-14 20:40:49 -0800 | [diff] [blame] | 565 | *delim = 0; |
| 566 | np->remote_mac[2] = simple_strtol(cur, NULL, 16); |
| 567 | cur = delim + 1; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 568 | if ((delim = strchr(cur, ':')) == NULL) |
| 569 | goto parse_failed; |
David S. Miller | c68b907 | 2006-11-14 20:40:49 -0800 | [diff] [blame] | 570 | *delim = 0; |
| 571 | np->remote_mac[3] = simple_strtol(cur, NULL, 16); |
| 572 | cur = delim + 1; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 573 | if ((delim = strchr(cur, ':')) == NULL) |
| 574 | goto parse_failed; |
David S. Miller | c68b907 | 2006-11-14 20:40:49 -0800 | [diff] [blame] | 575 | *delim = 0; |
| 576 | np->remote_mac[4] = simple_strtol(cur, NULL, 16); |
| 577 | cur = delim + 1; |
| 578 | np->remote_mac[5] = simple_strtol(cur, NULL, 16); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 579 | } |
| 580 | |
| 581 | printk(KERN_INFO "%s: remote ethernet address " |
| 582 | "%02x:%02x:%02x:%02x:%02x:%02x\n", |
| 583 | np->name, |
| 584 | np->remote_mac[0], |
| 585 | np->remote_mac[1], |
| 586 | np->remote_mac[2], |
| 587 | np->remote_mac[3], |
| 588 | np->remote_mac[4], |
| 589 | np->remote_mac[5]); |
| 590 | |
| 591 | return 0; |
| 592 | |
| 593 | parse_failed: |
| 594 | printk(KERN_INFO "%s: couldn't parse config at %s!\n", |
| 595 | np->name, cur); |
| 596 | return -1; |
| 597 | } |
| 598 | |
| 599 | int netpoll_setup(struct netpoll *np) |
| 600 | { |
| 601 | struct net_device *ndev = NULL; |
| 602 | struct in_device *in_dev; |
Jeff Moyer | 115c1d6 | 2005-06-22 22:05:31 -0700 | [diff] [blame] | 603 | struct netpoll_info *npinfo; |
Jeff Moyer | fbeec2e | 2005-06-22 22:05:59 -0700 | [diff] [blame] | 604 | unsigned long flags; |
Stephen Hemminger | b41848b | 2006-10-26 15:46:52 -0700 | [diff] [blame] | 605 | int err; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 606 | |
| 607 | if (np->dev_name) |
| 608 | ndev = dev_get_by_name(np->dev_name); |
| 609 | if (!ndev) { |
| 610 | printk(KERN_ERR "%s: %s doesn't exist, aborting.\n", |
| 611 | np->name, np->dev_name); |
Stephen Hemminger | b41848b | 2006-10-26 15:46:52 -0700 | [diff] [blame] | 612 | return -ENODEV; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 613 | } |
| 614 | |
| 615 | np->dev = ndev; |
Jeff Moyer | 115c1d6 | 2005-06-22 22:05:31 -0700 | [diff] [blame] | 616 | if (!ndev->npinfo) { |
| 617 | npinfo = kmalloc(sizeof(*npinfo), GFP_KERNEL); |
Stephen Hemminger | b41848b | 2006-10-26 15:46:52 -0700 | [diff] [blame] | 618 | if (!npinfo) { |
| 619 | err = -ENOMEM; |
Jeff Moyer | 115c1d6 | 2005-06-22 22:05:31 -0700 | [diff] [blame] | 620 | goto release; |
Stephen Hemminger | b41848b | 2006-10-26 15:46:52 -0700 | [diff] [blame] | 621 | } |
Jeff Moyer | 115c1d6 | 2005-06-22 22:05:31 -0700 | [diff] [blame] | 622 | |
Jeff Moyer | 1151312 | 2005-08-11 19:23:04 -0700 | [diff] [blame] | 623 | npinfo->rx_flags = 0; |
Jeff Moyer | fbeec2e | 2005-06-22 22:05:59 -0700 | [diff] [blame] | 624 | npinfo->rx_np = NULL; |
Ingo Molnar | a9f6a0d | 2005-09-09 13:10:41 -0700 | [diff] [blame] | 625 | spin_lock_init(&npinfo->poll_lock); |
Jeff Moyer | 115c1d6 | 2005-06-22 22:05:31 -0700 | [diff] [blame] | 626 | npinfo->poll_owner = -1; |
Stephen Hemminger | 2bdfe0b | 2006-10-26 15:46:54 -0700 | [diff] [blame] | 627 | |
Ingo Molnar | a9f6a0d | 2005-09-09 13:10:41 -0700 | [diff] [blame] | 628 | spin_lock_init(&npinfo->rx_lock); |
Neil Horman | 068c6e9 | 2006-06-26 00:04:27 -0700 | [diff] [blame] | 629 | skb_queue_head_init(&npinfo->arp_tx); |
Stephen Hemminger | b6cd27e | 2006-10-26 15:46:51 -0700 | [diff] [blame] | 630 | skb_queue_head_init(&npinfo->txq); |
| 631 | INIT_WORK(&npinfo->tx_work, queue_process, npinfo); |
| 632 | |
Stephen Hemminger | 93ec2c7 | 2006-10-26 15:46:50 -0700 | [diff] [blame] | 633 | atomic_set(&npinfo->refcnt, 1); |
| 634 | } else { |
Jeff Moyer | 115c1d6 | 2005-06-22 22:05:31 -0700 | [diff] [blame] | 635 | npinfo = ndev->npinfo; |
Stephen Hemminger | 93ec2c7 | 2006-10-26 15:46:50 -0700 | [diff] [blame] | 636 | atomic_inc(&npinfo->refcnt); |
| 637 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 638 | |
| 639 | if (!ndev->poll_controller) { |
| 640 | printk(KERN_ERR "%s: %s doesn't support polling, aborting.\n", |
| 641 | np->name, np->dev_name); |
Stephen Hemminger | b41848b | 2006-10-26 15:46:52 -0700 | [diff] [blame] | 642 | err = -ENOTSUPP; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 643 | goto release; |
| 644 | } |
| 645 | |
| 646 | if (!netif_running(ndev)) { |
| 647 | unsigned long atmost, atleast; |
| 648 | |
| 649 | printk(KERN_INFO "%s: device %s not up yet, forcing it\n", |
| 650 | np->name, np->dev_name); |
| 651 | |
Stephen Hemminger | 6756ae4 | 2006-03-20 22:23:58 -0800 | [diff] [blame] | 652 | rtnl_lock(); |
Stephen Hemminger | b41848b | 2006-10-26 15:46:52 -0700 | [diff] [blame] | 653 | err = dev_open(ndev); |
| 654 | rtnl_unlock(); |
| 655 | |
| 656 | if (err) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 657 | printk(KERN_ERR "%s: failed to open %s\n", |
Stephen Hemminger | b41848b | 2006-10-26 15:46:52 -0700 | [diff] [blame] | 658 | np->name, ndev->name); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 659 | goto release; |
| 660 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 661 | |
| 662 | atleast = jiffies + HZ/10; |
| 663 | atmost = jiffies + 4*HZ; |
| 664 | while (!netif_carrier_ok(ndev)) { |
| 665 | if (time_after(jiffies, atmost)) { |
| 666 | printk(KERN_NOTICE |
| 667 | "%s: timeout waiting for carrier\n", |
| 668 | np->name); |
| 669 | break; |
| 670 | } |
| 671 | cond_resched(); |
| 672 | } |
| 673 | |
| 674 | /* If carrier appears to come up instantly, we don't |
| 675 | * trust it and pause so that we don't pump all our |
| 676 | * queued console messages into the bitbucket. |
| 677 | */ |
| 678 | |
| 679 | if (time_before(jiffies, atleast)) { |
| 680 | printk(KERN_NOTICE "%s: carrier detect appears" |
| 681 | " untrustworthy, waiting 4 seconds\n", |
| 682 | np->name); |
| 683 | msleep(4000); |
| 684 | } |
| 685 | } |
| 686 | |
Kris Katterjohn | 3860288 | 2006-01-17 15:15:38 -0800 | [diff] [blame] | 687 | if (is_zero_ether_addr(np->local_mac) && ndev->dev_addr) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 688 | memcpy(np->local_mac, ndev->dev_addr, 6); |
| 689 | |
| 690 | if (!np->local_ip) { |
| 691 | rcu_read_lock(); |
Herbert Xu | e5ed639 | 2005-10-03 14:35:55 -0700 | [diff] [blame] | 692 | in_dev = __in_dev_get_rcu(ndev); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 693 | |
| 694 | if (!in_dev || !in_dev->ifa_list) { |
| 695 | rcu_read_unlock(); |
| 696 | printk(KERN_ERR "%s: no IP address for %s, aborting\n", |
| 697 | np->name, np->dev_name); |
Stephen Hemminger | b41848b | 2006-10-26 15:46:52 -0700 | [diff] [blame] | 698 | err = -EDESTADDRREQ; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 699 | goto release; |
| 700 | } |
| 701 | |
| 702 | np->local_ip = ntohl(in_dev->ifa_list->ifa_local); |
| 703 | rcu_read_unlock(); |
| 704 | printk(KERN_INFO "%s: local IP %d.%d.%d.%d\n", |
| 705 | np->name, HIPQUAD(np->local_ip)); |
| 706 | } |
| 707 | |
Jeff Moyer | fbeec2e | 2005-06-22 22:05:59 -0700 | [diff] [blame] | 708 | if (np->rx_hook) { |
| 709 | spin_lock_irqsave(&npinfo->rx_lock, flags); |
| 710 | npinfo->rx_flags |= NETPOLL_RX_ENABLED; |
| 711 | npinfo->rx_np = np; |
| 712 | spin_unlock_irqrestore(&npinfo->rx_lock, flags); |
| 713 | } |
Ingo Molnar | 2652076 | 2005-08-11 19:26:42 -0700 | [diff] [blame] | 714 | |
| 715 | /* fill up the skb queue */ |
| 716 | refill_skbs(); |
| 717 | |
Jeff Moyer | fbeec2e | 2005-06-22 22:05:59 -0700 | [diff] [blame] | 718 | /* last thing to do is link it to the net device structure */ |
Jeff Moyer | 115c1d6 | 2005-06-22 22:05:31 -0700 | [diff] [blame] | 719 | ndev->npinfo = npinfo; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 720 | |
Matt Mackall | 53fb95d | 2005-08-11 19:27:43 -0700 | [diff] [blame] | 721 | /* avoid racing with NAPI reading npinfo */ |
| 722 | synchronize_rcu(); |
| 723 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 724 | return 0; |
| 725 | |
| 726 | release: |
Jeff Moyer | 115c1d6 | 2005-06-22 22:05:31 -0700 | [diff] [blame] | 727 | if (!ndev->npinfo) |
| 728 | kfree(npinfo); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 729 | np->dev = NULL; |
| 730 | dev_put(ndev); |
Stephen Hemminger | b41848b | 2006-10-26 15:46:52 -0700 | [diff] [blame] | 731 | return err; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 732 | } |
| 733 | |
David S. Miller | c68b907 | 2006-11-14 20:40:49 -0800 | [diff] [blame] | 734 | static int __init netpoll_init(void) |
| 735 | { |
Stephen Hemminger | a1bcfac | 2006-11-14 10:43:58 -0800 | [diff] [blame] | 736 | skb_queue_head_init(&skb_pool); |
| 737 | return 0; |
| 738 | } |
| 739 | core_initcall(netpoll_init); |
| 740 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 741 | void netpoll_cleanup(struct netpoll *np) |
| 742 | { |
Jeff Moyer | fbeec2e | 2005-06-22 22:05:59 -0700 | [diff] [blame] | 743 | struct netpoll_info *npinfo; |
| 744 | unsigned long flags; |
| 745 | |
Jeff Moyer | 115c1d6 | 2005-06-22 22:05:31 -0700 | [diff] [blame] | 746 | if (np->dev) { |
Jeff Moyer | fbeec2e | 2005-06-22 22:05:59 -0700 | [diff] [blame] | 747 | npinfo = np->dev->npinfo; |
Stephen Hemminger | 93ec2c7 | 2006-10-26 15:46:50 -0700 | [diff] [blame] | 748 | if (npinfo) { |
| 749 | if (npinfo->rx_np == np) { |
| 750 | spin_lock_irqsave(&npinfo->rx_lock, flags); |
| 751 | npinfo->rx_np = NULL; |
| 752 | npinfo->rx_flags &= ~NETPOLL_RX_ENABLED; |
| 753 | spin_unlock_irqrestore(&npinfo->rx_lock, flags); |
| 754 | } |
| 755 | |
| 756 | np->dev->npinfo = NULL; |
| 757 | if (atomic_dec_and_test(&npinfo->refcnt)) { |
| 758 | skb_queue_purge(&npinfo->arp_tx); |
Stephen Hemminger | b6cd27e | 2006-10-26 15:46:51 -0700 | [diff] [blame] | 759 | skb_queue_purge(&npinfo->txq); |
Stephen Hemminger | 6c43ff1 | 2006-10-26 15:46:53 -0700 | [diff] [blame] | 760 | cancel_rearming_delayed_work(&npinfo->tx_work); |
Stephen Hemminger | b6cd27e | 2006-10-26 15:46:51 -0700 | [diff] [blame] | 761 | flush_scheduled_work(); |
Stephen Hemminger | 93ec2c7 | 2006-10-26 15:46:50 -0700 | [diff] [blame] | 762 | |
| 763 | kfree(npinfo); |
| 764 | } |
Jeff Moyer | fbeec2e | 2005-06-22 22:05:59 -0700 | [diff] [blame] | 765 | } |
Stephen Hemminger | 93ec2c7 | 2006-10-26 15:46:50 -0700 | [diff] [blame] | 766 | |
Jeff Moyer | 115c1d6 | 2005-06-22 22:05:31 -0700 | [diff] [blame] | 767 | dev_put(np->dev); |
| 768 | } |
Jeff Moyer | fbeec2e | 2005-06-22 22:05:59 -0700 | [diff] [blame] | 769 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 770 | np->dev = NULL; |
| 771 | } |
| 772 | |
| 773 | int netpoll_trap(void) |
| 774 | { |
| 775 | return atomic_read(&trapped); |
| 776 | } |
| 777 | |
| 778 | void netpoll_set_trap(int trap) |
| 779 | { |
| 780 | if (trap) |
| 781 | atomic_inc(&trapped); |
| 782 | else |
| 783 | atomic_dec(&trapped); |
| 784 | } |
| 785 | |
| 786 | EXPORT_SYMBOL(netpoll_set_trap); |
| 787 | EXPORT_SYMBOL(netpoll_trap); |
| 788 | EXPORT_SYMBOL(netpoll_parse_options); |
| 789 | EXPORT_SYMBOL(netpoll_setup); |
| 790 | EXPORT_SYMBOL(netpoll_cleanup); |
| 791 | EXPORT_SYMBOL(netpoll_send_udp); |
| 792 | EXPORT_SYMBOL(netpoll_poll); |