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. struct netdev_queue *txq;
  52. if (!netif_device_present(dev) || !netif_running(dev)) {
  53. __kfree_skb(skb);
  54. continue;
  55. }
  56. txq = netdev_get_tx_queue(dev, skb_get_queue_mapping(skb));
  57. local_irq_save(flags);
  58. __netif_tx_lock(txq, smp_processor_id());
  59. if (netif_tx_queue_stopped(txq) ||
  60. netif_tx_queue_frozen(txq) ||
  61. dev->hard_start_xmit(skb, dev) != NETDEV_TX_OK) {
  62. skb_queue_head(&npinfo->txq, skb);
  63. __netif_tx_unlock(txq);
  64. local_irq_restore(flags);
  65. schedule_delayed_work(&npinfo->tx_work, HZ/10);
  66. return;
  67. }
  68. __netif_tx_unlock(txq);
  69. local_irq_restore(flags);
  70. }
  71. }
  72. static __sum16 checksum_udp(struct sk_buff *skb, struct udphdr *uh,
  73. unsigned short ulen, __be32 saddr, __be32 daddr)
  74. {
  75. __wsum psum;
  76. if (uh->check == 0 || skb_csum_unnecessary(skb))
  77. return 0;
  78. psum = csum_tcpudp_nofold(saddr, daddr, ulen, IPPROTO_UDP, 0);
  79. if (skb->ip_summed == CHECKSUM_COMPLETE &&
  80. !csum_fold(csum_add(psum, skb->csum)))
  81. return 0;
  82. skb->csum = psum;
  83. return __skb_checksum_complete(skb);
  84. }
  85. /*
  86. * Check whether delayed processing was scheduled for our NIC. If so,
  87. * we attempt to grab the poll lock and use ->poll() to pump the card.
  88. * If this fails, either we've recursed in ->poll() or it's already
  89. * running on another CPU.
  90. *
  91. * Note: we don't mask interrupts with this lock because we're using
  92. * trylock here and interrupts are already disabled in the softirq
  93. * case. Further, we test the poll_owner to avoid recursion on UP
  94. * systems where the lock doesn't exist.
  95. *
  96. * In cases where there is bi-directional communications, reading only
  97. * one message at a time can lead to packets being dropped by the
  98. * network adapter, forcing superfluous retries and possibly timeouts.
  99. * Thus, we set our budget to greater than 1.
  100. */
  101. static int poll_one_napi(struct netpoll_info *npinfo,
  102. struct napi_struct *napi, int budget)
  103. {
  104. int work;
  105. /* net_rx_action's ->poll() invocations and our's are
  106. * synchronized by this test which is only made while
  107. * holding the napi->poll_lock.
  108. */
  109. if (!test_bit(NAPI_STATE_SCHED, &napi->state))
  110. return budget;
  111. npinfo->rx_flags |= NETPOLL_RX_DROP;
  112. atomic_inc(&trapped);
  113. set_bit(NAPI_STATE_NPSVC, &napi->state);
  114. work = napi->poll(napi, budget);
  115. clear_bit(NAPI_STATE_NPSVC, &napi->state);
  116. atomic_dec(&trapped);
  117. npinfo->rx_flags &= ~NETPOLL_RX_DROP;
  118. return budget - work;
  119. }
  120. static void poll_napi(struct net_device *dev)
  121. {
  122. struct napi_struct *napi;
  123. int budget = 16;
  124. list_for_each_entry(napi, &dev->napi_list, dev_list) {
  125. if (napi->poll_owner != smp_processor_id() &&
  126. spin_trylock(&napi->poll_lock)) {
  127. budget = poll_one_napi(dev->npinfo, napi, budget);
  128. spin_unlock(&napi->poll_lock);
  129. if (!budget)
  130. break;
  131. }
  132. }
  133. }
  134. static void service_arp_queue(struct netpoll_info *npi)
  135. {
  136. if (npi) {
  137. struct sk_buff *skb;
  138. while ((skb = skb_dequeue(&npi->arp_tx)))
  139. arp_reply(skb);
  140. }
  141. }
  142. void netpoll_poll(struct netpoll *np)
  143. {
  144. struct net_device *dev = np->dev;
  145. if (!dev || !netif_running(dev) || !dev->poll_controller)
  146. return;
  147. /* Process pending work on NIC */
  148. dev->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. struct netpoll_info *npinfo = np->dev->npinfo;
  225. if (!npinfo || !netif_running(dev) || !netif_device_present(dev)) {
  226. __kfree_skb(skb);
  227. return;
  228. }
  229. /* don't get messages out of order, and no recursion */
  230. if (skb_queue_len(&npinfo->txq) == 0 && !netpoll_owner_active(dev)) {
  231. struct netdev_queue *txq;
  232. unsigned long flags;
  233. txq = netdev_get_tx_queue(dev, skb_get_queue_mapping(skb));
  234. local_irq_save(flags);
  235. /* try until next clock tick */
  236. for (tries = jiffies_to_usecs(1)/USEC_PER_POLL;
  237. tries > 0; --tries) {
  238. if (__netif_tx_trylock(txq)) {
  239. if (!netif_tx_queue_stopped(txq))
  240. status = dev->hard_start_xmit(skb, dev);
  241. __netif_tx_unlock(txq);
  242. if (status == NETDEV_TX_OK)
  243. break;
  244. }
  245. /* tickle device maybe there is some cleanup */
  246. netpoll_poll(np);
  247. udelay(USEC_PER_POLL);
  248. }
  249. local_irq_restore(flags);
  250. }
  251. if (status != NETDEV_TX_OK) {
  252. skb_queue_tail(&npinfo->txq, skb);
  253. schedule_delayed_work(&npinfo->tx_work,0);
  254. }
  255. }
  256. void netpoll_send_udp(struct netpoll *np, const char *msg, int len)
  257. {
  258. int total_len, eth_len, ip_len, udp_len;
  259. struct sk_buff *skb;
  260. struct udphdr *udph;
  261. struct iphdr *iph;
  262. struct ethhdr *eth;
  263. udp_len = len + sizeof(*udph);
  264. ip_len = eth_len = udp_len + sizeof(*iph);
  265. total_len = eth_len + ETH_HLEN + NET_IP_ALIGN;
  266. skb = find_skb(np, total_len, total_len - len);
  267. if (!skb)
  268. return;
  269. skb_copy_to_linear_data(skb, msg, len);
  270. skb->len += len;
  271. skb_push(skb, sizeof(*udph));
  272. skb_reset_transport_header(skb);
  273. udph = udp_hdr(skb);
  274. udph->source = htons(np->local_port);
  275. udph->dest = htons(np->remote_port);
  276. udph->len = htons(udp_len);
  277. udph->check = 0;
  278. udph->check = csum_tcpudp_magic(htonl(np->local_ip),
  279. htonl(np->remote_ip),
  280. udp_len, IPPROTO_UDP,
  281. csum_partial((unsigned char *)udph, udp_len, 0));
  282. if (udph->check == 0)
  283. udph->check = CSUM_MANGLED_0;
  284. skb_push(skb, sizeof(*iph));
  285. skb_reset_network_header(skb);
  286. iph = ip_hdr(skb);
  287. /* iph->version = 4; iph->ihl = 5; */
  288. put_unaligned(0x45, (unsigned char *)iph);
  289. iph->tos = 0;
  290. put_unaligned(htons(ip_len), &(iph->tot_len));
  291. iph->id = 0;
  292. iph->frag_off = 0;
  293. iph->ttl = 64;
  294. iph->protocol = IPPROTO_UDP;
  295. iph->check = 0;
  296. put_unaligned(htonl(np->local_ip), &(iph->saddr));
  297. put_unaligned(htonl(np->remote_ip), &(iph->daddr));
  298. iph->check = ip_fast_csum((unsigned char *)iph, iph->ihl);
  299. eth = (struct ethhdr *) skb_push(skb, ETH_HLEN);
  300. skb_reset_mac_header(skb);
  301. skb->protocol = eth->h_proto = htons(ETH_P_IP);
  302. memcpy(eth->h_source, np->dev->dev_addr, ETH_ALEN);
  303. memcpy(eth->h_dest, np->remote_mac, ETH_ALEN);
  304. skb->dev = np->dev;
  305. netpoll_send_skb(np, skb);
  306. }
  307. static void arp_reply(struct sk_buff *skb)
  308. {
  309. struct netpoll_info *npinfo = skb->dev->npinfo;
  310. struct arphdr *arp;
  311. unsigned char *arp_ptr;
  312. int size, type = ARPOP_REPLY, ptype = ETH_P_ARP;
  313. __be32 sip, tip;
  314. unsigned char *sha;
  315. struct sk_buff *send_skb;
  316. struct netpoll *np = NULL;
  317. if (npinfo->rx_np && npinfo->rx_np->dev == skb->dev)
  318. np = npinfo->rx_np;
  319. if (!np)
  320. return;
  321. /* No arp on this interface */
  322. if (skb->dev->flags & IFF_NOARP)
  323. return;
  324. if (!pskb_may_pull(skb, arp_hdr_len(skb->dev)))
  325. return;
  326. skb_reset_network_header(skb);
  327. skb_reset_transport_header(skb);
  328. arp = arp_hdr(skb);
  329. if ((arp->ar_hrd != htons(ARPHRD_ETHER) &&
  330. arp->ar_hrd != htons(ARPHRD_IEEE802)) ||
  331. arp->ar_pro != htons(ETH_P_IP) ||
  332. arp->ar_op != htons(ARPOP_REQUEST))
  333. return;
  334. arp_ptr = (unsigned char *)(arp+1);
  335. /* save the location of the src hw addr */
  336. sha = arp_ptr;
  337. arp_ptr += skb->dev->addr_len;
  338. memcpy(&sip, arp_ptr, 4);
  339. arp_ptr += 4;
  340. /* if we actually cared about dst hw addr, it would get copied here */
  341. arp_ptr += skb->dev->addr_len;
  342. memcpy(&tip, arp_ptr, 4);
  343. /* Should we ignore arp? */
  344. if (tip != htonl(np->local_ip) ||
  345. ipv4_is_loopback(tip) || ipv4_is_multicast(tip))
  346. return;
  347. size = arp_hdr_len(skb->dev);
  348. send_skb = find_skb(np, size + LL_ALLOCATED_SPACE(np->dev),
  349. LL_RESERVED_SPACE(np->dev));
  350. if (!send_skb)
  351. return;
  352. skb_reset_network_header(send_skb);
  353. arp = (struct arphdr *) skb_put(send_skb, size);
  354. send_skb->dev = skb->dev;
  355. send_skb->protocol = htons(ETH_P_ARP);
  356. /* Fill the device header for the ARP frame */
  357. if (dev_hard_header(send_skb, skb->dev, ptype,
  358. sha, np->dev->dev_addr,
  359. send_skb->len) < 0) {
  360. kfree_skb(send_skb);
  361. return;
  362. }
  363. /*
  364. * Fill out the arp protocol part.
  365. *
  366. * we only support ethernet device type,
  367. * which (according to RFC 1390) should always equal 1 (Ethernet).
  368. */
  369. arp->ar_hrd = htons(np->dev->type);
  370. arp->ar_pro = htons(ETH_P_IP);
  371. arp->ar_hln = np->dev->addr_len;
  372. arp->ar_pln = 4;
  373. arp->ar_op = htons(type);
  374. arp_ptr=(unsigned char *)(arp + 1);
  375. memcpy(arp_ptr, np->dev->dev_addr, np->dev->addr_len);
  376. arp_ptr += np->dev->addr_len;
  377. memcpy(arp_ptr, &tip, 4);
  378. arp_ptr += 4;
  379. memcpy(arp_ptr, sha, np->dev->addr_len);
  380. arp_ptr += np->dev->addr_len;
  381. memcpy(arp_ptr, &sip, 4);
  382. netpoll_send_skb(np, send_skb);
  383. }
  384. int __netpoll_rx(struct sk_buff *skb)
  385. {
  386. int proto, len, ulen;
  387. struct iphdr *iph;
  388. struct udphdr *uh;
  389. struct netpoll_info *npi = skb->dev->npinfo;
  390. struct netpoll *np = npi->rx_np;
  391. if (!np)
  392. goto out;
  393. if (skb->dev->type != ARPHRD_ETHER)
  394. goto out;
  395. /* check if netpoll clients need ARP */
  396. if (skb->protocol == htons(ETH_P_ARP) &&
  397. atomic_read(&trapped)) {
  398. skb_queue_tail(&npi->arp_tx, skb);
  399. return 1;
  400. }
  401. proto = ntohs(eth_hdr(skb)->h_proto);
  402. if (proto != ETH_P_IP)
  403. goto out;
  404. if (skb->pkt_type == PACKET_OTHERHOST)
  405. goto out;
  406. if (skb_shared(skb))
  407. goto out;
  408. iph = (struct iphdr *)skb->data;
  409. if (!pskb_may_pull(skb, sizeof(struct iphdr)))
  410. goto out;
  411. if (iph->ihl < 5 || iph->version != 4)
  412. goto out;
  413. if (!pskb_may_pull(skb, iph->ihl*4))
  414. goto out;
  415. if (ip_fast_csum((u8 *)iph, iph->ihl) != 0)
  416. goto out;
  417. len = ntohs(iph->tot_len);
  418. if (skb->len < len || len < iph->ihl*4)
  419. goto out;
  420. /*
  421. * Our transport medium may have padded the buffer out.
  422. * Now We trim to the true length of the frame.
  423. */
  424. if (pskb_trim_rcsum(skb, len))
  425. goto out;
  426. if (iph->protocol != IPPROTO_UDP)
  427. goto out;
  428. len -= iph->ihl*4;
  429. uh = (struct udphdr *)(((char *)iph) + iph->ihl*4);
  430. ulen = ntohs(uh->len);
  431. if (ulen != len)
  432. goto out;
  433. if (checksum_udp(skb, uh, ulen, iph->saddr, iph->daddr))
  434. goto out;
  435. if (np->local_ip && np->local_ip != ntohl(iph->daddr))
  436. goto out;
  437. if (np->remote_ip && np->remote_ip != ntohl(iph->saddr))
  438. goto out;
  439. if (np->local_port && np->local_port != ntohs(uh->dest))
  440. goto out;
  441. np->rx_hook(np, ntohs(uh->source),
  442. (char *)(uh+1),
  443. ulen - sizeof(struct udphdr));
  444. kfree_skb(skb);
  445. return 1;
  446. out:
  447. if (atomic_read(&trapped)) {
  448. kfree_skb(skb);
  449. return 1;
  450. }
  451. return 0;
  452. }
  453. void netpoll_print_options(struct netpoll *np)
  454. {
  455. DECLARE_MAC_BUF(mac);
  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 %s\n",
  467. np->name, print_mac(mac, 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->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);