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, is_create,
347 vnm->hw_interface_link_up_down_functions);
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)
364 vnet_sw_interface_t *si = vnet_get_sw_interface (vnm, sw_if_index);
365 vlib_main_t *vm = vnm->vlib_main;
367 clib_error_t *error = 0;
369 (helper_flags & VNET_INTERFACE_SET_FLAGS_HELPER_IS_CREATE) != 0;
372 mask = VNET_SW_INTERFACE_FLAG_ADMIN_UP | VNET_SW_INTERFACE_FLAG_PUNT;
378 call_sw_interface_add_del_callbacks (vnm, sw_if_index, is_create);
382 if (flags & VNET_SW_INTERFACE_FLAG_ADMIN_UP)
384 /* Notify everyone when the interface is created as admin up */
385 error = call_elf_section_interface_callbacks (vnm, sw_if_index,
388 sw_interface_admin_up_down_functions);
395 vnet_sw_interface_t *si_sup = si;
397 /* Check that super interface is in correct state. */
398 if (si->type == VNET_SW_INTERFACE_TYPE_SUB)
400 si_sup = vnet_get_sw_interface (vnm, si->sup_sw_if_index);
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)) &&
405 && ((si_sup->flags & mask) ==
406 VNET_SW_INTERFACE_FLAG_ADMIN_UP))))
408 error = clib_error_return (0, "super-interface %U must be %U",
409 format_vnet_sw_interface_name, vnm,
411 format_vnet_sw_interface_flags,
417 /* Donot change state for slave link of bonded interfaces */
418 if (si->flags & VNET_SW_INTERFACE_FLAG_BOND_SLAVE)
420 error = clib_error_return
421 (0, "not allowed as %U belong to a BondEthernet interface",
422 format_vnet_sw_interface_name, vnm, si);
426 /* Already in the desired state? */
427 if ((si->flags & mask) == flags)
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)
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)
440 ~VNET_INTERFACE_SET_FLAGS_HELPER_WANT_REDISTRIBUTE;
445 VNET_INTERFACE_SET_FLAGS_HELPER_WANT_REDISTRIBUTE))
447 vnet_sw_hw_interface_state_t s;
448 s.sw_hw_if_index = sw_if_index;
450 mc_serialize (vm->mc_main, &vnet_sw_interface_set_flags_msg, &s);
453 error = call_elf_section_interface_callbacks
454 (vnm, sw_if_index, flags, vnm->sw_interface_admin_up_down_functions);
459 if (si->type == VNET_SW_INTERFACE_TYPE_HARDWARE)
461 vnet_hw_interface_t *hi =
462 vnet_get_hw_interface (vnm, si->hw_if_index);
463 vnet_hw_interface_class_t *hw_class =
464 vnet_get_hw_interface_class (vnm, hi->hw_class_index);
465 vnet_device_class_t *dev_class =
466 vnet_get_device_class (vnm, hi->dev_class_index);
468 /* save the si admin up flag */
469 old_flags = si->flags;
471 /* update si admin up flag in advance if we are going admin down */
472 if (!(flags & VNET_SW_INTERFACE_FLAG_ADMIN_UP))
473 si->flags &= ~VNET_SW_INTERFACE_FLAG_ADMIN_UP;
475 if (dev_class->admin_up_down_function
476 && (error = dev_class->admin_up_down_function (vnm,
480 /* restore si admin up flag to it's original state on errors */
481 si->flags = old_flags;
485 if (hw_class->admin_up_down_function
486 && (error = hw_class->admin_up_down_function (vnm,
490 /* restore si admin up flag to it's original state on errors */
491 si->flags = old_flags;
495 /* Admin down implies link down. */
496 if (!(flags & VNET_SW_INTERFACE_FLAG_ADMIN_UP)
497 && (hi->flags & VNET_HW_INTERFACE_FLAG_LINK_UP))
498 vnet_hw_interface_set_flags_helper (vnm, si->hw_if_index,
500 ~VNET_HW_INTERFACE_FLAG_LINK_UP,
513 vnet_hw_interface_set_flags (vnet_main_t * vnm, u32 hw_if_index, u32 flags)
515 return vnet_hw_interface_set_flags_helper
516 (vnm, hw_if_index, flags,
517 VNET_INTERFACE_SET_FLAGS_HELPER_WANT_REDISTRIBUTE);
521 vnet_sw_interface_set_flags (vnet_main_t * vnm, u32 sw_if_index, u32 flags)
523 return vnet_sw_interface_set_flags_helper
524 (vnm, sw_if_index, flags,
525 VNET_INTERFACE_SET_FLAGS_HELPER_WANT_REDISTRIBUTE);
529 vnet_create_sw_interface_no_callbacks (vnet_main_t * vnm,
530 vnet_sw_interface_t * template)
532 vnet_interface_main_t *im = &vnm->interface_main;
533 vnet_sw_interface_t *sw;
536 pool_get (im->sw_interfaces, sw);
537 sw_if_index = sw - im->sw_interfaces;
542 sw->sw_if_index = sw_if_index;
543 if (sw->type == VNET_SW_INTERFACE_TYPE_HARDWARE)
544 sw->sup_sw_if_index = sw->sw_if_index;
546 /* Allocate counters for this interface. */
550 vnet_interface_counter_lock (im);
552 for (i = 0; i < vec_len (im->sw_if_counters); i++)
554 vlib_validate_simple_counter (&im->sw_if_counters[i], sw_if_index);
555 vlib_zero_simple_counter (&im->sw_if_counters[i], sw_if_index);
558 for (i = 0; i < vec_len (im->combined_sw_if_counters); i++)
560 vlib_validate_combined_counter (&im->combined_sw_if_counters[i],
562 vlib_zero_combined_counter (&im->combined_sw_if_counters[i],
566 vnet_interface_counter_unlock (im);
573 vnet_create_sw_interface (vnet_main_t * vnm, vnet_sw_interface_t * template,
577 vnet_hw_interface_t *hi;
578 vnet_device_class_t *dev_class;
580 hi = vnet_get_sup_hw_interface (vnm, template->sup_sw_if_index);
581 dev_class = vnet_get_device_class (vnm, hi->dev_class_index);
583 if (template->type == VNET_SW_INTERFACE_TYPE_SUB &&
584 dev_class->subif_add_del_function)
586 error = dev_class->subif_add_del_function (vnm, hi->hw_if_index,
587 (struct vnet_sw_interface_t
593 *sw_if_index = vnet_create_sw_interface_no_callbacks (vnm, template);
594 error = vnet_sw_interface_set_flags_helper
595 (vnm, *sw_if_index, template->flags,
596 VNET_INTERFACE_SET_FLAGS_HELPER_IS_CREATE);
600 /* undo the work done by vnet_create_sw_interface_no_callbacks() */
601 vnet_interface_main_t *im = &vnm->interface_main;
602 vnet_sw_interface_t *sw =
603 pool_elt_at_index (im->sw_interfaces, *sw_if_index);
604 pool_put (im->sw_interfaces, sw);
611 vnet_delete_sw_interface (vnet_main_t * vnm, u32 sw_if_index)
613 vnet_interface_main_t *im = &vnm->interface_main;
614 vnet_sw_interface_t *sw =
615 pool_elt_at_index (im->sw_interfaces, sw_if_index);
617 /* Bring down interface in case it is up. */
619 vnet_sw_interface_set_flags (vnm, sw_if_index, /* flags */ 0);
621 call_sw_interface_add_del_callbacks (vnm, sw_if_index, /* is_create */ 0);
623 pool_put (im->sw_interfaces, sw);
627 setup_tx_node (vlib_main_t * vm,
628 u32 node_index, vnet_device_class_t * dev_class)
630 vlib_node_t *n = vlib_get_node (vm, node_index);
632 n->function = dev_class->tx_function;
633 n->format_trace = dev_class->format_tx_trace;
634 vlib_register_errors (vm, node_index,
635 dev_class->tx_function_n_errors,
636 dev_class->tx_function_error_strings);
640 setup_output_node (vlib_main_t * vm,
641 u32 node_index, vnet_hw_interface_class_t * hw_class)
643 vlib_node_t *n = vlib_get_node (vm, node_index);
644 n->format_buffer = hw_class->format_header;
645 n->unformat_buffer = hw_class->unformat_header;
648 /* Register an interface instance. */
650 vnet_register_interface (vnet_main_t * vnm,
653 u32 hw_class_index, u32 hw_instance)
655 vnet_interface_main_t *im = &vnm->interface_main;
656 vnet_hw_interface_t *hw;
657 vnet_device_class_t *dev_class =
658 vnet_get_device_class (vnm, dev_class_index);
659 vnet_hw_interface_class_t *hw_class =
660 vnet_get_hw_interface_class (vnm, hw_class_index);
661 vlib_main_t *vm = vnm->vlib_main;
663 char *tx_node_name, *output_node_name;
665 pool_get (im->hw_interfaces, hw);
667 hw_index = hw - im->hw_interfaces;
668 hw->hw_if_index = hw_index;
670 if (dev_class->format_device_name)
671 hw->name = format (0, "%U", dev_class->format_device_name, dev_instance);
672 else if (hw_class->format_interface_name)
673 hw->name = format (0, "%U", hw_class->format_interface_name,
676 hw->name = format (0, "%s%x", hw_class->name, dev_instance);
678 if (!im->hw_interface_by_name)
679 im->hw_interface_by_name = hash_create_vec ( /* size */ 0,
680 sizeof (hw->name[0]),
683 hash_set_mem (im->hw_interface_by_name, hw->name, hw_index);
685 /* Make hardware interface point to software interface. */
687 vnet_sw_interface_t sw;
689 memset (&sw, 0, sizeof (sw));
690 sw.type = VNET_SW_INTERFACE_TYPE_HARDWARE;
691 sw.hw_if_index = hw_index;
692 hw->sw_if_index = vnet_create_sw_interface_no_callbacks (vnm, &sw);
695 hw->dev_class_index = dev_class_index;
696 hw->dev_instance = dev_instance;
697 hw->hw_class_index = hw_class_index;
698 hw->hw_instance = hw_instance;
700 hw->max_rate_bits_per_sec = 0;
701 hw->min_packet_bytes = 0;
702 hw->per_packet_overhead_bytes = 0;
703 hw->max_l3_packet_bytes[VLIB_RX] = ~0;
704 hw->max_l3_packet_bytes[VLIB_TX] = ~0;
706 tx_node_name = (char *) format (0, "%v-tx", hw->name);
707 output_node_name = (char *) format (0, "%v-output", hw->name);
709 /* If we have previously deleted interface nodes, re-use them. */
710 if (vec_len (im->deleted_hw_interface_nodes) > 0)
712 vnet_hw_interface_nodes_t *hn;
713 vnet_interface_output_runtime_t *rt;
715 hn = vec_end (im->deleted_hw_interface_nodes) - 1;
717 hw->tx_node_index = hn->tx_node_index;
718 hw->output_node_index = hn->output_node_index;
720 vlib_node_rename (vm, hw->tx_node_index, "%v", tx_node_name);
721 vlib_node_rename (vm, hw->output_node_index, "%v", output_node_name);
723 rt = vlib_node_get_runtime_data (vm, hw->output_node_index);
724 ASSERT (rt->is_deleted == 1);
726 rt->hw_if_index = hw_index;
727 rt->sw_if_index = hw->sw_if_index;
728 rt->dev_instance = hw->dev_instance;
730 rt = vlib_node_get_runtime_data (vm, hw->tx_node_index);
731 rt->hw_if_index = hw_index;
732 rt->sw_if_index = hw->sw_if_index;
733 rt->dev_instance = hw->dev_instance;
735 vlib_worker_thread_node_runtime_update ();
736 _vec_len (im->deleted_hw_interface_nodes) -= 1;
740 vlib_node_registration_t r;
741 vnet_interface_output_runtime_t rt = {
742 .hw_if_index = hw_index,
743 .sw_if_index = hw->sw_if_index,
744 .dev_instance = hw->dev_instance,
748 memset (&r, 0, sizeof (r));
749 r.type = VLIB_NODE_TYPE_INTERNAL;
750 r.runtime_data = &rt;
751 r.runtime_data_bytes = sizeof (rt);
753 r.vector_size = sizeof (u32);
755 r.flags = VLIB_NODE_FLAG_IS_OUTPUT;
756 r.name = tx_node_name;
757 r.function = dev_class->tx_function;
759 hw->tx_node_index = vlib_register_node (vm, &r);
761 vlib_node_add_named_next_with_slot (vm, hw->tx_node_index,
763 VNET_INTERFACE_TX_NEXT_DROP);
766 r.name = output_node_name;
767 r.function = dev_class->no_flatten_output_chains ?
768 vnet_interface_output_node_no_flatten_multiarch_select () :
769 vnet_interface_output_node_multiarch_select ();
770 r.format_trace = format_vnet_interface_output_trace;
775 "interface is deleted",
778 r.n_errors = ARRAY_LEN (e);
781 hw->output_node_index = vlib_register_node (vm, &r);
783 #define _(sym,str) vlib_node_add_named_next_with_slot (vm, \
784 hw->output_node_index, str, \
785 VNET_INTERFACE_OUTPUT_NEXT_##sym);
786 foreach_intf_output_feat
788 vlib_node_add_named_next_with_slot (vm, hw->output_node_index,
790 VNET_INTERFACE_OUTPUT_NEXT_DROP);
791 vlib_node_add_next_with_slot (vm, hw->output_node_index,
793 VNET_INTERFACE_OUTPUT_NEXT_TX);
796 setup_output_node (vm, hw->output_node_index, hw_class);
797 setup_tx_node (vm, hw->tx_node_index, dev_class);
799 /* Call all up/down callbacks with zero flags when interface is created. */
800 vnet_sw_interface_set_flags_helper (vnm, hw->sw_if_index, /* flags */ 0,
801 VNET_INTERFACE_SET_FLAGS_HELPER_IS_CREATE);
802 vnet_hw_interface_set_flags_helper (vnm, hw_index, /* flags */ 0,
803 VNET_INTERFACE_SET_FLAGS_HELPER_IS_CREATE);
809 vnet_delete_hw_interface (vnet_main_t * vnm, u32 hw_if_index)
811 vnet_interface_main_t *im = &vnm->interface_main;
812 vnet_hw_interface_t *hw = vnet_get_hw_interface (vnm, hw_if_index);
813 vlib_main_t *vm = vnm->vlib_main;
815 /* If it is up, mark it down. */
817 vnet_hw_interface_set_flags (vnm, hw_if_index, /* flags */ 0);
819 /* Call delete callbacks. */
820 call_hw_interface_add_del_callbacks (vnm, hw_if_index, /* is_create */ 0);
822 /* Delete software interface corresponding to hardware interface. */
823 vnet_delete_sw_interface (vnm, hw->sw_if_index);
825 /* Delete any sub-interfaces. */
829 hash_foreach (id, sw_if_index, hw->sub_interface_sw_if_index_by_id, ({
830 vnet_delete_sw_interface (vnm, sw_if_index);
836 vnet_hw_interface_nodes_t *dn;
837 vnet_interface_output_runtime_t *rt =
838 vlib_node_get_runtime_data (vm, hw->output_node_index);
840 /* Mark node runtime as deleted so output node (if called) will drop packets. */
843 vlib_node_rename (vm, hw->output_node_index,
844 "interface-%d-output-deleted", hw_if_index);
845 vlib_node_rename (vm, hw->tx_node_index, "interface-%d-tx-deleted",
847 vec_add2 (im->deleted_hw_interface_nodes, dn, 1);
848 dn->tx_node_index = hw->tx_node_index;
849 dn->output_node_index = hw->output_node_index;
852 hash_unset_mem (im->hw_interface_by_name, hw->name);
855 pool_put (im->hw_interfaces, hw);
859 serialize_vnet_hw_interface_set_class (serialize_main_t * m, va_list * va)
861 u32 hw_if_index = va_arg (*va, u32);
862 char *hw_class_name = va_arg (*va, char *);
863 serialize_integer (m, hw_if_index, sizeof (hw_if_index));
864 serialize_cstring (m, hw_class_name);
868 unserialize_vnet_hw_interface_set_class (serialize_main_t * m, va_list * va)
870 CLIB_UNUSED (mc_main_t * mc) = va_arg (*va, mc_main_t *);
871 vnet_main_t *vnm = vnet_get_main ();
877 unserialize_integer (m, &hw_if_index, sizeof (hw_if_index));
878 unserialize_cstring (m, &hw_class_name);
880 hash_get (vnm->interface_main.hw_interface_class_by_name, hw_class_name);
882 error = vnet_hw_interface_set_class_helper (vnm, hw_if_index, p[0],
883 /* redistribute */ 0);
885 clib_error_report (error);
888 MC_SERIALIZE_MSG (vnet_hw_interface_set_class_msg, static) =
890 .name = "vnet_hw_interface_set_class",.serialize =
891 serialize_vnet_hw_interface_set_class,.unserialize =
892 unserialize_vnet_hw_interface_set_class,};
895 vnet_hw_interface_init_for_class (vnet_main_t * vnm, u32 hw_if_index,
896 u32 hw_class_index, u32 hw_instance)
898 vnet_hw_interface_t *hi = vnet_get_hw_interface (vnm, hw_if_index);
899 vnet_hw_interface_class_t *hc =
900 vnet_get_hw_interface_class (vnm, hw_class_index);
902 hi->hw_class_index = hw_class_index;
903 hi->hw_instance = hw_instance;
904 setup_output_node (vnm->vlib_main, hi->output_node_index, hc);
907 static clib_error_t *
908 vnet_hw_interface_set_class_helper (vnet_main_t * vnm, u32 hw_if_index,
909 u32 hw_class_index, u32 redistribute)
911 vnet_hw_interface_t *hi = vnet_get_hw_interface (vnm, hw_if_index);
912 vnet_sw_interface_t *si = vnet_get_sw_interface (vnm, hi->sw_if_index);
913 vnet_hw_interface_class_t *old_class =
914 vnet_get_hw_interface_class (vnm, hi->hw_class_index);
915 vnet_hw_interface_class_t *new_class =
916 vnet_get_hw_interface_class (vnm, hw_class_index);
917 vnet_device_class_t *dev_class =
918 vnet_get_device_class (vnm, hi->dev_class_index);
919 clib_error_t *error = 0;
921 /* New class equals old class? Nothing to do. */
922 if (hi->hw_class_index == hw_class_index)
925 /* No need (and incorrect since admin up flag may be set) to do error checking when
926 receiving unserialize message. */
929 if (si->flags & VNET_SW_INTERFACE_FLAG_ADMIN_UP)
930 return clib_error_return (0,
931 "%v must be admin down to change class from %s to %s",
932 hi->name, old_class->name, new_class->name);
934 /* Make sure interface supports given class. */
935 if ((new_class->is_valid_class_for_interface
936 && !new_class->is_valid_class_for_interface (vnm, hw_if_index,
938 || (dev_class->is_valid_class_for_interface
939 && !dev_class->is_valid_class_for_interface (vnm, hw_if_index,
941 return clib_error_return (0,
942 "%v class cannot be changed from %s to %s",
943 hi->name, old_class->name, new_class->name);
945 if (vnm->vlib_main->mc_main)
947 mc_serialize (vnm->vlib_main->mc_main,
948 &vnet_hw_interface_set_class_msg, hw_if_index,
954 if (old_class->hw_class_change)
955 old_class->hw_class_change (vnm, hw_if_index, old_class->index,
958 vnet_hw_interface_init_for_class (vnm, hw_if_index, new_class->index,
961 if (new_class->hw_class_change)
962 new_class->hw_class_change (vnm, hw_if_index, old_class->index,
965 if (dev_class->hw_class_change)
966 dev_class->hw_class_change (vnm, hw_if_index, new_class->index);
972 vnet_hw_interface_set_class (vnet_main_t * vnm, u32 hw_if_index,
975 return vnet_hw_interface_set_class_helper (vnm, hw_if_index, hw_class_index,
976 /* redistribute */ 1);
980 vnet_hw_interface_rx_redirect_to_node_helper (vnet_main_t * vnm,
985 vnet_hw_interface_t *hi = vnet_get_hw_interface (vnm, hw_if_index);
986 vnet_device_class_t *dev_class = vnet_get_device_class
987 (vnm, hi->dev_class_index);
991 /* $$$$ fixme someday maybe */
992 ASSERT (vnm->vlib_main->mc_main == 0);
994 if (dev_class->rx_redirect_to_node)
996 dev_class->rx_redirect_to_node (vnm, hw_if_index, node_index);
1000 return VNET_API_ERROR_UNIMPLEMENTED;
1004 vnet_hw_interface_rx_redirect_to_node (vnet_main_t * vnm, u32 hw_if_index,
1007 return vnet_hw_interface_rx_redirect_to_node_helper (vnm, hw_if_index,
1009 1 /* redistribute */ );
1013 vnet_sw_interface_compare (vnet_main_t * vnm,
1014 uword sw_if_index0, uword sw_if_index1)
1016 vnet_sw_interface_t *sup0 = vnet_get_sup_sw_interface (vnm, sw_if_index0);
1017 vnet_sw_interface_t *sup1 = vnet_get_sup_sw_interface (vnm, sw_if_index1);
1018 vnet_hw_interface_t *h0 = vnet_get_hw_interface (vnm, sup0->hw_if_index);
1019 vnet_hw_interface_t *h1 = vnet_get_hw_interface (vnm, sup1->hw_if_index);
1022 return vec_cmp (h0->name, h1->name);
1023 return (word) h0->hw_instance - (word) h1->hw_instance;
1027 vnet_hw_interface_compare (vnet_main_t * vnm,
1028 uword hw_if_index0, uword hw_if_index1)
1030 vnet_hw_interface_t *h0 = vnet_get_hw_interface (vnm, hw_if_index0);
1031 vnet_hw_interface_t *h1 = vnet_get_hw_interface (vnm, hw_if_index1);
1034 return vec_cmp (h0->name, h1->name);
1035 return (word) h0->hw_instance - (word) h1->hw_instance;
1039 vnet_interface_init (vlib_main_t * vm)
1041 vnet_main_t *vnm = vnet_get_main ();
1042 vnet_interface_main_t *im = &vnm->interface_main;
1043 vlib_buffer_t *b = 0;
1044 vnet_buffer_opaque_t *o = 0;
1047 * Keep people from shooting themselves in the foot.
1049 if (sizeof (b->opaque) != sizeof (vnet_buffer_opaque_t))
1051 #define _(a) if (sizeof(o->a) > sizeof (o->unused)) \
1053 ("FATAL: size of opaque union subtype %s is %d (max %d)", \
1054 #a, sizeof(o->a), sizeof (o->unused));
1055 foreach_buffer_opaque_union_subtype;
1058 return clib_error_return
1059 (0, "FATAL: size of vlib buffer opaque %d, size of vnet opaque %d",
1060 sizeof (b->opaque), sizeof (vnet_buffer_opaque_t));
1063 im->sw_if_counter_lock = clib_mem_alloc_aligned (CLIB_CACHE_LINE_BYTES,
1064 CLIB_CACHE_LINE_BYTES);
1065 im->sw_if_counter_lock[0] = 1; /* should be no need */
1067 vec_validate (im->sw_if_counters, VNET_N_SIMPLE_INTERFACE_COUNTER - 1);
1068 im->sw_if_counters[VNET_INTERFACE_COUNTER_DROP].name = "drops";
1069 im->sw_if_counters[VNET_INTERFACE_COUNTER_PUNT].name = "punts";
1070 im->sw_if_counters[VNET_INTERFACE_COUNTER_IP4].name = "ip4";
1071 im->sw_if_counters[VNET_INTERFACE_COUNTER_IP6].name = "ip6";
1072 im->sw_if_counters[VNET_INTERFACE_COUNTER_RX_NO_BUF].name = "rx-no-buf";
1073 im->sw_if_counters[VNET_INTERFACE_COUNTER_RX_MISS].name = "rx-miss";
1074 im->sw_if_counters[VNET_INTERFACE_COUNTER_RX_ERROR].name = "rx-error";
1075 im->sw_if_counters[VNET_INTERFACE_COUNTER_TX_ERROR].name = "tx-error";
1077 vec_validate (im->combined_sw_if_counters,
1078 VNET_N_COMBINED_INTERFACE_COUNTER - 1);
1079 im->combined_sw_if_counters[VNET_INTERFACE_COUNTER_RX].name = "rx";
1080 im->combined_sw_if_counters[VNET_INTERFACE_COUNTER_TX].name = "tx";
1082 im->sw_if_counter_lock[0] = 0;
1084 im->device_class_by_name = hash_create_string ( /* size */ 0,
1087 vnet_device_class_t *c;
1089 c = vnm->device_class_registrations;
1093 c->index = vec_len (im->device_classes);
1094 hash_set_mem (im->device_class_by_name, c->name, c->index);
1095 vec_add1 (im->device_classes, c[0]);
1096 c = c->next_class_registration;
1100 im->hw_interface_class_by_name = hash_create_string ( /* size */ 0,
1103 im->sw_if_index_by_sup_and_sub = hash_create_mem (0, sizeof (u64),
1106 vnet_hw_interface_class_t *c;
1108 c = vnm->hw_interface_class_registrations;
1112 c->index = vec_len (im->hw_interface_classes);
1113 hash_set_mem (im->hw_interface_class_by_name, c->name, c->index);
1114 vec_add1 (im->hw_interface_classes, c[0]);
1115 c = c->next_class_registration;
1120 clib_error_t *error;
1122 if ((error = vlib_call_init_function (vm, vnet_interface_cli_init)))
1129 VLIB_INIT_FUNCTION (vnet_interface_init);
1131 /* Kludge to renumber interface names [only!] */
1133 vnet_interface_name_renumber (u32 sw_if_index, u32 new_show_dev_instance)
1136 vnet_main_t *vnm = vnet_get_main ();
1137 vnet_interface_main_t *im = &vnm->interface_main;
1138 vnet_hw_interface_t *hi = vnet_get_sup_hw_interface (vnm, sw_if_index);
1140 vnet_device_class_t *dev_class = vnet_get_device_class
1141 (vnm, hi->dev_class_index);
1143 if (dev_class->name_renumber == 0 || dev_class->format_device_name == 0)
1144 return VNET_API_ERROR_UNIMPLEMENTED;
1146 rv = dev_class->name_renumber (hi, new_show_dev_instance);
1151 hash_unset_mem (im->hw_interface_by_name, hi->name);
1152 vec_free (hi->name);
1153 /* Use the mapping we set up to call it Ishmael */
1154 hi->name = format (0, "%U", dev_class->format_device_name,
1157 hash_set_mem (im->hw_interface_by_name, hi->name, hi->hw_if_index);
1162 vnet_interface_add_del_feature (vnet_main_t * vnm,
1165 intf_output_feat_t feature, int is_add)
1167 vnet_sw_interface_t *sw;
1169 sw = vnet_get_sw_interface (vnm, sw_if_index);
1174 sw->output_feature_bitmap |= (1 << feature);
1175 sw->output_feature_bitmap |= (1 << INTF_OUTPUT_FEAT_DONE);
1181 sw->output_feature_bitmap &= ~(1 << feature);
1182 if (sw->output_feature_bitmap == (1 << INTF_OUTPUT_FEAT_DONE))
1183 sw->output_feature_bitmap = 0;
1190 vnet_rename_interface (vnet_main_t * vnm, u32 hw_if_index, char *new_name)
1192 vnet_interface_main_t *im = &vnm->interface_main;
1193 vlib_main_t *vm = vnm->vlib_main;
1194 vnet_hw_interface_t *hw;
1196 clib_error_t *error = 0;
1198 hw = vnet_get_hw_interface (vnm, hw_if_index);
1201 return clib_error_return (0,
1202 "unable to find hw interface for index %u",
1206 old_name = hw->name;
1208 /* set new hw->name */
1209 hw->name = format (0, "%s", new_name);
1211 /* remove the old name to hw_if_index mapping and install the new one */
1212 hash_unset_mem (im->hw_interface_by_name, old_name);
1213 hash_set_mem (im->hw_interface_by_name, hw->name, hw_if_index);
1215 /* rename tx/output nodes */
1216 vlib_node_rename (vm, hw->tx_node_index, "%v-tx", hw->name);
1217 vlib_node_rename (vm, hw->output_node_index, "%v-output", hw->name);
1219 /* free the old name vector */
1220 vec_free (old_name);
1226 * fd.io coding-style-patch-verification: ON
1229 * eval: (c-set-style "gnu")