1*8542734aSAndroid Build Coastguard Worker /* 2*8542734aSAndroid Build Coastguard Worker * Copyright (C) 2011 The Android Open Source Project 3*8542734aSAndroid Build Coastguard Worker * 4*8542734aSAndroid Build Coastguard Worker * Licensed under the Apache License, Version 2.0 (the "License"); 5*8542734aSAndroid Build Coastguard Worker * you may not use this file except in compliance with the License. 6*8542734aSAndroid Build Coastguard Worker * You may obtain a copy of the License at 7*8542734aSAndroid Build Coastguard Worker * 8*8542734aSAndroid Build Coastguard Worker * http://www.apache.org/licenses/LICENSE-2.0 9*8542734aSAndroid Build Coastguard Worker * 10*8542734aSAndroid Build Coastguard Worker * Unless required by applicable law or agreed to in writing, software 11*8542734aSAndroid Build Coastguard Worker * distributed under the License is distributed on an "AS IS" BASIS, 12*8542734aSAndroid Build Coastguard Worker * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13*8542734aSAndroid Build Coastguard Worker * See the License for the specific language governing permissions and 14*8542734aSAndroid Build Coastguard Worker * limitations under the License. 15*8542734aSAndroid Build Coastguard Worker */ 16*8542734aSAndroid Build Coastguard Worker #ifndef _BANDWIDTH_CONTROLLER_H 17*8542734aSAndroid Build Coastguard Worker #define _BANDWIDTH_CONTROLLER_H 18*8542734aSAndroid Build Coastguard Worker 19*8542734aSAndroid Build Coastguard Worker #include <map> 20*8542734aSAndroid Build Coastguard Worker #include <set> 21*8542734aSAndroid Build Coastguard Worker #include <string> 22*8542734aSAndroid Build Coastguard Worker #include <utility> 23*8542734aSAndroid Build Coastguard Worker #include <vector> 24*8542734aSAndroid Build Coastguard Worker #include <mutex> 25*8542734aSAndroid Build Coastguard Worker 26*8542734aSAndroid Build Coastguard Worker #include "NetdConstants.h" 27*8542734aSAndroid Build Coastguard Worker 28*8542734aSAndroid Build Coastguard Worker class BandwidthController { 29*8542734aSAndroid Build Coastguard Worker public: 30*8542734aSAndroid Build Coastguard Worker std::mutex lock; 31*8542734aSAndroid Build Coastguard Worker 32*8542734aSAndroid Build Coastguard Worker BandwidthController(); 33*8542734aSAndroid Build Coastguard Worker 34*8542734aSAndroid Build Coastguard Worker int setupIptablesHooks(); 35*8542734aSAndroid Build Coastguard Worker 36*8542734aSAndroid Build Coastguard Worker int enableBandwidthControl(); 37*8542734aSAndroid Build Coastguard Worker int enableDataSaver(bool enable); 38*8542734aSAndroid Build Coastguard Worker 39*8542734aSAndroid Build Coastguard Worker int setInterfaceSharedQuota(const std::string& iface, int64_t bytes); 40*8542734aSAndroid Build Coastguard Worker int getInterfaceSharedQuota(int64_t *bytes); 41*8542734aSAndroid Build Coastguard Worker int removeInterfaceSharedQuota(const std::string& iface); 42*8542734aSAndroid Build Coastguard Worker 43*8542734aSAndroid Build Coastguard Worker int setInterfaceQuota(const std::string& iface, int64_t bytes); 44*8542734aSAndroid Build Coastguard Worker int getInterfaceQuota(const std::string& iface, int64_t* bytes); 45*8542734aSAndroid Build Coastguard Worker int removeInterfaceQuota(const std::string& iface); 46*8542734aSAndroid Build Coastguard Worker 47*8542734aSAndroid Build Coastguard Worker int addNaughtyApps(const std::vector<uint32_t>& appUids); 48*8542734aSAndroid Build Coastguard Worker int removeNaughtyApps(const std::vector<uint32_t>& appUids); 49*8542734aSAndroid Build Coastguard Worker int addNiceApps(const std::vector<uint32_t>& appUids); 50*8542734aSAndroid Build Coastguard Worker int removeNiceApps(const std::vector<uint32_t>& appUids); 51*8542734aSAndroid Build Coastguard Worker 52*8542734aSAndroid Build Coastguard Worker int setGlobalAlert(int64_t bytes); 53*8542734aSAndroid Build Coastguard Worker int removeGlobalAlert(); 54*8542734aSAndroid Build Coastguard Worker int setGlobalAlertInForwardChain(); 55*8542734aSAndroid Build Coastguard Worker int removeGlobalAlertInForwardChain(); 56*8542734aSAndroid Build Coastguard Worker 57*8542734aSAndroid Build Coastguard Worker int setInterfaceAlert(const std::string& iface, int64_t bytes); 58*8542734aSAndroid Build Coastguard Worker int removeInterfaceAlert(const std::string& iface); 59*8542734aSAndroid Build Coastguard Worker 60*8542734aSAndroid Build Coastguard Worker static const char LOCAL_INPUT[]; 61*8542734aSAndroid Build Coastguard Worker static const char LOCAL_FORWARD[]; 62*8542734aSAndroid Build Coastguard Worker static const char LOCAL_OUTPUT[]; 63*8542734aSAndroid Build Coastguard Worker static const char LOCAL_RAW_PREROUTING[]; 64*8542734aSAndroid Build Coastguard Worker static const char LOCAL_MANGLE_POSTROUTING[]; 65*8542734aSAndroid Build Coastguard Worker static const char LOCAL_GLOBAL_ALERT[]; 66*8542734aSAndroid Build Coastguard Worker 67*8542734aSAndroid Build Coastguard Worker enum IptJumpOp { IptJumpReject, IptJumpReturn }; 68*8542734aSAndroid Build Coastguard Worker enum IptOp { IptOpInsert, IptOpDelete }; 69*8542734aSAndroid Build Coastguard Worker 70*8542734aSAndroid Build Coastguard Worker private: 71*8542734aSAndroid Build Coastguard Worker struct QuotaInfo { 72*8542734aSAndroid Build Coastguard Worker int64_t quota; 73*8542734aSAndroid Build Coastguard Worker int64_t alert; 74*8542734aSAndroid Build Coastguard Worker }; 75*8542734aSAndroid Build Coastguard Worker 76*8542734aSAndroid Build Coastguard Worker enum IptIpVer { IptIpV4, IptIpV6 }; 77*8542734aSAndroid Build Coastguard Worker enum IptFullOp { IptFullOpInsert, IptFullOpDelete, IptFullOpAppend }; 78*8542734aSAndroid Build Coastguard Worker enum QuotaType { QuotaUnique, QuotaShared }; 79*8542734aSAndroid Build Coastguard Worker enum RunCmdErrHandling { RunCmdFailureBad, RunCmdFailureOk }; 80*8542734aSAndroid Build Coastguard Worker #if LOG_NDEBUG 81*8542734aSAndroid Build Coastguard Worker enum IptFailureLog { IptFailShow, IptFailHide }; 82*8542734aSAndroid Build Coastguard Worker #else 83*8542734aSAndroid Build Coastguard Worker enum IptFailureLog { IptFailShow, IptFailHide = IptFailShow }; 84*8542734aSAndroid Build Coastguard Worker #endif 85*8542734aSAndroid Build Coastguard Worker 86*8542734aSAndroid Build Coastguard Worker std::string makeDataSaverCommand(IptablesTarget target, bool enable); 87*8542734aSAndroid Build Coastguard Worker 88*8542734aSAndroid Build Coastguard Worker int runIptablesAlertCmd(IptOp op, const std::string& alertName, int64_t bytes); 89*8542734aSAndroid Build Coastguard Worker int runIptablesAlertFwdCmd(IptOp op, const std::string& alertName, int64_t bytes); 90*8542734aSAndroid Build Coastguard Worker 91*8542734aSAndroid Build Coastguard Worker int updateQuota(const std::string& alertName, int64_t bytes); 92*8542734aSAndroid Build Coastguard Worker 93*8542734aSAndroid Build Coastguard Worker int setCostlyAlert(const std::string& costName, int64_t bytes, int64_t* alertBytes); 94*8542734aSAndroid Build Coastguard Worker int removeCostlyAlert(const std::string& costName, int64_t* alertBytes); 95*8542734aSAndroid Build Coastguard Worker 96*8542734aSAndroid Build Coastguard Worker /* 97*8542734aSAndroid Build Coastguard Worker * Attempt to find the bw_costly_* tables that need flushing, 98*8542734aSAndroid Build Coastguard Worker * and flush them. 99*8542734aSAndroid Build Coastguard Worker * If doClean then remove the tables also. 100*8542734aSAndroid Build Coastguard Worker * Deals with both ip4 and ip6 tables. 101*8542734aSAndroid Build Coastguard Worker */ 102*8542734aSAndroid Build Coastguard Worker void flushExistingCostlyTables(bool doClean); 103*8542734aSAndroid Build Coastguard Worker static void parseAndFlushCostlyTables(const std::string& ruleList, bool doRemove); 104*8542734aSAndroid Build Coastguard Worker 105*8542734aSAndroid Build Coastguard Worker /* 106*8542734aSAndroid Build Coastguard Worker * Attempt to flush our tables. 107*8542734aSAndroid Build Coastguard Worker * If doClean then remove them also. 108*8542734aSAndroid Build Coastguard Worker * Deals with both ip4 and ip6 tables. 109*8542734aSAndroid Build Coastguard Worker */ 110*8542734aSAndroid Build Coastguard Worker void flushCleanTables(bool doClean); 111*8542734aSAndroid Build Coastguard Worker 112*8542734aSAndroid Build Coastguard Worker // For testing. 113*8542734aSAndroid Build Coastguard Worker friend class BandwidthControllerTest; 114*8542734aSAndroid Build Coastguard Worker static int (*execFunction)(int, char **, int *, bool, bool); 115*8542734aSAndroid Build Coastguard Worker static FILE *(*popenFunction)(const char *, const char *); 116*8542734aSAndroid Build Coastguard Worker static int (*iptablesRestoreFunction)(IptablesTarget, const std::string&, std::string *); 117*8542734aSAndroid Build Coastguard Worker 118*8542734aSAndroid Build Coastguard Worker static const char *opToString(IptOp op); 119*8542734aSAndroid Build Coastguard Worker static const char *jumpToString(IptJumpOp jumpHandling); 120*8542734aSAndroid Build Coastguard Worker 121*8542734aSAndroid Build Coastguard Worker int64_t mSharedQuotaBytes = 0; 122*8542734aSAndroid Build Coastguard Worker int64_t mGlobalAlertBytes = 0; 123*8542734aSAndroid Build Coastguard Worker 124*8542734aSAndroid Build Coastguard Worker std::map<std::string, QuotaInfo> mQuotaIfaces; 125*8542734aSAndroid Build Coastguard Worker std::set<std::string> mSharedQuotaIfaces; 126*8542734aSAndroid Build Coastguard Worker }; 127*8542734aSAndroid Build Coastguard Worker 128*8542734aSAndroid Build Coastguard Worker #endif 129