nat: timed out session scavenging upgrade
[vpp.git] / src / plugins / nat / nat44 / inlines.h
1 /*
2  * Copyright (c) 2020 Cisco and/or its affiliates.
3  * Licensed under the Apache License, Version 2.0 (the "License");
4  * you may not use this file except in compliance with the License.
5  * You may obtain a copy of the License at:
6  *
7  *     http://www.apache.org/licenses/LICENSE-2.0
8  *
9  * Unless required by applicable law or agreed to in writing, software
10  * distributed under the License is distributed on an "AS IS" BASIS,
11  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12  * See the License for the specific language governing permissions and
13  * limitations under the License.
14  */
15 /**
16  * @brief The NAT44 inline functions
17  */
18
19 #ifndef included_nat44_inlines_h__
20 #define included_nat44_inlines_h__
21
22 #include <vnet/fib/ip4_fib.h>
23 #include <nat/nat.h>
24
25 static_always_inline u8
26 nat44_maximum_sessions_exceeded (snat_main_t * sm, u32 thread_index)
27 {
28   if (pool_elts (sm->per_thread_data[thread_index].sessions) >=
29       sm->max_translations)
30     return 1;
31   return 0;
32 }
33
34 static_always_inline f64
35 nat44_minimal_timeout (snat_main_t * sm)
36 {
37   f64 min_timeout;
38
39   min_timeout = clib_min (sm->udp_timeout, sm->icmp_timeout);
40   min_timeout = clib_min (min_timeout, sm->icmp_timeout);
41   min_timeout = clib_min (min_timeout, sm->tcp_transitory_timeout);
42   min_timeout = clib_min (min_timeout, sm->tcp_established_timeout);
43
44   return min_timeout;
45 }
46
47 static_always_inline snat_session_t *
48 nat44_session_reuse_old (snat_main_t * sm, snat_user_t * u,
49                          snat_session_t * s, u32 thread_index, f64 now)
50 {
51   nat44_free_session_data (sm, s, thread_index, 0);
52   if (snat_is_session_static (s))
53     u->nstaticsessions--;
54   else
55     u->nsessions--;
56   s->flags = 0;
57   s->total_bytes = 0;
58   s->total_pkts = 0;
59   s->state = 0;
60   s->ext_host_addr.as_u32 = 0;
61   s->ext_host_port = 0;
62   s->ext_host_nat_addr.as_u32 = 0;
63   s->ext_host_nat_port = 0;
64   //
65   s->ha_last_refreshed = now;
66   return s;
67 }
68
69
70 static_always_inline snat_session_t *
71 nat44_session_alloc_new (snat_main_per_thread_data_t * tsm, snat_user_t * u,
72                          f64 now)
73 {
74   snat_session_t *s;
75   dlist_elt_t *per_user_translation_list_elt;
76
77   pool_get (tsm->sessions, s);
78   clib_memset (s, 0, sizeof (*s));
79   /* Create list elts */
80   pool_get (tsm->list_pool, per_user_translation_list_elt);
81   clib_dlist_init (tsm->list_pool,
82                    per_user_translation_list_elt - tsm->list_pool);
83
84   per_user_translation_list_elt->value = s - tsm->sessions;
85   s->per_user_index = per_user_translation_list_elt - tsm->list_pool;
86   s->per_user_list_head_index = u->sessions_per_user_list_head_index;
87
88   clib_dlist_addtail (tsm->list_pool,
89                       s->per_user_list_head_index,
90                       per_user_translation_list_elt - tsm->list_pool);
91
92   s->ha_last_refreshed = now;
93   return s;
94 }
95
96 static_always_inline void
97 nat44_user_del_sessions (snat_user_t * u, u32 thread_index)
98 {
99   dlist_elt_t *elt;
100   snat_session_t *s;
101
102   snat_main_t *sm = &snat_main;
103   snat_main_per_thread_data_t *tsm = &sm->per_thread_data[thread_index];
104
105   // get head
106   elt = pool_elt_at_index (tsm->list_pool,
107                            u->sessions_per_user_list_head_index);
108   // get first element
109   elt = pool_elt_at_index (tsm->list_pool, elt->next);
110
111   while (elt->value != ~0)
112     {
113       s = pool_elt_at_index (tsm->sessions, elt->value);
114       elt = pool_elt_at_index (tsm->list_pool, elt->next);
115
116       nat44_free_session_data (sm, s, thread_index, 0);
117       // TODO: needs refactoring as in nat44_user_session_cleanup
118       nat44_delete_session (sm, s, thread_index);
119     }
120 }
121
122 static_always_inline int
123 nat44_user_del (ip4_address_t * addr, u32 fib_index)
124 {
125   int rv = 1;
126
127   snat_main_t *sm = &snat_main;
128   snat_main_per_thread_data_t *tsm;
129
130   snat_user_key_t user_key;
131   clib_bihash_kv_8_8_t kv, value;
132
133   user_key.addr.as_u32 = addr->as_u32;
134   user_key.fib_index = fib_index;
135   kv.key = user_key.as_u64;
136
137   if (sm->num_workers > 1)
138     {
139       /* *INDENT-OFF* */
140       vec_foreach (tsm, sm->per_thread_data)
141         {
142           if (!clib_bihash_search_8_8 (&tsm->user_hash, &kv, &value))
143             {
144               nat44_user_del_sessions (
145                   pool_elt_at_index (tsm->users, value.value),
146                   tsm->thread_index);
147               rv = 0;
148               break;
149             }
150         }
151       /* *INDENT-ON* */
152     }
153   else
154     {
155       tsm = vec_elt_at_index (sm->per_thread_data, sm->num_workers);
156       if (!clib_bihash_search_8_8 (&tsm->user_hash, &kv, &value))
157         {
158           nat44_user_del_sessions (pool_elt_at_index
159                                    (tsm->users, value.value),
160                                    tsm->thread_index);
161           rv = 0;
162         }
163     }
164   return rv;
165 }
166
167 static_always_inline u32
168 nat44_user_session_cleanup (snat_user_t * u, u32 thread_index, f64 now)
169 {
170   u32 cleared = 0;
171   dlist_elt_t *elt;
172   snat_session_t *s;
173   u64 sess_timeout_time;
174
175   snat_main_t *sm = &snat_main;
176   snat_main_per_thread_data_t *tsm = &sm->per_thread_data[thread_index];
177
178   if (now < u->min_session_timeout)
179     goto done;
180   u->min_session_timeout = now + sm->min_timeout;
181
182   // get head
183   elt = pool_elt_at_index (tsm->list_pool,
184                            u->sessions_per_user_list_head_index);
185   // get first element
186   elt = pool_elt_at_index (tsm->list_pool, elt->next);
187
188   while (elt->value != ~0)
189     {
190       s = pool_elt_at_index (tsm->sessions, elt->value);
191       elt = pool_elt_at_index (tsm->list_pool, elt->next);
192
193       sess_timeout_time = s->last_heard +
194         (f64) nat44_session_get_timeout (sm, s);
195
196       if (now < sess_timeout_time)
197         continue;
198
199       // do cleanup of this call (refactor for ED NAT44 only)
200       nat44_free_session_data (sm, s, thread_index, 0);
201
202       clib_dlist_remove (tsm->list_pool, s->per_user_index);
203       pool_put_index (tsm->list_pool, s->per_user_index);
204       pool_put (tsm->sessions, s);
205       vlib_set_simple_counter (&sm->total_sessions, thread_index, 0,
206                                pool_elts (tsm->sessions));
207
208       if (snat_is_session_static (s))
209         u->nstaticsessions--;
210       else
211         u->nsessions--;
212
213       cleared++;
214     }
215 done:
216   return cleared;
217 }
218
219 static_always_inline u32
220 nat44_users_cleanup (u32 thread_index, f64 now)
221 {
222   snat_main_t *sm = &snat_main;
223   snat_main_per_thread_data_t *tsm;
224
225   u32 cleared = 0;
226
227   snat_user_key_t u_key;
228   clib_bihash_kv_8_8_t kv;
229
230   snat_user_t *u = 0;
231   u32 pool_index;
232
233   tsm = vec_elt_at_index (sm->per_thread_data, thread_index);
234
235   if (now < tsm->min_session_timeout)
236     goto done;
237   tsm->min_session_timeout = now + sm->min_timeout;
238   // consider
239   tsm->cleanup_timeout = tsm->min_session_timeout;
240
241   pool_index = ~0;
242   do
243     {
244       pool_index = pool_next_index (tsm->users, pool_index);
245       if (pool_index == ~0)
246         break;
247       u = pool_elt_at_index (tsm->users, pool_index);
248
249       cleared += nat44_user_session_cleanup (u, thread_index, now);
250
251       if (u->nstaticsessions == 0 && u->nsessions == 0)
252         {
253           u_key.addr.as_u32 = u->addr.as_u32;
254           u_key.fib_index = u->fib_index;
255           kv.key = u_key.as_u64;
256
257           // delete user
258           pool_put_index (tsm->list_pool,
259                           u->sessions_per_user_list_head_index);
260           pool_put (tsm->users, u);
261           clib_bihash_add_del_8_8 (&tsm->user_hash, &kv, 0);
262
263           // update total users counter
264           vlib_set_simple_counter (&sm->total_users, thread_index, 0,
265                                    pool_elts (tsm->users));
266         }
267     }
268   while (1);
269   tsm->cleared += cleared;
270   tsm->cleanup_runs++;
271 done:
272   return cleared;
273 }
274
275 static_always_inline u32
276 nat44_out_of_ports_cleanup (u32 thread_index, f64 now)
277 {
278   return nat44_users_cleanup (thread_index, now);
279 }
280
281 static_always_inline u32
282 nat44_max_translations_per_user_cleanup (snat_user_t * u, u32 thread_index,
283                                          f64 now)
284 {
285   return nat44_user_session_cleanup (u, thread_index, now);
286 }
287
288 static_always_inline u32
289 nat44_force_users_cleanup (void)
290 {
291   snat_main_t *sm = &snat_main;
292   snat_main_per_thread_data_t *tsm;
293
294   f64 now = vlib_time_now (vlib_get_main ());
295   u32 cleared = 0;
296
297   if (sm->num_workers > 1)
298     {
299       /* *INDENT-OFF* */
300       vec_foreach (tsm, sm->per_thread_data)
301         {
302           cleared += nat44_users_cleanup (tsm->thread_index, now);
303         }
304       /* *INDENT-ON* */
305     }
306   else
307     {
308       tsm = vec_elt_at_index (sm->per_thread_data, sm->num_workers);
309       cleared += nat44_users_cleanup (tsm->thread_index, now);
310     }
311   return cleared;
312 }
313
314 #endif /* included_nat44_inlines_h__ */
315
316 /*
317  * fd.io coding-style-patch-verification: ON
318  *
319  * Local Variables:
320  * eval: (c-set-style "gnu")
321  * End:
322  */