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.c: 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#include <vlib/vlib.h>
41cb9cadadSEd Warnicke#include <vnet/pg/pg.h>
42cb9cadadSEd Warnicke
43cb9cadadSEd Warnickestatic void
44cb9cadadSEd Warnickepg_edit_set_value_helper (pg_edit_t * e, u64 value, u8 * result)
45cb9cadadSEd Warnicke{
46cb9cadadSEd Warnicke  int i, j, n_bits_left;
4771e97c63SCalvin  u8 *v, tmp[8];
48cb9cadadSEd Warnicke
49cb9cadadSEd Warnicke  v = tmp;
50cb9cadadSEd Warnicke
51cb9cadadSEd Warnicke  n_bits_left = e->n_bits;
52cb9cadadSEd Warnicke  i = 0;
53cb9cadadSEd Warnicke  j = e->lsb_bit_offset % BITS (v[0]);
54cb9cadadSEd Warnicke
55cb9cadadSEd Warnicke  if (n_bits_left > 0 && j != 0)
56cb9cadadSEd Warnicke    {
57cb9cadadSEd Warnicke      v[i] = (value & 0xff) << j;
58cb9cadadSEd Warnicke      value >>= BITS (v[0]) - j;
59cb9cadadSEd Warnicke      n_bits_left -= BITS (v[0]) - j;
60cb9cadadSEd Warnicke      i += 1;
61cb9cadadSEd Warnicke    }
62cb9cadadSEd Warnicke
63cb9cadadSEd Warnicke  while (n_bits_left > 0)
64cb9cadadSEd Warnicke    {
65cb9cadadSEd Warnicke      v[i] = value & 0xff;
66cb9cadadSEd Warnicke      value >>= 8;
67cb9cadadSEd Warnicke      n_bits_left -= 8;
68cb9cadadSEd Warnicke      i += 1;
69cb9cadadSEd Warnicke    }
70cb9cadadSEd Warnicke
71cb9cadadSEd Warnicke  /* Convert to network byte order. */
72cb9cadadSEd Warnicke  for (j = 0; j < i; j++)
73cb9cadadSEd Warnicke    result[j] = v[i - 1 - j];
74cb9cadadSEd Warnicke}
75cb9cadadSEd Warnicke
76cb9cadadSEd Warnickevoid
77cb9cadadSEd Warnickepg_edit_set_value (pg_edit_t * e, int hi_or_lo, u64 value)
78cb9cadadSEd Warnicke{
79cb9cadadSEd Warnicke  pg_edit_alloc_value (e, hi_or_lo);
80cb9cadadSEd Warnicke  pg_edit_set_value_helper (e, value, e->values[hi_or_lo]);
81cb9cadadSEd Warnicke}
82cb9cadadSEd Warnicke
83cb9cadadSEd Warnicke/* Parse an int either %d or 0x%x into network byte order. */
8471e97c63SCalvinuword
8571e97c63SCalvinunformat_pg_number (unformat_input_t * input, va_list * args)
86cb9cadadSEd Warnicke{
8771e97c63SCalvin  u8 *result = va_arg (*args, u8 *);
8871e97c63SCalvin  pg_edit_t *e = va_arg (*args, pg_edit_t *);
89cb9cadadSEd Warnicke  u64 value;
90cb9cadadSEd Warnicke
91cb9cadadSEd Warnicke  ASSERT (BITS (value) >= e->n_bits);
92cb9cadadSEd Warnicke
9371e97c63SCalvin  if (!unformat (input, "0x%X", sizeof (value), &value)
9471e97c63SCalvin      && !unformat (input, "%D", sizeof (value), &value))
95cb9cadadSEd Warnicke    return 0;
96cb9cadadSEd Warnicke
97cb9cadadSEd Warnicke  /* Number given does not fit into bit field. */
9871e97c63SCalvin  if (e->n_bits < 64 && value >= (u64) 1 << (u64) e->n_bits)
99cb9cadadSEd Warnicke    return 0;
100cb9cadadSEd Warnicke
101cb9cadadSEd Warnicke  pg_edit_set_value_helper (e, value, result);
102cb9cadadSEd Warnicke  return 1;
103cb9cadadSEd Warnicke}
104cb9cadadSEd Warnicke
105cb9cadadSEd Warnickeuword
106cb9cadadSEd Warnickeunformat_pg_edit (unformat_input_t * input, va_list * args)
107cb9cadadSEd Warnicke{
10871e97c63SCalvin  unformat_function_t *f = va_arg (*args, unformat_function_t *);
10971e97c63SCalvin  pg_edit_t *e = va_arg (*args, pg_edit_t *);
110cb9cadadSEd Warnicke
111cb9cadadSEd Warnicke  pg_edit_alloc_value (e, PG_EDIT_LO);
11271e97c63SCalvin  if (!unformat_user (input, f, e->values[PG_EDIT_LO], e))
113cb9cadadSEd Warnicke    return 0;
114cb9cadadSEd Warnicke
115cb9cadadSEd Warnicke  pg_edit_alloc_value (e, PG_EDIT_HI);
116cb9cadadSEd Warnicke  if (unformat (input, "-%U", f, e->values[PG_EDIT_HI], e))
117cb9cadadSEd Warnicke    e->type = PG_EDIT_INCREMENT;
118cb9cadadSEd Warnicke  else if (unformat (input, "+%U", f, e->values[PG_EDIT_HI], e))
119cb9cadadSEd Warnicke    e->type = PG_EDIT_RANDOM;
120cb9cadadSEd Warnicke  else
121cb9cadadSEd Warnicke    e->type = PG_EDIT_FIXED;
122cb9cadadSEd Warnicke
123cb9cadadSEd Warnicke  return 1;
124cb9cadadSEd Warnicke}
125cb9cadadSEd Warnicke
126cb9cadadSEd Warnickeuword
127cb9cadadSEd Warnickeunformat_pg_payload (unformat_input_t * input, va_list * args)
128cb9cadadSEd Warnicke{
12971e97c63SCalvin  pg_stream_t *s = va_arg (*args, pg_stream_t *);
13064034367SDamjan Marion  vlib_main_t *vm = vlib_get_main ();
13171e97c63SCalvin  pg_edit_t *e;
132a052b78dSKingwel Xie  u32 i, node_index, len, max_len;
13371e97c63SCalvin  u8 *v;
13471e97c63SCalvin
135cb9cadadSEd Warnicke  v = 0;
136cb9cadadSEd Warnicke
137a052b78dSKingwel Xie  if (unformat (input, "incrementing %d", &len))
138a052b78dSKingwel Xie    {
139a052b78dSKingwel Xie      vec_resize (v, len);
140a052b78dSKingwel Xie      for (i = 0; i < len; i++)
141a052b78dSKingwel Xie	v[i] = i % len;
142a052b78dSKingwel Xie    }
143cb9cadadSEd Warnicke  else if (unformat (input, "hex 0x%U", unformat_hex_string, &v))
144cb9cadadSEd Warnicke    ;
145cb9cadadSEd Warnicke  else if (unformat (input, "%U", unformat_vlib_node, vm, &node_index))
146cb9cadadSEd Warnicke    {
14771e97c63SCalvin      pg_node_t *pn = pg_get_node (node_index);
14871e97c63SCalvin      if (!pn->unformat_edit)
149cb9cadadSEd Warnicke	return 0;
150cb9cadadSEd Warnicke      return unformat (input, "%U", pn->unformat_edit, s);
151cb9cadadSEd Warnicke    }
152cb9cadadSEd Warnicke
153cb9cadadSEd Warnicke  else
154cb9cadadSEd Warnicke    return 0;
155cb9cadadSEd Warnicke
156cb9cadadSEd Warnicke  /* Length not including this payload. */
157cb9cadadSEd Warnicke  max_len = pg_edit_group_n_bytes (s, 0);
158a052b78dSKingwel Xie
15949cd3badSKingwel Xie  if (max_len >= s->max_packet_bytes)
160cb9cadadSEd Warnicke    {
16149cd3badSKingwel Xie      /* no payload */
16249cd3badSKingwel Xie      len = 0;
163cb9cadadSEd Warnicke    }
16449cd3badSKingwel Xie  else
16549cd3badSKingwel Xie    {
16649cd3badSKingwel Xie      /* make a bigger v to hold the data */
16749cd3badSKingwel Xie      len = s->max_packet_bytes - max_len;
16849cd3badSKingwel Xie    }
16949cd3badSKingwel Xie
17049cd3badSKingwel Xie  vec_resize (v, len);
171cb9cadadSEd Warnicke
1728eb102c2SKingwel Xie  e = pg_create_edit_group (s, sizeof (e[0]), len, 0);
173cb9cadadSEd Warnicke
174cb9cadadSEd Warnicke  e->type = PG_EDIT_FIXED;
1758eb102c2SKingwel Xie  e->n_bits = len * BITS (v[0]);
176cb9cadadSEd Warnicke
177cb9cadadSEd Warnicke  /* Least significant bit is at end of bitstream, since everything is always bigendian. */
1788eb102c2SKingwel Xie  e->lsb_bit_offset = len > 0 ? e->n_bits - BITS (v[0]) : 0;
179cb9cadadSEd Warnicke
180cb9cadadSEd Warnicke  e->values[PG_EDIT_LO] = v;
181cb9cadadSEd Warnicke
182cb9cadadSEd Warnicke  return 1;
183cb9cadadSEd Warnicke}
18471e97c63SCalvin
18571e97c63SCalvin/*
18671e97c63SCalvin * fd.io coding-style-patch-verification: ON
18771e97c63SCalvin *
18871e97c63SCalvin * Local Variables:
18971e97c63SCalvin * eval: (c-set-style "gnu")
19071e97c63SCalvin * End:
19171e97c63SCalvin */
192