Initial commit of vpp code.
[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] = 1500;
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 VNET_DEVICE_CLASS (ethernet_simulated_device_class) = {
305   .name = "Loopback",
306   .format_device_name = format_simulated_ethernet_name,
307   .tx_function = simulated_ethernet_interface_tx,
308 };
309
310 int vnet_create_loopback_interface (u32 * sw_if_indexp, u8 *mac_address)
311 {
312   vnet_main_t * vnm = vnet_get_main();
313   vlib_main_t * vm = vlib_get_main();
314   clib_error_t * error;
315   static u32 instance;
316   u8 address[6];
317   u32 hw_if_index;
318   vnet_hw_interface_t * hw_if;
319   u32 slot;
320   int rv = 0;
321
322   ASSERT(sw_if_indexp);
323
324   *sw_if_indexp = (u32)~0;
325
326   memset (address, 0, sizeof (address));
327
328   /*
329    * Default MAC address (dead:0000:0000 + instance) is allocated
330    * if zero mac_address is configured. Otherwise, user-configurable MAC
331    * address is programmed on the loopback interface.
332    */
333   if (memcmp (address, mac_address, sizeof (address)))
334     memcpy (address, mac_address, sizeof (address));
335   else
336     {
337       address[0] = 0xde;
338       address[1] = 0xad;
339       address[5] = instance;
340     }
341
342   error = ethernet_register_interface
343     (vnm,
344      ethernet_simulated_device_class.index,
345      instance++,
346      address,
347      &hw_if_index, 
348      /* flag change */ 0);
349
350   if (error)
351     {
352       rv = VNET_API_ERROR_INVALID_REGISTRATION;
353       clib_error_report(error);
354       return rv;
355     }
356
357   hw_if = vnet_get_hw_interface (vnm, hw_if_index);
358   slot = vlib_node_add_named_next_with_slot
359     (vm, hw_if->tx_node_index,
360      "ethernet-input",
361      VNET_SIMULATED_ETHERNET_TX_NEXT_ETHERNET_INPUT);
362   ASSERT (slot == VNET_SIMULATED_ETHERNET_TX_NEXT_ETHERNET_INPUT);
363
364   {
365     vnet_sw_interface_t * si = vnet_get_hw_sw_interface (vnm, hw_if_index);
366     *sw_if_indexp = si->sw_if_index;
367   }
368
369   return 0;
370 }
371
372 static clib_error_t *
373 create_simulated_ethernet_interfaces (vlib_main_t * vm,
374                                       unformat_input_t * input,
375                                       vlib_cli_command_t * cmd)
376 {
377   int rv;
378   u32 sw_if_index;
379   u8 mac_address[6];
380
381   memset (mac_address, 0, sizeof (mac_address));
382
383   while (unformat_check_input (input) != UNFORMAT_END_OF_INPUT)
384     {
385       if (unformat (input, "mac %U", unformat_ethernet_address, mac_address))
386         ;
387       else
388         break;
389     }
390
391   rv = vnet_create_loopback_interface (&sw_if_index, mac_address);
392
393   if (rv)
394     return clib_error_return (0, "vnet_create_loopback_interface failed");
395
396   return 0;
397 }
398
399 VLIB_CLI_COMMAND (create_simulated_ethernet_interface_command, static) = {
400   .path = "loopback create-interface",
401   .short_help = "Create Loopback ethernet interface [mac <mac-addr>]",
402   .function = create_simulated_ethernet_interfaces,
403 };
404
405 ethernet_interface_t *
406 ethernet_get_interface (ethernet_main_t * em, u32 hw_if_index)
407 {
408   vnet_hw_interface_t * i = vnet_get_hw_interface (vnet_get_main(), hw_if_index);
409   return (i->hw_class_index == ethernet_hw_interface_class.index
410           ? pool_elt_at_index (em->interfaces, i->hw_instance)
411           : 0);
412 }
413
414 int vnet_delete_loopback_interface (u32 sw_if_index)
415 {
416   vnet_main_t * vnm = vnet_get_main();
417   vnet_sw_interface_t * si;
418
419   if (pool_is_free_index (vnm->interface_main.sw_interfaces,
420                           sw_if_index))
421     return VNET_API_ERROR_INVALID_SW_IF_INDEX;
422
423   si = vnet_get_sw_interface (vnm, sw_if_index);
424   ethernet_delete_interface (vnm, si->hw_if_index);
425
426   return 0;
427 }
428
429 static clib_error_t *
430 delete_simulated_ethernet_interfaces (vlib_main_t * vm,
431                                       unformat_input_t * input,
432                                       vlib_cli_command_t * cmd)
433 {
434   int rv;
435   u32 sw_if_index = ~0;
436   vnet_main_t * vnm = vnet_get_main();
437
438   while (unformat_check_input (input) != UNFORMAT_END_OF_INPUT)
439     {
440       if (unformat (input, "intfc %U",
441                     unformat_vnet_sw_interface, vnm, &sw_if_index))
442         ;
443       else
444         break;
445     }
446
447   if (sw_if_index == ~0)
448     return clib_error_return (0, "interface not specified");
449
450   rv = vnet_delete_loopback_interface (sw_if_index);
451
452   if (rv)
453     return clib_error_return (0, "vnet_delete_loopback_interface failed");
454
455   return 0;
456 }
457
458 VLIB_CLI_COMMAND (delete_simulated_ethernet_interface_command, static) = {
459   .path = "loopback delete-interface",
460   .short_help = "Delete Loopback ethernet interface intfc <interface>",
461   .function = delete_simulated_ethernet_interfaces,
462 };