nat: scavenging functionality removed
[vpp.git] / src / plugins / nat / nat44 / inlines.h
1 /*
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:
6  *
7  *     http://www.apache.org/licenses/LICENSE-2.0
8  *
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.
14  */
15 /**
16  * @brief The NAT44 inline functions
17  */
18
19 #ifndef included_nat44_inlines_h__
20 #define included_nat44_inlines_h__
21
22 #include <vnet/fib/ip4_fib.h>
23 #include <nat/nat.h>
24
25 static_always_inline u8
26 nat44_maximum_sessions_exceeded (snat_main_t * sm, u32 thread_index)
27 {
28   if (pool_elts (sm->per_thread_data[thread_index].sessions) >=
29       sm->max_translations)
30     return 1;
31   return 0;
32 }
33
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)
37 {
38   nat44_free_session_data (sm, s, thread_index, 0);
39   if (snat_is_session_static (s))
40     u->nstaticsessions--;
41   else
42     u->nsessions--;
43   s->flags = 0;
44   s->total_bytes = 0;
45   s->total_pkts = 0;
46   s->state = 0;
47   s->ext_host_addr.as_u32 = 0;
48   s->ext_host_port = 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;
53   return s;
54 }
55
56 static_always_inline snat_session_t *
57 nat44_session_alloc_new (snat_main_per_thread_data_t * tsm, snat_user_t * u,
58                          f64 now)
59 {
60   snat_session_t *s;
61   dlist_elt_t *per_user_translation_list_elt;
62
63   pool_get (tsm->sessions, s);
64   clib_memset (s, 0, sizeof (*s));
65   /* Create list elts */
66   pool_get (tsm->list_pool, per_user_translation_list_elt);
67   clib_dlist_init (tsm->list_pool,
68                    per_user_translation_list_elt - tsm->list_pool);
69
70   per_user_translation_list_elt->value = s - tsm->sessions;
71   s->per_user_index = per_user_translation_list_elt - tsm->list_pool;
72   s->per_user_list_head_index = u->sessions_per_user_list_head_index;
73
74   clib_dlist_addtail (tsm->list_pool,
75                       s->per_user_list_head_index,
76                       per_user_translation_list_elt - tsm->list_pool);
77
78   dlist_elt_t *lru_list_elt;
79   pool_get (tsm->global_lru_pool, lru_list_elt);
80   s->global_lru_index = lru_list_elt - tsm->global_lru_pool;
81   clib_dlist_addtail (tsm->global_lru_pool, tsm->global_lru_head_index,
82                       s->global_lru_index);
83   lru_list_elt->value = s - tsm->sessions;
84   s->last_lru_update = now;
85
86   s->ha_last_refreshed = now;
87   return s;
88 }
89
90 static_always_inline void
91 nat44_user_del_sessions (snat_user_t * u, u32 thread_index)
92 {
93   dlist_elt_t *elt;
94   snat_session_t *s;
95
96   snat_main_t *sm = &snat_main;
97   snat_main_per_thread_data_t *tsm = &sm->per_thread_data[thread_index];
98
99   // get head
100   elt = pool_elt_at_index (tsm->list_pool,
101                            u->sessions_per_user_list_head_index);
102   // get first element
103   elt = pool_elt_at_index (tsm->list_pool, elt->next);
104
105   while (elt->value != ~0)
106     {
107       s = pool_elt_at_index (tsm->sessions, elt->value);
108       elt = pool_elt_at_index (tsm->list_pool, elt->next);
109
110       nat44_free_session_data (sm, s, thread_index, 0);
111       nat44_delete_session (sm, s, thread_index);
112     }
113 }
114
115 static_always_inline int
116 nat44_user_del (ip4_address_t * addr, u32 fib_index)
117 {
118   int rv = 1;
119
120   snat_main_t *sm = &snat_main;
121   snat_main_per_thread_data_t *tsm;
122
123   snat_user_key_t user_key;
124   clib_bihash_kv_8_8_t kv, value;
125
126   user_key.addr.as_u32 = addr->as_u32;
127   user_key.fib_index = fib_index;
128   kv.key = user_key.as_u64;
129
130   if (sm->num_workers > 1)
131     {
132       /* *INDENT-OFF* */
133       vec_foreach (tsm, sm->per_thread_data)
134         {
135           if (!clib_bihash_search_8_8 (&tsm->user_hash, &kv, &value))
136             {
137               nat44_user_del_sessions (
138                   pool_elt_at_index (tsm->users, value.value),
139                   tsm->thread_index);
140               rv = 0;
141               break;
142             }
143         }
144       /* *INDENT-ON* */
145     }
146   else
147     {
148       tsm = vec_elt_at_index (sm->per_thread_data, sm->num_workers);
149       if (!clib_bihash_search_8_8 (&tsm->user_hash, &kv, &value))
150         {
151           nat44_user_del_sessions (pool_elt_at_index
152                                    (tsm->users, value.value),
153                                    tsm->thread_index);
154           rv = 0;
155         }
156     }
157   return rv;
158 }
159
160 #endif /* included_nat44_inlines_h__ */
161
162 /*
163  * fd.io coding-style-patch-verification: ON
164  *
165  * Local Variables:
166  * eval: (c-set-style "gnu")
167  * End:
168  */