trex_platform_api.h revision 558ce764
1/*
2 Itay Marom
3 Cisco Systems, Inc.
4*/
5
6/*
7Copyright (c) 2015-2015 Cisco Systems, Inc.
8
9Licensed under the Apache License, Version 2.0 (the "License");
10you may not use this file except in compliance with the License.
11You may obtain a copy of the License at
12
13    http://www.apache.org/licenses/LICENSE-2.0
14
15Unless required by applicable law or agreed to in writing, software
16distributed under the License is distributed on an "AS IS" BASIS,
17WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
18See the License for the specific language governing permissions and
19limitations under the License.
20*/
21
22#ifndef __TREX_PLATFORM_API_H__
23#define __TREX_PLATFORM_API_H__
24
25#include <stdint.h>
26#include <vector>
27#include <string>
28#include <string.h>
29#include "flow_stat_parser.h"
30#include "trex_defs.h"
31#include "trex_stateless_rx_defs.h"
32#include "trex_port_attr.h"
33#include <json/json.h>
34
35/**
36 * Global stats
37 *
38 * @author imarom (06-Oct-15)
39 */
40
41
42class TrexPlatformGlobalStats {
43public:
44    TrexPlatformGlobalStats() {
45        m_stats = {0};
46    }
47
48    struct {
49        double m_cpu_util;
50        double m_rx_cpu_util;
51        double m_tx_bps;
52        double m_rx_bps;
53
54        double m_tx_pps;
55        double m_rx_pps;
56
57        uint64_t m_total_tx_pkts;
58        uint64_t m_total_rx_pkts;
59
60        uint64_t m_total_tx_bytes;
61        uint64_t m_total_rx_bytes;
62
63        uint64_t m_tx_rx_errors;
64    } m_stats;
65};
66
67/**
68 * Per Interface stats
69 *
70 * @author imarom (26-Oct-15)
71 */
72class TrexPlatformInterfaceStats {
73
74public:
75    TrexPlatformInterfaceStats() {
76        m_stats = {0};
77    }
78
79public:
80
81    struct {
82
83        double m_tx_bps;
84        double m_rx_bps;
85
86        double m_tx_pps;
87        double m_rx_pps;
88
89        uint64_t m_total_tx_pkts;
90        uint64_t m_total_rx_pkts;
91
92        uint64_t m_total_tx_bytes;
93        uint64_t m_total_rx_bytes;
94
95        uint64_t m_tx_rx_errors;
96    } m_stats;
97};
98
99
100/**
101 * low level API interface
102 * can be implemented by DPDK or mock
103 *
104 * @author imarom (25-Oct-15)
105 */
106
107class TrexPlatformApi {
108public:
109    enum driver_stat_cap_e {
110        IF_STAT_IPV4_ID = 1,
111        IF_STAT_PAYLOAD = 2,
112        IF_STAT_IPV6_FLOW_LABEL = 4,
113        IF_STAT_RX_BYTES_COUNT = 8, // Card support counting rx bytes
114    };
115
116    /**
117     * interface static info
118     *
119     */
120    struct intf_info_st {
121        std::string     driver_name;
122        uint8_t         hw_macaddr[6];
123        std::string     pci_addr;
124        int             numa_node;
125        bool            has_crc;
126    };
127
128    virtual void port_id_to_cores(uint8_t port_id, std::vector<std::pair<uint8_t, uint8_t>> &cores_id_list) const = 0;
129    virtual void get_global_stats(TrexPlatformGlobalStats &stats) const = 0;
130    virtual void get_interface_stats(uint8_t interface_id, TrexPlatformInterfaceStats &stats) const = 0;
131
132    virtual void get_interface_info(uint8_t interface_id, intf_info_st &info) const = 0;
133
134    virtual void publish_async_data_now(uint32_t key, bool baseline) const = 0;
135    virtual void publish_async_port_attr_changed(uint8_t port_id) const = 0;
136    virtual uint8_t get_dp_core_count() const = 0;
137    virtual void get_interface_stat_info(uint8_t interface_id, uint16_t &num_counters, uint16_t &capabilities) const =0;
138    virtual int get_flow_stats(uint8_t port_id, void *stats, void *tx_stats, int min, int max, bool reset
139                               , TrexPlatformApi::driver_stat_cap_e type) const = 0;
140    virtual int get_rfc2544_info(void *rfc2544_info, int min, int max, bool reset) const = 0;
141    virtual int get_rx_err_cntrs(void *rx_err_cntrs) const = 0;
142    virtual int reset_hw_flow_stats(uint8_t port_id) const = 0;
143    virtual void get_port_num(uint8_t &port_num) const = 0;
144    virtual int add_rx_flow_stat_rule(uint8_t port_id, uint16_t l3_type, uint8_t l4_proto
145                                      , uint8_t ipv6_next_h, uint16_t id) const = 0;
146    virtual int del_rx_flow_stat_rule(uint8_t port_id, uint16_t l3_type, uint8_t l4_proto
147                                      , uint8_t ipv6_next_h, uint16_t id) const = 0;
148    virtual void flush_dp_messages() const = 0;
149    virtual int get_active_pgids(flow_stat_active_t &result) const = 0;
150    virtual int get_cpu_util_full(cpu_util_full_t &result) const = 0;
151    virtual int get_mbuf_util(Json::Value &result) const = 0;
152    virtual CFlowStatParser *get_flow_stat_parser() const = 0;
153    virtual TRexPortAttr *getPortAttrObj(uint8_t port_id) const = 0;
154    virtual void mark_for_shutdown() const = 0;
155    virtual int get_xstats_values(uint8_t port_id, xstats_values_t &xstats_values) const = 0;
156    virtual int get_xstats_names(uint8_t port_id, xstats_names_t &xstats_names) const = 0;
157
158    virtual ~TrexPlatformApi() {}
159};
160
161
162/**
163 * DPDK implementation of the platform API
164 *
165 * @author imarom (26-Oct-15)
166 */
167class TrexDpdkPlatformApi : public TrexPlatformApi {
168public:
169    void port_id_to_cores(uint8_t port_id, std::vector<std::pair<uint8_t, uint8_t>> &cores_id_list) const;
170    void get_global_stats(TrexPlatformGlobalStats &stats) const;
171    void get_interface_stats(uint8_t interface_id, TrexPlatformInterfaceStats &stats) const;
172
173    void get_interface_info(uint8_t interface_id, intf_info_st &info) const;
174
175    void publish_async_data_now(uint32_t key, bool baseline) const;
176    void publish_async_port_attr_changed(uint8_t port_id) const;
177    uint8_t get_dp_core_count() const;
178    void get_interface_stat_info(uint8_t interface_id, uint16_t &num_counters, uint16_t &capabilities) const;
179    int get_flow_stats(uint8_t port_id, void *stats, void *tx_stats, int min, int max, bool reset
180                       , TrexPlatformApi::driver_stat_cap_e type) const;
181    int get_rfc2544_info(void *rfc2544_info, int min, int max, bool reset) const;
182    int get_rx_err_cntrs(void *rx_err_cntrs) const;
183    int reset_hw_flow_stats(uint8_t port_id) const;
184    void get_port_num(uint8_t &port_num) const;
185    virtual int add_rx_flow_stat_rule(uint8_t port_id, uint16_t l3_type, uint8_t l4_proto
186                                      , uint8_t ipv6_next_h, uint16_t id) const;
187    virtual int del_rx_flow_stat_rule(uint8_t port_id, uint16_t l3_type, uint8_t l4_proto
188                                      , uint8_t ipv6_next_h, uint16_t id) const;
189    void flush_dp_messages() const;
190    int get_active_pgids(flow_stat_active_t &result) const;
191    int get_cpu_util_full(cpu_util_full_t &result) const;
192    int get_mbuf_util(Json::Value &result) const;
193    void mark_for_shutdown() const;
194    CFlowStatParser *get_flow_stat_parser() const;
195    TRexPortAttr *getPortAttrObj(uint8_t port_id) const;
196
197    int get_xstats_values(uint8_t port_id, xstats_values_t &xstats_values) const;
198    int get_xstats_names(uint8_t port_id, xstats_names_t &xstats_names) const;
199};
200
201
202/**
203 * for simulation
204 *
205 * @author imarom (25-Feb-16)
206 */
207class SimPlatformApi : public TrexPlatformApi {
208public:
209
210    SimPlatformApi(int dp_core_count) {
211        m_dp_core_count = dp_core_count;
212        m_port_attr = new SimTRexPortAttr();
213    }
214
215    ~SimPlatformApi() {
216        delete m_port_attr;
217    }
218
219    virtual uint8_t get_dp_core_count() const {
220        return m_dp_core_count;
221    }
222
223    virtual void get_global_stats(TrexPlatformGlobalStats &stats) const {
224    }
225
226    virtual void get_interface_info(uint8_t interface_id, intf_info_st &info) const {
227
228        info.driver_name = "TEST";
229        info.has_crc = true;
230        info.numa_node = 0;
231
232        memset(&info.hw_macaddr, 0, sizeof(info.hw_macaddr));
233    }
234
235    virtual void get_interface_stats(uint8_t interface_id, TrexPlatformInterfaceStats &stats) const {
236    }
237    virtual void get_interface_stat_info(uint8_t interface_id, uint16_t &num_counters, uint16_t &capabilities) const {num_counters=128; capabilities=TrexPlatformApi::IF_STAT_IPV4_ID | TrexPlatformApi::IF_STAT_PAYLOAD; }
238
239    virtual void port_id_to_cores(uint8_t port_id, std::vector<std::pair<uint8_t, uint8_t>> &cores_id_list) const {
240        for (int i = 0; i < m_dp_core_count; i++) {
241             cores_id_list.push_back(std::make_pair(i, 0));
242        }
243    }
244
245    virtual void publish_async_data_now(uint32_t key, bool baseline) const {
246
247    }
248
249    virtual void publish_async_port_attr_changed(uint8_t port_id) const {
250    }
251
252    int get_flow_stats(uint8_t port_id, void *stats, void *tx_stats, int min, int max, bool reset
253                       , TrexPlatformApi::driver_stat_cap_e type) const {return 0;};
254    virtual int get_rfc2544_info(void *rfc2544_info, int min, int max, bool reset) const {return 0;};
255    virtual int get_rx_err_cntrs(void *rx_err_cntrs) const {return 0;};
256    virtual int reset_hw_flow_stats(uint8_t port_id) const {return 0;};
257    virtual void get_port_num(uint8_t &port_num) const {port_num = 2;};
258    virtual int add_rx_flow_stat_rule(uint8_t port_id, uint16_t l3_type, uint8_t l4_proto
259                                      , uint8_t ipv6_next_h, uint16_t id) const {return 0;};
260    virtual int del_rx_flow_stat_rule(uint8_t port_id, uint16_t l3_type, uint8_t l4_proto
261                                      , uint8_t ipv6_next_h, uint16_t id) const {return 0;};
262
263    void flush_dp_messages() const {
264    }
265    int get_active_pgids(flow_stat_active_t &result) const {return 0;}
266    int get_cpu_util_full(cpu_util_full_t &result) const {return 0;}
267    int get_mbuf_util(Json::Value &result) const {return 0;}
268    CFlowStatParser *get_flow_stat_parser() const {return new CFlowStatParser();}
269    TRexPortAttr *getPortAttrObj(uint8_t port_id) const {return m_port_attr;}
270
271    void mark_for_shutdown() const {}
272    int get_xstats_values(uint8_t port_id, xstats_values_t &xstats_values) const {return 0;};
273    int get_xstats_names(uint8_t port_id, xstats_names_t &xstats_names) const {return 0;};
274
275private:
276    int m_dp_core_count;
277    SimTRexPortAttr * m_port_attr;
278};
279
280#endif /* __TREX_PLATFORM_API_H__ */
281