f648440fbf8b1d990dc9291448bf5640e0630291
[vpp.git] / src / vnet / 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  * interface.c: VNET interfaces/sub-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/plugin/plugin.h>
42 #include <vnet/adj/adj.h>
43 #include <vnet/adj/adj_mcast.h>
44 #include <vnet/ip/ip.h>
45 #include <vnet/interface/rx_queue_funcs.h>
46
47 /* *INDENT-OFF* */
48 VLIB_REGISTER_LOG_CLASS (if_default_log, static) = {
49   .class_name = "interface",
50 };
51 /* *INDENT-ON* */
52
53 #define log_debug(fmt,...) vlib_log_debug(if_default_log.class, fmt, __VA_ARGS__)
54 #define log_err(fmt,...) vlib_log_err(if_default_log.class, fmt, __VA_ARGS__)
55
56 typedef enum vnet_interface_helper_flags_t_
57 {
58   VNET_INTERFACE_SET_FLAGS_HELPER_IS_CREATE = (1 << 0),
59   VNET_INTERFACE_SET_FLAGS_HELPER_WANT_REDISTRIBUTE = (1 << 1),
60 } vnet_interface_helper_flags_t;
61
62 static clib_error_t *vnet_hw_interface_set_flags_helper (vnet_main_t * vnm,
63                                                          u32 hw_if_index,
64                                                          vnet_hw_interface_flags_t
65                                                          flags,
66                                                          vnet_interface_helper_flags_t
67                                                          helper_flags);
68
69 static clib_error_t *vnet_sw_interface_set_flags_helper (vnet_main_t * vnm,
70                                                          u32 sw_if_index,
71                                                          vnet_sw_interface_flags_t
72                                                          flags,
73                                                          vnet_interface_helper_flags_t
74                                                          helper_flags);
75
76 static clib_error_t *vnet_hw_interface_set_class_helper (vnet_main_t * vnm,
77                                                          u32 hw_if_index,
78                                                          u32 hw_class_index,
79                                                          u32 redistribute);
80
81 typedef struct
82 {
83   /* Either sw or hw interface index. */
84   u32 sw_hw_if_index;
85
86   /* Flags. */
87   u32 flags;
88 } vnet_sw_hw_interface_state_t;
89
90 static void
91 serialize_vec_vnet_sw_hw_interface_state (serialize_main_t * m, va_list * va)
92 {
93   vnet_sw_hw_interface_state_t *s =
94     va_arg (*va, vnet_sw_hw_interface_state_t *);
95   u32 n = va_arg (*va, u32);
96   u32 i;
97   for (i = 0; i < n; i++)
98     {
99       serialize_integer (m, s[i].sw_hw_if_index,
100                          sizeof (s[i].sw_hw_if_index));
101       serialize_integer (m, s[i].flags, sizeof (s[i].flags));
102     }
103 }
104
105 static void
106 unserialize_vec_vnet_sw_hw_interface_state (serialize_main_t * m,
107                                             va_list * va)
108 {
109   vnet_sw_hw_interface_state_t *s =
110     va_arg (*va, vnet_sw_hw_interface_state_t *);
111   u32 n = va_arg (*va, u32);
112   u32 i;
113   for (i = 0; i < n; i++)
114     {
115       unserialize_integer (m, &s[i].sw_hw_if_index,
116                            sizeof (s[i].sw_hw_if_index));
117       unserialize_integer (m, &s[i].flags, sizeof (s[i].flags));
118     }
119 }
120
121 static vnet_sw_interface_flags_t
122 vnet_hw_interface_flags_to_sw (vnet_hw_interface_flags_t hwf)
123 {
124   vnet_sw_interface_flags_t swf = VNET_SW_INTERFACE_FLAG_NONE;
125
126   if (hwf & VNET_HW_INTERFACE_FLAG_LINK_UP)
127     swf |= VNET_SW_INTERFACE_FLAG_ADMIN_UP;
128
129   return (swf);
130 }
131
132 void
133 serialize_vnet_interface_state (serialize_main_t * m, va_list * va)
134 {
135   vnet_main_t *vnm = va_arg (*va, vnet_main_t *);
136   vnet_sw_hw_interface_state_t *sts = 0, *st;
137   vnet_sw_interface_t *sif;
138   vnet_hw_interface_t *hif;
139   vnet_interface_main_t *im = &vnm->interface_main;
140
141   /* Serialize hardware interface classes since they may have changed.
142      Must do this before sending up/down flags. */
143   /* *INDENT-OFF* */
144   pool_foreach (hif, im->hw_interfaces)  {
145     vnet_hw_interface_class_t * hw_class = vnet_get_hw_interface_class (vnm, hif->hw_class_index);
146     serialize_cstring (m, hw_class->name);
147   }
148   /* *INDENT-ON* */
149
150   /* Send sw/hw interface state when non-zero. */
151   /* *INDENT-OFF* */
152   pool_foreach (sif, im->sw_interfaces)  {
153     if (sif->flags != 0)
154       {
155         vec_add2 (sts, st, 1);
156         st->sw_hw_if_index = sif->sw_if_index;
157         st->flags = sif->flags;
158       }
159   }
160   /* *INDENT-ON* */
161
162   vec_serialize (m, sts, serialize_vec_vnet_sw_hw_interface_state);
163
164   if (sts)
165     _vec_len (sts) = 0;
166
167   /* *INDENT-OFF* */
168   pool_foreach (hif, im->hw_interfaces)  {
169     if (hif->flags != 0)
170       {
171         vec_add2 (sts, st, 1);
172         st->sw_hw_if_index = hif->hw_if_index;
173         st->flags = vnet_hw_interface_flags_to_sw(hif->flags);
174       }
175   }
176   /* *INDENT-ON* */
177
178   vec_serialize (m, sts, serialize_vec_vnet_sw_hw_interface_state);
179
180   vec_free (sts);
181 }
182
183 static vnet_hw_interface_flags_t
184 vnet_sw_interface_flags_to_hw (vnet_sw_interface_flags_t swf)
185 {
186   vnet_hw_interface_flags_t hwf = VNET_HW_INTERFACE_FLAG_NONE;
187
188   if (swf & VNET_SW_INTERFACE_FLAG_ADMIN_UP)
189     hwf |= VNET_HW_INTERFACE_FLAG_LINK_UP;
190
191   return (hwf);
192 }
193
194 void
195 unserialize_vnet_interface_state (serialize_main_t * m, va_list * va)
196 {
197   vnet_main_t *vnm = va_arg (*va, vnet_main_t *);
198   vnet_sw_hw_interface_state_t *sts = 0, *st;
199
200   /* First set interface hardware class. */
201   {
202     vnet_interface_main_t *im = &vnm->interface_main;
203     vnet_hw_interface_t *hif;
204     char *class_name;
205     uword *p;
206     clib_error_t *error;
207
208     /* *INDENT-OFF* */
209     pool_foreach (hif, im->hw_interfaces)  {
210       unserialize_cstring (m, &class_name);
211       p = hash_get_mem (im->hw_interface_class_by_name, class_name);
212       if (p)
213         {
214           error = vnet_hw_interface_set_class_helper
215             (vnm, hif->hw_if_index, p[0], /* redistribute */ 0);
216         }
217       else
218         error = clib_error_return (0, "hw class %s AWOL?", class_name);
219
220       if (error)
221         clib_error_report (error);
222       vec_free (class_name);
223     }
224     /* *INDENT-ON* */
225   }
226
227   vec_unserialize (m, &sts, unserialize_vec_vnet_sw_hw_interface_state);
228   vec_foreach (st, sts)
229     vnet_sw_interface_set_flags_helper (vnm, st->sw_hw_if_index, st->flags,
230                                         /* no distribute */ 0);
231   vec_free (sts);
232
233   vec_unserialize (m, &sts, unserialize_vec_vnet_sw_hw_interface_state);
234   vec_foreach (st, sts)
235   {
236     vnet_hw_interface_set_flags_helper
237       (vnm, st->sw_hw_if_index, vnet_sw_interface_flags_to_hw (st->flags),
238        /* no distribute */ 0);
239   }
240   vec_free (sts);
241 }
242
243 static clib_error_t *
244 call_elf_section_interface_callbacks (vnet_main_t * vnm, u32 if_index,
245                                       u32 flags,
246                                       _vnet_interface_function_list_elt_t **
247                                       elts)
248 {
249   _vnet_interface_function_list_elt_t *elt;
250   vnet_interface_function_priority_t prio;
251   clib_error_t *error = 0;
252
253   for (prio = VNET_ITF_FUNC_PRIORITY_LOW;
254        prio <= VNET_ITF_FUNC_PRIORITY_HIGH; prio++)
255     {
256       elt = elts[prio];
257
258       while (elt)
259         {
260           error = elt->fp (vnm, if_index, flags);
261           if (error)
262             return error;
263           elt = elt->next_interface_function;
264         }
265     }
266   return error;
267 }
268
269 static clib_error_t *
270 call_hw_interface_add_del_callbacks (vnet_main_t * vnm, u32 hw_if_index,
271                                      u32 is_create)
272 {
273   vnet_hw_interface_t *hi = vnet_get_hw_interface (vnm, hw_if_index);
274   vnet_hw_interface_class_t *hw_class =
275     vnet_get_hw_interface_class (vnm, hi->hw_class_index);
276   vnet_device_class_t *dev_class =
277     vnet_get_device_class (vnm, hi->dev_class_index);
278   clib_error_t *error = 0;
279
280   if (hw_class->interface_add_del_function
281       && (error =
282           hw_class->interface_add_del_function (vnm, hw_if_index, is_create)))
283     return error;
284
285   if (dev_class->interface_add_del_function
286       && (error =
287           dev_class->interface_add_del_function (vnm, hw_if_index,
288                                                  is_create)))
289     return error;
290
291   error = call_elf_section_interface_callbacks
292     (vnm, hw_if_index, is_create, vnm->hw_interface_add_del_functions);
293
294   return error;
295 }
296
297 static clib_error_t *
298 call_sw_interface_add_del_callbacks (vnet_main_t * vnm, u32 sw_if_index,
299                                      u32 is_create)
300 {
301   return call_elf_section_interface_callbacks
302     (vnm, sw_if_index, is_create, vnm->sw_interface_add_del_functions);
303 }
304
305 static clib_error_t *
306 vnet_hw_interface_set_flags_helper (vnet_main_t * vnm, u32 hw_if_index,
307                                     vnet_hw_interface_flags_t flags,
308                                     vnet_interface_helper_flags_t
309                                     helper_flags)
310 {
311   vnet_hw_interface_t *hi = vnet_get_hw_interface (vnm, hw_if_index);
312   vnet_hw_interface_class_t *hw_class =
313     vnet_get_hw_interface_class (vnm, hi->hw_class_index);
314   u32 mask;
315   clib_error_t *error = 0;
316   u32 is_create =
317     (helper_flags & VNET_INTERFACE_SET_FLAGS_HELPER_IS_CREATE) != 0;
318
319   mask =
320     (VNET_HW_INTERFACE_FLAG_LINK_UP | VNET_HW_INTERFACE_FLAG_DUPLEX_MASK);
321   flags &= mask;
322
323   /* Call hardware interface add/del callbacks. */
324   if (is_create)
325     call_hw_interface_add_del_callbacks (vnm, hw_if_index, is_create);
326
327   /* Already in the desired state? */
328   if (!is_create && (hi->flags & mask) == flags)
329     goto done;
330
331   if ((hi->flags & VNET_HW_INTERFACE_FLAG_LINK_UP) !=
332       (flags & VNET_HW_INTERFACE_FLAG_LINK_UP))
333     {
334       /* Do hardware class (e.g. ethernet). */
335       if (hw_class->link_up_down_function
336           && (error = hw_class->link_up_down_function (vnm, hw_if_index,
337                                                        flags)))
338         goto done;
339
340       error = call_elf_section_interface_callbacks
341         (vnm, hw_if_index, flags, vnm->hw_interface_link_up_down_functions);
342
343       if (error)
344         goto done;
345     }
346
347   hi->flags &= ~mask;
348   hi->flags |= flags;
349
350 done:
351   if (error)
352     log_err ("hw_set_flags_helper: %U", format_clib_error, error);
353   return error;
354 }
355
356 static clib_error_t *
357 vnet_sw_interface_set_flags_helper (vnet_main_t * vnm, u32 sw_if_index,
358                                     vnet_sw_interface_flags_t flags,
359                                     vnet_interface_helper_flags_t
360                                     helper_flags)
361 {
362   vnet_sw_interface_t *si = vnet_get_sw_interface (vnm, sw_if_index);
363   u32 mask;
364   clib_error_t *error = 0;
365   u32 is_create =
366     (helper_flags & VNET_INTERFACE_SET_FLAGS_HELPER_IS_CREATE) != 0;
367   u32 old_flags;
368
369   mask = VNET_SW_INTERFACE_FLAG_ADMIN_UP | VNET_SW_INTERFACE_FLAG_PUNT;
370   flags &= mask;
371
372   if (is_create)
373     {
374       error =
375         call_sw_interface_add_del_callbacks (vnm, sw_if_index, is_create);
376       if (error)
377         goto done;
378
379       if (flags & VNET_SW_INTERFACE_FLAG_ADMIN_UP)
380         {
381           /* Notify everyone when the interface is created as admin up */
382           error = call_elf_section_interface_callbacks (vnm, sw_if_index,
383                                                         flags,
384                                                         vnm->
385                                                         sw_interface_admin_up_down_functions);
386           if (error)
387             goto done;
388         }
389     }
390   else
391     {
392       vnet_sw_interface_t *si_sup = si;
393
394       /* Check that super interface is in correct state. */
395       if (si->type == VNET_SW_INTERFACE_TYPE_SUB)
396         {
397           si_sup = vnet_get_sw_interface (vnm, si->sup_sw_if_index);
398
399           /* Check to see if we're bringing down the soft interface and if it's parent is up */
400           if ((flags != (si_sup->flags & mask)) &&
401               (!((flags == 0)
402                  && ((si_sup->flags & mask) ==
403                      VNET_SW_INTERFACE_FLAG_ADMIN_UP))))
404             {
405               error = clib_error_return (0, "super-interface %U must be %U",
406                                          format_vnet_sw_interface_name, vnm,
407                                          si_sup,
408                                          format_vnet_sw_interface_flags,
409                                          flags);
410               goto done;
411             }
412         }
413
414       /* Already in the desired state? */
415       if ((si->flags & mask) == flags)
416         goto done;
417
418       /* Sub-interfaces of hardware interfaces that do no redistribute,
419          do not redistribute themselves. */
420       if (si_sup->type == VNET_SW_INTERFACE_TYPE_HARDWARE)
421         {
422           vnet_hw_interface_t *hi =
423             vnet_get_hw_interface (vnm, si_sup->hw_if_index);
424           vnet_device_class_t *dev_class =
425             vnet_get_device_class (vnm, hi->dev_class_index);
426           if (!dev_class->redistribute)
427             helper_flags &=
428               ~VNET_INTERFACE_SET_FLAGS_HELPER_WANT_REDISTRIBUTE;
429         }
430
431       /* set the flags now before invoking the registered clients
432        * so that the state they query is consistent with the state here notified */
433       old_flags = si->flags;
434       si->flags &= ~mask;
435       si->flags |= flags;
436       if ((flags | old_flags) & VNET_SW_INTERFACE_FLAG_ADMIN_UP)
437         error = call_elf_section_interface_callbacks
438           (vnm, sw_if_index, flags,
439            vnm->sw_interface_admin_up_down_functions);
440
441       if (error)
442         {
443           /* restore flags on error */
444           si->flags = old_flags;
445           goto done;
446         }
447
448       if (si->type == VNET_SW_INTERFACE_TYPE_HARDWARE)
449         {
450           vnet_hw_interface_t *hi =
451             vnet_get_hw_interface (vnm, si->hw_if_index);
452           vnet_hw_interface_class_t *hw_class =
453             vnet_get_hw_interface_class (vnm, hi->hw_class_index);
454           vnet_device_class_t *dev_class =
455             vnet_get_device_class (vnm, hi->dev_class_index);
456
457           if ((flags & VNET_SW_INTERFACE_FLAG_ADMIN_UP) &&
458               (si->flags & VNET_SW_INTERFACE_FLAG_ERROR))
459             {
460               error = clib_error_return (0, "Interface in the error state");
461               goto done;
462             }
463
464           /* save the si admin up flag */
465           old_flags = si->flags;
466
467           /* update si admin up flag in advance if we are going admin down */
468           if (!(flags & VNET_SW_INTERFACE_FLAG_ADMIN_UP))
469             si->flags &= ~VNET_SW_INTERFACE_FLAG_ADMIN_UP;
470
471           if (dev_class->admin_up_down_function
472               && (error = dev_class->admin_up_down_function (vnm,
473                                                              si->hw_if_index,
474                                                              flags)))
475             {
476               /* restore si admin up flag to it's original state on errors */
477               si->flags = old_flags;
478               goto done;
479             }
480
481           if (hw_class->admin_up_down_function
482               && (error = hw_class->admin_up_down_function (vnm,
483                                                             si->hw_if_index,
484                                                             flags)))
485             {
486               /* restore si admin up flag to it's original state on errors */
487               si->flags = old_flags;
488               goto done;
489             }
490
491           /* Admin down implies link down. */
492           if (!(flags & VNET_SW_INTERFACE_FLAG_ADMIN_UP)
493               && (hi->flags & VNET_HW_INTERFACE_FLAG_LINK_UP))
494             vnet_hw_interface_set_flags_helper (vnm, si->hw_if_index,
495                                                 hi->flags &
496                                                 ~VNET_HW_INTERFACE_FLAG_LINK_UP,
497                                                 helper_flags);
498           vnet_hw_if_update_runtime_data (vnm, si->hw_if_index);
499         }
500     }
501
502   si->flags &= ~mask;
503   si->flags |= flags;
504
505 done:
506   if (error)
507     log_err ("sw_set_flags_helper: %U", format_clib_error, error);
508   return error;
509 }
510
511 clib_error_t *
512 vnet_hw_interface_set_flags (vnet_main_t * vnm, u32 hw_if_index,
513                              vnet_hw_interface_flags_t flags)
514 {
515   log_debug ("hw_set_flags: hw_if_index %u flags 0x%x", hw_if_index, flags);
516   return vnet_hw_interface_set_flags_helper
517     (vnm, hw_if_index, flags,
518      VNET_INTERFACE_SET_FLAGS_HELPER_WANT_REDISTRIBUTE);
519 }
520
521 clib_error_t *
522 vnet_sw_interface_set_flags (vnet_main_t * vnm, u32 sw_if_index,
523                              vnet_sw_interface_flags_t flags)
524 {
525   log_debug ("sw_set_flags: sw_if_index %u flags 0x%x", sw_if_index, flags);
526   return vnet_sw_interface_set_flags_helper
527     (vnm, sw_if_index, flags,
528      VNET_INTERFACE_SET_FLAGS_HELPER_WANT_REDISTRIBUTE);
529 }
530
531 void
532 vnet_sw_interface_admin_up (vnet_main_t * vnm, u32 sw_if_index)
533 {
534   u32 flags = vnet_sw_interface_get_flags (vnm, sw_if_index);
535   log_debug ("sw_admin_up: sw_if_index %u", sw_if_index);
536
537   if (!(flags & VNET_SW_INTERFACE_FLAG_ADMIN_UP))
538     {
539       flags |= VNET_SW_INTERFACE_FLAG_ADMIN_UP;
540       vnet_sw_interface_set_flags (vnm, sw_if_index, flags);
541     }
542 }
543
544 void
545 vnet_sw_interface_admin_down (vnet_main_t * vnm, u32 sw_if_index)
546 {
547   u32 flags = vnet_sw_interface_get_flags (vnm, sw_if_index);
548   log_debug ("sw_admin_down: sw_if_index %u", sw_if_index);
549
550   if (flags & VNET_SW_INTERFACE_FLAG_ADMIN_UP)
551     {
552       flags &= ~(VNET_SW_INTERFACE_FLAG_ADMIN_UP);
553       vnet_sw_interface_set_flags (vnm, sw_if_index, flags);
554     }
555 }
556
557 static u32
558 vnet_create_sw_interface_no_callbacks (vnet_main_t * vnm,
559                                        vnet_sw_interface_t * template)
560 {
561   vnet_interface_main_t *im = &vnm->interface_main;
562   vnet_sw_interface_t *sw;
563   u32 sw_if_index;
564
565   pool_get (im->sw_interfaces, sw);
566   sw_if_index = sw - im->sw_interfaces;
567
568   sw[0] = template[0];
569
570   sw->flags = 0;
571   sw->sw_if_index = sw_if_index;
572   if (sw->type == VNET_SW_INTERFACE_TYPE_HARDWARE)
573     sw->sup_sw_if_index = sw->sw_if_index;
574
575   /* Allocate counters for this interface. */
576   {
577     u32 i;
578
579     vnet_interface_counter_lock (im);
580
581     for (i = 0; i < vec_len (im->sw_if_counters); i++)
582       {
583         vlib_validate_simple_counter (&im->sw_if_counters[i], sw_if_index);
584         vlib_zero_simple_counter (&im->sw_if_counters[i], sw_if_index);
585       }
586
587     for (i = 0; i < vec_len (im->combined_sw_if_counters); i++)
588       {
589         vlib_validate_combined_counter (&im->combined_sw_if_counters[i],
590                                         sw_if_index);
591         vlib_zero_combined_counter (&im->combined_sw_if_counters[i],
592                                     sw_if_index);
593       }
594
595     vnet_interface_counter_unlock (im);
596   }
597
598   return sw_if_index;
599 }
600
601 clib_error_t *
602 vnet_create_sw_interface (vnet_main_t * vnm, vnet_sw_interface_t * template,
603                           u32 * sw_if_index)
604 {
605   vnet_interface_main_t *im = &vnm->interface_main;
606   clib_error_t *error;
607   vnet_hw_interface_t *hi;
608   vnet_device_class_t *dev_class;
609
610   if (template->sub.eth.flags.two_tags == 1
611       && template->sub.eth.flags.exact_match == 1
612       && (template->sub.eth.flags.inner_vlan_id_any == 1
613           || template->sub.eth.flags.outer_vlan_id_any == 1))
614     {
615       char *str = "inner-dot1q any exact-match is unsupported";
616       error = clib_error_return (0, str);
617       log_err ("create_sw_interface: %s", str);
618       return error;
619     }
620
621   hi = vnet_get_sup_hw_interface (vnm, template->sup_sw_if_index);
622   dev_class = vnet_get_device_class (vnm, hi->dev_class_index);
623
624   if (template->type == VNET_SW_INTERFACE_TYPE_SUB &&
625       dev_class->subif_add_del_function)
626     {
627       error = dev_class->subif_add_del_function (vnm, hi->hw_if_index,
628                                                  (struct vnet_sw_interface_t
629                                                   *) template, 1);
630       if (error)
631         return error;
632     }
633
634   *sw_if_index = vnet_create_sw_interface_no_callbacks (vnm, template);
635   error = vnet_sw_interface_set_flags_helper
636     (vnm, *sw_if_index, template->flags,
637      VNET_INTERFACE_SET_FLAGS_HELPER_IS_CREATE);
638
639   if (error)
640     {
641       /* undo the work done by vnet_create_sw_interface_no_callbacks() */
642       log_err ("create_sw_interface: set flags failed\n  %U",
643                format_clib_error, error);
644       vnet_sw_interface_t *sw =
645         pool_elt_at_index (im->sw_interfaces, *sw_if_index);
646       pool_put (im->sw_interfaces, sw);
647     }
648   else
649     {
650       vnet_sw_interface_t *sw =
651         pool_elt_at_index (im->sw_interfaces, *sw_if_index);
652       log_debug ("create_sw_interface: interface %U (sw_if_index %u) created",
653                  format_vnet_sw_interface_name, vnm, sw, *sw_if_index);
654     }
655
656   return error;
657 }
658
659 void
660 vnet_delete_sw_interface (vnet_main_t * vnm, u32 sw_if_index)
661 {
662   vnet_interface_main_t *im = &vnm->interface_main;
663   vnet_sw_interface_t *sw =
664     pool_elt_at_index (im->sw_interfaces, sw_if_index);
665
666   log_debug ("delete_sw_interface: sw_if_index %u, name '%U'",
667              sw_if_index, format_vnet_sw_if_index_name, vnm, sw_if_index);
668
669   /* Check if the interface has config and is removed from L2 BD or XConnect */
670   vnet_clear_sw_interface_tag (vnm, sw_if_index);
671
672   /* Bring down interface in case it is up. */
673   if (sw->flags != 0)
674     vnet_sw_interface_set_flags (vnm, sw_if_index, /* flags */ 0);
675
676   call_sw_interface_add_del_callbacks (vnm, sw_if_index, /* is_create */ 0);
677
678   pool_put (im->sw_interfaces, sw);
679 }
680
681 static clib_error_t *
682 call_sw_interface_mtu_change_callbacks (vnet_main_t * vnm, u32 sw_if_index)
683 {
684   return call_elf_section_interface_callbacks
685     (vnm, sw_if_index, 0, vnm->sw_interface_mtu_change_functions);
686 }
687
688 void
689 vnet_sw_interface_set_mtu (vnet_main_t * vnm, u32 sw_if_index, u32 mtu)
690 {
691   vnet_sw_interface_t *si = vnet_get_sw_interface (vnm, sw_if_index);
692
693   if (si->mtu[VNET_MTU_L3] != mtu)
694     {
695       si->mtu[VNET_MTU_L3] = mtu;
696       log_debug ("set_mtu: interface %U, new mtu %u",
697                  format_vnet_sw_if_index_name, vnm, sw_if_index, mtu);
698
699       call_sw_interface_mtu_change_callbacks (vnm, sw_if_index);
700     }
701 }
702
703 void
704 vnet_sw_interface_set_protocol_mtu (vnet_main_t * vnm, u32 sw_if_index,
705                                     u32 mtu[])
706 {
707   vnet_sw_interface_t *si = vnet_get_sw_interface (vnm, sw_if_index);
708   bool changed = false;
709   int i;
710
711   for (i = 0; i < VNET_N_MTU; i++)
712     {
713       if (si->mtu[i] != mtu[i])
714         {
715           si->mtu[i] = mtu[i];
716           changed = true;
717         }
718     }
719   /* Notify interested parties */
720   if (changed)
721     {
722       log_debug ("set_protocol_mtu: interface %U l3 %u ip4 %u ip6 %u mpls %u",
723                  format_vnet_sw_if_index_name, vnm, sw_if_index,
724                  mtu[VNET_MTU_L3], mtu[VNET_MTU_IP4], mtu[VNET_MTU_IP6],
725                  mtu[VNET_MTU_MPLS]);
726       call_sw_interface_mtu_change_callbacks (vnm, sw_if_index);
727     }
728 }
729
730 void
731 vnet_sw_interface_ip_directed_broadcast (vnet_main_t * vnm,
732                                          u32 sw_if_index, u8 enable)
733 {
734   vnet_sw_interface_t *si;
735
736   si = vnet_get_sw_interface (vnm, sw_if_index);
737
738   if (enable)
739     si->flags |= VNET_SW_INTERFACE_FLAG_DIRECTED_BCAST;
740   else
741     si->flags &= ~VNET_SW_INTERFACE_FLAG_DIRECTED_BCAST;
742
743   ip4_directed_broadcast (sw_if_index, enable);
744 }
745
746 /*
747  * Reflect a change in hardware MTU on protocol MTUs
748  */
749 static walk_rc_t
750 sw_interface_walk_callback (vnet_main_t * vnm, u32 sw_if_index, void *ctx)
751 {
752   u32 *link_mtu = ctx;
753   vnet_sw_interface_set_mtu (vnm, sw_if_index, *link_mtu);
754   return WALK_CONTINUE;
755 }
756
757 void
758 vnet_hw_interface_set_mtu (vnet_main_t * vnm, u32 hw_if_index, u32 mtu)
759 {
760   vnet_hw_interface_t *hi = vnet_get_hw_interface (vnm, hw_if_index);
761
762   if (hi->max_packet_bytes != mtu)
763     {
764       hi->max_packet_bytes = mtu;
765       ethernet_set_flags (vnm, hw_if_index, ETHERNET_INTERFACE_FLAG_MTU);
766       vnet_hw_interface_walk_sw (vnm, hw_if_index, sw_interface_walk_callback,
767                                  &mtu);
768     }
769 }
770
771 static void
772 setup_tx_node (vlib_main_t * vm,
773                u32 node_index, vnet_device_class_t * dev_class)
774 {
775   vlib_node_t *n = vlib_get_node (vm, node_index);
776
777   n->format_trace = dev_class->format_tx_trace;
778
779   /// XXX: Update this to use counter structure
780   vlib_register_errors (vm, node_index,
781                         dev_class->tx_function_n_errors,
782                         dev_class->tx_function_error_strings, 0);
783 }
784
785 static void
786 setup_output_node (vlib_main_t * vm,
787                    u32 node_index, vnet_hw_interface_class_t * hw_class)
788 {
789   vlib_node_t *n = vlib_get_node (vm, node_index);
790   n->format_buffer = hw_class->format_header;
791   n->unformat_buffer = hw_class->unformat_header;
792 }
793
794 /* Register an interface instance. */
795 u32
796 vnet_register_interface (vnet_main_t * vnm,
797                          u32 dev_class_index,
798                          u32 dev_instance,
799                          u32 hw_class_index, u32 hw_instance)
800 {
801   vnet_interface_main_t *im = &vnm->interface_main;
802   vnet_hw_interface_t *hw;
803   vnet_device_class_t *dev_class =
804     vnet_get_device_class (vnm, dev_class_index);
805   vnet_hw_interface_class_t *hw_class =
806     vnet_get_hw_interface_class (vnm, hw_class_index);
807   vlib_main_t *vm = vnm->vlib_main;
808   vnet_feature_config_main_t *fcm;
809   vnet_config_main_t *cm;
810   u32 hw_index, i;
811   char *tx_node_name = NULL, *output_node_name = NULL;
812   vlib_node_t *if_out_node =
813     vlib_get_node (vm, vnet_interface_output_node.index);
814
815   pool_get (im->hw_interfaces, hw);
816   clib_memset (hw, 0, sizeof (*hw));
817   hw->trace_classify_table_index = ~0;
818
819   hw_index = hw - im->hw_interfaces;
820   hw->hw_if_index = hw_index;
821   hw->default_rx_mode = VNET_HW_IF_RX_MODE_POLLING;
822
823   if (dev_class->format_device_name)
824     hw->name = format (0, "%U", dev_class->format_device_name, dev_instance);
825   else if (hw_class->format_interface_name)
826     hw->name = format (0, "%U", hw_class->format_interface_name,
827                        dev_instance);
828   else
829     hw->name = format (0, "%s%x", hw_class->name, dev_instance);
830
831   if (!im->hw_interface_by_name)
832     im->hw_interface_by_name = hash_create_vec ( /* size */ 0,
833                                                 sizeof (hw->name[0]),
834                                                 sizeof (uword));
835
836   hash_set_mem (im->hw_interface_by_name, hw->name, hw_index);
837
838   /* Make hardware interface point to software interface. */
839   {
840     vnet_sw_interface_t sw = {
841       .type = VNET_SW_INTERFACE_TYPE_HARDWARE,
842       .flood_class = VNET_FLOOD_CLASS_NORMAL,
843       .hw_if_index = hw_index
844     };
845     hw->sw_if_index = vnet_create_sw_interface_no_callbacks (vnm, &sw);
846   }
847
848   hw->dev_class_index = dev_class_index;
849   hw->dev_instance = dev_instance;
850   hw->hw_class_index = hw_class_index;
851   hw->hw_instance = hw_instance;
852
853   hw->max_rate_bits_per_sec = 0;
854   hw->min_packet_bytes = 0;
855   vnet_sw_interface_set_mtu (vnm, hw->sw_if_index, 0);
856
857   if (dev_class->tx_function == 0 && dev_class->tx_fn_registrations == 0)
858     goto no_output_nodes;       /* No output/tx nodes to create */
859
860   tx_node_name = (char *) format (0, "%v-tx", hw->name);
861   output_node_name = (char *) format (0, "%v-output", hw->name);
862
863   /* If we have previously deleted interface nodes, re-use them. */
864   if (vec_len (im->deleted_hw_interface_nodes) > 0)
865     {
866       vnet_hw_interface_nodes_t *hn;
867       vlib_node_t *node;
868       vlib_node_runtime_t *nrt;
869
870       hn = vec_end (im->deleted_hw_interface_nodes) - 1;
871
872       hw->tx_node_index = hn->tx_node_index;
873       hw->output_node_index = hn->output_node_index;
874
875       vlib_node_rename (vm, hw->tx_node_index, "%v", tx_node_name);
876       vlib_node_rename (vm, hw->output_node_index, "%v", output_node_name);
877
878       foreach_vlib_main ()
879         {
880           vnet_interface_output_runtime_t *rt;
881
882           rt =
883             vlib_node_get_runtime_data (this_vlib_main, hw->output_node_index);
884           ASSERT (rt->is_deleted == 1);
885           rt->is_deleted = 0;
886           rt->hw_if_index = hw_index;
887           rt->sw_if_index = hw->sw_if_index;
888           rt->dev_instance = hw->dev_instance;
889
890           rt = vlib_node_get_runtime_data (this_vlib_main, hw->tx_node_index);
891           rt->hw_if_index = hw_index;
892           rt->sw_if_index = hw->sw_if_index;
893           rt->dev_instance = hw->dev_instance;
894         }
895
896       /* The new class may differ from the old one.
897        * Functions have to be updated. */
898       node = vlib_get_node (vm, hw->output_node_index);
899       node->format_trace = format_vnet_interface_output_trace;
900       node->node_fn_registrations = if_out_node->node_fn_registrations;
901       node->function = if_out_node->function;
902
903       foreach_vlib_main ()
904         {
905           nrt = vlib_node_get_runtime (this_vlib_main, hw->output_node_index);
906           nrt->function = node->function;
907           vlib_node_runtime_perf_counter (this_vlib_main, nrt, 0, 0, 0,
908                                           VLIB_NODE_RUNTIME_PERF_RESET);
909         }
910
911       node = vlib_get_node (vm, hw->tx_node_index);
912       if (dev_class->tx_fn_registrations)
913         {
914           node->node_fn_registrations = dev_class->tx_fn_registrations;
915           node->function = vlib_node_get_preferred_node_fn_variant (
916             vm, dev_class->tx_fn_registrations);
917         }
918       else
919         node->function = dev_class->tx_function;
920       node->format_trace = dev_class->format_tx_trace;
921
922       foreach_vlib_main ()
923         {
924           nrt = vlib_node_get_runtime (this_vlib_main, hw->tx_node_index);
925           nrt->function = node->function;
926           vlib_node_runtime_perf_counter (this_vlib_main, nrt, 0, 0, 0,
927                                           VLIB_NODE_RUNTIME_PERF_RESET);
928         }
929
930       _vec_len (im->deleted_hw_interface_nodes) -= 1;
931     }
932   else
933     {
934       vlib_node_registration_t r;
935       vnet_interface_output_runtime_t rt = {
936         .hw_if_index = hw_index,
937         .sw_if_index = hw->sw_if_index,
938         .dev_instance = hw->dev_instance,
939         .is_deleted = 0,
940       };
941
942       clib_memset (&r, 0, sizeof (r));
943       r.type = VLIB_NODE_TYPE_INTERNAL;
944       r.runtime_data = &rt;
945       r.runtime_data_bytes = sizeof (rt);
946       r.scalar_size = 0;
947       r.vector_size = sizeof (u32);
948
949       r.flags = VLIB_NODE_FLAG_IS_OUTPUT;
950       r.name = tx_node_name;
951       if (dev_class->tx_fn_registrations)
952         {
953           r.function = 0;
954           r.node_fn_registrations = dev_class->tx_fn_registrations;
955         }
956       else
957         r.function = dev_class->tx_function;
958
959       hw->tx_node_index = vlib_register_node (vm, &r);
960
961       vlib_node_add_named_next_with_slot (vm, hw->tx_node_index,
962                                           "error-drop",
963                                           VNET_INTERFACE_TX_NEXT_DROP);
964
965       r.flags = 0;
966       r.name = output_node_name;
967       r.format_trace = format_vnet_interface_output_trace;
968       if (if_out_node->node_fn_registrations)
969         {
970           r.function = 0;
971           r.node_fn_registrations = if_out_node->node_fn_registrations;
972         }
973       else
974         r.function = if_out_node->function;
975
976       {
977         static char *e[] = {
978           "interface is down",
979           "interface is deleted",
980           "no buffers to segment GSO",
981         };
982
983         r.n_errors = ARRAY_LEN (e);
984         r.error_strings = e;
985       }
986       hw->output_node_index = vlib_register_node (vm, &r);
987
988       vlib_node_add_named_next_with_slot (vm, hw->output_node_index,
989                                           "error-drop",
990                                           VNET_INTERFACE_OUTPUT_NEXT_DROP);
991       vlib_node_add_next_with_slot (vm, hw->output_node_index,
992                                     hw->tx_node_index,
993                                     VNET_INTERFACE_OUTPUT_NEXT_TX);
994
995       /* add interface to the list of "output-interface" feature arc start nodes
996          and clone nexts from 1st interface if it exists */
997       fcm = vnet_feature_get_config_main (im->output_feature_arc_index);
998       cm = &fcm->config_main;
999       i = vec_len (cm->start_node_indices);
1000       vec_validate (cm->start_node_indices, i);
1001       cm->start_node_indices[i] = hw->output_node_index;
1002       if (hw_index)
1003         {
1004           /* copy nexts from 1st interface */
1005           vnet_hw_interface_t *first_hw;
1006           vlib_node_t *first_node;
1007
1008           first_hw = vnet_get_hw_interface (vnm, /* hw_if_index */ 0);
1009           first_node = vlib_get_node (vm, first_hw->output_node_index);
1010
1011           /* 1st 2 nexts are already added above */
1012           for (i = 2; i < vec_len (first_node->next_nodes); i++)
1013             vlib_node_add_next_with_slot (vm, hw->output_node_index,
1014                                           first_node->next_nodes[i], i);
1015         }
1016     }
1017
1018   setup_output_node (vm, hw->output_node_index, hw_class);
1019   setup_tx_node (vm, hw->tx_node_index, dev_class);
1020
1021 no_output_nodes:
1022   /* Call all up/down callbacks with zero flags when interface is created. */
1023   vnet_sw_interface_set_flags_helper (vnm, hw->sw_if_index, /* flags */ 0,
1024                                       VNET_INTERFACE_SET_FLAGS_HELPER_IS_CREATE);
1025   vnet_hw_interface_set_flags_helper (vnm, hw_index, /* flags */ 0,
1026                                       VNET_INTERFACE_SET_FLAGS_HELPER_IS_CREATE);
1027   vec_free (tx_node_name);
1028   vec_free (output_node_name);
1029
1030   return hw_index;
1031 }
1032
1033 void
1034 vnet_delete_hw_interface (vnet_main_t * vnm, u32 hw_if_index)
1035 {
1036   vnet_interface_main_t *im = &vnm->interface_main;
1037   vnet_hw_interface_t *hw = vnet_get_hw_interface (vnm, hw_if_index);
1038   vlib_main_t *vm = vnm->vlib_main;
1039   vnet_device_class_t *dev_class = vnet_get_device_class (vnm,
1040                                                           hw->dev_class_index);
1041   /* If it is up, mark it down. */
1042   if (hw->flags != 0)
1043     vnet_hw_interface_set_flags (vnm, hw_if_index, /* flags */ 0);
1044
1045   /* Call delete callbacks. */
1046   call_hw_interface_add_del_callbacks (vnm, hw_if_index, /* is_create */ 0);
1047
1048   /* delete rx queues */
1049   vnet_hw_if_unregister_all_rx_queues (vnm, hw_if_index);
1050   vnet_hw_if_update_runtime_data (vnm, hw_if_index);
1051
1052   /* Delete any sub-interfaces. */
1053   {
1054     u32 id, sw_if_index;
1055     /* *INDENT-OFF* */
1056     hash_foreach (id, sw_if_index, hw->sub_interface_sw_if_index_by_id,
1057     ({
1058       vnet_sw_interface_t *si = vnet_get_sw_interface (vnm, sw_if_index);
1059       u64 sup_and_sub_key =
1060         ((u64) (si->sup_sw_if_index) << 32) | (u64) si->sub.id;
1061       hash_unset_mem_free (&im->sw_if_index_by_sup_and_sub, &sup_and_sub_key);
1062       vnet_delete_sw_interface (vnm, sw_if_index);
1063     }));
1064     hash_free (hw->sub_interface_sw_if_index_by_id);
1065     /* *INDENT-ON* */
1066   }
1067
1068   /* Delete software interface corresponding to hardware interface. */
1069   vnet_delete_sw_interface (vnm, hw->sw_if_index);
1070
1071   if (dev_class->tx_function)
1072     {
1073       /* Put output/tx nodes into recycle pool */
1074       vnet_hw_interface_nodes_t *dn;
1075
1076       foreach_vlib_main ()
1077         {
1078           vnet_interface_output_runtime_t *rt =
1079             vlib_node_get_runtime_data (this_vlib_main, hw->output_node_index);
1080
1081           /* Mark node runtime as deleted so output node (if called)
1082            * will drop packets. */
1083           rt->is_deleted = 1;
1084         }
1085
1086       vlib_node_rename (vm, hw->output_node_index,
1087                         "interface-%d-output-deleted", hw_if_index);
1088       vlib_node_rename (vm, hw->tx_node_index, "interface-%d-tx-deleted",
1089                         hw_if_index);
1090       vec_add2 (im->deleted_hw_interface_nodes, dn, 1);
1091       dn->tx_node_index = hw->tx_node_index;
1092       dn->output_node_index = hw->output_node_index;
1093     }
1094
1095   hash_unset_mem (im->hw_interface_by_name, hw->name);
1096   vec_free (hw->name);
1097   vec_free (hw->hw_address);
1098   vec_free (hw->input_node_thread_index_by_queue);
1099   vec_free (hw->rx_queue_indices);
1100   pool_put (im->hw_interfaces, hw);
1101 }
1102
1103 void
1104 vnet_hw_interface_walk_sw (vnet_main_t * vnm,
1105                            u32 hw_if_index,
1106                            vnet_hw_sw_interface_walk_t fn, void *ctx)
1107 {
1108   vnet_hw_interface_t *hi;
1109   u32 id, sw_if_index;
1110
1111   hi = vnet_get_hw_interface (vnm, hw_if_index);
1112   /* the super first, then the sub interfaces */
1113   if (WALK_STOP == fn (vnm, hi->sw_if_index, ctx))
1114     return;
1115
1116   /* *INDENT-OFF* */
1117   hash_foreach (id, sw_if_index,
1118                 hi->sub_interface_sw_if_index_by_id,
1119   ({
1120     if (WALK_STOP == fn (vnm, sw_if_index, ctx))
1121       break;
1122   }));
1123   /* *INDENT-ON* */
1124 }
1125
1126 void
1127 vnet_hw_interface_walk (vnet_main_t * vnm,
1128                         vnet_hw_interface_walk_t fn, void *ctx)
1129 {
1130   vnet_interface_main_t *im;
1131   vnet_hw_interface_t *hi;
1132
1133   im = &vnm->interface_main;
1134
1135   /* *INDENT-OFF* */
1136   pool_foreach (hi, im->hw_interfaces)
1137    {
1138     if (WALK_STOP == fn(vnm, hi->hw_if_index, ctx))
1139       break;
1140   }
1141   /* *INDENT-ON* */
1142 }
1143
1144 void
1145 vnet_sw_interface_walk (vnet_main_t * vnm,
1146                         vnet_sw_interface_walk_t fn, void *ctx)
1147 {
1148   vnet_interface_main_t *im;
1149   vnet_sw_interface_t *si;
1150
1151   im = &vnm->interface_main;
1152
1153   /* *INDENT-OFF* */
1154   pool_foreach (si, im->sw_interfaces)
1155   {
1156     if (WALK_STOP == fn (vnm, si, ctx))
1157       break;
1158   }
1159   /* *INDENT-ON* */
1160 }
1161
1162 void
1163 vnet_hw_interface_init_for_class (vnet_main_t * vnm, u32 hw_if_index,
1164                                   u32 hw_class_index, u32 hw_instance)
1165 {
1166   vnet_hw_interface_t *hi = vnet_get_hw_interface (vnm, hw_if_index);
1167   vnet_hw_interface_class_t *hc =
1168     vnet_get_hw_interface_class (vnm, hw_class_index);
1169
1170   hi->hw_class_index = hw_class_index;
1171   hi->hw_instance = hw_instance;
1172   setup_output_node (vnm->vlib_main, hi->output_node_index, hc);
1173 }
1174
1175 static clib_error_t *
1176 vnet_hw_interface_set_class_helper (vnet_main_t * vnm, u32 hw_if_index,
1177                                     u32 hw_class_index, u32 redistribute)
1178 {
1179   vnet_hw_interface_t *hi = vnet_get_hw_interface (vnm, hw_if_index);
1180   vnet_sw_interface_t *si = vnet_get_sw_interface (vnm, hi->sw_if_index);
1181   vnet_hw_interface_class_t *old_class =
1182     vnet_get_hw_interface_class (vnm, hi->hw_class_index);
1183   vnet_hw_interface_class_t *new_class =
1184     vnet_get_hw_interface_class (vnm, hw_class_index);
1185   vnet_device_class_t *dev_class =
1186     vnet_get_device_class (vnm, hi->dev_class_index);
1187   clib_error_t *error = 0;
1188
1189   /* New class equals old class?  Nothing to do. */
1190   if (hi->hw_class_index == hw_class_index)
1191     return 0;
1192
1193   /* No need (and incorrect since admin up flag may be set) to do error checking when
1194      receiving unserialize message. */
1195   if (redistribute)
1196     {
1197       if (si->flags & VNET_SW_INTERFACE_FLAG_ADMIN_UP)
1198         return clib_error_return (0,
1199                                   "%v must be admin down to change class from %s to %s",
1200                                   hi->name, old_class->name, new_class->name);
1201
1202       /* Make sure interface supports given class. */
1203       if ((new_class->is_valid_class_for_interface
1204            && !new_class->is_valid_class_for_interface (vnm, hw_if_index,
1205                                                         hw_class_index))
1206           || (dev_class->is_valid_class_for_interface
1207               && !dev_class->is_valid_class_for_interface (vnm, hw_if_index,
1208                                                            hw_class_index)))
1209         return clib_error_return (0,
1210                                   "%v class cannot be changed from %s to %s",
1211                                   hi->name, old_class->name, new_class->name);
1212
1213     }
1214
1215   if (old_class->hw_class_change)
1216     old_class->hw_class_change (vnm, hw_if_index, old_class->index,
1217                                 new_class->index);
1218
1219   vnet_hw_interface_init_for_class (vnm, hw_if_index, new_class->index,
1220                                     /* instance */ ~0);
1221
1222   if (new_class->hw_class_change)
1223     new_class->hw_class_change (vnm, hw_if_index, old_class->index,
1224                                 new_class->index);
1225
1226   if (dev_class->hw_class_change)
1227     dev_class->hw_class_change (vnm, hw_if_index, new_class->index);
1228
1229   return error;
1230 }
1231
1232 clib_error_t *
1233 vnet_hw_interface_set_class (vnet_main_t * vnm, u32 hw_if_index,
1234                              u32 hw_class_index)
1235 {
1236   return vnet_hw_interface_set_class_helper (vnm, hw_if_index, hw_class_index,
1237                                              /* redistribute */ 1);
1238 }
1239
1240 static int
1241 vnet_hw_interface_rx_redirect_to_node_helper (vnet_main_t * vnm,
1242                                               u32 hw_if_index,
1243                                               u32 node_index,
1244                                               u32 redistribute)
1245 {
1246   vnet_hw_interface_t *hi = vnet_get_hw_interface (vnm, hw_if_index);
1247   vnet_device_class_t *dev_class = vnet_get_device_class
1248     (vnm, hi->dev_class_index);
1249
1250   if (dev_class->rx_redirect_to_node)
1251     {
1252       dev_class->rx_redirect_to_node (vnm, hw_if_index, node_index);
1253       return 0;
1254     }
1255
1256   return VNET_API_ERROR_UNIMPLEMENTED;
1257 }
1258
1259 int
1260 vnet_hw_interface_rx_redirect_to_node (vnet_main_t * vnm, u32 hw_if_index,
1261                                        u32 node_index)
1262 {
1263   return vnet_hw_interface_rx_redirect_to_node_helper (vnm, hw_if_index,
1264                                                        node_index,
1265                                                        1 /* redistribute */ );
1266 }
1267
1268 word
1269 vnet_sw_interface_compare (vnet_main_t * vnm,
1270                            uword sw_if_index0, uword sw_if_index1)
1271 {
1272   vnet_sw_interface_t *sup0 = vnet_get_sup_sw_interface (vnm, sw_if_index0);
1273   vnet_sw_interface_t *sup1 = vnet_get_sup_sw_interface (vnm, sw_if_index1);
1274   vnet_hw_interface_t *h0 = vnet_get_hw_interface (vnm, sup0->hw_if_index);
1275   vnet_hw_interface_t *h1 = vnet_get_hw_interface (vnm, sup1->hw_if_index);
1276
1277   if (h0 != h1)
1278     return vec_cmp (h0->name, h1->name);
1279   return (word) h0->hw_instance - (word) h1->hw_instance;
1280 }
1281
1282 word
1283 vnet_hw_interface_compare (vnet_main_t * vnm,
1284                            uword hw_if_index0, uword hw_if_index1)
1285 {
1286   vnet_hw_interface_t *h0 = vnet_get_hw_interface (vnm, hw_if_index0);
1287   vnet_hw_interface_t *h1 = vnet_get_hw_interface (vnm, hw_if_index1);
1288
1289   if (h0 != h1)
1290     return vec_cmp (h0->name, h1->name);
1291   return (word) h0->hw_instance - (word) h1->hw_instance;
1292 }
1293
1294 int
1295 vnet_sw_interface_is_p2p (vnet_main_t * vnm, u32 sw_if_index)
1296 {
1297   vnet_sw_interface_t *si = vnet_get_sw_interface (vnm, sw_if_index);
1298   if ((si->type == VNET_SW_INTERFACE_TYPE_P2P) ||
1299       (si->type == VNET_SW_INTERFACE_TYPE_PIPE))
1300     return 1;
1301
1302   vnet_hw_interface_t *hw = vnet_get_sup_hw_interface (vnm, sw_if_index);
1303   vnet_hw_interface_class_t *hc =
1304     vnet_get_hw_interface_class (vnm, hw->hw_class_index);
1305
1306   return (hc->flags & VNET_HW_INTERFACE_CLASS_FLAG_P2P);
1307 }
1308
1309 int
1310 vnet_sw_interface_is_nbma (vnet_main_t * vnm, u32 sw_if_index)
1311 {
1312   vnet_hw_interface_t *hw = vnet_get_sup_hw_interface (vnm, sw_if_index);
1313   vnet_hw_interface_class_t *hc =
1314     vnet_get_hw_interface_class (vnm, hw->hw_class_index);
1315
1316   return (hc->flags & VNET_HW_INTERFACE_CLASS_FLAG_NBMA);
1317 }
1318
1319 clib_error_t *
1320 vnet_interface_init (vlib_main_t * vm)
1321 {
1322   vnet_main_t *vnm = vnet_get_main ();
1323   vnet_interface_main_t *im = &vnm->interface_main;
1324   vlib_buffer_t *b = 0;
1325   vnet_buffer_opaque_t *o = 0;
1326   clib_error_t *error;
1327
1328   /*
1329    * Keep people from shooting themselves in the foot.
1330    */
1331   if (sizeof (b->opaque) != sizeof (vnet_buffer_opaque_t))
1332     {
1333 #define _(a) if (sizeof(o->a) > sizeof (o->unused))                     \
1334       clib_warning                                                      \
1335         ("FATAL: size of opaque union subtype %s is %d (max %d)",       \
1336          #a, sizeof(o->a), sizeof (o->unused));
1337       foreach_buffer_opaque_union_subtype;
1338 #undef _
1339
1340       return clib_error_return
1341         (0, "FATAL: size of vlib buffer opaque %d, size of vnet opaque %d",
1342          sizeof (b->opaque), sizeof (vnet_buffer_opaque_t));
1343     }
1344
1345   clib_spinlock_init (&im->sw_if_counter_lock);
1346   clib_spinlock_lock (&im->sw_if_counter_lock); /* should be no need */
1347
1348   vec_validate (im->sw_if_counters, VNET_N_SIMPLE_INTERFACE_COUNTER - 1);
1349 #define _(E,n,p)                                                        \
1350   im->sw_if_counters[VNET_INTERFACE_COUNTER_##E].name = #n;             \
1351   im->sw_if_counters[VNET_INTERFACE_COUNTER_##E].stat_segment_name = "/" #p "/" #n;
1352   foreach_simple_interface_counter_name
1353 #undef _
1354     vec_validate (im->combined_sw_if_counters,
1355                   VNET_N_COMBINED_INTERFACE_COUNTER - 1);
1356 #define _(E,n,p)                                                        \
1357   im->combined_sw_if_counters[VNET_INTERFACE_COUNTER_##E].name = #n;    \
1358   im->combined_sw_if_counters[VNET_INTERFACE_COUNTER_##E].stat_segment_name = "/" #p "/" #n;
1359   foreach_combined_interface_counter_name
1360 #undef _
1361     clib_spinlock_unlock (&im->sw_if_counter_lock);
1362
1363   im->device_class_by_name = hash_create_string ( /* size */ 0,
1364                                                  sizeof (uword));
1365   {
1366     vnet_device_class_t *c;
1367
1368     c = vnm->device_class_registrations;
1369
1370     while (c)
1371       {
1372         c->index = vec_len (im->device_classes);
1373         hash_set_mem (im->device_class_by_name, c->name, c->index);
1374
1375         /* to avoid confusion, please remove ".tx_function" statement
1376           from VNET_DEVICE_CLASS() if using function candidates */
1377         ASSERT (c->tx_fn_registrations == 0 || c->tx_function == 0);
1378
1379         if (c->tx_fn_registrations)
1380           c->tx_function = vlib_node_get_preferred_node_fn_variant (
1381             vm, c->tx_fn_registrations);
1382
1383         vec_add1 (im->device_classes, c[0]);
1384         c = c->next_class_registration;
1385       }
1386   }
1387
1388   im->hw_interface_class_by_name = hash_create_string ( /* size */ 0,
1389                                                        sizeof (uword));
1390
1391   im->rxq_index_by_hw_if_index_and_queue_id =
1392     hash_create_mem (0, sizeof (u64), sizeof (u32));
1393   im->sw_if_index_by_sup_and_sub = hash_create_mem (0, sizeof (u64),
1394                                                     sizeof (uword));
1395   {
1396     vnet_hw_interface_class_t *c;
1397
1398     c = vnm->hw_interface_class_registrations;
1399
1400     while (c)
1401       {
1402         c->index = vec_len (im->hw_interface_classes);
1403         hash_set_mem (im->hw_interface_class_by_name, c->name, c->index);
1404
1405         if (NULL == c->build_rewrite)
1406           c->build_rewrite = default_build_rewrite;
1407         if (NULL == c->update_adjacency)
1408           c->update_adjacency = default_update_adjacency;
1409
1410         vec_add1 (im->hw_interface_classes, c[0]);
1411         c = c->next_class_registration;
1412       }
1413   }
1414
1415   /* init per-thread data */
1416   vec_validate_aligned (im->per_thread_data, vlib_num_workers (),
1417                         CLIB_CACHE_LINE_BYTES);
1418
1419   if ((error = vlib_call_init_function (vm, vnet_interface_cli_init)))
1420     return error;
1421
1422   vnm->interface_tag_by_sw_if_index = hash_create (0, sizeof (uword));
1423
1424 #if VLIB_BUFFER_TRACE_TRAJECTORY > 0
1425   if ((error = vlib_call_init_function (vm, trajectory_trace_init)))
1426     return error;
1427 #endif
1428
1429   return 0;
1430 }
1431
1432 VLIB_INIT_FUNCTION (vnet_interface_init);
1433
1434 /* Kludge to renumber interface names [only!] */
1435 int
1436 vnet_interface_name_renumber (u32 sw_if_index, u32 new_show_dev_instance)
1437 {
1438   int rv;
1439   vnet_main_t *vnm = vnet_get_main ();
1440   vnet_interface_main_t *im = &vnm->interface_main;
1441   vnet_hw_interface_t *hi = vnet_get_sup_hw_interface (vnm, sw_if_index);
1442
1443   vnet_device_class_t *dev_class = vnet_get_device_class
1444     (vnm, hi->dev_class_index);
1445
1446   if (dev_class->name_renumber == 0 || dev_class->format_device_name == 0)
1447     return VNET_API_ERROR_UNIMPLEMENTED;
1448
1449   rv = dev_class->name_renumber (hi, new_show_dev_instance);
1450
1451   if (rv)
1452     return rv;
1453
1454   hash_unset_mem (im->hw_interface_by_name, hi->name);
1455   vec_free (hi->name);
1456   /* Use the mapping we set up to call it Ishmael */
1457   hi->name = format (0, "%U", dev_class->format_device_name,
1458                      hi->dev_instance);
1459
1460   hash_set_mem (im->hw_interface_by_name, hi->name, hi->hw_if_index);
1461   return rv;
1462 }
1463
1464 clib_error_t *
1465 vnet_rename_interface (vnet_main_t * vnm, u32 hw_if_index, char *new_name)
1466 {
1467   vnet_interface_main_t *im = &vnm->interface_main;
1468   vlib_main_t *vm = vnm->vlib_main;
1469   vnet_hw_interface_t *hw;
1470   u8 *old_name;
1471   clib_error_t *error = 0;
1472
1473   hw = vnet_get_hw_interface (vnm, hw_if_index);
1474   if (!hw)
1475     {
1476       return clib_error_return (0,
1477                                 "unable to find hw interface for index %u",
1478                                 hw_if_index);
1479     }
1480
1481   old_name = hw->name;
1482
1483   /* set new hw->name */
1484   hw->name = format (0, "%s", new_name);
1485
1486   /* remove the old name to hw_if_index mapping and install the new one */
1487   hash_unset_mem (im->hw_interface_by_name, old_name);
1488   hash_set_mem (im->hw_interface_by_name, hw->name, hw_if_index);
1489
1490   /* rename tx/output nodes */
1491   vlib_node_rename (vm, hw->tx_node_index, "%v-tx", hw->name);
1492   vlib_node_rename (vm, hw->output_node_index, "%v-output", hw->name);
1493
1494   /* free the old name vector */
1495   vec_free (old_name);
1496
1497   return error;
1498 }
1499
1500 clib_error_t *
1501 vnet_hw_interface_add_del_mac_address (vnet_main_t * vnm,
1502                                        u32 hw_if_index,
1503                                        const u8 * mac_address, u8 is_add)
1504 {
1505   clib_error_t *error = 0;
1506   vnet_hw_interface_t *hi = vnet_get_hw_interface (vnm, hw_if_index);
1507
1508   vnet_device_class_t *dev_class =
1509     vnet_get_device_class (vnm, hi->dev_class_index);
1510
1511   if (!hi->hw_address)
1512     {
1513       error =
1514         clib_error_return
1515         (0, "Secondary MAC Addresses not supported for interface index %u",
1516          hw_if_index);
1517       goto done;
1518     }
1519
1520   if (dev_class->mac_addr_add_del_function)
1521     error = dev_class->mac_addr_add_del_function (hi, mac_address, is_add);
1522
1523   if (!error)
1524     {
1525       vnet_hw_interface_class_t *hw_class;
1526
1527       hw_class = vnet_get_hw_interface_class (vnm, hi->hw_class_index);
1528
1529       if (NULL != hw_class->mac_addr_add_del_function)
1530         error = hw_class->mac_addr_add_del_function (hi, mac_address, is_add);
1531     }
1532
1533   /* If no errors, add to the list of secondary MACs on the ethernet intf */
1534   if (!error)
1535     ethernet_interface_add_del_address (&ethernet_main, hw_if_index,
1536                                         mac_address, is_add);
1537
1538 done:
1539   if (error)
1540     log_err ("hw_add_del_mac_address: %U", format_clib_error, error);
1541   return error;
1542 }
1543
1544 static clib_error_t *
1545 vnet_hw_interface_change_mac_address_helper (vnet_main_t * vnm,
1546                                              u32 hw_if_index,
1547                                              const u8 * mac_address)
1548 {
1549   clib_error_t *error = 0;
1550   vnet_hw_interface_t *hi = vnet_get_hw_interface (vnm, hw_if_index);
1551
1552   if (hi->hw_address)
1553     {
1554       u8 *old_address = vec_dup (hi->hw_address);
1555       vnet_device_class_t *dev_class =
1556         vnet_get_device_class (vnm, hi->dev_class_index);
1557       if (dev_class->mac_addr_change_function)
1558         {
1559           error =
1560             dev_class->mac_addr_change_function (hi, old_address,
1561                                                  mac_address);
1562         }
1563       if (!error)
1564         {
1565           vnet_hw_interface_class_t *hw_class;
1566
1567           hw_class = vnet_get_hw_interface_class (vnm, hi->hw_class_index);
1568
1569           if (NULL != hw_class->mac_addr_change_function)
1570             hw_class->mac_addr_change_function (hi, old_address, mac_address);
1571         }
1572       else
1573         {
1574           error =
1575             clib_error_return (0,
1576                                "MAC Address Change is not supported on this interface");
1577         }
1578       vec_free (old_address);
1579     }
1580   else
1581     {
1582       error =
1583         clib_error_return (0,
1584                            "mac address change is not supported for interface index %u",
1585                            hw_if_index);
1586     }
1587   return error;
1588 }
1589
1590 clib_error_t *
1591 vnet_hw_interface_change_mac_address (vnet_main_t * vnm, u32 hw_if_index,
1592                                       const u8 * mac_address)
1593 {
1594   return vnet_hw_interface_change_mac_address_helper
1595     (vnm, hw_if_index, mac_address);
1596 }
1597
1598 /* update the unnumbered state of an interface*/
1599 void
1600 vnet_sw_interface_update_unnumbered (u32 unnumbered_sw_if_index,
1601                                      u32 ip_sw_if_index, u8 enable)
1602 {
1603   vnet_main_t *vnm = vnet_get_main ();
1604   vnet_sw_interface_t *si;
1605   u32 was_unnum;
1606
1607   si = vnet_get_sw_interface (vnm, unnumbered_sw_if_index);
1608   was_unnum = (si->flags & VNET_SW_INTERFACE_FLAG_UNNUMBERED);
1609
1610   if (enable)
1611     {
1612       si->flags |= VNET_SW_INTERFACE_FLAG_UNNUMBERED;
1613       si->unnumbered_sw_if_index = ip_sw_if_index;
1614
1615       ip4_main.lookup_main.if_address_pool_index_by_sw_if_index
1616         [unnumbered_sw_if_index] =
1617         ip4_main.
1618         lookup_main.if_address_pool_index_by_sw_if_index[ip_sw_if_index];
1619       ip6_main.
1620         lookup_main.if_address_pool_index_by_sw_if_index
1621         [unnumbered_sw_if_index] =
1622         ip6_main.
1623         lookup_main.if_address_pool_index_by_sw_if_index[ip_sw_if_index];
1624     }
1625   else
1626     {
1627       si->flags &= ~(VNET_SW_INTERFACE_FLAG_UNNUMBERED);
1628       si->unnumbered_sw_if_index = (u32) ~ 0;
1629
1630       ip4_main.lookup_main.if_address_pool_index_by_sw_if_index
1631         [unnumbered_sw_if_index] = ~0;
1632       ip6_main.lookup_main.if_address_pool_index_by_sw_if_index
1633         [unnumbered_sw_if_index] = ~0;
1634     }
1635
1636   if (was_unnum != (si->flags & VNET_SW_INTERFACE_FLAG_UNNUMBERED))
1637     {
1638       ip4_sw_interface_enable_disable (unnumbered_sw_if_index, enable);
1639       ip6_sw_interface_enable_disable (unnumbered_sw_if_index, enable);
1640     }
1641 }
1642
1643 vnet_l3_packet_type_t
1644 vnet_link_to_l3_proto (vnet_link_t link)
1645 {
1646   switch (link)
1647     {
1648     case VNET_LINK_IP4:
1649       return (VNET_L3_PACKET_TYPE_IP4);
1650     case VNET_LINK_IP6:
1651       return (VNET_L3_PACKET_TYPE_IP6);
1652     case VNET_LINK_MPLS:
1653       return (VNET_L3_PACKET_TYPE_MPLS);
1654     case VNET_LINK_ARP:
1655       return (VNET_L3_PACKET_TYPE_ARP);
1656     case VNET_LINK_ETHERNET:
1657     case VNET_LINK_NSH:
1658       ASSERT (0);
1659       break;
1660     }
1661   ASSERT (0);
1662   return (0);
1663 }
1664
1665 vnet_mtu_t
1666 vnet_link_to_mtu (vnet_link_t link)
1667 {
1668   switch (link)
1669     {
1670     case VNET_LINK_IP4:
1671       return (VNET_MTU_IP4);
1672     case VNET_LINK_IP6:
1673       return (VNET_MTU_IP6);
1674     case VNET_LINK_MPLS:
1675       return (VNET_MTU_MPLS);
1676     default:
1677       return (VNET_MTU_L3);
1678     }
1679 }
1680
1681 u8 *
1682 default_build_rewrite (vnet_main_t * vnm,
1683                        u32 sw_if_index,
1684                        vnet_link_t link_type, const void *dst_address)
1685 {
1686   return (NULL);
1687 }
1688
1689 void
1690 default_update_adjacency (vnet_main_t * vnm, u32 sw_if_index, u32 ai)
1691 {
1692   ip_adjacency_t *adj;
1693
1694   adj = adj_get (ai);
1695
1696   switch (adj->lookup_next_index)
1697     {
1698     case IP_LOOKUP_NEXT_GLEAN:
1699       adj_glean_update_rewrite (ai);
1700       break;
1701     case IP_LOOKUP_NEXT_ARP:
1702     case IP_LOOKUP_NEXT_BCAST:
1703       /*
1704        * default rewrite in neighbour adj
1705        */
1706       adj_nbr_update_rewrite
1707         (ai,
1708          ADJ_NBR_REWRITE_FLAG_COMPLETE,
1709          vnet_build_rewrite_for_sw_interface (vnm,
1710                                               sw_if_index,
1711                                               adj_get_link_type (ai), NULL));
1712       break;
1713     case IP_LOOKUP_NEXT_MCAST:
1714       /*
1715        * mcast traffic also uses default rewrite string with no mcast
1716        * switch time updates.
1717        */
1718       adj_mcast_update_rewrite
1719         (ai,
1720          vnet_build_rewrite_for_sw_interface (vnm,
1721                                               sw_if_index,
1722                                               adj_get_link_type (ai),
1723                                               NULL), 0);
1724       break;
1725     case IP_LOOKUP_NEXT_DROP:
1726     case IP_LOOKUP_NEXT_PUNT:
1727     case IP_LOOKUP_NEXT_LOCAL:
1728     case IP_LOOKUP_NEXT_REWRITE:
1729     case IP_LOOKUP_NEXT_MCAST_MIDCHAIN:
1730     case IP_LOOKUP_NEXT_MIDCHAIN:
1731     case IP_LOOKUP_NEXT_ICMP_ERROR:
1732     case IP_LOOKUP_N_NEXT:
1733       ASSERT (0);
1734       break;
1735     }
1736 }
1737
1738 clib_error_t *
1739 vnet_hw_interface_set_rss_queues (vnet_main_t * vnm,
1740                                   vnet_hw_interface_t * hi,
1741                                   clib_bitmap_t * bitmap)
1742 {
1743   clib_error_t *error = 0;
1744   vnet_device_class_t *dev_class =
1745     vnet_get_device_class (vnm, hi->dev_class_index);
1746
1747   if (dev_class->set_rss_queues_function)
1748     {
1749       if (clib_bitmap_count_set_bits (bitmap) == 0)
1750         {
1751           error = clib_error_return (0,
1752                                      "must assign at least one valid rss queue");
1753           goto done;
1754         }
1755
1756       error = dev_class->set_rss_queues_function (vnm, hi, bitmap);
1757     }
1758   else
1759     {
1760       error = clib_error_return (0,
1761                                  "setting rss queues is not supported on this interface");
1762     }
1763
1764   if (!error)
1765     {
1766       clib_bitmap_free (hi->rss_queues);
1767       hi->rss_queues = clib_bitmap_dup (bitmap);
1768     }
1769
1770 done:
1771   if (error)
1772     log_err ("hw_set_rss_queues: %U", format_clib_error, error);
1773   return error;
1774 }
1775
1776 int collect_detailed_interface_stats_flag = 0;
1777
1778 void
1779 collect_detailed_interface_stats_flag_set (void)
1780 {
1781   collect_detailed_interface_stats_flag = 1;
1782 }
1783
1784 void
1785 collect_detailed_interface_stats_flag_clear (void)
1786 {
1787   collect_detailed_interface_stats_flag = 0;
1788 }
1789
1790 static clib_error_t *
1791 collect_detailed_interface_stats_cli (vlib_main_t * vm,
1792                                       unformat_input_t * input,
1793                                       vlib_cli_command_t * cmd)
1794 {
1795   unformat_input_t _line_input, *line_input = &_line_input;
1796   clib_error_t *error = NULL;
1797
1798   /* Get a line of input. */
1799   if (!unformat_user (input, unformat_line_input, line_input))
1800     return clib_error_return (0, "expected enable | disable");
1801
1802   while (unformat_check_input (line_input) != UNFORMAT_END_OF_INPUT)
1803     {
1804       if (unformat (line_input, "enable") || unformat (line_input, "on"))
1805         collect_detailed_interface_stats_flag_set ();
1806       else if (unformat (line_input, "disable")
1807                || unformat (line_input, "off"))
1808         collect_detailed_interface_stats_flag_clear ();
1809       else
1810         {
1811           error = clib_error_return (0, "unknown input `%U'",
1812                                      format_unformat_error, line_input);
1813           goto done;
1814         }
1815     }
1816
1817 done:
1818   unformat_free (line_input);
1819   return error;
1820 }
1821
1822 /* *INDENT-OFF* */
1823 VLIB_CLI_COMMAND (collect_detailed_interface_stats_command, static) = {
1824   .path = "interface collect detailed-stats",
1825   .short_help = "interface collect detailed-stats <enable|disable>",
1826   .function = collect_detailed_interface_stats_cli,
1827 };
1828 /* *INDENT-ON* */
1829
1830 /*
1831  * fd.io coding-style-patch-verification: ON
1832  *
1833  * Local Variables:
1834  * eval: (c-set-style "gnu")
1835  * End:
1836  */