FD.io VPP  v21.01.1
Vector Packet Processing
ct6_out2in.c
Go to the documentation of this file.
1 /*
2  * ct6_out2in.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_out2in_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_out2in_trace_t *t = va_arg (*args, ct6_out2in_trace_t *);
38 
39  s = format (s, "CT6_OUT2IN: 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_out2in_error \
49 _(PROCESSED, "ct6 packets processed") \
50 _(NO_SESSION, "ct6 no session drops")
51 
52 
53 typedef enum
54 {
55 #define _(sym,str) CT6_OUT2IN_ERROR_##sym,
57 #undef _
60 
61 #ifndef CLIB_MARCH_VARIANT
62 static char *ct6_out2in_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 
78  int is_trace)
79 {
80  u32 n_left_from, *from;
82  u16 nexts[VLIB_FRAME_SIZE], *next;
83  ct6_main_t *cmp = &ct6_main;
84  u32 my_thread_index = vm->thread_index;
85  f64 now = vlib_time_now (vm);
86  u32 dropped = 0;
87 
88  from = vlib_frame_vector_args (frame);
89  n_left_from = frame->n_vectors;
90 
91  vlib_get_buffers (vm, from, bufs, n_left_from);
92  b = bufs;
93  next = nexts;
94 
95 #if 0
96  while (n_left_from >= 4)
97  {
98  /* Prefetch next iteration. */
99  if (PREDICT_TRUE (n_left_from >= 8))
100  {
101  vlib_prefetch_buffer_header (b[4], STORE);
102  vlib_prefetch_buffer_header (b[5], STORE);
103  vlib_prefetch_buffer_header (b[6], STORE);
104  vlib_prefetch_buffer_header (b[7], STORE);
105  CLIB_PREFETCH (b[4]->data, CLIB_CACHE_LINE_BYTES, STORE);
106  CLIB_PREFETCH (b[5]->data, CLIB_CACHE_LINE_BYTES, STORE);
107  CLIB_PREFETCH (b[6]->data, CLIB_CACHE_LINE_BYTES, STORE);
108  CLIB_PREFETCH (b[7]->data, CLIB_CACHE_LINE_BYTES, STORE);
109  }
110 
111  /* $$$$ process 4x pkts right here */
112  next[0] = 0;
113  next[1] = 0;
114  next[2] = 0;
115  next[3] = 0;
116 
117  if (is_trace)
118  {
119  if (b[0]->flags & VLIB_BUFFER_IS_TRACED)
120  {
121  ct6_trace_t *t = vlib_add_trace (vm, node, b[0], sizeof (*t));
122  t->next_index = next[0];
123  t->sw_if_index = vnet_buffer (b[0])->sw_if_index[VLIB_RX];
124  }
125  if (b[1]->flags & VLIB_BUFFER_IS_TRACED)
126  {
127  ct6_trace_t *t = vlib_add_trace (vm, node, b[1], sizeof (*t));
128  t->next_index = next[1];
129  t->sw_if_index = vnet_buffer (b[1])->sw_if_index[VLIB_RX];
130  }
131  if (b[2]->flags & VLIB_BUFFER_IS_TRACED)
132  {
133  ct6_trace_t *t = vlib_add_trace (vm, node, b[2], sizeof (*t));
134  t->next_index = next[2];
135  t->sw_if_index = vnet_buffer (b[2])->sw_if_index[VLIB_RX];
136  }
137  if (b[3]->flags & VLIB_BUFFER_IS_TRACED)
138  {
139  ct6_trace_t *t = vlib_add_trace (vm, node, b[3], sizeof (*t));
140  t->next_index = next[3];
141  t->sw_if_index = vnet_buffer (b[3])->sw_if_index[VLIB_RX];
142  }
143  }
144 
145  b += 4;
146  next += 4;
147  n_left_from -= 4;
148  }
149 #endif
150 
151  while (n_left_from > 0)
152  {
154  ct6_session_key_t *key0;
155  ct6_session_t *s0;
156  u32 session_index0 = ~0;
157  u32 next0;
158 
159  ip6_header_t *ip0;
160  udp_header_t *udp0;
161 
162  /* Are we having fun yet? */
163  vnet_feature_next (&next0, b[0]);
164  next[0] = next0;
165 
166  ip0 = vlib_buffer_get_current (b[0]);
167 
168  /*
169  * Pass non-global unicast traffic
170  */
172  ||
174  goto trace0;
175  /* Pass non-udp, non-tcp traffic */
176  if (PREDICT_FALSE (ip0->protocol != IP_PROTOCOL_TCP &&
177  ip0->protocol != IP_PROTOCOL_UDP))
178  goto trace0;
179 
180  udp0 = ip6_next_header (ip0);
181 
182  /*
183  * See if we know about this flow.
184  */
185  key0 = (ct6_session_key_t *) & kvp0;
186  clib_memcpy_fast (&key0->src, &ip0->src_address,
187  sizeof (ip6_address_t));
188  clib_memcpy_fast (&key0->dst, &ip0->dst_address,
189  sizeof (ip6_address_t));
190  key0->as_u64[4] = 0;
191  key0->as_u64[5] = 0;
192  key0->sport = udp0->src_port;
193  key0->dport = udp0->dst_port;
194  key0->proto = ip0->protocol;
195 
196  /* Do we know about this session? */
197  if (clib_bihash_search_48_8 (&cmp->session_hash, &kvp0, &kvp0) < 0)
198  {
199  /* Bad engineer, no donut for you... */
200  next[0] = CT6_OUT2IN_NEXT_DROP;
201  b[0]->error = node->errors[CT6_OUT2IN_ERROR_NO_SESSION];
202  dropped++;
203  goto trace0;
204  }
205  else
206  {
207  s0 = pool_elt_at_index (cmp->sessions[my_thread_index], kvp0.value);
208  session_index0 = kvp0.value;
209  ct6_update_session_hit (cmp, s0, now);
210  }
211 
212  trace0:
213  if (is_trace)
214  {
215  if (b[0]->flags & VLIB_BUFFER_IS_TRACED)
216  {
217  ct6_out2in_trace_t *t =
218  vlib_add_trace (vm, node, b[0], sizeof (*t));
219  t->next_index = next[0];
220  t->sw_if_index = vnet_buffer (b[0])->sw_if_index[VLIB_RX];
221  t->session_index = session_index0;
222  }
223  }
224 
225  b += 1;
226  next += 1;
227  n_left_from -= 1;
228  }
229 
230  vlib_buffer_enqueue_to_next (vm, node, from, nexts, frame->n_vectors);
231 
233  CT6_OUT2IN_ERROR_PROCESSED, frame->n_vectors);
235  CT6_OUT2IN_ERROR_NO_SESSION, dropped);
236 
237  return frame->n_vectors;
238 }
239 
242 {
243  if (PREDICT_FALSE (node->flags & VLIB_NODE_FLAG_TRACE))
244  return ct6_out2in_inline (vm, node, frame, 1 /* is_trace */ );
245  else
246  return ct6_out2in_inline (vm, node, frame, 0 /* is_trace */ );
247 }
248 
249 /* *INDENT-OFF* */
250 #ifndef CLIB_MARCH_VARIANT
252 {
253  .name = "ct6-out2in",
254  .vector_size = sizeof (u32),
255  .format_trace = format_ct6_out2in_trace,
257 
258  .n_errors = ARRAY_LEN(ct6_out2in_error_strings),
259  .error_strings = ct6_out2in_error_strings,
260 
261  .n_next_nodes = CT6_OUT2IN_N_NEXT,
262 
263  /* edit / add dispositions here */
264  .next_nodes = {
265  [CT6_OUT2IN_NEXT_DROP] = "error-drop",
266  },
267 };
268 #endif /* CLIB_MARCH_VARIANT */
269 /* *INDENT-ON* */
270 
271 /*
272  * fd.io coding-style-patch-verification: ON
273  *
274  * Local Variables:
275  * eval: (c-set-style "gnu")
276  * End:
277  */
#define CLIB_UNUSED(x)
Definition: clib.h:87
vl_api_wireguard_peer_flags_t flags
Definition: wireguard.api:105
#define PREDICT_TRUE(x)
Definition: clib.h:122
#define clib_memcpy_fast(a, b, c)
Definition: string.h:81
ct6_main_t ct6_main
Definition: ct6.c:33
static f64 vlib_time_now(vlib_main_t *vm)
Definition: main.h:334
u32 thread_index
Definition: main.h:250
static char * ct6_out2in_error_strings[]
Definition: ct6_out2in.c:62
vlib_main_t * vm
Definition: in2out_ed.c:1580
ct6_out2in_error_t
Definition: ct6_out2in.c:53
#define VLIB_NODE_FN(node)
Definition: node.h:203
vlib_error_t * errors
Vector of errors for this node.
Definition: node.h:470
ip6_address_t src_address
Definition: ip6_packet.h:310
unsigned char u8
Definition: types.h:56
u8 data[128]
Definition: ipsec_types.api:90
Definition: ct6.h:58
double f64
Definition: types.h:142
ct6_session_t ** sessions
Definition: ct6.h:68
description fragment has unexpected format
Definition: map.api:433
#define vlib_prefetch_buffer_header(b, type)
Prefetch buffer metadata.
Definition: buffer.h:207
const cJSON *const b
Definition: cJSON.h:255
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
vlib_error_t error
Error code for buffers to be enqueued to error handler.
Definition: buffer.h:136
ct6_next_t
Definition: ct6_out2in.c:69
#define pool_elt_at_index(p, i)
Returns pointer to element at given index.
Definition: pool.h:546
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:233
#define PREDICT_FALSE(x)
Definition: clib.h:121
#define always_inline
Definition: ipsec.h:28
u32 node_index
Node index.
Definition: node.h:488
static void vlib_node_increment_counter(vlib_main_t *vm, u32 node_index, u32 counter_index, u64 increment)
Definition: node_funcs.h:1231
static uword ct6_out2in_inline(vlib_main_t *vm, vlib_node_runtime_t *node, vlib_frame_t *frame, int is_trace)
Definition: ct6_out2in.c:76
#define VLIB_REGISTER_NODE(x,...)
Definition: node.h:170
#define foreach_ct6_out2in_error
Definition: ct6_out2in.c:48
u16 n_vectors
Definition: node.h:397
#define CLIB_PREFETCH(addr, size, type)
Definition: cache.h:80
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:339
static_always_inline void vnet_feature_next(u32 *next0, vlib_buffer_t *b0)
Definition: feature.h:322
#define ARRAY_LEN(x)
Definition: clib.h:67
static void * ip6_next_header(ip6_header_t *i)
Definition: ip6_packet.h:376
vlib_main_t vlib_node_runtime_t * node
Definition: in2out_ed.c:1580
static u8 * format_ct6_out2in_trace(u8 *s, va_list *args)
Definition: ct6_out2in.c:33
static void ct6_update_session_hit(ct6_main_t *cmp, ct6_session_t *s0, f64 now)
Definition: ct6.h:165
struct _vlib_node_registration vlib_node_registration_t
static uword ip6_address_is_global_unicast(const ip6_address_t *a)
Definition: ip6_packet.h:267
vlib_main_t vlib_node_runtime_t vlib_frame_t * frame
Definition: in2out_ed.c:1581
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:297
clib_bihash_48_8_t session_hash
Definition: ct6.h:64
#define vnet_buffer(b)
Definition: buffer.h:417
void * vlib_add_trace(vlib_main_t *vm, vlib_node_runtime_t *r, vlib_buffer_t *b, u32 n_data_bytes)
Definition: trace.c:634
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:280
#define VLIB_NODE_FLAG_TRACE
Definition: node.h:302
#define CLIB_CACHE_LINE_BYTES
Definition: cache.h:59
Definition: defs.h:46
vlib_node_registration_t ct6_out2in_node
(constructor) VLIB_REGISTER_NODE (ct6_out2in_node)
Definition: ct6_out2in.c:44
ip6_address_t dst_address
Definition: ip6_packet.h:310