netpoll.c 19 KB

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