Skip to content

Commit

Permalink
Merge tag 'android-7.1.0_r4' of https://android.googlesource.com/plat…
Browse files Browse the repository at this point in the history
…form/system/netd into 71

Android 7.1.0 release 4

Change-Id: Ic161796a87351d81afb5de1105ee0ed2f95e158c
  • Loading branch information
Steve Kondik committed Oct 26, 2016
2 parents b081d9a + 21cf5fd commit a97730b
Show file tree
Hide file tree
Showing 28 changed files with 1,204 additions and 219 deletions.
10 changes: 8 additions & 2 deletions server/Android.mk
Original file line number Diff line number Diff line change
Expand Up @@ -145,16 +145,22 @@ include $(BUILD_EXECUTABLE)
include $(CLEAR_VARS)
LOCAL_MODULE := netd_unit_test
LOCAL_CFLAGS := -Wall -Werror -Wunused-parameter
LOCAL_C_INCLUDES := system/netd/server system/netd/server/binder system/core/logwrapper/include
LOCAL_C_INCLUDES := \
system/netd/include \
system/netd/server \
system/netd/server/binder \
system/core/logwrapper/include \

LOCAL_SRC_FILES := \
NetdConstants.cpp IptablesBaseTest.cpp \
BandwidthController.cpp BandwidthControllerTest.cpp \
FirewallControllerTest.cpp FirewallController.cpp \
NatControllerTest.cpp NatController.cpp \
SockDiagTest.cpp SockDiag.cpp \
StrictController.cpp StrictControllerTest.cpp \
UidRanges.cpp \

LOCAL_MODULE_TAGS := tests
LOCAL_SHARED_LIBRARIES := liblog libbase libcutils liblogwrap
LOCAL_SHARED_LIBRARIES := liblog libbase libcutils liblogwrap libsysutils
include $(BUILD_NATIVE_TEST)

97 changes: 70 additions & 27 deletions server/BandwidthController.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1195,6 +1195,16 @@ int BandwidthController::removeCostlyAlert(const char *costName, int64_t *alertB
return res;
}

void BandwidthController::addStats(TetherStatsList& statsList, const TetherStats& stats) {
for (TetherStats& existing : statsList) {
if (existing.addStatsIfMatch(stats)) {
return;
}
}
// No match. Insert a new interface pair.
statsList.push_back(stats);
}

