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_del_sessions (snat_user_t * u, u32 thread_index)
49 snat_main_t *sm = &snat_main;
50 snat_main_per_thread_data_t *tsm = &sm->per_thread_data[thread_index];
53 elt = pool_elt_at_index (tsm->list_pool,
54 u->sessions_per_user_list_head_index);
56 elt = pool_elt_at_index (tsm->list_pool, elt->next);
58 while (elt->value != ~0)
60 s = pool_elt_at_index (tsm->sessions, elt->value);
61 elt = pool_elt_at_index (tsm->list_pool, elt->next);
62 nat44_session_cleanup (s, thread_index);
66 static_always_inline int
67 nat44_user_del (ip4_address_t * addr, u32 fib_index)
71 snat_main_t *sm = &snat_main;
72 snat_main_per_thread_data_t *tsm;
74 snat_user_key_t user_key;
75 clib_bihash_kv_8_8_t kv, value;
77 user_key.addr.as_u32 = addr->as_u32;
78 user_key.fib_index = fib_index;
79 kv.key = user_key.as_u64;
81 if (sm->num_workers > 1)
84 vec_foreach (tsm, sm->per_thread_data)
86 if (!clib_bihash_search_8_8 (&tsm->user_hash, &kv, &value))
88 nat44_user_del_sessions (
89 pool_elt_at_index (tsm->users, value.value),
99 tsm = vec_elt_at_index (sm->per_thread_data, sm->num_workers);
100 if (!clib_bihash_search_8_8 (&tsm->user_hash, &kv, &value))
102 nat44_user_del_sessions (pool_elt_at_index
103 (tsm->users, value.value),
111 static_always_inline void
112 nat44_user_try_cleanup (snat_user_t * u, u32 thread_index, f64 now)
116 u64 sess_timeout_time;
118 snat_main_t *sm = &snat_main;
119 snat_main_per_thread_data_t *tsm = &sm->per_thread_data[thread_index];
122 elt = pool_elt_at_index (tsm->list_pool,
123 u->sessions_per_user_list_head_index);
125 elt = pool_elt_at_index (tsm->list_pool, elt->next);
127 while (elt->value != ~0)
129 s = pool_elt_at_index (tsm->sessions, elt->value);
130 elt = pool_elt_at_index (tsm->list_pool, elt->next);
132 sess_timeout_time = s->last_heard +
133 (f64) nat44_session_get_timeout (sm, s);
135 if (now < sess_timeout_time)
137 tsm->min_session_timeout =
138 clib_min (sess_timeout_time, tsm->min_session_timeout);
142 nat44_session_cleanup (s, thread_index);
146 static_always_inline void
147 nat44_session_try_cleanup (ip4_address_t * addr,
148 u32 fib_index, u32 thread_index, f64 now)
151 snat_user_key_t user_key;
152 clib_bihash_kv_8_8_t kv, value;
154 snat_main_t *sm = &snat_main;
155 snat_main_per_thread_data_t *tsm = &sm->per_thread_data[thread_index];
157 user_key.addr.as_u32 = addr->as_u32;
158 user_key.fib_index = fib_index;
159 kv.key = user_key.as_u64;
161 // lookup user for this traffic
162 if (PREDICT_FALSE (clib_bihash_search_8_8 (&tsm->user_hash, &kv, &value)))
164 // there is still place and a new user can be created
165 if (PREDICT_TRUE (pool_elts (tsm->sessions) < sm->max_translations))
168 if (now >= tsm->min_session_timeout)
170 tsm->min_session_timeout = ~0;
171 // there is no place so we try to cleanup all users in this thread
173 pool_foreach (u, tsm->users,
174 ({ nat44_user_try_cleanup (u, thread_index, now); }));
176 if (~0 == tsm->min_session_timeout)
178 tsm->min_session_timeout = 0;
184 if (now >= tsm->min_session_timeout)
186 tsm->min_session_timeout = ~0;
187 // each time user creates a new session we try to cleanup expired sessions
188 nat44_user_try_cleanup (pool_elt_at_index (tsm->users, value.value),
190 if (~0 == tsm->min_session_timeout)
192 tsm->min_session_timeout = 0;
197 static_always_inline void
198 nat44_force_session_cleanup (void)
202 snat_main_t *sm = &snat_main;
203 snat_main_per_thread_data_t *tsm;
205 vlib_main_t *vm = vlib_get_main ();
206 f64 now = vlib_time_now (vm);
208 // TODO: consider own timeouts
210 if (sm->num_workers > 1)
213 vec_foreach (tsm, sm->per_thread_data)
215 pool_foreach (u, tsm->users,
217 nat44_user_try_cleanup (u, tsm->thread_index, now);
224 tsm = vec_elt_at_index (sm->per_thread_data, sm->num_workers);
226 pool_foreach (u, tsm->users,
228 nat44_user_try_cleanup (u, tsm->thread_index, now);
234 #endif /* included_nat44_inlines_h__ */
237 * fd.io coding-style-patch-verification: ON
240 * eval: (c-set-style "gnu")