netpoll.c 21 KB

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