octeon: add clear counters for port and queues
[vpp.git] / src / plugins / ct6 / ct6.h
1
2 /*
3  * ct6.h - skeleton vpp engine plug-in header file
4  *
5  * Copyright (c) <current-year> <your-organization>
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 #ifndef __included_ct6_h__
19 #define __included_ct6_h__
20
21 #include <vnet/vnet.h>
22 #include <vnet/ip/ip.h>
23 #include <vnet/ethernet/ethernet.h>
24 #include <vppinfra/bihash_48_8.h>
25
26 #include <vppinfra/hash.h>
27 #include <vppinfra/error.h>
28
29 typedef CLIB_PACKED (struct
30 {
31   union
32   {
33     struct
34     {
35       /* out2in */
36       ip6_address_t src;
37       ip6_address_t dst;
38       u16 sport;
39       u16 dport;
40       u8 proto; /* byte 37 */
41     };
42     u64 as_u64[6];
43   };
44 }) ct6_session_key_t;
45
46 typedef struct
47 {
48   ct6_session_key_t key;
49   u32 thread_index;
50   u32 next_index;
51   u32 prev_index;
52   u32 hits;
53   f64 expires;
54 } ct6_session_t;
55
56 typedef struct
57 {
58   /* API message ID base */
59   u16 msg_id_base;
60
61   /* session lookup table */
62   clib_bihash_48_8_t session_hash;
63   u8 feature_initialized;
64
65   /* per_thread session pools */
66   ct6_session_t **sessions;
67   u32 *first_index;
68   u32 *last_index;
69
70   /* Config parameters */
71   f64 session_timeout_interval;
72   uword session_hash_memory;
73   u32 session_hash_buckets;
74   u32 max_sessions_per_worker;
75
76   /* convenience */
77   vlib_main_t *vlib_main;
78   vnet_main_t *vnet_main;
79   ethernet_main_t *ethernet_main;
80 } ct6_main_t;
81
82 extern ct6_main_t ct6_main;
83
84 extern vlib_node_registration_t ct6_out2in_node;
85 extern vlib_node_registration_t ct6_in2out_node;
86
87 format_function_t format_ct6_session;
88
89 ct6_session_t *ct6_create_or_recycle_session (ct6_main_t * cmp,
90                                               clib_bihash_kv_48_8_t * kvpp,
91                                               f64 now, u32 my_thread_index,
92                                               u32 * recyclep, u32 * createp);
93
94 static inline void
95 ct6_lru_remove (ct6_main_t * cmp, ct6_session_t * s0)
96 {
97   ct6_session_t *next_sess, *prev_sess;
98   u32 thread_index;
99   u32 s0_index;
100
101   thread_index = s0->thread_index;
102
103   s0_index = s0 - cmp->sessions[thread_index];
104
105   /* Deal with list heads */
106   if (s0_index == cmp->first_index[thread_index])
107     cmp->first_index[thread_index] = s0->next_index;
108   if (s0_index == cmp->last_index[thread_index])
109     cmp->last_index[thread_index] = s0->prev_index;
110
111   /* Fix next->prev */
112   if (s0->next_index != ~0)
113     {
114       next_sess = pool_elt_at_index (cmp->sessions[thread_index],
115                                      s0->next_index);
116       next_sess->prev_index = s0->prev_index;
117     }
118   /* Fix prev->next */
119   if (s0->prev_index != ~0)
120     {
121       prev_sess = pool_elt_at_index (cmp->sessions[thread_index],
122                                      s0->prev_index);
123       prev_sess->next_index = s0->next_index;
124     }
125 }
126
127 static inline void
128 ct6_lru_add (ct6_main_t * cmp, ct6_session_t * s0, f64 now)
129 {
130   ct6_session_t *next_sess;
131   u32 thread_index;
132   u32 s0_index;
133
134   s0->hits++;
135   s0->expires = now + cmp->session_timeout_interval;
136   thread_index = s0->thread_index;
137
138   s0_index = s0 - cmp->sessions[thread_index];
139
140   /*
141    * Re-add at the head of the forward LRU list,
142    * tail of the reverse LRU list
143    */
144   if (cmp->first_index[thread_index] != ~0)
145     {
146       next_sess = pool_elt_at_index (cmp->sessions[thread_index],
147                                      cmp->first_index[thread_index]);
148       next_sess->prev_index = s0_index;
149     }
150
151   s0->prev_index = ~0;
152
153   /* s0 now the new head of the LRU forward list */
154   s0->next_index = cmp->first_index[thread_index];
155   cmp->first_index[thread_index] = s0_index;
156
157   /* single session case: also the tail of the reverse LRU list */
158   if (cmp->last_index[thread_index] == ~0)
159     cmp->last_index[thread_index] = s0_index;
160 }
161
162 static inline void
163 ct6_update_session_hit (ct6_main_t * cmp, ct6_session_t * s0, f64 now)
164 {
165   ct6_lru_remove (cmp, s0);
166   ct6_lru_add (cmp, s0, now);
167 }
168
169 #endif /* __included_ct6_h__ */
170
171 /*
172  * fd.io coding-style-patch-verification: ON
173  *
174  * Local Variables:
175  * eval: (c-set-style "gnu")
176  * End:
177  */