2 * Copyright (C) 2008 The Android Open Source Project
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
8 * http://www.apache.org/licenses/LICENSE-2.0
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
22 #include <sys/socket.h>
24 #include <sys/types.h>
27 #include <linux/netlink.h>
28 #include <linux/rtnetlink.h>
29 #include <linux/pkt_sched.h>
31 #define LOG_TAG "ThrottleController"
32 #include <cutils/log.h>
35 #include "ThrottleController.h"
37 static char TC_PATH[] = "/system/bin/tc";
39 extern "C" int logwrap(int argc, const char **argv, int background);
40 extern "C" int ifc_init(void);
41 extern "C" int ifc_up(const char *name);
42 extern "C" int ifc_down(const char *name);
44 int ThrottleController::runTcCmd(const char *cmd) {
47 strncpy(buffer, cmd, sizeof(buffer)-1);
56 while ((tmp = strsep(&next, " "))) {
59 LOGE("tc argument overflow");
66 return logwrap(i, args, 0);
69 int ThrottleController::setInterfaceThrottle(const char *iface, int rxKbps, int txKbps) {
74 memset(ifn, 0, sizeof(ifn));
75 strncpy(ifn, iface, sizeof(ifn)-1);
84 * Target interface configuration
89 * Add root qdisc for the interface
91 sprintf(cmd, "qdisc add dev %s root handle 1: htb default 1 r2q 1000", ifn);
93 LOGE("Failed to add root qdisc (%s)", strerror(errno));
98 * Add our egress throttling class
100 sprintf(cmd, "class add dev %s parent 1: classid 1:1 htb rate %dkbit", ifn, txKbps);
102 LOGE("Failed to add egress throttling class (%s)", strerror(errno));
107 * Bring up the IFD device
110 if (ifc_up("ifb0")) {
111 LOGE("Failed to up ifb0 (%s)", strerror(errno));
116 * Add root qdisc for IFD
118 sprintf(cmd, "qdisc add dev ifb0 root handle 1: htb default 1 r2q 1000");
120 LOGE("Failed to add root ifb qdisc (%s)", strerror(errno));
125 * Add our ingress throttling class
127 sprintf(cmd, "class add dev ifb0 parent 1: classid 1:1 htb rate %dkbit", rxKbps);
129 LOGE("Failed to add ingress throttling class (%s)", strerror(errno));
134 * Add ingress qdisc for pkt redirection
136 sprintf(cmd, "qdisc add dev %s ingress", ifn);
138 LOGE("Failed to add ingress qdisc (%s)", strerror(errno));
143 * Add filter to link <ifn> -> ifb0
145 sprintf(cmd, "filter add dev %s parent ffff: protocol ip prio 10 u32 match "
146 "u32 0 0 flowid 1:1 action mirred egress redirect dev ifb0", ifn);
148 LOGE("Failed to add ifb filter (%s)", strerror(errno));
158 void ThrottleController::reset(const char *iface) {
161 sprintf(cmd, "qdisc del dev %s root", iface);
163 sprintf(cmd, "qdisc del dev %s ingress", iface);
166 runTcCmd("qdisc del dev ifb0 root");
169 int ThrottleController::getInterfaceRxThrottle(const char *iface, int *rx) {
174 int ThrottleController::getInterfaceTxThrottle(const char *iface, int *tx) {