blob: 0ece84fd9ccdaa79d5f1f0038f35dd702b4be9ec [file] [log] [blame]
Pavel Kotucek6899a302017-06-08 08:46:10 +02001/*
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>
20#include <vnet/ethernet/p2p_ethernet.h>
Pavel Kotucek15ac81c2017-06-20 14:00:26 +020021#include <vnet/l2/l2_input.h>
22
23p2p_ethernet_main_t p2p_main;
24
25static void
26create_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
34u32
35p2p_ethernet_lookup (u32 parent_if_index, u8 * client_mac)
36{
37 p2p_ethernet_main_t *p2pm = &p2p_main;
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}
Pavel Kotucek6899a302017-06-08 08:46:10 +020048
49int
50p2p_ethernet_add_del (vlib_main_t * vm, u32 parent_if_index,
Pavel Kotucek15ac81c2017-06-20 14:00:26 +020051 u8 * client_mac, u32 p2pe_subif_id, int is_add,
52 u32 * p2pe_if_index)
Pavel Kotucek6899a302017-06-08 08:46:10 +020053{
Pavel Kotucek15ac81c2017-06-20 14:00:26 +020054 vnet_main_t *vnm = vnet_get_main ();
55 p2p_ethernet_main_t *p2pm = &p2p_main;
56 vnet_interface_main_t *im = &vnm->interface_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 {
68 vnet_hw_interface_t *hi;
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)
81 clib_warning
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 = {
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
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
Pavel Kotucek4f80b812017-08-15 13:13:28 +020099 /* Allocate counters for this interface. */
100 {
101 u32 i;
102
103 vnet_interface_counter_lock (im);
104
105 for (i = 0; i < vec_len (im->sw_if_counters); i++)
106 {
107 vlib_validate_simple_counter (&im->sw_if_counters[i],
108 p2pe_sw_if_index);
109 vlib_zero_simple_counter (&im->sw_if_counters[i],
110 p2pe_sw_if_index);
111 }
112
113 for (i = 0; i < vec_len (im->combined_sw_if_counters); i++)
114 {
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],
118 p2pe_sw_if_index);
119 }
120
121 vnet_interface_counter_unlock (im);
122 }
123
Pavel Kotucek15ac81c2017-06-20 14:00:26 +0200124 vnet_interface_main_t *im = &vnm->interface_main;
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);
Damjan Marion38c61912023-10-17 16:06:26 +0000149 vnet_feature_enable_disable ("port-rx-eth", "p2p-ethernet-input",
150 parent_if_index, 1, 0, 0);
Pavel Kotucek15ac81c2017-06-20 14:00:26 +0200151 /* Set promiscuous mode on the l2 interface */
152 ethernet_set_flags (vnm, parent_if_index,
153 ETHERNET_INTERFACE_FLAG_ACCEPT_ALL);
154
155 }
156 p2pm->p2p_ethernet_by_sw_if_index[parent_if_index]++;
157 /* set the interface mode */
Xiaoming Jiange10c5242023-05-06 08:22:08 +0000158 set_int_l2_mode (vm, vnm, MODE_L3, p2pe_sw_if_index, 0,
Neale Rannsb4743802018-09-05 09:13:57 -0700159 L2_BD_PORT_TYPE_NORMAL, 0, 0);
Pavel Kotucek15ac81c2017-06-20 14:00:26 +0200160 return 0;
161 }
162 return VNET_API_ERROR_SUBIF_ALREADY_EXISTS;
163 }
164 else
165 {
166 if (p2pe_sw_if_index == ~0)
167 return VNET_API_ERROR_SUBIF_DOESNT_EXIST;
168 else
169 {
170 int rv = 0;
171 rv = vnet_delete_sub_interface (p2pe_sw_if_index);
172 if (!rv)
173 {
174 vec_validate (p2pm->p2p_ethernet_by_sw_if_index,
175 parent_if_index);
176 if (p2pm->p2p_ethernet_by_sw_if_index[parent_if_index] == 1)
177 {
178 vnet_feature_enable_disable ("device-input",
179 "p2p-ethernet-input",
180 parent_if_index, 0, 0, 0);
Damjan Marion38c61912023-10-17 16:06:26 +0000181 vnet_feature_enable_disable ("port-rx-eth",
182 "p2p-ethernet-input",
183 parent_if_index, 0, 0, 0);
Pavel Kotucek15ac81c2017-06-20 14:00:26 +0200184 /* Disable promiscuous mode on the l2 interface */
185 ethernet_set_flags (vnm, parent_if_index, 0);
186 }
187 p2pm->p2p_ethernet_by_sw_if_index[parent_if_index]--;
188
189 /* Remove p2p_ethernet from hash map */
190 p2p_key_t *p_p2pe_key;
191 p_p2pe_key = clib_mem_alloc (sizeof (*p_p2pe_key));
192 create_p2pe_key (p_p2pe_key, parent_if_index, client_mac);
193 hash_unset_mem (p2pm->p2p_ethernet_by_key, p_p2pe_key);
194 }
195 return rv;
196 }
197 }
Pavel Kotucek6899a302017-06-08 08:46:10 +0200198}
199
200static clib_error_t *
201vnet_p2p_ethernet_add_del (vlib_main_t * vm, unformat_input_t * input,
202 vlib_cli_command_t * cmd)
203{
204 vnet_main_t *vnm = vnet_get_main ();
205
206 int is_add = 1;
207 int remote_mac = 0;
208 u32 hw_if_index = ~0;
Pavel Kotucek15ac81c2017-06-20 14:00:26 +0200209 u32 sub_id = ~0;
Pavel Kotucek6899a302017-06-08 08:46:10 +0200210 u8 client_mac[6];
211
212 while (unformat_check_input (input) != UNFORMAT_END_OF_INPUT)
213 {
214 if (unformat
215 (input, "%U", unformat_vnet_hw_interface, vnm, &hw_if_index))
216 ;
217 else if (unformat (input, "%U", unformat_ethernet_address, &client_mac))
218 remote_mac = 1;
Pavel Kotucek15ac81c2017-06-20 14:00:26 +0200219 else if (unformat (input, "sub-id %d", &sub_id))
220 ;
Pavel Kotucek6899a302017-06-08 08:46:10 +0200221 else if (unformat (input, "del"))
222 is_add = 0;
223 else
224 break;
225 }
226
227 if (hw_if_index == ~0)
228 return clib_error_return (0, "Please specify parent interface ...");
229 if (!remote_mac)
230 return clib_error_return (0, "Please specify client MAC address ...");
Pavel Kotucek15ac81c2017-06-20 14:00:26 +0200231 if (sub_id == ~0 && is_add)
232 return clib_error_return (0, "Please specify sub-interface id ...");
Pavel Kotucek6899a302017-06-08 08:46:10 +0200233
234 u32 rv;
Pavel Kotucek15ac81c2017-06-20 14:00:26 +0200235 rv = p2p_ethernet_add_del (vm, hw_if_index, client_mac, sub_id, is_add, 0);
Pavel Kotucek6899a302017-06-08 08:46:10 +0200236 switch (rv)
237 {
238 case VNET_API_ERROR_BOND_SLAVE_NOT_ALLOWED:
239 return clib_error_return (0,
240 "not allowed as parent interface belongs to a BondEthernet interface");
241 case -1:
242 return clib_error_return (0,
243 "p2p ethernet for given parent interface and client mac already exists");
244 case -2:
245 return clib_error_return (0,
246 "couldn't create p2p ethernet subinterface");
247 case -3:
248 return clib_error_return (0,
249 "p2p ethernet for given parent interface and client mac doesn't exist");
250 default:
251 break;
252 }
253 return 0;
254}
255
Xiaoming Jiange10c5242023-05-06 08:22:08 +0000256VLIB_CLI_COMMAND (p2p_ethernet_add_del_command, static) = {
257 .path = "p2p_ethernet",
258 .function = vnet_p2p_ethernet_add_del,
259 .short_help = "p2p_ethernet <intfc> <mac-address> [sub-id <id>|del]",
260};
Pavel Kotucek6899a302017-06-08 08:46:10 +0200261
262static clib_error_t *
263p2p_ethernet_init (vlib_main_t * vm)
264{
Pavel Kotucek15ac81c2017-06-20 14:00:26 +0200265 p2p_ethernet_main_t *p2pm = &p2p_main;
266
267 p2pm->vlib_main = vm;
268 p2pm->vnet_main = vnet_get_main ();
269 p2pm->p2p_ethernet_by_key =
270 hash_create_mem (0, sizeof (p2p_key_t), sizeof (uword));
271
Pavel Kotucek6899a302017-06-08 08:46:10 +0200272 return 0;
273}
274
275VLIB_INIT_FUNCTION (p2p_ethernet_init);
276
277/*
278 * fd.io coding-style-patch-verification: ON
279 *
280 * Local Variables:
281 * eval: (c-set-style "gnu")
282 * End:
283 */