netpoll.c 21 KB

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