1 1.6 joerg /* $NetBSD: qop_wfq.c,v 1.6 2008/09/11 17:58:59 joerg Exp $ */ 2 1.5 itojun /* $KAME: qop_wfq.c,v 1.6 2001/12/03 08:20:56 kjc Exp $ */ 3 1.1 thorpej /* 4 1.1 thorpej * Copyright (C) 1999-2000 5 1.1 thorpej * Sony Computer Science Laboratories, Inc. All rights reserved. 6 1.1 thorpej * 7 1.1 thorpej * Redistribution and use in source and binary forms, with or without 8 1.1 thorpej * modification, are permitted provided that the following conditions 9 1.1 thorpej * are met: 10 1.1 thorpej * 1. Redistributions of source code must retain the above copyright 11 1.1 thorpej * notice, this list of conditions and the following disclaimer. 12 1.1 thorpej * 2. Redistributions in binary form must reproduce the above copyright 13 1.1 thorpej * notice, this list of conditions and the following disclaimer in the 14 1.1 thorpej * documentation and/or other materials provided with the distribution. 15 1.1 thorpej * 16 1.1 thorpej * THIS SOFTWARE IS PROVIDED BY SONY CSL AND CONTRIBUTORS ``AS IS'' AND 17 1.1 thorpej * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 18 1.1 thorpej * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 19 1.1 thorpej * ARE DISCLAIMED. IN NO EVENT SHALL SONY CSL OR CONTRIBUTORS BE LIABLE 20 1.1 thorpej * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 21 1.1 thorpej * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 22 1.1 thorpej * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 23 1.1 thorpej * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 24 1.1 thorpej * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 25 1.1 thorpej * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 26 1.1 thorpej * SUCH DAMAGE. 27 1.1 thorpej */ 28 1.1 thorpej 29 1.1 thorpej #include <sys/param.h> 30 1.1 thorpej #include <sys/socket.h> 31 1.1 thorpej #include <sys/sockio.h> 32 1.1 thorpej #include <sys/ioctl.h> 33 1.1 thorpej #include <sys/fcntl.h> 34 1.1 thorpej #include <net/if.h> 35 1.1 thorpej #include <netinet/in.h> 36 1.1 thorpej #include <arpa/inet.h> 37 1.1 thorpej 38 1.1 thorpej #include <stdio.h> 39 1.1 thorpej #include <stdlib.h> 40 1.1 thorpej #include <unistd.h> 41 1.1 thorpej #include <stddef.h> 42 1.1 thorpej #include <string.h> 43 1.1 thorpej #include <ctype.h> 44 1.1 thorpej #include <errno.h> 45 1.1 thorpej #include <syslog.h> 46 1.1 thorpej #include <netdb.h> 47 1.1 thorpej 48 1.1 thorpej #include <altq/altq.h> 49 1.1 thorpej #include <altq/altq_wfq.h> 50 1.1 thorpej #include "altq_qop.h" 51 1.1 thorpej #include "qop_wfq.h" 52 1.1 thorpej 53 1.2 itojun static int wfq_attach(struct ifinfo *); 54 1.2 itojun static int wfq_detach(struct ifinfo *); 55 1.2 itojun static int wfq_enable(struct ifinfo *); 56 1.2 itojun static int wfq_disable(struct ifinfo *); 57 1.1 thorpej 58 1.1 thorpej #define WFQ_DEVICE "/dev/altq/wfq" 59 1.1 thorpej 60 1.1 thorpej static int wfq_fd = -1; 61 1.1 thorpej static int wfq_refcount = 0; 62 1.1 thorpej 63 1.1 thorpej static struct qdisc_ops wfq_qdisc = { 64 1.1 thorpej ALTQT_WFQ, 65 1.1 thorpej "wfq", 66 1.1 thorpej wfq_attach, 67 1.1 thorpej wfq_detach, 68 1.1 thorpej NULL, /* clear */ 69 1.1 thorpej wfq_enable, 70 1.1 thorpej wfq_disable, 71 1.1 thorpej NULL, /* add class */ 72 1.1 thorpej NULL, /* modify class */ 73 1.1 thorpej NULL, /* delete class */ 74 1.1 thorpej NULL, /* add filter */ 75 1.1 thorpej NULL /* delete filter */ 76 1.1 thorpej }; 77 1.1 thorpej 78 1.1 thorpej /* 79 1.1 thorpej * parser interface 80 1.1 thorpej */ 81 1.1 thorpej #define EQUAL(s1, s2) (strcmp((s1), (s2)) == 0) 82 1.1 thorpej 83 1.1 thorpej int 84 1.1 thorpej wfq_interface_parser(const char *ifname, int argc, char **argv) 85 1.1 thorpej { 86 1.1 thorpej u_int bandwidth = 100000000; /* 100Mbps */ 87 1.1 thorpej u_int tbrsize = 0; 88 1.1 thorpej int hash_policy = 0; /* 0: use default */ 89 1.1 thorpej int nqueues = 0; /* 0: use default */ 90 1.1 thorpej int qsize = 0; /* 0: use default */ 91 1.1 thorpej 92 1.1 thorpej /* 93 1.1 thorpej * process options 94 1.1 thorpej */ 95 1.1 thorpej while (argc > 0) { 96 1.1 thorpej if (EQUAL(*argv, "bandwidth")) { 97 1.1 thorpej argc--; argv++; 98 1.1 thorpej if (argc > 0) 99 1.1 thorpej bandwidth = atobps(*argv); 100 1.1 thorpej } else if (EQUAL(*argv, "tbrsize")) { 101 1.1 thorpej argc--; argv++; 102 1.1 thorpej if (argc > 0) 103 1.1 thorpej tbrsize = atobytes(*argv); 104 1.1 thorpej } else if (EQUAL(*argv, "nqueues")) { 105 1.1 thorpej argc--; argv++; 106 1.1 thorpej if (argc > 0) 107 1.1 thorpej nqueues = (int)strtol(*argv, NULL, 0); 108 1.1 thorpej } else if (EQUAL(*argv, "qsize")) { 109 1.1 thorpej argc--; argv++; 110 1.1 thorpej if (argc > 0) 111 1.1 thorpej qsize = atobytes(*argv); 112 1.1 thorpej } else if (EQUAL(*argv, "hash")) { 113 1.1 thorpej argc--; argv++; 114 1.1 thorpej if (argc > 0) { 115 1.1 thorpej if (EQUAL(*argv, "dstaddr")) 116 1.1 thorpej hash_policy = WFQ_HASH_DSTADDR; 117 1.1 thorpej else if (EQUAL(*argv, "full")) 118 1.1 thorpej hash_policy = WFQ_HASH_FULL; 119 1.1 thorpej else if (EQUAL(*argv, "srcport")) 120 1.1 thorpej hash_policy = WFQ_HASH_SRCPORT; 121 1.6 joerg else if (EQUAL(*argv, "srcaddr")) 122 1.6 joerg hash_policy = WFQ_HASH_SRCADDR; 123 1.1 thorpej else { 124 1.1 thorpej LOG(LOG_ERR, 0, 125 1.5 itojun "Unknown hash policy '%s'", *argv); 126 1.1 thorpej return (0); 127 1.1 thorpej } 128 1.1 thorpej } 129 1.1 thorpej } else if (EQUAL(*argv, "wfq")) { 130 1.1 thorpej /* just skip */ 131 1.1 thorpej } else { 132 1.5 itojun LOG(LOG_ERR, 0, "Unknown keyword '%s'", *argv); 133 1.1 thorpej return (0); 134 1.1 thorpej } 135 1.1 thorpej argc--; argv++; 136 1.1 thorpej } 137 1.1 thorpej 138 1.1 thorpej if (qcmd_tbr_register(ifname, bandwidth, tbrsize) != 0) 139 1.1 thorpej return (0); 140 1.1 thorpej 141 1.1 thorpej if (qsize != 0 && qsize < 1500) { 142 1.1 thorpej LOG(LOG_ERR, 0, "qsize too small: %d bytes", qsize); 143 1.1 thorpej return (0); 144 1.1 thorpej } 145 1.1 thorpej 146 1.1 thorpej if (qcmd_wfq_add_if(ifname, bandwidth, 147 1.1 thorpej hash_policy, nqueues, qsize) != 0) 148 1.1 thorpej return (0); 149 1.1 thorpej return (1); 150 1.1 thorpej } 151 1.1 thorpej 152 1.1 thorpej /* 153 1.1 thorpej * qcmd api 154 1.1 thorpej */ 155 1.1 thorpej int 156 1.1 thorpej qcmd_wfq_add_if(const char *ifname, u_int bandwidth, int hash_policy, 157 1.1 thorpej int nqueues, int qsize) 158 1.1 thorpej { 159 1.1 thorpej int error; 160 1.1 thorpej 161 1.1 thorpej error = qop_wfq_add_if(NULL, ifname, bandwidth, 162 1.1 thorpej hash_policy, nqueues, qsize); 163 1.1 thorpej if (error != 0) 164 1.4 itojun LOG(LOG_ERR, errno, "%s: can't add wfq on interface '%s'", 165 1.1 thorpej qoperror(error), ifname); 166 1.1 thorpej return (error); 167 1.1 thorpej } 168 1.1 thorpej 169 1.1 thorpej /* 170 1.1 thorpej * qop api 171 1.1 thorpej */ 172 1.1 thorpej int 173 1.1 thorpej qop_wfq_add_if(struct ifinfo **rp, const char *ifname, u_int bandwidth, 174 1.1 thorpej int hash_policy, int nqueues, int qsize) 175 1.1 thorpej { 176 1.1 thorpej struct ifinfo *ifinfo = NULL; 177 1.1 thorpej struct wfq_ifinfo *wfq_ifinfo; 178 1.1 thorpej int error; 179 1.1 thorpej 180 1.1 thorpej if ((wfq_ifinfo = calloc(1, sizeof(*wfq_ifinfo))) == NULL) 181 1.1 thorpej return (QOPERR_NOMEM); 182 1.1 thorpej wfq_ifinfo->hash_policy = hash_policy; 183 1.1 thorpej wfq_ifinfo->nqueues = nqueues; 184 1.1 thorpej wfq_ifinfo->qsize = qsize; 185 1.1 thorpej 186 1.1 thorpej error = qop_add_if(&ifinfo, ifname, bandwidth, 187 1.1 thorpej &wfq_qdisc, wfq_ifinfo); 188 1.1 thorpej if (error != 0) { 189 1.1 thorpej free(wfq_ifinfo); 190 1.1 thorpej return (error); 191 1.1 thorpej } 192 1.1 thorpej 193 1.1 thorpej if (rp != NULL) 194 1.1 thorpej *rp = ifinfo; 195 1.1 thorpej return (0); 196 1.1 thorpej } 197 1.1 thorpej 198 1.1 thorpej /* 199 1.1 thorpej * system call interfaces for qdisc_ops 200 1.1 thorpej */ 201 1.1 thorpej static int 202 1.1 thorpej wfq_attach(struct ifinfo *ifinfo) 203 1.1 thorpej { 204 1.1 thorpej struct wfq_interface iface; 205 1.1 thorpej struct wfq_ifinfo *wfq_ifinfo; 206 1.1 thorpej struct wfq_conf conf; 207 1.1 thorpej 208 1.1 thorpej if (wfq_fd < 0 && 209 1.1 thorpej (wfq_fd = open(WFQ_DEVICE, O_RDWR)) < 0 && 210 1.1 thorpej (wfq_fd = open_module(WFQ_DEVICE, O_RDWR)) < 0) { 211 1.4 itojun LOG(LOG_ERR, errno, "WFQ open"); 212 1.1 thorpej return (QOPERR_SYSCALL); 213 1.1 thorpej } 214 1.1 thorpej 215 1.1 thorpej wfq_refcount++; 216 1.1 thorpej memset(&iface, 0, sizeof(iface)); 217 1.1 thorpej strncpy(iface.wfq_ifacename, ifinfo->ifname, IFNAMSIZ); 218 1.1 thorpej 219 1.1 thorpej if (ioctl(wfq_fd, WFQ_IF_ATTACH, &iface) < 0) 220 1.1 thorpej return (QOPERR_SYSCALL); 221 1.1 thorpej 222 1.1 thorpej /* set wfq parameters */ 223 1.1 thorpej wfq_ifinfo = (struct wfq_ifinfo *)ifinfo->private; 224 1.1 thorpej if (wfq_ifinfo->hash_policy != 0 || wfq_ifinfo->nqueues != 0 || 225 1.1 thorpej wfq_ifinfo->qsize != 0) { 226 1.1 thorpej memset(&conf, 0, sizeof(conf)); 227 1.1 thorpej strncpy(conf.iface.wfq_ifacename, ifinfo->ifname, IFNAMSIZ); 228 1.1 thorpej conf.hash_policy = wfq_ifinfo->hash_policy; 229 1.1 thorpej conf.nqueues = wfq_ifinfo->nqueues; 230 1.1 thorpej conf.qlimit = wfq_ifinfo->qsize; 231 1.1 thorpej if (ioctl(wfq_fd, WFQ_CONFIG, &conf) < 0) { 232 1.4 itojun LOG(LOG_ERR, errno, "WFQ_CONFIG"); 233 1.1 thorpej return (QOPERR_SYSCALL); 234 1.1 thorpej } 235 1.1 thorpej } 236 1.1 thorpej #if 1 237 1.4 itojun LOG(LOG_INFO, 0, "wfq attached to %s", iface.wfq_ifacename); 238 1.1 thorpej #endif 239 1.1 thorpej return (0); 240 1.1 thorpej } 241 1.1 thorpej 242 1.1 thorpej static int 243 1.1 thorpej wfq_detach(struct ifinfo *ifinfo) 244 1.1 thorpej { 245 1.1 thorpej struct wfq_interface iface; 246 1.1 thorpej 247 1.1 thorpej memset(&iface, 0, sizeof(iface)); 248 1.1 thorpej strncpy(iface.wfq_ifacename, ifinfo->ifname, IFNAMSIZ); 249 1.1 thorpej 250 1.1 thorpej if (ioctl(wfq_fd, WFQ_IF_DETACH, &iface) < 0) 251 1.1 thorpej return (QOPERR_SYSCALL); 252 1.1 thorpej 253 1.1 thorpej if (--wfq_refcount == 0) { 254 1.1 thorpej close(wfq_fd); 255 1.1 thorpej wfq_fd = -1; 256 1.1 thorpej } 257 1.1 thorpej return (0); 258 1.1 thorpej } 259 1.1 thorpej 260 1.1 thorpej static int 261 1.1 thorpej wfq_enable(struct ifinfo *ifinfo) 262 1.1 thorpej { 263 1.1 thorpej struct wfq_interface iface; 264 1.1 thorpej 265 1.1 thorpej memset(&iface, 0, sizeof(iface)); 266 1.1 thorpej strncpy(iface.wfq_ifacename, ifinfo->ifname, IFNAMSIZ); 267 1.1 thorpej 268 1.1 thorpej if (ioctl(wfq_fd, WFQ_ENABLE, &iface) < 0) 269 1.1 thorpej return (QOPERR_SYSCALL); 270 1.1 thorpej return (0); 271 1.1 thorpej } 272 1.1 thorpej 273 1.1 thorpej static int 274 1.1 thorpej wfq_disable(struct ifinfo *ifinfo) 275 1.1 thorpej { 276 1.1 thorpej struct wfq_interface iface; 277 1.1 thorpej 278 1.1 thorpej memset(&iface, 0, sizeof(iface)); 279 1.1 thorpej strncpy(iface.wfq_ifacename, ifinfo->ifname, IFNAMSIZ); 280 1.1 thorpej 281 1.1 thorpej if (ioctl(wfq_fd, WFQ_DISABLE, &iface) < 0) 282 1.1 thorpej return (QOPERR_SYSCALL); 283 1.1 thorpej return (0); 284 1.1 thorpej } 285