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 | #ifndef PATCHPANEL_COUNTERS_SERVICE_H_ |
| 6 | #define PATCHPANEL_COUNTERS_SERVICE_H_ |
| 7 | |
Jie Jiang | ed0b1cc | 2020-07-10 15:55:33 +0900 | [diff] [blame] | 8 | #include <map> |
Jie Jiang | 31a0b4e | 2020-07-09 15:06:16 +0900 | [diff] [blame] | 9 | #include <set> |
| 10 | #include <string> |
Jie Jiang | ed0b1cc | 2020-07-10 15:55:33 +0900 | [diff] [blame] | 11 | #include <utility> |
Jie Jiang | 31a0b4e | 2020-07-09 15:06:16 +0900 | [diff] [blame] | 12 | #include <vector> |
| 13 | |
Jie Jiang | ed0b1cc | 2020-07-10 15:55:33 +0900 | [diff] [blame] | 14 | #include <patchpanel/proto_bindings/patchpanel_service.pb.h> |
| 15 | |
Hugo Benichi | cd27f4e | 2020-11-19 18:32:23 +0900 | [diff] [blame] | 16 | #include "patchpanel/datapath.h" |
Jie Jiang | 31a0b4e | 2020-07-09 15:06:16 +0900 | [diff] [blame] | 17 | #include "patchpanel/minijailed_process_runner.h" |
Hugo Benichi | af2021b | 2020-11-20 14:07:16 +0900 | [diff] [blame] | 18 | #include "patchpanel/routing_service.h" |
Jie Jiang | 31a0b4e | 2020-07-09 15:06:16 +0900 | [diff] [blame] | 19 | |
| 20 | namespace patchpanel { |
| 21 | |
| 22 | // This class manages the iptables rules for traffic counters, and queries |
| 23 | // iptables to get the counters when a request comes. This class will set up |
| 24 | // several iptable rules to track the counters for each possible combination of |
| 25 | // {bytes, packets} x (Traffic source) x (shill device) x {rx, tx} x {IPv4, |
| 26 | // IPv6}. These counters will never be removed after they are set up, and thus |
| 27 | // they represent the traffic usage from boot time. |
| 28 | // |
Jie Jiang | 31a0b4e | 2020-07-09 15:06:16 +0900 | [diff] [blame] | 29 | // Implementation details |
| 30 | // |
| 31 | // Rules: All the rules/chains for accounting are in (INPUT, FORWARD or |
| 32 | // POSTROUTING) chain in the mangle table. These rules take effect after routing |
| 33 | // and will not change the fate of a packet. When a new interface comes up, we |
| 34 | // will create the following new rules/chains (using both iptables and |
| 35 | // ip6tables): |
Hugo Benichi | ae5803d | 2020-12-08 10:49:48 +0900 | [diff] [blame] | 36 | // - Two accounting chains: |
| 37 | // - For rx packets, `rx_{ifname}` for INPUT and FORWARD chains; |
| 38 | // - For tx packets, `tx_{ifname}` for POSTROUTING chain. |
| 39 | // - One accounting rule in each accounting chain for every source defined in |
| 40 | // RoutingService plus one final accounting rule for untagged traffic. |
| 41 | // - Jumping rules for each accounting chain in the corresponding prebuilt |
Jie Jiang | 31a0b4e | 2020-07-09 15:06:16 +0900 | [diff] [blame] | 42 | // chain, which matches packets with this new interface. |
Hugo Benichi | ae5803d | 2020-12-08 10:49:48 +0900 | [diff] [blame] | 43 | // The above accounting rules and chains will never be removed once created, so |
| 44 | // we will check if one rule exists before creating it. Jumping rules are added |
| 45 | // and removed dynamically based on physical device and vpn device creation and |
| 46 | // removal events. |
Jie Jiang | 31a0b4e | 2020-07-09 15:06:16 +0900 | [diff] [blame] | 47 | // |
| 48 | // Query: Two commands (iptables and ip6tables) will be executed in the mangle |
| 49 | // table to get all the chains and rules. And then we perform a text parsing on |
| 50 | // the output to get the counters. Counters for the same entry will be merged |
| 51 | // before return. |
| 52 | class CountersService { |
| 53 | public: |
Jie Jiang | ed0b1cc | 2020-07-10 15:55:33 +0900 | [diff] [blame] | 54 | using SourceDevice = std::pair<TrafficCounter::Source, std::string>; |
| 55 | struct Counter { |
| 56 | Counter() = default; |
| 57 | Counter(uint64_t rx_bytes, |
| 58 | uint64_t rx_packets, |
| 59 | uint64_t tx_bytes, |
| 60 | uint64_t tx_packets); |
| 61 | |
| 62 | uint64_t rx_bytes = 0; |
| 63 | uint64_t rx_packets = 0; |
| 64 | uint64_t tx_bytes = 0; |
| 65 | uint64_t tx_packets = 0; |
| 66 | }; |
| 67 | |
Hugo Benichi | 058a26a | 2020-11-26 10:10:39 +0900 | [diff] [blame] | 68 | CountersService(Datapath* datapath, MinijailedProcessRunner* runner); |
Jie Jiang | 31a0b4e | 2020-07-09 15:06:16 +0900 | [diff] [blame] | 69 | ~CountersService() = default; |
| 70 | |
Hugo Benichi | 058a26a | 2020-11-26 10:10:39 +0900 | [diff] [blame] | 71 | // Adds accounting rules and jump rules for a new physical device if this is |
| 72 | // the first time this device is seen. |
| 73 | void OnPhysicalDeviceAdded(const std::string& ifname); |
| 74 | // Removes jump rules for a physical device. |
| 75 | void OnPhysicalDeviceRemoved(const std::string& ifname); |
| 76 | // Adds accounting rules and jump rules for a new VPN device. |
| 77 | void OnVpnDeviceAdded(const std::string& ifname); |
| 78 | // Removes jump rules for a VPN device. |
| 79 | void OnVpnDeviceRemoved(const std::string& ifname); |
Jie Jiang | ed0b1cc | 2020-07-10 15:55:33 +0900 | [diff] [blame] | 80 | // Collects and returns counters from all the existing iptables rules. |
| 81 | // |devices| is the set of interfaces for which counters should be returned, |
| 82 | // any unknown interfaces will be ignored. If |devices| is empty, counters for |
| 83 | // all known interfaces will be returned. An empty map will be returned on |
| 84 | // any failure. Note that currently all traffic to/from an interface will be |
| 85 | // counted by (UNKNOWN, ifname), i.e., no other sources except for UNKNOWN are |
| 86 | // used. |
| 87 | std::map<SourceDevice, Counter> GetCounters( |
| 88 | const std::set<std::string>& devices); |
| 89 | |
Jie Jiang | 31a0b4e | 2020-07-09 15:06:16 +0900 | [diff] [blame] | 90 | private: |
Hugo Benichi | 058a26a | 2020-11-26 10:10:39 +0900 | [diff] [blame] | 91 | // Creates an iptables chain in the mangle table. Returns true if the chain |
| 92 | // was created, or false if the chain already existed. |
Hugo Benichi | cd27f4e | 2020-11-19 18:32:23 +0900 | [diff] [blame] | 93 | bool MakeAccountingChain(const std::string& chain_name); |
Hugo Benichi | af2021b | 2020-11-20 14:07:16 +0900 | [diff] [blame] | 94 | bool AddAccountingRule(const std::string& chain_name, TrafficSource source); |
Hugo Benichi | 058a26a | 2020-11-26 10:10:39 +0900 | [diff] [blame] | 95 | // Installs the required accounting chains and rules for the target |
Hugo Benichi | df5e102 | 2021-03-23 15:48:31 +0900 | [diff] [blame^] | 96 | // |chain_tag| if they did not exist already. |
| 97 | void SetupAccountingRules(const std::string& chain_tag); |
Hugo Benichi | 058a26a | 2020-11-26 10:10:39 +0900 | [diff] [blame] | 98 | // Installs jump rules in POSTROUTING to count traffic ingressing and |
| 99 | // egressing |ifname| with the accounting target |chain_tag|. |
| 100 | void SetupJumpRules(const std::string& op, |
| 101 | const std::string& ifname, |
| 102 | const std::string& chain_tag); |
Jie Jiang | 31a0b4e | 2020-07-09 15:06:16 +0900 | [diff] [blame] | 103 | |
Hugo Benichi | cd27f4e | 2020-11-19 18:32:23 +0900 | [diff] [blame] | 104 | Datapath* datapath_; |
Jie Jiang | 31a0b4e | 2020-07-09 15:06:16 +0900 | [diff] [blame] | 105 | MinijailedProcessRunner* runner_; |
Jie Jiang | 31a0b4e | 2020-07-09 15:06:16 +0900 | [diff] [blame] | 106 | }; |
| 107 | |
Hugo Benichi | 92fa203 | 2020-11-20 17:47:32 +0900 | [diff] [blame] | 108 | TrafficCounter::Source TrafficSourceToProto(TrafficSource source); |
Hugo Benichi | 93306e5 | 2020-12-04 16:08:00 +0900 | [diff] [blame] | 109 | TrafficSource ProtoToTrafficSource(TrafficCounter::Source source); |
Hugo Benichi | 92fa203 | 2020-11-20 17:47:32 +0900 | [diff] [blame] | 110 | |
Jie Jiang | 31a0b4e | 2020-07-09 15:06:16 +0900 | [diff] [blame] | 111 | } // namespace patchpanel |
| 112 | |
| 113 | #endif // PATCHPANEL_COUNTERS_SERVICE_H_ |