FD.io VPP  v20.01-48-g3e0dafb74
Vector Packet Processing
nat44_inlines.h
Go to the documentation of this file.
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 
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 
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,
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 +
56 
57  if (now < sess_timeout_time)
58  continue;
59 
60  nat44_session_cleanup (s, thread_index);
61  }
62 }
63 
66  u32 fib_index, u32 thread_index, f64 now)
67 {
68  snat_user_t *u = 0;
69  snat_user_key_t user_key;
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
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
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  */
u32 next
Definition: dlist.h:30
u32 sessions_per_user_list_head_index
Definition: nat.h:349
static_always_inline void nat44_user_try_cleanup(snat_user_t *u, u32 thread_index, f64 now)
Definition: nat44_inlines.h:34
static u32 nat44_session_get_timeout(snat_main_t *sm, snat_session_t *s)
Definition: nat_inlines.h:394
#define PREDICT_TRUE(x)
Definition: clib.h:112
unsigned long u64
Definition: types.h:89
void nat_free_session_data(snat_main_t *sm, snat_session_t *s, u32 thread_index, u8 is_ha)
Free NAT44 session data (lookup keys, external addrres port)
Definition: nat.c:194
dlist_elt_t * list_pool
Definition: nat.h:512
vhost_vring_addr_t addr
Definition: vhost_user.h:147
double f64
Definition: types.h:142
clib_bihash_8_8_t user_hash
Definition: nat.h:503
#define static_always_inline
Definition: clib.h:99
#define pool_foreach(VAR, POOL, BODY)
Iterate through pool.
Definition: pool.h:498
unsigned int u32
Definition: types.h:88
static void nat44_delete_session(snat_main_t *sm, snat_session_t *ses, u32 thread_index)
Definition: nat_inlines.h:289
u32 max_translations
Definition: nat.h:666
#define pool_elt_at_index(p, i)
Returns pointer to element at given index.
Definition: pool.h:519
u64 key
the key
Definition: bihash_8_8.h:35
#define PREDICT_FALSE(x)
Definition: clib.h:111
snat_main_t snat_main
Definition: nat.c:39
u64 value
the value
Definition: bihash_8_8.h:36
snat_user_t * users
Definition: nat.h:506
8 octet key, 8 octet key value pair
Definition: bihash_8_8.h:33
u8 value
Definition: qos.api:54
u64 as_u64
Definition: nat.h:140
ip4_address_t addr
Definition: nat.h:137
u32 value
Definition: dlist.h:32
snat_main_per_thread_data_t * per_thread_data
Definition: nat.h:568
static_always_inline void nat44_session_try_cleanup(ip4_address_t *addr, u32 fib_index, u32 thread_index, f64 now)
Definition: nat44_inlines.h:65
u32 fib_index
Definition: nat.h:138
static_always_inline void nat44_session_cleanup(snat_session_t *s, u32 thread_index)
The NAT44 inline functions.
Definition: nat44_inlines.h:25
snat_session_t * sessions
Definition: nat.h:509
static uword pool_elts(void *v)
Number of active elements in a pool.
Definition: pool.h:128