FD.io VPP  v21.10.1-2-g0a485f517
Vector Packet Processing
nat44_ei_inlines.h
Go to the documentation of this file.
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>
23 
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
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
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
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 
106 {
107  return value->value >> 32;
108 }
109 
112 {
113  return value->value & ~(u32) 0;
114 }
115 
118  u32 sw_if_index0, u32 ip4_addr)
119 {
120  nat44_ei_runtime_t *rt = (nat44_ei_runtime_t *) node->runtime_data;
121  u8 ip4_addr_exists;
122 
123  if (PREDICT_FALSE (rt->cached_sw_if_index != sw_if_index0))
124  {
125  ip_lookup_main_t *lm = &im->lookup_main;
127  ip4_address_t *a;
128 
129  rt->cached_sw_if_index = ~0;
130  hash_free (rt->cached_presence_by_ip4_address);
131 
133  lm, ia, sw_if_index0, 1 /* honor unnumbered */, ({
135  hash_set (rt->cached_presence_by_ip4_address, a->as_u32, 1);
136  rt->cached_sw_if_index = sw_if_index0;
137  }));
138 
139  if (rt->cached_sw_if_index == ~0)
140  return 0;
141  }
142 
143  ip4_addr_exists = !!hash_get (rt->cached_presence_by_ip4_address, ip4_addr);
144  if (PREDICT_FALSE (ip4_addr_exists))
145  return 1;
146  else
147  return 0;
148 }
149 
150 /** \brief Per-user LRU list maintenance */
151 always_inline void
154 {
155  /* don't update too often - timeout is in magnitude of seconds anyway */
156  if (s->last_heard > s->last_lru_update + 1)
157  {
159  s->per_user_index);
161  s->per_user_list_head_index, s->per_user_index);
162  s->last_lru_update = s->last_heard;
163  }
164 }
165 
166 always_inline void
168  u8 is_static)
169 {
170  if (u->nsessions + u->nstaticsessions < nm->max_translations_per_user)
171  {
172  if (is_static)
173  u->nstaticsessions++;
174  else
175  u->nsessions++;
176  }
177 }
178 
179 always_inline void
182 {
184  nat44_ei_user_key_t u_key;
187 
188  if (u->nstaticsessions == 0 && u->nsessions == 0)
189  {
190  u_key.addr.as_u32 = u->addr.as_u32;
191  u_key.fib_index = u->fib_index;
192  kv.key = u_key.as_u64;
193  pool_put_index (tnm->list_pool, u->sessions_per_user_list_head_index);
194  pool_put (tnm->users, u);
195  clib_bihash_add_del_8_8 (&tnm->user_hash, &kv, 0);
197  pool_elts (tnm->users));
198  }
199 }
200 
203 {
206  return 1;
207  return 0;
208 }
209 
210 always_inline void
211 nat44_ei_session_update_counters (nat44_ei_session_t *s, f64 now, uword bytes,
213 {
214  s->last_heard = now;
215  s->total_pkts++;
216  s->total_bytes += bytes;
217  nat_ha_sref (&s->out2in.addr, s->out2in.port, &s->ext_host_addr,
218  s->ext_host_port, s->nat_proto, s->out2in.fib_index,
219  s->total_pkts, s->total_bytes, thread_index,
220  &s->ha_last_refreshed, now);
221 }
222 
223 #endif /* __included_nat44_ei_inlines_h__ */
224 
225 /*
226  * fd.io coding-style-patch-verification: ON
227  *
228  * Local Variables:
229  * eval: (c-set-style "gnu")
230  * End:
231  */
tnm
nat44_ei_main_per_thread_data_t * tnm
Definition: nat44_ei_out2in.c:712
nat_value_get_thread_index
static u32 nat_value_get_thread_index(clib_bihash_kv_8_8_t *value)
Definition: nat44_ei_inlines.h:105
im
vnet_interface_main_t * im
Definition: interface_output.c:415
ip_interface_address_get_address
static void * ip_interface_address_get_address(ip_lookup_main_t *lm, ip_interface_address_t *a)
Definition: ip_interface.h:43
hash_set
#define hash_set(h, key, value)
Definition: hash.h:255
thread_index
u32 thread_index
Definition: nat44_ei_hairpinning.c:495
foreach_ip_interface_address
#define foreach_ip_interface_address(lm, a, sw_if_index, loop, body)
Definition: ip_interface.h:57
nat44_ei_user_key_t::fib_index
u32 fib_index
Definition: nat44_ei.h:106
hash_free
#define hash_free(h)
Definition: hash.h:310
nat44_ei_user_key_t::addr
ip4_address_t addr
Definition: nat44_ei.h:105
nat_ha_sref
void nat_ha_sref(ip4_address_t *out_addr, u16 out_port, ip4_address_t *eh_addr, u16 eh_port, u8 proto, u32 fib_index, u32 total_pkts, u64 total_bytes, u32 thread_index, f64 *last_refreshed, f64 now)
Create session refresh HA event.
Definition: nat44_ei_ha.c:879
clib.h
node
vlib_main_t vlib_node_runtime_t * node
Definition: nat44_ei.c:3047
nat_protocol_t
nat_protocol_t
Definition: lib.h:63
clib_bihash_kv_8_8_t::value
u64 value
the value
Definition: bihash_8_8.h:44
ip4_address_t::as_u32
u32 as_u32
Definition: ip4_packet.h:57
clib_bihash_kv_8_8_t::key
u64 key
the key
Definition: bihash_8_8.h:43
u16
unsigned short u16
Definition: types.h:57
pool_put
#define pool_put(P, E)
Free an object E in pool P.
Definition: pool.h:305
nat44_ei_main_per_thread_data_t::list_pool
dlist_elt_t * list_pool
Definition: nat44_ei.h:287
port
u16 port
Definition: lb_types.api:73
init_nat_i2o_kv
static void init_nat_i2o_kv(clib_bihash_kv_8_8_t *kv, nat44_ei_session_t *s, u32 thread_index, u32 session_index)
Definition: nat44_ei_inlines.h:80
addr
vhost_vring_addr_t addr
Definition: vhost_user.h:130
nat44_ei_main_s::max_translations_per_thread
u32 max_translations_per_thread
Definition: nat44_ei.h:347
pool_put_index
#define pool_put_index(p, i)
Free pool element with given index.
Definition: pool.h:337
key
typedef key
Definition: ipsec_types.api:91
nat44_ei_main_per_thread_data_t::sessions
nat44_ei_session_t * sessions
Definition: nat44_ei.h:284
nat44_ei_main_s
Definition: nat44_ei.h:317
nat44_ei_session_update_lru
static void nat44_ei_session_update_lru(nat44_ei_main_t *nm, nat44_ei_session_t *s, u32 thread_index)
Per-user LRU list maintenance.
Definition: nat44_ei_inlines.h:152
nat_value_get_session_index
static u32 nat_value_get_session_index(clib_bihash_kv_8_8_t *value)
Definition: nat44_ei_inlines.h:111
nat44_ei_user_key_t
Definition: nat44_ei.h:99
vec_elt_at_index
#define vec_elt_at_index(v, i)
Get vector value at index i checking that i is in bounds.
Definition: vec_bootstrap.h:203
init_nat_kv
static void init_nat_kv(clib_bihash_kv_8_8_t *kv, ip4_address_t addr, u16 port, u32 fib_index, nat_protocol_t proto, u32 thread_index, u32 session_index)
Definition: nat44_ei_inlines.h:64
PREDICT_FALSE
#define PREDICT_FALSE(x)
Definition: clib.h:124
hash_get
#define hash_get(h, key)
Definition: hash.h:249
static_always_inline
#define static_always_inline
Definition: clib.h:112
nat44_ei_main_s::max_translations_per_user
u32 max_translations_per_user
Definition: nat44_ei.h:348
uword
u64 uword
Definition: types.h:112
if
if(node->flags &VLIB_NODE_FLAG_TRACE) vnet_interface_output_trace(vm
clib_dlist_remove
static void clib_dlist_remove(dlist_elt_t *pool, u32 index)
Definition: dlist.h:99
f64
double f64
Definition: types.h:142
nat44_ei_main_s::per_thread_data
nat44_ei_main_per_thread_data_t * per_thread_data
Definition: nat44_ei.h:367
nat44_ei_is_interface_addr
static u8 nat44_ei_is_interface_addr(ip4_main_t *im, vlib_node_runtime_t *node, u32 sw_if_index0, u32 ip4_addr)
Definition: nat44_ei_inlines.h:117
ip4_address_t
Definition: ip4_packet.h:50
init_nat_o2i_kv
static void init_nat_o2i_kv(clib_bihash_kv_8_8_t *kv, nat44_ei_session_t *s, u32 thread_index, u32 session_index)
Definition: nat44_ei_inlines.h:96
nat44_ei_runtime_t
Definition: nat44_ei.h:305
nat44_ei.h
nat44_ei_main_per_thread_data_t::user_hash
clib_bihash_8_8_t user_hash
Definition: nat44_ei.h:278
always_inline
#define always_inline
Definition: rdma_mlx5dv.h:23
nat44_ei_user_key_t::as_u64
u64 as_u64
Definition: nat44_ei.h:108
vlib_set_simple_counter
static void vlib_set_simple_counter(vlib_simple_counter_main_t *cm, u32 thread_index, u32 index, u64 value)
Set a simple counter.
Definition: counter.h:109
u64
unsigned long u64
Definition: types.h:89
nat44_ei_main_per_thread_data_t::users
nat44_ei_user_t * users
Definition: nat44_ei.h:281
nat44_ei_user_session_increment
static void nat44_ei_user_session_increment(nat44_ei_main_t *nm, nat44_ei_user_t *u, u8 is_static)
Definition: nat44_ei_inlines.h:167
ASSERT
#define ASSERT(truth)
Definition: error_bootstrap.h:69
calc_nat_key
static u64 calc_nat_key(ip4_address_t addr, u16 port, u32 fib_index, u8 proto)
Definition: nat44_ei_inlines.h:25
u32
unsigned int u32
Definition: types.h:88
nat44_ei_delete_user_with_no_session
static void nat44_ei_delete_user_with_no_session(nat44_ei_main_t *nm, nat44_ei_user_t *u, u32 thread_index)
Definition: nat44_ei_inlines.h:180
clib_bihash_kv_8_8_t
8 octet key, 8 octet key value pair
Definition: bihash_8_8.h:41
pool_elts
static uword pool_elts(void *v)
Number of active elements in a pool.
Definition: pool.h:127
nat44_ei_maximum_sessions_exceeded
static_always_inline u8 nat44_ei_maximum_sessions_exceeded(nat44_ei_main_t *nm, u32 thread_index)
Definition: nat44_ei_inlines.h:202
nm
nat44_ei_main_t * nm
Definition: nat44_ei_hairpinning.c:413
value
u8 value
Definition: qos.api:54
ip_lookup_main_t
Definition: lookup.h:121
now
f64 now
Definition: nat44_ei_out2in.c:710
init_nat_i2o_k
static void init_nat_i2o_k(clib_bihash_kv_8_8_t *kv, nat44_ei_session_t *s)
Definition: nat44_ei_inlines.h:73
u8
unsigned char u8
Definition: types.h:56
a
a
Definition: bitmap.h:525
rt
vnet_interface_output_runtime_t * rt
Definition: interface_output.c:419
clib_dlist_addtail
static void clib_dlist_addtail(dlist_elt_t *pool, u32 head_index, u32 new_index)
Definition: dlist.h:43
split_nat_key
static void split_nat_key(u64 key, ip4_address_t *addr, u16 *port, u32 *fib_index, nat_protocol_t *proto)
Definition: nat44_ei_inlines.h:34
nat44_ei_main_s::total_users
vlib_simple_counter_main_t total_users
Definition: nat44_ei.h:409
ip_interface_address_t
Definition: lookup.h:89
vlib_node_runtime_t
Definition: node.h:454
proto
vl_api_ip_proto_t proto
Definition: acl_types.api:51
init_nat_o2i_k
static void init_nat_o2i_k(clib_bihash_kv_8_8_t *kv, nat44_ei_session_t *s)
Definition: nat44_ei_inlines.h:89
ip4_main_t
IPv4 main type.
Definition: ip4.h:107
init_nat_k
static void init_nat_k(clib_bihash_kv_8_8_t *kv, ip4_address_t addr, u16 port, u32 fib_index, nat_protocol_t proto)
Definition: nat44_ei_inlines.h:56
nat44_ei_ha.h
NAT active-passive HA.
nat44_ei_main_per_thread_data_t
Definition: nat44_ei.h:275
nat44_ei_session_update_counters
static void nat44_ei_session_update_counters(nat44_ei_session_t *s, f64 now, uword bytes, u32 thread_index)
Definition: nat44_ei_inlines.h:211