Initial commit of vpp code.
[vpp.git] / vnet / vnet / pg / edit.h
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   /* Invalid type used to poison edits. */
48   PG_EDIT_INVALID_TYPE,
49
50   /* Value is fixed: does not change for all packets in sequence. */
51   PG_EDIT_FIXED,
52
53   /* Value v increments between low and high values v_low <= v <= v_high. */
54   PG_EDIT_INCREMENT,
55
56   /* Random value between low and high values v_low <= v <= v_high. */
57   PG_EDIT_RANDOM,
58
59   /* Unspecified value; will be specified by some edit function. */
60   PG_EDIT_UNSPECIFIED,
61 } pg_edit_type_t;
62
63 typedef struct {
64   pg_edit_type_t type;
65
66   /* Bit offset within packet where value is to be written.
67      Bits are written in network byte order: high bits first.
68      This is the bit offset of the least significant bit: i.e. the
69      highest numbered byte * 8 plus bit offset within that byte.
70      Negative offsets encode special edits. */
71   i32 lsb_bit_offset;
72
73   /* Special offset indicating this edit is for packet length. */
74 #define PG_EDIT_PACKET_LENGTH (-1)
75
76   /* Number of bits in edit. */
77   u32 n_bits;
78
79   /* Low and high values for this edit.  Network byte order. */
80   u8 * values[2];
81 #define PG_EDIT_LO 0
82 #define PG_EDIT_HI 1
83
84   /* Last value used for increment edit type. */
85   u64 last_increment_value;
86 } pg_edit_t;
87
88 always_inline void
89 pg_edit_free (pg_edit_t * e)
90 {
91   int i;
92   for (i = 0; i < ARRAY_LEN (e->values); i++)
93     vec_free (e->values[i]);
94 }
95
96 #define pg_edit_init_bitfield(e,type,field,field_offset,field_n_bits)   \
97 do {                                                                    \
98   u32 _bo;                                                              \
99                                                                         \
100   ASSERT ((field_offset) < STRUCT_BITS_OF (type, field));               \
101                                                                         \
102   /* Start byte offset. */                                              \
103   _bo = STRUCT_OFFSET_OF (type, field);                                 \
104                                                                         \
105   /* Adjust for big endian byte order. */                               \
106   _bo += ((STRUCT_BITS_OF (type, field)                                 \
107            - (field_offset) - 1) / BITS (u8));                          \
108                                                                         \
109   (e)->lsb_bit_offset = _bo * BITS (u8) + ((field_offset) % BITS (u8)); \
110   (e)->n_bits = (field_n_bits);                                         \
111 } while (0)
112
113 /* Initialize edit for byte aligned fields. */
114 #define pg_edit_init(e,type,field) \
115    pg_edit_init_bitfield(e,type,field,0,STRUCT_BITS_OF(type,field))
116
117 static inline uword
118 pg_edit_n_alloc_bytes (pg_edit_t * e)
119 {
120   int i0, i1, n_bytes, n_bits_left;
121
122   i0 = e->lsb_bit_offset;
123   i1 = i0 % BITS (u8);
124
125   n_bytes = 0;
126   n_bits_left = e->n_bits;
127
128   if (n_bits_left > 0 && i1 != 0)
129     {
130       n_bytes++;
131       n_bits_left -= i1;
132       if (n_bits_left < 0)
133         n_bits_left = 0;
134     }
135
136   n_bytes += (n_bits_left / BITS (u8));
137   n_bytes += (n_bits_left % BITS (u8)) != 0;
138
139   return n_bytes;
140 }
141
142 static inline void
143 pg_edit_alloc_value (pg_edit_t * e, int i)
144 { vec_validate (e->values[i], e->lsb_bit_offset / BITS (u8)); }
145
146 extern void pg_edit_set_value (pg_edit_t * e, int hi_or_lo, u64 value);
147
148 static inline void
149 pg_edit_set_fixed (pg_edit_t * e, u64 value)
150 {
151   e->type = PG_EDIT_FIXED;
152   pg_edit_set_value (e, PG_EDIT_LO, value);
153 }
154
155 static inline void
156 pg_edit_copy_type_and_values (pg_edit_t * dst, pg_edit_t * src)
157 {
158   int i;
159   dst->type = src->type;
160   src->type = PG_EDIT_INVALID_TYPE;
161   for (i = 0; i < ARRAY_LEN (dst->values); i++)
162     {
163       dst->values[i] = src->values[i];
164       src->values[i] = 0;
165     }
166 }
167
168 static inline u64
169 pg_edit_get_value (pg_edit_t * e, int hi_or_lo)
170 {
171   u64 r = 0;
172   int i, n;
173   u8 * v = e->values[hi_or_lo];
174
175   n = round_pow2 (e->n_bits, BITS (u8)) / BITS (u8);
176
177   ASSERT (n <= vec_len (v));
178   ASSERT (n <= sizeof (r));
179
180   for (i = 0; i < n; i++)
181     r = (r << BITS (v[i])) + v[i];
182
183   return r;
184 }
185
186 static inline uword
187 pg_edit_is_fixed_with_value (pg_edit_t * e, u64 value)
188 {
189   return (e->type == PG_EDIT_FIXED
190           && value == pg_edit_get_value (e, PG_EDIT_LO));
191 }
192
193 uword unformat_pg_edit (unformat_input_t * input, va_list * args);
194 uword unformat_pg_payload (unformat_input_t * input, va_list * args);
195 uword unformat_pg_number (unformat_input_t * input, va_list * args);
196 uword unformat_pg_interface (unformat_input_t * input, va_list * args);
197
198 #endif /* included_packet_generator_pg_edit_h */