1*cba79e0eSjoerg /* $NetBSD: qop_wfq.c,v 1.6 2008/09/11 17:58:59 joerg Exp $ */
269881cf6Sitojun /* $KAME: qop_wfq.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_wfq.h>
5095a65560Sthorpej #include "altq_qop.h"
5195a65560Sthorpej #include "qop_wfq.h"
5295a65560Sthorpej
538b8734d2Sitojun static int wfq_attach(struct ifinfo *);
548b8734d2Sitojun static int wfq_detach(struct ifinfo *);
558b8734d2Sitojun static int wfq_enable(struct ifinfo *);
568b8734d2Sitojun static int wfq_disable(struct ifinfo *);
5795a65560Sthorpej
5895a65560Sthorpej #define WFQ_DEVICE "/dev/altq/wfq"
5995a65560Sthorpej
6095a65560Sthorpej static int wfq_fd = -1;
6195a65560Sthorpej static int wfq_refcount = 0;
6295a65560Sthorpej
6395a65560Sthorpej static struct qdisc_ops wfq_qdisc = {
6495a65560Sthorpej ALTQT_WFQ,
6595a65560Sthorpej "wfq",
6695a65560Sthorpej wfq_attach,
6795a65560Sthorpej wfq_detach,
6895a65560Sthorpej NULL, /* clear */
6995a65560Sthorpej wfq_enable,
7095a65560Sthorpej wfq_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
wfq_interface_parser(const char * ifname,int argc,char ** argv)8495a65560Sthorpej wfq_interface_parser(const char *ifname, int argc, char **argv)
8595a65560Sthorpej {
8695a65560Sthorpej u_int bandwidth = 100000000; /* 100Mbps */
8795a65560Sthorpej u_int tbrsize = 0;
8895a65560Sthorpej int hash_policy = 0; /* 0: use default */
8995a65560Sthorpej int nqueues = 0; /* 0: use default */
9095a65560Sthorpej int qsize = 0; /* 0: use default */
9195a65560Sthorpej
9295a65560Sthorpej /*
9395a65560Sthorpej * process options
9495a65560Sthorpej */
9595a65560Sthorpej while (argc > 0) {
9695a65560Sthorpej if (EQUAL(*argv, "bandwidth")) {
9795a65560Sthorpej argc--; argv++;
9895a65560Sthorpej if (argc > 0)
9995a65560Sthorpej bandwidth = atobps(*argv);
10095a65560Sthorpej } else if (EQUAL(*argv, "tbrsize")) {
10195a65560Sthorpej argc--; argv++;
10295a65560Sthorpej if (argc > 0)
10395a65560Sthorpej tbrsize = atobytes(*argv);
10495a65560Sthorpej } else if (EQUAL(*argv, "nqueues")) {
10595a65560Sthorpej argc--; argv++;
10695a65560Sthorpej if (argc > 0)
10795a65560Sthorpej nqueues = (int)strtol(*argv, NULL, 0);
10895a65560Sthorpej } else if (EQUAL(*argv, "qsize")) {
10995a65560Sthorpej argc--; argv++;
11095a65560Sthorpej if (argc > 0)
11195a65560Sthorpej qsize = atobytes(*argv);
11295a65560Sthorpej } else if (EQUAL(*argv, "hash")) {
11395a65560Sthorpej argc--; argv++;
11495a65560Sthorpej if (argc > 0) {
11595a65560Sthorpej if (EQUAL(*argv, "dstaddr"))
11695a65560Sthorpej hash_policy = WFQ_HASH_DSTADDR;
11795a65560Sthorpej else if (EQUAL(*argv, "full"))
11895a65560Sthorpej hash_policy = WFQ_HASH_FULL;
11995a65560Sthorpej else if (EQUAL(*argv, "srcport"))
12095a65560Sthorpej hash_policy = WFQ_HASH_SRCPORT;
121*cba79e0eSjoerg else if (EQUAL(*argv, "srcaddr"))
122*cba79e0eSjoerg hash_policy = WFQ_HASH_SRCADDR;
12395a65560Sthorpej else {
12495a65560Sthorpej LOG(LOG_ERR, 0,
12569881cf6Sitojun "Unknown hash policy '%s'", *argv);
12695a65560Sthorpej return (0);
12795a65560Sthorpej }
12895a65560Sthorpej }
12995a65560Sthorpej } else if (EQUAL(*argv, "wfq")) {
13095a65560Sthorpej /* just skip */
13195a65560Sthorpej } else {
13269881cf6Sitojun LOG(LOG_ERR, 0, "Unknown keyword '%s'", *argv);
13395a65560Sthorpej return (0);
13495a65560Sthorpej }
13595a65560Sthorpej argc--; argv++;
13695a65560Sthorpej }
13795a65560Sthorpej
13895a65560Sthorpej if (qcmd_tbr_register(ifname, bandwidth, tbrsize) != 0)
13995a65560Sthorpej return (0);
14095a65560Sthorpej
14195a65560Sthorpej if (qsize != 0 && qsize < 1500) {
14295a65560Sthorpej LOG(LOG_ERR, 0, "qsize too small: %d bytes", qsize);
14395a65560Sthorpej return (0);
14495a65560Sthorpej }
14595a65560Sthorpej
14695a65560Sthorpej if (qcmd_wfq_add_if(ifname, bandwidth,
14795a65560Sthorpej hash_policy, nqueues, qsize) != 0)
14895a65560Sthorpej return (0);
14995a65560Sthorpej return (1);
15095a65560Sthorpej }
15195a65560Sthorpej
15295a65560Sthorpej /*
15395a65560Sthorpej * qcmd api
15495a65560Sthorpej */
15595a65560Sthorpej int
qcmd_wfq_add_if(const char * ifname,u_int bandwidth,int hash_policy,int nqueues,int qsize)15695a65560Sthorpej qcmd_wfq_add_if(const char *ifname, u_int bandwidth, int hash_policy,
15795a65560Sthorpej int nqueues, int qsize)
15895a65560Sthorpej {
15995a65560Sthorpej int error;
16095a65560Sthorpej
16195a65560Sthorpej error = qop_wfq_add_if(NULL, ifname, bandwidth,
16295a65560Sthorpej hash_policy, nqueues, qsize);
16395a65560Sthorpej if (error != 0)
164d5e1f166Sitojun LOG(LOG_ERR, errno, "%s: can't add wfq on interface '%s'",
16595a65560Sthorpej qoperror(error), ifname);
16695a65560Sthorpej return (error);
16795a65560Sthorpej }
16895a65560Sthorpej
16995a65560Sthorpej /*
17095a65560Sthorpej * qop api
17195a65560Sthorpej */
17295a65560Sthorpej int
qop_wfq_add_if(struct ifinfo ** rp,const char * ifname,u_int bandwidth,int hash_policy,int nqueues,int qsize)17395a65560Sthorpej qop_wfq_add_if(struct ifinfo **rp, const char *ifname, u_int bandwidth,
17495a65560Sthorpej int hash_policy, int nqueues, int qsize)
17595a65560Sthorpej {
17695a65560Sthorpej struct ifinfo *ifinfo = NULL;
17795a65560Sthorpej struct wfq_ifinfo *wfq_ifinfo;
17895a65560Sthorpej int error;
17995a65560Sthorpej
18095a65560Sthorpej if ((wfq_ifinfo = calloc(1, sizeof(*wfq_ifinfo))) == NULL)
18195a65560Sthorpej return (QOPERR_NOMEM);
18295a65560Sthorpej wfq_ifinfo->hash_policy = hash_policy;
18395a65560Sthorpej wfq_ifinfo->nqueues = nqueues;
18495a65560Sthorpej wfq_ifinfo->qsize = qsize;
18595a65560Sthorpej
18695a65560Sthorpej error = qop_add_if(&ifinfo, ifname, bandwidth,
18795a65560Sthorpej &wfq_qdisc, wfq_ifinfo);
18895a65560Sthorpej if (error != 0) {
18995a65560Sthorpej free(wfq_ifinfo);
19095a65560Sthorpej return (error);
19195a65560Sthorpej }
19295a65560Sthorpej
19395a65560Sthorpej if (rp != NULL)
19495a65560Sthorpej *rp = ifinfo;
19595a65560Sthorpej return (0);
19695a65560Sthorpej }
19795a65560Sthorpej
19895a65560Sthorpej /*
19995a65560Sthorpej * system call interfaces for qdisc_ops
20095a65560Sthorpej */
20195a65560Sthorpej static int
wfq_attach(struct ifinfo * ifinfo)20295a65560Sthorpej wfq_attach(struct ifinfo *ifinfo)
20395a65560Sthorpej {
20495a65560Sthorpej struct wfq_interface iface;
20595a65560Sthorpej struct wfq_ifinfo *wfq_ifinfo;
20695a65560Sthorpej struct wfq_conf conf;
20795a65560Sthorpej
20895a65560Sthorpej if (wfq_fd < 0 &&
20995a65560Sthorpej (wfq_fd = open(WFQ_DEVICE, O_RDWR)) < 0 &&
21095a65560Sthorpej (wfq_fd = open_module(WFQ_DEVICE, O_RDWR)) < 0) {
211d5e1f166Sitojun LOG(LOG_ERR, errno, "WFQ open");
21295a65560Sthorpej return (QOPERR_SYSCALL);
21395a65560Sthorpej }
21495a65560Sthorpej
21595a65560Sthorpej wfq_refcount++;
21695a65560Sthorpej memset(&iface, 0, sizeof(iface));
21795a65560Sthorpej strncpy(iface.wfq_ifacename, ifinfo->ifname, IFNAMSIZ);
21895a65560Sthorpej
21995a65560Sthorpej if (ioctl(wfq_fd, WFQ_IF_ATTACH, &iface) < 0)
22095a65560Sthorpej return (QOPERR_SYSCALL);
22195a65560Sthorpej
22295a65560Sthorpej /* set wfq parameters */
22395a65560Sthorpej wfq_ifinfo = (struct wfq_ifinfo *)ifinfo->private;
22495a65560Sthorpej if (wfq_ifinfo->hash_policy != 0 || wfq_ifinfo->nqueues != 0 ||
22595a65560Sthorpej wfq_ifinfo->qsize != 0) {
22695a65560Sthorpej memset(&conf, 0, sizeof(conf));
22795a65560Sthorpej strncpy(conf.iface.wfq_ifacename, ifinfo->ifname, IFNAMSIZ);
22895a65560Sthorpej conf.hash_policy = wfq_ifinfo->hash_policy;
22995a65560Sthorpej conf.nqueues = wfq_ifinfo->nqueues;
23095a65560Sthorpej conf.qlimit = wfq_ifinfo->qsize;
23195a65560Sthorpej if (ioctl(wfq_fd, WFQ_CONFIG, &conf) < 0) {
232d5e1f166Sitojun LOG(LOG_ERR, errno, "WFQ_CONFIG");
23395a65560Sthorpej return (QOPERR_SYSCALL);
23495a65560Sthorpej }
23595a65560Sthorpej }
23695a65560Sthorpej #if 1
237d5e1f166Sitojun LOG(LOG_INFO, 0, "wfq attached to %s", iface.wfq_ifacename);
23895a65560Sthorpej #endif
23995a65560Sthorpej return (0);
24095a65560Sthorpej }
24195a65560Sthorpej
24295a65560Sthorpej static int
wfq_detach(struct ifinfo * ifinfo)24395a65560Sthorpej wfq_detach(struct ifinfo *ifinfo)
24495a65560Sthorpej {
24595a65560Sthorpej struct wfq_interface iface;
24695a65560Sthorpej
24795a65560Sthorpej memset(&iface, 0, sizeof(iface));
24895a65560Sthorpej strncpy(iface.wfq_ifacename, ifinfo->ifname, IFNAMSIZ);
24995a65560Sthorpej
25095a65560Sthorpej if (ioctl(wfq_fd, WFQ_IF_DETACH, &iface) < 0)
25195a65560Sthorpej return (QOPERR_SYSCALL);
25295a65560Sthorpej
25395a65560Sthorpej if (--wfq_refcount == 0) {
25495a65560Sthorpej close(wfq_fd);
25595a65560Sthorpej wfq_fd = -1;
25695a65560Sthorpej }
25795a65560Sthorpej return (0);
25895a65560Sthorpej }
25995a65560Sthorpej
26095a65560Sthorpej static int
wfq_enable(struct ifinfo * ifinfo)26195a65560Sthorpej wfq_enable(struct ifinfo *ifinfo)
26295a65560Sthorpej {
26395a65560Sthorpej struct wfq_interface iface;
26495a65560Sthorpej
26595a65560Sthorpej memset(&iface, 0, sizeof(iface));
26695a65560Sthorpej strncpy(iface.wfq_ifacename, ifinfo->ifname, IFNAMSIZ);
26795a65560Sthorpej
26895a65560Sthorpej if (ioctl(wfq_fd, WFQ_ENABLE, &iface) < 0)
26995a65560Sthorpej return (QOPERR_SYSCALL);
27095a65560Sthorpej return (0);
27195a65560Sthorpej }
27295a65560Sthorpej
27395a65560Sthorpej static int
wfq_disable(struct ifinfo * ifinfo)27495a65560Sthorpej wfq_disable(struct ifinfo *ifinfo)
27595a65560Sthorpej {
27695a65560Sthorpej struct wfq_interface iface;
27795a65560Sthorpej
27895a65560Sthorpej memset(&iface, 0, sizeof(iface));
27995a65560Sthorpej strncpy(iface.wfq_ifacename, ifinfo->ifname, IFNAMSIZ);
28095a65560Sthorpej
28195a65560Sthorpej if (ioctl(wfq_fd, WFQ_DISABLE, &iface) < 0)
28295a65560Sthorpej return (QOPERR_SYSCALL);
28395a65560Sthorpej return (0);
28495a65560Sthorpej }
285