summaryrefslogtreecommitdiffstats
path: root/src/stateless/common/trex_stateless_pkt.h
blob: f44355dc6a38252404a6d086c6c5d4a487cac536 (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
/*
  Itay Marom
  Cisco Systems, Inc.
*/

/*
  Copyright (c) 2016-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 __TREX_STATELESS_PKT_H__
#define __TREX_STATELESS_PKT_H__

#include <stdint.h>
#include <json/json.h>
#include "mbuf.h"
#include "common/base64.h"
#include "os_time.h"


/**
 * copies MBUF to a flat buffer
 * 
 */
void mbuf_to_buffer(uint8_t *dest, const rte_mbuf_t *m);

/**************************************
 * TRex packet
 * 
 *************************************/
class TrexPkt {
public:

    /**
     * origin of the created packet
     */
    enum origin_e {
        ORIGIN_NONE = 1,
        ORIGIN_TX,
        ORIGIN_RX
    };
    
    /**
     * generate a packet from MBUF
     */
    TrexPkt(const rte_mbuf_t *m, int port = -1, origin_e origin = ORIGIN_NONE, uint64_t index = 0);
    
    /**
     * duplicate an existing packet
     */
    TrexPkt(const TrexPkt &other);
 
    
    /**
     * sets a packet index 
     * used by a buffer of packets 
     */
    void set_index(uint64_t index) {
        m_index = index;
    }
    
    uint64_t get_index() const {
        return m_index;
    }
    
    /* slow path and also RVO - pass by value is ok */
    Json::Value to_json() const {
        Json::Value output;
        output["ts"]      = m_timestamp;
        output["binary"]  = base64_encode(m_raw, m_size);
        output["port"]    = m_port;
        output["index"]   = Json::UInt64(m_index);
        
        switch (m_origin) {
        case ORIGIN_TX:
            output["origin"]  = "TX";
            break;
        case ORIGIN_RX:
            output["origin"]  = "RX";
            break;
        default:
            output["origin"]  = "NONE";
            break;
        }
        
        return output;
    }

    ~TrexPkt() {
        if (m_raw) {
            delete [] m_raw;
        }
    }

    origin_e get_origin() const {
        return m_origin;
    }
    
    int get_port() const {
        return m_port;
    }
 
    uint16_t get_size() const {
        return m_size;   
    }
    
    dsec_t get_ts() const {
        return m_timestamp;
    }
    
private:

    uint8_t   *m_raw;
    uint16_t   m_size;
    dsec_t     m_timestamp;
    origin_e   m_origin;
    int        m_port;
    uint64_t   m_index;
};


/**************************************
 * TRex packet buffer
 * 
 *************************************/
class TrexPktBuffer {
public:

    /**
     * two modes for operations: 
     *  
     * MODE_DROP_HEAD - when the buffer is full, packets will be 
     * dropped from the head (the oldest packet) 
     *  
     * MODE_DROP_TAIL - when the buffer is full, packets will be 
     * dropped from the tail (the current packet) 
     */
    enum mode_e {
        MODE_DROP_HEAD = 1,
        MODE_DROP_TAIL = 2,
    };
    
    TrexPktBuffer(uint64_t size, mode_e mode = MODE_DROP_TAIL);
    ~TrexPktBuffer();

    /**
     * push a packet to the buffer 
     * packet will be generated from a MBUF 
     *  
     */
    void push(const rte_mbuf_t *m,
              int port = -1,
              TrexPkt::origin_e origin = TrexPkt::ORIGIN_NONE,
              uint64_t pkt_index = 0);
    
    /**
     * push an existing packet structure 
     * packet will be duplicated 
     * if pkt_index is non zero - it will be updated 
     */
    void push(const TrexPkt *pkt, uint64_t pkt_index = 0);
    
    /**
     * pops a packet from the buffer
     * usually for internal usage
     */
    const TrexPkt * pop();
    
    /**
     * pops N packets from the buffer
     * N must be <= get_element_count() 
     *  
     * returns a new buffer 
     */
    TrexPktBuffer * pop_n(uint32_t count);
    
    
    /**
     * generate a JSON output of the queue
     * 
     */
    Json::Value to_json() const;


    bool is_empty() const {
        return (m_head == m_tail);
    }

    bool is_full() const {
        return ( next(m_head) == m_tail);
    }

    /**
     * return the total amount of space possible
     */
    uint32_t get_capacity() const {
        /* one slot is used for diff between full/empty */
        return (m_size - 1);
    }
    
    /**
     * see mode_e
     * 
     */
    mode_e get_mode() const {
        return m_mode;
    }
    
    /**
     * returns how many elements are in the queue
     */
    uint32_t get_element_count() const;
    
    /**
     * current bytes holded by the buffer
     */
    uint32_t get_bytes() const {
        return m_bytes; 
    }
    
private:
    int next(int v) const {
        return ( (v + 1) % m_size );
    }

    void push_internal(const TrexPkt *pkt);
    
    mode_e          m_mode;
    int             m_head;
    int             m_tail;
    int             m_size;
    uint32_t        m_bytes;
    const TrexPkt **m_buffer;
};


#endif /* __TREX_STATELESS_PKT_H__*/