blob: 621beb792770e0cf2f653723ca4814e3e50304c6 [file] [log] [blame]
Ed Warnickecb9cada2015-12-08 15:45:58 -07001/*
Florin Corasc5df8c72019-04-08 07:42:30 -07002 * Copyright (c) 2015-2019 Cisco and/or its affiliates.
Ed Warnickecb9cada2015-12-08 15:45:58 -07003 * 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>
Dave Barachd7cb1b52016-12-09 09:52:16 -050041#include <vnet/ip/ip.h> /* for unformat_udp_udp_port */
Kingwel Xie4af47092018-10-26 23:42:15 -040042#include <vnet/udp/udp.h>
Ed Warnickecb9cada2015-12-08 15:45:58 -070043
44#define UDP_PG_EDIT_LENGTH (1 << 0)
45#define UDP_PG_EDIT_CHECKSUM (1 << 1)
46
47always_inline void
48udp_pg_edit_function_inline (pg_main_t * pg,
49 pg_stream_t * s,
50 pg_edit_group_t * g,
Dave Barachd7cb1b52016-12-09 09:52:16 -050051 u32 * packets, u32 n_packets, u32 flags)
Ed Warnickecb9cada2015-12-08 15:45:58 -070052{
Dave Barachd7cb1b52016-12-09 09:52:16 -050053 vlib_main_t *vm = vlib_get_main ();
Ed Warnickecb9cada2015-12-08 15:45:58 -070054 u32 ip_offset, udp_offset;
55
56 udp_offset = g->start_byte_offset;
Dave Barachd7cb1b52016-12-09 09:52:16 -050057 ip_offset = (g - 1)->start_byte_offset;
Ed Warnickecb9cada2015-12-08 15:45:58 -070058
59 while (n_packets >= 1)
60 {
Dave Barachd7cb1b52016-12-09 09:52:16 -050061 vlib_buffer_t *p0;
62 ip4_header_t *ip0;
63 udp_header_t *udp0;
Ed Warnickecb9cada2015-12-08 15:45:58 -070064 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);
Kingwel Xiefc3b8b82018-10-10 20:51:59 -040072 udp_len0 = vlib_buffer_length_in_chain (vm, p0) - udp_offset;
Ed Warnickecb9cada2015-12-08 15:45:58 -070073
74 if (flags & UDP_PG_EDIT_LENGTH)
Kingwel Xiefc3b8b82018-10-10 20:51:59 -040075 udp0->length = clib_host_to_net_u16 (udp_len0);
76 else
77 udp_len0 = clib_host_to_net_u16 (udp0->length);
Ed Warnickecb9cada2015-12-08 15:45:58 -070078
79 /* Initialize checksum with header. */
80 if (flags & UDP_PG_EDIT_CHECKSUM)
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
Dave Barachd7cb1b52016-12-09 09:52:16 -050092 sum0 =
93 ip_incremental_checksum_buffer (vm, p0, udp_offset, udp_len0,
94 sum0);
Ed Warnickecb9cada2015-12-08 15:45:58 -070095
Dave Barachd7cb1b52016-12-09 09:52:16 -050096 sum0 = ~ip_csum_fold (sum0);
Ed Warnickecb9cada2015-12-08 15:45:58 -070097
98 /* Zero checksum means checksumming disabled. */
99 sum0 = sum0 != 0 ? sum0 : 0xffff;
100
101 udp0->checksum = sum0;
102 }
103 }
104}
105
106static void
107udp_pg_edit_function (pg_main_t * pg,
108 pg_stream_t * s,
Dave Barachd7cb1b52016-12-09 09:52:16 -0500109 pg_edit_group_t * g, u32 * packets, u32 n_packets)
Ed Warnickecb9cada2015-12-08 15:45:58 -0700110{
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,
115 UDP_PG_EDIT_LENGTH);
116 break;
117
118 case UDP_PG_EDIT_CHECKSUM:
119 udp_pg_edit_function_inline (pg, s, g, packets, n_packets,
120 UDP_PG_EDIT_CHECKSUM);
121 break;
122
123 case UDP_PG_EDIT_CHECKSUM | UDP_PG_EDIT_LENGTH:
124 udp_pg_edit_function_inline (pg, s, g, packets, n_packets,
125 UDP_PG_EDIT_CHECKSUM | UDP_PG_EDIT_LENGTH);
126 break;
127
128 default:
129 ASSERT (0);
130 break;
131 }
132}
133
Dave Barachd7cb1b52016-12-09 09:52:16 -0500134typedef struct
135{
Ed Warnickecb9cada2015-12-08 15:45:58 -0700136 pg_edit_t src_port, dst_port;
137 pg_edit_t length;
138 pg_edit_t checksum;
139} pg_udp_header_t;
140
141static inline void
142pg_udp_header_init (pg_udp_header_t * p)
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);
Dave Barachd7cb1b52016-12-09 09:52:16 -0500146 _(src_port);
147 _(dst_port);
148 _(length);
149 _(checksum);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700150#undef _
151}
152
153uword
154unformat_pg_udp_header (unformat_input_t * input, va_list * args)
155{
Dave Barachd7cb1b52016-12-09 09:52:16 -0500156 pg_stream_t *s = va_arg (*args, pg_stream_t *);
157 pg_udp_header_t *p;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700158 u32 group_index;
Dave Barachd7cb1b52016-12-09 09:52:16 -0500159
Ed Warnickecb9cada2015-12-08 15:45:58 -0700160 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. */
165 p->checksum.type = PG_EDIT_UNSPECIFIED;
166 p->length.type = PG_EDIT_UNSPECIFIED;
167
Dave Barachd7cb1b52016-12-09 09:52:16 -0500168 if (!unformat (input, "UDP: %U -> %U",
169 unformat_pg_edit,
170 unformat_tcp_udp_port, &p->src_port,
171 unformat_pg_edit, unformat_tcp_udp_port, &p->dst_port))
Ed Warnickecb9cada2015-12-08 15:45:58 -0700172 goto error;
173
174 /* Parse options. */
175 while (1)
176 {
177 if (unformat (input, "length %U",
Dave Barachd7cb1b52016-12-09 09:52:16 -0500178 unformat_pg_edit, unformat_pg_number, &p->length))
Ed Warnickecb9cada2015-12-08 15:45:58 -0700179 ;
180
181 else if (unformat (input, "checksum %U",
Dave Barachd7cb1b52016-12-09 09:52:16 -0500182 unformat_pg_edit, unformat_pg_number, &p->checksum))
Ed Warnickecb9cada2015-12-08 15:45:58 -0700183 ;
184
185 /* Can't parse input: try next protocol level. */
186 else
187 break;
188 }
189
190 {
Dave Barachd7cb1b52016-12-09 09:52:16 -0500191 ip_main_t *im = &ip_main;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700192 u16 dst_port;
Dave Barachd7cb1b52016-12-09 09:52:16 -0500193 tcp_udp_port_info_t *pi;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700194
Kingwel Xie4af47092018-10-26 23:42:15 -0400195 /* For the pg format of applications over UDP local */
196 udp_dst_port_info_t *pi2 = NULL;
197
Ed Warnickecb9cada2015-12-08 15:45:58 -0700198 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);
Kingwel Xie4af47092018-10-26 23:42:15 -0400203 pi2 = udp_get_dst_port_info (&udp_main, dst_port, UDP_IP4);
204 if (!pi2)
205 pi2 = udp_get_dst_port_info (&udp_main, dst_port, UDP_IP6);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700206 }
207
208 if (pi && pi->unformat_pg_edit
209 && unformat_user (input, pi->unformat_pg_edit, s))
210 ;
Kingwel Xie4af47092018-10-26 23:42:15 -0400211 else if (pi2 && pi2->unformat_pg_edit
212 && unformat_user (input, pi2->unformat_pg_edit, s))
213 ;
Dave Barachd7cb1b52016-12-09 09:52:16 -0500214 else if (!unformat_user (input, unformat_pg_payload, s))
Ed Warnickecb9cada2015-12-08 15:45:58 -0700215 goto error;
216
217 p = pg_get_edit_group (s, group_index);
218 if (p->checksum.type == PG_EDIT_UNSPECIFIED
219 || p->length.type == PG_EDIT_UNSPECIFIED)
220 {
Dave Barachd7cb1b52016-12-09 09:52:16 -0500221 pg_edit_group_t *g = pg_stream_get_group (s, group_index);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700222 g->edit_function = udp_pg_edit_function;
223 g->edit_function_opaque = 0;
224 if (p->checksum.type == PG_EDIT_UNSPECIFIED)
225 g->edit_function_opaque |= UDP_PG_EDIT_CHECKSUM;
226 if (p->length.type == PG_EDIT_UNSPECIFIED)
227 g->edit_function_opaque |= UDP_PG_EDIT_LENGTH;
228 }
229
230 return 1;
231 }
232
Dave Barachd7cb1b52016-12-09 09:52:16 -0500233error:
Ed Warnickecb9cada2015-12-08 15:45:58 -0700234 /* Free up any edits we may have added. */
235 pg_free_edit_group (s);
236 return 0;
237}
238
Dave Barachd7cb1b52016-12-09 09:52:16 -0500239
240/*
241 * fd.io coding-style-patch-verification: ON
242 *
243 * Local Variables:
244 * eval: (c-set-style "gnu")
245 * End:
246 */