netpoll.c 22 KB

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