netpoll.c 31 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316
  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/kernel.h>
  14. #include <linux/netdevice.h>
  15. #include <linux/etherdevice.h>
  16. #include <linux/string.h>
  17. #include <linux/if_arp.h>
  18. #include <linux/inetdevice.h>
  19. #include <linux/inet.h>
  20. #include <linux/interrupt.h>
  21. #include <linux/netpoll.h>
  22. #include <linux/sched.h>
  23. #include <linux/delay.h>
  24. #include <linux/rcupdate.h>
  25. #include <linux/workqueue.h>
  26. #include <linux/slab.h>
  27. #include <linux/export.h>
  28. #include <linux/if_vlan.h>
  29. #include <net/tcp.h>
  30. #include <net/udp.h>
  31. #include <net/addrconf.h>
  32. #include <net/ndisc.h>
  33. #include <net/ip6_checksum.h>
  34. #include <asm/unaligned.h>
  35. #include <trace/events/napi.h>
  36. /*
  37. * We maintain a small pool of fully-sized skbs, to make sure the
  38. * message gets out even in extreme OOM situations.
  39. */
  40. #define MAX_UDP_CHUNK 1460
  41. #define MAX_SKBS 32
  42. static struct sk_buff_head skb_pool;
  43. static atomic_t trapped;
  44. DEFINE_STATIC_SRCU(netpoll_srcu);
  45. #define USEC_PER_POLL 50
  46. #define NETPOLL_RX_ENABLED 1
  47. #define NETPOLL_RX_DROP 2
  48. #define MAX_SKB_SIZE \
  49. (sizeof(struct ethhdr) + \
  50. sizeof(struct iphdr) + \
  51. sizeof(struct udphdr) + \
  52. MAX_UDP_CHUNK)
  53. static void zap_completion_queue(void);
  54. static void netpoll_neigh_reply(struct sk_buff *skb, struct netpoll_info *npinfo);
  55. static void netpoll_async_cleanup(struct work_struct *work);
  56. static unsigned int carrier_timeout = 4;
  57. module_param(carrier_timeout, uint, 0644);
  58. #define np_info(np, fmt, ...) \
  59. pr_info("%s: " fmt, np->name, ##__VA_ARGS__)
  60. #define np_err(np, fmt, ...) \
  61. pr_err("%s: " fmt, np->name, ##__VA_ARGS__)
  62. #define np_notice(np, fmt, ...) \
  63. pr_notice("%s: " fmt, np->name, ##__VA_ARGS__)
  64. static void queue_process(struct work_struct *work)
  65. {
  66. struct netpoll_info *npinfo =
  67. container_of(work, struct netpoll_info, tx_work.work);
  68. struct sk_buff *skb;
  69. unsigned long flags;
  70. while ((skb = skb_dequeue(&npinfo->txq))) {
  71. struct net_device *dev = skb->dev;
  72. const struct net_device_ops *ops = dev->netdev_ops;
  73. struct netdev_queue *txq;
  74. if (!netif_device_present(dev) || !netif_running(dev)) {
  75. __kfree_skb(skb);
  76. continue;
  77. }
  78. txq = netdev_get_tx_queue(dev, skb_get_queue_mapping(skb));
  79. local_irq_save(flags);
  80. __netif_tx_lock(txq, smp_processor_id());
  81. if (netif_xmit_frozen_or_stopped(txq) ||
  82. ops->ndo_start_xmit(skb, dev) != NETDEV_TX_OK) {
  83. skb_queue_head(&npinfo->txq, skb);
  84. __netif_tx_unlock(txq);
  85. local_irq_restore(flags);
  86. schedule_delayed_work(&npinfo->tx_work, HZ/10);
  87. return;
  88. }
  89. __netif_tx_unlock(txq);
  90. local_irq_restore(flags);
  91. }
  92. }
  93. static __sum16 checksum_udp(struct sk_buff *skb, struct udphdr *uh,
  94. unsigned short ulen, __be32 saddr, __be32 daddr)
  95. {
  96. __wsum psum;
  97. if (uh->check == 0 || skb_csum_unnecessary(skb))
  98. return 0;
  99. psum = csum_tcpudp_nofold(saddr, daddr, ulen, IPPROTO_UDP, 0);
  100. if (skb->ip_summed == CHECKSUM_COMPLETE &&
  101. !csum_fold(csum_add(psum, skb->csum)))
  102. return 0;
  103. skb->csum = psum;
  104. return __skb_checksum_complete(skb);
  105. }
  106. /*
  107. * Check whether delayed processing was scheduled for our NIC. If so,
  108. * we attempt to grab the poll lock and use ->poll() to pump the card.
  109. * If this fails, either we've recursed in ->poll() or it's already
  110. * running on another CPU.
  111. *
  112. * Note: we don't mask interrupts with this lock because we're using
  113. * trylock here and interrupts are already disabled in the softirq
  114. * case. Further, we test the poll_owner to avoid recursion on UP
  115. * systems where the lock doesn't exist.
  116. *
  117. * In cases where there is bi-directional communications, reading only
  118. * one message at a time can lead to packets being dropped by the
  119. * network adapter, forcing superfluous retries and possibly timeouts.
  120. * Thus, we set our budget to greater than 1.
  121. */
  122. static int poll_one_napi(struct netpoll_info *npinfo,
  123. struct napi_struct *napi, int budget)
  124. {
  125. int work;
  126. /* net_rx_action's ->poll() invocations and our's are
  127. * synchronized by this test which is only made while
  128. * holding the napi->poll_lock.
  129. */
  130. if (!test_bit(NAPI_STATE_SCHED, &napi->state))
  131. return budget;
  132. npinfo->rx_flags |= NETPOLL_RX_DROP;
  133. atomic_inc(&trapped);
  134. set_bit(NAPI_STATE_NPSVC, &napi->state);
  135. work = napi->poll(napi, budget);
  136. trace_napi_poll(napi);
  137. clear_bit(NAPI_STATE_NPSVC, &napi->state);
  138. atomic_dec(&trapped);
  139. npinfo->rx_flags &= ~NETPOLL_RX_DROP;
  140. return budget - work;
  141. }
  142. static void poll_napi(struct net_device *dev)
  143. {
  144. struct napi_struct *napi;
  145. int budget = 16;
  146. list_for_each_entry(napi, &dev->napi_list, dev_list) {
  147. if (napi->poll_owner != smp_processor_id() &&
  148. spin_trylock(&napi->poll_lock)) {
  149. budget = poll_one_napi(rcu_dereference_bh(dev->npinfo),
  150. napi, budget);
  151. spin_unlock(&napi->poll_lock);
  152. if (!budget)
  153. break;
  154. }
  155. }
  156. }
  157. static void service_neigh_queue(struct netpoll_info *npi)
  158. {
  159. if (npi) {
  160. struct sk_buff *skb;
  161. while ((skb = skb_dequeue(&npi->neigh_tx)))
  162. netpoll_neigh_reply(skb, npi);
  163. }
  164. }
  165. static void netpoll_poll_dev(struct net_device *dev)
  166. {
  167. const struct net_device_ops *ops;
  168. struct netpoll_info *ni = rcu_dereference_bh(dev->npinfo);
  169. /* Don't do any rx activity if the dev_lock mutex is held
  170. * the dev_open/close paths use this to block netpoll activity
  171. * while changing device state
  172. */
  173. if (down_trylock(&ni->dev_lock))
  174. return;
  175. if (!netif_running(dev)) {
  176. up(&ni->dev_lock);
  177. return;
  178. }
  179. ops = dev->netdev_ops;
  180. if (!ops->ndo_poll_controller) {
  181. up(&ni->dev_lock);
  182. return;
  183. }
  184. /* Process pending work on NIC */
  185. ops->ndo_poll_controller(dev);
  186. poll_napi(dev);
  187. up(&ni->dev_lock);
  188. if (dev->flags & IFF_SLAVE) {
  189. if (ni) {
  190. struct net_device *bond_dev;
  191. struct sk_buff *skb;
  192. struct netpoll_info *bond_ni;
  193. bond_dev = netdev_master_upper_dev_get_rcu(dev);
  194. bond_ni = rcu_dereference_bh(bond_dev->npinfo);
  195. while ((skb = skb_dequeue(&ni->neigh_tx))) {
  196. skb->dev = bond_dev;
  197. skb_queue_tail(&bond_ni->neigh_tx, skb);
  198. }
  199. }
  200. }
  201. service_neigh_queue(ni);
  202. zap_completion_queue();
  203. }
  204. void netpoll_rx_disable(struct net_device *dev)
  205. {
  206. struct netpoll_info *ni;
  207. int idx;
  208. might_sleep();
  209. idx = srcu_read_lock(&netpoll_srcu);
  210. ni = srcu_dereference(dev->npinfo, &netpoll_srcu);
  211. if (ni)
  212. down(&ni->dev_lock);
  213. srcu_read_unlock(&netpoll_srcu, idx);
  214. }
  215. EXPORT_SYMBOL(netpoll_rx_disable);
  216. void netpoll_rx_enable(struct net_device *dev)
  217. {
  218. struct netpoll_info *ni;
  219. rcu_read_lock();
  220. ni = rcu_dereference(dev->npinfo);
  221. if (ni)
  222. up(&ni->dev_lock);
  223. rcu_read_unlock();
  224. }
  225. EXPORT_SYMBOL(netpoll_rx_enable);
  226. static void refill_skbs(void)
  227. {
  228. struct sk_buff *skb;
  229. unsigned long flags;
  230. spin_lock_irqsave(&skb_pool.lock, flags);
  231. while (skb_pool.qlen < MAX_SKBS) {
  232. skb = alloc_skb(MAX_SKB_SIZE, GFP_ATOMIC);
  233. if (!skb)
  234. break;
  235. __skb_queue_tail(&skb_pool, skb);
  236. }
  237. spin_unlock_irqrestore(&skb_pool.lock, flags);
  238. }
  239. static void zap_completion_queue(void)
  240. {
  241. unsigned long flags;
  242. struct softnet_data *sd = &get_cpu_var(softnet_data);
  243. if (sd->completion_queue) {
  244. struct sk_buff *clist;
  245. local_irq_save(flags);
  246. clist = sd->completion_queue;
  247. sd->completion_queue = NULL;
  248. local_irq_restore(flags);
  249. while (clist != NULL) {
  250. struct sk_buff *skb = clist;
  251. clist = clist->next;
  252. if (skb->destructor) {
  253. atomic_inc(&skb->users);
  254. dev_kfree_skb_any(skb); /* put this one back */
  255. } else {
  256. __kfree_skb(skb);
  257. }
  258. }
  259. }
  260. put_cpu_var(softnet_data);
  261. }
  262. static struct sk_buff *find_skb(struct netpoll *np, int len, int reserve)
  263. {
  264. int count = 0;
  265. struct sk_buff *skb;
  266. zap_completion_queue();
  267. refill_skbs();
  268. repeat:
  269. skb = alloc_skb(len, GFP_ATOMIC);
  270. if (!skb)
  271. skb = skb_dequeue(&skb_pool);
  272. if (!skb) {
  273. if (++count < 10) {
  274. netpoll_poll_dev(np->dev);
  275. goto repeat;
  276. }
  277. return NULL;
  278. }
  279. atomic_set(&skb->users, 1);
  280. skb_reserve(skb, reserve);
  281. return skb;
  282. }
  283. static int netpoll_owner_active(struct net_device *dev)
  284. {
  285. struct napi_struct *napi;
  286. list_for_each_entry(napi, &dev->napi_list, dev_list) {
  287. if (napi->poll_owner == smp_processor_id())
  288. return 1;
  289. }
  290. return 0;
  291. }
  292. /* call with IRQ disabled */
  293. void netpoll_send_skb_on_dev(struct netpoll *np, struct sk_buff *skb,
  294. struct net_device *dev)
  295. {
  296. int status = NETDEV_TX_BUSY;
  297. unsigned long tries;
  298. const struct net_device_ops *ops = dev->netdev_ops;
  299. /* It is up to the caller to keep npinfo alive. */
  300. struct netpoll_info *npinfo;
  301. WARN_ON_ONCE(!irqs_disabled());
  302. npinfo = rcu_dereference_bh(np->dev->npinfo);
  303. if (!npinfo || !netif_running(dev) || !netif_device_present(dev)) {
  304. __kfree_skb(skb);
  305. return;
  306. }
  307. /* don't get messages out of order, and no recursion */
  308. if (skb_queue_len(&npinfo->txq) == 0 && !netpoll_owner_active(dev)) {
  309. struct netdev_queue *txq;
  310. txq = netdev_pick_tx(dev, skb);
  311. /* try until next clock tick */
  312. for (tries = jiffies_to_usecs(1)/USEC_PER_POLL;
  313. tries > 0; --tries) {
  314. if (__netif_tx_trylock(txq)) {
  315. if (!netif_xmit_stopped(txq)) {
  316. if (vlan_tx_tag_present(skb) &&
  317. !vlan_hw_offload_capable(netif_skb_features(skb),
  318. skb->vlan_proto)) {
  319. skb = __vlan_put_tag(skb, skb->vlan_proto, vlan_tx_tag_get(skb));
  320. if (unlikely(!skb))
  321. break;
  322. skb->vlan_tci = 0;
  323. }
  324. status = ops->ndo_start_xmit(skb, dev);
  325. if (status == NETDEV_TX_OK)
  326. txq_trans_update(txq);
  327. }
  328. __netif_tx_unlock(txq);
  329. if (status == NETDEV_TX_OK)
  330. break;
  331. }
  332. /* tickle device maybe there is some cleanup */
  333. netpoll_poll_dev(np->dev);
  334. udelay(USEC_PER_POLL);
  335. }
  336. WARN_ONCE(!irqs_disabled(),
  337. "netpoll_send_skb_on_dev(): %s enabled interrupts in poll (%pF)\n",
  338. dev->name, ops->ndo_start_xmit);
  339. }
  340. if (status != NETDEV_TX_OK) {
  341. skb_queue_tail(&npinfo->txq, skb);
  342. schedule_delayed_work(&npinfo->tx_work,0);
  343. }
  344. }
  345. EXPORT_SYMBOL(netpoll_send_skb_on_dev);
  346. void netpoll_send_udp(struct netpoll *np, const char *msg, int len)
  347. {
  348. int total_len, ip_len, udp_len;
  349. struct sk_buff *skb;
  350. struct udphdr *udph;
  351. struct iphdr *iph;
  352. struct ethhdr *eth;
  353. static atomic_t ip_ident;
  354. struct ipv6hdr *ip6h;
  355. udp_len = len + sizeof(*udph);
  356. if (np->ipv6)
  357. ip_len = udp_len + sizeof(*ip6h);
  358. else
  359. ip_len = udp_len + sizeof(*iph);
  360. total_len = ip_len + LL_RESERVED_SPACE(np->dev);
  361. skb = find_skb(np, total_len + np->dev->needed_tailroom,
  362. total_len - len);
  363. if (!skb)
  364. return;
  365. skb_copy_to_linear_data(skb, msg, len);
  366. skb_put(skb, len);
  367. skb_push(skb, sizeof(*udph));
  368. skb_reset_transport_header(skb);
  369. udph = udp_hdr(skb);
  370. udph->source = htons(np->local_port);
  371. udph->dest = htons(np->remote_port);
  372. udph->len = htons(udp_len);
  373. if (np->ipv6) {
  374. udph->check = 0;
  375. udph->check = csum_ipv6_magic(&np->local_ip.in6,
  376. &np->remote_ip.in6,
  377. udp_len, IPPROTO_UDP,
  378. csum_partial(udph, udp_len, 0));
  379. if (udph->check == 0)
  380. udph->check = CSUM_MANGLED_0;
  381. skb_push(skb, sizeof(*ip6h));
  382. skb_reset_network_header(skb);
  383. ip6h = ipv6_hdr(skb);
  384. /* ip6h->version = 6; ip6h->priority = 0; */
  385. put_unaligned(0x60, (unsigned char *)ip6h);
  386. ip6h->flow_lbl[0] = 0;
  387. ip6h->flow_lbl[1] = 0;
  388. ip6h->flow_lbl[2] = 0;
  389. ip6h->payload_len = htons(sizeof(struct udphdr) + len);
  390. ip6h->nexthdr = IPPROTO_UDP;
  391. ip6h->hop_limit = 32;
  392. ip6h->saddr = np->local_ip.in6;
  393. ip6h->daddr = np->remote_ip.in6;
  394. eth = (struct ethhdr *) skb_push(skb, ETH_HLEN);
  395. skb_reset_mac_header(skb);
  396. skb->protocol = eth->h_proto = htons(ETH_P_IPV6);
  397. } else {
  398. udph->check = 0;
  399. udph->check = csum_tcpudp_magic(np->local_ip.ip,
  400. np->remote_ip.ip,
  401. udp_len, IPPROTO_UDP,
  402. csum_partial(udph, udp_len, 0));
  403. if (udph->check == 0)
  404. udph->check = CSUM_MANGLED_0;
  405. skb_push(skb, sizeof(*iph));
  406. skb_reset_network_header(skb);
  407. iph = ip_hdr(skb);
  408. /* iph->version = 4; iph->ihl = 5; */
  409. put_unaligned(0x45, (unsigned char *)iph);
  410. iph->tos = 0;
  411. put_unaligned(htons(ip_len), &(iph->tot_len));
  412. iph->id = htons(atomic_inc_return(&ip_ident));
  413. iph->frag_off = 0;
  414. iph->ttl = 64;
  415. iph->protocol = IPPROTO_UDP;
  416. iph->check = 0;
  417. put_unaligned(np->local_ip.ip, &(iph->saddr));
  418. put_unaligned(np->remote_ip.ip, &(iph->daddr));
  419. iph->check = ip_fast_csum((unsigned char *)iph, iph->ihl);
  420. eth = (struct ethhdr *) skb_push(skb, ETH_HLEN);
  421. skb_reset_mac_header(skb);
  422. skb->protocol = eth->h_proto = htons(ETH_P_IP);
  423. }
  424. memcpy(eth->h_source, np->dev->dev_addr, ETH_ALEN);
  425. memcpy(eth->h_dest, np->remote_mac, ETH_ALEN);
  426. skb->dev = np->dev;
  427. netpoll_send_skb(np, skb);
  428. }
  429. EXPORT_SYMBOL(netpoll_send_udp);
  430. static void netpoll_neigh_reply(struct sk_buff *skb, struct netpoll_info *npinfo)
  431. {
  432. int size, type = ARPOP_REPLY;
  433. __be32 sip, tip;
  434. unsigned char *sha;
  435. struct sk_buff *send_skb;
  436. struct netpoll *np, *tmp;
  437. unsigned long flags;
  438. int hlen, tlen;
  439. int hits = 0, proto;
  440. if (list_empty(&npinfo->rx_np))
  441. return;
  442. /* Before checking the packet, we do some early
  443. inspection whether this is interesting at all */
  444. spin_lock_irqsave(&npinfo->rx_lock, flags);
  445. list_for_each_entry_safe(np, tmp, &npinfo->rx_np, rx) {
  446. if (np->dev == skb->dev)
  447. hits++;
  448. }
  449. spin_unlock_irqrestore(&npinfo->rx_lock, flags);
  450. /* No netpoll struct is using this dev */
  451. if (!hits)
  452. return;
  453. proto = ntohs(eth_hdr(skb)->h_proto);
  454. if (proto == ETH_P_ARP) {
  455. struct arphdr *arp;
  456. unsigned char *arp_ptr;
  457. /* No arp on this interface */
  458. if (skb->dev->flags & IFF_NOARP)
  459. return;
  460. if (!pskb_may_pull(skb, arp_hdr_len(skb->dev)))
  461. return;
  462. skb_reset_network_header(skb);
  463. skb_reset_transport_header(skb);
  464. arp = arp_hdr(skb);
  465. if ((arp->ar_hrd != htons(ARPHRD_ETHER) &&
  466. arp->ar_hrd != htons(ARPHRD_IEEE802)) ||
  467. arp->ar_pro != htons(ETH_P_IP) ||
  468. arp->ar_op != htons(ARPOP_REQUEST))
  469. return;
  470. arp_ptr = (unsigned char *)(arp+1);
  471. /* save the location of the src hw addr */
  472. sha = arp_ptr;
  473. arp_ptr += skb->dev->addr_len;
  474. memcpy(&sip, arp_ptr, 4);
  475. arp_ptr += 4;
  476. /* If we actually cared about dst hw addr,
  477. it would get copied here */
  478. arp_ptr += skb->dev->addr_len;
  479. memcpy(&tip, arp_ptr, 4);
  480. /* Should we ignore arp? */
  481. if (ipv4_is_loopback(tip) || ipv4_is_multicast(tip))
  482. return;
  483. size = arp_hdr_len(skb->dev);
  484. spin_lock_irqsave(&npinfo->rx_lock, flags);
  485. list_for_each_entry_safe(np, tmp, &npinfo->rx_np, rx) {
  486. if (tip != np->local_ip.ip)
  487. continue;
  488. hlen = LL_RESERVED_SPACE(np->dev);
  489. tlen = np->dev->needed_tailroom;
  490. send_skb = find_skb(np, size + hlen + tlen, hlen);
  491. if (!send_skb)
  492. continue;
  493. skb_reset_network_header(send_skb);
  494. arp = (struct arphdr *) skb_put(send_skb, size);
  495. send_skb->dev = skb->dev;
  496. send_skb->protocol = htons(ETH_P_ARP);
  497. /* Fill the device header for the ARP frame */
  498. if (dev_hard_header(send_skb, skb->dev, ETH_P_ARP,
  499. sha, np->dev->dev_addr,
  500. send_skb->len) < 0) {
  501. kfree_skb(send_skb);
  502. continue;
  503. }
  504. /*
  505. * Fill out the arp protocol part.
  506. *
  507. * we only support ethernet device type,
  508. * which (according to RFC 1390) should
  509. * always equal 1 (Ethernet).
  510. */
  511. arp->ar_hrd = htons(np->dev->type);
  512. arp->ar_pro = htons(ETH_P_IP);
  513. arp->ar_hln = np->dev->addr_len;
  514. arp->ar_pln = 4;
  515. arp->ar_op = htons(type);
  516. arp_ptr = (unsigned char *)(arp + 1);
  517. memcpy(arp_ptr, np->dev->dev_addr, np->dev->addr_len);
  518. arp_ptr += np->dev->addr_len;
  519. memcpy(arp_ptr, &tip, 4);
  520. arp_ptr += 4;
  521. memcpy(arp_ptr, sha, np->dev->addr_len);
  522. arp_ptr += np->dev->addr_len;
  523. memcpy(arp_ptr, &sip, 4);
  524. netpoll_send_skb(np, send_skb);
  525. /* If there are several rx_skb_hooks for the same
  526. * address we're fine by sending a single reply
  527. */
  528. break;
  529. }
  530. spin_unlock_irqrestore(&npinfo->rx_lock, flags);
  531. } else if( proto == ETH_P_IPV6) {
  532. #if IS_ENABLED(CONFIG_IPV6)
  533. struct nd_msg *msg;
  534. u8 *lladdr = NULL;
  535. struct ipv6hdr *hdr;
  536. struct icmp6hdr *icmp6h;
  537. const struct in6_addr *saddr;
  538. const struct in6_addr *daddr;
  539. struct inet6_dev *in6_dev = NULL;
  540. struct in6_addr *target;
  541. in6_dev = in6_dev_get(skb->dev);
  542. if (!in6_dev || !in6_dev->cnf.accept_ra)
  543. return;
  544. if (!pskb_may_pull(skb, skb->len))
  545. return;
  546. msg = (struct nd_msg *)skb_transport_header(skb);
  547. __skb_push(skb, skb->data - skb_transport_header(skb));
  548. if (ipv6_hdr(skb)->hop_limit != 255)
  549. return;
  550. if (msg->icmph.icmp6_code != 0)
  551. return;
  552. if (msg->icmph.icmp6_type != NDISC_NEIGHBOUR_SOLICITATION)
  553. return;
  554. saddr = &ipv6_hdr(skb)->saddr;
  555. daddr = &ipv6_hdr(skb)->daddr;
  556. size = sizeof(struct icmp6hdr) + sizeof(struct in6_addr);
  557. spin_lock_irqsave(&npinfo->rx_lock, flags);
  558. list_for_each_entry_safe(np, tmp, &npinfo->rx_np, rx) {
  559. if (!ipv6_addr_equal(daddr, &np->local_ip.in6))
  560. continue;
  561. hlen = LL_RESERVED_SPACE(np->dev);
  562. tlen = np->dev->needed_tailroom;
  563. send_skb = find_skb(np, size + hlen + tlen, hlen);
  564. if (!send_skb)
  565. continue;
  566. send_skb->protocol = htons(ETH_P_IPV6);
  567. send_skb->dev = skb->dev;
  568. skb_reset_network_header(send_skb);
  569. hdr = (struct ipv6hdr *) skb_put(send_skb, sizeof(struct ipv6hdr));
  570. *(__be32*)hdr = htonl(0x60000000);
  571. hdr->payload_len = htons(size);
  572. hdr->nexthdr = IPPROTO_ICMPV6;
  573. hdr->hop_limit = 255;
  574. hdr->saddr = *saddr;
  575. hdr->daddr = *daddr;
  576. icmp6h = (struct icmp6hdr *) skb_put(send_skb, sizeof(struct icmp6hdr));
  577. icmp6h->icmp6_type = NDISC_NEIGHBOUR_ADVERTISEMENT;
  578. icmp6h->icmp6_router = 0;
  579. icmp6h->icmp6_solicited = 1;
  580. target = (struct in6_addr *) skb_put(send_skb, sizeof(struct in6_addr));
  581. *target = msg->target;
  582. icmp6h->icmp6_cksum = csum_ipv6_magic(saddr, daddr, size,
  583. IPPROTO_ICMPV6,
  584. csum_partial(icmp6h,
  585. size, 0));
  586. if (dev_hard_header(send_skb, skb->dev, ETH_P_IPV6,
  587. lladdr, np->dev->dev_addr,
  588. send_skb->len) < 0) {
  589. kfree_skb(send_skb);
  590. continue;
  591. }
  592. netpoll_send_skb(np, send_skb);
  593. /* If there are several rx_skb_hooks for the same
  594. * address, we're fine by sending a single reply
  595. */
  596. break;
  597. }
  598. spin_unlock_irqrestore(&npinfo->rx_lock, flags);
  599. #endif
  600. }
  601. }
  602. static bool pkt_is_ns(struct sk_buff *skb)
  603. {
  604. struct nd_msg *msg;
  605. struct ipv6hdr *hdr;
  606. if (skb->protocol != htons(ETH_P_ARP))
  607. return false;
  608. if (!pskb_may_pull(skb, sizeof(struct ipv6hdr) + sizeof(struct nd_msg)))
  609. return false;
  610. msg = (struct nd_msg *)skb_transport_header(skb);
  611. __skb_push(skb, skb->data - skb_transport_header(skb));
  612. hdr = ipv6_hdr(skb);
  613. if (hdr->nexthdr != IPPROTO_ICMPV6)
  614. return false;
  615. if (hdr->hop_limit != 255)
  616. return false;
  617. if (msg->icmph.icmp6_code != 0)
  618. return false;
  619. if (msg->icmph.icmp6_type != NDISC_NEIGHBOUR_SOLICITATION)
  620. return false;
  621. return true;
  622. }
  623. int __netpoll_rx(struct sk_buff *skb, struct netpoll_info *npinfo)
  624. {
  625. int proto, len, ulen, data_len;
  626. int hits = 0, offset;
  627. const struct iphdr *iph;
  628. struct udphdr *uh;
  629. struct netpoll *np, *tmp;
  630. uint16_t source;
  631. if (list_empty(&npinfo->rx_np))
  632. goto out;
  633. if (skb->dev->type != ARPHRD_ETHER)
  634. goto out;
  635. /* check if netpoll clients need ARP */
  636. if (skb->protocol == htons(ETH_P_ARP) && atomic_read(&trapped)) {
  637. skb_queue_tail(&npinfo->neigh_tx, skb);
  638. return 1;
  639. } else if (pkt_is_ns(skb) && atomic_read(&trapped)) {
  640. skb_queue_tail(&npinfo->neigh_tx, skb);
  641. return 1;
  642. }
  643. if (skb->protocol == cpu_to_be16(ETH_P_8021Q)) {
  644. skb = vlan_untag(skb);
  645. if (unlikely(!skb))
  646. goto out;
  647. }
  648. proto = ntohs(eth_hdr(skb)->h_proto);
  649. if (proto != ETH_P_IP && proto != ETH_P_IPV6)
  650. goto out;
  651. if (skb->pkt_type == PACKET_OTHERHOST)
  652. goto out;
  653. if (skb_shared(skb))
  654. goto out;
  655. if (proto == ETH_P_IP) {
  656. if (!pskb_may_pull(skb, sizeof(struct iphdr)))
  657. goto out;
  658. iph = (struct iphdr *)skb->data;
  659. if (iph->ihl < 5 || iph->version != 4)
  660. goto out;
  661. if (!pskb_may_pull(skb, iph->ihl*4))
  662. goto out;
  663. iph = (struct iphdr *)skb->data;
  664. if (ip_fast_csum((u8 *)iph, iph->ihl) != 0)
  665. goto out;
  666. len = ntohs(iph->tot_len);
  667. if (skb->len < len || len < iph->ihl*4)
  668. goto out;
  669. /*
  670. * Our transport medium may have padded the buffer out.
  671. * Now We trim to the true length of the frame.
  672. */
  673. if (pskb_trim_rcsum(skb, len))
  674. goto out;
  675. iph = (struct iphdr *)skb->data;
  676. if (iph->protocol != IPPROTO_UDP)
  677. goto out;
  678. len -= iph->ihl*4;
  679. uh = (struct udphdr *)(((char *)iph) + iph->ihl*4);
  680. offset = (unsigned char *)(uh + 1) - skb->data;
  681. ulen = ntohs(uh->len);
  682. data_len = skb->len - offset;
  683. source = ntohs(uh->source);
  684. if (ulen != len)
  685. goto out;
  686. if (checksum_udp(skb, uh, ulen, iph->saddr, iph->daddr))
  687. goto out;
  688. list_for_each_entry_safe(np, tmp, &npinfo->rx_np, rx) {
  689. if (np->local_ip.ip && np->local_ip.ip != iph->daddr)
  690. continue;
  691. if (np->remote_ip.ip && np->remote_ip.ip != iph->saddr)
  692. continue;
  693. if (np->local_port && np->local_port != ntohs(uh->dest))
  694. continue;
  695. np->rx_skb_hook(np, source, skb, offset, data_len);
  696. hits++;
  697. }
  698. } else {
  699. #if IS_ENABLED(CONFIG_IPV6)
  700. const struct ipv6hdr *ip6h;
  701. if (!pskb_may_pull(skb, sizeof(struct ipv6hdr)))
  702. goto out;
  703. ip6h = (struct ipv6hdr *)skb->data;
  704. if (ip6h->version != 6)
  705. goto out;
  706. len = ntohs(ip6h->payload_len);
  707. if (!len)
  708. goto out;
  709. if (len + sizeof(struct ipv6hdr) > skb->len)
  710. goto out;
  711. if (pskb_trim_rcsum(skb, len + sizeof(struct ipv6hdr)))
  712. goto out;
  713. ip6h = ipv6_hdr(skb);
  714. if (!pskb_may_pull(skb, sizeof(struct udphdr)))
  715. goto out;
  716. uh = udp_hdr(skb);
  717. offset = (unsigned char *)(uh + 1) - skb->data;
  718. ulen = ntohs(uh->len);
  719. data_len = skb->len - offset;
  720. source = ntohs(uh->source);
  721. if (ulen != skb->len)
  722. goto out;
  723. if (udp6_csum_init(skb, uh, IPPROTO_UDP))
  724. goto out;
  725. list_for_each_entry_safe(np, tmp, &npinfo->rx_np, rx) {
  726. if (!ipv6_addr_equal(&np->local_ip.in6, &ip6h->daddr))
  727. continue;
  728. if (!ipv6_addr_equal(&np->remote_ip.in6, &ip6h->saddr))
  729. continue;
  730. if (np->local_port && np->local_port != ntohs(uh->dest))
  731. continue;
  732. np->rx_skb_hook(np, source, skb, offset, data_len);
  733. hits++;
  734. }
  735. #endif
  736. }
  737. if (!hits)
  738. goto out;
  739. kfree_skb(skb);
  740. return 1;
  741. out:
  742. if (atomic_read(&trapped)) {
  743. kfree_skb(skb);
  744. return 1;
  745. }
  746. return 0;
  747. }
  748. void netpoll_print_options(struct netpoll *np)
  749. {
  750. np_info(np, "local port %d\n", np->local_port);
  751. if (np->ipv6)
  752. np_info(np, "local IPv6 address %pI6c\n", &np->local_ip.in6);
  753. else
  754. np_info(np, "local IPv4 address %pI4\n", &np->local_ip.ip);
  755. np_info(np, "interface '%s'\n", np->dev_name);
  756. np_info(np, "remote port %d\n", np->remote_port);
  757. if (np->ipv6)
  758. np_info(np, "remote IPv6 address %pI6c\n", &np->remote_ip.in6);
  759. else
  760. np_info(np, "remote IPv4 address %pI4\n", &np->remote_ip.ip);
  761. np_info(np, "remote ethernet address %pM\n", np->remote_mac);
  762. }
  763. EXPORT_SYMBOL(netpoll_print_options);
  764. static int netpoll_parse_ip_addr(const char *str, union inet_addr *addr)
  765. {
  766. const char *end;
  767. if (!strchr(str, ':') &&
  768. in4_pton(str, -1, (void *)addr, -1, &end) > 0) {
  769. if (!*end)
  770. return 0;
  771. }
  772. if (in6_pton(str, -1, addr->in6.s6_addr, -1, &end) > 0) {
  773. #if IS_ENABLED(CONFIG_IPV6)
  774. if (!*end)
  775. return 1;
  776. #else
  777. return -1;
  778. #endif
  779. }
  780. return -1;
  781. }
  782. int netpoll_parse_options(struct netpoll *np, char *opt)
  783. {
  784. char *cur=opt, *delim;
  785. int ipv6;
  786. if (*cur != '@') {
  787. if ((delim = strchr(cur, '@')) == NULL)
  788. goto parse_failed;
  789. *delim = 0;
  790. if (kstrtou16(cur, 10, &np->local_port))
  791. goto parse_failed;
  792. cur = delim;
  793. }
  794. cur++;
  795. if (*cur != '/') {
  796. if ((delim = strchr(cur, '/')) == NULL)
  797. goto parse_failed;
  798. *delim = 0;
  799. ipv6 = netpoll_parse_ip_addr(cur, &np->local_ip);
  800. if (ipv6 < 0)
  801. goto parse_failed;
  802. else
  803. np->ipv6 = (bool)ipv6;
  804. cur = delim;
  805. }
  806. cur++;
  807. if (*cur != ',') {
  808. /* parse out dev name */
  809. if ((delim = strchr(cur, ',')) == NULL)
  810. goto parse_failed;
  811. *delim = 0;
  812. strlcpy(np->dev_name, cur, sizeof(np->dev_name));
  813. cur = delim;
  814. }
  815. cur++;
  816. if (*cur != '@') {
  817. /* dst port */
  818. if ((delim = strchr(cur, '@')) == NULL)
  819. goto parse_failed;
  820. *delim = 0;
  821. if (*cur == ' ' || *cur == '\t')
  822. np_info(np, "warning: whitespace is not allowed\n");
  823. if (kstrtou16(cur, 10, &np->remote_port))
  824. goto parse_failed;
  825. cur = delim;
  826. }
  827. cur++;
  828. /* dst ip */
  829. if ((delim = strchr(cur, '/')) == NULL)
  830. goto parse_failed;
  831. *delim = 0;
  832. ipv6 = netpoll_parse_ip_addr(cur, &np->remote_ip);
  833. if (ipv6 < 0)
  834. goto parse_failed;
  835. else if (np->ipv6 != (bool)ipv6)
  836. goto parse_failed;
  837. else
  838. np->ipv6 = (bool)ipv6;
  839. cur = delim + 1;
  840. if (*cur != 0) {
  841. /* MAC address */
  842. if (!mac_pton(cur, np->remote_mac))
  843. goto parse_failed;
  844. }
  845. netpoll_print_options(np);
  846. return 0;
  847. parse_failed:
  848. np_info(np, "couldn't parse config at '%s'!\n", cur);
  849. return -1;
  850. }
  851. EXPORT_SYMBOL(netpoll_parse_options);
  852. int __netpoll_setup(struct netpoll *np, struct net_device *ndev, gfp_t gfp)
  853. {
  854. struct netpoll_info *npinfo;
  855. const struct net_device_ops *ops;
  856. unsigned long flags;
  857. int err;
  858. np->dev = ndev;
  859. strlcpy(np->dev_name, ndev->name, IFNAMSIZ);
  860. INIT_WORK(&np->cleanup_work, netpoll_async_cleanup);
  861. if ((ndev->priv_flags & IFF_DISABLE_NETPOLL) ||
  862. !ndev->netdev_ops->ndo_poll_controller) {
  863. np_err(np, "%s doesn't support polling, aborting\n",
  864. np->dev_name);
  865. err = -ENOTSUPP;
  866. goto out;
  867. }
  868. if (!ndev->npinfo) {
  869. npinfo = kmalloc(sizeof(*npinfo), gfp);
  870. if (!npinfo) {
  871. err = -ENOMEM;
  872. goto out;
  873. }
  874. npinfo->rx_flags = 0;
  875. INIT_LIST_HEAD(&npinfo->rx_np);
  876. spin_lock_init(&npinfo->rx_lock);
  877. sema_init(&npinfo->dev_lock, 1);
  878. skb_queue_head_init(&npinfo->neigh_tx);
  879. skb_queue_head_init(&npinfo->txq);
  880. INIT_DELAYED_WORK(&npinfo->tx_work, queue_process);
  881. atomic_set(&npinfo->refcnt, 1);
  882. ops = np->dev->netdev_ops;
  883. if (ops->ndo_netpoll_setup) {
  884. err = ops->ndo_netpoll_setup(ndev, npinfo, gfp);
  885. if (err)
  886. goto free_npinfo;
  887. }
  888. } else {
  889. npinfo = rtnl_dereference(ndev->npinfo);
  890. atomic_inc(&npinfo->refcnt);
  891. }
  892. npinfo->netpoll = np;
  893. if (np->rx_skb_hook) {
  894. spin_lock_irqsave(&npinfo->rx_lock, flags);
  895. npinfo->rx_flags |= NETPOLL_RX_ENABLED;
  896. list_add_tail(&np->rx, &npinfo->rx_np);
  897. spin_unlock_irqrestore(&npinfo->rx_lock, flags);
  898. }
  899. /* last thing to do is link it to the net device structure */
  900. rcu_assign_pointer(ndev->npinfo, npinfo);
  901. return 0;
  902. free_npinfo:
  903. kfree(npinfo);
  904. out:
  905. return err;
  906. }
  907. EXPORT_SYMBOL_GPL(__netpoll_setup);
  908. int netpoll_setup(struct netpoll *np)
  909. {
  910. struct net_device *ndev = NULL;
  911. struct in_device *in_dev;
  912. int err;
  913. rtnl_lock();
  914. if (np->dev_name) {
  915. struct net *net = current->nsproxy->net_ns;
  916. ndev = __dev_get_by_name(net, np->dev_name);
  917. }
  918. if (!ndev) {
  919. np_err(np, "%s doesn't exist, aborting\n", np->dev_name);
  920. err = -ENODEV;
  921. goto unlock;
  922. }
  923. dev_hold(ndev);
  924. if (netdev_master_upper_dev_get(ndev)) {
  925. np_err(np, "%s is a slave device, aborting\n", np->dev_name);
  926. err = -EBUSY;
  927. goto put;
  928. }
  929. if (!netif_running(ndev)) {
  930. unsigned long atmost, atleast;
  931. np_info(np, "device %s not up yet, forcing it\n", np->dev_name);
  932. err = dev_open(ndev);
  933. if (err) {
  934. np_err(np, "failed to open %s\n", ndev->name);
  935. goto put;
  936. }
  937. rtnl_unlock();
  938. atleast = jiffies + HZ/10;
  939. atmost = jiffies + carrier_timeout * HZ;
  940. while (!netif_carrier_ok(ndev)) {
  941. if (time_after(jiffies, atmost)) {
  942. np_notice(np, "timeout waiting for carrier\n");
  943. break;
  944. }
  945. msleep(1);
  946. }
  947. /* If carrier appears to come up instantly, we don't
  948. * trust it and pause so that we don't pump all our
  949. * queued console messages into the bitbucket.
  950. */
  951. if (time_before(jiffies, atleast)) {
  952. np_notice(np, "carrier detect appears untrustworthy, waiting 4 seconds\n");
  953. msleep(4000);
  954. }
  955. rtnl_lock();
  956. }
  957. if (!np->local_ip.ip) {
  958. if (!np->ipv6) {
  959. in_dev = __in_dev_get_rtnl(ndev);
  960. if (!in_dev || !in_dev->ifa_list) {
  961. np_err(np, "no IP address for %s, aborting\n",
  962. np->dev_name);
  963. err = -EDESTADDRREQ;
  964. goto put;
  965. }
  966. np->local_ip.ip = in_dev->ifa_list->ifa_local;
  967. np_info(np, "local IP %pI4\n", &np->local_ip.ip);
  968. } else {
  969. #if IS_ENABLED(CONFIG_IPV6)
  970. struct inet6_dev *idev;
  971. err = -EDESTADDRREQ;
  972. idev = __in6_dev_get(ndev);
  973. if (idev) {
  974. struct inet6_ifaddr *ifp;
  975. read_lock_bh(&idev->lock);
  976. list_for_each_entry(ifp, &idev->addr_list, if_list) {
  977. if (ipv6_addr_type(&ifp->addr) & IPV6_ADDR_LINKLOCAL)
  978. continue;
  979. np->local_ip.in6 = ifp->addr;
  980. err = 0;
  981. break;
  982. }
  983. read_unlock_bh(&idev->lock);
  984. }
  985. if (err) {
  986. np_err(np, "no IPv6 address for %s, aborting\n",
  987. np->dev_name);
  988. goto put;
  989. } else
  990. np_info(np, "local IPv6 %pI6c\n", &np->local_ip.in6);
  991. #else
  992. np_err(np, "IPv6 is not supported %s, aborting\n",
  993. np->dev_name);
  994. err = -EINVAL;
  995. goto put;
  996. #endif
  997. }
  998. }
  999. /* fill up the skb queue */
  1000. refill_skbs();
  1001. err = __netpoll_setup(np, ndev, GFP_KERNEL);
  1002. if (err)
  1003. goto put;
  1004. rtnl_unlock();
  1005. return 0;
  1006. put:
  1007. dev_put(ndev);
  1008. unlock:
  1009. rtnl_unlock();
  1010. return err;
  1011. }
  1012. EXPORT_SYMBOL(netpoll_setup);
  1013. static int __init netpoll_init(void)
  1014. {
  1015. skb_queue_head_init(&skb_pool);
  1016. return 0;
  1017. }
  1018. core_initcall(netpoll_init);
  1019. static void rcu_cleanup_netpoll_info(struct rcu_head *rcu_head)
  1020. {
  1021. struct netpoll_info *npinfo =
  1022. container_of(rcu_head, struct netpoll_info, rcu);
  1023. skb_queue_purge(&npinfo->neigh_tx);
  1024. skb_queue_purge(&npinfo->txq);
  1025. /* we can't call cancel_delayed_work_sync here, as we are in softirq */
  1026. cancel_delayed_work(&npinfo->tx_work);
  1027. /* clean after last, unfinished work */
  1028. __skb_queue_purge(&npinfo->txq);
  1029. /* now cancel it again */
  1030. cancel_delayed_work(&npinfo->tx_work);
  1031. kfree(npinfo);
  1032. }
  1033. void __netpoll_cleanup(struct netpoll *np)
  1034. {
  1035. struct netpoll_info *npinfo;
  1036. unsigned long flags;
  1037. /* rtnl_dereference would be preferable here but
  1038. * rcu_cleanup_netpoll path can put us in here safely without
  1039. * holding the rtnl, so plain rcu_dereference it is
  1040. */
  1041. npinfo = rtnl_dereference(np->dev->npinfo);
  1042. if (!npinfo)
  1043. return;
  1044. if (!list_empty(&npinfo->rx_np)) {
  1045. spin_lock_irqsave(&npinfo->rx_lock, flags);
  1046. list_del(&np->rx);
  1047. if (list_empty(&npinfo->rx_np))
  1048. npinfo->rx_flags &= ~NETPOLL_RX_ENABLED;
  1049. spin_unlock_irqrestore(&npinfo->rx_lock, flags);
  1050. }
  1051. synchronize_srcu(&netpoll_srcu);
  1052. if (atomic_dec_and_test(&npinfo->refcnt)) {
  1053. const struct net_device_ops *ops;
  1054. ops = np->dev->netdev_ops;
  1055. if (ops->ndo_netpoll_cleanup)
  1056. ops->ndo_netpoll_cleanup(np->dev);
  1057. rcu_assign_pointer(np->dev->npinfo, NULL);
  1058. call_rcu_bh(&npinfo->rcu, rcu_cleanup_netpoll_info);
  1059. }
  1060. }
  1061. EXPORT_SYMBOL_GPL(__netpoll_cleanup);
  1062. static void netpoll_async_cleanup(struct work_struct *work)
  1063. {
  1064. struct netpoll *np = container_of(work, struct netpoll, cleanup_work);
  1065. rtnl_lock();
  1066. __netpoll_cleanup(np);
  1067. rtnl_unlock();
  1068. kfree(np);
  1069. }
  1070. void __netpoll_free_async(struct netpoll *np)
  1071. {
  1072. schedule_work(&np->cleanup_work);
  1073. }
  1074. EXPORT_SYMBOL_GPL(__netpoll_free_async);
  1075. void netpoll_cleanup(struct netpoll *np)
  1076. {
  1077. rtnl_lock();
  1078. if (!np->dev)
  1079. goto out;
  1080. __netpoll_cleanup(np);
  1081. dev_put(np->dev);
  1082. np->dev = NULL;
  1083. out:
  1084. rtnl_unlock();
  1085. }
  1086. EXPORT_SYMBOL(netpoll_cleanup);
  1087. int netpoll_trap(void)
  1088. {
  1089. return atomic_read(&trapped);
  1090. }
  1091. EXPORT_SYMBOL(netpoll_trap);
  1092. void netpoll_set_trap(int trap)
  1093. {
  1094. if (trap)
  1095. atomic_inc(&trapped);
  1096. else
  1097. atomic_dec(&trapped);
  1098. }
  1099. EXPORT_SYMBOL(netpoll_set_trap);