netpoll.c 21 KB

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