nat: per vrf session limits
[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 u8
35 nat44_ed_maximum_sessions_exceeded (snat_main_t * sm,
36                                     u32 fib_index, u32 thread_index)
37 {
38   u32 translations;
39   translations = pool_elts (sm->per_thread_data[thread_index].sessions);
40   if (vec_len (sm->max_translations_per_fib) <= fib_index)
41     fib_index = 0;
42   return translations >= sm->max_translations_per_fib[fib_index];
43 }
44
45 static_always_inline snat_session_t *
46 nat44_session_reuse_old (snat_main_t * sm, snat_user_t * u,
47                          snat_session_t * s, u32 thread_index, f64 now)
48 {
49   nat44_free_session_data (sm, s, thread_index, 0);
50   if (snat_is_session_static (s))
51     u->nstaticsessions--;
52   else
53     u->nsessions--;
54   s->flags = 0;
55   s->total_bytes = 0;
56   s->total_pkts = 0;
57   s->state = 0;
58   s->ext_host_addr.as_u32 = 0;
59   s->ext_host_port = 0;
60   s->ext_host_nat_addr.as_u32 = 0;
61   s->ext_host_nat_port = 0;
62   s->tcp_close_timestamp = 0;
63   s->ha_last_refreshed = now;
64   return s;
65 }
66
67 static_always_inline void
68 nat44_global_lru_insert (snat_main_per_thread_data_t * tsm,
69                          snat_session_t * s, f64 now)
70 {
71   dlist_elt_t *lru_list_elt;
72   pool_get (tsm->global_lru_pool, lru_list_elt);
73   s->global_lru_index = lru_list_elt - tsm->global_lru_pool;
74   clib_dlist_addtail (tsm->global_lru_pool, tsm->global_lru_head_index,
75                       s->global_lru_index);
76   lru_list_elt->value = s - tsm->sessions;
77   s->last_lru_update = now;
78 }
79
80 static_always_inline snat_session_t *
81 nat44_session_alloc_new (snat_main_per_thread_data_t * tsm, snat_user_t * u,
82                          f64 now)
83 {
84   snat_session_t *s;
85   dlist_elt_t *per_user_translation_list_elt;
86
87   pool_get (tsm->sessions, s);
88   clib_memset (s, 0, sizeof (*s));
89   /* Create list elts */
90   pool_get (tsm->list_pool, per_user_translation_list_elt);
91   clib_dlist_init (tsm->list_pool,
92                    per_user_translation_list_elt - tsm->list_pool);
93
94   per_user_translation_list_elt->value = s - tsm->sessions;
95   s->per_user_index = per_user_translation_list_elt - tsm->list_pool;
96   s->per_user_list_head_index = u->sessions_per_user_list_head_index;
97
98   clib_dlist_addtail (tsm->list_pool,
99                       s->per_user_list_head_index,
100                       per_user_translation_list_elt - tsm->list_pool);
101
102   nat44_global_lru_insert (tsm, s, now);
103   s->ha_last_refreshed = now;
104   return s;
105 }
106
107 static_always_inline void
108 nat44_user_del_sessions (snat_user_t * u, u32 thread_index)
109 {
110   dlist_elt_t *elt;
111   snat_session_t *s;
112
113   snat_main_t *sm = &snat_main;
114   snat_main_per_thread_data_t *tsm = &sm->per_thread_data[thread_index];
115
116   // get head
117   elt = pool_elt_at_index (tsm->list_pool,
118                            u->sessions_per_user_list_head_index);
119   // get first element
120   elt = pool_elt_at_index (tsm->list_pool, elt->next);
121
122   while (elt->value != ~0)
123     {
124       s = pool_elt_at_index (tsm->sessions, elt->value);
125       elt = pool_elt_at_index (tsm->list_pool, elt->next);
126
127       nat44_free_session_data (sm, s, thread_index, 0);
128       nat44_delete_session (sm, s, thread_index);
129     }
130 }
131
132 static_always_inline int
133 nat44_user_del (ip4_address_t * addr, u32 fib_index)
134 {
135   int rv = 1;
136
137   snat_main_t *sm = &snat_main;
138   snat_main_per_thread_data_t *tsm;
139
140   snat_user_key_t user_key;
141   clib_bihash_kv_8_8_t kv, value;
142
143   user_key.addr.as_u32 = addr->as_u32;
144   user_key.fib_index = fib_index;
145   kv.key = user_key.as_u64;
146
147   if (sm->num_workers > 1)
148     {
149       /* *INDENT-OFF* */
150       vec_foreach (tsm, sm->per_thread_data)
151         {
152           if (!clib_bihash_search_8_8 (&tsm->user_hash, &kv, &value))
153             {
154               nat44_user_del_sessions (
155                   pool_elt_at_index (tsm->users, value.value),
156                   tsm->thread_index);
157               rv = 0;
158               break;
159             }
160         }
161       /* *INDENT-ON* */
162     }
163   else
164     {
165       tsm = vec_elt_at_index (sm->per_thread_data, sm->num_workers);
166       if (!clib_bihash_search_8_8 (&tsm->user_hash, &kv, &value))
167         {
168           nat44_user_del_sessions (pool_elt_at_index
169                                    (tsm->users, value.value),
170                                    tsm->thread_index);
171           rv = 0;
172         }
173     }
174   return rv;
175 }
176
177 #endif /* included_nat44_inlines_h__ */
178
179 /*
180  * fd.io coding-style-patch-verification: ON
181  *
182  * Local Variables:
183  * eval: (c-set-style "gnu")
184  * End:
185  */