1cb9cadadSEd Warnicke/*
2cb9cadadSEd Warnicke * Copyright (c) 2015 Cisco and/or its affiliates.
3cb9cadadSEd Warnicke * Licensed under the Apache License, Version 2.0 (the "License");
4cb9cadadSEd Warnicke * you may not use this file except in compliance with the License.
5cb9cadadSEd Warnicke * You may obtain a copy of the License at:
6cb9cadadSEd Warnicke *
7cb9cadadSEd Warnicke *     http://www.apache.org/licenses/LICENSE-2.0
8cb9cadadSEd Warnicke *
9cb9cadadSEd Warnicke * Unless required by applicable law or agreed to in writing, software
10cb9cadadSEd Warnicke * distributed under the License is distributed on an "AS IS" BASIS,
11cb9cadadSEd Warnicke * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12cb9cadadSEd Warnicke * See the License for the specific language governing permissions and
13cb9cadadSEd Warnicke * limitations under the License.
14cb9cadadSEd Warnicke */
15cb9cadadSEd Warnicke/*
16cb9cadadSEd Warnicke * pg_edit.h: packet generator edits
17cb9cadadSEd Warnicke *
18cb9cadadSEd Warnicke * Copyright (c) 2008 Eliot Dresselhaus
19cb9cadadSEd Warnicke *
20cb9cadadSEd Warnicke * Permission is hereby granted, free of charge, to any person obtaining
21cb9cadadSEd Warnicke * a copy of this software and associated documentation files (the
22cb9cadadSEd Warnicke * "Software"), to deal in the Software without restriction, including
23cb9cadadSEd Warnicke * without limitation the rights to use, copy, modify, merge, publish,
24cb9cadadSEd Warnicke * distribute, sublicense, and/or sell copies of the Software, and to
25cb9cadadSEd Warnicke * permit persons to whom the Software is furnished to do so, subject to
26cb9cadadSEd Warnicke * the following conditions:
27cb9cadadSEd Warnicke *
28cb9cadadSEd Warnicke * The above copyright notice and this permission notice shall be
29cb9cadadSEd Warnicke * included in all copies or substantial portions of the Software.
30cb9cadadSEd Warnicke *
31cb9cadadSEd Warnicke *  THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
32cb9cadadSEd Warnicke *  EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
33cb9cadadSEd Warnicke *  MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
34cb9cadadSEd Warnicke *  NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
35cb9cadadSEd Warnicke *  LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
36cb9cadadSEd Warnicke *  OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
37cb9cadadSEd Warnicke *  WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
38cb9cadadSEd Warnicke */
39cb9cadadSEd Warnicke
40cb9cadadSEd Warnicke#ifndef included_packet_generator_pg_edit_h
41cb9cadadSEd Warnicke#define included_packet_generator_pg_edit_h
42cb9cadadSEd Warnicke
43cb9cadadSEd Warnicke#include <vppinfra/format.h>
44cb9cadadSEd Warnicke#include <vppinfra/vec.h>
45cb9cadadSEd Warnicke
4671e97c63SCalvintypedef enum
4771e97c63SCalvin{
48cb9cadadSEd Warnicke  /* Invalid type used to poison edits. */
49cb9cadadSEd Warnicke  PG_EDIT_INVALID_TYPE,
50cb9cadadSEd Warnicke
51cb9cadadSEd Warnicke  /* Value is fixed: does not change for all packets in sequence. */
52cb9cadadSEd Warnicke  PG_EDIT_FIXED,
53cb9cadadSEd Warnicke
54cb9cadadSEd Warnicke  /* Value v increments between low and high values v_low <= v <= v_high. */
55cb9cadadSEd Warnicke  PG_EDIT_INCREMENT,
56cb9cadadSEd Warnicke
57cb9cadadSEd Warnicke  /* Random value between low and high values v_low <= v <= v_high. */
58cb9cadadSEd Warnicke  PG_EDIT_RANDOM,
59cb9cadadSEd Warnicke
60cb9cadadSEd Warnicke  /* Unspecified value; will be specified by some edit function. */
61cb9cadadSEd Warnicke  PG_EDIT_UNSPECIFIED,
62cb9cadadSEd Warnicke} pg_edit_type_t;
63cb9cadadSEd Warnicke
6471e97c63SCalvintypedef struct
6571e97c63SCalvin{
66cb9cadadSEd Warnicke  pg_edit_type_t type;
67cb9cadadSEd Warnicke
68cb9cadadSEd Warnicke  /* Bit offset within packet where value is to be written.
69cb9cadadSEd Warnicke     Bits are written in network byte order: high bits first.
70cb9cadadSEd Warnicke     This is the bit offset of the least significant bit: i.e. the
71cb9cadadSEd Warnicke     highest numbered byte * 8 plus bit offset within that byte.
72cb9cadadSEd Warnicke     Negative offsets encode special edits. */
73cb9cadadSEd Warnicke  i32 lsb_bit_offset;
74cb9cadadSEd Warnicke
75cb9cadadSEd Warnicke  /* Special offset indicating this edit is for packet length. */
76cb9cadadSEd Warnicke#define PG_EDIT_PACKET_LENGTH (-1)
77cb9cadadSEd Warnicke
78cb9cadadSEd Warnicke  /* Number of bits in edit. */
79cb9cadadSEd Warnicke  u32 n_bits;
80cb9cadadSEd Warnicke
81cb9cadadSEd Warnicke  /* Low and high values for this edit.  Network byte order. */
8271e97c63SCalvin  u8 *values[2];
83cb9cadadSEd Warnicke#define PG_EDIT_LO 0
84cb9cadadSEd Warnicke#define PG_EDIT_HI 1
85cb9cadadSEd Warnicke
86cb9cadadSEd Warnicke  /* Last value used for increment edit type. */
87cb9cadadSEd Warnicke  u64 last_increment_value;
88cb9cadadSEd Warnicke} pg_edit_t;
89cb9cadadSEd Warnicke
90cb9cadadSEd Warnickealways_inline void
91cb9cadadSEd Warnickepg_edit_free (pg_edit_t * e)
92cb9cadadSEd Warnicke{
93cb9cadadSEd Warnicke  int i;
94cb9cadadSEd Warnicke  for (i = 0; i < ARRAY_LEN (e->values); i++)
95cb9cadadSEd Warnicke    vec_free (e->values[i]);
96cb9cadadSEd Warnicke}
97cb9cadadSEd Warnicke
98cb9cadadSEd Warnicke#define pg_edit_init_bitfield(e,type,field,field_offset,field_n_bits)	\
99cb9cadadSEd Warnickedo {									\
100cb9cadadSEd Warnicke  u32 _bo;								\
101cb9cadadSEd Warnicke									\
102cb9cadadSEd Warnicke  ASSERT ((field_offset) < STRUCT_BITS_OF (type, field));		\
103cb9cadadSEd Warnicke									\
104cb9cadadSEd Warnicke  /* Start byte offset. */						\
105cb9cadadSEd Warnicke  _bo = STRUCT_OFFSET_OF (type, field);					\
106cb9cadadSEd Warnicke									\
107cb9cadadSEd Warnicke  /* Adjust for big endian byte order. */				\
108cb9cadadSEd Warnicke  _bo += ((STRUCT_BITS_OF (type, field)					\
109cb9cadadSEd Warnicke	   - (field_offset) - 1) / BITS (u8));				\
110cb9cadadSEd Warnicke									\
111cb9cadadSEd Warnicke  (e)->lsb_bit_offset = _bo * BITS (u8) + ((field_offset) % BITS (u8));	\
112cb9cadadSEd Warnicke  (e)->n_bits = (field_n_bits);						\
113cb9cadadSEd Warnicke} while (0)
114cb9cadadSEd Warnicke
115cb9cadadSEd Warnicke/* Initialize edit for byte aligned fields. */
116cb9cadadSEd Warnicke#define pg_edit_init(e,type,field) \
117cb9cadadSEd Warnicke   pg_edit_init_bitfield(e,type,field,0,STRUCT_BITS_OF(type,field))
118cb9cadadSEd Warnicke
119cb9cadadSEd Warnickestatic inline uword
120cb9cadadSEd Warnickepg_edit_n_alloc_bytes (pg_edit_t * e)
121cb9cadadSEd Warnicke{
122cb9cadadSEd Warnicke  int i0, i1, n_bytes, n_bits_left;
123cb9cadadSEd Warnicke
124cb9cadadSEd Warnicke  i0 = e->lsb_bit_offset;
125cb9cadadSEd Warnicke  i1 = i0 % BITS (u8);
126cb9cadadSEd Warnicke
127cb9cadadSEd Warnicke  n_bytes = 0;
128cb9cadadSEd Warnicke  n_bits_left = e->n_bits;
129cb9cadadSEd Warnicke
130cb9cadadSEd Warnicke  if (n_bits_left > 0 && i1 != 0)
131cb9cadadSEd Warnicke    {
132cb9cadadSEd Warnicke      n_bytes++;
133cb9cadadSEd Warnicke      n_bits_left -= i1;
134cb9cadadSEd Warnicke      if (n_bits_left < 0)
135cb9cadadSEd Warnicke	n_bits_left = 0;
136cb9cadadSEd Warnicke    }
137cb9cadadSEd Warnicke
138cb9cadadSEd Warnicke  n_bytes += (n_bits_left / BITS (u8));
139cb9cadadSEd Warnicke  n_bytes += (n_bits_left % BITS (u8)) != 0;
140cb9cadadSEd Warnicke
141cb9cadadSEd Warnicke  return n_bytes;
142cb9cadadSEd Warnicke}
143cb9cadadSEd Warnicke
144cb9cadadSEd Warnickestatic inline void
145cb9cadadSEd Warnickepg_edit_alloc_value (pg_edit_t * e, int i)
14671e97c63SCalvin{
14771e97c63SCalvin  vec_validate (e->values[i], e->lsb_bit_offset / BITS (u8));
14871e97c63SCalvin}
149cb9cadadSEd Warnicke
150cb9cadadSEd Warnickeextern void pg_edit_set_value (pg_edit_t * e, int hi_or_lo, u64 value);
151cb9cadadSEd Warnicke
152cb9cadadSEd Warnickestatic inline void
153cb9cadadSEd Warnickepg_edit_set_fixed (pg_edit_t * e, u64 value)
154cb9cadadSEd Warnicke{
155cb9cadadSEd Warnicke  e->type = PG_EDIT_FIXED;
156cb9cadadSEd Warnicke  pg_edit_set_value (e, PG_EDIT_LO, value);
157cb9cadadSEd Warnicke}
158cb9cadadSEd Warnicke
159cb9cadadSEd Warnickestatic inline void
160cb9cadadSEd Warnickepg_edit_copy_type_and_values (pg_edit_t * dst, pg_edit_t * src)
161cb9cadadSEd Warnicke{
162cb9cadadSEd Warnicke  int i;
163cb9cadadSEd Warnicke  dst->type = src->type;
164cb9cadadSEd Warnicke  src->type = PG_EDIT_INVALID_TYPE;
165cb9cadadSEd Warnicke  for (i = 0; i < ARRAY_LEN (dst->values); i++)
166cb9cadadSEd Warnicke    {
167cb9cadadSEd Warnicke      dst->values[i] = src->values[i];
168cb9cadadSEd Warnicke      src->values[i] = 0;
169cb9cadadSEd Warnicke    }
170cb9cadadSEd Warnicke}
171cb9cadadSEd Warnicke
172cb9cadadSEd Warnickestatic inline u64
173cb9cadadSEd Warnickepg_edit_get_value (pg_edit_t * e, int hi_or_lo)
174cb9cadadSEd Warnicke{
175cb9cadadSEd Warnicke  u64 r = 0;
176cb9cadadSEd Warnicke  int i, n;
17771e97c63SCalvin  u8 *v = e->values[hi_or_lo];
178cb9cadadSEd Warnicke
179cb9cadadSEd Warnicke  n = round_pow2 (e->n_bits, BITS (u8)) / BITS (u8);
180cb9cadadSEd Warnicke
181cb9cadadSEd Warnicke  ASSERT (n <= vec_len (v));
182cb9cadadSEd Warnicke  ASSERT (n <= sizeof (r));
183cb9cadadSEd Warnicke
184cb9cadadSEd Warnicke  for (i = 0; i < n; i++)
185cb9cadadSEd Warnicke    r = (r << BITS (v[i])) + v[i];
186cb9cadadSEd Warnicke
187cb9cadadSEd Warnicke  return r;
188cb9cadadSEd Warnicke}
189cb9cadadSEd Warnicke
190cb9cadadSEd Warnickestatic inline uword
191cb9cadadSEd Warnickepg_edit_is_fixed_with_value (pg_edit_t * e, u64 value)
192cb9cadadSEd Warnicke{
193cb9cadadSEd Warnicke  return (e->type == PG_EDIT_FIXED
194cb9cadadSEd Warnicke	  && value == pg_edit_get_value (e, PG_EDIT_LO));
195cb9cadadSEd Warnicke}
196cb9cadadSEd Warnicke
197cb9cadadSEd Warnickeuword unformat_pg_edit (unformat_input_t * input, va_list * args);
198cb9cadadSEd Warnickeuword unformat_pg_payload (unformat_input_t * input, va_list * args);
199cb9cadadSEd Warnickeuword unformat_pg_number (unformat_input_t * input, va_list * args);
200cb9cadadSEd Warnickeuword unformat_pg_interface (unformat_input_t * input, va_list * args);
201cb9cadadSEd Warnicke
202cb9cadadSEd Warnicke#endif /* included_packet_generator_pg_edit_h */
20371e97c63SCalvin
20471e97c63SCalvin/*
20571e97c63SCalvin * fd.io coding-style-patch-verification: ON
20671e97c63SCalvin *
20771e97c63SCalvin * Local Variables:
20871e97c63SCalvin * eval: (c-set-style "gnu")
20971e97c63SCalvin * End:
21071e97c63SCalvin */
211