netpoll.c 19 KB

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