1 /*        $NetBSD: qdisc_cdnr.c,v 1.6 2008/05/02 19:07:44 xtraeme 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 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:   " };
56 
57 void
cdnr_stat_loop(int fd,const char * ifname,int count,int interval)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           const char                    **profile_names;
67           char                          _ifname[32];
68           int                           i, j, nprofile;
69           int cnt = count;
70           sigset_t            omask;
71 
72           if (ifname[0] == '_')
73                     ifname++;
74           snprintf(_ifname, sizeof(_ifname), "_%s", ifname);
75 
76           strlcpy(get_stats.iface.cdnr_ifname, ifname,
77                     sizeof(get_stats.iface.cdnr_ifname));
78           new = &stats1[0];
79           last = &stats2[0];
80 
81           for (i = 0; i < NELEMENTS; i++)
82                     stats1[i].tce_handle = stats2[i].tce_handle = CDNR_NULL_HANDLE;
83 
84           for (;;) {
85                     get_stats.nskip = 0;
86                     get_stats.nelements = NELEMENTS;
87                     get_stats.tce_stats = new;
88 
89                     if (ioctl(fd, CDNR_GETSTATS, &get_stats) < 0)
90                               err(1, "ioctl CDNR_GETSTATS");
91 
92                     gettimeofday(&cur_time, NULL);
93                     sec = calc_interval(&cur_time, &last_time);
94 
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);
103 
104                     for (i = 0; i < get_stats.nelements; i++) {
105                               sp = &new[i];
106                               lp = &last[i];
107 
108                               if (sp->tce_handle != lp->tce_handle) {
109                                         quip_chandle2name(_ifname, sp->tce_handle,
110                                             cdnrnames[i], sizeof(cdnrnames[0]));
111                               }
112 
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;
126                               }
127 
128                               if (nprofile == 0)
129                                         continue;
130 
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");
146                                         }
147                               }
148                     }
149                     printf("\n");
150 
151                     /* swap the buffer pointers */
152                     tmp = last;
153                     last = new;
154                     new = tmp;
155 
156                     last_time = cur_time;
157 
158                     if (count != 0 && --cnt == 0)
159                               break;
160 
161                     /* wait for alarm signal */
162                     if (sigprocmask(SIG_BLOCK, NULL, &omask) == 0)
163                               sigsuspend(&omask);
164           }
165 }
166