xref: /netbsd-src/usr.sbin/altq/libaltq/qop_red.c (revision 69881cf6b8dc615ac34107b2f982835e042159e3)
1*69881cf6Sitojun /*	$NetBSD: qop_red.c,v 1.5 2002/03/05 04:11:53 itojun Exp $	*/
2*69881cf6Sitojun /*	$KAME: qop_red.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_red.h>
5095a65560Sthorpej #include "altq_qop.h"
5195a65560Sthorpej #include "qop_red.h"
5295a65560Sthorpej 
538b8734d2Sitojun static int red_attach(struct ifinfo *);
548b8734d2Sitojun static int red_detach(struct ifinfo *);
558b8734d2Sitojun static int red_enable(struct ifinfo *);
568b8734d2Sitojun static int red_disable(struct ifinfo *);
5795a65560Sthorpej 
5895a65560Sthorpej #define RED_DEVICE	"/dev/altq/red"
5995a65560Sthorpej 
6095a65560Sthorpej static int red_fd = -1;
6195a65560Sthorpej static int red_refcount = 0;
6295a65560Sthorpej 
6395a65560Sthorpej static struct qdisc_ops red_qdisc = {
6495a65560Sthorpej 	ALTQT_RED,
6595a65560Sthorpej 	"red",
6695a65560Sthorpej 	red_attach,
6795a65560Sthorpej 	red_detach,
6895a65560Sthorpej 	NULL,			/* clear */
6995a65560Sthorpej 	red_enable,
7095a65560Sthorpej 	red_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
red_interface_parser(const char * ifname,int argc,char ** argv)8495a65560Sthorpej red_interface_parser(const char *ifname, int argc, char **argv)
8595a65560Sthorpej {
8695a65560Sthorpej 	u_int  	bandwidth = 100000000;	/* 100Mbps */
8795a65560Sthorpej 	u_int	tbrsize = 0;
8895a65560Sthorpej 	int	weight = 0;		/* 0: use default */
8995a65560Sthorpej 	int	inv_pmax = 0;		/* 0: use default */
9095a65560Sthorpej 	int	th_min = 0;		/* 0: use default */
9195a65560Sthorpej 	int	th_max = 0;		/* 0: use default */
9295a65560Sthorpej 	int	qlimit = 60;
9395a65560Sthorpej 	int	pkttime = 0;
9495a65560Sthorpej 	int	flags = 0;
9595a65560Sthorpej 	int	packet_size = 1000;
9695a65560Sthorpej 
9795a65560Sthorpej 	/*
9895a65560Sthorpej 	 * process options
9995a65560Sthorpej 	 */
10095a65560Sthorpej 	while (argc > 0) {
10195a65560Sthorpej 		if (EQUAL(*argv, "bandwidth")) {
10295a65560Sthorpej 			argc--; argv++;
10395a65560Sthorpej 			if (argc > 0)
10495a65560Sthorpej 				bandwidth = atobps(*argv);
10595a65560Sthorpej 		} else if (EQUAL(*argv, "tbrsize")) {
10695a65560Sthorpej 			argc--; argv++;
10795a65560Sthorpej 			if (argc > 0)
10895a65560Sthorpej 				tbrsize = atobytes(*argv);
10995a65560Sthorpej 		} else if (EQUAL(*argv, "packetsize")) {
11095a65560Sthorpej 			argc--; argv++;
11195a65560Sthorpej 			if (argc > 0)
11295a65560Sthorpej 				packet_size = atobytes(*argv);
11395a65560Sthorpej 		} else if (EQUAL(*argv, "weight")) {
11495a65560Sthorpej 			argc--; argv++;
11595a65560Sthorpej 			if (argc > 0)
11695a65560Sthorpej 				weight = (int)strtol(*argv, NULL, 0);
11795a65560Sthorpej 		} else if (EQUAL(*argv, "qlimit")) {
11895a65560Sthorpej 			argc--; argv++;
11995a65560Sthorpej 			if (argc > 0)
12095a65560Sthorpej 				qlimit = (int)strtol(*argv, NULL, 0);
12195a65560Sthorpej 		} else if (EQUAL(*argv, "thmin")) {
12295a65560Sthorpej 			argc--; argv++;
12395a65560Sthorpej 			if (argc > 0)
12495a65560Sthorpej 				th_min = (int)strtol(*argv, NULL, 0);
12595a65560Sthorpej 		} else if (EQUAL(*argv, "thmax")) {
12695a65560Sthorpej 			argc--; argv++;
12795a65560Sthorpej 			if (argc > 0)
12895a65560Sthorpej 				th_max = (int)strtol(*argv, NULL, 0);
12995a65560Sthorpej 		} else if (EQUAL(*argv, "invpmax")) {
13095a65560Sthorpej 			argc--; argv++;
13195a65560Sthorpej 			if (argc > 0)
13295a65560Sthorpej 				inv_pmax = (int)strtol(*argv, NULL, 0);
13395a65560Sthorpej 		} else if (EQUAL(*argv, "red")) {
13495a65560Sthorpej 			/* just skip */
13595a65560Sthorpej 		} else if (EQUAL(*argv, "ecn")) {
13695a65560Sthorpej 			flags |= REDF_ECN;
13795a65560Sthorpej 		} else if (EQUAL(*argv, "flowvalve")) {
13895a65560Sthorpej 			flags |= REDF_FLOWVALVE;
13995a65560Sthorpej 		} else {
140*69881cf6Sitojun 			LOG(LOG_ERR, 0, "Unknown keyword '%s'", *argv);
14195a65560Sthorpej 			return (0);
14295a65560Sthorpej 		}
14395a65560Sthorpej 		argc--; argv++;
14495a65560Sthorpej 	}
14595a65560Sthorpej 
14695a65560Sthorpej 	if (qcmd_tbr_register(ifname, bandwidth, tbrsize) != 0)
14795a65560Sthorpej 		return (0);
14895a65560Sthorpej 
14995a65560Sthorpej 	pkttime = packet_size * 8 * 1000 / (bandwidth / 1000);
15095a65560Sthorpej 	if (weight != 0) {
15195a65560Sthorpej 		/* check if weight is power of 2 */
15295a65560Sthorpej 		int i, w;
15395a65560Sthorpej 
15495a65560Sthorpej 		w = weight;
15595a65560Sthorpej 		for (i = 0; w > 1; i++)
15695a65560Sthorpej 			w = w >> 1;
15795a65560Sthorpej 		w = 1 << i;
15895a65560Sthorpej 		if (weight != w) {
15995a65560Sthorpej 			LOG(LOG_ERR, 0, "weight %d: should be power of 2",
16095a65560Sthorpej 			    weight);
16195a65560Sthorpej 			return (0);
16295a65560Sthorpej 		}
16395a65560Sthorpej 	}
16495a65560Sthorpej 
16595a65560Sthorpej 	if (qcmd_red_add_if(ifname, bandwidth, weight, inv_pmax,
16695a65560Sthorpej 			    th_min, th_max, qlimit, pkttime, flags) != 0)
16795a65560Sthorpej 		return (0);
16895a65560Sthorpej 	return (1);
16995a65560Sthorpej }
17095a65560Sthorpej 
17195a65560Sthorpej /*
17295a65560Sthorpej  * qcmd api
17395a65560Sthorpej  */
17495a65560Sthorpej int
qcmd_red_add_if(const char * ifname,u_int bandwidth,int weight,int inv_pmax,int th_min,int th_max,int qlimit,int pkttime,int flags)17595a65560Sthorpej qcmd_red_add_if(const char *ifname, u_int bandwidth, int weight,
17695a65560Sthorpej 		int inv_pmax, int th_min, int th_max, int qlimit,
17795a65560Sthorpej 		int pkttime, int flags)
17895a65560Sthorpej {
17995a65560Sthorpej 	int error;
18095a65560Sthorpej 
18195a65560Sthorpej 	error = qop_red_add_if(NULL, ifname, bandwidth, weight, inv_pmax,
18295a65560Sthorpej 			       th_min, th_max, qlimit, pkttime, flags);
18395a65560Sthorpej 	if (error != 0)
184d5e1f166Sitojun 		LOG(LOG_ERR, errno, "%s: can't add red on interface '%s'",
18595a65560Sthorpej 		    qoperror(error), ifname);
18695a65560Sthorpej 	return (error);
18795a65560Sthorpej }
18895a65560Sthorpej 
18995a65560Sthorpej /*
19095a65560Sthorpej  * qop api
19195a65560Sthorpej  */
19295a65560Sthorpej int
qop_red_add_if(struct ifinfo ** rp,const char * ifname,u_int bandwidth,int weight,int inv_pmax,int th_min,int th_max,int qlimit,int pkttime,int flags)19395a65560Sthorpej qop_red_add_if(struct ifinfo **rp, const char *ifname,
19495a65560Sthorpej 	       u_int bandwidth, int weight, int inv_pmax, int th_min,
19595a65560Sthorpej 	       int th_max, int qlimit, int pkttime, int flags)
19695a65560Sthorpej {
19795a65560Sthorpej 	struct ifinfo *ifinfo = NULL;
19895a65560Sthorpej 	struct red_ifinfo *red_ifinfo;
19995a65560Sthorpej 	int error;
20095a65560Sthorpej 
20195a65560Sthorpej 	if ((red_ifinfo = calloc(1, sizeof(*red_ifinfo))) == NULL)
20295a65560Sthorpej 		return (QOPERR_NOMEM);
20395a65560Sthorpej 	red_ifinfo->weight   = weight;
20495a65560Sthorpej 	red_ifinfo->inv_pmax = inv_pmax;
20595a65560Sthorpej 	red_ifinfo->th_min   = th_min;
20695a65560Sthorpej 	red_ifinfo->th_max   = th_max;
20795a65560Sthorpej 	red_ifinfo->qlimit   = qlimit;
20895a65560Sthorpej 	red_ifinfo->pkttime  = pkttime;
20995a65560Sthorpej 	red_ifinfo->flags    = flags;
21095a65560Sthorpej 
21195a65560Sthorpej 	error = qop_add_if(&ifinfo, ifname, bandwidth,
21295a65560Sthorpej 			   &red_qdisc, red_ifinfo);
21395a65560Sthorpej 	if (error != 0) {
21495a65560Sthorpej 		free(red_ifinfo);
21595a65560Sthorpej 		return (error);
21695a65560Sthorpej 	}
21795a65560Sthorpej 
21895a65560Sthorpej 	if (rp != NULL)
21995a65560Sthorpej 		*rp = ifinfo;
22095a65560Sthorpej 	return (0);
22195a65560Sthorpej }
22295a65560Sthorpej 
22395a65560Sthorpej /*
22495a65560Sthorpej  *  system call interfaces for qdisc_ops
22595a65560Sthorpej  */
22695a65560Sthorpej static int
red_attach(struct ifinfo * ifinfo)22795a65560Sthorpej red_attach(struct ifinfo *ifinfo)
22895a65560Sthorpej {
22995a65560Sthorpej 	struct red_interface iface;
23095a65560Sthorpej 	struct red_ifinfo *red_ifinfo;
23195a65560Sthorpej 	struct red_conf conf;
23295a65560Sthorpej 
23395a65560Sthorpej 	if (red_fd < 0 &&
23495a65560Sthorpej 	    (red_fd = open(RED_DEVICE, O_RDWR)) < 0 &&
23595a65560Sthorpej 	    (red_fd = open_module(RED_DEVICE, O_RDWR)) < 0) {
236d5e1f166Sitojun 		LOG(LOG_ERR, errno, "RED open");
23795a65560Sthorpej 		return (QOPERR_SYSCALL);
23895a65560Sthorpej 	}
23995a65560Sthorpej 
24095a65560Sthorpej 	red_refcount++;
24195a65560Sthorpej 	memset(&iface, 0, sizeof(iface));
24295a65560Sthorpej 	strncpy(iface.red_ifname, ifinfo->ifname, IFNAMSIZ);
24395a65560Sthorpej 
24495a65560Sthorpej 	if (ioctl(red_fd, RED_IF_ATTACH, &iface) < 0)
24595a65560Sthorpej 		return (QOPERR_SYSCALL);
24695a65560Sthorpej 
24795a65560Sthorpej 	/* set red parameters */
24895a65560Sthorpej 	red_ifinfo = (struct red_ifinfo *)ifinfo->private;
24995a65560Sthorpej 	memset(&conf, 0, sizeof(conf));
25095a65560Sthorpej 	strncpy(conf.iface.red_ifname, ifinfo->ifname, IFNAMSIZ);
25195a65560Sthorpej 	conf.red_weight	  = red_ifinfo->weight;
25295a65560Sthorpej 	conf.red_inv_pmax = red_ifinfo->inv_pmax;
25395a65560Sthorpej 	conf.red_thmin    = red_ifinfo->th_min;
25495a65560Sthorpej 	conf.red_thmax    = red_ifinfo->th_max;
25595a65560Sthorpej 	conf.red_limit    = red_ifinfo->qlimit;
25695a65560Sthorpej 	conf.red_flags    = red_ifinfo->flags;
25795a65560Sthorpej 	if (ioctl(red_fd, RED_CONFIG, &conf) < 0)
25895a65560Sthorpej 		return (QOPERR_SYSCALL);
25995a65560Sthorpej 
26095a65560Sthorpej #if 1
261d5e1f166Sitojun 	LOG(LOG_INFO, 0, "red attached to %s", iface.red_ifname);
26295a65560Sthorpej #endif
26395a65560Sthorpej 	return (0);
26495a65560Sthorpej }
26595a65560Sthorpej 
26695a65560Sthorpej static int
red_detach(struct ifinfo * ifinfo)26795a65560Sthorpej red_detach(struct ifinfo *ifinfo)
26895a65560Sthorpej {
26995a65560Sthorpej 	struct red_interface iface;
27095a65560Sthorpej 
27195a65560Sthorpej 	memset(&iface, 0, sizeof(iface));
27295a65560Sthorpej 	strncpy(iface.red_ifname, ifinfo->ifname, IFNAMSIZ);
27395a65560Sthorpej 
27495a65560Sthorpej 	if (ioctl(red_fd, RED_IF_DETACH, &iface) < 0)
27595a65560Sthorpej 		return (QOPERR_SYSCALL);
27695a65560Sthorpej 
27795a65560Sthorpej 	if (--red_refcount == 0) {
27895a65560Sthorpej 		close(red_fd);
27995a65560Sthorpej 		red_fd = -1;
28095a65560Sthorpej 	}
28195a65560Sthorpej 	return (0);
28295a65560Sthorpej }
28395a65560Sthorpej 
28495a65560Sthorpej static int
red_enable(struct ifinfo * ifinfo)28595a65560Sthorpej red_enable(struct ifinfo *ifinfo)
28695a65560Sthorpej {
28795a65560Sthorpej 	struct red_interface iface;
28895a65560Sthorpej 
28995a65560Sthorpej 	memset(&iface, 0, sizeof(iface));
29095a65560Sthorpej 	strncpy(iface.red_ifname, ifinfo->ifname, IFNAMSIZ);
29195a65560Sthorpej 
29295a65560Sthorpej 	if (ioctl(red_fd, RED_ENABLE, &iface) < 0)
29395a65560Sthorpej 		return (QOPERR_SYSCALL);
29495a65560Sthorpej 	return (0);
29595a65560Sthorpej }
29695a65560Sthorpej 
29795a65560Sthorpej static int
red_disable(struct ifinfo * ifinfo)29895a65560Sthorpej red_disable(struct ifinfo *ifinfo)
29995a65560Sthorpej {
30095a65560Sthorpej 	struct red_interface iface;
30195a65560Sthorpej 
30295a65560Sthorpej 	memset(&iface, 0, sizeof(iface));
30395a65560Sthorpej 	strncpy(iface.red_ifname, ifinfo->ifname, IFNAMSIZ);
30495a65560Sthorpej 
30595a65560Sthorpej 	if (ioctl(red_fd, RED_DISABLE, &iface) < 0)
30695a65560Sthorpej 		return (QOPERR_SYSCALL);
30795a65560Sthorpej 	return (0);
30895a65560Sthorpej }
309