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