Initial commit of vpp code.
[vpp.git] / vnet / vnet / ip / tcp_pg.c
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/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 static void
44 tcp_pg_edit_function (pg_main_t * pg,
45                       pg_stream_t * s,
46                       pg_edit_group_t * g,
47                       u32 * packets,
48                       u32 n_packets)
49 {
50   vlib_main_t * vm = pg->vlib_main;
51   u32 ip_offset, tcp_offset;
52
53   tcp_offset = g->start_byte_offset;
54   ip_offset = (g-1)->start_byte_offset;
55
56   while (n_packets >= 1)
57     {
58       vlib_buffer_t * p0;
59       ip4_header_t * ip0;
60       tcp_header_t * tcp0;
61       ip_csum_t sum0;
62       u32 tcp_len0;
63
64       p0 = vlib_get_buffer (vm, packets[0]);
65       n_packets -= 1;
66       packets += 1;
67
68       ASSERT (p0->current_data == 0);
69       ip0 = (void *) (p0->data + ip_offset);
70       tcp0 = (void *) (p0->data + tcp_offset);
71       tcp_len0 = clib_net_to_host_u16 (ip0->length) - sizeof (ip0[0]);
72
73       /* Initialize checksum with header. */
74       if (BITS (sum0) == 32)
75         {
76           sum0 = clib_mem_unaligned (&ip0->src_address, u32);
77           sum0 = ip_csum_with_carry (sum0, clib_mem_unaligned (&ip0->dst_address, u32));
78         }
79       else
80         sum0 = clib_mem_unaligned (&ip0->src_address, u64);
81
82       sum0 = ip_csum_with_carry
83         (sum0, clib_host_to_net_u32 (tcp_len0 + (ip0->protocol << 16)));
84
85       /* Invalidate possibly old checksum. */
86       tcp0->checksum = 0;
87
88       sum0 = ip_incremental_checksum_buffer (vm, p0, tcp_offset, tcp_len0, sum0);
89
90       tcp0->checksum = ~ ip_csum_fold (sum0);
91     }
92 }
93
94 typedef struct {
95   struct { pg_edit_t src, dst; } ports;
96   pg_edit_t seq_number, ack_number;
97   pg_edit_t tcp_header_u32s;
98 #define _(f) pg_edit_t f##_flag;
99   foreach_tcp_flag
100 #undef _
101   pg_edit_t window;
102   pg_edit_t checksum;
103   pg_edit_t urgent_pointer;
104 } pg_tcp_header_t;
105
106 static inline void
107 pg_tcp_header_init (pg_tcp_header_t * p)
108 {
109   /* Initialize fields that are not bit fields in the IP header. */
110 #define _(f) pg_edit_init (&p->f, tcp_header_t, f);
111   _ (ports.src);
112   _ (ports.dst);
113   _ (seq_number);
114   _ (ack_number);
115   _ (window);
116   _ (checksum);
117   _ (urgent_pointer);
118 #undef _
119
120   /* Initialize bit fields. */
121 #define _(f)                                            \
122   pg_edit_init_bitfield (&p->f##_flag, tcp_header_t,    \
123                          flags,                         \
124                          TCP_FLAG_BIT_##f, 1);
125
126   foreach_tcp_flag
127 #undef _
128
129   pg_edit_init_bitfield (&p->tcp_header_u32s, tcp_header_t,
130                          tcp_header_u32s_and_reserved,
131                          4, 4);
132 }
133
134 uword
135 unformat_pg_tcp_header (unformat_input_t * input, va_list * args)
136 {
137   pg_stream_t * s = va_arg (*args, pg_stream_t *);
138   pg_tcp_header_t * p;
139   u32 group_index;
140   
141   p = pg_create_edit_group (s, sizeof (p[0]), sizeof (tcp_header_t),
142                             &group_index);
143   pg_tcp_header_init (p);
144
145   /* Defaults. */
146   pg_edit_set_fixed (&p->seq_number, 0);
147   pg_edit_set_fixed (&p->ack_number, 0);
148
149   pg_edit_set_fixed (&p->tcp_header_u32s, sizeof (tcp_header_t) / sizeof (u32));
150
151   pg_edit_set_fixed (&p->window, 4096);
152   pg_edit_set_fixed (&p->urgent_pointer, 0);
153
154 #define _(f) pg_edit_set_fixed (&p->f##_flag, 0);
155   foreach_tcp_flag
156 #undef _
157
158   p->checksum.type = PG_EDIT_UNSPECIFIED;
159
160   if (! unformat (input, "TCP: %U -> %U",
161                   unformat_pg_edit,
162                     unformat_tcp_udp_port, &p->ports.src,
163                   unformat_pg_edit,
164                     unformat_tcp_udp_port, &p->ports.dst))
165     goto error;
166
167   /* Parse options. */
168   while (1)
169     {
170       if (unformat (input, "window %U",
171                     unformat_pg_edit,
172                     unformat_pg_number, &p->window))
173         ;
174
175       else if (unformat (input, "checksum %U",
176                          unformat_pg_edit,
177                          unformat_pg_number, &p->checksum))
178         ;
179
180       /* Flags. */
181 #define _(f) else if (unformat (input, #f)) pg_edit_set_fixed (&p->f##_flag, 1);
182   foreach_tcp_flag
183 #undef _
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;
193     tcp_udp_port_info_t * pi;
194
195     pi = 0;
196     if (p->ports.dst.type == PG_EDIT_FIXED)
197       {
198         dst_port = pg_edit_get_value (&p->ports.dst, PG_EDIT_LO);
199         pi = ip_get_tcp_udp_port_info (im, dst_port);
200       }
201
202     if (pi && pi->unformat_pg_edit
203         && unformat_user (input, pi->unformat_pg_edit, s))
204       ;
205
206     else if (! unformat_user (input, unformat_pg_payload, s))
207       goto error;
208
209     if (p->checksum.type == PG_EDIT_UNSPECIFIED)
210       {
211         pg_edit_group_t * g = pg_stream_get_group (s, group_index);
212         g->edit_function = tcp_pg_edit_function;
213         g->edit_function_opaque = 0;
214       }
215
216     return 1;
217   }
218
219  error:
220   /* Free up any edits we may have added. */
221   pg_free_edit_group (s);
222   return 0;
223 }
224