summaryrefslogtreecommitdiffstats
path: root/src/flow_stat.h
blob: 91ee76d53046ad2cab46f98d52e84dd13e75bdfe (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
/*
  Ido Barnea
  Cisco Systems, Inc.
*/

/*
  Copyright (c) 2015-2016 Cisco Systems, Inc.

  Licensed under the Apache License, Version 2.0 (the "License");
  you may not use this file except in compliance with the License.
  You may obtain a copy of the License at

  http://www.apache.org/licenses/LICENSE-2.0

  Unless required by applicable law or agreed to in writing, software
  distributed under the License is distributed on an "AS IS" BASIS,
  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  See the License for the specific language governing permissions and
  limitations under the License.
*/

#ifndef __FLOW_STAT_H__
#define __FLOW_STAT_H__
#include <stdio.h>
#include <string>
#include <map>
#include "trex_defs.h"
#include "trex_exception.h"
#include "trex_stream.h"
#include "msg_manager.h"
#include <internal_api/trex_platform_api.h>

// range reserved for rx stat measurement is from IP_ID_RESERVE_BASE to 0xffff
// Do not change this value. In i350 cards, we filter according to first byte of IP ID
// In other places, we identify packets by if (ip_id > IP_ID_RESERVE_BASE)
#define IP_ID_RESERVE_BASE 0xff00

typedef std::map<uint32_t, uint16_t> flow_stat_map_t;
typedef std::map<uint32_t, uint16_t>::iterator flow_stat_map_it_t;

class CRxCoreStateless;

struct flow_stat_payload_header {
    uint64_t time_stamp;
    uint16_t hw_id;
    uint16_t magic;
    uint32_t seq;
};


class TrexFStatEx : public TrexException {
 public:
    TrexFStatEx(const std::string &what, enum TrexExceptionTypes_t type): TrexException(what, type) {
    }
};

class tx_per_flow_t_ {
 public:
    tx_per_flow_t_() {
        clear();
    }
    inline uint64_t get_bytes() {
        return m_bytes;
    }
    inline uint64_t get_pkts() {
        return m_pkts;
    }
    inline void set_bytes(uint64_t bytes) {
        m_bytes = bytes;;
    }
    inline void set_pkts(uint64_t pkts) {
        m_pkts = pkts;
    }
    inline void add_bytes(uint64_t bytes) {
        m_bytes += bytes;;
    }
    inline void add_pkts(uint64_t pkts) {
        m_pkts += pkts;
    }
    inline void clear() {
        m_bytes = 0;
        m_pkts = 0;
    }
    inline tx_per_flow_t_ operator+ (const tx_per_flow_t_ &t_in) {
        tx_per_flow_t_ t_out;
        t_out.m_bytes = this->m_bytes + t_in.m_bytes;
        t_out.m_pkts = this->m_pkts + t_in.m_pkts;
        return t_out;
    }

    inline tx_per_flow_t_ operator- (const tx_per_flow_t_ &t_in) {
        tx_per_flow_t_ t_out;
        t_out.m_bytes = this->m_bytes - t_in.m_bytes;
        t_out.m_pkts = this->m_pkts - t_in.m_pkts;
        return t_out;
    }

    inline tx_per_flow_t_ operator+= (const tx_per_flow_t_ &t_in) {
        m_bytes += t_in.m_bytes;
        m_pkts += t_in.m_pkts;
        return *this;
    }

    inline bool operator!= (const tx_per_flow_t_ &t_in) {
        if ((m_bytes != t_in.m_bytes) || (m_pkts != t_in.m_pkts))
            return true;
        return false;
    }

    friend std::ostream& operator<<(std::ostream& os, const tx_per_flow_t_ &t) {
        os  << "p:" << t.m_pkts << " b:" << t.m_bytes;
        return os;
    }

 private:
    uint64_t m_bytes;
    uint64_t m_pkts;
};

typedef class tx_per_flow_t_ tx_per_flow_t;
typedef class tx_per_flow_t_ rx_per_flow_t;

class CPhyEthIF;
class CFlowStatParser;

class CFlowStatUserIdInfo {
 public:
    CFlowStatUserIdInfo(uint8_t proto);
    virtual ~CFlowStatUserIdInfo() {};
    friend std::ostream& operator<<(std::ostream& os, const CFlowStatUserIdInfo& cf);
    void set_rx_counter(uint8_t port, rx_per_flow_t val) {m_rx_counter[port] = val;}
    rx_per_flow_t get_rx_counter(uint8_t port) {return m_rx_counter[port] + m_rx_counter_base[port];}
    void set_tx_counter(uint8_t port, tx_per_flow_t val) {m_tx_counter[port] = val;}
    tx_per_flow_t get_tx_counter(uint8_t port) {return m_tx_counter[port] + m_tx_counter_base[port];}
    void set_hw_id(uint16_t hw_id) {m_hw_id = hw_id;}
    uint16_t get_hw_id() {return m_hw_id;}
    void reset_hw_id();
    bool is_hw_id() {return (m_hw_id != UINT16_MAX);}
    uint64_t get_proto() {return m_proto;}
    uint8_t get_ref_count() {return m_ref_count;}
    virtual void add_stream(uint8_t proto);
    int del_stream() {m_ref_count--; return m_ref_count;}
    void add_started_stream() {m_trans_ref_count++;}
    int stop_started_stream() {m_trans_ref_count--; return m_trans_ref_count;}
    bool is_started() {return (m_trans_ref_count != 0);}
    bool need_to_send_rx(uint8_t port) {return m_rx_changed[port];}
    bool need_to_send_tx(uint8_t port) {return m_tx_changed[port];}
    void set_no_need_to_send_rx(uint8_t port) {m_rx_changed[port] = false;}
    void set_no_need_to_send_tx(uint8_t port) {m_tx_changed[port] = false;}
    void set_need_to_send_rx(uint8_t port) {m_rx_changed[port] = true;}
    void set_need_to_send_tx(uint8_t port) {m_tx_changed[port] = true;}
    bool was_sent() {return m_was_sent == true;}
    void set_was_sent(bool val) {m_was_sent = val;}

 private:
    bool m_rx_changed[TREX_MAX_PORTS]; // Which RX counters changed since we last published
    bool m_tx_changed[TREX_MAX_PORTS]; // Which TX counters changed since we last published
    rx_per_flow_t m_rx_counter[TREX_MAX_PORTS]; // How many packets received with this user id since stream start
    // How many packets received with this user id, since stream creation, before stream start.
    rx_per_flow_t m_rx_counter_base[TREX_MAX_PORTS];
    tx_per_flow_t m_tx_counter[TREX_MAX_PORTS]; // How many packets transmitted with this user id since stream start
    // How many packets transmitted with this user id, since stream creation, before stream start.
    tx_per_flow_t m_tx_counter_base[TREX_MAX_PORTS];
    uint16_t m_hw_id;     // Associated hw id. UINT16_MAX if no associated hw id.
    uint8_t m_proto;      // protocol (UDP, TCP, other), associated with this user id.
    uint8_t m_ref_count;  // How many streams with this user id exists
    uint8_t m_trans_ref_count;  // How many streams with this user id currently transmit
    bool m_was_sent; // Did we send this info to clients once?
};

typedef std::map<uint32_t, class CFlowStatUserIdInfo *> flow_stat_user_id_map_t;
typedef std::map<uint32_t, class CFlowStatUserIdInfo *>::iterator flow_stat_user_id_map_it_t;

class CFlowStatUserIdInfoPayload : public CFlowStatUserIdInfo {
 public:
    CFlowStatUserIdInfoPayload(uint8_t proto) : CFlowStatUserIdInfo(proto){};
    virtual void add_stream(uint8_t proto);
};

class CFlowStatUserIdMap {
 public:
    CFlowStatUserIdMap();
    friend std::ostream& operator<<(std::ostream& os, const CFlowStatUserIdMap& cf);
    bool is_empty() {return (m_map.empty() == true);};
    uint16_t get_hw_id(uint32_t user_id);
    CFlowStatUserIdInfo * find_user_id(uint32_t user_id);
    CFlowStatUserIdInfo * add_user_id(uint32_t user_id, uint8_t proto);
    void add_stream(uint32_t user_id, uint8_t proto);
    int del_stream(uint32_t user_id);
    int start_stream(uint32_t user_id, uint16_t hw_id);
    int start_stream(uint32_t user_id);
    int stop_stream(uint32_t user_id);
    bool is_started(uint32_t user_id);
    uint8_t l4_proto(uint32_t user_id);
    uint16_t unmap(uint32_t user_id);
    flow_stat_user_id_map_it_t begin() {return m_map.begin();}
    flow_stat_user_id_map_it_t end() {return m_map.end();}
 private:
    flow_stat_user_id_map_t m_map;
};

class CFlowStatHwIdMap {
 public:
    CFlowStatHwIdMap();
    friend std::ostream& operator<<(std::ostream& os, const CFlowStatHwIdMap& cf);
    uint16_t find_free_hw_id();
    void map(uint16_t hw_id, uint32_t user_id);
    void unmap(uint16_t hw_id);
    uint32_t get_user_id(uint16_t hw_id) {return m_map[hw_id];};
 private:
    uint32_t m_map[MAX_FLOW_STATS]; // translation from hw id to user id
    uint16_t m_num_free; // How many free entries in the m_rules array
};

class CFlowStatRuleMgr {
 public:
    enum flow_stat_rule_types_e {
        FLOW_STAT_RULE_TYPE_NONE,
        FLOW_STAT_RULE_TYPE_IPV4_ID,
        FLOW_STAT_RULE_TYPE_PAYLOAD,
        FLOW_STAT_RULE_TYPE_IPV6_FLOW_LABEL,
    };

    CFlowStatRuleMgr();
    ~CFlowStatRuleMgr();
    friend std::ostream& operator<<(std::ostream& os, const CFlowStatRuleMgr& cf);
    void copy_state(TrexStream * from, TrexStream * to);
    void init_stream(TrexStream * stream);
    int add_stream(TrexStream * stream);
    int del_stream(TrexStream * stream);
    int start_stream(TrexStream * stream);
    int stop_stream(TrexStream * stream);
    int get_active_pgids(flow_stat_active_t &result);
    bool dump_json(std::string & json, bool baseline);

 private:
    void create();
    int compile_stream(const TrexStream * stream, CFlowStatParser *parser);
    int add_hw_rule(uint16_t hw_id, uint8_t proto);
    void send_start_stop_msg_to_rx(bool is_start);

 private:
    CFlowStatHwIdMap m_hw_id_map; // map hw ids to user ids
    // ??? need to make CFlowStatHwIdMap class adjustable per size. For now it is working since we allow same number
    // of IP ID and pyaload rules
    CFlowStatHwIdMap m_hw_id_map_payload; // map hw id numbers of payload rules to user ids
    CFlowStatUserIdMap m_user_id_map; // map user ids to hw ids
    uint8_t m_num_ports; // How many ports are being used
    const TrexPlatformApi *m_api;
    const CRxCoreStateless *m_rx_core;
    int m_max_hw_id; // max hw id we ever used
    int m_max_hw_id_payload; // max hw id we ever used for payload rules
    uint32_t m_num_started_streams; // How many started (transmitting) streams we have
    CNodeRing *m_ring_to_rx; // handle for sending messages to Rx core
    CFlowStatParser *m_parser;
    uint16_t m_capabilities;
};

#endif