FD.io VPP  v21.06-3-gbb25fbf28
Vector Packet Processing
udp_pg.c
Go to the documentation of this file.
1 /*
2  * Copyright (c) 2015-2019 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  * ip/udp_pg: UDP packet-generator interface
17  *
18  * Copyright (c) 2008 Eliot Dresselhaus
19  *
20  * Permission is hereby granted, free of charge, to any person obtaining
21  * a copy of this software and associated documentation files (the
22  * "Software"), to deal in the Software without restriction, including
23  * without limitation the rights to use, copy, modify, merge, publish,
24  * distribute, sublicense, and/or sell copies of the Software, and to
25  * permit persons to whom the Software is furnished to do so, subject to
26  * the following conditions:
27  *
28  * The above copyright notice and this permission notice shall be
29  * included in all copies or substantial portions of the Software.
30  *
31  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
32  * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
33  * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
34  * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
35  * LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
36  * OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
37  * WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
38  */
39 
40 #include <vnet/pg/pg.h>
41 #include <vnet/ip/ip.h> /* for unformat_udp_udp_port */
42 #include <vnet/udp/udp.h>
43 
44 #define UDP_PG_EDIT_LENGTH (1 << 0)
45 #define UDP_PG_EDIT_CHECKSUM (1 << 1)
46 
47 always_inline void
49  pg_stream_t * s,
50  pg_edit_group_t * g,
51  u32 * packets, u32 n_packets, u32 flags)
52 {
54  u32 ip_offset, udp_offset;
55 
56  udp_offset = g->start_byte_offset;
57  ip_offset = (g - 1)->start_byte_offset;
58 
59  while (n_packets >= 1)
60  {
61  vlib_buffer_t *p0;
62  ip4_header_t *ip0;
63  udp_header_t *udp0;
64  u32 udp_len0;
65 
66  p0 = vlib_get_buffer (vm, packets[0]);
67  n_packets -= 1;
68  packets += 1;
69 
70  ip0 = (void *) (p0->data + ip_offset);
71  udp0 = (void *) (p0->data + udp_offset);
72  udp_len0 = vlib_buffer_length_in_chain (vm, p0) - udp_offset;
73 
75  udp0->length = clib_host_to_net_u16 (udp_len0);
76  else
77  udp_len0 = clib_host_to_net_u16 (udp0->length);
78 
79  /* Initialize checksum with header. */
81  {
82  ip_csum_t sum0;
83 
84  sum0 = clib_mem_unaligned (&ip0->src_address, u64);
85 
86  sum0 = ip_csum_with_carry
87  (sum0, clib_host_to_net_u32 (udp_len0 + (ip0->protocol << 16)));
88 
89  /* Invalidate possibly old checksum. */
90  udp0->checksum = 0;
91 
92  sum0 =
93  ip_incremental_checksum_buffer (vm, p0, udp_offset, udp_len0,
94  sum0);
95 
96  sum0 = ~ip_csum_fold (sum0);
97 
98  /* Zero checksum means checksumming disabled. */
99  sum0 = sum0 != 0 ? sum0 : 0xffff;
100 
101  udp0->checksum = sum0;
102  }
103  }
104 }
105 
106 static void
108  pg_stream_t * s,
109  pg_edit_group_t * g, u32 * packets, u32 n_packets)
110 {
111  switch (g->edit_function_opaque)
112  {
113  case UDP_PG_EDIT_LENGTH:
114  udp_pg_edit_function_inline (pg, s, g, packets, n_packets,
116  break;
117 
119  udp_pg_edit_function_inline (pg, s, g, packets, n_packets,
121  break;
122 
124  udp_pg_edit_function_inline (pg, s, g, packets, n_packets,
126  break;
127 
128  default:
129  ASSERT (0);
130  break;
131  }
132 }
133 
134 typedef struct
135 {
140 
141 static inline void
143 {
144  /* Initialize fields that are not bit fields in the IP header. */
145 #define _(f) pg_edit_init (&p->f, udp_header_t, f);
146  _(src_port);
147  _(dst_port);
148  _(length);
149  _(checksum);
150 #undef _
151 }
152 
153 uword
154 unformat_pg_udp_header (unformat_input_t * input, va_list * args)
155 {
156  pg_stream_t *s = va_arg (*args, pg_stream_t *);
157  pg_udp_header_t *p;
158  u32 group_index;
159 
160  p = pg_create_edit_group (s, sizeof (p[0]), sizeof (udp_header_t),
161  &group_index);
162  pg_udp_header_init (p);
163 
164  /* Defaults. */
167 
168  if (!unformat (input, "UDP: %U -> %U",
172  goto error;
173 
174  /* Parse options. */
175  while (1)
176  {
177  if (unformat (input, "length %U",
179  ;
180 
181  else if (unformat (input, "checksum %U",
183  ;
184 
185  /* Can't parse input: try next protocol level. */
186  else
187  break;
188  }
189 
190  {
191  ip_main_t *im = &ip_main;
192  u16 dst_port;
194 
195  /* For the pg format of applications over UDP local */
196  udp_dst_port_info_t *pi2 = NULL;
197 
198  pi = 0;
199  if (p->dst_port.type == PG_EDIT_FIXED)
200  {
204  if (!pi2)
206  }
207 
208  if (pi && pi->unformat_pg_edit
209  && unformat_user (input, pi->unformat_pg_edit, s))
210  ;
211  else if (pi2 && pi2->unformat_pg_edit
212  && unformat_user (input, pi2->unformat_pg_edit, s))
213  ;
214  else if (!unformat_user (input, unformat_pg_payload, s))
215  goto error;
216 
217  p = pg_get_edit_group (s, group_index);
220  {
221  pg_edit_group_t *g = pg_stream_get_group (s, group_index);
223  g->edit_function_opaque = 0;
226  if (p->length.type == PG_EDIT_UNSPECIFIED)
228  }
229 
230  return 1;
231  }
232 
233 error:
234  /* Free up any edits we may have added. */
235  pg_free_edit_group (s);
236  return 0;
237 }
238 
239 
240 /*
241  * fd.io coding-style-patch-verification: ON
242  *
243  * Local Variables:
244  * eval: (c-set-style "gnu")
245  * End:
246  */
im
vnet_interface_main_t * im
Definition: interface_output.c:395
PG_EDIT_FIXED
@ PG_EDIT_FIXED
Definition: edit.h:52
udp_header_t::length
u16 length
Definition: udp_packet.h:51
udp_pg_edit_function
static void udp_pg_edit_function(pg_main_t *pg, pg_stream_t *s, pg_edit_group_t *g, u32 *packets, u32 n_packets)
Definition: udp_pg.c:107
unformat_user
uword unformat_user(unformat_input_t *input, unformat_function_t *func,...)
Definition: unformat.c:989
udp_dst_port_info_t
Definition: udp.h:69
pg_edit_group_t::start_byte_offset
u32 start_byte_offset
Definition: pg.h:69
dst_port
vl_api_ip_port_and_mask_t dst_port
Definition: flow_types.api:92
pg.h
vlib_get_buffer
static vlib_buffer_t * vlib_get_buffer(vlib_main_t *vm, u32 buffer_index)
Translate buffer index into buffer pointer.
Definition: buffer_funcs.h:111
UDP_PG_EDIT_CHECKSUM
#define UDP_PG_EDIT_CHECKSUM
Definition: udp_pg.c:45
PG_EDIT_UNSPECIFIED
@ PG_EDIT_UNSPECIFIED
Definition: edit.h:61
ip_main
ip_main_t ip_main
Definition: ip_init.c:42
u16
unsigned short u16
Definition: types.h:57
ip_incremental_checksum_buffer
static ip_csum_t ip_incremental_checksum_buffer(vlib_main_t *vm, vlib_buffer_t *first_buffer, u32 first_buffer_offset, u32 n_bytes_to_checksum, ip_csum_t sum)
Definition: ip.h:152
pg_udp_header_t::dst_port
pg_edit_t dst_port
Definition: udp_pg.c:136
vm
vlib_main_t * vm
X-connect all packets from the HOST to the PHY.
Definition: nat44_ei.c:3047
unformat_pg_number
uword unformat_pg_number(unformat_input_t *input, va_list *args)
Definition: edit.c:85
unformat_input_t
struct _unformat_input_t unformat_input_t
vlib_buffer_length_in_chain
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:433
udp_header_t
Definition: udp_packet.h:45
ip4_header_t
Definition: ip4_packet.h:87
error
Definition: cJSON.c:88
udp_dst_port_info_t::unformat_pg_edit
unformat_function_t * unformat_pg_edit
Definition: udp.h:87
pg_udp_header_t::checksum
pg_edit_t checksum
Definition: udp_pg.c:138
unformat
uword unformat(unformat_input_t *i, const char *fmt,...)
Definition: unformat.c:978
UDP_IP4
@ UDP_IP4
Definition: udp.h:93
pg_edit_group_t::edit_function_opaque
uword edit_function_opaque
Definition: pg.h:81
pg_edit_group_t
Definition: pg.h:56
PG_EDIT_LO
#define PG_EDIT_LO
Definition: edit.h:83
unformat_pg_udp_header
uword unformat_pg_udp_header(unformat_input_t *input, va_list *args)
Definition: udp_pg.c:154
pg_edit_t::type
pg_edit_type_t type
Definition: edit.h:66
uword
u64 uword
Definition: types.h:112
UDP_PG_EDIT_LENGTH
#define UDP_PG_EDIT_LENGTH
Definition: udp_pg.c:44
pg_get_edit_group
static void * pg_get_edit_group(pg_stream_t *s, u32 group_index)
Definition: pg.h:273
clib_mem_unaligned
#define clib_mem_unaligned(pointer, type)
Definition: types.h:155
pg_udp_header_t::length
pg_edit_t length
Definition: udp_pg.c:137
src_port
vl_api_ip_port_and_mask_t src_port
Definition: flow_types.api:91
pg_edit_get_value
static u64 pg_edit_get_value(pg_edit_t *e, int hi_or_lo)
Definition: edit.h:173
UDP_IP6
@ UDP_IP6
Definition: udp.h:92
pg_stream_get_group
static pg_edit_group_t * pg_stream_get_group(pg_stream_t *s, u32 group_index)
Definition: pg.h:233
pg_free_edit_group
static void pg_free_edit_group(pg_stream_t *s)
Definition: pg.h:292
unformat_pg_edit
uword unformat_pg_edit(unformat_input_t *input, va_list *args)
Definition: edit.c:106
ip_get_tcp_udp_port_info
static tcp_udp_port_info_t * ip_get_tcp_udp_port_info(ip_main_t *im, u32 port)
Definition: ip.h:143
udp_header_t::checksum
u16 checksum
Definition: udp_packet.h:55
unformat_pg_payload
uword unformat_pg_payload(unformat_input_t *input, va_list *args)
Definition: edit.c:127
unformat_tcp_udp_port
unformat_function_t unformat_tcp_udp_port
Definition: format.h:49
always_inline
#define always_inline
Definition: rdma_mlx5dv.h:23
ip4_header_t::src_address
ip4_address_t src_address
Definition: ip4_packet.h:125
udp_pg_edit_function_inline
static void udp_pg_edit_function_inline(pg_main_t *pg, pg_stream_t *s, pg_edit_group_t *g, u32 *packets, u32 n_packets, u32 flags)
Definition: udp_pg.c:48
pg_udp_header_init
static void pg_udp_header_init(pg_udp_header_t *p)
Definition: udp_pg.c:142
tcp_udp_port_info_t
Definition: ip.h:92
pg_main_t
Definition: pg.h:330
pg_udp_header_t
Definition: udp_pg.c:134
u64
unsigned long u64
Definition: types.h:89
udp_main
udp_main_t udp_main
Definition: udp.c:23
ASSERT
#define ASSERT(truth)
Definition: error_bootstrap.h:69
pg_create_edit_group
static void * pg_create_edit_group(pg_stream_t *s, int n_edit_bytes, int n_packet_bytes, u32 *group_index)
Definition: pg.h:239
ip_csum_with_carry
static ip_csum_t ip_csum_with_carry(ip_csum_t sum, ip_csum_t x)
Definition: ip_packet.h:248
ip.h
u32
unsigned int u32
Definition: types.h:88
udp.h
pg_udp_header_t::src_port
pg_edit_t src_port
Definition: udp_pg.c:136
length
char const int length
Definition: cJSON.h:163
udp_get_dst_port_info
static udp_dst_port_info_t * udp_get_dst_port_info(udp_main_t *um, udp_dst_port_t dst_port, u8 is_ip4)
Definition: udp.h:228
vlib_main_t
Definition: main.h:102
vlib_get_main
static vlib_main_t * vlib_get_main(void)
Definition: global_funcs.h:38
ip_csum_t
uword ip_csum_t
Definition: ip_packet.h:245
vlib_buffer_t::data
u8 data[]
Packet data.
Definition: buffer.h:204
tcp_udp_port_info_t::unformat_pg_edit
unformat_function_t * unformat_pg_edit
Definition: ip.h:104
pg_edit_t
Definition: edit.h:64
pg_stream_t
Definition: pg.h:96
ip_main_t
Definition: ip.h:107
pg_edit_group_t::edit_function
void(* edit_function)(struct pg_main_t *pg, struct pg_stream_t *s, struct pg_edit_group_t *g, u32 *buffers, u32 n_buffers)
Definition: pg.h:75
ip_csum_fold
static u16 ip_csum_fold(ip_csum_t c)
Definition: ip_packet.h:301
ip4_header_t::protocol
u8 protocol
Definition: ip4_packet.h:115
vlib_buffer_t
VLIB buffer representation.
Definition: buffer.h:111
packets
units packets
Definition: map.api:366
flags
vl_api_wireguard_peer_flags_t flags
Definition: wireguard.api:105