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