Patrick Welche <prlw1@cam.ac.uk>
[netbsd-mini2440.git] / usr.sbin / altq / altqstat / qdisc_conf.c
blob0eebdccf1a3a64ee9714e69a8ca8c55be3697a20
1 /* $NetBSD: qdisc_conf.c,v 1.4 2006/10/12 19:59:13 peter Exp $ */
2 /* $KAME: qdisc_conf.c,v 1.5 2002/10/26 06:59:54 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/socket.h>
31 #if defined(__NetBSD__) || defined(__OpenBSD__)
32 #include <sys/ioctl.h>
33 #endif
34 #include <sys/fcntl.h>
35 #include <net/if.h>
36 #include <netinet/in.h>
37 #include <altq/altq.h>
39 #include <stdio.h>
40 #include <stdlib.h>
41 #include <unistd.h>
42 #include <string.h>
43 #include <err.h>
45 #include "altqstat.h"
47 #define ALTQ_DEVICE "/dev/altq/altq"
49 struct qdisc_conf qdisc_table[] = {
50 {"cbq", ALTQT_CBQ, cbq_stat_loop},
51 {"hfsc", ALTQT_HFSC, hfsc_stat_loop},
52 {"cdnr", ALTQT_CDNR, cdnr_stat_loop},
53 {"wfq", ALTQT_WFQ, wfq_stat_loop},
54 {"fifoq", ALTQT_FIFOQ, fifoq_stat_loop},
55 {"red", ALTQT_RED, red_stat_loop},
56 {"rio", ALTQT_RIO, rio_stat_loop},
57 {"blue", ALTQT_BLUE, blue_stat_loop},
58 {"priq", ALTQT_PRIQ, priq_stat_loop},
59 {"jobs", ALTQT_JOBS, jobs_stat_loop},
60 {NULL, 0, NULL}
63 stat_loop_t *
64 qdisc2stat_loop(const char *qdisc_name)
66 struct qdisc_conf *st;
68 for (st = qdisc_table; st->qdisc_name != NULL; st++)
69 if (strcmp(st->qdisc_name, qdisc_name) == 0)
70 return (st->stat_loop);
71 return (NULL);
74 int
75 ifname2qdisc(const char *ifname, char *qname)
77 struct altqreq qtypereq;
78 int fd, qtype = 0;
80 if (ifname[0] == '_') {
81 /* input interface */
82 if (qname != NULL)
83 strlcpy(qname, "cdnr", 64);
84 return (ALTQT_CDNR);
87 strlcpy(qtypereq.ifname, ifname, sizeof(qtypereq.ifname));
88 if ((fd = open(ALTQ_DEVICE, O_RDONLY)) < 0) {
89 warn("can't open %s", ALTQ_DEVICE);
90 return (0);
92 if (ioctl(fd, ALTQGTYPE, &qtypereq) < 0) {
93 warn("ALTQGQTYPE");
94 return (0);
96 close(fd);
98 if (qname != NULL) {
99 struct qdisc_conf *st;
101 qtype = qtypereq.arg;
102 for (st = qdisc_table; st->qdisc_name != NULL; st++)
103 if (st->altqtype == qtype)
104 strlcpy(qname, st->qdisc_name, 64);
107 return (qtype);