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