summaryrefslogtreecommitdiffstats
path: root/src/rx_check.h
blob: 07f5684cbc661275708a505f6a6e405ac2d95f3a (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
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
#ifndef  RX_CHECK_H
#define  RX_CHECK_H
/*
 Hanoh Haim
 Cisco Systems, Inc.
*/

/*
Copyright (c) 2015-2015 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.
*/


#include "timer_wheel_pq.h"
#include "rx_check_header.h"
#include "time_histogram.h"
#include "utl_jitter.h"

                                 

typedef enum {
    CLIENT_SIDE = 0,    
    SERVER_SIDE = 1,    
    CS_NUM = 2,
    CS_INVALID = 255
} pkt_dir_enum_t;

typedef uint8_t   pkt_dir_t  ;


void  flow_aging_callback(CFlowTimerHandle * t);

class CRxCheckFlowPerDir {
public:
    CRxCheckFlowPerDir(){
        m_flags=0;
        m_pkts=0;
        m_seq=0;
        m_flow_size=0;

    }
    uint16_t         m_flow_size; // how many packets in this direction 
    uint16_t         m_pkts;
    uint16_t         m_seq;
private:
    uint16_t         m_flags;
public:

    void set_fif_seen(uint16_t flow_per_dir){
        m_pkts=1;
        m_seq=1;
        m_flags |=2;
        m_flow_size=flow_per_dir;
    }
    bool is_fif_seen(){
        return ( (m_flags & 2) ==2 ?true:false);
    }

    void set_init_not_from_first_packet(){
        m_flags |=1;
    }
    bool is_init_not_from_first_pkt(){
		return ( (m_flags & 2) ==2 ?true:false);
    }
};

class CRxCheckFlow {
public:
    CRxCheckFlow(){
        m_aging_timer_handle.m_callback =flow_aging_callback;
        m_aging_timer_handle.m_object = (void *)this;
        m_aging_timer_handle.m_id= 0x1234;
		m_oo_err=0;
        m_flags=0;
    }


public:
    /* timestamp of FIF */
    uint64_t            m_flow_id; /* key*/
    CRxCheckFlowPerDir  m_dir[CS_NUM];
    CFlowTimerHandle    m_aging_timer_handle;
    uint16_t            m_oo_err; /* out of order issue */
    uint16_t            m_flags;
public:

    uint16_t  get_total_pkt_seen(void){
        return (m_dir[0].m_pkts+
                m_dir[1].m_pkts);
    }
    uint16_t  get_total_pkt_expected(void){
        return (m_dir[0].m_flow_size+
                m_dir[1].m_flow_size);
    }

    bool      is_all_pkts_seen(void){
        int i;
        int c=0;
        for (i=0; i<2; i++) {
            if ( (m_dir[i].m_pkts!=m_dir[i].m_flow_size) ){
                return (false);
            }
            if (m_dir[i].m_flow_size>0) {
                c++;
            }
        }
        int expc=is_both_dir()?2:1;
        if ( expc == c ) {
            return ( (m_oo_err==0)?true:false );
        }
        return (false);
    }

    void set_both_dir(){
        m_flags |=2;
    }
    bool is_both_dir(){
        return ( (m_flags & 2) == 2 ?true:false);
    }



    void set_aged_correctly(){
        m_flags |=4;
    }
    bool is_aged_correctly(){
        return ( (m_flags & 4) ==4 ?true:false);
    }

};




class CRxCheckFlowTableStats {
public:

    uint64_t  m_total_rx;
    uint64_t  m_total_rx_bytes;

    uint64_t  m_lookup;
    uint64_t  m_found;
    uint64_t  m_fif;
    uint64_t  m_add;
    uint64_t  m_remove;
    uint64_t  m_active;
	uint64_t  m_err_drop;
	uint64_t  m_err_aged;


    uint64_t  m_err_no_magic;
    uint64_t  m_err_wrong_pkt_id;
    uint64_t  m_err_fif_seen_twice;
	uint64_t  m_err_open_with_no_fif_pkt;

	uint64_t  m_err_oo_dup; /* got same packets id twice  expect 1 got 0 */

	uint64_t  m_err_oo_early; /* miss packet ,expect 1 got 2 */
	uint64_t  m_err_oo_late;  /* early packet ,expect 7 got 6 */

    uint64_t  m_err_flow_length_changed;  /* early packet ,expect 7 got 6 */

    uint64_t get_total_err(void){
        return (m_err_drop+m_err_aged+
                    m_err_no_magic+
                    m_err_wrong_pkt_id+
                    m_err_fif_seen_twice+
                    m_err_open_with_no_fif_pkt+
                    m_err_oo_dup+
                    m_err_oo_early+
                    m_err_oo_late+m_err_flow_length_changed);

    }

public:
    void Clear();
    void Dump(FILE *fd);
    void dump_json(std::string & json);
};



typedef CRxCheckFlow * rx_check_flow_ptr;
typedef std::map<uint64_t, rx_check_flow_ptr, std::less<uint64_t> > rx_check_flow_map_t;
typedef rx_check_flow_map_t::iterator rx_check_flow_map_iter_t;


class CRxCheckFlowTableMap   {
public:
    virtual bool Create(int max_size);
    virtual void Delete();
    virtual bool remove(uint64_t fid );
    virtual CRxCheckFlow * lookup(uint64_t fid );
    virtual CRxCheckFlow * add(uint64_t fid );
    virtual void remove_all(void);
    void dump_all(FILE *fd);
    uint64_t count(void);
public:
    rx_check_flow_map_t          m_map;
};



class uint64_tHashEnv
{
public:
	static uint32_t Hash(uint64_t x)
	{
		return ( (x >>40) ^ (x & 0xffffffff));
	}
};

template<class T>
class CMyFSA {
public:
	bool Create(uint32_t size, bool supportGetNext, bool ctorRequired){
		return(true);
	}

    void Delete(){
	}
	void Reset(){
	}

	T * GetNewItem(){
		return (new T());
	}

	void ReturnItem(T *obj){
		delete obj;
	}
};



#if 0

typedef CHashEntry<uint64_t,CRxCheckFlow> rx_c_hash_ent_t;
typedef CCloseHash<uint64_t,CRxCheckFlow,uint64_tHashEnv,CMyFSA<rx_c_hash_ent_t> > rx_c_hash_t;


class CRxCheckFlowTableHash   {
public:
    bool Create(int max_size){
		return ( m_hash.Create(max_size,0,false,false,true) );
	}
    void Delete(){
		m_hash.Delete();
	}
    bool remove(uint64_t fid ) {
		return(m_hash.Remove(fid)==hsOK?true:false);
	}
    CRxCheckFlow * lookup(uint64_t fid ){
		rx_c_hash_ent_t *lp=m_hash.Find(fid);
		if (lp) {
			return (&lp->value);
		}else{
			return ((CRxCheckFlow *)NULL);
		}
	}
    CRxCheckFlow * add(uint64_t fid ){
		rx_c_hash_ent_t *lp;
		assert(m_hash.Insert(fid,lp)==hsOK);
		return (&lp->value);
	}

    void remove_all(void){
		
	}
    void dump_all(FILE *fd){
		m_hash.Dump(fd);
	}
    uint64_t count(void){
		return ( m_hash.GetSize());

	}
public:

	rx_c_hash_t                  m_hash;
};

#endif


// must be 2^
#define MAX_TEMPLATES_STATS 32

#define MAX_TEMPLATES_STATS_MASK (MAX_TEMPLATES_STATS-1)

class CPerTxthreadTemplateInfo {

public:
    CPerTxthreadTemplateInfo(){
        Clear();
    }
    void Clear(){
        memset(m_template_info,0,sizeof(m_template_info));
    }
    void Dump(FILE *fd);

    void Add(CPerTxthreadTemplateInfo * obj);

    void inc_template(uint8_t index){
        if (index<MAX_TEMPLATES_STATS) {
            m_template_info[index]++;
        }else{
            m_template_info[MAX_TEMPLATES_STATS-1]++;
        }
    }

    std::string dump_as_json(std::string name);

    uint64_t               m_template_info[MAX_TEMPLATES_STATS];
};

class CPerTemplateInfo {
public:
    CPerTemplateInfo()  {
        reset();
    }

    void reset(){
        m_errors=0;
        m_rx_pkts=0;
        m_jitter.reset();
    }

    void  calc(double dtime){
        m_jitter.calc(dtime);
    }

    uint32_t get_jitter_usec(){
        return ((uint32_t)(m_jitter.get_jitter()*1000000.0));
    }

    void      inc_error_counter(void){
        m_errors++;

    }

    uint64_t get_error_counter(){
        return (m_errors);
    }

    void      inc_rx_counter(void){
        m_rx_pkts++;
    }

    uint64_t get_rx_counter(){
        return (m_rx_pkts);
    }


private:
    uint64_t m_rx_pkts;
    CJitter  m_jitter;
    uint64_t m_errors;
};

class RxCheckManager {

public:
    bool Create();
    void Delete();
    void handle_packet(CRx_check_header * rxh);
	void Dump(FILE *fd);
    void DumpShort(FILE *fd);
    void DumpTemplate(FILE *fd,bool verbose);
    void DumpTemplateFull(FILE *fd);

    uint32_t getTemplateMaxJitter();

    void template_dump_json(std::string & json);

    uint64_t getTotalRx(void){
        return ( m_stats.m_total_rx );
    }

	void tw_drain();
    void tw_handle();

    void dump_json(std::string & json );

protected:
    void update_template_err(uint8_t template_id);

    CPerTemplateInfo * get_template(uint8_t index){
        uint8_t _id;
         if ( index < MAX_TEMPLATES_STATS_MASK ){
             _id=index;
         }else{
             _id=MAX_TEMPLATES_STATS_MASK;
         }
        return (&m_template_info[_id]);
    }

    bool on_flow_end(CRxCheckFlow * lp);
    friend void  flow_aging_callback(CFlowTimerHandle * t);
public:
    
    CTimerWheel                     m_tw;
    CRxCheckFlowTableMap           m_ft;
    CRxCheckFlowTableStats         m_stats;

    CTimeHistogram                 m_hist;
    CPerTemplateInfo               m_template_info[MAX_TEMPLATES_STATS];
    bool                           m_on_drain;
public:
    dsec_t                         m_cur_time;

};





#endif