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 | 8a93272 | 2011-07-13 19:17:35 -0700 | [diff] [blame^] | 29 | int getInterfaceSharedQuota(int64_t *bytes); |
JP Abgrall | fa6f46d | 2011-06-17 23:17:28 -0700 | [diff] [blame] | 30 | int removeInterfaceSharedQuota(const char *iface); |
| 31 | |
JP Abgrall | 0dad7c2 | 2011-06-24 11:58:14 -0700 | [diff] [blame] | 32 | int setInterfaceQuota(const char *iface, int64_t bytes); |
JP Abgrall | 8a93272 | 2011-07-13 19:17:35 -0700 | [diff] [blame^] | 33 | int getInterfaceQuota(const char *iface, int64_t *bytes); |
JP Abgrall | 0dad7c2 | 2011-06-24 11:58:14 -0700 | [diff] [blame] | 34 | int removeInterfaceQuota(const char *iface); |
| 35 | |
JP Abgrall | fa6f46d | 2011-06-17 23:17:28 -0700 | [diff] [blame] | 36 | int addNaughtyApps(int numUids, char *appUids[]); |
| 37 | int removeNaughtyApps(int numUids, char *appUids[]); |
JP Abgrall | 4a5f5ca | 2011-06-15 18:37:39 -0700 | [diff] [blame] | 38 | |
JP Abgrall | 8a93272 | 2011-07-13 19:17:35 -0700 | [diff] [blame^] | 39 | int setGlobalAlert(int64_t bytes); |
| 40 | int removeGlobalAlert(void); |
| 41 | |
| 42 | int setSharedAlert(int64_t bytes); |
| 43 | int removeSharedAlert(void); |
| 44 | |
| 45 | int setInterfaceAlert(const char *iface, int64_t bytes); |
| 46 | int removeInterfaceAlert(const char *iface); |
JP Abgrall | 0dad7c2 | 2011-06-24 11:58:14 -0700 | [diff] [blame] | 47 | |
JP Abgrall | 4a5f5ca | 2011-06-15 18:37:39 -0700 | [diff] [blame] | 48 | protected: |
JP Abgrall | 8a93272 | 2011-07-13 19:17:35 -0700 | [diff] [blame^] | 49 | class QuotaInfo { |
| 50 | public: |
| 51 | QuotaInfo(std::string ifn, int64_t q, int64_t a) |
| 52 | : ifaceName(ifn), quota(q), alert(a) {}; |
| 53 | std::string ifaceName; |
| 54 | int64_t quota; |
| 55 | int64_t alert; |
| 56 | }; |
JP Abgrall | 26e0d49 | 2011-06-24 19:21:51 -0700 | [diff] [blame] | 57 | enum IptIpVer { IptIpV4, IptIpV6 }; |
| 58 | enum IptOp { IptOpInsert, IptOpReplace, IptOpDelete }; |
| 59 | enum IptRejectOp { IptRejectAdd, IptRejectNoAdd }; |
| 60 | enum NaughtyAppOp { NaughtyAppOpAdd, NaughtyAppOpRemove }; |
| 61 | enum QuotaType { QuotaUnique, QuotaShared }; |
| 62 | enum RunCmdErrHandling { RunCmdFailureBad, RunCmdFailureOk }; |
JP Abgrall | 0dad7c2 | 2011-06-24 11:58:14 -0700 | [diff] [blame] | 63 | |
JP Abgrall | 0dad7c2 | 2011-06-24 11:58:14 -0700 | [diff] [blame] | 64 | std::list<std::string> sharedQuotaIfaces; |
JP Abgrall | 8a93272 | 2011-07-13 19:17:35 -0700 | [diff] [blame^] | 65 | int64_t sharedQuotaBytes; |
| 66 | int64_t sharedAlertBytes; |
JP Abgrall | 0dad7c2 | 2011-06-24 11:58:14 -0700 | [diff] [blame] | 67 | |
| 68 | std::list<QuotaInfo> quotaIfaces; |
| 69 | |
JP Abgrall | fa6f46d | 2011-06-17 23:17:28 -0700 | [diff] [blame] | 70 | std::list<int /*appUid*/> naughtyAppUids; |
JP Abgrall | 26e0d49 | 2011-06-24 19:21:51 -0700 | [diff] [blame] | 71 | int maninpulateNaughtyApps(int numUids, char *appStrUids[], NaughtyAppOp appOp); |
JP Abgrall | 4a5f5ca | 2011-06-15 18:37:39 -0700 | [diff] [blame] | 72 | |
JP Abgrall | 26e0d49 | 2011-06-24 19:21:51 -0700 | [diff] [blame] | 73 | int prepCostlyIface(const char *ifn, QuotaType quotaType); |
| 74 | int cleanupCostlyIface(const char *ifn, QuotaType quotaType); |
JP Abgrall | 0dad7c2 | 2011-06-24 11:58:14 -0700 | [diff] [blame] | 75 | |
| 76 | std::string makeIptablesNaughtyCmd(IptOp op, int uid); |
JP Abgrall | 26e0d49 | 2011-06-24 19:21:51 -0700 | [diff] [blame] | 77 | std::string makeIptablesQuotaCmd(IptOp op, const char *costName, int64_t quota); |
JP Abgrall | 0dad7c2 | 2011-06-24 11:58:14 -0700 | [diff] [blame] | 78 | |
JP Abgrall | 8a93272 | 2011-07-13 19:17:35 -0700 | [diff] [blame^] | 79 | int runIptablesAlertCmd(IptOp op, const char *alertName, int64_t bytes); |
| 80 | |
JP Abgrall | 0dad7c2 | 2011-06-24 11:58:14 -0700 | [diff] [blame] | 81 | /* Runs for both ipv4 and ipv6 iptables */ |
JP Abgrall | 26e0d49 | 2011-06-24 19:21:51 -0700 | [diff] [blame] | 82 | int runCommands(int numCommands, const char *commands[], RunCmdErrHandling cmdErrHandling); |
JP Abgrall | 0dad7c2 | 2011-06-24 11:58:14 -0700 | [diff] [blame] | 83 | /* Runs for both ipv4 and ipv6 iptables, appends -j REJECT --reject-with ... */ |
JP Abgrall | 26e0d49 | 2011-06-24 19:21:51 -0700 | [diff] [blame] | 84 | static int runIpxtablesCmd(const char *cmd, IptRejectOp rejectHandling); |
| 85 | static int runIptablesCmd(const char *cmd, IptRejectOp rejectHandling, IptIpVer iptIpVer); |
| 86 | |
| 87 | // Provides strncpy() + check overflow. |
| 88 | static int StrncpyAndCheck(char *buffer, const char *src, size_t buffSize); |
JP Abgrall | 0dad7c2 | 2011-06-24 11:58:14 -0700 | [diff] [blame] | 89 | |
JP Abgrall | 8a93272 | 2011-07-13 19:17:35 -0700 | [diff] [blame^] | 90 | int updateQuota(const char *alertName, int64_t bytes); |
| 91 | |
| 92 | int64_t globalAlertBytes; |
| 93 | int setCostlyAlert(const char *costName, int64_t bytes, int64_t *alertBytes); |
| 94 | int removeCostlyAlert(const char *costName, int64_t *alertBytes); |
| 95 | |
| 96 | |
JP Abgrall | 4a5f5ca | 2011-06-15 18:37:39 -0700 | [diff] [blame] | 97 | private: |
JP Abgrall | fa6f46d | 2011-06-17 23:17:28 -0700 | [diff] [blame] | 98 | static const char *cleanupCommands[]; |
| 99 | static const char *setupCommands[]; |
| 100 | static const char *basicAccountingCommands[]; |
| 101 | static const int MAX_CMD_LEN; |
| 102 | static const int MAX_IFACENAME_LEN; |
| 103 | static const int MAX_CMD_ARGS; |
| 104 | static const char IPTABLES_PATH[]; |
| 105 | static const char IP6TABLES_PATH[]; |
JP Abgrall | 8a93272 | 2011-07-13 19:17:35 -0700 | [diff] [blame^] | 106 | static const char ALERT_IPT_TEMPLATE[]; |
| 107 | static const int ALERT_RULE_POS_IN_COSTLY_CHAIN; |
JP Abgrall | 4a5f5ca | 2011-06-15 18:37:39 -0700 | [diff] [blame] | 108 | }; |
| 109 | |
| 110 | #endif |