VPP-363: add ability to change mac address of the interface
[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   vlib_register_errors (vm, node_index,
643                         dev_class->tx_function_n_errors,
644                         dev_class->tx_function_error_strings);
645 }
646
647 static void
648 setup_output_node (vlib_main_t * vm,
649                    u32 node_index, vnet_hw_interface_class_t * hw_class)
650 {
651   vlib_node_t *n = vlib_get_node (vm, node_index);
652   n->format_buffer = hw_class->format_header;
653   n->unformat_buffer = hw_class->unformat_header;
654 }
655
656 /* Register an interface instance. */
657 u32
658 vnet_register_interface (vnet_main_t * vnm,
659                          u32 dev_class_index,
660                          u32 dev_instance,
661                          u32 hw_class_index, u32 hw_instance)
662 {
663   vnet_interface_main_t *im = &vnm->interface_main;
664   vnet_hw_interface_t *hw;
665   vnet_device_class_t *dev_class =
666     vnet_get_device_class (vnm, dev_class_index);
667   vnet_hw_interface_class_t *hw_class =
668     vnet_get_hw_interface_class (vnm, hw_class_index);
669   vlib_main_t *vm = vnm->vlib_main;
670   u32 hw_index;
671   char *tx_node_name, *output_node_name;
672
673   pool_get (im->hw_interfaces, hw);
674
675   hw_index = hw - im->hw_interfaces;
676   hw->hw_if_index = hw_index;
677
678   if (dev_class->format_device_name)
679     hw->name = format (0, "%U", dev_class->format_device_name, dev_instance);
680   else if (hw_class->format_interface_name)
681     hw->name = format (0, "%U", hw_class->format_interface_name,
682                        dev_instance);
683   else
684     hw->name = format (0, "%s%x", hw_class->name, dev_instance);
685
686   if (!im->hw_interface_by_name)
687     im->hw_interface_by_name = hash_create_vec ( /* size */ 0,
688                                                 sizeof (hw->name[0]),
689                                                 sizeof (uword));
690
691   hash_set_mem (im->hw_interface_by_name, hw->name, hw_index);
692
693   /* Make hardware interface point to software interface. */
694   {
695     vnet_sw_interface_t sw;
696
697     memset (&sw, 0, sizeof (sw));
698     sw.type = VNET_SW_INTERFACE_TYPE_HARDWARE;
699     sw.hw_if_index = hw_index;
700     hw->sw_if_index = vnet_create_sw_interface_no_callbacks (vnm, &sw);
701   }
702
703   hw->dev_class_index = dev_class_index;
704   hw->dev_instance = dev_instance;
705   hw->hw_class_index = hw_class_index;
706   hw->hw_instance = hw_instance;
707
708   hw->max_rate_bits_per_sec = 0;
709   hw->min_packet_bytes = 0;
710   hw->per_packet_overhead_bytes = 0;
711   hw->max_l3_packet_bytes[VLIB_RX] = ~0;
712   hw->max_l3_packet_bytes[VLIB_TX] = ~0;
713
714   tx_node_name = (char *) format (0, "%v-tx", hw->name);
715   output_node_name = (char *) format (0, "%v-output", hw->name);
716
717   /* If we have previously deleted interface nodes, re-use them. */
718   if (vec_len (im->deleted_hw_interface_nodes) > 0)
719     {
720       vnet_hw_interface_nodes_t *hn;
721       vnet_interface_output_runtime_t *rt;
722
723       hn = vec_end (im->deleted_hw_interface_nodes) - 1;
724
725       hw->tx_node_index = hn->tx_node_index;
726       hw->output_node_index = hn->output_node_index;
727
728       vlib_node_rename (vm, hw->tx_node_index, "%v", tx_node_name);
729       vlib_node_rename (vm, hw->output_node_index, "%v", output_node_name);
730
731       rt = vlib_node_get_runtime_data (vm, hw->output_node_index);
732       ASSERT (rt->is_deleted == 1);
733       rt->is_deleted = 0;
734       rt->hw_if_index = hw_index;
735       rt->sw_if_index = hw->sw_if_index;
736       rt->dev_instance = hw->dev_instance;
737
738       rt = vlib_node_get_runtime_data (vm, hw->tx_node_index);
739       rt->hw_if_index = hw_index;
740       rt->sw_if_index = hw->sw_if_index;
741       rt->dev_instance = hw->dev_instance;
742
743       vlib_worker_thread_node_runtime_update ();
744       _vec_len (im->deleted_hw_interface_nodes) -= 1;
745     }
746   else
747     {
748       vlib_node_registration_t r;
749       vnet_interface_output_runtime_t rt = {
750         .hw_if_index = hw_index,
751         .sw_if_index = hw->sw_if_index,
752         .dev_instance = hw->dev_instance,
753         .is_deleted = 0,
754       };
755
756       memset (&r, 0, sizeof (r));
757       r.type = VLIB_NODE_TYPE_INTERNAL;
758       r.runtime_data = &rt;
759       r.runtime_data_bytes = sizeof (rt);
760       r.scalar_size = 0;
761       r.vector_size = sizeof (u32);
762
763       r.flags = VLIB_NODE_FLAG_IS_OUTPUT;
764       r.name = tx_node_name;
765       r.function = dev_class->tx_function;
766
767       hw->tx_node_index = vlib_register_node (vm, &r);
768
769       vlib_node_add_named_next_with_slot (vm, hw->tx_node_index,
770                                           "error-drop",
771                                           VNET_INTERFACE_TX_NEXT_DROP);
772
773       r.flags = 0;
774       r.name = output_node_name;
775       r.function = dev_class->no_flatten_output_chains ?
776         vnet_interface_output_node_no_flatten_multiarch_select () :
777         vnet_interface_output_node_multiarch_select ();
778       r.format_trace = format_vnet_interface_output_trace;
779
780       {
781         static char *e[] = {
782           "interface is down",
783           "interface is deleted",
784         };
785
786         r.n_errors = ARRAY_LEN (e);
787         r.error_strings = e;
788       }
789       hw->output_node_index = vlib_register_node (vm, &r);
790
791 #define _(sym,str) vlib_node_add_named_next_with_slot (vm, \
792                      hw->output_node_index, str,           \
793                      VNET_INTERFACE_OUTPUT_NEXT_##sym);
794       foreach_intf_output_feat
795 #undef _
796         vlib_node_add_named_next_with_slot (vm, hw->output_node_index,
797                                             "error-drop",
798                                             VNET_INTERFACE_OUTPUT_NEXT_DROP);
799       vlib_node_add_next_with_slot (vm, hw->output_node_index,
800                                     hw->tx_node_index,
801                                     VNET_INTERFACE_OUTPUT_NEXT_TX);
802     }
803
804   setup_output_node (vm, hw->output_node_index, hw_class);
805   setup_tx_node (vm, hw->tx_node_index, dev_class);
806
807   /* Call all up/down callbacks with zero flags when interface is created. */
808   vnet_sw_interface_set_flags_helper (vnm, hw->sw_if_index, /* flags */ 0,
809                                       VNET_INTERFACE_SET_FLAGS_HELPER_IS_CREATE);
810   vnet_hw_interface_set_flags_helper (vnm, hw_index, /* flags */ 0,
811                                       VNET_INTERFACE_SET_FLAGS_HELPER_IS_CREATE);
812
813   return hw_index;
814 }
815
816 void
817 vnet_delete_hw_interface (vnet_main_t * vnm, u32 hw_if_index)
818 {
819   vnet_interface_main_t *im = &vnm->interface_main;
820   vnet_hw_interface_t *hw = vnet_get_hw_interface (vnm, hw_if_index);
821   vlib_main_t *vm = vnm->vlib_main;
822
823   /* If it is up, mark it down. */
824   if (hw->flags != 0)
825     vnet_hw_interface_set_flags (vnm, hw_if_index, /* flags */ 0);
826
827   /* Call delete callbacks. */
828   call_hw_interface_add_del_callbacks (vnm, hw_if_index, /* is_create */ 0);
829
830   /* Delete software interface corresponding to hardware interface. */
831   vnet_delete_sw_interface (vnm, hw->sw_if_index);
832
833   /* Delete any sub-interfaces. */
834   {
835     u32 id, sw_if_index;
836     /* *INDENT-OFF* */
837     hash_foreach (id, sw_if_index, hw->sub_interface_sw_if_index_by_id, ({
838       vnet_delete_sw_interface (vnm, sw_if_index);
839     }));
840     /* *INDENT-ON* */
841   }
842
843   {
844     vnet_hw_interface_nodes_t *dn;
845     vnet_interface_output_runtime_t *rt =
846       vlib_node_get_runtime_data (vm, hw->output_node_index);
847
848     /* Mark node runtime as deleted so output node (if called) will drop packets. */
849     rt->is_deleted = 1;
850
851     vlib_node_rename (vm, hw->output_node_index,
852                       "interface-%d-output-deleted", hw_if_index);
853     vlib_node_rename (vm, hw->tx_node_index, "interface-%d-tx-deleted",
854                       hw_if_index);
855     vec_add2 (im->deleted_hw_interface_nodes, dn, 1);
856     dn->tx_node_index = hw->tx_node_index;
857     dn->output_node_index = hw->output_node_index;
858   }
859
860   hash_unset_mem (im->hw_interface_by_name, hw->name);
861   vec_free (hw->name);
862
863   pool_put (im->hw_interfaces, hw);
864 }
865
866 static void
867 serialize_vnet_hw_interface_set_class (serialize_main_t * m, va_list * va)
868 {
869   u32 hw_if_index = va_arg (*va, u32);
870   char *hw_class_name = va_arg (*va, char *);
871   serialize_integer (m, hw_if_index, sizeof (hw_if_index));
872   serialize_cstring (m, hw_class_name);
873 }
874
875 static void
876 unserialize_vnet_hw_interface_set_class (serialize_main_t * m, va_list * va)
877 {
878   CLIB_UNUSED (mc_main_t * mc) = va_arg (*va, mc_main_t *);
879   vnet_main_t *vnm = vnet_get_main ();
880   u32 hw_if_index;
881   char *hw_class_name;
882   uword *p;
883   clib_error_t *error;
884
885   unserialize_integer (m, &hw_if_index, sizeof (hw_if_index));
886   unserialize_cstring (m, &hw_class_name);
887   p =
888     hash_get (vnm->interface_main.hw_interface_class_by_name, hw_class_name);
889   ASSERT (p != 0);
890   error = vnet_hw_interface_set_class_helper (vnm, hw_if_index, p[0],
891                                               /* redistribute */ 0);
892   if (error)
893     clib_error_report (error);
894 }
895
896 MC_SERIALIZE_MSG (vnet_hw_interface_set_class_msg, static) =
897 {
898 .name = "vnet_hw_interface_set_class",.serialize =
899     serialize_vnet_hw_interface_set_class,.unserialize =
900     unserialize_vnet_hw_interface_set_class,};
901
902 void
903 vnet_hw_interface_init_for_class (vnet_main_t * vnm, u32 hw_if_index,
904                                   u32 hw_class_index, u32 hw_instance)
905 {
906   vnet_hw_interface_t *hi = vnet_get_hw_interface (vnm, hw_if_index);
907   vnet_hw_interface_class_t *hc =
908     vnet_get_hw_interface_class (vnm, hw_class_index);
909
910   hi->hw_class_index = hw_class_index;
911   hi->hw_instance = hw_instance;
912   setup_output_node (vnm->vlib_main, hi->output_node_index, hc);
913 }
914
915 static clib_error_t *
916 vnet_hw_interface_set_class_helper (vnet_main_t * vnm, u32 hw_if_index,
917                                     u32 hw_class_index, u32 redistribute)
918 {
919   vnet_hw_interface_t *hi = vnet_get_hw_interface (vnm, hw_if_index);
920   vnet_sw_interface_t *si = vnet_get_sw_interface (vnm, hi->sw_if_index);
921   vnet_hw_interface_class_t *old_class =
922     vnet_get_hw_interface_class (vnm, hi->hw_class_index);
923   vnet_hw_interface_class_t *new_class =
924     vnet_get_hw_interface_class (vnm, hw_class_index);
925   vnet_device_class_t *dev_class =
926     vnet_get_device_class (vnm, hi->dev_class_index);
927   clib_error_t *error = 0;
928
929   /* New class equals old class?  Nothing to do. */
930   if (hi->hw_class_index == hw_class_index)
931     return 0;
932
933   /* No need (and incorrect since admin up flag may be set) to do error checking when
934      receiving unserialize message. */
935   if (redistribute)
936     {
937       if (si->flags & VNET_SW_INTERFACE_FLAG_ADMIN_UP)
938         return clib_error_return (0,
939                                   "%v must be admin down to change class from %s to %s",
940                                   hi->name, old_class->name, new_class->name);
941
942       /* Make sure interface supports given class. */
943       if ((new_class->is_valid_class_for_interface
944            && !new_class->is_valid_class_for_interface (vnm, hw_if_index,
945                                                         hw_class_index))
946           || (dev_class->is_valid_class_for_interface
947               && !dev_class->is_valid_class_for_interface (vnm, hw_if_index,
948                                                            hw_class_index)))
949         return clib_error_return (0,
950                                   "%v class cannot be changed from %s to %s",
951                                   hi->name, old_class->name, new_class->name);
952
953       if (vnm->vlib_main->mc_main)
954         {
955           mc_serialize (vnm->vlib_main->mc_main,
956                         &vnet_hw_interface_set_class_msg, hw_if_index,
957                         new_class->name);
958           return 0;
959         }
960     }
961
962   if (old_class->hw_class_change)
963     old_class->hw_class_change (vnm, hw_if_index, old_class->index,
964                                 new_class->index);
965
966   vnet_hw_interface_init_for_class (vnm, hw_if_index, new_class->index,
967                                     /* instance */ ~0);
968
969   if (new_class->hw_class_change)
970     new_class->hw_class_change (vnm, hw_if_index, old_class->index,
971                                 new_class->index);
972
973   if (dev_class->hw_class_change)
974     dev_class->hw_class_change (vnm, hw_if_index, new_class->index);
975
976   return error;
977 }
978
979 clib_error_t *
980 vnet_hw_interface_set_class (vnet_main_t * vnm, u32 hw_if_index,
981                              u32 hw_class_index)
982 {
983   return vnet_hw_interface_set_class_helper (vnm, hw_if_index, hw_class_index,
984                                              /* redistribute */ 1);
985 }
986
987 static int
988 vnet_hw_interface_rx_redirect_to_node_helper (vnet_main_t * vnm,
989                                               u32 hw_if_index,
990                                               u32 node_index,
991                                               u32 redistribute)
992 {
993   vnet_hw_interface_t *hi = vnet_get_hw_interface (vnm, hw_if_index);
994   vnet_device_class_t *dev_class = vnet_get_device_class
995     (vnm, hi->dev_class_index);
996
997   if (redistribute)
998     {
999       /* $$$$ fixme someday maybe */
1000       ASSERT (vnm->vlib_main->mc_main == 0);
1001     }
1002   if (dev_class->rx_redirect_to_node)
1003     {
1004       dev_class->rx_redirect_to_node (vnm, hw_if_index, node_index);
1005       return 0;
1006     }
1007
1008   return VNET_API_ERROR_UNIMPLEMENTED;
1009 }
1010
1011 int
1012 vnet_hw_interface_rx_redirect_to_node (vnet_main_t * vnm, u32 hw_if_index,
1013                                        u32 node_index)
1014 {
1015   return vnet_hw_interface_rx_redirect_to_node_helper (vnm, hw_if_index,
1016                                                        node_index,
1017                                                        1 /* redistribute */ );
1018 }
1019
1020 word
1021 vnet_sw_interface_compare (vnet_main_t * vnm,
1022                            uword sw_if_index0, uword sw_if_index1)
1023 {
1024   vnet_sw_interface_t *sup0 = vnet_get_sup_sw_interface (vnm, sw_if_index0);
1025   vnet_sw_interface_t *sup1 = vnet_get_sup_sw_interface (vnm, sw_if_index1);
1026   vnet_hw_interface_t *h0 = vnet_get_hw_interface (vnm, sup0->hw_if_index);
1027   vnet_hw_interface_t *h1 = vnet_get_hw_interface (vnm, sup1->hw_if_index);
1028
1029   if (h0 != h1)
1030     return vec_cmp (h0->name, h1->name);
1031   return (word) h0->hw_instance - (word) h1->hw_instance;
1032 }
1033
1034 word
1035 vnet_hw_interface_compare (vnet_main_t * vnm,
1036                            uword hw_if_index0, uword hw_if_index1)
1037 {
1038   vnet_hw_interface_t *h0 = vnet_get_hw_interface (vnm, hw_if_index0);
1039   vnet_hw_interface_t *h1 = vnet_get_hw_interface (vnm, hw_if_index1);
1040
1041   if (h0 != h1)
1042     return vec_cmp (h0->name, h1->name);
1043   return (word) h0->hw_instance - (word) h1->hw_instance;
1044 }
1045
1046 clib_error_t *
1047 vnet_interface_init (vlib_main_t * vm)
1048 {
1049   vnet_main_t *vnm = vnet_get_main ();
1050   vnet_interface_main_t *im = &vnm->interface_main;
1051   vlib_buffer_t *b = 0;
1052   vnet_buffer_opaque_t *o = 0;
1053
1054   /*
1055    * Keep people from shooting themselves in the foot.
1056    */
1057   if (sizeof (b->opaque) != sizeof (vnet_buffer_opaque_t))
1058     {
1059 #define _(a) if (sizeof(o->a) > sizeof (o->unused))                     \
1060       clib_warning                                                      \
1061         ("FATAL: size of opaque union subtype %s is %d (max %d)",       \
1062          #a, sizeof(o->a), sizeof (o->unused));
1063       foreach_buffer_opaque_union_subtype;
1064 #undef _
1065
1066       return clib_error_return
1067         (0, "FATAL: size of vlib buffer opaque %d, size of vnet opaque %d",
1068          sizeof (b->opaque), sizeof (vnet_buffer_opaque_t));
1069     }
1070
1071   im->sw_if_counter_lock = clib_mem_alloc_aligned (CLIB_CACHE_LINE_BYTES,
1072                                                    CLIB_CACHE_LINE_BYTES);
1073   im->sw_if_counter_lock[0] = 1;        /* should be no need */
1074
1075   vec_validate (im->sw_if_counters, VNET_N_SIMPLE_INTERFACE_COUNTER - 1);
1076   im->sw_if_counters[VNET_INTERFACE_COUNTER_DROP].name = "drops";
1077   im->sw_if_counters[VNET_INTERFACE_COUNTER_PUNT].name = "punts";
1078   im->sw_if_counters[VNET_INTERFACE_COUNTER_IP4].name = "ip4";
1079   im->sw_if_counters[VNET_INTERFACE_COUNTER_IP6].name = "ip6";
1080   im->sw_if_counters[VNET_INTERFACE_COUNTER_RX_NO_BUF].name = "rx-no-buf";
1081   im->sw_if_counters[VNET_INTERFACE_COUNTER_RX_MISS].name = "rx-miss";
1082   im->sw_if_counters[VNET_INTERFACE_COUNTER_RX_ERROR].name = "rx-error";
1083   im->sw_if_counters[VNET_INTERFACE_COUNTER_TX_ERROR].name = "tx-error";
1084
1085   vec_validate (im->combined_sw_if_counters,
1086                 VNET_N_COMBINED_INTERFACE_COUNTER - 1);
1087   im->combined_sw_if_counters[VNET_INTERFACE_COUNTER_RX].name = "rx";
1088   im->combined_sw_if_counters[VNET_INTERFACE_COUNTER_TX].name = "tx";
1089
1090   im->sw_if_counter_lock[0] = 0;
1091
1092   im->device_class_by_name = hash_create_string ( /* size */ 0,
1093                                                  sizeof (uword));
1094   {
1095     vnet_device_class_t *c;
1096
1097     c = vnm->device_class_registrations;
1098
1099     while (c)
1100       {
1101         c->index = vec_len (im->device_classes);
1102         hash_set_mem (im->device_class_by_name, c->name, c->index);
1103         vec_add1 (im->device_classes, c[0]);
1104         c = c->next_class_registration;
1105       }
1106   }
1107
1108   im->hw_interface_class_by_name = hash_create_string ( /* size */ 0,
1109                                                        sizeof (uword));
1110
1111   im->sw_if_index_by_sup_and_sub = hash_create_mem (0, sizeof (u64),
1112                                                     sizeof (uword));
1113   {
1114     vnet_hw_interface_class_t *c;
1115
1116     c = vnm->hw_interface_class_registrations;
1117
1118     while (c)
1119       {
1120         c->index = vec_len (im->hw_interface_classes);
1121         hash_set_mem (im->hw_interface_class_by_name, c->name, c->index);
1122         vec_add1 (im->hw_interface_classes, c[0]);
1123         c = c->next_class_registration;
1124       }
1125   }
1126
1127   {
1128     clib_error_t *error;
1129
1130     if ((error = vlib_call_init_function (vm, vnet_interface_cli_init)))
1131       return error;
1132
1133     return error;
1134   }
1135 }
1136
1137 VLIB_INIT_FUNCTION (vnet_interface_init);
1138
1139 /* Kludge to renumber interface names [only!] */
1140 int
1141 vnet_interface_name_renumber (u32 sw_if_index, u32 new_show_dev_instance)
1142 {
1143   int rv;
1144   vnet_main_t *vnm = vnet_get_main ();
1145   vnet_interface_main_t *im = &vnm->interface_main;
1146   vnet_hw_interface_t *hi = vnet_get_sup_hw_interface (vnm, sw_if_index);
1147
1148   vnet_device_class_t *dev_class = vnet_get_device_class
1149     (vnm, hi->dev_class_index);
1150
1151   if (dev_class->name_renumber == 0 || dev_class->format_device_name == 0)
1152     return VNET_API_ERROR_UNIMPLEMENTED;
1153
1154   rv = dev_class->name_renumber (hi, new_show_dev_instance);
1155
1156   if (rv)
1157     return rv;
1158
1159   hash_unset_mem (im->hw_interface_by_name, hi->name);
1160   vec_free (hi->name);
1161   /* Use the mapping we set up to call it Ishmael */
1162   hi->name = format (0, "%U", dev_class->format_device_name,
1163                      hi->dev_instance);
1164
1165   hash_set_mem (im->hw_interface_by_name, hi->name, hi->hw_if_index);
1166   return rv;
1167 }
1168
1169 int
1170 vnet_interface_add_del_feature (vnet_main_t * vnm,
1171                                 vlib_main_t * vm,
1172                                 u32 sw_if_index,
1173                                 intf_output_feat_t feature, int is_add)
1174 {
1175   vnet_sw_interface_t *sw;
1176
1177   sw = vnet_get_sw_interface (vnm, sw_if_index);
1178
1179   if (is_add)
1180     {
1181
1182       sw->output_feature_bitmap |= (1 << feature);
1183       sw->output_feature_bitmap |= (1 << INTF_OUTPUT_FEAT_DONE);
1184
1185     }
1186   else
1187     {                           /* delete */
1188
1189       sw->output_feature_bitmap &= ~(1 << feature);
1190       if (sw->output_feature_bitmap == (1 << INTF_OUTPUT_FEAT_DONE))
1191         sw->output_feature_bitmap = 0;
1192
1193     }
1194   return 0;
1195 }
1196
1197 clib_error_t *
1198 vnet_rename_interface (vnet_main_t * vnm, u32 hw_if_index, char *new_name)
1199 {
1200   vnet_interface_main_t *im = &vnm->interface_main;
1201   vlib_main_t *vm = vnm->vlib_main;
1202   vnet_hw_interface_t *hw;
1203   u8 *old_name;
1204   clib_error_t *error = 0;
1205
1206   hw = vnet_get_hw_interface (vnm, hw_if_index);
1207   if (!hw)
1208     {
1209       return clib_error_return (0,
1210                                 "unable to find hw interface for index %u",
1211                                 hw_if_index);
1212     }
1213
1214   old_name = hw->name;
1215
1216   /* set new hw->name */
1217   hw->name = format (0, "%s", new_name);
1218
1219   /* remove the old name to hw_if_index mapping and install the new one */
1220   hash_unset_mem (im->hw_interface_by_name, old_name);
1221   hash_set_mem (im->hw_interface_by_name, hw->name, hw_if_index);
1222
1223   /* rename tx/output nodes */
1224   vlib_node_rename (vm, hw->tx_node_index, "%v-tx", hw->name);
1225   vlib_node_rename (vm, hw->output_node_index, "%v-output", hw->name);
1226
1227   /* free the old name vector */
1228   vec_free (old_name);
1229
1230   return error;
1231 }
1232
1233 static clib_error_t *
1234 vnet_hw_interface_change_mac_address_helper (vnet_main_t * vnm,
1235                                              u32 hw_if_index, u64 mac_address)
1236 {
1237   clib_error_t *error = 0;
1238   vnet_hw_interface_t *hi = vnet_get_hw_interface (vnm, hw_if_index);
1239
1240   if (hi->hw_address)
1241     {
1242       vnet_device_class_t *dev_class =
1243         vnet_get_device_class (vnm, hi->dev_class_index);
1244       if (dev_class->mac_addr_change_function)
1245         {
1246           error =
1247             dev_class->mac_addr_change_function (vnet_get_hw_interface
1248                                                  (vnm, hw_if_index),
1249                                                  (char *) &mac_address);
1250         }
1251       if (!error)
1252         {
1253           ethernet_main_t *em = &ethernet_main;
1254           ethernet_interface_t *ei =
1255             pool_elt_at_index (em->interfaces, hi->hw_instance);
1256
1257           clib_memcpy (hi->hw_address, (u8 *) & mac_address,
1258                        sizeof (hi->hw_address));
1259           clib_memcpy (ei->address, (u8 *) & mac_address,
1260                        sizeof (ei->address));
1261           ethernet_arp_change_mac (vnm, hw_if_index);
1262           ethernet_ndp_change_mac (vnm->vlib_main, hw_if_index);
1263         }
1264       else
1265         {
1266           error =
1267             clib_error_return (0,
1268                                "MAC Address Change is not supported on this interface");
1269         }
1270     }
1271   else
1272     {
1273       error =
1274         clib_error_return (0,
1275                            "mac address change is not supported for interface index %u",
1276                            hw_if_index);
1277     }
1278   return error;
1279 }
1280
1281 clib_error_t *
1282 vnet_hw_interface_change_mac_address (vnet_main_t * vnm, u32 hw_if_index,
1283                                       u64 mac_address)
1284 {
1285   return vnet_hw_interface_change_mac_address_helper
1286     (vnm, hw_if_index, mac_address);
1287 }
1288
1289 /*
1290  * fd.io coding-style-patch-verification: ON
1291  *
1292  * Local Variables:
1293  * eval: (c-set-style "gnu")
1294  * End:
1295  */