netpoll.c 22 KB

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