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/ip6_pg: IP v4 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/ip/ip.h>
41 #include <vnet/pg/pg.h>
44 ip6_pg_edit_function (pg_main_t * pg,
50 vlib_main_t * vm = vlib_get_main();
51 u32 ip_header_offset = g->start_byte_offset;
53 while (n_packets >= 2)
56 vlib_buffer_t * p0, * p1;
57 ip6_header_t * ip0, * ip1;
61 p0 = vlib_get_buffer (vm, pi0);
62 p1 = vlib_get_buffer (vm, pi1);
66 ip0 = (void *) (p0->data + ip_header_offset);
67 ip1 = (void *) (p1->data + ip_header_offset);
69 ip0->payload_length = clib_host_to_net_u16 (vlib_buffer_length_in_chain (vm, p0) - ip_header_offset - sizeof (ip0[0]));
70 ip1->payload_length = clib_host_to_net_u16 (vlib_buffer_length_in_chain (vm, p1) - ip_header_offset - sizeof (ip1[0]));
73 while (n_packets >= 1)
80 p0 = vlib_get_buffer (vm, pi0);
84 ip0 = (void *) (p0->data + ip_header_offset);
86 ip0->payload_length = clib_host_to_net_u16 (vlib_buffer_length_in_chain (vm, p0) - ip_header_offset - sizeof (ip0[0]));
92 pg_edit_t traffic_class;
94 pg_edit_t payload_length;
97 pg_edit_t src_address, dst_address;
101 pg_ip6_header_init (pg_ip6_header_t * p)
103 /* Initialize fields that are not bit fields in the IP header. */
104 #define _(f) pg_edit_init (&p->f, ip6_header_t, f);
112 /* Initialize bit fields. */
113 pg_edit_init_bitfield (&p->ip_version, ip6_header_t,
114 ip_version_traffic_class_and_flow_label,
116 pg_edit_init_bitfield (&p->traffic_class, ip6_header_t,
117 ip_version_traffic_class_and_flow_label,
119 pg_edit_init_bitfield (&p->flow_label, ip6_header_t,
120 ip_version_traffic_class_and_flow_label,
125 unformat_pg_ip6_header (unformat_input_t * input, va_list * args)
127 pg_stream_t * s = va_arg (*args, pg_stream_t *);
131 p = pg_create_edit_group (s, sizeof (p[0]), sizeof (ip6_header_t),
133 pg_ip6_header_init (p);
136 pg_edit_set_fixed (&p->ip_version, 6);
137 pg_edit_set_fixed (&p->traffic_class, 0);
138 pg_edit_set_fixed (&p->flow_label, 0);
139 pg_edit_set_fixed (&p->hop_limit, 64);
141 p->payload_length.type = PG_EDIT_UNSPECIFIED;
143 if (! unformat (input, "%U: %U -> %U",
145 unformat_ip_protocol, &p->protocol,
147 unformat_ip6_address, &p->src_address,
149 unformat_ip6_address, &p->dst_address))
155 if (unformat (input, "version %U",
157 unformat_pg_number, &p->ip_version))
160 else if (unformat (input, "traffic-class %U",
162 unformat_pg_number, &p->traffic_class))
165 else if (unformat (input, "length %U",
167 unformat_pg_number, &p->payload_length))
170 else if (unformat (input, "hop-limit %U",
172 unformat_pg_number, &p->hop_limit))
175 /* Can't parse input: try next protocol level. */
181 ip_main_t * im = &ip_main;
182 ip_protocol_t protocol;
183 ip_protocol_info_t * pi;
186 if (p->protocol.type == PG_EDIT_FIXED)
188 protocol = pg_edit_get_value (&p->protocol, PG_EDIT_LO);
189 pi = ip_get_protocol_info (im, protocol);
192 if (pi && pi->unformat_pg_edit
193 && unformat_user (input, pi->unformat_pg_edit, s))
196 else if (! unformat_user (input, unformat_pg_payload, s))
199 if (p->payload_length.type == PG_EDIT_UNSPECIFIED
200 && s->min_packet_bytes == s->max_packet_bytes
201 && group_index + 1 < vec_len (s->edit_groups))
203 pg_edit_set_fixed (&p->payload_length,
204 pg_edit_group_n_bytes (s, group_index) - sizeof (ip6_header_t));
207 p = pg_get_edit_group (s, group_index);
208 if (p->payload_length.type == PG_EDIT_UNSPECIFIED)
210 pg_edit_group_t * g = pg_stream_get_group (s, group_index);
211 g->edit_function = ip6_pg_edit_function;
218 /* Free up any edits we may have added. */
219 pg_free_edit_group (s);