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 snat_session_t *
35 nat44_session_reuse_old (snat_main_t * sm, snat_user_t * u,
36 snat_session_t * s, u32 thread_index, f64 now)
38 nat44_free_session_data (sm, s, thread_index, 0);
39 if (snat_is_session_static (s))
47 s->ext_host_addr.as_u32 = 0;
49 s->ext_host_nat_addr.as_u32 = 0;
50 s->ext_host_nat_port = 0;
51 s->tcp_close_timestamp = 0;
52 s->ha_last_refreshed = now;
56 static_always_inline void
57 nat44_global_lru_insert (snat_main_per_thread_data_t * tsm,
58 snat_session_t * s, f64 now)
60 dlist_elt_t *lru_list_elt;
61 pool_get (tsm->global_lru_pool, lru_list_elt);
62 s->global_lru_index = lru_list_elt - tsm->global_lru_pool;
63 clib_dlist_addtail (tsm->global_lru_pool, tsm->global_lru_head_index,
65 lru_list_elt->value = s - tsm->sessions;
66 s->last_lru_update = now;
69 static_always_inline snat_session_t *
70 nat44_session_alloc_new (snat_main_per_thread_data_t * tsm, snat_user_t * u,
74 dlist_elt_t *per_user_translation_list_elt;
76 pool_get (tsm->sessions, s);
77 clib_memset (s, 0, sizeof (*s));
78 /* Create list elts */
79 pool_get (tsm->list_pool, per_user_translation_list_elt);
80 clib_dlist_init (tsm->list_pool,
81 per_user_translation_list_elt - tsm->list_pool);
83 per_user_translation_list_elt->value = s - tsm->sessions;
84 s->per_user_index = per_user_translation_list_elt - tsm->list_pool;
85 s->per_user_list_head_index = u->sessions_per_user_list_head_index;
87 clib_dlist_addtail (tsm->list_pool,
88 s->per_user_list_head_index,
89 per_user_translation_list_elt - tsm->list_pool);
91 nat44_global_lru_insert (tsm, s, now);
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 nat44_delete_session (sm, s, thread_index);
121 static_always_inline int
122 nat44_user_del (ip4_address_t * addr, u32 fib_index)
126 snat_main_t *sm = &snat_main;
127 snat_main_per_thread_data_t *tsm;
129 snat_user_key_t user_key;
130 clib_bihash_kv_8_8_t kv, value;
132 user_key.addr.as_u32 = addr->as_u32;
133 user_key.fib_index = fib_index;
134 kv.key = user_key.as_u64;
136 if (sm->num_workers > 1)
139 vec_foreach (tsm, sm->per_thread_data)
141 if (!clib_bihash_search_8_8 (&tsm->user_hash, &kv, &value))
143 nat44_user_del_sessions (
144 pool_elt_at_index (tsm->users, value.value),
154 tsm = vec_elt_at_index (sm->per_thread_data, sm->num_workers);
155 if (!clib_bihash_search_8_8 (&tsm->user_hash, &kv, &value))
157 nat44_user_del_sessions (pool_elt_at_index
158 (tsm->users, value.value),
166 #endif /* included_nat44_inlines_h__ */
169 * fd.io coding-style-patch-verification: ON
172 * eval: (c-set-style "gnu")