1*ba31dc0cSpeter /* $NetBSD: qdisc_red.c,v 1.5 2006/10/28 11:43:02 peter Exp $ */
2dd191f37Speter /* $KAME: qdisc_red.c,v 1.5 2002/11/08 06:36:18 kjc Exp $ */
38726857eSthorpej /*
48726857eSthorpej * Copyright (C) 1999-2000
58726857eSthorpej * Sony Computer Science Laboratories, Inc. All rights reserved.
68726857eSthorpej *
78726857eSthorpej * Redistribution and use in source and binary forms, with or without
88726857eSthorpej * modification, are permitted provided that the following conditions
98726857eSthorpej * are met:
108726857eSthorpej * 1. Redistributions of source code must retain the above copyright
118726857eSthorpej * notice, this list of conditions and the following disclaimer.
128726857eSthorpej * 2. Redistributions in binary form must reproduce the above copyright
138726857eSthorpej * notice, this list of conditions and the following disclaimer in the
148726857eSthorpej * documentation and/or other materials provided with the distribution.
158726857eSthorpej *
168726857eSthorpej * THIS SOFTWARE IS PROVIDED BY SONY CSL AND CONTRIBUTORS ``AS IS'' AND
178726857eSthorpej * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
188726857eSthorpej * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
198726857eSthorpej * ARE DISCLAIMED. IN NO EVENT SHALL SONY CSL OR CONTRIBUTORS BE LIABLE
208726857eSthorpej * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
218726857eSthorpej * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
228726857eSthorpej * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
238726857eSthorpej * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
248726857eSthorpej * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
258726857eSthorpej * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
268726857eSthorpej * SUCH DAMAGE.
278726857eSthorpej */
288726857eSthorpej
298726857eSthorpej #include <sys/param.h>
308726857eSthorpej #include <sys/ioctl.h>
318726857eSthorpej #include <sys/time.h>
328726857eSthorpej #include <sys/socket.h>
338726857eSthorpej #include <net/if.h>
348726857eSthorpej #include <netinet/in.h>
358726857eSthorpej #include <altq/altq.h>
368726857eSthorpej #include <altq/altq_red.h>
378726857eSthorpej
388726857eSthorpej #include <stdio.h>
398726857eSthorpej #include <stdlib.h>
408726857eSthorpej #include <unistd.h>
418726857eSthorpej #include <string.h>
42dd191f37Speter #include <signal.h>
438726857eSthorpej #include <errno.h>
448726857eSthorpej #include <err.h>
458726857eSthorpej
468726857eSthorpej #include "altqstat.h"
478726857eSthorpej
488726857eSthorpej static int avg_scale = 4096; /* default fixed-point scale */
498726857eSthorpej
508726857eSthorpej void
red_stat_loop(int fd,const char * ifname,int count,int interval)518726857eSthorpej red_stat_loop(int fd, const char *ifname, int count, int interval)
528726857eSthorpej {
538726857eSthorpej struct red_stats red_stats;
548726857eSthorpej struct timeval cur_time, last_time;
558726857eSthorpej u_int64_t last_bytes;
568726857eSthorpej double sec;
578726857eSthorpej int cnt = count;
58dd191f37Speter sigset_t omask;
598726857eSthorpej
608b8734d2Sitojun strlcpy(red_stats.iface.red_ifname, ifname,
618b8734d2Sitojun sizeof(red_stats.iface.red_ifname));
628726857eSthorpej
638726857eSthorpej gettimeofday(&last_time, NULL);
648726857eSthorpej last_time.tv_sec -= interval;
658726857eSthorpej last_bytes = 0;
668726857eSthorpej
67*ba31dc0cSpeter for (;;) {
688726857eSthorpej if (ioctl(fd, RED_GETSTATS, &red_stats) < 0)
698726857eSthorpej err(1, "ioctl RED_GETSTATS");
708726857eSthorpej
718726857eSthorpej gettimeofday(&cur_time, NULL);
728726857eSthorpej sec = calc_interval(&cur_time, &last_time);
738726857eSthorpej
748726857eSthorpej printf(" weight:%d inv_pmax:%d qthresh:(%d,%d)\n",
758726857eSthorpej red_stats.weight, red_stats.inv_pmax,
768726857eSthorpej red_stats.th_min, red_stats.th_max);
778726857eSthorpej printf(" q_len:%d (avg: %.2f), q_limit:%d\n",
788726857eSthorpej red_stats.q_len,
798726857eSthorpej ((double)red_stats.q_avg)/(double)avg_scale,
808726857eSthorpej red_stats.q_limit);
818726857eSthorpej printf(" xmit:%llu pkts, drop:%llu pkts (forced: %u, early: %u)\n",
828726857eSthorpej (ull)red_stats.xmit_cnt.packets,
838726857eSthorpej (ull)red_stats.drop_cnt.packets,
848726857eSthorpej red_stats.drop_forced, red_stats.drop_unforced);
858726857eSthorpej if (red_stats.marked_packets != 0)
868726857eSthorpej printf(" marked: %u\n", red_stats.marked_packets);
878726857eSthorpej printf(" throughput: %sbps\n",
888726857eSthorpej rate2str(calc_rate(red_stats.xmit_cnt.bytes,
898726857eSthorpej last_bytes, sec)));
908726857eSthorpej if (red_stats.fv_alloc > 0) {
918726857eSthorpej printf(" flowvalve: alloc:%u flows:%u\n",
928726857eSthorpej red_stats.fv_alloc, red_stats.fv_flows);
938726857eSthorpej printf(" predrop:%u pass:%u escape:%u\n",
948726857eSthorpej red_stats.fv_predrop, red_stats.fv_pass,
958726857eSthorpej red_stats.fv_escape);
968726857eSthorpej }
978726857eSthorpej printf("\n");
988726857eSthorpej
998726857eSthorpej last_bytes = red_stats.xmit_cnt.bytes;
1008726857eSthorpej last_time = cur_time;
101dd191f37Speter
102*ba31dc0cSpeter if (count != 0 && --cnt == 0)
103*ba31dc0cSpeter break;
104*ba31dc0cSpeter
105dd191f37Speter /* wait for alarm signal */
106dd191f37Speter if (sigprocmask(SIG_BLOCK, NULL, &omask) == 0)
107dd191f37Speter sigsuspend(&omask);
1088726857eSthorpej }
1098726857eSthorpej }
1108726857eSthorpej
1118726857eSthorpej int
print_redstats(struct redstats * rp)1128726857eSthorpej print_redstats(struct redstats *rp)
1138726857eSthorpej {
1148726857eSthorpej printf(" RED q_avg:%.2f xmit:%llu (forced:%u early:%u marked:%u)\n",
1158726857eSthorpej ((double)rp->q_avg)/(double)avg_scale,
1168726857eSthorpej (ull)rp->xmit_cnt.packets,
1178726857eSthorpej rp->drop_forced,
1188726857eSthorpej rp->drop_unforced,
1198726857eSthorpej rp->marked_packets);
1208726857eSthorpej return 0;
1218726857eSthorpej }
122