netpoll.c 19 KB

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