netpoll.c 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860
  1. // SPDX-License-Identifier: GPL-2.0-only
  2. /*
  3. * Common framework for low-level network console, dump, and debugger code
  4. *
  5. * Sep 8 2003 Matt Mackall <mpm@selenic.com>
  6. *
  7. * based on the netconsole code from:
  8. *
  9. * Copyright (C) 2001 Ingo Molnar <mingo@redhat.com>
  10. * Copyright (C) 2002 Red Hat, Inc.
  11. */
  12. #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
  13. #include <linux/moduleparam.h>
  14. #include <linux/kernel.h>
  15. #include <linux/netdevice.h>
  16. #include <linux/etherdevice.h>
  17. #include <linux/string.h>
  18. #include <linux/if_arp.h>
  19. #include <linux/inetdevice.h>
  20. #include <linux/inet.h>
  21. #include <linux/interrupt.h>
  22. #include <linux/netpoll.h>
  23. #include <linux/sched.h>
  24. #include <linux/delay.h>
  25. #include <linux/rcupdate.h>
  26. #include <linux/workqueue.h>
  27. #include <linux/slab.h>
  28. #include <linux/export.h>
  29. #include <linux/if_vlan.h>
  30. #include <net/tcp.h>
  31. #include <net/udp.h>
  32. #include <net/addrconf.h>
  33. #include <net/ndisc.h>
  34. #include <net/ip6_checksum.h>
  35. #include <linux/unaligned.h>
  36. #include <trace/events/napi.h>
  37. #include <linux/kconfig.h>
  38. /*
  39. * We maintain a small pool of fully-sized skbs, to make sure the
  40. * message gets out even in extreme OOM situations.
  41. */
  42. #define MAX_UDP_CHUNK 1460
  43. #define MAX_SKBS 32
  44. #define USEC_PER_POLL 50
  45. #define MAX_SKB_SIZE \
  46. (sizeof(struct ethhdr) + \
  47. sizeof(struct iphdr) + \
  48. sizeof(struct udphdr) + \
  49. MAX_UDP_CHUNK)
  50. static void zap_completion_queue(void);
  51. static unsigned int carrier_timeout = 4;
  52. module_param(carrier_timeout, uint, 0644);
  53. static netdev_tx_t netpoll_start_xmit(struct sk_buff *skb,
  54. struct net_device *dev,
  55. struct netdev_queue *txq)
  56. {
  57. netdev_tx_t status = NETDEV_TX_OK;
  58. netdev_features_t features;
  59. features = netif_skb_features(skb);
  60. if (skb_vlan_tag_present(skb) &&
  61. !vlan_hw_offload_capable(features, skb->vlan_proto)) {
  62. skb = __vlan_hwaccel_push_inside(skb);
  63. if (unlikely(!skb)) {
  64. /* This is actually a packet drop, but we
  65. * don't want the code that calls this
  66. * function to try and operate on a NULL skb.
  67. */
  68. goto out;
  69. }
  70. }
  71. status = netdev_start_xmit(skb, dev, txq, false);
  72. out:
  73. return status;
  74. }
  75. static void queue_process(struct work_struct *work)
  76. {
  77. struct netpoll_info *npinfo =
  78. container_of(work, struct netpoll_info, tx_work.work);
  79. struct sk_buff *skb;
  80. unsigned long flags;
  81. while ((skb = skb_dequeue(&npinfo->txq))) {
  82. struct net_device *dev = skb->dev;
  83. struct netdev_queue *txq;
  84. unsigned int q_index;
  85. if (!netif_device_present(dev) || !netif_running(dev)) {
  86. kfree_skb(skb);
  87. continue;
  88. }
  89. local_irq_save(flags);
  90. /* check if skb->queue_mapping is still valid */
  91. q_index = skb_get_queue_mapping(skb);
  92. if (unlikely(q_index >= dev->real_num_tx_queues)) {
  93. q_index = q_index % dev->real_num_tx_queues;
  94. skb_set_queue_mapping(skb, q_index);
  95. }
  96. txq = netdev_get_tx_queue(dev, q_index);
  97. HARD_TX_LOCK(dev, txq, smp_processor_id());
  98. if (netif_xmit_frozen_or_stopped(txq) ||
  99. !dev_xmit_complete(netpoll_start_xmit(skb, dev, txq))) {
  100. skb_queue_head(&npinfo->txq, skb);
  101. HARD_TX_UNLOCK(dev, txq);
  102. local_irq_restore(flags);
  103. schedule_delayed_work(&npinfo->tx_work, HZ/10);
  104. return;
  105. }
  106. HARD_TX_UNLOCK(dev, txq);
  107. local_irq_restore(flags);
  108. }
  109. }
  110. static int netif_local_xmit_active(struct net_device *dev)
  111. {
  112. int i;
  113. for (i = 0; i < dev->num_tx_queues; i++) {
  114. struct netdev_queue *txq = netdev_get_tx_queue(dev, i);
  115. if (netif_tx_owned(txq, smp_processor_id()))
  116. return 1;
  117. }
  118. return 0;
  119. }
  120. static void poll_one_napi(struct napi_struct *napi)
  121. {
  122. int work;
  123. /* If we set this bit but see that it has already been set,
  124. * that indicates that napi has been disabled and we need
  125. * to abort this operation
  126. */
  127. if (test_and_set_bit(NAPI_STATE_NPSVC, &napi->state))
  128. return;
  129. /* We explicitly pass the polling call a budget of 0 to
  130. * indicate that we are clearing the Tx path only.
  131. */
  132. work = napi->poll(napi, 0);
  133. WARN_ONCE(work, "%pS exceeded budget in poll\n", napi->poll);
  134. trace_napi_poll(napi, work, 0);
  135. clear_bit(NAPI_STATE_NPSVC, &napi->state);
  136. }
  137. static void poll_napi(struct net_device *dev)
  138. {
  139. struct napi_struct *napi;
  140. int cpu = smp_processor_id();
  141. list_for_each_entry_rcu(napi, &dev->napi_list, dev_list) {
  142. if (cmpxchg(&napi->poll_owner, -1, cpu) == -1) {
  143. poll_one_napi(napi);
  144. smp_store_release(&napi->poll_owner, -1);
  145. }
  146. }
  147. }
  148. void netpoll_poll_dev(struct net_device *dev)
  149. {
  150. struct netpoll_info *ni = rcu_dereference_bh(dev->npinfo);
  151. const struct net_device_ops *ops;
  152. /* Don't do any rx activity if the dev_lock mutex is held
  153. * the dev_open/close paths use this to block netpoll activity
  154. * while changing device state
  155. */
  156. if (!ni || down_trylock(&ni->dev_lock))
  157. return;
  158. /* Some drivers will take the same locks in poll and xmit,
  159. * we can't poll if local CPU is already in xmit.
  160. */
  161. if (!netif_running(dev) || netif_local_xmit_active(dev)) {
  162. up(&ni->dev_lock);
  163. return;
  164. }
  165. ops = dev->netdev_ops;
  166. if (ops->ndo_poll_controller)
  167. ops->ndo_poll_controller(dev);
  168. poll_napi(dev);
  169. up(&ni->dev_lock);
  170. zap_completion_queue();
  171. }
  172. EXPORT_SYMBOL(netpoll_poll_dev);
  173. void netpoll_poll_disable(struct net_device *dev)
  174. {
  175. struct netpoll_info *ni;
  176. might_sleep();
  177. ni = rtnl_dereference(dev->npinfo);
  178. if (ni)
  179. down(&ni->dev_lock);
  180. }
  181. void netpoll_poll_enable(struct net_device *dev)
  182. {
  183. struct netpoll_info *ni;
  184. ni = rtnl_dereference(dev->npinfo);
  185. if (ni)
  186. up(&ni->dev_lock);
  187. }
  188. static void refill_skbs(struct netpoll *np)
  189. {
  190. struct sk_buff_head *skb_pool;
  191. struct sk_buff *skb;
  192. skb_pool = &np->skb_pool;
  193. while (READ_ONCE(skb_pool->qlen) < MAX_SKBS) {
  194. skb = alloc_skb(MAX_SKB_SIZE, GFP_ATOMIC);
  195. if (!skb)
  196. break;
  197. skb_queue_tail(skb_pool, skb);
  198. }
  199. }
  200. static void zap_completion_queue(void)
  201. {
  202. unsigned long flags;
  203. struct softnet_data *sd = &get_cpu_var(softnet_data);
  204. if (sd->completion_queue) {
  205. struct sk_buff *clist;
  206. local_irq_save(flags);
  207. clist = sd->completion_queue;
  208. sd->completion_queue = NULL;
  209. local_irq_restore(flags);
  210. while (clist != NULL) {
  211. struct sk_buff *skb = clist;
  212. clist = clist->next;
  213. if (!skb_irq_freeable(skb)) {
  214. refcount_set(&skb->users, 1);
  215. dev_kfree_skb_any(skb); /* put this one back */
  216. } else {
  217. __kfree_skb(skb);
  218. }
  219. }
  220. }
  221. put_cpu_var(softnet_data);
  222. }
  223. static struct sk_buff *find_skb(struct netpoll *np, int len, int reserve)
  224. {
  225. int count = 0;
  226. struct sk_buff *skb;
  227. zap_completion_queue();
  228. repeat:
  229. skb = alloc_skb(len, GFP_ATOMIC);
  230. if (!skb) {
  231. skb = skb_dequeue(&np->skb_pool);
  232. schedule_work(&np->refill_wq);
  233. }
  234. if (!skb) {
  235. if (++count < 10) {
  236. netpoll_poll_dev(np->dev);
  237. goto repeat;
  238. }
  239. return NULL;
  240. }
  241. refcount_set(&skb->users, 1);
  242. skb_reserve(skb, reserve);
  243. return skb;
  244. }
  245. static int netpoll_owner_active(struct net_device *dev)
  246. {
  247. struct napi_struct *napi;
  248. list_for_each_entry_rcu(napi, &dev->napi_list, dev_list) {
  249. if (READ_ONCE(napi->poll_owner) == smp_processor_id())
  250. return 1;
  251. }
  252. return 0;
  253. }
  254. /* call with IRQ disabled */
  255. static netdev_tx_t __netpoll_send_skb(struct netpoll *np, struct sk_buff *skb)
  256. {
  257. netdev_tx_t status = NETDEV_TX_BUSY;
  258. netdev_tx_t ret = NET_XMIT_DROP;
  259. struct net_device *dev;
  260. unsigned long tries;
  261. /* It is up to the caller to keep npinfo alive. */
  262. struct netpoll_info *npinfo;
  263. lockdep_assert_irqs_disabled();
  264. dev = np->dev;
  265. rcu_read_lock();
  266. npinfo = rcu_dereference_bh(dev->npinfo);
  267. if (!npinfo || !netif_running(dev) || !netif_device_present(dev)) {
  268. dev_kfree_skb_irq(skb);
  269. goto out;
  270. }
  271. /* don't get messages out of order, and no recursion */
  272. if (skb_queue_len(&npinfo->txq) == 0 && !netpoll_owner_active(dev)) {
  273. struct netdev_queue *txq;
  274. txq = netdev_core_pick_tx(dev, skb, NULL);
  275. /* try until next clock tick */
  276. for (tries = jiffies_to_usecs(1)/USEC_PER_POLL;
  277. tries > 0; --tries) {
  278. if (HARD_TX_TRYLOCK(dev, txq)) {
  279. if (!netif_xmit_stopped(txq))
  280. status = netpoll_start_xmit(skb, dev, txq);
  281. HARD_TX_UNLOCK(dev, txq);
  282. if (dev_xmit_complete(status))
  283. break;
  284. }
  285. /* tickle device maybe there is some cleanup */
  286. netpoll_poll_dev(np->dev);
  287. udelay(USEC_PER_POLL);
  288. }
  289. WARN_ONCE(!irqs_disabled(),
  290. "netpoll_send_skb_on_dev(): %s enabled interrupts in poll (%pS)\n",
  291. dev->name, dev->netdev_ops->ndo_start_xmit);
  292. }
  293. if (!dev_xmit_complete(status)) {
  294. skb_queue_tail(&npinfo->txq, skb);
  295. schedule_delayed_work(&npinfo->tx_work,0);
  296. }
  297. ret = NETDEV_TX_OK;
  298. out:
  299. rcu_read_unlock();
  300. return ret;
  301. }
  302. static void netpoll_udp_checksum(struct netpoll *np, struct sk_buff *skb,
  303. int len)
  304. {
  305. struct udphdr *udph;
  306. int udp_len;
  307. udp_len = len + sizeof(struct udphdr);
  308. udph = udp_hdr(skb);
  309. /* check needs to be set, since it will be consumed in csum_partial */
  310. udph->check = 0;
  311. if (np->ipv6)
  312. udph->check = csum_ipv6_magic(&np->local_ip.in6,
  313. &np->remote_ip.in6,
  314. udp_len, IPPROTO_UDP,
  315. csum_partial(udph, udp_len, 0));
  316. else
  317. udph->check = csum_tcpudp_magic(np->local_ip.ip,
  318. np->remote_ip.ip,
  319. udp_len, IPPROTO_UDP,
  320. csum_partial(udph, udp_len, 0));
  321. if (udph->check == 0)
  322. udph->check = CSUM_MANGLED_0;
  323. }
  324. netdev_tx_t netpoll_send_skb(struct netpoll *np, struct sk_buff *skb)
  325. {
  326. unsigned long flags;
  327. netdev_tx_t ret;
  328. if (unlikely(!np)) {
  329. dev_kfree_skb_irq(skb);
  330. ret = NET_XMIT_DROP;
  331. } else {
  332. local_irq_save(flags);
  333. ret = __netpoll_send_skb(np, skb);
  334. local_irq_restore(flags);
  335. }
  336. return ret;
  337. }
  338. EXPORT_SYMBOL(netpoll_send_skb);
  339. static void push_ipv6(struct netpoll *np, struct sk_buff *skb, int len)
  340. {
  341. struct ipv6hdr *ip6h;
  342. skb_push(skb, sizeof(struct ipv6hdr));
  343. skb_reset_network_header(skb);
  344. ip6h = ipv6_hdr(skb);
  345. /* ip6h->version = 6; ip6h->priority = 0; */
  346. *(unsigned char *)ip6h = 0x60;
  347. ip6h->flow_lbl[0] = 0;
  348. ip6h->flow_lbl[1] = 0;
  349. ip6h->flow_lbl[2] = 0;
  350. ip6h->payload_len = htons(sizeof(struct udphdr) + len);
  351. ip6h->nexthdr = IPPROTO_UDP;
  352. ip6h->hop_limit = 32;
  353. ip6h->saddr = np->local_ip.in6;
  354. ip6h->daddr = np->remote_ip.in6;
  355. skb->protocol = htons(ETH_P_IPV6);
  356. }
  357. static void push_ipv4(struct netpoll *np, struct sk_buff *skb, int len)
  358. {
  359. static atomic_t ip_ident;
  360. struct iphdr *iph;
  361. int ip_len;
  362. ip_len = len + sizeof(struct udphdr) + sizeof(struct iphdr);
  363. skb_push(skb, sizeof(struct iphdr));
  364. skb_reset_network_header(skb);
  365. iph = ip_hdr(skb);
  366. /* iph->version = 4; iph->ihl = 5; */
  367. *(unsigned char *)iph = 0x45;
  368. iph->tos = 0;
  369. put_unaligned(htons(ip_len), &iph->tot_len);
  370. iph->id = htons(atomic_inc_return(&ip_ident));
  371. iph->frag_off = 0;
  372. iph->ttl = 64;
  373. iph->protocol = IPPROTO_UDP;
  374. iph->check = 0;
  375. put_unaligned(np->local_ip.ip, &iph->saddr);
  376. put_unaligned(np->remote_ip.ip, &iph->daddr);
  377. iph->check = ip_fast_csum((unsigned char *)iph, iph->ihl);
  378. skb->protocol = htons(ETH_P_IP);
  379. }
  380. static void push_udp(struct netpoll *np, struct sk_buff *skb, int len)
  381. {
  382. struct udphdr *udph;
  383. int udp_len;
  384. udp_len = len + sizeof(struct udphdr);
  385. skb_push(skb, sizeof(struct udphdr));
  386. skb_reset_transport_header(skb);
  387. udph = udp_hdr(skb);
  388. udph->source = htons(np->local_port);
  389. udph->dest = htons(np->remote_port);
  390. udph->len = htons(udp_len);
  391. netpoll_udp_checksum(np, skb, len);
  392. }
  393. static void push_eth(struct netpoll *np, struct sk_buff *skb)
  394. {
  395. struct ethhdr *eth;
  396. eth = skb_push(skb, ETH_HLEN);
  397. skb_reset_mac_header(skb);
  398. ether_addr_copy(eth->h_source, np->dev->dev_addr);
  399. ether_addr_copy(eth->h_dest, np->remote_mac);
  400. if (np->ipv6)
  401. eth->h_proto = htons(ETH_P_IPV6);
  402. else
  403. eth->h_proto = htons(ETH_P_IP);
  404. }
  405. int netpoll_send_udp(struct netpoll *np, const char *msg, int len)
  406. {
  407. int total_len, ip_len, udp_len;
  408. struct sk_buff *skb;
  409. if (!IS_ENABLED(CONFIG_PREEMPT_RT))
  410. WARN_ON_ONCE(!irqs_disabled());
  411. udp_len = len + sizeof(struct udphdr);
  412. if (np->ipv6)
  413. ip_len = udp_len + sizeof(struct ipv6hdr);
  414. else
  415. ip_len = udp_len + sizeof(struct iphdr);
  416. total_len = ip_len + LL_RESERVED_SPACE(np->dev);
  417. skb = find_skb(np, total_len + np->dev->needed_tailroom,
  418. total_len - len);
  419. if (!skb)
  420. return -ENOMEM;
  421. skb_copy_to_linear_data(skb, msg, len);
  422. skb_put(skb, len);
  423. push_udp(np, skb, len);
  424. if (np->ipv6)
  425. push_ipv6(np, skb, len);
  426. else
  427. push_ipv4(np, skb, len);
  428. push_eth(np, skb);
  429. skb->dev = np->dev;
  430. return (int)netpoll_send_skb(np, skb);
  431. }
  432. EXPORT_SYMBOL(netpoll_send_udp);
  433. static void skb_pool_flush(struct netpoll *np)
  434. {
  435. struct sk_buff_head *skb_pool;
  436. cancel_work_sync(&np->refill_wq);
  437. skb_pool = &np->skb_pool;
  438. skb_queue_purge_reason(skb_pool, SKB_CONSUMED);
  439. }
  440. static void refill_skbs_work_handler(struct work_struct *work)
  441. {
  442. struct netpoll *np =
  443. container_of(work, struct netpoll, refill_wq);
  444. refill_skbs(np);
  445. }
  446. int __netpoll_setup(struct netpoll *np, struct net_device *ndev)
  447. {
  448. struct netpoll_info *npinfo;
  449. const struct net_device_ops *ops;
  450. int err;
  451. skb_queue_head_init(&np->skb_pool);
  452. INIT_WORK(&np->refill_wq, refill_skbs_work_handler);
  453. if (ndev->priv_flags & IFF_DISABLE_NETPOLL) {
  454. np_err(np, "%s doesn't support polling, aborting\n",
  455. ndev->name);
  456. err = -ENOTSUPP;
  457. goto out;
  458. }
  459. npinfo = rtnl_dereference(ndev->npinfo);
  460. if (!npinfo) {
  461. npinfo = kmalloc_obj(*npinfo);
  462. if (!npinfo) {
  463. err = -ENOMEM;
  464. goto out;
  465. }
  466. sema_init(&npinfo->dev_lock, 1);
  467. skb_queue_head_init(&npinfo->txq);
  468. INIT_DELAYED_WORK(&npinfo->tx_work, queue_process);
  469. refcount_set(&npinfo->refcnt, 1);
  470. ops = ndev->netdev_ops;
  471. if (ops->ndo_netpoll_setup) {
  472. err = ops->ndo_netpoll_setup(ndev);
  473. if (err)
  474. goto free_npinfo;
  475. }
  476. } else {
  477. refcount_inc(&npinfo->refcnt);
  478. }
  479. np->dev = ndev;
  480. strscpy(np->dev_name, ndev->name, IFNAMSIZ);
  481. /* fill up the skb queue */
  482. refill_skbs(np);
  483. /* last thing to do is link it to the net device structure */
  484. rcu_assign_pointer(ndev->npinfo, npinfo);
  485. return 0;
  486. free_npinfo:
  487. kfree(npinfo);
  488. out:
  489. return err;
  490. }
  491. EXPORT_SYMBOL_GPL(__netpoll_setup);
  492. /*
  493. * Returns a pointer to a string representation of the identifier used
  494. * to select the egress interface for the given netpoll instance. buf
  495. * must be a buffer of length at least MAC_ADDR_STR_LEN + 1.
  496. */
  497. static char *egress_dev(struct netpoll *np, char *buf)
  498. {
  499. if (np->dev_name[0])
  500. return np->dev_name;
  501. snprintf(buf, MAC_ADDR_STR_LEN, "%pM", np->dev_mac);
  502. return buf;
  503. }
  504. static void netpoll_wait_carrier(struct netpoll *np, struct net_device *ndev,
  505. unsigned int timeout)
  506. {
  507. unsigned long atmost;
  508. atmost = jiffies + timeout * HZ;
  509. while (!netif_carrier_ok(ndev)) {
  510. if (time_after(jiffies, atmost)) {
  511. np_notice(np, "timeout waiting for carrier\n");
  512. break;
  513. }
  514. msleep(1);
  515. }
  516. }
  517. /*
  518. * Take the IPv6 from ndev and populate local_ip structure in netpoll
  519. */
  520. static int netpoll_take_ipv6(struct netpoll *np, struct net_device *ndev)
  521. {
  522. char buf[MAC_ADDR_STR_LEN + 1];
  523. int err = -EDESTADDRREQ;
  524. struct inet6_dev *idev;
  525. if (!IS_ENABLED(CONFIG_IPV6)) {
  526. np_err(np, "IPv6 is not supported %s, aborting\n",
  527. egress_dev(np, buf));
  528. return -EINVAL;
  529. }
  530. idev = __in6_dev_get(ndev);
  531. if (idev) {
  532. struct inet6_ifaddr *ifp;
  533. read_lock_bh(&idev->lock);
  534. list_for_each_entry(ifp, &idev->addr_list, if_list) {
  535. if (!!(ipv6_addr_type(&ifp->addr) & IPV6_ADDR_LINKLOCAL) !=
  536. !!(ipv6_addr_type(&np->remote_ip.in6) & IPV6_ADDR_LINKLOCAL))
  537. continue;
  538. /* Got the IP, let's return */
  539. np->local_ip.in6 = ifp->addr;
  540. err = 0;
  541. break;
  542. }
  543. read_unlock_bh(&idev->lock);
  544. }
  545. if (err) {
  546. np_err(np, "no IPv6 address for %s, aborting\n",
  547. egress_dev(np, buf));
  548. return err;
  549. }
  550. np_info(np, "local IPv6 %pI6c\n", &np->local_ip.in6);
  551. return 0;
  552. }
  553. /*
  554. * Take the IPv4 from ndev and populate local_ip structure in netpoll
  555. */
  556. static int netpoll_take_ipv4(struct netpoll *np, struct net_device *ndev)
  557. {
  558. char buf[MAC_ADDR_STR_LEN + 1];
  559. const struct in_ifaddr *ifa;
  560. struct in_device *in_dev;
  561. in_dev = __in_dev_get_rtnl(ndev);
  562. if (!in_dev) {
  563. np_err(np, "no IP address for %s, aborting\n",
  564. egress_dev(np, buf));
  565. return -EDESTADDRREQ;
  566. }
  567. ifa = rtnl_dereference(in_dev->ifa_list);
  568. if (!ifa) {
  569. np_err(np, "no IP address for %s, aborting\n",
  570. egress_dev(np, buf));
  571. return -EDESTADDRREQ;
  572. }
  573. np->local_ip.ip = ifa->ifa_local;
  574. np_info(np, "local IP %pI4\n", &np->local_ip.ip);
  575. return 0;
  576. }
  577. int netpoll_setup(struct netpoll *np)
  578. {
  579. struct net *net = current->nsproxy->net_ns;
  580. char buf[MAC_ADDR_STR_LEN + 1];
  581. struct net_device *ndev = NULL;
  582. bool ip_overwritten = false;
  583. int err;
  584. rtnl_lock();
  585. if (np->dev_name[0])
  586. ndev = __dev_get_by_name(net, np->dev_name);
  587. else if (is_valid_ether_addr(np->dev_mac))
  588. ndev = dev_getbyhwaddr(net, ARPHRD_ETHER, np->dev_mac);
  589. if (!ndev) {
  590. np_err(np, "%s doesn't exist, aborting\n", egress_dev(np, buf));
  591. err = -ENODEV;
  592. goto unlock;
  593. }
  594. netdev_hold(ndev, &np->dev_tracker, GFP_KERNEL);
  595. if (netdev_master_upper_dev_get(ndev)) {
  596. np_err(np, "%s is a slave device, aborting\n",
  597. egress_dev(np, buf));
  598. err = -EBUSY;
  599. goto put;
  600. }
  601. if (!netif_running(ndev)) {
  602. np_info(np, "device %s not up yet, forcing it\n",
  603. egress_dev(np, buf));
  604. err = dev_open(ndev, NULL);
  605. if (err) {
  606. np_err(np, "failed to open %s\n", ndev->name);
  607. goto put;
  608. }
  609. rtnl_unlock();
  610. netpoll_wait_carrier(np, ndev, carrier_timeout);
  611. rtnl_lock();
  612. }
  613. if (!np->local_ip.ip) {
  614. if (!np->ipv6) {
  615. err = netpoll_take_ipv4(np, ndev);
  616. if (err)
  617. goto put;
  618. } else {
  619. err = netpoll_take_ipv6(np, ndev);
  620. if (err)
  621. goto put;
  622. }
  623. ip_overwritten = true;
  624. }
  625. err = __netpoll_setup(np, ndev);
  626. if (err)
  627. goto flush;
  628. rtnl_unlock();
  629. /* Make sure all NAPI polls which started before dev->npinfo
  630. * was visible have exited before we start calling NAPI poll.
  631. * NAPI skips locking if dev->npinfo is NULL.
  632. */
  633. synchronize_rcu();
  634. return 0;
  635. flush:
  636. skb_pool_flush(np);
  637. put:
  638. DEBUG_NET_WARN_ON_ONCE(np->dev);
  639. if (ip_overwritten)
  640. memset(&np->local_ip, 0, sizeof(np->local_ip));
  641. netdev_put(ndev, &np->dev_tracker);
  642. unlock:
  643. rtnl_unlock();
  644. return err;
  645. }
  646. EXPORT_SYMBOL(netpoll_setup);
  647. static void rcu_cleanup_netpoll_info(struct rcu_head *rcu_head)
  648. {
  649. struct netpoll_info *npinfo =
  650. container_of(rcu_head, struct netpoll_info, rcu);
  651. skb_queue_purge(&npinfo->txq);
  652. /* we can't call cancel_delayed_work_sync here, as we are in softirq */
  653. cancel_delayed_work(&npinfo->tx_work);
  654. /* clean after last, unfinished work */
  655. __skb_queue_purge(&npinfo->txq);
  656. /* now cancel it again */
  657. cancel_delayed_work(&npinfo->tx_work);
  658. kfree(npinfo);
  659. }
  660. static void __netpoll_cleanup(struct netpoll *np)
  661. {
  662. struct netpoll_info *npinfo;
  663. npinfo = rtnl_dereference(np->dev->npinfo);
  664. if (!npinfo)
  665. return;
  666. /* At this point, there is a single npinfo instance per netdevice, and
  667. * its refcnt tracks how many netpoll structures are linked to it. We
  668. * only perform npinfo cleanup when the refcnt decrements to zero.
  669. */
  670. if (refcount_dec_and_test(&npinfo->refcnt)) {
  671. const struct net_device_ops *ops;
  672. ops = np->dev->netdev_ops;
  673. if (ops->ndo_netpoll_cleanup)
  674. ops->ndo_netpoll_cleanup(np->dev);
  675. RCU_INIT_POINTER(np->dev->npinfo, NULL);
  676. call_rcu(&npinfo->rcu, rcu_cleanup_netpoll_info);
  677. }
  678. skb_pool_flush(np);
  679. }
  680. void __netpoll_free(struct netpoll *np)
  681. {
  682. ASSERT_RTNL();
  683. /* Wait for transmitting packets to finish before freeing. */
  684. synchronize_net();
  685. __netpoll_cleanup(np);
  686. kfree(np);
  687. }
  688. EXPORT_SYMBOL_GPL(__netpoll_free);
  689. void do_netpoll_cleanup(struct netpoll *np)
  690. {
  691. __netpoll_cleanup(np);
  692. netdev_put(np->dev, &np->dev_tracker);
  693. np->dev = NULL;
  694. }
  695. EXPORT_SYMBOL(do_netpoll_cleanup);
  696. void netpoll_cleanup(struct netpoll *np)
  697. {
  698. rtnl_lock();
  699. if (!np->dev)
  700. goto out;
  701. do_netpoll_cleanup(np);
  702. out:
  703. rtnl_unlock();
  704. }
  705. EXPORT_SYMBOL(netpoll_cleanup);