1 1.5 itojun /* $NetBSD: qop_red.c,v 1.5 2002/03/05 04:11:53 itojun Exp $ */ 2 1.5 itojun /* $KAME: qop_red.c,v 1.6 2001/12/03 08:20:55 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_qop.h" 51 1.1 thorpej #include "qop_red.h" 52 1.1 thorpej 53 1.2 itojun static int red_attach(struct ifinfo *); 54 1.2 itojun static int red_detach(struct ifinfo *); 55 1.2 itojun static int red_enable(struct ifinfo *); 56 1.2 itojun static int red_disable(struct ifinfo *); 57 1.1 thorpej 58 1.1 thorpej #define RED_DEVICE "/dev/altq/red" 59 1.1 thorpej 60 1.1 thorpej static int red_fd = -1; 61 1.1 thorpej static int red_refcount = 0; 62 1.1 thorpej 63 1.1 thorpej static struct qdisc_ops red_qdisc = { 64 1.1 thorpej ALTQT_RED, 65 1.1 thorpej "red", 66 1.1 thorpej red_attach, 67 1.1 thorpej red_detach, 68 1.1 thorpej NULL, /* clear */ 69 1.1 thorpej red_enable, 70 1.1 thorpej red_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 red_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 weight = 0; /* 0: use default */ 89 1.1 thorpej int inv_pmax = 0; /* 0: use default */ 90 1.1 thorpej int th_min = 0; /* 0: use default */ 91 1.1 thorpej int th_max = 0; /* 0: use default */ 92 1.1 thorpej int qlimit = 60; 93 1.1 thorpej int pkttime = 0; 94 1.1 thorpej int flags = 0; 95 1.1 thorpej int packet_size = 1000; 96 1.1 thorpej 97 1.1 thorpej /* 98 1.1 thorpej * process options 99 1.1 thorpej */ 100 1.1 thorpej while (argc > 0) { 101 1.1 thorpej if (EQUAL(*argv, "bandwidth")) { 102 1.1 thorpej argc--; argv++; 103 1.1 thorpej if (argc > 0) 104 1.1 thorpej bandwidth = atobps(*argv); 105 1.1 thorpej } else if (EQUAL(*argv, "tbrsize")) { 106 1.1 thorpej argc--; argv++; 107 1.1 thorpej if (argc > 0) 108 1.1 thorpej tbrsize = atobytes(*argv); 109 1.1 thorpej } else if (EQUAL(*argv, "packetsize")) { 110 1.1 thorpej argc--; argv++; 111 1.1 thorpej if (argc > 0) 112 1.1 thorpej packet_size = atobytes(*argv); 113 1.1 thorpej } else if (EQUAL(*argv, "weight")) { 114 1.1 thorpej argc--; argv++; 115 1.1 thorpej if (argc > 0) 116 1.1 thorpej weight = (int)strtol(*argv, NULL, 0); 117 1.1 thorpej } else if (EQUAL(*argv, "qlimit")) { 118 1.1 thorpej argc--; argv++; 119 1.1 thorpej if (argc > 0) 120 1.1 thorpej qlimit = (int)strtol(*argv, NULL, 0); 121 1.1 thorpej } else if (EQUAL(*argv, "thmin")) { 122 1.1 thorpej argc--; argv++; 123 1.1 thorpej if (argc > 0) 124 1.1 thorpej th_min = (int)strtol(*argv, NULL, 0); 125 1.1 thorpej } else if (EQUAL(*argv, "thmax")) { 126 1.1 thorpej argc--; argv++; 127 1.1 thorpej if (argc > 0) 128 1.1 thorpej th_max = (int)strtol(*argv, NULL, 0); 129 1.1 thorpej } else if (EQUAL(*argv, "invpmax")) { 130 1.1 thorpej argc--; argv++; 131 1.1 thorpej if (argc > 0) 132 1.1 thorpej inv_pmax = (int)strtol(*argv, NULL, 0); 133 1.1 thorpej } else if (EQUAL(*argv, "red")) { 134 1.1 thorpej /* just skip */ 135 1.1 thorpej } else if (EQUAL(*argv, "ecn")) { 136 1.1 thorpej flags |= REDF_ECN; 137 1.1 thorpej } else if (EQUAL(*argv, "flowvalve")) { 138 1.1 thorpej flags |= REDF_FLOWVALVE; 139 1.1 thorpej } else { 140 1.5 itojun LOG(LOG_ERR, 0, "Unknown keyword '%s'", *argv); 141 1.1 thorpej return (0); 142 1.1 thorpej } 143 1.1 thorpej argc--; argv++; 144 1.1 thorpej } 145 1.1 thorpej 146 1.1 thorpej if (qcmd_tbr_register(ifname, bandwidth, tbrsize) != 0) 147 1.1 thorpej return (0); 148 1.1 thorpej 149 1.1 thorpej pkttime = packet_size * 8 * 1000 / (bandwidth / 1000); 150 1.1 thorpej if (weight != 0) { 151 1.1 thorpej /* check if weight is power of 2 */ 152 1.1 thorpej int i, w; 153 1.1 thorpej 154 1.1 thorpej w = weight; 155 1.1 thorpej for (i = 0; w > 1; i++) 156 1.1 thorpej w = w >> 1; 157 1.1 thorpej w = 1 << i; 158 1.1 thorpej if (weight != w) { 159 1.1 thorpej LOG(LOG_ERR, 0, "weight %d: should be power of 2", 160 1.1 thorpej weight); 161 1.1 thorpej return (0); 162 1.1 thorpej } 163 1.1 thorpej } 164 1.1 thorpej 165 1.1 thorpej if (qcmd_red_add_if(ifname, bandwidth, weight, inv_pmax, 166 1.1 thorpej th_min, th_max, qlimit, pkttime, flags) != 0) 167 1.1 thorpej return (0); 168 1.1 thorpej return (1); 169 1.1 thorpej } 170 1.1 thorpej 171 1.1 thorpej /* 172 1.1 thorpej * qcmd api 173 1.1 thorpej */ 174 1.1 thorpej int 175 1.1 thorpej qcmd_red_add_if(const char *ifname, u_int bandwidth, int weight, 176 1.1 thorpej int inv_pmax, int th_min, int th_max, int qlimit, 177 1.1 thorpej int pkttime, int flags) 178 1.1 thorpej { 179 1.1 thorpej int error; 180 1.1 thorpej 181 1.1 thorpej error = qop_red_add_if(NULL, ifname, bandwidth, weight, inv_pmax, 182 1.1 thorpej th_min, th_max, qlimit, pkttime, flags); 183 1.1 thorpej if (error != 0) 184 1.4 itojun LOG(LOG_ERR, errno, "%s: can't add red on interface '%s'", 185 1.1 thorpej qoperror(error), ifname); 186 1.1 thorpej return (error); 187 1.1 thorpej } 188 1.1 thorpej 189 1.1 thorpej /* 190 1.1 thorpej * qop api 191 1.1 thorpej */ 192 1.1 thorpej int 193 1.1 thorpej qop_red_add_if(struct ifinfo **rp, const char *ifname, 194 1.1 thorpej u_int bandwidth, int weight, int inv_pmax, int th_min, 195 1.1 thorpej int th_max, int qlimit, int pkttime, int flags) 196 1.1 thorpej { 197 1.1 thorpej struct ifinfo *ifinfo = NULL; 198 1.1 thorpej struct red_ifinfo *red_ifinfo; 199 1.1 thorpej int error; 200 1.1 thorpej 201 1.1 thorpej if ((red_ifinfo = calloc(1, sizeof(*red_ifinfo))) == NULL) 202 1.1 thorpej return (QOPERR_NOMEM); 203 1.1 thorpej red_ifinfo->weight = weight; 204 1.1 thorpej red_ifinfo->inv_pmax = inv_pmax; 205 1.1 thorpej red_ifinfo->th_min = th_min; 206 1.1 thorpej red_ifinfo->th_max = th_max; 207 1.1 thorpej red_ifinfo->qlimit = qlimit; 208 1.1 thorpej red_ifinfo->pkttime = pkttime; 209 1.1 thorpej red_ifinfo->flags = flags; 210 1.1 thorpej 211 1.1 thorpej error = qop_add_if(&ifinfo, ifname, bandwidth, 212 1.1 thorpej &red_qdisc, red_ifinfo); 213 1.1 thorpej if (error != 0) { 214 1.1 thorpej free(red_ifinfo); 215 1.1 thorpej return (error); 216 1.1 thorpej } 217 1.1 thorpej 218 1.1 thorpej if (rp != NULL) 219 1.1 thorpej *rp = ifinfo; 220 1.1 thorpej return (0); 221 1.1 thorpej } 222 1.1 thorpej 223 1.1 thorpej /* 224 1.1 thorpej * system call interfaces for qdisc_ops 225 1.1 thorpej */ 226 1.1 thorpej static int 227 1.1 thorpej red_attach(struct ifinfo *ifinfo) 228 1.1 thorpej { 229 1.1 thorpej struct red_interface iface; 230 1.1 thorpej struct red_ifinfo *red_ifinfo; 231 1.1 thorpej struct red_conf conf; 232 1.1 thorpej 233 1.1 thorpej if (red_fd < 0 && 234 1.1 thorpej (red_fd = open(RED_DEVICE, O_RDWR)) < 0 && 235 1.1 thorpej (red_fd = open_module(RED_DEVICE, O_RDWR)) < 0) { 236 1.4 itojun LOG(LOG_ERR, errno, "RED open"); 237 1.1 thorpej return (QOPERR_SYSCALL); 238 1.1 thorpej } 239 1.1 thorpej 240 1.1 thorpej red_refcount++; 241 1.1 thorpej memset(&iface, 0, sizeof(iface)); 242 1.1 thorpej strncpy(iface.red_ifname, ifinfo->ifname, IFNAMSIZ); 243 1.1 thorpej 244 1.1 thorpej if (ioctl(red_fd, RED_IF_ATTACH, &iface) < 0) 245 1.1 thorpej return (QOPERR_SYSCALL); 246 1.1 thorpej 247 1.1 thorpej /* set red parameters */ 248 1.1 thorpej red_ifinfo = (struct red_ifinfo *)ifinfo->private; 249 1.1 thorpej memset(&conf, 0, sizeof(conf)); 250 1.1 thorpej strncpy(conf.iface.red_ifname, ifinfo->ifname, IFNAMSIZ); 251 1.1 thorpej conf.red_weight = red_ifinfo->weight; 252 1.1 thorpej conf.red_inv_pmax = red_ifinfo->inv_pmax; 253 1.1 thorpej conf.red_thmin = red_ifinfo->th_min; 254 1.1 thorpej conf.red_thmax = red_ifinfo->th_max; 255 1.1 thorpej conf.red_limit = red_ifinfo->qlimit; 256 1.1 thorpej conf.red_flags = red_ifinfo->flags; 257 1.1 thorpej if (ioctl(red_fd, RED_CONFIG, &conf) < 0) 258 1.1 thorpej return (QOPERR_SYSCALL); 259 1.1 thorpej 260 1.1 thorpej #if 1 261 1.4 itojun LOG(LOG_INFO, 0, "red attached to %s", iface.red_ifname); 262 1.1 thorpej #endif 263 1.1 thorpej return (0); 264 1.1 thorpej } 265 1.1 thorpej 266 1.1 thorpej static int 267 1.1 thorpej red_detach(struct ifinfo *ifinfo) 268 1.1 thorpej { 269 1.1 thorpej struct red_interface iface; 270 1.1 thorpej 271 1.1 thorpej memset(&iface, 0, sizeof(iface)); 272 1.1 thorpej strncpy(iface.red_ifname, ifinfo->ifname, IFNAMSIZ); 273 1.1 thorpej 274 1.1 thorpej if (ioctl(red_fd, RED_IF_DETACH, &iface) < 0) 275 1.1 thorpej return (QOPERR_SYSCALL); 276 1.1 thorpej 277 1.1 thorpej if (--red_refcount == 0) { 278 1.1 thorpej close(red_fd); 279 1.1 thorpej red_fd = -1; 280 1.1 thorpej } 281 1.1 thorpej return (0); 282 1.1 thorpej } 283 1.1 thorpej 284 1.1 thorpej static int 285 1.1 thorpej red_enable(struct ifinfo *ifinfo) 286 1.1 thorpej { 287 1.1 thorpej struct red_interface iface; 288 1.1 thorpej 289 1.1 thorpej memset(&iface, 0, sizeof(iface)); 290 1.1 thorpej strncpy(iface.red_ifname, ifinfo->ifname, IFNAMSIZ); 291 1.1 thorpej 292 1.1 thorpej if (ioctl(red_fd, RED_ENABLE, &iface) < 0) 293 1.1 thorpej return (QOPERR_SYSCALL); 294 1.1 thorpej return (0); 295 1.1 thorpej } 296 1.1 thorpej 297 1.1 thorpej static int 298 1.1 thorpej red_disable(struct ifinfo *ifinfo) 299 1.1 thorpej { 300 1.1 thorpej struct red_interface iface; 301 1.1 thorpej 302 1.1 thorpej memset(&iface, 0, sizeof(iface)); 303 1.1 thorpej strncpy(iface.red_ifname, ifinfo->ifname, IFNAMSIZ); 304 1.1 thorpej 305 1.1 thorpej if (ioctl(red_fd, RED_DISABLE, &iface) < 0) 306 1.1 thorpej return (QOPERR_SYSCALL); 307 1.1 thorpej return (0); 308 1.1 thorpej } 309