xref: /netbsd/usr.sbin/altq/altqstat/qdisc_cdnr.c (revision bf9ec67e)
1 /*	$NetBSD: qdisc_cdnr.c,v 1.3 2001/08/16 07:48:10 itojun Exp $	*/
2 /*	$KAME: qdisc_cdnr.c,v 1.4 2001/08/15 12:51:58 kjc Exp $	*/
3 /*
4  * Copyright (C) 1999-2000
5  *	Sony Computer Science Laboratories, Inc.  All rights reserved.
6  *
7  * Redistribution and use in source and binary forms, with or without
8  * modification, are permitted provided that the following conditions
9  * are met:
10  * 1. Redistributions of source code must retain the above copyright
11  *    notice, this list of conditions and the following disclaimer.
12  * 2. Redistributions in binary form must reproduce the above copyright
13  *    notice, this list of conditions and the following disclaimer in the
14  *    documentation and/or other materials provided with the distribution.
15  *
16  * THIS SOFTWARE IS PROVIDED BY SONY CSL AND CONTRIBUTORS ``AS IS'' AND
17  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
18  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
19  * ARE DISCLAIMED.  IN NO EVENT SHALL SONY CSL OR CONTRIBUTORS BE LIABLE
20  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
21  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
22  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
23  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
24  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
25  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
26  * SUCH DAMAGE.
27  */
28 
29 #include <sys/param.h>
30 #include <sys/ioctl.h>
31 #include <sys/time.h>
32 #include <sys/socket.h>
33 #include <net/if.h>
34 #include <netinet/in.h>
35 #include <altq/altq.h>
36 #include <altq/altq_cdnr.h>
37 
38 #include <stdio.h>
39 #include <stdlib.h>
40 #include <unistd.h>
41 #include <string.h>
42 #include <math.h>
43 #include <errno.h>
44 #include <err.h>
45 
46 #include "quip_client.h"
47 #include "altqstat.h"
48 
49 #define NELEMENTS	64
50 #define MAX_PROB	(128*1024)
51 
52 static char *element_names[] = { "none", "top", "element", "tbmeter", "trtcm",
53 				 "tswtcm" };
54 static char *tbmprof_names[] = { "in:    ", "out:   " };
55 static char *tcmprof_names[] = { "green: ", "yellow:", "red:   " };
56 
57 void
58 cdnr_stat_loop(int fd, const char *ifname, int count, int interval)
59 {
60 	struct tce_stats	stats1[NELEMENTS], stats2[NELEMENTS];
61 	char			cdnrnames[NELEMENTS][128];
62 	struct cdnr_get_stats	get_stats;
63 	struct tce_stats	*sp, *lp, *new, *last, *tmp;
64 	struct timeval		cur_time, last_time;
65 	double			sec;
66 	char			**profile_names, _ifname[32];
67 	int			i, j, nprofile;
68 	int cnt = count;
69 
70 	if (ifname[0] == '_')
71 		ifname++;
72 	snprintf(_ifname, sizeof(_ifname), "_%s", ifname);
73 
74 	strlcpy(get_stats.iface.cdnr_ifname, ifname,
75 		sizeof(get_stats.iface.cdnr_ifname));
76 	new = &stats1[0];
77 	last = &stats2[0];
78 
79 	for (i = 0; i < NELEMENTS; i++)
80 		stats1[i].tce_handle = stats2[i].tce_handle = CDNR_NULL_HANDLE;
81 
82 	while (count == 0 || cnt-- > 0) {
83 		get_stats.nskip = 0;
84 		get_stats.nelements = NELEMENTS;
85 		get_stats.tce_stats = new;
86 
87 		if (ioctl(fd, CDNR_GETSTATS, &get_stats) < 0)
88 			err(1, "ioctl CDNR_GETSTATS");
89 
90 		gettimeofday(&cur_time, NULL);
91 		sec = calc_interval(&cur_time, &last_time);
92 
93 		printf("actions:\n");
94 		printf("  pass:%llu drop:%llu mark:%llu next:%llu return:%llu none:%llu\n",
95 		       (ull)get_stats.cnts[TCACODE_PASS].packets,
96 		       (ull)get_stats.cnts[TCACODE_DROP].packets,
97 		       (ull)get_stats.cnts[TCACODE_MARK].packets,
98 		       (ull)get_stats.cnts[TCACODE_NEXT].packets,
99 		       (ull)get_stats.cnts[TCACODE_RETURN].packets,
100 		       (ull)get_stats.cnts[TCACODE_NONE].packets);
101 
102 		for (i = 0; i < get_stats.nelements; i++) {
103 			sp = &new[i];
104 			lp = &last[i];
105 
106 			if (sp->tce_handle != lp->tce_handle) {
107 				quip_chandle2name(_ifname, sp->tce_handle,
108 						  cdnrnames[i], sizeof(cdnrnames[0]));
109 				continue;
110 			}
111 
112 			switch (sp->tce_type) {
113 			case TCETYPE_TBMETER:
114 				nprofile = 2;
115 				profile_names = tbmprof_names;
116 				break;
117 			case TCETYPE_TRTCM:
118 			case TCETYPE_TSWTCM:
119 				nprofile = 3;
120 				profile_names = tcmprof_names;
121 				break;
122 			default:
123 				profile_names = tbmprof_names; /* silence cc */
124 				nprofile = 0;
125 			}
126 
127 			if (nprofile == 0)
128 				continue;
129 
130 			printf("[%s: %s] handle:%#lx\n",
131 			       element_names[sp->tce_type], cdnrnames[i],
132 			       sp->tce_handle);
133 			for (j = 0; j < nprofile; j++) {
134 				printf("  %s %10llu pkts %16llu bytes (%sbps)\n",
135 				       profile_names[j],
136 				       (ull)sp->tce_cnts[j].packets,
137 				       (ull)sp->tce_cnts[j].bytes,
138 				       rate2str(
139 					       calc_rate(sp->tce_cnts[j].bytes,
140 							 lp->tce_cnts[j].bytes,
141 							 sec)));
142 			}
143 		}
144 		printf("\n");
145 
146 		/* swap the buffer pointers */
147 		tmp = last;
148 		last = new;
149 		new = tmp;
150 
151 		last_time = cur_time;
152 		sleep(interval);
153 	}
154 }
155