xref: /netbsd-src/usr.sbin/altq/altqstat/qdisc_conf.c (revision d710132b4b8ce7f7cccaaf660cb16aa16b4077a0)
1 /*	$NetBSD: qdisc_conf.c,v 1.3 2001/08/16 07:48:11 itojun Exp $	*/
2 /*	$KAME: qdisc_conf.c,v 1.4 2001/08/15 12:51:59 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 	{NULL, 		0,		NULL}
60 };
61 
62 stat_loop_t *
63 qdisc2stat_loop(const char *qdisc_name)
64 {
65 	struct qdisc_conf *stat;
66 
67 	for (stat = qdisc_table; stat->qdisc_name != NULL; stat++)
68 		if (strcmp(stat->qdisc_name, qdisc_name) == 0)
69 			return (stat->stat_loop);
70 	return (NULL);
71 }
72 
73 int
74 ifname2qdisc(const char *ifname, char *qname)
75 {
76 	struct altqreq qtypereq;
77 	int fd, qtype = 0;
78 
79 	if (ifname[0] == '_') {
80 		/* input interface */
81 		if (qname != NULL)
82 			strlcpy(qname, "cdnr", 64);
83 		return (ALTQT_CDNR);
84 	}
85 
86 	strlcpy(qtypereq.ifname, ifname, sizeof(qtypereq.ifname));
87 	if ((fd = open(ALTQ_DEVICE, O_RDONLY)) < 0) {
88 		warn("can't open %s", ALTQ_DEVICE);
89 		return (0);
90 	}
91 	if (ioctl(fd, ALTQGTYPE, &qtypereq) < 0) {
92 		warn("ALTQGQTYPE");
93 		return (0);
94 	}
95 	close(fd);
96 
97 	if (qname != NULL) {
98 		struct qdisc_conf *stat;
99 
100 		qtype = qtypereq.arg;
101 		for (stat = qdisc_table; stat->qdisc_name != NULL; stat++)
102 			if (stat->altqtype == qtype)
103 				strlcpy(qname, stat->qdisc_name, 64);
104 	}
105 
106 	return (qtype);
107 }
108 
109