FD.io VPP  v21.06-3-gbb25fbf28
Vector Packet Processing
ct6_in2out.c
Go to the documentation of this file.
1 /*
2  * ct6_in2out.c - ip6 connection tracker, inside-to-outside path
3  *
4  * Copyright (c) 2019 Cisco and/or its affiliates.
5  * Licensed under the Apache License, Version 2.0 (the "License");
6  * you may not use this file except in compliance with the License.
7  * You may obtain a copy of the License at:
8  *
9  * http://www.apache.org/licenses/LICENSE-2.0
10  *
11  * Unless required by applicable law or agreed to in writing, software
12  * distributed under the License is distributed on an "AS IS" BASIS,
13  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14  * See the License for the specific language governing permissions and
15  * limitations under the License.
16  */
17 #include <vlib/vlib.h>
18 #include <vnet/vnet.h>
19 #include <vppinfra/error.h>
20 #include <ct6/ct6.h>
21 
22 typedef struct
23 {
28 
29 #ifndef CLIB_MARCH_VARIANT
30 
31 /* packet trace format function */
32 static u8 *
33 format_ct6_in2out_trace (u8 * s, va_list * args)
34 {
35  CLIB_UNUSED (vlib_main_t * vm) = va_arg (*args, vlib_main_t *);
36  CLIB_UNUSED (vlib_node_t * node) = va_arg (*args, vlib_node_t *);
37  ct6_in2out_trace_t *t = va_arg (*args, ct6_in2out_trace_t *);
38 
39  s = format (s, "CT6_IN2OUT: sw_if_index %d, next index %d session %d\n",
41  return s;
42 }
43 
45 
46 #endif /* CLIB_MARCH_VARIANT */
47 
48 #define foreach_ct6_in2out_error \
49 _(PROCESSED, "ct6 packets processed") \
50 _(CREATED, "ct6 sessions created") \
51 _(RECYCLED, "ct6 sessions recycled")
52 
53 typedef enum
54 {
55 #define _(sym,str) CT6_IN2OUT_ERROR_##sym,
57 #undef _
60 
61 #ifndef CLIB_MARCH_VARIANT
62 static char *ct6_in2out_error_strings[] = {
63 #define _(sym,string) string,
65 #undef _
66 };
67 #endif /* CLIB_MARCH_VARIANT */
68 
69 typedef enum
70 {
73 } ct6_next_t;
74 
75 #ifndef CLIB_MARCH_VARIANT
79  u32 my_thread_index, u32 * recyclep,
80  u32 * createp)
81 {
82  ct6_session_t *s0;
83 
84  /* Empty arena? */
85  if (PREDICT_FALSE (cmp->last_index[my_thread_index] == ~0))
86  goto alloc0;
87 
88  /* Look at the least-recently-used session */
89  s0 = pool_elt_at_index (cmp->sessions[my_thread_index],
90  cmp->last_index[my_thread_index]);
91 
92  if (CLIB_DEBUG > 0 && s0->expires < now)
93  clib_warning ("session %d expired %.2f time now %.2f",
94  s0 - cmp->sessions[my_thread_index], s0->expires, now);
95 
96  if (CLIB_DEBUG > 0 && pool_elts (cmp->sessions[my_thread_index]) >=
98  clib_warning ("recycle session %d have %d max %d",
99  s0 - cmp->sessions[my_thread_index],
100  pool_elts (cmp->sessions[my_thread_index]),
102 
103  /* Session expired, or we have as many sessions as is allowed by law? */
104  if ((s0->expires < now) || (pool_elts (cmp->sessions[my_thread_index])
105  >= cmp->max_sessions_per_worker))
106  {
107  /* recycle the session */
108  if (clib_bihash_add_del_48_8 (&cmp->session_hash,
109  (clib_bihash_kv_48_8_t *) s0,
110  0 /* is_add */ ) < 0)
111  clib_warning ("session %d not found in hash?",
112  s0 - cmp->sessions[my_thread_index]);
113 
114  ct6_lru_remove (cmp, s0);
115  *recyclep += 1;
116  }
117  else
118  {
119  alloc0:
120  /* Allocate a fresh session */
121  pool_get (cmp->sessions[my_thread_index], s0);
122  *createp += 1;
123  }
124 
125  /* Session setup */
126  memset (s0, 0, sizeof (*s0));
127  clib_memcpy_fast (s0, kvpp, sizeof (ct6_session_key_t));
128  s0->thread_index = my_thread_index;
129  s0->expires = now + cmp->session_timeout_interval;
130  kvpp->value = s0 - cmp->sessions[my_thread_index];
131  clib_bihash_add_del_48_8 (&cmp->session_hash, kvpp, 1 /* is_add */ );
132  ct6_lru_add (cmp, s0, now);
133  return s0;
134 }
135 #endif /* CLIB_MARCH_VARIANT */
136 
140  int is_trace)
141 {
142  u32 n_left_from, *from;
145  ct6_main_t *cmp = &ct6_main;
146  u32 my_thread_index = vm->thread_index;
147  f64 now = vlib_time_now (vm);
148  u32 created = 0;
149  u32 recycled = 0;
150 
152  n_left_from = frame->n_vectors;
153 
155  b = bufs;
156  next = nexts;
157 
158 #if 0
159  while (n_left_from >= 4)
160  {
161  /* Prefetch next iteration. */
162  if (PREDICT_TRUE (n_left_from >= 8))
163  {
164  vlib_prefetch_buffer_header (b[4], STORE);
165  vlib_prefetch_buffer_header (b[5], STORE);
166  vlib_prefetch_buffer_header (b[6], STORE);
167  vlib_prefetch_buffer_header (b[7], STORE);
172  }
173 
174  /* $$$$ process 4x pkts right here */
175  next[0] = 0;
176  next[1] = 0;
177  next[2] = 0;
178  next[3] = 0;
179 
180  if (is_trace)
181  {
182  if (b[0]->flags & VLIB_BUFFER_IS_TRACED)
183  {
184  ct6_trace_t *t = vlib_add_trace (vm, node, b[0], sizeof (*t));
185  t->next_index = next[0];
186  t->sw_if_index = vnet_buffer (b[0])->sw_if_index[VLIB_RX];
187  }
188  if (b[1]->flags & VLIB_BUFFER_IS_TRACED)
189  {
190  ct6_trace_t *t = vlib_add_trace (vm, node, b[1], sizeof (*t));
191  t->next_index = next[1];
192  t->sw_if_index = vnet_buffer (b[1])->sw_if_index[VLIB_RX];
193  }
194  if (b[2]->flags & VLIB_BUFFER_IS_TRACED)
195  {
196  ct6_trace_t *t = vlib_add_trace (vm, node, b[2], sizeof (*t));
197  t->next_index = next[2];
198  t->sw_if_index = vnet_buffer (b[2])->sw_if_index[VLIB_RX];
199  }
200  if (b[3]->flags & VLIB_BUFFER_IS_TRACED)
201  {
202  ct6_trace_t *t = vlib_add_trace (vm, node, b[3], sizeof (*t));
203  t->next_index = next[3];
204  t->sw_if_index = vnet_buffer (b[3])->sw_if_index[VLIB_RX];
205  }
206  }
207 
208  b += 4;
209  next += 4;
210  n_left_from -= 4;
211  }
212 #endif
213 
214  while (n_left_from > 0)
215  {
217  ct6_session_key_t *key0;
218  ct6_session_t *s0;
219  u32 session_index0 = ~0;
220  u32 next0, delta0;
221  ethernet_header_t *e0;
222 
223  ip6_header_t *ip0;
224  udp_header_t *udp0;
225 
226  /* $$$ Set to 0 for pg testing */
227  if (1)
228  {
229  vnet_feature_next (&next0, b[0]);
230  next[0] = next0;
231  }
232  else
234 
235  /*
236  * This is an output feature which runs at the last possible
237  * moment. Assume an ethernet header. Make sure the packet is
238  * actually ipv6 before we do anything else.
239  *
240  * Unfortunately, we have to re-parse the L2 header.
241  */
242 
243  e0 = vlib_buffer_get_current (b[0]);
244  delta0 = sizeof (*e0);
245  delta0 += (e0->type == clib_net_to_host_u16 (ETHERNET_TYPE_VLAN))
246  ? 4 : 0;
247  delta0 += (e0->type == clib_net_to_host_u16 (ETHERNET_TYPE_DOT1AD))
248  ? 8 : 0;
249 
250  if (PREDICT_TRUE (delta0 == sizeof (*e0)))
251  {
252  if (e0->type != clib_host_to_net_u16 (ETHERNET_TYPE_IP6))
253  goto trace0;
254  }
255  else
256  {
257  u16 *tagged_etype_ptr = vlib_buffer_get_current (b[0]) + delta0 - 2;
258  if (*tagged_etype_ptr != clib_host_to_net_u16 (ETHERNET_TYPE_IP6))
259  goto trace0;
260  }
261 
262  ip0 = (ip6_header_t *) (vlib_buffer_get_current (b[0]) + delta0);
263 
264  /*
265  * Pass non-global unicast traffic
266  */
268  ||
270  goto trace0;
271  /* Pass non-udp, non-tcp traffic */
272  if (PREDICT_FALSE (ip0->protocol != IP_PROTOCOL_TCP &&
273  ip0->protocol != IP_PROTOCOL_UDP))
274  goto trace0;
275 
276  udp0 = ip6_next_header (ip0);
277 
278  /*
279  * See if we know about this flow.
280  * Key set up for the out2in path, the performant case
281  */
282  key0 = (ct6_session_key_t *) & kvp0;
283  clib_memcpy_fast (&key0->src, &ip0->dst_address,
284  sizeof (ip6_address_t));
285  clib_memcpy_fast (&key0->dst, &ip0->src_address,
286  sizeof (ip6_address_t));
287  key0->as_u64[4] = 0;
288  key0->as_u64[5] = 0;
289  key0->sport = udp0->dst_port;
290  key0->dport = udp0->src_port;
291  key0->proto = ip0->protocol;
292 
293  /* Need to create a new session? */
294  if (clib_bihash_search_48_8 (&cmp->session_hash, &kvp0, &kvp0) < 0)
295  {
296  s0 =
297  ct6_create_or_recycle_session (cmp, &kvp0, now, my_thread_index,
298  &recycled, &created);
299  session_index0 = kvp0.value;
300  }
301  else
302  {
303  s0 = pool_elt_at_index (cmp->sessions[my_thread_index], kvp0.value);
304  session_index0 = kvp0.value;
305  ct6_update_session_hit (cmp, s0, now);
306  }
307 
308  trace0:
309  if (is_trace)
310  {
311  if (b[0]->flags & VLIB_BUFFER_IS_TRACED)
312  {
313  ct6_in2out_trace_t *t =
314  vlib_add_trace (vm, node, b[0], sizeof (*t));
315  t->next_index = next[0];
316  t->sw_if_index = vnet_buffer (b[0])->sw_if_index[VLIB_RX];
317  t->session_index = session_index0;
318  }
319  }
320 
321  b += 1;
322  next += 1;
323  n_left_from -= 1;
324  }
325 
327 
328  vlib_node_increment_counter (vm, node->node_index,
329  CT6_IN2OUT_ERROR_PROCESSED, frame->n_vectors);
330  vlib_node_increment_counter (vm, node->node_index,
331  CT6_IN2OUT_ERROR_CREATED, created);
332  vlib_node_increment_counter (vm, node->node_index,
333  CT6_IN2OUT_ERROR_RECYCLED, recycled);
334 
335  return frame->n_vectors;
336 }
337 
340 {
341  if (PREDICT_FALSE (node->flags & VLIB_NODE_FLAG_TRACE))
342  return ct6_in2out_inline (vm, node, frame, 1 /* is_trace */ );
343  else
344  return ct6_in2out_inline (vm, node, frame, 0 /* is_trace */ );
345 }
346 
347 /* *INDENT-OFF* */
348 #ifndef CLIB_MARCH_VARIANT
350 {
351  .name = "ct6-in2out",
352  .vector_size = sizeof (u32),
353  .format_trace = format_ct6_in2out_trace,
355 
356  .n_errors = ARRAY_LEN(ct6_in2out_error_strings),
357  .error_strings = ct6_in2out_error_strings,
358 
359  .n_next_nodes = CT6_IN2OUT_N_NEXT,
360 
361  /* edit / add dispositions here */
362  .next_nodes = {
363  [CT6_IN2OUT_NEXT_DROP] = "error-drop",
364  },
365  .unformat_buffer = unformat_ethernet_header,
366 };
367 #endif /* CLIB_MARCH_VARIANT */
368 /* *INDENT-ON* */
369 
370 /*
371  * fd.io coding-style-patch-verification: ON
372  *
373  * Local Variables:
374  * eval: (c-set-style "gnu")
375  * End:
376  */
vlib.h
udp_header_t::src_port
u16 src_port
Definition: udp_packet.h:48
bufs
vlib_buffer_t * bufs[VLIB_FRAME_SIZE]
Definition: nat44_ei_out2in.c:717
vlib_prefetch_buffer_header
#define vlib_prefetch_buffer_header(b, type)
Prefetch buffer metadata.
Definition: buffer.h:231
frame
vlib_main_t vlib_node_runtime_t vlib_frame_t * frame
Definition: nat44_ei.c:3048
ct6_in2out_trace_t::session_index
u32 session_index
Definition: ct6_in2out.c:26
ip6_address_is_global_unicast
static uword ip6_address_is_global_unicast(const ip6_address_t *a)
Definition: ip6_packet.h:267
ct6_in2out_node
vlib_node_registration_t ct6_in2out_node
(constructor) VLIB_REGISTER_NODE (ct6_in2out_node)
Definition: ct6_in2out.c:44
ct6_session_t::thread_index
u32 thread_index
Definition: ct6.h:51
pool_elt_at_index
#define pool_elt_at_index(p, i)
Returns pointer to element at given index.
Definition: pool.h:553
ip6_header_t::protocol
u8 protocol
Definition: ip6_packet.h:304
vlib_get_buffers
vlib_get_buffers(vm, from, b, n_left_from)
next
u16 * next
Definition: nat44_ei_out2in.c:718
CT6_IN2OUT_NEXT_DROP
@ CT6_IN2OUT_NEXT_DROP
Definition: ct6_in2out.c:71
VLIB_NODE_TYPE_INTERNAL
@ VLIB_NODE_TYPE_INTERNAL
Definition: node.h:72
VLIB_FRAME_SIZE
#define VLIB_FRAME_SIZE
Definition: node.h:368
node
vlib_main_t vlib_node_runtime_t * node
Definition: nat44_ei.c:3047
ct6_next_t
ct6_next_t
Definition: ct6_in2out.c:69
ct6_update_session_hit
static void ct6_update_session_hit(ct6_main_t *cmp, ct6_session_t *s0, f64 now)
Definition: ct6.h:165
ct6_in2out_trace_t::sw_if_index
u32 sw_if_index
Definition: ct6_in2out.c:24
u16
unsigned short u16
Definition: types.h:57
ethernet_header_t::type
u16 type
Definition: packet.h:59
vm
vlib_main_t * vm
X-connect all packets from the HOST to the PHY.
Definition: nat44_ei.c:3047
VLIB_RX
@ VLIB_RX
Definition: defs.h:46
vlib_buffer_enqueue_to_next
vlib_buffer_enqueue_to_next(vm, node, from,(u16 *) nexts, frame->n_vectors)
ip6_next_header
static void * ip6_next_header(ip6_header_t *i)
Definition: ip6_packet.h:407
vlib_frame_t
Definition: node.h:372
clib_memcpy_fast
static_always_inline void * clib_memcpy_fast(void *restrict dst, const void *restrict src, size_t n)
Definition: string.h:92
udp_header_t
Definition: udp_packet.h:45
ct6_in2out_trace_t
Definition: ct6_in2out.c:22
ct6_main_t
Definition: ct6.h:58
ct6_main_t::session_hash
clib_bihash_48_8_t session_hash
Definition: ct6.h:64
CLIB_PREFETCH
#define CLIB_PREFETCH(addr, size, type)
Definition: cache.h:80
error.h
VLIB_NODE_FN
#define VLIB_NODE_FN(node)
Definition: node.h:202
ct6_main_t::sessions
ct6_session_t ** sessions
Definition: ct6.h:68
CLIB_UNUSED
#define CLIB_UNUSED(x)
Definition: clib.h:90
vnet_buffer
#define vnet_buffer(b)
Definition: buffer.h:437
ct6_lru_add
static void ct6_lru_add(ct6_main_t *cmp, ct6_session_t *s0, f64 now)
Definition: ct6.h:130
CT6_IN2OUT_N_NEXT
@ CT6_IN2OUT_N_NEXT
Definition: ct6_in2out.c:72
VLIB_NODE_FLAG_TRACE
#define VLIB_NODE_FLAG_TRACE
Definition: node.h:291
PREDICT_FALSE
#define PREDICT_FALSE(x)
Definition: clib.h:124
vnet_feature_next
static_always_inline void vnet_feature_next(u32 *next0, vlib_buffer_t *b0)
Definition: feature.h:322
ARRAY_LEN
#define ARRAY_LEN(x)
Definition: clib.h:70
vlib_frame_vector_args
static void * vlib_frame_vector_args(vlib_frame_t *f)
Get pointer to frame vector data.
Definition: node_funcs.h:301
uword
u64 uword
Definition: types.h:112
ethernet_header_t
Definition: packet.h:52
vlib_main_t::thread_index
u32 thread_index
Definition: main.h:213
vlib_node_increment_counter
static void vlib_node_increment_counter(vlib_main_t *vm, u32 node_index, u32 counter_index, u64 increment)
Definition: node_funcs.h:1244
ct6_session_t::expires
f64 expires
Definition: ct6.h:55
ct6_lru_remove
static void ct6_lru_remove(ct6_main_t *cmp, ct6_session_t *s0)
Definition: ct6.h:97
format_ct6_in2out_trace
static u8 * format_ct6_in2out_trace(u8 *s, va_list *args)
Definition: ct6_in2out.c:33
f64
double f64
Definition: types.h:142
ip6_header_t::dst_address
ip6_address_t dst_address
Definition: ip6_packet.h:310
ct6_in2out_inline
static uword ct6_in2out_inline(vlib_main_t *vm, vlib_node_runtime_t *node, vlib_frame_t *frame, int is_trace)
Definition: ct6_in2out.c:138
pool_get
#define pool_get(P, E)
Allocate an object E from a pool P (unspecified alignment).
Definition: pool.h:255
CLIB_CACHE_LINE_BYTES
#define CLIB_CACHE_LINE_BYTES
Definition: cache.h:59
vlib_node_registration_t
struct _vlib_node_registration vlib_node_registration_t
ct6_create_or_recycle_session
ct6_session_t * ct6_create_or_recycle_session(ct6_main_t *cmp, clib_bihash_kv_48_8_t *kvpp, f64 now, u32 my_thread_index, u32 *recyclep, u32 *createp)
Definition: ct6_in2out.c:77
data
u8 data[128]
Definition: ipsec_types.api:92
CT6_IN2OUT_N_ERROR
@ CT6_IN2OUT_N_ERROR
Definition: ct6_in2out.c:58
always_inline
#define always_inline
Definition: rdma_mlx5dv.h:23
unformat_ethernet_header
uword unformat_ethernet_header(unformat_input_t *input, va_list *args)
Definition: format.c:290
format
description fragment has unexpected format
Definition: map.api:433
foreach_ct6_in2out_error
#define foreach_ct6_in2out_error
Definition: ct6_in2out.c:48
u32
unsigned int u32
Definition: types.h:88
ct6.h
udp_header_t::dst_port
u16 dst_port
Definition: udp_packet.h:48
ct6_session_t
Definition: ct6.h:48
pool_elts
static uword pool_elts(void *v)
Number of active elements in a pool.
Definition: pool.h:127
ct6_in2out_error_t
ct6_in2out_error_t
Definition: ct6_in2out.c:53
ct6_in2out_error_strings
static char * ct6_in2out_error_strings[]
Definition: ct6_in2out.c:62
ip6_header_t
Definition: ip6_packet.h:294
now
f64 now
Definition: nat44_ei_out2in.c:710
ip6_header_t::src_address
ip6_address_t src_address
Definition: ip6_packet.h:310
vlib_main_t
Definition: main.h:102
vlib_node_t
Definition: node.h:247
vlib_add_trace
void * vlib_add_trace(vlib_main_t *vm, vlib_node_runtime_t *r, vlib_buffer_t *b, u32 n_data_bytes)
Definition: trace.c:628
b
vlib_buffer_t ** b
Definition: nat44_ei_out2in.c:717
clib_bihash_kv_48_8_t::value
u64 value
Definition: bihash_48_8.h:42
u8
unsigned char u8
Definition: types.h:56
vlib_buffer_get_current
static void * vlib_buffer_get_current(vlib_buffer_t *b)
Get pointer to current data to process.
Definition: buffer.h:257
ct6_main_t::max_sessions_per_worker
u32 max_sessions_per_worker
Definition: ct6.h:76
clib_warning
#define clib_warning(format, args...)
Definition: error.h:59
nexts
u16 nexts[VLIB_FRAME_SIZE]
Definition: nat44_ei_out2in.c:718
ct6_in2out_trace_t::next_index
u32 next_index
Definition: ct6_in2out.c:25
ct6_main_t::session_timeout_interval
f64 session_timeout_interval
Definition: ct6.h:73
vlib_time_now
static f64 vlib_time_now(vlib_main_t *vm)
Definition: main.h:325
vnet.h
vlib_node_runtime_t
Definition: node.h:454
from
from
Definition: nat44_ei_hairpinning.c:415
PREDICT_TRUE
#define PREDICT_TRUE(x)
Definition: clib.h:125
ct6_main
ct6_main_t ct6_main
Definition: ct6.c:33
ct6_main_t::last_index
u32 * last_index
Definition: ct6.h:70
n_left_from
n_left_from
Definition: nat44_ei_hairpinning.c:416
clib_bihash_kv_48_8_t
Definition: bihash_48_8.h:39
type
vl_api_fib_path_type_t type
Definition: fib_types.api:123
vlib_buffer_t
VLIB buffer representation.
Definition: buffer.h:111
VLIB_REGISTER_NODE
#define VLIB_REGISTER_NODE(x,...)
Definition: node.h:169
flags
vl_api_wireguard_peer_flags_t flags
Definition: wireguard.api:105