netpoll.c 19 KB

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