netpoll.c 20 KB

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