netpoll.c 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806
  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/smp_lock.h>
  12. #include <linux/netdevice.h>
  13. #include <linux/etherdevice.h>
  14. #include <linux/string.h>
  15. #include <linux/if_arp.h>
  16. #include <linux/inetdevice.h>
  17. #include <linux/inet.h>
  18. #include <linux/interrupt.h>
  19. #include <linux/netpoll.h>
  20. #include <linux/sched.h>
  21. #include <linux/delay.h>
  22. #include <linux/rcupdate.h>
  23. #include <linux/workqueue.h>
  24. #include <net/tcp.h>
  25. #include <net/udp.h>
  26. #include <asm/unaligned.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. #define MAX_RETRIES 20000
  35. static DEFINE_SPINLOCK(skb_list_lock);
  36. static int nr_skbs;
  37. static struct sk_buff *skbs;
  38. static DEFINE_SPINLOCK(queue_lock);
  39. static int queue_depth;
  40. static struct sk_buff *queue_head, *queue_tail;
  41. static atomic_t trapped;
  42. #define NETPOLL_RX_ENABLED 1
  43. #define NETPOLL_RX_DROP 2
  44. #define MAX_SKB_SIZE \
  45. (MAX_UDP_CHUNK + sizeof(struct udphdr) + \
  46. sizeof(struct iphdr) + sizeof(struct ethhdr))
  47. static void zap_completion_queue(void);
  48. static void arp_reply(struct sk_buff *skb);
  49. static void queue_process(void *p)
  50. {
  51. unsigned long flags;
  52. struct sk_buff *skb;
  53. while (queue_head) {
  54. spin_lock_irqsave(&queue_lock, flags);
  55. skb = queue_head;
  56. queue_head = skb->next;
  57. if (skb == queue_tail)
  58. queue_head = NULL;
  59. queue_depth--;
  60. spin_unlock_irqrestore(&queue_lock, flags);
  61. dev_queue_xmit(skb);
  62. }
  63. }
  64. static DECLARE_WORK(send_queue, queue_process, NULL);
  65. void netpoll_queue(struct sk_buff *skb)
  66. {
  67. unsigned long flags;
  68. if (queue_depth == MAX_QUEUE_DEPTH) {
  69. __kfree_skb(skb);
  70. return;
  71. }
  72. spin_lock_irqsave(&queue_lock, flags);
  73. if (!queue_head)
  74. queue_head = skb;
  75. else
  76. queue_tail->next = skb;
  77. queue_tail = skb;
  78. queue_depth++;
  79. spin_unlock_irqrestore(&queue_lock, flags);
  80. schedule_work(&send_queue);
  81. }
  82. static int checksum_udp(struct sk_buff *skb, struct udphdr *uh,
  83. unsigned short ulen, u32 saddr, u32 daddr)
  84. {
  85. unsigned int psum;
  86. if (uh->check == 0 || skb->ip_summed == CHECKSUM_UNNECESSARY)
  87. return 0;
  88. psum = csum_tcpudp_nofold(saddr, daddr, ulen, IPPROTO_UDP, 0);
  89. if (skb->ip_summed == CHECKSUM_COMPLETE &&
  90. !(u16)csum_fold(csum_add(psum, skb->csum)))
  91. return 0;
  92. skb->csum = psum;
  93. return __skb_checksum_complete(skb);
  94. }
  95. /*
  96. * Check whether delayed processing was scheduled for our NIC. If so,
  97. * we attempt to grab the poll lock and use ->poll() to pump the card.
  98. * If this fails, either we've recursed in ->poll() or it's already
  99. * running on another CPU.
  100. *
  101. * Note: we don't mask interrupts with this lock because we're using
  102. * trylock here and interrupts are already disabled in the softirq
  103. * case. Further, we test the poll_owner to avoid recursion on UP
  104. * systems where the lock doesn't exist.
  105. *
  106. * In cases where there is bi-directional communications, reading only
  107. * one message at a time can lead to packets being dropped by the
  108. * network adapter, forcing superfluous retries and possibly timeouts.
  109. * Thus, we set our budget to greater than 1.
  110. */
  111. static void poll_napi(struct netpoll *np)
  112. {
  113. struct netpoll_info *npinfo = np->dev->npinfo;
  114. int budget = 16;
  115. if (test_bit(__LINK_STATE_RX_SCHED, &np->dev->state) &&
  116. npinfo->poll_owner != smp_processor_id() &&
  117. spin_trylock(&npinfo->poll_lock)) {
  118. npinfo->rx_flags |= NETPOLL_RX_DROP;
  119. atomic_inc(&trapped);
  120. np->dev->poll(np->dev, &budget);
  121. atomic_dec(&trapped);
  122. npinfo->rx_flags &= ~NETPOLL_RX_DROP;
  123. spin_unlock(&npinfo->poll_lock);
  124. }
  125. }
  126. static void service_arp_queue(struct netpoll_info *npi)
  127. {
  128. struct sk_buff *skb;
  129. if (unlikely(!npi))
  130. return;
  131. skb = skb_dequeue(&npi->arp_tx);
  132. while (skb != NULL) {
  133. arp_reply(skb);
  134. skb = skb_dequeue(&npi->arp_tx);
  135. }
  136. return;
  137. }
  138. void netpoll_poll(struct netpoll *np)
  139. {
  140. if(!np->dev || !netif_running(np->dev) || !np->dev->poll_controller)
  141. return;
  142. /* Process pending work on NIC */
  143. np->dev->poll_controller(np->dev);
  144. if (np->dev->poll)
  145. poll_napi(np);
  146. service_arp_queue(np->dev->npinfo);
  147. zap_completion_queue();
  148. }
  149. static void refill_skbs(void)
  150. {
  151. struct sk_buff *skb;
  152. unsigned long flags;
  153. spin_lock_irqsave(&skb_list_lock, flags);
  154. while (nr_skbs < MAX_SKBS) {
  155. skb = alloc_skb(MAX_SKB_SIZE, GFP_ATOMIC);
  156. if (!skb)
  157. break;
  158. skb->next = skbs;
  159. skbs = skb;
  160. nr_skbs++;
  161. }
  162. spin_unlock_irqrestore(&skb_list_lock, flags);
  163. }
  164. static void zap_completion_queue(void)
  165. {
  166. unsigned long flags;
  167. struct softnet_data *sd = &get_cpu_var(softnet_data);
  168. if (sd->completion_queue) {
  169. struct sk_buff *clist;
  170. local_irq_save(flags);
  171. clist = sd->completion_queue;
  172. sd->completion_queue = NULL;
  173. local_irq_restore(flags);
  174. while (clist != NULL) {
  175. struct sk_buff *skb = clist;
  176. clist = clist->next;
  177. if(skb->destructor)
  178. dev_kfree_skb_any(skb); /* put this one back */
  179. else
  180. __kfree_skb(skb);
  181. }
  182. }
  183. put_cpu_var(softnet_data);
  184. }
  185. static struct sk_buff * find_skb(struct netpoll *np, int len, int reserve)
  186. {
  187. int once = 1, count = 0;
  188. unsigned long flags;
  189. struct sk_buff *skb = NULL;
  190. zap_completion_queue();
  191. repeat:
  192. if (nr_skbs < MAX_SKBS)
  193. refill_skbs();
  194. skb = alloc_skb(len, GFP_ATOMIC);
  195. if (!skb) {
  196. spin_lock_irqsave(&skb_list_lock, flags);
  197. skb = skbs;
  198. if (skb) {
  199. skbs = skb->next;
  200. skb->next = NULL;
  201. nr_skbs--;
  202. }
  203. spin_unlock_irqrestore(&skb_list_lock, flags);
  204. }
  205. if(!skb) {
  206. count++;
  207. if (once && (count == 1000000)) {
  208. printk("out of netpoll skbs!\n");
  209. once = 0;
  210. }
  211. netpoll_poll(np);
  212. goto repeat;
  213. }
  214. atomic_set(&skb->users, 1);
  215. skb_reserve(skb, reserve);
  216. return skb;
  217. }
  218. static void netpoll_send_skb(struct netpoll *np, struct sk_buff *skb)
  219. {
  220. int status;
  221. struct netpoll_info *npinfo;
  222. if (!np || !np->dev || !netif_running(np->dev)) {
  223. __kfree_skb(skb);
  224. return;
  225. }
  226. npinfo = np->dev->npinfo;
  227. /* avoid recursion */
  228. if (npinfo->poll_owner == smp_processor_id() ||
  229. np->dev->xmit_lock_owner == smp_processor_id()) {
  230. if (np->drop)
  231. np->drop(skb);
  232. else
  233. __kfree_skb(skb);
  234. return;
  235. }
  236. do {
  237. npinfo->tries--;
  238. netif_tx_lock(np->dev);
  239. /*
  240. * network drivers do not expect to be called if the queue is
  241. * stopped.
  242. */
  243. status = NETDEV_TX_BUSY;
  244. if (!netif_queue_stopped(np->dev))
  245. status = np->dev->hard_start_xmit(skb, np->dev);
  246. netif_tx_unlock(np->dev);
  247. /* success */
  248. if(!status) {
  249. npinfo->tries = MAX_RETRIES; /* reset */
  250. return;
  251. }
  252. /* transmit busy */
  253. netpoll_poll(np);
  254. udelay(50);
  255. } while (npinfo->tries > 0);
  256. }
  257. void netpoll_send_udp(struct netpoll *np, const char *msg, int len)
  258. {
  259. int total_len, eth_len, ip_len, udp_len;
  260. struct sk_buff *skb;
  261. struct udphdr *udph;
  262. struct iphdr *iph;
  263. struct ethhdr *eth;
  264. udp_len = len + sizeof(*udph);
  265. ip_len = eth_len = udp_len + sizeof(*iph);
  266. total_len = eth_len + ETH_HLEN + NET_IP_ALIGN;
  267. skb = find_skb(np, total_len, total_len - len);
  268. if (!skb)
  269. return;
  270. memcpy(skb->data, msg, len);
  271. skb->len += len;
  272. udph = (struct udphdr *) skb_push(skb, sizeof(*udph));
  273. udph->source = htons(np->local_port);
  274. udph->dest = htons(np->remote_port);
  275. udph->len = htons(udp_len);
  276. udph->check = 0;
  277. iph = (struct iphdr *)skb_push(skb, sizeof(*iph));
  278. /* iph->version = 4; iph->ihl = 5; */
  279. put_unaligned(0x45, (unsigned char *)iph);
  280. iph->tos = 0;
  281. put_unaligned(htons(ip_len), &(iph->tot_len));
  282. iph->id = 0;
  283. iph->frag_off = 0;
  284. iph->ttl = 64;
  285. iph->protocol = IPPROTO_UDP;
  286. iph->check = 0;
  287. put_unaligned(htonl(np->local_ip), &(iph->saddr));
  288. put_unaligned(htonl(np->remote_ip), &(iph->daddr));
  289. iph->check = ip_fast_csum((unsigned char *)iph, iph->ihl);
  290. eth = (struct ethhdr *) skb_push(skb, ETH_HLEN);
  291. eth->h_proto = htons(ETH_P_IP);
  292. memcpy(eth->h_source, np->local_mac, 6);
  293. memcpy(eth->h_dest, np->remote_mac, 6);
  294. skb->dev = np->dev;
  295. netpoll_send_skb(np, skb);
  296. }
  297. static void arp_reply(struct sk_buff *skb)
  298. {
  299. struct netpoll_info *npinfo = skb->dev->npinfo;
  300. struct arphdr *arp;
  301. unsigned char *arp_ptr;
  302. int size, type = ARPOP_REPLY, ptype = ETH_P_ARP;
  303. u32 sip, tip;
  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->h.raw = skb->nh.raw = skb->data;
  318. arp = skb->nh.arph;
  319. if ((arp->ar_hrd != htons(ARPHRD_ETHER) &&
  320. arp->ar_hrd != htons(ARPHRD_IEEE802)) ||
  321. arp->ar_pro != htons(ETH_P_IP) ||
  322. arp->ar_op != htons(ARPOP_REQUEST))
  323. return;
  324. arp_ptr = (unsigned char *)(arp+1) + skb->dev->addr_len;
  325. memcpy(&sip, arp_ptr, 4);
  326. arp_ptr += 4 + skb->dev->addr_len;
  327. memcpy(&tip, arp_ptr, 4);
  328. /* Should we ignore arp? */
  329. if (tip != htonl(np->local_ip) || LOOPBACK(tip) || MULTICAST(tip))
  330. return;
  331. size = sizeof(struct arphdr) + 2 * (skb->dev->addr_len + 4);
  332. send_skb = find_skb(np, size + LL_RESERVED_SPACE(np->dev),
  333. LL_RESERVED_SPACE(np->dev));
  334. if (!send_skb)
  335. return;
  336. send_skb->nh.raw = send_skb->data;
  337. arp = (struct arphdr *) skb_put(send_skb, size);
  338. send_skb->dev = skb->dev;
  339. send_skb->protocol = htons(ETH_P_ARP);
  340. /* Fill the device header for the ARP frame */
  341. if (np->dev->hard_header &&
  342. np->dev->hard_header(send_skb, skb->dev, ptype,
  343. np->remote_mac, np->local_mac,
  344. send_skb->len) < 0) {
  345. kfree_skb(send_skb);
  346. return;
  347. }
  348. /*
  349. * Fill out the arp protocol part.
  350. *
  351. * we only support ethernet device type,
  352. * which (according to RFC 1390) should always equal 1 (Ethernet).
  353. */
  354. arp->ar_hrd = htons(np->dev->type);
  355. arp->ar_pro = htons(ETH_P_IP);
  356. arp->ar_hln = np->dev->addr_len;
  357. arp->ar_pln = 4;
  358. arp->ar_op = htons(type);
  359. arp_ptr=(unsigned char *)(arp + 1);
  360. memcpy(arp_ptr, np->dev->dev_addr, np->dev->addr_len);
  361. arp_ptr += np->dev->addr_len;
  362. memcpy(arp_ptr, &tip, 4);
  363. arp_ptr += 4;
  364. memcpy(arp_ptr, np->remote_mac, np->dev->addr_len);
  365. arp_ptr += np->dev->addr_len;
  366. memcpy(arp_ptr, &sip, 4);
  367. netpoll_send_skb(np, send_skb);
  368. }
  369. int __netpoll_rx(struct sk_buff *skb)
  370. {
  371. int proto, len, ulen;
  372. struct iphdr *iph;
  373. struct udphdr *uh;
  374. struct netpoll_info *npi = skb->dev->npinfo;
  375. struct netpoll *np = npi->rx_np;
  376. if (!np)
  377. goto out;
  378. if (skb->dev->type != ARPHRD_ETHER)
  379. goto out;
  380. /* check if netpoll clients need ARP */
  381. if (skb->protocol == __constant_htons(ETH_P_ARP) &&
  382. atomic_read(&trapped)) {
  383. skb_queue_tail(&npi->arp_tx, skb);
  384. return 1;
  385. }
  386. proto = ntohs(eth_hdr(skb)->h_proto);
  387. if (proto != ETH_P_IP)
  388. goto out;
  389. if (skb->pkt_type == PACKET_OTHERHOST)
  390. goto out;
  391. if (skb_shared(skb))
  392. goto out;
  393. iph = (struct iphdr *)skb->data;
  394. if (!pskb_may_pull(skb, sizeof(struct iphdr)))
  395. goto out;
  396. if (iph->ihl < 5 || iph->version != 4)
  397. goto out;
  398. if (!pskb_may_pull(skb, iph->ihl*4))
  399. goto out;
  400. if (ip_fast_csum((u8 *)iph, iph->ihl) != 0)
  401. goto out;
  402. len = ntohs(iph->tot_len);
  403. if (skb->len < len || len < iph->ihl*4)
  404. goto out;
  405. if (iph->protocol != IPPROTO_UDP)
  406. goto out;
  407. len -= iph->ihl*4;
  408. uh = (struct udphdr *)(((char *)iph) + iph->ihl*4);
  409. ulen = ntohs(uh->len);
  410. if (ulen != len)
  411. goto out;
  412. if (checksum_udp(skb, uh, ulen, iph->saddr, iph->daddr))
  413. goto out;
  414. if (np->local_ip && np->local_ip != ntohl(iph->daddr))
  415. goto out;
  416. if (np->remote_ip && np->remote_ip != ntohl(iph->saddr))
  417. goto out;
  418. if (np->local_port && np->local_port != ntohs(uh->dest))
  419. goto out;
  420. np->rx_hook(np, ntohs(uh->source),
  421. (char *)(uh+1),
  422. ulen - sizeof(struct udphdr));
  423. kfree_skb(skb);
  424. return 1;
  425. out:
  426. if (atomic_read(&trapped)) {
  427. kfree_skb(skb);
  428. return 1;
  429. }
  430. return 0;
  431. }
  432. int netpoll_parse_options(struct netpoll *np, char *opt)
  433. {
  434. char *cur=opt, *delim;
  435. if(*cur != '@') {
  436. if ((delim = strchr(cur, '@')) == NULL)
  437. goto parse_failed;
  438. *delim=0;
  439. np->local_port=simple_strtol(cur, NULL, 10);
  440. cur=delim;
  441. }
  442. cur++;
  443. printk(KERN_INFO "%s: local port %d\n", np->name, np->local_port);
  444. if(*cur != '/') {
  445. if ((delim = strchr(cur, '/')) == NULL)
  446. goto parse_failed;
  447. *delim=0;
  448. np->local_ip=ntohl(in_aton(cur));
  449. cur=delim;
  450. printk(KERN_INFO "%s: local IP %d.%d.%d.%d\n",
  451. np->name, HIPQUAD(np->local_ip));
  452. }
  453. cur++;
  454. if ( *cur != ',') {
  455. /* parse out dev name */
  456. if ((delim = strchr(cur, ',')) == NULL)
  457. goto parse_failed;
  458. *delim=0;
  459. strlcpy(np->dev_name, cur, sizeof(np->dev_name));
  460. cur=delim;
  461. }
  462. cur++;
  463. printk(KERN_INFO "%s: interface %s\n", np->name, np->dev_name);
  464. if ( *cur != '@' ) {
  465. /* dst port */
  466. if ((delim = strchr(cur, '@')) == NULL)
  467. goto parse_failed;
  468. *delim=0;
  469. np->remote_port=simple_strtol(cur, NULL, 10);
  470. cur=delim;
  471. }
  472. cur++;
  473. printk(KERN_INFO "%s: remote port %d\n", np->name, np->remote_port);
  474. /* dst ip */
  475. if ((delim = strchr(cur, '/')) == NULL)
  476. goto parse_failed;
  477. *delim=0;
  478. np->remote_ip=ntohl(in_aton(cur));
  479. cur=delim+1;
  480. printk(KERN_INFO "%s: remote IP %d.%d.%d.%d\n",
  481. np->name, HIPQUAD(np->remote_ip));
  482. if( *cur != 0 )
  483. {
  484. /* MAC address */
  485. if ((delim = strchr(cur, ':')) == NULL)
  486. goto parse_failed;
  487. *delim=0;
  488. np->remote_mac[0]=simple_strtol(cur, NULL, 16);
  489. cur=delim+1;
  490. if ((delim = strchr(cur, ':')) == NULL)
  491. goto parse_failed;
  492. *delim=0;
  493. np->remote_mac[1]=simple_strtol(cur, NULL, 16);
  494. cur=delim+1;
  495. if ((delim = strchr(cur, ':')) == NULL)
  496. goto parse_failed;
  497. *delim=0;
  498. np->remote_mac[2]=simple_strtol(cur, NULL, 16);
  499. cur=delim+1;
  500. if ((delim = strchr(cur, ':')) == NULL)
  501. goto parse_failed;
  502. *delim=0;
  503. np->remote_mac[3]=simple_strtol(cur, NULL, 16);
  504. cur=delim+1;
  505. if ((delim = strchr(cur, ':')) == NULL)
  506. goto parse_failed;
  507. *delim=0;
  508. np->remote_mac[4]=simple_strtol(cur, NULL, 16);
  509. cur=delim+1;
  510. np->remote_mac[5]=simple_strtol(cur, NULL, 16);
  511. }
  512. printk(KERN_INFO "%s: remote ethernet address "
  513. "%02x:%02x:%02x:%02x:%02x:%02x\n",
  514. np->name,
  515. np->remote_mac[0],
  516. np->remote_mac[1],
  517. np->remote_mac[2],
  518. np->remote_mac[3],
  519. np->remote_mac[4],
  520. np->remote_mac[5]);
  521. return 0;
  522. parse_failed:
  523. printk(KERN_INFO "%s: couldn't parse config at %s!\n",
  524. np->name, cur);
  525. return -1;
  526. }
  527. int netpoll_setup(struct netpoll *np)
  528. {
  529. struct net_device *ndev = NULL;
  530. struct in_device *in_dev;
  531. struct netpoll_info *npinfo;
  532. unsigned long flags;
  533. if (np->dev_name)
  534. ndev = dev_get_by_name(np->dev_name);
  535. if (!ndev) {
  536. printk(KERN_ERR "%s: %s doesn't exist, aborting.\n",
  537. np->name, np->dev_name);
  538. return -1;
  539. }
  540. np->dev = ndev;
  541. if (!ndev->npinfo) {
  542. npinfo = kmalloc(sizeof(*npinfo), GFP_KERNEL);
  543. if (!npinfo)
  544. goto release;
  545. npinfo->rx_flags = 0;
  546. npinfo->rx_np = NULL;
  547. spin_lock_init(&npinfo->poll_lock);
  548. npinfo->poll_owner = -1;
  549. npinfo->tries = MAX_RETRIES;
  550. spin_lock_init(&npinfo->rx_lock);
  551. skb_queue_head_init(&npinfo->arp_tx);
  552. } else
  553. npinfo = ndev->npinfo;
  554. if (!ndev->poll_controller) {
  555. printk(KERN_ERR "%s: %s doesn't support polling, aborting.\n",
  556. np->name, np->dev_name);
  557. goto release;
  558. }
  559. if (!netif_running(ndev)) {
  560. unsigned long atmost, atleast;
  561. printk(KERN_INFO "%s: device %s not up yet, forcing it\n",
  562. np->name, np->dev_name);
  563. rtnl_lock();
  564. if (dev_change_flags(ndev, ndev->flags | IFF_UP) < 0) {
  565. printk(KERN_ERR "%s: failed to open %s\n",
  566. np->name, np->dev_name);
  567. rtnl_unlock();
  568. goto release;
  569. }
  570. rtnl_unlock();
  571. atleast = jiffies + HZ/10;
  572. atmost = jiffies + 4*HZ;
  573. while (!netif_carrier_ok(ndev)) {
  574. if (time_after(jiffies, atmost)) {
  575. printk(KERN_NOTICE
  576. "%s: timeout waiting for carrier\n",
  577. np->name);
  578. break;
  579. }
  580. cond_resched();
  581. }
  582. /* If carrier appears to come up instantly, we don't
  583. * trust it and pause so that we don't pump all our
  584. * queued console messages into the bitbucket.
  585. */
  586. if (time_before(jiffies, atleast)) {
  587. printk(KERN_NOTICE "%s: carrier detect appears"
  588. " untrustworthy, waiting 4 seconds\n",
  589. np->name);
  590. msleep(4000);
  591. }
  592. }
  593. if (is_zero_ether_addr(np->local_mac) && ndev->dev_addr)
  594. memcpy(np->local_mac, ndev->dev_addr, 6);
  595. if (!np->local_ip) {
  596. rcu_read_lock();
  597. in_dev = __in_dev_get_rcu(ndev);
  598. if (!in_dev || !in_dev->ifa_list) {
  599. rcu_read_unlock();
  600. printk(KERN_ERR "%s: no IP address for %s, aborting\n",
  601. np->name, np->dev_name);
  602. goto release;
  603. }
  604. np->local_ip = ntohl(in_dev->ifa_list->ifa_local);
  605. rcu_read_unlock();
  606. printk(KERN_INFO "%s: local IP %d.%d.%d.%d\n",
  607. np->name, HIPQUAD(np->local_ip));
  608. }
  609. if (np->rx_hook) {
  610. spin_lock_irqsave(&npinfo->rx_lock, flags);
  611. npinfo->rx_flags |= NETPOLL_RX_ENABLED;
  612. npinfo->rx_np = np;
  613. spin_unlock_irqrestore(&npinfo->rx_lock, flags);
  614. }
  615. /* fill up the skb queue */
  616. refill_skbs();
  617. /* last thing to do is link it to the net device structure */
  618. ndev->npinfo = npinfo;
  619. /* avoid racing with NAPI reading npinfo */
  620. synchronize_rcu();
  621. return 0;
  622. release:
  623. if (!ndev->npinfo)
  624. kfree(npinfo);
  625. np->dev = NULL;
  626. dev_put(ndev);
  627. return -1;
  628. }
  629. void netpoll_cleanup(struct netpoll *np)
  630. {
  631. struct netpoll_info *npinfo;
  632. unsigned long flags;
  633. if (np->dev) {
  634. npinfo = np->dev->npinfo;
  635. if (npinfo && npinfo->rx_np == np) {
  636. spin_lock_irqsave(&npinfo->rx_lock, flags);
  637. npinfo->rx_np = NULL;
  638. npinfo->rx_flags &= ~NETPOLL_RX_ENABLED;
  639. spin_unlock_irqrestore(&npinfo->rx_lock, flags);
  640. }
  641. dev_put(np->dev);
  642. }
  643. np->dev = NULL;
  644. }
  645. int netpoll_trap(void)
  646. {
  647. return atomic_read(&trapped);
  648. }
  649. void netpoll_set_trap(int trap)
  650. {
  651. if (trap)
  652. atomic_inc(&trapped);
  653. else
  654. atomic_dec(&trapped);
  655. }
  656. EXPORT_SYMBOL(netpoll_set_trap);
  657. EXPORT_SYMBOL(netpoll_trap);
  658. EXPORT_SYMBOL(netpoll_parse_options);
  659. EXPORT_SYMBOL(netpoll_setup);
  660. EXPORT_SYMBOL(netpoll_cleanup);
  661. EXPORT_SYMBOL(netpoll_send_udp);
  662. EXPORT_SYMBOL(netpoll_poll);
  663. EXPORT_SYMBOL(netpoll_queue);