netpoll.c 17 KB

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