BandwidthController.h revision fa6f46d3370ae5475fc3bc8273bbe04ee7348d60
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
28fa6f46d3370ae5475fc3bc8273bbe04ee7348d60JP Abgrall    int setInterfaceSharedQuota(int64_t bytes, const char *iface);
29fa6f46d3370ae5475fc3bc8273bbe04ee7348d60JP Abgrall    int removeInterfaceSharedQuota(const char *iface);
30fa6f46d3370ae5475fc3bc8273bbe04ee7348d60JP Abgrall
31fa6f46d3370ae5475fc3bc8273bbe04ee7348d60JP Abgrall    int addNaughtyApps(int numUids, char *appUids[]);
32fa6f46d3370ae5475fc3bc8273bbe04ee7348d60JP Abgrall    int removeNaughtyApps(int numUids, char *appUids[]);
334a5f5ca3c9e07fc3e6feca2afde07f41a8a64f11JP Abgrall
344a5f5ca3c9e07fc3e6feca2afde07f41a8a64f11JP Abgrallprotected:
35fa6f46d3370ae5475fc3bc8273bbe04ee7348d60JP Abgrall    int runCommands(int numCommands, const char *commands[],
36fa6f46d3370ae5475fc3bc8273bbe04ee7348d60JP Abgrall            bool allowFailure = false, bool isIpv6 = false);
37fa6f46d3370ae5475fc3bc8273bbe04ee7348d60JP Abgrall    typedef std::pair<std::string /*ifaceName*/, int64_t /*quota*/> QuotaInfo;
38fa6f46d3370ae5475fc3bc8273bbe04ee7348d60JP Abgrall    enum IptOp {IptOpInsert, IptOpReplace, IptOpDelete};
39fa6f46d3370ae5475fc3bc8273bbe04ee7348d60JP Abgrall    int64_t sharedQuotaBytes;
40fa6f46d3370ae5475fc3bc8273bbe04ee7348d60JP Abgrall    std::list<QuotaInfo> ifaceRules;
41fa6f46d3370ae5475fc3bc8273bbe04ee7348d60JP Abgrall    std::list<int /*appUid*/> naughtyAppUids;
42fa6f46d3370ae5475fc3bc8273bbe04ee7348d60JP Abgrall    std::string makeIptablesNaughtyCmd(IptOp op, int uid, bool isIp6);
43fa6f46d3370ae5475fc3bc8273bbe04ee7348d60JP Abgrall    std::string makeIptablesQuotaCmd(IptOp op, char *costName, int64_t quota, bool isIp6);
44fa6f46d3370ae5475fc3bc8273bbe04ee7348d60JP Abgrall    int maninpulateNaughtyApps(int numUids, char *appStrUids[], bool doAdd);
454a5f5ca3c9e07fc3e6feca2afde07f41a8a64f11JP Abgrall
464a5f5ca3c9e07fc3e6feca2afde07f41a8a64f11JP Abgrallprivate:
47fa6f46d3370ae5475fc3bc8273bbe04ee7348d60JP Abgrall    static const char *cleanupCommands[];
48fa6f46d3370ae5475fc3bc8273bbe04ee7348d60JP Abgrall    static const char *setupCommands[];
49fa6f46d3370ae5475fc3bc8273bbe04ee7348d60JP Abgrall    static const char *basicAccountingCommands[];
50fa6f46d3370ae5475fc3bc8273bbe04ee7348d60JP Abgrall    static const int MAX_CMD_LEN;
51fa6f46d3370ae5475fc3bc8273bbe04ee7348d60JP Abgrall    static const int MAX_IFACENAME_LEN;
52fa6f46d3370ae5475fc3bc8273bbe04ee7348d60JP Abgrall    static const int MAX_CMD_ARGS;
53fa6f46d3370ae5475fc3bc8273bbe04ee7348d60JP Abgrall    static const char IPTABLES_PATH[];
54fa6f46d3370ae5475fc3bc8273bbe04ee7348d60JP Abgrall    static const char IP6TABLES_PATH[];
554a5f5ca3c9e07fc3e6feca2afde07f41a8a64f11JP Abgrall
56fa6f46d3370ae5475fc3bc8273bbe04ee7348d60JP Abgrall    static int runIptablesCmd(const char *cmd, bool isIp6 = false);
574a5f5ca3c9e07fc3e6feca2afde07f41a8a64f11JP Abgrall};
584a5f5ca3c9e07fc3e6feca2afde07f41a8a64f11JP Abgrall
594a5f5ca3c9e07fc3e6feca2afde07f41a8a64f11JP Abgrall#endif
60