Home | History | Annotate | Line # | Download | only in altqstat
qdisc_red.c revision 1.2
      1 /*	$KAME: qdisc_red.c,v 1.3 2001/08/15 12:51:59 kjc Exp $	*/
      2 /*
      3  * Copyright (C) 1999-2000
      4  *	Sony Computer Science Laboratories, Inc.  All rights reserved.
      5  *
      6  * Redistribution and use in source and binary forms, with or without
      7  * modification, are permitted provided that the following conditions
      8  * are met:
      9  * 1. Redistributions of source code must retain the above copyright
     10  *    notice, this list of conditions and the following disclaimer.
     11  * 2. Redistributions in binary form must reproduce the above copyright
     12  *    notice, this list of conditions and the following disclaimer in the
     13  *    documentation and/or other materials provided with the distribution.
     14  *
     15  * THIS SOFTWARE IS PROVIDED BY SONY CSL AND CONTRIBUTORS ``AS IS'' AND
     16  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
     17  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
     18  * ARE DISCLAIMED.  IN NO EVENT SHALL SONY CSL OR CONTRIBUTORS BE LIABLE
     19  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
     20  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
     21  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
     22  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
     23  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
     24  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
     25  * SUCH DAMAGE.
     26  */
     27 
     28 #include <sys/param.h>
     29 #include <sys/ioctl.h>
     30 #include <sys/time.h>
     31 #include <sys/socket.h>
     32 #include <net/if.h>
     33 #include <netinet/in.h>
     34 #include <altq/altq.h>
     35 #include <altq/altq_red.h>
     36 
     37 #include <stdio.h>
     38 #include <stdlib.h>
     39 #include <unistd.h>
     40 #include <string.h>
     41 #include <math.h>
     42 #include <errno.h>
     43 #include <err.h>
     44 
     45 #include "altqstat.h"
     46 
     47 static int avg_scale = 4096;	/* default fixed-point scale */
     48 
     49 void
     50 red_stat_loop(int fd, const char *ifname, int count, int interval)
     51 {
     52 	struct red_stats red_stats;
     53 	struct timeval cur_time, last_time;
     54 	u_int64_t last_bytes;
     55 	double sec;
     56 	int cnt = count;
     57 
     58 	strlcpy(red_stats.iface.red_ifname, ifname,
     59 		sizeof(red_stats.iface.red_ifname));
     60 
     61 	gettimeofday(&last_time, NULL);
     62 	last_time.tv_sec -= interval;
     63 	last_bytes = 0;
     64 
     65 	while (count == 0 || cnt-- > 0) {
     66 
     67 		if (ioctl(fd, RED_GETSTATS, &red_stats) < 0)
     68 			err(1, "ioctl RED_GETSTATS");
     69 
     70 		gettimeofday(&cur_time, NULL);
     71 		sec = calc_interval(&cur_time, &last_time);
     72 
     73 		printf(" weight:%d inv_pmax:%d qthresh:(%d,%d)\n",
     74 		       red_stats.weight,  red_stats.inv_pmax,
     75 		       red_stats.th_min,  red_stats.th_max);
     76 		printf(" q_len:%d (avg: %.2f), q_limit:%d\n",
     77 		       red_stats.q_len,
     78 		       ((double)red_stats.q_avg)/(double)avg_scale,
     79 		       red_stats.q_limit);
     80 		printf(" xmit:%llu pkts, drop:%llu pkts (forced: %u, early: %u)\n",
     81 		       (ull)red_stats.xmit_cnt.packets,
     82 		       (ull)red_stats.drop_cnt.packets,
     83 		       red_stats.drop_forced, red_stats.drop_unforced);
     84 		if (red_stats.marked_packets != 0)
     85 			printf(" marked: %u\n", red_stats.marked_packets);
     86 		printf(" throughput: %sbps\n",
     87 		       rate2str(calc_rate(red_stats.xmit_cnt.bytes,
     88 					  last_bytes, sec)));
     89 		if (red_stats.fv_alloc > 0) {
     90 			printf(" flowvalve: alloc:%u flows:%u\n",
     91 			       red_stats.fv_alloc, red_stats.fv_flows);
     92 			printf("  predrop:%u pass:%u escape:%u\n",
     93 			       red_stats.fv_predrop, red_stats.fv_pass,
     94 			       red_stats.fv_escape);
     95 		}
     96 		printf("\n");
     97 
     98 		last_bytes = red_stats.xmit_cnt.bytes;
     99 		last_time = cur_time;
    100 		sleep(interval);
    101 	}
    102 }
    103 
    104 int
    105 print_redstats(struct redstats *rp)
    106 {
    107 	printf("     RED q_avg:%.2f xmit:%llu (forced:%u early:%u marked:%u)\n",
    108 	       ((double)rp->q_avg)/(double)avg_scale,
    109 	       (ull)rp->xmit_cnt.packets,
    110 	       rp->drop_forced,
    111 	       rp->drop_unforced,
    112 	       rp->marked_packets);
    113 	return 0;
    114 }
    115