JP Abgrall | 4a5f5ca | 2011-06-15 18:37:39 -0700 | [diff] [blame] | 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 | |
Joel Scherpelz | ced1dd9 | 2017-06-28 10:19:52 +0900 | [diff] [blame] | 19 | #include <map> |
| 20 | #include <set> |
JP Abgrall | 4a5f5ca | 2011-06-15 18:37:39 -0700 | [diff] [blame] | 21 | #include <string> |
Joel Scherpelz | bcad661 | 2017-05-30 10:55:11 +0900 | [diff] [blame] | 22 | #include <utility> |
| 23 | #include <vector> |
Luke Huang | d1ee462 | 2018-06-29 13:49:58 +0800 | [diff] [blame] | 24 | #include <mutex> |
JP Abgrall | baeccc4 | 2013-06-25 09:44:10 -0700 | [diff] [blame] | 25 | |
Lorenzo Colitti | 13debb8 | 2016-03-27 17:46:30 +0900 | [diff] [blame] | 26 | #include "NetdConstants.h" |
| 27 | |
JP Abgrall | 4a5f5ca | 2011-06-15 18:37:39 -0700 | [diff] [blame] | 28 | class BandwidthController { |
| 29 | public: |
Luke Huang | d1ee462 | 2018-06-29 13:49:58 +0800 | [diff] [blame] | 30 | std::mutex lock; |
Lorenzo Colitti | dedd271 | 2016-03-22 12:36:29 +0900 | [diff] [blame] | 31 | |
JP Abgrall | fa6f46d | 2011-06-17 23:17:28 -0700 | [diff] [blame] | 32 | BandwidthController(); |
JP Abgrall | 0031cea | 2012-04-17 16:38:23 -0700 | [diff] [blame] | 33 | |
Joel Scherpelz | bcad661 | 2017-05-30 10:55:11 +0900 | [diff] [blame] | 34 | int setupIptablesHooks(); |
Chenbo Feng | 44c0f44 | 2018-07-10 16:54:30 -0700 | [diff] [blame] | 35 | void setBpfEnabled(bool isEnabled); |
JP Abgrall | 0031cea | 2012-04-17 16:38:23 -0700 | [diff] [blame] | 36 | |
Luke Huang | f44a3c1 | 2018-09-07 12:10:12 +0800 | [diff] [blame] | 37 | int enableBandwidthControl(); |
Joel Scherpelz | bcad661 | 2017-05-30 10:55:11 +0900 | [diff] [blame] | 38 | int disableBandwidthControl(); |
Lorenzo Colitti | 7618ccb | 2016-03-18 12:36:03 +0900 | [diff] [blame] | 39 | int enableDataSaver(bool enable); |
JP Abgrall | fa6f46d | 2011-06-17 23:17:28 -0700 | [diff] [blame] | 40 | |
Joel Scherpelz | bcad661 | 2017-05-30 10:55:11 +0900 | [diff] [blame] | 41 | int setInterfaceSharedQuota(const std::string& iface, int64_t bytes); |
JP Abgrall | 8a93272 | 2011-07-13 19:17:35 -0700 | [diff] [blame] | 42 | int getInterfaceSharedQuota(int64_t *bytes); |
Joel Scherpelz | bcad661 | 2017-05-30 10:55:11 +0900 | [diff] [blame] | 43 | int removeInterfaceSharedQuota(const std::string& iface); |
JP Abgrall | fa6f46d | 2011-06-17 23:17:28 -0700 | [diff] [blame] | 44 | |
Joel Scherpelz | bcad661 | 2017-05-30 10:55:11 +0900 | [diff] [blame] | 45 | int setInterfaceQuota(const std::string& iface, int64_t bytes); |
| 46 | int getInterfaceQuota(const std::string& iface, int64_t* bytes); |
| 47 | int removeInterfaceQuota(const std::string& iface); |
JP Abgrall | 0dad7c2 | 2011-06-24 11:58:14 -0700 | [diff] [blame] | 48 | |
Luke Huang | 531f5d3 | 2018-08-03 15:19:05 +0800 | [diff] [blame] | 49 | // TODO: Remove after removing these commands in CommandListener |
Bernie Innocenti | 15bb55c | 2018-06-03 16:19:51 +0900 | [diff] [blame] | 50 | int addNaughtyApps(int numUids, const char* const appUids[]); |
| 51 | int removeNaughtyApps(int numUids, const char* const appUids[]); |
| 52 | int addNiceApps(int numUids, const char* const appUids[]); |
| 53 | int removeNiceApps(int numUids, const char* const appUids[]); |
JP Abgrall | 4a5f5ca | 2011-06-15 18:37:39 -0700 | [diff] [blame] | 54 | |
Luke Huang | 531f5d3 | 2018-08-03 15:19:05 +0800 | [diff] [blame] | 55 | int addNaughtyApps(const std::vector<std::string>& appStrUid); |
| 56 | int removeNaughtyApps(const std::vector<std::string>& appStrUid); |
| 57 | int addNiceApps(const std::vector<std::string>& appStrUid); |
| 58 | int removeNiceApps(const std::vector<std::string>& appStrUid); |
| 59 | |
JP Abgrall | 8a93272 | 2011-07-13 19:17:35 -0700 | [diff] [blame] | 60 | int setGlobalAlert(int64_t bytes); |
Joel Scherpelz | bcad661 | 2017-05-30 10:55:11 +0900 | [diff] [blame] | 61 | int removeGlobalAlert(); |
| 62 | int setGlobalAlertInForwardChain(); |
| 63 | int removeGlobalAlertInForwardChain(); |
JP Abgrall | 8a93272 | 2011-07-13 19:17:35 -0700 | [diff] [blame] | 64 | |
| 65 | int setSharedAlert(int64_t bytes); |
Joel Scherpelz | bcad661 | 2017-05-30 10:55:11 +0900 | [diff] [blame] | 66 | int removeSharedAlert(); |
JP Abgrall | 8a93272 | 2011-07-13 19:17:35 -0700 | [diff] [blame] | 67 | |
Joel Scherpelz | bcad661 | 2017-05-30 10:55:11 +0900 | [diff] [blame] | 68 | int setInterfaceAlert(const std::string& iface, int64_t bytes); |
| 69 | int removeInterfaceAlert(const std::string& iface); |
JP Abgrall | 0dad7c2 | 2011-06-24 11:58:14 -0700 | [diff] [blame] | 70 | |
Joel Scherpelz | bcad661 | 2017-05-30 10:55:11 +0900 | [diff] [blame] | 71 | static const char LOCAL_INPUT[]; |
| 72 | static const char LOCAL_FORWARD[]; |
| 73 | static const char LOCAL_OUTPUT[]; |
| 74 | static const char LOCAL_RAW_PREROUTING[]; |
| 75 | static const char LOCAL_MANGLE_POSTROUTING[]; |
Luke Huang | ae038f8 | 2018-11-05 11:17:31 +0900 | [diff] [blame] | 76 | static const char LOCAL_GLOBAL_ALERT[]; |
Jeff Sharkey | 8e188ed | 2012-07-12 18:32:03 -0700 | [diff] [blame] | 77 | |
Chenbo Feng | 95892f3 | 2018-06-07 14:52:02 -0700 | [diff] [blame] | 78 | enum IptJumpOp { IptJumpReject, IptJumpReturn, IptJumpNoAdd }; |
| 79 | enum IptOp { IptOpInsert, IptOpDelete }; |
| 80 | |
Joel Scherpelz | bcad661 | 2017-05-30 10:55:11 +0900 | [diff] [blame] | 81 | private: |
Joel Scherpelz | ced1dd9 | 2017-06-28 10:19:52 +0900 | [diff] [blame] | 82 | struct QuotaInfo { |
JP Abgrall | 8a93272 | 2011-07-13 19:17:35 -0700 | [diff] [blame] | 83 | int64_t quota; |
| 84 | int64_t alert; |
| 85 | }; |
JP Abgrall | db7da58 | 2011-09-18 12:57:32 -0700 | [diff] [blame] | 86 | |
JP Abgrall | 26e0d49 | 2011-06-24 19:21:51 -0700 | [diff] [blame] | 87 | enum IptIpVer { IptIpV4, IptIpV6 }; |
Lorenzo Colitti | d9db08c | 2017-04-28 11:06:40 +0900 | [diff] [blame] | 88 | enum IptFullOp { IptFullOpInsert, IptFullOpDelete, IptFullOpAppend }; |
JP Abgrall | 26e0d49 | 2011-06-24 19:21:51 -0700 | [diff] [blame] | 89 | enum QuotaType { QuotaUnique, QuotaShared }; |
| 90 | enum RunCmdErrHandling { RunCmdFailureBad, RunCmdFailureOk }; |
JP Abgrall | 1fb02df | 2012-04-24 23:27:44 -0700 | [diff] [blame] | 91 | #if LOG_NDEBUG |
| 92 | enum IptFailureLog { IptFailShow, IptFailHide }; |
| 93 | #else |
| 94 | enum IptFailureLog { IptFailShow, IptFailHide = IptFailShow }; |
| 95 | #endif |
JP Abgrall | a9ba4cb | 2013-07-02 19:08:48 -0700 | [diff] [blame] | 96 | |
Lorenzo Colitti | aff2879 | 2017-09-26 17:46:18 +0900 | [diff] [blame] | 97 | std::string makeDataSaverCommand(IptablesTarget target, bool enable); |
| 98 | |
Joel Scherpelz | bcad661 | 2017-05-30 10:55:11 +0900 | [diff] [blame] | 99 | int manipulateSpecialApps(const std::vector<std::string>& appStrUids, const std::string& chain, |
| 100 | IptJumpOp jumpHandling, IptOp appOp); |
JP Abgrall | 4a5f5ca | 2011-06-15 18:37:39 -0700 | [diff] [blame] | 101 | |
Joel Scherpelz | bcad661 | 2017-05-30 10:55:11 +0900 | [diff] [blame] | 102 | int runIptablesAlertCmd(IptOp op, const std::string& alertName, int64_t bytes); |
| 103 | int runIptablesAlertFwdCmd(IptOp op, const std::string& alertName, int64_t bytes); |
JP Abgrall | 8a93272 | 2011-07-13 19:17:35 -0700 | [diff] [blame] | 104 | |
Joel Scherpelz | bcad661 | 2017-05-30 10:55:11 +0900 | [diff] [blame] | 105 | int updateQuota(const std::string& alertName, int64_t bytes); |
JP Abgrall | 8a93272 | 2011-07-13 19:17:35 -0700 | [diff] [blame] | 106 | |
Joel Scherpelz | bcad661 | 2017-05-30 10:55:11 +0900 | [diff] [blame] | 107 | int setCostlyAlert(const std::string& costName, int64_t bytes, int64_t* alertBytes); |
| 108 | int removeCostlyAlert(const std::string& costName, int64_t* alertBytes); |
JP Abgrall | 8a93272 | 2011-07-13 19:17:35 -0700 | [diff] [blame] | 109 | |
JP Abgrall | 0e540ec | 2013-08-26 15:13:10 -0700 | [diff] [blame] | 110 | /* |
| 111 | * Attempt to find the bw_costly_* tables that need flushing, |
| 112 | * and flush them. |
| 113 | * If doClean then remove the tables also. |
| 114 | * Deals with both ip4 and ip6 tables. |
| 115 | */ |
| 116 | void flushExistingCostlyTables(bool doClean); |
Lorenzo Colitti | 56c4b1e | 2017-02-01 02:45:10 +0900 | [diff] [blame] | 117 | static void parseAndFlushCostlyTables(const std::string& ruleList, bool doRemove); |
JP Abgrall | 0e540ec | 2013-08-26 15:13:10 -0700 | [diff] [blame] | 118 | |
| 119 | /* |
| 120 | * Attempt to flush our tables. |
| 121 | * If doClean then remove them also. |
| 122 | * Deals with both ip4 and ip6 tables. |
| 123 | */ |
| 124 | void flushCleanTables(bool doClean); |
| 125 | |
Joel Scherpelz | ced1dd9 | 2017-06-28 10:19:52 +0900 | [diff] [blame] | 126 | // For testing. |
| 127 | friend class BandwidthControllerTest; |
| 128 | static int (*execFunction)(int, char **, int *, bool, bool); |
| 129 | static FILE *(*popenFunction)(const char *, const char *); |
| 130 | static int (*iptablesRestoreFunction)(IptablesTarget, const std::string&, std::string *); |
JP Abgrall | db7da58 | 2011-09-18 12:57:32 -0700 | [diff] [blame] | 131 | |
Joel Scherpelz | ced1dd9 | 2017-06-28 10:19:52 +0900 | [diff] [blame] | 132 | static const char *opToString(IptOp op); |
| 133 | static const char *jumpToString(IptJumpOp jumpHandling); |
| 134 | |
Ken Chen | 7905cae | 2018-10-23 23:14:25 +0800 | [diff] [blame] | 135 | bool mBpfSupported = false; |
Chenbo Feng | 95892f3 | 2018-06-07 14:52:02 -0700 | [diff] [blame] | 136 | |
Joel Scherpelz | ced1dd9 | 2017-06-28 10:19:52 +0900 | [diff] [blame] | 137 | int64_t mSharedQuotaBytes = 0; |
| 138 | int64_t mSharedAlertBytes = 0; |
| 139 | int64_t mGlobalAlertBytes = 0; |
JP Abgrall | c6c6734 | 2011-10-07 16:28:54 -0700 | [diff] [blame] | 140 | |
Joel Scherpelz | ced1dd9 | 2017-06-28 10:19:52 +0900 | [diff] [blame] | 141 | std::map<std::string, QuotaInfo> mQuotaIfaces; |
| 142 | std::set<std::string> mSharedQuotaIfaces; |
JP Abgrall | 4a5f5ca | 2011-06-15 18:37:39 -0700 | [diff] [blame] | 143 | }; |
| 144 | |
| 145 | #endif |