netpoll.c 21 KB

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