Initial commit of vpp code.
[vpp.git] / vnet / vnet / vcgn / cnat_va_db.h
1 /*
2  *------------------------------------------------------------------
3  * cnat_va_db.h - definition for virtual assembly database
4  *
5  * Copyright (c) 2013 Cisco and/or its affiliates.
6  * Licensed under the Apache License, Version 2.0 (the "License");
7  * you may not use this file except in compliance with the License.
8  * You may obtain a copy of the License at:
9  *
10  *     http://www.apache.org/licenses/LICENSE-2.0
11  *
12  * Unless required by applicable law or agreed to in writing, software
13  * distributed under the License is distributed on an "AS IS" BASIS,
14  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15  * See the License for the specific language governing permissions and
16  * limitations under the License.
17  *------------------------------------------------------------------
18  */
19
20 #ifndef __CNAT_VA_DB_H__
21 #define __CNAT_VA_DB_H__
22
23 #include <clib_lite.h>
24
25 #define FRAG_DEBUG 1
26
27 /* virtual assemble hash database size ~ 16B x 64K = 1MB */ 
28
29 #define VA_TOTAL_ENTRIES    (64*1024)
30 #define VA_ENTRY_PER_BUCKET (8)   /* make sure size is power of 2 for circular FIFO */ 
31 #define VA_BUCKET_MASK      (VA_ENTRY_PER_BUCKET -1)
32 #define VA_BUCKETS          (VA_TOTAL_ENTRIES / VA_ENTRY_PER_BUCKET)
33 #define VA_KEY_SIZE         12      
34
35 typedef struct _va_entry {
36    /* key: top 12 bytes */
37     u32 src_ip;
38     u32 dst_ip;
39     u16 vrf;       /* overloaded with protocol info with top two bits */
40     u16 ip_id;
41
42     /* values */
43     u16 src_port;
44     u16 dst_port;
45 } va_entry_t;
46
47 typedef struct _va_keys {
48     u64 key64;    /* src & dst IP */
49     u32 key32;    /* vrf, protocol and ip_id */
50 } va_keys;
51
52 typedef union {
53     va_entry_t e;
54     va_keys    k;
55 } va_lookup_key;
56
57 typedef struct _va_bucket_t {
58     u32 head_entry;
59     u32 next_available_entry;    /* ~0 for empty bucket */
60     u32 new_entry_counter;       /* for debug purpose */
61     va_entry_t va_entry[VA_ENTRY_PER_BUCKET];
62 } va_bucket_t;
63
64 extern va_bucket_t va_bucket[];   /* hash table in cnat_va_db.c */
65
66 void va_bucket_init ();
67
68 inline void va_db_add_new_entry (u32 bucket_index, va_lookup_key * );
69 inline int  va_db_delete_entry (u32 bucket_index, va_lookup_key * );
70 inline va_entry_t * va_db_lookup (u32 bucket_index, va_lookup_key * key);
71
72 #ifdef FRAG_DEBUG
73
74 #define FRAG_DEBUG_PRINTF1(a)                                \
75      if (frag_debug_flag) {                                  \
76          PLATFORM_DEBUG_PRINT(a);                                          \
77      }
78
79 #define FRAG_DEBUG_PRINTF2(a, b)                             \
80      if (frag_debug_flag) {                                  \
81          PLATFORM_DEBUG_PRINT(a, b);                                       \
82      }
83
84 #define FRAG_DEBUG_PRINTF3(a, b, c)                          \
85      if (frag_debug_flag) {                                  \
86          PLATFORM_DEBUG_PRINT(a, b, c);                                    \
87      }
88
89 #define FRAG_DEBUG_PRINTF4(a, b, c, d)                       \
90     if (frag_debug_flag) {                                   \
91          PLATFORM_DEBUG_PRINT(a, b, c, d);                                 \
92     }
93
94 #define FRAG_DEBUG_PRINTF5(a, b, c, d, e)                    \
95     if (frag_debug_flag) {                                   \
96          PLATFORM_DEBUG_PRINT(a, b, c, d, e);                              \
97     }
98
99 #define FRAG_DEBUG_PRINTF6(a, b, c, d, e, f)                 \
100     if (frag_debug_flag) {                                   \
101          PLATFORM_DEBUG_PRINT(a, b, c, d, e, f);                           \
102     }
103 #else
104
105 #define FRAG_DEBUG_PRINTF1(a)
106
107 #define FRAG_DEBUG_PRINTF2(a, b)
108
109 #define FRAG_DEBUG_PRINTF3(a, b, c)
110
111 #define FRAG_DEBUG_PRINTF4(a, b, c, d)
112
113 #define FRAG_DEBUG_PRINTF5(a, b, c, d, e)
114
115 #define FRAG_DEBUG_PRINTF6(a, b, c, d, e, f)
116
117 #endif
118
119 #endif  /* __CNAT_VA_DB_H__ */
120
121