netpoll.c 22 KB

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