FD.io VPP  v21.06-3-gbb25fbf28
Vector Packet Processing
ip4_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/ip4_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 #define IP4_PG_EDIT_CHECKSUM (1 << 0)
44 #define IP4_PG_EDIT_LENGTH (1 << 1)
45 
48  u32 * packets,
49  u32 n_packets,
50  u32 ip_header_offset, u32 flags)
51 {
52  ASSERT (flags != 0);
53 
54  while (n_packets >= 2)
55  {
56  u32 pi0, pi1;
57  vlib_buffer_t *p0, *p1;
58  ip4_header_t *ip0, *ip1;
59  ip_csum_t sum0, sum1;
60 
61  pi0 = packets[0];
62  pi1 = packets[1];
63  p0 = vlib_get_buffer (vm, pi0);
64  p1 = vlib_get_buffer (vm, pi1);
65  n_packets -= 2;
66  packets += 2;
67 
68  ip0 = (void *) (p0->data + ip_header_offset);
69  ip1 = (void *) (p1->data + ip_header_offset);
70 
72  {
73  ip0->length =
74  clib_host_to_net_u16 (vlib_buffer_length_in_chain (vm, p0) -
75  ip_header_offset);
76  ip1->length =
77  clib_host_to_net_u16 (vlib_buffer_length_in_chain (vm, p1) -
78  ip_header_offset);
79  }
80 
82  {
83  ASSERT (ip4_header_bytes (ip0) == sizeof (ip0[0]));
84  ASSERT (ip4_header_bytes (ip1) == sizeof (ip1[0]));
85 
86  ip0->checksum = 0;
87  ip1->checksum = 0;
88 
89  ip4_partial_header_checksum_x2 (ip0, ip1, sum0, sum1);
90  ip0->checksum = ~ip_csum_fold (sum0);
91  ip1->checksum = ~ip_csum_fold (sum1);
92 
95  }
96  }
97 
98  while (n_packets >= 1)
99  {
100  u32 pi0;
101  vlib_buffer_t *p0;
102  ip4_header_t *ip0;
103  ip_csum_t sum0;
104 
105  pi0 = packets[0];
106  p0 = vlib_get_buffer (vm, pi0);
107  n_packets -= 1;
108  packets += 1;
109 
110  ip0 = (void *) (p0->data + ip_header_offset);
111 
113  ip0->length =
114  clib_host_to_net_u16 (vlib_buffer_length_in_chain (vm, p0) -
115  ip_header_offset);
116 
118  {
119  ASSERT (ip4_header_bytes (ip0) == sizeof (ip0[0]));
120 
121  ip0->checksum = 0;
122 
123  ip4_partial_header_checksum_x1 (ip0, sum0);
124  ip0->checksum = ~ip_csum_fold (sum0);
125 
127  }
128  }
129 }
130 
131 static void
133  pg_stream_t * s,
134  pg_edit_group_t * g, u32 * packets, u32 n_packets)
135 {
137  u32 ip_offset;
138 
139  ip_offset = g->start_byte_offset;
140 
141  switch (g->edit_function_opaque)
142  {
143  case IP4_PG_EDIT_LENGTH:
144  compute_length_and_or_checksum (vm, packets, n_packets, ip_offset,
146  break;
147 
149  compute_length_and_or_checksum (vm, packets, n_packets, ip_offset,
151  break;
152 
154  compute_length_and_or_checksum (vm, packets, n_packets, ip_offset,
157  break;
158 
159  default:
160  ASSERT (0);
161  break;
162  }
163 }
164 
165 typedef struct
166 {
167  pg_edit_t ip_version, header_length;
170 
172 
173  /* Flags together with fragment offset. */
174  pg_edit_t mf_flag, df_flag, ce_flag;
175 
177 
179 
181 
182  pg_edit_t src_address, dst_address;
184 
185 static inline void
187 {
188  /* Initialize fields that are not bit fields in the IP header. */
189 #define _(f) pg_edit_init (&p->f, ip4_header_t, f);
190  _(tos);
191  _(length);
192  _(fragment_id);
193  _(ttl);
194  _(protocol);
195  _(checksum);
196  _(src_address);
197  _(dst_address);
198 #undef _
199 
200  /* Initialize bit fields. */
202  ip_version_and_header_length, 0, 4);
204  ip_version_and_header_length, 4, 4);
205 
207  flags_and_fragment_offset, 0, 13);
209  flags_and_fragment_offset, 13, 1);
211  flags_and_fragment_offset, 14, 1);
213  flags_and_fragment_offset, 15, 1);
214 }
215 
216 uword
217 unformat_pg_ip4_header (unformat_input_t * input, va_list * args)
218 {
219  pg_stream_t *s = va_arg (*args, pg_stream_t *);
220  pg_ip4_header_t *p;
221  u32 group_index;
222 
223  p = pg_create_edit_group (s, sizeof (p[0]), sizeof (ip4_header_t),
224  &group_index);
225  pg_ip4_header_init (p);
226 
227  /* Defaults. */
228  pg_edit_set_fixed (&p->ip_version, 4);
229  pg_edit_set_fixed (&p->header_length, sizeof (ip4_header_t) / sizeof (u32));
230 
231  pg_edit_set_fixed (&p->tos, 0);
232  pg_edit_set_fixed (&p->ttl, 64);
233 
236  pg_edit_set_fixed (&p->mf_flag, 0);
237  pg_edit_set_fixed (&p->df_flag, 0);
238  pg_edit_set_fixed (&p->ce_flag, 0);
239 
242 
243  if (unformat (input, "%U: %U -> %U",
249  goto found;
250 
251  if (!unformat (input, "%U:",
253  goto error;
254 
255 found:
256  /* Parse options. */
257  while (1)
258  {
259  if (unformat (input, "version %U",
261  ;
262 
263  else if (unformat (input, "header-length %U",
266  ;
267 
268  else if (unformat (input, "tos %U",
270  ;
271 
272  else if (unformat (input, "length %U",
274  ;
275 
276  else if (unformat (input, "checksum %U",
278  ;
279 
280  else if (unformat (input, "ttl %U",
282  ;
283 
284  else if (unformat (input, "fragment id %U offset %U",
289  {
290  int i;
291  for (i = 0; i < ARRAY_LEN (p->fragment_offset.values); i++)
294  i) / 8);
295 
296  }
297 
298  /* Flags. */
299  else if (unformat (input, "mf") || unformat (input, "MF"))
300  pg_edit_set_fixed (&p->mf_flag, 1);
301 
302  else if (unformat (input, "df") || unformat (input, "DF"))
303  pg_edit_set_fixed (&p->df_flag, 1);
304 
305  else if (unformat (input, "ce") || unformat (input, "CE"))
306  pg_edit_set_fixed (&p->ce_flag, 1);
307 
308  /* Can't parse input: try next protocol level. */
309  else
310  break;
311  }
312 
313  {
314  ip_main_t *im = &ip_main;
316  ip_protocol_info_t *pi;
317 
318  pi = 0;
319  if (p->protocol.type == PG_EDIT_FIXED)
320  {
323  }
324 
325  if (pi && pi->unformat_pg_edit
326  && unformat_user (input, pi->unformat_pg_edit, s))
327  ;
328 
329  else if (!unformat_user (input, unformat_pg_payload, s))
330  goto error;
331 
334  && group_index + 1 < vec_len (s->edit_groups))
335  {
337  pg_edit_group_n_bytes (s, group_index));
338  }
339 
340  /* Compute IP header checksum if all edits are fixed. */
342  {
343  ip4_header_t fixed_header, fixed_mask, cmp_mask;
344 
345  /* See if header is all fixed and specified except for
346  checksum field. */
347  clib_memset (&cmp_mask, ~0, sizeof (cmp_mask));
348  cmp_mask.checksum = 0;
349 
350  pg_edit_group_get_fixed_packet_data (s, group_index,
351  &fixed_header, &fixed_mask);
352  if (!memcmp (&fixed_mask, &cmp_mask, sizeof (cmp_mask)))
354  clib_net_to_host_u16 (ip4_header_checksum
355  (&fixed_header)));
356  }
357 
358  p = pg_get_edit_group (s, group_index);
361  {
362  pg_edit_group_t *g = pg_stream_get_group (s, group_index);
364  g->edit_function_opaque = 0;
365  if (p->length.type == PG_EDIT_UNSPECIFIED)
369  }
370 
371  return 1;
372  }
373 
374 error:
375  /* Free up any edits we may have added. */
376  pg_free_edit_group (s);
377  return 0;
378 }
379 
380 
381 /*
382  * fd.io coding-style-patch-verification: ON
383  *
384  * Local Variables:
385  * eval: (c-set-style "gnu")
386  * End:
387  */
pg_ip4_header_t::protocol
pg_edit_t protocol
Definition: ip4_pg.c:178
ip4_partial_header_checksum_x2
#define ip4_partial_header_checksum_x2(ip0, ip1, sum0, sum1)
Definition: ip4_packet.h:419
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
pg_edit_group_get_fixed_packet_data
void pg_edit_group_get_fixed_packet_data(pg_stream_t *s, u32 group_index, void *fixed_packet_data, void *fixed_packet_data_mask)
Definition: stream.c:391
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.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_ip4_header_t::ip_version
pg_edit_t ip_version
Definition: ip4_pg.c:167
ttl
u8 ttl
Definition: fib_types.api:26
ip4_header_checksum_is_valid
static uword ip4_header_checksum_is_valid(ip4_header_t *i)
Definition: ip4_packet.h:396
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
pg_ip4_header_t
Definition: ip4_pg.c:165
pg_ip4_header_t::checksum
pg_edit_t checksum
Definition: ip4_pg.c:180
pg_ip4_header_t::length
pg_edit_t length
Definition: ip4_pg.c:169
vm
vlib_main_t * vm
X-connect all packets from the HOST to the PHY.
Definition: nat44_ei.c:3047
pg_ip4_header_t::ttl
pg_edit_t ttl
Definition: ip4_pg.c:176
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
compute_length_and_or_checksum
static_always_inline void compute_length_and_or_checksum(vlib_main_t *vm, u32 *packets, u32 n_packets, u32 ip_header_offset, u32 flags)
Definition: ip4_pg.c:47
ip4_header_t
Definition: ip4_packet.h:87
error
Definition: cJSON.c:88
ip4_header_t::length
u16 length
Definition: ip4_packet.h:99
unformat
uword unformat(unformat_input_t *i, const char *fmt,...)
Definition: unformat.c:978
pg_edit_group_t::edit_function_opaque
uword edit_function_opaque
Definition: pg.h:81
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_ip4_header_t::header_length
pg_edit_t header_length
Definition: ip4_pg.c:167
pg_stream_t::max_packet_bytes
u32 max_packet_bytes
Definition: pg.h:113
pg_ip4_header_t::fragment_id
pg_edit_t fragment_id
Definition: ip4_pg.c:171
PG_EDIT_LO
#define PG_EDIT_LO
Definition: edit.h:83
ARRAY_LEN
#define ARRAY_LEN(x)
Definition: clib.h:70
pg_edit_t::type
pg_edit_type_t type
Definition: edit.h:66
static_always_inline
#define static_always_inline
Definition: clib.h:112
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
i
sll srl srl sll sra u16x4 i
Definition: vector_sse42.h:261
pg_edit_get_value
static u64 pg_edit_get_value(pg_edit_t *e, int hi_or_lo)
Definition: edit.h:173
ip4_header_t::checksum
u16 checksum
Definition: ip4_packet.h:118
unformat_ip_protocol
unformat_function_t unformat_ip_protocol
Definition: format.h:46
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_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_main_t
Definition: pg.h:330
ASSERT
#define ASSERT(truth)
Definition: error_bootstrap.h:69
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
pg_ip4_header_t::tos
pg_edit_t tos
Definition: ip4_pg.c:168
ip_protocol_info_t
Definition: ip.h:70
protocol
vl_api_ip_proto_t protocol
Definition: lb_types.api:72
ip4_partial_header_checksum_x1
#define ip4_partial_header_checksum_x1(ip0, sum0)
Definition: ip4_packet.h:401
unformat_pg_ip4_header
uword unformat_pg_ip4_header(unformat_input_t *input, va_list *args)
Definition: ip4_pg.c:217
ip_protocol_t
enum ip_protocol ip_protocol_t
length
char const int length
Definition: cJSON.h:163
pg_ip4_header_t::dst_address
pg_edit_t dst_address
Definition: ip4_pg.c:182
pg_ip4_header_t::mf_flag
pg_edit_t mf_flag
Definition: ip4_pg.c:174
clib_memset
clib_memset(h->entries, 0, sizeof(h->entries[0]) *entries)
vlib_main_t
Definition: main.h:102
pg_ip4_header_t::df_flag
pg_edit_t df_flag
Definition: ip4_pg.c:174
vlib_get_main
static vlib_main_t * vlib_get_main(void)
Definition: global_funcs.h:38
pg_ip4_header_t::fragment_offset
pg_edit_t fragment_offset
Definition: ip4_pg.c:171
ip4_header_checksum
static u16 ip4_header_checksum(ip4_header_t *i)
Definition: ip4_packet.h:314
ip_csum_t
uword ip_csum_t
Definition: ip_packet.h:245
vlib_buffer_t::data
u8 data[]
Packet data.
Definition: buffer.h:204
pg_edit_t
Definition: edit.h:64
ip4_header_bytes
static int ip4_header_bytes(const ip4_header_t *i)
Definition: ip4_packet.h:190
pg_stream_t
Definition: pg.h:96
unformat_ip4_address
unformat_function_t unformat_ip4_address
Definition: format.h:68
ip_main_t
Definition: ip.h:107
pg_ip4_header_t::src_address
pg_edit_t src_address
Definition: ip4_pg.c:182
pg_edit_set_value
void pg_edit_set_value(pg_edit_t *e, int hi_or_lo, u64 value)
Definition: edit.c:77
src_address
vl_api_address_union_t src_address
Definition: ip_types.api:122
IP4_PG_EDIT_CHECKSUM
#define IP4_PG_EDIT_CHECKSUM
Definition: ip4_pg.c:43
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_csum_fold
static u16 ip_csum_fold(ip_csum_t c)
Definition: ip_packet.h:301
ip_protocol_info_t::unformat_pg_edit
unformat_function_t * unformat_pg_edit
Definition: ip.h:88
IP4_PG_EDIT_LENGTH
#define IP4_PG_EDIT_LENGTH
Definition: ip4_pg.c:44
pg_ip4_header_t::ce_flag
pg_edit_t ce_flag
Definition: ip4_pg.c:174
ip4_pg_edit_function
static void ip4_pg_edit_function(pg_main_t *pg, pg_stream_t *s, pg_edit_group_t *g, u32 *packets, u32 n_packets)
Definition: ip4_pg.c:132
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
pg_ip4_header_init
static void pg_ip4_header_init(pg_ip4_header_t *p)
Definition: ip4_pg.c:186
packets
units packets
Definition: map.api:366
pg_edit_t::values
u8 * values[2]
Definition: edit.h:82
flags
vl_api_wireguard_peer_flags_t flags
Definition: wireguard.api:105