nat: api & cli command for forcing session cleanup
[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         continue;
69
70       nat44_session_cleanup (s, thread_index);
71     }
72 }
73
74 static_always_inline void
75 nat44_session_try_cleanup (ip4_address_t * addr,
76                            u32 fib_index, u32 thread_index, f64 now)
77 {
78   snat_user_t *u = 0;
79   snat_user_key_t user_key;
80   clib_bihash_kv_8_8_t kv, value;
81
82   snat_main_t *sm = &snat_main;
83   snat_main_per_thread_data_t *tsm = &sm->per_thread_data[thread_index];
84
85   user_key.addr.as_u32 = addr->as_u32;
86   user_key.fib_index = fib_index;
87   kv.key = user_key.as_u64;
88
89   // lookup user for this traffic
90   if (PREDICT_FALSE (clib_bihash_search_8_8 (&tsm->user_hash, &kv, &value)))
91     {
92       // there is still place and a new user can be created
93       if (PREDICT_TRUE (pool_elts (tsm->sessions) < sm->max_translations))
94         return;
95
96       // there is no place so we try to cleanup all users in this thread
97       /* *INDENT-OFF* */
98       pool_foreach (u, tsm->users,
99       ({
100         nat44_user_try_cleanup (u, thread_index, now);
101       }));
102       /* *INDENT-ON* */
103       return;
104     }
105
106   // each time user creates a new session we try to cleanup expired sessions
107   nat44_user_try_cleanup (pool_elt_at_index (tsm->users, value.value),
108                           thread_index, now);
109 }
110
111 static_always_inline void
112 nat44_force_session_cleanup (void)
113 {
114   snat_user_t *u = 0;
115
116   snat_main_t *sm = &snat_main;
117   snat_main_per_thread_data_t *tsm;
118
119   vlib_main_t *vm = vlib_get_main ();
120   f64 now = vlib_time_now (vm);
121
122   // TODO: consider own timeouts
123
124   if (sm->num_workers > 1)
125     {
126       /* *INDENT-OFF* */
127       vec_foreach (tsm, sm->per_thread_data)
128         {
129           pool_foreach (u, tsm->users,
130           ({
131             nat44_user_try_cleanup (u, tsm->thread_index, now);
132           }));
133         }
134       /* *INDENT-ON* */
135     }
136   else
137     {
138       tsm = vec_elt_at_index (sm->per_thread_data, sm->num_workers);
139       /* *INDENT-OFF* */
140       pool_foreach (u, tsm->users,
141       ({
142         nat44_user_try_cleanup (u, tsm->thread_index, now);
143       }));
144       /* *INDENT-ON* */
145     }
146 }
147
148 #endif /* included_nat44_inlines_h__ */
149
150 /*
151  * fd.io coding-style-patch-verification: ON
152  *
153  * Local Variables:
154  * eval: (c-set-style "gnu")
155  * End:
156  */