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 f64
35 nat44_minimal_timeout (snat_main_t * sm)
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);
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)
51 nat44_free_session_data (sm, s, thread_index, 0);
52 if (snat_is_session_static (s))
60 s->ext_host_addr.as_u32 = 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;
70 static_always_inline snat_session_t *
71 nat44_session_alloc_new (snat_main_per_thread_data_t * tsm, snat_user_t * u,
75 dlist_elt_t *per_user_translation_list_elt;
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);
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;
88 clib_dlist_addtail (tsm->list_pool,
89 s->per_user_list_head_index,
90 per_user_translation_list_elt - tsm->list_pool);
92 dlist_elt_t *lru_list_elt;
93 pool_get (tsm->global_lru_pool, lru_list_elt);
94 s->global_lru_index = lru_list_elt - tsm->global_lru_pool;
95 clib_dlist_addtail (tsm->global_lru_pool, tsm->global_lru_head_index,
97 lru_list_elt->value = s - tsm->sessions;
98 s->last_lru_update = now;
100 s->ha_last_refreshed = now;
104 static_always_inline void
105 nat44_user_del_sessions (snat_user_t * u, u32 thread_index)
110 snat_main_t *sm = &snat_main;
111 snat_main_per_thread_data_t *tsm = &sm->per_thread_data[thread_index];
114 elt = pool_elt_at_index (tsm->list_pool,
115 u->sessions_per_user_list_head_index);
117 elt = pool_elt_at_index (tsm->list_pool, elt->next);
119 while (elt->value != ~0)
121 s = pool_elt_at_index (tsm->sessions, elt->value);
122 elt = pool_elt_at_index (tsm->list_pool, elt->next);
124 nat44_free_session_data (sm, s, thread_index, 0);
125 // TODO: needs refactoring as in nat44_user_session_cleanup
126 nat44_delete_session (sm, s, thread_index);
130 static_always_inline int
131 nat44_user_del (ip4_address_t * addr, u32 fib_index)
135 snat_main_t *sm = &snat_main;
136 snat_main_per_thread_data_t *tsm;
138 snat_user_key_t user_key;
139 clib_bihash_kv_8_8_t kv, value;
141 user_key.addr.as_u32 = addr->as_u32;
142 user_key.fib_index = fib_index;
143 kv.key = user_key.as_u64;
145 if (sm->num_workers > 1)
148 vec_foreach (tsm, sm->per_thread_data)
150 if (!clib_bihash_search_8_8 (&tsm->user_hash, &kv, &value))
152 nat44_user_del_sessions (
153 pool_elt_at_index (tsm->users, value.value),
163 tsm = vec_elt_at_index (sm->per_thread_data, sm->num_workers);
164 if (!clib_bihash_search_8_8 (&tsm->user_hash, &kv, &value))
166 nat44_user_del_sessions (pool_elt_at_index
167 (tsm->users, value.value),
175 static_always_inline u32
176 nat44_user_session_cleanup (snat_user_t * u, u32 thread_index, f64 now)
181 u64 sess_timeout_time;
183 snat_main_t *sm = &snat_main;
184 snat_main_per_thread_data_t *tsm = &sm->per_thread_data[thread_index];
186 if (now < u->min_session_timeout)
188 u->min_session_timeout = now + sm->min_timeout;
191 elt = pool_elt_at_index (tsm->list_pool,
192 u->sessions_per_user_list_head_index);
194 elt = pool_elt_at_index (tsm->list_pool, elt->next);
196 while (elt->value != ~0)
198 s = pool_elt_at_index (tsm->sessions, elt->value);
199 elt = pool_elt_at_index (tsm->list_pool, elt->next);
201 sess_timeout_time = s->last_heard +
202 (f64) nat44_session_get_timeout (sm, s);
204 if (s->tcp_close_timestamp)
207 clib_min (sess_timeout_time, s->tcp_close_timestamp);
210 if (now < sess_timeout_time)
213 // do cleanup of this call (refactor for ED NAT44 only)
214 nat44_free_session_data (sm, s, thread_index, 0);
216 clib_dlist_remove (tsm->list_pool, s->per_user_index);
217 pool_put_index (tsm->list_pool, s->per_user_index);
218 clib_dlist_remove (tsm->global_lru_pool, s->global_lru_index);
219 pool_put_index (tsm->global_lru_pool, s->global_lru_index);
220 pool_put (tsm->sessions, s);
221 vlib_set_simple_counter (&sm->total_sessions, thread_index, 0,
222 pool_elts (tsm->sessions));
224 if (snat_is_session_static (s))
225 u->nstaticsessions--;
235 static_always_inline u32
236 nat44_users_cleanup (u32 thread_index, f64 now)
238 snat_main_t *sm = &snat_main;
239 snat_main_per_thread_data_t *tsm;
243 snat_user_key_t u_key;
244 clib_bihash_kv_8_8_t kv;
249 tsm = vec_elt_at_index (sm->per_thread_data, thread_index);
251 if (now < tsm->min_session_timeout)
253 tsm->min_session_timeout = now + sm->min_timeout;
255 tsm->cleanup_timeout = tsm->min_session_timeout;
260 pool_index = pool_next_index (tsm->users, pool_index);
261 if (pool_index == ~0)
263 u = pool_elt_at_index (tsm->users, pool_index);
265 cleared += nat44_user_session_cleanup (u, thread_index, now);
267 if (u->nstaticsessions == 0 && u->nsessions == 0)
269 u_key.addr.as_u32 = u->addr.as_u32;
270 u_key.fib_index = u->fib_index;
271 kv.key = u_key.as_u64;
274 pool_put_index (tsm->list_pool,
275 u->sessions_per_user_list_head_index);
276 pool_put (tsm->users, u);
277 clib_bihash_add_del_8_8 (&tsm->user_hash, &kv, 0);
279 // update total users counter
280 vlib_set_simple_counter (&sm->total_users, thread_index, 0,
281 pool_elts (tsm->users));
285 tsm->cleared += cleared;
291 static_always_inline u32
292 nat44_out_of_ports_cleanup (u32 thread_index, f64 now)
294 return nat44_users_cleanup (thread_index, now);
297 static_always_inline u32
298 nat44_max_translations_per_user_cleanup (snat_user_t * u, u32 thread_index,
301 return nat44_user_session_cleanup (u, thread_index, now);
304 static_always_inline u32
305 nat44_force_users_cleanup (void)
307 snat_main_t *sm = &snat_main;
308 snat_main_per_thread_data_t *tsm;
310 f64 now = vlib_time_now (vlib_get_main ());
313 if (sm->num_workers > 1)
316 vec_foreach (tsm, sm->per_thread_data)
318 cleared += nat44_users_cleanup (tsm->thread_index, now);
324 tsm = vec_elt_at_index (sm->per_thread_data, sm->num_workers);
325 cleared += nat44_users_cleanup (tsm->thread_index, now);
330 #endif /* included_nat44_inlines_h__ */
333 * fd.io coding-style-patch-verification: ON
336 * eval: (c-set-style "gnu")