Changeset 2f0141c49bb55157f24738404aa77367a6a45fed
- Timestamp:
- 06/08/09 18:29:43 (3 years ago)
- Children:
- 5987af1b79b0c1fbd4aa9d30a53ec6879853ece3
- Parents:
- 03dc5e370ef4fbf66e34cbe5bc5b878ed764149c
- git-committer:
- Neutron Soutmun <neo.neutron@…> (06/08/09 18:29:43)
- Files:
-
- 5 modified
-
src/rh-config.c (modified) (2 diffs)
-
src/rh-config.h (modified) (4 diffs)
-
src/rh-task-bandwidth.c (modified) (11 diffs)
-
tools/bandwidth.sh.in (modified) (11 diffs)
-
tools/firewall.sh.in (modified) (1 diff)
Legend:
- Unmodified
- Added
- Removed
-
src/rh-config.c
rebf1b31 r2f0141c 13 13 #include "rahunasd.h" 14 14 #include "rh-config.h" 15 16 GList *interfaces_list = NULL; 17 static unsigned long ifb_reserved = 0; 15 18 16 19 enum lcfg_status rahunas_visitor(const char *key, void *data, size_t size, … … 346 349 return 0; 347 350 } 351 352 353 GList *append_interface (GList *inf, 354 const char *inf_name) 355 { 356 GList *runner = NULL; 357 struct interfaces *iface = NULL; 358 struct interfaces *item = NULL; 359 int ifb_ifno; 360 361 if (!inf_name) 362 return inf; 363 364 item = (struct interfaces *) malloc (sizeof (struct interfaces)); 365 if (!item) 366 return inf; 367 368 runner = g_list_first (inf); 369 while (runner != NULL) 370 { 371 iface = (struct interfaces *)runner->data; 372 if (strncmp(iface->dev_internal, inf_name, strlen(inf_name)) == 0) 373 { 374 // Already in the list 375 (iface->hit)++; 376 free(item); 377 return inf; 378 } 379 runner = g_list_next (runner); 380 } 381 382 done: 383 ifb_ifno = ifb_interface_reserve (); 384 if (ifb_ifno < 0) 385 { 386 free (item); 387 return inf; 388 } 389 390 strncpy(item->dev_internal, inf_name, 32); 391 sprintf(item->dev_ifb, "ifb%d", ifb_ifno); 392 item->init = 0; 393 item->hit = 1; 394 DP ("Interface append: %s, %s", item->dev_internal, item->dev_ifb); 395 396 return g_list_append (inf, item); 397 } 398 399 GList *remove_interface (GList *inf, 400 const char *inf_name) 401 { 402 GList *runner = NULL; 403 struct interfaces *iface = NULL; 404 int ifb_ifno; 405 406 if (!inf_name || !inf) 407 return inf; 408 409 runner = g_list_first (inf); 410 while (runner != NULL) 411 { 412 iface = (struct interfaces *)runner->data; 413 if (strncmp (iface->dev_internal, inf_name, strlen (inf_name)) == 0) 414 { 415 iface->hit--; 416 if (iface->hit <=0 ) 417 { 418 sscanf (iface->dev_ifb, "ifb%d", &ifb_ifno); 419 ifb_interface_release (ifb_ifno); 420 if (iface) 421 free (iface); 422 423 return g_list_delete_link (inf, runner); 424 } 425 } 426 427 runner = g_list_next (runner); 428 } 429 430 return inf; 431 } 432 433 434 struct interfaces *get_interface (GList *inf, 435 const char *inf_name) 436 { 437 GList *runner = NULL; 438 struct interfaces *iface = NULL; 439 440 if (!inf_name || !inf) 441 return NULL; 442 443 runner = g_list_first (inf); 444 445 while (runner != NULL) 446 { 447 iface = (struct interfaces *) runner->data; 448 if (strncmp (iface->dev_internal, inf_name, strlen (inf_name)) == 0) 449 return iface; 450 451 runner = g_list_next (runner); 452 } 453 454 return NULL; 455 } 456 457 int ifb_interface_reserve (void) 458 { 459 int i; 460 unsigned long mask = 1; 461 462 for (i=0; i < MAX_IFB_IFACE; i++) 463 { 464 mask <<= i; 465 if (!(ifb_reserved & mask)) 466 { 467 ifb_reserved |= mask; 468 return i; 469 } 470 } 471 472 return -1; 473 } 474 475 void ifb_interface_release (int ifno) 476 { 477 unsigned long mask = 1; 478 479 mask <<= ifno; 480 mask = ~mask; 481 ifb_reserved &= mask; 482 } -
src/rh-config.h
raace70f r2f0141c 20 20 #define XMLSERVICE_URL "/rahunas_service/xmlrpc_service.php" 21 21 22 #define MAX_IFB_IFACE 64 23 22 24 #define CONFIG_FILE RAHUNAS_CONF_DIR "rahunas.conf" 23 25 #define DEFAULT_PID RAHUNAS_RUN_DIR "rahunasd.pid" 24 26 #define DB_NAME "rahunas" 27 28 struct interfaces { 29 char dev_internal[32]; 30 char dev_ifb[32]; 31 int hit; 32 int init; 33 }; 25 34 26 35 struct rahunas_main_config { … … 40 49 char *dev_external; 41 50 char *dev_internal; 51 struct interfaces *iface; 42 52 char *vlan; 43 53 char *vlan_raw_dev_external; … … 91 101 }; 92 102 103 extern GList *interfaces_list; 104 93 105 int get_config(const char *cfg_file, union rahunas_config *config); 94 106 int get_value(const char *cfg_file, const char *key, void **data, size_t *len); … … 98 110 enum lcfg_status rahunas_visitor(const char *key, void *data, size_t size, 99 111 void *user_data); 112 113 GList *append_interface (GList *inf, 114 const char *inf_name); 115 GList *remove_interface (GList *inf, 116 const char *inf_name); 117 struct interfaces *get_interface (GList *inf, 118 const char *inf_name); 119 int ifb_interface_reserve (void); 120 void ifb_interface_release (int ifno); 100 121 #endif // __RH_CONFIG_H -
src/rh-task-bandwidth.c
rebf1b31 r2f0141c 18 18 #include "rh-task-memset.h" 19 19 #include "rh-utils.h" 20 21 22 20 23 21 #define BANDWIDTH_WRAPPER "/etc/rahunas/bandwidth.sh" … … 136 134 } 137 135 138 int bandwidth_start(void) 139 { 140 char *args[3]; 136 int bandwidth_start(struct vserver *vs) 137 { 138 char *args[5]; 139 struct interfaces *iface = vs->vserver_config->iface; 140 int ret; 141 141 142 142 DP("Bandwidth: start"); … … 144 144 args[0] = BANDWIDTH_WRAPPER; 145 145 args[1] = "start"; 146 args[2] = (char *) 0; 147 148 return bandwidth_exec(NULL, args); 149 } 150 151 int bandwidth_stop(void) 152 { 153 char *args[3]; 146 args[2] = iface->dev_internal; 147 args[3] = iface->dev_ifb; 148 args[4] = (char *) 0; 149 150 ret = bandwidth_exec(vs, args); 151 return ret; 152 } 153 154 int bandwidth_stop(struct vserver *vs) 155 { 156 char *args[5]; 157 struct interfaces *iface = vs->vserver_config->iface; 158 int ret; 154 159 155 160 DP("Bandwidth: stop"); … … 157 162 args[0] = BANDWIDTH_WRAPPER; 158 163 args[1] = "stop"; 159 args[2] = (char *) 0; 160 161 return bandwidth_exec(NULL, args); 164 args[2] = iface->dev_internal; 165 args[3] = iface->dev_ifb; 166 args[4] = (char *) 0; 167 168 ret = bandwidth_exec(vs, args); 169 return ret; 162 170 } 163 171 164 172 int bandwidth_add(struct vserver *vs, struct bandwidth_req *bw_req) 165 173 { 166 char *args[7]; 174 char *args[9]; 175 struct interfaces *iface = vs->vserver_config->iface; 167 176 168 177 DP("Bandwidth: request %s %s %s %s", bw_req->slot_id, … … 175 184 args[4] = bw_req->bandwidth_max_down; 176 185 args[5] = bw_req->bandwidth_max_up; 177 args[6] = (char *) 0; 186 args[6] = iface->dev_internal; 187 args[7] = iface->dev_ifb; 188 args[8] = (char *) 0; 178 189 179 190 return bandwidth_exec(vs, args); … … 182 193 int bandwidth_del(struct vserver *vs, struct bandwidth_req *bw_req) 183 194 { 184 char *args[4]; 195 char *args[6]; 196 struct interfaces *iface = vs->vserver_config->iface; 185 197 186 198 DP("Bandwidth: request %s", bw_req->slot_id); … … 189 201 args[1] = "del"; 190 202 args[2] = bw_req->slot_id; 191 args[3] = (char *) 0; 203 args[3] = iface->dev_internal; 204 args[4] = iface->dev_ifb; 205 args[5] = (char *) 0; 192 206 193 207 return bandwidth_exec(vs, args); … … 197 211 static int startservice (void) 198 212 { 199 return bandwidth_start();213 /* Do nothing */ 200 214 } 201 215 … … 203 217 static int stopservice (void) 204 218 { 205 return bandwidth_stop();219 /* Do nothing */ 206 220 } 207 221 … … 209 223 static void init (struct vserver *vs) 210 224 { 211 /* Do nothing */ 225 struct interfaces *iface = NULL; 226 if (!vs) 227 return; 228 229 if (vs->vserver_config->init_flag == VS_RELOAD) 230 return; 231 232 interfaces_list = append_interface (interfaces_list, 233 vs->vserver_config->dev_internal); 234 vs->vserver_config->iface = get_interface (interfaces_list, 235 vs->vserver_config->dev_internal); 236 iface = vs->vserver_config->iface; 237 if (!iface->init) 238 if (bandwidth_start(vs) >= 0) 239 iface->init = 1; 212 240 } 213 241 … … 215 243 static int cleanup (struct vserver *vs) 216 244 { 217 /* Do nothing */ 245 struct interfaces *iface = NULL; 246 if (!vs) 247 return; 248 249 if (vs->vserver_config->init_flag == VS_RELOAD) 250 return; 251 252 iface = vs->vserver_config->iface; 253 DP ("Bandwidth Cleanup: init=%d, hit=%d", iface->init, iface->hit); 254 if (iface->init && iface->hit <= 1) 255 if (bandwidth_stop(vs) >= 0) 256 iface->init = 0; 257 258 interfaces_list = remove_interface (interfaces_list, 259 vs->vserver_config->dev_internal); 218 260 } 219 261 -
tools/bandwidth.sh.in
r9679928 r2f0141c 9 9 TC=/sbin/tc 10 10 IP=/sbin/ip 11 IFCONFIG=/sbin/ifconfig 11 12 12 13 INIT=@sysconfdir@/default/rahunas … … 16 17 INTERFACE_SPEED=102400 17 18 18 SHAPING_DOWN_INF=imq019 SHAPING_UP_INF=imq120 21 19 INTERFACE_ID=9999 22 20 BITTORRENT_ID=9998 … … 29 27 test "$RUN_DAEMON" = "yes" || exit 0 30 28 test -f $RAHUNAS_CONFIG || exit 1 29 30 SHAPING_DOWN_INF="" 31 SHAPING_UP_INF="" 31 32 32 33 set -e … … 56 57 57 58 # Interface Uplink 59 $IFCONFIG $SHAPING_UP_INF up 58 60 $TC qdisc add dev $SHAPING_UP_INF root handle 2: htb \ 59 61 default ${INTERFACE_ID} 60 62 $TC class add dev $SHAPING_UP_INF parent 2: classid 2:${INTERFACE_ID} \ 61 63 htb rate ${INTERFACE_SPEED}Kbit 64 65 # Redirect incoming traffic to IFB 66 $TC qdisc add dev $SHAPING_DOWN_INF ingress 67 $TC filter add dev $SHAPING_DOWN_INF parent ffff: protocol ip prio 2 u32 \ 68 match u32 0 0 flowid 1:${INTERFACE_ID} \ 69 action mirred egress redirect dev $SHAPING_UP_INF 62 70 ;; 63 71 stop) 72 # Redirect incoming traffic to IFB 73 $TC filter del dev $SHAPING_DOWN_INF parent ffff: protocol ip prio 2 u32 \ 74 match u32 0 0 flowid 1:${INTERFACE_ID} \ 75 action mirred egress redirect dev $SHAPING_UP_INF 76 $TC qdisc del dev $SHAPING_DOWN_INF ingress 77 64 78 # Interface Downlink 65 79 $TC qdisc del dev $SHAPING_DOWN_INF root … … 67 81 # Interface Uplink 68 82 $TC qdisc del dev $SHAPING_UP_INF root 83 84 $IFCONFIG $SHAPING_UP_INF down 69 85 ;; 70 86 esac … … 114 130 115 131 usage_add() { 116 echo "Usage: $1 add ID IP DOWNSPEED UPSPEED "132 echo "Usage: $1 add ID IP DOWNSPEED UPSPEED DOWN_IF UP_IF" 117 133 echo " ID - ID number from 1 to 9900" 118 134 echo " IP - IPv4 Address" 119 135 echo " DOWNSPEED - Download speed (bits/s)" 120 136 echo " UPSPEED - Upload speed (bits/s)" 137 echo " DOWN_IF - Downstream interface (eth0, eth1, vlan1, ...)" 138 echo " UP_IF - Upstream interface (ifb0, ifb1, ...)" 121 139 } 122 140 123 141 usage_del() { 124 echo "Usage: $1 del ID "142 echo "Usage: $1 del ID DOWN_IF UP_IF" 125 143 echo " ID - ID number from 1 to 9900" 144 echo " DOWN_IF - Downstream interface (eth0, eth1, vlan1, ...)" 145 echo " UP_IF - Upstream interface (ifb0, ifb1, ...)" 126 146 } 127 147 … … 175 195 case "$1" in 176 196 start) 177 start || true 197 if [ -z "$2" ] || [ -z "$3" ]; then 198 MESSAGE="NOT COMPLETED" 199 else 200 SHAPING_DOWN_INF=$2 201 SHAPING_UP_INF=$3 202 RUN=${RUN}-${SHAPING_DOWN_INF}-${SHAPING_UP_INF} 203 start || true 204 fi 205 178 206 test -n "$MESSAGE" || MESSAGE="NOT COMPLETED" 179 207 echo $MESSAGE 180 208 ;; 181 209 stop) 182 stop || true 210 if [ -z "$2" ] || [ -z "$3" ]; then 211 MESSAGE="NOT COMPLETED" 212 else 213 SHAPING_DOWN_INF=$2 214 SHAPING_UP_INF=$3 215 RUN=${RUN}-${SHAPING_DOWN_INF}-${SHAPING_UP_INF} 216 stop || true 217 fi 218 183 219 test -n "$MESSAGE" || MESSAGE="NOT COMPLETED" 184 220 echo $MESSAGE … … 186 222 ;; 187 223 restart) 188 stop || true 189 test -n "$MESSAGE" || MESSAGE="NOT COMPLETED" 190 echo $MESSAGE 191 192 start || true 224 if [ -z "$2" ] || [ -z "$3" ]; then 225 MESSAGE="NOT COMPLETED" 226 else 227 SHAPING_DOWN_INF=$2 228 SHAPING_UP_INF=$3 229 RUN=${RUN}-${SHAPING_DOWN_INF}-${SHAPING_UP_INF} 230 stop || true 231 start || true 232 fi 233 193 234 test -n "$MESSAGE" || MESSAGE="NOT COMPLETED" 194 235 echo $MESSAGE … … 201 242 fi 202 243 203 if [ $# != 5]; then244 if [ $# != 7 ]; then 204 245 echo "Error: too few arguments" 205 246 usage_add $N … … 207 248 fi 208 249 209 bw_add $2 $3 $4 $5 || true 250 if [ -z "$6" ] || [ -z "$7" ]; then 251 MESSAGE="NOT COMPLETED" 252 else 253 SHAPING_DOWN_INF=$6 254 SHAPING_UP_INF=$7 255 RUN=${RUN}-${SHAPING_DOWN_INF}-${SHAPING_UP_INF} 256 bw_add $2 $3 $4 $5 || true 257 fi 210 258 echo $MESSAGE 211 259 ;; … … 216 264 fi 217 265 218 if [ $# != 2]; then266 if [ $# != 4 ]; then 219 267 echo "Error: too few arguments" 220 268 usage_del $N 221 269 exit 1 222 270 fi 223 224 bw_del $2 || true 271 if [ -z "$3" ] || [ -z "$4" ]; then 272 MESSAGE="NOT COMPLETED" 273 else 274 SHAPING_DOWN_INF=$3 275 SHAPING_UP_INF=$4 276 RUN=${RUN}-${SHAPING_DOWN_INF}-${SHAPING_UP_INF} 277 bw_del $2 || true 278 fi 225 279 echo $MESSAGE 226 280 ;; -
tools/firewall.sh.in
r03dc5e3 r2f0141c 585 585 -j CONNMARK --set-mark 2 586 586 done 587 588 ##589 # Bandwidth Shaping: IMQ - Intermediate Queueing Device590 ##591 if [ "$BANDWIDTH_SHAPE" = "yes" ]; then592 $IPTABLES -t mangle -I $CHAIN_MANGLE_POSTROUTING $DEV_OUT_PARAM $DEV_INTERNAL -j IMQ --todev 0593 $IPTABLES -t mangle -I $CHAIN_MANGLE_PREROUTING $DEV_IN_PARAM $DEV_INTERNAL -j IMQ --todev 1594 fi595 587 596 588 ##
