Home | History | Annotate | Line # | Download | only in libaltq
      1 /*	$NetBSD: qop_red.c,v 1.5 2002/03/05 04:11:53 itojun Exp $	*/
      2 /*	$KAME: qop_red.c,v 1.6 2001/12/03 08:20:55 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 #include <sys/sockio.h>
     32 #include <sys/ioctl.h>
     33 #include <sys/fcntl.h>
     34 #include <net/if.h>
     35 #include <netinet/in.h>
     36 #include <arpa/inet.h>
     37 
     38 #include <stdio.h>
     39 #include <stdlib.h>
     40 #include <unistd.h>
     41 #include <stddef.h>
     42 #include <string.h>
     43 #include <ctype.h>
     44 #include <errno.h>
     45 #include <syslog.h>
     46 #include <netdb.h>
     47 
     48 #include <altq/altq.h>
     49 #include <altq/altq_red.h>
     50 #include "altq_qop.h"
     51 #include "qop_red.h"
     52 
     53 static int red_attach(struct ifinfo *);
     54 static int red_detach(struct ifinfo *);
     55 static int red_enable(struct ifinfo *);
     56 static int red_disable(struct ifinfo *);
     57 
     58 #define RED_DEVICE	"/dev/altq/red"
     59 
     60 static int red_fd = -1;
     61 static int red_refcount = 0;
     62 
     63 static struct qdisc_ops red_qdisc = {
     64 	ALTQT_RED,
     65 	"red",
     66 	red_attach,
     67 	red_detach,
     68 	NULL,			/* clear */
     69 	red_enable,
     70 	red_disable,
     71 	NULL,			/* add class */
     72 	NULL,			/* modify class */
     73 	NULL,			/* delete class */
     74 	NULL,			/* add filter */
     75 	NULL			/* delete filter */
     76 };
     77 
     78 /*
     79  * parser interface
     80  */
     81 #define EQUAL(s1, s2)	(strcmp((s1), (s2)) == 0)
     82 
     83 int
     84 red_interface_parser(const char *ifname, int argc, char **argv)
     85 {
     86 	u_int  	bandwidth = 100000000;	/* 100Mbps */
     87 	u_int	tbrsize = 0;
     88 	int	weight = 0;		/* 0: use default */
     89 	int	inv_pmax = 0;		/* 0: use default */
     90 	int	th_min = 0;		/* 0: use default */
     91 	int	th_max = 0;		/* 0: use default */
     92 	int	qlimit = 60;
     93 	int	pkttime = 0;
     94 	int	flags = 0;
     95 	int	packet_size = 1000;
     96 
     97 	/*
     98 	 * process options
     99 	 */
    100 	while (argc > 0) {
    101 		if (EQUAL(*argv, "bandwidth")) {
    102 			argc--; argv++;
    103 			if (argc > 0)
    104 				bandwidth = atobps(*argv);
    105 		} else if (EQUAL(*argv, "tbrsize")) {
    106 			argc--; argv++;
    107 			if (argc > 0)
    108 				tbrsize = atobytes(*argv);
    109 		} else if (EQUAL(*argv, "packetsize")) {
    110 			argc--; argv++;
    111 			if (argc > 0)
    112 				packet_size = atobytes(*argv);
    113 		} else if (EQUAL(*argv, "weight")) {
    114 			argc--; argv++;
    115 			if (argc > 0)
    116 				weight = (int)strtol(*argv, NULL, 0);
    117 		} else if (EQUAL(*argv, "qlimit")) {
    118 			argc--; argv++;
    119 			if (argc > 0)
    120 				qlimit = (int)strtol(*argv, NULL, 0);
    121 		} else if (EQUAL(*argv, "thmin")) {
    122 			argc--; argv++;
    123 			if (argc > 0)
    124 				th_min = (int)strtol(*argv, NULL, 0);
    125 		} else if (EQUAL(*argv, "thmax")) {
    126 			argc--; argv++;
    127 			if (argc > 0)
    128 				th_max = (int)strtol(*argv, NULL, 0);
    129 		} else if (EQUAL(*argv, "invpmax")) {
    130 			argc--; argv++;
    131 			if (argc > 0)
    132 				inv_pmax = (int)strtol(*argv, NULL, 0);
    133 		} else if (EQUAL(*argv, "red")) {
    134 			/* just skip */
    135 		} else if (EQUAL(*argv, "ecn")) {
    136 			flags |= REDF_ECN;
    137 		} else if (EQUAL(*argv, "flowvalve")) {
    138 			flags |= REDF_FLOWVALVE;
    139 		} else {
    140 			LOG(LOG_ERR, 0, "Unknown keyword '%s'", *argv);
    141 			return (0);
    142 		}
    143 		argc--; argv++;
    144 	}
    145 
    146 	if (qcmd_tbr_register(ifname, bandwidth, tbrsize) != 0)
    147 		return (0);
    148 
    149 	pkttime = packet_size * 8 * 1000 / (bandwidth / 1000);
    150 	if (weight != 0) {
    151 		/* check if weight is power of 2 */
    152 		int i, w;
    153 
    154 		w = weight;
    155 		for (i = 0; w > 1; i++)
    156 			w = w >> 1;
    157 		w = 1 << i;
    158 		if (weight != w) {
    159 			LOG(LOG_ERR, 0, "weight %d: should be power of 2",
    160 			    weight);
    161 			return (0);
    162 		}
    163 	}
    164 
    165 	if (qcmd_red_add_if(ifname, bandwidth, weight, inv_pmax,
    166 			    th_min, th_max, qlimit, pkttime, flags) != 0)
    167 		return (0);
    168 	return (1);
    169 }
    170 
    171 /*
    172  * qcmd api
    173  */
    174 int
    175 qcmd_red_add_if(const char *ifname, u_int bandwidth, int weight,
    176 		int inv_pmax, int th_min, int th_max, int qlimit,
    177 		int pkttime, int flags)
    178 {
    179 	int error;
    180 
    181 	error = qop_red_add_if(NULL, ifname, bandwidth, weight, inv_pmax,
    182 			       th_min, th_max, qlimit, pkttime, flags);
    183 	if (error != 0)
    184 		LOG(LOG_ERR, errno, "%s: can't add red on interface '%s'",
    185 		    qoperror(error), ifname);
    186 	return (error);
    187 }
    188 
    189 /*
    190  * qop api
    191  */
    192 int
    193 qop_red_add_if(struct ifinfo **rp, const char *ifname,
    194 	       u_int bandwidth, int weight, int inv_pmax, int th_min,
    195 	       int th_max, int qlimit, int pkttime, int flags)
    196 {
    197 	struct ifinfo *ifinfo = NULL;
    198 	struct red_ifinfo *red_ifinfo;
    199 	int error;
    200 
    201 	if ((red_ifinfo = calloc(1, sizeof(*red_ifinfo))) == NULL)
    202 		return (QOPERR_NOMEM);
    203 	red_ifinfo->weight   = weight;
    204 	red_ifinfo->inv_pmax = inv_pmax;
    205 	red_ifinfo->th_min   = th_min;
    206 	red_ifinfo->th_max   = th_max;
    207 	red_ifinfo->qlimit   = qlimit;
    208 	red_ifinfo->pkttime  = pkttime;
    209 	red_ifinfo->flags    = flags;
    210 
    211 	error = qop_add_if(&ifinfo, ifname, bandwidth,
    212 			   &red_qdisc, red_ifinfo);
    213 	if (error != 0) {
    214 		free(red_ifinfo);
    215 		return (error);
    216 	}
    217 
    218 	if (rp != NULL)
    219 		*rp = ifinfo;
    220 	return (0);
    221 }
    222 
    223 /*
    224  *  system call interfaces for qdisc_ops
    225  */
    226 static int
    227 red_attach(struct ifinfo *ifinfo)
    228 {
    229 	struct red_interface iface;
    230 	struct red_ifinfo *red_ifinfo;
    231 	struct red_conf conf;
    232 
    233 	if (red_fd < 0 &&
    234 	    (red_fd = open(RED_DEVICE, O_RDWR)) < 0 &&
    235 	    (red_fd = open_module(RED_DEVICE, O_RDWR)) < 0) {
    236 		LOG(LOG_ERR, errno, "RED open");
    237 		return (QOPERR_SYSCALL);
    238 	}
    239 
    240 	red_refcount++;
    241 	memset(&iface, 0, sizeof(iface));
    242 	strncpy(iface.red_ifname, ifinfo->ifname, IFNAMSIZ);
    243 
    244 	if (ioctl(red_fd, RED_IF_ATTACH, &iface) < 0)
    245 		return (QOPERR_SYSCALL);
    246 
    247 	/* set red parameters */
    248 	red_ifinfo = (struct red_ifinfo *)ifinfo->private;
    249 	memset(&conf, 0, sizeof(conf));
    250 	strncpy(conf.iface.red_ifname, ifinfo->ifname, IFNAMSIZ);
    251 	conf.red_weight	  = red_ifinfo->weight;
    252 	conf.red_inv_pmax = red_ifinfo->inv_pmax;
    253 	conf.red_thmin    = red_ifinfo->th_min;
    254 	conf.red_thmax    = red_ifinfo->th_max;
    255 	conf.red_limit    = red_ifinfo->qlimit;
    256 	conf.red_flags    = red_ifinfo->flags;
    257 	if (ioctl(red_fd, RED_CONFIG, &conf) < 0)
    258 		return (QOPERR_SYSCALL);
    259 
    260 #if 1
    261 	LOG(LOG_INFO, 0, "red attached to %s", iface.red_ifname);
    262 #endif
    263 	return (0);
    264 }
    265 
    266 static int
    267 red_detach(struct ifinfo *ifinfo)
    268 {
    269 	struct red_interface iface;
    270 
    271 	memset(&iface, 0, sizeof(iface));
    272 	strncpy(iface.red_ifname, ifinfo->ifname, IFNAMSIZ);
    273 
    274 	if (ioctl(red_fd, RED_IF_DETACH, &iface) < 0)
    275 		return (QOPERR_SYSCALL);
    276 
    277 	if (--red_refcount == 0) {
    278 		close(red_fd);
    279 		red_fd = -1;
    280 	}
    281 	return (0);
    282 }
    283 
    284 static int
    285 red_enable(struct ifinfo *ifinfo)
    286 {
    287 	struct red_interface iface;
    288 
    289 	memset(&iface, 0, sizeof(iface));
    290 	strncpy(iface.red_ifname, ifinfo->ifname, IFNAMSIZ);
    291 
    292 	if (ioctl(red_fd, RED_ENABLE, &iface) < 0)
    293 		return (QOPERR_SYSCALL);
    294 	return (0);
    295 }
    296 
    297 static int
    298 red_disable(struct ifinfo *ifinfo)
    299 {
    300 	struct red_interface iface;
    301 
    302 	memset(&iface, 0, sizeof(iface));
    303 	strncpy(iface.red_ifname, ifinfo->ifname, IFNAMSIZ);
    304 
    305 	if (ioctl(red_fd, RED_DISABLE, &iface) < 0)
    306 		return (QOPERR_SYSCALL);
    307 	return (0);
    308 }
    309