netpoll.c 18 KB

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