netpoll.c 19 KB

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