blob: 850326a8f218ade81acd2af3479dec670c3d600c [file] [log] [blame]
JP Abgrall4a5f5ca2011-06-15 18:37:39 -07001/*
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 Scherpelzced1dd92017-06-28 10:19:52 +090019#include <map>
20#include <set>
JP Abgrall4a5f5ca2011-06-15 18:37:39 -070021#include <string>
Joel Scherpelzbcad6612017-05-30 10:55:11 +090022#include <utility>
23#include <vector>
Luke Huangd1ee4622018-06-29 13:49:58 +080024#include <mutex>
JP Abgrallbaeccc42013-06-25 09:44:10 -070025
Lorenzo Colitti13debb82016-03-27 17:46:30 +090026#include "NetdConstants.h"
27
JP Abgrall4a5f5ca2011-06-15 18:37:39 -070028class BandwidthController {
29public:
Luke Huangd1ee4622018-06-29 13:49:58 +080030 std::mutex lock;
Lorenzo Colittidedd2712016-03-22 12:36:29 +090031
JP Abgrallfa6f46d2011-06-17 23:17:28 -070032 BandwidthController();
JP Abgrall0031cea2012-04-17 16:38:23 -070033
Joel Scherpelzbcad6612017-05-30 10:55:11 +090034 int setupIptablesHooks();
Chenbo Feng44c0f442018-07-10 16:54:30 -070035 void setBpfEnabled(bool isEnabled);
JP Abgrall0031cea2012-04-17 16:38:23 -070036
Luke Huangf44a3c12018-09-07 12:10:12 +080037 int enableBandwidthControl();
Joel Scherpelzbcad6612017-05-30 10:55:11 +090038 int disableBandwidthControl();
Lorenzo Colitti7618ccb2016-03-18 12:36:03 +090039 int enableDataSaver(bool enable);
JP Abgrallfa6f46d2011-06-17 23:17:28 -070040
Joel Scherpelzbcad6612017-05-30 10:55:11 +090041 int setInterfaceSharedQuota(const std::string& iface, int64_t bytes);
JP Abgrall8a932722011-07-13 19:17:35 -070042 int getInterfaceSharedQuota(int64_t *bytes);
Joel Scherpelzbcad6612017-05-30 10:55:11 +090043 int removeInterfaceSharedQuota(const std::string& iface);
JP Abgrallfa6f46d2011-06-17 23:17:28 -070044
Joel Scherpelzbcad6612017-05-30 10:55:11 +090045 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 Abgrall0dad7c22011-06-24 11:58:14 -070048
Luke Huang531f5d32018-08-03 15:19:05 +080049 // TODO: Remove after removing these commands in CommandListener
Bernie Innocenti15bb55c2018-06-03 16:19:51 +090050 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 Abgrall4a5f5ca2011-06-15 18:37:39 -070054
Luke Huang531f5d32018-08-03 15:19:05 +080055 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
Uldiniad9748e942018-03-01 08:54:00 -050060 int addRestrictAppsOnInterface(const std::string& usecase, const std::string& iface,
61 const std::vector<std::string>& appStrUid);
62 int removeRestrictAppsOnInterface(const std::string& usecase, const std::string& iface,
63 const std::vector<std::string>& appStrUid);
64
JP Abgrall8a932722011-07-13 19:17:35 -070065 int setGlobalAlert(int64_t bytes);
Joel Scherpelzbcad6612017-05-30 10:55:11 +090066 int removeGlobalAlert();
67 int setGlobalAlertInForwardChain();
68 int removeGlobalAlertInForwardChain();
JP Abgrall8a932722011-07-13 19:17:35 -070069
70 int setSharedAlert(int64_t bytes);
Joel Scherpelzbcad6612017-05-30 10:55:11 +090071 int removeSharedAlert();
JP Abgrall8a932722011-07-13 19:17:35 -070072
Joel Scherpelzbcad6612017-05-30 10:55:11 +090073 int setInterfaceAlert(const std::string& iface, int64_t bytes);
74 int removeInterfaceAlert(const std::string& iface);
JP Abgrall0dad7c22011-06-24 11:58:14 -070075
Joel Scherpelzbcad6612017-05-30 10:55:11 +090076 static const char LOCAL_INPUT[];
77 static const char LOCAL_FORWARD[];
78 static const char LOCAL_OUTPUT[];
79 static const char LOCAL_RAW_PREROUTING[];
80 static const char LOCAL_MANGLE_POSTROUTING[];
Luke Huangae038f82018-11-05 11:17:31 +090081 static const char LOCAL_GLOBAL_ALERT[];
Jeff Sharkey8e188ed2012-07-12 18:32:03 -070082
Chenbo Feng95892f32018-06-07 14:52:02 -070083 enum IptJumpOp { IptJumpReject, IptJumpReturn, IptJumpNoAdd };
84 enum IptOp { IptOpInsert, IptOpDelete };
85
Joel Scherpelzbcad6612017-05-30 10:55:11 +090086 private:
Joel Scherpelzced1dd92017-06-28 10:19:52 +090087 struct QuotaInfo {
JP Abgrall8a932722011-07-13 19:17:35 -070088 int64_t quota;
89 int64_t alert;
90 };
JP Abgralldb7da582011-09-18 12:57:32 -070091
JP Abgrall26e0d492011-06-24 19:21:51 -070092 enum IptIpVer { IptIpV4, IptIpV6 };
Lorenzo Colittid9db08c2017-04-28 11:06:40 +090093 enum IptFullOp { IptFullOpInsert, IptFullOpDelete, IptFullOpAppend };
JP Abgrall26e0d492011-06-24 19:21:51 -070094 enum QuotaType { QuotaUnique, QuotaShared };
95 enum RunCmdErrHandling { RunCmdFailureBad, RunCmdFailureOk };
JP Abgrall1fb02df2012-04-24 23:27:44 -070096#if LOG_NDEBUG
97 enum IptFailureLog { IptFailShow, IptFailHide };
98#else
99 enum IptFailureLog { IptFailShow, IptFailHide = IptFailShow };
100#endif
JP Abgralla9ba4cb2013-07-02 19:08:48 -0700101
Lorenzo Colittiaff28792017-09-26 17:46:18 +0900102 std::string makeDataSaverCommand(IptablesTarget target, bool enable);
103
Uldiniad9748e942018-03-01 08:54:00 -0500104 int manipulateRestrictAppsInOut(const std::string& usecase, const std::string& iface,
105 const std::vector<std::string>& appStrUids, IptOp appOp);
106
Sam Mortimer797848d2020-04-03 23:49:28 -0700107 int appsOnInterfaceAccounting(const std::string& usecase,
108 const std::vector<std::string>& appStrUids,
109 IptOp op, bool update);
Uldiniad9748e942018-03-01 08:54:00 -0500110
Joel Scherpelzbcad6612017-05-30 10:55:11 +0900111 int manipulateSpecialApps(const std::vector<std::string>& appStrUids, const std::string& chain,
112 IptJumpOp jumpHandling, IptOp appOp);
JP Abgrall4a5f5ca2011-06-15 18:37:39 -0700113
Joel Scherpelzbcad6612017-05-30 10:55:11 +0900114 int runIptablesAlertCmd(IptOp op, const std::string& alertName, int64_t bytes);
115 int runIptablesAlertFwdCmd(IptOp op, const std::string& alertName, int64_t bytes);
JP Abgrall8a932722011-07-13 19:17:35 -0700116
Joel Scherpelzbcad6612017-05-30 10:55:11 +0900117 int updateQuota(const std::string& alertName, int64_t bytes);
JP Abgrall8a932722011-07-13 19:17:35 -0700118
Joel Scherpelzbcad6612017-05-30 10:55:11 +0900119 int setCostlyAlert(const std::string& costName, int64_t bytes, int64_t* alertBytes);
120 int removeCostlyAlert(const std::string& costName, int64_t* alertBytes);
JP Abgrall8a932722011-07-13 19:17:35 -0700121
JP Abgrall0e540ec2013-08-26 15:13:10 -0700122 /*
123 * Attempt to find the bw_costly_* tables that need flushing,
124 * and flush them.
125 * If doClean then remove the tables also.
126 * Deals with both ip4 and ip6 tables.
127 */
128 void flushExistingCostlyTables(bool doClean);
Lorenzo Colitti56c4b1e2017-02-01 02:45:10 +0900129 static void parseAndFlushCostlyTables(const std::string& ruleList, bool doRemove);
JP Abgrall0e540ec2013-08-26 15:13:10 -0700130
131 /*
132 * Attempt to flush our tables.
133 * If doClean then remove them also.
134 * Deals with both ip4 and ip6 tables.
135 */
136 void flushCleanTables(bool doClean);
137
Joel Scherpelzced1dd92017-06-28 10:19:52 +0900138 // For testing.
139 friend class BandwidthControllerTest;
140 static int (*execFunction)(int, char **, int *, bool, bool);
141 static FILE *(*popenFunction)(const char *, const char *);
142 static int (*iptablesRestoreFunction)(IptablesTarget, const std::string&, std::string *);
JP Abgralldb7da582011-09-18 12:57:32 -0700143
Joel Scherpelzced1dd92017-06-28 10:19:52 +0900144 static const char *opToString(IptOp op);
145 static const char *jumpToString(IptJumpOp jumpHandling);
146
Ken Chen7905cae2018-10-23 23:14:25 +0800147 bool mBpfSupported = false;
Chenbo Feng95892f32018-06-07 14:52:02 -0700148
Joel Scherpelzced1dd92017-06-28 10:19:52 +0900149 int64_t mSharedQuotaBytes = 0;
150 int64_t mSharedAlertBytes = 0;
151 int64_t mGlobalAlertBytes = 0;
JP Abgrallc6c67342011-10-07 16:28:54 -0700152
Joel Scherpelzced1dd92017-06-28 10:19:52 +0900153 std::map<std::string, QuotaInfo> mQuotaIfaces;
154 std::set<std::string> mSharedQuotaIfaces;
Uldiniad9748e942018-03-01 08:54:00 -0500155
156 std::map<std::string /* interface name*/, std::vector<int /*appUid*/>> mRestrictAppsOnInterface;
JP Abgrall4a5f5ca2011-06-15 18:37:39 -0700157};
158
159#endif