netpoll.c 18 KB

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