netpoll.c 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955
  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. 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->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. EXPORT_SYMBOL(netpoll_poll_dev);
  172. void netpoll_poll(struct netpoll *np)
  173. {
  174. netpoll_poll_dev(np->dev);
  175. }
  176. EXPORT_SYMBOL(netpoll_poll);
  177. static void refill_skbs(void)
  178. {
  179. struct sk_buff *skb;
  180. unsigned long flags;
  181. spin_lock_irqsave(&skb_pool.lock, flags);
  182. while (skb_pool.qlen < MAX_SKBS) {
  183. skb = alloc_skb(MAX_SKB_SIZE, GFP_ATOMIC);
  184. if (!skb)
  185. break;
  186. __skb_queue_tail(&skb_pool, skb);
  187. }
  188. spin_unlock_irqrestore(&skb_pool.lock, flags);
  189. }
  190. static void zap_completion_queue(void)
  191. {
  192. unsigned long flags;
  193. struct softnet_data *sd = &get_cpu_var(softnet_data);
  194. if (sd->completion_queue) {
  195. struct sk_buff *clist;
  196. local_irq_save(flags);
  197. clist = sd->completion_queue;
  198. sd->completion_queue = NULL;
  199. local_irq_restore(flags);
  200. while (clist != NULL) {
  201. struct sk_buff *skb = clist;
  202. clist = clist->next;
  203. if (skb->destructor) {
  204. atomic_inc(&skb->users);
  205. dev_kfree_skb_any(skb); /* put this one back */
  206. } else {
  207. __kfree_skb(skb);
  208. }
  209. }
  210. }
  211. put_cpu_var(softnet_data);
  212. }
  213. static struct sk_buff *find_skb(struct netpoll *np, int len, int reserve)
  214. {
  215. int count = 0;
  216. struct sk_buff *skb;
  217. zap_completion_queue();
  218. refill_skbs();
  219. repeat:
  220. skb = alloc_skb(len, GFP_ATOMIC);
  221. if (!skb)
  222. skb = skb_dequeue(&skb_pool);
  223. if (!skb) {
  224. if (++count < 10) {
  225. netpoll_poll(np);
  226. goto repeat;
  227. }
  228. return NULL;
  229. }
  230. atomic_set(&skb->users, 1);
  231. skb_reserve(skb, reserve);
  232. return skb;
  233. }
  234. static int netpoll_owner_active(struct net_device *dev)
  235. {
  236. struct napi_struct *napi;
  237. list_for_each_entry(napi, &dev->napi_list, dev_list) {
  238. if (napi->poll_owner == smp_processor_id())
  239. return 1;
  240. }
  241. return 0;
  242. }
  243. void netpoll_send_skb_on_dev(struct netpoll *np, struct sk_buff *skb,
  244. struct net_device *dev)
  245. {
  246. int status = NETDEV_TX_BUSY;
  247. unsigned long tries;
  248. const struct net_device_ops *ops = dev->netdev_ops;
  249. /* It is up to the caller to keep npinfo alive. */
  250. struct netpoll_info *npinfo = np->dev->npinfo;
  251. if (!npinfo || !netif_running(dev) || !netif_device_present(dev)) {
  252. __kfree_skb(skb);
  253. return;
  254. }
  255. /* don't get messages out of order, and no recursion */
  256. if (skb_queue_len(&npinfo->txq) == 0 && !netpoll_owner_active(dev)) {
  257. struct netdev_queue *txq;
  258. unsigned long flags;
  259. txq = netdev_get_tx_queue(dev, skb_get_queue_mapping(skb));
  260. local_irq_save(flags);
  261. /* try until next clock tick */
  262. for (tries = jiffies_to_usecs(1)/USEC_PER_POLL;
  263. tries > 0; --tries) {
  264. if (__netif_tx_trylock(txq)) {
  265. if (!netif_tx_queue_stopped(txq)) {
  266. status = ops->ndo_start_xmit(skb, dev);
  267. if (status == NETDEV_TX_OK)
  268. txq_trans_update(txq);
  269. }
  270. __netif_tx_unlock(txq);
  271. if (status == NETDEV_TX_OK)
  272. break;
  273. }
  274. /* tickle device maybe there is some cleanup */
  275. netpoll_poll(np);
  276. udelay(USEC_PER_POLL);
  277. }
  278. WARN_ONCE(!irqs_disabled(),
  279. "netpoll_send_skb(): %s enabled interrupts in poll (%pF)\n",
  280. dev->name, ops->ndo_start_xmit);
  281. local_irq_restore(flags);
  282. }
  283. if (status != NETDEV_TX_OK) {
  284. skb_queue_tail(&npinfo->txq, skb);
  285. schedule_delayed_work(&npinfo->tx_work,0);
  286. }
  287. }
  288. EXPORT_SYMBOL(netpoll_send_skb_on_dev);
  289. void netpoll_send_udp(struct netpoll *np, const char *msg, int len)
  290. {
  291. int total_len, eth_len, ip_len, udp_len;
  292. struct sk_buff *skb;
  293. struct udphdr *udph;
  294. struct iphdr *iph;
  295. struct ethhdr *eth;
  296. udp_len = len + sizeof(*udph);
  297. ip_len = eth_len = udp_len + sizeof(*iph);
  298. total_len = eth_len + ETH_HLEN + NET_IP_ALIGN;
  299. skb = find_skb(np, total_len, total_len - len);
  300. if (!skb)
  301. return;
  302. skb_copy_to_linear_data(skb, msg, len);
  303. skb->len += len;
  304. skb_push(skb, sizeof(*udph));
  305. skb_reset_transport_header(skb);
  306. udph = udp_hdr(skb);
  307. udph->source = htons(np->local_port);
  308. udph->dest = htons(np->remote_port);
  309. udph->len = htons(udp_len);
  310. udph->check = 0;
  311. udph->check = csum_tcpudp_magic(np->local_ip,
  312. np->remote_ip,
  313. udp_len, IPPROTO_UDP,
  314. csum_partial(udph, udp_len, 0));
  315. if (udph->check == 0)
  316. udph->check = CSUM_MANGLED_0;
  317. skb_push(skb, sizeof(*iph));
  318. skb_reset_network_header(skb);
  319. iph = ip_hdr(skb);
  320. /* iph->version = 4; iph->ihl = 5; */
  321. put_unaligned(0x45, (unsigned char *)iph);
  322. iph->tos = 0;
  323. put_unaligned(htons(ip_len), &(iph->tot_len));
  324. iph->id = 0;
  325. iph->frag_off = 0;
  326. iph->ttl = 64;
  327. iph->protocol = IPPROTO_UDP;
  328. iph->check = 0;
  329. put_unaligned(np->local_ip, &(iph->saddr));
  330. put_unaligned(np->remote_ip, &(iph->daddr));
  331. iph->check = ip_fast_csum((unsigned char *)iph, iph->ihl);
  332. eth = (struct ethhdr *) skb_push(skb, ETH_HLEN);
  333. skb_reset_mac_header(skb);
  334. skb->protocol = eth->h_proto = htons(ETH_P_IP);
  335. memcpy(eth->h_source, np->dev->dev_addr, ETH_ALEN);
  336. memcpy(eth->h_dest, np->remote_mac, ETH_ALEN);
  337. skb->dev = np->dev;
  338. netpoll_send_skb(np, skb);
  339. }
  340. EXPORT_SYMBOL(netpoll_send_udp);
  341. static void arp_reply(struct sk_buff *skb)
  342. {
  343. struct netpoll_info *npinfo = skb->dev->npinfo;
  344. struct arphdr *arp;
  345. unsigned char *arp_ptr;
  346. int size, type = ARPOP_REPLY, ptype = ETH_P_ARP;
  347. __be32 sip, tip;
  348. unsigned char *sha;
  349. struct sk_buff *send_skb;
  350. struct netpoll *np, *tmp;
  351. unsigned long flags;
  352. int hits = 0;
  353. if (list_empty(&npinfo->rx_np))
  354. return;
  355. /* Before checking the packet, we do some early
  356. inspection whether this is interesting at all */
  357. spin_lock_irqsave(&npinfo->rx_lock, flags);
  358. list_for_each_entry_safe(np, tmp, &npinfo->rx_np, rx) {
  359. if (np->dev == skb->dev)
  360. hits++;
  361. }
  362. spin_unlock_irqrestore(&npinfo->rx_lock, flags);
  363. /* No netpoll struct is using this dev */
  364. if (!hits)
  365. return;
  366. /* No arp on this interface */
  367. if (skb->dev->flags & IFF_NOARP)
  368. return;
  369. if (!pskb_may_pull(skb, arp_hdr_len(skb->dev)))
  370. return;
  371. skb_reset_network_header(skb);
  372. skb_reset_transport_header(skb);
  373. arp = arp_hdr(skb);
  374. if ((arp->ar_hrd != htons(ARPHRD_ETHER) &&
  375. arp->ar_hrd != htons(ARPHRD_IEEE802)) ||
  376. arp->ar_pro != htons(ETH_P_IP) ||
  377. arp->ar_op != htons(ARPOP_REQUEST))
  378. return;
  379. arp_ptr = (unsigned char *)(arp+1);
  380. /* save the location of the src hw addr */
  381. sha = arp_ptr;
  382. arp_ptr += skb->dev->addr_len;
  383. memcpy(&sip, arp_ptr, 4);
  384. arp_ptr += 4;
  385. /* If we actually cared about dst hw addr,
  386. it would get copied here */
  387. arp_ptr += skb->dev->addr_len;
  388. memcpy(&tip, arp_ptr, 4);
  389. /* Should we ignore arp? */
  390. if (ipv4_is_loopback(tip) || ipv4_is_multicast(tip))
  391. return;
  392. size = arp_hdr_len(skb->dev);
  393. spin_lock_irqsave(&npinfo->rx_lock, flags);
  394. list_for_each_entry_safe(np, tmp, &npinfo->rx_np, rx) {
  395. if (tip != np->local_ip)
  396. continue;
  397. send_skb = find_skb(np, size + LL_ALLOCATED_SPACE(np->dev),
  398. LL_RESERVED_SPACE(np->dev));
  399. if (!send_skb)
  400. continue;
  401. skb_reset_network_header(send_skb);
  402. arp = (struct arphdr *) skb_put(send_skb, size);
  403. send_skb->dev = skb->dev;
  404. send_skb->protocol = htons(ETH_P_ARP);
  405. /* Fill the device header for the ARP frame */
  406. if (dev_hard_header(send_skb, skb->dev, ptype,
  407. sha, np->dev->dev_addr,
  408. send_skb->len) < 0) {
  409. kfree_skb(send_skb);
  410. continue;
  411. }
  412. /*
  413. * Fill out the arp protocol part.
  414. *
  415. * we only support ethernet device type,
  416. * which (according to RFC 1390) should
  417. * always equal 1 (Ethernet).
  418. */
  419. arp->ar_hrd = htons(np->dev->type);
  420. arp->ar_pro = htons(ETH_P_IP);
  421. arp->ar_hln = np->dev->addr_len;
  422. arp->ar_pln = 4;
  423. arp->ar_op = htons(type);
  424. arp_ptr = (unsigned char *)(arp + 1);
  425. memcpy(arp_ptr, np->dev->dev_addr, np->dev->addr_len);
  426. arp_ptr += np->dev->addr_len;
  427. memcpy(arp_ptr, &tip, 4);
  428. arp_ptr += 4;
  429. memcpy(arp_ptr, sha, np->dev->addr_len);
  430. arp_ptr += np->dev->addr_len;
  431. memcpy(arp_ptr, &sip, 4);
  432. netpoll_send_skb(np, send_skb);
  433. /* If there are several rx_hooks for the same address,
  434. we're fine by sending a single reply */
  435. break;
  436. }
  437. spin_unlock_irqrestore(&npinfo->rx_lock, flags);
  438. }
  439. int __netpoll_rx(struct sk_buff *skb)
  440. {
  441. int proto, len, ulen;
  442. int hits = 0;
  443. const struct iphdr *iph;
  444. struct udphdr *uh;
  445. struct netpoll_info *npinfo = skb->dev->npinfo;
  446. struct netpoll *np, *tmp;
  447. if (list_empty(&npinfo->rx_np))
  448. goto out;
  449. if (skb->dev->type != ARPHRD_ETHER)
  450. goto out;
  451. /* check if netpoll clients need ARP */
  452. if (skb->protocol == htons(ETH_P_ARP) &&
  453. atomic_read(&trapped)) {
  454. skb_queue_tail(&npinfo->arp_tx, skb);
  455. return 1;
  456. }
  457. proto = ntohs(eth_hdr(skb)->h_proto);
  458. if (proto != ETH_P_IP)
  459. goto out;
  460. if (skb->pkt_type == PACKET_OTHERHOST)
  461. goto out;
  462. if (skb_shared(skb))
  463. goto out;
  464. iph = (struct iphdr *)skb->data;
  465. if (!pskb_may_pull(skb, sizeof(struct iphdr)))
  466. goto out;
  467. if (iph->ihl < 5 || iph->version != 4)
  468. goto out;
  469. if (!pskb_may_pull(skb, iph->ihl*4))
  470. goto out;
  471. if (ip_fast_csum((u8 *)iph, iph->ihl) != 0)
  472. goto out;
  473. len = ntohs(iph->tot_len);
  474. if (skb->len < len || len < iph->ihl*4)
  475. goto out;
  476. /*
  477. * Our transport medium may have padded the buffer out.
  478. * Now We trim to the true length of the frame.
  479. */
  480. if (pskb_trim_rcsum(skb, len))
  481. goto out;
  482. if (iph->protocol != IPPROTO_UDP)
  483. goto out;
  484. len -= iph->ihl*4;
  485. uh = (struct udphdr *)(((char *)iph) + iph->ihl*4);
  486. ulen = ntohs(uh->len);
  487. if (ulen != len)
  488. goto out;
  489. if (checksum_udp(skb, uh, ulen, iph->saddr, iph->daddr))
  490. goto out;
  491. list_for_each_entry_safe(np, tmp, &npinfo->rx_np, rx) {
  492. if (np->local_ip && np->local_ip != iph->daddr)
  493. continue;
  494. if (np->remote_ip && np->remote_ip != iph->saddr)
  495. continue;
  496. if (np->local_port && np->local_port != ntohs(uh->dest))
  497. continue;
  498. np->rx_hook(np, ntohs(uh->source),
  499. (char *)(uh+1),
  500. ulen - sizeof(struct udphdr));
  501. hits++;
  502. }
  503. if (!hits)
  504. goto out;
  505. kfree_skb(skb);
  506. return 1;
  507. out:
  508. if (atomic_read(&trapped)) {
  509. kfree_skb(skb);
  510. return 1;
  511. }
  512. return 0;
  513. }
  514. void netpoll_print_options(struct netpoll *np)
  515. {
  516. printk(KERN_INFO "%s: local port %d\n",
  517. np->name, np->local_port);
  518. printk(KERN_INFO "%s: local IP %pI4\n",
  519. np->name, &np->local_ip);
  520. printk(KERN_INFO "%s: interface '%s'\n",
  521. np->name, np->dev_name);
  522. printk(KERN_INFO "%s: remote port %d\n",
  523. np->name, np->remote_port);
  524. printk(KERN_INFO "%s: remote IP %pI4\n",
  525. np->name, &np->remote_ip);
  526. printk(KERN_INFO "%s: remote ethernet address %pM\n",
  527. np->name, 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. printk(KERN_INFO "%s: warning: whitespace"
  565. "is not allowed\n", np->name);
  566. np->remote_port = simple_strtol(cur, NULL, 10);
  567. cur = delim;
  568. }
  569. cur++;
  570. /* dst ip */
  571. if ((delim = strchr(cur, '/')) == NULL)
  572. goto parse_failed;
  573. *delim = 0;
  574. np->remote_ip = in_aton(cur);
  575. cur = delim + 1;
  576. if (*cur != 0) {
  577. /* MAC address */
  578. if (!mac_pton(cur, np->remote_mac))
  579. goto parse_failed;
  580. }
  581. netpoll_print_options(np);
  582. return 0;
  583. parse_failed:
  584. printk(KERN_INFO "%s: couldn't parse config at '%s'!\n",
  585. np->name, cur);
  586. return -1;
  587. }
  588. EXPORT_SYMBOL(netpoll_parse_options);
  589. int __netpoll_setup(struct netpoll *np)
  590. {
  591. struct net_device *ndev = np->dev;
  592. struct netpoll_info *npinfo;
  593. const struct net_device_ops *ops;
  594. unsigned long flags;
  595. int err;
  596. if ((ndev->priv_flags & IFF_DISABLE_NETPOLL) ||
  597. !ndev->netdev_ops->ndo_poll_controller) {
  598. printk(KERN_ERR "%s: %s doesn't support polling, aborting.\n",
  599. np->name, np->dev_name);
  600. err = -ENOTSUPP;
  601. goto out;
  602. }
  603. if (!ndev->npinfo) {
  604. npinfo = kmalloc(sizeof(*npinfo), GFP_KERNEL);
  605. if (!npinfo) {
  606. err = -ENOMEM;
  607. goto out;
  608. }
  609. npinfo->rx_flags = 0;
  610. INIT_LIST_HEAD(&npinfo->rx_np);
  611. spin_lock_init(&npinfo->rx_lock);
  612. skb_queue_head_init(&npinfo->arp_tx);
  613. skb_queue_head_init(&npinfo->txq);
  614. INIT_DELAYED_WORK(&npinfo->tx_work, queue_process);
  615. atomic_set(&npinfo->refcnt, 1);
  616. ops = np->dev->netdev_ops;
  617. if (ops->ndo_netpoll_setup) {
  618. err = ops->ndo_netpoll_setup(ndev, npinfo);
  619. if (err)
  620. goto free_npinfo;
  621. }
  622. } else {
  623. npinfo = ndev->npinfo;
  624. atomic_inc(&npinfo->refcnt);
  625. }
  626. npinfo->netpoll = np;
  627. if (np->rx_hook) {
  628. spin_lock_irqsave(&npinfo->rx_lock, flags);
  629. npinfo->rx_flags |= NETPOLL_RX_ENABLED;
  630. list_add_tail(&np->rx, &npinfo->rx_np);
  631. spin_unlock_irqrestore(&npinfo->rx_lock, flags);
  632. }
  633. /* last thing to do is link it to the net device structure */
  634. rcu_assign_pointer(ndev->npinfo, npinfo);
  635. return 0;
  636. free_npinfo:
  637. kfree(npinfo);
  638. out:
  639. return err;
  640. }
  641. EXPORT_SYMBOL_GPL(__netpoll_setup);
  642. int netpoll_setup(struct netpoll *np)
  643. {
  644. struct net_device *ndev = NULL;
  645. struct in_device *in_dev;
  646. int err;
  647. if (np->dev_name)
  648. ndev = dev_get_by_name(&init_net, np->dev_name);
  649. if (!ndev) {
  650. printk(KERN_ERR "%s: %s doesn't exist, aborting.\n",
  651. np->name, np->dev_name);
  652. return -ENODEV;
  653. }
  654. if (ndev->master) {
  655. printk(KERN_ERR "%s: %s is a slave device, aborting.\n",
  656. np->name, np->dev_name);
  657. err = -EBUSY;
  658. goto put;
  659. }
  660. if (!netif_running(ndev)) {
  661. unsigned long atmost, atleast;
  662. printk(KERN_INFO "%s: device %s not up yet, forcing it\n",
  663. np->name, np->dev_name);
  664. rtnl_lock();
  665. err = dev_open(ndev);
  666. rtnl_unlock();
  667. if (err) {
  668. printk(KERN_ERR "%s: failed to open %s\n",
  669. np->name, ndev->name);
  670. goto put;
  671. }
  672. atleast = jiffies + HZ/10;
  673. atmost = jiffies + carrier_timeout * HZ;
  674. while (!netif_carrier_ok(ndev)) {
  675. if (time_after(jiffies, atmost)) {
  676. printk(KERN_NOTICE
  677. "%s: timeout waiting for carrier\n",
  678. np->name);
  679. break;
  680. }
  681. msleep(1);
  682. }
  683. /* If carrier appears to come up instantly, we don't
  684. * trust it and pause so that we don't pump all our
  685. * queued console messages into the bitbucket.
  686. */
  687. if (time_before(jiffies, atleast)) {
  688. printk(KERN_NOTICE "%s: carrier detect appears"
  689. " untrustworthy, waiting 4 seconds\n",
  690. np->name);
  691. msleep(4000);
  692. }
  693. }
  694. if (!np->local_ip) {
  695. rcu_read_lock();
  696. in_dev = __in_dev_get_rcu(ndev);
  697. if (!in_dev || !in_dev->ifa_list) {
  698. rcu_read_unlock();
  699. printk(KERN_ERR "%s: no IP address for %s, aborting\n",
  700. np->name, np->dev_name);
  701. err = -EDESTADDRREQ;
  702. goto put;
  703. }
  704. np->local_ip = in_dev->ifa_list->ifa_local;
  705. rcu_read_unlock();
  706. printk(KERN_INFO "%s: local IP %pI4\n", np->name, &np->local_ip);
  707. }
  708. np->dev = ndev;
  709. /* fill up the skb queue */
  710. refill_skbs();
  711. rtnl_lock();
  712. err = __netpoll_setup(np);
  713. rtnl_unlock();
  714. if (err)
  715. goto put;
  716. return 0;
  717. put:
  718. dev_put(ndev);
  719. return err;
  720. }
  721. EXPORT_SYMBOL(netpoll_setup);
  722. static int __init netpoll_init(void)
  723. {
  724. skb_queue_head_init(&skb_pool);
  725. return 0;
  726. }
  727. core_initcall(netpoll_init);
  728. void __netpoll_cleanup(struct netpoll *np)
  729. {
  730. struct netpoll_info *npinfo;
  731. unsigned long flags;
  732. npinfo = np->dev->npinfo;
  733. if (!npinfo)
  734. return;
  735. if (!list_empty(&npinfo->rx_np)) {
  736. spin_lock_irqsave(&npinfo->rx_lock, flags);
  737. list_del(&np->rx);
  738. if (list_empty(&npinfo->rx_np))
  739. npinfo->rx_flags &= ~NETPOLL_RX_ENABLED;
  740. spin_unlock_irqrestore(&npinfo->rx_lock, flags);
  741. }
  742. if (atomic_dec_and_test(&npinfo->refcnt)) {
  743. const struct net_device_ops *ops;
  744. ops = np->dev->netdev_ops;
  745. if (ops->ndo_netpoll_cleanup)
  746. ops->ndo_netpoll_cleanup(np->dev);
  747. rcu_assign_pointer(np->dev->npinfo, NULL);
  748. /* avoid racing with NAPI reading npinfo */
  749. synchronize_rcu_bh();
  750. skb_queue_purge(&npinfo->arp_tx);
  751. skb_queue_purge(&npinfo->txq);
  752. cancel_delayed_work_sync(&npinfo->tx_work);
  753. /* clean after last, unfinished work */
  754. __skb_queue_purge(&npinfo->txq);
  755. kfree(npinfo);
  756. }
  757. }
  758. EXPORT_SYMBOL_GPL(__netpoll_cleanup);
  759. void netpoll_cleanup(struct netpoll *np)
  760. {
  761. if (!np->dev)
  762. return;
  763. rtnl_lock();
  764. __netpoll_cleanup(np);
  765. rtnl_unlock();
  766. dev_put(np->dev);
  767. np->dev = NULL;
  768. }
  769. EXPORT_SYMBOL(netpoll_cleanup);
  770. int netpoll_trap(void)
  771. {
  772. return atomic_read(&trapped);
  773. }
  774. EXPORT_SYMBOL(netpoll_trap);
  775. void netpoll_set_trap(int trap)
  776. {
  777. if (trap)
  778. atomic_inc(&trapped);
  779. else
  780. atomic_dec(&trapped);
  781. }
  782. EXPORT_SYMBOL(netpoll_set_trap);