FD.io VPP  v21.06-3-gbb25fbf28
Vector Packet Processing
p2p_ethernet.c
Go to the documentation of this file.
1 /*
2  * p2p_ethernet.c: p2p ethernet
3  *
4  * Copyright (c) 2012 Cisco and/or its affiliates.
5  * Licensed under the Apache License, Version 2.0 (the "License");
6  * you may not use this file except in compliance with the License.
7  * You may obtain a copy of the License at:
8  *
9  * http://www.apache.org/licenses/LICENSE-2.0
10  *
11  * Unless required by applicable law or agreed to in writing, software
12  * distributed under the License is distributed on an "AS IS" BASIS,
13  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14  * See the License for the specific language governing permissions and
15  * limitations under the License.
16  */
17 
18 #include <vppinfra/bihash_16_8.h>
19 #include <vnet/vnet.h>
21 #include <vnet/l2/l2_input.h>
22 
24 
25 static void
26 create_p2pe_key (p2p_key_t * p2pe_key, u32 parent_if_index, u8 * client_mac)
27 {
28  clib_memcpy (p2pe_key->mac, client_mac, 6);
29  p2pe_key->pad1 = 0;
30  p2pe_key->hw_if_index = parent_if_index;
31  p2pe_key->pad2 = 0;
32 }
33 
34 u32
35 p2p_ethernet_lookup (u32 parent_if_index, u8 * client_mac)
36 {
38  p2p_key_t p2pe_key;
39  uword *p;
40 
41  create_p2pe_key (&p2pe_key, parent_if_index, client_mac);
42  p = hash_get_mem (p2pm->p2p_ethernet_by_key, &p2pe_key);
43  if (p)
44  return p[0];
45 
46  return ~0;
47 }
48 
49 int
50 p2p_ethernet_add_del (vlib_main_t * vm, u32 parent_if_index,
51  u8 * client_mac, u32 p2pe_subif_id, int is_add,
52  u32 * p2pe_if_index)
53 {
54  vnet_main_t *vnm = vnet_get_main ();
57 
58  u32 p2pe_sw_if_index = ~0;
59  p2pe_sw_if_index = p2p_ethernet_lookup (parent_if_index, client_mac);
60 
61  if (p2pe_if_index)
62  *p2pe_if_index = ~0;
63 
64  if (is_add)
65  {
66  if (p2pe_sw_if_index == ~0)
67  {
69 
70  hi = vnet_get_hw_interface (vnm, parent_if_index);
71  if (hi->bond_info == VNET_HW_INTERFACE_BOND_INFO_SLAVE)
72  return VNET_API_ERROR_BOND_SLAVE_NOT_ALLOWED;
73 
74  u64 sup_and_sub_key =
75  ((u64) (hi->sw_if_index) << 32) | (u64) p2pe_subif_id;
76  uword *p;
77  p = hash_get_mem (im->sw_if_index_by_sup_and_sub, &sup_and_sub_key);
78  if (p)
79  {
80  if (CLIB_DEBUG > 0)
82  ("p2p ethernet sub-interface on sw_if_index %d with sub id %d already exists\n",
83  hi->sw_if_index, p2pe_subif_id);
84  return VNET_API_ERROR_SUBIF_ALREADY_EXISTS;
85  }
86  vnet_sw_interface_t template = {
88  .flood_class = VNET_FLOOD_CLASS_NORMAL,
89  .sup_sw_if_index = hi->sw_if_index,
90  .sub.id = p2pe_subif_id
91  };
92 
93  clib_memcpy (template.p2p.client_mac, client_mac,
94  sizeof (template.p2p.client_mac));
95 
96  if (vnet_create_sw_interface (vnm, &template, &p2pe_sw_if_index))
97  return VNET_API_ERROR_SUBIF_CREATE_FAILED;
98 
99  /* Allocate counters for this interface. */
100  {
101  u32 i;
102 
104 
105  for (i = 0; i < vec_len (im->sw_if_counters); i++)
106  {
108  p2pe_sw_if_index);
110  p2pe_sw_if_index);
111  }
112 
113  for (i = 0; i < vec_len (im->combined_sw_if_counters); i++)
114  {
116  [i], p2pe_sw_if_index);
118  p2pe_sw_if_index);
119  }
120 
122  }
123 
125  sup_and_sub_key =
126  ((u64) (hi->sw_if_index) << 32) | (u64) p2pe_subif_id;
127  u64 *kp = clib_mem_alloc (sizeof (*kp));
128 
129  *kp = sup_and_sub_key;
130  hash_set (hi->sub_interface_sw_if_index_by_id, p2pe_subif_id,
131  p2pe_sw_if_index);
132  hash_set_mem (im->sw_if_index_by_sup_and_sub, kp, p2pe_sw_if_index);
133 
134  p2p_key_t *p_p2pe_key;
135  p_p2pe_key = clib_mem_alloc (sizeof (*p_p2pe_key));
136  create_p2pe_key (p_p2pe_key, parent_if_index, client_mac);
137  hash_set_mem (p2pm->p2p_ethernet_by_key, p_p2pe_key,
138  p2pe_sw_if_index);
139 
140  if (p2pe_if_index)
141  *p2pe_if_index = p2pe_sw_if_index;
142 
143  vec_validate (p2pm->p2p_ethernet_by_sw_if_index, parent_if_index);
144  if (p2pm->p2p_ethernet_by_sw_if_index[parent_if_index] == 0)
145  {
146  vnet_feature_enable_disable ("device-input",
147  "p2p-ethernet-input",
148  parent_if_index, 1, 0, 0);
149  /* Set promiscuous mode on the l2 interface */
150  ethernet_set_flags (vnm, parent_if_index,
152 
153  }
154  p2pm->p2p_ethernet_by_sw_if_index[parent_if_index]++;
155  /* set the interface mode */
156  set_int_l2_mode (vm, vnm, MODE_L3, p2pe_subif_id, 0,
157  L2_BD_PORT_TYPE_NORMAL, 0, 0);
158  return 0;
159  }
160  return VNET_API_ERROR_SUBIF_ALREADY_EXISTS;
161  }
162  else
163  {
164  if (p2pe_sw_if_index == ~0)
165  return VNET_API_ERROR_SUBIF_DOESNT_EXIST;
166  else
167  {
168  int rv = 0;
169  rv = vnet_delete_sub_interface (p2pe_sw_if_index);
170  if (!rv)
171  {
173  parent_if_index);
174  if (p2pm->p2p_ethernet_by_sw_if_index[parent_if_index] == 1)
175  {
176  vnet_feature_enable_disable ("device-input",
177  "p2p-ethernet-input",
178  parent_if_index, 0, 0, 0);
179  /* Disable promiscuous mode on the l2 interface */
180  ethernet_set_flags (vnm, parent_if_index, 0);
181  }
182  p2pm->p2p_ethernet_by_sw_if_index[parent_if_index]--;
183 
184  /* Remove p2p_ethernet from hash map */
185  p2p_key_t *p_p2pe_key;
186  p_p2pe_key = clib_mem_alloc (sizeof (*p_p2pe_key));
187  create_p2pe_key (p_p2pe_key, parent_if_index, client_mac);
188  hash_unset_mem (p2pm->p2p_ethernet_by_key, p_p2pe_key);
189  }
190  return rv;
191  }
192  }
193 }
194 
195 static clib_error_t *
197  vlib_cli_command_t * cmd)
198 {
199  vnet_main_t *vnm = vnet_get_main ();
200 
201  int is_add = 1;
202  int remote_mac = 0;
203  u32 hw_if_index = ~0;
204  u32 sub_id = ~0;
205  u8 client_mac[6];
206 
208  {
209  if (unformat
210  (input, "%U", unformat_vnet_hw_interface, vnm, &hw_if_index))
211  ;
212  else if (unformat (input, "%U", unformat_ethernet_address, &client_mac))
213  remote_mac = 1;
214  else if (unformat (input, "sub-id %d", &sub_id))
215  ;
216  else if (unformat (input, "del"))
217  is_add = 0;
218  else
219  break;
220  }
221 
222  if (hw_if_index == ~0)
223  return clib_error_return (0, "Please specify parent interface ...");
224  if (!remote_mac)
225  return clib_error_return (0, "Please specify client MAC address ...");
226  if (sub_id == ~0 && is_add)
227  return clib_error_return (0, "Please specify sub-interface id ...");
228 
229  u32 rv;
230  rv = p2p_ethernet_add_del (vm, hw_if_index, client_mac, sub_id, is_add, 0);
231  switch (rv)
232  {
233  case VNET_API_ERROR_BOND_SLAVE_NOT_ALLOWED:
234  return clib_error_return (0,
235  "not allowed as parent interface belongs to a BondEthernet interface");
236  case -1:
237  return clib_error_return (0,
238  "p2p ethernet for given parent interface and client mac already exists");
239  case -2:
240  return clib_error_return (0,
241  "couldn't create p2p ethernet subinterface");
242  case -3:
243  return clib_error_return (0,
244  "p2p ethernet for given parent interface and client mac doesn't exist");
245  default:
246  break;
247  }
248  return 0;
249 }
250 
252 {
253 .path = "p2p_ethernet ",.function = vnet_p2p_ethernet_add_del,.short_help =
254  "p2p_ethernet <intfc> <mac-address> [sub-id <id> | del]",};
255 
256 static clib_error_t *
258 {
259  p2p_ethernet_main_t *p2pm = &p2p_main;
260 
261  p2pm->vlib_main = vm;
262  p2pm->vnet_main = vnet_get_main ();
263  p2pm->p2p_ethernet_by_key =
264  hash_create_mem (0, sizeof (p2p_key_t), sizeof (uword));
265 
266  return 0;
267 }
268 
270 
271 /*
272  * fd.io coding-style-patch-verification: ON
273  *
274  * Local Variables:
275  * eval: (c-set-style "gnu")
276  * End:
277  */
im
vnet_interface_main_t * im
Definition: interface_output.c:395
p2p_main
p2p_ethernet_main_t p2p_main
Definition: p2p_ethernet.c:23
unformat_ethernet_address
uword unformat_ethernet_address(unformat_input_t *input, va_list *args)
Definition: format.c:233
p2p_ethernet_add_del_command
static vlib_cli_command_t p2p_ethernet_add_del_command
(constructor) VLIB_CLI_COMMAND (p2p_ethernet_add_del_command)
Definition: p2p_ethernet.c:251
vnet_sw_interface_t::type
vnet_sw_interface_type_t type
Definition: interface.h:870
vnet_sw_interface_t
Definition: interface.h:868
clib_memcpy
#define clib_memcpy(d, s, n)
Definition: string.h:197
ethernet_set_flags
u32 ethernet_set_flags(vnet_main_t *vnm, u32 hw_if_index, u32 flags)
Definition: interface.c:441
p2p_ethernet_main_t::p2p_ethernet_by_key
uword * p2p_ethernet_by_key
Hash mapping parent sw_if_index and client mac address to p2p_ethernet sub-interface.
Definition: p2p_ethernet.h:26
clib_error_return
#define clib_error_return(e, args...)
Definition: error.h:99
vlib_cli_command_t::path
char * path
Definition: cli.h:96
vnet_interface_main_t
Definition: interface.h:989
VNET_FLOOD_CLASS_NORMAL
@ VNET_FLOOD_CLASS_NORMAL
Definition: interface.h:818
p2p_ethernet_init
static clib_error_t * p2p_ethernet_init(vlib_main_t *vm)
Definition: p2p_ethernet.c:257
p2p_key_t::pad1
u16 pad1
Definition: p2p_ethernet.h:55
vlib_validate_combined_counter
void vlib_validate_combined_counter(vlib_combined_counter_main_t *cm, u32 index)
validate a combined counter
Definition: counter.c:119
vm
vlib_main_t * vm
X-connect all packets from the HOST to the PHY.
Definition: nat44_ei.c:3047
vnet_interface_main_t::sw_if_counters
vlib_simple_counter_main_t * sw_if_counters
Definition: interface.h:1022
hi
vl_api_ip4_address_t hi
Definition: arp.api:37
unformat_input_t
struct _unformat_input_t unformat_input_t
unformat
uword unformat(unformat_input_t *i, const char *fmt,...)
Definition: unformat.c:978
hash_set
#define hash_set(h, key, value)
Definition: hash.h:255
p2p_ethernet_add_del
int p2p_ethernet_add_del(vlib_main_t *vm, u32 parent_if_index, u8 *client_mac, u32 p2pe_subif_id, int is_add, u32 *p2pe_if_index)
Definition: p2p_ethernet.c:50
vec_len
#define vec_len(v)
Number of elements in vector (rvalue-only, NULL tolerant)
Definition: vec_bootstrap.h:142
vnet_get_hw_interface
static vnet_hw_interface_t * vnet_get_hw_interface(vnet_main_t *vnm, u32 hw_if_index)
Definition: interface_funcs.h:44
vnet_get_main
vnet_main_t * vnet_get_main(void)
Definition: pnat_test_stubs.h:56
unformat_check_input
static uword unformat_check_input(unformat_input_t *i)
Definition: format.h:163
vnet_create_sw_interface
clib_error_t * vnet_create_sw_interface(vnet_main_t *vnm, vnet_sw_interface_t *template, u32 *sw_if_index)
Definition: interface.c:619
MODE_L3
#define MODE_L3
Definition: l2_input.h:282
uword
u64 uword
Definition: types.h:112
vlib_zero_combined_counter
static void vlib_zero_combined_counter(vlib_combined_counter_main_t *cm, u32 index)
Clear a combined counter Clears the set of per-thread counters.
Definition: counter.h:298
hash_create_mem
#define hash_create_mem(elts, key_bytes, value_bytes)
Definition: hash.h:660
hash_set_mem
#define hash_set_mem(h, key, value)
Definition: hash.h:275
i
sll srl srl sll sra u16x4 i
Definition: vector_sse42.h:261
vec_validate
#define vec_validate(V, I)
Make sure vector is long enough for given index (no header, unspecified alignment)
Definition: vec.h:523
VLIB_CLI_COMMAND
#define VLIB_CLI_COMMAND(x,...)
Definition: cli.h:163
VNET_HW_INTERFACE_BOND_INFO_SLAVE
#define VNET_HW_INTERFACE_BOND_INFO_SLAVE
Definition: interface.h:715
p2p_ethernet_main_t::vnet_main
vnet_main_t * vnet_main
Definition: p2p_ethernet.h:35
unformat_vnet_hw_interface
unformat_function_t unformat_vnet_hw_interface
Definition: interface_funcs.h:460
vnet_p2p_ethernet_add_del
static clib_error_t * vnet_p2p_ethernet_add_del(vlib_main_t *vm, unformat_input_t *input, vlib_cli_command_t *cmd)
Definition: p2p_ethernet.c:196
vnet_interface_main_t::combined_sw_if_counters
vlib_combined_counter_main_t * combined_sw_if_counters
Definition: interface.h:1023
p2p_key_t
Key struct for P2P Ethernet Key fields: parent sw_if_index and client mac address all fields in NET b...
Definition: p2p_ethernet.h:53
l2_input.h
p2p_key_t::pad2
u32 pad2
Definition: p2p_ethernet.h:57
p2p_ethernet.h
set_int_l2_mode
u32 set_int_l2_mode(vlib_main_t *vm, vnet_main_t *vnet_main, u32 mode, u32 sw_if_index, u32 bd_index, l2_bd_port_type_t port_type, u32 shg, u32 xc_sw_if_index)
Set the subinterface to run in l2 or l3 mode.
Definition: l2_input.c:256
vnet_hw_interface_t
Definition: interface.h:638
vnet_main_t
Definition: vnet.h:76
vnet_interface_counter_unlock
static void vnet_interface_counter_unlock(vnet_interface_main_t *im)
Definition: interface.h:1057
L2_BD_PORT_TYPE_NORMAL
@ L2_BD_PORT_TYPE_NORMAL
Definition: l2_bd.h:28
hash_get_mem
#define hash_get_mem(h, key)
Definition: hash.h:269
u64
unsigned long u64
Definition: types.h:89
vlib_zero_simple_counter
static void vlib_zero_simple_counter(vlib_simple_counter_main_t *cm, u32 index)
Clear a simple counter Clears the set of per-thread u16 counters, and the u64 counter.
Definition: counter.h:154
p2p_ethernet_lookup
u32 p2p_ethernet_lookup(u32 parent_if_index, u8 *client_mac)
Definition: p2p_ethernet.c:35
create_p2pe_key
static void create_p2pe_key(p2p_key_t *p2pe_key, u32 parent_if_index, u8 *client_mac)
Definition: p2p_ethernet.c:26
p2p_ethernet_main_t::p2p_ethernet_by_sw_if_index
u32 * p2p_ethernet_by_sw_if_index
Definition: p2p_ethernet.h:28
u32
unsigned int u32
Definition: types.h:88
VLIB_INIT_FUNCTION
#define VLIB_INIT_FUNCTION(x)
Definition: init.h:172
p2p_key_t::hw_if_index
u32 hw_if_index
Definition: p2p_ethernet.h:56
vlib_validate_simple_counter
void vlib_validate_simple_counter(vlib_simple_counter_main_t *cm, u32 index)
validate a simple counter
Definition: counter.c:79
bihash_16_8.h
vnet_feature_enable_disable
int vnet_feature_enable_disable(const char *arc_name, const char *node_name, u32 sw_if_index, int enable_disable, void *feature_config, u32 n_feature_config_bytes)
Definition: pnat_test_stubs.h:50
vlib_main_t
Definition: main.h:102
p2p_ethernet_main_t::vlib_main
vlib_main_t * vlib_main
Definition: p2p_ethernet.h:34
u8
unsigned char u8
Definition: types.h:56
clib_error_t
Definition: clib_error.h:21
hash_unset_mem
#define hash_unset_mem(h, key)
Definition: hash.h:291
vlib_init_function_t
clib_error_t *() vlib_init_function_t(struct vlib_main_t *vm)
Definition: init.h:51
ETHERNET_INTERFACE_FLAG_ACCEPT_ALL
#define ETHERNET_INTERFACE_FLAG_ACCEPT_ALL
Definition: ethernet.h:163
vnet_interface_main_t::sw_if_index_by_sup_and_sub
uword * sw_if_index_by_sup_and_sub
Definition: interface.h:1017
VNET_SW_INTERFACE_TYPE_P2P
@ VNET_SW_INTERFACE_TYPE_P2P
Definition: interface.h:767
clib_warning
#define clib_warning(format, args...)
Definition: error.h:59
rv
int __clib_unused rv
Definition: application.c:491
p2p_key_t::mac
u8 mac[6]
Definition: p2p_ethernet.h:54
vnet.h
vlib_cli_command_t
Definition: cli.h:92
vnet_interface_counter_lock
static void vnet_interface_counter_lock(vnet_interface_main_t *im)
Definition: interface.h:1050
clib_mem_alloc
static void * clib_mem_alloc(uword size)
Definition: mem.h:253
vnet_delete_sub_interface
int vnet_delete_sub_interface(u32 sw_if_index)
Definition: interface.c:1095
vnet_main_t::interface_main
vnet_interface_main_t interface_main
Definition: vnet.h:81
UNFORMAT_END_OF_INPUT
#define UNFORMAT_END_OF_INPUT
Definition: format.h:137
p2p_ethernet_main_t
Definition: p2p_ethernet.h:22