Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 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/ip6_pg: IP v4 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 | ip6_pg_edit_function (pg_main_t * pg, |
| 45 | pg_stream_t * s, |
Dave Barach | d7cb1b5 | 2016-12-09 09:52:16 -0500 | [diff] [blame] | 46 | pg_edit_group_t * g, u32 * packets, u32 n_packets) |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 47 | { |
Dave Barach | d7cb1b5 | 2016-12-09 09:52:16 -0500 | [diff] [blame] | 48 | vlib_main_t *vm = vlib_get_main (); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 49 | u32 ip_header_offset = g->start_byte_offset; |
| 50 | |
| 51 | while (n_packets >= 2) |
| 52 | { |
| 53 | u32 pi0, pi1; |
Dave Barach | d7cb1b5 | 2016-12-09 09:52:16 -0500 | [diff] [blame] | 54 | vlib_buffer_t *p0, *p1; |
| 55 | ip6_header_t *ip0, *ip1; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 56 | |
| 57 | pi0 = packets[0]; |
| 58 | pi1 = packets[1]; |
| 59 | p0 = vlib_get_buffer (vm, pi0); |
| 60 | p1 = vlib_get_buffer (vm, pi1); |
| 61 | n_packets -= 2; |
| 62 | packets += 2; |
| 63 | |
| 64 | ip0 = (void *) (p0->data + ip_header_offset); |
| 65 | ip1 = (void *) (p1->data + ip_header_offset); |
| 66 | |
Dave Barach | d7cb1b5 | 2016-12-09 09:52:16 -0500 | [diff] [blame] | 67 | ip0->payload_length = |
| 68 | clib_host_to_net_u16 (vlib_buffer_length_in_chain (vm, p0) - |
| 69 | ip_header_offset - sizeof (ip0[0])); |
| 70 | ip1->payload_length = |
| 71 | clib_host_to_net_u16 (vlib_buffer_length_in_chain (vm, p1) - |
| 72 | ip_header_offset - sizeof (ip1[0])); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 73 | } |
| 74 | |
| 75 | while (n_packets >= 1) |
| 76 | { |
| 77 | u32 pi0; |
Dave Barach | d7cb1b5 | 2016-12-09 09:52:16 -0500 | [diff] [blame] | 78 | vlib_buffer_t *p0; |
| 79 | ip6_header_t *ip0; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 80 | |
| 81 | pi0 = packets[0]; |
| 82 | p0 = vlib_get_buffer (vm, pi0); |
| 83 | n_packets -= 1; |
| 84 | packets += 1; |
| 85 | |
| 86 | ip0 = (void *) (p0->data + ip_header_offset); |
| 87 | |
Dave Barach | d7cb1b5 | 2016-12-09 09:52:16 -0500 | [diff] [blame] | 88 | ip0->payload_length = |
| 89 | clib_host_to_net_u16 (vlib_buffer_length_in_chain (vm, p0) - |
| 90 | ip_header_offset - sizeof (ip0[0])); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 91 | } |
| 92 | } |
| 93 | |
Dave Barach | d7cb1b5 | 2016-12-09 09:52:16 -0500 | [diff] [blame] | 94 | typedef struct |
| 95 | { |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 96 | pg_edit_t ip_version; |
| 97 | pg_edit_t traffic_class; |
| 98 | pg_edit_t flow_label; |
| 99 | pg_edit_t payload_length; |
| 100 | pg_edit_t protocol; |
| 101 | pg_edit_t hop_limit; |
| 102 | pg_edit_t src_address, dst_address; |
| 103 | } pg_ip6_header_t; |
| 104 | |
| 105 | static inline void |
| 106 | pg_ip6_header_init (pg_ip6_header_t * p) |
| 107 | { |
| 108 | /* Initialize fields that are not bit fields in the IP header. */ |
| 109 | #define _(f) pg_edit_init (&p->f, ip6_header_t, f); |
Dave Barach | d7cb1b5 | 2016-12-09 09:52:16 -0500 | [diff] [blame] | 110 | _(payload_length); |
| 111 | _(hop_limit); |
| 112 | _(protocol); |
| 113 | _(src_address); |
| 114 | _(dst_address); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 115 | #undef _ |
| 116 | |
| 117 | /* Initialize bit fields. */ |
| 118 | pg_edit_init_bitfield (&p->ip_version, ip6_header_t, |
Dave Barach | d7cb1b5 | 2016-12-09 09:52:16 -0500 | [diff] [blame] | 119 | ip_version_traffic_class_and_flow_label, 28, 4); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 120 | pg_edit_init_bitfield (&p->traffic_class, ip6_header_t, |
Dave Barach | d7cb1b5 | 2016-12-09 09:52:16 -0500 | [diff] [blame] | 121 | ip_version_traffic_class_and_flow_label, 20, 8); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 122 | pg_edit_init_bitfield (&p->flow_label, ip6_header_t, |
Dave Barach | d7cb1b5 | 2016-12-09 09:52:16 -0500 | [diff] [blame] | 123 | ip_version_traffic_class_and_flow_label, 0, 20); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 124 | } |
| 125 | |
| 126 | uword |
| 127 | unformat_pg_ip6_header (unformat_input_t * input, va_list * args) |
| 128 | { |
Dave Barach | d7cb1b5 | 2016-12-09 09:52:16 -0500 | [diff] [blame] | 129 | pg_stream_t *s = va_arg (*args, pg_stream_t *); |
| 130 | pg_ip6_header_t *p; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 131 | u32 group_index; |
Dave Barach | d7cb1b5 | 2016-12-09 09:52:16 -0500 | [diff] [blame] | 132 | |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 133 | p = pg_create_edit_group (s, sizeof (p[0]), sizeof (ip6_header_t), |
| 134 | &group_index); |
| 135 | pg_ip6_header_init (p); |
| 136 | |
| 137 | /* Defaults. */ |
| 138 | pg_edit_set_fixed (&p->ip_version, 6); |
| 139 | pg_edit_set_fixed (&p->traffic_class, 0); |
| 140 | pg_edit_set_fixed (&p->flow_label, 0); |
| 141 | pg_edit_set_fixed (&p->hop_limit, 64); |
| 142 | |
| 143 | p->payload_length.type = PG_EDIT_UNSPECIFIED; |
| 144 | |
Dave Barach | d7cb1b5 | 2016-12-09 09:52:16 -0500 | [diff] [blame] | 145 | if (!unformat (input, "%U: %U -> %U", |
| 146 | unformat_pg_edit, |
| 147 | unformat_ip_protocol, &p->protocol, |
| 148 | unformat_pg_edit, |
| 149 | unformat_ip6_address, &p->src_address, |
| 150 | unformat_pg_edit, unformat_ip6_address, &p->dst_address)) |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 151 | goto error; |
| 152 | |
| 153 | /* Parse options. */ |
| 154 | while (1) |
| 155 | { |
| 156 | if (unformat (input, "version %U", |
Dave Barach | d7cb1b5 | 2016-12-09 09:52:16 -0500 | [diff] [blame] | 157 | unformat_pg_edit, unformat_pg_number, &p->ip_version)) |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 158 | ; |
| 159 | |
| 160 | else if (unformat (input, "traffic-class %U", |
| 161 | unformat_pg_edit, |
| 162 | unformat_pg_number, &p->traffic_class)) |
| 163 | ; |
| 164 | |
| 165 | else if (unformat (input, "length %U", |
| 166 | unformat_pg_edit, |
| 167 | unformat_pg_number, &p->payload_length)) |
| 168 | ; |
| 169 | |
| 170 | else if (unformat (input, "hop-limit %U", |
Dave Barach | d7cb1b5 | 2016-12-09 09:52:16 -0500 | [diff] [blame] | 171 | unformat_pg_edit, unformat_pg_number, &p->hop_limit)) |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 172 | ; |
| 173 | |
| 174 | /* Can't parse input: try next protocol level. */ |
| 175 | else |
| 176 | break; |
| 177 | } |
| 178 | |
| 179 | { |
Dave Barach | d7cb1b5 | 2016-12-09 09:52:16 -0500 | [diff] [blame] | 180 | ip_main_t *im = &ip_main; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 181 | ip_protocol_t protocol; |
Dave Barach | d7cb1b5 | 2016-12-09 09:52:16 -0500 | [diff] [blame] | 182 | ip_protocol_info_t *pi; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 183 | |
| 184 | pi = 0; |
| 185 | if (p->protocol.type == PG_EDIT_FIXED) |
| 186 | { |
| 187 | protocol = pg_edit_get_value (&p->protocol, PG_EDIT_LO); |
| 188 | pi = ip_get_protocol_info (im, protocol); |
| 189 | } |
| 190 | |
| 191 | if (pi && pi->unformat_pg_edit |
| 192 | && unformat_user (input, pi->unformat_pg_edit, s)) |
| 193 | ; |
| 194 | |
Dave Barach | d7cb1b5 | 2016-12-09 09:52:16 -0500 | [diff] [blame] | 195 | else if (!unformat_user (input, unformat_pg_payload, s)) |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 196 | goto error; |
| 197 | |
| 198 | if (p->payload_length.type == PG_EDIT_UNSPECIFIED |
| 199 | && s->min_packet_bytes == s->max_packet_bytes |
| 200 | && group_index + 1 < vec_len (s->edit_groups)) |
| 201 | { |
| 202 | pg_edit_set_fixed (&p->payload_length, |
Dave Barach | d7cb1b5 | 2016-12-09 09:52:16 -0500 | [diff] [blame] | 203 | pg_edit_group_n_bytes (s, |
| 204 | group_index) - |
| 205 | sizeof (ip6_header_t)); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 206 | } |
| 207 | |
| 208 | p = pg_get_edit_group (s, group_index); |
| 209 | if (p->payload_length.type == PG_EDIT_UNSPECIFIED) |
| 210 | { |
Dave Barach | d7cb1b5 | 2016-12-09 09:52:16 -0500 | [diff] [blame] | 211 | pg_edit_group_t *g = pg_stream_get_group (s, group_index); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 212 | g->edit_function = ip6_pg_edit_function; |
| 213 | } |
| 214 | |
| 215 | return 1; |
| 216 | } |
| 217 | |
Dave Barach | d7cb1b5 | 2016-12-09 09:52:16 -0500 | [diff] [blame] | 218 | error: |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 219 | /* Free up any edits we may have added. */ |
| 220 | pg_free_edit_group (s); |
| 221 | return 0; |
| 222 | } |
| 223 | |
Dave Barach | d7cb1b5 | 2016-12-09 09:52:16 -0500 | [diff] [blame] | 224 | |
| 225 | /* |
| 226 | * fd.io coding-style-patch-verification: ON |
| 227 | * |
| 228 | * Local Variables: |
| 229 | * eval: (c-set-style "gnu") |
| 230 | * End: |
| 231 | */ |