netpoll.c 22 KB

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