/*
* Parse the ptks and bytes out of:
* Chain natctrl_tether_counters (4 references)
Expand All @@ -1203,11 +1213,18 @@ int BandwidthController::removeCostlyAlert(const char *costName, int64_t *alertB
* 27 2002 RETURN all -- rmnet0 wlan0 0.0.0.0/0 0.0.0.0/0
* 1040 107471 RETURN all -- bt-pan rmnet0 0.0.0.0/0 0.0.0.0/0
* 1450 1708806 RETURN all -- rmnet0 bt-pan 0.0.0.0/0 0.0.0.0/0
* or:
* Chain natctrl_tether_counters (0 references)
* pkts bytes target prot opt in out source destination
* 0 0 RETURN all wlan0 rmnet_data0 ::/0 ::/0
* 0 0 RETURN all rmnet_data0 wlan0 ::/0 ::/0
*
* It results in an error if invoked and no tethering counter rules exist. The constraint
* helps detect complete parsing failure.
*/
int BandwidthController::parseForwardChainStats(SocketClient *cli, const TetherStats filter,
FILE *fp, std::string &extraProcessingInfo) {
int BandwidthController::addForwardChainStats(const TetherStats& filter,
TetherStatsList& statsList, FILE *fp,
std::string &extraProcessingInfo) {
int res;
char lineBuffer[MAX_IPT_OUTPUT_LINE_LEN];
char iface0[MAX_IPT_OUTPUT_LINE_LEN];
Expand All @@ -1230,8 +1247,17 @@ int BandwidthController::parseForwardChainStats(SocketClient *cli, const TetherS
while (NULL != (buffPtr = fgets(lineBuffer, MAX_IPT_OUTPUT_LINE_LEN, fp))) {
/* Clean up, so a failed parse can still print info */
iface0[0] = iface1[0] = rest[0] = packets = bytes = 0;
res = sscanf(buffPtr, "%" SCNd64" %" SCNd64" RETURN all -- %s %s 0.%s",
&packets, &bytes, iface0, iface1, rest);
if (strstr(buffPtr, "0.0.0.0")) {
// IPv4 has -- indicating what to do with fragments...
// 26 2373 RETURN all -- wlan0 rmnet0 0.0.0.0/0 0.0.0.0/0
res = sscanf(buffPtr, "%" SCNd64" %" SCNd64" RETURN all -- %s %s 0.%s",
&packets, &bytes, iface0, iface1, rest);
} else {
// ... but IPv6 does not.
// 26 2373 RETURN all wlan0 rmnet0 ::/0 ::/0
res = sscanf(buffPtr, "%" SCNd64" %" SCNd64" RETURN all %s %s ::/%s",
&packets, &bytes, iface0, iface1, rest);
}
ALOGV("parse res=%d iface0=<%s> iface1=<%s> pkts=%" PRId64" bytes=%" PRId64" rest=<%s> orig line=<%s>", res,
iface0, iface1, packets, bytes, rest, buffPtr);
extraProcessingInfo += buffPtr;
Expand Down Expand Up @@ -1283,17 +1309,13 @@ int BandwidthController::parseForwardChainStats(SocketClient *cli, const TetherS
}
if (stats.rxBytes != -1 && stats.txBytes != -1) {
ALOGV("rx_bytes=%" PRId64" tx_bytes=%" PRId64" filterPair=%d", stats.rxBytes, stats.txBytes, filterPair);
/* Send out stats, and prep for the next if needed. */
char *msg = stats.getStatsLine();
addStats(statsList, stats);
if (filterPair) {
cli->sendMsg(ResponseCode::TetheringStatsResult, msg, false);
return 0;
} else {
cli->sendMsg(ResponseCode::TetheringStatsListResult, msg, false);
statsFound++;
stats = filter;
}
free(msg);
statsFound++;
}
}

Expand All @@ -1303,7 +1325,6 @@ int BandwidthController::parseForwardChainStats(SocketClient *cli, const TetherS
(!statsFound && !filterPair)) {
return -1;
}
cli->sendMsg(ResponseCode::CommandOkay, "Tethering stats list completed", false);
return 0;
}

Expand All @@ -1314,31 +1335,53 @@ char *BandwidthController::TetherStats::getStatsLine(void) const {
return msg;
}

int BandwidthController::getTetherStats(SocketClient *cli, TetherStats &stats, std::string &extraProcessingInfo) {
int res;
std::string fullCmd;
FILE *iptOutput;

std::string getTetherStatsCommand(const char *binary) {
/*
* Why not use some kind of lib to talk to iptables?
* Because the only libs are libiptc and libip6tc in iptables, and they are
* not easy to use. They require the known iptables match modules to be
* preloaded/linked, and require apparently a lot of wrapper code to get
* the wanted info.
*/
fullCmd = IPTABLES_PATH;
fullCmd += " -nvx -w -L ";
fullCmd += NatController::LOCAL_TETHER_COUNTERS_CHAIN;
iptOutput = popenFunction(fullCmd.c_str(), "r");
if (!iptOutput) {
ALOGE("Failed to run %s err=%s", fullCmd.c_str(), strerror(errno));
extraProcessingInfo += "Failed to run iptables.";
return -1;
return android::base::StringPrintf("%s -nvx -w -L %s", binary,
NatController::LOCAL_TETHER_COUNTERS_CHAIN);
}

int BandwidthController::getTetherStats(SocketClient *cli, TetherStats& filter,
std::string &extraProcessingInfo) {
int res = 0;
std::string fullCmd;
FILE *iptOutput;

TetherStatsList statsList;

for (const auto binary : {IPTABLES_PATH, IP6TABLES_PATH}) {
fullCmd = getTetherStatsCommand(binary);
iptOutput = popenFunction(fullCmd.c_str(), "r");
if (!iptOutput) {
ALOGE("Failed to run %s err=%s", fullCmd.c_str(), strerror(errno));
extraProcessingInfo += "Failed to run iptables.";
return -1;
}

res = addForwardChainStats(filter, statsList, iptOutput, extraProcessingInfo);
pclose(iptOutput);
if (res != 0) {
return res;
}
}

if (filter.intIface[0] && filter.extIface[0] && statsList.size() == 1) {
cli->sendMsg(ResponseCode::TetheringStatsResult, statsList[0].getStatsLine(), false);
} else {
for (const auto& stats: statsList) {
cli->sendMsg(ResponseCode::TetheringStatsListResult, stats.getStatsLine(), false);
}
if (res == 0) {
cli->sendMsg(ResponseCode::CommandOkay, "Tethering stats list completed", false);
}
}
res = parseForwardChainStats(cli, stats, iptOutput, extraProcessingInfo);
pclose(iptOutput);

/* Currently NatController doesn't do ipv6 tethering, so we are done. */
return res;
}

Expand Down
20 changes: 20 additions & 0 deletions server/BandwidthController.h
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,17 @@ class BandwidthController {
* The caller is responsible for free()'ing the returned ptr.
*/
char *getStatsLine(void) const;

bool addStatsIfMatch(const TetherStats& other) {
if (intIface == other.intIface && extIface == other.extIface) {
rxBytes += other.rxBytes;
rxPackets += other.rxPackets;
txBytes += other.txBytes;
txPackets += other.txPackets;
return true;
}
return false;
}
};

BandwidthController();
Expand Down Expand Up @@ -170,6 +181,15 @@ class BandwidthController {
int setCostlyAlert(const char *costName, int64_t bytes, int64_t *alertBytes);
int removeCostlyAlert(const char *costName, int64_t *alertBytes);

typedef std::vector<TetherStats> TetherStatsList;

static void addStats(TetherStatsList& statsList, const TetherStats& stats);

static int addForwardChainStats(const TetherStats& filter,
TetherStatsList& statsList, FILE *fp,
std::string &extraProcessingInfo);


/*
* stats should never have only intIface initialized. Other 3 combos are ok.
* fp should be a file to the apropriate FORWARD chain of iptables rules.
Expand Down
Loading

0 comments on commit a97730b

Please sign in to comment.