netpoll.c 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806
  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. skb->nh.iph = iph = (struct iphdr *)skb_push(skb, sizeof(*iph));
  278. /* iph->version = 4; iph->ihl = 5; */
  279. put_unaligned(0x45, (unsigned char *)iph);
  280. iph->tos = 0;
  281. put_unaligned(htons(ip_len), &(iph->tot_len));
  282. iph->id = 0;
  283. iph->frag_off = 0;
  284. iph->ttl = 64;
  285. iph->protocol = IPPROTO_UDP;
  286. iph->check = 0;
  287. put_unaligned(htonl(np->local_ip), &(iph->saddr));
  288. put_unaligned(htonl(np->remote_ip), &(iph->daddr));
  289. iph->check = ip_fast_csum((unsigned char *)iph, iph->ihl);
  290. eth = (struct ethhdr *) skb_push(skb, ETH_HLEN);
  291. skb->mac.raw = skb->data;
  292. skb->protocol = eth->h_proto = htons(ETH_P_IP);
  293. memcpy(eth->h_source, np->local_mac, 6);
  294. memcpy(eth->h_dest, np->remote_mac, 6);
  295. skb->dev = np->dev;
  296. netpoll_send_skb(np, skb);
  297. }
  298. static void arp_reply(struct sk_buff *skb)
  299. {
  300. struct netpoll_info *npinfo = skb->dev->npinfo;
  301. struct arphdr *arp;
  302. unsigned char *arp_ptr;
  303. int size, type = ARPOP_REPLY, ptype = ETH_P_ARP;
  304. u32 sip, tip;
  305. struct sk_buff *send_skb;
  306. struct netpoll *np = NULL;
  307. if (npinfo->rx_np && npinfo->rx_np->dev == skb->dev)
  308. np = npinfo->rx_np;
  309. if (!np)
  310. return;
  311. /* No arp on this interface */
  312. if (skb->dev->flags & IFF_NOARP)
  313. return;
  314. if (!pskb_may_pull(skb, (sizeof(struct arphdr) +
  315. (2 * skb->dev->addr_len) +
  316. (2 * sizeof(u32)))))
  317. return;
  318. skb->h.raw = skb->nh.raw = skb->data;
  319. arp = skb->nh.arph;
  320. if ((arp->ar_hrd != htons(ARPHRD_ETHER) &&
  321. arp->ar_hrd != htons(ARPHRD_IEEE802)) ||
  322. arp->ar_pro != htons(ETH_P_IP) ||
  323. arp->ar_op != htons(ARPOP_REQUEST))
  324. return;
  325. arp_ptr = (unsigned char *)(arp+1) + skb->dev->addr_len;
  326. memcpy(&sip, arp_ptr, 4);
  327. arp_ptr += 4 + skb->dev->addr_len;
  328. memcpy(&tip, arp_ptr, 4);
  329. /* Should we ignore arp? */
  330. if (tip != htonl(np->local_ip) || LOOPBACK(tip) || MULTICAST(tip))
  331. return;
  332. size = sizeof(struct arphdr) + 2 * (skb->dev->addr_len + 4);
  333. send_skb = find_skb(np, size + LL_RESERVED_SPACE(np->dev),
  334. LL_RESERVED_SPACE(np->dev));
  335. if (!send_skb)
  336. return;
  337. send_skb->nh.raw = send_skb->data;
  338. arp = (struct arphdr *) skb_put(send_skb, size);
  339. send_skb->dev = skb->dev;
  340. send_skb->protocol = htons(ETH_P_ARP);
  341. /* Fill the device header for the ARP frame */
  342. if (np->dev->hard_header &&
  343. np->dev->hard_header(send_skb, skb->dev, ptype,
  344. np->remote_mac, np->local_mac,
  345. send_skb->len) < 0) {
  346. kfree_skb(send_skb);
  347. return;
  348. }
  349. /*
  350. * Fill out the arp protocol part.
  351. *
  352. * we only support ethernet device type,
  353. * which (according to RFC 1390) should always equal 1 (Ethernet).
  354. */
  355. arp->ar_hrd = htons(np->dev->type);
  356. arp->ar_pro = htons(ETH_P_IP);
  357. arp->ar_hln = np->dev->addr_len;
  358. arp->ar_pln = 4;
  359. arp->ar_op = htons(type);
  360. arp_ptr=(unsigned char *)(arp + 1);
  361. memcpy(arp_ptr, np->dev->dev_addr, np->dev->addr_len);
  362. arp_ptr += np->dev->addr_len;
  363. memcpy(arp_ptr, &tip, 4);
  364. arp_ptr += 4;
  365. memcpy(arp_ptr, np->remote_mac, np->dev->addr_len);
  366. arp_ptr += np->dev->addr_len;
  367. memcpy(arp_ptr, &sip, 4);
  368. netpoll_send_skb(np, send_skb);
  369. }
  370. int __netpoll_rx(struct sk_buff *skb)
  371. {
  372. int proto, len, ulen;
  373. struct iphdr *iph;
  374. struct udphdr *uh;
  375. struct netpoll_info *npi = skb->dev->npinfo;
  376. struct netpoll *np = npi->rx_np;
  377. if (!np)
  378. goto out;
  379. if (skb->dev->type != ARPHRD_ETHER)
  380. goto out;
  381. /* check if netpoll clients need ARP */
  382. if (skb->protocol == __constant_htons(ETH_P_ARP) &&
  383. atomic_read(&trapped)) {
  384. skb_queue_tail(&npi->arp_tx, skb);
  385. return 1;
  386. }
  387. proto = ntohs(eth_hdr(skb)->h_proto);
  388. if (proto != ETH_P_IP)
  389. goto out;
  390. if (skb->pkt_type == PACKET_OTHERHOST)
  391. goto out;
  392. if (skb_shared(skb))
  393. goto out;
  394. iph = (struct iphdr *)skb->data;
  395. if (!pskb_may_pull(skb, sizeof(struct iphdr)))
  396. goto out;
  397. if (iph->ihl < 5 || iph->version != 4)
  398. goto out;
  399. if (!pskb_may_pull(skb, iph->ihl*4))
  400. goto out;
  401. if (ip_fast_csum((u8 *)iph, iph->ihl) != 0)
  402. goto out;
  403. len = ntohs(iph->tot_len);
  404. if (skb->len < len || len < iph->ihl*4)
  405. goto out;
  406. if (iph->protocol != IPPROTO_UDP)
  407. goto out;
  408. len -= iph->ihl*4;
  409. uh = (struct udphdr *)(((char *)iph) + iph->ihl*4);
  410. ulen = ntohs(uh->len);
  411. if (ulen != len)
  412. goto out;
  413. if (checksum_udp(skb, uh, ulen, iph->saddr, iph->daddr))
  414. goto out;
  415. if (np->local_ip && np->local_ip != ntohl(iph->daddr))
  416. goto out;
  417. if (np->remote_ip && np->remote_ip != ntohl(iph->saddr))
  418. goto out;
  419. if (np->local_port && np->local_port != ntohs(uh->dest))
  420. goto out;
  421. np->rx_hook(np, ntohs(uh->source),
  422. (char *)(uh+1),
  423. ulen - sizeof(struct udphdr));
  424. kfree_skb(skb);
  425. return 1;
  426. out:
  427. if (atomic_read(&trapped)) {
  428. kfree_skb(skb);
  429. return 1;
  430. }
  431. return 0;
  432. }
  433. int netpoll_parse_options(struct netpoll *np, char *opt)
  434. {
  435. char *cur=opt, *delim;
  436. if(*cur != '@') {
  437. if ((delim = strchr(cur, '@')) == NULL)
  438. goto parse_failed;
  439. *delim=0;
  440. np->local_port=simple_strtol(cur, NULL, 10);
  441. cur=delim;
  442. }
  443. cur++;
  444. printk(KERN_INFO "%s: local port %d\n", np->name, np->local_port);
  445. if(*cur != '/') {
  446. if ((delim = strchr(cur, '/')) == NULL)
  447. goto parse_failed;
  448. *delim=0;
  449. np->local_ip=ntohl(in_aton(cur));
  450. cur=delim;
  451. printk(KERN_INFO "%s: local IP %d.%d.%d.%d\n",
  452. np->name, HIPQUAD(np->local_ip));
  453. }
  454. cur++;
  455. if ( *cur != ',') {
  456. /* parse out dev name */
  457. if ((delim = strchr(cur, ',')) == NULL)
  458. goto parse_failed;
  459. *delim=0;
  460. strlcpy(np->dev_name, cur, sizeof(np->dev_name));
  461. cur=delim;
  462. }
  463. cur++;
  464. printk(KERN_INFO "%s: interface %s\n", np->name, np->dev_name);
  465. if ( *cur != '@' ) {
  466. /* dst port */
  467. if ((delim = strchr(cur, '@')) == NULL)
  468. goto parse_failed;
  469. *delim=0;
  470. np->remote_port=simple_strtol(cur, NULL, 10);
  471. cur=delim;
  472. }
  473. cur++;
  474. printk(KERN_INFO "%s: remote port %d\n", np->name, np->remote_port);
  475. /* dst ip */
  476. if ((delim = strchr(cur, '/')) == NULL)
  477. goto parse_failed;
  478. *delim=0;
  479. np->remote_ip=ntohl(in_aton(cur));
  480. cur=delim+1;
  481. printk(KERN_INFO "%s: remote IP %d.%d.%d.%d\n",
  482. np->name, HIPQUAD(np->remote_ip));
  483. if( *cur != 0 )
  484. {
  485. /* MAC address */
  486. if ((delim = strchr(cur, ':')) == NULL)
  487. goto parse_failed;
  488. *delim=0;
  489. np->remote_mac[0]=simple_strtol(cur, NULL, 16);
  490. cur=delim+1;
  491. if ((delim = strchr(cur, ':')) == NULL)
  492. goto parse_failed;
  493. *delim=0;
  494. np->remote_mac[1]=simple_strtol(cur, NULL, 16);
  495. cur=delim+1;
  496. if ((delim = strchr(cur, ':')) == NULL)
  497. goto parse_failed;
  498. *delim=0;
  499. np->remote_mac[2]=simple_strtol(cur, NULL, 16);
  500. cur=delim+1;
  501. if ((delim = strchr(cur, ':')) == NULL)
  502. goto parse_failed;
  503. *delim=0;
  504. np->remote_mac[3]=simple_strtol(cur, NULL, 16);
  505. cur=delim+1;
  506. if ((delim = strchr(cur, ':')) == NULL)
  507. goto parse_failed;
  508. *delim=0;
  509. np->remote_mac[4]=simple_strtol(cur, NULL, 16);
  510. cur=delim+1;
  511. np->remote_mac[5]=simple_strtol(cur, NULL, 16);
  512. }
  513. printk(KERN_INFO "%s: remote ethernet address "
  514. "%02x:%02x:%02x:%02x:%02x:%02x\n",
  515. np->name,
  516. np->remote_mac[0],
  517. np->remote_mac[1],
  518. np->remote_mac[2],
  519. np->remote_mac[3],
  520. np->remote_mac[4],
  521. np->remote_mac[5]);
  522. return 0;
  523. parse_failed:
  524. printk(KERN_INFO "%s: couldn't parse config at %s!\n",
  525. np->name, cur);
  526. return -1;
  527. }
  528. int netpoll_setup(struct netpoll *np)
  529. {
  530. struct net_device *ndev = NULL;
  531. struct in_device *in_dev;
  532. struct netpoll_info *npinfo;
  533. unsigned long flags;
  534. if (np->dev_name)
  535. ndev = dev_get_by_name(np->dev_name);
  536. if (!ndev) {
  537. printk(KERN_ERR "%s: %s doesn't exist, aborting.\n",
  538. np->name, np->dev_name);
  539. return -1;
  540. }
  541. np->dev = ndev;
  542. if (!ndev->npinfo) {
  543. npinfo = kmalloc(sizeof(*npinfo), GFP_KERNEL);
  544. if (!npinfo)
  545. goto release;
  546. npinfo->rx_flags = 0;
  547. npinfo->rx_np = NULL;
  548. spin_lock_init(&npinfo->poll_lock);
  549. npinfo->poll_owner = -1;
  550. npinfo->tries = MAX_RETRIES;
  551. spin_lock_init(&npinfo->rx_lock);
  552. skb_queue_head_init(&npinfo->arp_tx);
  553. } else
  554. npinfo = ndev->npinfo;
  555. if (!ndev->poll_controller) {
  556. printk(KERN_ERR "%s: %s doesn't support polling, aborting.\n",
  557. np->name, np->dev_name);
  558. goto release;
  559. }
  560. if (!netif_running(ndev)) {
  561. unsigned long atmost, atleast;
  562. printk(KERN_INFO "%s: device %s not up yet, forcing it\n",
  563. np->name, np->dev_name);
  564. rtnl_lock();
  565. if (dev_change_flags(ndev, ndev->flags | IFF_UP) < 0) {
  566. printk(KERN_ERR "%s: failed to open %s\n",
  567. np->name, np->dev_name);
  568. rtnl_unlock();
  569. goto release;
  570. }
  571. rtnl_unlock();
  572. atleast = jiffies + HZ/10;
  573. atmost = jiffies + 4*HZ;
  574. while (!netif_carrier_ok(ndev)) {
  575. if (time_after(jiffies, atmost)) {
  576. printk(KERN_NOTICE
  577. "%s: timeout waiting for carrier\n",
  578. np->name);
  579. break;
  580. }
  581. cond_resched();
  582. }
  583. /* If carrier appears to come up instantly, we don't
  584. * trust it and pause so that we don't pump all our
  585. * queued console messages into the bitbucket.
  586. */
  587. if (time_before(jiffies, atleast)) {
  588. printk(KERN_NOTICE "%s: carrier detect appears"
  589. " untrustworthy, waiting 4 seconds\n",
  590. np->name);
  591. msleep(4000);
  592. }
  593. }
  594. if (is_zero_ether_addr(np->local_mac) && ndev->dev_addr)
  595. memcpy(np->local_mac, ndev->dev_addr, 6);
  596. if (!np->local_ip) {
  597. rcu_read_lock();
  598. in_dev = __in_dev_get_rcu(ndev);
  599. if (!in_dev || !in_dev->ifa_list) {
  600. rcu_read_unlock();
  601. printk(KERN_ERR "%s: no IP address for %s, aborting\n",
  602. np->name, np->dev_name);
  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 -1;
  629. }
  630. void netpoll_cleanup(struct netpoll *np)
  631. {
  632. struct netpoll_info *npinfo;
  633. unsigned long flags;
  634. if (np->dev) {
  635. npinfo = np->dev->npinfo;
  636. if (npinfo && npinfo->rx_np == np) {
  637. spin_lock_irqsave(&npinfo->rx_lock, flags);
  638. npinfo->rx_np = NULL;
  639. npinfo->rx_flags &= ~NETPOLL_RX_ENABLED;
  640. spin_unlock_irqrestore(&npinfo->rx_lock, flags);
  641. }
  642. dev_put(np->dev);
  643. }
  644. np->dev = NULL;
  645. }
  646. int netpoll_trap(void)
  647. {
  648. return atomic_read(&trapped);
  649. }
  650. void netpoll_set_trap(int trap)
  651. {
  652. if (trap)
  653. atomic_inc(&trapped);
  654. else
  655. atomic_dec(&trapped);
  656. }
  657. EXPORT_SYMBOL(netpoll_set_trap);
  658. EXPORT_SYMBOL(netpoll_trap);
  659. EXPORT_SYMBOL(netpoll_parse_options);
  660. EXPORT_SYMBOL(netpoll_setup);
  661. EXPORT_SYMBOL(netpoll_cleanup);
  662. EXPORT_SYMBOL(netpoll_send_udp);
  663. EXPORT_SYMBOL(netpoll_poll);
  664. EXPORT_SYMBOL(netpoll_queue);