netpoll.c 22 KB

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