FD.io VPP  v21.01.1
Vector Packet Processing
node.c
Go to the documentation of this file.
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 {
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 
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,
55 #undef _
58 
59 #ifndef CLIB_MARCH_VARIANT
60 static char *oddbuf_error_strings[] = {
61 #define _(sym,string) string,
63 #undef _
64 };
65 #endif /* CLIB_MARCH_VARIANT */
66 
67 typedef enum
68 {
72 
73 
77  int is_ip4, int is_trace)
78 {
80  u32 n_left_from, *from;
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;
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 
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
189 {
190  .name = "oddbuf",
191  .vector_size = sizeof (u32),
192  .format_trace = format_oddbuf_trace,
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  */
u32 flags
buffer flags: VLIB_BUFFER_FREE_LIST_INDEX_MASK: bits used to store free list index, VLIB_BUFFER_IS_TRACED: trace this buffer.
Definition: buffer.h:124
#define CLIB_UNUSED(x)
Definition: clib.h:87
vl_api_wireguard_peer_flags_t flags
Definition: wireguard.api:105
i16 current_data
signed offset in data[], pre_data[] that we are currently processing.
Definition: buffer.h:110
u16 current_length
Nbytes between current data and the end of this buffer.
Definition: buffer.h:113
vl_api_address_t src
Definition: gre.api:54
#define foreach_oddbuf_error
Definition: node.c:48
vlib_main_t * vm
Definition: in2out_ed.c:1580
#define VLIB_NODE_FN(node)
Definition: node.h:203
static uword vlib_buffer_length_in_chain(vlib_main_t *vm, vlib_buffer_t *b)
Get length in bytes of the buffer chain.
Definition: buffer_funcs.h:402
unsigned char u8
Definition: types.h:56
oddbuf_error_t
Definition: node.c:51
description fragment has unexpected format
Definition: map.api:433
const cJSON *const b
Definition: cJSON.h:255
static u8 * format_oddbuf_trace(u8 *s, va_list *args)
Definition: node.c:33
static void * ip4_next_header(ip4_header_t *i)
Definition: ip4_packet.h:196
oddbuf_next_t
Definition: node.c:67
unsigned int u32
Definition: types.h:88
#define VLIB_FRAME_SIZE
Definition: node.h:378
vl_api_fib_path_type_t type
Definition: fib_types.api:123
static __clib_warn_unused_result u32 vlib_buffer_alloc(vlib_main_t *vm, u32 *buffers, u32 n_buffers)
Allocate buffers into supplied array.
Definition: buffer_funcs.h:677
unsigned short u16
Definition: types.h:57
static void * vlib_buffer_get_current(vlib_buffer_t *b)
Get pointer to current data to process.
Definition: buffer.h:233
#define PREDICT_FALSE(x)
Definition: clib.h:121
#define always_inline
Definition: ipsec.h:28
int second_chunk_offset
Definition: oddbuf.h:38
vl_api_address_t dst
Definition: gre.api:55
int first_chunk_offset
Definition: oddbuf.h:39
u32 sw_if_index
Definition: node.c:24
#define VLIB_REGISTER_NODE(x,...)
Definition: node.h:170
u16 n_vectors
Definition: node.h:397
static_always_inline void vlib_buffer_enqueue_to_next(vlib_main_t *vm, vlib_node_runtime_t *node, u32 *buffers, u16 *nexts, uword count)
Definition: buffer_node.h:339
sll srl srl sll sra u16x4 i
Definition: vector_sse42.h:317
static_always_inline void vnet_feature_next(u32 *next0, vlib_buffer_t *b0)
Definition: feature.h:322
#define clib_warning(format, args...)
Definition: error.h:59
u8 data[]
Packet data.
Definition: buffer.h:181
static uword oddbuf_inline(vlib_main_t *vm, vlib_node_runtime_t *node, vlib_frame_t *frame, int is_ip4, int is_trace)
Definition: node.c:75
#define ARRAY_LEN(x)
Definition: clib.h:67
vlib_main_t vlib_node_runtime_t * node
Definition: in2out_ed.c:1580
u16 ip4_tcp_udp_compute_checksum(vlib_main_t *vm, vlib_buffer_t *p0, ip4_header_t *ip0)
Definition: ip4_forward.c:1328
u32 next_index
Definition: node.c:25
struct _vlib_node_registration vlib_node_registration_t
vl_api_address_t ip
Definition: l2.api:501
static char * oddbuf_error_strings[]
Definition: node.c:60
u32 next_buffer
Next buffer for this linked-list of buffers.
Definition: buffer.h:140
vlib_main_t vlib_node_runtime_t vlib_frame_t * frame
Definition: in2out_ed.c:1581
VLIB buffer representation.
Definition: buffer.h:102
u64 uword
Definition: types.h:112
static void * vlib_frame_vector_args(vlib_frame_t *f)
Get pointer to frame vector data.
Definition: node_funcs.h:297
#define vnet_buffer(b)
Definition: buffer.h:417
vlib_node_registration_t oddbuf_node
(constructor) VLIB_REGISTER_NODE (oddbuf_node)
Definition: node.c:44
u16 udp_checksum
Definition: node.c:26
void * vlib_add_trace(vlib_main_t *vm, vlib_node_runtime_t *r, vlib_buffer_t *b, u32 n_data_bytes)
Definition: trace.c:634
static_always_inline void vlib_get_buffers(vlib_main_t *vm, u32 *bi, vlib_buffer_t **b, int count)
Translate array of buffer indices into buffer pointers.
Definition: buffer_funcs.h:280
#define VLIB_NODE_FLAG_TRACE
Definition: node.h:302
static vlib_buffer_t * vlib_get_buffer(vlib_main_t *vm, u32 buffer_index)
Translate buffer index into buffer pointer.
Definition: buffer_funcs.h:85
Definition: defs.h:46
oddbuf_main_t oddbuf_main
Definition: oddbuf.c:34
int n_to_copy
Definition: oddbuf.h:37