FD.io VPP  v20.05.1-6-gf53edbc3b
Vector Packet Processing
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  * @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 
27 {
28  if (pool_elts (sm->per_thread_data[thread_index].sessions) >=
29  sm->max_translations)
30  return 1;
31  return 0;
32 }
33 
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 *
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_closed_timestamp = 0;
63  s->ha_last_refreshed = now;
64  return s;
65 }
66 
69 {
70  snat_main_t *sm = &snat_main;
72 
73  /* *INDENT-OFF* */
74  vec_foreach (tsm, sm->per_thread_data)
75  {
76  u32 ti;
77 
78  nat44_db_free (tsm);
79  nat44_db_init (tsm);
80 
81  ti = tsm->snat_thread_index;
82  // clear per thread session counters
83  vlib_set_simple_counter (&sm->total_users, ti, 0, 0);
84  vlib_set_simple_counter (&sm->total_sessions, ti, 0, 0);
85  }
86  /* *INDENT-ON* */
87 }
88 
91 {
92  dlist_elt_t *elt;
93  snat_session_t *s;
94 
95  snat_main_t *sm = &snat_main;
96  snat_main_per_thread_data_t *tsm = &sm->per_thread_data[thread_index];
97 
98  // get head
99  elt = pool_elt_at_index (tsm->list_pool,
101  // get first element
102  elt = pool_elt_at_index (tsm->list_pool, elt->next);
103 
104  while (elt->value != ~0)
105  {
106  s = pool_elt_at_index (tsm->sessions, elt->value);
107  elt = pool_elt_at_index (tsm->list_pool, elt->next);
108 
109  nat44_free_session_data (sm, s, thread_index, 0);
110  nat44_delete_session (sm, s, thread_index);
111  }
112 }
113 
116 {
117  int rv = 1;
118 
119  snat_main_t *sm = &snat_main;
121 
122  snat_user_key_t user_key;
124 
125  if (sm->deterministic || sm->endpoint_dependent)
126  return rv;
127 
128  user_key.addr.as_u32 = addr->as_u32;
129  user_key.fib_index = fib_index;
130  kv.key = user_key.as_u64;
131 
132  if (sm->num_workers > 1)
133  {
134  /* *INDENT-OFF* */
135  vec_foreach (tsm, sm->per_thread_data)
136  {
137  if (!clib_bihash_search_8_8 (&tsm->user_hash, &kv, &value))
138  {
140  pool_elt_at_index (tsm->users, value.value),
141  tsm->thread_index);
142  rv = 0;
143  break;
144  }
145  }
146  /* *INDENT-ON* */
147  }
148  else
149  {
151  if (!clib_bihash_search_8_8 (&tsm->user_hash, &kv, &value))
152  {
154  (tsm->users, value.value),
155  tsm->thread_index);
156  rv = 0;
157  }
158  }
159  return rv;
160 }
161 
162 #endif /* included_nat44_inlines_h__ */
163 
164 /*
165  * fd.io coding-style-patch-verification: ON
166  *
167  * Local Variables:
168  * eval: (c-set-style "gnu")
169  * End:
170  */
u32 next
Definition: dlist.h:30
u32 * max_translations_per_fib
Definition: nat.h:636
#define snat_is_session_static(s)
Check if SNAT session is created from static mapping.
Definition: nat.h:728
u32 sessions_per_user_list_head_index
Definition: nat.h:307
void nat44_db_init(snat_main_per_thread_data_t *tsm)
Initialize NAT44 data.
Definition: nat.c:3869
static_always_inline snat_session_t * nat44_session_reuse_old(snat_main_t *sm, snat_user_t *u, snat_session_t *s, u32 thread_index, f64 now)
Definition: inlines.h:46
u32 nsessions
Definition: nat.h:308
u32 nstaticsessions
Definition: nat.h:309
static_always_inline void nat44_user_del_sessions(snat_user_t *u, u32 thread_index)
Definition: inlines.h:90
dlist_elt_t * list_pool
Definition: nat.h:470
void nat44_db_free(snat_main_per_thread_data_t *tsm)
Free NAT44 data.
Definition: nat.c:3931
vhost_vring_addr_t addr
Definition: vhost_user.h:254
unsigned char u8
Definition: types.h:56
u8 deterministic
Definition: nat.h:629
double f64
Definition: types.h:142
clib_bihash_8_8_t user_hash
Definition: nat.h:461
#define static_always_inline
Definition: clib.h:106
#define vec_elt_at_index(v, i)
Get vector value at index i checking that i is in bounds.
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:249
u32 max_translations
Definition: nat.h:635
#define pool_elt_at_index(p, i)
Returns pointer to element at given index.
Definition: pool.h:534
static_always_inline int nat44_user_del(ip4_address_t *addr, u32 fib_index)
Definition: inlines.h:115
u64 key
the key
Definition: bihash_8_8.h:41
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:94
snat_main_t snat_main
Definition: nat.c:41
u64 value
the value
Definition: bihash_8_8.h:42
snat_user_t * users
Definition: nat.h:464
8 octet key, 8 octet key value pair
Definition: bihash_8_8.h:39
void nat44_free_session_data(snat_main_t *sm, snat_session_t *s, u32 thread_index, u8 is_ha)
Free NAT44 ED session data (lookup keys, external address port)
Definition: nat.c:352
static_always_inline u8 nat44_maximum_sessions_exceeded(snat_main_t *sm, u32 thread_index)
The NAT44 inline functions.
Definition: inlines.h:26
u8 value
Definition: qos.api:54
u32 num_workers
Definition: nat.h:527
u64 as_u64
Definition: nat.h:111
ip4_address_t addr
Definition: nat.h:108
static_always_inline u8 nat44_ed_maximum_sessions_exceeded(snat_main_t *sm, u32 fib_index, u32 thread_index)
Definition: inlines.h:35
u32 value
Definition: dlist.h:32
vlib_simple_counter_main_t total_users
Definition: nat.h:659
static_always_inline void nat44_sessions_clear()
Definition: inlines.h:68
#define vec_len(v)
Number of elements in vector (rvalue-only, NULL tolerant)
snat_main_per_thread_data_t * per_thread_data
Definition: nat.h:536
u32 fib_index
Definition: nat.h:109
#define vec_foreach(var, vec)
Vector iterator.
u8 endpoint_dependent
Definition: nat.h:631
vlib_simple_counter_main_t total_sessions
Definition: nat.h:660
snat_session_t * sessions
Definition: nat.h:467
static uword pool_elts(void *v)
Number of active elements in a pool.
Definition: pool.h:128