netpoll.c 18 KB

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