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