dpdk: Add support for Mellanox ConnectX-4 devices
[vpp.git] / src / vnet / lawful-intercept / node.c
1 /*
2  * Copyright (c) 2015 Cisco and/or its affiliates.
3  * Licensed under the Apache License, Version 2.0 (the "License");
4  * you may not use this file except in compliance with the License.
5  * You may obtain a copy of the License at:
6  *
7  *     http://www.apache.org/licenses/LICENSE-2.0
8  *
9  * Unless required by applicable law or agreed to in writing, software
10  * distributed under the License is distributed on an "AS IS" BASIS,
11  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12  * See the License for the specific language governing permissions and
13  * limitations under the License.
14  */
15
16 #include <vlib/vlib.h>
17 #include <vnet/vnet.h>
18 #include <vppinfra/error.h>
19
20 #include <vnet/lawful-intercept/lawful_intercept.h>
21
22 #include <vppinfra/error.h>
23 #include <vppinfra/elog.h>
24
25 vlib_node_registration_t li_hit_node;
26
27 typedef struct {
28   u32 next_index;
29 } li_hit_trace_t;
30
31 /* packet trace format function */
32 static u8 * format_li_hit_trace (u8 * s, va_list * args)
33 {
34   CLIB_UNUSED (vlib_main_t * vm) = va_arg (*args, vlib_main_t *);
35   CLIB_UNUSED (vlib_node_t * node) = va_arg (*args, vlib_node_t *);
36   li_hit_trace_t * t = va_arg (*args, li_hit_trace_t *);
37   
38   s = format (s, "LI_HIT: next index %d", t->next_index);
39
40   return s;
41 }
42
43 vlib_node_registration_t li_hit_node;
44
45 #define foreach_li_hit_error                    \
46 _(HITS, "LI packets processed")                 \
47 _(NO_COLLECTOR, "No collector configured")
48
49 typedef enum {
50 #define _(sym,str) LI_HIT_ERROR_##sym,
51   foreach_li_hit_error
52 #undef _
53   LI_HIT_N_ERROR,
54 } li_hit_error_t;
55
56 static char * li_hit_error_strings[] = {
57 #define _(sym,string) string,
58   foreach_li_hit_error
59 #undef _
60 };
61
62 typedef enum {
63   LI_HIT_NEXT_ETHERNET,
64   LI_HIT_N_NEXT,
65 } li_hit_next_t;
66
67 static uword
68 li_hit_node_fn (vlib_main_t * vm,
69                   vlib_node_runtime_t * node,
70                   vlib_frame_t * frame)
71 {
72   u32 n_left_from, * from, * to_next;
73   li_hit_next_t next_index;
74   vlib_frame_t * int_frame = 0;
75   u32 * to_int_next = 0;
76   li_main_t * lm = &li_main;
77   
78   from = vlib_frame_vector_args (frame);
79   n_left_from = frame->n_vectors;
80   next_index = node->cached_next_index;
81
82   if (PREDICT_FALSE (vec_len (lm->collectors) == 0))
83     {
84       vlib_node_increment_counter (vm, li_hit_node.index, 
85                                    LI_HIT_ERROR_NO_COLLECTOR, 
86                                    n_left_from);
87     }
88   else
89     {
90       /* The intercept frame... */
91       int_frame = vlib_get_frame_to_node (vm, ip4_lookup_node.index);
92       to_int_next = vlib_frame_vector_args (int_frame);
93     }
94   
95   while (n_left_from > 0)
96     {
97       u32 n_left_to_next;
98
99       vlib_get_next_frame (vm, node, next_index,
100                            to_next, n_left_to_next);
101
102 #if 0
103       while (n_left_from >= 4 && n_left_to_next >= 2)
104         {
105           u32 next0 = LI_HIT_NEXT_INTERFACE_OUTPUT;
106           u32 next1 = LI_HIT_NEXT_INTERFACE_OUTPUT;
107           u32 sw_if_index0, sw_if_index1;
108           u8 tmp0[6], tmp1[6];
109           ethernet_header_t *en0, *en1;
110           u32 bi0, bi1;
111           vlib_buffer_t * b0, * b1;
112           
113           /* Prefetch next iteration. */
114           {
115             vlib_buffer_t * p2, * p3;
116             
117             p2 = vlib_get_buffer (vm, from[2]);
118             p3 = vlib_get_buffer (vm, from[3]);
119             
120             vlib_prefetch_buffer_header (p2, LOAD);
121             vlib_prefetch_buffer_header (p3, LOAD);
122
123             CLIB_PREFETCH (p2->data, CLIB_CACHE_LINE_BYTES, STORE);
124             CLIB_PREFETCH (p3->data, CLIB_CACHE_LINE_BYTES, STORE);
125           }
126
127           /* speculatively enqueue b0 and b1 to the current next frame */
128           to_next[0] = bi0 = from[0];
129           to_next[1] = bi1 = from[1];
130           from += 2;
131           to_next += 2;
132           n_left_from -= 2;
133           n_left_to_next -= 2;
134
135           b0 = vlib_get_buffer (vm, bi0);
136           b1 = vlib_get_buffer (vm, bi1);
137
138           /* $$$$$ Dual loop: process 2 x packets here $$$$$ */
139           ASSERT (b0->current_data == 0);
140           ASSERT (b1->current_data == 0);
141           
142           en0 = vlib_buffer_get_current (b0);
143           en1 = vlib_buffer_get_current (b1);
144
145           sw_if_index0 = vnet_buffer(b0)->sw_if_index[VLIB_RX];
146           sw_if_index1 = vnet_buffer(b1)->sw_if_index[VLIB_RX];
147
148           /* Send pkt back out the RX interface */
149           vnet_buffer(b0)->sw_if_index[VLIB_TX] = sw_if_index0;
150           vnet_buffer(b1)->sw_if_index[VLIB_TX] = sw_if_index1;
151
152           /* $$$$$ End of processing 2 x packets $$$$$ */
153
154           if (PREDICT_FALSE((node->flags & VLIB_NODE_FLAG_TRACE)))
155             {
156               if (b0->flags & VLIB_BUFFER_IS_TRACED) 
157                 {
158                     li_hit_trace_t *t = 
159                       vlib_add_trace (vm, node, b0, sizeof (*t));
160                     t->sw_if_index = sw_if_index0;
161                     t->next_index = next0;
162                   }
163                 if (b1->flags & VLIB_BUFFER_IS_TRACED) 
164                   {
165                     li_hit_trace_t *t = 
166                       vlib_add_trace (vm, node, b1, sizeof (*t));
167                     t->sw_if_index = sw_if_index1;
168                     t->next_index = next1;
169                   }
170               }
171             
172             /* verify speculative enqueues, maybe switch current next frame */
173             vlib_validate_buffer_enqueue_x2 (vm, node, next_index,
174                                              to_next, n_left_to_next,
175                                              bi0, bi1, next0, next1);
176         }
177 #endif /* $$$ dual-loop off */
178
179       while (n_left_from > 0 && n_left_to_next > 0)
180         {
181           u32 bi0;
182           vlib_buffer_t * b0;
183           vlib_buffer_t * c0;
184           ip4_udp_header_t * iu0;
185           ip4_header_t * ip0;
186           udp_header_t * udp0;
187           u32 next0 = LI_HIT_NEXT_ETHERNET;
188
189           /* speculatively enqueue b0 to the current next frame */
190           bi0 = from[0];
191           to_next[0] = bi0;
192           from += 1;
193           to_next += 1;
194           n_left_from -= 1;
195           n_left_to_next -= 1;
196
197           b0 = vlib_get_buffer (vm, bi0);
198           if (PREDICT_TRUE(to_int_next != 0))
199             {
200               /* Make an intercept copy */
201               c0 = vlib_buffer_copy (vm, b0);
202               
203               vlib_buffer_advance(c0, -sizeof(*iu0));
204
205               iu0 = vlib_buffer_get_current(c0);
206               ip0 = &iu0->ip4;
207               
208               ip0->ip_version_and_header_length = 0x45;
209               ip0->ttl = 254;
210               ip0->protocol = IP_PROTOCOL_UDP;
211               
212               ip0->src_address.as_u32 = lm->src_addrs[0].as_u32;
213               ip0->dst_address.as_u32 = lm->collectors[0].as_u32;
214               ip0->length = vlib_buffer_length_in_chain (vm, c0);
215               ip0->checksum = ip4_header_checksum (ip0);
216               
217               udp0 = &iu0->udp;
218               udp0->src_port = udp0->dst_port = 
219                   clib_host_to_net_u16(lm->ports[0]);
220               udp0->checksum = 0;
221               udp0->length = 
222                   clib_net_to_host_u16 (vlib_buffer_length_in_chain (vm , b0));
223               
224               to_int_next [0] = vlib_get_buffer_index (vm, c0);
225               to_int_next++;
226             }
227
228           if (PREDICT_FALSE((node->flags & VLIB_NODE_FLAG_TRACE) 
229                             && (b0->flags & VLIB_BUFFER_IS_TRACED))) 
230             {
231               li_hit_trace_t *t = 
232                  vlib_add_trace (vm, node, b0, sizeof (*t));
233               t->next_index = next0;
234             }
235             
236           /* verify speculative enqueue, maybe switch current next frame */
237           vlib_validate_buffer_enqueue_x1 (vm, node, next_index,
238                                            to_next, n_left_to_next,
239                                            bi0, next0);
240         }
241
242       vlib_put_next_frame (vm, node, next_index, n_left_to_next);
243     }
244
245   if (int_frame)
246     {
247       int_frame->n_vectors = frame->n_vectors;
248       vlib_put_frame_to_node (vm, ip4_lookup_node.index, int_frame);
249     }
250
251   vlib_node_increment_counter (vm, li_hit_node.index, 
252                                LI_HIT_ERROR_HITS, frame->n_vectors);
253   return frame->n_vectors;
254 }
255
256 VLIB_REGISTER_NODE (li_hit_node) = {
257   .function = li_hit_node_fn,
258   .name = "li-hit",
259   .vector_size = sizeof (u32),
260   .format_trace = format_li_hit_trace,
261   .type = VLIB_NODE_TYPE_INTERNAL,
262   
263   .n_errors = ARRAY_LEN(li_hit_error_strings),
264   .error_strings = li_hit_error_strings,
265
266   .n_next_nodes = LI_HIT_N_NEXT,
267
268   /* edit / add dispositions here */
269   .next_nodes = {
270         [LI_HIT_NEXT_ETHERNET] = "ethernet-input-not-l2",
271   },
272 };
273
274 VLIB_NODE_FUNCTION_MULTIARCH (li_hit_node, li_hit_node_fn)
275