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 (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. WARN_ON_ONCE(!irqs_disabled());
  313. udp_len = len + sizeof(*udph);
  314. if (np->ipv6)
  315. ip_len = udp_len + sizeof(*ip6h);
  316. else
  317. ip_len = udp_len + sizeof(*iph);
  318. total_len = ip_len + LL_RESERVED_SPACE(np->dev);
  319. skb = find_skb(np, total_len + np->dev->needed_tailroom,
  320. total_len - len);
  321. if (!skb)
  322. return;
  323. skb_copy_to_linear_data(skb, msg, len);
  324. skb_put(skb, len);
  325. skb_push(skb, sizeof(*udph));
  326. skb_reset_transport_header(skb);
  327. udph = udp_hdr(skb);
  328. udph->source = htons(np->local_port);
  329. udph->dest = htons(np->remote_port);
  330. udph->len = htons(udp_len);
  331. if (np->ipv6) {
  332. udph->check = 0;
  333. udph->check = csum_ipv6_magic(&np->local_ip.in6,
  334. &np->remote_ip.in6,
  335. udp_len, IPPROTO_UDP,
  336. csum_partial(udph, udp_len, 0));
  337. if (udph->check == 0)
  338. udph->check = CSUM_MANGLED_0;
  339. skb_push(skb, sizeof(*ip6h));
  340. skb_reset_network_header(skb);
  341. ip6h = ipv6_hdr(skb);
  342. /* ip6h->version = 6; ip6h->priority = 0; */
  343. put_unaligned(0x60, (unsigned char *)ip6h);
  344. ip6h->flow_lbl[0] = 0;
  345. ip6h->flow_lbl[1] = 0;
  346. ip6h->flow_lbl[2] = 0;
  347. ip6h->payload_len = htons(sizeof(struct udphdr) + len);
  348. ip6h->nexthdr = IPPROTO_UDP;
  349. ip6h->hop_limit = 32;
  350. ip6h->saddr = np->local_ip.in6;
  351. ip6h->daddr = np->remote_ip.in6;
  352. eth = (struct ethhdr *) skb_push(skb, ETH_HLEN);
  353. skb_reset_mac_header(skb);
  354. skb->protocol = eth->h_proto = htons(ETH_P_IPV6);
  355. } else {
  356. udph->check = 0;
  357. udph->check = csum_tcpudp_magic(np->local_ip.ip,
  358. np->remote_ip.ip,
  359. udp_len, IPPROTO_UDP,
  360. csum_partial(udph, udp_len, 0));
  361. if (udph->check == 0)
  362. udph->check = CSUM_MANGLED_0;
  363. skb_push(skb, sizeof(*iph));
  364. skb_reset_network_header(skb);
  365. iph = ip_hdr(skb);
  366. /* iph->version = 4; iph->ihl = 5; */
  367. put_unaligned(0x45, (unsigned char *)iph);
  368. iph->tos = 0;
  369. put_unaligned(htons(ip_len), &(iph->tot_len));
  370. iph->id = htons(atomic_inc_return(&ip_ident));
  371. iph->frag_off = 0;
  372. iph->ttl = 64;
  373. iph->protocol = IPPROTO_UDP;
  374. iph->check = 0;
  375. put_unaligned(np->local_ip.ip, &(iph->saddr));
  376. put_unaligned(np->remote_ip.ip, &(iph->daddr));
  377. iph->check = ip_fast_csum((unsigned char *)iph, iph->ihl);
  378. eth = (struct ethhdr *) skb_push(skb, ETH_HLEN);
  379. skb_reset_mac_header(skb);
  380. skb->protocol = eth->h_proto = htons(ETH_P_IP);
  381. }
  382. ether_addr_copy(eth->h_source, np->dev->dev_addr);
  383. ether_addr_copy(eth->h_dest, np->remote_mac);
  384. skb->dev = np->dev;
  385. netpoll_send_skb(np, skb);
  386. }
  387. EXPORT_SYMBOL(netpoll_send_udp);
  388. void netpoll_print_options(struct netpoll *np)
  389. {
  390. np_info(np, "local port %d\n", np->local_port);
  391. if (np->ipv6)
  392. np_info(np, "local IPv6 address %pI6c\n", &np->local_ip.in6);
  393. else
  394. np_info(np, "local IPv4 address %pI4\n", &np->local_ip.ip);
  395. np_info(np, "interface '%s'\n", np->dev_name);
  396. np_info(np, "remote port %d\n", np->remote_port);
  397. if (np->ipv6)
  398. np_info(np, "remote IPv6 address %pI6c\n", &np->remote_ip.in6);
  399. else
  400. np_info(np, "remote IPv4 address %pI4\n", &np->remote_ip.ip);
  401. np_info(np, "remote ethernet address %pM\n", np->remote_mac);
  402. }
  403. EXPORT_SYMBOL(netpoll_print_options);
  404. static int netpoll_parse_ip_addr(const char *str, union inet_addr *addr)
  405. {
  406. const char *end;
  407. if (!strchr(str, ':') &&
  408. in4_pton(str, -1, (void *)addr, -1, &end) > 0) {
  409. if (!*end)
  410. return 0;
  411. }
  412. if (in6_pton(str, -1, addr->in6.s6_addr, -1, &end) > 0) {
  413. #if IS_ENABLED(CONFIG_IPV6)
  414. if (!*end)
  415. return 1;
  416. #else
  417. return -1;
  418. #endif
  419. }
  420. return -1;
  421. }
  422. int netpoll_parse_options(struct netpoll *np, char *opt)
  423. {
  424. char *cur=opt, *delim;
  425. int ipv6;
  426. bool ipversion_set = false;
  427. if (*cur != '@') {
  428. if ((delim = strchr(cur, '@')) == NULL)
  429. goto parse_failed;
  430. *delim = 0;
  431. if (kstrtou16(cur, 10, &np->local_port))
  432. goto parse_failed;
  433. cur = delim;
  434. }
  435. cur++;
  436. if (*cur != '/') {
  437. ipversion_set = true;
  438. if ((delim = strchr(cur, '/')) == NULL)
  439. goto parse_failed;
  440. *delim = 0;
  441. ipv6 = netpoll_parse_ip_addr(cur, &np->local_ip);
  442. if (ipv6 < 0)
  443. goto parse_failed;
  444. else
  445. np->ipv6 = (bool)ipv6;
  446. cur = delim;
  447. }
  448. cur++;
  449. if (*cur != ',') {
  450. /* parse out dev name */
  451. if ((delim = strchr(cur, ',')) == NULL)
  452. goto parse_failed;
  453. *delim = 0;
  454. strlcpy(np->dev_name, cur, sizeof(np->dev_name));
  455. cur = delim;
  456. }
  457. cur++;
  458. if (*cur != '@') {
  459. /* dst port */
  460. if ((delim = strchr(cur, '@')) == NULL)
  461. goto parse_failed;
  462. *delim = 0;
  463. if (*cur == ' ' || *cur == '\t')
  464. np_info(np, "warning: whitespace is not allowed\n");
  465. if (kstrtou16(cur, 10, &np->remote_port))
  466. goto parse_failed;
  467. cur = delim;
  468. }
  469. cur++;
  470. /* dst ip */
  471. if ((delim = strchr(cur, '/')) == NULL)
  472. goto parse_failed;
  473. *delim = 0;
  474. ipv6 = netpoll_parse_ip_addr(cur, &np->remote_ip);
  475. if (ipv6 < 0)
  476. goto parse_failed;
  477. else if (ipversion_set && np->ipv6 != (bool)ipv6)
  478. goto parse_failed;
  479. else
  480. np->ipv6 = (bool)ipv6;
  481. cur = delim + 1;
  482. if (*cur != 0) {
  483. /* MAC address */
  484. if (!mac_pton(cur, np->remote_mac))
  485. goto parse_failed;
  486. }
  487. netpoll_print_options(np);
  488. return 0;
  489. parse_failed:
  490. np_info(np, "couldn't parse config at '%s'!\n", cur);
  491. return -1;
  492. }
  493. EXPORT_SYMBOL(netpoll_parse_options);
  494. int __netpoll_setup(struct netpoll *np, struct net_device *ndev)
  495. {
  496. struct netpoll_info *npinfo;
  497. const struct net_device_ops *ops;
  498. int err;
  499. np->dev = ndev;
  500. strlcpy(np->dev_name, ndev->name, IFNAMSIZ);
  501. INIT_WORK(&np->cleanup_work, netpoll_async_cleanup);
  502. if ((ndev->priv_flags & IFF_DISABLE_NETPOLL) ||
  503. !ndev->netdev_ops->ndo_poll_controller) {
  504. np_err(np, "%s doesn't support polling, aborting\n",
  505. np->dev_name);
  506. err = -ENOTSUPP;
  507. goto out;
  508. }
  509. if (!ndev->npinfo) {
  510. npinfo = kmalloc(sizeof(*npinfo), GFP_KERNEL);
  511. if (!npinfo) {
  512. err = -ENOMEM;
  513. goto out;
  514. }
  515. sema_init(&npinfo->dev_lock, 1);
  516. skb_queue_head_init(&npinfo->txq);
  517. INIT_DELAYED_WORK(&npinfo->tx_work, queue_process);
  518. atomic_set(&npinfo->refcnt, 1);
  519. ops = np->dev->netdev_ops;
  520. if (ops->ndo_netpoll_setup) {
  521. err = ops->ndo_netpoll_setup(ndev, npinfo);
  522. if (err)
  523. goto free_npinfo;
  524. }
  525. } else {
  526. npinfo = rtnl_dereference(ndev->npinfo);
  527. atomic_inc(&npinfo->refcnt);
  528. }
  529. npinfo->netpoll = np;
  530. /* last thing to do is link it to the net device structure */
  531. rcu_assign_pointer(ndev->npinfo, npinfo);
  532. return 0;
  533. free_npinfo:
  534. kfree(npinfo);
  535. out:
  536. return err;
  537. }
  538. EXPORT_SYMBOL_GPL(__netpoll_setup);
  539. int netpoll_setup(struct netpoll *np)
  540. {
  541. struct net_device *ndev = NULL;
  542. struct in_device *in_dev;
  543. int err;
  544. rtnl_lock();
  545. if (np->dev_name) {
  546. struct net *net = current->nsproxy->net_ns;
  547. ndev = __dev_get_by_name(net, np->dev_name);
  548. }
  549. if (!ndev) {
  550. np_err(np, "%s doesn't exist, aborting\n", np->dev_name);
  551. err = -ENODEV;
  552. goto unlock;
  553. }
  554. dev_hold(ndev);
  555. if (netdev_master_upper_dev_get(ndev)) {
  556. np_err(np, "%s is a slave device, aborting\n", np->dev_name);
  557. err = -EBUSY;
  558. goto put;
  559. }
  560. if (!netif_running(ndev)) {
  561. unsigned long atmost, atleast;
  562. np_info(np, "device %s not up yet, forcing it\n", np->dev_name);
  563. err = dev_open(ndev);
  564. if (err) {
  565. np_err(np, "failed to open %s\n", ndev->name);
  566. goto put;
  567. }
  568. rtnl_unlock();
  569. atleast = jiffies + HZ/10;
  570. atmost = jiffies + carrier_timeout * HZ;
  571. while (!netif_carrier_ok(ndev)) {
  572. if (time_after(jiffies, atmost)) {
  573. np_notice(np, "timeout waiting for carrier\n");
  574. break;
  575. }
  576. msleep(1);
  577. }
  578. /* If carrier appears to come up instantly, we don't
  579. * trust it and pause so that we don't pump all our
  580. * queued console messages into the bitbucket.
  581. */
  582. if (time_before(jiffies, atleast)) {
  583. np_notice(np, "carrier detect appears untrustworthy, waiting 4 seconds\n");
  584. msleep(4000);
  585. }
  586. rtnl_lock();
  587. }
  588. if (!np->local_ip.ip) {
  589. if (!np->ipv6) {
  590. in_dev = __in_dev_get_rtnl(ndev);
  591. if (!in_dev || !in_dev->ifa_list) {
  592. np_err(np, "no IP address for %s, aborting\n",
  593. np->dev_name);
  594. err = -EDESTADDRREQ;
  595. goto put;
  596. }
  597. np->local_ip.ip = in_dev->ifa_list->ifa_local;
  598. np_info(np, "local IP %pI4\n", &np->local_ip.ip);
  599. } else {
  600. #if IS_ENABLED(CONFIG_IPV6)
  601. struct inet6_dev *idev;
  602. err = -EDESTADDRREQ;
  603. idev = __in6_dev_get(ndev);
  604. if (idev) {
  605. struct inet6_ifaddr *ifp;
  606. read_lock_bh(&idev->lock);
  607. list_for_each_entry(ifp, &idev->addr_list, if_list) {
  608. if (ipv6_addr_type(&ifp->addr) & IPV6_ADDR_LINKLOCAL)
  609. continue;
  610. np->local_ip.in6 = ifp->addr;
  611. err = 0;
  612. break;
  613. }
  614. read_unlock_bh(&idev->lock);
  615. }
  616. if (err) {
  617. np_err(np, "no IPv6 address for %s, aborting\n",
  618. np->dev_name);
  619. goto put;
  620. } else
  621. np_info(np, "local IPv6 %pI6c\n", &np->local_ip.in6);
  622. #else
  623. np_err(np, "IPv6 is not supported %s, aborting\n",
  624. np->dev_name);
  625. err = -EINVAL;
  626. goto put;
  627. #endif
  628. }
  629. }
  630. /* fill up the skb queue */
  631. refill_skbs();
  632. err = __netpoll_setup(np, ndev);
  633. if (err)
  634. goto put;
  635. rtnl_unlock();
  636. return 0;
  637. put:
  638. dev_put(ndev);
  639. unlock:
  640. rtnl_unlock();
  641. return err;
  642. }
  643. EXPORT_SYMBOL(netpoll_setup);
  644. static int __init netpoll_init(void)
  645. {
  646. skb_queue_head_init(&skb_pool);
  647. return 0;
  648. }
  649. core_initcall(netpoll_init);
  650. static void rcu_cleanup_netpoll_info(struct rcu_head *rcu_head)
  651. {
  652. struct netpoll_info *npinfo =
  653. container_of(rcu_head, struct netpoll_info, rcu);
  654. skb_queue_purge(&npinfo->txq);
  655. /* we can't call cancel_delayed_work_sync here, as we are in softirq */
  656. cancel_delayed_work(&npinfo->tx_work);
  657. /* clean after last, unfinished work */
  658. __skb_queue_purge(&npinfo->txq);
  659. /* now cancel it again */
  660. cancel_delayed_work(&npinfo->tx_work);
  661. kfree(npinfo);
  662. }
  663. void __netpoll_cleanup(struct netpoll *np)
  664. {
  665. struct netpoll_info *npinfo;
  666. /* rtnl_dereference would be preferable here but
  667. * rcu_cleanup_netpoll path can put us in here safely without
  668. * holding the rtnl, so plain rcu_dereference it is
  669. */
  670. npinfo = rtnl_dereference(np->dev->npinfo);
  671. if (!npinfo)
  672. return;
  673. synchronize_srcu(&netpoll_srcu);
  674. if (atomic_dec_and_test(&npinfo->refcnt)) {
  675. const struct net_device_ops *ops;
  676. ops = np->dev->netdev_ops;
  677. if (ops->ndo_netpoll_cleanup)
  678. ops->ndo_netpoll_cleanup(np->dev);
  679. RCU_INIT_POINTER(np->dev->npinfo, NULL);
  680. call_rcu_bh(&npinfo->rcu, rcu_cleanup_netpoll_info);
  681. } else
  682. RCU_INIT_POINTER(np->dev->npinfo, NULL);
  683. }
  684. EXPORT_SYMBOL_GPL(__netpoll_cleanup);
  685. static void netpoll_async_cleanup(struct work_struct *work)
  686. {
  687. struct netpoll *np = container_of(work, struct netpoll, cleanup_work);
  688. rtnl_lock();
  689. __netpoll_cleanup(np);
  690. rtnl_unlock();
  691. kfree(np);
  692. }
  693. void __netpoll_free_async(struct netpoll *np)
  694. {
  695. schedule_work(&np->cleanup_work);
  696. }
  697. EXPORT_SYMBOL_GPL(__netpoll_free_async);
  698. void netpoll_cleanup(struct netpoll *np)
  699. {
  700. rtnl_lock();
  701. if (!np->dev)
  702. goto out;
  703. __netpoll_cleanup(np);
  704. dev_put(np->dev);
  705. np->dev = NULL;
  706. out:
  707. rtnl_unlock();
  708. }
  709. EXPORT_SYMBOL(netpoll_cleanup);