2 * ct6_out2in.c - ip6 connection tracker, inside-to-outside path
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:
9 * http://www.apache.org/licenses/LICENSE-2.0
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.
17 #include <vlib/vlib.h>
18 #include <vnet/vnet.h>
19 #include <vppinfra/error.h>
29 #ifndef CLIB_MARCH_VARIANT
31 /* packet trace format function */
33 format_ct6_out2in_trace (u8 * s, va_list * args)
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 *);
39 s = format (s, "CT6_OUT2IN: sw_if_index %d, next index %d session %d\n",
40 t->sw_if_index, t->next_index, t->session_index);
44 vlib_node_registration_t ct6_out2in_node;
46 #endif /* CLIB_MARCH_VARIANT */
48 #define foreach_ct6_out2in_error \
49 _(PROCESSED, "ct6 packets processed") \
50 _(NO_SESSION, "ct6 no session drops")
55 #define _(sym,str) CT6_OUT2IN_ERROR_##sym,
56 foreach_ct6_out2in_error
61 #ifndef CLIB_MARCH_VARIANT
62 static char *ct6_out2in_error_strings[] = {
63 #define _(sym,string) string,
64 foreach_ct6_out2in_error
67 #endif /* CLIB_MARCH_VARIANT */
76 ct6_out2in_inline (vlib_main_t * vm,
77 vlib_node_runtime_t * node, vlib_frame_t * frame,
80 u32 n_left_from, *from;
81 vlib_buffer_t *bufs[VLIB_FRAME_SIZE], **b;
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);
88 from = vlib_frame_vector_args (frame);
89 n_left_from = frame->n_vectors;
91 vlib_get_buffers (vm, from, bufs, n_left_from);
96 while (n_left_from >= 4)
98 /* Prefetch next iteration. */
99 if (PREDICT_TRUE (n_left_from >= 8))
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_store (b[4]->data);
106 clib_prefetch_store (b[5]->data);
107 clib_prefetch_store (b[6]->data);
108 clib_prefetch_store (b[7]->data);
111 /* $$$$ process 4x pkts right here */
119 if (b[0]->flags & VLIB_BUFFER_IS_TRACED)
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];
125 if (b[1]->flags & VLIB_BUFFER_IS_TRACED)
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];
131 if (b[2]->flags & VLIB_BUFFER_IS_TRACED)
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];
137 if (b[3]->flags & VLIB_BUFFER_IS_TRACED)
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];
151 while (n_left_from > 0)
153 clib_bihash_kv_48_8_t kvp0;
154 ct6_session_key_t *key0;
156 u32 session_index0 = ~0;
162 /* Are we having fun yet? */
163 vnet_feature_next (&next0, b[0]);
166 ip0 = vlib_buffer_get_current (b[0]);
169 * Pass non-global unicast traffic
171 if (PREDICT_FALSE (!ip6_address_is_global_unicast (&ip0->src_address)
173 !ip6_address_is_global_unicast (&ip0->src_address)))
175 /* Pass non-udp, non-tcp traffic */
176 if (PREDICT_FALSE (ip0->protocol != IP_PROTOCOL_TCP &&
177 ip0->protocol != IP_PROTOCOL_UDP))
180 udp0 = ip6_next_header (ip0);
183 * See if we know about this flow.
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));
192 key0->sport = udp0->src_port;
193 key0->dport = udp0->dst_port;
194 key0->proto = ip0->protocol;
196 /* Do we know about this session? */
197 if (clib_bihash_search_48_8 (&cmp->session_hash, &kvp0, &kvp0) < 0)
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];
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);
215 if (b[0]->flags & VLIB_BUFFER_IS_TRACED)
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;
230 vlib_buffer_enqueue_to_next (vm, node, from, nexts, frame->n_vectors);
232 vlib_node_increment_counter (vm, node->node_index,
233 CT6_OUT2IN_ERROR_PROCESSED, frame->n_vectors);
234 vlib_node_increment_counter (vm, node->node_index,
235 CT6_OUT2IN_ERROR_NO_SESSION, dropped);
237 return frame->n_vectors;
240 VLIB_NODE_FN (ct6_out2in_node) (vlib_main_t * vm, vlib_node_runtime_t * node,
241 vlib_frame_t * frame)
243 if (PREDICT_FALSE (node->flags & VLIB_NODE_FLAG_TRACE))
244 return ct6_out2in_inline (vm, node, frame, 1 /* is_trace */ );
246 return ct6_out2in_inline (vm, node, frame, 0 /* is_trace */ );
249 #ifndef CLIB_MARCH_VARIANT
250 VLIB_REGISTER_NODE (ct6_out2in_node) =
252 .name = "ct6-out2in",
253 .vector_size = sizeof (u32),
254 .format_trace = format_ct6_out2in_trace,
255 .type = VLIB_NODE_TYPE_INTERNAL,
257 .n_errors = ARRAY_LEN(ct6_out2in_error_strings),
258 .error_strings = ct6_out2in_error_strings,
260 .n_next_nodes = CT6_OUT2IN_N_NEXT,
262 /* edit / add dispositions here */
264 [CT6_OUT2IN_NEXT_DROP] = "error-drop",
267 #endif /* CLIB_MARCH_VARIANT */
270 * fd.io coding-style-patch-verification: ON
273 * eval: (c-set-style "gnu")