2 * p2p_ethernet.c: p2p ethernet
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:
9 * http://www.apache.org/licenses/LICENSE-2.0
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.
18 #include <vppinfra/bihash_16_8.h>
19 #include <vnet/vnet.h>
20 #include <vnet/ethernet/p2p_ethernet.h>
21 #include <vnet/l2/l2_input.h>
23 p2p_ethernet_main_t p2p_main;
26 create_p2pe_key (p2p_key_t * p2pe_key, u32 parent_if_index, u8 * client_mac)
28 clib_memcpy (p2pe_key->mac, client_mac, 6);
30 p2pe_key->hw_if_index = parent_if_index;
35 p2p_ethernet_lookup (u32 parent_if_index, u8 * client_mac)
37 p2p_ethernet_main_t *p2pm = &p2p_main;
41 create_p2pe_key (&p2pe_key, parent_if_index, client_mac);
42 p = hash_get_mem (p2pm->p2p_ethernet_by_key, &p2pe_key);
50 p2p_ethernet_add_del (vlib_main_t * vm, u32 parent_if_index,
51 u8 * client_mac, u32 p2pe_subif_id, int is_add,
54 vnet_main_t *vnm = vnet_get_main ();
55 p2p_ethernet_main_t *p2pm = &p2p_main;
56 vnet_interface_main_t *im = &vnm->interface_main;
58 u32 p2pe_sw_if_index = ~0;
59 p2pe_sw_if_index = p2p_ethernet_lookup (parent_if_index, client_mac);
66 if (p2pe_sw_if_index == ~0)
68 vnet_hw_interface_t *hi;
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;
75 ((u64) (hi->sw_if_index) << 32) | (u64) p2pe_subif_id;
77 p = hash_get_mem (im->sw_if_index_by_sup_and_sub, &sup_and_sub_key);
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;
86 vnet_sw_interface_t template = {
87 .type = VNET_SW_INTERFACE_TYPE_P2P,
88 .flood_class = VNET_FLOOD_CLASS_NORMAL,
89 .sup_sw_if_index = hi->sw_if_index,
90 .sub.id = p2pe_subif_id
93 clib_memcpy (template.p2p.client_mac, client_mac,
94 sizeof (template.p2p.client_mac));
96 if (vnet_create_sw_interface (vnm, &template, &p2pe_sw_if_index))
97 return VNET_API_ERROR_SUBIF_CREATE_FAILED;
99 /* Allocate counters for this interface. */
103 vnet_interface_counter_lock (im);
105 for (i = 0; i < vec_len (im->sw_if_counters); i++)
107 vlib_validate_simple_counter (&im->sw_if_counters[i],
109 vlib_zero_simple_counter (&im->sw_if_counters[i],
113 for (i = 0; i < vec_len (im->combined_sw_if_counters); i++)
115 vlib_validate_combined_counter (&im->combined_sw_if_counters
116 [i], p2pe_sw_if_index);
117 vlib_zero_combined_counter (&im->combined_sw_if_counters[i],
121 vnet_interface_counter_unlock (im);
124 vnet_interface_main_t *im = &vnm->interface_main;
126 ((u64) (hi->sw_if_index) << 32) | (u64) p2pe_subif_id;
127 u64 *kp = clib_mem_alloc (sizeof (*kp));
129 *kp = sup_and_sub_key;
130 hash_set (hi->sub_interface_sw_if_index_by_id, p2pe_subif_id,
132 hash_set_mem (im->sw_if_index_by_sup_and_sub, kp, p2pe_sw_if_index);
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,
141 *p2pe_if_index = p2pe_sw_if_index;
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)
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,
151 ETHERNET_INTERFACE_FLAG_ACCEPT_ALL);
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);
160 return VNET_API_ERROR_SUBIF_ALREADY_EXISTS;
164 if (p2pe_sw_if_index == ~0)
165 return VNET_API_ERROR_SUBIF_DOESNT_EXIST;
169 rv = vnet_delete_sub_interface (p2pe_sw_if_index);
172 vec_validate (p2pm->p2p_ethernet_by_sw_if_index,
174 if (p2pm->p2p_ethernet_by_sw_if_index[parent_if_index] == 1)
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);
182 p2pm->p2p_ethernet_by_sw_if_index[parent_if_index]--;
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);
195 static clib_error_t *
196 vnet_p2p_ethernet_add_del (vlib_main_t * vm, unformat_input_t * input,
197 vlib_cli_command_t * cmd)
199 vnet_main_t *vnm = vnet_get_main ();
203 u32 hw_if_index = ~0;
207 while (unformat_check_input (input) != UNFORMAT_END_OF_INPUT)
210 (input, "%U", unformat_vnet_hw_interface, vnm, &hw_if_index))
212 else if (unformat (input, "%U", unformat_ethernet_address, &client_mac))
214 else if (unformat (input, "sub-id %d", &sub_id))
216 else if (unformat (input, "del"))
222 if (hw_if_index == ~0)
223 return clib_error_return (0, "Please specify parent interface ...");
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 ...");
230 rv = p2p_ethernet_add_del (vm, hw_if_index, client_mac, sub_id, is_add, 0);
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");
237 return clib_error_return (0,
238 "p2p ethernet for given parent interface and client mac already exists");
240 return clib_error_return (0,
241 "couldn't create p2p ethernet subinterface");
243 return clib_error_return (0,
244 "p2p ethernet for given parent interface and client mac doesn't exist");
251 VLIB_CLI_COMMAND (p2p_ethernet_add_del_command, static) =
253 .path = "p2p_ethernet ",.function = vnet_p2p_ethernet_add_del,.short_help =
254 "p2p_ethernet <intfc> <mac-address> [sub-id <id> | del]",};
256 static clib_error_t *
257 p2p_ethernet_init (vlib_main_t * vm)
259 p2p_ethernet_main_t *p2pm = &p2p_main;
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));
269 VLIB_INIT_FUNCTION (p2p_ethernet_init);
272 * fd.io coding-style-patch-verification: ON
275 * eval: (c-set-style "gnu")