Patrick Welche <prlw1@cam.ac.uk>
[netbsd-mini2440.git] / usr.sbin / altq / altqstat / qdisc_cdnr.c
blobc47081c2aae8bbe760935b9a6e839c7e778cf4d3
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.
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.
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.
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>
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>
46 #include "quip_client.h"
47 #include "altqstat.h"
49 #define NELEMENTS 64
50 #define MAX_PROB (128*1024)
52 static const char *element_names[] = { "none", "top", "element", "tbmeter", "trtcm",
53 "tswtcm" };
54 static const char *tbmprof_names[] = { "in: ", "out: " };
55 static const char *tcmprof_names[] = { "green: ", "yellow:", "red: " };
57 void
58 cdnr_stat_loop(int fd, const char *ifname, int count, int interval)
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 const char **profile_names;
67 char _ifname[32];
68 int i, j, nprofile;
69 int cnt = count;
70 sigset_t omask;
72 if (ifname[0] == '_')
73 ifname++;
74 snprintf(_ifname, sizeof(_ifname), "_%s", ifname);
76 strlcpy(get_stats.iface.cdnr_ifname, ifname,
77 sizeof(get_stats.iface.cdnr_ifname));
78 new = &stats1[0];
79 last = &stats2[0];
81 for (i = 0; i < NELEMENTS; i++)
82 stats1[i].tce_handle = stats2[i].tce_handle = CDNR_NULL_HANDLE;
84 for (;;) {
85 get_stats.nskip = 0;
86 get_stats.nelements = NELEMENTS;
87 get_stats.tce_stats = new;
89 if (ioctl(fd, CDNR_GETSTATS, &get_stats) < 0)
90 err(1, "ioctl CDNR_GETSTATS");
92 gettimeofday(&cur_time, NULL);
93 sec = calc_interval(&cur_time, &last_time);
95 printf("actions:\n");
96 printf(" pass:%llu drop:%llu mark:%llu next:%llu return:%llu none:%llu\n",
97 (ull)get_stats.cnts[TCACODE_PASS].packets,
98 (ull)get_stats.cnts[TCACODE_DROP].packets,
99 (ull)get_stats.cnts[TCACODE_MARK].packets,
100 (ull)get_stats.cnts[TCACODE_NEXT].packets,
101 (ull)get_stats.cnts[TCACODE_RETURN].packets,
102 (ull)get_stats.cnts[TCACODE_NONE].packets);
104 for (i = 0; i < get_stats.nelements; i++) {
105 sp = &new[i];
106 lp = &last[i];
108 if (sp->tce_handle != lp->tce_handle) {
109 quip_chandle2name(_ifname, sp->tce_handle,
110 cdnrnames[i], sizeof(cdnrnames[0]));
113 switch (sp->tce_type) {
114 case TCETYPE_TBMETER:
115 nprofile = 2;
116 profile_names = tbmprof_names;
117 break;
118 case TCETYPE_TRTCM:
119 case TCETYPE_TSWTCM:
120 nprofile = 3;
121 profile_names = tcmprof_names;
122 break;
123 default:
124 profile_names = tbmprof_names; /* silence cc */
125 nprofile = 0;
128 if (nprofile == 0)
129 continue;
131 printf("[%s: %s] handle:%#lx\n",
132 element_names[sp->tce_type], cdnrnames[i],
133 sp->tce_handle);
134 for (j = 0; j < nprofile; j++) {
135 printf(" %s %10llu pkts %16llu bytes",
136 profile_names[j],
137 (ull)sp->tce_cnts[j].packets,
138 (ull)sp->tce_cnts[j].bytes);
139 if (lp->tce_handle != CDNR_NULL_HANDLE) {
140 printf(" (%sbps)\n",
141 rate2str(calc_rate(
142 sp->tce_cnts[j].bytes,
143 lp->tce_cnts[j].bytes, sec)));
144 } else {
145 printf("\n");
149 printf("\n");
151 /* swap the buffer pointers */
152 tmp = last;
153 last = new;
154 new = tmp;
156 last_time = cur_time;
158 if (count != 0 && --cnt == 0)
159 break;
161 /* wait for alarm signal */
162 if (sigprocmask(SIG_BLOCK, NULL, &omask) == 0)
163 sigsuspend(&omask);