netpoll.c 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947
  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, eth_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 = eth_len = udp_len + sizeof(*iph);
  302. total_len = eth_len + ETH_HLEN + NET_IP_ALIGN;
  303. skb = find_skb(np, total_len, total_len - len);
  304. if (!skb)
  305. return;
  306. skb_copy_to_linear_data(skb, msg, len);
  307. skb->len += len;
  308. skb_push(skb, sizeof(*udph));
  309. skb_reset_transport_header(skb);
  310. udph = udp_hdr(skb);
  311. udph->source = htons(np->local_port);
  312. udph->dest = htons(np->remote_port);
  313. udph->len = htons(udp_len);
  314. udph->check = 0;
  315. udph->check = csum_tcpudp_magic(np->local_ip,
  316. np->remote_ip,
  317. udp_len, IPPROTO_UDP,
  318. csum_partial(udph, udp_len, 0));
  319. if (udph->check == 0)
  320. udph->check = CSUM_MANGLED_0;
  321. skb_push(skb, sizeof(*iph));
  322. skb_reset_network_header(skb);
  323. iph = ip_hdr(skb);
  324. /* iph->version = 4; iph->ihl = 5; */
  325. put_unaligned(0x45, (unsigned char *)iph);
  326. iph->tos = 0;
  327. put_unaligned(htons(ip_len), &(iph->tot_len));
  328. iph->id = 0;
  329. iph->frag_off = 0;
  330. iph->ttl = 64;
  331. iph->protocol = IPPROTO_UDP;
  332. iph->check = 0;
  333. put_unaligned(np->local_ip, &(iph->saddr));
  334. put_unaligned(np->remote_ip, &(iph->daddr));
  335. iph->check = ip_fast_csum((unsigned char *)iph, iph->ihl);
  336. eth = (struct ethhdr *) skb_push(skb, ETH_HLEN);
  337. skb_reset_mac_header(skb);
  338. skb->protocol = eth->h_proto = htons(ETH_P_IP);
  339. memcpy(eth->h_source, np->dev->dev_addr, ETH_ALEN);
  340. memcpy(eth->h_dest, np->remote_mac, ETH_ALEN);
  341. skb->dev = np->dev;
  342. netpoll_send_skb(np, skb);
  343. }
  344. EXPORT_SYMBOL(netpoll_send_udp);
  345. static void arp_reply(struct sk_buff *skb)
  346. {
  347. struct netpoll_info *npinfo = skb->dev->npinfo;
  348. struct arphdr *arp;
  349. unsigned char *arp_ptr;
  350. int size, type = ARPOP_REPLY, ptype = ETH_P_ARP;
  351. __be32 sip, tip;
  352. unsigned char *sha;
  353. struct sk_buff *send_skb;
  354. struct netpoll *np, *tmp;
  355. unsigned long flags;
  356. int hlen, tlen;
  357. int hits = 0;
  358. if (list_empty(&npinfo->rx_np))
  359. return;
  360. /* Before checking the packet, we do some early
  361. inspection whether this is interesting at all */
  362. spin_lock_irqsave(&npinfo->rx_lock, flags);
  363. list_for_each_entry_safe(np, tmp, &npinfo->rx_np, rx) {
  364. if (np->dev == skb->dev)
  365. hits++;
  366. }
  367. spin_unlock_irqrestore(&npinfo->rx_lock, flags);
  368. /* No netpoll struct is using this dev */
  369. if (!hits)
  370. return;
  371. /* No arp on this interface */
  372. if (skb->dev->flags & IFF_NOARP)
  373. return;
  374. if (!pskb_may_pull(skb, arp_hdr_len(skb->dev)))
  375. return;
  376. skb_reset_network_header(skb);
  377. skb_reset_transport_header(skb);
  378. arp = arp_hdr(skb);
  379. if ((arp->ar_hrd != htons(ARPHRD_ETHER) &&
  380. arp->ar_hrd != htons(ARPHRD_IEEE802)) ||
  381. arp->ar_pro != htons(ETH_P_IP) ||
  382. arp->ar_op != htons(ARPOP_REQUEST))
  383. return;
  384. arp_ptr = (unsigned char *)(arp+1);
  385. /* save the location of the src hw addr */
  386. sha = arp_ptr;
  387. arp_ptr += skb->dev->addr_len;
  388. memcpy(&sip, arp_ptr, 4);
  389. arp_ptr += 4;
  390. /* If we actually cared about dst hw addr,
  391. it would get copied here */
  392. arp_ptr += skb->dev->addr_len;
  393. memcpy(&tip, arp_ptr, 4);
  394. /* Should we ignore arp? */
  395. if (ipv4_is_loopback(tip) || ipv4_is_multicast(tip))
  396. return;
  397. size = arp_hdr_len(skb->dev);
  398. spin_lock_irqsave(&npinfo->rx_lock, flags);
  399. list_for_each_entry_safe(np, tmp, &npinfo->rx_np, rx) {
  400. if (tip != np->local_ip)
  401. continue;
  402. hlen = LL_RESERVED_SPACE(np->dev);
  403. tlen = np->dev->needed_tailroom;
  404. send_skb = find_skb(np, size + hlen + tlen, hlen);
  405. if (!send_skb)
  406. continue;
  407. skb_reset_network_header(send_skb);
  408. arp = (struct arphdr *) skb_put(send_skb, size);
  409. send_skb->dev = skb->dev;
  410. send_skb->protocol = htons(ETH_P_ARP);
  411. /* Fill the device header for the ARP frame */
  412. if (dev_hard_header(send_skb, skb->dev, ptype,
  413. sha, np->dev->dev_addr,
  414. send_skb->len) < 0) {
  415. kfree_skb(send_skb);
  416. continue;
  417. }
  418. /*
  419. * Fill out the arp protocol part.
  420. *
  421. * we only support ethernet device type,
  422. * which (according to RFC 1390) should
  423. * always equal 1 (Ethernet).
  424. */
  425. arp->ar_hrd = htons(np->dev->type);
  426. arp->ar_pro = htons(ETH_P_IP);
  427. arp->ar_hln = np->dev->addr_len;
  428. arp->ar_pln = 4;
  429. arp->ar_op = htons(type);
  430. arp_ptr = (unsigned char *)(arp + 1);
  431. memcpy(arp_ptr, np->dev->dev_addr, np->dev->addr_len);
  432. arp_ptr += np->dev->addr_len;
  433. memcpy(arp_ptr, &tip, 4);
  434. arp_ptr += 4;
  435. memcpy(arp_ptr, sha, np->dev->addr_len);
  436. arp_ptr += np->dev->addr_len;
  437. memcpy(arp_ptr, &sip, 4);
  438. netpoll_send_skb(np, send_skb);
  439. /* If there are several rx_hooks for the same address,
  440. we're fine by sending a single reply */
  441. break;
  442. }
  443. spin_unlock_irqrestore(&npinfo->rx_lock, flags);
  444. }
  445. int __netpoll_rx(struct sk_buff *skb)
  446. {
  447. int proto, len, ulen;
  448. int hits = 0;
  449. const struct iphdr *iph;
  450. struct udphdr *uh;
  451. struct netpoll_info *npinfo = skb->dev->npinfo;
  452. struct netpoll *np, *tmp;
  453. if (list_empty(&npinfo->rx_np))
  454. goto out;
  455. if (skb->dev->type != ARPHRD_ETHER)
  456. goto out;
  457. /* check if netpoll clients need ARP */
  458. if (skb->protocol == htons(ETH_P_ARP) &&
  459. atomic_read(&trapped)) {
  460. skb_queue_tail(&npinfo->arp_tx, skb);
  461. return 1;
  462. }
  463. proto = ntohs(eth_hdr(skb)->h_proto);
  464. if (proto != ETH_P_IP)
  465. goto out;
  466. if (skb->pkt_type == PACKET_OTHERHOST)
  467. goto out;
  468. if (skb_shared(skb))
  469. goto out;
  470. if (!pskb_may_pull(skb, sizeof(struct iphdr)))
  471. goto out;
  472. iph = (struct iphdr *)skb->data;
  473. if (iph->ihl < 5 || iph->version != 4)
  474. goto out;
  475. if (!pskb_may_pull(skb, iph->ihl*4))
  476. goto out;
  477. iph = (struct iphdr *)skb->data;
  478. if (ip_fast_csum((u8 *)iph, iph->ihl) != 0)
  479. goto out;
  480. len = ntohs(iph->tot_len);
  481. if (skb->len < len || len < iph->ihl*4)
  482. goto out;
  483. /*
  484. * Our transport medium may have padded the buffer out.
  485. * Now We trim to the true length of the frame.
  486. */
  487. if (pskb_trim_rcsum(skb, len))
  488. goto out;
  489. iph = (struct iphdr *)skb->data;
  490. if (iph->protocol != IPPROTO_UDP)
  491. goto out;
  492. len -= iph->ihl*4;
  493. uh = (struct udphdr *)(((char *)iph) + iph->ihl*4);
  494. ulen = ntohs(uh->len);
  495. if (ulen != len)
  496. goto out;
  497. if (checksum_udp(skb, uh, ulen, iph->saddr, iph->daddr))
  498. goto out;
  499. list_for_each_entry_safe(np, tmp, &npinfo->rx_np, rx) {
  500. if (np->local_ip && np->local_ip != iph->daddr)
  501. continue;
  502. if (np->remote_ip && np->remote_ip != iph->saddr)
  503. continue;
  504. if (np->local_port && np->local_port != ntohs(uh->dest))
  505. continue;
  506. np->rx_hook(np, ntohs(uh->source),
  507. (char *)(uh+1),
  508. ulen - sizeof(struct udphdr));
  509. hits++;
  510. }
  511. if (!hits)
  512. goto out;
  513. kfree_skb(skb);
  514. return 1;
  515. out:
  516. if (atomic_read(&trapped)) {
  517. kfree_skb(skb);
  518. return 1;
  519. }
  520. return 0;
  521. }
  522. void netpoll_print_options(struct netpoll *np)
  523. {
  524. np_info(np, "local port %d\n", np->local_port);
  525. np_info(np, "local IP %pI4\n", &np->local_ip);
  526. np_info(np, "interface '%s'\n", np->dev_name);
  527. np_info(np, "remote port %d\n", np->remote_port);
  528. np_info(np, "remote IP %pI4\n", &np->remote_ip);
  529. np_info(np, "remote ethernet address %pM\n", np->remote_mac);
  530. }
  531. EXPORT_SYMBOL(netpoll_print_options);
  532. int netpoll_parse_options(struct netpoll *np, char *opt)
  533. {
  534. char *cur=opt, *delim;
  535. if (*cur != '@') {
  536. if ((delim = strchr(cur, '@')) == NULL)
  537. goto parse_failed;
  538. *delim = 0;
  539. np->local_port = simple_strtol(cur, NULL, 10);
  540. cur = delim;
  541. }
  542. cur++;
  543. if (*cur != '/') {
  544. if ((delim = strchr(cur, '/')) == NULL)
  545. goto parse_failed;
  546. *delim = 0;
  547. np->local_ip = in_aton(cur);
  548. cur = delim;
  549. }
  550. cur++;
  551. if (*cur != ',') {
  552. /* parse out dev name */
  553. if ((delim = strchr(cur, ',')) == NULL)
  554. goto parse_failed;
  555. *delim = 0;
  556. strlcpy(np->dev_name, cur, sizeof(np->dev_name));
  557. cur = delim;
  558. }
  559. cur++;
  560. if (*cur != '@') {
  561. /* dst port */
  562. if ((delim = strchr(cur, '@')) == NULL)
  563. goto parse_failed;
  564. *delim = 0;
  565. if (*cur == ' ' || *cur == '\t')
  566. np_info(np, "warning: whitespace is not allowed\n");
  567. np->remote_port = simple_strtol(cur, NULL, 10);
  568. cur = delim;
  569. }
  570. cur++;
  571. /* dst ip */
  572. if ((delim = strchr(cur, '/')) == NULL)
  573. goto parse_failed;
  574. *delim = 0;
  575. np->remote_ip = in_aton(cur);
  576. cur = delim + 1;
  577. if (*cur != 0) {
  578. /* MAC address */
  579. if (!mac_pton(cur, np->remote_mac))
  580. goto parse_failed;
  581. }
  582. netpoll_print_options(np);
  583. return 0;
  584. parse_failed:
  585. np_info(np, "couldn't parse config at '%s'!\n", cur);
  586. return -1;
  587. }
  588. EXPORT_SYMBOL(netpoll_parse_options);
  589. int __netpoll_setup(struct netpoll *np)
  590. {
  591. struct net_device *ndev = np->dev;
  592. struct netpoll_info *npinfo;
  593. const struct net_device_ops *ops;
  594. unsigned long flags;
  595. int err;
  596. if ((ndev->priv_flags & IFF_DISABLE_NETPOLL) ||
  597. !ndev->netdev_ops->ndo_poll_controller) {
  598. np_err(np, "%s doesn't support polling, aborting\n",
  599. np->dev_name);
  600. err = -ENOTSUPP;
  601. goto out;
  602. }
  603. if (!ndev->npinfo) {
  604. npinfo = kmalloc(sizeof(*npinfo), GFP_KERNEL);
  605. if (!npinfo) {
  606. err = -ENOMEM;
  607. goto out;
  608. }
  609. npinfo->rx_flags = 0;
  610. INIT_LIST_HEAD(&npinfo->rx_np);
  611. spin_lock_init(&npinfo->rx_lock);
  612. skb_queue_head_init(&npinfo->arp_tx);
  613. skb_queue_head_init(&npinfo->txq);
  614. INIT_DELAYED_WORK(&npinfo->tx_work, queue_process);
  615. atomic_set(&npinfo->refcnt, 1);
  616. ops = np->dev->netdev_ops;
  617. if (ops->ndo_netpoll_setup) {
  618. err = ops->ndo_netpoll_setup(ndev, npinfo);
  619. if (err)
  620. goto free_npinfo;
  621. }
  622. } else {
  623. npinfo = ndev->npinfo;
  624. atomic_inc(&npinfo->refcnt);
  625. }
  626. npinfo->netpoll = np;
  627. if (np->rx_hook) {
  628. spin_lock_irqsave(&npinfo->rx_lock, flags);
  629. npinfo->rx_flags |= NETPOLL_RX_ENABLED;
  630. list_add_tail(&np->rx, &npinfo->rx_np);
  631. spin_unlock_irqrestore(&npinfo->rx_lock, flags);
  632. }
  633. /* last thing to do is link it to the net device structure */
  634. rcu_assign_pointer(ndev->npinfo, npinfo);
  635. return 0;
  636. free_npinfo:
  637. kfree(npinfo);
  638. out:
  639. return err;
  640. }
  641. EXPORT_SYMBOL_GPL(__netpoll_setup);
  642. int netpoll_setup(struct netpoll *np)
  643. {
  644. struct net_device *ndev = NULL;
  645. struct in_device *in_dev;
  646. int err;
  647. if (np->dev_name)
  648. ndev = dev_get_by_name(&init_net, np->dev_name);
  649. if (!ndev) {
  650. np_err(np, "%s doesn't exist, aborting\n", np->dev_name);
  651. return -ENODEV;
  652. }
  653. if (ndev->master) {
  654. np_err(np, "%s is a slave device, aborting\n", np->dev_name);
  655. err = -EBUSY;
  656. goto put;
  657. }
  658. if (!netif_running(ndev)) {
  659. unsigned long atmost, atleast;
  660. np_info(np, "device %s not up yet, forcing it\n", np->dev_name);
  661. rtnl_lock();
  662. err = dev_open(ndev);
  663. rtnl_unlock();
  664. if (err) {
  665. np_err(np, "failed to open %s\n", ndev->name);
  666. goto put;
  667. }
  668. atleast = jiffies + HZ/10;
  669. atmost = jiffies + carrier_timeout * HZ;
  670. while (!netif_carrier_ok(ndev)) {
  671. if (time_after(jiffies, atmost)) {
  672. np_notice(np, "timeout waiting for carrier\n");
  673. break;
  674. }
  675. msleep(1);
  676. }
  677. /* If carrier appears to come up instantly, we don't
  678. * trust it and pause so that we don't pump all our
  679. * queued console messages into the bitbucket.
  680. */
  681. if (time_before(jiffies, atleast)) {
  682. np_notice(np, "carrier detect appears untrustworthy, waiting 4 seconds\n");
  683. msleep(4000);
  684. }
  685. }
  686. if (!np->local_ip) {
  687. rcu_read_lock();
  688. in_dev = __in_dev_get_rcu(ndev);
  689. if (!in_dev || !in_dev->ifa_list) {
  690. rcu_read_unlock();
  691. np_err(np, "no IP address for %s, aborting\n",
  692. np->dev_name);
  693. err = -EDESTADDRREQ;
  694. goto put;
  695. }
  696. np->local_ip = in_dev->ifa_list->ifa_local;
  697. rcu_read_unlock();
  698. np_info(np, "local IP %pI4\n", &np->local_ip);
  699. }
  700. np->dev = ndev;
  701. /* fill up the skb queue */
  702. refill_skbs();
  703. rtnl_lock();
  704. err = __netpoll_setup(np);
  705. rtnl_unlock();
  706. if (err)
  707. goto put;
  708. return 0;
  709. put:
  710. dev_put(ndev);
  711. return err;
  712. }
  713. EXPORT_SYMBOL(netpoll_setup);
  714. static int __init netpoll_init(void)
  715. {
  716. skb_queue_head_init(&skb_pool);
  717. return 0;
  718. }
  719. core_initcall(netpoll_init);
  720. void __netpoll_cleanup(struct netpoll *np)
  721. {
  722. struct netpoll_info *npinfo;
  723. unsigned long flags;
  724. npinfo = np->dev->npinfo;
  725. if (!npinfo)
  726. return;
  727. if (!list_empty(&npinfo->rx_np)) {
  728. spin_lock_irqsave(&npinfo->rx_lock, flags);
  729. list_del(&np->rx);
  730. if (list_empty(&npinfo->rx_np))
  731. npinfo->rx_flags &= ~NETPOLL_RX_ENABLED;
  732. spin_unlock_irqrestore(&npinfo->rx_lock, flags);
  733. }
  734. if (atomic_dec_and_test(&npinfo->refcnt)) {
  735. const struct net_device_ops *ops;
  736. ops = np->dev->netdev_ops;
  737. if (ops->ndo_netpoll_cleanup)
  738. ops->ndo_netpoll_cleanup(np->dev);
  739. RCU_INIT_POINTER(np->dev->npinfo, NULL);
  740. /* avoid racing with NAPI reading npinfo */
  741. synchronize_rcu_bh();
  742. skb_queue_purge(&npinfo->arp_tx);
  743. skb_queue_purge(&npinfo->txq);
  744. cancel_delayed_work_sync(&npinfo->tx_work);
  745. /* clean after last, unfinished work */
  746. __skb_queue_purge(&npinfo->txq);
  747. kfree(npinfo);
  748. }
  749. }
  750. EXPORT_SYMBOL_GPL(__netpoll_cleanup);
  751. void netpoll_cleanup(struct netpoll *np)
  752. {
  753. if (!np->dev)
  754. return;
  755. rtnl_lock();
  756. __netpoll_cleanup(np);
  757. rtnl_unlock();
  758. dev_put(np->dev);
  759. np->dev = NULL;
  760. }
  761. EXPORT_SYMBOL(netpoll_cleanup);
  762. int netpoll_trap(void)
  763. {
  764. return atomic_read(&trapped);
  765. }
  766. EXPORT_SYMBOL(netpoll_trap);
  767. void netpoll_set_trap(int trap)
  768. {
  769. if (trap)
  770. atomic_inc(&trapped);
  771. else
  772. atomic_dec(&trapped);
  773. }
  774. EXPORT_SYMBOL(netpoll_set_trap);