netpoll.c 17 KB

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