netpoll.c 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962
  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. static struct sk_buff_head skb_pool;
  36. static atomic_t trapped;
  37. #define USEC_PER_POLL 50
  38. #define NETPOLL_RX_ENABLED 1
  39. #define NETPOLL_RX_DROP 2
  40. #define MAX_SKB_SIZE \
  41. (MAX_UDP_CHUNK + sizeof(struct udphdr) + \
  42. sizeof(struct iphdr) + sizeof(struct ethhdr))
  43. static void zap_completion_queue(void);
  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_frozen_or_stopped(txq) ||
  65. ops->ndo_start_xmit(skb, dev) != NETDEV_TX_OK) {
  66. skb_queue_head(&npinfo->txq, skb);
  67. __netif_tx_unlock(txq);
  68. local_irq_restore(flags);
  69. schedule_delayed_work(&npinfo->tx_work, HZ/10);
  70. return;
  71. }
  72. __netif_tx_unlock(txq);
  73. local_irq_restore(flags);
  74. }
  75. }
  76. static __sum16 checksum_udp(struct sk_buff *skb, struct udphdr *uh,
  77. unsigned short ulen, __be32 saddr, __be32 daddr)
  78. {
  79. __wsum psum;
  80. if (uh->check == 0 || skb_csum_unnecessary(skb))
  81. return 0;
  82. psum = csum_tcpudp_nofold(saddr, daddr, ulen, IPPROTO_UDP, 0);
  83. if (skb->ip_summed == CHECKSUM_COMPLETE &&
  84. !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 int poll_one_napi(struct netpoll_info *npinfo,
  106. struct napi_struct *napi, int budget)
  107. {
  108. int work;
  109. /* net_rx_action's ->poll() invocations and our's are
  110. * synchronized by this test which is only made while
  111. * holding the napi->poll_lock.
  112. */
  113. if (!test_bit(NAPI_STATE_SCHED, &napi->state))
  114. return budget;
  115. npinfo->rx_flags |= NETPOLL_RX_DROP;
  116. atomic_inc(&trapped);
  117. set_bit(NAPI_STATE_NPSVC, &napi->state);
  118. work = napi->poll(napi, budget);
  119. trace_napi_poll(napi);
  120. clear_bit(NAPI_STATE_NPSVC, &napi->state);
  121. atomic_dec(&trapped);
  122. npinfo->rx_flags &= ~NETPOLL_RX_DROP;
  123. return budget - work;
  124. }
  125. static void poll_napi(struct net_device *dev)
  126. {
  127. struct napi_struct *napi;
  128. int budget = 16;
  129. list_for_each_entry(napi, &dev->napi_list, dev_list) {
  130. if (napi->poll_owner != smp_processor_id() &&
  131. spin_trylock(&napi->poll_lock)) {
  132. budget = poll_one_napi(dev->npinfo, napi, budget);
  133. spin_unlock(&napi->poll_lock);
  134. if (!budget)
  135. break;
  136. }
  137. }
  138. }
  139. static void service_arp_queue(struct netpoll_info *npi)
  140. {
  141. if (npi) {
  142. struct sk_buff *skb;
  143. while ((skb = skb_dequeue(&npi->arp_tx)))
  144. arp_reply(skb);
  145. }
  146. }
  147. void netpoll_poll_dev(struct net_device *dev)
  148. {
  149. const struct net_device_ops *ops;
  150. if (!dev || !netif_running(dev))
  151. return;
  152. ops = dev->netdev_ops;
  153. if (!ops->ndo_poll_controller)
  154. return;
  155. /* Process pending work on NIC */
  156. ops->ndo_poll_controller(dev);
  157. poll_napi(dev);
  158. service_arp_queue(dev->npinfo);
  159. zap_completion_queue();
  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 void zap_completion_queue(void)
  181. {
  182. unsigned long flags;
  183. struct softnet_data *sd = &get_cpu_var(softnet_data);
  184. if (sd->completion_queue) {
  185. struct sk_buff *clist;
  186. local_irq_save(flags);
  187. clist = sd->completion_queue;
  188. sd->completion_queue = NULL;
  189. local_irq_restore(flags);
  190. while (clist != NULL) {
  191. struct sk_buff *skb = clist;
  192. clist = clist->next;
  193. if (skb->destructor) {
  194. atomic_inc(&skb->users);
  195. dev_kfree_skb_any(skb); /* put this one back */
  196. } else {
  197. __kfree_skb(skb);
  198. }
  199. }
  200. }
  201. put_cpu_var(softnet_data);
  202. }
  203. static struct sk_buff *find_skb(struct netpoll *np, int len, int reserve)
  204. {
  205. int count = 0;
  206. struct sk_buff *skb;
  207. zap_completion_queue();
  208. refill_skbs();
  209. repeat:
  210. skb = alloc_skb(len, GFP_ATOMIC);
  211. if (!skb)
  212. skb = skb_dequeue(&skb_pool);
  213. if (!skb) {
  214. if (++count < 10) {
  215. netpoll_poll(np);
  216. goto repeat;
  217. }
  218. return NULL;
  219. }
  220. atomic_set(&skb->users, 1);
  221. skb_reserve(skb, reserve);
  222. return skb;
  223. }
  224. static int netpoll_owner_active(struct net_device *dev)
  225. {
  226. struct napi_struct *napi;
  227. list_for_each_entry(napi, &dev->napi_list, dev_list) {
  228. if (napi->poll_owner == smp_processor_id())
  229. return 1;
  230. }
  231. return 0;
  232. }
  233. void netpoll_send_skb_on_dev(struct netpoll *np, struct sk_buff *skb,
  234. struct net_device *dev)
  235. {
  236. int status = NETDEV_TX_BUSY;
  237. unsigned long tries;
  238. const struct net_device_ops *ops = dev->netdev_ops;
  239. /* It is up to the caller to keep npinfo alive. */
  240. struct netpoll_info *npinfo = np->dev->npinfo;
  241. if (!npinfo || !netif_running(dev) || !netif_device_present(dev)) {
  242. __kfree_skb(skb);
  243. return;
  244. }
  245. /* don't get messages out of order, and no recursion */
  246. if (skb_queue_len(&npinfo->txq) == 0 && !netpoll_owner_active(dev)) {
  247. struct netdev_queue *txq;
  248. unsigned long flags;
  249. txq = netdev_get_tx_queue(dev, skb_get_queue_mapping(skb));
  250. local_irq_save(flags);
  251. /* try until next clock tick */
  252. for (tries = jiffies_to_usecs(1)/USEC_PER_POLL;
  253. tries > 0; --tries) {
  254. if (__netif_tx_trylock(txq)) {
  255. if (!netif_tx_queue_stopped(txq)) {
  256. dev->priv_flags |= IFF_IN_NETPOLL;
  257. status = ops->ndo_start_xmit(skb, dev);
  258. dev->priv_flags &= ~IFF_IN_NETPOLL;
  259. if (status == NETDEV_TX_OK)
  260. txq_trans_update(txq);
  261. }
  262. __netif_tx_unlock(txq);
  263. if (status == NETDEV_TX_OK)
  264. break;
  265. }
  266. /* tickle device maybe there is some cleanup */
  267. netpoll_poll(np);
  268. udelay(USEC_PER_POLL);
  269. }
  270. WARN_ONCE(!irqs_disabled(),
  271. "netpoll_send_skb(): %s enabled interrupts in poll (%pF)\n",
  272. dev->name, ops->ndo_start_xmit);
  273. local_irq_restore(flags);
  274. }
  275. if (status != NETDEV_TX_OK) {
  276. skb_queue_tail(&npinfo->txq, skb);
  277. schedule_delayed_work(&npinfo->tx_work,0);
  278. }
  279. }
  280. EXPORT_SYMBOL(netpoll_send_skb_on_dev);
  281. void netpoll_send_udp(struct netpoll *np, const char *msg, int len)
  282. {
  283. int total_len, eth_len, ip_len, udp_len;
  284. struct sk_buff *skb;
  285. struct udphdr *udph;
  286. struct iphdr *iph;
  287. struct ethhdr *eth;
  288. udp_len = len + sizeof(*udph);
  289. ip_len = eth_len = udp_len + sizeof(*iph);
  290. total_len = eth_len + ETH_HLEN + NET_IP_ALIGN;
  291. skb = find_skb(np, total_len, total_len - len);
  292. if (!skb)
  293. return;
  294. skb_copy_to_linear_data(skb, msg, len);
  295. skb->len += len;
  296. skb_push(skb, sizeof(*udph));
  297. skb_reset_transport_header(skb);
  298. udph = udp_hdr(skb);
  299. udph->source = htons(np->local_port);
  300. udph->dest = htons(np->remote_port);
  301. udph->len = htons(udp_len);
  302. udph->check = 0;
  303. udph->check = csum_tcpudp_magic(np->local_ip,
  304. np->remote_ip,
  305. udp_len, IPPROTO_UDP,
  306. csum_partial(udph, udp_len, 0));
  307. if (udph->check == 0)
  308. udph->check = CSUM_MANGLED_0;
  309. skb_push(skb, sizeof(*iph));
  310. skb_reset_network_header(skb);
  311. iph = ip_hdr(skb);
  312. /* iph->version = 4; iph->ihl = 5; */
  313. put_unaligned(0x45, (unsigned char *)iph);
  314. iph->tos = 0;
  315. put_unaligned(htons(ip_len), &(iph->tot_len));
  316. iph->id = 0;
  317. iph->frag_off = 0;
  318. iph->ttl = 64;
  319. iph->protocol = IPPROTO_UDP;
  320. iph->check = 0;
  321. put_unaligned(np->local_ip, &(iph->saddr));
  322. put_unaligned(np->remote_ip, &(iph->daddr));
  323. iph->check = ip_fast_csum((unsigned char *)iph, iph->ihl);
  324. eth = (struct ethhdr *) skb_push(skb, ETH_HLEN);
  325. skb_reset_mac_header(skb);
  326. skb->protocol = eth->h_proto = htons(ETH_P_IP);
  327. memcpy(eth->h_source, np->dev->dev_addr, ETH_ALEN);
  328. memcpy(eth->h_dest, np->remote_mac, ETH_ALEN);
  329. skb->dev = np->dev;
  330. netpoll_send_skb(np, skb);
  331. }
  332. EXPORT_SYMBOL(netpoll_send_udp);
  333. static void arp_reply(struct sk_buff *skb)
  334. {
  335. struct netpoll_info *npinfo = skb->dev->npinfo;
  336. struct arphdr *arp;
  337. unsigned char *arp_ptr;
  338. int size, type = ARPOP_REPLY, ptype = ETH_P_ARP;
  339. __be32 sip, tip;
  340. unsigned char *sha;
  341. struct sk_buff *send_skb;
  342. struct netpoll *np, *tmp;
  343. unsigned long flags;
  344. int hits = 0;
  345. if (list_empty(&npinfo->rx_np))
  346. return;
  347. /* Before checking the packet, we do some early
  348. inspection whether this is interesting at all */
  349. spin_lock_irqsave(&npinfo->rx_lock, flags);
  350. list_for_each_entry_safe(np, tmp, &npinfo->rx_np, rx) {
  351. if (np->dev == skb->dev)
  352. hits++;
  353. }
  354. spin_unlock_irqrestore(&npinfo->rx_lock, flags);
  355. /* No netpoll struct is using this dev */
  356. if (!hits)
  357. return;
  358. /* No arp on this interface */
  359. if (skb->dev->flags & IFF_NOARP)
  360. return;
  361. if (!pskb_may_pull(skb, arp_hdr_len(skb->dev)))
  362. return;
  363. skb_reset_network_header(skb);
  364. skb_reset_transport_header(skb);
  365. arp = arp_hdr(skb);
  366. if ((arp->ar_hrd != htons(ARPHRD_ETHER) &&
  367. arp->ar_hrd != htons(ARPHRD_IEEE802)) ||
  368. arp->ar_pro != htons(ETH_P_IP) ||
  369. arp->ar_op != htons(ARPOP_REQUEST))
  370. return;
  371. arp_ptr = (unsigned char *)(arp+1);
  372. /* save the location of the src hw addr */
  373. sha = arp_ptr;
  374. arp_ptr += skb->dev->addr_len;
  375. memcpy(&sip, arp_ptr, 4);
  376. arp_ptr += 4;
  377. /* If we actually cared about dst hw addr,
  378. it would get copied here */
  379. arp_ptr += skb->dev->addr_len;
  380. memcpy(&tip, arp_ptr, 4);
  381. /* Should we ignore arp? */
  382. if (ipv4_is_loopback(tip) || ipv4_is_multicast(tip))
  383. return;
  384. size = arp_hdr_len(skb->dev);
  385. spin_lock_irqsave(&npinfo->rx_lock, flags);
  386. list_for_each_entry_safe(np, tmp, &npinfo->rx_np, rx) {
  387. if (tip != np->local_ip)
  388. continue;
  389. send_skb = find_skb(np, size + LL_ALLOCATED_SPACE(np->dev),
  390. LL_RESERVED_SPACE(np->dev));
  391. if (!send_skb)
  392. continue;
  393. skb_reset_network_header(send_skb);
  394. arp = (struct arphdr *) skb_put(send_skb, size);
  395. send_skb->dev = skb->dev;
  396. send_skb->protocol = htons(ETH_P_ARP);
  397. /* Fill the device header for the ARP frame */
  398. if (dev_hard_header(send_skb, skb->dev, ptype,
  399. sha, np->dev->dev_addr,
  400. send_skb->len) < 0) {
  401. kfree_skb(send_skb);
  402. continue;
  403. }
  404. /*
  405. * Fill out the arp protocol part.
  406. *
  407. * we only support ethernet device type,
  408. * which (according to RFC 1390) should
  409. * always equal 1 (Ethernet).
  410. */
  411. arp->ar_hrd = htons(np->dev->type);
  412. arp->ar_pro = htons(ETH_P_IP);
  413. arp->ar_hln = np->dev->addr_len;
  414. arp->ar_pln = 4;
  415. arp->ar_op = htons(type);
  416. arp_ptr = (unsigned char *)(arp + 1);
  417. memcpy(arp_ptr, np->dev->dev_addr, np->dev->addr_len);
  418. arp_ptr += np->dev->addr_len;
  419. memcpy(arp_ptr, &tip, 4);
  420. arp_ptr += 4;
  421. memcpy(arp_ptr, sha, np->dev->addr_len);
  422. arp_ptr += np->dev->addr_len;
  423. memcpy(arp_ptr, &sip, 4);
  424. netpoll_send_skb(np, send_skb);
  425. /* If there are several rx_hooks for the same address,
  426. we're fine by sending a single reply */
  427. break;
  428. }
  429. spin_unlock_irqrestore(&npinfo->rx_lock, flags);
  430. }
  431. int __netpoll_rx(struct sk_buff *skb)
  432. {
  433. int proto, len, ulen;
  434. int hits = 0;
  435. struct iphdr *iph;
  436. struct udphdr *uh;
  437. struct netpoll_info *npinfo = skb->dev->npinfo;
  438. struct netpoll *np, *tmp;
  439. if (list_empty(&npinfo->rx_np))
  440. goto out;
  441. if (skb->dev->type != ARPHRD_ETHER)
  442. goto out;
  443. /* check if netpoll clients need ARP */
  444. if (skb->protocol == htons(ETH_P_ARP) &&
  445. atomic_read(&trapped)) {
  446. skb_queue_tail(&npinfo->arp_tx, skb);
  447. return 1;
  448. }
  449. proto = ntohs(eth_hdr(skb)->h_proto);
  450. if (proto != ETH_P_IP)
  451. goto out;
  452. if (skb->pkt_type == PACKET_OTHERHOST)
  453. goto out;
  454. if (skb_shared(skb))
  455. goto out;
  456. iph = (struct iphdr *)skb->data;
  457. if (!pskb_may_pull(skb, sizeof(struct iphdr)))
  458. goto out;
  459. if (iph->ihl < 5 || iph->version != 4)
  460. goto out;
  461. if (!pskb_may_pull(skb, iph->ihl*4))
  462. goto out;
  463. if (ip_fast_csum((u8 *)iph, iph->ihl) != 0)
  464. goto out;
  465. len = ntohs(iph->tot_len);
  466. if (skb->len < len || len < iph->ihl*4)
  467. goto out;
  468. /*
  469. * Our transport medium may have padded the buffer out.
  470. * Now We trim to the true length of the frame.
  471. */
  472. if (pskb_trim_rcsum(skb, len))
  473. goto out;
  474. if (iph->protocol != IPPROTO_UDP)
  475. goto out;
  476. len -= iph->ihl*4;
  477. uh = (struct udphdr *)(((char *)iph) + iph->ihl*4);
  478. ulen = ntohs(uh->len);
  479. if (ulen != len)
  480. goto out;
  481. if (checksum_udp(skb, uh, ulen, iph->saddr, iph->daddr))
  482. goto out;
  483. list_for_each_entry_safe(np, tmp, &npinfo->rx_np, rx) {
  484. if (np->local_ip && np->local_ip != iph->daddr)
  485. continue;
  486. if (np->remote_ip && np->remote_ip != iph->saddr)
  487. continue;
  488. if (np->local_port && np->local_port != ntohs(uh->dest))
  489. continue;
  490. np->rx_hook(np, ntohs(uh->source),
  491. (char *)(uh+1),
  492. ulen - sizeof(struct udphdr));
  493. hits++;
  494. }
  495. if (!hits)
  496. goto out;
  497. kfree_skb(skb);
  498. return 1;
  499. out:
  500. if (atomic_read(&trapped)) {
  501. kfree_skb(skb);
  502. return 1;
  503. }
  504. return 0;
  505. }
  506. void netpoll_print_options(struct netpoll *np)
  507. {
  508. printk(KERN_INFO "%s: local port %d\n",
  509. np->name, np->local_port);
  510. printk(KERN_INFO "%s: local IP %pI4\n",
  511. np->name, &np->local_ip);
  512. printk(KERN_INFO "%s: interface '%s'\n",
  513. np->name, np->dev_name);
  514. printk(KERN_INFO "%s: remote port %d\n",
  515. np->name, np->remote_port);
  516. printk(KERN_INFO "%s: remote IP %pI4\n",
  517. np->name, &np->remote_ip);
  518. printk(KERN_INFO "%s: remote ethernet address %pM\n",
  519. np->name, np->remote_mac);
  520. }
  521. EXPORT_SYMBOL(netpoll_print_options);
  522. int netpoll_parse_options(struct netpoll *np, char *opt)
  523. {
  524. char *cur=opt, *delim;
  525. if (*cur != '@') {
  526. if ((delim = strchr(cur, '@')) == NULL)
  527. goto parse_failed;
  528. *delim = 0;
  529. np->local_port = simple_strtol(cur, NULL, 10);
  530. cur = delim;
  531. }
  532. cur++;
  533. if (*cur != '/') {
  534. if ((delim = strchr(cur, '/')) == NULL)
  535. goto parse_failed;
  536. *delim = 0;
  537. np->local_ip = in_aton(cur);
  538. cur = delim;
  539. }
  540. cur++;
  541. if (*cur != ',') {
  542. /* parse out dev name */
  543. if ((delim = strchr(cur, ',')) == NULL)
  544. goto parse_failed;
  545. *delim = 0;
  546. strlcpy(np->dev_name, cur, sizeof(np->dev_name));
  547. cur = delim;
  548. }
  549. cur++;
  550. if (*cur != '@') {
  551. /* dst port */
  552. if ((delim = strchr(cur, '@')) == NULL)
  553. goto parse_failed;
  554. *delim = 0;
  555. if (*cur == ' ' || *cur == '\t')
  556. printk(KERN_INFO "%s: warning: whitespace"
  557. "is not allowed\n", np->name);
  558. np->remote_port = simple_strtol(cur, NULL, 10);
  559. cur = delim;
  560. }
  561. cur++;
  562. /* dst ip */
  563. if ((delim = strchr(cur, '/')) == NULL)
  564. goto parse_failed;
  565. *delim = 0;
  566. np->remote_ip = in_aton(cur);
  567. cur = delim + 1;
  568. if (*cur != 0) {
  569. /* MAC address */
  570. if ((delim = strchr(cur, ':')) == NULL)
  571. goto parse_failed;
  572. *delim = 0;
  573. np->remote_mac[0] = simple_strtol(cur, NULL, 16);
  574. cur = delim + 1;
  575. if ((delim = strchr(cur, ':')) == NULL)
  576. goto parse_failed;
  577. *delim = 0;
  578. np->remote_mac[1] = simple_strtol(cur, NULL, 16);
  579. cur = delim + 1;
  580. if ((delim = strchr(cur, ':')) == NULL)
  581. goto parse_failed;
  582. *delim = 0;
  583. np->remote_mac[2] = simple_strtol(cur, NULL, 16);
  584. cur = delim + 1;
  585. if ((delim = strchr(cur, ':')) == NULL)
  586. goto parse_failed;
  587. *delim = 0;
  588. np->remote_mac[3] = simple_strtol(cur, NULL, 16);
  589. cur = delim + 1;
  590. if ((delim = strchr(cur, ':')) == NULL)
  591. goto parse_failed;
  592. *delim = 0;
  593. np->remote_mac[4] = simple_strtol(cur, NULL, 16);
  594. cur = delim + 1;
  595. np->remote_mac[5] = simple_strtol(cur, NULL, 16);
  596. }
  597. netpoll_print_options(np);
  598. return 0;
  599. parse_failed:
  600. printk(KERN_INFO "%s: couldn't parse config at '%s'!\n",
  601. np->name, cur);
  602. return -1;
  603. }
  604. EXPORT_SYMBOL(netpoll_parse_options);
  605. int __netpoll_setup(struct netpoll *np)
  606. {
  607. struct net_device *ndev = np->dev;
  608. struct netpoll_info *npinfo;
  609. const struct net_device_ops *ops;
  610. unsigned long flags;
  611. int err;
  612. if ((ndev->priv_flags & IFF_DISABLE_NETPOLL) ||
  613. !ndev->netdev_ops->ndo_poll_controller) {
  614. printk(KERN_ERR "%s: %s doesn't support polling, aborting.\n",
  615. np->name, np->dev_name);
  616. err = -ENOTSUPP;
  617. goto out;
  618. }
  619. if (!ndev->npinfo) {
  620. npinfo = kmalloc(sizeof(*npinfo), GFP_KERNEL);
  621. if (!npinfo) {
  622. err = -ENOMEM;
  623. goto out;
  624. }
  625. npinfo->rx_flags = 0;
  626. INIT_LIST_HEAD(&npinfo->rx_np);
  627. spin_lock_init(&npinfo->rx_lock);
  628. skb_queue_head_init(&npinfo->arp_tx);
  629. skb_queue_head_init(&npinfo->txq);
  630. INIT_DELAYED_WORK(&npinfo->tx_work, queue_process);
  631. atomic_set(&npinfo->refcnt, 1);
  632. ops = np->dev->netdev_ops;
  633. if (ops->ndo_netpoll_setup) {
  634. err = ops->ndo_netpoll_setup(ndev, npinfo);
  635. if (err)
  636. goto free_npinfo;
  637. }
  638. } else {
  639. npinfo = ndev->npinfo;
  640. atomic_inc(&npinfo->refcnt);
  641. }
  642. npinfo->netpoll = np;
  643. if (np->rx_hook) {
  644. spin_lock_irqsave(&npinfo->rx_lock, flags);
  645. npinfo->rx_flags |= NETPOLL_RX_ENABLED;
  646. list_add_tail(&np->rx, &npinfo->rx_np);
  647. spin_unlock_irqrestore(&npinfo->rx_lock, flags);
  648. }
  649. /* last thing to do is link it to the net device structure */
  650. rcu_assign_pointer(ndev->npinfo, npinfo);
  651. return 0;
  652. free_npinfo:
  653. kfree(npinfo);
  654. out:
  655. return err;
  656. }
  657. EXPORT_SYMBOL_GPL(__netpoll_setup);
  658. int netpoll_setup(struct netpoll *np)
  659. {
  660. struct net_device *ndev = NULL;
  661. struct in_device *in_dev;
  662. int err;
  663. if (np->dev_name)
  664. ndev = dev_get_by_name(&init_net, np->dev_name);
  665. if (!ndev) {
  666. printk(KERN_ERR "%s: %s doesn't exist, aborting.\n",
  667. np->name, np->dev_name);
  668. return -ENODEV;
  669. }
  670. if (!netif_running(ndev)) {
  671. unsigned long atmost, atleast;
  672. printk(KERN_INFO "%s: device %s not up yet, forcing it\n",
  673. np->name, np->dev_name);
  674. rtnl_lock();
  675. err = dev_open(ndev);
  676. rtnl_unlock();
  677. if (err) {
  678. printk(KERN_ERR "%s: failed to open %s\n",
  679. np->name, ndev->name);
  680. goto put;
  681. }
  682. atleast = jiffies + HZ/10;
  683. atmost = jiffies + carrier_timeout * HZ;
  684. while (!netif_carrier_ok(ndev)) {
  685. if (time_after(jiffies, atmost)) {
  686. printk(KERN_NOTICE
  687. "%s: timeout waiting for carrier\n",
  688. np->name);
  689. break;
  690. }
  691. msleep(1);
  692. }
  693. /* If carrier appears to come up instantly, we don't
  694. * trust it and pause so that we don't pump all our
  695. * queued console messages into the bitbucket.
  696. */
  697. if (time_before(jiffies, atleast)) {
  698. printk(KERN_NOTICE "%s: carrier detect appears"
  699. " untrustworthy, waiting 4 seconds\n",
  700. np->name);
  701. msleep(4000);
  702. }
  703. }
  704. if (!np->local_ip) {
  705. rcu_read_lock();
  706. in_dev = __in_dev_get_rcu(ndev);
  707. if (!in_dev || !in_dev->ifa_list) {
  708. rcu_read_unlock();
  709. printk(KERN_ERR "%s: no IP address for %s, aborting\n",
  710. np->name, np->dev_name);
  711. err = -EDESTADDRREQ;
  712. goto put;
  713. }
  714. np->local_ip = in_dev->ifa_list->ifa_local;
  715. rcu_read_unlock();
  716. printk(KERN_INFO "%s: local IP %pI4\n", np->name, &np->local_ip);
  717. }
  718. np->dev = ndev;
  719. /* fill up the skb queue */
  720. refill_skbs();
  721. rtnl_lock();
  722. err = __netpoll_setup(np);
  723. rtnl_unlock();
  724. if (err)
  725. goto put;
  726. return 0;
  727. put:
  728. dev_put(ndev);
  729. return err;
  730. }
  731. EXPORT_SYMBOL(netpoll_setup);
  732. static int __init netpoll_init(void)
  733. {
  734. skb_queue_head_init(&skb_pool);
  735. return 0;
  736. }
  737. core_initcall(netpoll_init);
  738. void __netpoll_cleanup(struct netpoll *np)
  739. {
  740. struct netpoll_info *npinfo;
  741. unsigned long flags;
  742. npinfo = np->dev->npinfo;
  743. if (!npinfo)
  744. return;
  745. if (!list_empty(&npinfo->rx_np)) {
  746. spin_lock_irqsave(&npinfo->rx_lock, flags);
  747. list_del(&np->rx);
  748. if (list_empty(&npinfo->rx_np))
  749. npinfo->rx_flags &= ~NETPOLL_RX_ENABLED;
  750. spin_unlock_irqrestore(&npinfo->rx_lock, flags);
  751. }
  752. if (atomic_dec_and_test(&npinfo->refcnt)) {
  753. const struct net_device_ops *ops;
  754. ops = np->dev->netdev_ops;
  755. if (ops->ndo_netpoll_cleanup)
  756. ops->ndo_netpoll_cleanup(np->dev);
  757. rcu_assign_pointer(np->dev->npinfo, NULL);
  758. /* avoid racing with NAPI reading npinfo */
  759. synchronize_rcu_bh();
  760. skb_queue_purge(&npinfo->arp_tx);
  761. skb_queue_purge(&npinfo->txq);
  762. cancel_delayed_work_sync(&npinfo->tx_work);
  763. /* clean after last, unfinished work */
  764. __skb_queue_purge(&npinfo->txq);
  765. kfree(npinfo);
  766. }
  767. }
  768. EXPORT_SYMBOL_GPL(__netpoll_cleanup);
  769. void netpoll_cleanup(struct netpoll *np)
  770. {
  771. if (!np->dev)
  772. return;
  773. rtnl_lock();
  774. __netpoll_cleanup(np);
  775. rtnl_unlock();
  776. dev_put(np->dev);
  777. np->dev = NULL;
  778. }
  779. EXPORT_SYMBOL(netpoll_cleanup);
  780. int netpoll_trap(void)
  781. {
  782. return atomic_read(&trapped);
  783. }
  784. EXPORT_SYMBOL(netpoll_trap);
  785. void netpoll_set_trap(int trap)
  786. {
  787. if (trap)
  788. atomic_inc(&trapped);
  789. else
  790. atomic_dec(&trapped);
  791. }
  792. EXPORT_SYMBOL(netpoll_set_trap);