netpoll.c 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855
  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. if (!netif_device_present(dev) || !netif_running(dev)) {
  52. __kfree_skb(skb);
  53. continue;
  54. }
  55. local_irq_save(flags);
  56. netif_tx_lock(dev);
  57. if ((netif_queue_stopped(dev) ||
  58. netif_subqueue_stopped(dev, skb)) ||
  59. dev->hard_start_xmit(skb, dev) != NETDEV_TX_OK) {
  60. skb_queue_head(&npinfo->txq, skb);
  61. netif_tx_unlock(dev);
  62. local_irq_restore(flags);
  63. schedule_delayed_work(&npinfo->tx_work, HZ/10);
  64. return;
  65. }
  66. netif_tx_unlock(dev);
  67. local_irq_restore(flags);
  68. }
  69. }
  70. static __sum16 checksum_udp(struct sk_buff *skb, struct udphdr *uh,
  71. unsigned short ulen, __be32 saddr, __be32 daddr)
  72. {
  73. __wsum psum;
  74. if (uh->check == 0 || skb_csum_unnecessary(skb))
  75. return 0;
  76. psum = csum_tcpudp_nofold(saddr, daddr, ulen, IPPROTO_UDP, 0);
  77. if (skb->ip_summed == CHECKSUM_COMPLETE &&
  78. !csum_fold(csum_add(psum, skb->csum)))
  79. return 0;
  80. skb->csum = psum;
  81. return __skb_checksum_complete(skb);
  82. }
  83. /*
  84. * Check whether delayed processing was scheduled for our NIC. If so,
  85. * we attempt to grab the poll lock and use ->poll() to pump the card.
  86. * If this fails, either we've recursed in ->poll() or it's already
  87. * running on another CPU.
  88. *
  89. * Note: we don't mask interrupts with this lock because we're using
  90. * trylock here and interrupts are already disabled in the softirq
  91. * case. Further, we test the poll_owner to avoid recursion on UP
  92. * systems where the lock doesn't exist.
  93. *
  94. * In cases where there is bi-directional communications, reading only
  95. * one message at a time can lead to packets being dropped by the
  96. * network adapter, forcing superfluous retries and possibly timeouts.
  97. * Thus, we set our budget to greater than 1.
  98. */
  99. static int poll_one_napi(struct netpoll_info *npinfo,
  100. struct napi_struct *napi, int budget)
  101. {
  102. int work;
  103. /* net_rx_action's ->poll() invocations and our's are
  104. * synchronized by this test which is only made while
  105. * holding the napi->poll_lock.
  106. */
  107. if (!test_bit(NAPI_STATE_SCHED, &napi->state))
  108. return budget;
  109. npinfo->rx_flags |= NETPOLL_RX_DROP;
  110. atomic_inc(&trapped);
  111. work = napi->poll(napi, budget);
  112. atomic_dec(&trapped);
  113. npinfo->rx_flags &= ~NETPOLL_RX_DROP;
  114. return budget - work;
  115. }
  116. static void poll_napi(struct netpoll *np)
  117. {
  118. struct netpoll_info *npinfo = np->dev->npinfo;
  119. struct napi_struct *napi;
  120. int budget = 16;
  121. list_for_each_entry(napi, &np->dev->napi_list, dev_list) {
  122. if (napi->poll_owner != smp_processor_id() &&
  123. spin_trylock(&napi->poll_lock)) {
  124. budget = poll_one_napi(npinfo, napi, budget);
  125. spin_unlock(&napi->poll_lock);
  126. if (!budget)
  127. break;
  128. }
  129. }
  130. }
  131. static void service_arp_queue(struct netpoll_info *npi)
  132. {
  133. struct sk_buff *skb;
  134. if (unlikely(!npi))
  135. return;
  136. skb = skb_dequeue(&npi->arp_tx);
  137. while (skb != NULL) {
  138. arp_reply(skb);
  139. skb = skb_dequeue(&npi->arp_tx);
  140. }
  141. }
  142. void netpoll_poll(struct netpoll *np)
  143. {
  144. if (!np->dev || !netif_running(np->dev) || !np->dev->poll_controller)
  145. return;
  146. /* Process pending work on NIC */
  147. np->dev->poll_controller(np->dev);
  148. if (!list_empty(&np->dev->napi_list))
  149. poll_napi(np);
  150. service_arp_queue(np->dev->npinfo);
  151. zap_completion_queue();
  152. }
  153. static void refill_skbs(void)
  154. {
  155. struct sk_buff *skb;
  156. unsigned long flags;
  157. spin_lock_irqsave(&skb_pool.lock, flags);
  158. while (skb_pool.qlen < MAX_SKBS) {
  159. skb = alloc_skb(MAX_SKB_SIZE, GFP_ATOMIC);
  160. if (!skb)
  161. break;
  162. __skb_queue_tail(&skb_pool, skb);
  163. }
  164. spin_unlock_irqrestore(&skb_pool.lock, flags);
  165. }
  166. static void zap_completion_queue(void)
  167. {
  168. unsigned long flags;
  169. struct softnet_data *sd = &get_cpu_var(softnet_data);
  170. if (sd->completion_queue) {
  171. struct sk_buff *clist;
  172. local_irq_save(flags);
  173. clist = sd->completion_queue;
  174. sd->completion_queue = NULL;
  175. local_irq_restore(flags);
  176. while (clist != NULL) {
  177. struct sk_buff *skb = clist;
  178. clist = clist->next;
  179. if (skb->destructor)
  180. dev_kfree_skb_any(skb); /* put this one back */
  181. else
  182. __kfree_skb(skb);
  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. unsigned long flags;
  230. local_irq_save(flags);
  231. /* try until next clock tick */
  232. for (tries = jiffies_to_usecs(1)/USEC_PER_POLL;
  233. tries > 0; --tries) {
  234. if (netif_tx_trylock(dev)) {
  235. if (!netif_queue_stopped(dev) &&
  236. !netif_subqueue_stopped(dev, skb))
  237. status = dev->hard_start_xmit(skb, dev);
  238. netif_tx_unlock(dev);
  239. if (status == NETDEV_TX_OK)
  240. break;
  241. }
  242. /* tickle device maybe there is some cleanup */
  243. netpoll_poll(np);
  244. udelay(USEC_PER_POLL);
  245. }
  246. local_irq_restore(flags);
  247. }
  248. if (status != NETDEV_TX_OK) {
  249. skb_queue_tail(&npinfo->txq, skb);
  250. schedule_delayed_work(&npinfo->tx_work,0);
  251. }
  252. }
  253. void netpoll_send_udp(struct netpoll *np, const char *msg, int len)
  254. {
  255. int total_len, eth_len, ip_len, udp_len;
  256. struct sk_buff *skb;
  257. struct udphdr *udph;
  258. struct iphdr *iph;
  259. struct ethhdr *eth;
  260. udp_len = len + sizeof(*udph);
  261. ip_len = eth_len = udp_len + sizeof(*iph);
  262. total_len = eth_len + ETH_HLEN + NET_IP_ALIGN;
  263. skb = find_skb(np, total_len, total_len - len);
  264. if (!skb)
  265. return;
  266. skb_copy_to_linear_data(skb, msg, len);
  267. skb->len += len;
  268. skb_push(skb, sizeof(*udph));
  269. skb_reset_transport_header(skb);
  270. udph = udp_hdr(skb);
  271. udph->source = htons(np->local_port);
  272. udph->dest = htons(np->remote_port);
  273. udph->len = htons(udp_len);
  274. udph->check = 0;
  275. udph->check = csum_tcpudp_magic(htonl(np->local_ip),
  276. htonl(np->remote_ip),
  277. udp_len, IPPROTO_UDP,
  278. csum_partial((unsigned char *)udph, udp_len, 0));
  279. if (udph->check == 0)
  280. udph->check = CSUM_MANGLED_0;
  281. skb_push(skb, sizeof(*iph));
  282. skb_reset_network_header(skb);
  283. iph = ip_hdr(skb);
  284. /* iph->version = 4; iph->ihl = 5; */
  285. put_unaligned(0x45, (unsigned char *)iph);
  286. iph->tos = 0;
  287. put_unaligned(htons(ip_len), &(iph->tot_len));
  288. iph->id = 0;
  289. iph->frag_off = 0;
  290. iph->ttl = 64;
  291. iph->protocol = IPPROTO_UDP;
  292. iph->check = 0;
  293. put_unaligned(htonl(np->local_ip), &(iph->saddr));
  294. put_unaligned(htonl(np->remote_ip), &(iph->daddr));
  295. iph->check = ip_fast_csum((unsigned char *)iph, iph->ihl);
  296. eth = (struct ethhdr *) skb_push(skb, ETH_HLEN);
  297. skb_reset_mac_header(skb);
  298. skb->protocol = eth->h_proto = htons(ETH_P_IP);
  299. memcpy(eth->h_source, np->local_mac, 6);
  300. memcpy(eth->h_dest, np->remote_mac, 6);
  301. skb->dev = np->dev;
  302. netpoll_send_skb(np, skb);
  303. }
  304. static void arp_reply(struct sk_buff *skb)
  305. {
  306. struct netpoll_info *npinfo = skb->dev->npinfo;
  307. struct arphdr *arp;
  308. unsigned char *arp_ptr;
  309. int size, type = ARPOP_REPLY, ptype = ETH_P_ARP;
  310. __be32 sip, tip;
  311. unsigned char *sha;
  312. struct sk_buff *send_skb;
  313. struct netpoll *np = NULL;
  314. if (npinfo->rx_np && npinfo->rx_np->dev == skb->dev)
  315. np = npinfo->rx_np;
  316. if (!np)
  317. return;
  318. /* No arp on this interface */
  319. if (skb->dev->flags & IFF_NOARP)
  320. return;
  321. if (!pskb_may_pull(skb, (sizeof(struct arphdr) +
  322. (2 * skb->dev->addr_len) +
  323. (2 * sizeof(u32)))))
  324. return;
  325. skb_reset_network_header(skb);
  326. skb_reset_transport_header(skb);
  327. arp = arp_hdr(skb);
  328. if ((arp->ar_hrd != htons(ARPHRD_ETHER) &&
  329. arp->ar_hrd != htons(ARPHRD_IEEE802)) ||
  330. arp->ar_pro != htons(ETH_P_IP) ||
  331. arp->ar_op != htons(ARPOP_REQUEST))
  332. return;
  333. arp_ptr = (unsigned char *)(arp+1);
  334. /* save the location of the src hw addr */
  335. sha = arp_ptr;
  336. arp_ptr += skb->dev->addr_len;
  337. memcpy(&sip, arp_ptr, 4);
  338. arp_ptr += 4;
  339. /* if we actually cared about dst hw addr, it would get copied here */
  340. arp_ptr += skb->dev->addr_len;
  341. memcpy(&tip, arp_ptr, 4);
  342. /* Should we ignore arp? */
  343. if (tip != htonl(np->local_ip) || LOOPBACK(tip) || MULTICAST(tip))
  344. return;
  345. size = sizeof(struct arphdr) + 2 * (skb->dev->addr_len + 4);
  346. send_skb = find_skb(np, size + LL_RESERVED_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->local_mac,
  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 (is_zero_ether_addr(np->local_mac) && ndev->dev_addr)
  619. memcpy(np->local_mac, ndev->dev_addr, 6);
  620. if (!np->local_ip) {
  621. rcu_read_lock();
  622. in_dev = __in_dev_get_rcu(ndev);
  623. if (!in_dev || !in_dev->ifa_list) {
  624. rcu_read_unlock();
  625. printk(KERN_ERR "%s: no IP address for %s, aborting\n",
  626. np->name, np->dev_name);
  627. err = -EDESTADDRREQ;
  628. goto release;
  629. }
  630. np->local_ip = ntohl(in_dev->ifa_list->ifa_local);
  631. rcu_read_unlock();
  632. printk(KERN_INFO "%s: local IP %d.%d.%d.%d\n",
  633. np->name, HIPQUAD(np->local_ip));
  634. }
  635. if (np->rx_hook) {
  636. spin_lock_irqsave(&npinfo->rx_lock, flags);
  637. npinfo->rx_flags |= NETPOLL_RX_ENABLED;
  638. npinfo->rx_np = np;
  639. spin_unlock_irqrestore(&npinfo->rx_lock, flags);
  640. }
  641. /* fill up the skb queue */
  642. refill_skbs();
  643. /* last thing to do is link it to the net device structure */
  644. ndev->npinfo = npinfo;
  645. /* avoid racing with NAPI reading npinfo */
  646. synchronize_rcu();
  647. return 0;
  648. release:
  649. if (!ndev->npinfo)
  650. kfree(npinfo);
  651. np->dev = NULL;
  652. dev_put(ndev);
  653. return err;
  654. }
  655. static int __init netpoll_init(void)
  656. {
  657. skb_queue_head_init(&skb_pool);
  658. return 0;
  659. }
  660. core_initcall(netpoll_init);
  661. void netpoll_cleanup(struct netpoll *np)
  662. {
  663. struct netpoll_info *npinfo;
  664. unsigned long flags;
  665. if (np->dev) {
  666. npinfo = np->dev->npinfo;
  667. if (npinfo) {
  668. if (npinfo->rx_np == np) {
  669. spin_lock_irqsave(&npinfo->rx_lock, flags);
  670. npinfo->rx_np = NULL;
  671. npinfo->rx_flags &= ~NETPOLL_RX_ENABLED;
  672. spin_unlock_irqrestore(&npinfo->rx_lock, flags);
  673. }
  674. if (atomic_dec_and_test(&npinfo->refcnt)) {
  675. skb_queue_purge(&npinfo->arp_tx);
  676. skb_queue_purge(&npinfo->txq);
  677. cancel_rearming_delayed_work(&npinfo->tx_work);
  678. /* clean after last, unfinished work */
  679. if (!skb_queue_empty(&npinfo->txq)) {
  680. struct sk_buff *skb;
  681. skb = __skb_dequeue(&npinfo->txq);
  682. kfree_skb(skb);
  683. }
  684. kfree(npinfo);
  685. np->dev->npinfo = NULL;
  686. }
  687. }
  688. dev_put(np->dev);
  689. }
  690. np->dev = NULL;
  691. }
  692. int netpoll_trap(void)
  693. {
  694. return atomic_read(&trapped);
  695. }
  696. void netpoll_set_trap(int trap)
  697. {
  698. if (trap)
  699. atomic_inc(&trapped);
  700. else
  701. atomic_dec(&trapped);
  702. }
  703. EXPORT_SYMBOL(netpoll_set_trap);
  704. EXPORT_SYMBOL(netpoll_trap);
  705. EXPORT_SYMBOL(netpoll_print_options);
  706. EXPORT_SYMBOL(netpoll_parse_options);
  707. EXPORT_SYMBOL(netpoll_setup);
  708. EXPORT_SYMBOL(netpoll_cleanup);
  709. EXPORT_SYMBOL(netpoll_send_udp);
  710. EXPORT_SYMBOL(netpoll_poll);