netpoll.c 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854
  1. /*
  2. * Common framework for low-level network console, dump, and debugger code
  3. *
  4. * Sep 8 2003 Matt Mackall <mpm@selenic.com>
  5. *
  6. * based on the netconsole code from:
  7. *
  8. * Copyright (C) 2001 Ingo Molnar <mingo@redhat.com>
  9. * Copyright (C) 2002 Red Hat, Inc.
  10. */
  11. #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
  12. #include <linux/moduleparam.h>
  13. #include <linux/kernel.h>
  14. #include <linux/netdevice.h>
  15. #include <linux/etherdevice.h>
  16. #include <linux/string.h>
  17. #include <linux/if_arp.h>
  18. #include <linux/inetdevice.h>
  19. #include <linux/inet.h>
  20. #include <linux/interrupt.h>
  21. #include <linux/netpoll.h>
  22. #include <linux/sched.h>
  23. #include <linux/delay.h>
  24. #include <linux/rcupdate.h>
  25. #include <linux/workqueue.h>
  26. #include <linux/slab.h>
  27. #include <linux/export.h>
  28. #include <linux/if_vlan.h>
  29. #include <net/tcp.h>
  30. #include <net/udp.h>
  31. #include <net/addrconf.h>
  32. #include <net/ndisc.h>
  33. #include <net/ip6_checksum.h>
  34. #include <asm/unaligned.h>
  35. #include <trace/events/napi.h>
  36. /*
  37. * We maintain a small pool of fully-sized skbs, to make sure the
  38. * message gets out even in extreme OOM situations.
  39. */
  40. #define MAX_UDP_CHUNK 1460
  41. #define MAX_SKBS 32
  42. static struct sk_buff_head skb_pool;
  43. DEFINE_STATIC_SRCU(netpoll_srcu);
  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 void netpoll_async_cleanup(struct work_struct *work);
  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 int netpoll_start_xmit(struct sk_buff *skb, struct net_device *dev,
  61. struct netdev_queue *txq)
  62. {
  63. int status = NETDEV_TX_OK;
  64. netdev_features_t features;
  65. features = netif_skb_features(skb);
  66. if (vlan_tx_tag_present(skb) &&
  67. !vlan_hw_offload_capable(features, skb->vlan_proto)) {
  68. skb = __vlan_put_tag(skb, skb->vlan_proto,
  69. vlan_tx_tag_get(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. skb->vlan_tci = 0;
  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. if (!netif_device_present(dev) || !netif_running(dev)) {
  93. kfree_skb(skb);
  94. continue;
  95. }
  96. txq = skb_get_tx_queue(dev, skb);
  97. local_irq_save(flags);
  98. HARD_TX_LOCK(dev, txq, smp_processor_id());
  99. if (netif_xmit_frozen_or_stopped(txq) ||
  100. netpoll_start_xmit(skb, dev, txq) != NETDEV_TX_OK) {
  101. skb_queue_head(&npinfo->txq, skb);
  102. HARD_TX_UNLOCK(dev, txq);
  103. local_irq_restore(flags);
  104. schedule_delayed_work(&npinfo->tx_work, HZ/10);
  105. return;
  106. }
  107. HARD_TX_UNLOCK(dev, txq);
  108. local_irq_restore(flags);
  109. }
  110. }
  111. /*
  112. * Check whether delayed processing was scheduled for our NIC. If so,
  113. * we attempt to grab the poll lock and use ->poll() to pump the card.
  114. * If this fails, either we've recursed in ->poll() or it's already
  115. * running on another CPU.
  116. *
  117. * Note: we don't mask interrupts with this lock because we're using
  118. * trylock here and interrupts are already disabled in the softirq
  119. * case. Further, we test the poll_owner to avoid recursion on UP
  120. * systems where the lock doesn't exist.
  121. */
  122. static int poll_one_napi(struct napi_struct *napi, int budget)
  123. {
  124. int work;
  125. /* net_rx_action's ->poll() invocations and our's are
  126. * synchronized by this test which is only made while
  127. * holding the napi->poll_lock.
  128. */
  129. if (!test_bit(NAPI_STATE_SCHED, &napi->state))
  130. return budget;
  131. set_bit(NAPI_STATE_NPSVC, &napi->state);
  132. work = napi->poll(napi, budget);
  133. WARN_ONCE(work > budget, "%pF exceeded budget in poll\n", napi->poll);
  134. trace_napi_poll(napi);
  135. clear_bit(NAPI_STATE_NPSVC, &napi->state);
  136. return budget - work;
  137. }
  138. static void poll_napi(struct net_device *dev, int budget)
  139. {
  140. struct napi_struct *napi;
  141. list_for_each_entry(napi, &dev->napi_list, dev_list) {
  142. if (napi->poll_owner != smp_processor_id() &&
  143. spin_trylock(&napi->poll_lock)) {
  144. budget = poll_one_napi(napi, budget);
  145. spin_unlock(&napi->poll_lock);
  146. }
  147. }
  148. }
  149. static void netpoll_poll_dev(struct net_device *dev)
  150. {
  151. const struct net_device_ops *ops;
  152. struct netpoll_info *ni = rcu_dereference_bh(dev->npinfo);
  153. int budget = 0;
  154. /* Don't do any rx activity if the dev_lock mutex is held
  155. * the dev_open/close paths use this to block netpoll activity
  156. * while changing device state
  157. */
  158. if (down_trylock(&ni->dev_lock))
  159. return;
  160. if (!netif_running(dev)) {
  161. up(&ni->dev_lock);
  162. return;
  163. }
  164. ops = dev->netdev_ops;
  165. if (!ops->ndo_poll_controller) {
  166. up(&ni->dev_lock);
  167. return;
  168. }
  169. /* Process pending work on NIC */
  170. ops->ndo_poll_controller(dev);
  171. poll_napi(dev, budget);
  172. up(&ni->dev_lock);
  173. zap_completion_queue();
  174. }
  175. void netpoll_poll_disable(struct net_device *dev)
  176. {
  177. struct netpoll_info *ni;
  178. int idx;
  179. might_sleep();
  180. idx = srcu_read_lock(&netpoll_srcu);
  181. ni = srcu_dereference(dev->npinfo, &netpoll_srcu);
  182. if (ni)
  183. down(&ni->dev_lock);
  184. srcu_read_unlock(&netpoll_srcu, idx);
  185. }
  186. EXPORT_SYMBOL(netpoll_poll_disable);
  187. void netpoll_poll_enable(struct net_device *dev)
  188. {
  189. struct netpoll_info *ni;
  190. rcu_read_lock();
  191. ni = rcu_dereference(dev->npinfo);
  192. if (ni)
  193. up(&ni->dev_lock);
  194. rcu_read_unlock();
  195. }
  196. EXPORT_SYMBOL(netpoll_poll_enable);
  197. static void refill_skbs(void)
  198. {
  199. struct sk_buff *skb;
  200. unsigned long flags;
  201. spin_lock_irqsave(&skb_pool.lock, flags);
  202. while (skb_pool.qlen < MAX_SKBS) {
  203. skb = alloc_skb(MAX_SKB_SIZE, GFP_ATOMIC);
  204. if (!skb)
  205. break;
  206. __skb_queue_tail(&skb_pool, skb);
  207. }
  208. spin_unlock_irqrestore(&skb_pool.lock, flags);
  209. }
  210. static void zap_completion_queue(void)
  211. {
  212. unsigned long flags;
  213. struct softnet_data *sd = &get_cpu_var(softnet_data);
  214. if (sd->completion_queue) {
  215. struct sk_buff *clist;
  216. local_irq_save(flags);
  217. clist = sd->completion_queue;
  218. sd->completion_queue = NULL;
  219. local_irq_restore(flags);
  220. while (clist != NULL) {
  221. struct sk_buff *skb = clist;
  222. clist = clist->next;
  223. if (!skb_irq_freeable(skb)) {
  224. atomic_inc(&skb->users);
  225. dev_kfree_skb_any(skb); /* put this one back */
  226. } else {
  227. __kfree_skb(skb);
  228. }
  229. }
  230. }
  231. put_cpu_var(softnet_data);
  232. }
  233. static struct sk_buff *find_skb(struct netpoll *np, int len, int reserve)
  234. {
  235. int count = 0;
  236. struct sk_buff *skb;
  237. zap_completion_queue();
  238. refill_skbs();
  239. repeat:
  240. skb = alloc_skb(len, GFP_ATOMIC);
  241. if (!skb)
  242. skb = skb_dequeue(&skb_pool);
  243. if (!skb) {
  244. if (++count < 10) {
  245. netpoll_poll_dev(np->dev);
  246. goto repeat;
  247. }
  248. return NULL;
  249. }
  250. atomic_set(&skb->users, 1);
  251. skb_reserve(skb, reserve);
  252. return skb;
  253. }
  254. static int netpoll_owner_active(struct net_device *dev)
  255. {
  256. struct napi_struct *napi;
  257. list_for_each_entry(napi, &dev->napi_list, dev_list) {
  258. if (napi->poll_owner == smp_processor_id())
  259. return 1;
  260. }
  261. return 0;
  262. }
  263. /* call with IRQ disabled */
  264. void netpoll_send_skb_on_dev(struct netpoll *np, struct sk_buff *skb,
  265. struct net_device *dev)
  266. {
  267. int status = NETDEV_TX_BUSY;
  268. unsigned long tries;
  269. /* It is up to the caller to keep npinfo alive. */
  270. struct netpoll_info *npinfo;
  271. WARN_ON_ONCE(!irqs_disabled());
  272. npinfo = rcu_dereference_bh(np->dev->npinfo);
  273. if (!npinfo || !netif_running(dev) || !netif_device_present(dev)) {
  274. dev_kfree_skb_irq(skb);
  275. return;
  276. }
  277. /* don't get messages out of order, and no recursion */
  278. if (skb_queue_len(&npinfo->txq) == 0 && !netpoll_owner_active(dev)) {
  279. struct netdev_queue *txq;
  280. txq = netdev_pick_tx(dev, skb, NULL);
  281. /* try until next clock tick */
  282. for (tries = jiffies_to_usecs(1)/USEC_PER_POLL;
  283. tries > 0; --tries) {
  284. if (HARD_TX_TRYLOCK(dev, txq)) {
  285. if (!netif_xmit_stopped(txq))
  286. status = netpoll_start_xmit(skb, dev, txq);
  287. HARD_TX_UNLOCK(dev, txq);
  288. if (status == NETDEV_TX_OK)
  289. break;
  290. }
  291. /* tickle device maybe there is some cleanup */
  292. netpoll_poll_dev(np->dev);
  293. udelay(USEC_PER_POLL);
  294. }
  295. WARN_ONCE(!irqs_disabled(),
  296. "netpoll_send_skb_on_dev(): %s enabled interrupts in poll (%pF)\n",
  297. dev->name, dev->netdev_ops->ndo_start_xmit);
  298. }
  299. if (status != NETDEV_TX_OK) {
  300. skb_queue_tail(&npinfo->txq, skb);
  301. schedule_delayed_work(&npinfo->tx_work,0);
  302. }
  303. }
  304. EXPORT_SYMBOL(netpoll_send_skb_on_dev);
  305. void netpoll_send_udp(struct netpoll *np, const char *msg, int len)
  306. {
  307. int total_len, ip_len, udp_len;
  308. struct sk_buff *skb;
  309. struct udphdr *udph;
  310. struct iphdr *iph;
  311. struct ethhdr *eth;
  312. static atomic_t ip_ident;
  313. struct ipv6hdr *ip6h;
  314. udp_len = len + sizeof(*udph);
  315. if (np->ipv6)
  316. ip_len = udp_len + sizeof(*ip6h);
  317. else
  318. ip_len = udp_len + sizeof(*iph);
  319. total_len = ip_len + LL_RESERVED_SPACE(np->dev);
  320. skb = find_skb(np, total_len + np->dev->needed_tailroom,
  321. total_len - len);
  322. if (!skb)
  323. return;
  324. skb_copy_to_linear_data(skb, msg, len);
  325. skb_put(skb, len);
  326. skb_push(skb, sizeof(*udph));
  327. skb_reset_transport_header(skb);
  328. udph = udp_hdr(skb);
  329. udph->source = htons(np->local_port);
  330. udph->dest = htons(np->remote_port);
  331. udph->len = htons(udp_len);
  332. if (np->ipv6) {
  333. udph->check = 0;
  334. udph->check = csum_ipv6_magic(&np->local_ip.in6,
  335. &np->remote_ip.in6,
  336. udp_len, IPPROTO_UDP,
  337. csum_partial(udph, udp_len, 0));
  338. if (udph->check == 0)
  339. udph->check = CSUM_MANGLED_0;
  340. skb_push(skb, sizeof(*ip6h));
  341. skb_reset_network_header(skb);
  342. ip6h = ipv6_hdr(skb);
  343. /* ip6h->version = 6; ip6h->priority = 0; */
  344. put_unaligned(0x60, (unsigned char *)ip6h);
  345. ip6h->flow_lbl[0] = 0;
  346. ip6h->flow_lbl[1] = 0;
  347. ip6h->flow_lbl[2] = 0;
  348. ip6h->payload_len = htons(sizeof(struct udphdr) + len);
  349. ip6h->nexthdr = IPPROTO_UDP;
  350. ip6h->hop_limit = 32;
  351. ip6h->saddr = np->local_ip.in6;
  352. ip6h->daddr = np->remote_ip.in6;
  353. eth = (struct ethhdr *) skb_push(skb, ETH_HLEN);
  354. skb_reset_mac_header(skb);
  355. skb->protocol = eth->h_proto = htons(ETH_P_IPV6);
  356. } else {
  357. udph->check = 0;
  358. udph->check = csum_tcpudp_magic(np->local_ip.ip,
  359. np->remote_ip.ip,
  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(*iph));
  365. skb_reset_network_header(skb);
  366. iph = ip_hdr(skb);
  367. /* iph->version = 4; iph->ihl = 5; */
  368. put_unaligned(0x45, (unsigned char *)iph);
  369. iph->tos = 0;
  370. put_unaligned(htons(ip_len), &(iph->tot_len));
  371. iph->id = htons(atomic_inc_return(&ip_ident));
  372. iph->frag_off = 0;
  373. iph->ttl = 64;
  374. iph->protocol = IPPROTO_UDP;
  375. iph->check = 0;
  376. put_unaligned(np->local_ip.ip, &(iph->saddr));
  377. put_unaligned(np->remote_ip.ip, &(iph->daddr));
  378. iph->check = ip_fast_csum((unsigned char *)iph, iph->ihl);
  379. eth = (struct ethhdr *) skb_push(skb, ETH_HLEN);
  380. skb_reset_mac_header(skb);
  381. skb->protocol = eth->h_proto = htons(ETH_P_IP);
  382. }
  383. ether_addr_copy(eth->h_source, np->dev->dev_addr);
  384. ether_addr_copy(eth->h_dest, np->remote_mac);
  385. skb->dev = np->dev;
  386. netpoll_send_skb(np, skb);
  387. }
  388. EXPORT_SYMBOL(netpoll_send_udp);
  389. void netpoll_print_options(struct netpoll *np)
  390. {
  391. np_info(np, "local port %d\n", np->local_port);
  392. if (np->ipv6)
  393. np_info(np, "local IPv6 address %pI6c\n", &np->local_ip.in6);
  394. else
  395. np_info(np, "local IPv4 address %pI4\n", &np->local_ip.ip);
  396. np_info(np, "interface '%s'\n", np->dev_name);
  397. np_info(np, "remote port %d\n", np->remote_port);
  398. if (np->ipv6)
  399. np_info(np, "remote IPv6 address %pI6c\n", &np->remote_ip.in6);
  400. else
  401. np_info(np, "remote IPv4 address %pI4\n", &np->remote_ip.ip);
  402. np_info(np, "remote ethernet address %pM\n", np->remote_mac);
  403. }
  404. EXPORT_SYMBOL(netpoll_print_options);
  405. static int netpoll_parse_ip_addr(const char *str, union inet_addr *addr)
  406. {
  407. const char *end;
  408. if (!strchr(str, ':') &&
  409. in4_pton(str, -1, (void *)addr, -1, &end) > 0) {
  410. if (!*end)
  411. return 0;
  412. }
  413. if (in6_pton(str, -1, addr->in6.s6_addr, -1, &end) > 0) {
  414. #if IS_ENABLED(CONFIG_IPV6)
  415. if (!*end)
  416. return 1;
  417. #else
  418. return -1;
  419. #endif
  420. }
  421. return -1;
  422. }
  423. int netpoll_parse_options(struct netpoll *np, char *opt)
  424. {
  425. char *cur=opt, *delim;
  426. int ipv6;
  427. bool ipversion_set = false;
  428. if (*cur != '@') {
  429. if ((delim = strchr(cur, '@')) == NULL)
  430. goto parse_failed;
  431. *delim = 0;
  432. if (kstrtou16(cur, 10, &np->local_port))
  433. goto parse_failed;
  434. cur = delim;
  435. }
  436. cur++;
  437. if (*cur != '/') {
  438. ipversion_set = true;
  439. if ((delim = strchr(cur, '/')) == NULL)
  440. goto parse_failed;
  441. *delim = 0;
  442. ipv6 = netpoll_parse_ip_addr(cur, &np->local_ip);
  443. if (ipv6 < 0)
  444. goto parse_failed;
  445. else
  446. np->ipv6 = (bool)ipv6;
  447. cur = delim;
  448. }
  449. cur++;
  450. if (*cur != ',') {
  451. /* parse out dev name */
  452. if ((delim = strchr(cur, ',')) == NULL)
  453. goto parse_failed;
  454. *delim = 0;
  455. strlcpy(np->dev_name, cur, sizeof(np->dev_name));
  456. cur = delim;
  457. }
  458. cur++;
  459. if (*cur != '@') {
  460. /* dst port */
  461. if ((delim = strchr(cur, '@')) == NULL)
  462. goto parse_failed;
  463. *delim = 0;
  464. if (*cur == ' ' || *cur == '\t')
  465. np_info(np, "warning: whitespace is not allowed\n");
  466. if (kstrtou16(cur, 10, &np->remote_port))
  467. goto parse_failed;
  468. cur = delim;
  469. }
  470. cur++;
  471. /* dst ip */
  472. if ((delim = strchr(cur, '/')) == NULL)
  473. goto parse_failed;
  474. *delim = 0;
  475. ipv6 = netpoll_parse_ip_addr(cur, &np->remote_ip);
  476. if (ipv6 < 0)
  477. goto parse_failed;
  478. else if (ipversion_set && np->ipv6 != (bool)ipv6)
  479. goto parse_failed;
  480. else
  481. np->ipv6 = (bool)ipv6;
  482. cur = delim + 1;
  483. if (*cur != 0) {
  484. /* MAC address */
  485. if (!mac_pton(cur, np->remote_mac))
  486. goto parse_failed;
  487. }
  488. netpoll_print_options(np);
  489. return 0;
  490. parse_failed:
  491. np_info(np, "couldn't parse config at '%s'!\n", cur);
  492. return -1;
  493. }
  494. EXPORT_SYMBOL(netpoll_parse_options);
  495. int __netpoll_setup(struct netpoll *np, struct net_device *ndev)
  496. {
  497. struct netpoll_info *npinfo;
  498. const struct net_device_ops *ops;
  499. int err;
  500. np->dev = ndev;
  501. strlcpy(np->dev_name, ndev->name, IFNAMSIZ);
  502. INIT_WORK(&np->cleanup_work, netpoll_async_cleanup);
  503. if ((ndev->priv_flags & IFF_DISABLE_NETPOLL) ||
  504. !ndev->netdev_ops->ndo_poll_controller) {
  505. np_err(np, "%s doesn't support polling, aborting\n",
  506. np->dev_name);
  507. err = -ENOTSUPP;
  508. goto out;
  509. }
  510. if (!ndev->npinfo) {
  511. npinfo = kmalloc(sizeof(*npinfo), GFP_KERNEL);
  512. if (!npinfo) {
  513. err = -ENOMEM;
  514. goto out;
  515. }
  516. sema_init(&npinfo->dev_lock, 1);
  517. skb_queue_head_init(&npinfo->txq);
  518. INIT_DELAYED_WORK(&npinfo->tx_work, queue_process);
  519. atomic_set(&npinfo->refcnt, 1);
  520. ops = np->dev->netdev_ops;
  521. if (ops->ndo_netpoll_setup) {
  522. err = ops->ndo_netpoll_setup(ndev, npinfo);
  523. if (err)
  524. goto free_npinfo;
  525. }
  526. } else {
  527. npinfo = rtnl_dereference(ndev->npinfo);
  528. atomic_inc(&npinfo->refcnt);
  529. }
  530. npinfo->netpoll = np;
  531. /* last thing to do is link it to the net device structure */
  532. rcu_assign_pointer(ndev->npinfo, npinfo);
  533. return 0;
  534. free_npinfo:
  535. kfree(npinfo);
  536. out:
  537. return err;
  538. }
  539. EXPORT_SYMBOL_GPL(__netpoll_setup);
  540. int netpoll_setup(struct netpoll *np)
  541. {
  542. struct net_device *ndev = NULL;
  543. struct in_device *in_dev;
  544. int err;
  545. rtnl_lock();
  546. if (np->dev_name) {
  547. struct net *net = current->nsproxy->net_ns;
  548. ndev = __dev_get_by_name(net, np->dev_name);
  549. }
  550. if (!ndev) {
  551. np_err(np, "%s doesn't exist, aborting\n", np->dev_name);
  552. err = -ENODEV;
  553. goto unlock;
  554. }
  555. dev_hold(ndev);
  556. if (netdev_master_upper_dev_get(ndev)) {
  557. np_err(np, "%s is a slave device, aborting\n", np->dev_name);
  558. err = -EBUSY;
  559. goto put;
  560. }
  561. if (!netif_running(ndev)) {
  562. unsigned long atmost, atleast;
  563. np_info(np, "device %s not up yet, forcing it\n", np->dev_name);
  564. err = dev_open(ndev);
  565. if (err) {
  566. np_err(np, "failed to open %s\n", ndev->name);
  567. goto put;
  568. }
  569. rtnl_unlock();
  570. atleast = jiffies + HZ/10;
  571. atmost = jiffies + carrier_timeout * HZ;
  572. while (!netif_carrier_ok(ndev)) {
  573. if (time_after(jiffies, atmost)) {
  574. np_notice(np, "timeout waiting for carrier\n");
  575. break;
  576. }
  577. msleep(1);
  578. }
  579. /* If carrier appears to come up instantly, we don't
  580. * trust it and pause so that we don't pump all our
  581. * queued console messages into the bitbucket.
  582. */
  583. if (time_before(jiffies, atleast)) {
  584. np_notice(np, "carrier detect appears untrustworthy, waiting 4 seconds\n");
  585. msleep(4000);
  586. }
  587. rtnl_lock();
  588. }
  589. if (!np->local_ip.ip) {
  590. if (!np->ipv6) {
  591. in_dev = __in_dev_get_rtnl(ndev);
  592. if (!in_dev || !in_dev->ifa_list) {
  593. np_err(np, "no IP address for %s, aborting\n",
  594. np->dev_name);
  595. err = -EDESTADDRREQ;
  596. goto put;
  597. }
  598. np->local_ip.ip = in_dev->ifa_list->ifa_local;
  599. np_info(np, "local IP %pI4\n", &np->local_ip.ip);
  600. } else {
  601. #if IS_ENABLED(CONFIG_IPV6)
  602. struct inet6_dev *idev;
  603. err = -EDESTADDRREQ;
  604. idev = __in6_dev_get(ndev);
  605. if (idev) {
  606. struct inet6_ifaddr *ifp;
  607. read_lock_bh(&idev->lock);
  608. list_for_each_entry(ifp, &idev->addr_list, if_list) {
  609. if (ipv6_addr_type(&ifp->addr) & IPV6_ADDR_LINKLOCAL)
  610. continue;
  611. np->local_ip.in6 = ifp->addr;
  612. err = 0;
  613. break;
  614. }
  615. read_unlock_bh(&idev->lock);
  616. }
  617. if (err) {
  618. np_err(np, "no IPv6 address for %s, aborting\n",
  619. np->dev_name);
  620. goto put;
  621. } else
  622. np_info(np, "local IPv6 %pI6c\n", &np->local_ip.in6);
  623. #else
  624. np_err(np, "IPv6 is not supported %s, aborting\n",
  625. np->dev_name);
  626. err = -EINVAL;
  627. goto put;
  628. #endif
  629. }
  630. }
  631. /* fill up the skb queue */
  632. refill_skbs();
  633. err = __netpoll_setup(np, ndev);
  634. if (err)
  635. goto put;
  636. rtnl_unlock();
  637. return 0;
  638. put:
  639. dev_put(ndev);
  640. unlock:
  641. rtnl_unlock();
  642. return err;
  643. }
  644. EXPORT_SYMBOL(netpoll_setup);
  645. static int __init netpoll_init(void)
  646. {
  647. skb_queue_head_init(&skb_pool);
  648. return 0;
  649. }
  650. core_initcall(netpoll_init);
  651. static void rcu_cleanup_netpoll_info(struct rcu_head *rcu_head)
  652. {
  653. struct netpoll_info *npinfo =
  654. container_of(rcu_head, struct netpoll_info, rcu);
  655. skb_queue_purge(&npinfo->txq);
  656. /* we can't call cancel_delayed_work_sync here, as we are in softirq */
  657. cancel_delayed_work(&npinfo->tx_work);
  658. /* clean after last, unfinished work */
  659. __skb_queue_purge(&npinfo->txq);
  660. /* now cancel it again */
  661. cancel_delayed_work(&npinfo->tx_work);
  662. kfree(npinfo);
  663. }
  664. void __netpoll_cleanup(struct netpoll *np)
  665. {
  666. struct netpoll_info *npinfo;
  667. /* rtnl_dereference would be preferable here but
  668. * rcu_cleanup_netpoll path can put us in here safely without
  669. * holding the rtnl, so plain rcu_dereference it is
  670. */
  671. npinfo = rtnl_dereference(np->dev->npinfo);
  672. if (!npinfo)
  673. return;
  674. synchronize_srcu(&netpoll_srcu);
  675. if (atomic_dec_and_test(&npinfo->refcnt)) {
  676. const struct net_device_ops *ops;
  677. ops = np->dev->netdev_ops;
  678. if (ops->ndo_netpoll_cleanup)
  679. ops->ndo_netpoll_cleanup(np->dev);
  680. RCU_INIT_POINTER(np->dev->npinfo, NULL);
  681. call_rcu_bh(&npinfo->rcu, rcu_cleanup_netpoll_info);
  682. } else
  683. RCU_INIT_POINTER(np->dev->npinfo, NULL);
  684. }
  685. EXPORT_SYMBOL_GPL(__netpoll_cleanup);
  686. static void netpoll_async_cleanup(struct work_struct *work)
  687. {
  688. struct netpoll *np = container_of(work, struct netpoll, cleanup_work);
  689. rtnl_lock();
  690. __netpoll_cleanup(np);
  691. rtnl_unlock();
  692. kfree(np);
  693. }
  694. void __netpoll_free_async(struct netpoll *np)
  695. {
  696. schedule_work(&np->cleanup_work);
  697. }
  698. EXPORT_SYMBOL_GPL(__netpoll_free_async);
  699. void netpoll_cleanup(struct netpoll *np)
  700. {
  701. rtnl_lock();
  702. if (!np->dev)
  703. goto out;
  704. __netpoll_cleanup(np);
  705. dev_put(np->dev);
  706. np->dev = NULL;
  707. out:
  708. rtnl_unlock();
  709. }
  710. EXPORT_SYMBOL(netpoll_cleanup);