netpoll.c 21 KB

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