xref: /netbsd-src/usr.sbin/altq/libaltq/qop_blue.c (revision 69881cf6b8dc615ac34107b2f982835e042159e3)
1*69881cf6Sitojun /*	$NetBSD: qop_blue.c,v 1.5 2002/03/05 04:11:53 itojun Exp $	*/
2*69881cf6Sitojun /*	$KAME: qop_blue.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_blue.h>
5095a65560Sthorpej #include "altq_qop.h"
5195a65560Sthorpej #include "qop_blue.h"
5295a65560Sthorpej 
538b8734d2Sitojun static int blue_attach(struct ifinfo *);
548b8734d2Sitojun static int blue_detach(struct ifinfo *);
558b8734d2Sitojun static int blue_enable(struct ifinfo *);
568b8734d2Sitojun static int blue_disable(struct ifinfo *);
5795a65560Sthorpej 
5895a65560Sthorpej #define BLUE_DEVICE	"/dev/altq/blue"
5995a65560Sthorpej 
6095a65560Sthorpej static int blue_fd = -1;
6195a65560Sthorpej static int blue_refcount = 0;
6295a65560Sthorpej 
6395a65560Sthorpej static struct qdisc_ops blue_qdisc = {
6495a65560Sthorpej 	ALTQT_BLUE,
6595a65560Sthorpej 	"blue",
6695a65560Sthorpej 	blue_attach,
6795a65560Sthorpej 	blue_detach,
6895a65560Sthorpej 	NULL,			/* clear */
6995a65560Sthorpej 	blue_enable,
7095a65560Sthorpej 	blue_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
blue_interface_parser(const char * ifname,int argc,char ** argv)8495a65560Sthorpej blue_interface_parser(const char *ifname, int argc, char **argv)
8595a65560Sthorpej {
8695a65560Sthorpej 	u_int  	bandwidth = 100000000;	/* 100Mbps */
8795a65560Sthorpej 	u_int	tbrsize = 0;
8895a65560Sthorpej 	int	max_pmark = 4000;
8995a65560Sthorpej 	int	hold_time = 1000;
9095a65560Sthorpej 	int	qlimit = 60;
9195a65560Sthorpej 	int	pkttime = 0;
9295a65560Sthorpej 	int	flags = 0;
9395a65560Sthorpej 	int	packet_size = 1000;
9495a65560Sthorpej 
9595a65560Sthorpej 	/*
9695a65560Sthorpej 	 * process options
9795a65560Sthorpej 	 */
9895a65560Sthorpej 	while (argc > 0) {
9995a65560Sthorpej 		if (EQUAL(*argv, "bandwidth")) {
10095a65560Sthorpej 			argc--; argv++;
10195a65560Sthorpej 			if (argc > 0)
10295a65560Sthorpej 				bandwidth = atobps(*argv);
10395a65560Sthorpej 		} else if (EQUAL(*argv, "tbrsize")) {
10495a65560Sthorpej 			argc--; argv++;
10595a65560Sthorpej 			if (argc > 0)
10695a65560Sthorpej 				tbrsize = atobytes(*argv);
10795a65560Sthorpej 		} else if (EQUAL(*argv, "packetsize")) {
10895a65560Sthorpej 			argc--; argv++;
10995a65560Sthorpej 			if (argc > 0)
11095a65560Sthorpej 				packet_size = atobytes(*argv);
11195a65560Sthorpej 		} else if (EQUAL(*argv, "qlimit")) {
11295a65560Sthorpej 			argc--; argv++;
11395a65560Sthorpej 			if (argc > 0)
11495a65560Sthorpej 				qlimit = (int)strtol(*argv, NULL, 0);
11595a65560Sthorpej 		} else if (EQUAL(*argv, "maxpmark")) {
11695a65560Sthorpej 			argc--; argv++;
11795a65560Sthorpej 			if (argc > 0)
11895a65560Sthorpej 				max_pmark = (int)strtol(*argv, NULL, 0);
11995a65560Sthorpej 		} else if (EQUAL(*argv, "holdtime")) {
12095a65560Sthorpej 			argc--; argv++;
12195a65560Sthorpej 			if (argc > 0)
12295a65560Sthorpej 				hold_time = (int)strtol(*argv, NULL, 0);
12395a65560Sthorpej 		} else if (EQUAL(*argv, "blue")) {
12495a65560Sthorpej 			/* just skip */
12595a65560Sthorpej 		} else if (EQUAL(*argv, "ecn")) {
12695a65560Sthorpej 			flags |= BLUEF_ECN;
12795a65560Sthorpej 		} else {
128*69881cf6Sitojun 			LOG(LOG_ERR, 0, "Unknown keyword '%s'", *argv);
12995a65560Sthorpej 			return (0);
13095a65560Sthorpej 		}
13195a65560Sthorpej 		argc--; argv++;
13295a65560Sthorpej 	}
13395a65560Sthorpej 
13495a65560Sthorpej 	if (qcmd_tbr_register(ifname, bandwidth, tbrsize) != 0)
13595a65560Sthorpej 		return (0);
13695a65560Sthorpej 
13795a65560Sthorpej 	pkttime = packet_size * 8 * 1000 / (bandwidth / 1000);
13895a65560Sthorpej 
13995a65560Sthorpej 	if (qcmd_blue_add_if(ifname, bandwidth, max_pmark, hold_time,
14095a65560Sthorpej 			     qlimit, pkttime, flags) != 0)
14195a65560Sthorpej 		return (0);
14295a65560Sthorpej 	return (1);
14395a65560Sthorpej }
14495a65560Sthorpej 
14595a65560Sthorpej /*
14695a65560Sthorpej  * qcmd api
14795a65560Sthorpej  */
14895a65560Sthorpej int
qcmd_blue_add_if(const char * ifname,u_int bandwidth,int max_pmark,int hold_time,int qlimit,int pkttime,int flags)14995a65560Sthorpej qcmd_blue_add_if(const char *ifname, u_int bandwidth, int max_pmark,
15095a65560Sthorpej 		 int hold_time, int qlimit, int pkttime, int flags)
15195a65560Sthorpej {
15295a65560Sthorpej 	int error;
15395a65560Sthorpej 
15495a65560Sthorpej 	error = qop_blue_add_if(NULL, ifname, bandwidth, max_pmark, hold_time,
15595a65560Sthorpej 				qlimit, pkttime, flags);
15695a65560Sthorpej 	if (error != 0)
157d5e1f166Sitojun 		LOG(LOG_ERR, errno, "%s: can't add blue on interface '%s'",
15895a65560Sthorpej 		    qoperror(error), ifname);
15995a65560Sthorpej 	return (error);
16095a65560Sthorpej }
16195a65560Sthorpej 
16295a65560Sthorpej /*
16395a65560Sthorpej  * qop api
16495a65560Sthorpej  */
16595a65560Sthorpej int
qop_blue_add_if(struct ifinfo ** rp,const char * ifname,u_int bandwidth,int max_pmark,int hold_time,int qlimit,int pkttime,int flags)16695a65560Sthorpej qop_blue_add_if(struct ifinfo **rp, const char *ifname, u_int bandwidth,
16795a65560Sthorpej 		int max_pmark, int hold_time, int qlimit,
16895a65560Sthorpej 		int pkttime, int flags)
16995a65560Sthorpej {
17095a65560Sthorpej 	struct ifinfo *ifinfo = NULL;
17195a65560Sthorpej 	struct blue_ifinfo *blue_ifinfo;
17295a65560Sthorpej 	int error;
17395a65560Sthorpej 
17495a65560Sthorpej 	if ((blue_ifinfo = calloc(1, sizeof(*blue_ifinfo))) == NULL)
17595a65560Sthorpej 		return (QOPERR_NOMEM);
17695a65560Sthorpej 	blue_ifinfo->max_pmark = max_pmark;
17795a65560Sthorpej 	blue_ifinfo->hold_time = hold_time;
17895a65560Sthorpej 	blue_ifinfo->qlimit    = qlimit;
17995a65560Sthorpej 	blue_ifinfo->pkttime   = pkttime;
18095a65560Sthorpej 	blue_ifinfo->flags     = flags;
18195a65560Sthorpej 
18295a65560Sthorpej 	error = qop_add_if(&ifinfo, ifname, bandwidth,
18395a65560Sthorpej 			   &blue_qdisc, blue_ifinfo);
18495a65560Sthorpej 	if (error != 0) {
18595a65560Sthorpej 		free(blue_ifinfo);
18695a65560Sthorpej 		return (error);
18795a65560Sthorpej 	}
18895a65560Sthorpej 
18995a65560Sthorpej 	if (rp != NULL)
19095a65560Sthorpej 		*rp = ifinfo;
19195a65560Sthorpej 	return (0);
19295a65560Sthorpej }
19395a65560Sthorpej 
19495a65560Sthorpej /*
19595a65560Sthorpej  *  system call interfaces for qdisc_ops
19695a65560Sthorpej  */
19795a65560Sthorpej static int
blue_attach(struct ifinfo * ifinfo)19895a65560Sthorpej blue_attach(struct ifinfo *ifinfo)
19995a65560Sthorpej {
20095a65560Sthorpej 	struct blue_interface iface;
20195a65560Sthorpej 	struct blue_ifinfo *blue_ifinfo;
20295a65560Sthorpej 	struct blue_conf conf;
20395a65560Sthorpej 
20495a65560Sthorpej 	if (blue_fd < 0 &&
20595a65560Sthorpej 	    (blue_fd = open(BLUE_DEVICE, O_RDWR)) < 0 &&
20695a65560Sthorpej 	    (blue_fd = open_module(BLUE_DEVICE, O_RDWR)) < 0) {
207d5e1f166Sitojun 		LOG(LOG_ERR, errno, "BLUE open");
20895a65560Sthorpej 		return (QOPERR_SYSCALL);
20995a65560Sthorpej 	}
21095a65560Sthorpej 
21195a65560Sthorpej 	blue_refcount++;
21295a65560Sthorpej 	memset(&iface, 0, sizeof(iface));
21395a65560Sthorpej 	strncpy(iface.blue_ifname, ifinfo->ifname, IFNAMSIZ);
21495a65560Sthorpej 
21595a65560Sthorpej 	if (ioctl(blue_fd, BLUE_IF_ATTACH, &iface) < 0)
21695a65560Sthorpej 		return (QOPERR_SYSCALL);
21795a65560Sthorpej 
21895a65560Sthorpej 	/* set blue parameters */
21995a65560Sthorpej 	blue_ifinfo = (struct blue_ifinfo *)ifinfo->private;
22095a65560Sthorpej 	memset(&conf, 0, sizeof(conf));
22195a65560Sthorpej 	strncpy(conf.iface.blue_ifname, ifinfo->ifname, IFNAMSIZ);
22295a65560Sthorpej 	conf.blue_max_pmark = blue_ifinfo->max_pmark;
22395a65560Sthorpej 	conf.blue_hold_time = blue_ifinfo->hold_time;
22495a65560Sthorpej 	conf.blue_limit     = blue_ifinfo->qlimit;
22595a65560Sthorpej 	conf.blue_pkttime   = blue_ifinfo->pkttime;
22695a65560Sthorpej 	conf.blue_flags     = blue_ifinfo->flags;
22795a65560Sthorpej 	if (ioctl(blue_fd, BLUE_CONFIG, &conf) < 0)
22895a65560Sthorpej 		return (QOPERR_SYSCALL);
22995a65560Sthorpej 
23095a65560Sthorpej #if 1
231d5e1f166Sitojun 	LOG(LOG_INFO, 0, "blue attached to %s", iface.blue_ifname);
23295a65560Sthorpej #endif
23395a65560Sthorpej 	return (0);
23495a65560Sthorpej }
23595a65560Sthorpej 
23695a65560Sthorpej static int
blue_detach(struct ifinfo * ifinfo)23795a65560Sthorpej blue_detach(struct ifinfo *ifinfo)
23895a65560Sthorpej {
23995a65560Sthorpej 	struct blue_interface iface;
24095a65560Sthorpej 
24195a65560Sthorpej 	memset(&iface, 0, sizeof(iface));
24295a65560Sthorpej 	strncpy(iface.blue_ifname, ifinfo->ifname, IFNAMSIZ);
24395a65560Sthorpej 
24495a65560Sthorpej 	if (ioctl(blue_fd, BLUE_IF_DETACH, &iface) < 0)
24595a65560Sthorpej 		return (QOPERR_SYSCALL);
24695a65560Sthorpej 
24795a65560Sthorpej 	if (--blue_refcount == 0) {
24895a65560Sthorpej 		close(blue_fd);
24995a65560Sthorpej 		blue_fd = -1;
25095a65560Sthorpej 	}
25195a65560Sthorpej 	return (0);
25295a65560Sthorpej }
25395a65560Sthorpej 
25495a65560Sthorpej static int
blue_enable(struct ifinfo * ifinfo)25595a65560Sthorpej blue_enable(struct ifinfo *ifinfo)
25695a65560Sthorpej {
25795a65560Sthorpej 	struct blue_interface iface;
25895a65560Sthorpej 
25995a65560Sthorpej 	memset(&iface, 0, sizeof(iface));
26095a65560Sthorpej 	strncpy(iface.blue_ifname, ifinfo->ifname, IFNAMSIZ);
26195a65560Sthorpej 
26295a65560Sthorpej 	if (ioctl(blue_fd, BLUE_ENABLE, &iface) < 0)
26395a65560Sthorpej 		return (QOPERR_SYSCALL);
26495a65560Sthorpej 	return (0);
26595a65560Sthorpej }
26695a65560Sthorpej 
26795a65560Sthorpej static int
blue_disable(struct ifinfo * ifinfo)26895a65560Sthorpej blue_disable(struct ifinfo *ifinfo)
26995a65560Sthorpej {
27095a65560Sthorpej 	struct blue_interface iface;
27195a65560Sthorpej 
27295a65560Sthorpej 	memset(&iface, 0, sizeof(iface));
27395a65560Sthorpej 	strncpy(iface.blue_ifname, ifinfo->ifname, IFNAMSIZ);
27495a65560Sthorpej 
27595a65560Sthorpej 	if (ioctl(blue_fd, BLUE_DISABLE, &iface) < 0)
27695a65560Sthorpej 		return (QOPERR_SYSCALL);
27795a65560Sthorpej 	return (0);
27895a65560Sthorpej }
279