nat: avoid running pointless session cleanups
[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 void
35 nat44_session_cleanup (snat_session_t * s, u32 thread_index)
36 {
37   snat_main_t *sm = &snat_main;
38
39   nat_free_session_data (sm, s, thread_index, 0);
40   nat44_delete_session (sm, s, thread_index);
41 }
42
43 static_always_inline void
44 nat44_user_try_cleanup (snat_user_t * u, u32 thread_index, f64 now)
45 {
46   dlist_elt_t *elt;
47   snat_session_t *s;
48   u64 sess_timeout_time;
49
50   snat_main_t *sm = &snat_main;
51   snat_main_per_thread_data_t *tsm = &sm->per_thread_data[thread_index];
52
53   // get head
54   elt = pool_elt_at_index (tsm->list_pool,
55                            u->sessions_per_user_list_head_index);
56   // get first element
57   elt = pool_elt_at_index (tsm->list_pool, elt->next);
58
59   while (elt->value != ~0)
60     {
61       s = pool_elt_at_index (tsm->sessions, elt->value);
62       elt = pool_elt_at_index (tsm->list_pool, elt->next);
63
64       sess_timeout_time = s->last_heard +
65         (f64) nat44_session_get_timeout (sm, s);
66
67       if (now < sess_timeout_time)
68         {
69           tsm->min_session_timeout =
70             clib_min (sess_timeout_time, tsm->min_session_timeout);
71           continue;
72         }
73
74       nat44_session_cleanup (s, thread_index);
75     }
76 }
77
78 static_always_inline void
79 nat44_session_try_cleanup (ip4_address_t * addr,
80                            u32 fib_index, u32 thread_index, f64 now)
81 {
82   snat_user_t *u = 0;
83   snat_user_key_t user_key;
84   clib_bihash_kv_8_8_t kv, value;
85
86   snat_main_t *sm = &snat_main;
87   snat_main_per_thread_data_t *tsm = &sm->per_thread_data[thread_index];
88
89   user_key.addr.as_u32 = addr->as_u32;
90   user_key.fib_index = fib_index;
91   kv.key = user_key.as_u64;
92
93   // lookup user for this traffic
94   if (PREDICT_FALSE (clib_bihash_search_8_8 (&tsm->user_hash, &kv, &value)))
95     {
96       // there is still place and a new user can be created
97       if (PREDICT_TRUE (pool_elts (tsm->sessions) < sm->max_translations))
98         return;
99
100       if (now >= tsm->min_session_timeout)
101         {
102           tsm->min_session_timeout = ~0;
103           // there is no place so we try to cleanup all users in this thread
104           /* *INDENT-OFF* */
105           pool_foreach (u, tsm->users,
106                         ({ nat44_user_try_cleanup (u, thread_index, now); }));
107           /* *INDENT-ON* */
108           if (~0 == tsm->min_session_timeout)
109             {
110               tsm->min_session_timeout = 0;
111             }
112         }
113       return;
114     }
115
116   if (now >= tsm->min_session_timeout)
117     {
118       tsm->min_session_timeout = ~0;
119       // each time user creates a new session we try to cleanup expired sessions
120       nat44_user_try_cleanup (pool_elt_at_index (tsm->users, value.value),
121                               thread_index, now);
122       if (~0 == tsm->min_session_timeout)
123         {
124           tsm->min_session_timeout = 0;
125         }
126     }
127 }
128
129 static_always_inline void
130 nat44_force_session_cleanup (void)
131 {
132   snat_user_t *u = 0;
133
134   snat_main_t *sm = &snat_main;
135   snat_main_per_thread_data_t *tsm;
136
137   vlib_main_t *vm = vlib_get_main ();
138   f64 now = vlib_time_now (vm);
139
140   // TODO: consider own timeouts
141
142   if (sm->num_workers > 1)
143     {
144       /* *INDENT-OFF* */
145       vec_foreach (tsm, sm->per_thread_data)
146         {
147           pool_foreach (u, tsm->users,
148           ({
149             nat44_user_try_cleanup (u, tsm->thread_index, now);
150           }));
151         }
152       /* *INDENT-ON* */
153     }
154   else
155     {
156       tsm = vec_elt_at_index (sm->per_thread_data, sm->num_workers);
157       /* *INDENT-OFF* */
158       pool_foreach (u, tsm->users,
159       ({
160         nat44_user_try_cleanup (u, tsm->thread_index, now);
161       }));
162       /* *INDENT-ON* */
163     }
164 }
165
166 #endif /* included_nat44_inlines_h__ */
167
168 /*
169  * fd.io coding-style-patch-verification: ON
170  *
171  * Local Variables:
172  * eval: (c-set-style "gnu")
173  * End:
174  */