e3f667b5ffee9134e99a3a12ae8f4bb649c59c30
[vpp.git] / src / vnet / ethernet / p2p_ethernet.c
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>
20 #include <vnet/ethernet/p2p_ethernet.h>
21 #include <vnet/l2/l2_input.h>
22
23 p2p_ethernet_main_t p2p_main;
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 {
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 }
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 ();
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
99           vnet_interface_main_t *im = &vnm->interface_main;
100           sup_and_sub_key =
101             ((u64) (hi->sw_if_index) << 32) | (u64) p2pe_subif_id;
102           u64 *kp = clib_mem_alloc (sizeof (*kp));
103
104           *kp = sup_and_sub_key;
105           hash_set (hi->sub_interface_sw_if_index_by_id, p2pe_subif_id,
106                     p2pe_sw_if_index);
107           hash_set_mem (im->sw_if_index_by_sup_and_sub, kp, p2pe_sw_if_index);
108
109           p2p_key_t *p_p2pe_key;
110           p_p2pe_key = clib_mem_alloc (sizeof (*p_p2pe_key));
111           create_p2pe_key (p_p2pe_key, parent_if_index, client_mac);
112           hash_set_mem (p2pm->p2p_ethernet_by_key, p_p2pe_key,
113                         p2pe_sw_if_index);
114
115           if (p2pe_if_index)
116             *p2pe_if_index = p2pe_sw_if_index;
117
118           vec_validate (p2pm->p2p_ethernet_by_sw_if_index, parent_if_index);
119           if (p2pm->p2p_ethernet_by_sw_if_index[parent_if_index] == 0)
120             {
121               vnet_feature_enable_disable ("device-input",
122                                            "p2p-ethernet-input",
123                                            parent_if_index, 1, 0, 0);
124               /* Set promiscuous mode on the l2 interface */
125               ethernet_set_flags (vnm, parent_if_index,
126                                   ETHERNET_INTERFACE_FLAG_ACCEPT_ALL);
127
128             }
129           p2pm->p2p_ethernet_by_sw_if_index[parent_if_index]++;
130           /* set the interface mode */
131           set_int_l2_mode (vm, vnm, MODE_L3, p2pe_subif_id, 0, 0, 0, 0);
132           return 0;
133         }
134       return VNET_API_ERROR_SUBIF_ALREADY_EXISTS;
135     }
136   else
137     {
138       if (p2pe_sw_if_index == ~0)
139         return VNET_API_ERROR_SUBIF_DOESNT_EXIST;
140       else
141         {
142           int rv = 0;
143           rv = vnet_delete_sub_interface (p2pe_sw_if_index);
144           if (!rv)
145             {
146               vec_validate (p2pm->p2p_ethernet_by_sw_if_index,
147                             parent_if_index);
148               if (p2pm->p2p_ethernet_by_sw_if_index[parent_if_index] == 1)
149                 {
150                   vnet_feature_enable_disable ("device-input",
151                                                "p2p-ethernet-input",
152                                                parent_if_index, 0, 0, 0);
153                   /* Disable promiscuous mode on the l2 interface */
154                   ethernet_set_flags (vnm, parent_if_index, 0);
155                 }
156               p2pm->p2p_ethernet_by_sw_if_index[parent_if_index]--;
157
158               /* Remove p2p_ethernet from hash map */
159               p2p_key_t *p_p2pe_key;
160               p_p2pe_key = clib_mem_alloc (sizeof (*p_p2pe_key));
161               create_p2pe_key (p_p2pe_key, parent_if_index, client_mac);
162               hash_unset_mem (p2pm->p2p_ethernet_by_key, p_p2pe_key);
163             }
164           return rv;
165         }
166     }
167 }
168
169 static clib_error_t *
170 vnet_p2p_ethernet_add_del (vlib_main_t * vm, unformat_input_t * input,
171                            vlib_cli_command_t * cmd)
172 {
173   vnet_main_t *vnm = vnet_get_main ();
174
175   int is_add = 1;
176   int remote_mac = 0;
177   u32 hw_if_index = ~0;
178   u32 sub_id = ~0;
179   u8 client_mac[6];
180
181   while (unformat_check_input (input) != UNFORMAT_END_OF_INPUT)
182     {
183       if (unformat
184           (input, "%U", unformat_vnet_hw_interface, vnm, &hw_if_index))
185         ;
186       else if (unformat (input, "%U", unformat_ethernet_address, &client_mac))
187         remote_mac = 1;
188       else if (unformat (input, "sub-id %d", &sub_id))
189         ;
190       else if (unformat (input, "del"))
191         is_add = 0;
192       else
193         break;
194     }
195
196   if (hw_if_index == ~0)
197     return clib_error_return (0, "Please specify parent interface ...");
198   if (!remote_mac)
199     return clib_error_return (0, "Please specify client MAC address ...");
200   if (sub_id == ~0 && is_add)
201     return clib_error_return (0, "Please specify sub-interface id ...");
202
203   u32 rv;
204   rv = p2p_ethernet_add_del (vm, hw_if_index, client_mac, sub_id, is_add, 0);
205   switch (rv)
206     {
207     case VNET_API_ERROR_BOND_SLAVE_NOT_ALLOWED:
208       return clib_error_return (0,
209                                 "not allowed as parent interface belongs to a BondEthernet interface");
210     case -1:
211       return clib_error_return (0,
212                                 "p2p ethernet for given parent interface and client mac already exists");
213     case -2:
214       return clib_error_return (0,
215                                 "couldn't create p2p ethernet subinterface");
216     case -3:
217       return clib_error_return (0,
218                                 "p2p ethernet for given parent interface and client mac doesn't exist");
219     default:
220       break;
221     }
222   return 0;
223 }
224
225 VLIB_CLI_COMMAND (p2p_ethernet_add_del_command, static) =
226 {
227 .path = "p2p_ethernet ",.function = vnet_p2p_ethernet_add_del,.short_help =
228     "p2p_ethernet <intfc> <mac-address> [sub-id <id> | del]",};
229
230 static clib_error_t *
231 p2p_ethernet_init (vlib_main_t * vm)
232 {
233   p2p_ethernet_main_t *p2pm = &p2p_main;
234
235   p2pm->vlib_main = vm;
236   p2pm->vnet_main = vnet_get_main ();
237   p2pm->p2p_ethernet_by_key =
238     hash_create_mem (0, sizeof (p2p_key_t), sizeof (uword));
239
240   return 0;
241 }
242
243 VLIB_INIT_FUNCTION (p2p_ethernet_init);
244
245 /*
246  * fd.io coding-style-patch-verification: ON
247  *
248  * Local Variables:
249  * eval: (c-set-style "gnu")
250  * End:
251  */