1a1992c9ff3e0d180c1f3042658ab9671d61a2fb8San Mehat/*
2a1992c9ff3e0d180c1f3042658ab9671d61a2fb8San Mehat * Copyright (C) 2008 The Android Open Source Project
3a1992c9ff3e0d180c1f3042658ab9671d61a2fb8San Mehat *
4a1992c9ff3e0d180c1f3042658ab9671d61a2fb8San Mehat * Licensed under the Apache License, Version 2.0 (the "License");
5a1992c9ff3e0d180c1f3042658ab9671d61a2fb8San Mehat * you may not use this file except in compliance with the License.
6a1992c9ff3e0d180c1f3042658ab9671d61a2fb8San Mehat * You may obtain a copy of the License at
7a1992c9ff3e0d180c1f3042658ab9671d61a2fb8San Mehat *
8a1992c9ff3e0d180c1f3042658ab9671d61a2fb8San Mehat *      http://www.apache.org/licenses/LICENSE-2.0
9a1992c9ff3e0d180c1f3042658ab9671d61a2fb8San Mehat *
10a1992c9ff3e0d180c1f3042658ab9671d61a2fb8San Mehat * Unless required by applicable law or agreed to in writing, software
11a1992c9ff3e0d180c1f3042658ab9671d61a2fb8San Mehat * distributed under the License is distributed on an "AS IS" BASIS,
12a1992c9ff3e0d180c1f3042658ab9671d61a2fb8San Mehat * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13a1992c9ff3e0d180c1f3042658ab9671d61a2fb8San Mehat * See the License for the specific language governing permissions and
14a1992c9ff3e0d180c1f3042658ab9671d61a2fb8San Mehat * limitations under the License.
15a1992c9ff3e0d180c1f3042658ab9671d61a2fb8San Mehat */
16a1992c9ff3e0d180c1f3042658ab9671d61a2fb8San Mehat
17a1992c9ff3e0d180c1f3042658ab9671d61a2fb8San Mehat#include <stdlib.h>
18a1992c9ff3e0d180c1f3042658ab9671d61a2fb8San Mehat#include <errno.h>
19a1992c9ff3e0d180c1f3042658ab9671d61a2fb8San Mehat#include <fcntl.h>
20ff2c0d8c13457e43f0d4bf06d3177271aac104c1Olivier Bailly#include <string.h>
21a1992c9ff3e0d180c1f3042658ab9671d61a2fb8San Mehat
22a1992c9ff3e0d180c1f3042658ab9671d61a2fb8San Mehat#include <sys/socket.h>
23a1992c9ff3e0d180c1f3042658ab9671d61a2fb8San Mehat#include <sys/stat.h>
24a1992c9ff3e0d180c1f3042658ab9671d61a2fb8San Mehat#include <sys/types.h>
25a1992c9ff3e0d180c1f3042658ab9671d61a2fb8San Mehat#include <sys/wait.h>
26a1992c9ff3e0d180c1f3042658ab9671d61a2fb8San Mehat
27a1992c9ff3e0d180c1f3042658ab9671d61a2fb8San Mehat#include <linux/netlink.h>
28a1992c9ff3e0d180c1f3042658ab9671d61a2fb8San Mehat#include <linux/rtnetlink.h>
29a1992c9ff3e0d180c1f3042658ab9671d61a2fb8San Mehat#include <linux/pkt_sched.h>
30a1992c9ff3e0d180c1f3042658ab9671d61a2fb8San Mehat
31a1992c9ff3e0d180c1f3042658ab9671d61a2fb8San Mehat#define LOG_TAG "ThrottleController"
32a1992c9ff3e0d180c1f3042658ab9671d61a2fb8San Mehat#include <cutils/log.h>
33a1992c9ff3e0d180c1f3042658ab9671d61a2fb8San Mehat
34a1992c9ff3e0d180c1f3042658ab9671d61a2fb8San Mehat
35a1992c9ff3e0d180c1f3042658ab9671d61a2fb8San Mehat#include "ThrottleController.h"
36c462177bd58e3bf0ac4f618934dae060569e3e0bRobert Greenwalt#include "NetdConstants.h"
37a1992c9ff3e0d180c1f3042658ab9671d61a2fb8San Mehat
389e5e0ce62e88ddf9a09798eda51b0c270d354c8eJP Abgrallextern "C" int system_nosh(const char *command);
397e1f476ce965d9bde6e834bbe862faef548b2abbSan Mehatextern "C" int ifc_init(void);
407e1f476ce965d9bde6e834bbe862faef548b2abbSan Mehatextern "C" int ifc_up(const char *name);
417e1f476ce965d9bde6e834bbe862faef548b2abbSan Mehatextern "C" int ifc_down(const char *name);
42a1992c9ff3e0d180c1f3042658ab9671d61a2fb8San Mehat
43a1992c9ff3e0d180c1f3042658ab9671d61a2fb8San Mehatint ThrottleController::runTcCmd(const char *cmd) {
4411b4e9b26fe7b878992162afb39f5a8acfd143edJP Abgrall    char *buffer;
4511b4e9b26fe7b878992162afb39f5a8acfd143edJP Abgrall    size_t len = strnlen(cmd, 255);
4611b4e9b26fe7b878992162afb39f5a8acfd143edJP Abgrall    int res;
4711b4e9b26fe7b878992162afb39f5a8acfd143edJP Abgrall
4811b4e9b26fe7b878992162afb39f5a8acfd143edJP Abgrall    if (len == 255) {
495ea0c05a1e7d8e664b808aa1bb1efd08fdb2fb13Steve Block        ALOGE("tc command too long");
5011b4e9b26fe7b878992162afb39f5a8acfd143edJP Abgrall        errno = E2BIG;
5111b4e9b26fe7b878992162afb39f5a8acfd143edJP Abgrall        return -1;
52a1992c9ff3e0d180c1f3042658ab9671d61a2fb8San Mehat    }
53a1992c9ff3e0d180c1f3042658ab9671d61a2fb8San Mehat
5411b4e9b26fe7b878992162afb39f5a8acfd143edJP Abgrall    asprintf(&buffer, "%s %s", TC_PATH, cmd);
559e5e0ce62e88ddf9a09798eda51b0c270d354c8eJP Abgrall    res = system_nosh(buffer);
5611b4e9b26fe7b878992162afb39f5a8acfd143edJP Abgrall    free(buffer);
5711b4e9b26fe7b878992162afb39f5a8acfd143edJP Abgrall    return res;
58a1992c9ff3e0d180c1f3042658ab9671d61a2fb8San Mehat}
59a1992c9ff3e0d180c1f3042658ab9671d61a2fb8San Mehat
60a1992c9ff3e0d180c1f3042658ab9671d61a2fb8San Mehatint ThrottleController::setInterfaceThrottle(const char *iface, int rxKbps, int txKbps) {
617e1f476ce965d9bde6e834bbe862faef548b2abbSan Mehat    char cmd[512];
627e1f476ce965d9bde6e834bbe862faef548b2abbSan Mehat    char ifn[65];
63a1992c9ff3e0d180c1f3042658ab9671d61a2fb8San Mehat    int rc;
64a1992c9ff3e0d180c1f3042658ab9671d61a2fb8San Mehat
657e1f476ce965d9bde6e834bbe862faef548b2abbSan Mehat    memset(ifn, 0, sizeof(ifn));
667e1f476ce965d9bde6e834bbe862faef548b2abbSan Mehat    strncpy(ifn, iface, sizeof(ifn)-1);
677e1f476ce965d9bde6e834bbe862faef548b2abbSan Mehat
68a1992c9ff3e0d180c1f3042658ab9671d61a2fb8San Mehat    if (txKbps == -1) {
697e1f476ce965d9bde6e834bbe862faef548b2abbSan Mehat        reset(ifn);
70a1992c9ff3e0d180c1f3042658ab9671d61a2fb8San Mehat        return 0;
71a1992c9ff3e0d180c1f3042658ab9671d61a2fb8San Mehat    }
72a1992c9ff3e0d180c1f3042658ab9671d61a2fb8San Mehat
737e1f476ce965d9bde6e834bbe862faef548b2abbSan Mehat    /*
747e1f476ce965d9bde6e834bbe862faef548b2abbSan Mehat     *
757e1f476ce965d9bde6e834bbe862faef548b2abbSan Mehat     * Target interface configuration
767e1f476ce965d9bde6e834bbe862faef548b2abbSan Mehat     *
777e1f476ce965d9bde6e834bbe862faef548b2abbSan Mehat     */
787e1f476ce965d9bde6e834bbe862faef548b2abbSan Mehat
797e1f476ce965d9bde6e834bbe862faef548b2abbSan Mehat    /*
807e1f476ce965d9bde6e834bbe862faef548b2abbSan Mehat     * Add root qdisc for the interface
817e1f476ce965d9bde6e834bbe862faef548b2abbSan Mehat     */
829d8d728596234e1e49cb1f8267b190bcc2d4b685San Mehat    sprintf(cmd, "qdisc add dev %s root handle 1: htb default 1 r2q 1000", ifn);
837e1f476ce965d9bde6e834bbe862faef548b2abbSan Mehat    if (runTcCmd(cmd)) {
845ea0c05a1e7d8e664b808aa1bb1efd08fdb2fb13Steve Block        ALOGE("Failed to add root qdisc (%s)", strerror(errno));
857e1f476ce965d9bde6e834bbe862faef548b2abbSan Mehat        goto fail;
86a1992c9ff3e0d180c1f3042658ab9671d61a2fb8San Mehat    }
87a1992c9ff3e0d180c1f3042658ab9671d61a2fb8San Mehat
887e1f476ce965d9bde6e834bbe862faef548b2abbSan Mehat    /*
897e1f476ce965d9bde6e834bbe862faef548b2abbSan Mehat     * Add our egress throttling class
907e1f476ce965d9bde6e834bbe862faef548b2abbSan Mehat     */
919d8d728596234e1e49cb1f8267b190bcc2d4b685San Mehat    sprintf(cmd, "class add dev %s parent 1: classid 1:1 htb rate %dkbit", ifn, txKbps);
927e1f476ce965d9bde6e834bbe862faef548b2abbSan Mehat    if (runTcCmd(cmd)) {
935ea0c05a1e7d8e664b808aa1bb1efd08fdb2fb13Steve Block        ALOGE("Failed to add egress throttling class (%s)", strerror(errno));
947e1f476ce965d9bde6e834bbe862faef548b2abbSan Mehat        goto fail;
95a1992c9ff3e0d180c1f3042658ab9671d61a2fb8San Mehat    }
96a1992c9ff3e0d180c1f3042658ab9671d61a2fb8San Mehat
977e1f476ce965d9bde6e834bbe862faef548b2abbSan Mehat    /*
987e1f476ce965d9bde6e834bbe862faef548b2abbSan Mehat     * Bring up the IFD device
997e1f476ce965d9bde6e834bbe862faef548b2abbSan Mehat     */
1007e1f476ce965d9bde6e834bbe862faef548b2abbSan Mehat    ifc_init();
1017e1f476ce965d9bde6e834bbe862faef548b2abbSan Mehat    if (ifc_up("ifb0")) {
1025ea0c05a1e7d8e664b808aa1bb1efd08fdb2fb13Steve Block        ALOGE("Failed to up ifb0 (%s)", strerror(errno));
1037e1f476ce965d9bde6e834bbe862faef548b2abbSan Mehat        goto fail;
1047e1f476ce965d9bde6e834bbe862faef548b2abbSan Mehat    }
1057e1f476ce965d9bde6e834bbe862faef548b2abbSan Mehat
1067e1f476ce965d9bde6e834bbe862faef548b2abbSan Mehat    /*
1079d8d728596234e1e49cb1f8267b190bcc2d4b685San Mehat     * Add root qdisc for IFD
1087e1f476ce965d9bde6e834bbe862faef548b2abbSan Mehat     */
1099d8d728596234e1e49cb1f8267b190bcc2d4b685San Mehat    sprintf(cmd, "qdisc add dev ifb0 root handle 1: htb default 1 r2q 1000");
1107e1f476ce965d9bde6e834bbe862faef548b2abbSan Mehat    if (runTcCmd(cmd)) {
1115ea0c05a1e7d8e664b808aa1bb1efd08fdb2fb13Steve Block        ALOGE("Failed to add root ifb qdisc (%s)", strerror(errno));
1127e1f476ce965d9bde6e834bbe862faef548b2abbSan Mehat        goto fail;
1137e1f476ce965d9bde6e834bbe862faef548b2abbSan Mehat    }
1147e1f476ce965d9bde6e834bbe862faef548b2abbSan Mehat
1157e1f476ce965d9bde6e834bbe862faef548b2abbSan Mehat    /*
1169d8d728596234e1e49cb1f8267b190bcc2d4b685San Mehat     * Add our ingress throttling class
1177e1f476ce965d9bde6e834bbe862faef548b2abbSan Mehat     */
1189d8d728596234e1e49cb1f8267b190bcc2d4b685San Mehat    sprintf(cmd, "class add dev ifb0 parent 1: classid 1:1 htb rate %dkbit", rxKbps);
1197e1f476ce965d9bde6e834bbe862faef548b2abbSan Mehat    if (runTcCmd(cmd)) {
1205ea0c05a1e7d8e664b808aa1bb1efd08fdb2fb13Steve Block        ALOGE("Failed to add ingress throttling class (%s)", strerror(errno));
1217e1f476ce965d9bde6e834bbe862faef548b2abbSan Mehat        goto fail;
1227e1f476ce965d9bde6e834bbe862faef548b2abbSan Mehat    }
1237e1f476ce965d9bde6e834bbe862faef548b2abbSan Mehat
1247e1f476ce965d9bde6e834bbe862faef548b2abbSan Mehat    /*
1259d8d728596234e1e49cb1f8267b190bcc2d4b685San Mehat     * Add ingress qdisc for pkt redirection
1267e1f476ce965d9bde6e834bbe862faef548b2abbSan Mehat     */
1279d8d728596234e1e49cb1f8267b190bcc2d4b685San Mehat    sprintf(cmd, "qdisc add dev %s ingress", ifn);
1287e1f476ce965d9bde6e834bbe862faef548b2abbSan Mehat    if (runTcCmd(cmd)) {
1295ea0c05a1e7d8e664b808aa1bb1efd08fdb2fb13Steve Block        ALOGE("Failed to add ingress qdisc (%s)", strerror(errno));
1307e1f476ce965d9bde6e834bbe862faef548b2abbSan Mehat        goto fail;
1317e1f476ce965d9bde6e834bbe862faef548b2abbSan Mehat    }
1327e1f476ce965d9bde6e834bbe862faef548b2abbSan Mehat
1337e1f476ce965d9bde6e834bbe862faef548b2abbSan Mehat    /*
1349d8d728596234e1e49cb1f8267b190bcc2d4b685San Mehat     * Add filter to link <ifn> -> ifb0
1357e1f476ce965d9bde6e834bbe862faef548b2abbSan Mehat     */
1369d8d728596234e1e49cb1f8267b190bcc2d4b685San Mehat    sprintf(cmd, "filter add dev %s parent ffff: protocol ip prio 10 u32 match "
1379d8d728596234e1e49cb1f8267b190bcc2d4b685San Mehat            "u32 0 0 flowid 1:1 action mirred egress redirect dev ifb0", ifn);
1387e1f476ce965d9bde6e834bbe862faef548b2abbSan Mehat    if (runTcCmd(cmd)) {
1395ea0c05a1e7d8e664b808aa1bb1efd08fdb2fb13Steve Block        ALOGE("Failed to add ifb filter (%s)", strerror(errno));
1407e1f476ce965d9bde6e834bbe862faef548b2abbSan Mehat        goto fail;
141a1992c9ff3e0d180c1f3042658ab9671d61a2fb8San Mehat    }
142a1992c9ff3e0d180c1f3042658ab9671d61a2fb8San Mehat
143a1992c9ff3e0d180c1f3042658ab9671d61a2fb8San Mehat    return 0;
1447e1f476ce965d9bde6e834bbe862faef548b2abbSan Mehatfail:
1457e1f476ce965d9bde6e834bbe862faef548b2abbSan Mehat    reset(ifn);
1467e1f476ce965d9bde6e834bbe862faef548b2abbSan Mehat    return -1;
147a1992c9ff3e0d180c1f3042658ab9671d61a2fb8San Mehat}
148a1992c9ff3e0d180c1f3042658ab9671d61a2fb8San Mehat
149a1992c9ff3e0d180c1f3042658ab9671d61a2fb8San Mehatvoid ThrottleController::reset(const char *iface) {
1507e1f476ce965d9bde6e834bbe862faef548b2abbSan Mehat    char cmd[128];
1517e1f476ce965d9bde6e834bbe862faef548b2abbSan Mehat
1527e1f476ce965d9bde6e834bbe862faef548b2abbSan Mehat    sprintf(cmd, "qdisc del dev %s root", iface);
1537e1f476ce965d9bde6e834bbe862faef548b2abbSan Mehat    runTcCmd(cmd);
1547e1f476ce965d9bde6e834bbe862faef548b2abbSan Mehat    sprintf(cmd, "qdisc del dev %s ingress", iface);
155a1992c9ff3e0d180c1f3042658ab9671d61a2fb8San Mehat    runTcCmd(cmd);
1567e1f476ce965d9bde6e834bbe862faef548b2abbSan Mehat
1577e1f476ce965d9bde6e834bbe862faef548b2abbSan Mehat    runTcCmd("qdisc del dev ifb0 root");
158a1992c9ff3e0d180c1f3042658ab9671d61a2fb8San Mehat}
159a1992c9ff3e0d180c1f3042658ab9671d61a2fb8San Mehat
160a1992c9ff3e0d180c1f3042658ab9671d61a2fb8San Mehatint ThrottleController::getInterfaceRxThrottle(const char *iface, int *rx) {
161a1992c9ff3e0d180c1f3042658ab9671d61a2fb8San Mehat    *rx = 0;
162a1992c9ff3e0d180c1f3042658ab9671d61a2fb8San Mehat    return 0;
163a1992c9ff3e0d180c1f3042658ab9671d61a2fb8San Mehat}
164a1992c9ff3e0d180c1f3042658ab9671d61a2fb8San Mehat
165a1992c9ff3e0d180c1f3042658ab9671d61a2fb8San Mehatint ThrottleController::getInterfaceTxThrottle(const char *iface, int *tx) {
166a1992c9ff3e0d180c1f3042658ab9671d61a2fb8San Mehat    *tx = 0;
167a1992c9ff3e0d180c1f3042658ab9671d61a2fb8San Mehat    return 0;
168a1992c9ff3e0d180c1f3042658ab9671d61a2fb8San Mehat}
169