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;
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 s->ha_last_refreshed = now;
96 static_always_inline void
97 nat44_user_del_sessions (snat_user_t * u, u32 thread_index)
102 snat_main_t *sm = &snat_main;
103 snat_main_per_thread_data_t *tsm = &sm->per_thread_data[thread_index];
106 elt = pool_elt_at_index (tsm->list_pool,
107 u->sessions_per_user_list_head_index);
109 elt = pool_elt_at_index (tsm->list_pool, elt->next);
111 while (elt->value != ~0)
113 s = pool_elt_at_index (tsm->sessions, elt->value);
114 elt = pool_elt_at_index (tsm->list_pool, elt->next);
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);
122 static_always_inline int
123 nat44_user_del (ip4_address_t * addr, u32 fib_index)
127 snat_main_t *sm = &snat_main;
128 snat_main_per_thread_data_t *tsm;
130 snat_user_key_t user_key;
131 clib_bihash_kv_8_8_t kv, value;
133 user_key.addr.as_u32 = addr->as_u32;
134 user_key.fib_index = fib_index;
135 kv.key = user_key.as_u64;
137 if (sm->num_workers > 1)
140 vec_foreach (tsm, sm->per_thread_data)
142 if (!clib_bihash_search_8_8 (&tsm->user_hash, &kv, &value))
144 nat44_user_del_sessions (
145 pool_elt_at_index (tsm->users, value.value),
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))
158 nat44_user_del_sessions (pool_elt_at_index
159 (tsm->users, value.value),
167 static_always_inline u32
168 nat44_user_session_cleanup (snat_user_t * u, u32 thread_index, f64 now)
173 u64 sess_timeout_time;
175 snat_main_t *sm = &snat_main;
176 snat_main_per_thread_data_t *tsm = &sm->per_thread_data[thread_index];
178 if (now < u->min_session_timeout)
180 u->min_session_timeout = now + sm->min_timeout;
183 elt = pool_elt_at_index (tsm->list_pool,
184 u->sessions_per_user_list_head_index);
186 elt = pool_elt_at_index (tsm->list_pool, elt->next);
188 while (elt->value != ~0)
190 s = pool_elt_at_index (tsm->sessions, elt->value);
191 elt = pool_elt_at_index (tsm->list_pool, elt->next);
193 sess_timeout_time = s->last_heard +
194 (f64) nat44_session_get_timeout (sm, s);
196 if (now < sess_timeout_time)
199 // do cleanup of this call (refactor for ED NAT44 only)
200 nat44_free_session_data (sm, s, thread_index, 0);
202 clib_dlist_remove (tsm->list_pool, s->per_user_index);
203 pool_put_index (tsm->list_pool, s->per_user_index);
204 pool_put (tsm->sessions, s);
205 vlib_set_simple_counter (&sm->total_sessions, thread_index, 0,
206 pool_elts (tsm->sessions));
208 if (snat_is_session_static (s))
209 u->nstaticsessions--;
219 static_always_inline u32
220 nat44_users_cleanup (u32 thread_index, f64 now)
222 snat_main_t *sm = &snat_main;
223 snat_main_per_thread_data_t *tsm;
227 snat_user_key_t u_key;
228 clib_bihash_kv_8_8_t kv;
233 tsm = vec_elt_at_index (sm->per_thread_data, thread_index);
235 if (now < tsm->min_session_timeout)
237 tsm->min_session_timeout = now + sm->min_timeout;
239 tsm->cleanup_timeout = tsm->min_session_timeout;
244 pool_index = pool_next_index (tsm->users, pool_index);
245 if (pool_index == ~0)
247 u = pool_elt_at_index (tsm->users, pool_index);
249 cleared += nat44_user_session_cleanup (u, thread_index, now);
251 if (u->nstaticsessions == 0 && u->nsessions == 0)
253 u_key.addr.as_u32 = u->addr.as_u32;
254 u_key.fib_index = u->fib_index;
255 kv.key = u_key.as_u64;
258 pool_put_index (tsm->list_pool,
259 u->sessions_per_user_list_head_index);
260 pool_put (tsm->users, u);
261 clib_bihash_add_del_8_8 (&tsm->user_hash, &kv, 0);
263 // update total users counter
264 vlib_set_simple_counter (&sm->total_users, thread_index, 0,
265 pool_elts (tsm->users));
269 tsm->cleared += cleared;
275 static_always_inline u32
276 nat44_out_of_ports_cleanup (u32 thread_index, f64 now)
278 return nat44_users_cleanup (thread_index, now);
281 static_always_inline u32
282 nat44_max_translations_per_user_cleanup (snat_user_t * u, u32 thread_index,
285 return nat44_user_session_cleanup (u, thread_index, now);
288 static_always_inline u32
289 nat44_force_users_cleanup (void)
291 snat_main_t *sm = &snat_main;
292 snat_main_per_thread_data_t *tsm;
294 f64 now = vlib_time_now (vlib_get_main ());
297 if (sm->num_workers > 1)
300 vec_foreach (tsm, sm->per_thread_data)
302 cleared += nat44_users_cleanup (tsm->thread_index, now);
308 tsm = vec_elt_at_index (sm->per_thread_data, sm->num_workers);
309 cleared += nat44_users_cleanup (tsm->thread_index, now);
314 #endif /* included_nat44_inlines_h__ */
317 * fd.io coding-style-patch-verification: ON
320 * eval: (c-set-style "gnu")