FD.io VPP  v21.06-3-gbb25fbf28
Vector Packet Processing
edit.h
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  * pg_edit.h: packet generator edits
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 #ifndef included_packet_generator_pg_edit_h
41 #define included_packet_generator_pg_edit_h
42 
43 #include <vppinfra/format.h>
44 #include <vppinfra/vec.h>
45 
46 typedef enum
47 {
48  /* Invalid type used to poison edits. */
50 
51  /* Value is fixed: does not change for all packets in sequence. */
53 
54  /* Value v increments between low and high values v_low <= v <= v_high. */
56 
57  /* Random value between low and high values v_low <= v <= v_high. */
59 
60  /* Unspecified value; will be specified by some edit function. */
63 
64 typedef struct
65 {
67 
68  /* Bit offset within packet where value is to be written.
69  Bits are written in network byte order: high bits first.
70  This is the bit offset of the least significant bit: i.e. the
71  highest numbered byte * 8 plus bit offset within that byte.
72  Negative offsets encode special edits. */
74 
75  /* Special offset indicating this edit is for packet length. */
76 #define PG_EDIT_PACKET_LENGTH (-1)
77 
78  /* Number of bits in edit. */
80 
81  /* Low and high values for this edit. Network byte order. */
82  u8 *values[2];
83 #define PG_EDIT_LO 0
84 #define PG_EDIT_HI 1
85 
86  /* Last value used for increment edit type. */
88 } pg_edit_t;
89 
90 always_inline void
92 {
93  int i;
94  for (i = 0; i < ARRAY_LEN (e->values); i++)
95  vec_free (e->values[i]);
96 }
97 
98 #define pg_edit_init_bitfield(e,type,field,field_offset,field_n_bits) \
99 do { \
100  u32 _bo; \
101  \
102  ASSERT ((field_offset) < STRUCT_BITS_OF (type, field)); \
103  \
104  /* Start byte offset. */ \
105  _bo = STRUCT_OFFSET_OF (type, field); \
106  \
107  /* Adjust for big endian byte order. */ \
108  _bo += ((STRUCT_BITS_OF (type, field) \
109  - (field_offset) - 1) / BITS (u8)); \
110  \
111  (e)->lsb_bit_offset = _bo * BITS (u8) + ((field_offset) % BITS (u8)); \
112  (e)->n_bits = (field_n_bits); \
113 } while (0)
114 
115 /* Initialize edit for byte aligned fields. */
116 #define pg_edit_init(e,type,field) \
117  pg_edit_init_bitfield(e,type,field,0,STRUCT_BITS_OF(type,field))
118 
119 static inline uword
121 {
122  int i0, i1, n_bytes, n_bits_left;
123 
124  i0 = e->lsb_bit_offset;
125  i1 = i0 % BITS (u8);
126 
127  n_bytes = 0;
128  n_bits_left = e->n_bits;
129 
130  if (n_bits_left > 0 && i1 != 0)
131  {
132  n_bytes++;
133  n_bits_left -= i1;
134  if (n_bits_left < 0)
135  n_bits_left = 0;
136  }
137 
138  n_bytes += (n_bits_left / BITS (u8));
139  n_bytes += (n_bits_left % BITS (u8)) != 0;
140 
141  return n_bytes;
142 }
143 
144 static inline void
146 {
147  vec_validate (e->values[i], e->lsb_bit_offset / BITS (u8));
148 }
149 
150 extern void pg_edit_set_value (pg_edit_t * e, int hi_or_lo, u64 value);
151 
152 static inline void
154 {
155  e->type = PG_EDIT_FIXED;
157 }
158 
159 static inline void
161 {
162  int i;
163  dst->type = src->type;
164  src->type = PG_EDIT_INVALID_TYPE;
165  for (i = 0; i < ARRAY_LEN (dst->values); i++)
166  {
167  dst->values[i] = src->values[i];
168  src->values[i] = 0;
169  }
170 }
171 
172 static inline u64
173 pg_edit_get_value (pg_edit_t * e, int hi_or_lo)
174 {
175  u64 r = 0;
176  int i, n;
177  u8 *v = e->values[hi_or_lo];
178 
179  n = round_pow2 (e->n_bits, BITS (u8)) / BITS (u8);
180 
181  ASSERT (n <= vec_len (v));
182  ASSERT (n <= sizeof (r));
183 
184  for (i = 0; i < n; i++)
185  r = (r << BITS (v[i])) + v[i];
186 
187  return r;
188 }
189 
190 static inline uword
192 {
193  return (e->type == PG_EDIT_FIXED
194  && value == pg_edit_get_value (e, PG_EDIT_LO));
195 }
196 
197 uword unformat_pg_edit (unformat_input_t * input, va_list * args);
198 uword unformat_pg_payload (unformat_input_t * input, va_list * args);
199 uword unformat_pg_number (unformat_input_t * input, va_list * args);
200 uword unformat_pg_interface (unformat_input_t * input, va_list * args);
201 
202 #endif /* included_packet_generator_pg_edit_h */
203 
204 /*
205  * fd.io coding-style-patch-verification: ON
206  *
207  * Local Variables:
208  * eval: (c-set-style "gnu")
209  * End:
210  */
unformat_pg_payload
uword unformat_pg_payload(unformat_input_t *input, va_list *args)
Definition: edit.c:127
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_copy_type_and_values
static void pg_edit_copy_type_and_values(pg_edit_t *dst, pg_edit_t *src)
Definition: edit.h:160
pg_edit_set_value
void pg_edit_set_value(pg_edit_t *e, int hi_or_lo, u64 value)
Definition: edit.c:77
PG_EDIT_UNSPECIFIED
@ PG_EDIT_UNSPECIFIED
Definition: edit.h:61
unformat_input_t
struct _unformat_input_t unformat_input_t
r
vnet_hw_if_output_node_runtime_t * r
Definition: interface_output.c:1071
i32
signed int i32
Definition: types.h:77
round_pow2
static uword round_pow2(uword x, uword pow2)
Definition: clib.h:279
vec_len
#define vec_len(v)
Number of elements in vector (rvalue-only, NULL tolerant)
Definition: vec_bootstrap.h:142
pg_edit_t::last_increment_value
u64 last_increment_value
Definition: edit.h:87
pg_edit_n_alloc_bytes
static uword pg_edit_n_alloc_bytes(pg_edit_t *e)
Definition: edit.h:120
PG_EDIT_LO
#define PG_EDIT_LO
Definition: edit.h:83
ARRAY_LEN
#define ARRAY_LEN(x)
Definition: clib.h:70
BITS
#define BITS(x)
Definition: clib.h:69
pg_edit_t::type
pg_edit_type_t type
Definition: edit.h:66
pg_edit_t::n_bits
u32 n_bits
Definition: edit.h:79
uword
u64 uword
Definition: types.h:112
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
vec_validate
#define vec_validate(V, I)
Make sure vector is long enough for given index (no header, unspecified alignment)
Definition: vec.h:523
format.h
src
vl_api_address_t src
Definition: gre.api:54
vec_free
#define vec_free(V)
Free vector's memory (no header).
Definition: vec.h:395
always_inline
#define always_inline
Definition: rdma_mlx5dv.h:23
u64
unsigned long u64
Definition: types.h:89
ASSERT
#define ASSERT(truth)
Definition: error_bootstrap.h:69
u32
unsigned int u32
Definition: types.h:88
n_bytes
u32 n_bytes
Definition: interface_output.c:401
dst
vl_api_ip4_address_t dst
Definition: pnat.api:41
pg_edit_alloc_value
static void pg_edit_alloc_value(pg_edit_t *e, int i)
Definition: edit.h:145
value
u8 value
Definition: qos.api:54
pg_edit_is_fixed_with_value
static uword pg_edit_is_fixed_with_value(pg_edit_t *e, u64 value)
Definition: edit.h:191
vec.h
u8
unsigned char u8
Definition: types.h:56
PG_EDIT_RANDOM
@ PG_EDIT_RANDOM
Definition: edit.h:58
pg_edit_free
static void pg_edit_free(pg_edit_t *e)
Definition: edit.h:91
unformat_pg_number
uword unformat_pg_number(unformat_input_t *input, va_list *args)
Definition: edit.c:85
pg_edit_t
Definition: edit.h:64
pg_edit_t::lsb_bit_offset
i32 lsb_bit_offset
Definition: edit.h:73
pg_edit_type_t
pg_edit_type_t
Definition: edit.h:46
PG_EDIT_INVALID_TYPE
@ PG_EDIT_INVALID_TYPE
Definition: edit.h:49
unformat_pg_interface
uword unformat_pg_interface(unformat_input_t *input, va_list *args)
PG_EDIT_INCREMENT
@ PG_EDIT_INCREMENT
Definition: edit.h:55
unformat_pg_edit
uword unformat_pg_edit(unformat_input_t *input, va_list *args)
Definition: edit.c:106
pg_edit_t::values
u8 * values[2]
Definition: edit.h:82