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