netpoll.c 21 KB

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