L2 over LISP and GRE (VPP-457)
[vpp.git] / vnet / 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/fib/ip6_fib.h>
43
44 #define VNET_INTERFACE_SET_FLAGS_HELPER_IS_CREATE (1 << 0)
45 #define VNET_INTERFACE_SET_FLAGS_HELPER_WANT_REDISTRIBUTE (1 << 1)
46
47 static clib_error_t *vnet_hw_interface_set_flags_helper (vnet_main_t * vnm,
48                                                          u32 hw_if_index,
49                                                          u32 flags,
50                                                          u32 helper_flags);
51
52 static clib_error_t *vnet_sw_interface_set_flags_helper (vnet_main_t * vnm,
53                                                          u32 sw_if_index,
54                                                          u32 flags,
55                                                          u32 helper_flags);
56
57 static clib_error_t *vnet_hw_interface_set_class_helper (vnet_main_t * vnm,
58                                                          u32 hw_if_index,
59                                                          u32 hw_class_index,
60                                                          u32 redistribute);
61
62 typedef struct
63 {
64   /* Either sw or hw interface index. */
65   u32 sw_hw_if_index;
66
67   /* Flags. */
68   u32 flags;
69 } vnet_sw_hw_interface_state_t;
70
71 static void
72 serialize_vec_vnet_sw_hw_interface_state (serialize_main_t * m, va_list * va)
73 {
74   vnet_sw_hw_interface_state_t *s =
75     va_arg (*va, vnet_sw_hw_interface_state_t *);
76   u32 n = va_arg (*va, u32);
77   u32 i;
78   for (i = 0; i < n; i++)
79     {
80       serialize_integer (m, s[i].sw_hw_if_index,
81                          sizeof (s[i].sw_hw_if_index));
82       serialize_integer (m, s[i].flags, sizeof (s[i].flags));
83     }
84 }
85
86 static void
87 unserialize_vec_vnet_sw_hw_interface_state (serialize_main_t * m,
88                                             va_list * va)
89 {
90   vnet_sw_hw_interface_state_t *s =
91     va_arg (*va, vnet_sw_hw_interface_state_t *);
92   u32 n = va_arg (*va, u32);
93   u32 i;
94   for (i = 0; i < n; i++)
95     {
96       unserialize_integer (m, &s[i].sw_hw_if_index,
97                            sizeof (s[i].sw_hw_if_index));
98       unserialize_integer (m, &s[i].flags, sizeof (s[i].flags));
99     }
100 }
101
102 static void
103 serialize_vnet_sw_hw_interface_set_flags (serialize_main_t * m, va_list * va)
104 {
105   vnet_sw_hw_interface_state_t *s =
106     va_arg (*va, vnet_sw_hw_interface_state_t *);
107   serialize (m, serialize_vec_vnet_sw_hw_interface_state, s, 1);
108 }
109
110 static void
111 unserialize_vnet_sw_interface_set_flags (serialize_main_t * m, va_list * va)
112 {
113   CLIB_UNUSED (mc_main_t * mc) = va_arg (*va, mc_main_t *);
114   vnet_sw_hw_interface_state_t s;
115
116   unserialize (m, unserialize_vec_vnet_sw_hw_interface_state, &s, 1);
117
118   vnet_sw_interface_set_flags_helper
119     (vnet_get_main (), s.sw_hw_if_index, s.flags,
120      /* helper_flags no redistribution */ 0);
121 }
122
123 static void
124 unserialize_vnet_hw_interface_set_flags (serialize_main_t * m, va_list * va)
125 {
126   CLIB_UNUSED (mc_main_t * mc) = va_arg (*va, mc_main_t *);
127   vnet_sw_hw_interface_state_t s;
128
129   unserialize (m, unserialize_vec_vnet_sw_hw_interface_state, &s, 1);
130
131   vnet_hw_interface_set_flags_helper
132     (vnet_get_main (), s.sw_hw_if_index, s.flags,
133      /* helper_flags no redistribution */ 0);
134 }
135
136 MC_SERIALIZE_MSG (vnet_sw_interface_set_flags_msg, static) =
137 {
138 .name = "vnet_sw_interface_set_flags",.serialize =
139     serialize_vnet_sw_hw_interface_set_flags,.unserialize =
140     unserialize_vnet_sw_interface_set_flags,};
141
142 MC_SERIALIZE_MSG (vnet_hw_interface_set_flags_msg, static) =
143 {
144 .name = "vnet_hw_interface_set_flags",.serialize =
145     serialize_vnet_sw_hw_interface_set_flags,.unserialize =
146     unserialize_vnet_hw_interface_set_flags,};
147
148 void
149 serialize_vnet_interface_state (serialize_main_t * m, va_list * va)
150 {
151   vnet_main_t *vnm = va_arg (*va, vnet_main_t *);
152   vnet_sw_hw_interface_state_t *sts = 0, *st;
153   vnet_sw_interface_t *sif;
154   vnet_hw_interface_t *hif;
155   vnet_interface_main_t *im = &vnm->interface_main;
156
157   /* Serialize hardware interface classes since they may have changed.
158      Must do this before sending up/down flags. */
159   /* *INDENT-OFF* */
160   pool_foreach (hif, im->hw_interfaces, ({
161     vnet_hw_interface_class_t * hw_class = vnet_get_hw_interface_class (vnm, hif->hw_class_index);
162     serialize_cstring (m, hw_class->name);
163   }));
164   /* *INDENT-ON* */
165
166   /* Send sw/hw interface state when non-zero. */
167   /* *INDENT-OFF* */
168   pool_foreach (sif, im->sw_interfaces, ({
169     if (sif->flags != 0)
170       {
171         vec_add2 (sts, st, 1);
172         st->sw_hw_if_index = sif->sw_if_index;
173         st->flags = sif->flags;
174       }
175   }));
176   /* *INDENT-ON* */
177
178   vec_serialize (m, sts, serialize_vec_vnet_sw_hw_interface_state);
179
180   if (sts)
181     _vec_len (sts) = 0;
182
183   /* *INDENT-OFF* */
184   pool_foreach (hif, im->hw_interfaces, ({
185     if (hif->flags != 0)
186       {
187         vec_add2 (sts, st, 1);
188         st->sw_hw_if_index = hif->hw_if_index;
189         st->flags = hif->flags;
190       }
191   }));
192   /* *INDENT-ON* */
193
194   vec_serialize (m, sts, serialize_vec_vnet_sw_hw_interface_state);
195
196   vec_free (sts);
197 }
198
199 void
200 unserialize_vnet_interface_state (serialize_main_t * m, va_list * va)
201 {
202   vnet_main_t *vnm = va_arg (*va, vnet_main_t *);
203   vnet_sw_hw_interface_state_t *sts = 0, *st;
204
205   /* First set interface hardware class. */
206   {
207     vnet_interface_main_t *im = &vnm->interface_main;
208     vnet_hw_interface_t *hif;
209     char *class_name;
210     uword *p;
211     clib_error_t *error;
212
213     /* *INDENT-OFF* */
214     pool_foreach (hif, im->hw_interfaces, ({
215       unserialize_cstring (m, &class_name);
216       p = hash_get_mem (im->hw_interface_class_by_name, class_name);
217       ASSERT (p != 0);
218       error = vnet_hw_interface_set_class_helper (vnm, hif->hw_if_index, p[0], /* redistribute */ 0);
219       if (error)
220         clib_error_report (error);
221       vec_free (class_name);
222     }));
223     /* *INDENT-ON* */
224   }
225
226   vec_unserialize (m, &sts, unserialize_vec_vnet_sw_hw_interface_state);
227   vec_foreach (st, sts)
228     vnet_sw_interface_set_flags_helper (vnm, st->sw_hw_if_index, st->flags,
229                                         /* no distribute */ 0);
230   vec_free (sts);
231
232   vec_unserialize (m, &sts, unserialize_vec_vnet_sw_hw_interface_state);
233   vec_foreach (st, sts)
234     vnet_hw_interface_set_flags_helper (vnm, st->sw_hw_if_index, st->flags,
235                                         /* no distribute */ 0);
236   vec_free (sts);
237 }
238
239 static clib_error_t *
240 call_elf_section_interface_callbacks (vnet_main_t * vnm, u32 if_index,
241                                       u32 flags,
242                                       _vnet_interface_function_list_elt_t *
243                                       elt)
244 {
245   clib_error_t *error = 0;
246
247   while (elt)
248     {
249       error = elt->fp (vnm, if_index, flags);
250       if (error)
251         return error;
252       elt = elt->next_interface_function;
253     }
254   return error;
255 }
256
257 static clib_error_t *
258 call_hw_interface_add_del_callbacks (vnet_main_t * vnm, u32 hw_if_index,
259                                      u32 is_create)
260 {
261   vnet_hw_interface_t *hi = vnet_get_hw_interface (vnm, hw_if_index);
262   vnet_hw_interface_class_t *hw_class =
263     vnet_get_hw_interface_class (vnm, hi->hw_class_index);
264   vnet_device_class_t *dev_class =
265     vnet_get_device_class (vnm, hi->dev_class_index);
266   clib_error_t *error = 0;
267
268   if (hw_class->interface_add_del_function
269       && (error =
270           hw_class->interface_add_del_function (vnm, hw_if_index, is_create)))
271     return error;
272
273   if (dev_class->interface_add_del_function
274       && (error =
275           dev_class->interface_add_del_function (vnm, hw_if_index,
276                                                  is_create)))
277     return error;
278
279   error = call_elf_section_interface_callbacks
280     (vnm, hw_if_index, is_create, vnm->hw_interface_add_del_functions);
281
282   return error;
283 }
284
285 static clib_error_t *
286 call_sw_interface_add_del_callbacks (vnet_main_t * vnm, u32 sw_if_index,
287                                      u32 is_create)
288 {
289   return call_elf_section_interface_callbacks
290     (vnm, sw_if_index, is_create, vnm->sw_interface_add_del_functions);
291 }
292
293 #define VNET_INTERFACE_SET_FLAGS_HELPER_IS_CREATE (1 << 0)
294 #define VNET_INTERFACE_SET_FLAGS_HELPER_WANT_REDISTRIBUTE (1 << 1)
295
296 static clib_error_t *
297 vnet_hw_interface_set_flags_helper (vnet_main_t * vnm, u32 hw_if_index,
298                                     u32 flags, u32 helper_flags)
299 {
300   vnet_hw_interface_t *hi = vnet_get_hw_interface (vnm, hw_if_index);
301   vnet_hw_interface_class_t *hw_class =
302     vnet_get_hw_interface_class (vnm, hi->hw_class_index);
303   vnet_device_class_t *dev_class =
304     vnet_get_device_class (vnm, hi->dev_class_index);
305   vlib_main_t *vm = vnm->vlib_main;
306   u32 mask;
307   clib_error_t *error = 0;
308   u32 is_create =
309     (helper_flags & VNET_INTERFACE_SET_FLAGS_HELPER_IS_CREATE) != 0;
310
311   mask =
312     (VNET_HW_INTERFACE_FLAG_LINK_UP | VNET_HW_INTERFACE_FLAG_DUPLEX_MASK |
313      VNET_HW_INTERFACE_FLAG_SPEED_MASK);
314   flags &= mask;
315
316   /* Call hardware interface add/del callbacks. */
317   if (is_create)
318     call_hw_interface_add_del_callbacks (vnm, hw_if_index, is_create);
319
320   /* Already in the desired state? */
321   if (!is_create && (hi->flags & mask) == flags)
322     goto done;
323
324   /* Some interface classes do not redistribute (e.g. are local). */
325   if (!dev_class->redistribute)
326     helper_flags &= ~VNET_INTERFACE_SET_FLAGS_HELPER_WANT_REDISTRIBUTE;
327
328   if (vm->mc_main
329       && (helper_flags & VNET_INTERFACE_SET_FLAGS_HELPER_WANT_REDISTRIBUTE))
330     {
331       vnet_sw_hw_interface_state_t s;
332       s.sw_hw_if_index = hw_if_index;
333       s.flags = flags;
334       mc_serialize (vm->mc_main, &vnet_hw_interface_set_flags_msg, &s);
335     }
336
337   if ((hi->flags & VNET_HW_INTERFACE_FLAG_LINK_UP) !=
338       (flags & VNET_HW_INTERFACE_FLAG_LINK_UP))
339     {
340       /* Do hardware class (e.g. ethernet). */
341       if (hw_class->link_up_down_function
342           && (error = hw_class->link_up_down_function (vnm, hw_if_index,
343                                                        flags)))
344         goto done;
345
346       error = call_elf_section_interface_callbacks
347         (vnm, hw_if_index, flags, vnm->hw_interface_link_up_down_functions);
348
349       if (error)
350         goto done;
351     }
352
353   hi->flags &= ~mask;
354   hi->flags |= flags;
355
356 done:
357   return error;
358 }
359
360 static clib_error_t *
361 vnet_sw_interface_set_flags_helper (vnet_main_t * vnm, u32 sw_if_index,
362                                     u32 flags, u32 helper_flags)
363 {
364   vnet_sw_interface_t *si = vnet_get_sw_interface (vnm, sw_if_index);
365   vlib_main_t *vm = vnm->vlib_main;
366   u32 mask;
367   clib_error_t *error = 0;
368   u32 is_create =
369     (helper_flags & VNET_INTERFACE_SET_FLAGS_HELPER_IS_CREATE) != 0;
370   u32 old_flags;
371
372   mask = VNET_SW_INTERFACE_FLAG_ADMIN_UP | VNET_SW_INTERFACE_FLAG_PUNT;
373   flags &= mask;
374
375   if (is_create)
376     {
377       error =
378         call_sw_interface_add_del_callbacks (vnm, sw_if_index, is_create);
379       if (error)
380         goto done;
381
382       if (flags & VNET_SW_INTERFACE_FLAG_ADMIN_UP)
383         {
384           /* Notify everyone when the interface is created as admin up */
385           error = call_elf_section_interface_callbacks (vnm, sw_if_index,
386                                                         flags,
387                                                         vnm->
388                                                         sw_interface_admin_up_down_functions);
389           if (error)
390             goto done;
391         }
392     }
393   else
394     {
395       vnet_sw_interface_t *si_sup = si;
396
397       /* Check that super interface is in correct state. */
398       if (si->type == VNET_SW_INTERFACE_TYPE_SUB)
399         {
400           si_sup = vnet_get_sw_interface (vnm, si->sup_sw_if_index);
401
402           /* Check to see if we're bringing down the soft interface and if it's parent is up */
403           if ((flags != (si_sup->flags & mask)) &&
404               (!((flags == 0)
405                  && ((si_sup->flags & mask) ==
406                      VNET_SW_INTERFACE_FLAG_ADMIN_UP))))
407             {
408               error = clib_error_return (0, "super-interface %U must be %U",
409                                          format_vnet_sw_interface_name, vnm,
410                                          si_sup,
411                                          format_vnet_sw_interface_flags,
412                                          flags);
413               goto done;
414             }
415         }
416
417       /* Donot change state for slave link of bonded interfaces */
418       if (si->flags & VNET_SW_INTERFACE_FLAG_BOND_SLAVE)
419         {
420           error = clib_error_return
421             (0, "not allowed as %U belong to a BondEthernet interface",
422              format_vnet_sw_interface_name, vnm, si);
423           goto done;
424         }
425
426       /* Already in the desired state? */
427       if ((si->flags & mask) == flags)
428         goto done;
429
430       /* Sub-interfaces of hardware interfaces that do no redistribute,
431          do not redistribute themselves. */
432       if (si_sup->type == VNET_SW_INTERFACE_TYPE_HARDWARE)
433         {
434           vnet_hw_interface_t *hi =
435             vnet_get_hw_interface (vnm, si_sup->hw_if_index);
436           vnet_device_class_t *dev_class =
437             vnet_get_device_class (vnm, hi->dev_class_index);
438           if (!dev_class->redistribute)
439             helper_flags &=
440               ~VNET_INTERFACE_SET_FLAGS_HELPER_WANT_REDISTRIBUTE;
441         }
442
443       if (vm->mc_main
444           && (helper_flags &
445               VNET_INTERFACE_SET_FLAGS_HELPER_WANT_REDISTRIBUTE))
446         {
447           vnet_sw_hw_interface_state_t s;
448           s.sw_hw_if_index = sw_if_index;
449           s.flags = flags;
450           mc_serialize (vm->mc_main, &vnet_sw_interface_set_flags_msg, &s);
451         }
452
453       /* set the flags now before invoking the registered clients
454        * so that the state they query is consistent with the state here notified */
455       old_flags = si->flags;
456       si->flags &= ~mask;
457       si->flags |= flags;
458       if ((flags | old_flags) & VNET_SW_INTERFACE_FLAG_ADMIN_UP)
459         error = call_elf_section_interface_callbacks
460           (vnm, sw_if_index, flags,
461            vnm->sw_interface_admin_up_down_functions);
462       si->flags = old_flags;
463
464       if (error)
465         goto done;
466
467       if (si->type == VNET_SW_INTERFACE_TYPE_HARDWARE)
468         {
469           vnet_hw_interface_t *hi =
470             vnet_get_hw_interface (vnm, si->hw_if_index);
471           vnet_hw_interface_class_t *hw_class =
472             vnet_get_hw_interface_class (vnm, hi->hw_class_index);
473           vnet_device_class_t *dev_class =
474             vnet_get_device_class (vnm, hi->dev_class_index);
475
476           /* save the si admin up flag */
477           old_flags = si->flags;
478
479           /* update si admin up flag in advance if we are going admin down */
480           if (!(flags & VNET_SW_INTERFACE_FLAG_ADMIN_UP))
481             si->flags &= ~VNET_SW_INTERFACE_FLAG_ADMIN_UP;
482
483           if (dev_class->admin_up_down_function
484               && (error = dev_class->admin_up_down_function (vnm,
485                                                              si->hw_if_index,
486                                                              flags)))
487             {
488               /* restore si admin up flag to it's original state on errors */
489               si->flags = old_flags;
490               goto done;
491             }
492
493           if (hw_class->admin_up_down_function
494               && (error = hw_class->admin_up_down_function (vnm,
495                                                             si->hw_if_index,
496                                                             flags)))
497             {
498               /* restore si admin up flag to it's original state on errors */
499               si->flags = old_flags;
500               goto done;
501             }
502
503           /* Admin down implies link down. */
504           if (!(flags & VNET_SW_INTERFACE_FLAG_ADMIN_UP)
505               && (hi->flags & VNET_HW_INTERFACE_FLAG_LINK_UP))
506             vnet_hw_interface_set_flags_helper (vnm, si->hw_if_index,
507                                                 hi->flags &
508                                                 ~VNET_HW_INTERFACE_FLAG_LINK_UP,
509                                                 helper_flags);
510         }
511     }
512
513   si->flags &= ~mask;
514   si->flags |= flags;
515
516 done:
517   return error;
518 }
519
520 clib_error_t *
521 vnet_hw_interface_set_flags (vnet_main_t * vnm, u32 hw_if_index, u32 flags)
522 {
523   return vnet_hw_interface_set_flags_helper
524     (vnm, hw_if_index, flags,
525      VNET_INTERFACE_SET_FLAGS_HELPER_WANT_REDISTRIBUTE);
526 }
527
528 clib_error_t *
529 vnet_sw_interface_set_flags (vnet_main_t * vnm, u32 sw_if_index, u32 flags)
530 {
531   return vnet_sw_interface_set_flags_helper
532     (vnm, sw_if_index, flags,
533      VNET_INTERFACE_SET_FLAGS_HELPER_WANT_REDISTRIBUTE);
534 }
535
536 static u32
537 vnet_create_sw_interface_no_callbacks (vnet_main_t * vnm,
538                                        vnet_sw_interface_t * template)
539 {
540   vnet_interface_main_t *im = &vnm->interface_main;
541   vnet_sw_interface_t *sw;
542   u32 sw_if_index;
543
544   pool_get (im->sw_interfaces, sw);
545   sw_if_index = sw - im->sw_interfaces;
546
547   sw[0] = template[0];
548
549   sw->flags = 0;
550   sw->sw_if_index = sw_if_index;
551   if (sw->type == VNET_SW_INTERFACE_TYPE_HARDWARE)
552     sw->sup_sw_if_index = sw->sw_if_index;
553
554   /* Allocate counters for this interface. */
555   {
556     u32 i;
557
558     vnet_interface_counter_lock (im);
559
560     for (i = 0; i < vec_len (im->sw_if_counters); i++)
561       {
562         vlib_validate_simple_counter (&im->sw_if_counters[i], sw_if_index);
563         vlib_zero_simple_counter (&im->sw_if_counters[i], sw_if_index);
564       }
565
566     for (i = 0; i < vec_len (im->combined_sw_if_counters); i++)
567       {
568         vlib_validate_combined_counter (&im->combined_sw_if_counters[i],
569                                         sw_if_index);
570         vlib_zero_combined_counter (&im->combined_sw_if_counters[i],
571                                     sw_if_index);
572       }
573
574     vnet_interface_counter_unlock (im);
575   }
576
577   return sw_if_index;
578 }
579
580 clib_error_t *
581 vnet_create_sw_interface (vnet_main_t * vnm, vnet_sw_interface_t * template,
582                           u32 * sw_if_index)
583 {
584   clib_error_t *error;
585   vnet_hw_interface_t *hi;
586   vnet_device_class_t *dev_class;
587
588   hi = vnet_get_sup_hw_interface (vnm, template->sup_sw_if_index);
589   dev_class = vnet_get_device_class (vnm, hi->dev_class_index);
590
591   if (template->type == VNET_SW_INTERFACE_TYPE_SUB &&
592       dev_class->subif_add_del_function)
593     {
594       error = dev_class->subif_add_del_function (vnm, hi->hw_if_index,
595                                                  (struct vnet_sw_interface_t
596                                                   *) template, 1);
597       if (error)
598         return error;
599     }
600
601   *sw_if_index = vnet_create_sw_interface_no_callbacks (vnm, template);
602   error = vnet_sw_interface_set_flags_helper
603     (vnm, *sw_if_index, template->flags,
604      VNET_INTERFACE_SET_FLAGS_HELPER_IS_CREATE);
605
606   if (error)
607     {
608       /* undo the work done by vnet_create_sw_interface_no_callbacks() */
609       vnet_interface_main_t *im = &vnm->interface_main;
610       vnet_sw_interface_t *sw =
611         pool_elt_at_index (im->sw_interfaces, *sw_if_index);
612       pool_put (im->sw_interfaces, sw);
613     }
614
615   return error;
616 }
617
618 void
619 vnet_delete_sw_interface (vnet_main_t * vnm, u32 sw_if_index)
620 {
621   vnet_interface_main_t *im = &vnm->interface_main;
622   vnet_sw_interface_t *sw =
623     pool_elt_at_index (im->sw_interfaces, sw_if_index);
624
625   /* Bring down interface in case it is up. */
626   if (sw->flags != 0)
627     vnet_sw_interface_set_flags (vnm, sw_if_index, /* flags */ 0);
628
629   call_sw_interface_add_del_callbacks (vnm, sw_if_index, /* is_create */ 0);
630
631   pool_put (im->sw_interfaces, sw);
632 }
633
634 static void
635 setup_tx_node (vlib_main_t * vm,
636                u32 node_index, vnet_device_class_t * dev_class)
637 {
638   vlib_node_t *n = vlib_get_node (vm, node_index);
639
640   n->function = dev_class->tx_function;
641   n->format_trace = dev_class->format_tx_trace;
642
643   vlib_register_errors (vm, node_index,
644                         dev_class->tx_function_n_errors,
645                         dev_class->tx_function_error_strings);
646 }
647
648 static void
649 setup_output_node (vlib_main_t * vm,
650                    u32 node_index, vnet_hw_interface_class_t * hw_class)
651 {
652   vlib_node_t *n = vlib_get_node (vm, node_index);
653   n->format_buffer = hw_class->format_header;
654   n->unformat_buffer = hw_class->unformat_header;
655 }
656
657 /* Register an interface instance. */
658 u32
659 vnet_register_interface (vnet_main_t * vnm,
660                          u32 dev_class_index,
661                          u32 dev_instance,
662                          u32 hw_class_index, u32 hw_instance)
663 {
664   vnet_interface_main_t *im = &vnm->interface_main;
665   vnet_hw_interface_t *hw;
666   vnet_device_class_t *dev_class =
667     vnet_get_device_class (vnm, dev_class_index);
668   vnet_hw_interface_class_t *hw_class =
669     vnet_get_hw_interface_class (vnm, hw_class_index);
670   vlib_main_t *vm = vnm->vlib_main;
671   u32 hw_index;
672   char *tx_node_name, *output_node_name;
673
674   pool_get (im->hw_interfaces, hw);
675
676   hw_index = hw - im->hw_interfaces;
677   hw->hw_if_index = hw_index;
678
679   if (dev_class->format_device_name)
680     hw->name = format (0, "%U", dev_class->format_device_name, dev_instance);
681   else if (hw_class->format_interface_name)
682     hw->name = format (0, "%U", hw_class->format_interface_name,
683                        dev_instance);
684   else
685     hw->name = format (0, "%s%x", hw_class->name, dev_instance);
686
687   if (!im->hw_interface_by_name)
688     im->hw_interface_by_name = hash_create_vec ( /* size */ 0,
689                                                 sizeof (hw->name[0]),
690                                                 sizeof (uword));
691
692   hash_set_mem (im->hw_interface_by_name, hw->name, hw_index);
693
694   /* Make hardware interface point to software interface. */
695   {
696     vnet_sw_interface_t sw;
697
698     memset (&sw, 0, sizeof (sw));
699     sw.type = VNET_SW_INTERFACE_TYPE_HARDWARE;
700     sw.hw_if_index = hw_index;
701     hw->sw_if_index = vnet_create_sw_interface_no_callbacks (vnm, &sw);
702   }
703
704   hw->dev_class_index = dev_class_index;
705   hw->dev_instance = dev_instance;
706   hw->hw_class_index = hw_class_index;
707   hw->hw_instance = hw_instance;
708
709   hw->max_rate_bits_per_sec = 0;
710   hw->min_packet_bytes = 0;
711   hw->per_packet_overhead_bytes = 0;
712   hw->max_l3_packet_bytes[VLIB_RX] = ~0;
713   hw->max_l3_packet_bytes[VLIB_TX] = ~0;
714
715   tx_node_name = (char *) format (0, "%v-tx", hw->name);
716   output_node_name = (char *) format (0, "%v-output", hw->name);
717
718   /* If we have previously deleted interface nodes, re-use them. */
719   if (vec_len (im->deleted_hw_interface_nodes) > 0)
720     {
721       vnet_hw_interface_nodes_t *hn;
722       vnet_interface_output_runtime_t *rt;
723
724       hn = vec_end (im->deleted_hw_interface_nodes) - 1;
725
726       hw->tx_node_index = hn->tx_node_index;
727       hw->output_node_index = hn->output_node_index;
728
729       vlib_node_rename (vm, hw->tx_node_index, "%v", tx_node_name);
730       vlib_node_rename (vm, hw->output_node_index, "%v", output_node_name);
731
732       rt = vlib_node_get_runtime_data (vm, hw->output_node_index);
733       ASSERT (rt->is_deleted == 1);
734       rt->is_deleted = 0;
735       rt->hw_if_index = hw_index;
736       rt->sw_if_index = hw->sw_if_index;
737       rt->dev_instance = hw->dev_instance;
738
739       rt = vlib_node_get_runtime_data (vm, hw->tx_node_index);
740       rt->hw_if_index = hw_index;
741       rt->sw_if_index = hw->sw_if_index;
742       rt->dev_instance = hw->dev_instance;
743
744       vlib_worker_thread_node_runtime_update ();
745       _vec_len (im->deleted_hw_interface_nodes) -= 1;
746     }
747   else
748     {
749       vlib_node_registration_t r;
750       vnet_interface_output_runtime_t rt = {
751         .hw_if_index = hw_index,
752         .sw_if_index = hw->sw_if_index,
753         .dev_instance = hw->dev_instance,
754         .is_deleted = 0,
755       };
756
757       memset (&r, 0, sizeof (r));
758       r.type = VLIB_NODE_TYPE_INTERNAL;
759       r.runtime_data = &rt;
760       r.runtime_data_bytes = sizeof (rt);
761       r.scalar_size = 0;
762       r.vector_size = sizeof (u32);
763
764       r.flags = VLIB_NODE_FLAG_IS_OUTPUT;
765       r.name = tx_node_name;
766       r.function = dev_class->tx_function;
767
768       hw->tx_node_index = vlib_register_node (vm, &r);
769
770       vlib_node_add_named_next_with_slot (vm, hw->tx_node_index,
771                                           "error-drop",
772                                           VNET_INTERFACE_TX_NEXT_DROP);
773
774       r.flags = 0;
775       r.name = output_node_name;
776       r.function = dev_class->no_flatten_output_chains ?
777         vnet_interface_output_node_no_flatten_multiarch_select () :
778         vnet_interface_output_node_multiarch_select ();
779       r.format_trace = format_vnet_interface_output_trace;
780
781       {
782         static char *e[] = {
783           "interface is down",
784           "interface is deleted",
785         };
786
787         r.n_errors = ARRAY_LEN (e);
788         r.error_strings = e;
789       }
790       hw->output_node_index = vlib_register_node (vm, &r);
791
792 #define _(sym,str) vlib_node_add_named_next_with_slot (vm, \
793                      hw->output_node_index, str,           \
794                      VNET_INTERFACE_OUTPUT_NEXT_##sym);
795       foreach_intf_output_feat
796 #undef _
797         vlib_node_add_named_next_with_slot (vm, hw->output_node_index,
798                                             "error-drop",
799                                             VNET_INTERFACE_OUTPUT_NEXT_DROP);
800       vlib_node_add_next_with_slot (vm, hw->output_node_index,
801                                     hw->tx_node_index,
802                                     VNET_INTERFACE_OUTPUT_NEXT_TX);
803     }
804
805   setup_output_node (vm, hw->output_node_index, hw_class);
806   setup_tx_node (vm, hw->tx_node_index, dev_class);
807
808   /* Call all up/down callbacks with zero flags when interface is created. */
809   vnet_sw_interface_set_flags_helper (vnm, hw->sw_if_index, /* flags */ 0,
810                                       VNET_INTERFACE_SET_FLAGS_HELPER_IS_CREATE);
811   vnet_hw_interface_set_flags_helper (vnm, hw_index, /* flags */ 0,
812                                       VNET_INTERFACE_SET_FLAGS_HELPER_IS_CREATE);
813
814   return hw_index;
815 }
816
817 void
818 vnet_delete_hw_interface (vnet_main_t * vnm, u32 hw_if_index)
819 {
820   vnet_interface_main_t *im = &vnm->interface_main;
821   vnet_hw_interface_t *hw = vnet_get_hw_interface (vnm, hw_if_index);
822   vlib_main_t *vm = vnm->vlib_main;
823
824   /* If it is up, mark it down. */
825   if (hw->flags != 0)
826     vnet_hw_interface_set_flags (vnm, hw_if_index, /* flags */ 0);
827
828   /* Call delete callbacks. */
829   call_hw_interface_add_del_callbacks (vnm, hw_if_index, /* is_create */ 0);
830
831   /* Delete software interface corresponding to hardware interface. */
832   vnet_delete_sw_interface (vnm, hw->sw_if_index);
833
834   /* Delete any sub-interfaces. */
835   {
836     u32 id, sw_if_index;
837     /* *INDENT-OFF* */
838     hash_foreach (id, sw_if_index, hw->sub_interface_sw_if_index_by_id, ({
839       vnet_delete_sw_interface (vnm, sw_if_index);
840     }));
841     /* *INDENT-ON* */
842   }
843
844   {
845     vnet_hw_interface_nodes_t *dn;
846     vnet_interface_output_runtime_t *rt =
847       vlib_node_get_runtime_data (vm, hw->output_node_index);
848
849     /* Mark node runtime as deleted so output node (if called) will drop packets. */
850     rt->is_deleted = 1;
851
852     vlib_node_rename (vm, hw->output_node_index,
853                       "interface-%d-output-deleted", hw_if_index);
854     vlib_node_rename (vm, hw->tx_node_index, "interface-%d-tx-deleted",
855                       hw_if_index);
856     vec_add2 (im->deleted_hw_interface_nodes, dn, 1);
857     dn->tx_node_index = hw->tx_node_index;
858     dn->output_node_index = hw->output_node_index;
859   }
860
861   hash_unset_mem (im->hw_interface_by_name, hw->name);
862   vec_free (hw->name);
863
864   pool_put (im->hw_interfaces, hw);
865 }
866
867 static void
868 serialize_vnet_hw_interface_set_class (serialize_main_t * m, va_list * va)
869 {
870   u32 hw_if_index = va_arg (*va, u32);
871   char *hw_class_name = va_arg (*va, char *);
872   serialize_integer (m, hw_if_index, sizeof (hw_if_index));
873   serialize_cstring (m, hw_class_name);
874 }
875
876 static void
877 unserialize_vnet_hw_interface_set_class (serialize_main_t * m, va_list * va)
878 {
879   CLIB_UNUSED (mc_main_t * mc) = va_arg (*va, mc_main_t *);
880   vnet_main_t *vnm = vnet_get_main ();
881   u32 hw_if_index;
882   char *hw_class_name;
883   uword *p;
884   clib_error_t *error;
885
886   unserialize_integer (m, &hw_if_index, sizeof (hw_if_index));
887   unserialize_cstring (m, &hw_class_name);
888   p =
889     hash_get (vnm->interface_main.hw_interface_class_by_name, hw_class_name);
890   ASSERT (p != 0);
891   error = vnet_hw_interface_set_class_helper (vnm, hw_if_index, p[0],
892                                               /* redistribute */ 0);
893   if (error)
894     clib_error_report (error);
895 }
896
897 MC_SERIALIZE_MSG (vnet_hw_interface_set_class_msg, static) =
898 {
899 .name = "vnet_hw_interface_set_class",.serialize =
900     serialize_vnet_hw_interface_set_class,.unserialize =
901     unserialize_vnet_hw_interface_set_class,};
902
903 void
904 vnet_hw_interface_init_for_class (vnet_main_t * vnm, u32 hw_if_index,
905                                   u32 hw_class_index, u32 hw_instance)
906 {
907   vnet_hw_interface_t *hi = vnet_get_hw_interface (vnm, hw_if_index);
908   vnet_hw_interface_class_t *hc =
909     vnet_get_hw_interface_class (vnm, hw_class_index);
910
911   hi->hw_class_index = hw_class_index;
912   hi->hw_instance = hw_instance;
913   setup_output_node (vnm->vlib_main, hi->output_node_index, hc);
914 }
915
916 static clib_error_t *
917 vnet_hw_interface_set_class_helper (vnet_main_t * vnm, u32 hw_if_index,
918                                     u32 hw_class_index, u32 redistribute)
919 {
920   vnet_hw_interface_t *hi = vnet_get_hw_interface (vnm, hw_if_index);
921   vnet_sw_interface_t *si = vnet_get_sw_interface (vnm, hi->sw_if_index);
922   vnet_hw_interface_class_t *old_class =
923     vnet_get_hw_interface_class (vnm, hi->hw_class_index);
924   vnet_hw_interface_class_t *new_class =
925     vnet_get_hw_interface_class (vnm, hw_class_index);
926   vnet_device_class_t *dev_class =
927     vnet_get_device_class (vnm, hi->dev_class_index);
928   clib_error_t *error = 0;
929
930   /* New class equals old class?  Nothing to do. */
931   if (hi->hw_class_index == hw_class_index)
932     return 0;
933
934   /* No need (and incorrect since admin up flag may be set) to do error checking when
935      receiving unserialize message. */
936   if (redistribute)
937     {
938       if (si->flags & VNET_SW_INTERFACE_FLAG_ADMIN_UP)
939         return clib_error_return (0,
940                                   "%v must be admin down to change class from %s to %s",
941                                   hi->name, old_class->name, new_class->name);
942
943       /* Make sure interface supports given class. */
944       if ((new_class->is_valid_class_for_interface
945            && !new_class->is_valid_class_for_interface (vnm, hw_if_index,
946                                                         hw_class_index))
947           || (dev_class->is_valid_class_for_interface
948               && !dev_class->is_valid_class_for_interface (vnm, hw_if_index,
949                                                            hw_class_index)))
950         return clib_error_return (0,
951                                   "%v class cannot be changed from %s to %s",
952                                   hi->name, old_class->name, new_class->name);
953
954       if (vnm->vlib_main->mc_main)
955         {
956           mc_serialize (vnm->vlib_main->mc_main,
957                         &vnet_hw_interface_set_class_msg, hw_if_index,
958                         new_class->name);
959           return 0;
960         }
961     }
962
963   if (old_class->hw_class_change)
964     old_class->hw_class_change (vnm, hw_if_index, old_class->index,
965                                 new_class->index);
966
967   vnet_hw_interface_init_for_class (vnm, hw_if_index, new_class->index,
968                                     /* instance */ ~0);
969
970   if (new_class->hw_class_change)
971     new_class->hw_class_change (vnm, hw_if_index, old_class->index,
972                                 new_class->index);
973
974   if (dev_class->hw_class_change)
975     dev_class->hw_class_change (vnm, hw_if_index, new_class->index);
976
977   return error;
978 }
979
980 clib_error_t *
981 vnet_hw_interface_set_class (vnet_main_t * vnm, u32 hw_if_index,
982                              u32 hw_class_index)
983 {
984   return vnet_hw_interface_set_class_helper (vnm, hw_if_index, hw_class_index,
985                                              /* redistribute */ 1);
986 }
987
988 static int
989 vnet_hw_interface_rx_redirect_to_node_helper (vnet_main_t * vnm,
990                                               u32 hw_if_index,
991                                               u32 node_index,
992                                               u32 redistribute)
993 {
994   vnet_hw_interface_t *hi = vnet_get_hw_interface (vnm, hw_if_index);
995   vnet_device_class_t *dev_class = vnet_get_device_class
996     (vnm, hi->dev_class_index);
997
998   if (redistribute)
999     {
1000       /* $$$$ fixme someday maybe */
1001       ASSERT (vnm->vlib_main->mc_main == 0);
1002     }
1003   if (dev_class->rx_redirect_to_node)
1004     {
1005       dev_class->rx_redirect_to_node (vnm, hw_if_index, node_index);
1006       return 0;
1007     }
1008
1009   return VNET_API_ERROR_UNIMPLEMENTED;
1010 }
1011
1012 int
1013 vnet_hw_interface_rx_redirect_to_node (vnet_main_t * vnm, u32 hw_if_index,
1014                                        u32 node_index)
1015 {
1016   return vnet_hw_interface_rx_redirect_to_node_helper (vnm, hw_if_index,
1017                                                        node_index,
1018                                                        1 /* redistribute */ );
1019 }
1020
1021 word
1022 vnet_sw_interface_compare (vnet_main_t * vnm,
1023                            uword sw_if_index0, uword sw_if_index1)
1024 {
1025   vnet_sw_interface_t *sup0 = vnet_get_sup_sw_interface (vnm, sw_if_index0);
1026   vnet_sw_interface_t *sup1 = vnet_get_sup_sw_interface (vnm, sw_if_index1);
1027   vnet_hw_interface_t *h0 = vnet_get_hw_interface (vnm, sup0->hw_if_index);
1028   vnet_hw_interface_t *h1 = vnet_get_hw_interface (vnm, sup1->hw_if_index);
1029
1030   if (h0 != h1)
1031     return vec_cmp (h0->name, h1->name);
1032   return (word) h0->hw_instance - (word) h1->hw_instance;
1033 }
1034
1035 word
1036 vnet_hw_interface_compare (vnet_main_t * vnm,
1037                            uword hw_if_index0, uword hw_if_index1)
1038 {
1039   vnet_hw_interface_t *h0 = vnet_get_hw_interface (vnm, hw_if_index0);
1040   vnet_hw_interface_t *h1 = vnet_get_hw_interface (vnm, hw_if_index1);
1041
1042   if (h0 != h1)
1043     return vec_cmp (h0->name, h1->name);
1044   return (word) h0->hw_instance - (word) h1->hw_instance;
1045 }
1046
1047 clib_error_t *
1048 vnet_interface_init (vlib_main_t * vm)
1049 {
1050   vnet_main_t *vnm = vnet_get_main ();
1051   vnet_interface_main_t *im = &vnm->interface_main;
1052   vlib_buffer_t *b = 0;
1053   vnet_buffer_opaque_t *o = 0;
1054
1055   /*
1056    * Keep people from shooting themselves in the foot.
1057    */
1058   if (sizeof (b->opaque) != sizeof (vnet_buffer_opaque_t))
1059     {
1060 #define _(a) if (sizeof(o->a) > sizeof (o->unused))                     \
1061       clib_warning                                                      \
1062         ("FATAL: size of opaque union subtype %s is %d (max %d)",       \
1063          #a, sizeof(o->a), sizeof (o->unused));
1064       foreach_buffer_opaque_union_subtype;
1065 #undef _
1066
1067       return clib_error_return
1068         (0, "FATAL: size of vlib buffer opaque %d, size of vnet opaque %d",
1069          sizeof (b->opaque), sizeof (vnet_buffer_opaque_t));
1070     }
1071
1072   im->sw_if_counter_lock = clib_mem_alloc_aligned (CLIB_CACHE_LINE_BYTES,
1073                                                    CLIB_CACHE_LINE_BYTES);
1074   im->sw_if_counter_lock[0] = 1;        /* should be no need */
1075
1076   vec_validate (im->sw_if_counters, VNET_N_SIMPLE_INTERFACE_COUNTER - 1);
1077   im->sw_if_counters[VNET_INTERFACE_COUNTER_DROP].name = "drops";
1078   im->sw_if_counters[VNET_INTERFACE_COUNTER_PUNT].name = "punts";
1079   im->sw_if_counters[VNET_INTERFACE_COUNTER_IP4].name = "ip4";
1080   im->sw_if_counters[VNET_INTERFACE_COUNTER_IP6].name = "ip6";
1081   im->sw_if_counters[VNET_INTERFACE_COUNTER_RX_NO_BUF].name = "rx-no-buf";
1082   im->sw_if_counters[VNET_INTERFACE_COUNTER_RX_MISS].name = "rx-miss";
1083   im->sw_if_counters[VNET_INTERFACE_COUNTER_RX_ERROR].name = "rx-error";
1084   im->sw_if_counters[VNET_INTERFACE_COUNTER_TX_ERROR].name = "tx-error";
1085
1086   vec_validate (im->combined_sw_if_counters,
1087                 VNET_N_COMBINED_INTERFACE_COUNTER - 1);
1088   im->combined_sw_if_counters[VNET_INTERFACE_COUNTER_RX].name = "rx";
1089   im->combined_sw_if_counters[VNET_INTERFACE_COUNTER_TX].name = "tx";
1090
1091   im->sw_if_counter_lock[0] = 0;
1092
1093   im->device_class_by_name = hash_create_string ( /* size */ 0,
1094                                                  sizeof (uword));
1095   {
1096     vnet_device_class_t *c;
1097
1098     c = vnm->device_class_registrations;
1099
1100     while (c)
1101       {
1102         c->index = vec_len (im->device_classes);
1103         hash_set_mem (im->device_class_by_name, c->name, c->index);
1104         vec_add1 (im->device_classes, c[0]);
1105         c = c->next_class_registration;
1106       }
1107   }
1108
1109   im->hw_interface_class_by_name = hash_create_string ( /* size */ 0,
1110                                                        sizeof (uword));
1111
1112   im->sw_if_index_by_sup_and_sub = hash_create_mem (0, sizeof (u64),
1113                                                     sizeof (uword));
1114   {
1115     vnet_hw_interface_class_t *c;
1116
1117     c = vnm->hw_interface_class_registrations;
1118
1119     while (c)
1120       {
1121         c->index = vec_len (im->hw_interface_classes);
1122         hash_set_mem (im->hw_interface_class_by_name, c->name, c->index);
1123         vec_add1 (im->hw_interface_classes, c[0]);
1124         c = c->next_class_registration;
1125       }
1126   }
1127
1128   {
1129     clib_error_t *error;
1130
1131     if ((error = vlib_call_init_function (vm, vnet_interface_cli_init)))
1132       return error;
1133
1134     return error;
1135   }
1136 }
1137
1138 VLIB_INIT_FUNCTION (vnet_interface_init);
1139
1140 /* Kludge to renumber interface names [only!] */
1141 int
1142 vnet_interface_name_renumber (u32 sw_if_index, u32 new_show_dev_instance)
1143 {
1144   int rv;
1145   vnet_main_t *vnm = vnet_get_main ();
1146   vnet_interface_main_t *im = &vnm->interface_main;
1147   vnet_hw_interface_t *hi = vnet_get_sup_hw_interface (vnm, sw_if_index);
1148
1149   vnet_device_class_t *dev_class = vnet_get_device_class
1150     (vnm, hi->dev_class_index);
1151
1152   if (dev_class->name_renumber == 0 || dev_class->format_device_name == 0)
1153     return VNET_API_ERROR_UNIMPLEMENTED;
1154
1155   rv = dev_class->name_renumber (hi, new_show_dev_instance);
1156
1157   if (rv)
1158     return rv;
1159
1160   hash_unset_mem (im->hw_interface_by_name, hi->name);
1161   vec_free (hi->name);
1162   /* Use the mapping we set up to call it Ishmael */
1163   hi->name = format (0, "%U", dev_class->format_device_name,
1164                      hi->dev_instance);
1165
1166   hash_set_mem (im->hw_interface_by_name, hi->name, hi->hw_if_index);
1167   return rv;
1168 }
1169
1170 int
1171 vnet_interface_add_del_feature (vnet_main_t * vnm,
1172                                 vlib_main_t * vm,
1173                                 u32 sw_if_index,
1174                                 intf_output_feat_t feature, int is_add)
1175 {
1176   vnet_sw_interface_t *sw;
1177
1178   sw = vnet_get_sw_interface (vnm, sw_if_index);
1179
1180   if (is_add)
1181     {
1182
1183       sw->output_feature_bitmap |= (1 << feature);
1184       sw->output_feature_bitmap |= (1 << INTF_OUTPUT_FEAT_DONE);
1185
1186     }
1187   else
1188     {                           /* delete */
1189
1190       sw->output_feature_bitmap &= ~(1 << feature);
1191       if (sw->output_feature_bitmap == (1 << INTF_OUTPUT_FEAT_DONE))
1192         sw->output_feature_bitmap = 0;
1193
1194     }
1195   return 0;
1196 }
1197
1198 clib_error_t *
1199 vnet_rename_interface (vnet_main_t * vnm, u32 hw_if_index, char *new_name)
1200 {
1201   vnet_interface_main_t *im = &vnm->interface_main;
1202   vlib_main_t *vm = vnm->vlib_main;
1203   vnet_hw_interface_t *hw;
1204   u8 *old_name;
1205   clib_error_t *error = 0;
1206
1207   hw = vnet_get_hw_interface (vnm, hw_if_index);
1208   if (!hw)
1209     {
1210       return clib_error_return (0,
1211                                 "unable to find hw interface for index %u",
1212                                 hw_if_index);
1213     }
1214
1215   old_name = hw->name;
1216
1217   /* set new hw->name */
1218   hw->name = format (0, "%s", new_name);
1219
1220   /* remove the old name to hw_if_index mapping and install the new one */
1221   hash_unset_mem (im->hw_interface_by_name, old_name);
1222   hash_set_mem (im->hw_interface_by_name, hw->name, hw_if_index);
1223
1224   /* rename tx/output nodes */
1225   vlib_node_rename (vm, hw->tx_node_index, "%v-tx", hw->name);
1226   vlib_node_rename (vm, hw->output_node_index, "%v-output", hw->name);
1227
1228   /* free the old name vector */
1229   vec_free (old_name);
1230
1231   return error;
1232 }
1233
1234 static clib_error_t *
1235 vnet_hw_interface_change_mac_address_helper (vnet_main_t * vnm,
1236                                              u32 hw_if_index, u64 mac_address)
1237 {
1238   clib_error_t *error = 0;
1239   vnet_hw_interface_t *hi = vnet_get_hw_interface (vnm, hw_if_index);
1240
1241   if (hi->hw_address)
1242     {
1243       vnet_device_class_t *dev_class =
1244         vnet_get_device_class (vnm, hi->dev_class_index);
1245       if (dev_class->mac_addr_change_function)
1246         {
1247           error =
1248             dev_class->mac_addr_change_function (vnet_get_hw_interface
1249                                                  (vnm, hw_if_index),
1250                                                  (char *) &mac_address);
1251         }
1252       if (!error)
1253         {
1254           ethernet_main_t *em = &ethernet_main;
1255           ethernet_interface_t *ei =
1256             pool_elt_at_index (em->interfaces, hi->hw_instance);
1257
1258           clib_memcpy (hi->hw_address, (u8 *) & mac_address,
1259                        sizeof (hi->hw_address));
1260           clib_memcpy (ei->address, (u8 *) & mac_address,
1261                        sizeof (ei->address));
1262           ethernet_arp_change_mac (vnm, hw_if_index);
1263           ethernet_ndp_change_mac (vnm->vlib_main, hw_if_index);
1264         }
1265       else
1266         {
1267           error =
1268             clib_error_return (0,
1269                                "MAC Address Change is not supported on this interface");
1270         }
1271     }
1272   else
1273     {
1274       error =
1275         clib_error_return (0,
1276                            "mac address change is not supported for interface index %u",
1277                            hw_if_index);
1278     }
1279   return error;
1280 }
1281
1282 clib_error_t *
1283 vnet_hw_interface_change_mac_address (vnet_main_t * vnm, u32 hw_if_index,
1284                                       u64 mac_address)
1285 {
1286   return vnet_hw_interface_change_mac_address_helper
1287     (vnm, hw_if_index, mac_address);
1288 }
1289
1290 /*
1291  * fd.io coding-style-patch-verification: ON
1292  *
1293  * Local Variables:
1294  * eval: (c-set-style "gnu")
1295  * End:
1296  */