blob: f4fce50d6b942830180669fa2e435a994121434e [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
19#include <list>
20#include <string>
JP Abgrallfa6f46d2011-06-17 23:17:28 -070021#include <utility> // for pair
JP Abgrall4a5f5ca2011-06-15 18:37:39 -070022class BandwidthController {
23public:
JP Abgrallfa6f46d2011-06-17 23:17:28 -070024 BandwidthController();
25 int enableBandwidthControl(void);
26 int disableBandwidthControl(void);
27
JP Abgrall0dad7c22011-06-24 11:58:14 -070028 int setInterfaceSharedQuota(const char *iface, int64_t bytes);
JP Abgrallfa6f46d2011-06-17 23:17:28 -070029 int removeInterfaceSharedQuota(const char *iface);
30
JP Abgrall0dad7c22011-06-24 11:58:14 -070031 int setInterfaceQuota(const char *iface, int64_t bytes);
32 int removeInterfaceQuota(const char *iface);
33
JP Abgrallfa6f46d2011-06-17 23:17:28 -070034 int addNaughtyApps(int numUids, char *appUids[]);
35 int removeNaughtyApps(int numUids, char *appUids[]);
JP Abgrall4a5f5ca2011-06-15 18:37:39 -070036
JP Abgrall0dad7c22011-06-24 11:58:14 -070037
JP Abgrall4a5f5ca2011-06-15 18:37:39 -070038protected:
JP Abgrallfa6f46d2011-06-17 23:17:28 -070039 typedef std::pair<std::string /*ifaceName*/, int64_t /*quota*/> QuotaInfo;
JP Abgrall26e0d492011-06-24 19:21:51 -070040 enum IptIpVer { IptIpV4, IptIpV6 };
41 enum IptOp { IptOpInsert, IptOpReplace, IptOpDelete };
42 enum IptRejectOp { IptRejectAdd, IptRejectNoAdd };
43 enum NaughtyAppOp { NaughtyAppOpAdd, NaughtyAppOpRemove };
44 enum QuotaType { QuotaUnique, QuotaShared };
45 enum RunCmdErrHandling { RunCmdFailureBad, RunCmdFailureOk };
JP Abgrall0dad7c22011-06-24 11:58:14 -070046
JP Abgrallfa6f46d2011-06-17 23:17:28 -070047 int64_t sharedQuotaBytes;
JP Abgrall0dad7c22011-06-24 11:58:14 -070048 std::list<std::string> sharedQuotaIfaces;
49
50 std::list<QuotaInfo> quotaIfaces;
51
JP Abgrallfa6f46d2011-06-17 23:17:28 -070052 std::list<int /*appUid*/> naughtyAppUids;
JP Abgrall26e0d492011-06-24 19:21:51 -070053 int maninpulateNaughtyApps(int numUids, char *appStrUids[], NaughtyAppOp appOp);
JP Abgrall4a5f5ca2011-06-15 18:37:39 -070054
JP Abgrall26e0d492011-06-24 19:21:51 -070055 int prepCostlyIface(const char *ifn, QuotaType quotaType);
56 int cleanupCostlyIface(const char *ifn, QuotaType quotaType);
JP Abgrall0dad7c22011-06-24 11:58:14 -070057
58 std::string makeIptablesNaughtyCmd(IptOp op, int uid);
JP Abgrall26e0d492011-06-24 19:21:51 -070059 std::string makeIptablesQuotaCmd(IptOp op, const char *costName, int64_t quota);
JP Abgrall0dad7c22011-06-24 11:58:14 -070060
61 /* Runs for both ipv4 and ipv6 iptables */
JP Abgrall26e0d492011-06-24 19:21:51 -070062 int runCommands(int numCommands, const char *commands[], RunCmdErrHandling cmdErrHandling);
JP Abgrall0dad7c22011-06-24 11:58:14 -070063 /* Runs for both ipv4 and ipv6 iptables, appends -j REJECT --reject-with ... */
JP Abgrall26e0d492011-06-24 19:21:51 -070064 static int runIpxtablesCmd(const char *cmd, IptRejectOp rejectHandling);
65 static int runIptablesCmd(const char *cmd, IptRejectOp rejectHandling, IptIpVer iptIpVer);
66
67 // Provides strncpy() + check overflow.
68 static int StrncpyAndCheck(char *buffer, const char *src, size_t buffSize);
JP Abgrall0dad7c22011-06-24 11:58:14 -070069
JP Abgrall4a5f5ca2011-06-15 18:37:39 -070070private:
JP Abgrallfa6f46d2011-06-17 23:17:28 -070071 static const char *cleanupCommands[];
72 static const char *setupCommands[];
73 static const char *basicAccountingCommands[];
74 static const int MAX_CMD_LEN;
75 static const int MAX_IFACENAME_LEN;
76 static const int MAX_CMD_ARGS;
77 static const char IPTABLES_PATH[];
78 static const char IP6TABLES_PATH[];
JP Abgrall4a5f5ca2011-06-15 18:37:39 -070079
JP Abgrall4a5f5ca2011-06-15 18:37:39 -070080};
81
82#endif