netpoll.c 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945
  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/netdevice.h>
  14. #include <linux/etherdevice.h>
  15. #include <linux/string.h>
  16. #include <linux/if_arp.h>
  17. #include <linux/inetdevice.h>
  18. #include <linux/inet.h>
  19. #include <linux/interrupt.h>
  20. #include <linux/netpoll.h>
  21. #include <linux/sched.h>
  22. #include <linux/delay.h>
  23. #include <linux/rcupdate.h>
  24. #include <linux/workqueue.h>
  25. #include <linux/slab.h>
  26. #include <linux/export.h>
  27. #include <net/tcp.h>
  28. #include <net/udp.h>
  29. #include <asm/unaligned.h>
  30. #include <trace/events/napi.h>
  31. /*
  32. * We maintain a small pool of fully-sized skbs, to make sure the
  33. * message gets out even in extreme OOM situations.
  34. */
  35. #define MAX_UDP_CHUNK 1460
  36. #define MAX_SKBS 32
  37. static struct sk_buff_head skb_pool;
  38. static atomic_t trapped;
  39. #define USEC_PER_POLL 50
  40. #define NETPOLL_RX_ENABLED 1
  41. #define NETPOLL_RX_DROP 2
  42. #define MAX_SKB_SIZE \
  43. (MAX_UDP_CHUNK + sizeof(struct udphdr) + \
  44. sizeof(struct iphdr) + sizeof(struct ethhdr))
  45. static void zap_completion_queue(void);
  46. static void arp_reply(struct sk_buff *skb);
  47. static unsigned int carrier_timeout = 4;
  48. module_param(carrier_timeout, uint, 0644);
  49. #define np_info(np, fmt, ...) \
  50. pr_info("%s: " fmt, np->name, ##__VA_ARGS__)
  51. #define np_err(np, fmt, ...) \
  52. pr_err("%s: " fmt, np->name, ##__VA_ARGS__)
  53. #define np_notice(np, fmt, ...) \
  54. pr_notice("%s: " fmt, np->name, ##__VA_ARGS__)
  55. static void queue_process(struct work_struct *work)
  56. {
  57. struct netpoll_info *npinfo =
  58. container_of(work, struct netpoll_info, tx_work.work);
  59. struct sk_buff *skb;
  60. unsigned long flags;
  61. while ((skb = skb_dequeue(&npinfo->txq))) {
  62. struct net_device *dev = skb->dev;
  63. const struct net_device_ops *ops = dev->netdev_ops;
  64. struct netdev_queue *txq;
  65. if (!netif_device_present(dev) || !netif_running(dev)) {
  66. __kfree_skb(skb);
  67. continue;
  68. }
  69. txq = netdev_get_tx_queue(dev, skb_get_queue_mapping(skb));
  70. local_irq_save(flags);
  71. __netif_tx_lock(txq, smp_processor_id());
  72. if (netif_xmit_frozen_or_stopped(txq) ||
  73. ops->ndo_start_xmit(skb, dev) != NETDEV_TX_OK) {
  74. skb_queue_head(&npinfo->txq, skb);
  75. __netif_tx_unlock(txq);
  76. local_irq_restore(flags);
  77. schedule_delayed_work(&npinfo->tx_work, HZ/10);
  78. return;
  79. }
  80. __netif_tx_unlock(txq);
  81. local_irq_restore(flags);
  82. }
  83. }
  84. static __sum16 checksum_udp(struct sk_buff *skb, struct udphdr *uh,
  85. unsigned short ulen, __be32 saddr, __be32 daddr)
  86. {
  87. __wsum psum;
  88. if (uh->check == 0 || skb_csum_unnecessary(skb))
  89. return 0;
  90. psum = csum_tcpudp_nofold(saddr, daddr, ulen, IPPROTO_UDP, 0);
  91. if (skb->ip_summed == CHECKSUM_COMPLETE &&
  92. !csum_fold(csum_add(psum, skb->csum)))
  93. return 0;
  94. skb->csum = psum;
  95. return __skb_checksum_complete(skb);
  96. }
  97. /*
  98. * Check whether delayed processing was scheduled for our NIC. If so,
  99. * we attempt to grab the poll lock and use ->poll() to pump the card.
  100. * If this fails, either we've recursed in ->poll() or it's already
  101. * running on another CPU.
  102. *
  103. * Note: we don't mask interrupts with this lock because we're using
  104. * trylock here and interrupts are already disabled in the softirq
  105. * case. Further, we test the poll_owner to avoid recursion on UP
  106. * systems where the lock doesn't exist.
  107. *
  108. * In cases where there is bi-directional communications, reading only
  109. * one message at a time can lead to packets being dropped by the
  110. * network adapter, forcing superfluous retries and possibly timeouts.
  111. * Thus, we set our budget to greater than 1.
  112. */
  113. static int poll_one_napi(struct netpoll_info *npinfo,
  114. struct napi_struct *napi, int budget)
  115. {
  116. int work;
  117. /* net_rx_action's ->poll() invocations and our's are
  118. * synchronized by this test which is only made while
  119. * holding the napi->poll_lock.
  120. */
  121. if (!test_bit(NAPI_STATE_SCHED, &napi->state))
  122. return budget;
  123. npinfo->rx_flags |= NETPOLL_RX_DROP;
  124. atomic_inc(&trapped);
  125. set_bit(NAPI_STATE_NPSVC, &napi->state);
  126. work = napi->poll(napi, budget);
  127. trace_napi_poll(napi);
  128. clear_bit(NAPI_STATE_NPSVC, &napi->state);
  129. atomic_dec(&trapped);
  130. npinfo->rx_flags &= ~NETPOLL_RX_DROP;
  131. return budget - work;
  132. }
  133. static void poll_napi(struct net_device *dev)
  134. {
  135. struct napi_struct *napi;
  136. int budget = 16;
  137. list_for_each_entry(napi, &dev->napi_list, dev_list) {
  138. if (napi->poll_owner != smp_processor_id() &&
  139. spin_trylock(&napi->poll_lock)) {
  140. budget = poll_one_napi(dev->npinfo, napi, budget);
  141. spin_unlock(&napi->poll_lock);
  142. if (!budget)
  143. break;
  144. }
  145. }
  146. }
  147. static void service_arp_queue(struct netpoll_info *npi)
  148. {
  149. if (npi) {
  150. struct sk_buff *skb;
  151. while ((skb = skb_dequeue(&npi->arp_tx)))
  152. arp_reply(skb);
  153. }
  154. }
  155. static void netpoll_poll_dev(struct net_device *dev)
  156. {
  157. const struct net_device_ops *ops;
  158. if (!dev || !netif_running(dev))
  159. return;
  160. ops = dev->netdev_ops;
  161. if (!ops->ndo_poll_controller)
  162. return;
  163. /* Process pending work on NIC */
  164. ops->ndo_poll_controller(dev);
  165. poll_napi(dev);
  166. if (dev->priv_flags & IFF_SLAVE) {
  167. if (dev->npinfo) {
  168. struct net_device *bond_dev = dev->master;
  169. struct sk_buff *skb;
  170. while ((skb = skb_dequeue(&dev->npinfo->arp_tx))) {
  171. skb->dev = bond_dev;
  172. skb_queue_tail(&bond_dev->npinfo->arp_tx, skb);
  173. }
  174. }
  175. }
  176. service_arp_queue(dev->npinfo);
  177. zap_completion_queue();
  178. }
  179. static void refill_skbs(void)
  180. {
  181. struct sk_buff *skb;
  182. unsigned long flags;
  183. spin_lock_irqsave(&skb_pool.lock, flags);
  184. while (skb_pool.qlen < MAX_SKBS) {
  185. skb = alloc_skb(MAX_SKB_SIZE, GFP_ATOMIC);
  186. if (!skb)
  187. break;
  188. __skb_queue_tail(&skb_pool, skb);
  189. }
  190. spin_unlock_irqrestore(&skb_pool.lock, flags);
  191. }
  192. static void zap_completion_queue(void)
  193. {
  194. unsigned long flags;
  195. struct softnet_data *sd = &get_cpu_var(softnet_data);
  196. if (sd->completion_queue) {
  197. struct sk_buff *clist;
  198. local_irq_save(flags);
  199. clist = sd->completion_queue;
  200. sd->completion_queue = NULL;
  201. local_irq_restore(flags);
  202. while (clist != NULL) {
  203. struct sk_buff *skb = clist;
  204. clist = clist->next;
  205. if (skb->destructor) {
  206. atomic_inc(&skb->users);
  207. dev_kfree_skb_any(skb); /* put this one back */
  208. } else {
  209. __kfree_skb(skb);
  210. }
  211. }
  212. }
  213. put_cpu_var(softnet_data);
  214. }
  215. static struct sk_buff *find_skb(struct netpoll *np, int len, int reserve)
  216. {
  217. int count = 0;
  218. struct sk_buff *skb;
  219. zap_completion_queue();
  220. refill_skbs();
  221. repeat:
  222. skb = alloc_skb(len, GFP_ATOMIC);
  223. if (!skb)
  224. skb = skb_dequeue(&skb_pool);
  225. if (!skb) {
  226. if (++count < 10) {
  227. netpoll_poll_dev(np->dev);
  228. goto repeat;
  229. }
  230. return NULL;
  231. }
  232. atomic_set(&skb->users, 1);
  233. skb_reserve(skb, reserve);
  234. return skb;
  235. }
  236. static int netpoll_owner_active(struct net_device *dev)
  237. {
  238. struct napi_struct *napi;
  239. list_for_each_entry(napi, &dev->napi_list, dev_list) {
  240. if (napi->poll_owner == smp_processor_id())
  241. return 1;
  242. }
  243. return 0;
  244. }
  245. void netpoll_send_skb_on_dev(struct netpoll *np, struct sk_buff *skb,
  246. struct net_device *dev)
  247. {
  248. int status = NETDEV_TX_BUSY;
  249. unsigned long tries;
  250. const struct net_device_ops *ops = dev->netdev_ops;
  251. /* It is up to the caller to keep npinfo alive. */
  252. struct netpoll_info *npinfo = np->dev->npinfo;
  253. if (!npinfo || !netif_running(dev) || !netif_device_present(dev)) {
  254. __kfree_skb(skb);
  255. return;
  256. }
  257. /* don't get messages out of order, and no recursion */
  258. if (skb_queue_len(&npinfo->txq) == 0 && !netpoll_owner_active(dev)) {
  259. struct netdev_queue *txq;
  260. unsigned long flags;
  261. txq = netdev_get_tx_queue(dev, skb_get_queue_mapping(skb));
  262. local_irq_save(flags);
  263. /* try until next clock tick */
  264. for (tries = jiffies_to_usecs(1)/USEC_PER_POLL;
  265. tries > 0; --tries) {
  266. if (__netif_tx_trylock(txq)) {
  267. if (!netif_xmit_stopped(txq)) {
  268. status = ops->ndo_start_xmit(skb, dev);
  269. if (status == NETDEV_TX_OK)
  270. txq_trans_update(txq);
  271. }
  272. __netif_tx_unlock(txq);
  273. if (status == NETDEV_TX_OK)
  274. break;
  275. }
  276. /* tickle device maybe there is some cleanup */
  277. netpoll_poll_dev(np->dev);
  278. udelay(USEC_PER_POLL);
  279. }
  280. WARN_ONCE(!irqs_disabled(),
  281. "netpoll_send_skb(): %s enabled interrupts in poll (%pF)\n",
  282. dev->name, ops->ndo_start_xmit);
  283. local_irq_restore(flags);
  284. }
  285. if (status != NETDEV_TX_OK) {
  286. skb_queue_tail(&npinfo->txq, skb);
  287. schedule_delayed_work(&npinfo->tx_work,0);
  288. }
  289. }
  290. EXPORT_SYMBOL(netpoll_send_skb_on_dev);
  291. void netpoll_send_udp(struct netpoll *np, const char *msg, int len)
  292. {
  293. int total_len, eth_len, ip_len, udp_len;
  294. struct sk_buff *skb;
  295. struct udphdr *udph;
  296. struct iphdr *iph;
  297. struct ethhdr *eth;
  298. udp_len = len + sizeof(*udph);
  299. ip_len = eth_len = udp_len + sizeof(*iph);
  300. total_len = eth_len + ETH_HLEN + NET_IP_ALIGN;
  301. skb = find_skb(np, total_len, total_len - len);
  302. if (!skb)
  303. return;
  304. skb_copy_to_linear_data(skb, msg, len);
  305. skb->len += len;
  306. skb_push(skb, sizeof(*udph));
  307. skb_reset_transport_header(skb);
  308. udph = udp_hdr(skb);
  309. udph->source = htons(np->local_port);
  310. udph->dest = htons(np->remote_port);
  311. udph->len = htons(udp_len);
  312. udph->check = 0;
  313. udph->check = csum_tcpudp_magic(np->local_ip,
  314. np->remote_ip,
  315. udp_len, IPPROTO_UDP,
  316. csum_partial(udph, udp_len, 0));
  317. if (udph->check == 0)
  318. udph->check = CSUM_MANGLED_0;
  319. skb_push(skb, sizeof(*iph));
  320. skb_reset_network_header(skb);
  321. iph = ip_hdr(skb);
  322. /* iph->version = 4; iph->ihl = 5; */
  323. put_unaligned(0x45, (unsigned char *)iph);
  324. iph->tos = 0;
  325. put_unaligned(htons(ip_len), &(iph->tot_len));
  326. iph->id = 0;
  327. iph->frag_off = 0;
  328. iph->ttl = 64;
  329. iph->protocol = IPPROTO_UDP;
  330. iph->check = 0;
  331. put_unaligned(np->local_ip, &(iph->saddr));
  332. put_unaligned(np->remote_ip, &(iph->daddr));
  333. iph->check = ip_fast_csum((unsigned char *)iph, iph->ihl);
  334. eth = (struct ethhdr *) skb_push(skb, ETH_HLEN);
  335. skb_reset_mac_header(skb);
  336. skb->protocol = eth->h_proto = htons(ETH_P_IP);
  337. memcpy(eth->h_source, np->dev->dev_addr, ETH_ALEN);
  338. memcpy(eth->h_dest, np->remote_mac, ETH_ALEN);
  339. skb->dev = np->dev;
  340. netpoll_send_skb(np, skb);
  341. }
  342. EXPORT_SYMBOL(netpoll_send_udp);
  343. static void arp_reply(struct sk_buff *skb)
  344. {
  345. struct netpoll_info *npinfo = skb->dev->npinfo;
  346. struct arphdr *arp;
  347. unsigned char *arp_ptr;
  348. int size, type = ARPOP_REPLY, ptype = ETH_P_ARP;
  349. __be32 sip, tip;
  350. unsigned char *sha;
  351. struct sk_buff *send_skb;
  352. struct netpoll *np, *tmp;
  353. unsigned long flags;
  354. int hlen, tlen;
  355. int hits = 0;
  356. if (list_empty(&npinfo->rx_np))
  357. return;
  358. /* Before checking the packet, we do some early
  359. inspection whether this is interesting at all */
  360. spin_lock_irqsave(&npinfo->rx_lock, flags);
  361. list_for_each_entry_safe(np, tmp, &npinfo->rx_np, rx) {
  362. if (np->dev == skb->dev)
  363. hits++;
  364. }
  365. spin_unlock_irqrestore(&npinfo->rx_lock, flags);
  366. /* No netpoll struct is using this dev */
  367. if (!hits)
  368. return;
  369. /* No arp on this interface */
  370. if (skb->dev->flags & IFF_NOARP)
  371. return;
  372. if (!pskb_may_pull(skb, arp_hdr_len(skb->dev)))
  373. return;
  374. skb_reset_network_header(skb);
  375. skb_reset_transport_header(skb);
  376. arp = arp_hdr(skb);
  377. if ((arp->ar_hrd != htons(ARPHRD_ETHER) &&
  378. arp->ar_hrd != htons(ARPHRD_IEEE802)) ||
  379. arp->ar_pro != htons(ETH_P_IP) ||
  380. arp->ar_op != htons(ARPOP_REQUEST))
  381. return;
  382. arp_ptr = (unsigned char *)(arp+1);
  383. /* save the location of the src hw addr */
  384. sha = arp_ptr;
  385. arp_ptr += skb->dev->addr_len;
  386. memcpy(&sip, arp_ptr, 4);
  387. arp_ptr += 4;
  388. /* If we actually cared about dst hw addr,
  389. it would get copied here */
  390. arp_ptr += skb->dev->addr_len;
  391. memcpy(&tip, arp_ptr, 4);
  392. /* Should we ignore arp? */
  393. if (ipv4_is_loopback(tip) || ipv4_is_multicast(tip))
  394. return;
  395. size = arp_hdr_len(skb->dev);
  396. spin_lock_irqsave(&npinfo->rx_lock, flags);
  397. list_for_each_entry_safe(np, tmp, &npinfo->rx_np, rx) {
  398. if (tip != np->local_ip)
  399. continue;
  400. hlen = LL_RESERVED_SPACE(np->dev);
  401. tlen = np->dev->needed_tailroom;
  402. send_skb = find_skb(np, size + hlen + tlen, hlen);
  403. if (!send_skb)
  404. continue;
  405. skb_reset_network_header(send_skb);
  406. arp = (struct arphdr *) skb_put(send_skb, size);
  407. send_skb->dev = skb->dev;
  408. send_skb->protocol = htons(ETH_P_ARP);
  409. /* Fill the device header for the ARP frame */
  410. if (dev_hard_header(send_skb, skb->dev, ptype,
  411. sha, np->dev->dev_addr,
  412. send_skb->len) < 0) {
  413. kfree_skb(send_skb);
  414. continue;
  415. }
  416. /*
  417. * Fill out the arp protocol part.
  418. *
  419. * we only support ethernet device type,
  420. * which (according to RFC 1390) should
  421. * always equal 1 (Ethernet).
  422. */
  423. arp->ar_hrd = htons(np->dev->type);
  424. arp->ar_pro = htons(ETH_P_IP);
  425. arp->ar_hln = np->dev->addr_len;
  426. arp->ar_pln = 4;
  427. arp->ar_op = htons(type);
  428. arp_ptr = (unsigned char *)(arp + 1);
  429. memcpy(arp_ptr, np->dev->dev_addr, np->dev->addr_len);
  430. arp_ptr += np->dev->addr_len;
  431. memcpy(arp_ptr, &tip, 4);
  432. arp_ptr += 4;
  433. memcpy(arp_ptr, sha, np->dev->addr_len);
  434. arp_ptr += np->dev->addr_len;
  435. memcpy(arp_ptr, &sip, 4);
  436. netpoll_send_skb(np, send_skb);
  437. /* If there are several rx_hooks for the same address,
  438. we're fine by sending a single reply */
  439. break;
  440. }
  441. spin_unlock_irqrestore(&npinfo->rx_lock, flags);
  442. }
  443. int __netpoll_rx(struct sk_buff *skb)
  444. {
  445. int proto, len, ulen;
  446. int hits = 0;
  447. const struct iphdr *iph;
  448. struct udphdr *uh;
  449. struct netpoll_info *npinfo = skb->dev->npinfo;
  450. struct netpoll *np, *tmp;
  451. if (list_empty(&npinfo->rx_np))
  452. goto out;
  453. if (skb->dev->type != ARPHRD_ETHER)
  454. goto out;
  455. /* check if netpoll clients need ARP */
  456. if (skb->protocol == htons(ETH_P_ARP) &&
  457. atomic_read(&trapped)) {
  458. skb_queue_tail(&npinfo->arp_tx, skb);
  459. return 1;
  460. }
  461. proto = ntohs(eth_hdr(skb)->h_proto);
  462. if (proto != ETH_P_IP)
  463. goto out;
  464. if (skb->pkt_type == PACKET_OTHERHOST)
  465. goto out;
  466. if (skb_shared(skb))
  467. goto out;
  468. if (!pskb_may_pull(skb, sizeof(struct iphdr)))
  469. goto out;
  470. iph = (struct iphdr *)skb->data;
  471. if (iph->ihl < 5 || iph->version != 4)
  472. goto out;
  473. if (!pskb_may_pull(skb, iph->ihl*4))
  474. goto out;
  475. iph = (struct iphdr *)skb->data;
  476. if (ip_fast_csum((u8 *)iph, iph->ihl) != 0)
  477. goto out;
  478. len = ntohs(iph->tot_len);
  479. if (skb->len < len || len < iph->ihl*4)
  480. goto out;
  481. /*
  482. * Our transport medium may have padded the buffer out.
  483. * Now We trim to the true length of the frame.
  484. */
  485. if (pskb_trim_rcsum(skb, len))
  486. goto out;
  487. iph = (struct iphdr *)skb->data;
  488. if (iph->protocol != IPPROTO_UDP)
  489. goto out;
  490. len -= iph->ihl*4;
  491. uh = (struct udphdr *)(((char *)iph) + iph->ihl*4);
  492. ulen = ntohs(uh->len);
  493. if (ulen != len)
  494. goto out;
  495. if (checksum_udp(skb, uh, ulen, iph->saddr, iph->daddr))
  496. goto out;
  497. list_for_each_entry_safe(np, tmp, &npinfo->rx_np, rx) {
  498. if (np->local_ip && np->local_ip != iph->daddr)
  499. continue;
  500. if (np->remote_ip && np->remote_ip != iph->saddr)
  501. continue;
  502. if (np->local_port && np->local_port != ntohs(uh->dest))
  503. continue;
  504. np->rx_hook(np, ntohs(uh->source),
  505. (char *)(uh+1),
  506. ulen - sizeof(struct udphdr));
  507. hits++;
  508. }
  509. if (!hits)
  510. goto out;
  511. kfree_skb(skb);
  512. return 1;
  513. out:
  514. if (atomic_read(&trapped)) {
  515. kfree_skb(skb);
  516. return 1;
  517. }
  518. return 0;
  519. }
  520. void netpoll_print_options(struct netpoll *np)
  521. {
  522. np_info(np, "local port %d\n", np->local_port);
  523. np_info(np, "local IP %pI4\n", &np->local_ip);
  524. np_info(np, "interface '%s'\n", np->dev_name);
  525. np_info(np, "remote port %d\n", np->remote_port);
  526. np_info(np, "remote IP %pI4\n", &np->remote_ip);
  527. np_info(np, "remote ethernet address %pM\n", np->remote_mac);
  528. }
  529. EXPORT_SYMBOL(netpoll_print_options);
  530. int netpoll_parse_options(struct netpoll *np, char *opt)
  531. {
  532. char *cur=opt, *delim;
  533. if (*cur != '@') {
  534. if ((delim = strchr(cur, '@')) == NULL)
  535. goto parse_failed;
  536. *delim = 0;
  537. np->local_port = simple_strtol(cur, NULL, 10);
  538. cur = delim;
  539. }
  540. cur++;
  541. if (*cur != '/') {
  542. if ((delim = strchr(cur, '/')) == NULL)
  543. goto parse_failed;
  544. *delim = 0;
  545. np->local_ip = in_aton(cur);
  546. cur = delim;
  547. }
  548. cur++;
  549. if (*cur != ',') {
  550. /* parse out dev name */
  551. if ((delim = strchr(cur, ',')) == NULL)
  552. goto parse_failed;
  553. *delim = 0;
  554. strlcpy(np->dev_name, cur, sizeof(np->dev_name));
  555. cur = delim;
  556. }
  557. cur++;
  558. if (*cur != '@') {
  559. /* dst port */
  560. if ((delim = strchr(cur, '@')) == NULL)
  561. goto parse_failed;
  562. *delim = 0;
  563. if (*cur == ' ' || *cur == '\t')
  564. np_info(np, "warning: whitespace is not allowed\n");
  565. np->remote_port = simple_strtol(cur, NULL, 10);
  566. cur = delim;
  567. }
  568. cur++;
  569. /* dst ip */
  570. if ((delim = strchr(cur, '/')) == NULL)
  571. goto parse_failed;
  572. *delim = 0;
  573. np->remote_ip = in_aton(cur);
  574. cur = delim + 1;
  575. if (*cur != 0) {
  576. /* MAC address */
  577. if (!mac_pton(cur, np->remote_mac))
  578. goto parse_failed;
  579. }
  580. netpoll_print_options(np);
  581. return 0;
  582. parse_failed:
  583. np_info(np, "couldn't parse config at '%s'!\n", cur);
  584. return -1;
  585. }
  586. EXPORT_SYMBOL(netpoll_parse_options);
  587. int __netpoll_setup(struct netpoll *np)
  588. {
  589. struct net_device *ndev = np->dev;
  590. struct netpoll_info *npinfo;
  591. const struct net_device_ops *ops;
  592. unsigned long flags;
  593. int err;
  594. if ((ndev->priv_flags & IFF_DISABLE_NETPOLL) ||
  595. !ndev->netdev_ops->ndo_poll_controller) {
  596. np_err(np, "%s doesn't support polling, aborting\n",
  597. np->dev_name);
  598. err = -ENOTSUPP;
  599. goto out;
  600. }
  601. if (!ndev->npinfo) {
  602. npinfo = kmalloc(sizeof(*npinfo), GFP_KERNEL);
  603. if (!npinfo) {
  604. err = -ENOMEM;
  605. goto out;
  606. }
  607. npinfo->rx_flags = 0;
  608. INIT_LIST_HEAD(&npinfo->rx_np);
  609. spin_lock_init(&npinfo->rx_lock);
  610. skb_queue_head_init(&npinfo->arp_tx);
  611. skb_queue_head_init(&npinfo->txq);
  612. INIT_DELAYED_WORK(&npinfo->tx_work, queue_process);
  613. atomic_set(&npinfo->refcnt, 1);
  614. ops = np->dev->netdev_ops;
  615. if (ops->ndo_netpoll_setup) {
  616. err = ops->ndo_netpoll_setup(ndev, npinfo);
  617. if (err)
  618. goto free_npinfo;
  619. }
  620. } else {
  621. npinfo = ndev->npinfo;
  622. atomic_inc(&npinfo->refcnt);
  623. }
  624. npinfo->netpoll = np;
  625. if (np->rx_hook) {
  626. spin_lock_irqsave(&npinfo->rx_lock, flags);
  627. npinfo->rx_flags |= NETPOLL_RX_ENABLED;
  628. list_add_tail(&np->rx, &npinfo->rx_np);
  629. spin_unlock_irqrestore(&npinfo->rx_lock, flags);
  630. }
  631. /* last thing to do is link it to the net device structure */
  632. rcu_assign_pointer(ndev->npinfo, npinfo);
  633. return 0;
  634. free_npinfo:
  635. kfree(npinfo);
  636. out:
  637. return err;
  638. }
  639. EXPORT_SYMBOL_GPL(__netpoll_setup);
  640. int netpoll_setup(struct netpoll *np)
  641. {
  642. struct net_device *ndev = NULL;
  643. struct in_device *in_dev;
  644. int err;
  645. if (np->dev_name)
  646. ndev = dev_get_by_name(&init_net, np->dev_name);
  647. if (!ndev) {
  648. np_err(np, "%s doesn't exist, aborting\n", np->dev_name);
  649. return -ENODEV;
  650. }
  651. if (ndev->master) {
  652. np_err(np, "%s is a slave device, aborting\n", np->dev_name);
  653. err = -EBUSY;
  654. goto put;
  655. }
  656. if (!netif_running(ndev)) {
  657. unsigned long atmost, atleast;
  658. np_info(np, "device %s not up yet, forcing it\n", np->dev_name);
  659. rtnl_lock();
  660. err = dev_open(ndev);
  661. rtnl_unlock();
  662. if (err) {
  663. np_err(np, "failed to open %s\n", ndev->name);
  664. goto put;
  665. }
  666. atleast = jiffies + HZ/10;
  667. atmost = jiffies + carrier_timeout * HZ;
  668. while (!netif_carrier_ok(ndev)) {
  669. if (time_after(jiffies, atmost)) {
  670. np_notice(np, "timeout waiting for carrier\n");
  671. break;
  672. }
  673. msleep(1);
  674. }
  675. /* If carrier appears to come up instantly, we don't
  676. * trust it and pause so that we don't pump all our
  677. * queued console messages into the bitbucket.
  678. */
  679. if (time_before(jiffies, atleast)) {
  680. np_notice(np, "carrier detect appears untrustworthy, waiting 4 seconds\n");
  681. msleep(4000);
  682. }
  683. }
  684. if (!np->local_ip) {
  685. rcu_read_lock();
  686. in_dev = __in_dev_get_rcu(ndev);
  687. if (!in_dev || !in_dev->ifa_list) {
  688. rcu_read_unlock();
  689. np_err(np, "no IP address for %s, aborting\n",
  690. np->dev_name);
  691. err = -EDESTADDRREQ;
  692. goto put;
  693. }
  694. np->local_ip = in_dev->ifa_list->ifa_local;
  695. rcu_read_unlock();
  696. np_info(np, "local IP %pI4\n", &np->local_ip);
  697. }
  698. np->dev = ndev;
  699. /* fill up the skb queue */
  700. refill_skbs();
  701. rtnl_lock();
  702. err = __netpoll_setup(np);
  703. rtnl_unlock();
  704. if (err)
  705. goto put;
  706. return 0;
  707. put:
  708. dev_put(ndev);
  709. return err;
  710. }
  711. EXPORT_SYMBOL(netpoll_setup);
  712. static int __init netpoll_init(void)
  713. {
  714. skb_queue_head_init(&skb_pool);
  715. return 0;
  716. }
  717. core_initcall(netpoll_init);
  718. void __netpoll_cleanup(struct netpoll *np)
  719. {
  720. struct netpoll_info *npinfo;
  721. unsigned long flags;
  722. npinfo = np->dev->npinfo;
  723. if (!npinfo)
  724. return;
  725. if (!list_empty(&npinfo->rx_np)) {
  726. spin_lock_irqsave(&npinfo->rx_lock, flags);
  727. list_del(&np->rx);
  728. if (list_empty(&npinfo->rx_np))
  729. npinfo->rx_flags &= ~NETPOLL_RX_ENABLED;
  730. spin_unlock_irqrestore(&npinfo->rx_lock, flags);
  731. }
  732. if (atomic_dec_and_test(&npinfo->refcnt)) {
  733. const struct net_device_ops *ops;
  734. ops = np->dev->netdev_ops;
  735. if (ops->ndo_netpoll_cleanup)
  736. ops->ndo_netpoll_cleanup(np->dev);
  737. RCU_INIT_POINTER(np->dev->npinfo, NULL);
  738. /* avoid racing with NAPI reading npinfo */
  739. synchronize_rcu_bh();
  740. skb_queue_purge(&npinfo->arp_tx);
  741. skb_queue_purge(&npinfo->txq);
  742. cancel_delayed_work_sync(&npinfo->tx_work);
  743. /* clean after last, unfinished work */
  744. __skb_queue_purge(&npinfo->txq);
  745. kfree(npinfo);
  746. }
  747. }
  748. EXPORT_SYMBOL_GPL(__netpoll_cleanup);
  749. void netpoll_cleanup(struct netpoll *np)
  750. {
  751. if (!np->dev)
  752. return;
  753. rtnl_lock();
  754. __netpoll_cleanup(np);
  755. rtnl_unlock();
  756. dev_put(np->dev);
  757. np->dev = NULL;
  758. }
  759. EXPORT_SYMBOL(netpoll_cleanup);
  760. int netpoll_trap(void)
  761. {
  762. return atomic_read(&trapped);
  763. }
  764. EXPORT_SYMBOL(netpoll_trap);
  765. void netpoll_set_trap(int trap)
  766. {
  767. if (trap)
  768. atomic_inc(&trapped);
  769. else
  770. atomic_dec(&trapped);
  771. }
  772. EXPORT_SYMBOL(netpoll_set_trap);