netpoll.c 19 KB

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