FD.io VPP  v16.09
Vector Packet Processing
ip6_pg.c
Go to the documentation of this file.
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
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_header_offset = g->start_byte_offset;
52 
53  while (n_packets >= 2)
54  {
55  u32 pi0, pi1;
56  vlib_buffer_t * p0, * p1;
57  ip6_header_t * ip0, * ip1;
58 
59  pi0 = packets[0];
60  pi1 = packets[1];
61  p0 = vlib_get_buffer (vm, pi0);
62  p1 = vlib_get_buffer (vm, pi1);
63  n_packets -= 2;
64  packets += 2;
65 
66  ip0 = (void *) (p0->data + ip_header_offset);
67  ip1 = (void *) (p1->data + ip_header_offset);
68 
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]));
71  }
72 
73  while (n_packets >= 1)
74  {
75  u32 pi0;
76  vlib_buffer_t * p0;
77  ip6_header_t * ip0;
78 
79  pi0 = packets[0];
80  p0 = vlib_get_buffer (vm, pi0);
81  n_packets -= 1;
82  packets += 1;
83 
84  ip0 = (void *) (p0->data + ip_header_offset);
85 
86  ip0->payload_length = clib_host_to_net_u16 (vlib_buffer_length_in_chain (vm, p0) - ip_header_offset - sizeof (ip0[0]));
87  }
88 }
89 
90 typedef struct {
97  pg_edit_t src_address, dst_address;
99 
100 static inline void
102 {
103  /* Initialize fields that are not bit fields in the IP header. */
104 #define _(f) pg_edit_init (&p->f, ip6_header_t, f);
105  _ (payload_length);
106  _ (hop_limit);
107  _ (protocol);
108  _ (src_address);
109  _ (dst_address);
110 #undef _
111 
112  /* Initialize bit fields. */
114  ip_version_traffic_class_and_flow_label,
115  28, 4);
117  ip_version_traffic_class_and_flow_label,
118  20, 8);
120  ip_version_traffic_class_and_flow_label,
121  0, 20);
122 }
123 
124 uword
125 unformat_pg_ip6_header (unformat_input_t * input, va_list * args)
126 {
127  pg_stream_t * s = va_arg (*args, pg_stream_t *);
128  pg_ip6_header_t * p;
129  u32 group_index;
130 
131  p = pg_create_edit_group (s, sizeof (p[0]), sizeof (ip6_header_t),
132  &group_index);
133  pg_ip6_header_init (p);
134 
135  /* Defaults. */
136  pg_edit_set_fixed (&p->ip_version, 6);
138  pg_edit_set_fixed (&p->flow_label, 0);
139  pg_edit_set_fixed (&p->hop_limit, 64);
140 
142 
143  if (! unformat (input, "%U: %U -> %U",
150  goto error;
151 
152  /* Parse options. */
153  while (1)
154  {
155  if (unformat (input, "version %U",
158  ;
159 
160  else if (unformat (input, "traffic-class %U",
163  ;
164 
165  else if (unformat (input, "length %U",
168  ;
169 
170  else if (unformat (input, "hop-limit %U",
173  ;
174 
175  /* Can't parse input: try next protocol level. */
176  else
177  break;
178  }
179 
180  {
181  ip_main_t * im = &ip_main;
182  ip_protocol_t protocol;
183  ip_protocol_info_t * pi;
184 
185  pi = 0;
186  if (p->protocol.type == PG_EDIT_FIXED)
187  {
188  protocol = pg_edit_get_value (&p->protocol, PG_EDIT_LO);
189  pi = ip_get_protocol_info (im, protocol);
190  }
191 
192  if (pi && pi->unformat_pg_edit
193  && unformat_user (input, pi->unformat_pg_edit, s))
194  ;
195 
196  else if (! unformat_user (input, unformat_pg_payload, s))
197  goto error;
198 
201  && group_index + 1 < vec_len (s->edit_groups))
202  {
204  pg_edit_group_n_bytes (s, group_index) - sizeof (ip6_header_t));
205  }
206 
207  p = pg_get_edit_group (s, group_index);
209  {
210  pg_edit_group_t * g = pg_stream_get_group (s, group_index);
212  }
213 
214  return 1;
215  }
216 
217  error:
218  /* Free up any edits we may have added. */
219  pg_free_edit_group (s);
220  return 0;
221 }
222 
Definition: edit.h:63
#define PG_EDIT_LO
Definition: edit.h:81
uword unformat(unformat_input_t *i, char *fmt,...)
Definition: unformat.c:966
static void * pg_get_edit_group(pg_stream_t *s, u32 group_index)
Definition: pg.h:256
Definition: pg.h:302
static void pg_edit_set_fixed(pg_edit_t *e, u64 value)
Definition: edit.h:149
pg_edit_group_t * edit_groups
Definition: pg.h:107
pg_edit_t flow_label
Definition: ip6_pg.c:93
uword unformat_pg_edit(unformat_input_t *input, va_list *args)
Definition: edit.c:106
Definition: ip.h:109
static uword vlib_buffer_length_in_chain(vlib_main_t *vm, vlib_buffer_t *b)
Get length in bytes of the buffer chain.
Definition: buffer_funcs.h:112
static void ip6_pg_edit_function(pg_main_t *pg, pg_stream_t *s, pg_edit_group_t *g, u32 *packets, u32 n_packets)
Definition: ip6_pg.c:44
u32 start_byte_offset
Definition: pg.h:66
static pg_edit_group_t * pg_stream_get_group(pg_stream_t *s, u32 group_index)
Definition: pg.h:216
unformat_function_t * unformat_pg_edit
Definition: ip.h:91
static void pg_ip6_header_init(pg_ip6_header_t *p)
Definition: ip6_pg.c:101
uword unformat_pg_payload(unformat_input_t *input, va_list *args)
Definition: edit.c:127
pg_edit_type_t type
Definition: edit.h:64
uword unformat_user(unformat_input_t *input, unformat_function_t *func,...)
Definition: unformat.c:977
static void * pg_create_edit_group(pg_stream_t *s, int n_edit_bytes, int n_packet_bytes, u32 *group_index)
Definition: pg.h:220
static ip_protocol_info_t * ip_get_protocol_info(ip_main_t *im, u32 protocol)
Definition: ip.h:136
pg_edit_t ip_version
Definition: ip6_pg.c:91
static u64 pg_edit_get_value(pg_edit_t *e, int hi_or_lo)
Definition: edit.h:169
#define pg_edit_init_bitfield(e, type, field, field_offset, field_n_bits)
Definition: edit.h:96
enum ip_protocol ip_protocol_t
unformat_function_t unformat_ip6_address
Definition: format.h:86
unformat_function_t unformat_ip_protocol
Definition: format.h:46
ip_main_t ip_main
Definition: ip_init.c:42
u32 min_packet_bytes
Definition: pg.h:112
u32 max_packet_bytes
Definition: pg.h:112
static uword pg_edit_group_n_bytes(pg_stream_t *s, u32 group_index)
Definition: pg.h:264
pg_edit_t src_address
Definition: ip6_pg.c:97
pg_edit_t traffic_class
Definition: ip6_pg.c:92
vlib_main_t * vlib_main
Definition: pg.h:304
unsigned int u32
Definition: types.h:88
pg_edit_t payload_length
Definition: ip6_pg.c:94
static void pg_free_edit_group(pg_stream_t *s)
Definition: pg.h:275
Definition: pg.h:95
u64 uword
Definition: types.h:112
u16 payload_length
Definition: ip6_packet.h:289
#define vec_len(v)
Number of elements in vector (rvalue-only, NULL tolerant)
void(* edit_function)(struct pg_main_t *pg, struct pg_stream_t *s, struct pg_edit_group_t *g, u32 *buffers, u32 n_buffers)
Definition: pg.h:72
pg_edit_t dst_address
Definition: ip6_pg.c:97
pg_edit_t hop_limit
Definition: ip6_pg.c:96
uword unformat_pg_number(unformat_input_t *input, va_list *args)
Definition: edit.c:84
u8 data[0]
Packet data.
Definition: buffer.h:151
pg_edit_t protocol
Definition: ip6_pg.c:95
struct _unformat_input_t unformat_input_t
uword unformat_pg_ip6_header(unformat_input_t *input, va_list *args)
Definition: ip6_pg.c:125
static vlib_buffer_t * vlib_get_buffer(vlib_main_t *vm, u32 buffer_index)
Translate buffer index into buffer pointer.
Definition: buffer_funcs.h:69