FD.io VPP  v16.06
Vector Packet Processing
udp_pg.c
Go to the documentation of this file.
1 /*
2  * Copyright (c) 2015 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 
43 #define UDP_PG_EDIT_LENGTH (1 << 0)
44 #define UDP_PG_EDIT_CHECKSUM (1 << 1)
45 
46 always_inline void
48  pg_stream_t * s,
49  pg_edit_group_t * g,
50  u32 * packets,
51  u32 n_packets,
52  u32 flags)
53 {
54  vlib_main_t * vm = pg->vlib_main;
55  u32 ip_offset, udp_offset;
56 
57  udp_offset = g->start_byte_offset;
58  ip_offset = (g-1)->start_byte_offset;
59 
60  while (n_packets >= 1)
61  {
62  vlib_buffer_t * p0;
63  ip4_header_t * ip0;
64  udp_header_t * udp0;
65  u32 udp_len0;
66 
67  p0 = vlib_get_buffer (vm, packets[0]);
68  n_packets -= 1;
69  packets += 1;
70 
71  ip0 = (void *) (p0->data + ip_offset);
72  udp0 = (void *) (p0->data + udp_offset);
73  udp_len0 = clib_net_to_host_u16 (ip0->length) - sizeof (ip0[0]);
74 
75  if (flags & UDP_PG_EDIT_LENGTH)
76  udp0->length =
77  clib_net_to_host_u16 (vlib_buffer_length_in_chain (vm, p0)
78  - ip_offset);
79 
80  /* Initialize checksum with header. */
81  if (flags & UDP_PG_EDIT_CHECKSUM)
82  {
83  ip_csum_t sum0;
84 
85  sum0 = clib_mem_unaligned (&ip0->src_address, u64);
86 
87  sum0 = ip_csum_with_carry
88  (sum0, clib_host_to_net_u32 (udp_len0 + (ip0->protocol << 16)));
89 
90  /* Invalidate possibly old checksum. */
91  udp0->checksum = 0;
92 
93  sum0 = ip_incremental_checksum_buffer (vm, p0, udp_offset, udp_len0, sum0);
94 
95  sum0 = ~ ip_csum_fold (sum0);
96 
97  /* Zero checksum means checksumming disabled. */
98  sum0 = sum0 != 0 ? sum0 : 0xffff;
99 
100  udp0->checksum = sum0;
101  }
102  }
103 }
104 
105 static void
107  pg_stream_t * s,
108  pg_edit_group_t * g,
109  u32 * packets,
110  u32 n_packets)
111 {
112  switch (g->edit_function_opaque)
113  {
114  case UDP_PG_EDIT_LENGTH:
115  udp_pg_edit_function_inline (pg, s, g, packets, n_packets,
117  break;
118 
120  udp_pg_edit_function_inline (pg, s, g, packets, n_packets,
122  break;
123 
125  udp_pg_edit_function_inline (pg, s, g, packets, n_packets,
127  break;
128 
129  default:
130  ASSERT (0);
131  break;
132  }
133 }
134 
135 typedef struct {
136  pg_edit_t src_port, dst_port;
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",
173  goto error;
174 
175  /* Parse options. */
176  while (1)
177  {
178  if (unformat (input, "length %U",
181  ;
182 
183  else if (unformat (input, "checksum %U",
186  ;
187 
188  /* Can't parse input: try next protocol level. */
189  else
190  break;
191  }
192 
193  {
194  ip_main_t * im = &ip_main;
195  u16 dst_port;
196  tcp_udp_port_info_t * pi;
197 
198  pi = 0;
199  if (p->dst_port.type == PG_EDIT_FIXED)
200  {
201  dst_port = pg_edit_get_value (&p->dst_port, PG_EDIT_LO);
202  pi = ip_get_tcp_udp_port_info (im, dst_port);
203  }
204 
205  if (pi && pi->unformat_pg_edit
206  && unformat_user (input, pi->unformat_pg_edit, s))
207  ;
208 
209  else if (! unformat_user (input, unformat_pg_payload, s))
210  goto error;
211 
212  p = pg_get_edit_group (s, group_index);
215  {
216  pg_edit_group_t * g = pg_stream_get_group (s, group_index);
218  g->edit_function_opaque = 0;
221  if (p->length.type == PG_EDIT_UNSPECIFIED)
223  }
224 
225  return 1;
226  }
227 
228  error:
229  /* Free up any edits we may have added. */
230  pg_free_edit_group (s);
231  return 0;
232 }
233 
Definition: edit.h:63
#define UDP_PG_EDIT_CHECKSUM
Definition: udp_pg.c:44
#define PG_EDIT_LO
Definition: edit.h:81
uword unformat(unformat_input_t *i, char *fmt,...)
Definition: unformat.c:942
ip4_address_t src_address
Definition: ip4_packet.h:138
Definition: pg.h:293
pg_edit_t checksum
Definition: udp_pg.c:138
uword unformat_pg_edit(unformat_input_t *input, va_list *args)
Definition: edit.c:106
uword ip_csum_t
Definition: ip_packet.h:86
always_inline 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:153
Definition: ip.h:108
u32 start_byte_offset
Definition: pg.h:62
always_inline pg_edit_group_t * pg_stream_get_group(pg_stream_t *s, u32 group_index)
Definition: pg.h:210
#define always_inline
Definition: clib.h:84
uword unformat_pg_payload(unformat_input_t *input, va_list *args)
Definition: edit.c:127
pg_edit_type_t type
Definition: edit.h:64
#define UDP_PG_EDIT_LENGTH
Definition: udp_pg.c:43
always_inline 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:112
unsigned long u64
Definition: types.h:89
uword unformat_user(unformat_input_t *input, unformat_function_t *func,...)
Definition: unformat.c:953
always_inline u16 ip_csum_fold(ip_csum_t c)
Definition: ip_packet.h:138
pg_edit_t src_port
Definition: udp_pg.c:136
static u64 pg_edit_get_value(pg_edit_t *e, int hi_or_lo)
Definition: edit.h:169
always_inline void * pg_create_edit_group(pg_stream_t *s, int n_edit_bytes, int n_packet_bytes, u32 *group_index)
Definition: pg.h:214
always_inline void * pg_get_edit_group(pg_stream_t *s, u32 group_index)
Definition: pg.h:250
uword unformat_pg_udp_header(unformat_input_t *input, va_list *args)
Definition: udp_pg.c:154
ip_main_t ip_main
Definition: ip_init.c:42
pg_edit_t dst_port
Definition: udp_pg.c:136
static tcp_udp_port_info_t * ip_get_tcp_udp_port_info(ip_main_t *im, u32 port)
Definition: ip.h:144
#define ASSERT(truth)
vlib_main_t * vlib_main
Definition: pg.h:295
unsigned int u32
Definition: types.h:88
unformat_function_t * unformat_pg_edit
Definition: ip.h:105
static void pg_udp_header_init(pg_udp_header_t *p)
Definition: udp_pg.c:142
uword edit_function_opaque
Definition: pg.h:75
Definition: pg.h:91
always_inline 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:47
always_inline ip_csum_t ip_csum_with_carry(ip_csum_t sum, ip_csum_t x)
Definition: ip_packet.h:89
u64 uword
Definition: types.h:112
unsigned short u16
Definition: types.h:57
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:68
#define clib_mem_unaligned(pointer, type)
Definition: types.h:153
uword unformat_pg_number(unformat_input_t *input, va_list *args)
Definition: edit.c:84
u8 data[0]
Packet data.
Definition: buffer.h:150
pg_edit_t length
Definition: udp_pg.c:137
struct _unformat_input_t unformat_input_t
unformat_function_t unformat_tcp_udp_port
Definition: format.h:49
u32 flags
Definition: vhost-user.h:73
always_inline void pg_free_edit_group(pg_stream_t *s)
Definition: pg.h:269
always_inline vlib_buffer_t * vlib_get_buffer(vlib_main_t *vm, u32 buffer_index)
Translate buffer index into buffer pointer.
Definition: buffer_funcs.h:69
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:106