netpoll.c 20 KB

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