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:
7 * http://www.apache.org/licenses/LICENSE-2.0
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.
16 * ip/udp_pg: UDP packet-generator interface
18 * Copyright (c) 2008 Eliot Dresselhaus
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:
28 * The above copyright notice and this permission notice shall be
29 * included in all copies or substantial portions of the Software.
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.
40 #include <vnet/pg/pg.h>
41 #include <vnet/ip/ip.h> /* for unformat_udp_udp_port */
43 #define UDP_PG_EDIT_LENGTH (1 << 0)
44 #define UDP_PG_EDIT_CHECKSUM (1 << 1)
47 udp_pg_edit_function_inline (pg_main_t * pg,
54 vlib_main_t * vm = pg->vlib_main;
55 u32 ip_offset, udp_offset;
57 udp_offset = g->start_byte_offset;
58 ip_offset = (g-1)->start_byte_offset;
60 while (n_packets >= 1)
67 p0 = vlib_get_buffer (vm, packets[0]);
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]);
75 if (flags & UDP_PG_EDIT_LENGTH)
77 clib_net_to_host_u16 (vlib_buffer_length_in_chain (vm, p0)
80 /* Initialize checksum with header. */
81 if (flags & UDP_PG_EDIT_CHECKSUM)
85 sum0 = clib_mem_unaligned (&ip0->src_address, u64);
87 sum0 = ip_csum_with_carry
88 (sum0, clib_host_to_net_u32 (udp_len0 + (ip0->protocol << 16)));
90 /* Invalidate possibly old checksum. */
93 sum0 = ip_incremental_checksum_buffer (vm, p0, udp_offset, udp_len0, sum0);
95 sum0 = ~ ip_csum_fold (sum0);
97 /* Zero checksum means checksumming disabled. */
98 sum0 = sum0 != 0 ? sum0 : 0xffff;
100 udp0->checksum = sum0;
106 udp_pg_edit_function (pg_main_t * pg,
112 switch (g->edit_function_opaque)
114 case UDP_PG_EDIT_LENGTH:
115 udp_pg_edit_function_inline (pg, s, g, packets, n_packets,
119 case UDP_PG_EDIT_CHECKSUM:
120 udp_pg_edit_function_inline (pg, s, g, packets, n_packets,
121 UDP_PG_EDIT_CHECKSUM);
124 case UDP_PG_EDIT_CHECKSUM | UDP_PG_EDIT_LENGTH:
125 udp_pg_edit_function_inline (pg, s, g, packets, n_packets,
126 UDP_PG_EDIT_CHECKSUM | UDP_PG_EDIT_LENGTH);
136 pg_edit_t src_port, dst_port;
142 pg_udp_header_init (pg_udp_header_t * p)
144 /* Initialize fields that are not bit fields in the IP header. */
145 #define _(f) pg_edit_init (&p->f, udp_header_t, f);
154 unformat_pg_udp_header (unformat_input_t * input, va_list * args)
156 pg_stream_t * s = va_arg (*args, pg_stream_t *);
160 p = pg_create_edit_group (s, sizeof (p[0]), sizeof (udp_header_t),
162 pg_udp_header_init (p);
165 p->checksum.type = PG_EDIT_UNSPECIFIED;
166 p->length.type = PG_EDIT_UNSPECIFIED;
168 if (! unformat (input, "UDP: %U -> %U",
170 unformat_tcp_udp_port, &p->src_port,
172 unformat_tcp_udp_port, &p->dst_port))
178 if (unformat (input, "length %U",
180 unformat_pg_number, &p->length))
183 else if (unformat (input, "checksum %U",
185 unformat_pg_number, &p->checksum))
188 /* Can't parse input: try next protocol level. */
194 ip_main_t * im = &ip_main;
196 tcp_udp_port_info_t * pi;
199 if (p->dst_port.type == PG_EDIT_FIXED)
201 dst_port = pg_edit_get_value (&p->dst_port, PG_EDIT_LO);
202 pi = ip_get_tcp_udp_port_info (im, dst_port);
205 if (pi && pi->unformat_pg_edit
206 && unformat_user (input, pi->unformat_pg_edit, s))
209 else if (! unformat_user (input, unformat_pg_payload, s))
212 p = pg_get_edit_group (s, group_index);
213 if (p->checksum.type == PG_EDIT_UNSPECIFIED
214 || p->length.type == PG_EDIT_UNSPECIFIED)
216 pg_edit_group_t * g = pg_stream_get_group (s, group_index);
217 g->edit_function = udp_pg_edit_function;
218 g->edit_function_opaque = 0;
219 if (p->checksum.type == PG_EDIT_UNSPECIFIED)
220 g->edit_function_opaque |= UDP_PG_EDIT_CHECKSUM;
221 if (p->length.type == PG_EDIT_UNSPECIFIED)
222 g->edit_function_opaque |= UDP_PG_EDIT_LENGTH;
229 /* Free up any edits we may have added. */
230 pg_free_edit_group (s);