netpoll.c 20 KB

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