xref: /netbsd-src/usr.sbin/altq/altqstat/qdisc_cdnr.c (revision 8b0f9554ff8762542c4defc4f70e1eb76fb508fa)
1 /*	$NetBSD: qdisc_cdnr.c,v 1.5 2006/10/28 11:43:02 peter Exp $	*/
2 /*	$KAME: qdisc_cdnr.c,v 1.6 2002/11/08 06:36:18 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 <signal.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 	sigset_t		omask;
70 
71 	if (ifname[0] == '_')
72 		ifname++;
73 	snprintf(_ifname, sizeof(_ifname), "_%s", ifname);
74 
75 	strlcpy(get_stats.iface.cdnr_ifname, ifname,
76 		sizeof(get_stats.iface.cdnr_ifname));
77 	new = &stats1[0];
78 	last = &stats2[0];
79 
80 	for (i = 0; i < NELEMENTS; i++)
81 		stats1[i].tce_handle = stats2[i].tce_handle = CDNR_NULL_HANDLE;
82 
83 	for (;;) {
84 		get_stats.nskip = 0;
85 		get_stats.nelements = NELEMENTS;
86 		get_stats.tce_stats = new;
87 
88 		if (ioctl(fd, CDNR_GETSTATS, &get_stats) < 0)
89 			err(1, "ioctl CDNR_GETSTATS");
90 
91 		gettimeofday(&cur_time, NULL);
92 		sec = calc_interval(&cur_time, &last_time);
93 
94 		printf("actions:\n");
95 		printf("  pass:%llu drop:%llu mark:%llu next:%llu return:%llu none:%llu\n",
96 		       (ull)get_stats.cnts[TCACODE_PASS].packets,
97 		       (ull)get_stats.cnts[TCACODE_DROP].packets,
98 		       (ull)get_stats.cnts[TCACODE_MARK].packets,
99 		       (ull)get_stats.cnts[TCACODE_NEXT].packets,
100 		       (ull)get_stats.cnts[TCACODE_RETURN].packets,
101 		       (ull)get_stats.cnts[TCACODE_NONE].packets);
102 
103 		for (i = 0; i < get_stats.nelements; i++) {
104 			sp = &new[i];
105 			lp = &last[i];
106 
107 			if (sp->tce_handle != lp->tce_handle) {
108 				quip_chandle2name(_ifname, sp->tce_handle,
109 				    cdnrnames[i], sizeof(cdnrnames[0]));
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",
135 				    profile_names[j],
136 				    (ull)sp->tce_cnts[j].packets,
137 				    (ull)sp->tce_cnts[j].bytes);
138 				if (lp->tce_handle != CDNR_NULL_HANDLE) {
139 					printf(" (%sbps)\n",
140 					    rate2str(calc_rate(
141 					        sp->tce_cnts[j].bytes,
142 						lp->tce_cnts[j].bytes, sec)));
143 				} else {
144 					printf("\n");
145 				}
146 			}
147 		}
148 		printf("\n");
149 
150 		/* swap the buffer pointers */
151 		tmp = last;
152 		last = new;
153 		new = tmp;
154 
155 		last_time = cur_time;
156 
157 		if (count != 0 && --cnt == 0)
158 			break;
159 
160 		/* wait for alarm signal */
161 		if (sigprocmask(SIG_BLOCK, NULL, &omask) == 0)
162 			sigsuspend(&omask);
163 	}
164 }
165