trex_stateless_rx_core.h revision 4477491b
1/*
2  Ido Barnea
3  Cisco Systems, Inc.
4*/
5
6/*
7  Copyright (c) 2016-2016 Cisco Systems, Inc.
8
9  Licensed under the Apache License, Version 2.0 (the "License");
10  you may not use this file except in compliance with the License.
11  You may obtain a copy of the License at
12
13  http://www.apache.org/licenses/LICENSE-2.0
14
15  Unless required by applicable law or agreed to in writing, software
16  distributed under the License is distributed on an "AS IS" BASIS,
17  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
18  See the License for the specific language governing permissions and
19  limitations under the License.
20*/
21#ifndef __TREX_STATELESS_RX_CORE_H__
22#define __TREX_STATELESS_RX_CORE_H__
23#include <stdint.h>
24#include "stateful_rx_core.h"
25#include "os_time.h"
26#include "pal/linux/sanb_atomic.h"
27#include "utl_cpuu.h"
28#include "trex_stateless_rx_port_mngr.h"
29
30class TrexStatelessCpToRxMsgBase;
31
32class CCPortLatencyStl {
33 public:
34    void reset();
35
36 public:
37    rx_per_flow_t m_rx_pg_stat[MAX_FLOW_STATS];
38    rx_per_flow_t m_rx_pg_stat_payload[MAX_FLOW_STATS_PAYLOAD];
39};
40
41class CRFC2544Info {
42 public:
43    void create();
44    void stop();
45    void reset();
46    void export_data(rfc2544_info_t_ &obj);
47    inline void add_sample(double stime) {
48        m_latency.Add(stime);
49        m_jitter.calc(stime);
50    }
51    inline void sample_period_end() {
52        m_latency.update();
53    }
54    inline uint32_t get_seq() {return m_seq;}
55    inline void set_seq(uint32_t val) {m_seq = val;}
56    inline void inc_seq_err(uint64_t val) {m_seq_err += val;}
57    inline void dec_seq_err() {if (m_seq_err >0) {m_seq_err--;}}
58    inline void inc_seq_err_too_big() {m_seq_err_events_too_big++;}
59    inline void inc_seq_err_too_low() {m_seq_err_events_too_low++;}
60    inline void inc_dup() {m_dup++;}
61    inline void inc_ooo() {m_ooo++;}
62    inline uint16_t get_exp_flow_seq() {return m_exp_flow_seq;}
63    inline void set_exp_flow_seq(uint16_t flow_seq) {m_exp_flow_seq = flow_seq;}
64    inline uint16_t get_prev_flow_seq() {return m_prev_flow_seq;}
65    inline bool no_flow_seq() {return (m_exp_flow_seq == FLOW_STAT_PAYLOAD_INITIAL_FLOW_SEQ) ? true : false;}
66 private:
67    uint32_t m_seq; // expected next seq num
68    CTimeHistogram  m_latency; // latency info
69    CJitter         m_jitter;
70    uint64_t m_seq_err; // How many packet seq num gaps we saw (packets lost or out of order)
71    uint64_t m_seq_err_events_too_big; // How many packet seq num greater than expected events we had
72    uint64_t m_seq_err_events_too_low; // How many packet seq num lower than expected events we had
73    uint64_t m_ooo; // Packets we got with seq num lower than expected (We guess they are out of order)
74    uint64_t m_dup; // Packets we got with same seq num
75    uint16_t m_exp_flow_seq; // flow sequence number we should see in latency header
76    // flow sequence number previously used with this id. We use this to catch packets arriving late from an old flow
77    uint16_t m_prev_flow_seq;
78};
79
80class CRxCoreErrCntrs {
81    friend CRxCoreStateless;
82
83 public:
84    uint64_t get_bad_header() {return m_bad_header;}
85    uint64_t get_old_flow() {return m_old_flow;}
86    CRxCoreErrCntrs() {
87        reset();
88    }
89    void reset() {
90        m_bad_header = 0;
91        m_old_flow = 0;
92    }
93
94 public:
95    uint64_t m_bad_header;
96    uint64_t m_old_flow;
97};
98
99/**
100 * stateless RX core
101 *
102 */
103class CRxCoreStateless {
104
105    /**
106     * core states
107     */
108    enum state_e {
109        STATE_IDLE,
110        STATE_WORKING,
111        STATE_QUIT
112    };
113
114 public:
115    void start();
116    void create(const CRxSlCfg &cfg);
117    void reset_rx_stats(uint8_t port_id);
118    int get_rx_stats(uint8_t port_id, rx_per_flow_t *rx_stats, int min, int max, bool reset
119                     , TrexPlatformApi::driver_stat_cap_e type);
120    int get_rfc2544_info(rfc2544_info_t *rfc2544_info, int min, int max, bool reset);
121    int get_rx_err_cntrs(CRxCoreErrCntrs *rx_err);
122
123
124    void quit() {m_state = STATE_QUIT;}
125    bool is_working() const {return (m_state == STATE_WORKING);}
126    double get_cpu_util();
127    void update_cpu_util();
128
129    const RXPacketBuffer *get_rx_queue_pkts(uint8_t port_id) {
130        return m_rx_port_mngr[port_id].get_pkt_buffer();
131    }
132
133    /**
134     * start capturing of RX packets on a specific port
135     *
136     */
137    void start_recorder(uint8_t port_id, const std::string &pcap_filename, uint64_t limit);
138    void stop_recorder(uint8_t port_id);
139
140    /**
141     * start RX queueing of packets
142     *
143     */
144    void start_queue(uint8_t port_id, uint64_t size);
145    void stop_queue(uint8_t port_id);
146
147    /**
148     * enable latency feature for RX packets
149     * will be apply to all ports
150     */
151    void enable_latency();
152    void disable_latency();
153
154    const RXPortManager &get_rx_port_mngr(uint8_t port_id);
155
156 private:
157    void handle_cp_msg(TrexStatelessCpToRxMsgBase *msg);
158    bool periodic_check_for_cp_messages();
159    void tickle();
160    void idle_state_loop();
161
162    void recalculate_next_state();
163    bool are_any_features_active();
164
165    void capture_pkt(rte_mbuf_t *m);
166    void handle_rx_queue_msgs(uint8_t thread_id, CNodeRing * r);
167    void handle_work_stage();
168    void port_manager_tick();
169
170    int process_all_pending_pkts(bool flush_rx = false);
171
172    void flush_all_pending_pkts() {
173        process_all_pending_pkts(true);
174    }
175
176    void try_rx_queues();
177
178 private:
179    TrexMonitor      m_monitor;
180    uint32_t         m_max_ports;
181    bool             m_capture;
182    state_e          m_state;
183    CNodeRing       *m_ring_from_cp;
184    CNodeRing       *m_ring_to_cp;
185    CCpuUtlDp        m_cpu_dp_u;
186    CCpuUtlCp        m_cpu_cp_u;
187
188    // Used for acking "work" (go out of idle) messages from cp
189    volatile bool m_ack_start_work_msg __rte_cache_aligned;
190
191    CRxCoreErrCntrs m_err_cntrs;
192    CRFC2544Info m_rfc2544[MAX_FLOW_STATS_PAYLOAD];
193
194    RXPortManager m_rx_port_mngr[TREX_MAX_PORTS];
195};
196#endif
197