1 1.5 itojun /* $NetBSD: qop_rio.c,v 1.5 2002/03/05 04:11:53 itojun Exp $ */ 2 1.5 itojun /* $KAME: qop_rio.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_red.h> 50 1.1 thorpej #include <altq/altq_rio.h> 51 1.1 thorpej #include "altq_qop.h" 52 1.1 thorpej #include "qop_rio.h" 53 1.1 thorpej 54 1.2 itojun static int rio_attach(struct ifinfo *); 55 1.2 itojun static int rio_detach(struct ifinfo *); 56 1.2 itojun static int rio_enable(struct ifinfo *); 57 1.2 itojun static int rio_disable(struct ifinfo *); 58 1.1 thorpej 59 1.1 thorpej #define RIO_DEVICE "/dev/altq/rio" 60 1.1 thorpej 61 1.1 thorpej static int rio_fd = -1; 62 1.1 thorpej static int rio_refcount = 0; 63 1.1 thorpej 64 1.1 thorpej static struct qdisc_ops rio_qdisc = { 65 1.1 thorpej ALTQT_RIO, 66 1.1 thorpej "rio", 67 1.1 thorpej rio_attach, 68 1.1 thorpej rio_detach, 69 1.1 thorpej NULL, /* clear */ 70 1.1 thorpej rio_enable, 71 1.1 thorpej rio_disable, 72 1.1 thorpej NULL, /* add class */ 73 1.1 thorpej NULL, /* modify class */ 74 1.1 thorpej NULL, /* delete class */ 75 1.1 thorpej NULL, /* add filter */ 76 1.1 thorpej NULL /* delete filter */ 77 1.1 thorpej }; 78 1.1 thorpej 79 1.1 thorpej /* 80 1.1 thorpej * parser interface 81 1.1 thorpej */ 82 1.1 thorpej #define EQUAL(s1, s2) (strcmp((s1), (s2)) == 0) 83 1.1 thorpej 84 1.1 thorpej int 85 1.1 thorpej rio_interface_parser(const char *ifname, int argc, char **argv) 86 1.1 thorpej { 87 1.1 thorpej u_int bandwidth = 100000000; /* 100Mbps */ 88 1.1 thorpej u_int tbrsize = 0; 89 1.1 thorpej int weight = 0; /* 0: use default */ 90 1.1 thorpej int lo_inv_pmax = 0; /* 0: use default */ 91 1.1 thorpej int lo_th_min = 0; /* 0: use default */ 92 1.1 thorpej int lo_th_max = 0; /* 0: use default */ 93 1.1 thorpej int med_inv_pmax = 0; /* 0: use default */ 94 1.1 thorpej int med_th_min = 0; /* 0: use default */ 95 1.1 thorpej int med_th_max = 0; /* 0: use default */ 96 1.1 thorpej int hi_inv_pmax = 0; /* 0: use default */ 97 1.1 thorpej int hi_th_min = 0; /* 0: use default */ 98 1.1 thorpej int hi_th_max = 0; /* 0: use default */ 99 1.1 thorpej int qlimit = 60; 100 1.1 thorpej int pkttime = 0; 101 1.1 thorpej int flags = 0; 102 1.1 thorpej int packet_size = 1000; 103 1.1 thorpej 104 1.1 thorpej /* 105 1.1 thorpej * process options 106 1.1 thorpej */ 107 1.1 thorpej while (argc > 0) { 108 1.1 thorpej if (EQUAL(*argv, "bandwidth")) { 109 1.1 thorpej argc--; argv++; 110 1.1 thorpej if (argc > 0) 111 1.1 thorpej bandwidth = atobps(*argv); 112 1.1 thorpej } else if (EQUAL(*argv, "tbrsize")) { 113 1.1 thorpej argc--; argv++; 114 1.1 thorpej if (argc > 0) 115 1.1 thorpej tbrsize = atobytes(*argv); 116 1.1 thorpej } else if (EQUAL(*argv, "packetsize")) { 117 1.1 thorpej argc--; argv++; 118 1.1 thorpej if (argc > 0) 119 1.1 thorpej packet_size = atobytes(*argv); 120 1.1 thorpej } else if (EQUAL(*argv, "weight")) { 121 1.1 thorpej argc--; argv++; 122 1.1 thorpej if (argc > 0) 123 1.1 thorpej weight = (int)strtol(*argv, NULL, 0); 124 1.1 thorpej } else if (EQUAL(*argv, "qlimit")) { 125 1.1 thorpej argc--; argv++; 126 1.1 thorpej if (argc > 0) 127 1.1 thorpej qlimit = (int)strtol(*argv, NULL, 0); 128 1.1 thorpej } else if (EQUAL(*argv, "lo_thmin")) { 129 1.1 thorpej argc--; argv++; 130 1.1 thorpej if (argc > 0) 131 1.1 thorpej lo_th_min = (int)strtol(*argv, NULL, 0); 132 1.1 thorpej } else if (EQUAL(*argv, "lo_thmax")) { 133 1.1 thorpej argc--; argv++; 134 1.1 thorpej if (argc > 0) 135 1.1 thorpej lo_th_max = (int)strtol(*argv, NULL, 0); 136 1.1 thorpej } else if (EQUAL(*argv, "lo_invpmax")) { 137 1.1 thorpej argc--; argv++; 138 1.1 thorpej if (argc > 0) 139 1.1 thorpej lo_inv_pmax = (int)strtol(*argv, NULL, 0); 140 1.1 thorpej } else if (EQUAL(*argv, "med_thmin")) { 141 1.1 thorpej argc--; argv++; 142 1.1 thorpej if (argc > 0) 143 1.1 thorpej med_th_min = (int)strtol(*argv, NULL, 0); 144 1.1 thorpej } else if (EQUAL(*argv, "med_thmax")) { 145 1.1 thorpej argc--; argv++; 146 1.1 thorpej if (argc > 0) 147 1.1 thorpej med_th_max = (int)strtol(*argv, NULL, 0); 148 1.1 thorpej } else if (EQUAL(*argv, "med_invpmax")) { 149 1.1 thorpej argc--; argv++; 150 1.1 thorpej if (argc > 0) 151 1.1 thorpej med_inv_pmax = (int)strtol(*argv, NULL, 0); 152 1.1 thorpej } else if (EQUAL(*argv, "hi_thmin")) { 153 1.1 thorpej argc--; argv++; 154 1.1 thorpej if (argc > 0) 155 1.1 thorpej hi_th_min = (int)strtol(*argv, NULL, 0); 156 1.1 thorpej } else if (EQUAL(*argv, "hi_thmax")) { 157 1.1 thorpej argc--; argv++; 158 1.1 thorpej if (argc > 0) 159 1.1 thorpej hi_th_max = (int)strtol(*argv, NULL, 0); 160 1.1 thorpej } else if (EQUAL(*argv, "hi_invpmax")) { 161 1.1 thorpej argc--; argv++; 162 1.1 thorpej if (argc > 0) 163 1.1 thorpej hi_inv_pmax = (int)strtol(*argv, NULL, 0); 164 1.1 thorpej } else if (EQUAL(*argv, "rio")) { 165 1.1 thorpej /* just skip */ 166 1.1 thorpej } else if (EQUAL(*argv, "ecn")) { 167 1.1 thorpej flags |= RIOF_ECN; 168 1.1 thorpej } else { 169 1.5 itojun LOG(LOG_ERR, 0, "Unknown keyword '%s'", *argv); 170 1.1 thorpej return (0); 171 1.1 thorpej } 172 1.1 thorpej argc--; argv++; 173 1.1 thorpej } 174 1.1 thorpej 175 1.1 thorpej if (qcmd_tbr_register(ifname, bandwidth, tbrsize) != 0) 176 1.1 thorpej return (0); 177 1.1 thorpej 178 1.1 thorpej pkttime = packet_size * 8 * 1000 / (bandwidth / 1000); 179 1.1 thorpej if (weight != 0) { 180 1.1 thorpej /* check if weight is power of 2 */ 181 1.1 thorpej int i, w; 182 1.1 thorpej 183 1.1 thorpej w = weight; 184 1.1 thorpej for (i = 0; w > 1; i++) 185 1.1 thorpej w = w >> 1; 186 1.1 thorpej w = 1 << i; 187 1.1 thorpej if (weight != w) { 188 1.1 thorpej LOG(LOG_ERR, 0, "weight %d: should be power of 2", 189 1.1 thorpej weight); 190 1.1 thorpej return (0); 191 1.1 thorpej } 192 1.1 thorpej } 193 1.1 thorpej 194 1.1 thorpej if (qcmd_rio_add_if(ifname, bandwidth, weight, 195 1.1 thorpej lo_inv_pmax, lo_th_min, lo_th_max, 196 1.1 thorpej med_inv_pmax, med_th_min, med_th_max, 197 1.1 thorpej hi_inv_pmax, hi_th_min, hi_th_max, 198 1.1 thorpej qlimit, pkttime, flags) != 0) 199 1.1 thorpej return (0); 200 1.1 thorpej return (1); 201 1.1 thorpej } 202 1.1 thorpej 203 1.1 thorpej /* 204 1.1 thorpej * qcmd api 205 1.1 thorpej */ 206 1.1 thorpej int 207 1.1 thorpej qcmd_rio_add_if(const char *ifname, u_int bandwidth, int weight, 208 1.1 thorpej int lo_inv_pmax, int lo_th_min, int lo_th_max, 209 1.1 thorpej int med_inv_pmax, int med_th_min, int med_th_max, 210 1.1 thorpej int hi_inv_pmax, int hi_th_min, int hi_th_max, 211 1.1 thorpej int qlimit, int pkttime, int flags) 212 1.1 thorpej { 213 1.1 thorpej struct redparams red_params[RIO_NDROPPREC]; 214 1.1 thorpej int error; 215 1.1 thorpej 216 1.1 thorpej red_params[0].inv_pmax = lo_inv_pmax; 217 1.1 thorpej red_params[0].th_min = lo_th_min; 218 1.1 thorpej red_params[0].th_max = lo_th_max; 219 1.1 thorpej red_params[1].inv_pmax = med_inv_pmax; 220 1.1 thorpej red_params[1].th_min = med_th_min; 221 1.1 thorpej red_params[1].th_max = med_th_max; 222 1.1 thorpej red_params[2].inv_pmax = hi_inv_pmax; 223 1.1 thorpej red_params[2].th_min = hi_th_min; 224 1.1 thorpej red_params[2].th_max = hi_th_max; 225 1.1 thorpej 226 1.1 thorpej error = qop_rio_add_if(NULL, ifname, bandwidth, weight, red_params, 227 1.1 thorpej qlimit, pkttime, flags); 228 1.1 thorpej if (error != 0) 229 1.4 itojun LOG(LOG_ERR, errno, "%s: can't add rio on interface '%s'", 230 1.1 thorpej qoperror(error), ifname); 231 1.1 thorpej return (error); 232 1.1 thorpej } 233 1.1 thorpej 234 1.1 thorpej /* 235 1.1 thorpej * qop api 236 1.1 thorpej */ 237 1.1 thorpej int 238 1.1 thorpej qop_rio_add_if(struct ifinfo **rp, const char *ifname, 239 1.1 thorpej u_int bandwidth, int weight, struct redparams *red_params, 240 1.1 thorpej int qlimit, int pkttime, int flags) 241 1.1 thorpej { 242 1.1 thorpej struct ifinfo *ifinfo = NULL; 243 1.1 thorpej struct rio_ifinfo *rio_ifinfo; 244 1.1 thorpej int i, error; 245 1.1 thorpej 246 1.1 thorpej if ((rio_ifinfo = calloc(1, sizeof(*rio_ifinfo))) == NULL) 247 1.1 thorpej return (QOPERR_NOMEM); 248 1.1 thorpej for (i = 0; i < RIO_NDROPPREC; i++) 249 1.1 thorpej rio_ifinfo->red_params[i] = red_params[i]; 250 1.1 thorpej rio_ifinfo->weight = weight; 251 1.1 thorpej rio_ifinfo->qlimit = qlimit; 252 1.1 thorpej rio_ifinfo->pkttime = pkttime; 253 1.1 thorpej rio_ifinfo->flags = flags; 254 1.1 thorpej 255 1.1 thorpej error = qop_add_if(&ifinfo, ifname, bandwidth, 256 1.1 thorpej &rio_qdisc, rio_ifinfo); 257 1.1 thorpej if (error != 0) { 258 1.1 thorpej free(rio_ifinfo); 259 1.1 thorpej return (error); 260 1.1 thorpej } 261 1.1 thorpej 262 1.1 thorpej if (rp != NULL) 263 1.1 thorpej *rp = ifinfo; 264 1.1 thorpej return (0); 265 1.1 thorpej } 266 1.1 thorpej 267 1.1 thorpej /* 268 1.1 thorpej * system call interfaces for qdisc_ops 269 1.1 thorpej */ 270 1.1 thorpej static int 271 1.1 thorpej rio_attach(struct ifinfo *ifinfo) 272 1.1 thorpej { 273 1.1 thorpej struct rio_interface iface; 274 1.1 thorpej struct rio_ifinfo *rio_ifinfo; 275 1.1 thorpej struct rio_conf conf; 276 1.1 thorpej int i; 277 1.1 thorpej 278 1.1 thorpej if (rio_fd < 0 && 279 1.1 thorpej (rio_fd = open(RIO_DEVICE, O_RDWR)) < 0 && 280 1.1 thorpej (rio_fd = open_module(RIO_DEVICE, O_RDWR)) < 0) { 281 1.4 itojun LOG(LOG_ERR, errno, "RIO open"); 282 1.1 thorpej return (QOPERR_SYSCALL); 283 1.1 thorpej } 284 1.1 thorpej 285 1.1 thorpej rio_refcount++; 286 1.1 thorpej memset(&iface, 0, sizeof(iface)); 287 1.1 thorpej strncpy(iface.rio_ifname, ifinfo->ifname, IFNAMSIZ); 288 1.1 thorpej 289 1.1 thorpej if (ioctl(rio_fd, RIO_IF_ATTACH, &iface) < 0) 290 1.1 thorpej return (QOPERR_SYSCALL); 291 1.1 thorpej 292 1.1 thorpej /* set rio parameters */ 293 1.1 thorpej rio_ifinfo = (struct rio_ifinfo *)ifinfo->private; 294 1.1 thorpej memset(&conf, 0, sizeof(conf)); 295 1.1 thorpej strncpy(conf.iface.rio_ifname, ifinfo->ifname, IFNAMSIZ); 296 1.1 thorpej for (i = 0; i < RIO_NDROPPREC; i++) 297 1.1 thorpej conf.q_params[i] = rio_ifinfo->red_params[i]; 298 1.1 thorpej conf.rio_weight = rio_ifinfo->weight; 299 1.1 thorpej conf.rio_limit = rio_ifinfo->qlimit; 300 1.1 thorpej conf.rio_flags = rio_ifinfo->flags; 301 1.1 thorpej if (ioctl(rio_fd, RIO_CONFIG, &conf) < 0) 302 1.1 thorpej return (QOPERR_SYSCALL); 303 1.1 thorpej 304 1.1 thorpej #if 1 305 1.4 itojun LOG(LOG_INFO, 0, "rio attached to %s", iface.rio_ifname); 306 1.1 thorpej #endif 307 1.1 thorpej return (0); 308 1.1 thorpej } 309 1.1 thorpej 310 1.1 thorpej static int 311 1.1 thorpej rio_detach(struct ifinfo *ifinfo) 312 1.1 thorpej { 313 1.1 thorpej struct rio_interface iface; 314 1.1 thorpej 315 1.1 thorpej memset(&iface, 0, sizeof(iface)); 316 1.1 thorpej strncpy(iface.rio_ifname, ifinfo->ifname, IFNAMSIZ); 317 1.1 thorpej 318 1.1 thorpej if (ioctl(rio_fd, RIO_IF_DETACH, &iface) < 0) 319 1.1 thorpej return (QOPERR_SYSCALL); 320 1.1 thorpej 321 1.1 thorpej if (--rio_refcount == 0) { 322 1.1 thorpej close(rio_fd); 323 1.1 thorpej rio_fd = -1; 324 1.1 thorpej } 325 1.1 thorpej return (0); 326 1.1 thorpej } 327 1.1 thorpej 328 1.1 thorpej static int 329 1.1 thorpej rio_enable(struct ifinfo *ifinfo) 330 1.1 thorpej { 331 1.1 thorpej struct rio_interface iface; 332 1.1 thorpej 333 1.1 thorpej memset(&iface, 0, sizeof(iface)); 334 1.1 thorpej strncpy(iface.rio_ifname, ifinfo->ifname, IFNAMSIZ); 335 1.1 thorpej 336 1.1 thorpej if (ioctl(rio_fd, RIO_ENABLE, &iface) < 0) 337 1.1 thorpej return (QOPERR_SYSCALL); 338 1.1 thorpej return (0); 339 1.1 thorpej } 340 1.1 thorpej 341 1.1 thorpej static int 342 1.1 thorpej rio_disable(struct ifinfo *ifinfo) 343 1.1 thorpej { 344 1.1 thorpej struct rio_interface iface; 345 1.1 thorpej 346 1.1 thorpej memset(&iface, 0, sizeof(iface)); 347 1.1 thorpej strncpy(iface.rio_ifname, ifinfo->ifname, IFNAMSIZ); 348 1.1 thorpej 349 1.1 thorpej if (ioctl(rio_fd, RIO_DISABLE, &iface) < 0) 350 1.1 thorpej return (QOPERR_SYSCALL); 351 1.1 thorpej return (0); 352 1.1 thorpej } 353