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 * ethernet_pg.c: packet generator ethernet interface
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#include <vnet/ethernet/ethernet.h>
43cb9cadadSEd Warnicke
44e70dcc86SKeith Burns (alagalah)typedef struct
45e70dcc86SKeith Burns (alagalah){
46cb9cadadSEd Warnicke  pg_edit_t type;
47cb9cadadSEd Warnicke  pg_edit_t src_address;
48cb9cadadSEd Warnicke  pg_edit_t dst_address;
49cb9cadadSEd Warnicke} pg_ethernet_header_t;
50cb9cadadSEd Warnicke
51cb9cadadSEd Warnickestatic inline void
52cb9cadadSEd Warnickepg_ethernet_header_init (pg_ethernet_header_t * e)
53cb9cadadSEd Warnicke{
54cb9cadadSEd Warnicke  pg_edit_init (&e->type, ethernet_header_t, type);
55cb9cadadSEd Warnicke  pg_edit_init (&e->src_address, ethernet_header_t, src_address);
56cb9cadadSEd Warnicke  pg_edit_init (&e->dst_address, ethernet_header_t, dst_address);
57cb9cadadSEd Warnicke}
58cb9cadadSEd Warnicke
59e70dcc86SKeith Burns (alagalah)typedef struct
60e70dcc86SKeith Burns (alagalah){
61cb9cadadSEd Warnicke  pg_edit_t type;
62cb9cadadSEd Warnicke  pg_edit_t id;
63cb9cadadSEd Warnicke  pg_edit_t cfi;
64cb9cadadSEd Warnicke  pg_edit_t priority;
65cb9cadadSEd Warnicke} pg_ethernet_vlan_header_t;
66cb9cadadSEd Warnicke
67cb9cadadSEd Warnickestatic inline void
68e70dcc86SKeith Burns (alagalah)pg_ethernet_vlan_header_init (pg_ethernet_vlan_header_t * v, int vlan_index)
69cb9cadadSEd Warnicke{
70cb9cadadSEd Warnicke  ASSERT (vlan_index < ARRAY_LEN (((ethernet_max_header_t *) 0)->vlan));
71cb9cadadSEd Warnicke  pg_edit_init (&v->type, ethernet_max_header_t, vlan[vlan_index].type);
72cb9cadadSEd Warnicke
73cb9cadadSEd Warnicke  pg_edit_init_bitfield (&v->id, ethernet_max_header_t,
74e70dcc86SKeith Burns (alagalah)			 vlan[vlan_index].priority_cfi_and_id, 0, 12);
75cb9cadadSEd Warnicke  pg_edit_init_bitfield (&v->cfi, ethernet_max_header_t,
76e70dcc86SKeith Burns (alagalah)			 vlan[vlan_index].priority_cfi_and_id, 12, 1);
77cb9cadadSEd Warnicke  pg_edit_init_bitfield (&v->priority, ethernet_max_header_t,
78e70dcc86SKeith Burns (alagalah)			 vlan[vlan_index].priority_cfi_and_id, 13, 3);
79cb9cadadSEd Warnicke}
80cb9cadadSEd Warnicke
81cb9cadadSEd Warnickeuword
82cb9cadadSEd Warnickeunformat_pg_ethernet_header (unformat_input_t * input, va_list * args)
83cb9cadadSEd Warnicke{
84e70dcc86SKeith Burns (alagalah)  pg_stream_t *s = va_arg (*args, pg_stream_t *);
85e70dcc86SKeith Burns (alagalah)  pg_ethernet_header_t *e;
86e70dcc86SKeith Burns (alagalah)  pg_ethernet_vlan_header_t *v;
87e70dcc86SKeith Burns (alagalah)  pg_edit_t *ether_type_edit;
88cb9cadadSEd Warnicke  u32 n_vlan, error, group_index;
89e70dcc86SKeith Burns (alagalah)
90cb9cadadSEd Warnicke  e = pg_create_edit_group (s, sizeof (e[0]), sizeof (ethernet_header_t),
91cb9cadadSEd Warnicke			    &group_index);
92cb9cadadSEd Warnicke  pg_ethernet_header_init (e);
93cb9cadadSEd Warnicke  error = 1;
94cb9cadadSEd Warnicke
95e70dcc86SKeith Burns (alagalah)  if (!unformat (input, "%U: %U -> %U",
96e70dcc86SKeith Burns (alagalah)		 unformat_pg_edit,
97e70dcc86SKeith Burns (alagalah)		 unformat_ethernet_type_net_byte_order, &e->type,
98e70dcc86SKeith Burns (alagalah)		 unformat_pg_edit,
99e70dcc86SKeith Burns (alagalah)		 unformat_ethernet_address, &e->src_address,
100e70dcc86SKeith Burns (alagalah)		 unformat_pg_edit,
101e70dcc86SKeith Burns (alagalah)		 unformat_ethernet_address, &e->dst_address))
102cb9cadadSEd Warnicke    goto done;
103cb9cadadSEd Warnicke
104cb9cadadSEd Warnicke  n_vlan = 0;
105cb9cadadSEd Warnicke  while (unformat (input, "vlan"))
106cb9cadadSEd Warnicke    {
107cb9cadadSEd Warnicke      v = pg_add_edits (s, sizeof (v[0]), sizeof (ethernet_vlan_header_t),
108cb9cadadSEd Warnicke			group_index);
109cb9cadadSEd Warnicke      pg_ethernet_vlan_header_init (v, n_vlan);
110cb9cadadSEd Warnicke
111e70dcc86SKeith Burns (alagalah)      if (!unformat_user (input, unformat_pg_edit,
112e70dcc86SKeith Burns (alagalah)			  unformat_pg_number, &v->id))
113cb9cadadSEd Warnicke	goto done;
114cb9cadadSEd Warnicke
115e70dcc86SKeith Burns (alagalah)      if (!unformat (input, "priority %U", unformat_pg_edit,
116e70dcc86SKeith Burns (alagalah)		     unformat_pg_number, &v->priority))
117cb9cadadSEd Warnicke	pg_edit_set_fixed (&v->priority, 0);
118cb9cadadSEd Warnicke
119e70dcc86SKeith Burns (alagalah)      if (!unformat (input, "cfi %U", unformat_pg_edit,
120e70dcc86SKeith Burns (alagalah)		     unformat_pg_number, &v->cfi))
121cb9cadadSEd Warnicke	pg_edit_set_fixed (&v->cfi, 0);
122cb9cadadSEd Warnicke
123cb9cadadSEd Warnicke      /* Too many vlans given. */
124cb9cadadSEd Warnicke      if (n_vlan >= 2)
125cb9cadadSEd Warnicke	goto done;
126cb9cadadSEd Warnicke
127cb9cadadSEd Warnicke      n_vlan++;
128cb9cadadSEd Warnicke    }
129cb9cadadSEd Warnicke
130cb9cadadSEd Warnicke  /* Address of e may have changed due to vlan edits being added */
131cb9cadadSEd Warnicke  e = pg_get_edit_group (s, group_index);
132cb9cadadSEd Warnicke  v = (void *) (e + 1);
133cb9cadadSEd Warnicke
134cb9cadadSEd Warnicke  /* Correct types for vlan packets. */
135cb9cadadSEd Warnicke  ether_type_edit = &e->type;
136cb9cadadSEd Warnicke  if (n_vlan > 0)
137cb9cadadSEd Warnicke    {
138cb9cadadSEd Warnicke      int i;
139cb9cadadSEd Warnicke
140cb9cadadSEd Warnicke      ether_type_edit = &v[n_vlan - 1].type;
141cb9cadadSEd Warnicke      pg_edit_copy_type_and_values (ether_type_edit, &e->type);
142cb9cadadSEd Warnicke      pg_edit_set_fixed (&e->type, ETHERNET_TYPE_VLAN);
143cb9cadadSEd Warnicke
144cb9cadadSEd Warnicke      for (i = 0; i < n_vlan - 1; i++)
145cb9cadadSEd Warnicke	pg_edit_set_fixed (&v[i].type, ETHERNET_TYPE_VLAN);
146cb9cadadSEd Warnicke    }
147cb9cadadSEd Warnicke
148cb9cadadSEd Warnicke  {
149e70dcc86SKeith Burns (alagalah)    ethernet_main_t *em = &ethernet_main;
150e70dcc86SKeith Burns (alagalah)    ethernet_type_info_t *ti = 0;
151e70dcc86SKeith Burns (alagalah)    pg_node_t *pg_node = 0;
152cb9cadadSEd Warnicke
153cb9cadadSEd Warnicke    if (ether_type_edit->type == PG_EDIT_FIXED)
154cb9cadadSEd Warnicke      {
155cb9cadadSEd Warnicke	u16 t = *(u16 *) ether_type_edit->values[PG_EDIT_LO];
156cb9cadadSEd Warnicke	ti = ethernet_get_type_info (em, clib_net_to_host_u16 (t));
157cb9cadadSEd Warnicke	if (ti && ti->node_index != ~0)
158cb9cadadSEd Warnicke	  pg_node = pg_get_node (ti->node_index);
159cb9cadadSEd Warnicke      }
160cb9cadadSEd Warnicke
161cb9cadadSEd Warnicke    if (pg_node && pg_node->unformat_edit
162cb9cadadSEd Warnicke	&& unformat_user (input, pg_node->unformat_edit, s))
163cb9cadadSEd Warnicke      ;
164e70dcc86SKeith Burns (alagalah)    else if (!unformat_user (input, unformat_pg_payload, s))
165cb9cadadSEd Warnicke      goto done;
166cb9cadadSEd Warnicke  }
167cb9cadadSEd Warnicke
168cb9cadadSEd Warnicke  error = 0;
169cb9cadadSEd Warnicke
170e70dcc86SKeith Burns (alagalah)done:
171cb9cadadSEd Warnicke  if (error)
172cb9cadadSEd Warnicke    pg_free_edit_group (s);
173cb9cadadSEd Warnicke  return error == 0;
174cb9cadadSEd Warnicke}
175cb9cadadSEd Warnicke
176e70dcc86SKeith Burns (alagalah)
177e70dcc86SKeith Burns (alagalah)/*
178e70dcc86SKeith Burns (alagalah) * fd.io coding-style-patch-verification: ON
179e70dcc86SKeith Burns (alagalah) *
180e70dcc86SKeith Burns (alagalah) * Local Variables:
181e70dcc86SKeith Burns (alagalah) * eval: (c-set-style "gnu")
182e70dcc86SKeith Burns (alagalah) * End:
183e70dcc86SKeith Burns (alagalah) */
184