fcaf57383efce0506d6ae5a1232955294584256e
[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   s->tcp_close_timestamp = 0;
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 (s->tcp_close_timestamp)
197         {
198           sess_timeout_time =
199             clib_min (sess_timeout_time, s->tcp_close_timestamp);
200         }
201
202       if (now < sess_timeout_time)
203         continue;
204
205       // do cleanup of this call (refactor for ED NAT44 only)
206       nat44_free_session_data (sm, s, thread_index, 0);
207
208       clib_dlist_remove (tsm->list_pool, s->per_user_index);
209       pool_put_index (tsm->list_pool, s->per_user_index);
210       pool_put (tsm->sessions, s);
211       vlib_set_simple_counter (&sm->total_sessions, thread_index, 0,
212                                pool_elts (tsm->sessions));
213
214       if (snat_is_session_static (s))
215         u->nstaticsessions--;
216       else
217         u->nsessions--;
218
219       cleared++;
220     }
221 done:
222   return cleared;
223 }
224
225 static_always_inline u32
226 nat44_users_cleanup (u32 thread_index, f64 now)
227 {
228   snat_main_t *sm = &snat_main;
229   snat_main_per_thread_data_t *tsm;
230
231   u32 cleared = 0;
232
233   snat_user_key_t u_key;
234   clib_bihash_kv_8_8_t kv;
235
236   snat_user_t *u = 0;
237   u32 pool_index;
238
239   tsm = vec_elt_at_index (sm->per_thread_data, thread_index);
240
241   if (now < tsm->min_session_timeout)
242     goto done;
243   tsm->min_session_timeout = now + sm->min_timeout;
244   // consider
245   tsm->cleanup_timeout = tsm->min_session_timeout;
246
247   pool_index = ~0;
248   do
249     {
250       pool_index = pool_next_index (tsm->users, pool_index);
251       if (pool_index == ~0)
252         break;
253       u = pool_elt_at_index (tsm->users, pool_index);
254
255       cleared += nat44_user_session_cleanup (u, thread_index, now);
256
257       if (u->nstaticsessions == 0 && u->nsessions == 0)
258         {
259           u_key.addr.as_u32 = u->addr.as_u32;
260           u_key.fib_index = u->fib_index;
261           kv.key = u_key.as_u64;
262
263           // delete user
264           pool_put_index (tsm->list_pool,
265                           u->sessions_per_user_list_head_index);
266           pool_put (tsm->users, u);
267           clib_bihash_add_del_8_8 (&tsm->user_hash, &kv, 0);
268
269           // update total users counter
270           vlib_set_simple_counter (&sm->total_users, thread_index, 0,
271                                    pool_elts (tsm->users));
272         }
273     }
274   while (1);
275   tsm->cleared += cleared;
276   tsm->cleanup_runs++;
277 done:
278   return cleared;
279 }
280
281 static_always_inline u32
282 nat44_out_of_ports_cleanup (u32 thread_index, f64 now)
283 {
284   return nat44_users_cleanup (thread_index, now);
285 }
286
287 static_always_inline u32
288 nat44_max_translations_per_user_cleanup (snat_user_t * u, u32 thread_index,
289                                          f64 now)
290 {
291   return nat44_user_session_cleanup (u, thread_index, now);
292 }
293
294 static_always_inline u32
295 nat44_force_users_cleanup (void)
296 {
297   snat_main_t *sm = &snat_main;
298   snat_main_per_thread_data_t *tsm;
299
300   f64 now = vlib_time_now (vlib_get_main ());
301   u32 cleared = 0;
302
303   if (sm->num_workers > 1)
304     {
305       /* *INDENT-OFF* */
306       vec_foreach (tsm, sm->per_thread_data)
307         {
308           cleared += nat44_users_cleanup (tsm->thread_index, now);
309         }
310       /* *INDENT-ON* */
311     }
312   else
313     {
314       tsm = vec_elt_at_index (sm->per_thread_data, sm->num_workers);
315       cleared += nat44_users_cleanup (tsm->thread_index, now);
316     }
317   return cleared;
318 }
319
320 #endif /* included_nat44_inlines_h__ */
321
322 /*
323  * fd.io coding-style-patch-verification: ON
324  *
325  * Local Variables:
326  * eval: (c-set-style "gnu")
327  * End:
328  */