netpoll.c 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862
  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/events/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);
  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. if (status == NETDEV_TX_OK)
  250. txq_trans_update(txq);
  251. }
  252. __netif_tx_unlock(txq);
  253. if (status == NETDEV_TX_OK)
  254. break;
  255. }
  256. /* tickle device maybe there is some cleanup */
  257. netpoll_poll(np);
  258. udelay(USEC_PER_POLL);
  259. }
  260. local_irq_restore(flags);
  261. }
  262. if (status != NETDEV_TX_OK) {
  263. skb_queue_tail(&npinfo->txq, skb);
  264. schedule_delayed_work(&npinfo->tx_work,0);
  265. }
  266. }
  267. void netpoll_send_udp(struct netpoll *np, const char *msg, int len)
  268. {
  269. int total_len, eth_len, ip_len, udp_len;
  270. struct sk_buff *skb;
  271. struct udphdr *udph;
  272. struct iphdr *iph;
  273. struct ethhdr *eth;
  274. udp_len = len + sizeof(*udph);
  275. ip_len = eth_len = udp_len + sizeof(*iph);
  276. total_len = eth_len + ETH_HLEN + NET_IP_ALIGN;
  277. skb = find_skb(np, total_len, total_len - len);
  278. if (!skb)
  279. return;
  280. skb_copy_to_linear_data(skb, msg, len);
  281. skb->len += len;
  282. skb_push(skb, sizeof(*udph));
  283. skb_reset_transport_header(skb);
  284. udph = udp_hdr(skb);
  285. udph->source = htons(np->local_port);
  286. udph->dest = htons(np->remote_port);
  287. udph->len = htons(udp_len);
  288. udph->check = 0;
  289. udph->check = csum_tcpudp_magic(np->local_ip,
  290. np->remote_ip,
  291. udp_len, IPPROTO_UDP,
  292. csum_partial(udph, udp_len, 0));
  293. if (udph->check == 0)
  294. udph->check = CSUM_MANGLED_0;
  295. skb_push(skb, sizeof(*iph));
  296. skb_reset_network_header(skb);
  297. iph = ip_hdr(skb);
  298. /* iph->version = 4; iph->ihl = 5; */
  299. put_unaligned(0x45, (unsigned char *)iph);
  300. iph->tos = 0;
  301. put_unaligned(htons(ip_len), &(iph->tot_len));
  302. iph->id = 0;
  303. iph->frag_off = 0;
  304. iph->ttl = 64;
  305. iph->protocol = IPPROTO_UDP;
  306. iph->check = 0;
  307. put_unaligned(np->local_ip, &(iph->saddr));
  308. put_unaligned(np->remote_ip, &(iph->daddr));
  309. iph->check = ip_fast_csum((unsigned char *)iph, iph->ihl);
  310. eth = (struct ethhdr *) skb_push(skb, ETH_HLEN);
  311. skb_reset_mac_header(skb);
  312. skb->protocol = eth->h_proto = htons(ETH_P_IP);
  313. memcpy(eth->h_source, np->dev->dev_addr, ETH_ALEN);
  314. memcpy(eth->h_dest, np->remote_mac, ETH_ALEN);
  315. skb->dev = np->dev;
  316. netpoll_send_skb(np, skb);
  317. }
  318. static void arp_reply(struct sk_buff *skb)
  319. {
  320. struct netpoll_info *npinfo = skb->dev->npinfo;
  321. struct arphdr *arp;
  322. unsigned char *arp_ptr;
  323. int size, type = ARPOP_REPLY, ptype = ETH_P_ARP;
  324. __be32 sip, tip;
  325. unsigned char *sha;
  326. struct sk_buff *send_skb;
  327. struct netpoll *np = NULL;
  328. if (npinfo->rx_np && npinfo->rx_np->dev == skb->dev)
  329. np = npinfo->rx_np;
  330. if (!np)
  331. return;
  332. /* No arp on this interface */
  333. if (skb->dev->flags & IFF_NOARP)
  334. return;
  335. if (!pskb_may_pull(skb, arp_hdr_len(skb->dev)))
  336. return;
  337. skb_reset_network_header(skb);
  338. skb_reset_transport_header(skb);
  339. arp = arp_hdr(skb);
  340. if ((arp->ar_hrd != htons(ARPHRD_ETHER) &&
  341. arp->ar_hrd != htons(ARPHRD_IEEE802)) ||
  342. arp->ar_pro != htons(ETH_P_IP) ||
  343. arp->ar_op != htons(ARPOP_REQUEST))
  344. return;
  345. arp_ptr = (unsigned char *)(arp+1);
  346. /* save the location of the src hw addr */
  347. sha = arp_ptr;
  348. arp_ptr += skb->dev->addr_len;
  349. memcpy(&sip, arp_ptr, 4);
  350. arp_ptr += 4;
  351. /* if we actually cared about dst hw addr, it would get copied here */
  352. arp_ptr += skb->dev->addr_len;
  353. memcpy(&tip, arp_ptr, 4);
  354. /* Should we ignore arp? */
  355. if (tip != np->local_ip ||
  356. ipv4_is_loopback(tip) || ipv4_is_multicast(tip))
  357. return;
  358. size = arp_hdr_len(skb->dev);
  359. send_skb = find_skb(np, size + LL_ALLOCATED_SPACE(np->dev),
  360. LL_RESERVED_SPACE(np->dev));
  361. if (!send_skb)
  362. return;
  363. skb_reset_network_header(send_skb);
  364. arp = (struct arphdr *) skb_put(send_skb, size);
  365. send_skb->dev = skb->dev;
  366. send_skb->protocol = htons(ETH_P_ARP);
  367. /* Fill the device header for the ARP frame */
  368. if (dev_hard_header(send_skb, skb->dev, ptype,
  369. sha, np->dev->dev_addr,
  370. send_skb->len) < 0) {
  371. kfree_skb(send_skb);
  372. return;
  373. }
  374. /*
  375. * Fill out the arp protocol part.
  376. *
  377. * we only support ethernet device type,
  378. * which (according to RFC 1390) should always equal 1 (Ethernet).
  379. */
  380. arp->ar_hrd = htons(np->dev->type);
  381. arp->ar_pro = htons(ETH_P_IP);
  382. arp->ar_hln = np->dev->addr_len;
  383. arp->ar_pln = 4;
  384. arp->ar_op = htons(type);
  385. arp_ptr=(unsigned char *)(arp + 1);
  386. memcpy(arp_ptr, np->dev->dev_addr, np->dev->addr_len);
  387. arp_ptr += np->dev->addr_len;
  388. memcpy(arp_ptr, &tip, 4);
  389. arp_ptr += 4;
  390. memcpy(arp_ptr, sha, np->dev->addr_len);
  391. arp_ptr += np->dev->addr_len;
  392. memcpy(arp_ptr, &sip, 4);
  393. netpoll_send_skb(np, send_skb);
  394. }
  395. int __netpoll_rx(struct sk_buff *skb)
  396. {
  397. int proto, len, ulen;
  398. struct iphdr *iph;
  399. struct udphdr *uh;
  400. struct netpoll_info *npi = skb->dev->npinfo;
  401. struct netpoll *np = npi->rx_np;
  402. if (!np)
  403. goto out;
  404. if (skb->dev->type != ARPHRD_ETHER)
  405. goto out;
  406. /* check if netpoll clients need ARP */
  407. if (skb->protocol == htons(ETH_P_ARP) &&
  408. atomic_read(&trapped)) {
  409. skb_queue_tail(&npi->arp_tx, skb);
  410. return 1;
  411. }
  412. proto = ntohs(eth_hdr(skb)->h_proto);
  413. if (proto != ETH_P_IP)
  414. goto out;
  415. if (skb->pkt_type == PACKET_OTHERHOST)
  416. goto out;
  417. if (skb_shared(skb))
  418. goto out;
  419. iph = (struct iphdr *)skb->data;
  420. if (!pskb_may_pull(skb, sizeof(struct iphdr)))
  421. goto out;
  422. if (iph->ihl < 5 || iph->version != 4)
  423. goto out;
  424. if (!pskb_may_pull(skb, iph->ihl*4))
  425. goto out;
  426. if (ip_fast_csum((u8 *)iph, iph->ihl) != 0)
  427. goto out;
  428. len = ntohs(iph->tot_len);
  429. if (skb->len < len || len < iph->ihl*4)
  430. goto out;
  431. /*
  432. * Our transport medium may have padded the buffer out.
  433. * Now We trim to the true length of the frame.
  434. */
  435. if (pskb_trim_rcsum(skb, len))
  436. goto out;
  437. if (iph->protocol != IPPROTO_UDP)
  438. goto out;
  439. len -= iph->ihl*4;
  440. uh = (struct udphdr *)(((char *)iph) + iph->ihl*4);
  441. ulen = ntohs(uh->len);
  442. if (ulen != len)
  443. goto out;
  444. if (checksum_udp(skb, uh, ulen, iph->saddr, iph->daddr))
  445. goto out;
  446. if (np->local_ip && np->local_ip != iph->daddr)
  447. goto out;
  448. if (np->remote_ip && np->remote_ip != iph->saddr)
  449. goto out;
  450. if (np->local_port && np->local_port != ntohs(uh->dest))
  451. goto out;
  452. np->rx_hook(np, ntohs(uh->source),
  453. (char *)(uh+1),
  454. ulen - sizeof(struct udphdr));
  455. kfree_skb(skb);
  456. return 1;
  457. out:
  458. if (atomic_read(&trapped)) {
  459. kfree_skb(skb);
  460. return 1;
  461. }
  462. return 0;
  463. }
  464. void netpoll_print_options(struct netpoll *np)
  465. {
  466. printk(KERN_INFO "%s: local port %d\n",
  467. np->name, np->local_port);
  468. printk(KERN_INFO "%s: local IP %pI4\n",
  469. np->name, &np->local_ip);
  470. printk(KERN_INFO "%s: interface %s\n",
  471. np->name, np->dev_name);
  472. printk(KERN_INFO "%s: remote port %d\n",
  473. np->name, np->remote_port);
  474. printk(KERN_INFO "%s: remote IP %pI4\n",
  475. np->name, &np->remote_ip);
  476. printk(KERN_INFO "%s: remote ethernet address %pM\n",
  477. np->name, np->remote_mac);
  478. }
  479. int netpoll_parse_options(struct netpoll *np, char *opt)
  480. {
  481. char *cur=opt, *delim;
  482. if (*cur != '@') {
  483. if ((delim = strchr(cur, '@')) == NULL)
  484. goto parse_failed;
  485. *delim = 0;
  486. np->local_port = simple_strtol(cur, NULL, 10);
  487. cur = delim;
  488. }
  489. cur++;
  490. if (*cur != '/') {
  491. if ((delim = strchr(cur, '/')) == NULL)
  492. goto parse_failed;
  493. *delim = 0;
  494. np->local_ip = in_aton(cur);
  495. cur = delim;
  496. }
  497. cur++;
  498. if (*cur != ',') {
  499. /* parse out dev name */
  500. if ((delim = strchr(cur, ',')) == NULL)
  501. goto parse_failed;
  502. *delim = 0;
  503. strlcpy(np->dev_name, cur, sizeof(np->dev_name));
  504. cur = delim;
  505. }
  506. cur++;
  507. if (*cur != '@') {
  508. /* dst port */
  509. if ((delim = strchr(cur, '@')) == NULL)
  510. goto parse_failed;
  511. *delim = 0;
  512. np->remote_port = simple_strtol(cur, NULL, 10);
  513. cur = delim;
  514. }
  515. cur++;
  516. /* dst ip */
  517. if ((delim = strchr(cur, '/')) == NULL)
  518. goto parse_failed;
  519. *delim = 0;
  520. np->remote_ip = in_aton(cur);
  521. cur = delim + 1;
  522. if (*cur != 0) {
  523. /* MAC address */
  524. if ((delim = strchr(cur, ':')) == NULL)
  525. goto parse_failed;
  526. *delim = 0;
  527. np->remote_mac[0] = 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[1] = 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[2] = 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[3] = simple_strtol(cur, NULL, 16);
  543. cur = delim + 1;
  544. if ((delim = strchr(cur, ':')) == NULL)
  545. goto parse_failed;
  546. *delim = 0;
  547. np->remote_mac[4] = simple_strtol(cur, NULL, 16);
  548. cur = delim + 1;
  549. np->remote_mac[5] = simple_strtol(cur, NULL, 16);
  550. }
  551. netpoll_print_options(np);
  552. return 0;
  553. parse_failed:
  554. printk(KERN_INFO "%s: couldn't parse config at %s!\n",
  555. np->name, cur);
  556. return -1;
  557. }
  558. int netpoll_setup(struct netpoll *np)
  559. {
  560. struct net_device *ndev = NULL;
  561. struct in_device *in_dev;
  562. struct netpoll_info *npinfo;
  563. unsigned long flags;
  564. int err;
  565. if (np->dev_name)
  566. ndev = dev_get_by_name(&init_net, np->dev_name);
  567. if (!ndev) {
  568. printk(KERN_ERR "%s: %s doesn't exist, aborting.\n",
  569. np->name, np->dev_name);
  570. return -ENODEV;
  571. }
  572. np->dev = ndev;
  573. if (!ndev->npinfo) {
  574. npinfo = kmalloc(sizeof(*npinfo), GFP_KERNEL);
  575. if (!npinfo) {
  576. err = -ENOMEM;
  577. goto release;
  578. }
  579. npinfo->rx_flags = 0;
  580. npinfo->rx_np = NULL;
  581. spin_lock_init(&npinfo->rx_lock);
  582. skb_queue_head_init(&npinfo->arp_tx);
  583. skb_queue_head_init(&npinfo->txq);
  584. INIT_DELAYED_WORK(&npinfo->tx_work, queue_process);
  585. atomic_set(&npinfo->refcnt, 1);
  586. } else {
  587. npinfo = ndev->npinfo;
  588. atomic_inc(&npinfo->refcnt);
  589. }
  590. if (!ndev->netdev_ops->ndo_poll_controller) {
  591. printk(KERN_ERR "%s: %s doesn't support polling, aborting.\n",
  592. np->name, np->dev_name);
  593. err = -ENOTSUPP;
  594. goto release;
  595. }
  596. if (!netif_running(ndev)) {
  597. unsigned long atmost, atleast;
  598. printk(KERN_INFO "%s: device %s not up yet, forcing it\n",
  599. np->name, np->dev_name);
  600. rtnl_lock();
  601. err = dev_open(ndev);
  602. rtnl_unlock();
  603. if (err) {
  604. printk(KERN_ERR "%s: failed to open %s\n",
  605. np->name, ndev->name);
  606. goto release;
  607. }
  608. atleast = jiffies + HZ/10;
  609. atmost = jiffies + 4*HZ;
  610. while (!netif_carrier_ok(ndev)) {
  611. if (time_after(jiffies, atmost)) {
  612. printk(KERN_NOTICE
  613. "%s: timeout waiting for carrier\n",
  614. np->name);
  615. break;
  616. }
  617. msleep(1);
  618. }
  619. /* If carrier appears to come up instantly, we don't
  620. * trust it and pause so that we don't pump all our
  621. * queued console messages into the bitbucket.
  622. */
  623. if (time_before(jiffies, atleast)) {
  624. printk(KERN_NOTICE "%s: carrier detect appears"
  625. " untrustworthy, waiting 4 seconds\n",
  626. np->name);
  627. msleep(4000);
  628. }
  629. }
  630. if (!np->local_ip) {
  631. rcu_read_lock();
  632. in_dev = __in_dev_get_rcu(ndev);
  633. if (!in_dev || !in_dev->ifa_list) {
  634. rcu_read_unlock();
  635. printk(KERN_ERR "%s: no IP address for %s, aborting\n",
  636. np->name, np->dev_name);
  637. err = -EDESTADDRREQ;
  638. goto release;
  639. }
  640. np->local_ip = in_dev->ifa_list->ifa_local;
  641. rcu_read_unlock();
  642. printk(KERN_INFO "%s: local IP %pI4\n", np->name, &np->local_ip);
  643. }
  644. if (np->rx_hook) {
  645. spin_lock_irqsave(&npinfo->rx_lock, flags);
  646. npinfo->rx_flags |= NETPOLL_RX_ENABLED;
  647. npinfo->rx_np = np;
  648. spin_unlock_irqrestore(&npinfo->rx_lock, flags);
  649. }
  650. /* fill up the skb queue */
  651. refill_skbs();
  652. /* last thing to do is link it to the net device structure */
  653. ndev->npinfo = npinfo;
  654. /* avoid racing with NAPI reading npinfo */
  655. synchronize_rcu();
  656. return 0;
  657. release:
  658. if (!ndev->npinfo)
  659. kfree(npinfo);
  660. np->dev = NULL;
  661. dev_put(ndev);
  662. return err;
  663. }
  664. static int __init netpoll_init(void)
  665. {
  666. skb_queue_head_init(&skb_pool);
  667. return 0;
  668. }
  669. core_initcall(netpoll_init);
  670. void netpoll_cleanup(struct netpoll *np)
  671. {
  672. struct netpoll_info *npinfo;
  673. unsigned long flags;
  674. if (np->dev) {
  675. npinfo = np->dev->npinfo;
  676. if (npinfo) {
  677. if (npinfo->rx_np == np) {
  678. spin_lock_irqsave(&npinfo->rx_lock, flags);
  679. npinfo->rx_np = NULL;
  680. npinfo->rx_flags &= ~NETPOLL_RX_ENABLED;
  681. spin_unlock_irqrestore(&npinfo->rx_lock, flags);
  682. }
  683. if (atomic_dec_and_test(&npinfo->refcnt)) {
  684. skb_queue_purge(&npinfo->arp_tx);
  685. skb_queue_purge(&npinfo->txq);
  686. cancel_rearming_delayed_work(&npinfo->tx_work);
  687. /* clean after last, unfinished work */
  688. __skb_queue_purge(&npinfo->txq);
  689. kfree(npinfo);
  690. np->dev->npinfo = NULL;
  691. }
  692. }
  693. dev_put(np->dev);
  694. }
  695. np->dev = NULL;
  696. }
  697. int netpoll_trap(void)
  698. {
  699. return atomic_read(&trapped);
  700. }
  701. void netpoll_set_trap(int trap)
  702. {
  703. if (trap)
  704. atomic_inc(&trapped);
  705. else
  706. atomic_dec(&trapped);
  707. }
  708. EXPORT_SYMBOL(netpoll_set_trap);
  709. EXPORT_SYMBOL(netpoll_trap);
  710. EXPORT_SYMBOL(netpoll_print_options);
  711. EXPORT_SYMBOL(netpoll_parse_options);
  712. EXPORT_SYMBOL(netpoll_setup);
  713. EXPORT_SYMBOL(netpoll_cleanup);
  714. EXPORT_SYMBOL(netpoll_send_udp);
  715. EXPORT_SYMBOL(netpoll_poll);