netpoll.c 17 KB

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