netd: tethering stats: persistent + list-all support
* Persistent stats
Previously we would parse the iptables counters out of the FORWARD
rules used for tethering. Those rules could come an go before they
were parsed, which would cause us to incorrectly count traffic.
Now we have separate counting rules (and quota2 counters) which
persist beyond tethering.
* Rename the iface0/iface1
Match NatControllers notions for tethering ifaces during enable.
Detect weird call from userspace (until b/9565268 gets fixed),
or else it leaves an ugly iptables state.
* The commands affected:
- ndc bandwidth gettetheringstats intIface extIface
. no change from before: return a single stats line
- ndc bandwidth gettetheringstats
. return a list of results showing all tethered stats
- ndc bandwidth gettetheringstats "" extIface
- ndc bandwidth gettetheringstats intIface
. return a list of results matching the tethering on
the given interface.
Bug: 9565268
Bug: 5868832
Change-Id: I8559d9a184abcffaf65998fb3cc8c9c50d46bf06
diff --git a/BandwidthController.h b/BandwidthController.h
index b4a2c20..13805af 100644
--- a/BandwidthController.h
+++ b/BandwidthController.h
@@ -20,6 +20,8 @@
#include <string>
#include <utility> // for pair
+#include <sysutils/SocketClient.h>
+
class BandwidthController {
public:
class TetherStats {
@@ -27,22 +29,24 @@
TetherStats(void)
: rxBytes(-1), rxPackets(-1),
txBytes(-1), txPackets(-1) {};
- TetherStats(std::string ifnIn, std::string ifnOut,
+ TetherStats(std::string intIfn, std::string extIfn,
int64_t rxB, int64_t rxP,
int64_t txB, int64_t txP)
- : ifaceIn(ifnIn), ifaceOut(ifnOut),
+ : intIface(intIfn), extIface(extIfn),
rxBytes(rxB), rxPackets(rxP),
- txBytes(txB), txPackets(txP) {};
- std::string ifaceIn;
- std::string ifaceOut;
+ txBytes(txB), txPackets(txP) {};
+ /* Internal interface. Same as NatController's notion. */
+ std::string intIface;
+ /* External interface. Same as NatController's notion. */
+ std::string extIface;
int64_t rxBytes, rxPackets;
int64_t txBytes, txPackets;
/*
* Allocates a new string representing this:
- * ifaceIn ifaceOut rx_bytes rx_packets tx_bytes tx_packets
+ * intIface extIface rx_bytes rx_packets tx_bytes tx_packets
* The caller is responsible for free()'ing the returned ptr.
*/
- char *getStatsLine(void);
+ char *getStatsLine(void) const;
};
BandwidthController();
@@ -75,10 +79,13 @@
int removeInterfaceAlert(const char *iface);
/*
- * stats should have ifaceIn and ifaceOut initialized.
- * Byte counts should be left to the default (-1).
+ * For single pair of ifaces, stats should have ifaceIn and ifaceOut initialized.
+ * For all pairs, stats should have ifaceIn=ifaceOut="".
+ * Sends out to the cli the single stat (TetheringStatsReluts) or a list of stats
+ * (TetheringStatsListResult+CommandOkay).
+ * Error is to be handled on the outside
*/
- int getTetherStats(TetherStats &stats, std::string &extraProcessingInfo);
+ int getTetherStats(SocketClient *cli, TetherStats &stats, std::string &extraProcessingInfo);
static const char* LOCAL_INPUT;
static const char* LOCAL_FORWARD;
@@ -136,11 +143,15 @@
int removeCostlyAlert(const char *costName, int64_t *alertBytes);
/*
- * stats should have ifaceIn and ifaceOut initialized.
- * fp should be a file to the FORWARD rules of iptables.
+ * 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.
* extraProcessingInfo: contains raw parsed data, and error info.
+ * This strongly requires that setup of the rules is in a specific order:
+ * in:intIface out:extIface
+ * in:extIface out:intIface
+ * and the rules are grouped in pairs when more that one tethering was setup.
*/
- static int parseForwardChainStats(TetherStats &stats, FILE *fp,
+ static int parseForwardChainStats(SocketClient *cli, const TetherStats filter, FILE *fp,
std::string &extraProcessingInfo);
/*------------------*/