netpoll.c 19 KB

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