1 /*        $NetBSD: qdisc_conf.c,v 1.6 2011/01/04 09:13:22 wiz 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.
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/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>
38 
39 #include <stdio.h>
40 #include <stdlib.h>
41 #include <unistd.h>
42 #include <string.h>
43 #include <err.h>
44 
45 #include "altqstat.h"
46 
47 #define ALTQ_DEVICE "/dev/altq/altq"
48 
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}
61 };
62 
63 stat_loop_t *
qdisc2stat_loop(const char * qdisc_name)64 qdisc2stat_loop(const char *qdisc_name)
65 {
66           struct qdisc_conf *st;
67 
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);
72 }
73 
74 int
ifname2qdisc(const char * ifname,char * qname)75 ifname2qdisc(const char *ifname, char *qname)
76 {
77           struct altqreq qtypereq;
78           int fd, qtype = 0;
79 
80           if (ifname[0] == '_') {
81                     /* input interface */
82                     if (qname != NULL)
83                               strlcpy(qname, "cdnr", 64);
84                     return (ALTQT_CDNR);
85           }
86 
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);
91           }
92           if (ioctl(fd, ALTQGTYPE, &qtypereq) < 0) {
93                     warn("ALTQGQTYPE");
94                     close(fd);
95                     return (0);
96           }
97           close(fd);
98 
99           if (qname != NULL) {
100                     struct qdisc_conf *st;
101 
102                     qtype = qtypereq.arg;
103                     for (st = qdisc_table; st->qdisc_name != NULL; st++)
104                               if (st->altqtype == qtype)
105                                         strlcpy(qname, st->qdisc_name, 64);
106           }
107 
108           return (qtype);
109 }
110 
111