ipv6 connection tracking plugin
[vpp.git] / src / plugins / ct6 / ct6_out2in.c
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 <vnet/pg/pg.h>
20 #include <vppinfra/error.h>
21 #include <ct6/ct6.h>
22
23 typedef struct
24 {
25   u32 sw_if_index;
26   u32 next_index;
27   u32 session_index;
28 } ct6_out2in_trace_t;
29
30 #ifndef CLIB_MARCH_VARIANT
31
32 /* packet trace format function */
33 static u8 *
34 format_ct6_out2in_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_out2in_trace_t *t = va_arg (*args, ct6_out2in_trace_t *);
39
40   s = format (s, "CT6_OUT2IN: sw_if_index %d, next index %d session %d\n",
41               t->sw_if_index, t->next_index, t->session_index);
42   return s;
43 }
44
45 vlib_node_registration_t ct6_out2in_node;
46
47 #endif /* CLIB_MARCH_VARIANT */
48
49 #define foreach_ct6_out2in_error                \
50 _(PROCESSED, "ct6 packets processed")           \
51 _(NO_SESSION, "ct6 no session drops")
52
53
54 typedef enum
55 {
56 #define _(sym,str) CT6_OUT2IN_ERROR_##sym,
57   foreach_ct6_out2in_error
58 #undef _
59     CT6_OUT2IN_N_ERROR,
60 } ct6_out2in_error_t;
61
62 #ifndef CLIB_MARCH_VARIANT
63 static char *ct6_out2in_error_strings[] = {
64 #define _(sym,string) string,
65   foreach_ct6_out2in_error
66 #undef _
67 };
68 #endif /* CLIB_MARCH_VARIANT */
69
70 typedef enum
71 {
72   CT6_OUT2IN_NEXT_DROP,
73   CT6_OUT2IN_N_NEXT,
74 } ct6_next_t;
75
76 always_inline uword
77 ct6_out2in_inline (vlib_main_t * vm,
78                    vlib_node_runtime_t * node, vlib_frame_t * frame,
79                    int is_trace)
80 {
81   u32 n_left_from, *from;
82   vlib_buffer_t *bufs[VLIB_FRAME_SIZE], **b;
83   u16 nexts[VLIB_FRAME_SIZE], *next;
84   ct6_main_t *cmp = &ct6_main;
85   u32 my_thread_index = vm->thread_index;
86   f64 now = vlib_time_now (vm);
87   u32 dropped = 0;
88
89   from = vlib_frame_vector_args (frame);
90   n_left_from = frame->n_vectors;
91
92   vlib_get_buffers (vm, from, bufs, n_left_from);
93   b = bufs;
94   next = nexts;
95
96 #if 0
97   while (n_left_from >= 4)
98     {
99       /* Prefetch next iteration. */
100       if (PREDICT_TRUE (n_left_from >= 8))
101         {
102           vlib_prefetch_buffer_header (b[4], STORE);
103           vlib_prefetch_buffer_header (b[5], STORE);
104           vlib_prefetch_buffer_header (b[6], STORE);
105           vlib_prefetch_buffer_header (b[7], STORE);
106           CLIB_PREFETCH (b[4]->data, CLIB_CACHE_LINE_BYTES, STORE);
107           CLIB_PREFETCH (b[5]->data, CLIB_CACHE_LINE_BYTES, STORE);
108           CLIB_PREFETCH (b[6]->data, CLIB_CACHE_LINE_BYTES, STORE);
109           CLIB_PREFETCH (b[7]->data, CLIB_CACHE_LINE_BYTES, STORE);
110         }
111
112       /* $$$$ process 4x pkts right here */
113       next[0] = 0;
114       next[1] = 0;
115       next[2] = 0;
116       next[3] = 0;
117
118       if (is_trace)
119         {
120           if (b[0]->flags & VLIB_BUFFER_IS_TRACED)
121             {
122               ct6_trace_t *t = vlib_add_trace (vm, node, b[0], sizeof (*t));
123               t->next_index = next[0];
124               t->sw_if_index = vnet_buffer (b[0])->sw_if_index[VLIB_RX];
125             }
126           if (b[1]->flags & VLIB_BUFFER_IS_TRACED)
127             {
128               ct6_trace_t *t = vlib_add_trace (vm, node, b[1], sizeof (*t));
129               t->next_index = next[1];
130               t->sw_if_index = vnet_buffer (b[1])->sw_if_index[VLIB_RX];
131             }
132           if (b[2]->flags & VLIB_BUFFER_IS_TRACED)
133             {
134               ct6_trace_t *t = vlib_add_trace (vm, node, b[2], sizeof (*t));
135               t->next_index = next[2];
136               t->sw_if_index = vnet_buffer (b[2])->sw_if_index[VLIB_RX];
137             }
138           if (b[3]->flags & VLIB_BUFFER_IS_TRACED)
139             {
140               ct6_trace_t *t = vlib_add_trace (vm, node, b[3], sizeof (*t));
141               t->next_index = next[3];
142               t->sw_if_index = vnet_buffer (b[3])->sw_if_index[VLIB_RX];
143             }
144         }
145
146       b += 4;
147       next += 4;
148       n_left_from -= 4;
149     }
150 #endif
151
152   while (n_left_from > 0)
153     {
154       clib_bihash_kv_48_8_t kvp0;
155       ct6_session_key_t *key0;
156       ct6_session_t *s0;
157       u32 session_index0 = ~0;
158       u32 next0;
159
160       ip6_header_t *ip0;
161       udp_header_t *udp0;
162
163       /* Are we having fun yet? */
164       vnet_feature_next (&next0, b[0]);
165       next[0] = next0;
166
167       ip0 = vlib_buffer_get_current (b[0]);
168
169       /*
170        * Pass non-global unicast traffic
171        */
172       if (PREDICT_FALSE (!ip6_address_is_global_unicast (&ip0->src_address)
173                          ||
174                          !ip6_address_is_global_unicast (&ip0->src_address)))
175         goto trace0;
176       /* Pass non-udp, non-tcp traffic */
177       if (PREDICT_FALSE (ip0->protocol != IP_PROTOCOL_TCP &&
178                          ip0->protocol != IP_PROTOCOL_UDP))
179         goto trace0;
180
181       udp0 = ip6_next_header (ip0);
182
183       /*
184        * See if we know about this flow.
185        */
186       key0 = (ct6_session_key_t *) & kvp0;
187       clib_memcpy_fast (&key0->src, &ip0->src_address,
188                         sizeof (ip6_address_t));
189       clib_memcpy_fast (&key0->dst, &ip0->dst_address,
190                         sizeof (ip6_address_t));
191       key0->as_u64[4] = 0;
192       key0->as_u64[5] = 0;
193       key0->sport = udp0->src_port;
194       key0->dport = udp0->dst_port;
195       key0->proto = ip0->protocol;
196
197       /* Do we know about this session? */
198       if (clib_bihash_search_48_8 (&cmp->session_hash, &kvp0, &kvp0) < 0)
199         {
200           /* Bad engineer, no donut for you... */
201           next[0] = CT6_OUT2IN_NEXT_DROP;
202           b[0]->error = node->errors[CT6_OUT2IN_ERROR_NO_SESSION];
203           dropped++;
204           goto trace0;
205         }
206       else
207         {
208           s0 = pool_elt_at_index (cmp->sessions[my_thread_index], kvp0.value);
209           session_index0 = kvp0.value;
210           ct6_update_session_hit (cmp, s0, now);
211         }
212
213     trace0:
214       if (is_trace)
215         {
216           if (b[0]->flags & VLIB_BUFFER_IS_TRACED)
217             {
218               ct6_out2in_trace_t *t =
219                 vlib_add_trace (vm, node, b[0], sizeof (*t));
220               t->next_index = next[0];
221               t->sw_if_index = vnet_buffer (b[0])->sw_if_index[VLIB_RX];
222               t->session_index = session_index0;
223             }
224         }
225
226       b += 1;
227       next += 1;
228       n_left_from -= 1;
229     }
230
231   vlib_buffer_enqueue_to_next (vm, node, from, nexts, frame->n_vectors);
232
233   vlib_node_increment_counter (vm, node->node_index,
234                                CT6_OUT2IN_ERROR_PROCESSED, frame->n_vectors);
235   vlib_node_increment_counter (vm, node->node_index,
236                                CT6_OUT2IN_ERROR_NO_SESSION, dropped);
237
238   return frame->n_vectors;
239 }
240
241 VLIB_NODE_FN (ct6_out2in_node) (vlib_main_t * vm, vlib_node_runtime_t * node,
242                                 vlib_frame_t * frame)
243 {
244   if (PREDICT_FALSE (node->flags & VLIB_NODE_FLAG_TRACE))
245     return ct6_out2in_inline (vm, node, frame, 1 /* is_trace */ );
246   else
247     return ct6_out2in_inline (vm, node, frame, 0 /* is_trace */ );
248 }
249
250 /* *INDENT-OFF* */
251 #ifndef CLIB_MARCH_VARIANT
252 VLIB_REGISTER_NODE (ct6_out2in_node) =
253 {
254   .name = "ct6-out2in",
255   .vector_size = sizeof (u32),
256   .format_trace = format_ct6_out2in_trace,
257   .type = VLIB_NODE_TYPE_INTERNAL,
258
259   .n_errors = ARRAY_LEN(ct6_out2in_error_strings),
260   .error_strings = ct6_out2in_error_strings,
261
262   .n_next_nodes = CT6_OUT2IN_N_NEXT,
263
264   /* edit / add dispositions here */
265   .next_nodes = {
266         [CT6_OUT2IN_NEXT_DROP] = "error-drop",
267   },
268 };
269 #endif /* CLIB_MARCH_VARIANT */
270 /* *INDENT-ON* */
271
272 /*
273  * fd.io coding-style-patch-verification: ON
274  *
275  * Local Variables:
276  * eval: (c-set-style "gnu")
277  * End:
278  */