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 | |
| 19 | #include <list> |
| 20 | #include <string> |
JP Abgrall | fa6f46d | 2011-06-17 23:17:28 -0700 | [diff] [blame] | 21 | #include <utility> // for pair |
JP Abgrall | 4a5f5ca | 2011-06-15 18:37:39 -0700 | [diff] [blame] | 22 | class BandwidthController { |
| 23 | public: |
JP Abgrall | fa6f46d | 2011-06-17 23:17:28 -0700 | [diff] [blame] | 24 | BandwidthController(); |
| 25 | int enableBandwidthControl(void); |
| 26 | int disableBandwidthControl(void); |
| 27 | |
JP Abgrall | 0dad7c2 | 2011-06-24 11:58:14 -0700 | [diff] [blame^] | 28 | int setInterfaceSharedQuota(const char *iface, int64_t bytes); |
JP Abgrall | fa6f46d | 2011-06-17 23:17:28 -0700 | [diff] [blame] | 29 | int removeInterfaceSharedQuota(const char *iface); |
| 30 | |
JP Abgrall | 0dad7c2 | 2011-06-24 11:58:14 -0700 | [diff] [blame^] | 31 | int setInterfaceQuota(const char *iface, int64_t bytes); |
| 32 | int removeInterfaceQuota(const char *iface); |
| 33 | |
JP Abgrall | fa6f46d | 2011-06-17 23:17:28 -0700 | [diff] [blame] | 34 | int addNaughtyApps(int numUids, char *appUids[]); |
| 35 | int removeNaughtyApps(int numUids, char *appUids[]); |
JP Abgrall | 4a5f5ca | 2011-06-15 18:37:39 -0700 | [diff] [blame] | 36 | |
JP Abgrall | 0dad7c2 | 2011-06-24 11:58:14 -0700 | [diff] [blame^] | 37 | |
JP Abgrall | 4a5f5ca | 2011-06-15 18:37:39 -0700 | [diff] [blame] | 38 | protected: |
JP Abgrall | fa6f46d | 2011-06-17 23:17:28 -0700 | [diff] [blame] | 39 | typedef std::pair<std::string /*ifaceName*/, int64_t /*quota*/> QuotaInfo; |
| 40 | enum IptOp {IptOpInsert, IptOpReplace, IptOpDelete}; |
JP Abgrall | 0dad7c2 | 2011-06-24 11:58:14 -0700 | [diff] [blame^] | 41 | |
JP Abgrall | fa6f46d | 2011-06-17 23:17:28 -0700 | [diff] [blame] | 42 | int64_t sharedQuotaBytes; |
JP Abgrall | 0dad7c2 | 2011-06-24 11:58:14 -0700 | [diff] [blame^] | 43 | std::list<std::string> sharedQuotaIfaces; |
| 44 | |
| 45 | std::list<QuotaInfo> quotaIfaces; |
| 46 | |
JP Abgrall | fa6f46d | 2011-06-17 23:17:28 -0700 | [diff] [blame] | 47 | std::list<int /*appUid*/> naughtyAppUids; |
JP Abgrall | fa6f46d | 2011-06-17 23:17:28 -0700 | [diff] [blame] | 48 | int maninpulateNaughtyApps(int numUids, char *appStrUids[], bool doAdd); |
JP Abgrall | 4a5f5ca | 2011-06-15 18:37:39 -0700 | [diff] [blame] | 49 | |
JP Abgrall | 0dad7c2 | 2011-06-24 11:58:14 -0700 | [diff] [blame^] | 50 | int prepCostlyIface(const char *ifn, bool isShared); |
| 51 | int cleanupCostlyIface(const char *ifn, bool isShared); |
| 52 | |
| 53 | std::string makeIptablesNaughtyCmd(IptOp op, int uid); |
| 54 | std::string makeIptablesQuotaCmd(IptOp op, char *costName, int64_t quota); |
| 55 | |
| 56 | /* Runs for both ipv4 and ipv6 iptables */ |
| 57 | int runCommands(int numCommands, const char *commands[], bool allowFailure); |
| 58 | /* Runs for both ipv4 and ipv6 iptables, appends -j REJECT --reject-with ... */ |
| 59 | static int runIpxtablesCmd(const char *cmd, bool appendReject); |
| 60 | static int runIptablesCmd(const char *cmd, bool appendReject, bool isIp6); |
| 61 | |
JP Abgrall | 4a5f5ca | 2011-06-15 18:37:39 -0700 | [diff] [blame] | 62 | private: |
JP Abgrall | fa6f46d | 2011-06-17 23:17:28 -0700 | [diff] [blame] | 63 | static const char *cleanupCommands[]; |
| 64 | static const char *setupCommands[]; |
| 65 | static const char *basicAccountingCommands[]; |
| 66 | static const int MAX_CMD_LEN; |
| 67 | static const int MAX_IFACENAME_LEN; |
| 68 | static const int MAX_CMD_ARGS; |
| 69 | static const char IPTABLES_PATH[]; |
| 70 | static const char IP6TABLES_PATH[]; |
JP Abgrall | 4a5f5ca | 2011-06-15 18:37:39 -0700 | [diff] [blame] | 71 | |
JP Abgrall | 4a5f5ca | 2011-06-15 18:37:39 -0700 | [diff] [blame] | 72 | }; |
| 73 | |
| 74 | #endif |