Home | History | Annotate | Line # | Download | only in libaltq
qop_wfq.c revision 1.4
      1 /*	$NetBSD: qop_wfq.c,v 1.4 2001/08/22 08:52:37 itojun Exp $	*/
      2 /*	$KAME: qop_wfq.c,v 1.5 2001/08/16 10:39:15 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_wfq.h>
     50 #include "altq_qop.h"
     51 #include "qop_wfq.h"
     52 
     53 static int wfq_attach(struct ifinfo *);
     54 static int wfq_detach(struct ifinfo *);
     55 static int wfq_enable(struct ifinfo *);
     56 static int wfq_disable(struct ifinfo *);
     57 
     58 #define WFQ_DEVICE	"/dev/altq/wfq"
     59 
     60 static int wfq_fd = -1;
     61 static int wfq_refcount = 0;
     62 
     63 static struct qdisc_ops wfq_qdisc = {
     64 	ALTQT_WFQ,
     65 	"wfq",
     66 	wfq_attach,
     67 	wfq_detach,
     68 	NULL,			/* clear */
     69 	wfq_enable,
     70 	wfq_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 wfq_interface_parser(const char *ifname, int argc, char **argv)
     85 {
     86 	u_int  	bandwidth = 100000000;	/* 100Mbps */
     87 	u_int	tbrsize = 0;
     88 	int	hash_policy = 0;	/* 0: use default */
     89 	int	nqueues = 0;		/* 0: use default */
     90 	int	qsize = 0;		/* 0: use default */
     91 
     92 	/*
     93 	 * process options
     94 	 */
     95 	while (argc > 0) {
     96 		if (EQUAL(*argv, "bandwidth")) {
     97 			argc--; argv++;
     98 			if (argc > 0)
     99 				bandwidth = atobps(*argv);
    100 		} else if (EQUAL(*argv, "tbrsize")) {
    101 			argc--; argv++;
    102 			if (argc > 0)
    103 				tbrsize = atobytes(*argv);
    104 		} else if (EQUAL(*argv, "nqueues")) {
    105 			argc--; argv++;
    106 			if (argc > 0)
    107 				nqueues = (int)strtol(*argv, NULL, 0);
    108 		} else if (EQUAL(*argv, "qsize")) {
    109 			argc--; argv++;
    110 			if (argc > 0)
    111 				qsize = atobytes(*argv);
    112 		} else if (EQUAL(*argv, "hash")) {
    113 			argc--; argv++;
    114 			if (argc > 0) {
    115 				if (EQUAL(*argv, "dstaddr"))
    116 					hash_policy = WFQ_HASH_DSTADDR;
    117 				else if (EQUAL(*argv, "full"))
    118 					hash_policy = WFQ_HASH_FULL;
    119 				else if (EQUAL(*argv, "srcport"))
    120 					hash_policy = WFQ_HASH_SRCPORT;
    121 				else {
    122 					LOG(LOG_ERR, 0,
    123 					    "Unknown hash policy '%s'",
    124 					    argv);
    125 					return (0);
    126 				}
    127 			}
    128 		} else if (EQUAL(*argv, "wfq")) {
    129 			/* just skip */
    130 		} else {
    131 			LOG(LOG_ERR, 0, "Unknown keyword '%s'", argv);
    132 			return (0);
    133 		}
    134 		argc--; argv++;
    135 	}
    136 
    137 	if (qcmd_tbr_register(ifname, bandwidth, tbrsize) != 0)
    138 		return (0);
    139 
    140 	if (qsize != 0 && qsize < 1500) {
    141 		LOG(LOG_ERR, 0, "qsize too small: %d bytes", qsize);
    142 		return (0);
    143 	}
    144 
    145 	if (qcmd_wfq_add_if(ifname, bandwidth,
    146 			    hash_policy, nqueues, qsize) != 0)
    147 		return (0);
    148 	return (1);
    149 }
    150 
    151 /*
    152  * qcmd api
    153  */
    154 int
    155 qcmd_wfq_add_if(const char *ifname, u_int bandwidth, int hash_policy,
    156 		int nqueues, int qsize)
    157 {
    158 	int error;
    159 
    160 	error = qop_wfq_add_if(NULL, ifname, bandwidth,
    161 			       hash_policy, nqueues, qsize);
    162 	if (error != 0)
    163 		LOG(LOG_ERR, errno, "%s: can't add wfq on interface '%s'",
    164 		    qoperror(error), ifname);
    165 	return (error);
    166 }
    167 
    168 /*
    169  * qop api
    170  */
    171 int
    172 qop_wfq_add_if(struct ifinfo **rp, const char *ifname, u_int bandwidth,
    173 	       int hash_policy, int nqueues, int qsize)
    174 {
    175 	struct ifinfo *ifinfo = NULL;
    176 	struct wfq_ifinfo *wfq_ifinfo;
    177 	int error;
    178 
    179 	if ((wfq_ifinfo = calloc(1, sizeof(*wfq_ifinfo))) == NULL)
    180 		return (QOPERR_NOMEM);
    181 	wfq_ifinfo->hash_policy = hash_policy;
    182 	wfq_ifinfo->nqueues     = nqueues;
    183 	wfq_ifinfo->qsize       = qsize;
    184 
    185 	error = qop_add_if(&ifinfo, ifname, bandwidth,
    186 			   &wfq_qdisc, wfq_ifinfo);
    187 	if (error != 0) {
    188 		free(wfq_ifinfo);
    189 		return (error);
    190 	}
    191 
    192 	if (rp != NULL)
    193 		*rp = ifinfo;
    194 	return (0);
    195 }
    196 
    197 /*
    198  *  system call interfaces for qdisc_ops
    199  */
    200 static int
    201 wfq_attach(struct ifinfo *ifinfo)
    202 {
    203 	struct wfq_interface iface;
    204 	struct wfq_ifinfo *wfq_ifinfo;
    205 	struct wfq_conf conf;
    206 
    207 	if (wfq_fd < 0 &&
    208 	    (wfq_fd = open(WFQ_DEVICE, O_RDWR)) < 0 &&
    209 	    (wfq_fd = open_module(WFQ_DEVICE, O_RDWR)) < 0) {
    210 		LOG(LOG_ERR, errno, "WFQ open");
    211 		return (QOPERR_SYSCALL);
    212 	}
    213 
    214 	wfq_refcount++;
    215 	memset(&iface, 0, sizeof(iface));
    216 	strncpy(iface.wfq_ifacename, ifinfo->ifname, IFNAMSIZ);
    217 
    218 	if (ioctl(wfq_fd, WFQ_IF_ATTACH, &iface) < 0)
    219 		return (QOPERR_SYSCALL);
    220 
    221 	/* set wfq parameters */
    222 	wfq_ifinfo = (struct wfq_ifinfo *)ifinfo->private;
    223 	if (wfq_ifinfo->hash_policy != 0 || wfq_ifinfo->nqueues != 0 ||
    224 	    wfq_ifinfo->qsize != 0) {
    225 		memset(&conf, 0, sizeof(conf));
    226 		strncpy(conf.iface.wfq_ifacename, ifinfo->ifname, IFNAMSIZ);
    227 		conf.hash_policy = wfq_ifinfo->hash_policy;
    228 		conf.nqueues     = wfq_ifinfo->nqueues;
    229 		conf.qlimit      = wfq_ifinfo->qsize;
    230 		if (ioctl(wfq_fd, WFQ_CONFIG, &conf) < 0) {
    231 			LOG(LOG_ERR, errno, "WFQ_CONFIG");
    232 			return (QOPERR_SYSCALL);
    233 		}
    234 	}
    235 #if 1
    236 	LOG(LOG_INFO, 0, "wfq attached to %s", iface.wfq_ifacename);
    237 #endif
    238 	return (0);
    239 }
    240 
    241 static int
    242 wfq_detach(struct ifinfo *ifinfo)
    243 {
    244 	struct wfq_interface iface;
    245 
    246 	memset(&iface, 0, sizeof(iface));
    247 	strncpy(iface.wfq_ifacename, ifinfo->ifname, IFNAMSIZ);
    248 
    249 	if (ioctl(wfq_fd, WFQ_IF_DETACH, &iface) < 0)
    250 		return (QOPERR_SYSCALL);
    251 
    252 	if (--wfq_refcount == 0) {
    253 		close(wfq_fd);
    254 		wfq_fd = -1;
    255 	}
    256 	return (0);
    257 }
    258 
    259 static int
    260 wfq_enable(struct ifinfo *ifinfo)
    261 {
    262 	struct wfq_interface iface;
    263 
    264 	memset(&iface, 0, sizeof(iface));
    265 	strncpy(iface.wfq_ifacename, ifinfo->ifname, IFNAMSIZ);
    266 
    267 	if (ioctl(wfq_fd, WFQ_ENABLE, &iface) < 0)
    268 		return (QOPERR_SYSCALL);
    269 	return (0);
    270 }
    271 
    272 static int
    273 wfq_disable(struct ifinfo *ifinfo)
    274 {
    275 	struct wfq_interface iface;
    276 
    277 	memset(&iface, 0, sizeof(iface));
    278 	strncpy(iface.wfq_ifacename, ifinfo->ifname, IFNAMSIZ);
    279 
    280 	if (ioctl(wfq_fd, WFQ_DISABLE, &iface) < 0)
    281 		return (QOPERR_SYSCALL);
    282 	return (0);
    283 }
    284