netpoll.c 19 KB

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