sr: some fixes for SRv6 CLI/API
[vpp.git] / src / plugins / nat / nat44_inlines.h
1 /*
2  * Copyright (c) 2019 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 #ifndef __included_nat44_inlines_h__
19 #define __included_nat44_inlines_h__
20
21 #include <vnet/fib/ip4_fib.h>
22 #include <nat/nat.h>
23
24 static_always_inline void
25 nat44_session_cleanup (snat_session_t * s, u32 thread_index)
26 {
27   snat_main_t *sm = &snat_main;
28
29   nat_free_session_data (sm, s, thread_index, 0);
30   nat44_delete_session (sm, s, thread_index);
31 }
32
33 static_always_inline void
34 nat44_user_try_cleanup (snat_user_t * u, u32 thread_index, f64 now)
35 {
36   dlist_elt_t *elt;
37   snat_session_t *s;
38   u64 sess_timeout_time;
39
40   snat_main_t *sm = &snat_main;
41   snat_main_per_thread_data_t *tsm = &sm->per_thread_data[thread_index];
42
43   // get head
44   elt = pool_elt_at_index (tsm->list_pool,
45                            u->sessions_per_user_list_head_index);
46   // get first element
47   elt = pool_elt_at_index (tsm->list_pool, elt->next);
48
49   while (elt->value != ~0)
50     {
51       s = pool_elt_at_index (tsm->sessions, elt->value);
52       elt = pool_elt_at_index (tsm->list_pool, elt->next);
53
54       sess_timeout_time = s->last_heard +
55         (f64) nat44_session_get_timeout (sm, s);
56
57       if (now < sess_timeout_time)
58         continue;
59
60       nat44_session_cleanup (s, thread_index);
61     }
62 }
63
64 static_always_inline void
65 nat44_session_try_cleanup (ip4_address_t * addr,
66                            u32 fib_index, u32 thread_index, f64 now)
67 {
68   snat_user_t *u = 0;
69   snat_user_key_t user_key;
70   clib_bihash_kv_8_8_t kv, value;
71
72   snat_main_t *sm = &snat_main;
73   snat_main_per_thread_data_t *tsm = &sm->per_thread_data[thread_index];
74
75   user_key.addr.as_u32 = addr->as_u32;
76   user_key.fib_index = fib_index;
77   kv.key = user_key.as_u64;
78
79   // lookup user for this traffic
80   if (PREDICT_FALSE (clib_bihash_search_8_8 (&tsm->user_hash, &kv, &value)))
81     {
82       // there is still place and a new user can be created
83       if (PREDICT_TRUE (pool_elts (tsm->sessions) < sm->max_translations))
84         return;
85
86       // there is no place so we try to cleanup all users in this thread
87       /* *INDENT-OFF* */
88       pool_foreach (u, tsm->users,
89       ({
90         nat44_user_try_cleanup (u, thread_index, now);
91       }));
92       /* *INDENT-ON* */
93       return;
94     }
95
96   // each time user creates a new session we try to cleanup expired sessions
97   nat44_user_try_cleanup (pool_elt_at_index (tsm->users, value.value),
98                           thread_index, now);
99 }
100
101 #endif /* __included_nat44_inlines_h__ */
102
103 /*
104  * fd.io coding-style-patch-verification: ON
105  *
106  * Local Variables:
107  * eval: (c-set-style "gnu")
108  * End:
109  */