netpoll.c 19 KB

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