San Mehat | a1992c9 | 2010-04-07 15:21:36 -0700 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2008 The Android Open Source Project |
| 3 | * |
| 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 |
| 7 | * |
| 8 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | * |
| 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. |
| 15 | */ |
| 16 | |
| 17 | #include <stdlib.h> |
| 18 | #include <errno.h> |
| 19 | #include <fcntl.h> |
Olivier Bailly | ff2c0d8 | 2010-11-17 11:45:07 -0800 | [diff] [blame] | 20 | #include <string.h> |
San Mehat | a1992c9 | 2010-04-07 15:21:36 -0700 | [diff] [blame] | 21 | |
| 22 | #include <sys/socket.h> |
| 23 | #include <sys/stat.h> |
| 24 | #include <sys/types.h> |
| 25 | #include <sys/wait.h> |
| 26 | |
| 27 | #include <linux/netlink.h> |
| 28 | #include <linux/rtnetlink.h> |
| 29 | #include <linux/pkt_sched.h> |
| 30 | |
| 31 | #define LOG_TAG "ThrottleController" |
| 32 | #include <cutils/log.h> |
| 33 | |
| 34 | |
| 35 | #include "ThrottleController.h" |
| 36 | |
| 37 | static char TC_PATH[] = "/system/bin/tc"; |
| 38 | |
JP Abgrall | 9e5e0ce | 2011-12-14 15:20:59 -0800 | [diff] [blame] | 39 | extern "C" int system_nosh(const char *command); |
San Mehat | 7e1f476 | 2010-04-09 16:26:24 -0700 | [diff] [blame] | 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); |
San Mehat | a1992c9 | 2010-04-07 15:21:36 -0700 | [diff] [blame] | 43 | |
| 44 | int ThrottleController::runTcCmd(const char *cmd) { |
JP Abgrall | 11b4e9b | 2011-08-11 15:34:49 -0700 | [diff] [blame] | 45 | char *buffer; |
| 46 | size_t len = strnlen(cmd, 255); |
| 47 | int res; |
San Mehat | a1992c9 | 2010-04-07 15:21:36 -0700 | [diff] [blame] | 48 | |
JP Abgrall | 11b4e9b | 2011-08-11 15:34:49 -0700 | [diff] [blame] | 49 | if (len == 255) { |
Steve Block | 5ea0c05 | 2012-01-06 19:18:11 +0000 | [diff] [blame] | 50 | ALOGE("tc command too long"); |
JP Abgrall | 11b4e9b | 2011-08-11 15:34:49 -0700 | [diff] [blame] | 51 | errno = E2BIG; |
| 52 | return -1; |
San Mehat | a1992c9 | 2010-04-07 15:21:36 -0700 | [diff] [blame] | 53 | } |
San Mehat | a1992c9 | 2010-04-07 15:21:36 -0700 | [diff] [blame] | 54 | |
JP Abgrall | 11b4e9b | 2011-08-11 15:34:49 -0700 | [diff] [blame] | 55 | asprintf(&buffer, "%s %s", TC_PATH, cmd); |
JP Abgrall | 9e5e0ce | 2011-12-14 15:20:59 -0800 | [diff] [blame] | 56 | res = system_nosh(buffer); |
JP Abgrall | 11b4e9b | 2011-08-11 15:34:49 -0700 | [diff] [blame] | 57 | free(buffer); |
| 58 | return res; |
San Mehat | a1992c9 | 2010-04-07 15:21:36 -0700 | [diff] [blame] | 59 | } |
| 60 | |
| 61 | int ThrottleController::setInterfaceThrottle(const char *iface, int rxKbps, int txKbps) { |
San Mehat | 7e1f476 | 2010-04-09 16:26:24 -0700 | [diff] [blame] | 62 | char cmd[512]; |
| 63 | char ifn[65]; |
San Mehat | a1992c9 | 2010-04-07 15:21:36 -0700 | [diff] [blame] | 64 | int rc; |
| 65 | |
San Mehat | 7e1f476 | 2010-04-09 16:26:24 -0700 | [diff] [blame] | 66 | memset(ifn, 0, sizeof(ifn)); |
| 67 | strncpy(ifn, iface, sizeof(ifn)-1); |
| 68 | |
San Mehat | a1992c9 | 2010-04-07 15:21:36 -0700 | [diff] [blame] | 69 | if (txKbps == -1) { |
San Mehat | 7e1f476 | 2010-04-09 16:26:24 -0700 | [diff] [blame] | 70 | reset(ifn); |
San Mehat | a1992c9 | 2010-04-07 15:21:36 -0700 | [diff] [blame] | 71 | return 0; |
| 72 | } |
| 73 | |
San Mehat | 7e1f476 | 2010-04-09 16:26:24 -0700 | [diff] [blame] | 74 | /* |
| 75 | * |
| 76 | * Target interface configuration |
| 77 | * |
| 78 | */ |
San Mehat | a1992c9 | 2010-04-07 15:21:36 -0700 | [diff] [blame] | 79 | |
San Mehat | 7e1f476 | 2010-04-09 16:26:24 -0700 | [diff] [blame] | 80 | /* |
| 81 | * Add root qdisc for the interface |
| 82 | */ |
San Mehat | 9d8d728 | 2010-04-30 06:19:07 -0700 | [diff] [blame] | 83 | sprintf(cmd, "qdisc add dev %s root handle 1: htb default 1 r2q 1000", ifn); |
San Mehat | a1992c9 | 2010-04-07 15:21:36 -0700 | [diff] [blame] | 84 | if (runTcCmd(cmd)) { |
Steve Block | 5ea0c05 | 2012-01-06 19:18:11 +0000 | [diff] [blame] | 85 | ALOGE("Failed to add root qdisc (%s)", strerror(errno)); |
San Mehat | 7e1f476 | 2010-04-09 16:26:24 -0700 | [diff] [blame] | 86 | goto fail; |
| 87 | } |
| 88 | |
| 89 | /* |
| 90 | * Add our egress throttling class |
| 91 | */ |
San Mehat | 9d8d728 | 2010-04-30 06:19:07 -0700 | [diff] [blame] | 92 | sprintf(cmd, "class add dev %s parent 1: classid 1:1 htb rate %dkbit", ifn, txKbps); |
San Mehat | 7e1f476 | 2010-04-09 16:26:24 -0700 | [diff] [blame] | 93 | if (runTcCmd(cmd)) { |
Steve Block | 5ea0c05 | 2012-01-06 19:18:11 +0000 | [diff] [blame] | 94 | ALOGE("Failed to add egress throttling class (%s)", strerror(errno)); |
San Mehat | 7e1f476 | 2010-04-09 16:26:24 -0700 | [diff] [blame] | 95 | goto fail; |
| 96 | } |
| 97 | |
| 98 | /* |
San Mehat | 7e1f476 | 2010-04-09 16:26:24 -0700 | [diff] [blame] | 99 | * Bring up the IFD device |
| 100 | */ |
| 101 | ifc_init(); |
| 102 | if (ifc_up("ifb0")) { |
Steve Block | 5ea0c05 | 2012-01-06 19:18:11 +0000 | [diff] [blame] | 103 | ALOGE("Failed to up ifb0 (%s)", strerror(errno)); |
San Mehat | 7e1f476 | 2010-04-09 16:26:24 -0700 | [diff] [blame] | 104 | goto fail; |
| 105 | } |
| 106 | |
| 107 | /* |
San Mehat | 9d8d728 | 2010-04-30 06:19:07 -0700 | [diff] [blame] | 108 | * Add root qdisc for IFD |
| 109 | */ |
| 110 | sprintf(cmd, "qdisc add dev ifb0 root handle 1: htb default 1 r2q 1000"); |
| 111 | if (runTcCmd(cmd)) { |
Steve Block | 5ea0c05 | 2012-01-06 19:18:11 +0000 | [diff] [blame] | 112 | ALOGE("Failed to add root ifb qdisc (%s)", strerror(errno)); |
San Mehat | 9d8d728 | 2010-04-30 06:19:07 -0700 | [diff] [blame] | 113 | goto fail; |
| 114 | } |
| 115 | |
| 116 | /* |
| 117 | * Add our ingress throttling class |
| 118 | */ |
| 119 | sprintf(cmd, "class add dev ifb0 parent 1: classid 1:1 htb rate %dkbit", rxKbps); |
| 120 | if (runTcCmd(cmd)) { |
Steve Block | 5ea0c05 | 2012-01-06 19:18:11 +0000 | [diff] [blame] | 121 | ALOGE("Failed to add ingress throttling class (%s)", strerror(errno)); |
San Mehat | 9d8d728 | 2010-04-30 06:19:07 -0700 | [diff] [blame] | 122 | goto fail; |
| 123 | } |
| 124 | |
| 125 | /* |
San Mehat | 7e1f476 | 2010-04-09 16:26:24 -0700 | [diff] [blame] | 126 | * Add ingress qdisc for pkt redirection |
| 127 | */ |
| 128 | sprintf(cmd, "qdisc add dev %s ingress", ifn); |
| 129 | if (runTcCmd(cmd)) { |
Steve Block | 5ea0c05 | 2012-01-06 19:18:11 +0000 | [diff] [blame] | 130 | ALOGE("Failed to add ingress qdisc (%s)", strerror(errno)); |
San Mehat | 7e1f476 | 2010-04-09 16:26:24 -0700 | [diff] [blame] | 131 | goto fail; |
| 132 | } |
| 133 | |
| 134 | /* |
| 135 | * Add filter to link <ifn> -> ifb0 |
| 136 | */ |
San Mehat | 9d8d728 | 2010-04-30 06:19:07 -0700 | [diff] [blame] | 137 | sprintf(cmd, "filter add dev %s parent ffff: protocol ip prio 10 u32 match " |
San Mehat | 7e1f476 | 2010-04-09 16:26:24 -0700 | [diff] [blame] | 138 | "u32 0 0 flowid 1:1 action mirred egress redirect dev ifb0", ifn); |
| 139 | if (runTcCmd(cmd)) { |
Steve Block | 5ea0c05 | 2012-01-06 19:18:11 +0000 | [diff] [blame] | 140 | ALOGE("Failed to add ifb filter (%s)", strerror(errno)); |
San Mehat | 7e1f476 | 2010-04-09 16:26:24 -0700 | [diff] [blame] | 141 | goto fail; |
| 142 | } |
| 143 | |
San Mehat | a1992c9 | 2010-04-07 15:21:36 -0700 | [diff] [blame] | 144 | return 0; |
San Mehat | 7e1f476 | 2010-04-09 16:26:24 -0700 | [diff] [blame] | 145 | fail: |
| 146 | reset(ifn); |
| 147 | return -1; |
San Mehat | a1992c9 | 2010-04-07 15:21:36 -0700 | [diff] [blame] | 148 | } |
| 149 | |
| 150 | void ThrottleController::reset(const char *iface) { |
San Mehat | 7e1f476 | 2010-04-09 16:26:24 -0700 | [diff] [blame] | 151 | char cmd[128]; |
| 152 | |
| 153 | sprintf(cmd, "qdisc del dev %s root", iface); |
San Mehat | a1992c9 | 2010-04-07 15:21:36 -0700 | [diff] [blame] | 154 | runTcCmd(cmd); |
San Mehat | 7e1f476 | 2010-04-09 16:26:24 -0700 | [diff] [blame] | 155 | sprintf(cmd, "qdisc del dev %s ingress", iface); |
| 156 | runTcCmd(cmd); |
| 157 | |
| 158 | runTcCmd("qdisc del dev ifb0 root"); |
San Mehat | a1992c9 | 2010-04-07 15:21:36 -0700 | [diff] [blame] | 159 | } |
| 160 | |
| 161 | int ThrottleController::getInterfaceRxThrottle(const char *iface, int *rx) { |
| 162 | *rx = 0; |
| 163 | return 0; |
| 164 | } |
| 165 | |
| 166 | int ThrottleController::getInterfaceTxThrottle(const char *iface, int *tx) { |
| 167 | *tx = 0; |
| 168 | return 0; |
| 169 | } |