Loopback tx: support multiple tx intfcs per frame
[vpp.git] / src / 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 #include <vnet/l2/l2_input.h>
45 #include <vnet/adj/adj.h>
46
47 /**
48  * @file
49  * @brief Loopback Interfaces.
50  *
51  * This file contains code to manage loopback interfaces.
52  */
53
54 const u8 *
55 ethernet_ip4_mcast_dst_addr (void)
56 {
57   const static u8 ethernet_mcast_dst_mac[] = {
58     0x1, 0x0, 0x5e, 0x0, 0x0, 0x0,
59   };
60
61   return (ethernet_mcast_dst_mac);
62 }
63
64 const u8 *
65 ethernet_ip6_mcast_dst_addr (void)
66 {
67   const static u8 ethernet_mcast_dst_mac[] = {
68     0x33, 0x33, 0x00, 0x0, 0x0, 0x0,
69   };
70
71   return (ethernet_mcast_dst_mac);
72 }
73
74 /**
75  * @brief build a rewrite string to use for sending packets of type 'link_type'
76  * to 'dst_address'
77  */
78 u8 *
79 ethernet_build_rewrite (vnet_main_t * vnm,
80                         u32 sw_if_index,
81                         vnet_link_t link_type, const void *dst_address)
82 {
83   vnet_sw_interface_t *sub_sw = vnet_get_sw_interface (vnm, sw_if_index);
84   vnet_sw_interface_t *sup_sw = vnet_get_sup_sw_interface (vnm, sw_if_index);
85   vnet_hw_interface_t *hw = vnet_get_sup_hw_interface (vnm, sw_if_index);
86   ethernet_main_t *em = &ethernet_main;
87   ethernet_interface_t *ei;
88   ethernet_header_t *h;
89   ethernet_type_t type;
90   uword n_bytes = sizeof (h[0]);
91   u8 *rewrite = NULL;
92   u8 is_p2p = 0;
93
94   if ((sub_sw->type == VNET_SW_INTERFACE_TYPE_P2P) ||
95       (sub_sw->type == VNET_SW_INTERFACE_TYPE_PIPE))
96     is_p2p = 1;
97   if (sub_sw != sup_sw)
98     {
99       if (sub_sw->sub.eth.flags.one_tag)
100         {
101           n_bytes += sizeof (ethernet_vlan_header_t);
102         }
103       else if (sub_sw->sub.eth.flags.two_tags)
104         {
105           n_bytes += 2 * (sizeof (ethernet_vlan_header_t));
106         }
107       else if (PREDICT_FALSE (is_p2p))
108         {
109           n_bytes = sizeof (ethernet_header_t);
110         }
111       if (PREDICT_FALSE (!is_p2p))
112         {
113           // Check for encaps that are not supported for L3 interfaces
114           if (!(sub_sw->sub.eth.flags.exact_match) ||
115               (sub_sw->sub.eth.flags.default_sub) ||
116               (sub_sw->sub.eth.flags.outer_vlan_id_any) ||
117               (sub_sw->sub.eth.flags.inner_vlan_id_any))
118             {
119               return 0;
120             }
121         }
122       else
123         {
124           n_bytes = sizeof (ethernet_header_t);
125         }
126     }
127
128   switch (link_type)
129     {
130 #define _(a,b) case VNET_LINK_##a: type = ETHERNET_TYPE_##b; break
131       _(IP4, IP4);
132       _(IP6, IP6);
133       _(MPLS, MPLS);
134       _(ARP, ARP);
135 #undef _
136     default:
137       return NULL;
138     }
139
140   vec_validate (rewrite, n_bytes - 1);
141   h = (ethernet_header_t *) rewrite;
142   ei = pool_elt_at_index (em->interfaces, hw->hw_instance);
143   clib_memcpy (h->src_address, ei->address, sizeof (h->src_address));
144   if (is_p2p)
145     {
146       clib_memcpy (h->dst_address, sub_sw->p2p.client_mac,
147                    sizeof (h->dst_address));
148     }
149   else
150     {
151       if (dst_address)
152         clib_memcpy (h->dst_address, dst_address, sizeof (h->dst_address));
153       else
154         memset (h->dst_address, ~0, sizeof (h->dst_address));   /* broadcast */
155     }
156
157   if (PREDICT_FALSE (!is_p2p) && sub_sw->sub.eth.flags.one_tag)
158     {
159       ethernet_vlan_header_t *outer = (void *) (h + 1);
160
161       h->type = sub_sw->sub.eth.flags.dot1ad ?
162         clib_host_to_net_u16 (ETHERNET_TYPE_DOT1AD) :
163         clib_host_to_net_u16 (ETHERNET_TYPE_VLAN);
164       outer->priority_cfi_and_id =
165         clib_host_to_net_u16 (sub_sw->sub.eth.outer_vlan_id);
166       outer->type = clib_host_to_net_u16 (type);
167
168     }
169   else if (PREDICT_FALSE (!is_p2p) && sub_sw->sub.eth.flags.two_tags)
170     {
171       ethernet_vlan_header_t *outer = (void *) (h + 1);
172       ethernet_vlan_header_t *inner = (void *) (outer + 1);
173
174       h->type = sub_sw->sub.eth.flags.dot1ad ?
175         clib_host_to_net_u16 (ETHERNET_TYPE_DOT1AD) :
176         clib_host_to_net_u16 (ETHERNET_TYPE_VLAN);
177       outer->priority_cfi_and_id =
178         clib_host_to_net_u16 (sub_sw->sub.eth.outer_vlan_id);
179       outer->type = clib_host_to_net_u16 (ETHERNET_TYPE_VLAN);
180       inner->priority_cfi_and_id =
181         clib_host_to_net_u16 (sub_sw->sub.eth.inner_vlan_id);
182       inner->type = clib_host_to_net_u16 (type);
183
184     }
185   else
186     {
187       h->type = clib_host_to_net_u16 (type);
188     }
189
190   return (rewrite);
191 }
192
193 void
194 ethernet_update_adjacency (vnet_main_t * vnm, u32 sw_if_index, u32 ai)
195 {
196   ip_adjacency_t *adj;
197
198   adj = adj_get (ai);
199
200   vnet_sw_interface_t *si = vnet_get_sw_interface (vnm, sw_if_index);
201   if ((si->type == VNET_SW_INTERFACE_TYPE_P2P) ||
202       (si->type == VNET_SW_INTERFACE_TYPE_PIPE))
203     {
204       default_update_adjacency (vnm, sw_if_index, ai);
205     }
206   else if (FIB_PROTOCOL_IP4 == adj->ia_nh_proto)
207     {
208       arp_update_adjacency (vnm, sw_if_index, ai);
209     }
210   else if (FIB_PROTOCOL_IP6 == adj->ia_nh_proto)
211     {
212       ip6_ethernet_update_adjacency (vnm, sw_if_index, ai);
213     }
214   else
215     {
216       ASSERT (0);
217     }
218 }
219
220 static clib_error_t *
221 ethernet_mac_change (vnet_hw_interface_t * hi, char *mac_address)
222 {
223   ethernet_interface_t *ei;
224   ethernet_main_t *em;
225
226   em = &ethernet_main;
227   ei = pool_elt_at_index (em->interfaces, hi->hw_instance);
228
229   vec_validate (hi->hw_address,
230                 STRUCT_SIZE_OF (ethernet_header_t, src_address) - 1);
231   clib_memcpy (hi->hw_address, mac_address, vec_len (hi->hw_address));
232
233   clib_memcpy (ei->address, (u8 *) mac_address, sizeof (ei->address));
234   ethernet_arp_change_mac (hi->sw_if_index);
235   ethernet_ndp_change_mac (hi->sw_if_index);
236
237   return (NULL);
238 }
239
240 /* *INDENT-OFF* */
241 VNET_HW_INTERFACE_CLASS (ethernet_hw_interface_class) = {
242   .name = "Ethernet",
243   .format_address = format_ethernet_address,
244   .format_header = format_ethernet_header_with_length,
245   .unformat_hw_address = unformat_ethernet_address,
246   .unformat_header = unformat_ethernet_header,
247   .build_rewrite = ethernet_build_rewrite,
248   .update_adjacency = ethernet_update_adjacency,
249   .mac_addr_change_function = ethernet_mac_change,
250 };
251 /* *INDENT-ON* */
252
253 uword
254 unformat_ethernet_interface (unformat_input_t * input, va_list * args)
255 {
256   vnet_main_t *vnm = va_arg (*args, vnet_main_t *);
257   u32 *result = va_arg (*args, u32 *);
258   u32 hw_if_index;
259   ethernet_main_t *em = &ethernet_main;
260   ethernet_interface_t *eif;
261
262   if (!unformat_user (input, unformat_vnet_hw_interface, vnm, &hw_if_index))
263     return 0;
264
265   eif = ethernet_get_interface (em, hw_if_index);
266   if (eif)
267     {
268       *result = hw_if_index;
269       return 1;
270     }
271   return 0;
272 }
273
274 clib_error_t *
275 ethernet_register_interface (vnet_main_t * vnm,
276                              u32 dev_class_index,
277                              u32 dev_instance,
278                              u8 * address,
279                              u32 * hw_if_index_return,
280                              ethernet_flag_change_function_t flag_change)
281 {
282   ethernet_main_t *em = &ethernet_main;
283   ethernet_interface_t *ei;
284   vnet_hw_interface_t *hi;
285   clib_error_t *error = 0;
286   u32 hw_if_index;
287
288   pool_get (em->interfaces, ei);
289   ei->flag_change = flag_change;
290
291   hw_if_index = vnet_register_interface
292     (vnm,
293      dev_class_index, dev_instance,
294      ethernet_hw_interface_class.index, ei - em->interfaces);
295   *hw_if_index_return = hw_if_index;
296
297   hi = vnet_get_hw_interface (vnm, hw_if_index);
298
299   ethernet_setup_node (vnm->vlib_main, hi->output_node_index);
300
301   hi->min_packet_bytes = hi->min_supported_packet_bytes =
302     ETHERNET_MIN_PACKET_BYTES;
303   hi->max_packet_bytes = hi->max_supported_packet_bytes =
304     ETHERNET_MAX_PACKET_BYTES;
305
306   /* Standard default ethernet MTU. */
307   vnet_sw_interface_set_mtu (vnm, hi->sw_if_index, 9000);
308
309   clib_memcpy (ei->address, address, sizeof (ei->address));
310   vec_add (hi->hw_address, address, sizeof (ei->address));
311
312   if (error)
313     {
314       pool_put (em->interfaces, ei);
315       return error;
316     }
317   return error;
318 }
319
320 void
321 ethernet_delete_interface (vnet_main_t * vnm, u32 hw_if_index)
322 {
323   ethernet_main_t *em = &ethernet_main;
324   ethernet_interface_t *ei;
325   vnet_hw_interface_t *hi;
326   main_intf_t *main_intf;
327   vlan_table_t *vlan_table;
328   u32 idx;
329
330   hi = vnet_get_hw_interface (vnm, hw_if_index);
331   ei = pool_elt_at_index (em->interfaces, hi->hw_instance);
332
333   /* Delete vlan mapping table for dot1q and dot1ad. */
334   main_intf = vec_elt_at_index (em->main_intfs, hi->hw_if_index);
335   if (main_intf->dot1q_vlans)
336     {
337       vlan_table = vec_elt_at_index (em->vlan_pool, main_intf->dot1q_vlans);
338       for (idx = 0; idx < ETHERNET_N_VLAN; idx++)
339         {
340           if (vlan_table->vlans[idx].qinqs)
341             {
342               pool_put_index (em->qinq_pool, vlan_table->vlans[idx].qinqs);
343             }
344         }
345       pool_put_index (em->vlan_pool, main_intf->dot1q_vlans);
346       main_intf->dot1q_vlans = 0;
347     }
348   if (main_intf->dot1ad_vlans)
349     {
350       vlan_table = vec_elt_at_index (em->vlan_pool, main_intf->dot1ad_vlans);
351       for (idx = 0; idx < ETHERNET_N_VLAN; idx++)
352         {
353           if (vlan_table->vlans[idx].qinqs)
354             {
355               pool_put_index (em->qinq_pool, vlan_table->vlans[idx].qinqs);
356             }
357         }
358       pool_put_index (em->vlan_pool, main_intf->dot1ad_vlans);
359       main_intf->dot1ad_vlans = 0;
360     }
361
362   vnet_delete_hw_interface (vnm, hw_if_index);
363   pool_put (em->interfaces, ei);
364 }
365
366 u32
367 ethernet_set_flags (vnet_main_t * vnm, u32 hw_if_index, u32 flags)
368 {
369   ethernet_main_t *em = &ethernet_main;
370   vnet_hw_interface_t *hi;
371   ethernet_interface_t *ei;
372
373   hi = vnet_get_hw_interface (vnm, hw_if_index);
374
375   ASSERT (hi->hw_class_index == ethernet_hw_interface_class.index);
376
377   ei = pool_elt_at_index (em->interfaces, hi->hw_instance);
378   if (ei->flag_change)
379     return ei->flag_change (vnm, hi, flags);
380   return (u32) ~ 0;
381 }
382
383 /**
384  * Echo packets back to ethernet/l2-input.
385  */
386 static uword
387 simulated_ethernet_interface_tx (vlib_main_t * vm,
388                                  vlib_node_runtime_t *
389                                  node, vlib_frame_t * frame)
390 {
391   u32 n_left_from, *from;
392   u32 next_index = 0;
393   u32 n_bytes;
394   u32 thread_index = vm->thread_index;
395   vnet_main_t *vnm = vnet_get_main ();
396   vnet_interface_main_t *im = &vnm->interface_main;
397   l2_input_config_t *config;
398   vlib_buffer_t *bufs[VLIB_FRAME_SIZE], **b;
399   u16 nexts[VLIB_FRAME_SIZE], *next;
400   u32 new_rx_sw_if_index = ~0;
401   u32 new_tx_sw_if_index = ~0;
402
403   n_left_from = frame->n_vectors;
404   from = vlib_frame_args (frame);
405
406   vlib_get_buffers (vm, from, bufs, n_left_from);
407   b = bufs;
408   next = nexts;
409
410   /* Ordinarily, this is the only config lookup. */
411   config = l2input_intf_config (vnet_buffer (b[0])->sw_if_index[VLIB_TX]);
412   next_index =
413     config->bridge ? VNET_SIMULATED_ETHERNET_TX_NEXT_L2_INPUT :
414     VNET_SIMULATED_ETHERNET_TX_NEXT_ETHERNET_INPUT;
415   new_tx_sw_if_index = config->bvi ? L2INPUT_BVI : ~0;
416   new_rx_sw_if_index = vnet_buffer (b[0])->sw_if_index[VLIB_TX];
417
418   while (n_left_from >= 4)
419     {
420       u32 sw_if_index0, sw_if_index1, sw_if_index2, sw_if_index3;
421       u32 not_all_match_config;
422
423       /* Prefetch next iteration. */
424       if (PREDICT_TRUE (n_left_from >= 8))
425         {
426           vlib_prefetch_buffer_header (b[4], STORE);
427           vlib_prefetch_buffer_header (b[5], STORE);
428           vlib_prefetch_buffer_header (b[6], STORE);
429           vlib_prefetch_buffer_header (b[7], STORE);
430         }
431
432       /* Make sure all pkts were transmitted on the same (loop) intfc */
433       sw_if_index0 = vnet_buffer (b[0])->sw_if_index[VLIB_TX];
434       sw_if_index1 = vnet_buffer (b[1])->sw_if_index[VLIB_TX];
435       sw_if_index2 = vnet_buffer (b[2])->sw_if_index[VLIB_TX];
436       sw_if_index3 = vnet_buffer (b[3])->sw_if_index[VLIB_TX];
437
438       not_all_match_config = (sw_if_index0 ^ sw_if_index1)
439         ^ (sw_if_index2 ^ sw_if_index3);
440       not_all_match_config += sw_if_index0 ^ new_rx_sw_if_index;
441
442       /* Speed path / expected case: all pkts on the same intfc */
443       if (PREDICT_TRUE (not_all_match_config == 0))
444         {
445           next[0] = next_index;
446           next[1] = next_index;
447           next[2] = next_index;
448           next[3] = next_index;
449           vnet_buffer (b[0])->sw_if_index[VLIB_RX] = new_rx_sw_if_index;
450           vnet_buffer (b[1])->sw_if_index[VLIB_RX] = new_rx_sw_if_index;
451           vnet_buffer (b[2])->sw_if_index[VLIB_RX] = new_rx_sw_if_index;
452           vnet_buffer (b[3])->sw_if_index[VLIB_RX] = new_rx_sw_if_index;
453           vnet_buffer (b[0])->sw_if_index[VLIB_TX] = new_tx_sw_if_index;
454           vnet_buffer (b[1])->sw_if_index[VLIB_TX] = new_tx_sw_if_index;
455           vnet_buffer (b[2])->sw_if_index[VLIB_TX] = new_tx_sw_if_index;
456           vnet_buffer (b[3])->sw_if_index[VLIB_TX] = new_tx_sw_if_index;
457           n_bytes = vlib_buffer_length_in_chain (vm, b[0]);
458           n_bytes += vlib_buffer_length_in_chain (vm, b[1]);
459           n_bytes += vlib_buffer_length_in_chain (vm, b[2]);
460           n_bytes += vlib_buffer_length_in_chain (vm, b[3]);
461
462           if (next_index == VNET_SIMULATED_ETHERNET_TX_NEXT_L2_INPUT)
463             {
464               vnet_update_l2_len (b[0]);
465               vnet_update_l2_len (b[1]);
466               vnet_update_l2_len (b[2]);
467               vnet_update_l2_len (b[3]);
468             }
469
470           /* increment TX interface stat */
471           vlib_increment_combined_counter (im->combined_sw_if_counters +
472                                            VNET_INTERFACE_COUNTER_TX,
473                                            thread_index, new_rx_sw_if_index,
474                                            4 /* pkts */ , n_bytes);
475           b += 4;
476           next += 4;
477           n_left_from -= 4;
478           continue;
479         }
480
481       /*
482        * Slow path: we know that at least one of the pkts
483        * was transmitted on a different sw_if_index, so
484        * check each sw_if_index against the cached data and proceed
485        * accordingly.
486        *
487        * This shouldn't happen, but code can (and does) bypass the
488        * per-interface output node, so deal with it.
489        */
490       if (PREDICT_FALSE (vnet_buffer (b[0])->sw_if_index[VLIB_TX]
491                          != new_rx_sw_if_index))
492         {
493           config = l2input_intf_config
494             (vnet_buffer (b[0])->sw_if_index[VLIB_TX]);
495           next_index =
496             config->bridge ? VNET_SIMULATED_ETHERNET_TX_NEXT_L2_INPUT :
497             VNET_SIMULATED_ETHERNET_TX_NEXT_ETHERNET_INPUT;
498           new_tx_sw_if_index = config->bvi ? L2INPUT_BVI : ~0;
499           new_rx_sw_if_index = vnet_buffer (b[0])->sw_if_index[VLIB_TX];
500         }
501       next[0] = next_index;
502       vnet_buffer (b[0])->sw_if_index[VLIB_RX] = new_rx_sw_if_index;
503       vnet_buffer (b[0])->sw_if_index[VLIB_TX] = new_tx_sw_if_index;
504       n_bytes = vlib_buffer_length_in_chain (vm, b[0]);
505       if (next_index == VNET_SIMULATED_ETHERNET_TX_NEXT_L2_INPUT)
506         vnet_update_l2_len (b[0]);
507
508       vlib_increment_combined_counter (im->combined_sw_if_counters +
509                                        VNET_INTERFACE_COUNTER_TX,
510                                        thread_index, new_rx_sw_if_index,
511                                        1 /* pkts */ , n_bytes);
512
513       if (PREDICT_FALSE (vnet_buffer (b[1])->sw_if_index[VLIB_TX]
514                          != new_rx_sw_if_index))
515         {
516           config = l2input_intf_config
517             (vnet_buffer (b[1])->sw_if_index[VLIB_TX]);
518           next_index =
519             config->bridge ? VNET_SIMULATED_ETHERNET_TX_NEXT_L2_INPUT :
520             VNET_SIMULATED_ETHERNET_TX_NEXT_ETHERNET_INPUT;
521           new_rx_sw_if_index = vnet_buffer (b[1])->sw_if_index[VLIB_TX];
522           new_tx_sw_if_index = config->bvi ? L2INPUT_BVI : ~0;
523         }
524       next[1] = next_index;
525       vnet_buffer (b[1])->sw_if_index[VLIB_RX] = new_rx_sw_if_index;
526       vnet_buffer (b[1])->sw_if_index[VLIB_TX] = new_tx_sw_if_index;
527       n_bytes = vlib_buffer_length_in_chain (vm, b[1]);
528       if (next_index == VNET_SIMULATED_ETHERNET_TX_NEXT_L2_INPUT)
529         vnet_update_l2_len (b[1]);
530
531       vlib_increment_combined_counter (im->combined_sw_if_counters +
532                                        VNET_INTERFACE_COUNTER_TX,
533                                        thread_index, new_rx_sw_if_index,
534                                        1 /* pkts */ , n_bytes);
535
536       if (PREDICT_FALSE (vnet_buffer (b[2])->sw_if_index[VLIB_TX]
537                          != new_rx_sw_if_index))
538         {
539           config = l2input_intf_config
540             (vnet_buffer (b[2])->sw_if_index[VLIB_TX]);
541           next_index =
542             config->bridge ? VNET_SIMULATED_ETHERNET_TX_NEXT_L2_INPUT :
543             VNET_SIMULATED_ETHERNET_TX_NEXT_ETHERNET_INPUT;
544           new_rx_sw_if_index = vnet_buffer (b[2])->sw_if_index[VLIB_TX];
545           new_tx_sw_if_index = config->bvi ? L2INPUT_BVI : ~0;
546         }
547       next[2] = next_index;
548       vnet_buffer (b[2])->sw_if_index[VLIB_RX] = new_rx_sw_if_index;
549       vnet_buffer (b[2])->sw_if_index[VLIB_TX] = new_tx_sw_if_index;
550       n_bytes = vlib_buffer_length_in_chain (vm, b[2]);
551       if (next_index == VNET_SIMULATED_ETHERNET_TX_NEXT_L2_INPUT)
552         vnet_update_l2_len (b[2]);
553
554       vlib_increment_combined_counter (im->combined_sw_if_counters +
555                                        VNET_INTERFACE_COUNTER_TX,
556                                        thread_index, new_rx_sw_if_index,
557                                        1 /* pkts */ , n_bytes);
558
559       if (PREDICT_FALSE (vnet_buffer (b[3])->sw_if_index[VLIB_TX]
560                          != new_rx_sw_if_index))
561         {
562           config = l2input_intf_config
563             (vnet_buffer (b[3])->sw_if_index[VLIB_TX]);
564           next_index =
565             config->bridge ? VNET_SIMULATED_ETHERNET_TX_NEXT_L2_INPUT :
566             VNET_SIMULATED_ETHERNET_TX_NEXT_ETHERNET_INPUT;
567           new_rx_sw_if_index = vnet_buffer (b[3])->sw_if_index[VLIB_TX];
568           new_tx_sw_if_index = config->bvi ? L2INPUT_BVI : ~0;
569         }
570       next[3] = next_index;
571       vnet_buffer (b[3])->sw_if_index[VLIB_RX] = new_rx_sw_if_index;
572       vnet_buffer (b[3])->sw_if_index[VLIB_TX] = new_tx_sw_if_index;
573       n_bytes = vlib_buffer_length_in_chain (vm, b[3]);
574       if (next_index == VNET_SIMULATED_ETHERNET_TX_NEXT_L2_INPUT)
575         vnet_update_l2_len (b[3]);
576
577       vlib_increment_combined_counter (im->combined_sw_if_counters +
578                                        VNET_INTERFACE_COUNTER_TX,
579                                        thread_index, new_rx_sw_if_index,
580                                        1 /* pkts */ , n_bytes);
581       b += 4;
582       next += 4;
583       n_left_from -= 4;
584     }
585   while (n_left_from > 0)
586     {
587       if (PREDICT_FALSE (vnet_buffer (b[0])->sw_if_index[VLIB_TX]
588                          != new_rx_sw_if_index))
589         {
590           config = l2input_intf_config
591             (vnet_buffer (b[0])->sw_if_index[VLIB_TX]);
592           next_index =
593             config->bridge ? VNET_SIMULATED_ETHERNET_TX_NEXT_L2_INPUT :
594             VNET_SIMULATED_ETHERNET_TX_NEXT_ETHERNET_INPUT;
595           new_tx_sw_if_index = config->bvi ? L2INPUT_BVI : ~0;
596           new_rx_sw_if_index = vnet_buffer (b[0])->sw_if_index[VLIB_TX];
597         }
598       next[0] = next_index;
599       vnet_buffer (b[0])->sw_if_index[VLIB_RX] = new_rx_sw_if_index;
600       vnet_buffer (b[0])->sw_if_index[VLIB_TX] = new_tx_sw_if_index;
601       n_bytes = vlib_buffer_length_in_chain (vm, b[0]);
602       if (next_index == VNET_SIMULATED_ETHERNET_TX_NEXT_L2_INPUT)
603         vnet_update_l2_len (b[0]);
604
605       vlib_increment_combined_counter (im->combined_sw_if_counters +
606                                        VNET_INTERFACE_COUNTER_TX,
607                                        thread_index, new_rx_sw_if_index,
608                                        1 /* pkts */ , n_bytes);
609       b += 1;
610       next += 1;
611       n_left_from -= 1;
612     }
613
614   vlib_buffer_enqueue_to_next (vm, node, from, nexts, frame->n_vectors);
615
616   return frame->n_vectors;
617 }
618
619 static u8 *
620 format_simulated_ethernet_name (u8 * s, va_list * args)
621 {
622   u32 dev_instance = va_arg (*args, u32);
623   return format (s, "loop%d", dev_instance);
624 }
625
626 static clib_error_t *
627 simulated_ethernet_admin_up_down (vnet_main_t * vnm, u32 hw_if_index,
628                                   u32 flags)
629 {
630   u32 hw_flags = (flags & VNET_SW_INTERFACE_FLAG_ADMIN_UP) ?
631     VNET_HW_INTERFACE_FLAG_LINK_UP : 0;
632   vnet_hw_interface_set_flags (vnm, hw_if_index, hw_flags);
633   return 0;
634 }
635
636 /* *INDENT-OFF* */
637 VNET_DEVICE_CLASS (ethernet_simulated_device_class) = {
638   .name = "Loopback",
639   .format_device_name = format_simulated_ethernet_name,
640   .tx_function = simulated_ethernet_interface_tx,
641   .admin_up_down_function = simulated_ethernet_admin_up_down,
642 };
643 /* *INDENT-ON* */
644
645 VLIB_DEVICE_TX_FUNCTION_MULTIARCH (ethernet_simulated_device_class,
646                                    simulated_ethernet_interface_tx);
647
648 /*
649  * Maintain a bitmap of allocated loopback instance numbers.
650  */
651 #define LOOPBACK_MAX_INSTANCE           (16 * 1024)
652
653 static u32
654 loopback_instance_alloc (u8 is_specified, u32 want)
655 {
656   ethernet_main_t *em = &ethernet_main;
657
658   /*
659    * Check for dynamically allocaetd instance number.
660    */
661   if (!is_specified)
662     {
663       u32 bit;
664
665       bit = clib_bitmap_first_clear (em->bm_loopback_instances);
666       if (bit >= LOOPBACK_MAX_INSTANCE)
667         {
668           return ~0;
669         }
670       em->bm_loopback_instances = clib_bitmap_set (em->bm_loopback_instances,
671                                                    bit, 1);
672       return bit;
673     }
674
675   /*
676    * In range?
677    */
678   if (want >= LOOPBACK_MAX_INSTANCE)
679     {
680       return ~0;
681     }
682
683   /*
684    * Already in use?
685    */
686   if (clib_bitmap_get (em->bm_loopback_instances, want))
687     {
688       return ~0;
689     }
690
691   /*
692    * Grant allocation request.
693    */
694   em->bm_loopback_instances = clib_bitmap_set (em->bm_loopback_instances,
695                                                want, 1);
696
697   return want;
698 }
699
700 static int
701 loopback_instance_free (u32 instance)
702 {
703   ethernet_main_t *em = &ethernet_main;
704
705   if (instance >= LOOPBACK_MAX_INSTANCE)
706     {
707       return -1;
708     }
709
710   if (clib_bitmap_get (em->bm_loopback_instances, instance) == 0)
711     {
712       return -1;
713     }
714
715   em->bm_loopback_instances = clib_bitmap_set (em->bm_loopback_instances,
716                                                instance, 0);
717   return 0;
718 }
719
720 int
721 vnet_create_loopback_interface (u32 * sw_if_indexp, u8 * mac_address,
722                                 u8 is_specified, u32 user_instance)
723 {
724   vnet_main_t *vnm = vnet_get_main ();
725   vlib_main_t *vm = vlib_get_main ();
726   clib_error_t *error;
727   u32 instance;
728   u8 address[6];
729   u32 hw_if_index;
730   vnet_hw_interface_t *hw_if;
731   u32 slot;
732   int rv = 0;
733
734   ASSERT (sw_if_indexp);
735
736   *sw_if_indexp = (u32) ~ 0;
737
738   memset (address, 0, sizeof (address));
739
740   /*
741    * Allocate a loopback instance.  Either select on dynamically
742    * or try to use the desired user_instance number.
743    */
744   instance = loopback_instance_alloc (is_specified, user_instance);
745   if (instance == ~0)
746     {
747       return VNET_API_ERROR_INVALID_REGISTRATION;
748     }
749
750   /*
751    * Default MAC address (dead:0000:0000 + instance) is allocated
752    * if zero mac_address is configured. Otherwise, user-configurable MAC
753    * address is programmed on the loopback interface.
754    */
755   if (memcmp (address, mac_address, sizeof (address)))
756     clib_memcpy (address, mac_address, sizeof (address));
757   else
758     {
759       address[0] = 0xde;
760       address[1] = 0xad;
761       address[5] = instance;
762     }
763
764   error = ethernet_register_interface
765     (vnm,
766      ethernet_simulated_device_class.index, instance, address, &hw_if_index,
767      /* flag change */ 0);
768
769   if (error)
770     {
771       rv = VNET_API_ERROR_INVALID_REGISTRATION;
772       clib_error_report (error);
773       return rv;
774     }
775
776   hw_if = vnet_get_hw_interface (vnm, hw_if_index);
777   slot = vlib_node_add_named_next_with_slot
778     (vm, hw_if->tx_node_index,
779      "ethernet-input", VNET_SIMULATED_ETHERNET_TX_NEXT_ETHERNET_INPUT);
780   ASSERT (slot == VNET_SIMULATED_ETHERNET_TX_NEXT_ETHERNET_INPUT);
781
782   slot = vlib_node_add_named_next_with_slot
783     (vm, hw_if->tx_node_index,
784      "l2-input", VNET_SIMULATED_ETHERNET_TX_NEXT_L2_INPUT);
785   ASSERT (slot == VNET_SIMULATED_ETHERNET_TX_NEXT_L2_INPUT);
786
787   {
788     vnet_sw_interface_t *si = vnet_get_hw_sw_interface (vnm, hw_if_index);
789     *sw_if_indexp = si->sw_if_index;
790
791     /* By default don't flood to loopbacks, as packets just keep
792      * coming back ... If this loopback becomes a BVI, we'll change it */
793     si->flood_class = VNET_FLOOD_CLASS_NO_FLOOD;
794   }
795
796   return 0;
797 }
798
799 static clib_error_t *
800 create_simulated_ethernet_interfaces (vlib_main_t * vm,
801                                       unformat_input_t * input,
802                                       vlib_cli_command_t * cmd)
803 {
804   int rv;
805   u32 sw_if_index;
806   u8 mac_address[6];
807   u8 is_specified = 0;
808   u32 user_instance = 0;
809
810   memset (mac_address, 0, sizeof (mac_address));
811
812   while (unformat_check_input (input) != UNFORMAT_END_OF_INPUT)
813     {
814       if (unformat (input, "mac %U", unformat_ethernet_address, mac_address))
815         ;
816       if (unformat (input, "instance %d", &user_instance))
817         is_specified = 1;
818       else
819         break;
820     }
821
822   rv = vnet_create_loopback_interface (&sw_if_index, mac_address,
823                                        is_specified, user_instance);
824
825   if (rv)
826     return clib_error_return (0, "vnet_create_loopback_interface failed");
827
828   vlib_cli_output (vm, "%U\n", format_vnet_sw_if_index_name, vnet_get_main (),
829                    sw_if_index);
830   return 0;
831 }
832
833 /*?
834  * Create a loopback interface. Optionally, a MAC Address can be
835  * provided. If not provided, de:ad:00:00:00:<loopId> will be used.
836  *
837  * @cliexpar
838  * The following two command syntaxes are equivalent:
839  * @cliexcmd{loopback create-interface [mac <mac-addr>] [instance <instance>]}
840  * @cliexcmd{create loopback interface [mac <mac-addr>] [instance <instance>]}
841  * Example of how to create a loopback interface:
842  * @cliexcmd{loopback create-interface}
843 ?*/
844 /* *INDENT-OFF* */
845 VLIB_CLI_COMMAND (create_simulated_ethernet_interface_command, static) = {
846   .path = "loopback create-interface",
847   .short_help = "loopback create-interface [mac <mac-addr>] [instance <instance>]",
848   .function = create_simulated_ethernet_interfaces,
849 };
850 /* *INDENT-ON* */
851
852 /*?
853  * Create a loopback interface. Optionally, a MAC Address can be
854  * provided. If not provided, de:ad:00:00:00:<loopId> will be used.
855  *
856  * @cliexpar
857  * The following two command syntaxes are equivalent:
858  * @cliexcmd{loopback create-interface [mac <mac-addr>] [instance <instance>]}
859  * @cliexcmd{create loopback interface [mac <mac-addr>] [instance <instance>]}
860  * Example of how to create a loopback interface:
861  * @cliexcmd{create loopback interface}
862 ?*/
863 /* *INDENT-OFF* */
864 VLIB_CLI_COMMAND (create_loopback_interface_command, static) = {
865   .path = "create loopback interface",
866   .short_help = "create loopback interface [mac <mac-addr>] [instance <instance>]",
867   .function = create_simulated_ethernet_interfaces,
868 };
869 /* *INDENT-ON* */
870
871 ethernet_interface_t *
872 ethernet_get_interface (ethernet_main_t * em, u32 hw_if_index)
873 {
874   vnet_hw_interface_t *i =
875     vnet_get_hw_interface (vnet_get_main (), hw_if_index);
876   return (i->hw_class_index ==
877           ethernet_hw_interface_class.
878           index ? pool_elt_at_index (em->interfaces, i->hw_instance) : 0);
879 }
880
881 int
882 vnet_delete_loopback_interface (u32 sw_if_index)
883 {
884   vnet_main_t *vnm = vnet_get_main ();
885
886   if (pool_is_free_index (vnm->interface_main.sw_interfaces, sw_if_index))
887     return VNET_API_ERROR_INVALID_SW_IF_INDEX;
888
889   vnet_hw_interface_t *hw = vnet_get_sup_hw_interface (vnm, sw_if_index);
890   if (hw == 0 || hw->dev_class_index != ethernet_simulated_device_class.index)
891     return VNET_API_ERROR_INVALID_SW_IF_INDEX;
892
893   if (loopback_instance_free (hw->dev_instance) < 0)
894     return VNET_API_ERROR_INVALID_SW_IF_INDEX;
895
896   ethernet_delete_interface (vnm, hw->hw_if_index);
897
898   return 0;
899 }
900
901 int
902 vnet_delete_sub_interface (u32 sw_if_index)
903 {
904   vnet_main_t *vnm = vnet_get_main ();
905   vnet_sw_interface_t *si;
906   int rv = 0;
907
908   if (pool_is_free_index (vnm->interface_main.sw_interfaces, sw_if_index))
909     return VNET_API_ERROR_INVALID_SW_IF_INDEX;
910
911   si = vnet_get_sw_interface (vnm, sw_if_index);
912   if (si->type == VNET_SW_INTERFACE_TYPE_SUB ||
913       si->type == VNET_SW_INTERFACE_TYPE_PIPE ||
914       si->type == VNET_SW_INTERFACE_TYPE_P2P)
915     {
916       vnet_interface_main_t *im = &vnm->interface_main;
917       vnet_hw_interface_t *hi = vnet_get_sup_hw_interface (vnm, sw_if_index);
918       u64 sup_and_sub_key =
919         ((u64) (si->sup_sw_if_index) << 32) | (u64) si->sub.id;
920       hash_unset_mem_free (&im->sw_if_index_by_sup_and_sub, &sup_and_sub_key);
921       hash_unset (hi->sub_interface_sw_if_index_by_id, si->sub.id);
922       vnet_delete_sw_interface (vnm, sw_if_index);
923     }
924   else
925     rv = VNET_API_ERROR_INVALID_SUB_SW_IF_INDEX;
926
927   return rv;
928 }
929
930 static clib_error_t *
931 delete_simulated_ethernet_interfaces (vlib_main_t * vm,
932                                       unformat_input_t * input,
933                                       vlib_cli_command_t * cmd)
934 {
935   int rv;
936   u32 sw_if_index = ~0;
937   vnet_main_t *vnm = vnet_get_main ();
938
939   while (unformat_check_input (input) != UNFORMAT_END_OF_INPUT)
940     {
941       if (unformat (input, "intfc %U",
942                     unformat_vnet_sw_interface, vnm, &sw_if_index))
943         ;
944       else
945         break;
946     }
947
948   if (sw_if_index == ~0)
949     return clib_error_return (0, "interface not specified");
950
951   rv = vnet_delete_loopback_interface (sw_if_index);
952
953   if (rv)
954     return clib_error_return (0, "vnet_delete_loopback_interface failed");
955
956   return 0;
957 }
958
959 static clib_error_t *
960 delete_sub_interface (vlib_main_t * vm,
961                       unformat_input_t * input, vlib_cli_command_t * cmd)
962 {
963   int rv = 0;
964   u32 sw_if_index = ~0;
965   vnet_main_t *vnm = vnet_get_main ();
966
967   while (unformat_check_input (input) != UNFORMAT_END_OF_INPUT)
968     {
969       if (unformat
970           (input, "%U", unformat_vnet_sw_interface, vnm, &sw_if_index))
971         ;
972       else
973         break;
974     }
975   if (sw_if_index == ~0)
976     return clib_error_return (0, "interface doesn't exist");
977
978   if (pool_is_free_index (vnm->interface_main.sw_interfaces, sw_if_index))
979     rv = VNET_API_ERROR_INVALID_SW_IF_INDEX;
980   else
981     rv = vnet_delete_sub_interface (sw_if_index);
982   if (rv)
983     return clib_error_return (0, "delete_subinterface_interface failed");
984   return 0;
985 }
986
987 /*?
988  * Delete a loopback interface.
989  *
990  * @cliexpar
991  * The following two command syntaxes are equivalent:
992  * @cliexcmd{loopback delete-interface intfc <interface>}
993  * @cliexcmd{delete loopback interface intfc <interface>}
994  * Example of how to delete a loopback interface:
995  * @cliexcmd{loopback delete-interface intfc loop0}
996 ?*/
997 /* *INDENT-OFF* */
998 VLIB_CLI_COMMAND (delete_simulated_ethernet_interface_command, static) = {
999   .path = "loopback delete-interface",
1000   .short_help = "loopback delete-interface intfc <interface>",
1001   .function = delete_simulated_ethernet_interfaces,
1002 };
1003 /* *INDENT-ON* */
1004
1005 /*?
1006  * Delete a loopback interface.
1007  *
1008  * @cliexpar
1009  * The following two command syntaxes are equivalent:
1010  * @cliexcmd{loopback delete-interface intfc <interface>}
1011  * @cliexcmd{delete loopback interface intfc <interface>}
1012  * Example of how to delete a loopback interface:
1013  * @cliexcmd{delete loopback interface intfc loop0}
1014 ?*/
1015 /* *INDENT-OFF* */
1016 VLIB_CLI_COMMAND (delete_loopback_interface_command, static) = {
1017   .path = "delete loopback interface",
1018   .short_help = "delete loopback interface intfc <interface>",
1019   .function = delete_simulated_ethernet_interfaces,
1020 };
1021 /* *INDENT-ON* */
1022
1023 /*?
1024  * Delete a sub-interface.
1025  *
1026  * @cliexpar
1027  * Example of how to delete a sub-interface:
1028  * @cliexcmd{delete sub-interface GigabitEthernet0/8/0.200}
1029 ?*/
1030 /* *INDENT-OFF* */
1031 VLIB_CLI_COMMAND (delete_sub_interface_command, static) = {
1032   .path = "delete sub-interface",
1033   .short_help = "delete sub-interface <interface>",
1034   .function = delete_sub_interface,
1035 };
1036 /* *INDENT-ON* */
1037
1038 /*
1039  * fd.io coding-style-patch-verification: ON
1040  *
1041  * Local Variables:
1042  * eval: (c-set-style "gnu")
1043  * End:
1044  */