FD.io VPP  v17.04-9-g99c0734
Vector Packet Processing
tcp_pg.c
Go to the documentation of this file.
1 /*
2  * Copyright (c) 2016 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/tcp_pg: TCP 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/ip/ip.h>
41 #include <vnet/pg/pg.h>
42 
43 /* TCP flags bit 0 first. */
44 #define foreach_tcp_flag \
45  _ (FIN) \
46  _ (SYN) \
47  _ (RST) \
48  _ (PSH) \
49  _ (ACK) \
50  _ (URG) \
51  _ (ECE) \
52  _ (CWR)
53 
54 static void
56  pg_stream_t * s,
57  pg_edit_group_t * g,
58  u32 * packets,
59  u32 n_packets)
60 {
62  u32 ip_offset, tcp_offset;
63 
64  tcp_offset = g->start_byte_offset;
65  ip_offset = (g-1)->start_byte_offset;
66 
67  while (n_packets >= 1)
68  {
69  vlib_buffer_t * p0;
70  ip4_header_t * ip0;
71  tcp_header_t * tcp0;
72  ip_csum_t sum0;
73  u32 tcp_len0;
74 
75  p0 = vlib_get_buffer (vm, packets[0]);
76  n_packets -= 1;
77  packets += 1;
78 
79  ASSERT (p0->current_data == 0);
80  ip0 = (void *) (p0->data + ip_offset);
81  tcp0 = (void *) (p0->data + tcp_offset);
82  tcp_len0 = clib_net_to_host_u16 (ip0->length) - sizeof (ip0[0]);
83 
84  /* Initialize checksum with header. */
85  if (BITS (sum0) == 32)
86  {
87  sum0 = clib_mem_unaligned (&ip0->src_address, u32);
88  sum0 = ip_csum_with_carry (sum0, clib_mem_unaligned (&ip0->dst_address, u32));
89  }
90  else
91  sum0 = clib_mem_unaligned (&ip0->src_address, u64);
92 
93  sum0 = ip_csum_with_carry
94  (sum0, clib_host_to_net_u32 (tcp_len0 + (ip0->protocol << 16)));
95 
96  /* Invalidate possibly old checksum. */
97  tcp0->checksum = 0;
98 
99  sum0 = ip_incremental_checksum_buffer (vm, p0, tcp_offset, tcp_len0, sum0);
100 
101  tcp0->checksum = ~ ip_csum_fold (sum0);
102  }
103 }
104 
105 typedef struct {
107  pg_edit_t seq_number, ack_number;
109 #define _(f) pg_edit_t f##_flag;
111 #undef _
116 
117 static inline void
119 {
120  /* Initialize fields that are not bit fields in the IP header. */
121 #define _(f) pg_edit_init (&p->f, tcp_header_t, f);
122  _ (src);
123  _ (dst);
124  _ (seq_number);
125  _ (ack_number);
126  _ (window);
127  _ (checksum);
128  _ (urgent_pointer);
129 #undef _
130 
131  /* Initialize bit fields. */
132 #define _(f) \
133  pg_edit_init_bitfield (&p->f##_flag, tcp_header_t, \
134  flags, \
135  TCP_FLAG_BIT_##f, 1);
136 
138 #undef _
139 
141  data_offset_and_reserved,
142  4, 4);
143 }
144 
145 uword
146 unformat_pg_tcp_header (unformat_input_t * input, va_list * args)
147 {
148  pg_stream_t * s = va_arg (*args, pg_stream_t *);
149  pg_tcp_header_t * p;
150  u32 group_index;
151 
152  p = pg_create_edit_group (s, sizeof (p[0]), sizeof (tcp_header_t),
153  &group_index);
154  pg_tcp_header_init (p);
155 
156  /* Defaults. */
157  pg_edit_set_fixed (&p->seq_number, 0);
158  pg_edit_set_fixed (&p->ack_number, 0);
159 
161  sizeof (tcp_header_t) / sizeof (u32));
162 
163  pg_edit_set_fixed (&p->window, 4096);
165 
166 #define _(f) pg_edit_set_fixed (&p->f##_flag, 0);
168 #undef _
169 
171 
172  if (! unformat (input, "TCP: %U -> %U",
177  goto error;
178 
179  /* Parse options. */
180  while (1)
181  {
182  if (unformat (input, "window %U",
185  ;
186 
187  else if (unformat (input, "checksum %U",
190  ;
191 
192  /* Flags. */
193 #define _(f) else if (unformat (input, #f)) pg_edit_set_fixed (&p->f##_flag, 1);
195 #undef _
196 
197  /* Can't parse input: try next protocol level. */
198  else
199  break;
200  }
201 
202  {
203  ip_main_t * im = &ip_main;
204  u16 dst_port;
205  tcp_udp_port_info_t * pi;
206 
207  pi = 0;
208  if (p->dst.type == PG_EDIT_FIXED)
209  {
210  dst_port = pg_edit_get_value (&p->dst, PG_EDIT_LO);
211  pi = ip_get_tcp_udp_port_info (im, dst_port);
212  }
213 
214  if (pi && pi->unformat_pg_edit
215  && unformat_user (input, pi->unformat_pg_edit, s))
216  ;
217 
218  else if (! unformat_user (input, unformat_pg_payload, s))
219  goto error;
220 
222  {
223  pg_edit_group_t * g = pg_stream_get_group (s, group_index);
225  g->edit_function_opaque = 0;
226  }
227 
228  return 1;
229  }
230 
231  error:
232  /* Free up any edits we may have added. */
233  pg_free_edit_group (s);
234  return 0;
235 }
236 
Definition: edit.h:64
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:151
pg_edit_t data_offset_and_reserved
Definition: tcp_pg.c:108
#define PG_EDIT_LO
Definition: edit.h:83
ip4_address_t src_address
Definition: ip4_packet.h:163
Definition: pg.h:310
static void pg_edit_set_fixed(pg_edit_t *e, u64 value)
Definition: edit.h:153
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:73
#define foreach_tcp_flag
Definition: tcp_pg.c:44
pg_edit_t dst
Definition: tcp_pg.c:106
uword unformat_user(unformat_input_t *input, unformat_function_t *func,...)
Definition: unformat.c:982
uword unformat_pg_edit(unformat_input_t *input, va_list *args)
Definition: edit.c:106
uword ip_csum_t
Definition: ip_packet.h:90
static ip_csum_t ip_csum_with_carry(ip_csum_t sum, ip_csum_t x)
Definition: ip_packet.h:93
Definition: ip.h:106
struct _tcp_header tcp_header_t
u32 start_byte_offset
Definition: pg.h:67
static pg_edit_group_t * pg_stream_get_group(pg_stream_t *s, u32 group_index)
Definition: pg.h:219
i16 current_data
signed offset in data[], pre_data[] that we are currently processing.
Definition: buffer.h:67
ip4_address_t dst_address
Definition: ip4_packet.h:163
uword unformat_pg_payload(unformat_input_t *input, va_list *args)
Definition: edit.c:127
pg_edit_type_t type
Definition: edit.h:66
foreach_tcp_flag pg_edit_t window
Definition: tcp_pg.c:112
pg_edit_t seq_number
Definition: tcp_pg.c:107
unsigned long u64
Definition: types.h:89
static void * pg_create_edit_group(pg_stream_t *s, int n_edit_bytes, int n_packet_bytes, u32 *group_index)
Definition: pg.h:225
pg_edit_t urgent_pointer
Definition: tcp_pg.c:114
struct _unformat_input_t unformat_input_t
static u64 pg_edit_get_value(pg_edit_t *e, int hi_or_lo)
Definition: edit.h:173
pg_edit_t src
Definition: tcp_pg.c:106
#define pg_edit_init_bitfield(e, type, field, field_offset, field_n_bits)
Definition: edit.h:98
static void tcp_pg_edit_function(pg_main_t *pg, pg_stream_t *s, pg_edit_group_t *g, u32 *packets, u32 n_packets)
Definition: tcp_pg.c:55
vlib_main_t * vm
Definition: buffer.c:276
ip_main_t ip_main
Definition: ip_init.c:42
static tcp_udp_port_info_t * ip_get_tcp_udp_port_info(ip_main_t *im, u32 port)
Definition: ip.h:142
#define ASSERT(truth)
unsigned int u32
Definition: types.h:88
unformat_function_t * unformat_pg_edit
Definition: ip.h:103
static void pg_free_edit_group(pg_stream_t *s)
Definition: pg.h:278
uword edit_function_opaque
Definition: pg.h:79
Definition: pg.h:96
static vlib_main_t * vlib_get_main(void)
Definition: global_funcs.h:23
unformat_function_t unformat_tcp_udp_port
Definition: format.h:49
u64 uword
Definition: types.h:112
unsigned short u16
Definition: types.h:57
pg_edit_t checksum
Definition: tcp_pg.c:113
pg_edit_t ack_number
Definition: tcp_pg.c:107
uword unformat_pg_tcp_header(unformat_input_t *input, va_list *args)
Definition: tcp_pg.c:146
#define clib_mem_unaligned(pointer, type)
Definition: types.h:155
uword unformat_pg_number(unformat_input_t *input, va_list *args)
Definition: edit.c:85
u8 data[0]
Packet data.
Definition: buffer.h:152
static void pg_tcp_header_init(pg_tcp_header_t *p)
Definition: tcp_pg.c:118
#define BITS(x)
Definition: clib.h:58
static vlib_buffer_t * vlib_get_buffer(vlib_main_t *vm, u32 buffer_index)
Translate buffer index into buffer pointer.
Definition: buffer_funcs.h:57
static u16 ip_csum_fold(ip_csum_t c)
Definition: ip_packet.h:145
uword unformat(unformat_input_t *i, const char *fmt,...)
Definition: unformat.c:971