netpoll.c 21 KB

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