netpoll.c 21 KB

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