FD.io VPP  v21.06-3-gbb25fbf28
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, u32 * packets, u32 n_packets)
47 {
49  u32 ip_header_offset = g->start_byte_offset;
50 
51  while (n_packets >= 2)
52  {
53  u32 pi0, pi1;
54  vlib_buffer_t *p0, *p1;
55  ip6_header_t *ip0, *ip1;
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 
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]));
73  }
74 
75  while (n_packets >= 1)
76  {
77  u32 pi0;
78  vlib_buffer_t *p0;
79  ip6_header_t *ip0;
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 
88  ip0->payload_length =
89  clib_host_to_net_u16 (vlib_buffer_length_in_chain (vm, p0) -
90  ip_header_offset - sizeof (ip0[0]));
91  }
92 }
93 
94 typedef struct
95 {
102  pg_edit_t src_address, dst_address;
104 
105 static inline void
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);
110  _(payload_length);
111  _(hop_limit);
112  _(protocol);
113  _(src_address);
114  _(dst_address);
115 #undef _
116 
117  /* Initialize bit fields. */
119  ip_version_traffic_class_and_flow_label, 28, 4);
121  ip_version_traffic_class_and_flow_label, 20, 8);
123  ip_version_traffic_class_and_flow_label, 0, 20);
124 }
125 
126 uword
127 unformat_pg_ip6_header (unformat_input_t * input, va_list * args)
128 {
129  pg_stream_t *s = va_arg (*args, pg_stream_t *);
130  pg_ip6_header_t *p;
131  u32 group_index;
132 
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);
140  pg_edit_set_fixed (&p->flow_label, 0);
141  pg_edit_set_fixed (&p->hop_limit, 64);
142 
144 
145  if (!unformat (input, "%U: %U -> %U",
151  goto error;
152 
153  /* Parse options. */
154  while (1)
155  {
156  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",
172  ;
173 
174  /* Can't parse input: try next protocol level. */
175  else
176  break;
177  }
178 
179  {
180  ip_main_t *im = &ip_main;
182  ip_protocol_info_t *pi;
183 
184  pi = 0;
185  if (p->protocol.type == PG_EDIT_FIXED)
186  {
189  }
190 
191  if (pi && pi->unformat_pg_edit
192  && unformat_user (input, pi->unformat_pg_edit, s))
193  ;
194 
195  else if (!unformat_user (input, unformat_pg_payload, s))
196  goto error;
197 
200  && group_index + 1 < vec_len (s->edit_groups))
201  {
204  group_index) -
205  sizeof (ip6_header_t));
206  }
207 
208  p = pg_get_edit_group (s, group_index);
210  {
211  pg_edit_group_t *g = pg_stream_get_group (s, group_index);
213  }
214 
215  return 1;
216  }
217 
218 error:
219  /* Free up any edits we may have added. */
220  pg_free_edit_group (s);
221  return 0;
222 }
223 
224 
225 /*
226  * fd.io coding-style-patch-verification: ON
227  *
228  * Local Variables:
229  * eval: (c-set-style "gnu")
230  * End:
231  */
pg_stream_t::edit_groups
pg_edit_group_t * edit_groups
Definition: pg.h:108
im
vnet_interface_main_t * im
Definition: interface_output.c:395
PG_EDIT_FIXED
@ PG_EDIT_FIXED
Definition: edit.h:52
pg_edit_set_fixed
static void pg_edit_set_fixed(pg_edit_t *e, u64 value)
Definition: edit.h:153
unformat_user
uword unformat_user(unformat_input_t *input, unformat_function_t *func,...)
Definition: unformat.c:989
pg_stream_t::min_packet_bytes
u32 min_packet_bytes
Definition: pg.h:113
pg_edit_group_t::start_byte_offset
u32 start_byte_offset
Definition: pg.h:69
pg_edit_group_n_bytes
static uword pg_edit_group_n_bytes(pg_stream_t *s, u32 group_index)
Definition: pg.h:281
pg_ip6_header_t
Definition: ip6_pg.c:94
pg.h
vlib_get_buffer
static vlib_buffer_t * vlib_get_buffer(vlib_main_t *vm, u32 buffer_index)
Translate buffer index into buffer pointer.
Definition: buffer_funcs.h:111
pg_ip6_header_t::src_address
pg_edit_t src_address
Definition: ip6_pg.c:102
PG_EDIT_UNSPECIFIED
@ PG_EDIT_UNSPECIFIED
Definition: edit.h:61
ip_main
ip_main_t ip_main
Definition: ip_init.c:42
ip_get_protocol_info
static ip_protocol_info_t * ip_get_protocol_info(ip_main_t *im, u32 protocol)
Definition: ip.h:134
vm
vlib_main_t * vm
X-connect all packets from the HOST to the PHY.
Definition: nat44_ei.c:3047
pg_ip6_header_t::dst_address
pg_edit_t dst_address
Definition: ip6_pg.c:102
unformat_pg_number
uword unformat_pg_number(unformat_input_t *input, va_list *args)
Definition: edit.c:85
unformat_input_t
struct _unformat_input_t unformat_input_t
vlib_buffer_length_in_chain
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:433
ip6_pg_edit_function
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
error
Definition: cJSON.c:88
pg_ip6_header_t::protocol
pg_edit_t protocol
Definition: ip6_pg.c:100
unformat
uword unformat(unformat_input_t *i, const char *fmt,...)
Definition: unformat.c:978
pg_edit_group_t
Definition: pg.h:56
vec_len
#define vec_len(v)
Number of elements in vector (rvalue-only, NULL tolerant)
Definition: vec_bootstrap.h:142
pg_stream_t::max_packet_bytes
u32 max_packet_bytes
Definition: pg.h:113
PG_EDIT_LO
#define PG_EDIT_LO
Definition: edit.h:83
pg_edit_t::type
pg_edit_type_t type
Definition: edit.h:66
pg_ip6_header_init
static void pg_ip6_header_init(pg_ip6_header_t *p)
Definition: ip6_pg.c:106
uword
u64 uword
Definition: types.h:112
pg_get_edit_group
static void * pg_get_edit_group(pg_stream_t *s, u32 group_index)
Definition: pg.h:273
pg_edit_get_value
static u64 pg_edit_get_value(pg_edit_t *e, int hi_or_lo)
Definition: edit.h:173
pg_ip6_header_t::flow_label
pg_edit_t flow_label
Definition: ip6_pg.c:98
unformat_ip_protocol
unformat_function_t unformat_ip_protocol
Definition: format.h:46
hop_limit
u8 hop_limit
Definition: tunnel_types.api:88
pg_stream_get_group
static pg_edit_group_t * pg_stream_get_group(pg_stream_t *s, u32 group_index)
Definition: pg.h:233
pg_ip6_header_t::payload_length
pg_edit_t payload_length
Definition: ip6_pg.c:99
pg_free_edit_group
static void pg_free_edit_group(pg_stream_t *s)
Definition: pg.h:292
unformat_pg_edit
uword unformat_pg_edit(unformat_input_t *input, va_list *args)
Definition: edit.c:106
unformat_pg_payload
uword unformat_pg_payload(unformat_input_t *input, va_list *args)
Definition: edit.c:127
pg_ip6_header_t::ip_version
pg_edit_t ip_version
Definition: ip6_pg.c:96
pg_ip6_header_t::hop_limit
pg_edit_t hop_limit
Definition: ip6_pg.c:101
pg_main_t
Definition: pg.h:330
pg_create_edit_group
static void * pg_create_edit_group(pg_stream_t *s, int n_edit_bytes, int n_packet_bytes, u32 *group_index)
Definition: pg.h:239
ip.h
u32
unsigned int u32
Definition: types.h:88
ip_protocol_info_t
Definition: ip.h:70
protocol
vl_api_ip_proto_t protocol
Definition: lb_types.api:72
ip6_header_t
Definition: ip6_packet.h:294
ip_protocol_t
enum ip_protocol ip_protocol_t
vlib_main_t
Definition: main.h:102
vlib_get_main
static vlib_main_t * vlib_get_main(void)
Definition: global_funcs.h:38
vlib_buffer_t::data
u8 data[]
Packet data.
Definition: buffer.h:204
pg_ip6_header_t::traffic_class
pg_edit_t traffic_class
Definition: ip6_pg.c:97
pg_edit_t
Definition: edit.h:64
unformat_ip6_address
unformat_function_t unformat_ip6_address
Definition: format.h:89
pg_stream_t
Definition: pg.h:96
unformat_pg_ip6_header
uword unformat_pg_ip6_header(unformat_input_t *input, va_list *args)
Definition: ip6_pg.c:127
ip_main_t
Definition: ip.h:107
ip6_header_t::payload_length
u16 payload_length
Definition: ip6_packet.h:301
src_address
vl_api_address_union_t src_address
Definition: ip_types.api:122
pg_edit_group_t::edit_function
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:75
ip_protocol_info_t::unformat_pg_edit
unformat_function_t * unformat_pg_edit
Definition: ip.h:88
pg_edit_init_bitfield
#define pg_edit_init_bitfield(e, type, field, field_offset, field_n_bits)
Definition: edit.h:98
vlib_buffer_t
VLIB buffer representation.
Definition: buffer.h:111
packets
units packets
Definition: map.api:366