netpoll.c 21 KB

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