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 #include <mutex> 25 26 #include "NetdConstants.h" 27 28 class BandwidthController { 29 public: 30 std::mutex lock; 31 32 BandwidthController(); 33 34 int setupIptablesHooks(); 35 36 int enableBandwidthControl(); 37 int disableBandwidthControl(); 38 int enableDataSaver(bool enable); 39 40 int setInterfaceSharedQuota(const std::string& iface, int64_t bytes); 41 int getInterfaceSharedQuota(int64_t *bytes); 42 int removeInterfaceSharedQuota(const std::string& iface); 43 44 int setInterfaceQuota(const std::string& iface, int64_t bytes); 45 int getInterfaceQuota(const std::string& iface, int64_t* bytes); 46 int removeInterfaceQuota(const std::string& iface); 47 48 int addNaughtyApps(const std::vector<uint32_t>& appUids); 49 int removeNaughtyApps(const std::vector<uint32_t>& appUids); 50 int addNiceApps(const std::vector<uint32_t>& appUids); 51 int removeNiceApps(const std::vector<uint32_t>& appUids); 52 53 int setGlobalAlert(int64_t bytes); 54 int removeGlobalAlert(); 55 int setGlobalAlertInForwardChain(); 56 int removeGlobalAlertInForwardChain(); 57 58 int setSharedAlert(int64_t bytes); 59 int removeSharedAlert(); 60 61 int setInterfaceAlert(const std::string& iface, int64_t bytes); 62 int removeInterfaceAlert(const std::string& iface); 63 64 static const char LOCAL_INPUT[]; 65 static const char LOCAL_FORWARD[]; 66 static const char LOCAL_OUTPUT[]; 67 static const char LOCAL_RAW_PREROUTING[]; 68 static const char LOCAL_MANGLE_POSTROUTING[]; 69 static const char LOCAL_GLOBAL_ALERT[]; 70 71 enum IptJumpOp { IptJumpReject, IptJumpReturn }; 72 enum IptOp { IptOpInsert, IptOpDelete }; 73 74 private: 75 struct QuotaInfo { 76 int64_t quota; 77 int64_t alert; 78 }; 79 80 enum IptIpVer { IptIpV4, IptIpV6 }; 81 enum IptFullOp { IptFullOpInsert, IptFullOpDelete, IptFullOpAppend }; 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 runIptablesAlertCmd(IptOp op, const std::string& alertName, int64_t bytes); 93 int runIptablesAlertFwdCmd(IptOp op, const std::string& alertName, int64_t bytes); 94 95 int updateQuota(const std::string& alertName, int64_t bytes); 96 97 int setCostlyAlert(const std::string& costName, int64_t bytes, int64_t* alertBytes); 98 int removeCostlyAlert(const std::string& costName, int64_t* alertBytes); 99 100 /* 101 * Attempt to find the bw_costly_* tables that need flushing, 102 * and flush them. 103 * If doClean then remove the tables also. 104 * Deals with both ip4 and ip6 tables. 105 */ 106 void flushExistingCostlyTables(bool doClean); 107 static void parseAndFlushCostlyTables(const std::string& ruleList, bool doRemove); 108 109 /* 110 * Attempt to flush our tables. 111 * If doClean then remove them also. 112 * Deals with both ip4 and ip6 tables. 113 */ 114 void flushCleanTables(bool doClean); 115 116 // For testing. 117 friend class BandwidthControllerTest; 118 static int (*execFunction)(int, char **, int *, bool, bool); 119 static FILE *(*popenFunction)(const char *, const char *); 120 static int (*iptablesRestoreFunction)(IptablesTarget, const std::string&, std::string *); 121 122 static const char *opToString(IptOp op); 123 static const char *jumpToString(IptJumpOp jumpHandling); 124 125 int64_t mSharedQuotaBytes = 0; 126 int64_t mSharedAlertBytes = 0; 127 int64_t mGlobalAlertBytes = 0; 128 129 std::map<std::string, QuotaInfo> mQuotaIfaces; 130 std::set<std::string> mSharedQuotaIfaces; 131 }; 132 133 #endif 134