BandwidthController.h revision 8a93272255f1b7e3083a97e1e28ddf675c0c7fb0
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
224a5f5ca3c9e07fc3e6feca2afde07f41a8a64f11JP Abgrallclass BandwidthController {
234a5f5ca3c9e07fc3e6feca2afde07f41a8a64f11JP Abgrallpublic:
24fa6f46d3370ae5475fc3bc8273bbe04ee7348d60JP Abgrall    BandwidthController();
25fa6f46d3370ae5475fc3bc8273bbe04ee7348d60JP Abgrall    int enableBandwidthControl(void);
26fa6f46d3370ae5475fc3bc8273bbe04ee7348d60JP Abgrall    int disableBandwidthControl(void);
27fa6f46d3370ae5475fc3bc8273bbe04ee7348d60JP Abgrall
280dad7c2f1f6994fbe5e85b9e1fc72d29d6453211JP Abgrall    int setInterfaceSharedQuota(const char *iface, int64_t bytes);
298a93272255f1b7e3083a97e1e28ddf675c0c7fb0JP Abgrall    int getInterfaceSharedQuota(int64_t *bytes);
30fa6f46d3370ae5475fc3bc8273bbe04ee7348d60JP Abgrall    int removeInterfaceSharedQuota(const char *iface);
31fa6f46d3370ae5475fc3bc8273bbe04ee7348d60JP Abgrall
320dad7c2f1f6994fbe5e85b9e1fc72d29d6453211JP Abgrall    int setInterfaceQuota(const char *iface, int64_t bytes);
338a93272255f1b7e3083a97e1e28ddf675c0c7fb0JP Abgrall    int getInterfaceQuota(const char *iface, int64_t *bytes);
340dad7c2f1f6994fbe5e85b9e1fc72d29d6453211JP Abgrall    int removeInterfaceQuota(const char *iface);
350dad7c2f1f6994fbe5e85b9e1fc72d29d6453211JP Abgrall
36fa6f46d3370ae5475fc3bc8273bbe04ee7348d60JP Abgrall    int addNaughtyApps(int numUids, char *appUids[]);
37fa6f46d3370ae5475fc3bc8273bbe04ee7348d60JP Abgrall    int removeNaughtyApps(int numUids, char *appUids[]);
384a5f5ca3c9e07fc3e6feca2afde07f41a8a64f11JP Abgrall
398a93272255f1b7e3083a97e1e28ddf675c0c7fb0JP Abgrall    int setGlobalAlert(int64_t bytes);
408a93272255f1b7e3083a97e1e28ddf675c0c7fb0JP Abgrall    int removeGlobalAlert(void);
418a93272255f1b7e3083a97e1e28ddf675c0c7fb0JP Abgrall
428a93272255f1b7e3083a97e1e28ddf675c0c7fb0JP Abgrall    int setSharedAlert(int64_t bytes);
438a93272255f1b7e3083a97e1e28ddf675c0c7fb0JP Abgrall    int removeSharedAlert(void);
448a93272255f1b7e3083a97e1e28ddf675c0c7fb0JP Abgrall
458a93272255f1b7e3083a97e1e28ddf675c0c7fb0JP Abgrall    int setInterfaceAlert(const char *iface, int64_t bytes);
468a93272255f1b7e3083a97e1e28ddf675c0c7fb0JP Abgrall    int removeInterfaceAlert(const char *iface);
470dad7c2f1f6994fbe5e85b9e1fc72d29d6453211JP Abgrall
484a5f5ca3c9e07fc3e6feca2afde07f41a8a64f11JP Abgrallprotected:
498a93272255f1b7e3083a97e1e28ddf675c0c7fb0JP Abgrall    class QuotaInfo {
508a93272255f1b7e3083a97e1e28ddf675c0c7fb0JP Abgrall    public:
518a93272255f1b7e3083a97e1e28ddf675c0c7fb0JP Abgrall      QuotaInfo(std::string ifn, int64_t q, int64_t a)
528a93272255f1b7e3083a97e1e28ddf675c0c7fb0JP Abgrall              : ifaceName(ifn), quota(q), alert(a) {};
538a93272255f1b7e3083a97e1e28ddf675c0c7fb0JP Abgrall        std::string ifaceName;
548a93272255f1b7e3083a97e1e28ddf675c0c7fb0JP Abgrall        int64_t quota;
558a93272255f1b7e3083a97e1e28ddf675c0c7fb0JP Abgrall        int64_t alert;
568a93272255f1b7e3083a97e1e28ddf675c0c7fb0JP Abgrall    };
5726e0d49fa743d7881104196a9eda733bd2aac92fJP Abgrall    enum IptIpVer { IptIpV4, IptIpV6 };
5826e0d49fa743d7881104196a9eda733bd2aac92fJP Abgrall    enum IptOp { IptOpInsert, IptOpReplace, IptOpDelete };
5926e0d49fa743d7881104196a9eda733bd2aac92fJP Abgrall    enum IptRejectOp { IptRejectAdd, IptRejectNoAdd };
6026e0d49fa743d7881104196a9eda733bd2aac92fJP Abgrall    enum NaughtyAppOp { NaughtyAppOpAdd, NaughtyAppOpRemove };
6126e0d49fa743d7881104196a9eda733bd2aac92fJP Abgrall    enum QuotaType { QuotaUnique, QuotaShared };
6226e0d49fa743d7881104196a9eda733bd2aac92fJP Abgrall    enum RunCmdErrHandling { RunCmdFailureBad, RunCmdFailureOk };
630dad7c2f1f6994fbe5e85b9e1fc72d29d6453211JP Abgrall
640dad7c2f1f6994fbe5e85b9e1fc72d29d6453211JP Abgrall    std::list<std::string> sharedQuotaIfaces;
658a93272255f1b7e3083a97e1e28ddf675c0c7fb0JP Abgrall    int64_t sharedQuotaBytes;
668a93272255f1b7e3083a97e1e28ddf675c0c7fb0JP Abgrall    int64_t sharedAlertBytes;
670dad7c2f1f6994fbe5e85b9e1fc72d29d6453211JP Abgrall
680dad7c2f1f6994fbe5e85b9e1fc72d29d6453211JP Abgrall    std::list<QuotaInfo> quotaIfaces;
690dad7c2f1f6994fbe5e85b9e1fc72d29d6453211JP Abgrall
70fa6f46d3370ae5475fc3bc8273bbe04ee7348d60JP Abgrall    std::list<int /*appUid*/> naughtyAppUids;
7126e0d49fa743d7881104196a9eda733bd2aac92fJP Abgrall    int maninpulateNaughtyApps(int numUids, char *appStrUids[], NaughtyAppOp appOp);
724a5f5ca3c9e07fc3e6feca2afde07f41a8a64f11JP Abgrall
7326e0d49fa743d7881104196a9eda733bd2aac92fJP Abgrall    int prepCostlyIface(const char *ifn, QuotaType quotaType);
7426e0d49fa743d7881104196a9eda733bd2aac92fJP Abgrall    int cleanupCostlyIface(const char *ifn, QuotaType quotaType);
750dad7c2f1f6994fbe5e85b9e1fc72d29d6453211JP Abgrall
760dad7c2f1f6994fbe5e85b9e1fc72d29d6453211JP Abgrall    std::string makeIptablesNaughtyCmd(IptOp op, int uid);
7726e0d49fa743d7881104196a9eda733bd2aac92fJP Abgrall    std::string makeIptablesQuotaCmd(IptOp op, const char *costName, int64_t quota);
780dad7c2f1f6994fbe5e85b9e1fc72d29d6453211JP Abgrall
798a93272255f1b7e3083a97e1e28ddf675c0c7fb0JP Abgrall    int runIptablesAlertCmd(IptOp op, const char *alertName, int64_t bytes);
808a93272255f1b7e3083a97e1e28ddf675c0c7fb0JP Abgrall
810dad7c2f1f6994fbe5e85b9e1fc72d29d6453211JP Abgrall    /* Runs for both ipv4 and ipv6 iptables */
8226e0d49fa743d7881104196a9eda733bd2aac92fJP Abgrall    int runCommands(int numCommands, const char *commands[], RunCmdErrHandling cmdErrHandling);
830dad7c2f1f6994fbe5e85b9e1fc72d29d6453211JP Abgrall    /* Runs for both ipv4 and ipv6 iptables, appends -j REJECT --reject-with ...  */
8426e0d49fa743d7881104196a9eda733bd2aac92fJP Abgrall    static int runIpxtablesCmd(const char *cmd, IptRejectOp rejectHandling);
8526e0d49fa743d7881104196a9eda733bd2aac92fJP Abgrall    static int runIptablesCmd(const char *cmd, IptRejectOp rejectHandling, IptIpVer iptIpVer);
8626e0d49fa743d7881104196a9eda733bd2aac92fJP Abgrall
8726e0d49fa743d7881104196a9eda733bd2aac92fJP Abgrall    // Provides strncpy() + check overflow.
8826e0d49fa743d7881104196a9eda733bd2aac92fJP Abgrall    static int StrncpyAndCheck(char *buffer, const char *src, size_t buffSize);
890dad7c2f1f6994fbe5e85b9e1fc72d29d6453211JP Abgrall
908a93272255f1b7e3083a97e1e28ddf675c0c7fb0JP Abgrall    int updateQuota(const char *alertName, int64_t bytes);
918a93272255f1b7e3083a97e1e28ddf675c0c7fb0JP Abgrall
928a93272255f1b7e3083a97e1e28ddf675c0c7fb0JP Abgrall    int64_t globalAlertBytes;
938a93272255f1b7e3083a97e1e28ddf675c0c7fb0JP Abgrall    int setCostlyAlert(const char *costName, int64_t bytes, int64_t *alertBytes);
948a93272255f1b7e3083a97e1e28ddf675c0c7fb0JP Abgrall    int removeCostlyAlert(const char *costName, int64_t *alertBytes);
958a93272255f1b7e3083a97e1e28ddf675c0c7fb0JP Abgrall
968a93272255f1b7e3083a97e1e28ddf675c0c7fb0JP Abgrall
974a5f5ca3c9e07fc3e6feca2afde07f41a8a64f11JP Abgrallprivate:
98fa6f46d3370ae5475fc3bc8273bbe04ee7348d60JP Abgrall    static const char *cleanupCommands[];
99fa6f46d3370ae5475fc3bc8273bbe04ee7348d60JP Abgrall    static const char *setupCommands[];
100fa6f46d3370ae5475fc3bc8273bbe04ee7348d60JP Abgrall    static const char *basicAccountingCommands[];
101fa6f46d3370ae5475fc3bc8273bbe04ee7348d60JP Abgrall    static const int MAX_CMD_LEN;
102fa6f46d3370ae5475fc3bc8273bbe04ee7348d60JP Abgrall    static const int MAX_IFACENAME_LEN;
103fa6f46d3370ae5475fc3bc8273bbe04ee7348d60JP Abgrall    static const int MAX_CMD_ARGS;
104fa6f46d3370ae5475fc3bc8273bbe04ee7348d60JP Abgrall    static const char IPTABLES_PATH[];
105fa6f46d3370ae5475fc3bc8273bbe04ee7348d60JP Abgrall    static const char IP6TABLES_PATH[];
1068a93272255f1b7e3083a97e1e28ddf675c0c7fb0JP Abgrall    static const char ALERT_IPT_TEMPLATE[];
1078a93272255f1b7e3083a97e1e28ddf675c0c7fb0JP Abgrall    static const int ALERT_RULE_POS_IN_COSTLY_CHAIN;
1084a5f5ca3c9e07fc3e6feca2afde07f41a8a64f11JP Abgrall};
1094a5f5ca3c9e07fc3e6feca2afde07f41a8a64f11JP Abgrall
1104a5f5ca3c9e07fc3e6feca2afde07f41a8a64f11JP Abgrall#endif
111