xref: /netbsd-src/usr.sbin/altq/libaltq/qop_rio.c (revision 69881cf6b8dc615ac34107b2f982835e042159e3)
1*69881cf6Sitojun /*	$NetBSD: qop_rio.c,v 1.5 2002/03/05 04:11:53 itojun Exp $	*/
2*69881cf6Sitojun /*	$KAME: qop_rio.c,v 1.6 2001/12/03 08:20:56 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_red.h>
5095a65560Sthorpej #include <altq/altq_rio.h>
5195a65560Sthorpej #include "altq_qop.h"
5295a65560Sthorpej #include "qop_rio.h"
5395a65560Sthorpej 
548b8734d2Sitojun static int rio_attach(struct ifinfo *);
558b8734d2Sitojun static int rio_detach(struct ifinfo *);
568b8734d2Sitojun static int rio_enable(struct ifinfo *);
578b8734d2Sitojun static int rio_disable(struct ifinfo *);
5895a65560Sthorpej 
5995a65560Sthorpej #define RIO_DEVICE	"/dev/altq/rio"
6095a65560Sthorpej 
6195a65560Sthorpej static int rio_fd = -1;
6295a65560Sthorpej static int rio_refcount = 0;
6395a65560Sthorpej 
6495a65560Sthorpej static struct qdisc_ops rio_qdisc = {
6595a65560Sthorpej 	ALTQT_RIO,
6695a65560Sthorpej 	"rio",
6795a65560Sthorpej 	rio_attach,
6895a65560Sthorpej 	rio_detach,
6995a65560Sthorpej 	NULL,			/* clear */
7095a65560Sthorpej 	rio_enable,
7195a65560Sthorpej 	rio_disable,
7295a65560Sthorpej 	NULL,			/* add class */
7395a65560Sthorpej 	NULL,			/* modify class */
7495a65560Sthorpej 	NULL,			/* delete class */
7595a65560Sthorpej 	NULL,			/* add filter */
7695a65560Sthorpej 	NULL			/* delete filter */
7795a65560Sthorpej };
7895a65560Sthorpej 
7995a65560Sthorpej /*
8095a65560Sthorpej  * parser interface
8195a65560Sthorpej  */
8295a65560Sthorpej #define EQUAL(s1, s2)	(strcmp((s1), (s2)) == 0)
8395a65560Sthorpej 
8495a65560Sthorpej int
rio_interface_parser(const char * ifname,int argc,char ** argv)8595a65560Sthorpej rio_interface_parser(const char *ifname, int argc, char **argv)
8695a65560Sthorpej {
8795a65560Sthorpej 	u_int  	bandwidth = 100000000;	/* 100Mbps */
8895a65560Sthorpej 	u_int	tbrsize = 0;
8995a65560Sthorpej 	int	weight = 0;		/* 0: use default */
9095a65560Sthorpej 	int	lo_inv_pmax = 0;	/* 0: use default */
9195a65560Sthorpej 	int	lo_th_min = 0;		/* 0: use default */
9295a65560Sthorpej 	int	lo_th_max = 0;		/* 0: use default */
9395a65560Sthorpej 	int	med_inv_pmax = 0;	/* 0: use default */
9495a65560Sthorpej 	int	med_th_min = 0;		/* 0: use default */
9595a65560Sthorpej 	int	med_th_max = 0;		/* 0: use default */
9695a65560Sthorpej 	int	hi_inv_pmax = 0;	/* 0: use default */
9795a65560Sthorpej 	int	hi_th_min = 0;		/* 0: use default */
9895a65560Sthorpej 	int	hi_th_max = 0;		/* 0: use default */
9995a65560Sthorpej 	int	qlimit = 60;
10095a65560Sthorpej 	int	pkttime = 0;
10195a65560Sthorpej 	int	flags = 0;
10295a65560Sthorpej 	int	packet_size = 1000;
10395a65560Sthorpej 
10495a65560Sthorpej 	/*
10595a65560Sthorpej 	 * process options
10695a65560Sthorpej 	 */
10795a65560Sthorpej 	while (argc > 0) {
10895a65560Sthorpej 		if (EQUAL(*argv, "bandwidth")) {
10995a65560Sthorpej 			argc--; argv++;
11095a65560Sthorpej 			if (argc > 0)
11195a65560Sthorpej 				bandwidth = atobps(*argv);
11295a65560Sthorpej 		} else if (EQUAL(*argv, "tbrsize")) {
11395a65560Sthorpej 			argc--; argv++;
11495a65560Sthorpej 			if (argc > 0)
11595a65560Sthorpej 				tbrsize = atobytes(*argv);
11695a65560Sthorpej 		} else if (EQUAL(*argv, "packetsize")) {
11795a65560Sthorpej 			argc--; argv++;
11895a65560Sthorpej 			if (argc > 0)
11995a65560Sthorpej 				packet_size = atobytes(*argv);
12095a65560Sthorpej 		} else if (EQUAL(*argv, "weight")) {
12195a65560Sthorpej 			argc--; argv++;
12295a65560Sthorpej 			if (argc > 0)
12395a65560Sthorpej 				weight = (int)strtol(*argv, NULL, 0);
12495a65560Sthorpej 		} else if (EQUAL(*argv, "qlimit")) {
12595a65560Sthorpej 			argc--; argv++;
12695a65560Sthorpej 			if (argc > 0)
12795a65560Sthorpej 				qlimit = (int)strtol(*argv, NULL, 0);
12895a65560Sthorpej 		} else if (EQUAL(*argv, "lo_thmin")) {
12995a65560Sthorpej 			argc--; argv++;
13095a65560Sthorpej 			if (argc > 0)
13195a65560Sthorpej 				lo_th_min = (int)strtol(*argv, NULL, 0);
13295a65560Sthorpej 		} else if (EQUAL(*argv, "lo_thmax")) {
13395a65560Sthorpej 			argc--; argv++;
13495a65560Sthorpej 			if (argc > 0)
13595a65560Sthorpej 				lo_th_max = (int)strtol(*argv, NULL, 0);
13695a65560Sthorpej 		} else if (EQUAL(*argv, "lo_invpmax")) {
13795a65560Sthorpej 			argc--; argv++;
13895a65560Sthorpej 			if (argc > 0)
13995a65560Sthorpej 				lo_inv_pmax = (int)strtol(*argv, NULL, 0);
14095a65560Sthorpej 		} else if (EQUAL(*argv, "med_thmin")) {
14195a65560Sthorpej 			argc--; argv++;
14295a65560Sthorpej 			if (argc > 0)
14395a65560Sthorpej 				med_th_min = (int)strtol(*argv, NULL, 0);
14495a65560Sthorpej 		} else if (EQUAL(*argv, "med_thmax")) {
14595a65560Sthorpej 			argc--; argv++;
14695a65560Sthorpej 			if (argc > 0)
14795a65560Sthorpej 				med_th_max = (int)strtol(*argv, NULL, 0);
14895a65560Sthorpej 		} else if (EQUAL(*argv, "med_invpmax")) {
14995a65560Sthorpej 			argc--; argv++;
15095a65560Sthorpej 			if (argc > 0)
15195a65560Sthorpej 				med_inv_pmax = (int)strtol(*argv, NULL, 0);
15295a65560Sthorpej 		} else if (EQUAL(*argv, "hi_thmin")) {
15395a65560Sthorpej 			argc--; argv++;
15495a65560Sthorpej 			if (argc > 0)
15595a65560Sthorpej 				hi_th_min = (int)strtol(*argv, NULL, 0);
15695a65560Sthorpej 		} else if (EQUAL(*argv, "hi_thmax")) {
15795a65560Sthorpej 			argc--; argv++;
15895a65560Sthorpej 			if (argc > 0)
15995a65560Sthorpej 				hi_th_max = (int)strtol(*argv, NULL, 0);
16095a65560Sthorpej 		} else if (EQUAL(*argv, "hi_invpmax")) {
16195a65560Sthorpej 			argc--; argv++;
16295a65560Sthorpej 			if (argc > 0)
16395a65560Sthorpej 				hi_inv_pmax = (int)strtol(*argv, NULL, 0);
16495a65560Sthorpej 		} else if (EQUAL(*argv, "rio")) {
16595a65560Sthorpej 			/* just skip */
16695a65560Sthorpej 		} else if (EQUAL(*argv, "ecn")) {
16795a65560Sthorpej 			flags |= RIOF_ECN;
16895a65560Sthorpej 		} else {
169*69881cf6Sitojun 			LOG(LOG_ERR, 0, "Unknown keyword '%s'", *argv);
17095a65560Sthorpej 			return (0);
17195a65560Sthorpej 		}
17295a65560Sthorpej 		argc--; argv++;
17395a65560Sthorpej 	}
17495a65560Sthorpej 
17595a65560Sthorpej 	if (qcmd_tbr_register(ifname, bandwidth, tbrsize) != 0)
17695a65560Sthorpej 		return (0);
17795a65560Sthorpej 
17895a65560Sthorpej 	pkttime = packet_size * 8 * 1000 / (bandwidth / 1000);
17995a65560Sthorpej 	if (weight != 0) {
18095a65560Sthorpej 		/* check if weight is power of 2 */
18195a65560Sthorpej 		int i, w;
18295a65560Sthorpej 
18395a65560Sthorpej 		w = weight;
18495a65560Sthorpej 		for (i = 0; w > 1; i++)
18595a65560Sthorpej 			w = w >> 1;
18695a65560Sthorpej 		w = 1 << i;
18795a65560Sthorpej 		if (weight != w) {
18895a65560Sthorpej 			LOG(LOG_ERR, 0, "weight %d: should be power of 2",
18995a65560Sthorpej 			    weight);
19095a65560Sthorpej 			return (0);
19195a65560Sthorpej 		}
19295a65560Sthorpej 	}
19395a65560Sthorpej 
19495a65560Sthorpej 	if (qcmd_rio_add_if(ifname, bandwidth, weight,
19595a65560Sthorpej 			    lo_inv_pmax, lo_th_min, lo_th_max,
19695a65560Sthorpej 			    med_inv_pmax, med_th_min, med_th_max,
19795a65560Sthorpej 			    hi_inv_pmax, hi_th_min, hi_th_max,
19895a65560Sthorpej 			    qlimit, pkttime, flags) != 0)
19995a65560Sthorpej 		return (0);
20095a65560Sthorpej 	return (1);
20195a65560Sthorpej }
20295a65560Sthorpej 
20395a65560Sthorpej /*
20495a65560Sthorpej  * qcmd api
20595a65560Sthorpej  */
20695a65560Sthorpej int
qcmd_rio_add_if(const char * ifname,u_int bandwidth,int weight,int lo_inv_pmax,int lo_th_min,int lo_th_max,int med_inv_pmax,int med_th_min,int med_th_max,int hi_inv_pmax,int hi_th_min,int hi_th_max,int qlimit,int pkttime,int flags)20795a65560Sthorpej qcmd_rio_add_if(const char *ifname, u_int bandwidth, int weight,
20895a65560Sthorpej 		int lo_inv_pmax, int lo_th_min, int lo_th_max,
20995a65560Sthorpej 		int med_inv_pmax, int med_th_min, int med_th_max,
21095a65560Sthorpej 		int hi_inv_pmax, int hi_th_min, int hi_th_max,
21195a65560Sthorpej 		int qlimit, int pkttime, int flags)
21295a65560Sthorpej {
21395a65560Sthorpej 	struct redparams red_params[RIO_NDROPPREC];
21495a65560Sthorpej 	int error;
21595a65560Sthorpej 
21695a65560Sthorpej 	red_params[0].inv_pmax = lo_inv_pmax;
21795a65560Sthorpej 	red_params[0].th_min   = lo_th_min;
21895a65560Sthorpej 	red_params[0].th_max   = lo_th_max;
21995a65560Sthorpej 	red_params[1].inv_pmax = med_inv_pmax;
22095a65560Sthorpej 	red_params[1].th_min   = med_th_min;
22195a65560Sthorpej 	red_params[1].th_max   = med_th_max;
22295a65560Sthorpej 	red_params[2].inv_pmax = hi_inv_pmax;
22395a65560Sthorpej 	red_params[2].th_min   = hi_th_min;
22495a65560Sthorpej 	red_params[2].th_max   = hi_th_max;
22595a65560Sthorpej 
22695a65560Sthorpej 	error = qop_rio_add_if(NULL, ifname, bandwidth, weight, red_params,
22795a65560Sthorpej 			       qlimit, pkttime, flags);
22895a65560Sthorpej 	if (error != 0)
229d5e1f166Sitojun 		LOG(LOG_ERR, errno, "%s: can't add rio on interface '%s'",
23095a65560Sthorpej 		    qoperror(error), ifname);
23195a65560Sthorpej 	return (error);
23295a65560Sthorpej }
23395a65560Sthorpej 
23495a65560Sthorpej /*
23595a65560Sthorpej  * qop api
23695a65560Sthorpej  */
23795a65560Sthorpej int
qop_rio_add_if(struct ifinfo ** rp,const char * ifname,u_int bandwidth,int weight,struct redparams * red_params,int qlimit,int pkttime,int flags)23895a65560Sthorpej qop_rio_add_if(struct ifinfo **rp, const char *ifname,
23995a65560Sthorpej 	       u_int bandwidth, int weight, struct redparams *red_params,
24095a65560Sthorpej 	       int qlimit, int pkttime, int flags)
24195a65560Sthorpej {
24295a65560Sthorpej 	struct ifinfo *ifinfo = NULL;
24395a65560Sthorpej 	struct rio_ifinfo *rio_ifinfo;
24495a65560Sthorpej 	int i, error;
24595a65560Sthorpej 
24695a65560Sthorpej 	if ((rio_ifinfo = calloc(1, sizeof(*rio_ifinfo))) == NULL)
24795a65560Sthorpej 		return (QOPERR_NOMEM);
24895a65560Sthorpej 	for (i = 0; i < RIO_NDROPPREC; i++)
24995a65560Sthorpej 		rio_ifinfo->red_params[i] = red_params[i];
25095a65560Sthorpej 	rio_ifinfo->weight   = weight;
25195a65560Sthorpej 	rio_ifinfo->qlimit   = qlimit;
25295a65560Sthorpej 	rio_ifinfo->pkttime  = pkttime;
25395a65560Sthorpej 	rio_ifinfo->flags    = flags;
25495a65560Sthorpej 
25595a65560Sthorpej 	error = qop_add_if(&ifinfo, ifname, bandwidth,
25695a65560Sthorpej 			   &rio_qdisc, rio_ifinfo);
25795a65560Sthorpej 	if (error != 0) {
25895a65560Sthorpej 		free(rio_ifinfo);
25995a65560Sthorpej 		return (error);
26095a65560Sthorpej 	}
26195a65560Sthorpej 
26295a65560Sthorpej 	if (rp != NULL)
26395a65560Sthorpej 		*rp = ifinfo;
26495a65560Sthorpej 	return (0);
26595a65560Sthorpej }
26695a65560Sthorpej 
26795a65560Sthorpej /*
26895a65560Sthorpej  *  system call interfaces for qdisc_ops
26995a65560Sthorpej  */
27095a65560Sthorpej static int
rio_attach(struct ifinfo * ifinfo)27195a65560Sthorpej rio_attach(struct ifinfo *ifinfo)
27295a65560Sthorpej {
27395a65560Sthorpej 	struct rio_interface iface;
27495a65560Sthorpej 	struct rio_ifinfo *rio_ifinfo;
27595a65560Sthorpej 	struct rio_conf conf;
27695a65560Sthorpej 	int i;
27795a65560Sthorpej 
27895a65560Sthorpej 	if (rio_fd < 0 &&
27995a65560Sthorpej 	    (rio_fd = open(RIO_DEVICE, O_RDWR)) < 0 &&
28095a65560Sthorpej 	    (rio_fd = open_module(RIO_DEVICE, O_RDWR)) < 0) {
281d5e1f166Sitojun 		LOG(LOG_ERR, errno, "RIO open");
28295a65560Sthorpej 		return (QOPERR_SYSCALL);
28395a65560Sthorpej 	}
28495a65560Sthorpej 
28595a65560Sthorpej 	rio_refcount++;
28695a65560Sthorpej 	memset(&iface, 0, sizeof(iface));
28795a65560Sthorpej 	strncpy(iface.rio_ifname, ifinfo->ifname, IFNAMSIZ);
28895a65560Sthorpej 
28995a65560Sthorpej 	if (ioctl(rio_fd, RIO_IF_ATTACH, &iface) < 0)
29095a65560Sthorpej 		return (QOPERR_SYSCALL);
29195a65560Sthorpej 
29295a65560Sthorpej 	/* set rio parameters */
29395a65560Sthorpej 	rio_ifinfo = (struct rio_ifinfo *)ifinfo->private;
29495a65560Sthorpej 	memset(&conf, 0, sizeof(conf));
29595a65560Sthorpej 	strncpy(conf.iface.rio_ifname, ifinfo->ifname, IFNAMSIZ);
29695a65560Sthorpej 	for (i = 0; i < RIO_NDROPPREC; i++)
29795a65560Sthorpej 		conf.q_params[i] = rio_ifinfo->red_params[i];
29895a65560Sthorpej 	conf.rio_weight	  = rio_ifinfo->weight;
29995a65560Sthorpej 	conf.rio_limit    = rio_ifinfo->qlimit;
30095a65560Sthorpej 	conf.rio_flags    = rio_ifinfo->flags;
30195a65560Sthorpej 	if (ioctl(rio_fd, RIO_CONFIG, &conf) < 0)
30295a65560Sthorpej 		return (QOPERR_SYSCALL);
30395a65560Sthorpej 
30495a65560Sthorpej #if 1
305d5e1f166Sitojun 	LOG(LOG_INFO, 0, "rio attached to %s", iface.rio_ifname);
30695a65560Sthorpej #endif
30795a65560Sthorpej 	return (0);
30895a65560Sthorpej }
30995a65560Sthorpej 
31095a65560Sthorpej static int
rio_detach(struct ifinfo * ifinfo)31195a65560Sthorpej rio_detach(struct ifinfo *ifinfo)
31295a65560Sthorpej {
31395a65560Sthorpej 	struct rio_interface iface;
31495a65560Sthorpej 
31595a65560Sthorpej 	memset(&iface, 0, sizeof(iface));
31695a65560Sthorpej 	strncpy(iface.rio_ifname, ifinfo->ifname, IFNAMSIZ);
31795a65560Sthorpej 
31895a65560Sthorpej 	if (ioctl(rio_fd, RIO_IF_DETACH, &iface) < 0)
31995a65560Sthorpej 		return (QOPERR_SYSCALL);
32095a65560Sthorpej 
32195a65560Sthorpej 	if (--rio_refcount == 0) {
32295a65560Sthorpej 		close(rio_fd);
32395a65560Sthorpej 		rio_fd = -1;
32495a65560Sthorpej 	}
32595a65560Sthorpej 	return (0);
32695a65560Sthorpej }
32795a65560Sthorpej 
32895a65560Sthorpej static int
rio_enable(struct ifinfo * ifinfo)32995a65560Sthorpej rio_enable(struct ifinfo *ifinfo)
33095a65560Sthorpej {
33195a65560Sthorpej 	struct rio_interface iface;
33295a65560Sthorpej 
33395a65560Sthorpej 	memset(&iface, 0, sizeof(iface));
33495a65560Sthorpej 	strncpy(iface.rio_ifname, ifinfo->ifname, IFNAMSIZ);
33595a65560Sthorpej 
33695a65560Sthorpej 	if (ioctl(rio_fd, RIO_ENABLE, &iface) < 0)
33795a65560Sthorpej 		return (QOPERR_SYSCALL);
33895a65560Sthorpej 	return (0);
33995a65560Sthorpej }
34095a65560Sthorpej 
34195a65560Sthorpej static int
rio_disable(struct ifinfo * ifinfo)34295a65560Sthorpej rio_disable(struct ifinfo *ifinfo)
34395a65560Sthorpej {
34495a65560Sthorpej 	struct rio_interface iface;
34595a65560Sthorpej 
34695a65560Sthorpej 	memset(&iface, 0, sizeof(iface));
34795a65560Sthorpej 	strncpy(iface.rio_ifname, ifinfo->ifname, IFNAMSIZ);
34895a65560Sthorpej 
34995a65560Sthorpej 	if (ioctl(rio_fd, RIO_DISABLE, &iface) < 0)
35095a65560Sthorpej 		return (QOPERR_SYSCALL);
35195a65560Sthorpej 	return (0);
35295a65560Sthorpej }
353