blob: 405e2ef9747e4d1cab16cfc594443ed049d66622 [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
28 int setInterfaceSharedQuota(int64_t bytes, const char *iface);
29 int removeInterfaceSharedQuota(const char *iface);
30
31 int addNaughtyApps(int numUids, char *appUids[]);
32 int removeNaughtyApps(int numUids, char *appUids[]);
JP Abgrall4a5f5ca2011-06-15 18:37:39 -070033
34protected:
JP Abgrallfa6f46d2011-06-17 23:17:28 -070035 int runCommands(int numCommands, const char *commands[],
36 bool allowFailure = false, bool isIpv6 = false);
37 typedef std::pair<std::string /*ifaceName*/, int64_t /*quota*/> QuotaInfo;
38 enum IptOp {IptOpInsert, IptOpReplace, IptOpDelete};
39 int64_t sharedQuotaBytes;
40 std::list<QuotaInfo> ifaceRules;
41 std::list<int /*appUid*/> naughtyAppUids;
42 std::string makeIptablesNaughtyCmd(IptOp op, int uid, bool isIp6);
43 std::string makeIptablesQuotaCmd(IptOp op, char *costName, int64_t quota, bool isIp6);
44 int maninpulateNaughtyApps(int numUids, char *appStrUids[], bool doAdd);
JP Abgrall4a5f5ca2011-06-15 18:37:39 -070045
46private:
JP Abgrallfa6f46d2011-06-17 23:17:28 -070047 static const char *cleanupCommands[];
48 static const char *setupCommands[];
49 static const char *basicAccountingCommands[];
50 static const int MAX_CMD_LEN;
51 static const int MAX_IFACENAME_LEN;
52 static const int MAX_CMD_ARGS;
53 static const char IPTABLES_PATH[];
54 static const char IP6TABLES_PATH[];
JP Abgrall4a5f5ca2011-06-15 18:37:39 -070055
JP Abgrallfa6f46d2011-06-17 23:17:28 -070056 static int runIptablesCmd(const char *cmd, bool isIp6 = false);
JP Abgrall4a5f5ca2011-06-15 18:37:39 -070057};
58
59#endif