netpoll.c 18 KB

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