netpoll.c 17 KB

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