netpoll.c 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964
  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_dev(struct net_device *dev)
  150. {
  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. EXPORT_SYMBOL(netpoll_poll_dev);
  164. void netpoll_poll(struct netpoll *np)
  165. {
  166. netpoll_poll_dev(np->dev);
  167. }
  168. EXPORT_SYMBOL(netpoll_poll);
  169. static void refill_skbs(void)
  170. {
  171. struct sk_buff *skb;
  172. unsigned long flags;
  173. spin_lock_irqsave(&skb_pool.lock, flags);
  174. while (skb_pool.qlen < MAX_SKBS) {
  175. skb = alloc_skb(MAX_SKB_SIZE, GFP_ATOMIC);
  176. if (!skb)
  177. break;
  178. __skb_queue_tail(&skb_pool, skb);
  179. }
  180. spin_unlock_irqrestore(&skb_pool.lock, flags);
  181. }
  182. static void zap_completion_queue(void)
  183. {
  184. unsigned long flags;
  185. struct softnet_data *sd = &get_cpu_var(softnet_data);
  186. if (sd->completion_queue) {
  187. struct sk_buff *clist;
  188. local_irq_save(flags);
  189. clist = sd->completion_queue;
  190. sd->completion_queue = NULL;
  191. local_irq_restore(flags);
  192. while (clist != NULL) {
  193. struct sk_buff *skb = clist;
  194. clist = clist->next;
  195. if (skb->destructor) {
  196. atomic_inc(&skb->users);
  197. dev_kfree_skb_any(skb); /* put this one back */
  198. } else {
  199. __kfree_skb(skb);
  200. }
  201. }
  202. }
  203. put_cpu_var(softnet_data);
  204. }
  205. static struct sk_buff *find_skb(struct netpoll *np, int len, int reserve)
  206. {
  207. int count = 0;
  208. struct sk_buff *skb;
  209. zap_completion_queue();
  210. refill_skbs();
  211. repeat:
  212. skb = alloc_skb(len, GFP_ATOMIC);
  213. if (!skb)
  214. skb = skb_dequeue(&skb_pool);
  215. if (!skb) {
  216. if (++count < 10) {
  217. netpoll_poll(np);
  218. goto repeat;
  219. }
  220. return NULL;
  221. }
  222. atomic_set(&skb->users, 1);
  223. skb_reserve(skb, reserve);
  224. return skb;
  225. }
  226. static int netpoll_owner_active(struct net_device *dev)
  227. {
  228. struct napi_struct *napi;
  229. list_for_each_entry(napi, &dev->napi_list, dev_list) {
  230. if (napi->poll_owner == smp_processor_id())
  231. return 1;
  232. }
  233. return 0;
  234. }
  235. void netpoll_send_skb(struct netpoll *np, struct sk_buff *skb)
  236. {
  237. int status = NETDEV_TX_BUSY;
  238. unsigned long tries;
  239. struct net_device *dev = np->dev;
  240. const struct net_device_ops *ops = dev->netdev_ops;
  241. /* It is up to the caller to keep npinfo alive. */
  242. struct netpoll_info *npinfo = np->dev->npinfo;
  243. if (!npinfo || !netif_running(dev) || !netif_device_present(dev)) {
  244. __kfree_skb(skb);
  245. return;
  246. }
  247. /* don't get messages out of order, and no recursion */
  248. if (skb_queue_len(&npinfo->txq) == 0 && !netpoll_owner_active(dev)) {
  249. struct netdev_queue *txq;
  250. unsigned long flags;
  251. txq = netdev_get_tx_queue(dev, skb_get_queue_mapping(skb));
  252. local_irq_save(flags);
  253. /* try until next clock tick */
  254. for (tries = jiffies_to_usecs(1)/USEC_PER_POLL;
  255. tries > 0; --tries) {
  256. if (__netif_tx_trylock(txq)) {
  257. if (!netif_tx_queue_stopped(txq)) {
  258. dev->priv_flags |= IFF_IN_NETPOLL;
  259. status = ops->ndo_start_xmit(skb, dev);
  260. dev->priv_flags &= ~IFF_IN_NETPOLL;
  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(np);
  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);
  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. 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 ((delim = strchr(cur, ':')) == NULL)
  573. goto parse_failed;
  574. *delim = 0;
  575. np->remote_mac[0] = 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[1] = simple_strtol(cur, NULL, 16);
  581. cur = delim + 1;
  582. if ((delim = strchr(cur, ':')) == NULL)
  583. goto parse_failed;
  584. *delim = 0;
  585. np->remote_mac[2] = simple_strtol(cur, NULL, 16);
  586. cur = delim + 1;
  587. if ((delim = strchr(cur, ':')) == NULL)
  588. goto parse_failed;
  589. *delim = 0;
  590. np->remote_mac[3] = simple_strtol(cur, NULL, 16);
  591. cur = delim + 1;
  592. if ((delim = strchr(cur, ':')) == NULL)
  593. goto parse_failed;
  594. *delim = 0;
  595. np->remote_mac[4] = simple_strtol(cur, NULL, 16);
  596. cur = delim + 1;
  597. np->remote_mac[5] = simple_strtol(cur, NULL, 16);
  598. }
  599. netpoll_print_options(np);
  600. return 0;
  601. parse_failed:
  602. printk(KERN_INFO "%s: couldn't parse config at '%s'!\n",
  603. np->name, cur);
  604. return -1;
  605. }
  606. EXPORT_SYMBOL(netpoll_parse_options);
  607. int __netpoll_setup(struct netpoll *np)
  608. {
  609. struct net_device *ndev = np->dev;
  610. struct netpoll_info *npinfo;
  611. const struct net_device_ops *ops;
  612. unsigned long flags;
  613. int err;
  614. if ((ndev->priv_flags & IFF_DISABLE_NETPOLL) ||
  615. !ndev->netdev_ops->ndo_poll_controller) {
  616. printk(KERN_ERR "%s: %s doesn't support polling, aborting.\n",
  617. np->name, np->dev_name);
  618. err = -ENOTSUPP;
  619. goto out;
  620. }
  621. if (!ndev->npinfo) {
  622. npinfo = kmalloc(sizeof(*npinfo), GFP_KERNEL);
  623. if (!npinfo) {
  624. err = -ENOMEM;
  625. goto out;
  626. }
  627. npinfo->rx_flags = 0;
  628. INIT_LIST_HEAD(&npinfo->rx_np);
  629. spin_lock_init(&npinfo->rx_lock);
  630. skb_queue_head_init(&npinfo->arp_tx);
  631. skb_queue_head_init(&npinfo->txq);
  632. INIT_DELAYED_WORK(&npinfo->tx_work, queue_process);
  633. atomic_set(&npinfo->refcnt, 1);
  634. ops = np->dev->netdev_ops;
  635. if (ops->ndo_netpoll_setup) {
  636. err = ops->ndo_netpoll_setup(ndev, npinfo);
  637. if (err)
  638. goto free_npinfo;
  639. }
  640. } else {
  641. npinfo = ndev->npinfo;
  642. atomic_inc(&npinfo->refcnt);
  643. }
  644. npinfo->netpoll = np;
  645. if (np->rx_hook) {
  646. spin_lock_irqsave(&npinfo->rx_lock, flags);
  647. npinfo->rx_flags |= NETPOLL_RX_ENABLED;
  648. list_add_tail(&np->rx, &npinfo->rx_np);
  649. spin_unlock_irqrestore(&npinfo->rx_lock, flags);
  650. }
  651. /* last thing to do is link it to the net device structure */
  652. rcu_assign_pointer(ndev->npinfo, npinfo);
  653. return 0;
  654. free_npinfo:
  655. kfree(npinfo);
  656. out:
  657. return err;
  658. }
  659. EXPORT_SYMBOL_GPL(__netpoll_setup);
  660. int netpoll_setup(struct netpoll *np)
  661. {
  662. struct net_device *ndev = NULL;
  663. struct in_device *in_dev;
  664. int err;
  665. if (np->dev_name)
  666. ndev = dev_get_by_name(&init_net, np->dev_name);
  667. if (!ndev) {
  668. printk(KERN_ERR "%s: %s doesn't exist, aborting.\n",
  669. np->name, np->dev_name);
  670. return -ENODEV;
  671. }
  672. if (!netif_running(ndev)) {
  673. unsigned long atmost, atleast;
  674. printk(KERN_INFO "%s: device %s not up yet, forcing it\n",
  675. np->name, np->dev_name);
  676. rtnl_lock();
  677. err = dev_open(ndev);
  678. rtnl_unlock();
  679. if (err) {
  680. printk(KERN_ERR "%s: failed to open %s\n",
  681. np->name, ndev->name);
  682. goto put;
  683. }
  684. atleast = jiffies + HZ/10;
  685. atmost = jiffies + carrier_timeout * HZ;
  686. while (!netif_carrier_ok(ndev)) {
  687. if (time_after(jiffies, atmost)) {
  688. printk(KERN_NOTICE
  689. "%s: timeout waiting for carrier\n",
  690. np->name);
  691. break;
  692. }
  693. msleep(1);
  694. }
  695. /* If carrier appears to come up instantly, we don't
  696. * trust it and pause so that we don't pump all our
  697. * queued console messages into the bitbucket.
  698. */
  699. if (time_before(jiffies, atleast)) {
  700. printk(KERN_NOTICE "%s: carrier detect appears"
  701. " untrustworthy, waiting 4 seconds\n",
  702. np->name);
  703. msleep(4000);
  704. }
  705. }
  706. if (!np->local_ip) {
  707. rcu_read_lock();
  708. in_dev = __in_dev_get_rcu(ndev);
  709. if (!in_dev || !in_dev->ifa_list) {
  710. rcu_read_unlock();
  711. printk(KERN_ERR "%s: no IP address for %s, aborting\n",
  712. np->name, np->dev_name);
  713. err = -EDESTADDRREQ;
  714. goto put;
  715. }
  716. np->local_ip = in_dev->ifa_list->ifa_local;
  717. rcu_read_unlock();
  718. printk(KERN_INFO "%s: local IP %pI4\n", np->name, &np->local_ip);
  719. }
  720. np->dev = ndev;
  721. /* fill up the skb queue */
  722. refill_skbs();
  723. rtnl_lock();
  724. err = __netpoll_setup(np);
  725. rtnl_unlock();
  726. if (err)
  727. goto put;
  728. return 0;
  729. put:
  730. dev_put(ndev);
  731. return err;
  732. }
  733. EXPORT_SYMBOL(netpoll_setup);
  734. static int __init netpoll_init(void)
  735. {
  736. skb_queue_head_init(&skb_pool);
  737. return 0;
  738. }
  739. core_initcall(netpoll_init);
  740. void __netpoll_cleanup(struct netpoll *np)
  741. {
  742. struct netpoll_info *npinfo;
  743. unsigned long flags;
  744. npinfo = np->dev->npinfo;
  745. if (!npinfo)
  746. return;
  747. if (!list_empty(&npinfo->rx_np)) {
  748. spin_lock_irqsave(&npinfo->rx_lock, flags);
  749. list_del(&np->rx);
  750. if (list_empty(&npinfo->rx_np))
  751. npinfo->rx_flags &= ~NETPOLL_RX_ENABLED;
  752. spin_unlock_irqrestore(&npinfo->rx_lock, flags);
  753. }
  754. if (atomic_dec_and_test(&npinfo->refcnt)) {
  755. const struct net_device_ops *ops;
  756. ops = np->dev->netdev_ops;
  757. if (ops->ndo_netpoll_cleanup)
  758. ops->ndo_netpoll_cleanup(np->dev);
  759. rcu_assign_pointer(np->dev->npinfo, NULL);
  760. /* avoid racing with NAPI reading npinfo */
  761. synchronize_rcu_bh();
  762. skb_queue_purge(&npinfo->arp_tx);
  763. skb_queue_purge(&npinfo->txq);
  764. cancel_rearming_delayed_work(&npinfo->tx_work);
  765. /* clean after last, unfinished work */
  766. __skb_queue_purge(&npinfo->txq);
  767. kfree(npinfo);
  768. }
  769. }
  770. EXPORT_SYMBOL_GPL(__netpoll_cleanup);
  771. void netpoll_cleanup(struct netpoll *np)
  772. {
  773. if (!np->dev)
  774. return;
  775. rtnl_lock();
  776. __netpoll_cleanup(np);
  777. rtnl_unlock();
  778. dev_put(np->dev);
  779. np->dev = NULL;
  780. }
  781. EXPORT_SYMBOL(netpoll_cleanup);
  782. int netpoll_trap(void)
  783. {
  784. return atomic_read(&trapped);
  785. }
  786. EXPORT_SYMBOL(netpoll_trap);
  787. void netpoll_set_trap(int trap)
  788. {
  789. if (trap)
  790. atomic_inc(&trapped);
  791. else
  792. atomic_dec(&trapped);
  793. }
  794. EXPORT_SYMBOL(netpoll_set_trap);