netpoll.c 19 KB

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