Jie Jiang | 31a0b4e | 2020-07-09 15:06:16 +0900 | [diff] [blame] | 1 | // Copyright 2020 The Chromium OS Authors. All rights reserved. |
| 2 | // Use of this source code is governed by a BSD-style license that can be |
| 3 | // found in the LICENSE file. |
| 4 | |
| 5 | #include "patchpanel/counters_service.h" |
| 6 | |
| 7 | #include <set> |
| 8 | #include <string> |
Jie Jiang | ed0b1cc | 2020-07-10 15:55:33 +0900 | [diff] [blame] | 9 | #include <utility> |
Jie Jiang | 31a0b4e | 2020-07-09 15:06:16 +0900 | [diff] [blame] | 10 | #include <vector> |
| 11 | |
Qijiang Fan | 713061e | 2021-03-08 15:45:12 +0900 | [diff] [blame] | 12 | #include <base/check.h> |
Jie Jiang | ed0b1cc | 2020-07-10 15:55:33 +0900 | [diff] [blame] | 13 | #include <base/strings/strcat.h> |
| 14 | #include <base/strings/string_split.h> |
| 15 | #include <re2/re2.h> |
| 16 | |
Jie Jiang | 31a0b4e | 2020-07-09 15:06:16 +0900 | [diff] [blame] | 17 | namespace patchpanel { |
| 18 | |
| 19 | namespace { |
| 20 | |
Jie Jiang | ed0b1cc | 2020-07-10 15:55:33 +0900 | [diff] [blame] | 21 | using Counter = CountersService::Counter; |
| 22 | using SourceDevice = CountersService::SourceDevice; |
| 23 | |
Jie Jiang | 31a0b4e | 2020-07-09 15:06:16 +0900 | [diff] [blame] | 24 | constexpr char kMangleTable[] = "mangle"; |
Hugo Benichi | 058a26a | 2020-11-26 10:10:39 +0900 | [diff] [blame] | 25 | constexpr char kVpnChainTag[] = "vpn"; |
| 26 | constexpr char kRxTag[] = "rx_"; |
| 27 | constexpr char kTxTag[] = "tx_"; |
Jie Jiang | 31a0b4e | 2020-07-09 15:06:16 +0900 | [diff] [blame] | 28 | |
Jie Jiang | ed0b1cc | 2020-07-10 15:55:33 +0900 | [diff] [blame] | 29 | // The following regexs and code is written and tested for iptables v1.6.2. |
| 30 | // Output code of iptables can be found at: |
| 31 | // https://git.netfilter.org/iptables/tree/iptables/iptables.c?h=v1.6.2 |
| 32 | |
| 33 | // The chain line looks like: |
Hugo Benichi | 33af8f7 | 2020-11-20 10:08:47 +0900 | [diff] [blame] | 34 | // "Chain tx_eth0 (2 references)". |
| 35 | // This regex extracts "tx" (direction), "eth0" (ifname) from this example. |
| 36 | constexpr LazyRE2 kChainLine = {R"(Chain (rx|tx)_(\w+).*)"}; |
Jie Jiang | ed0b1cc | 2020-07-10 15:55:33 +0900 | [diff] [blame] | 37 | |
Hugo Benichi | ae5803d | 2020-12-08 10:49:48 +0900 | [diff] [blame] | 38 | // The counter line for a defined source looks like (some spaces are deleted to |
| 39 | // make it fit in one line): |
| 40 | // " 5374 6172 RETURN all -- any any anywhere anywhere mark match 0x2000/0x3f00" |
| 41 | // The final counter line for catching untagged traffic looks like: |
| 42 | // " 5374 6172 all -- any any anywhere anywhere" |
| 43 | // The first two counters are captured for pkts and bytes. For lines with a mark |
| 44 | // matcher, the source is also captured. |
Hugo Benichi | 92fa203 | 2020-11-20 17:47:32 +0900 | [diff] [blame] | 45 | constexpr LazyRE2 kCounterLine = {R"( *(\d+) +(\d+).*mark match (.*)/0x3f00)"}; |
Hugo Benichi | ae5803d | 2020-12-08 10:49:48 +0900 | [diff] [blame] | 46 | constexpr LazyRE2 kFinalCounterLine = {R"( *(\d+) +(\d+).*anywhere\s*)"}; |
| 47 | |
| 48 | bool MatchCounterLine(const std::string& line, |
| 49 | uint64_t* pkts, |
| 50 | uint64_t* bytes, |
| 51 | TrafficSource* source) { |
| 52 | Fwmark mark; |
| 53 | if (RE2::FullMatch(line, *kCounterLine, pkts, bytes, |
| 54 | RE2::Hex(&mark.fwmark))) { |
| 55 | *source = mark.Source(); |
| 56 | return true; |
| 57 | } |
| 58 | |
| 59 | if (RE2::FullMatch(line, *kFinalCounterLine, pkts, bytes)) { |
| 60 | *source = TrafficSource::UNKNOWN; |
| 61 | return true; |
| 62 | } |
| 63 | |
| 64 | return false; |
| 65 | } |
Jie Jiang | ed0b1cc | 2020-07-10 15:55:33 +0900 | [diff] [blame] | 66 | |
| 67 | // Parses the output of `iptables -L -x -v` (or `ip6tables`) and adds the parsed |
| 68 | // values into the corresponding counters in |counters|. An example of |output| |
| 69 | // can be found in the test file. This function will try to find the pattern of: |
| 70 | // <one chain line for an accounting chain> |
| 71 | // <one header line> |
| 72 | // <one counter line for an accounting rule> |
| 73 | // The interface name and direction (rx or tx) will be extracted from the chain |
| 74 | // line, and then the values extracted from the counter line will be added into |
| 75 | // the counter for that interface. Note that this function will not fully |
| 76 | // validate if |output| is an output from iptables. |
| 77 | bool ParseOutput(const std::string& output, |
| 78 | const std::set<std::string>& devices, |
| 79 | std::map<SourceDevice, Counter>* counters) { |
| 80 | DCHECK(counters); |
| 81 | const std::vector<std::string> lines = base::SplitString( |
| 82 | output, "\n", base::KEEP_WHITESPACE, base::SPLIT_WANT_ALL); |
| 83 | |
| 84 | // Finds the chain line for an accounting chain first, and then parse the |
| 85 | // following line(s) to get the counters for this chain. Repeats this process |
| 86 | // until we reach the end of |output|. |
| 87 | for (auto it = lines.cbegin(); it != lines.cend(); it++) { |
| 88 | // Finds the chain name line. |
| 89 | std::string direction, ifname; |
| 90 | while (it != lines.cend() && |
| 91 | !RE2::FullMatch(*it, *kChainLine, &direction, &ifname)) |
| 92 | it++; |
| 93 | |
| 94 | if (it == lines.cend()) |
| 95 | break; |
| 96 | |
| 97 | // Skips this group if this ifname is not requested. |
| 98 | if (!devices.empty() && devices.find(ifname) == devices.end()) |
| 99 | continue; |
| 100 | |
| 101 | // Skips the chain name line and the header line. |
| 102 | if (lines.cend() - it <= 2) { |
Hugo Benichi | ae5803d | 2020-12-08 10:49:48 +0900 | [diff] [blame] | 103 | LOG(ERROR) << "Invalid iptables output for " << direction << "_" |
| 104 | << ifname; |
Jie Jiang | ed0b1cc | 2020-07-10 15:55:33 +0900 | [diff] [blame] | 105 | return false; |
| 106 | } |
| 107 | it += 2; |
| 108 | |
Hugo Benichi | ae5803d | 2020-12-08 10:49:48 +0900 | [diff] [blame] | 109 | // Checks that there are some counter rules defined. |
| 110 | if (it == lines.cend() || it->empty()) { |
| 111 | LOG(ERROR) << "No counter rule defined for " << direction << "_" |
| 112 | << ifname; |
| 113 | return false; |
| 114 | } |
| 115 | |
Hugo Benichi | 92fa203 | 2020-11-20 17:47:32 +0900 | [diff] [blame] | 116 | // The next block of lines are the counters lines for individual sources. |
| 117 | for (; it != lines.cend() && !it->empty(); it++) { |
| 118 | uint64_t pkts, bytes; |
Hugo Benichi | ae5803d | 2020-12-08 10:49:48 +0900 | [diff] [blame] | 119 | TrafficSource source; |
| 120 | if (!MatchCounterLine(*it, &pkts, &bytes, &source)) { |
| 121 | LOG(ERROR) << "Cannot parse counter line \"" << *it << "\" for " |
| 122 | << direction << "_" << ifname; |
Hugo Benichi | 92fa203 | 2020-11-20 17:47:32 +0900 | [diff] [blame] | 123 | return false; |
| 124 | } |
Jie Jiang | ed0b1cc | 2020-07-10 15:55:33 +0900 | [diff] [blame] | 125 | |
Hugo Benichi | 92fa203 | 2020-11-20 17:47:32 +0900 | [diff] [blame] | 126 | if (pkts == 0 && bytes == 0) |
| 127 | continue; |
| 128 | |
Hugo Benichi | ae5803d | 2020-12-08 10:49:48 +0900 | [diff] [blame] | 129 | auto& counter = |
| 130 | (*counters)[std::make_pair(TrafficSourceToProto(source), ifname)]; |
Hugo Benichi | 92fa203 | 2020-11-20 17:47:32 +0900 | [diff] [blame] | 131 | if (direction == "rx") { |
Hugo Benichi | 92fa203 | 2020-11-20 17:47:32 +0900 | [diff] [blame] | 132 | counter.rx_bytes += bytes; |
Hugo Benichi | ae5803d | 2020-12-08 10:49:48 +0900 | [diff] [blame] | 133 | counter.rx_packets += pkts; |
Hugo Benichi | 92fa203 | 2020-11-20 17:47:32 +0900 | [diff] [blame] | 134 | } else { |
Hugo Benichi | 92fa203 | 2020-11-20 17:47:32 +0900 | [diff] [blame] | 135 | counter.tx_bytes += bytes; |
Hugo Benichi | ae5803d | 2020-12-08 10:49:48 +0900 | [diff] [blame] | 136 | counter.tx_packets += pkts; |
Hugo Benichi | 92fa203 | 2020-11-20 17:47:32 +0900 | [diff] [blame] | 137 | } |
Jie Jiang | ed0b1cc | 2020-07-10 15:55:33 +0900 | [diff] [blame] | 138 | } |
Jason Jeremy Iman | 54d3f45 | 2021-06-02 17:29:41 +0900 | [diff] [blame] | 139 | |
| 140 | if (it == lines.cend()) |
| 141 | break; |
Jie Jiang | ed0b1cc | 2020-07-10 15:55:33 +0900 | [diff] [blame] | 142 | } |
| 143 | return true; |
| 144 | } |
| 145 | |
Jie Jiang | 31a0b4e | 2020-07-09 15:06:16 +0900 | [diff] [blame] | 146 | } // namespace |
| 147 | |
Jie Jiang | ed0b1cc | 2020-07-10 15:55:33 +0900 | [diff] [blame] | 148 | Counter::Counter(uint64_t rx_bytes, |
| 149 | uint64_t rx_packets, |
| 150 | uint64_t tx_bytes, |
| 151 | uint64_t tx_packets) |
| 152 | : rx_bytes(rx_bytes), |
| 153 | rx_packets(rx_packets), |
| 154 | tx_bytes(tx_bytes), |
| 155 | tx_packets(tx_packets) {} |
| 156 | |
Hugo Benichi | 058a26a | 2020-11-26 10:10:39 +0900 | [diff] [blame] | 157 | CountersService::CountersService(Datapath* datapath, |
Jie Jiang | 31a0b4e | 2020-07-09 15:06:16 +0900 | [diff] [blame] | 158 | MinijailedProcessRunner* runner) |
Hugo Benichi | 058a26a | 2020-11-26 10:10:39 +0900 | [diff] [blame] | 159 | : datapath_(datapath), runner_(runner) {} |
Jie Jiang | 31a0b4e | 2020-07-09 15:06:16 +0900 | [diff] [blame] | 160 | |
Jie Jiang | ed0b1cc | 2020-07-10 15:55:33 +0900 | [diff] [blame] | 161 | std::map<SourceDevice, Counter> CountersService::GetCounters( |
| 162 | const std::set<std::string>& devices) { |
| 163 | std::map<SourceDevice, Counter> counters; |
| 164 | |
| 165 | // Handles counters for IPv4 and IPv6 separately and returns failure if either |
| 166 | // of the procession fails, since counters for only IPv4 or IPv6 are biased. |
| 167 | std::string iptables_result; |
| 168 | int ret = runner_->iptables(kMangleTable, {"-L", "-x", "-v", "-w"}, |
| 169 | true /*log_failures*/, &iptables_result); |
| 170 | if (ret != 0 || iptables_result.empty()) { |
| 171 | LOG(ERROR) << "Failed to query IPv4 counters"; |
| 172 | return {}; |
| 173 | } |
| 174 | if (!ParseOutput(iptables_result, devices, &counters)) { |
| 175 | LOG(ERROR) << "Failed to parse IPv4 counters"; |
| 176 | return {}; |
| 177 | } |
| 178 | |
| 179 | std::string ip6tables_result; |
| 180 | ret = runner_->ip6tables(kMangleTable, {"-L", "-x", "-v", "-w"}, |
| 181 | true /*log_failures*/, &ip6tables_result); |
| 182 | if (ret != 0 || ip6tables_result.empty()) { |
| 183 | LOG(ERROR) << "Failed to query IPv6 counters"; |
| 184 | return {}; |
| 185 | } |
| 186 | if (!ParseOutput(ip6tables_result, devices, &counters)) { |
| 187 | LOG(ERROR) << "Failed to parse IPv6 counters"; |
| 188 | return {}; |
| 189 | } |
| 190 | |
| 191 | return counters; |
| 192 | } |
| 193 | |
Hugo Benichi | 058a26a | 2020-11-26 10:10:39 +0900 | [diff] [blame] | 194 | void CountersService::OnPhysicalDeviceAdded(const std::string& ifname) { |
Hugo Benichi | df5e102 | 2021-03-23 15:48:31 +0900 | [diff] [blame] | 195 | SetupAccountingRules(ifname); |
| 196 | SetupJumpRules("-A", ifname, ifname); |
Hugo Benichi | 058a26a | 2020-11-26 10:10:39 +0900 | [diff] [blame] | 197 | } |
| 198 | |
| 199 | void CountersService::OnPhysicalDeviceRemoved(const std::string& ifname) { |
| 200 | SetupJumpRules("-D", ifname, ifname); |
| 201 | } |
| 202 | |
| 203 | void CountersService::OnVpnDeviceAdded(const std::string& ifname) { |
Hugo Benichi | df5e102 | 2021-03-23 15:48:31 +0900 | [diff] [blame] | 204 | SetupAccountingRules(kVpnChainTag); |
Hugo Benichi | 058a26a | 2020-11-26 10:10:39 +0900 | [diff] [blame] | 205 | SetupJumpRules("-A", ifname, kVpnChainTag); |
| 206 | } |
| 207 | |
| 208 | void CountersService::OnVpnDeviceRemoved(const std::string& ifname) { |
| 209 | SetupJumpRules("-D", ifname, kVpnChainTag); |
Jie Jiang | cf74915 | 2020-07-09 22:20:58 +0900 | [diff] [blame] | 210 | } |
Jie Jiang | 31a0b4e | 2020-07-09 15:06:16 +0900 | [diff] [blame] | 211 | |
Hugo Benichi | cd27f4e | 2020-11-19 18:32:23 +0900 | [diff] [blame] | 212 | bool CountersService::MakeAccountingChain(const std::string& chain_name) { |
| 213 | return datapath_->ModifyChain(IpFamily::Dual, kMangleTable, "-N", chain_name, |
| 214 | false /*log_failures*/); |
Jie Jiang | 31a0b4e | 2020-07-09 15:06:16 +0900 | [diff] [blame] | 215 | } |
| 216 | |
Hugo Benichi | af2021b | 2020-11-20 14:07:16 +0900 | [diff] [blame] | 217 | bool CountersService::AddAccountingRule(const std::string& chain_name, |
| 218 | TrafficSource source) { |
| 219 | std::vector<std::string> args = {"-A", |
| 220 | chain_name, |
| 221 | "-m", |
| 222 | "mark", |
| 223 | "--mark", |
| 224 | Fwmark::FromSource(source).ToString() + "/" + |
| 225 | kFwmarkAllSourcesMask.ToString(), |
| 226 | "-j", |
| 227 | "RETURN", |
| 228 | "-w"}; |
| 229 | return datapath_->ModifyIptables(IpFamily::Dual, kMangleTable, args); |
Jie Jiang | 31a0b4e | 2020-07-09 15:06:16 +0900 | [diff] [blame] | 230 | } |
| 231 | |
Hugo Benichi | df5e102 | 2021-03-23 15:48:31 +0900 | [diff] [blame] | 232 | void CountersService::SetupAccountingRules(const std::string& chain_tag) { |
Hugo Benichi | 058a26a | 2020-11-26 10:10:39 +0900 | [diff] [blame] | 233 | // For a new target accounting chain, create |
Hugo Benichi | 33af8f7 | 2020-11-20 10:08:47 +0900 | [diff] [blame] | 234 | // 1) an accounting chain to jump to, |
Hugo Benichi | 058a26a | 2020-11-26 10:10:39 +0900 | [diff] [blame] | 235 | // 2) source accounting rules in the chain. |
Jie Jiang | cf74915 | 2020-07-09 22:20:58 +0900 | [diff] [blame] | 236 | // Note that the length of a chain name must less than 29 chars and IFNAMSIZ |
| 237 | // is 16 so we can only use at most 12 chars for the prefix. |
Hugo Benichi | 058a26a | 2020-11-26 10:10:39 +0900 | [diff] [blame] | 238 | const std::string ingress_chain = kRxTag + chain_tag; |
| 239 | const std::string egress_chain = kTxTag + chain_tag; |
Hugo Benichi | ddf0084 | 2020-11-20 10:24:08 +0900 | [diff] [blame] | 240 | |
| 241 | // Creates egress and ingress traffic chains, or stops if they already exist. |
Hugo Benichi | 058a26a | 2020-11-26 10:10:39 +0900 | [diff] [blame] | 242 | if (!MakeAccountingChain(egress_chain) || |
| 243 | !MakeAccountingChain(ingress_chain)) { |
| 244 | LOG(INFO) << "Traffic accounting chains already exist for " << chain_tag; |
Hugo Benichi | df5e102 | 2021-03-23 15:48:31 +0900 | [diff] [blame] | 245 | return; |
Hugo Benichi | ddf0084 | 2020-11-20 10:24:08 +0900 | [diff] [blame] | 246 | } |
| 247 | |
Hugo Benichi | af2021b | 2020-11-20 14:07:16 +0900 | [diff] [blame] | 248 | // Add source accounting rules. |
| 249 | for (TrafficSource source : kAllSources) { |
| 250 | AddAccountingRule(ingress_chain, source); |
| 251 | AddAccountingRule(egress_chain, source); |
| 252 | } |
Hugo Benichi | ae5803d | 2020-12-08 10:49:48 +0900 | [diff] [blame] | 253 | // Add catch-all accounting rule for any remaining and untagged traffic. |
| 254 | datapath_->ModifyIptables(IpFamily::Dual, kMangleTable, |
| 255 | {"-A", ingress_chain, "-w"}); |
| 256 | datapath_->ModifyIptables(IpFamily::Dual, kMangleTable, |
| 257 | {"-A", egress_chain, "-w"}); |
Hugo Benichi | 058a26a | 2020-11-26 10:10:39 +0900 | [diff] [blame] | 258 | } |
| 259 | |
| 260 | void CountersService::SetupJumpRules(const std::string& op, |
| 261 | const std::string& ifname, |
| 262 | const std::string& chain_tag) { |
| 263 | // For each device create a jumping rule in mangle POSTROUTING for egress |
| 264 | // traffic, and two jumping rules in mangle INPUT and FORWARD for ingress |
| 265 | // traffic. |
| 266 | datapath_->ModifyIptables( |
| 267 | IpFamily::Dual, kMangleTable, |
Hugo Benichi | 2f8a1ea | 2020-12-14 14:19:21 +0900 | [diff] [blame] | 268 | {op, "FORWARD", "-i", ifname, "-j", kRxTag + chain_tag, "-w"}); |
Hugo Benichi | 058a26a | 2020-11-26 10:10:39 +0900 | [diff] [blame] | 269 | datapath_->ModifyIptables( |
| 270 | IpFamily::Dual, kMangleTable, |
Hugo Benichi | 2f8a1ea | 2020-12-14 14:19:21 +0900 | [diff] [blame] | 271 | {op, "INPUT", "-i", ifname, "-j", kRxTag + chain_tag, "-w"}); |
Hugo Benichi | 058a26a | 2020-11-26 10:10:39 +0900 | [diff] [blame] | 272 | datapath_->ModifyIptables( |
| 273 | IpFamily::Dual, kMangleTable, |
Hugo Benichi | 2f8a1ea | 2020-12-14 14:19:21 +0900 | [diff] [blame] | 274 | {op, "POSTROUTING", "-o", ifname, "-j", kTxTag + chain_tag, "-w"}); |
Jie Jiang | cf74915 | 2020-07-09 22:20:58 +0900 | [diff] [blame] | 275 | } |
| 276 | |
Hugo Benichi | 92fa203 | 2020-11-20 17:47:32 +0900 | [diff] [blame] | 277 | TrafficCounter::Source TrafficSourceToProto(TrafficSource source) { |
| 278 | switch (source) { |
| 279 | case CHROME: |
| 280 | return TrafficCounter::CHROME; |
| 281 | case USER: |
| 282 | return TrafficCounter::USER; |
| 283 | case UPDATE_ENGINE: |
| 284 | return TrafficCounter::UPDATE_ENGINE; |
| 285 | case SYSTEM: |
| 286 | return TrafficCounter::SYSTEM; |
| 287 | case HOST_VPN: |
| 288 | return TrafficCounter::VPN; |
| 289 | case ARC: |
| 290 | return TrafficCounter::ARC; |
| 291 | case CROSVM: |
| 292 | return TrafficCounter::CROSVM; |
| 293 | case PLUGINVM: |
| 294 | return TrafficCounter::PLUGINVM; |
| 295 | case TETHER_DOWNSTREAM: |
| 296 | return TrafficCounter::SYSTEM; |
| 297 | case ARC_VPN: |
| 298 | return TrafficCounter::VPN; |
| 299 | case UNKNOWN: |
| 300 | default: |
| 301 | return TrafficCounter::UNKNOWN; |
| 302 | } |
| 303 | } |
| 304 | |
Hugo Benichi | 93306e5 | 2020-12-04 16:08:00 +0900 | [diff] [blame] | 305 | TrafficSource ProtoToTrafficSource(TrafficCounter::Source source) { |
| 306 | switch (source) { |
| 307 | case TrafficCounter::CHROME: |
| 308 | return CHROME; |
| 309 | case TrafficCounter::USER: |
| 310 | return USER; |
| 311 | case TrafficCounter::UPDATE_ENGINE: |
| 312 | return UPDATE_ENGINE; |
| 313 | case TrafficCounter::SYSTEM: |
| 314 | return SYSTEM; |
| 315 | case TrafficCounter::VPN: |
| 316 | return HOST_VPN; |
| 317 | case TrafficCounter::ARC: |
| 318 | return ARC; |
| 319 | case TrafficCounter::CROSVM: |
| 320 | return CROSVM; |
| 321 | case TrafficCounter::PLUGINVM: |
| 322 | return PLUGINVM; |
| 323 | default: |
| 324 | case TrafficCounter::UNKNOWN: |
| 325 | return UNKNOWN; |
| 326 | } |
| 327 | } |
| 328 | |
Jie Jiang | 31a0b4e | 2020-07-09 15:06:16 +0900 | [diff] [blame] | 329 | } // namespace patchpanel |