nat: Final NAT44 EI/ED split patch
[vpp.git] / src / plugins / nat / nat44-ei / nat44_ei_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 #ifndef __included_nat44_ei_inlines_h__
17 #define __included_nat44_ei_inlines_h__
18
19 #include <vppinfra/clib.h>
20
21 #include <nat/nat44-ei/nat44_ei.h>
22 #include <nat/nat44-ei/nat44_ei_ha.h>
23
24 always_inline u64
25 calc_nat_key (ip4_address_t addr, u16 port, u32 fib_index, u8 proto)
26 {
27   ASSERT (fib_index <= (1 << 14) - 1);
28   ASSERT (proto <= (1 << 3) - 1);
29   return (u64) addr.as_u32 << 32 | (u64) port << 16 | fib_index << 3 |
30          (proto & 0x7);
31 }
32
33 always_inline void
34 split_nat_key (u64 key, ip4_address_t *addr, u16 *port, u32 *fib_index,
35                nat_protocol_t *proto)
36 {
37   if (addr)
38     {
39       addr->as_u32 = key >> 32;
40     }
41   if (port)
42     {
43       *port = (key >> 16) & (u16) ~0;
44     }
45   if (fib_index)
46     {
47       *fib_index = key >> 3 & ((1 << 13) - 1);
48     }
49   if (proto)
50     {
51       *proto = key & 0x7;
52     }
53 }
54
55 always_inline void
56 init_nat_k (clib_bihash_kv_8_8_t *kv, ip4_address_t addr, u16 port,
57             u32 fib_index, nat_protocol_t proto)
58 {
59   kv->key = calc_nat_key (addr, port, fib_index, proto);
60   kv->value = ~0ULL;
61 }
62
63 always_inline void
64 init_nat_kv (clib_bihash_kv_8_8_t *kv, ip4_address_t addr, u16 port,
65              u32 fib_index, nat_protocol_t proto, u32 thread_index,
66              u32 session_index)
67 {
68   init_nat_k (kv, addr, port, fib_index, proto);
69   kv->value = (u64) thread_index << 32 | session_index;
70 }
71
72 always_inline void
73 init_nat_i2o_k (clib_bihash_kv_8_8_t *kv, nat44_ei_session_t *s)
74 {
75   return init_nat_k (kv, s->in2out.addr, s->in2out.port, s->in2out.fib_index,
76                      s->nat_proto);
77 }
78
79 always_inline void
80 init_nat_i2o_kv (clib_bihash_kv_8_8_t *kv, nat44_ei_session_t *s,
81                  u32 thread_index, u32 session_index)
82 {
83   init_nat_k (kv, s->in2out.addr, s->in2out.port, s->in2out.fib_index,
84               s->nat_proto);
85   kv->value = (u64) thread_index << 32 | session_index;
86 }
87
88 always_inline void
89 init_nat_o2i_k (clib_bihash_kv_8_8_t *kv, nat44_ei_session_t *s)
90 {
91   return init_nat_k (kv, s->out2in.addr, s->out2in.port, s->out2in.fib_index,
92                      s->nat_proto);
93 }
94
95 always_inline void
96 init_nat_o2i_kv (clib_bihash_kv_8_8_t *kv, nat44_ei_session_t *s,
97                  u32 thread_index, u32 session_index)
98 {
99   init_nat_k (kv, s->out2in.addr, s->out2in.port, s->out2in.fib_index,
100               s->nat_proto);
101   kv->value = (u64) thread_index << 32 | session_index;
102 }
103
104 always_inline u32
105 nat_value_get_thread_index (clib_bihash_kv_8_8_t *value)
106 {
107   return value->value >> 32;
108 }
109
110 always_inline u32
111 nat_value_get_session_index (clib_bihash_kv_8_8_t *value)
112 {
113   return value->value & ~(u32) 0;
114 }
115
116 always_inline u8
117 nat44_ei_is_interface_addr (ip4_main_t *im, vlib_node_runtime_t *node,
118                             u32 sw_if_index0, u32 ip4_addr)
119 {
120   nat44_ei_runtime_t *rt = (nat44_ei_runtime_t *) node->runtime_data;
121   ip4_address_t *first_int_addr;
122
123   if (PREDICT_FALSE (rt->cached_sw_if_index != sw_if_index0))
124     {
125       first_int_addr = ip4_interface_first_address (
126         im, sw_if_index0, 0 /* just want the address */);
127       rt->cached_sw_if_index = sw_if_index0;
128       if (first_int_addr)
129         rt->cached_ip4_address = first_int_addr->as_u32;
130       else
131         rt->cached_ip4_address = 0;
132     }
133
134   if (PREDICT_FALSE (ip4_addr == rt->cached_ip4_address))
135     return 1;
136   else
137     return 0;
138 }
139
140 /** \brief Per-user LRU list maintenance */
141 always_inline void
142 nat44_ei_session_update_lru (nat44_ei_main_t *nm, nat44_ei_session_t *s,
143                              u32 thread_index)
144 {
145   /* don't update too often - timeout is in magnitude of seconds anyway */
146   if (s->last_heard > s->last_lru_update + 1)
147     {
148       clib_dlist_remove (nm->per_thread_data[thread_index].list_pool,
149                          s->per_user_index);
150       clib_dlist_addtail (nm->per_thread_data[thread_index].list_pool,
151                           s->per_user_list_head_index, s->per_user_index);
152       s->last_lru_update = s->last_heard;
153     }
154 }
155
156 always_inline void
157 nat44_ei_user_session_increment (nat44_ei_main_t *nm, nat44_ei_user_t *u,
158                                  u8 is_static)
159 {
160   if (u->nsessions + u->nstaticsessions < nm->max_translations_per_user)
161     {
162       if (is_static)
163         u->nstaticsessions++;
164       else
165         u->nsessions++;
166     }
167 }
168
169 always_inline void
170 nat44_ei_delete_user_with_no_session (nat44_ei_main_t *nm, nat44_ei_user_t *u,
171                                       u32 thread_index)
172 {
173   clib_bihash_kv_8_8_t kv;
174   nat44_ei_user_key_t u_key;
175   nat44_ei_main_per_thread_data_t *tnm =
176     vec_elt_at_index (nm->per_thread_data, thread_index);
177
178   if (u->nstaticsessions == 0 && u->nsessions == 0)
179     {
180       u_key.addr.as_u32 = u->addr.as_u32;
181       u_key.fib_index = u->fib_index;
182       kv.key = u_key.as_u64;
183       pool_put_index (tnm->list_pool, u->sessions_per_user_list_head_index);
184       pool_put (tnm->users, u);
185       clib_bihash_add_del_8_8 (&tnm->user_hash, &kv, 0);
186       vlib_set_simple_counter (&nm->total_users, thread_index, 0,
187                                pool_elts (tnm->users));
188     }
189 }
190
191 static_always_inline u8
192 nat44_ei_maximum_sessions_exceeded (nat44_ei_main_t *nm, u32 thread_index)
193 {
194   if (pool_elts (nm->per_thread_data[thread_index].sessions) >=
195       nm->max_translations_per_thread)
196     return 1;
197   return 0;
198 }
199
200 always_inline void
201 nat44_ei_session_update_counters (nat44_ei_session_t *s, f64 now, uword bytes,
202                                   u32 thread_index)
203 {
204   s->last_heard = now;
205   s->total_pkts++;
206   s->total_bytes += bytes;
207   nat_ha_sref (&s->out2in.addr, s->out2in.port, &s->ext_host_addr,
208                s->ext_host_port, s->nat_proto, s->out2in.fib_index,
209                s->total_pkts, s->total_bytes, thread_index,
210                &s->ha_last_refreshed, now);
211 }
212
213 #endif /* __included_nat44_ei_inlines_h__ */
214
215 /*
216  * fd.io coding-style-patch-verification: ON
217  *
218  * Local Variables:
219  * eval: (c-set-style "gnu")
220  * End:
221  */