netpoll.c 19 KB

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