FD.io VPP  v19.08.1-401-g8e4ed521a
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 <vnet/pg/pg.h>
20 #include <vppinfra/error.h>
21 #include <ct6/ct6.h>
22 
23 typedef struct
24 {
29 
30 #ifndef CLIB_MARCH_VARIANT
31 
32 /* packet trace format function */
33 static u8 *
34 format_ct6_in2out_trace (u8 * s, va_list * args)
35 {
36  CLIB_UNUSED (vlib_main_t * vm) = va_arg (*args, vlib_main_t *);
37  CLIB_UNUSED (vlib_node_t * node) = va_arg (*args, vlib_node_t *);
38  ct6_in2out_trace_t *t = va_arg (*args, ct6_in2out_trace_t *);
39 
40  s = format (s, "CT6_IN2OUT: sw_if_index %d, next index %d session %d\n",
42  return s;
43 }
44 
46 
47 #endif /* CLIB_MARCH_VARIANT */
48 
49 #define foreach_ct6_in2out_error \
50 _(PROCESSED, "ct6 packets processed") \
51 _(CREATED, "ct6 sessions created") \
52 _(RECYCLED, "ct6 sessions recycled")
53 
54 typedef enum
55 {
56 #define _(sym,str) CT6_IN2OUT_ERROR_##sym,
58 #undef _
61 
62 #ifndef CLIB_MARCH_VARIANT
63 static char *ct6_in2out_error_strings[] = {
64 #define _(sym,string) string,
66 #undef _
67 };
68 #endif /* CLIB_MARCH_VARIANT */
69 
70 typedef enum
71 {
74 } ct6_next_t;
75 
76 #ifndef CLIB_MARCH_VARIANT
79  clib_bihash_kv_48_8_t * kvpp, f64 now,
80  u32 my_thread_index, u32 * recyclep,
81  u32 * createp)
82 {
83  ct6_session_t *s0;
84 
85  /* Empty arena? */
86  if (PREDICT_FALSE (cmp->last_index[my_thread_index] == ~0))
87  goto alloc0;
88 
89  /* Look at the least-recently-used session */
90  s0 = pool_elt_at_index (cmp->sessions[my_thread_index],
91  cmp->last_index[my_thread_index]);
92 
93  if (s0->expires < now)
94  clib_warning ("session %d expired %.2f time now %.2f",
95  s0 - cmp->sessions[my_thread_index], s0->expires, now);
96 
97  if (pool_elts (cmp->sessions[my_thread_index]) >=
99  clib_warning ("recycle session %d have %d max %d",
100  s0 - cmp->sessions[my_thread_index],
101  pool_elts (cmp->sessions[my_thread_index]),
103 
104  /* Session expired, or we have as many sessions as is allowed by law? */
105  if ((s0->expires < now) || (pool_elts (cmp->sessions[my_thread_index])
106  >= cmp->max_sessions_per_worker))
107  {
108  /* recycle the session */
109  if (clib_bihash_add_del_48_8 (&cmp->session_hash,
110  (clib_bihash_kv_48_8_t *) s0,
111  0 /* is_add */ ) < 0)
112  clib_warning ("session %d not found in hash?",
113  s0 - cmp->sessions[my_thread_index]);
114 
115  ct6_lru_remove (cmp, s0);
116  *recyclep += 1;
117  }
118  else
119  {
120  alloc0:
121  /* Allocate a fresh session */
122  pool_get (cmp->sessions[my_thread_index], s0);
123  *createp += 1;
124  }
125 
126  /* Session setup */
127  memset (s0, 0, sizeof (*s0));
128  clib_memcpy_fast (s0, kvpp, sizeof (ct6_session_key_t));
129  s0->thread_index = my_thread_index;
130  s0->expires = now + cmp->session_timeout_interval;
131  kvpp->value = s0 - cmp->sessions[my_thread_index];
132  clib_bihash_add_del_48_8 (&cmp->session_hash, kvpp, 1 /* is_add */ );
133  ct6_lru_add (cmp, s0, now);
134  return s0;
135 }
136 #endif /* CLIB_MARCH_VARIANT */
137 
140  vlib_node_runtime_t * node, vlib_frame_t * frame,
141  int is_trace)
142 {
143  u32 n_left_from, *from;
144  vlib_buffer_t *bufs[VLIB_FRAME_SIZE], **b;
145  u16 nexts[VLIB_FRAME_SIZE], *next;
146  ct6_main_t *cmp = &ct6_main;
147  u32 my_thread_index = vm->thread_index;
148  f64 now = vlib_time_now (vm);
149  u32 created = 0;
150  u32 recycled = 0;
151 
152  from = vlib_frame_vector_args (frame);
153  n_left_from = frame->n_vectors;
154 
155  vlib_get_buffers (vm, from, bufs, n_left_from);
156  b = bufs;
157  next = nexts;
158 
159 #if 0
160  while (n_left_from >= 4)
161  {
162  /* Prefetch next iteration. */
163  if (PREDICT_TRUE (n_left_from >= 8))
164  {
165  vlib_prefetch_buffer_header (b[4], STORE);
166  vlib_prefetch_buffer_header (b[5], STORE);
167  vlib_prefetch_buffer_header (b[6], STORE);
168  vlib_prefetch_buffer_header (b[7], STORE);
169  CLIB_PREFETCH (b[4]->data, CLIB_CACHE_LINE_BYTES, STORE);
170  CLIB_PREFETCH (b[5]->data, CLIB_CACHE_LINE_BYTES, STORE);
171  CLIB_PREFETCH (b[6]->data, CLIB_CACHE_LINE_BYTES, STORE);
172  CLIB_PREFETCH (b[7]->data, CLIB_CACHE_LINE_BYTES, STORE);
173  }
174 
175  /* $$$$ process 4x pkts right here */
176  next[0] = 0;
177  next[1] = 0;
178  next[2] = 0;
179  next[3] = 0;
180 
181  if (is_trace)
182  {
183  if (b[0]->flags & VLIB_BUFFER_IS_TRACED)
184  {
185  ct6_trace_t *t = vlib_add_trace (vm, node, b[0], sizeof (*t));
186  t->next_index = next[0];
187  t->sw_if_index = vnet_buffer (b[0])->sw_if_index[VLIB_RX];
188  }
189  if (b[1]->flags & VLIB_BUFFER_IS_TRACED)
190  {
191  ct6_trace_t *t = vlib_add_trace (vm, node, b[1], sizeof (*t));
192  t->next_index = next[1];
193  t->sw_if_index = vnet_buffer (b[1])->sw_if_index[VLIB_RX];
194  }
195  if (b[2]->flags & VLIB_BUFFER_IS_TRACED)
196  {
197  ct6_trace_t *t = vlib_add_trace (vm, node, b[2], sizeof (*t));
198  t->next_index = next[2];
199  t->sw_if_index = vnet_buffer (b[2])->sw_if_index[VLIB_RX];
200  }
201  if (b[3]->flags & VLIB_BUFFER_IS_TRACED)
202  {
203  ct6_trace_t *t = vlib_add_trace (vm, node, b[3], sizeof (*t));
204  t->next_index = next[3];
205  t->sw_if_index = vnet_buffer (b[3])->sw_if_index[VLIB_RX];
206  }
207  }
208 
209  b += 4;
210  next += 4;
211  n_left_from -= 4;
212  }
213 #endif
214 
215  while (n_left_from > 0)
216  {
218  ct6_session_key_t *key0;
219  ct6_session_t *s0;
220  u32 session_index0 = ~0;
221  u32 next0, delta0;
222  ethernet_header_t *e0;
223 
224  ip6_header_t *ip0;
225  udp_header_t *udp0;
226 
227  /* $$$ Set to 0 for pg testing */
228  if (1)
229  {
230  vnet_feature_next (&next0, b[0]);
231  next[0] = next0;
232  }
233  else
234  next[0] = CT6_IN2OUT_NEXT_DROP;
235 
236  /*
237  * This is an output feature which runs at the last possible
238  * moment. Assume an ethernet header. Make sure the packet is
239  * actually ipv6 before we do anything else.
240  *
241  * Unfortunately, we have to re-parse the L2 header.
242  */
243 
244  e0 = vlib_buffer_get_current (b[0]);
245  delta0 = sizeof (*e0);
246  delta0 += (e0->type == clib_net_to_host_u16 (ETHERNET_TYPE_VLAN))
247  ? 4 : 0;
248  delta0 += (e0->type == clib_net_to_host_u16 (ETHERNET_TYPE_DOT1AD))
249  ? 8 : 0;
250 
251  if (PREDICT_TRUE (delta0 == sizeof (*e0)))
252  {
253  if (e0->type != clib_host_to_net_u16 (ETHERNET_TYPE_IP6))
254  goto trace0;
255  }
256  else
257  {
258  u16 *tagged_etype_ptr = vlib_buffer_get_current (b[0]) + delta0 - 2;
259  if (*tagged_etype_ptr != clib_host_to_net_u16 (ETHERNET_TYPE_IP6))
260  goto trace0;
261  }
262 
263  ip0 = (ip6_header_t *) (vlib_buffer_get_current (b[0]) + delta0);
264 
265  /*
266  * Pass non-global unicast traffic
267  */
269  ||
271  goto trace0;
272  /* Pass non-udp, non-tcp traffic */
273  if (PREDICT_FALSE (ip0->protocol != IP_PROTOCOL_TCP &&
274  ip0->protocol != IP_PROTOCOL_UDP))
275  goto trace0;
276 
277  udp0 = ip6_next_header (ip0);
278 
279  /*
280  * See if we know about this flow.
281  * Key set up for the out2in path, the performant case
282  */
283  key0 = (ct6_session_key_t *) & kvp0;
284  clib_memcpy_fast (&key0->src, &ip0->dst_address,
285  sizeof (ip6_address_t));
286  clib_memcpy_fast (&key0->dst, &ip0->src_address,
287  sizeof (ip6_address_t));
288  key0->as_u64[4] = 0;
289  key0->as_u64[5] = 0;
290  key0->sport = udp0->dst_port;
291  key0->dport = udp0->src_port;
292  key0->proto = ip0->protocol;
293 
294  /* Need to create a new session? */
295  if (clib_bihash_search_48_8 (&cmp->session_hash, &kvp0, &kvp0) < 0)
296  {
297  s0 =
298  ct6_create_or_recycle_session (cmp, &kvp0, now, my_thread_index,
299  &recycled, &created);
300  session_index0 = kvp0.value;
301  }
302  else
303  {
304  s0 = pool_elt_at_index (cmp->sessions[my_thread_index], kvp0.value);
305  session_index0 = kvp0.value;
306  ct6_update_session_hit (cmp, s0, now);
307  }
308 
309  trace0:
310  if (is_trace)
311  {
312  if (b[0]->flags & VLIB_BUFFER_IS_TRACED)
313  {
314  ct6_in2out_trace_t *t =
315  vlib_add_trace (vm, node, b[0], sizeof (*t));
316  t->next_index = next[0];
317  t->sw_if_index = vnet_buffer (b[0])->sw_if_index[VLIB_RX];
318  t->session_index = session_index0;
319  }
320  }
321 
322  b += 1;
323  next += 1;
324  n_left_from -= 1;
325  }
326 
327  vlib_buffer_enqueue_to_next (vm, node, from, nexts, frame->n_vectors);
328 
330  CT6_IN2OUT_ERROR_PROCESSED, frame->n_vectors);
332  CT6_IN2OUT_ERROR_CREATED, created);
334  CT6_IN2OUT_ERROR_RECYCLED, recycled);
335 
336  return frame->n_vectors;
337 }
338 
340  vlib_frame_t * frame)
341 {
342  if (PREDICT_FALSE (node->flags & VLIB_NODE_FLAG_TRACE))
343  return ct6_in2out_inline (vm, node, frame, 1 /* is_trace */ );
344  else
345  return ct6_in2out_inline (vm, node, frame, 0 /* is_trace */ );
346 }
347 
348 /* *INDENT-OFF* */
349 #ifndef CLIB_MARCH_VARIANT
351 {
352  .name = "ct6-in2out",
353  .vector_size = sizeof (u32),
354  .format_trace = format_ct6_in2out_trace,
356 
357  .n_errors = ARRAY_LEN(ct6_in2out_error_strings),
358  .error_strings = ct6_in2out_error_strings,
359 
360  .n_next_nodes = CT6_IN2OUT_N_NEXT,
361 
362  /* edit / add dispositions here */
363  .next_nodes = {
364  [CT6_IN2OUT_NEXT_DROP] = "error-drop",
365  },
366  .unformat_buffer = unformat_ethernet_header,
367 };
368 #endif /* CLIB_MARCH_VARIANT */
369 /* *INDENT-ON* */
370 
371 /*
372  * fd.io coding-style-patch-verification: ON
373  *
374  * Local Variables:
375  * eval: (c-set-style "gnu")
376  * End:
377  */
u32 flags
Definition: vhost_user.h:141
#define CLIB_UNUSED(x)
Definition: clib.h:82
#define PREDICT_TRUE(x)
Definition: clib.h:112
static char * ct6_in2out_error_strings[]
Definition: ct6_in2out.c:63
static void ct6_lru_remove(ct6_main_t *cmp, ct6_session_t *s0)
Definition: ct6.h:97
#define clib_memcpy_fast(a, b, c)
Definition: string.h:81
ct6_main_t ct6_main
Definition: ct6.c:53
static f64 vlib_time_now(vlib_main_t *vm)
Definition: main.h:279
vlib_node_registration_t ct6_in2out_node
(constructor) VLIB_REGISTER_NODE (ct6_in2out_node)
Definition: ct6_in2out.c:45
u32 thread_index
Definition: main.h:218
u8 * format(u8 *s, const char *fmt,...)
Definition: format.c:424
u8 data[128]
Definition: ipsec.api:251
#define VLIB_NODE_FN(node)
Definition: node.h:202
#define pool_get(P, E)
Allocate an object E from a pool P (unspecified alignment).
Definition: pool.h:236
ip6_address_t src_address
Definition: ip6_packet.h:383
unsigned char u8
Definition: types.h:56
Definition: ct6.h:58
double f64
Definition: types.h:142
f64 expires
Definition: ct6.h:55
static void ct6_lru_add(ct6_main_t *cmp, ct6_session_t *s0, f64 now)
Definition: ct6.h:130
ct6_session_t ** sessions
Definition: ct6.h:68
#define always_inline
Definition: clib.h:98
#define vlib_prefetch_buffer_header(b, type)
Prefetch buffer metadata.
Definition: buffer.h:203
unsigned int u32
Definition: types.h:88
#define VLIB_FRAME_SIZE
Definition: node.h:378
vl_api_fib_path_type_t type
Definition: fib_types.api:123
#define pool_elt_at_index(p, i)
Returns pointer to element at given index.
Definition: pool.h:514
unsigned short u16
Definition: types.h:57
static void * vlib_buffer_get_current(vlib_buffer_t *b)
Get pointer to current data to process.
Definition: buffer.h:229
#define PREDICT_FALSE(x)
Definition: clib.h:111
u32 node_index
Node index.
Definition: node.h:496
static void vlib_node_increment_counter(vlib_main_t *vm, u32 node_index, u32 counter_index, u64 increment)
Definition: node_funcs.h:1150
ct6_in2out_error_t
Definition: ct6_in2out.c:54
#define VLIB_REGISTER_NODE(x,...)
Definition: node.h:169
u16 n_vectors
Definition: node.h:397
#define CLIB_PREFETCH(addr, size, type)
Definition: cache.h:80
vlib_main_t * vm
Definition: buffer.c:323
static_always_inline void vlib_buffer_enqueue_to_next(vlib_main_t *vm, vlib_node_runtime_t *node, u32 *buffers, u16 *nexts, uword count)
Definition: buffer_node.h:332
static_always_inline void vnet_feature_next(u32 *next0, vlib_buffer_t *b0)
Definition: feature.h:302
#define clib_warning(format, args...)
Definition: error.h:59
#define foreach_ct6_in2out_error
Definition: ct6_in2out.c:49
#define ARRAY_LEN(x)
Definition: clib.h:62
u32 thread_index
Definition: ct6.h:51
static void * ip6_next_header(ip6_header_t *i)
Definition: ip6_packet.h:410
u32 * last_index
Definition: ct6.h:70
static void ct6_update_session_hit(ct6_main_t *cmp, ct6_session_t *s0, f64 now)
Definition: ct6.h:165
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:139
ct6_next_t
Definition: ct6_in2out.c:70
static void * vlib_add_trace(vlib_main_t *vm, vlib_node_runtime_t *r, vlib_buffer_t *b, u32 n_data_bytes)
Definition: trace_funcs.h:55
struct _vlib_node_registration vlib_node_registration_t
static uword ip6_address_is_global_unicast(const ip6_address_t *a)
Definition: ip6_packet.h:340
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:78
VLIB buffer representation.
Definition: buffer.h:102
u64 uword
Definition: types.h:112
static void * vlib_frame_vector_args(vlib_frame_t *f)
Get pointer to frame vector data.
Definition: node_funcs.h:244
clib_bihash_48_8_t session_hash
Definition: ct6.h:64
#define vnet_buffer(b)
Definition: buffer.h:365
static u8 * format_ct6_in2out_trace(u8 *s, va_list *args)
Definition: ct6_in2out.c:34
static_always_inline void vlib_get_buffers(vlib_main_t *vm, u32 *bi, vlib_buffer_t **b, int count)
Translate array of buffer indices into buffer pointers.
Definition: buffer_funcs.h:244
#define VLIB_NODE_FLAG_TRACE
Definition: node.h:302
#define CLIB_CACHE_LINE_BYTES
Definition: cache.h:59
u32 max_sessions_per_worker
Definition: ct6.h:76
f64 session_timeout_interval
Definition: ct6.h:73
Definition: defs.h:46
uword unformat_ethernet_header(unformat_input_t *input, va_list *args)
Definition: format.c:290
ip6_address_t dst_address
Definition: ip6_packet.h:383
static uword pool_elts(void *v)
Number of active elements in a pool.
Definition: pool.h:128