2 * node.c - - awkward chained buffer geometry test tool
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:
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 <vnet/pg/pg.h>
20 #include <vppinfra/error.h>
21 #include <oddbuf/oddbuf.h>
30 #ifndef CLIB_MARCH_VARIANT
32 /* packet trace format function */
34 format_oddbuf_trace (u8 * s, va_list * args)
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 *);
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);
45 vlib_node_registration_t oddbuf_node;
47 #endif /* CLIB_MARCH_VARIANT */
49 #define foreach_oddbuf_error \
50 _(SWAPPED, "Mac swap packets processed")
54 #define _(sym,str) ODDBUF_ERROR_##sym,
60 #ifndef CLIB_MARCH_VARIANT
61 static char *oddbuf_error_strings[] = {
62 #define _(sym,string) string,
66 #endif /* CLIB_MARCH_VARIANT */
76 oddbuf_inline (vlib_main_t * vm,
77 vlib_node_runtime_t * node, vlib_frame_t * frame,
78 int is_ip4, int is_trace)
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;
85 u16 nexts[VLIB_FRAME_SIZE], *next;
86 u16 save_current_length;
90 ethernet_header_t *eh;
95 from = vlib_frame_vector_args (frame);
96 n_left_from = frame->n_vectors;
98 vlib_get_buffers (vm, from, bufs, n_left_from);
102 while (n_left_from > 0)
105 vnet_feature_next (&next0, b0);
108 if (vlib_buffer_alloc (vm, &bi, 1) != 1)
110 clib_warning ("Buffer alloc fail, skipping");
114 if (om->first_chunk_offset)
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;
121 eh = vlib_buffer_get_current (b0);
122 ip = (ip4_header_t *) (eh + 1);
123 udp = (udp_header_t *) (ip4_next_header (ip));
127 save_current_length = vlib_buffer_length_in_chain (vm, b0);
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;
134 src = b0->data + b0->current_data + b0->current_length -
136 b0next->current_data = om->second_chunk_offset;
137 b0next->current_length = om->n_to_copy;
138 dst = b0next->data + b0next->current_data;
140 for (i = 0; i < om->n_to_copy; i++)
143 b0->current_length -= om->n_to_copy;
144 b0next->current_length = om->n_to_copy;
146 if (vlib_buffer_length_in_chain (vm, b0) != save_current_length)
147 clib_warning ("OOPS, length incorrect after chunk split...");
151 udp->checksum = ip4_tcp_udp_compute_checksum (vm, b0, ip);
155 if (b[0]->flags & VLIB_BUFFER_IS_TRACED)
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);
171 vlib_buffer_enqueue_to_next (vm, node, from, nexts, frame->n_vectors);
173 return frame->n_vectors;
176 VLIB_NODE_FN (oddbuf_node) (vlib_main_t * vm, vlib_node_runtime_t * node,
177 vlib_frame_t * frame)
179 if (PREDICT_FALSE (node->flags & VLIB_NODE_FLAG_TRACE))
180 return oddbuf_inline (vm, node, frame, 1 /* is_ip4 */ ,
183 return oddbuf_inline (vm, node, frame, 1 /* is_ip4 */ ,
188 #ifndef CLIB_MARCH_VARIANT
189 VLIB_REGISTER_NODE (oddbuf_node) =
192 .vector_size = sizeof (u32),
193 .format_trace = format_oddbuf_trace,
194 .type = VLIB_NODE_TYPE_INTERNAL,
196 .n_errors = ARRAY_LEN(oddbuf_error_strings),
197 .error_strings = oddbuf_error_strings,
199 .n_next_nodes = ODDBUF_N_NEXT,
201 /* edit / add dispositions here */
203 [ODDBUF_NEXT_DROP] = "error-drop",
206 #endif /* CLIB_MARCH_VARIANT */
210 * fd.io coding-style-patch-verification: ON
213 * eval: (c-set-style "gnu")