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:
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
19 #ifndef included_nat44_inlines_h__
20 #define included_nat44_inlines_h__
22 #include <vnet/fib/ip4_fib.h>
25 static_always_inline u8
26 nat44_maximum_sessions_exceeded (snat_main_t * sm, u32 thread_index)
28 if (pool_elts (sm->per_thread_data[thread_index].sessions) >=
34 static_always_inline void
35 nat44_session_cleanup (snat_session_t * s, u32 thread_index)
37 snat_main_t *sm = &snat_main;
39 nat_free_session_data (sm, s, thread_index, 0);
40 nat44_delete_session (sm, s, thread_index);
43 static_always_inline void
44 nat44_user_try_cleanup (snat_user_t * u, u32 thread_index, f64 now)
48 u64 sess_timeout_time;
50 snat_main_t *sm = &snat_main;
51 snat_main_per_thread_data_t *tsm = &sm->per_thread_data[thread_index];
54 elt = pool_elt_at_index (tsm->list_pool,
55 u->sessions_per_user_list_head_index);
57 elt = pool_elt_at_index (tsm->list_pool, elt->next);
59 while (elt->value != ~0)
61 s = pool_elt_at_index (tsm->sessions, elt->value);
62 elt = pool_elt_at_index (tsm->list_pool, elt->next);
64 sess_timeout_time = s->last_heard +
65 (f64) nat44_session_get_timeout (sm, s);
67 if (now < sess_timeout_time)
70 nat44_session_cleanup (s, thread_index);
74 static_always_inline void
75 nat44_session_try_cleanup (ip4_address_t * addr,
76 u32 fib_index, u32 thread_index, f64 now)
79 snat_user_key_t user_key;
80 clib_bihash_kv_8_8_t kv, value;
82 snat_main_t *sm = &snat_main;
83 snat_main_per_thread_data_t *tsm = &sm->per_thread_data[thread_index];
85 user_key.addr.as_u32 = addr->as_u32;
86 user_key.fib_index = fib_index;
87 kv.key = user_key.as_u64;
89 // lookup user for this traffic
90 if (PREDICT_FALSE (clib_bihash_search_8_8 (&tsm->user_hash, &kv, &value)))
92 // there is still place and a new user can be created
93 if (PREDICT_TRUE (pool_elts (tsm->sessions) < sm->max_translations))
96 // there is no place so we try to cleanup all users in this thread
98 pool_foreach (u, tsm->users,
100 nat44_user_try_cleanup (u, thread_index, now);
106 // each time user creates a new session we try to cleanup expired sessions
107 nat44_user_try_cleanup (pool_elt_at_index (tsm->users, value.value),
111 #endif /* included_nat44_inlines_h__ */
114 * fd.io coding-style-patch-verification: ON
117 * eval: (c-set-style "gnu")