BandwidthController.h revision db7da58e8d2aa021060098057f944ef754be06e3
14a5f5ca3c9e07fc3e6feca2afde07f41a8a64f11JP Abgrall/* 24a5f5ca3c9e07fc3e6feca2afde07f41a8a64f11JP Abgrall * Copyright (C) 2011 The Android Open Source Project 34a5f5ca3c9e07fc3e6feca2afde07f41a8a64f11JP Abgrall * 44a5f5ca3c9e07fc3e6feca2afde07f41a8a64f11JP Abgrall * Licensed under the Apache License, Version 2.0 (the "License"); 54a5f5ca3c9e07fc3e6feca2afde07f41a8a64f11JP Abgrall * you may not use this file except in compliance with the License. 64a5f5ca3c9e07fc3e6feca2afde07f41a8a64f11JP Abgrall * You may obtain a copy of the License at 74a5f5ca3c9e07fc3e6feca2afde07f41a8a64f11JP Abgrall * 84a5f5ca3c9e07fc3e6feca2afde07f41a8a64f11JP Abgrall * http://www.apache.org/licenses/LICENSE-2.0 94a5f5ca3c9e07fc3e6feca2afde07f41a8a64f11JP Abgrall * 104a5f5ca3c9e07fc3e6feca2afde07f41a8a64f11JP Abgrall * Unless required by applicable law or agreed to in writing, software 114a5f5ca3c9e07fc3e6feca2afde07f41a8a64f11JP Abgrall * distributed under the License is distributed on an "AS IS" BASIS, 124a5f5ca3c9e07fc3e6feca2afde07f41a8a64f11JP Abgrall * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 134a5f5ca3c9e07fc3e6feca2afde07f41a8a64f11JP Abgrall * See the License for the specific language governing permissions and 144a5f5ca3c9e07fc3e6feca2afde07f41a8a64f11JP Abgrall * limitations under the License. 154a5f5ca3c9e07fc3e6feca2afde07f41a8a64f11JP Abgrall */ 164a5f5ca3c9e07fc3e6feca2afde07f41a8a64f11JP Abgrall#ifndef _BANDWIDTH_CONTROLLER_H 174a5f5ca3c9e07fc3e6feca2afde07f41a8a64f11JP Abgrall#define _BANDWIDTH_CONTROLLER_H 184a5f5ca3c9e07fc3e6feca2afde07f41a8a64f11JP Abgrall 194a5f5ca3c9e07fc3e6feca2afde07f41a8a64f11JP Abgrall#include <list> 204a5f5ca3c9e07fc3e6feca2afde07f41a8a64f11JP Abgrall#include <string> 21fa6f46d3370ae5475fc3bc8273bbe04ee7348d60JP Abgrall#include <utility> // for pair 22db7da58e8d2aa021060098057f944ef754be06e3JP Abgrall 234a5f5ca3c9e07fc3e6feca2afde07f41a8a64f11JP Abgrallclass BandwidthController { 244a5f5ca3c9e07fc3e6feca2afde07f41a8a64f11JP Abgrallpublic: 25db7da58e8d2aa021060098057f944ef754be06e3JP Abgrall class TetherStats { 26db7da58e8d2aa021060098057f944ef754be06e3JP Abgrall public: 27db7da58e8d2aa021060098057f944ef754be06e3JP Abgrall TetherStats(void) 28db7da58e8d2aa021060098057f944ef754be06e3JP Abgrall : rxBytes(-1), rxPackets(-1), 29db7da58e8d2aa021060098057f944ef754be06e3JP Abgrall txBytes(-1), txPackets(-1) {}; 30db7da58e8d2aa021060098057f944ef754be06e3JP Abgrall TetherStats(std::string ifnIn, std::string ifnOut, 31db7da58e8d2aa021060098057f944ef754be06e3JP Abgrall int64_t rxB, int64_t rxP, 32db7da58e8d2aa021060098057f944ef754be06e3JP Abgrall int64_t txB, int64_t txP) 33db7da58e8d2aa021060098057f944ef754be06e3JP Abgrall : ifaceIn(ifnIn), ifaceOut(ifnOut), 34db7da58e8d2aa021060098057f944ef754be06e3JP Abgrall rxBytes(rxB), rxPackets(rxP), 35db7da58e8d2aa021060098057f944ef754be06e3JP Abgrall txBytes(txB), txPackets(txP) {}; 36db7da58e8d2aa021060098057f944ef754be06e3JP Abgrall std::string ifaceIn; 37db7da58e8d2aa021060098057f944ef754be06e3JP Abgrall std::string ifaceOut; 38db7da58e8d2aa021060098057f944ef754be06e3JP Abgrall int64_t rxBytes, rxPackets; 39db7da58e8d2aa021060098057f944ef754be06e3JP Abgrall int64_t txBytes, txPackets; 40db7da58e8d2aa021060098057f944ef754be06e3JP Abgrall /* 41db7da58e8d2aa021060098057f944ef754be06e3JP Abgrall * Allocates a new string representing this: 42db7da58e8d2aa021060098057f944ef754be06e3JP Abgrall * ifaceIn ifaceOut rx_bytes rx_packets tx_bytes tx_packets 43db7da58e8d2aa021060098057f944ef754be06e3JP Abgrall * The caller is responsible for free()'ing the returned ptr. 44db7da58e8d2aa021060098057f944ef754be06e3JP Abgrall */ 45db7da58e8d2aa021060098057f944ef754be06e3JP Abgrall char *getStatsLine(void); 46db7da58e8d2aa021060098057f944ef754be06e3JP Abgrall }; 47db7da58e8d2aa021060098057f944ef754be06e3JP Abgrall 48fa6f46d3370ae5475fc3bc8273bbe04ee7348d60JP Abgrall BandwidthController(); 49fa6f46d3370ae5475fc3bc8273bbe04ee7348d60JP Abgrall int enableBandwidthControl(void); 50fa6f46d3370ae5475fc3bc8273bbe04ee7348d60JP Abgrall int disableBandwidthControl(void); 51fa6f46d3370ae5475fc3bc8273bbe04ee7348d60JP Abgrall 520dad7c2f1f6994fbe5e85b9e1fc72d29d6453211JP Abgrall int setInterfaceSharedQuota(const char *iface, int64_t bytes); 538a93272255f1b7e3083a97e1e28ddf675c0c7fb0JP Abgrall int getInterfaceSharedQuota(int64_t *bytes); 54fa6f46d3370ae5475fc3bc8273bbe04ee7348d60JP Abgrall int removeInterfaceSharedQuota(const char *iface); 55fa6f46d3370ae5475fc3bc8273bbe04ee7348d60JP Abgrall 560dad7c2f1f6994fbe5e85b9e1fc72d29d6453211JP Abgrall int setInterfaceQuota(const char *iface, int64_t bytes); 578a93272255f1b7e3083a97e1e28ddf675c0c7fb0JP Abgrall int getInterfaceQuota(const char *iface, int64_t *bytes); 580dad7c2f1f6994fbe5e85b9e1fc72d29d6453211JP Abgrall int removeInterfaceQuota(const char *iface); 590dad7c2f1f6994fbe5e85b9e1fc72d29d6453211JP Abgrall 60fa6f46d3370ae5475fc3bc8273bbe04ee7348d60JP Abgrall int addNaughtyApps(int numUids, char *appUids[]); 61fa6f46d3370ae5475fc3bc8273bbe04ee7348d60JP Abgrall int removeNaughtyApps(int numUids, char *appUids[]); 624a5f5ca3c9e07fc3e6feca2afde07f41a8a64f11JP Abgrall 638a93272255f1b7e3083a97e1e28ddf675c0c7fb0JP Abgrall int setGlobalAlert(int64_t bytes); 648a93272255f1b7e3083a97e1e28ddf675c0c7fb0JP Abgrall int removeGlobalAlert(void); 658a93272255f1b7e3083a97e1e28ddf675c0c7fb0JP Abgrall 668a93272255f1b7e3083a97e1e28ddf675c0c7fb0JP Abgrall int setSharedAlert(int64_t bytes); 678a93272255f1b7e3083a97e1e28ddf675c0c7fb0JP Abgrall int removeSharedAlert(void); 688a93272255f1b7e3083a97e1e28ddf675c0c7fb0JP Abgrall 698a93272255f1b7e3083a97e1e28ddf675c0c7fb0JP Abgrall int setInterfaceAlert(const char *iface, int64_t bytes); 708a93272255f1b7e3083a97e1e28ddf675c0c7fb0JP Abgrall int removeInterfaceAlert(const char *iface); 710dad7c2f1f6994fbe5e85b9e1fc72d29d6453211JP Abgrall 72db7da58e8d2aa021060098057f944ef754be06e3JP Abgrall /* 73db7da58e8d2aa021060098057f944ef754be06e3JP Abgrall * stats should have ifaceIn and ifaceOut initialized. 74db7da58e8d2aa021060098057f944ef754be06e3JP Abgrall * Byte counts should be left to the default (-1). 75db7da58e8d2aa021060098057f944ef754be06e3JP Abgrall */ 76db7da58e8d2aa021060098057f944ef754be06e3JP Abgrall int getTetherStats(TetherStats &stats); 77db7da58e8d2aa021060098057f944ef754be06e3JP Abgrall 784a5f5ca3c9e07fc3e6feca2afde07f41a8a64f11JP Abgrallprotected: 798a93272255f1b7e3083a97e1e28ddf675c0c7fb0JP Abgrall class QuotaInfo { 808a93272255f1b7e3083a97e1e28ddf675c0c7fb0JP Abgrall public: 818a93272255f1b7e3083a97e1e28ddf675c0c7fb0JP Abgrall QuotaInfo(std::string ifn, int64_t q, int64_t a) 828a93272255f1b7e3083a97e1e28ddf675c0c7fb0JP Abgrall : ifaceName(ifn), quota(q), alert(a) {}; 838a93272255f1b7e3083a97e1e28ddf675c0c7fb0JP Abgrall std::string ifaceName; 848a93272255f1b7e3083a97e1e28ddf675c0c7fb0JP Abgrall int64_t quota; 858a93272255f1b7e3083a97e1e28ddf675c0c7fb0JP Abgrall int64_t alert; 868a93272255f1b7e3083a97e1e28ddf675c0c7fb0JP Abgrall }; 87db7da58e8d2aa021060098057f944ef754be06e3JP Abgrall 8826e0d49fa743d7881104196a9eda733bd2aac92fJP Abgrall enum IptIpVer { IptIpV4, IptIpV6 }; 8926e0d49fa743d7881104196a9eda733bd2aac92fJP Abgrall enum IptOp { IptOpInsert, IptOpReplace, IptOpDelete }; 9026e0d49fa743d7881104196a9eda733bd2aac92fJP Abgrall enum IptRejectOp { IptRejectAdd, IptRejectNoAdd }; 9126e0d49fa743d7881104196a9eda733bd2aac92fJP Abgrall enum NaughtyAppOp { NaughtyAppOpAdd, NaughtyAppOpRemove }; 9226e0d49fa743d7881104196a9eda733bd2aac92fJP Abgrall enum QuotaType { QuotaUnique, QuotaShared }; 9326e0d49fa743d7881104196a9eda733bd2aac92fJP Abgrall enum RunCmdErrHandling { RunCmdFailureBad, RunCmdFailureOk }; 940dad7c2f1f6994fbe5e85b9e1fc72d29d6453211JP Abgrall 9526e0d49fa743d7881104196a9eda733bd2aac92fJP Abgrall int maninpulateNaughtyApps(int numUids, char *appStrUids[], NaughtyAppOp appOp); 964a5f5ca3c9e07fc3e6feca2afde07f41a8a64f11JP Abgrall 9726e0d49fa743d7881104196a9eda733bd2aac92fJP Abgrall int prepCostlyIface(const char *ifn, QuotaType quotaType); 9826e0d49fa743d7881104196a9eda733bd2aac92fJP Abgrall int cleanupCostlyIface(const char *ifn, QuotaType quotaType); 990dad7c2f1f6994fbe5e85b9e1fc72d29d6453211JP Abgrall 1000dad7c2f1f6994fbe5e85b9e1fc72d29d6453211JP Abgrall std::string makeIptablesNaughtyCmd(IptOp op, int uid); 10126e0d49fa743d7881104196a9eda733bd2aac92fJP Abgrall std::string makeIptablesQuotaCmd(IptOp op, const char *costName, int64_t quota); 1020dad7c2f1f6994fbe5e85b9e1fc72d29d6453211JP Abgrall 1038a93272255f1b7e3083a97e1e28ddf675c0c7fb0JP Abgrall int runIptablesAlertCmd(IptOp op, const char *alertName, int64_t bytes); 1048a93272255f1b7e3083a97e1e28ddf675c0c7fb0JP Abgrall 1050dad7c2f1f6994fbe5e85b9e1fc72d29d6453211JP Abgrall /* Runs for both ipv4 and ipv6 iptables */ 10626e0d49fa743d7881104196a9eda733bd2aac92fJP Abgrall int runCommands(int numCommands, const char *commands[], RunCmdErrHandling cmdErrHandling); 1070dad7c2f1f6994fbe5e85b9e1fc72d29d6453211JP Abgrall /* Runs for both ipv4 and ipv6 iptables, appends -j REJECT --reject-with ... */ 10826e0d49fa743d7881104196a9eda733bd2aac92fJP Abgrall static int runIpxtablesCmd(const char *cmd, IptRejectOp rejectHandling); 10926e0d49fa743d7881104196a9eda733bd2aac92fJP Abgrall static int runIptablesCmd(const char *cmd, IptRejectOp rejectHandling, IptIpVer iptIpVer); 11026e0d49fa743d7881104196a9eda733bd2aac92fJP Abgrall 11126e0d49fa743d7881104196a9eda733bd2aac92fJP Abgrall // Provides strncpy() + check overflow. 11226e0d49fa743d7881104196a9eda733bd2aac92fJP Abgrall static int StrncpyAndCheck(char *buffer, const char *src, size_t buffSize); 1130dad7c2f1f6994fbe5e85b9e1fc72d29d6453211JP Abgrall 1148a93272255f1b7e3083a97e1e28ddf675c0c7fb0JP Abgrall int updateQuota(const char *alertName, int64_t bytes); 1158a93272255f1b7e3083a97e1e28ddf675c0c7fb0JP Abgrall 1168a93272255f1b7e3083a97e1e28ddf675c0c7fb0JP Abgrall int setCostlyAlert(const char *costName, int64_t bytes, int64_t *alertBytes); 1178a93272255f1b7e3083a97e1e28ddf675c0c7fb0JP Abgrall int removeCostlyAlert(const char *costName, int64_t *alertBytes); 1188a93272255f1b7e3083a97e1e28ddf675c0c7fb0JP Abgrall 11911b4e9b26fe7b878992162afb39f5a8acfd143edJP Abgrall /* 120db7da58e8d2aa021060098057f944ef754be06e3JP Abgrall * stats should have ifaceIn and ifaceOut initialized. 121db7da58e8d2aa021060098057f944ef754be06e3JP Abgrall * fp should be a file to the FORWARD rules of iptables. 12211b4e9b26fe7b878992162afb39f5a8acfd143edJP Abgrall */ 123db7da58e8d2aa021060098057f944ef754be06e3JP Abgrall static int parseForwardChainStats(TetherStats &stats, FILE *fp); 124db7da58e8d2aa021060098057f944ef754be06e3JP Abgrall 125db7da58e8d2aa021060098057f944ef754be06e3JP Abgrall /*------------------*/ 126db7da58e8d2aa021060098057f944ef754be06e3JP Abgrall 127db7da58e8d2aa021060098057f944ef754be06e3JP Abgrall std::list<std::string> sharedQuotaIfaces; 128db7da58e8d2aa021060098057f944ef754be06e3JP Abgrall int64_t sharedQuotaBytes; 129db7da58e8d2aa021060098057f944ef754be06e3JP Abgrall int64_t sharedAlertBytes; 130db7da58e8d2aa021060098057f944ef754be06e3JP Abgrall int64_t globalAlertBytes; 131db7da58e8d2aa021060098057f944ef754be06e3JP Abgrall std::list<QuotaInfo> quotaIfaces; 132db7da58e8d2aa021060098057f944ef754be06e3JP Abgrall std::list<int /*appUid*/> naughtyAppUids; 1338a93272255f1b7e3083a97e1e28ddf675c0c7fb0JP Abgrall 1344a5f5ca3c9e07fc3e6feca2afde07f41a8a64f11JP Abgrallprivate: 135db7da58e8d2aa021060098057f944ef754be06e3JP Abgrall static const char *IPT_CLEANUP_COMMANDS[]; 136db7da58e8d2aa021060098057f944ef754be06e3JP Abgrall static const char *IPT_SETUP_COMMANDS[]; 137db7da58e8d2aa021060098057f944ef754be06e3JP Abgrall static const char *IPT_BASIC_ACCOUNTING_COMMANDS[]; 138db7da58e8d2aa021060098057f944ef754be06e3JP Abgrall 139db7da58e8d2aa021060098057f944ef754be06e3JP Abgrall /* Alphabetical */ 1408a93272255f1b7e3083a97e1e28ddf675c0c7fb0JP Abgrall static const char ALERT_IPT_TEMPLATE[]; 141db7da58e8d2aa021060098057f944ef754be06e3JP Abgrall static const int ALERT_RULE_POS_IN_COSTLY_CHAIN; 142db7da58e8d2aa021060098057f944ef754be06e3JP Abgrall static const char IP6TABLES_PATH[]; 143db7da58e8d2aa021060098057f944ef754be06e3JP Abgrall static const char IPTABLES_PATH[]; 144db7da58e8d2aa021060098057f944ef754be06e3JP Abgrall static const int MAX_CMD_ARGS; 145db7da58e8d2aa021060098057f944ef754be06e3JP Abgrall static const int MAX_CMD_LEN; 146db7da58e8d2aa021060098057f944ef754be06e3JP Abgrall static const int MAX_IFACENAME_LEN; 147db7da58e8d2aa021060098057f944ef754be06e3JP Abgrall static const int MAX_IPT_OUTPUT_LINE_LEN; 148db7da58e8d2aa021060098057f944ef754be06e3JP Abgrall 149db7da58e8d2aa021060098057f944ef754be06e3JP Abgrall /* 150db7da58e8d2aa021060098057f944ef754be06e3JP Abgrall * When false, it will directly use system() instead of logwrap() 151db7da58e8d2aa021060098057f944ef754be06e3JP Abgrall */ 152db7da58e8d2aa021060098057f944ef754be06e3JP Abgrall static bool useLogwrapCall; 1534a5f5ca3c9e07fc3e6feca2afde07f41a8a64f11JP Abgrall}; 1544a5f5ca3c9e07fc3e6feca2afde07f41a8a64f11JP Abgrall 1554a5f5ca3c9e07fc3e6feca2afde07f41a8a64f11JP Abgrall#endif 156