netpoll.c 19 KB

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