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