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) >=
29 sm->max_translations_per_thread)
34 static_always_inline u8
35 nat44_ed_maximum_sessions_exceeded (snat_main_t * sm,
36 u32 fib_index, u32 thread_index)
39 translations = pool_elts (sm->per_thread_data[thread_index].sessions);
40 if (vec_len (sm->max_translations_per_fib) <= fib_index)
42 return translations >= sm->max_translations_per_fib[fib_index];
45 static_always_inline snat_session_t *
46 nat44_session_reuse_old (snat_main_t * sm, snat_user_t * u,
47 snat_session_t * s, u32 thread_index, f64 now)
49 nat44_free_session_data (sm, s, thread_index, 0);
50 if (snat_is_session_static (s))
58 s->ext_host_addr.as_u32 = 0;
60 s->ext_host_nat_addr.as_u32 = 0;
61 s->ext_host_nat_port = 0;
62 s->tcp_closed_timestamp = 0;
63 s->ha_last_refreshed = now;
67 static_always_inline void
68 nat44_user_del_sessions (snat_user_t * u, u32 thread_index)
73 snat_main_t *sm = &snat_main;
74 snat_main_per_thread_data_t *tsm = &sm->per_thread_data[thread_index];
77 elt = pool_elt_at_index (tsm->list_pool,
78 u->sessions_per_user_list_head_index);
80 elt = pool_elt_at_index (tsm->list_pool, elt->next);
82 while (elt->value != ~0)
84 s = pool_elt_at_index (tsm->sessions, elt->value);
85 elt = pool_elt_at_index (tsm->list_pool, elt->next);
87 nat44_free_session_data (sm, s, thread_index, 0);
88 nat44_delete_session (sm, s, thread_index);
92 static_always_inline int
93 nat44_user_del (ip4_address_t * addr, u32 fib_index)
97 snat_main_t *sm = &snat_main;
98 snat_main_per_thread_data_t *tsm;
100 snat_user_key_t user_key;
101 clib_bihash_kv_8_8_t kv, value;
103 if (sm->endpoint_dependent)
106 user_key.addr.as_u32 = addr->as_u32;
107 user_key.fib_index = fib_index;
108 kv.key = user_key.as_u64;
110 if (sm->num_workers > 1)
113 vec_foreach (tsm, sm->per_thread_data)
115 if (!clib_bihash_search_8_8 (&tsm->user_hash, &kv, &value))
117 nat44_user_del_sessions (
118 pool_elt_at_index (tsm->users, value.value),
128 tsm = vec_elt_at_index (sm->per_thread_data, sm->num_workers);
129 if (!clib_bihash_search_8_8 (&tsm->user_hash, &kv, &value))
131 nat44_user_del_sessions (pool_elt_at_index
132 (tsm->users, value.value),
140 #endif /* included_nat44_inlines_h__ */
143 * fd.io coding-style-patch-verification: ON
146 * eval: (c-set-style "gnu")