netpoll.c 19 KB

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