xref: /netbsd/usr.sbin/altq/libaltq/qop_fifoq.c (revision 69881cf6)
1*69881cf6Sitojun /*	$NetBSD: qop_fifoq.c,v 1.5 2002/03/05 04:11:53 itojun Exp $	*/
2*69881cf6Sitojun /*	$KAME: qop_fifoq.c,v 1.6 2001/12/03 08:20:55 kjc Exp $	*/
395a65560Sthorpej /*
495a65560Sthorpej  * Copyright (C) 1999-2000
595a65560Sthorpej  *	Sony Computer Science Laboratories, Inc.  All rights reserved.
695a65560Sthorpej  *
795a65560Sthorpej  * Redistribution and use in source and binary forms, with or without
895a65560Sthorpej  * modification, are permitted provided that the following conditions
995a65560Sthorpej  * are met:
1095a65560Sthorpej  * 1. Redistributions of source code must retain the above copyright
1195a65560Sthorpej  *    notice, this list of conditions and the following disclaimer.
1295a65560Sthorpej  * 2. Redistributions in binary form must reproduce the above copyright
1395a65560Sthorpej  *    notice, this list of conditions and the following disclaimer in the
1495a65560Sthorpej  *    documentation and/or other materials provided with the distribution.
1595a65560Sthorpej  *
1695a65560Sthorpej  * THIS SOFTWARE IS PROVIDED BY SONY CSL AND CONTRIBUTORS ``AS IS'' AND
1795a65560Sthorpej  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
1895a65560Sthorpej  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
1995a65560Sthorpej  * ARE DISCLAIMED.  IN NO EVENT SHALL SONY CSL OR CONTRIBUTORS BE LIABLE
2095a65560Sthorpej  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
2195a65560Sthorpej  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
2295a65560Sthorpej  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
2395a65560Sthorpej  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
2495a65560Sthorpej  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
2595a65560Sthorpej  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
2695a65560Sthorpej  * SUCH DAMAGE.
2795a65560Sthorpej  */
2895a65560Sthorpej 
2995a65560Sthorpej #include <sys/param.h>
3095a65560Sthorpej #include <sys/socket.h>
3195a65560Sthorpej #include <sys/sockio.h>
3295a65560Sthorpej #include <sys/ioctl.h>
3395a65560Sthorpej #include <sys/fcntl.h>
3495a65560Sthorpej #include <net/if.h>
3595a65560Sthorpej #include <netinet/in.h>
3695a65560Sthorpej #include <arpa/inet.h>
3795a65560Sthorpej 
3895a65560Sthorpej #include <stdio.h>
3995a65560Sthorpej #include <stdlib.h>
4095a65560Sthorpej #include <unistd.h>
4195a65560Sthorpej #include <stddef.h>
4295a65560Sthorpej #include <string.h>
4395a65560Sthorpej #include <ctype.h>
4495a65560Sthorpej #include <errno.h>
4595a65560Sthorpej #include <syslog.h>
4695a65560Sthorpej #include <netdb.h>
4795a65560Sthorpej 
4895a65560Sthorpej #include <altq/altq.h>
4995a65560Sthorpej #include <altq/altq_fifoq.h>
5095a65560Sthorpej #include "altq_qop.h"
5195a65560Sthorpej #include "qop_fifoq.h"
5295a65560Sthorpej 
538b8734d2Sitojun static int fifoq_attach(struct ifinfo *);
548b8734d2Sitojun static int fifoq_detach(struct ifinfo *);
558b8734d2Sitojun static int fifoq_enable(struct ifinfo *);
568b8734d2Sitojun static int fifoq_disable(struct ifinfo *);
5795a65560Sthorpej 
5895a65560Sthorpej #define FIFOQ_DEVICE	"/dev/altq/fifoq"
5995a65560Sthorpej 
6095a65560Sthorpej static int fifoq_fd = -1;
6195a65560Sthorpej static int fifoq_refcount = 0;
6295a65560Sthorpej 
6395a65560Sthorpej static struct qdisc_ops fifoq_qdisc = {
6495a65560Sthorpej 	ALTQT_FIFOQ,
6595a65560Sthorpej 	"fifoq",
6695a65560Sthorpej 	fifoq_attach,
6795a65560Sthorpej 	fifoq_detach,
6895a65560Sthorpej 	NULL,			/* clear */
6995a65560Sthorpej 	fifoq_enable,
7095a65560Sthorpej 	fifoq_disable,
7195a65560Sthorpej 	NULL,			/* add class */
7295a65560Sthorpej 	NULL,			/* modify class */
7395a65560Sthorpej 	NULL,			/* delete class */
7495a65560Sthorpej 	NULL,			/* add filter */
7595a65560Sthorpej 	NULL			/* delete filter */
7695a65560Sthorpej };
7795a65560Sthorpej 
7895a65560Sthorpej /*
7995a65560Sthorpej  * parser interface
8095a65560Sthorpej  */
8195a65560Sthorpej #define EQUAL(s1, s2)	(strcmp((s1), (s2)) == 0)
8295a65560Sthorpej 
8395a65560Sthorpej int
fifoq_interface_parser(const char * ifname,int argc,char ** argv)8495a65560Sthorpej fifoq_interface_parser(const char *ifname, int argc, char **argv)
8595a65560Sthorpej {
8695a65560Sthorpej 	u_int  	bandwidth = 100000000;	/* 100Mbps */
8795a65560Sthorpej 	u_int	tbrsize = 0;
8895a65560Sthorpej 	int	qlimit = 50;
8995a65560Sthorpej 
9095a65560Sthorpej 	/*
9195a65560Sthorpej 	 * process options
9295a65560Sthorpej 	 */
9395a65560Sthorpej 	while (argc > 0) {
9495a65560Sthorpej 		if (EQUAL(*argv, "bandwidth")) {
9595a65560Sthorpej 			argc--; argv++;
9695a65560Sthorpej 			if (argc > 0)
9795a65560Sthorpej 				bandwidth = atobps(*argv);
9895a65560Sthorpej 		} else if (EQUAL(*argv, "tbrsize")) {
9995a65560Sthorpej 			argc--; argv++;
10095a65560Sthorpej 			if (argc > 0)
10195a65560Sthorpej 				tbrsize = atobytes(*argv);
10295a65560Sthorpej 		} else if (EQUAL(*argv, "qlimit")) {
10395a65560Sthorpej 			argc--; argv++;
10495a65560Sthorpej 			if (argc > 0)
10595a65560Sthorpej 				qlimit = (int)strtol(*argv, NULL, 0);
10695a65560Sthorpej 		} else if (EQUAL(*argv, "fifoq")) {
10795a65560Sthorpej 			/* just skip */
10895a65560Sthorpej 		} else {
109*69881cf6Sitojun 			LOG(LOG_ERR, 0, "Unknown keyword '%s'", *argv);
11095a65560Sthorpej 			return (0);
11195a65560Sthorpej 		}
11295a65560Sthorpej 		argc--; argv++;
11395a65560Sthorpej 	}
11495a65560Sthorpej 
11595a65560Sthorpej 	if (qcmd_tbr_register(ifname, bandwidth, tbrsize) != 0)
11695a65560Sthorpej 		return (0);
11795a65560Sthorpej 
11895a65560Sthorpej 	if (qcmd_fifoq_add_if(ifname, bandwidth, qlimit) != 0)
11995a65560Sthorpej 		return (0);
12095a65560Sthorpej 	return (1);
12195a65560Sthorpej }
12295a65560Sthorpej 
12395a65560Sthorpej /*
12495a65560Sthorpej  * qcmd api
12595a65560Sthorpej  */
12695a65560Sthorpej int
qcmd_fifoq_add_if(const char * ifname,u_int bandwidth,int qlimit)12795a65560Sthorpej qcmd_fifoq_add_if(const char *ifname, u_int bandwidth, int qlimit)
12895a65560Sthorpej {
12995a65560Sthorpej 	int error;
13095a65560Sthorpej 
13195a65560Sthorpej 	error = qop_fifoq_add_if(NULL, ifname, bandwidth, qlimit);
13295a65560Sthorpej 	if (error != 0)
133d5e1f166Sitojun 		LOG(LOG_ERR, errno, "%s: can't add fifoq on interface '%s'",
13495a65560Sthorpej 		    qoperror(error), ifname);
13595a65560Sthorpej 	return (error);
13695a65560Sthorpej }
13795a65560Sthorpej 
13895a65560Sthorpej /*
13995a65560Sthorpej  * qop api
14095a65560Sthorpej  */
14195a65560Sthorpej int
qop_fifoq_add_if(struct ifinfo ** rp,const char * ifname,u_int bandwidth,int qlimit)14295a65560Sthorpej qop_fifoq_add_if(struct ifinfo **rp, const char *ifname,
14395a65560Sthorpej 		 u_int bandwidth, int qlimit)
14495a65560Sthorpej {
14595a65560Sthorpej 	struct ifinfo *ifinfo = NULL;
14695a65560Sthorpej 	struct fifoq_ifinfo *fifoq_ifinfo;
14795a65560Sthorpej 	int error;
14895a65560Sthorpej 
14995a65560Sthorpej 	if ((fifoq_ifinfo = calloc(1, sizeof(*fifoq_ifinfo))) == NULL)
15095a65560Sthorpej 		return (QOPERR_NOMEM);
15195a65560Sthorpej 	fifoq_ifinfo->qlimit   = qlimit;
15295a65560Sthorpej 
15395a65560Sthorpej 	error = qop_add_if(&ifinfo, ifname, bandwidth,
15495a65560Sthorpej 			   &fifoq_qdisc, fifoq_ifinfo);
15595a65560Sthorpej 	if (error != 0) {
15695a65560Sthorpej 		free(fifoq_ifinfo);
15795a65560Sthorpej 		return (error);
15895a65560Sthorpej 	}
15995a65560Sthorpej 
16095a65560Sthorpej 	if (rp != NULL)
16195a65560Sthorpej 		*rp = ifinfo;
16295a65560Sthorpej 	return (0);
16395a65560Sthorpej }
16495a65560Sthorpej 
16595a65560Sthorpej /*
16695a65560Sthorpej  *  system call interfaces for qdisc_ops
16795a65560Sthorpej  */
16895a65560Sthorpej static int
fifoq_attach(struct ifinfo * ifinfo)16995a65560Sthorpej fifoq_attach(struct ifinfo *ifinfo)
17095a65560Sthorpej {
17195a65560Sthorpej 	struct fifoq_interface iface;
17295a65560Sthorpej 	struct fifoq_ifinfo *fifoq_ifinfo;
17395a65560Sthorpej 	struct fifoq_conf conf;
17495a65560Sthorpej 
17595a65560Sthorpej 	if (fifoq_fd < 0 &&
17695a65560Sthorpej 	    (fifoq_fd = open(FIFOQ_DEVICE, O_RDWR)) < 0 &&
17795a65560Sthorpej 	    (fifoq_fd = open_module(FIFOQ_DEVICE, O_RDWR)) < 0) {
178d5e1f166Sitojun 		LOG(LOG_ERR, errno, "FIFOQ open");
17995a65560Sthorpej 		return (QOPERR_SYSCALL);
18095a65560Sthorpej 	}
18195a65560Sthorpej 
18295a65560Sthorpej 	fifoq_refcount++;
18395a65560Sthorpej 	memset(&iface, 0, sizeof(iface));
18495a65560Sthorpej 	strncpy(iface.fifoq_ifname, ifinfo->ifname, IFNAMSIZ);
18595a65560Sthorpej 
18695a65560Sthorpej 	if (ioctl(fifoq_fd, FIFOQ_IF_ATTACH, &iface) < 0)
18795a65560Sthorpej 		return (QOPERR_SYSCALL);
18895a65560Sthorpej 
18995a65560Sthorpej 	/* set fifoq parameters */
19095a65560Sthorpej 	fifoq_ifinfo = (struct fifoq_ifinfo *)ifinfo->private;
19195a65560Sthorpej 	if (fifoq_ifinfo->qlimit > 0) {
19295a65560Sthorpej 		memset(&conf, 0, sizeof(conf));
19395a65560Sthorpej 		strncpy(conf.iface.fifoq_ifname, ifinfo->ifname, IFNAMSIZ);
19495a65560Sthorpej 		conf.fifoq_limit = fifoq_ifinfo->qlimit;
19595a65560Sthorpej 		if (ioctl(fifoq_fd, FIFOQ_CONFIG, &conf) < 0)
19695a65560Sthorpej 			return (QOPERR_SYSCALL);
19795a65560Sthorpej 	}
19895a65560Sthorpej #if 1
199d5e1f166Sitojun 	LOG(LOG_INFO, 0, "fifoq attached to %s", iface.fifoq_ifname);
20095a65560Sthorpej #endif
20195a65560Sthorpej 	return (0);
20295a65560Sthorpej }
20395a65560Sthorpej 
20495a65560Sthorpej static int
fifoq_detach(struct ifinfo * ifinfo)20595a65560Sthorpej fifoq_detach(struct ifinfo *ifinfo)
20695a65560Sthorpej {
20795a65560Sthorpej 	struct fifoq_interface iface;
20895a65560Sthorpej 
20995a65560Sthorpej 	memset(&iface, 0, sizeof(iface));
21095a65560Sthorpej 	strncpy(iface.fifoq_ifname, ifinfo->ifname, IFNAMSIZ);
21195a65560Sthorpej 
21295a65560Sthorpej 	if (ioctl(fifoq_fd, FIFOQ_IF_DETACH, &iface) < 0)
21395a65560Sthorpej 		return (QOPERR_SYSCALL);
21495a65560Sthorpej 
21595a65560Sthorpej 	if (--fifoq_refcount == 0) {
21695a65560Sthorpej 		close(fifoq_fd);
21795a65560Sthorpej 		fifoq_fd = -1;
21895a65560Sthorpej 	}
21995a65560Sthorpej 	return (0);
22095a65560Sthorpej }
22195a65560Sthorpej 
22295a65560Sthorpej static int
fifoq_enable(struct ifinfo * ifinfo)22395a65560Sthorpej fifoq_enable(struct ifinfo *ifinfo)
22495a65560Sthorpej {
22595a65560Sthorpej 	struct fifoq_interface iface;
22695a65560Sthorpej 
22795a65560Sthorpej 	memset(&iface, 0, sizeof(iface));
22895a65560Sthorpej 	strncpy(iface.fifoq_ifname, ifinfo->ifname, IFNAMSIZ);
22995a65560Sthorpej 
23095a65560Sthorpej 	if (ioctl(fifoq_fd, FIFOQ_ENABLE, &iface) < 0)
23195a65560Sthorpej 		return (QOPERR_SYSCALL);
23295a65560Sthorpej 	return (0);
23395a65560Sthorpej }
23495a65560Sthorpej 
23595a65560Sthorpej static int
fifoq_disable(struct ifinfo * ifinfo)23695a65560Sthorpej fifoq_disable(struct ifinfo *ifinfo)
23795a65560Sthorpej {
23895a65560Sthorpej 	struct fifoq_interface iface;
23995a65560Sthorpej 
24095a65560Sthorpej 	memset(&iface, 0, sizeof(iface));
24195a65560Sthorpej 	strncpy(iface.fifoq_ifname, ifinfo->ifname, IFNAMSIZ);
24295a65560Sthorpej 
24395a65560Sthorpej 	if (ioctl(fifoq_fd, FIFOQ_DISABLE, &iface) < 0)
24495a65560Sthorpej 		return (QOPERR_SYSCALL);
24595a65560Sthorpej 	return (0);
24695a65560Sthorpej }
247