Set hardware iface state for loopback interfaces
[vpp.git] / vnet / vnet / ethernet / interface.c
1 /*
2  * Copyright (c) 2015 Cisco and/or its affiliates.
3  * Licensed under the Apache License, Version 2.0 (the "License");
4  * you may not use this file except in compliance with the License.
5  * You may obtain a copy of the License at:
6  *
7  *     http://www.apache.org/licenses/LICENSE-2.0
8  *
9  * Unless required by applicable law or agreed to in writing, software
10  * distributed under the License is distributed on an "AS IS" BASIS,
11  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12  * See the License for the specific language governing permissions and
13  * limitations under the License.
14  */
15 /*
16  * ethernet_interface.c: ethernet interfaces
17  *
18  * Copyright (c) 2008 Eliot Dresselhaus
19  *
20  * Permission is hereby granted, free of charge, to any person obtaining
21  * a copy of this software and associated documentation files (the
22  * "Software"), to deal in the Software without restriction, including
23  * without limitation the rights to use, copy, modify, merge, publish,
24  * distribute, sublicense, and/or sell copies of the Software, and to
25  * permit persons to whom the Software is furnished to do so, subject to
26  * the following conditions:
27  *
28  * The above copyright notice and this permission notice shall be
29  * included in all copies or substantial portions of the Software.
30  *
31  *  THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
32  *  EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
33  *  MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
34  *  NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
35  *  LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
36  *  OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
37  *  WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
38  */
39
40 #include <vnet/vnet.h>
41 #include <vnet/ip/ip.h>
42 #include <vnet/pg/pg.h>
43 #include <vnet/ethernet/ethernet.h>
44
45 static uword ethernet_set_rewrite (vnet_main_t * vnm,
46                                    u32 sw_if_index,
47                                    u32 l3_type,
48                                    void * dst_address,
49                                    void * rewrite,
50                                    uword max_rewrite_bytes)
51 {
52   vnet_sw_interface_t * sub_sw = vnet_get_sw_interface (vnm, sw_if_index);
53   vnet_sw_interface_t * sup_sw = vnet_get_sup_sw_interface (vnm, sw_if_index);
54   vnet_hw_interface_t * hw = vnet_get_sup_hw_interface (vnm, sw_if_index);
55   ethernet_main_t * em = &ethernet_main;
56   ethernet_interface_t * ei;
57   ethernet_header_t * h = rewrite;
58   ethernet_type_t type;
59   uword n_bytes = sizeof (h[0]);
60
61   if (sub_sw != sup_sw) {
62     if (sub_sw->sub.eth.flags.one_tag) {
63       n_bytes += sizeof (ethernet_vlan_header_t);
64     } else if (sub_sw->sub.eth.flags.two_tags) {
65       n_bytes += 2 * (sizeof (ethernet_vlan_header_t));
66     }
67     // Check for encaps that are not supported for L3 interfaces
68     if (!(sub_sw->sub.eth.flags.exact_match) ||
69         (sub_sw->sub.eth.flags.default_sub) ||
70         (sub_sw->sub.eth.flags.outer_vlan_id_any) ||
71         (sub_sw->sub.eth.flags.inner_vlan_id_any)) {
72       return 0;
73     }
74   }
75
76   if (n_bytes > max_rewrite_bytes)
77     return 0;
78
79   switch (l3_type) {
80 #define _(a,b) case VNET_L3_PACKET_TYPE_##a: type = ETHERNET_TYPE_##b; break
81     _ (IP4, IP4);
82     _ (IP6, IP6);
83     _ (MPLS_UNICAST, MPLS_UNICAST);
84     _ (MPLS_MULTICAST, MPLS_MULTICAST);
85     _ (ARP, ARP);
86 #undef _
87   default:
88     return 0;
89   }
90
91   ei = pool_elt_at_index (em->interfaces, hw->hw_instance);
92   memcpy (h->src_address, ei->address, sizeof (h->src_address));
93   if (dst_address)
94     memcpy (h->dst_address, dst_address, sizeof (h->dst_address));
95   else
96     memset (h->dst_address, ~0, sizeof (h->dst_address)); /* broadcast */
97
98   if (sub_sw->sub.eth.flags.one_tag) {
99     ethernet_vlan_header_t * outer = (void *) (h + 1);
100
101     h->type = sub_sw->sub.eth.flags.dot1ad ?
102               clib_host_to_net_u16 (ETHERNET_TYPE_DOT1AD) :
103               clib_host_to_net_u16 (ETHERNET_TYPE_VLAN);
104     outer->priority_cfi_and_id = clib_host_to_net_u16 (sub_sw->sub.eth.outer_vlan_id);
105     outer->type = clib_host_to_net_u16 (type);
106
107   } else if (sub_sw->sub.eth.flags.two_tags) {
108     ethernet_vlan_header_t * outer = (void *) (h + 1);
109     ethernet_vlan_header_t * inner = (void *) (outer + 1);
110
111     h->type = sub_sw->sub.eth.flags.dot1ad ?
112               clib_host_to_net_u16 (ETHERNET_TYPE_DOT1AD) :
113               clib_host_to_net_u16 (ETHERNET_TYPE_VLAN);
114     outer->priority_cfi_and_id = clib_host_to_net_u16 (sub_sw->sub.eth.outer_vlan_id);
115     outer->type = clib_host_to_net_u16 (ETHERNET_TYPE_VLAN);
116     inner->priority_cfi_and_id = clib_host_to_net_u16 (sub_sw->sub.eth.inner_vlan_id);
117     inner->type = clib_host_to_net_u16 (type);
118
119   } else {
120     h->type = clib_host_to_net_u16 (type);
121   }
122
123   return n_bytes;
124 }
125
126 VNET_HW_INTERFACE_CLASS (ethernet_hw_interface_class) = {
127   .name = "Ethernet",
128   .format_address = format_ethernet_address,
129   .format_header = format_ethernet_header_with_length,
130   .unformat_hw_address = unformat_ethernet_address,
131   .unformat_header = unformat_ethernet_header,
132   .set_rewrite = ethernet_set_rewrite,
133 };
134
135 uword unformat_ethernet_interface (unformat_input_t * input, va_list * args)
136 {
137   vnet_main_t * vnm = va_arg (*args, vnet_main_t *);
138   u32 * result = va_arg (*args, u32 *);
139   u32 hw_if_index;
140   ethernet_main_t * em = &ethernet_main;
141   ethernet_interface_t * eif;
142
143   if (! unformat_user (input, unformat_vnet_hw_interface, vnm, &hw_if_index))
144     return 0;
145
146   eif = ethernet_get_interface (em, hw_if_index);
147   if (eif)
148     {
149       *result =  hw_if_index;
150       return 1;
151     }
152   return 0;
153 }
154
155 clib_error_t *
156 ethernet_register_interface (vnet_main_t * vnm,
157                              u32 dev_class_index,
158                              u32 dev_instance,
159                              u8 * address,
160                              u32 * hw_if_index_return, 
161                              ethernet_flag_change_function_t flag_change)
162 {
163   ethernet_main_t * em = &ethernet_main;
164   ethernet_interface_t * ei;
165   vnet_hw_interface_t * hi;
166   clib_error_t * error = 0;
167   u32 hw_if_index;
168
169   pool_get (em->interfaces, ei);
170   ei->flag_change = flag_change;
171
172   hw_if_index = vnet_register_interface
173     (vnm,
174      dev_class_index, dev_instance,
175      ethernet_hw_interface_class.index,
176      ei - em->interfaces);
177   *hw_if_index_return = hw_if_index;
178
179   hi = vnet_get_hw_interface (vnm, hw_if_index);
180
181   ethernet_setup_node (vnm->vlib_main, hi->output_node_index);
182
183   hi->min_packet_bytes = ETHERNET_MIN_PACKET_BYTES;
184   hi->max_packet_bytes = ETHERNET_MAX_PACKET_BYTES;
185   hi->per_packet_overhead_bytes =
186     /* preamble */ 8 + /* inter frame gap */ 12;
187
188   /* Standard default ethernet MTU. */
189   hi->max_l3_packet_bytes[VLIB_RX] = hi->max_l3_packet_bytes[VLIB_TX] = 9000;
190
191   memcpy (ei->address, address, sizeof (ei->address));
192   vec_free (hi->hw_address);
193   vec_add (hi->hw_address, address, sizeof (ei->address));
194
195   if (error)
196     {
197       pool_put (em->interfaces, ei);
198       return error;
199     }
200   return error;
201 }
202                              
203 void
204 ethernet_delete_interface (vnet_main_t * vnm, u32 hw_if_index)
205 {
206   ethernet_main_t * em = &ethernet_main;
207   ethernet_interface_t * ei;
208   vnet_hw_interface_t * hi;
209   main_intf_t * main_intf;
210   vlan_table_t * vlan_table;
211   u32           idx;
212
213   hi = vnet_get_hw_interface (vnm, hw_if_index);
214   ei = pool_elt_at_index (em->interfaces, hi->hw_instance);
215
216   /* Delete vlan mapping table for dot1q and dot1ad. */
217   main_intf = vec_elt_at_index (em->main_intfs, hi->hw_if_index);
218   if (main_intf->dot1q_vlans) {
219     vlan_table = vec_elt_at_index (em->vlan_pool, main_intf->dot1q_vlans);
220     for (idx=0; idx<ETHERNET_N_VLAN; idx++ ) {
221       if (vlan_table->vlans[idx].qinqs) {
222         pool_put_index(em->qinq_pool, vlan_table->vlans[idx].qinqs);
223       }
224     }
225     pool_put_index(em->vlan_pool, main_intf->dot1q_vlans);
226   }
227   if (main_intf->dot1ad_vlans) {
228     vlan_table = vec_elt_at_index (em->vlan_pool, main_intf->dot1ad_vlans);
229     for (idx=0; idx<ETHERNET_N_VLAN; idx++ ) {
230       if (vlan_table->vlans[idx].qinqs) {
231         pool_put_index(em->qinq_pool, vlan_table->vlans[idx].qinqs);
232       }
233     }
234     pool_put_index(em->vlan_pool, main_intf->dot1ad_vlans);
235   }
236   
237   vnet_delete_hw_interface (vnm, hw_if_index);
238   pool_put (em->interfaces, ei);
239 }
240
241 u32 
242 ethernet_set_flags (vnet_main_t * vnm, u32 hw_if_index, u32 flags)
243 {
244   ethernet_main_t * em = &ethernet_main;
245   vnet_hw_interface_t * hi;
246   ethernet_interface_t * ei;
247   
248   hi = vnet_get_hw_interface (vnm, hw_if_index);
249
250   ASSERT (hi->hw_class_index == ethernet_hw_interface_class.index);
251
252   ei = pool_elt_at_index (em->interfaces, hi->hw_instance);
253   if (ei->flag_change)
254     return ei->flag_change (vnm, hi, flags);
255   return (u32)~0;
256 }
257
258 #define VNET_SIMULATED_ETHERNET_TX_NEXT_ETHERNET_INPUT VNET_INTERFACE_TX_N_NEXT
259
260 /* Echo packets back to ethernet input. */
261 static uword
262 simulated_ethernet_interface_tx (vlib_main_t * vm,
263                                  vlib_node_runtime_t * node,
264                                  vlib_frame_t * frame)
265 {
266   u32 n_left_from, n_left_to_next, n_copy, * from, * to_next;
267   u32 next_index = VNET_SIMULATED_ETHERNET_TX_NEXT_ETHERNET_INPUT;
268   u32 i;
269   vlib_buffer_t * b;
270
271   n_left_from = frame->n_vectors;
272   from = vlib_frame_args (frame);
273
274   while (n_left_from > 0)
275     {
276       vlib_get_next_frame (vm, node, next_index, to_next, n_left_to_next);
277
278       n_copy = clib_min (n_left_from, n_left_to_next);
279
280       memcpy (to_next, from, n_copy * sizeof (from[0]));
281       n_left_to_next -= n_copy;
282       n_left_from -= n_copy;
283       for (i = 0; i < n_copy; i++)
284         {
285           b = vlib_get_buffer (vm, from[i]);
286           /* Set up RX and TX indices as if received from a real driver */
287           vnet_buffer (b)->sw_if_index[VLIB_RX] = 
288               vnet_buffer (b)->sw_if_index[VLIB_TX];
289           vnet_buffer (b)->sw_if_index[VLIB_TX] = (u32) ~0;
290         }
291
292       vlib_put_next_frame (vm, node, next_index, n_left_to_next);
293     }
294
295   return n_left_from;
296 }
297
298 static u8 * format_simulated_ethernet_name (u8 * s, va_list * args)
299 {
300   u32 dev_instance = va_arg (*args, u32);
301   return format (s, "loop%d", dev_instance);
302 }
303
304 static clib_error_t *
305 simulated_ethernet_admin_up_down (vnet_main_t * vnm, u32 hw_if_index, u32 flags)
306 {
307   u32 hw_flags = (flags & VNET_SW_INTERFACE_FLAG_ADMIN_UP) ?
308       VNET_HW_INTERFACE_FLAG_LINK_UP : 0;
309   vnet_hw_interface_set_flags (vnm, hw_if_index, hw_flags);
310   return 0;
311 }
312
313 VNET_DEVICE_CLASS (ethernet_simulated_device_class) = {
314   .name = "Loopback",
315   .format_device_name = format_simulated_ethernet_name,
316   .tx_function = simulated_ethernet_interface_tx,
317   .admin_up_down_function = simulated_ethernet_admin_up_down,
318 };
319
320 int vnet_create_loopback_interface (u32 * sw_if_indexp, u8 *mac_address)
321 {
322   vnet_main_t * vnm = vnet_get_main();
323   vlib_main_t * vm = vlib_get_main();
324   clib_error_t * error;
325   static u32 instance;
326   u8 address[6];
327   u32 hw_if_index;
328   vnet_hw_interface_t * hw_if;
329   u32 slot;
330   int rv = 0;
331
332   ASSERT(sw_if_indexp);
333
334   *sw_if_indexp = (u32)~0;
335
336   memset (address, 0, sizeof (address));
337
338   /*
339    * Default MAC address (dead:0000:0000 + instance) is allocated
340    * if zero mac_address is configured. Otherwise, user-configurable MAC
341    * address is programmed on the loopback interface.
342    */
343   if (memcmp (address, mac_address, sizeof (address)))
344     memcpy (address, mac_address, sizeof (address));
345   else
346     {
347       address[0] = 0xde;
348       address[1] = 0xad;
349       address[5] = instance;
350     }
351
352   error = ethernet_register_interface
353     (vnm,
354      ethernet_simulated_device_class.index,
355      instance++,
356      address,
357      &hw_if_index, 
358      /* flag change */ 0);
359
360   if (error)
361     {
362       rv = VNET_API_ERROR_INVALID_REGISTRATION;
363       clib_error_report(error);
364       return rv;
365     }
366
367   hw_if = vnet_get_hw_interface (vnm, hw_if_index);
368   slot = vlib_node_add_named_next_with_slot
369     (vm, hw_if->tx_node_index,
370      "ethernet-input",
371      VNET_SIMULATED_ETHERNET_TX_NEXT_ETHERNET_INPUT);
372   ASSERT (slot == VNET_SIMULATED_ETHERNET_TX_NEXT_ETHERNET_INPUT);
373
374   {
375     vnet_sw_interface_t * si = vnet_get_hw_sw_interface (vnm, hw_if_index);
376     *sw_if_indexp = si->sw_if_index;
377   }
378
379   return 0;
380 }
381
382 static clib_error_t *
383 create_simulated_ethernet_interfaces (vlib_main_t * vm,
384                                       unformat_input_t * input,
385                                       vlib_cli_command_t * cmd)
386 {
387   int rv;
388   u32 sw_if_index;
389   u8 mac_address[6];
390
391   memset (mac_address, 0, sizeof (mac_address));
392
393   while (unformat_check_input (input) != UNFORMAT_END_OF_INPUT)
394     {
395       if (unformat (input, "mac %U", unformat_ethernet_address, mac_address))
396         ;
397       else
398         break;
399     }
400
401   rv = vnet_create_loopback_interface (&sw_if_index, mac_address);
402
403   if (rv)
404     return clib_error_return (0, "vnet_create_loopback_interface failed");
405
406   return 0;
407 }
408
409 VLIB_CLI_COMMAND (create_simulated_ethernet_interface_command, static) = {
410   .path = "loopback create-interface",
411   .short_help = "Create Loopback ethernet interface [mac <mac-addr>]",
412   .function = create_simulated_ethernet_interfaces,
413 };
414
415 ethernet_interface_t *
416 ethernet_get_interface (ethernet_main_t * em, u32 hw_if_index)
417 {
418   vnet_hw_interface_t * i = vnet_get_hw_interface (vnet_get_main(), hw_if_index);
419   return (i->hw_class_index == ethernet_hw_interface_class.index
420           ? pool_elt_at_index (em->interfaces, i->hw_instance)
421           : 0);
422 }
423
424 int vnet_delete_loopback_interface (u32 sw_if_index)
425 {
426   vnet_main_t * vnm = vnet_get_main();
427   vnet_sw_interface_t * si;
428
429   if (pool_is_free_index (vnm->interface_main.sw_interfaces,
430                           sw_if_index))
431     return VNET_API_ERROR_INVALID_SW_IF_INDEX;
432
433   si = vnet_get_sw_interface (vnm, sw_if_index);
434   ethernet_delete_interface (vnm, si->hw_if_index);
435
436   return 0;
437 }
438
439 static clib_error_t *
440 delete_simulated_ethernet_interfaces (vlib_main_t * vm,
441                                       unformat_input_t * input,
442                                       vlib_cli_command_t * cmd)
443 {
444   int rv;
445   u32 sw_if_index = ~0;
446   vnet_main_t * vnm = vnet_get_main();
447
448   while (unformat_check_input (input) != UNFORMAT_END_OF_INPUT)
449     {
450       if (unformat (input, "intfc %U",
451                     unformat_vnet_sw_interface, vnm, &sw_if_index))
452         ;
453       else
454         break;
455     }
456
457   if (sw_if_index == ~0)
458     return clib_error_return (0, "interface not specified");
459
460   rv = vnet_delete_loopback_interface (sw_if_index);
461
462   if (rv)
463     return clib_error_return (0, "vnet_delete_loopback_interface failed");
464
465   return 0;
466 }
467
468 VLIB_CLI_COMMAND (delete_simulated_ethernet_interface_command, static) = {
469   .path = "loopback delete-interface",
470   .short_help = "Delete Loopback ethernet interface intfc <interface>",
471   .function = delete_simulated_ethernet_interfaces,
472 };