1 /* 2 * Copyright (C) 2011 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 #ifndef _BANDWIDTH_CONTROLLER_H 17 #define _BANDWIDTH_CONTROLLER_H 18 19 #include <map> 20 #include <set> 21 #include <string> 22 #include <utility> 23 #include <vector> 24 25 #include <utils/RWLock.h> 26 27 #include "NetdConstants.h" 28 29 class BandwidthController { 30 public: 31 android::RWLock lock; 32 33 BandwidthController(); 34 35 int setupIptablesHooks(); 36 static bool getBpfStatsStatus(); 37 38 int enableBandwidthControl(bool force); 39 int disableBandwidthControl(); 40 int enableDataSaver(bool enable); 41 42 int setInterfaceSharedQuota(const std::string& iface, int64_t bytes); 43 int getInterfaceSharedQuota(int64_t *bytes); 44 int removeInterfaceSharedQuota(const std::string& iface); 45 46 int setInterfaceQuota(const std::string& iface, int64_t bytes); 47 int getInterfaceQuota(const std::string& iface, int64_t* bytes); 48 int removeInterfaceQuota(const std::string& iface); 49 50 int addNaughtyApps(int numUids, char *appUids[]); 51 int removeNaughtyApps(int numUids, char *appUids[]); 52 int addNiceApps(int numUids, char *appUids[]); 53 int removeNiceApps(int numUids, char *appUids[]); 54 55 int setGlobalAlert(int64_t bytes); 56 int removeGlobalAlert(); 57 int setGlobalAlertInForwardChain(); 58 int removeGlobalAlertInForwardChain(); 59 60 int setSharedAlert(int64_t bytes); 61 int removeSharedAlert(); 62 63 int setInterfaceAlert(const std::string& iface, int64_t bytes); 64 int removeInterfaceAlert(const std::string& iface); 65 66 static const char LOCAL_INPUT[]; 67 static const char LOCAL_FORWARD[]; 68 static const char LOCAL_OUTPUT[]; 69 static const char LOCAL_RAW_PREROUTING[]; 70 static const char LOCAL_MANGLE_POSTROUTING[]; 71 72 private: 73 struct QuotaInfo { 74 int64_t quota; 75 int64_t alert; 76 }; 77 78 enum IptIpVer { IptIpV4, IptIpV6 }; 79 enum IptFullOp { IptFullOpInsert, IptFullOpDelete, IptFullOpAppend }; 80 enum IptJumpOp { IptJumpReject, IptJumpReturn, IptJumpNoAdd }; 81 enum IptOp { IptOpInsert, IptOpDelete }; 82 enum QuotaType { QuotaUnique, QuotaShared }; 83 enum RunCmdErrHandling { RunCmdFailureBad, RunCmdFailureOk }; 84 #if LOG_NDEBUG 85 enum IptFailureLog { IptFailShow, IptFailHide }; 86 #else 87 enum IptFailureLog { IptFailShow, IptFailHide = IptFailShow }; 88 #endif 89 90 std::string makeDataSaverCommand(IptablesTarget target, bool enable); 91 92 int manipulateSpecialApps(const std::vector<std::string>& appStrUids, const std::string& chain, 93 IptJumpOp jumpHandling, IptOp appOp); 94 95 int runIptablesAlertCmd(IptOp op, const std::string& alertName, int64_t bytes); 96 int runIptablesAlertFwdCmd(IptOp op, const std::string& alertName, int64_t bytes); 97 98 int updateQuota(const std::string& alertName, int64_t bytes); 99 100 int setCostlyAlert(const std::string& costName, int64_t bytes, int64_t* alertBytes); 101 int removeCostlyAlert(const std::string& costName, int64_t* alertBytes); 102 103 /* 104 * Attempt to find the bw_costly_* tables that need flushing, 105 * and flush them. 106 * If doClean then remove the tables also. 107 * Deals with both ip4 and ip6 tables. 108 */ 109 void flushExistingCostlyTables(bool doClean); 110 static void parseAndFlushCostlyTables(const std::string& ruleList, bool doRemove); 111 112 /* 113 * Attempt to flush our tables. 114 * If doClean then remove them also. 115 * Deals with both ip4 and ip6 tables. 116 */ 117 void flushCleanTables(bool doClean); 118 119 // For testing. 120 friend class BandwidthControllerTest; 121 static int (*execFunction)(int, char **, int *, bool, bool); 122 static FILE *(*popenFunction)(const char *, const char *); 123 static int (*iptablesRestoreFunction)(IptablesTarget, const std::string&, std::string *); 124 125 static const char *opToString(IptOp op); 126 static const char *jumpToString(IptJumpOp jumpHandling); 127 128 int64_t mSharedQuotaBytes = 0; 129 int64_t mSharedAlertBytes = 0; 130 int64_t mGlobalAlertBytes = 0; 131 /* 132 * This tracks the number of tethers setup. 133 * The FORWARD chain is updated in the following cases: 134 * - The 1st time a globalAlert is setup and there are tethers setup. 135 * - Anytime a globalAlert is removed and there are tethers setup. 136 * - The 1st tether is setup and there is a globalAlert active. 137 * - The last tether is removed and there is a globalAlert active. 138 */ 139 int mGlobalAlertTetherCount = 0; 140 141 std::map<std::string, QuotaInfo> mQuotaIfaces; 142 std::set<std::string> mSharedQuotaIfaces; 143 }; 144 145 #endif 146