misc: Purge unused pg includes
[vpp.git] / src / plugins / oddbuf / node.c
1 /*
2  * node.c - - awkward chained buffer geometry test tool
3  *
4  * Copyright (c) 2019 by 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 <oddbuf/oddbuf.h>
21
22 typedef struct
23 {
24   u32 sw_if_index;
25   u32 next_index;
26   u16 udp_checksum;
27 } oddbuf_trace_t;
28
29 #ifndef CLIB_MARCH_VARIANT
30
31 /* packet trace format function */
32 static u8 *
33 format_oddbuf_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   oddbuf_trace_t *t = va_arg (*args, oddbuf_trace_t *);
38
39   s = format (s, "ODDBUF: sw_if_index %d, next index %d, udp checksum %04x\n",
40               t->sw_if_index, t->next_index, (u32) t->udp_checksum);
41   return s;
42 }
43
44 vlib_node_registration_t oddbuf_node;
45
46 #endif /* CLIB_MARCH_VARIANT */
47
48 #define foreach_oddbuf_error \
49 _(SWAPPED, "Mac swap packets processed")
50
51 typedef enum
52 {
53 #define _(sym,str) ODDBUF_ERROR_##sym,
54   foreach_oddbuf_error
55 #undef _
56     ODDBUF_N_ERROR,
57 } oddbuf_error_t;
58
59 #ifndef CLIB_MARCH_VARIANT
60 static char *oddbuf_error_strings[] = {
61 #define _(sym,string) string,
62   foreach_oddbuf_error
63 #undef _
64 };
65 #endif /* CLIB_MARCH_VARIANT */
66
67 typedef enum
68 {
69   ODDBUF_NEXT_DROP,
70   ODDBUF_N_NEXT,
71 } oddbuf_next_t;
72
73
74 always_inline uword
75 oddbuf_inline (vlib_main_t * vm,
76                vlib_node_runtime_t * node, vlib_frame_t * frame,
77                int is_ip4, int is_trace)
78 {
79   oddbuf_main_t *om = &oddbuf_main;
80   u32 n_left_from, *from;
81   vlib_buffer_t *bufs[VLIB_FRAME_SIZE], **b;
82   vlib_buffer_t *b0, *b0next;
83   u32 bi;
84   u16 nexts[VLIB_FRAME_SIZE], *next;
85   u16 save_current_length;
86   u32 next0;
87   u8 *src, *dst;
88   int i;
89   ethernet_header_t *eh;
90   ip4_header_t *ip;
91   udp_header_t *udp;
92
93
94   from = vlib_frame_vector_args (frame);
95   n_left_from = frame->n_vectors;
96
97   vlib_get_buffers (vm, from, bufs, n_left_from);
98   b = bufs;
99   next = nexts;
100
101   while (n_left_from > 0)
102     {
103       b0 = b[0];
104       vnet_feature_next (&next0, b0);
105       nexts[0] = next0;
106
107       if (vlib_buffer_alloc (vm, &bi, 1) != 1)
108         {
109           clib_warning ("Buffer alloc fail, skipping");
110           goto skip;
111         }
112
113       if (om->first_chunk_offset)
114         {
115           memmove (b0->data + b0->current_data + om->first_chunk_offset,
116                    b0->data + b0->current_data, b0->current_length);
117           b0->current_data += om->first_chunk_offset;
118         }
119
120       eh = vlib_buffer_get_current (b0);
121       ip = (ip4_header_t *) (eh + 1);
122       udp = (udp_header_t *) (ip4_next_header (ip));
123
124       if (1)
125         {
126           save_current_length = vlib_buffer_length_in_chain (vm, b0);
127
128           b0next = vlib_get_buffer (vm, bi);
129           b0->flags |= VLIB_BUFFER_NEXT_PRESENT;
130           b0->flags &= ~VLIB_BUFFER_TOTAL_LENGTH_VALID;
131           b0->next_buffer = bi;
132
133           src = b0->data + b0->current_data + b0->current_length -
134             om->n_to_copy;
135           b0next->current_data = om->second_chunk_offset;
136           b0next->current_length = om->n_to_copy;
137           dst = b0next->data + b0next->current_data;
138
139           for (i = 0; i < om->n_to_copy; i++)
140             dst[i] = src[i];
141
142           b0->current_length -= om->n_to_copy;
143           b0next->current_length = om->n_to_copy;
144
145           if (vlib_buffer_length_in_chain (vm, b0) != save_current_length)
146             clib_warning ("OOPS, length incorrect after chunk split...");
147         }
148
149       udp->checksum = 0;
150       udp->checksum = ip4_tcp_udp_compute_checksum (vm, b0, ip);
151
152       if (is_trace)
153         {
154           if (b[0]->flags & VLIB_BUFFER_IS_TRACED)
155             {
156               oddbuf_trace_t *t =
157                 vlib_add_trace (vm, node, b[0], sizeof (*t));
158               t->next_index = next[0];
159               t->sw_if_index = vnet_buffer (b[0])->sw_if_index[VLIB_RX];
160               t->udp_checksum = clib_net_to_host_u16 (udp->checksum);
161             }
162         }
163
164     skip:
165       b += 1;
166       next += 1;
167       n_left_from -= 1;
168     }
169
170   vlib_buffer_enqueue_to_next (vm, node, from, nexts, frame->n_vectors);
171
172   return frame->n_vectors;
173 }
174
175 VLIB_NODE_FN (oddbuf_node) (vlib_main_t * vm, vlib_node_runtime_t * node,
176                             vlib_frame_t * frame)
177 {
178   if (PREDICT_FALSE (node->flags & VLIB_NODE_FLAG_TRACE))
179     return oddbuf_inline (vm, node, frame, 1 /* is_ip4 */ ,
180                           1 /* is_trace */ );
181   else
182     return oddbuf_inline (vm, node, frame, 1 /* is_ip4 */ ,
183                           0 /* is_trace */ );
184 }
185
186 /* *INDENT-OFF* */
187 #ifndef CLIB_MARCH_VARIANT
188 VLIB_REGISTER_NODE (oddbuf_node) =
189 {
190   .name = "oddbuf",
191   .vector_size = sizeof (u32),
192   .format_trace = format_oddbuf_trace,
193   .type = VLIB_NODE_TYPE_INTERNAL,
194
195   .n_errors = ARRAY_LEN(oddbuf_error_strings),
196   .error_strings = oddbuf_error_strings,
197
198   .n_next_nodes = ODDBUF_N_NEXT,
199
200   /* edit / add dispositions here */
201   .next_nodes = {
202         [ODDBUF_NEXT_DROP] = "error-drop",
203   },
204 };
205 #endif /* CLIB_MARCH_VARIANT */
206 /* *INDENT-ON* */
207
208 /*
209  * fd.io coding-style-patch-verification: ON
210  *
211  * Local Variables:
212  * eval: (c-set-style "gnu")
213  * End:
214  */