netpoll.c 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869
  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. struct net_device *dev;
  266. unsigned long tries;
  267. /* It is up to the caller to keep npinfo alive. */
  268. struct netpoll_info *npinfo;
  269. lockdep_assert_irqs_disabled();
  270. dev = np->dev;
  271. npinfo = rcu_dereference_bh(dev->npinfo);
  272. if (!npinfo || !netif_running(dev) || !netif_device_present(dev)) {
  273. dev_kfree_skb_irq(skb);
  274. return NET_XMIT_DROP;
  275. }
  276. /* don't get messages out of order, and no recursion */
  277. if (skb_queue_len(&npinfo->txq) == 0 && !netpoll_owner_active(dev)) {
  278. struct netdev_queue *txq;
  279. txq = netdev_core_pick_tx(dev, skb, NULL);
  280. /* try until next clock tick */
  281. for (tries = jiffies_to_usecs(1)/USEC_PER_POLL;
  282. tries > 0; --tries) {
  283. if (HARD_TX_TRYLOCK(dev, txq)) {
  284. if (!netif_xmit_stopped(txq))
  285. status = netpoll_start_xmit(skb, dev, txq);
  286. HARD_TX_UNLOCK(dev, txq);
  287. if (dev_xmit_complete(status))
  288. break;
  289. }
  290. /* tickle device maybe there is some cleanup */
  291. netpoll_poll_dev(np->dev);
  292. udelay(USEC_PER_POLL);
  293. }
  294. WARN_ONCE(!irqs_disabled(),
  295. "netpoll_send_skb_on_dev(): %s enabled interrupts in poll (%pS)\n",
  296. dev->name, dev->netdev_ops->ndo_start_xmit);
  297. }
  298. if (!dev_xmit_complete(status)) {
  299. skb_queue_tail(&npinfo->txq, skb);
  300. schedule_delayed_work(&npinfo->tx_work,0);
  301. }
  302. return NETDEV_TX_OK;
  303. }
  304. netdev_tx_t netpoll_send_skb(struct netpoll *np, struct sk_buff *skb)
  305. {
  306. unsigned long flags;
  307. netdev_tx_t ret;
  308. if (unlikely(!np)) {
  309. dev_kfree_skb_irq(skb);
  310. ret = NET_XMIT_DROP;
  311. } else {
  312. local_irq_save(flags);
  313. ret = __netpoll_send_skb(np, skb);
  314. local_irq_restore(flags);
  315. }
  316. return ret;
  317. }
  318. EXPORT_SYMBOL(netpoll_send_skb);
  319. void netpoll_send_udp(struct netpoll *np, const char *msg, int len)
  320. {
  321. int total_len, ip_len, udp_len;
  322. struct sk_buff *skb;
  323. struct udphdr *udph;
  324. struct iphdr *iph;
  325. struct ethhdr *eth;
  326. static atomic_t ip_ident;
  327. struct ipv6hdr *ip6h;
  328. if (!IS_ENABLED(CONFIG_PREEMPT_RT))
  329. WARN_ON_ONCE(!irqs_disabled());
  330. udp_len = len + sizeof(*udph);
  331. if (np->ipv6)
  332. ip_len = udp_len + sizeof(*ip6h);
  333. else
  334. ip_len = udp_len + sizeof(*iph);
  335. total_len = ip_len + LL_RESERVED_SPACE(np->dev);
  336. skb = find_skb(np, total_len + np->dev->needed_tailroom,
  337. total_len - len);
  338. if (!skb)
  339. return;
  340. skb_copy_to_linear_data(skb, msg, len);
  341. skb_put(skb, len);
  342. skb_push(skb, sizeof(*udph));
  343. skb_reset_transport_header(skb);
  344. udph = udp_hdr(skb);
  345. udph->source = htons(np->local_port);
  346. udph->dest = htons(np->remote_port);
  347. udph->len = htons(udp_len);
  348. if (np->ipv6) {
  349. udph->check = 0;
  350. udph->check = csum_ipv6_magic(&np->local_ip.in6,
  351. &np->remote_ip.in6,
  352. udp_len, IPPROTO_UDP,
  353. csum_partial(udph, udp_len, 0));
  354. if (udph->check == 0)
  355. udph->check = CSUM_MANGLED_0;
  356. skb_push(skb, sizeof(*ip6h));
  357. skb_reset_network_header(skb);
  358. ip6h = ipv6_hdr(skb);
  359. /* ip6h->version = 6; ip6h->priority = 0; */
  360. *(unsigned char *)ip6h = 0x60;
  361. ip6h->flow_lbl[0] = 0;
  362. ip6h->flow_lbl[1] = 0;
  363. ip6h->flow_lbl[2] = 0;
  364. ip6h->payload_len = htons(sizeof(struct udphdr) + len);
  365. ip6h->nexthdr = IPPROTO_UDP;
  366. ip6h->hop_limit = 32;
  367. ip6h->saddr = np->local_ip.in6;
  368. ip6h->daddr = np->remote_ip.in6;
  369. eth = skb_push(skb, ETH_HLEN);
  370. skb_reset_mac_header(skb);
  371. skb->protocol = eth->h_proto = htons(ETH_P_IPV6);
  372. } else {
  373. udph->check = 0;
  374. udph->check = csum_tcpudp_magic(np->local_ip.ip,
  375. np->remote_ip.ip,
  376. udp_len, IPPROTO_UDP,
  377. csum_partial(udph, udp_len, 0));
  378. if (udph->check == 0)
  379. udph->check = CSUM_MANGLED_0;
  380. skb_push(skb, sizeof(*iph));
  381. skb_reset_network_header(skb);
  382. iph = ip_hdr(skb);
  383. /* iph->version = 4; iph->ihl = 5; */
  384. *(unsigned char *)iph = 0x45;
  385. iph->tos = 0;
  386. put_unaligned(htons(ip_len), &(iph->tot_len));
  387. iph->id = htons(atomic_inc_return(&ip_ident));
  388. iph->frag_off = 0;
  389. iph->ttl = 64;
  390. iph->protocol = IPPROTO_UDP;
  391. iph->check = 0;
  392. put_unaligned(np->local_ip.ip, &(iph->saddr));
  393. put_unaligned(np->remote_ip.ip, &(iph->daddr));
  394. iph->check = ip_fast_csum((unsigned char *)iph, iph->ihl);
  395. eth = skb_push(skb, ETH_HLEN);
  396. skb_reset_mac_header(skb);
  397. skb->protocol = eth->h_proto = htons(ETH_P_IP);
  398. }
  399. ether_addr_copy(eth->h_source, np->dev->dev_addr);
  400. ether_addr_copy(eth->h_dest, np->remote_mac);
  401. skb->dev = np->dev;
  402. netpoll_send_skb(np, skb);
  403. }
  404. EXPORT_SYMBOL(netpoll_send_udp);
  405. void netpoll_print_options(struct netpoll *np)
  406. {
  407. np_info(np, "local port %d\n", np->local_port);
  408. if (np->ipv6)
  409. np_info(np, "local IPv6 address %pI6c\n", &np->local_ip.in6);
  410. else
  411. np_info(np, "local IPv4 address %pI4\n", &np->local_ip.ip);
  412. np_info(np, "interface '%s'\n", np->dev_name);
  413. np_info(np, "remote port %d\n", np->remote_port);
  414. if (np->ipv6)
  415. np_info(np, "remote IPv6 address %pI6c\n", &np->remote_ip.in6);
  416. else
  417. np_info(np, "remote IPv4 address %pI4\n", &np->remote_ip.ip);
  418. np_info(np, "remote ethernet address %pM\n", np->remote_mac);
  419. }
  420. EXPORT_SYMBOL(netpoll_print_options);
  421. static int netpoll_parse_ip_addr(const char *str, union inet_addr *addr)
  422. {
  423. const char *end;
  424. if (!strchr(str, ':') &&
  425. in4_pton(str, -1, (void *)addr, -1, &end) > 0) {
  426. if (!*end)
  427. return 0;
  428. }
  429. if (in6_pton(str, -1, addr->in6.s6_addr, -1, &end) > 0) {
  430. #if IS_ENABLED(CONFIG_IPV6)
  431. if (!*end)
  432. return 1;
  433. #else
  434. return -1;
  435. #endif
  436. }
  437. return -1;
  438. }
  439. int netpoll_parse_options(struct netpoll *np, char *opt)
  440. {
  441. char *cur=opt, *delim;
  442. int ipv6;
  443. bool ipversion_set = false;
  444. if (*cur != '@') {
  445. if ((delim = strchr(cur, '@')) == NULL)
  446. goto parse_failed;
  447. *delim = 0;
  448. if (kstrtou16(cur, 10, &np->local_port))
  449. goto parse_failed;
  450. cur = delim;
  451. }
  452. cur++;
  453. if (*cur != '/') {
  454. ipversion_set = true;
  455. if ((delim = strchr(cur, '/')) == NULL)
  456. goto parse_failed;
  457. *delim = 0;
  458. ipv6 = netpoll_parse_ip_addr(cur, &np->local_ip);
  459. if (ipv6 < 0)
  460. goto parse_failed;
  461. else
  462. np->ipv6 = (bool)ipv6;
  463. cur = delim;
  464. }
  465. cur++;
  466. if (*cur != ',') {
  467. /* parse out dev name */
  468. if ((delim = strchr(cur, ',')) == NULL)
  469. goto parse_failed;
  470. *delim = 0;
  471. strscpy(np->dev_name, cur, sizeof(np->dev_name));
  472. cur = delim;
  473. }
  474. cur++;
  475. if (*cur != '@') {
  476. /* dst port */
  477. if ((delim = strchr(cur, '@')) == NULL)
  478. goto parse_failed;
  479. *delim = 0;
  480. if (*cur == ' ' || *cur == '\t')
  481. np_info(np, "warning: whitespace is not allowed\n");
  482. if (kstrtou16(cur, 10, &np->remote_port))
  483. goto parse_failed;
  484. cur = delim;
  485. }
  486. cur++;
  487. /* dst ip */
  488. if ((delim = strchr(cur, '/')) == NULL)
  489. goto parse_failed;
  490. *delim = 0;
  491. ipv6 = netpoll_parse_ip_addr(cur, &np->remote_ip);
  492. if (ipv6 < 0)
  493. goto parse_failed;
  494. else if (ipversion_set && np->ipv6 != (bool)ipv6)
  495. goto parse_failed;
  496. else
  497. np->ipv6 = (bool)ipv6;
  498. cur = delim + 1;
  499. if (*cur != 0) {
  500. /* MAC address */
  501. if (!mac_pton(cur, np->remote_mac))
  502. goto parse_failed;
  503. }
  504. netpoll_print_options(np);
  505. return 0;
  506. parse_failed:
  507. np_info(np, "couldn't parse config at '%s'!\n", cur);
  508. return -1;
  509. }
  510. EXPORT_SYMBOL(netpoll_parse_options);
  511. int __netpoll_setup(struct netpoll *np, struct net_device *ndev)
  512. {
  513. struct netpoll_info *npinfo;
  514. const struct net_device_ops *ops;
  515. int err;
  516. if (ndev->priv_flags & IFF_DISABLE_NETPOLL) {
  517. np_err(np, "%s doesn't support polling, aborting\n",
  518. ndev->name);
  519. err = -ENOTSUPP;
  520. goto out;
  521. }
  522. if (!rcu_access_pointer(ndev->npinfo)) {
  523. npinfo = kmalloc(sizeof(*npinfo), GFP_KERNEL);
  524. if (!npinfo) {
  525. err = -ENOMEM;
  526. goto out;
  527. }
  528. sema_init(&npinfo->dev_lock, 1);
  529. skb_queue_head_init(&npinfo->txq);
  530. INIT_DELAYED_WORK(&npinfo->tx_work, queue_process);
  531. refcount_set(&npinfo->refcnt, 1);
  532. ops = ndev->netdev_ops;
  533. if (ops->ndo_netpoll_setup) {
  534. err = ops->ndo_netpoll_setup(ndev, npinfo);
  535. if (err)
  536. goto free_npinfo;
  537. }
  538. } else {
  539. npinfo = rtnl_dereference(ndev->npinfo);
  540. refcount_inc(&npinfo->refcnt);
  541. }
  542. np->dev = ndev;
  543. strscpy(np->dev_name, ndev->name, IFNAMSIZ);
  544. npinfo->netpoll = np;
  545. /* last thing to do is link it to the net device structure */
  546. rcu_assign_pointer(ndev->npinfo, npinfo);
  547. return 0;
  548. free_npinfo:
  549. kfree(npinfo);
  550. out:
  551. return err;
  552. }
  553. EXPORT_SYMBOL_GPL(__netpoll_setup);
  554. int netpoll_setup(struct netpoll *np)
  555. {
  556. struct net_device *ndev = NULL;
  557. bool ip_overwritten = false;
  558. struct in_device *in_dev;
  559. int err;
  560. rtnl_lock();
  561. if (np->dev_name[0]) {
  562. struct net *net = current->nsproxy->net_ns;
  563. ndev = __dev_get_by_name(net, np->dev_name);
  564. }
  565. if (!ndev) {
  566. np_err(np, "%s doesn't exist, aborting\n", np->dev_name);
  567. err = -ENODEV;
  568. goto unlock;
  569. }
  570. netdev_hold(ndev, &np->dev_tracker, GFP_KERNEL);
  571. if (netdev_master_upper_dev_get(ndev)) {
  572. np_err(np, "%s is a slave device, aborting\n", np->dev_name);
  573. err = -EBUSY;
  574. goto put;
  575. }
  576. if (!netif_running(ndev)) {
  577. unsigned long atmost;
  578. np_info(np, "device %s not up yet, forcing it\n", np->dev_name);
  579. err = dev_open(ndev, NULL);
  580. if (err) {
  581. np_err(np, "failed to open %s\n", ndev->name);
  582. goto put;
  583. }
  584. rtnl_unlock();
  585. atmost = jiffies + carrier_timeout * HZ;
  586. while (!netif_carrier_ok(ndev)) {
  587. if (time_after(jiffies, atmost)) {
  588. np_notice(np, "timeout waiting for carrier\n");
  589. break;
  590. }
  591. msleep(1);
  592. }
  593. rtnl_lock();
  594. }
  595. if (!np->local_ip.ip) {
  596. if (!np->ipv6) {
  597. const struct in_ifaddr *ifa;
  598. in_dev = __in_dev_get_rtnl(ndev);
  599. if (!in_dev)
  600. goto put_noaddr;
  601. ifa = rtnl_dereference(in_dev->ifa_list);
  602. if (!ifa) {
  603. put_noaddr:
  604. np_err(np, "no IP address for %s, aborting\n",
  605. np->dev_name);
  606. err = -EDESTADDRREQ;
  607. goto put;
  608. }
  609. np->local_ip.ip = ifa->ifa_local;
  610. ip_overwritten = true;
  611. np_info(np, "local IP %pI4\n", &np->local_ip.ip);
  612. } else {
  613. #if IS_ENABLED(CONFIG_IPV6)
  614. struct inet6_dev *idev;
  615. err = -EDESTADDRREQ;
  616. idev = __in6_dev_get(ndev);
  617. if (idev) {
  618. struct inet6_ifaddr *ifp;
  619. read_lock_bh(&idev->lock);
  620. list_for_each_entry(ifp, &idev->addr_list, if_list) {
  621. if (!!(ipv6_addr_type(&ifp->addr) & IPV6_ADDR_LINKLOCAL) !=
  622. !!(ipv6_addr_type(&np->remote_ip.in6) & IPV6_ADDR_LINKLOCAL))
  623. continue;
  624. np->local_ip.in6 = ifp->addr;
  625. ip_overwritten = true;
  626. err = 0;
  627. break;
  628. }
  629. read_unlock_bh(&idev->lock);
  630. }
  631. if (err) {
  632. np_err(np, "no IPv6 address for %s, aborting\n",
  633. np->dev_name);
  634. goto put;
  635. } else
  636. np_info(np, "local IPv6 %pI6c\n", &np->local_ip.in6);
  637. #else
  638. np_err(np, "IPv6 is not supported %s, aborting\n",
  639. np->dev_name);
  640. err = -EINVAL;
  641. goto put;
  642. #endif
  643. }
  644. }
  645. /* fill up the skb queue */
  646. refill_skbs();
  647. err = __netpoll_setup(np, ndev);
  648. if (err)
  649. goto put;
  650. rtnl_unlock();
  651. return 0;
  652. put:
  653. DEBUG_NET_WARN_ON_ONCE(np->dev);
  654. if (ip_overwritten)
  655. memset(&np->local_ip, 0, sizeof(np->local_ip));
  656. netdev_put(ndev, &np->dev_tracker);
  657. unlock:
  658. rtnl_unlock();
  659. return err;
  660. }
  661. EXPORT_SYMBOL(netpoll_setup);
  662. static int __init netpoll_init(void)
  663. {
  664. skb_queue_head_init(&skb_pool);
  665. return 0;
  666. }
  667. core_initcall(netpoll_init);
  668. static void rcu_cleanup_netpoll_info(struct rcu_head *rcu_head)
  669. {
  670. struct netpoll_info *npinfo =
  671. container_of(rcu_head, struct netpoll_info, rcu);
  672. skb_queue_purge(&npinfo->txq);
  673. /* we can't call cancel_delayed_work_sync here, as we are in softirq */
  674. cancel_delayed_work(&npinfo->tx_work);
  675. /* clean after last, unfinished work */
  676. __skb_queue_purge(&npinfo->txq);
  677. /* now cancel it again */
  678. cancel_delayed_work(&npinfo->tx_work);
  679. kfree(npinfo);
  680. }
  681. void __netpoll_cleanup(struct netpoll *np)
  682. {
  683. struct netpoll_info *npinfo;
  684. npinfo = rtnl_dereference(np->dev->npinfo);
  685. if (!npinfo)
  686. return;
  687. if (refcount_dec_and_test(&npinfo->refcnt)) {
  688. const struct net_device_ops *ops;
  689. ops = np->dev->netdev_ops;
  690. if (ops->ndo_netpoll_cleanup)
  691. ops->ndo_netpoll_cleanup(np->dev);
  692. RCU_INIT_POINTER(np->dev->npinfo, NULL);
  693. call_rcu(&npinfo->rcu, rcu_cleanup_netpoll_info);
  694. } else
  695. RCU_INIT_POINTER(np->dev->npinfo, NULL);
  696. }
  697. EXPORT_SYMBOL_GPL(__netpoll_cleanup);
  698. void __netpoll_free(struct netpoll *np)
  699. {
  700. ASSERT_RTNL();
  701. /* Wait for transmitting packets to finish before freeing. */
  702. synchronize_rcu();
  703. __netpoll_cleanup(np);
  704. kfree(np);
  705. }
  706. EXPORT_SYMBOL_GPL(__netpoll_free);
  707. void do_netpoll_cleanup(struct netpoll *np)
  708. {
  709. __netpoll_cleanup(np);
  710. netdev_put(np->dev, &np->dev_tracker);
  711. np->dev = NULL;
  712. }
  713. EXPORT_SYMBOL(do_netpoll_cleanup);
  714. void netpoll_cleanup(struct netpoll *np)
  715. {
  716. rtnl_lock();
  717. if (!np->dev)
  718. goto out;
  719. do_netpoll_cleanup(np);
  720. out:
  721. rtnl_unlock();
  722. }
  723. EXPORT_SYMBOL(netpoll_cleanup);