1 /*
2  * Copyright (C) 2020 The Android Open Source Project
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  *      http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16 
17 #pragma once
18 
19 #include <libnl++/Buffer.h>
20 
21 #include <linux/rtnetlink.h>
22 
23 #include <sstream>
24 
25 namespace android::nl::protocols::route {
26 
27 // rtnl_link_ifmap
28 void mapToStream(std::stringstream& ss, const Buffer<nlattr> attr);
29 
30 // ifla_cacheinfo
31 void ifla_cacheinfoToStream(std::stringstream& ss, const Buffer<nlattr> attr);
32 
33 // rtnl_link_stats or rtnl_link_stats64
34 template <typename T>
statsToStream(std::stringstream & ss,const Buffer<nlattr> attr)35 void statsToStream(std::stringstream& ss, const Buffer<nlattr> attr) {
36     const auto& [ok, data] = attr.data<T>().getFirst();
37     if (!ok) {
38         ss << "invalid structure";
39         return;
40     }
41     ss << '{'                              //
42        << data.rx_packets << ','           //
43        << data.tx_packets << ','           //
44        << data.rx_bytes << ','             //
45        << data.tx_bytes << ','             //
46        << data.rx_errors << ','            //
47        << data.tx_errors << ','            //
48        << data.rx_dropped << ','           //
49        << data.tx_dropped << ','           //
50        << data.multicast << ','            //
51        << data.collisions << ','           //
52        << data.rx_length_errors << ','     //
53        << data.rx_over_errors << ','       //
54        << data.rx_crc_errors << ','        //
55        << data.rx_frame_errors << ','      //
56        << data.rx_fifo_errors << ','       //
57        << data.rx_missed_errors << ','     //
58        << data.tx_aborted_errors << ','    //
59        << data.tx_carrier_errors << ','    //
60        << data.tx_fifo_errors << ','       //
61        << data.tx_heartbeat_errors << ','  //
62        << data.tx_window_errors << ','     //
63        << data.rx_compressed << ','        //
64        << data.tx_compressed << ','        //
65        << data.rx_nohandler << '}';
66 }
67 
68 }  // namespace android::nl::protocols::route
69