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:
7 * http://www.apache.org/licenses/LICENSE-2.0
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.
16 * interface.c: VNET interfaces/sub-interfaces
18 * Copyright (c) 2008 Eliot Dresselhaus
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:
28 * The above copyright notice and this permission notice shall be
29 * included in all copies or substantial portions of the Software.
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.
40 #include <vnet/vnet.h>
41 #include <vnet/plugin/plugin.h>
43 #define VNET_INTERFACE_SET_FLAGS_HELPER_IS_CREATE (1 << 0)
44 #define VNET_INTERFACE_SET_FLAGS_HELPER_WANT_REDISTRIBUTE (1 << 1)
46 static clib_error_t *vnet_hw_interface_set_flags_helper (vnet_main_t * vnm,
51 static clib_error_t *vnet_sw_interface_set_flags_helper (vnet_main_t * vnm,
56 static clib_error_t *vnet_hw_interface_set_class_helper (vnet_main_t * vnm,
63 /* Either sw or hw interface index. */
68 } vnet_sw_hw_interface_state_t;
71 serialize_vec_vnet_sw_hw_interface_state (serialize_main_t * m, va_list * va)
73 vnet_sw_hw_interface_state_t *s =
74 va_arg (*va, vnet_sw_hw_interface_state_t *);
75 u32 n = va_arg (*va, u32);
77 for (i = 0; i < n; i++)
79 serialize_integer (m, s[i].sw_hw_if_index,
80 sizeof (s[i].sw_hw_if_index));
81 serialize_integer (m, s[i].flags, sizeof (s[i].flags));
86 unserialize_vec_vnet_sw_hw_interface_state (serialize_main_t * m,
89 vnet_sw_hw_interface_state_t *s =
90 va_arg (*va, vnet_sw_hw_interface_state_t *);
91 u32 n = va_arg (*va, u32);
93 for (i = 0; i < n; i++)
95 unserialize_integer (m, &s[i].sw_hw_if_index,
96 sizeof (s[i].sw_hw_if_index));
97 unserialize_integer (m, &s[i].flags, sizeof (s[i].flags));
102 serialize_vnet_sw_hw_interface_set_flags (serialize_main_t * m, va_list * va)
104 vnet_sw_hw_interface_state_t *s =
105 va_arg (*va, vnet_sw_hw_interface_state_t *);
106 serialize (m, serialize_vec_vnet_sw_hw_interface_state, s, 1);
110 unserialize_vnet_sw_interface_set_flags (serialize_main_t * m, va_list * va)
112 CLIB_UNUSED (mc_main_t * mc) = va_arg (*va, mc_main_t *);
113 vnet_sw_hw_interface_state_t s;
115 unserialize (m, unserialize_vec_vnet_sw_hw_interface_state, &s, 1);
117 vnet_sw_interface_set_flags_helper
118 (vnet_get_main (), s.sw_hw_if_index, s.flags,
119 /* helper_flags no redistribution */ 0);
123 unserialize_vnet_hw_interface_set_flags (serialize_main_t * m, va_list * va)
125 CLIB_UNUSED (mc_main_t * mc) = va_arg (*va, mc_main_t *);
126 vnet_sw_hw_interface_state_t s;
128 unserialize (m, unserialize_vec_vnet_sw_hw_interface_state, &s, 1);
130 vnet_hw_interface_set_flags_helper
131 (vnet_get_main (), s.sw_hw_if_index, s.flags,
132 /* helper_flags no redistribution */ 0);
135 MC_SERIALIZE_MSG (vnet_sw_interface_set_flags_msg, static) =
137 .name = "vnet_sw_interface_set_flags",.serialize =
138 serialize_vnet_sw_hw_interface_set_flags,.unserialize =
139 unserialize_vnet_sw_interface_set_flags,};
141 MC_SERIALIZE_MSG (vnet_hw_interface_set_flags_msg, static) =
143 .name = "vnet_hw_interface_set_flags",.serialize =
144 serialize_vnet_sw_hw_interface_set_flags,.unserialize =
145 unserialize_vnet_hw_interface_set_flags,};
148 serialize_vnet_interface_state (serialize_main_t * m, va_list * va)
150 vnet_main_t *vnm = va_arg (*va, vnet_main_t *);
151 vnet_sw_hw_interface_state_t *sts = 0, *st;
152 vnet_sw_interface_t *sif;
153 vnet_hw_interface_t *hif;
154 vnet_interface_main_t *im = &vnm->interface_main;
156 /* Serialize hardware interface classes since they may have changed.
157 Must do this before sending up/down flags. */
159 pool_foreach (hif, im->hw_interfaces, ({
160 vnet_hw_interface_class_t * hw_class = vnet_get_hw_interface_class (vnm, hif->hw_class_index);
161 serialize_cstring (m, hw_class->name);
165 /* Send sw/hw interface state when non-zero. */
167 pool_foreach (sif, im->sw_interfaces, ({
170 vec_add2 (sts, st, 1);
171 st->sw_hw_if_index = sif->sw_if_index;
172 st->flags = sif->flags;
177 vec_serialize (m, sts, serialize_vec_vnet_sw_hw_interface_state);
183 pool_foreach (hif, im->hw_interfaces, ({
186 vec_add2 (sts, st, 1);
187 st->sw_hw_if_index = hif->hw_if_index;
188 st->flags = hif->flags;
193 vec_serialize (m, sts, serialize_vec_vnet_sw_hw_interface_state);
199 unserialize_vnet_interface_state (serialize_main_t * m, va_list * va)
201 vnet_main_t *vnm = va_arg (*va, vnet_main_t *);
202 vnet_sw_hw_interface_state_t *sts = 0, *st;
204 /* First set interface hardware class. */
206 vnet_interface_main_t *im = &vnm->interface_main;
207 vnet_hw_interface_t *hif;
213 pool_foreach (hif, im->hw_interfaces, ({
214 unserialize_cstring (m, &class_name);
215 p = hash_get_mem (im->hw_interface_class_by_name, class_name);
217 error = vnet_hw_interface_set_class_helper (vnm, hif->hw_if_index, p[0], /* redistribute */ 0);
219 clib_error_report (error);
220 vec_free (class_name);
225 vec_unserialize (m, &sts, unserialize_vec_vnet_sw_hw_interface_state);
226 vec_foreach (st, sts)
227 vnet_sw_interface_set_flags_helper (vnm, st->sw_hw_if_index, st->flags,
228 /* no distribute */ 0);
231 vec_unserialize (m, &sts, unserialize_vec_vnet_sw_hw_interface_state);
232 vec_foreach (st, sts)
233 vnet_hw_interface_set_flags_helper (vnm, st->sw_hw_if_index, st->flags,
234 /* no distribute */ 0);
238 static clib_error_t *
239 call_elf_section_interface_callbacks (vnet_main_t * vnm, u32 if_index,
241 _vnet_interface_function_list_elt_t *
244 clib_error_t *error = 0;
248 error = elt->fp (vnm, if_index, flags);
251 elt = elt->next_interface_function;
256 static clib_error_t *
257 call_hw_interface_add_del_callbacks (vnet_main_t * vnm, u32 hw_if_index,
260 vnet_hw_interface_t *hi = vnet_get_hw_interface (vnm, hw_if_index);
261 vnet_hw_interface_class_t *hw_class =
262 vnet_get_hw_interface_class (vnm, hi->hw_class_index);
263 vnet_device_class_t *dev_class =
264 vnet_get_device_class (vnm, hi->dev_class_index);
265 clib_error_t *error = 0;
267 if (hw_class->interface_add_del_function
269 hw_class->interface_add_del_function (vnm, hw_if_index, is_create)))
272 if (dev_class->interface_add_del_function
274 dev_class->interface_add_del_function (vnm, hw_if_index,
278 error = call_elf_section_interface_callbacks
279 (vnm, hw_if_index, is_create, vnm->hw_interface_add_del_functions);
284 static clib_error_t *
285 call_sw_interface_add_del_callbacks (vnet_main_t * vnm, u32 sw_if_index,
288 return call_elf_section_interface_callbacks
289 (vnm, sw_if_index, is_create, vnm->sw_interface_add_del_functions);
292 #define VNET_INTERFACE_SET_FLAGS_HELPER_IS_CREATE (1 << 0)
293 #define VNET_INTERFACE_SET_FLAGS_HELPER_WANT_REDISTRIBUTE (1 << 1)
295 static clib_error_t *
296 vnet_hw_interface_set_flags_helper (vnet_main_t * vnm, u32 hw_if_index,
297 u32 flags, u32 helper_flags)
299 vnet_hw_interface_t *hi = vnet_get_hw_interface (vnm, hw_if_index);
300 vnet_hw_interface_class_t *hw_class =
301 vnet_get_hw_interface_class (vnm, hi->hw_class_index);
302 vnet_device_class_t *dev_class =
303 vnet_get_device_class (vnm, hi->dev_class_index);
304 vlib_main_t *vm = vnm->vlib_main;
306 clib_error_t *error = 0;
308 (helper_flags & VNET_INTERFACE_SET_FLAGS_HELPER_IS_CREATE) != 0;
311 (VNET_HW_INTERFACE_FLAG_LINK_UP | VNET_HW_INTERFACE_FLAG_DUPLEX_MASK |
312 VNET_HW_INTERFACE_FLAG_SPEED_MASK);
315 /* Call hardware interface add/del callbacks. */
317 call_hw_interface_add_del_callbacks (vnm, hw_if_index, is_create);
319 /* Already in the desired state? */
320 if (!is_create && (hi->flags & mask) == flags)
323 /* Some interface classes do not redistribute (e.g. are local). */
324 if (!dev_class->redistribute)
325 helper_flags &= ~VNET_INTERFACE_SET_FLAGS_HELPER_WANT_REDISTRIBUTE;
328 && (helper_flags & VNET_INTERFACE_SET_FLAGS_HELPER_WANT_REDISTRIBUTE))
330 vnet_sw_hw_interface_state_t s;
331 s.sw_hw_if_index = hw_if_index;
333 mc_serialize (vm->mc_main, &vnet_hw_interface_set_flags_msg, &s);
336 if ((hi->flags & VNET_HW_INTERFACE_FLAG_LINK_UP) !=
337 (flags & VNET_HW_INTERFACE_FLAG_LINK_UP))
339 /* Do hardware class (e.g. ethernet). */
340 if (hw_class->link_up_down_function
341 && (error = hw_class->link_up_down_function (vnm, hw_if_index,
345 error = call_elf_section_interface_callbacks
346 (vnm, hw_if_index, flags, vnm->hw_interface_link_up_down_functions);
359 static clib_error_t *
360 vnet_sw_interface_set_flags_helper (vnet_main_t * vnm, u32 sw_if_index,
361 u32 flags, u32 helper_flags)
363 vnet_sw_interface_t *si = vnet_get_sw_interface (vnm, sw_if_index);
364 vlib_main_t *vm = vnm->vlib_main;
366 clib_error_t *error = 0;
368 (helper_flags & VNET_INTERFACE_SET_FLAGS_HELPER_IS_CREATE) != 0;
371 mask = VNET_SW_INTERFACE_FLAG_ADMIN_UP | VNET_SW_INTERFACE_FLAG_PUNT;
377 call_sw_interface_add_del_callbacks (vnm, sw_if_index, is_create);
381 if (flags & VNET_SW_INTERFACE_FLAG_ADMIN_UP)
383 /* Notify everyone when the interface is created as admin up */
384 error = call_elf_section_interface_callbacks (vnm, sw_if_index,
387 sw_interface_admin_up_down_functions);
394 vnet_sw_interface_t *si_sup = si;
396 /* Check that super interface is in correct state. */
397 if (si->type == VNET_SW_INTERFACE_TYPE_SUB)
399 si_sup = vnet_get_sw_interface (vnm, si->sup_sw_if_index);
401 /* Check to see if we're bringing down the soft interface and if it's parent is up */
402 if ((flags != (si_sup->flags & mask)) &&
404 && ((si_sup->flags & mask) ==
405 VNET_SW_INTERFACE_FLAG_ADMIN_UP))))
407 error = clib_error_return (0, "super-interface %U must be %U",
408 format_vnet_sw_interface_name, vnm,
410 format_vnet_sw_interface_flags,
416 /* Donot change state for slave link of bonded interfaces */
417 if (si->flags & VNET_SW_INTERFACE_FLAG_BOND_SLAVE)
419 error = clib_error_return
420 (0, "not allowed as %U belong to a BondEthernet interface",
421 format_vnet_sw_interface_name, vnm, si);
425 /* Already in the desired state? */
426 if ((si->flags & mask) == flags)
429 /* Sub-interfaces of hardware interfaces that do no redistribute,
430 do not redistribute themselves. */
431 if (si_sup->type == VNET_SW_INTERFACE_TYPE_HARDWARE)
433 vnet_hw_interface_t *hi =
434 vnet_get_hw_interface (vnm, si_sup->hw_if_index);
435 vnet_device_class_t *dev_class =
436 vnet_get_device_class (vnm, hi->dev_class_index);
437 if (!dev_class->redistribute)
439 ~VNET_INTERFACE_SET_FLAGS_HELPER_WANT_REDISTRIBUTE;
444 VNET_INTERFACE_SET_FLAGS_HELPER_WANT_REDISTRIBUTE))
446 vnet_sw_hw_interface_state_t s;
447 s.sw_hw_if_index = sw_if_index;
449 mc_serialize (vm->mc_main, &vnet_sw_interface_set_flags_msg, &s);
452 error = call_elf_section_interface_callbacks
453 (vnm, sw_if_index, flags, vnm->sw_interface_admin_up_down_functions);
458 if (si->type == VNET_SW_INTERFACE_TYPE_HARDWARE)
460 vnet_hw_interface_t *hi =
461 vnet_get_hw_interface (vnm, si->hw_if_index);
462 vnet_hw_interface_class_t *hw_class =
463 vnet_get_hw_interface_class (vnm, hi->hw_class_index);
464 vnet_device_class_t *dev_class =
465 vnet_get_device_class (vnm, hi->dev_class_index);
467 /* save the si admin up flag */
468 old_flags = si->flags;
470 /* update si admin up flag in advance if we are going admin down */
471 if (!(flags & VNET_SW_INTERFACE_FLAG_ADMIN_UP))
472 si->flags &= ~VNET_SW_INTERFACE_FLAG_ADMIN_UP;
474 if (dev_class->admin_up_down_function
475 && (error = dev_class->admin_up_down_function (vnm,
479 /* restore si admin up flag to it's original state on errors */
480 si->flags = old_flags;
484 if (hw_class->admin_up_down_function
485 && (error = hw_class->admin_up_down_function (vnm,
489 /* restore si admin up flag to it's original state on errors */
490 si->flags = old_flags;
494 /* Admin down implies link down. */
495 if (!(flags & VNET_SW_INTERFACE_FLAG_ADMIN_UP)
496 && (hi->flags & VNET_HW_INTERFACE_FLAG_LINK_UP))
497 vnet_hw_interface_set_flags_helper (vnm, si->hw_if_index,
499 ~VNET_HW_INTERFACE_FLAG_LINK_UP,
512 vnet_hw_interface_set_flags (vnet_main_t * vnm, u32 hw_if_index, u32 flags)
514 return vnet_hw_interface_set_flags_helper
515 (vnm, hw_if_index, flags,
516 VNET_INTERFACE_SET_FLAGS_HELPER_WANT_REDISTRIBUTE);
520 vnet_sw_interface_set_flags (vnet_main_t * vnm, u32 sw_if_index, u32 flags)
522 return vnet_sw_interface_set_flags_helper
523 (vnm, sw_if_index, flags,
524 VNET_INTERFACE_SET_FLAGS_HELPER_WANT_REDISTRIBUTE);
528 vnet_create_sw_interface_no_callbacks (vnet_main_t * vnm,
529 vnet_sw_interface_t * template)
531 vnet_interface_main_t *im = &vnm->interface_main;
532 vnet_sw_interface_t *sw;
535 pool_get (im->sw_interfaces, sw);
536 sw_if_index = sw - im->sw_interfaces;
541 sw->sw_if_index = sw_if_index;
542 if (sw->type == VNET_SW_INTERFACE_TYPE_HARDWARE)
543 sw->sup_sw_if_index = sw->sw_if_index;
545 /* Allocate counters for this interface. */
549 vnet_interface_counter_lock (im);
551 for (i = 0; i < vec_len (im->sw_if_counters); i++)
553 vlib_validate_simple_counter (&im->sw_if_counters[i], sw_if_index);
554 vlib_zero_simple_counter (&im->sw_if_counters[i], sw_if_index);
557 for (i = 0; i < vec_len (im->combined_sw_if_counters); i++)
559 vlib_validate_combined_counter (&im->combined_sw_if_counters[i],
561 vlib_zero_combined_counter (&im->combined_sw_if_counters[i],
565 vnet_interface_counter_unlock (im);
572 vnet_create_sw_interface (vnet_main_t * vnm, vnet_sw_interface_t * template,
576 vnet_hw_interface_t *hi;
577 vnet_device_class_t *dev_class;
579 hi = vnet_get_sup_hw_interface (vnm, template->sup_sw_if_index);
580 dev_class = vnet_get_device_class (vnm, hi->dev_class_index);
582 if (template->type == VNET_SW_INTERFACE_TYPE_SUB &&
583 dev_class->subif_add_del_function)
585 error = dev_class->subif_add_del_function (vnm, hi->hw_if_index,
586 (struct vnet_sw_interface_t
592 *sw_if_index = vnet_create_sw_interface_no_callbacks (vnm, template);
593 error = vnet_sw_interface_set_flags_helper
594 (vnm, *sw_if_index, template->flags,
595 VNET_INTERFACE_SET_FLAGS_HELPER_IS_CREATE);
599 /* undo the work done by vnet_create_sw_interface_no_callbacks() */
600 vnet_interface_main_t *im = &vnm->interface_main;
601 vnet_sw_interface_t *sw =
602 pool_elt_at_index (im->sw_interfaces, *sw_if_index);
603 pool_put (im->sw_interfaces, sw);
610 vnet_delete_sw_interface (vnet_main_t * vnm, u32 sw_if_index)
612 vnet_interface_main_t *im = &vnm->interface_main;
613 vnet_sw_interface_t *sw =
614 pool_elt_at_index (im->sw_interfaces, sw_if_index);
616 /* Bring down interface in case it is up. */
618 vnet_sw_interface_set_flags (vnm, sw_if_index, /* flags */ 0);
620 call_sw_interface_add_del_callbacks (vnm, sw_if_index, /* is_create */ 0);
622 pool_put (im->sw_interfaces, sw);
626 setup_tx_node (vlib_main_t * vm,
627 u32 node_index, vnet_device_class_t * dev_class)
629 vlib_node_t *n = vlib_get_node (vm, node_index);
631 n->function = dev_class->tx_function;
632 n->format_trace = dev_class->format_tx_trace;
633 vlib_register_errors (vm, node_index,
634 dev_class->tx_function_n_errors,
635 dev_class->tx_function_error_strings);
639 setup_output_node (vlib_main_t * vm,
640 u32 node_index, vnet_hw_interface_class_t * hw_class)
642 vlib_node_t *n = vlib_get_node (vm, node_index);
643 n->format_buffer = hw_class->format_header;
644 n->unformat_buffer = hw_class->unformat_header;
647 /* Register an interface instance. */
649 vnet_register_interface (vnet_main_t * vnm,
652 u32 hw_class_index, u32 hw_instance)
654 vnet_interface_main_t *im = &vnm->interface_main;
655 vnet_hw_interface_t *hw;
656 vnet_device_class_t *dev_class =
657 vnet_get_device_class (vnm, dev_class_index);
658 vnet_hw_interface_class_t *hw_class =
659 vnet_get_hw_interface_class (vnm, hw_class_index);
660 vlib_main_t *vm = vnm->vlib_main;
662 char *tx_node_name, *output_node_name;
664 pool_get (im->hw_interfaces, hw);
666 hw_index = hw - im->hw_interfaces;
667 hw->hw_if_index = hw_index;
669 if (dev_class->format_device_name)
670 hw->name = format (0, "%U", dev_class->format_device_name, dev_instance);
671 else if (hw_class->format_interface_name)
672 hw->name = format (0, "%U", hw_class->format_interface_name,
675 hw->name = format (0, "%s%x", hw_class->name, dev_instance);
677 if (!im->hw_interface_by_name)
678 im->hw_interface_by_name = hash_create_vec ( /* size */ 0,
679 sizeof (hw->name[0]),
682 hash_set_mem (im->hw_interface_by_name, hw->name, hw_index);
684 /* Make hardware interface point to software interface. */
686 vnet_sw_interface_t sw;
688 memset (&sw, 0, sizeof (sw));
689 sw.type = VNET_SW_INTERFACE_TYPE_HARDWARE;
690 sw.hw_if_index = hw_index;
691 hw->sw_if_index = vnet_create_sw_interface_no_callbacks (vnm, &sw);
694 hw->dev_class_index = dev_class_index;
695 hw->dev_instance = dev_instance;
696 hw->hw_class_index = hw_class_index;
697 hw->hw_instance = hw_instance;
699 hw->max_rate_bits_per_sec = 0;
700 hw->min_packet_bytes = 0;
701 hw->per_packet_overhead_bytes = 0;
702 hw->max_l3_packet_bytes[VLIB_RX] = ~0;
703 hw->max_l3_packet_bytes[VLIB_TX] = ~0;
705 tx_node_name = (char *) format (0, "%v-tx", hw->name);
706 output_node_name = (char *) format (0, "%v-output", hw->name);
708 /* If we have previously deleted interface nodes, re-use them. */
709 if (vec_len (im->deleted_hw_interface_nodes) > 0)
711 vnet_hw_interface_nodes_t *hn;
712 vnet_interface_output_runtime_t *rt;
714 hn = vec_end (im->deleted_hw_interface_nodes) - 1;
716 hw->tx_node_index = hn->tx_node_index;
717 hw->output_node_index = hn->output_node_index;
719 vlib_node_rename (vm, hw->tx_node_index, "%v", tx_node_name);
720 vlib_node_rename (vm, hw->output_node_index, "%v", output_node_name);
722 rt = vlib_node_get_runtime_data (vm, hw->output_node_index);
723 ASSERT (rt->is_deleted == 1);
725 rt->hw_if_index = hw_index;
726 rt->sw_if_index = hw->sw_if_index;
727 rt->dev_instance = hw->dev_instance;
729 rt = vlib_node_get_runtime_data (vm, hw->tx_node_index);
730 rt->hw_if_index = hw_index;
731 rt->sw_if_index = hw->sw_if_index;
732 rt->dev_instance = hw->dev_instance;
734 vlib_worker_thread_node_runtime_update ();
735 _vec_len (im->deleted_hw_interface_nodes) -= 1;
739 vlib_node_registration_t r;
740 vnet_interface_output_runtime_t rt = {
741 .hw_if_index = hw_index,
742 .sw_if_index = hw->sw_if_index,
743 .dev_instance = hw->dev_instance,
747 memset (&r, 0, sizeof (r));
748 r.type = VLIB_NODE_TYPE_INTERNAL;
749 r.runtime_data = &rt;
750 r.runtime_data_bytes = sizeof (rt);
752 r.vector_size = sizeof (u32);
754 r.flags = VLIB_NODE_FLAG_IS_OUTPUT;
755 r.name = tx_node_name;
756 r.function = dev_class->tx_function;
758 hw->tx_node_index = vlib_register_node (vm, &r);
760 vlib_node_add_named_next_with_slot (vm, hw->tx_node_index,
762 VNET_INTERFACE_TX_NEXT_DROP);
765 r.name = output_node_name;
766 r.function = dev_class->no_flatten_output_chains ?
767 vnet_interface_output_node_no_flatten_multiarch_select () :
768 vnet_interface_output_node_multiarch_select ();
769 r.format_trace = format_vnet_interface_output_trace;
774 "interface is deleted",
777 r.n_errors = ARRAY_LEN (e);
780 hw->output_node_index = vlib_register_node (vm, &r);
782 #define _(sym,str) vlib_node_add_named_next_with_slot (vm, \
783 hw->output_node_index, str, \
784 VNET_INTERFACE_OUTPUT_NEXT_##sym);
785 foreach_intf_output_feat
787 vlib_node_add_named_next_with_slot (vm, hw->output_node_index,
789 VNET_INTERFACE_OUTPUT_NEXT_DROP);
790 vlib_node_add_next_with_slot (vm, hw->output_node_index,
792 VNET_INTERFACE_OUTPUT_NEXT_TX);
795 setup_output_node (vm, hw->output_node_index, hw_class);
796 setup_tx_node (vm, hw->tx_node_index, dev_class);
798 /* Call all up/down callbacks with zero flags when interface is created. */
799 vnet_sw_interface_set_flags_helper (vnm, hw->sw_if_index, /* flags */ 0,
800 VNET_INTERFACE_SET_FLAGS_HELPER_IS_CREATE);
801 vnet_hw_interface_set_flags_helper (vnm, hw_index, /* flags */ 0,
802 VNET_INTERFACE_SET_FLAGS_HELPER_IS_CREATE);
808 vnet_delete_hw_interface (vnet_main_t * vnm, u32 hw_if_index)
810 vnet_interface_main_t *im = &vnm->interface_main;
811 vnet_hw_interface_t *hw = vnet_get_hw_interface (vnm, hw_if_index);
812 vlib_main_t *vm = vnm->vlib_main;
814 /* If it is up, mark it down. */
816 vnet_hw_interface_set_flags (vnm, hw_if_index, /* flags */ 0);
818 /* Call delete callbacks. */
819 call_hw_interface_add_del_callbacks (vnm, hw_if_index, /* is_create */ 0);
821 /* Delete software interface corresponding to hardware interface. */
822 vnet_delete_sw_interface (vnm, hw->sw_if_index);
824 /* Delete any sub-interfaces. */
828 hash_foreach (id, sw_if_index, hw->sub_interface_sw_if_index_by_id, ({
829 vnet_delete_sw_interface (vnm, sw_if_index);
835 vnet_hw_interface_nodes_t *dn;
836 vnet_interface_output_runtime_t *rt =
837 vlib_node_get_runtime_data (vm, hw->output_node_index);
839 /* Mark node runtime as deleted so output node (if called) will drop packets. */
842 vlib_node_rename (vm, hw->output_node_index,
843 "interface-%d-output-deleted", hw_if_index);
844 vlib_node_rename (vm, hw->tx_node_index, "interface-%d-tx-deleted",
846 vec_add2 (im->deleted_hw_interface_nodes, dn, 1);
847 dn->tx_node_index = hw->tx_node_index;
848 dn->output_node_index = hw->output_node_index;
851 hash_unset_mem (im->hw_interface_by_name, hw->name);
854 pool_put (im->hw_interfaces, hw);
858 serialize_vnet_hw_interface_set_class (serialize_main_t * m, va_list * va)
860 u32 hw_if_index = va_arg (*va, u32);
861 char *hw_class_name = va_arg (*va, char *);
862 serialize_integer (m, hw_if_index, sizeof (hw_if_index));
863 serialize_cstring (m, hw_class_name);
867 unserialize_vnet_hw_interface_set_class (serialize_main_t * m, va_list * va)
869 CLIB_UNUSED (mc_main_t * mc) = va_arg (*va, mc_main_t *);
870 vnet_main_t *vnm = vnet_get_main ();
876 unserialize_integer (m, &hw_if_index, sizeof (hw_if_index));
877 unserialize_cstring (m, &hw_class_name);
879 hash_get (vnm->interface_main.hw_interface_class_by_name, hw_class_name);
881 error = vnet_hw_interface_set_class_helper (vnm, hw_if_index, p[0],
882 /* redistribute */ 0);
884 clib_error_report (error);
887 MC_SERIALIZE_MSG (vnet_hw_interface_set_class_msg, static) =
889 .name = "vnet_hw_interface_set_class",.serialize =
890 serialize_vnet_hw_interface_set_class,.unserialize =
891 unserialize_vnet_hw_interface_set_class,};
894 vnet_hw_interface_init_for_class (vnet_main_t * vnm, u32 hw_if_index,
895 u32 hw_class_index, u32 hw_instance)
897 vnet_hw_interface_t *hi = vnet_get_hw_interface (vnm, hw_if_index);
898 vnet_hw_interface_class_t *hc =
899 vnet_get_hw_interface_class (vnm, hw_class_index);
901 hi->hw_class_index = hw_class_index;
902 hi->hw_instance = hw_instance;
903 setup_output_node (vnm->vlib_main, hi->output_node_index, hc);
906 static clib_error_t *
907 vnet_hw_interface_set_class_helper (vnet_main_t * vnm, u32 hw_if_index,
908 u32 hw_class_index, u32 redistribute)
910 vnet_hw_interface_t *hi = vnet_get_hw_interface (vnm, hw_if_index);
911 vnet_sw_interface_t *si = vnet_get_sw_interface (vnm, hi->sw_if_index);
912 vnet_hw_interface_class_t *old_class =
913 vnet_get_hw_interface_class (vnm, hi->hw_class_index);
914 vnet_hw_interface_class_t *new_class =
915 vnet_get_hw_interface_class (vnm, hw_class_index);
916 vnet_device_class_t *dev_class =
917 vnet_get_device_class (vnm, hi->dev_class_index);
918 clib_error_t *error = 0;
920 /* New class equals old class? Nothing to do. */
921 if (hi->hw_class_index == hw_class_index)
924 /* No need (and incorrect since admin up flag may be set) to do error checking when
925 receiving unserialize message. */
928 if (si->flags & VNET_SW_INTERFACE_FLAG_ADMIN_UP)
929 return clib_error_return (0,
930 "%v must be admin down to change class from %s to %s",
931 hi->name, old_class->name, new_class->name);
933 /* Make sure interface supports given class. */
934 if ((new_class->is_valid_class_for_interface
935 && !new_class->is_valid_class_for_interface (vnm, hw_if_index,
937 || (dev_class->is_valid_class_for_interface
938 && !dev_class->is_valid_class_for_interface (vnm, hw_if_index,
940 return clib_error_return (0,
941 "%v class cannot be changed from %s to %s",
942 hi->name, old_class->name, new_class->name);
944 if (vnm->vlib_main->mc_main)
946 mc_serialize (vnm->vlib_main->mc_main,
947 &vnet_hw_interface_set_class_msg, hw_if_index,
953 if (old_class->hw_class_change)
954 old_class->hw_class_change (vnm, hw_if_index, old_class->index,
957 vnet_hw_interface_init_for_class (vnm, hw_if_index, new_class->index,
960 if (new_class->hw_class_change)
961 new_class->hw_class_change (vnm, hw_if_index, old_class->index,
964 if (dev_class->hw_class_change)
965 dev_class->hw_class_change (vnm, hw_if_index, new_class->index);
971 vnet_hw_interface_set_class (vnet_main_t * vnm, u32 hw_if_index,
974 return vnet_hw_interface_set_class_helper (vnm, hw_if_index, hw_class_index,
975 /* redistribute */ 1);
979 vnet_hw_interface_rx_redirect_to_node_helper (vnet_main_t * vnm,
984 vnet_hw_interface_t *hi = vnet_get_hw_interface (vnm, hw_if_index);
985 vnet_device_class_t *dev_class = vnet_get_device_class
986 (vnm, hi->dev_class_index);
990 /* $$$$ fixme someday maybe */
991 ASSERT (vnm->vlib_main->mc_main == 0);
993 if (dev_class->rx_redirect_to_node)
995 dev_class->rx_redirect_to_node (vnm, hw_if_index, node_index);
999 return VNET_API_ERROR_UNIMPLEMENTED;
1003 vnet_hw_interface_rx_redirect_to_node (vnet_main_t * vnm, u32 hw_if_index,
1006 return vnet_hw_interface_rx_redirect_to_node_helper (vnm, hw_if_index,
1008 1 /* redistribute */ );
1012 vnet_sw_interface_compare (vnet_main_t * vnm,
1013 uword sw_if_index0, uword sw_if_index1)
1015 vnet_sw_interface_t *sup0 = vnet_get_sup_sw_interface (vnm, sw_if_index0);
1016 vnet_sw_interface_t *sup1 = vnet_get_sup_sw_interface (vnm, sw_if_index1);
1017 vnet_hw_interface_t *h0 = vnet_get_hw_interface (vnm, sup0->hw_if_index);
1018 vnet_hw_interface_t *h1 = vnet_get_hw_interface (vnm, sup1->hw_if_index);
1021 return vec_cmp (h0->name, h1->name);
1022 return (word) h0->hw_instance - (word) h1->hw_instance;
1026 vnet_hw_interface_compare (vnet_main_t * vnm,
1027 uword hw_if_index0, uword hw_if_index1)
1029 vnet_hw_interface_t *h0 = vnet_get_hw_interface (vnm, hw_if_index0);
1030 vnet_hw_interface_t *h1 = vnet_get_hw_interface (vnm, hw_if_index1);
1033 return vec_cmp (h0->name, h1->name);
1034 return (word) h0->hw_instance - (word) h1->hw_instance;
1038 vnet_interface_init (vlib_main_t * vm)
1040 vnet_main_t *vnm = vnet_get_main ();
1041 vnet_interface_main_t *im = &vnm->interface_main;
1042 vlib_buffer_t *b = 0;
1043 vnet_buffer_opaque_t *o = 0;
1046 * Keep people from shooting themselves in the foot.
1048 if (sizeof (b->opaque) != sizeof (vnet_buffer_opaque_t))
1050 #define _(a) if (sizeof(o->a) > sizeof (o->unused)) \
1052 ("FATAL: size of opaque union subtype %s is %d (max %d)", \
1053 #a, sizeof(o->a), sizeof (o->unused));
1054 foreach_buffer_opaque_union_subtype;
1057 return clib_error_return
1058 (0, "FATAL: size of vlib buffer opaque %d, size of vnet opaque %d",
1059 sizeof (b->opaque), sizeof (vnet_buffer_opaque_t));
1062 im->sw_if_counter_lock = clib_mem_alloc_aligned (CLIB_CACHE_LINE_BYTES,
1063 CLIB_CACHE_LINE_BYTES);
1064 im->sw_if_counter_lock[0] = 1; /* should be no need */
1066 vec_validate (im->sw_if_counters, VNET_N_SIMPLE_INTERFACE_COUNTER - 1);
1067 im->sw_if_counters[VNET_INTERFACE_COUNTER_DROP].name = "drops";
1068 im->sw_if_counters[VNET_INTERFACE_COUNTER_PUNT].name = "punts";
1069 im->sw_if_counters[VNET_INTERFACE_COUNTER_IP4].name = "ip4";
1070 im->sw_if_counters[VNET_INTERFACE_COUNTER_IP6].name = "ip6";
1071 im->sw_if_counters[VNET_INTERFACE_COUNTER_RX_NO_BUF].name = "rx-no-buf";
1072 im->sw_if_counters[VNET_INTERFACE_COUNTER_RX_MISS].name = "rx-miss";
1073 im->sw_if_counters[VNET_INTERFACE_COUNTER_RX_ERROR].name = "rx-error";
1074 im->sw_if_counters[VNET_INTERFACE_COUNTER_TX_ERROR].name = "tx-error";
1076 vec_validate (im->combined_sw_if_counters,
1077 VNET_N_COMBINED_INTERFACE_COUNTER - 1);
1078 im->combined_sw_if_counters[VNET_INTERFACE_COUNTER_RX].name = "rx";
1079 im->combined_sw_if_counters[VNET_INTERFACE_COUNTER_TX].name = "tx";
1081 im->sw_if_counter_lock[0] = 0;
1083 im->device_class_by_name = hash_create_string ( /* size */ 0,
1086 vnet_device_class_t *c;
1088 c = vnm->device_class_registrations;
1092 c->index = vec_len (im->device_classes);
1093 hash_set_mem (im->device_class_by_name, c->name, c->index);
1094 vec_add1 (im->device_classes, c[0]);
1095 c = c->next_class_registration;
1099 im->hw_interface_class_by_name = hash_create_string ( /* size */ 0,
1102 im->sw_if_index_by_sup_and_sub = hash_create_mem (0, sizeof (u64),
1105 vnet_hw_interface_class_t *c;
1107 c = vnm->hw_interface_class_registrations;
1111 c->index = vec_len (im->hw_interface_classes);
1112 hash_set_mem (im->hw_interface_class_by_name, c->name, c->index);
1113 vec_add1 (im->hw_interface_classes, c[0]);
1114 c = c->next_class_registration;
1119 clib_error_t *error;
1121 if ((error = vlib_call_init_function (vm, vnet_interface_cli_init)))
1128 VLIB_INIT_FUNCTION (vnet_interface_init);
1130 /* Kludge to renumber interface names [only!] */
1132 vnet_interface_name_renumber (u32 sw_if_index, u32 new_show_dev_instance)
1135 vnet_main_t *vnm = vnet_get_main ();
1136 vnet_interface_main_t *im = &vnm->interface_main;
1137 vnet_hw_interface_t *hi = vnet_get_sup_hw_interface (vnm, sw_if_index);
1139 vnet_device_class_t *dev_class = vnet_get_device_class
1140 (vnm, hi->dev_class_index);
1142 if (dev_class->name_renumber == 0 || dev_class->format_device_name == 0)
1143 return VNET_API_ERROR_UNIMPLEMENTED;
1145 rv = dev_class->name_renumber (hi, new_show_dev_instance);
1150 hash_unset_mem (im->hw_interface_by_name, hi->name);
1151 vec_free (hi->name);
1152 /* Use the mapping we set up to call it Ishmael */
1153 hi->name = format (0, "%U", dev_class->format_device_name,
1156 hash_set_mem (im->hw_interface_by_name, hi->name, hi->hw_if_index);
1161 vnet_interface_add_del_feature (vnet_main_t * vnm,
1164 intf_output_feat_t feature, int is_add)
1166 vnet_sw_interface_t *sw;
1168 sw = vnet_get_sw_interface (vnm, sw_if_index);
1173 sw->output_feature_bitmap |= (1 << feature);
1174 sw->output_feature_bitmap |= (1 << INTF_OUTPUT_FEAT_DONE);
1180 sw->output_feature_bitmap &= ~(1 << feature);
1181 if (sw->output_feature_bitmap == (1 << INTF_OUTPUT_FEAT_DONE))
1182 sw->output_feature_bitmap = 0;
1189 vnet_rename_interface (vnet_main_t * vnm, u32 hw_if_index, char *new_name)
1191 vnet_interface_main_t *im = &vnm->interface_main;
1192 vlib_main_t *vm = vnm->vlib_main;
1193 vnet_hw_interface_t *hw;
1195 clib_error_t *error = 0;
1197 hw = vnet_get_hw_interface (vnm, hw_if_index);
1200 return clib_error_return (0,
1201 "unable to find hw interface for index %u",
1205 old_name = hw->name;
1207 /* set new hw->name */
1208 hw->name = format (0, "%s", new_name);
1210 /* remove the old name to hw_if_index mapping and install the new one */
1211 hash_unset_mem (im->hw_interface_by_name, old_name);
1212 hash_set_mem (im->hw_interface_by_name, hw->name, hw_if_index);
1214 /* rename tx/output nodes */
1215 vlib_node_rename (vm, hw->tx_node_index, "%v-tx", hw->name);
1216 vlib_node_rename (vm, hw->output_node_index, "%v-output", hw->name);
1218 /* free the old name vector */
1219 vec_free (old_name);
1225 * fd.io coding-style-patch-verification: ON
1228 * eval: (c-set-style "gnu")