Home | History | Annotate | Line # | Download | only in altqstat
qdisc_cdnr.c revision 1.1
      1 /*	$KAME: qdisc_cdnr.c,v 1.3 2000/10/18 09:15:16 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_cdnr.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 "quip_client.h"
     46 #include "altqstat.h"
     47 
     48 #define NELEMENTS	64
     49 #define MAX_PROB	(128*1024)
     50 
     51 static char *element_names[] = { "none", "top", "element", "tbmeter", "trtcm",
     52 				 "tswtcm" };
     53 static char *tbmprof_names[] = { "in:    ", "out:   " };
     54 static char *tcmprof_names[] = { "green: ", "yellow:", "red:   " };
     55 
     56 void
     57 cdnr_stat_loop(int fd, const char *ifname, int count, int interval)
     58 {
     59 	struct tce_stats	stats1[NELEMENTS], stats2[NELEMENTS];
     60 	char			cdnrnames[NELEMENTS][128];
     61 	struct cdnr_get_stats	get_stats;
     62 	struct tce_stats	*sp, *lp, *new, *last, *tmp;
     63 	struct timeval		cur_time, last_time;
     64 	double			sec;
     65 	char			**profile_names, _ifname[32];
     66 	int			i, j, nprofile;
     67 	int cnt = count;
     68 
     69 	if (ifname[0] == '_')
     70 		ifname++;
     71 	sprintf(_ifname, "_%s", ifname);
     72 
     73 	strcpy(get_stats.iface.cdnr_ifname, ifname);
     74 	new = &stats1[0];
     75 	last = &stats2[0];
     76 
     77 	for (i = 0; i < NELEMENTS; i++)
     78 		stats1[i].tce_handle = stats2[i].tce_handle = CDNR_NULL_HANDLE;
     79 
     80 	while (count == 0 || cnt-- > 0) {
     81 		get_stats.nskip = 0;
     82 		get_stats.nelements = NELEMENTS;
     83 		get_stats.tce_stats = new;
     84 
     85 		if (ioctl(fd, CDNR_GETSTATS, &get_stats) < 0)
     86 			err(1, "ioctl CDNR_GETSTATS");
     87 
     88 		gettimeofday(&cur_time, NULL);
     89 		sec = calc_interval(&cur_time, &last_time);
     90 
     91 		printf("actions:\n");
     92 		printf("  pass:%llu drop:%llu mark:%llu next:%llu return:%llu none:%llu\n",
     93 		       (ull)get_stats.cnts[TCACODE_PASS].packets,
     94 		       (ull)get_stats.cnts[TCACODE_DROP].packets,
     95 		       (ull)get_stats.cnts[TCACODE_MARK].packets,
     96 		       (ull)get_stats.cnts[TCACODE_NEXT].packets,
     97 		       (ull)get_stats.cnts[TCACODE_RETURN].packets,
     98 		       (ull)get_stats.cnts[TCACODE_NONE].packets);
     99 
    100 		for (i = 0; i < get_stats.nelements; i++) {
    101 			sp = &new[i];
    102 			lp = &last[i];
    103 
    104 			if (sp->tce_handle != lp->tce_handle) {
    105 				quip_chandle2name(_ifname, sp->tce_handle,
    106 						  cdnrnames[i]);
    107 				continue;
    108 			}
    109 
    110 			switch (sp->tce_type) {
    111 			case TCETYPE_TBMETER:
    112 				nprofile = 2;
    113 				profile_names = tbmprof_names;
    114 				break;
    115 			case TCETYPE_TRTCM:
    116 			case TCETYPE_TSWTCM:
    117 				nprofile = 3;
    118 				profile_names = tcmprof_names;
    119 				break;
    120 			default:
    121 				profile_names = tbmprof_names; /* silence cc */
    122 				nprofile = 0;
    123 			}
    124 
    125 			if (nprofile == 0)
    126 				continue;
    127 
    128 			printf("[%s: %s] handle:%#lx\n",
    129 			       element_names[sp->tce_type], cdnrnames[i],
    130 			       sp->tce_handle);
    131 			for (j = 0; j < nprofile; j++) {
    132 				printf("  %s %10llu pkts %16llu bytes (%sbps)\n",
    133 				       profile_names[j],
    134 				       (ull)sp->tce_cnts[j].packets,
    135 				       (ull)sp->tce_cnts[j].bytes,
    136 				       rate2str(
    137 					       calc_rate(sp->tce_cnts[j].bytes,
    138 							 lp->tce_cnts[j].bytes,
    139 							 sec)));
    140 			}
    141 		}
    142 		printf("\n");
    143 
    144 		/* swap the buffer pointers */
    145 		tmp = last;
    146 		last = new;
    147 		new = tmp;
    148 
    149 		last_time = cur_time;
    150 		sleep(interval);
    151 	}
    152 }
    153