2 * Copyright (c) 2019 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:
7 * http://www.apache.org/licenses/LICENSE-2.0
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.
16 * @brief The NAT44 inline functions
18 #ifndef __included_nat44_inlines_h__
19 #define __included_nat44_inlines_h__
21 #include <vnet/fib/ip4_fib.h>
24 static_always_inline void
25 nat44_session_cleanup (snat_session_t * s, u32 thread_index)
27 snat_main_t *sm = &snat_main;
29 nat_free_session_data (sm, s, thread_index, 0);
30 nat44_delete_session (sm, s, thread_index);
33 static_always_inline void
34 nat44_user_try_cleanup (snat_user_t * u, u32 thread_index, f64 now)
38 u64 sess_timeout_time;
40 snat_main_t *sm = &snat_main;
41 snat_main_per_thread_data_t *tsm = &sm->per_thread_data[thread_index];
44 elt = pool_elt_at_index (tsm->list_pool,
45 u->sessions_per_user_list_head_index);
47 elt = pool_elt_at_index (tsm->list_pool, elt->next);
49 while (elt->value != ~0)
51 s = pool_elt_at_index (tsm->sessions, elt->value);
52 elt = pool_elt_at_index (tsm->list_pool, elt->next);
54 sess_timeout_time = s->last_heard +
55 (f64) nat44_session_get_timeout (sm, s);
57 if (now < sess_timeout_time)
60 nat44_session_cleanup (s, thread_index);
64 static_always_inline void
65 nat44_session_try_cleanup (ip4_address_t * addr,
66 u32 fib_index, u32 thread_index, f64 now)
69 snat_user_key_t user_key;
70 clib_bihash_kv_8_8_t kv, value;
72 snat_main_t *sm = &snat_main;
73 snat_main_per_thread_data_t *tsm = &sm->per_thread_data[thread_index];
75 user_key.addr.as_u32 = addr->as_u32;
76 user_key.fib_index = fib_index;
77 kv.key = user_key.as_u64;
79 // lookup user for this traffic
80 if (PREDICT_FALSE (clib_bihash_search_8_8 (&tsm->user_hash, &kv, &value)))
82 // there is still place and a new user can be created
83 if (PREDICT_TRUE (pool_elts (tsm->sessions) < sm->max_translations))
86 // there is no place so we try to cleanup all users in this thread
88 pool_foreach (u, tsm->users,
90 nat44_user_try_cleanup (u, thread_index, now);
96 // each time user creates a new session we try to cleanup expired sessions
97 nat44_user_try_cleanup (pool_elt_at_index (tsm->users, value.value),
101 #endif /* __included_nat44_inlines_h__ */
104 * fd.io coding-style-patch-verification: ON
107 * eval: (c-set-style "gnu")