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>
42 #include <vnet/fib/ip6_fib.h>
44 #define VNET_INTERFACE_SET_FLAGS_HELPER_IS_CREATE (1 << 0)
45 #define VNET_INTERFACE_SET_FLAGS_HELPER_WANT_REDISTRIBUTE (1 << 1)
47 static clib_error_t *vnet_hw_interface_set_flags_helper (vnet_main_t * vnm,
52 static clib_error_t *vnet_sw_interface_set_flags_helper (vnet_main_t * vnm,
57 static clib_error_t *vnet_hw_interface_set_class_helper (vnet_main_t * vnm,
64 /* Either sw or hw interface index. */
69 } vnet_sw_hw_interface_state_t;
72 serialize_vec_vnet_sw_hw_interface_state (serialize_main_t * m, va_list * va)
74 vnet_sw_hw_interface_state_t *s =
75 va_arg (*va, vnet_sw_hw_interface_state_t *);
76 u32 n = va_arg (*va, u32);
78 for (i = 0; i < n; i++)
80 serialize_integer (m, s[i].sw_hw_if_index,
81 sizeof (s[i].sw_hw_if_index));
82 serialize_integer (m, s[i].flags, sizeof (s[i].flags));
87 unserialize_vec_vnet_sw_hw_interface_state (serialize_main_t * m,
90 vnet_sw_hw_interface_state_t *s =
91 va_arg (*va, vnet_sw_hw_interface_state_t *);
92 u32 n = va_arg (*va, u32);
94 for (i = 0; i < n; i++)
96 unserialize_integer (m, &s[i].sw_hw_if_index,
97 sizeof (s[i].sw_hw_if_index));
98 unserialize_integer (m, &s[i].flags, sizeof (s[i].flags));
103 serialize_vnet_sw_hw_interface_set_flags (serialize_main_t * m, va_list * va)
105 vnet_sw_hw_interface_state_t *s =
106 va_arg (*va, vnet_sw_hw_interface_state_t *);
107 serialize (m, serialize_vec_vnet_sw_hw_interface_state, s, 1);
111 unserialize_vnet_sw_interface_set_flags (serialize_main_t * m, va_list * va)
113 CLIB_UNUSED (mc_main_t * mc) = va_arg (*va, mc_main_t *);
114 vnet_sw_hw_interface_state_t s;
116 unserialize (m, unserialize_vec_vnet_sw_hw_interface_state, &s, 1);
118 vnet_sw_interface_set_flags_helper
119 (vnet_get_main (), s.sw_hw_if_index, s.flags,
120 /* helper_flags no redistribution */ 0);
124 unserialize_vnet_hw_interface_set_flags (serialize_main_t * m, va_list * va)
126 CLIB_UNUSED (mc_main_t * mc) = va_arg (*va, mc_main_t *);
127 vnet_sw_hw_interface_state_t s;
129 unserialize (m, unserialize_vec_vnet_sw_hw_interface_state, &s, 1);
131 vnet_hw_interface_set_flags_helper
132 (vnet_get_main (), s.sw_hw_if_index, s.flags,
133 /* helper_flags no redistribution */ 0);
136 MC_SERIALIZE_MSG (vnet_sw_interface_set_flags_msg, static) =
138 .name = "vnet_sw_interface_set_flags",.serialize =
139 serialize_vnet_sw_hw_interface_set_flags,.unserialize =
140 unserialize_vnet_sw_interface_set_flags,};
142 MC_SERIALIZE_MSG (vnet_hw_interface_set_flags_msg, static) =
144 .name = "vnet_hw_interface_set_flags",.serialize =
145 serialize_vnet_sw_hw_interface_set_flags,.unserialize =
146 unserialize_vnet_hw_interface_set_flags,};
149 serialize_vnet_interface_state (serialize_main_t * m, va_list * va)
151 vnet_main_t *vnm = va_arg (*va, vnet_main_t *);
152 vnet_sw_hw_interface_state_t *sts = 0, *st;
153 vnet_sw_interface_t *sif;
154 vnet_hw_interface_t *hif;
155 vnet_interface_main_t *im = &vnm->interface_main;
157 /* Serialize hardware interface classes since they may have changed.
158 Must do this before sending up/down flags. */
160 pool_foreach (hif, im->hw_interfaces, ({
161 vnet_hw_interface_class_t * hw_class = vnet_get_hw_interface_class (vnm, hif->hw_class_index);
162 serialize_cstring (m, hw_class->name);
166 /* Send sw/hw interface state when non-zero. */
168 pool_foreach (sif, im->sw_interfaces, ({
171 vec_add2 (sts, st, 1);
172 st->sw_hw_if_index = sif->sw_if_index;
173 st->flags = sif->flags;
178 vec_serialize (m, sts, serialize_vec_vnet_sw_hw_interface_state);
184 pool_foreach (hif, im->hw_interfaces, ({
187 vec_add2 (sts, st, 1);
188 st->sw_hw_if_index = hif->hw_if_index;
189 st->flags = hif->flags;
194 vec_serialize (m, sts, serialize_vec_vnet_sw_hw_interface_state);
200 unserialize_vnet_interface_state (serialize_main_t * m, va_list * va)
202 vnet_main_t *vnm = va_arg (*va, vnet_main_t *);
203 vnet_sw_hw_interface_state_t *sts = 0, *st;
205 /* First set interface hardware class. */
207 vnet_interface_main_t *im = &vnm->interface_main;
208 vnet_hw_interface_t *hif;
214 pool_foreach (hif, im->hw_interfaces, ({
215 unserialize_cstring (m, &class_name);
216 p = hash_get_mem (im->hw_interface_class_by_name, class_name);
218 error = vnet_hw_interface_set_class_helper (vnm, hif->hw_if_index, p[0], /* redistribute */ 0);
220 clib_error_report (error);
221 vec_free (class_name);
226 vec_unserialize (m, &sts, unserialize_vec_vnet_sw_hw_interface_state);
227 vec_foreach (st, sts)
228 vnet_sw_interface_set_flags_helper (vnm, st->sw_hw_if_index, st->flags,
229 /* no distribute */ 0);
232 vec_unserialize (m, &sts, unserialize_vec_vnet_sw_hw_interface_state);
233 vec_foreach (st, sts)
234 vnet_hw_interface_set_flags_helper (vnm, st->sw_hw_if_index, st->flags,
235 /* no distribute */ 0);
239 static clib_error_t *
240 call_elf_section_interface_callbacks (vnet_main_t * vnm, u32 if_index,
242 _vnet_interface_function_list_elt_t *
245 clib_error_t *error = 0;
249 error = elt->fp (vnm, if_index, flags);
252 elt = elt->next_interface_function;
257 static clib_error_t *
258 call_hw_interface_add_del_callbacks (vnet_main_t * vnm, u32 hw_if_index,
261 vnet_hw_interface_t *hi = vnet_get_hw_interface (vnm, hw_if_index);
262 vnet_hw_interface_class_t *hw_class =
263 vnet_get_hw_interface_class (vnm, hi->hw_class_index);
264 vnet_device_class_t *dev_class =
265 vnet_get_device_class (vnm, hi->dev_class_index);
266 clib_error_t *error = 0;
268 if (hw_class->interface_add_del_function
270 hw_class->interface_add_del_function (vnm, hw_if_index, is_create)))
273 if (dev_class->interface_add_del_function
275 dev_class->interface_add_del_function (vnm, hw_if_index,
279 error = call_elf_section_interface_callbacks
280 (vnm, hw_if_index, is_create, vnm->hw_interface_add_del_functions);
285 static clib_error_t *
286 call_sw_interface_add_del_callbacks (vnet_main_t * vnm, u32 sw_if_index,
289 return call_elf_section_interface_callbacks
290 (vnm, sw_if_index, is_create, vnm->sw_interface_add_del_functions);
293 #define VNET_INTERFACE_SET_FLAGS_HELPER_IS_CREATE (1 << 0)
294 #define VNET_INTERFACE_SET_FLAGS_HELPER_WANT_REDISTRIBUTE (1 << 1)
296 static clib_error_t *
297 vnet_hw_interface_set_flags_helper (vnet_main_t * vnm, u32 hw_if_index,
298 u32 flags, u32 helper_flags)
300 vnet_hw_interface_t *hi = vnet_get_hw_interface (vnm, hw_if_index);
301 vnet_hw_interface_class_t *hw_class =
302 vnet_get_hw_interface_class (vnm, hi->hw_class_index);
303 vnet_device_class_t *dev_class =
304 vnet_get_device_class (vnm, hi->dev_class_index);
305 vlib_main_t *vm = vnm->vlib_main;
307 clib_error_t *error = 0;
309 (helper_flags & VNET_INTERFACE_SET_FLAGS_HELPER_IS_CREATE) != 0;
312 (VNET_HW_INTERFACE_FLAG_LINK_UP | VNET_HW_INTERFACE_FLAG_DUPLEX_MASK |
313 VNET_HW_INTERFACE_FLAG_SPEED_MASK);
316 /* Call hardware interface add/del callbacks. */
318 call_hw_interface_add_del_callbacks (vnm, hw_if_index, is_create);
320 /* Already in the desired state? */
321 if (!is_create && (hi->flags & mask) == flags)
324 /* Some interface classes do not redistribute (e.g. are local). */
325 if (!dev_class->redistribute)
326 helper_flags &= ~VNET_INTERFACE_SET_FLAGS_HELPER_WANT_REDISTRIBUTE;
329 && (helper_flags & VNET_INTERFACE_SET_FLAGS_HELPER_WANT_REDISTRIBUTE))
331 vnet_sw_hw_interface_state_t s;
332 s.sw_hw_if_index = hw_if_index;
334 mc_serialize (vm->mc_main, &vnet_hw_interface_set_flags_msg, &s);
337 if ((hi->flags & VNET_HW_INTERFACE_FLAG_LINK_UP) !=
338 (flags & VNET_HW_INTERFACE_FLAG_LINK_UP))
340 /* Do hardware class (e.g. ethernet). */
341 if (hw_class->link_up_down_function
342 && (error = hw_class->link_up_down_function (vnm, hw_if_index,
346 error = call_elf_section_interface_callbacks
347 (vnm, hw_if_index, flags, 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 /* set the flags now before invoking the registered clients
454 * so that the state they query is consistent with the state here notified */
455 old_flags = si->flags;
458 if ((flags | old_flags) & VNET_SW_INTERFACE_FLAG_ADMIN_UP)
459 error = call_elf_section_interface_callbacks
460 (vnm, sw_if_index, flags,
461 vnm->sw_interface_admin_up_down_functions);
462 si->flags = old_flags;
467 if (si->type == VNET_SW_INTERFACE_TYPE_HARDWARE)
469 vnet_hw_interface_t *hi =
470 vnet_get_hw_interface (vnm, si->hw_if_index);
471 vnet_hw_interface_class_t *hw_class =
472 vnet_get_hw_interface_class (vnm, hi->hw_class_index);
473 vnet_device_class_t *dev_class =
474 vnet_get_device_class (vnm, hi->dev_class_index);
476 /* save the si admin up flag */
477 old_flags = si->flags;
479 /* update si admin up flag in advance if we are going admin down */
480 if (!(flags & VNET_SW_INTERFACE_FLAG_ADMIN_UP))
481 si->flags &= ~VNET_SW_INTERFACE_FLAG_ADMIN_UP;
483 if (dev_class->admin_up_down_function
484 && (error = dev_class->admin_up_down_function (vnm,
488 /* restore si admin up flag to it's original state on errors */
489 si->flags = old_flags;
493 if (hw_class->admin_up_down_function
494 && (error = hw_class->admin_up_down_function (vnm,
498 /* restore si admin up flag to it's original state on errors */
499 si->flags = old_flags;
503 /* Admin down implies link down. */
504 if (!(flags & VNET_SW_INTERFACE_FLAG_ADMIN_UP)
505 && (hi->flags & VNET_HW_INTERFACE_FLAG_LINK_UP))
506 vnet_hw_interface_set_flags_helper (vnm, si->hw_if_index,
508 ~VNET_HW_INTERFACE_FLAG_LINK_UP,
521 vnet_hw_interface_set_flags (vnet_main_t * vnm, u32 hw_if_index, u32 flags)
523 return vnet_hw_interface_set_flags_helper
524 (vnm, hw_if_index, flags,
525 VNET_INTERFACE_SET_FLAGS_HELPER_WANT_REDISTRIBUTE);
529 vnet_sw_interface_set_flags (vnet_main_t * vnm, u32 sw_if_index, u32 flags)
531 return vnet_sw_interface_set_flags_helper
532 (vnm, sw_if_index, flags,
533 VNET_INTERFACE_SET_FLAGS_HELPER_WANT_REDISTRIBUTE);
537 vnet_create_sw_interface_no_callbacks (vnet_main_t * vnm,
538 vnet_sw_interface_t * template)
540 vnet_interface_main_t *im = &vnm->interface_main;
541 vnet_sw_interface_t *sw;
544 pool_get (im->sw_interfaces, sw);
545 sw_if_index = sw - im->sw_interfaces;
550 sw->sw_if_index = sw_if_index;
551 if (sw->type == VNET_SW_INTERFACE_TYPE_HARDWARE)
552 sw->sup_sw_if_index = sw->sw_if_index;
554 /* Allocate counters for this interface. */
558 vnet_interface_counter_lock (im);
560 for (i = 0; i < vec_len (im->sw_if_counters); i++)
562 vlib_validate_simple_counter (&im->sw_if_counters[i], sw_if_index);
563 vlib_zero_simple_counter (&im->sw_if_counters[i], sw_if_index);
566 for (i = 0; i < vec_len (im->combined_sw_if_counters); i++)
568 vlib_validate_combined_counter (&im->combined_sw_if_counters[i],
570 vlib_zero_combined_counter (&im->combined_sw_if_counters[i],
574 vnet_interface_counter_unlock (im);
581 vnet_create_sw_interface (vnet_main_t * vnm, vnet_sw_interface_t * template,
585 vnet_hw_interface_t *hi;
586 vnet_device_class_t *dev_class;
588 hi = vnet_get_sup_hw_interface (vnm, template->sup_sw_if_index);
589 dev_class = vnet_get_device_class (vnm, hi->dev_class_index);
591 if (template->type == VNET_SW_INTERFACE_TYPE_SUB &&
592 dev_class->subif_add_del_function)
594 error = dev_class->subif_add_del_function (vnm, hi->hw_if_index,
595 (struct vnet_sw_interface_t
601 *sw_if_index = vnet_create_sw_interface_no_callbacks (vnm, template);
602 error = vnet_sw_interface_set_flags_helper
603 (vnm, *sw_if_index, template->flags,
604 VNET_INTERFACE_SET_FLAGS_HELPER_IS_CREATE);
608 /* undo the work done by vnet_create_sw_interface_no_callbacks() */
609 vnet_interface_main_t *im = &vnm->interface_main;
610 vnet_sw_interface_t *sw =
611 pool_elt_at_index (im->sw_interfaces, *sw_if_index);
612 pool_put (im->sw_interfaces, sw);
619 vnet_delete_sw_interface (vnet_main_t * vnm, u32 sw_if_index)
621 vnet_interface_main_t *im = &vnm->interface_main;
622 vnet_sw_interface_t *sw =
623 pool_elt_at_index (im->sw_interfaces, sw_if_index);
625 /* Bring down interface in case it is up. */
627 vnet_sw_interface_set_flags (vnm, sw_if_index, /* flags */ 0);
629 call_sw_interface_add_del_callbacks (vnm, sw_if_index, /* is_create */ 0);
631 pool_put (im->sw_interfaces, sw);
635 setup_tx_node (vlib_main_t * vm,
636 u32 node_index, vnet_device_class_t * dev_class)
638 vlib_node_t *n = vlib_get_node (vm, node_index);
640 n->function = dev_class->tx_function;
641 n->format_trace = dev_class->format_tx_trace;
642 vlib_register_errors (vm, node_index,
643 dev_class->tx_function_n_errors,
644 dev_class->tx_function_error_strings);
648 setup_output_node (vlib_main_t * vm,
649 u32 node_index, vnet_hw_interface_class_t * hw_class)
651 vlib_node_t *n = vlib_get_node (vm, node_index);
652 n->format_buffer = hw_class->format_header;
653 n->unformat_buffer = hw_class->unformat_header;
656 /* Register an interface instance. */
658 vnet_register_interface (vnet_main_t * vnm,
661 u32 hw_class_index, u32 hw_instance)
663 vnet_interface_main_t *im = &vnm->interface_main;
664 vnet_hw_interface_t *hw;
665 vnet_device_class_t *dev_class =
666 vnet_get_device_class (vnm, dev_class_index);
667 vnet_hw_interface_class_t *hw_class =
668 vnet_get_hw_interface_class (vnm, hw_class_index);
669 vlib_main_t *vm = vnm->vlib_main;
671 char *tx_node_name, *output_node_name;
673 pool_get (im->hw_interfaces, hw);
675 hw_index = hw - im->hw_interfaces;
676 hw->hw_if_index = hw_index;
678 if (dev_class->format_device_name)
679 hw->name = format (0, "%U", dev_class->format_device_name, dev_instance);
680 else if (hw_class->format_interface_name)
681 hw->name = format (0, "%U", hw_class->format_interface_name,
684 hw->name = format (0, "%s%x", hw_class->name, dev_instance);
686 if (!im->hw_interface_by_name)
687 im->hw_interface_by_name = hash_create_vec ( /* size */ 0,
688 sizeof (hw->name[0]),
691 hash_set_mem (im->hw_interface_by_name, hw->name, hw_index);
693 /* Make hardware interface point to software interface. */
695 vnet_sw_interface_t sw;
697 memset (&sw, 0, sizeof (sw));
698 sw.type = VNET_SW_INTERFACE_TYPE_HARDWARE;
699 sw.hw_if_index = hw_index;
700 hw->sw_if_index = vnet_create_sw_interface_no_callbacks (vnm, &sw);
703 hw->dev_class_index = dev_class_index;
704 hw->dev_instance = dev_instance;
705 hw->hw_class_index = hw_class_index;
706 hw->hw_instance = hw_instance;
708 hw->max_rate_bits_per_sec = 0;
709 hw->min_packet_bytes = 0;
710 hw->per_packet_overhead_bytes = 0;
711 hw->max_l3_packet_bytes[VLIB_RX] = ~0;
712 hw->max_l3_packet_bytes[VLIB_TX] = ~0;
714 tx_node_name = (char *) format (0, "%v-tx", hw->name);
715 output_node_name = (char *) format (0, "%v-output", hw->name);
717 /* If we have previously deleted interface nodes, re-use them. */
718 if (vec_len (im->deleted_hw_interface_nodes) > 0)
720 vnet_hw_interface_nodes_t *hn;
721 vnet_interface_output_runtime_t *rt;
723 hn = vec_end (im->deleted_hw_interface_nodes) - 1;
725 hw->tx_node_index = hn->tx_node_index;
726 hw->output_node_index = hn->output_node_index;
728 vlib_node_rename (vm, hw->tx_node_index, "%v", tx_node_name);
729 vlib_node_rename (vm, hw->output_node_index, "%v", output_node_name);
731 rt = vlib_node_get_runtime_data (vm, hw->output_node_index);
732 ASSERT (rt->is_deleted == 1);
734 rt->hw_if_index = hw_index;
735 rt->sw_if_index = hw->sw_if_index;
736 rt->dev_instance = hw->dev_instance;
738 rt = vlib_node_get_runtime_data (vm, hw->tx_node_index);
739 rt->hw_if_index = hw_index;
740 rt->sw_if_index = hw->sw_if_index;
741 rt->dev_instance = hw->dev_instance;
743 vlib_worker_thread_node_runtime_update ();
744 _vec_len (im->deleted_hw_interface_nodes) -= 1;
748 vlib_node_registration_t r;
749 vnet_interface_output_runtime_t rt = {
750 .hw_if_index = hw_index,
751 .sw_if_index = hw->sw_if_index,
752 .dev_instance = hw->dev_instance,
756 memset (&r, 0, sizeof (r));
757 r.type = VLIB_NODE_TYPE_INTERNAL;
758 r.runtime_data = &rt;
759 r.runtime_data_bytes = sizeof (rt);
761 r.vector_size = sizeof (u32);
763 r.flags = VLIB_NODE_FLAG_IS_OUTPUT;
764 r.name = tx_node_name;
765 r.function = dev_class->tx_function;
767 hw->tx_node_index = vlib_register_node (vm, &r);
769 vlib_node_add_named_next_with_slot (vm, hw->tx_node_index,
771 VNET_INTERFACE_TX_NEXT_DROP);
774 r.name = output_node_name;
775 r.function = dev_class->no_flatten_output_chains ?
776 vnet_interface_output_node_no_flatten_multiarch_select () :
777 vnet_interface_output_node_multiarch_select ();
778 r.format_trace = format_vnet_interface_output_trace;
783 "interface is deleted",
786 r.n_errors = ARRAY_LEN (e);
789 hw->output_node_index = vlib_register_node (vm, &r);
791 #define _(sym,str) vlib_node_add_named_next_with_slot (vm, \
792 hw->output_node_index, str, \
793 VNET_INTERFACE_OUTPUT_NEXT_##sym);
794 foreach_intf_output_feat
796 vlib_node_add_named_next_with_slot (vm, hw->output_node_index,
798 VNET_INTERFACE_OUTPUT_NEXT_DROP);
799 vlib_node_add_next_with_slot (vm, hw->output_node_index,
801 VNET_INTERFACE_OUTPUT_NEXT_TX);
804 setup_output_node (vm, hw->output_node_index, hw_class);
805 setup_tx_node (vm, hw->tx_node_index, dev_class);
807 /* Call all up/down callbacks with zero flags when interface is created. */
808 vnet_sw_interface_set_flags_helper (vnm, hw->sw_if_index, /* flags */ 0,
809 VNET_INTERFACE_SET_FLAGS_HELPER_IS_CREATE);
810 vnet_hw_interface_set_flags_helper (vnm, hw_index, /* flags */ 0,
811 VNET_INTERFACE_SET_FLAGS_HELPER_IS_CREATE);
817 vnet_delete_hw_interface (vnet_main_t * vnm, u32 hw_if_index)
819 vnet_interface_main_t *im = &vnm->interface_main;
820 vnet_hw_interface_t *hw = vnet_get_hw_interface (vnm, hw_if_index);
821 vlib_main_t *vm = vnm->vlib_main;
823 /* If it is up, mark it down. */
825 vnet_hw_interface_set_flags (vnm, hw_if_index, /* flags */ 0);
827 /* Call delete callbacks. */
828 call_hw_interface_add_del_callbacks (vnm, hw_if_index, /* is_create */ 0);
830 /* Delete software interface corresponding to hardware interface. */
831 vnet_delete_sw_interface (vnm, hw->sw_if_index);
833 /* Delete any sub-interfaces. */
837 hash_foreach (id, sw_if_index, hw->sub_interface_sw_if_index_by_id, ({
838 vnet_delete_sw_interface (vnm, sw_if_index);
844 vnet_hw_interface_nodes_t *dn;
845 vnet_interface_output_runtime_t *rt =
846 vlib_node_get_runtime_data (vm, hw->output_node_index);
848 /* Mark node runtime as deleted so output node (if called) will drop packets. */
851 vlib_node_rename (vm, hw->output_node_index,
852 "interface-%d-output-deleted", hw_if_index);
853 vlib_node_rename (vm, hw->tx_node_index, "interface-%d-tx-deleted",
855 vec_add2 (im->deleted_hw_interface_nodes, dn, 1);
856 dn->tx_node_index = hw->tx_node_index;
857 dn->output_node_index = hw->output_node_index;
860 hash_unset_mem (im->hw_interface_by_name, hw->name);
863 pool_put (im->hw_interfaces, hw);
867 serialize_vnet_hw_interface_set_class (serialize_main_t * m, va_list * va)
869 u32 hw_if_index = va_arg (*va, u32);
870 char *hw_class_name = va_arg (*va, char *);
871 serialize_integer (m, hw_if_index, sizeof (hw_if_index));
872 serialize_cstring (m, hw_class_name);
876 unserialize_vnet_hw_interface_set_class (serialize_main_t * m, va_list * va)
878 CLIB_UNUSED (mc_main_t * mc) = va_arg (*va, mc_main_t *);
879 vnet_main_t *vnm = vnet_get_main ();
885 unserialize_integer (m, &hw_if_index, sizeof (hw_if_index));
886 unserialize_cstring (m, &hw_class_name);
888 hash_get (vnm->interface_main.hw_interface_class_by_name, hw_class_name);
890 error = vnet_hw_interface_set_class_helper (vnm, hw_if_index, p[0],
891 /* redistribute */ 0);
893 clib_error_report (error);
896 MC_SERIALIZE_MSG (vnet_hw_interface_set_class_msg, static) =
898 .name = "vnet_hw_interface_set_class",.serialize =
899 serialize_vnet_hw_interface_set_class,.unserialize =
900 unserialize_vnet_hw_interface_set_class,};
903 vnet_hw_interface_init_for_class (vnet_main_t * vnm, u32 hw_if_index,
904 u32 hw_class_index, u32 hw_instance)
906 vnet_hw_interface_t *hi = vnet_get_hw_interface (vnm, hw_if_index);
907 vnet_hw_interface_class_t *hc =
908 vnet_get_hw_interface_class (vnm, hw_class_index);
910 hi->hw_class_index = hw_class_index;
911 hi->hw_instance = hw_instance;
912 setup_output_node (vnm->vlib_main, hi->output_node_index, hc);
915 static clib_error_t *
916 vnet_hw_interface_set_class_helper (vnet_main_t * vnm, u32 hw_if_index,
917 u32 hw_class_index, u32 redistribute)
919 vnet_hw_interface_t *hi = vnet_get_hw_interface (vnm, hw_if_index);
920 vnet_sw_interface_t *si = vnet_get_sw_interface (vnm, hi->sw_if_index);
921 vnet_hw_interface_class_t *old_class =
922 vnet_get_hw_interface_class (vnm, hi->hw_class_index);
923 vnet_hw_interface_class_t *new_class =
924 vnet_get_hw_interface_class (vnm, hw_class_index);
925 vnet_device_class_t *dev_class =
926 vnet_get_device_class (vnm, hi->dev_class_index);
927 clib_error_t *error = 0;
929 /* New class equals old class? Nothing to do. */
930 if (hi->hw_class_index == hw_class_index)
933 /* No need (and incorrect since admin up flag may be set) to do error checking when
934 receiving unserialize message. */
937 if (si->flags & VNET_SW_INTERFACE_FLAG_ADMIN_UP)
938 return clib_error_return (0,
939 "%v must be admin down to change class from %s to %s",
940 hi->name, old_class->name, new_class->name);
942 /* Make sure interface supports given class. */
943 if ((new_class->is_valid_class_for_interface
944 && !new_class->is_valid_class_for_interface (vnm, hw_if_index,
946 || (dev_class->is_valid_class_for_interface
947 && !dev_class->is_valid_class_for_interface (vnm, hw_if_index,
949 return clib_error_return (0,
950 "%v class cannot be changed from %s to %s",
951 hi->name, old_class->name, new_class->name);
953 if (vnm->vlib_main->mc_main)
955 mc_serialize (vnm->vlib_main->mc_main,
956 &vnet_hw_interface_set_class_msg, hw_if_index,
962 if (old_class->hw_class_change)
963 old_class->hw_class_change (vnm, hw_if_index, old_class->index,
966 vnet_hw_interface_init_for_class (vnm, hw_if_index, new_class->index,
969 if (new_class->hw_class_change)
970 new_class->hw_class_change (vnm, hw_if_index, old_class->index,
973 if (dev_class->hw_class_change)
974 dev_class->hw_class_change (vnm, hw_if_index, new_class->index);
980 vnet_hw_interface_set_class (vnet_main_t * vnm, u32 hw_if_index,
983 return vnet_hw_interface_set_class_helper (vnm, hw_if_index, hw_class_index,
984 /* redistribute */ 1);
988 vnet_hw_interface_rx_redirect_to_node_helper (vnet_main_t * vnm,
993 vnet_hw_interface_t *hi = vnet_get_hw_interface (vnm, hw_if_index);
994 vnet_device_class_t *dev_class = vnet_get_device_class
995 (vnm, hi->dev_class_index);
999 /* $$$$ fixme someday maybe */
1000 ASSERT (vnm->vlib_main->mc_main == 0);
1002 if (dev_class->rx_redirect_to_node)
1004 dev_class->rx_redirect_to_node (vnm, hw_if_index, node_index);
1008 return VNET_API_ERROR_UNIMPLEMENTED;
1012 vnet_hw_interface_rx_redirect_to_node (vnet_main_t * vnm, u32 hw_if_index,
1015 return vnet_hw_interface_rx_redirect_to_node_helper (vnm, hw_if_index,
1017 1 /* redistribute */ );
1021 vnet_sw_interface_compare (vnet_main_t * vnm,
1022 uword sw_if_index0, uword sw_if_index1)
1024 vnet_sw_interface_t *sup0 = vnet_get_sup_sw_interface (vnm, sw_if_index0);
1025 vnet_sw_interface_t *sup1 = vnet_get_sup_sw_interface (vnm, sw_if_index1);
1026 vnet_hw_interface_t *h0 = vnet_get_hw_interface (vnm, sup0->hw_if_index);
1027 vnet_hw_interface_t *h1 = vnet_get_hw_interface (vnm, sup1->hw_if_index);
1030 return vec_cmp (h0->name, h1->name);
1031 return (word) h0->hw_instance - (word) h1->hw_instance;
1035 vnet_hw_interface_compare (vnet_main_t * vnm,
1036 uword hw_if_index0, uword hw_if_index1)
1038 vnet_hw_interface_t *h0 = vnet_get_hw_interface (vnm, hw_if_index0);
1039 vnet_hw_interface_t *h1 = vnet_get_hw_interface (vnm, hw_if_index1);
1042 return vec_cmp (h0->name, h1->name);
1043 return (word) h0->hw_instance - (word) h1->hw_instance;
1047 vnet_interface_init (vlib_main_t * vm)
1049 vnet_main_t *vnm = vnet_get_main ();
1050 vnet_interface_main_t *im = &vnm->interface_main;
1051 vlib_buffer_t *b = 0;
1052 vnet_buffer_opaque_t *o = 0;
1055 * Keep people from shooting themselves in the foot.
1057 if (sizeof (b->opaque) != sizeof (vnet_buffer_opaque_t))
1059 #define _(a) if (sizeof(o->a) > sizeof (o->unused)) \
1061 ("FATAL: size of opaque union subtype %s is %d (max %d)", \
1062 #a, sizeof(o->a), sizeof (o->unused));
1063 foreach_buffer_opaque_union_subtype;
1066 return clib_error_return
1067 (0, "FATAL: size of vlib buffer opaque %d, size of vnet opaque %d",
1068 sizeof (b->opaque), sizeof (vnet_buffer_opaque_t));
1071 im->sw_if_counter_lock = clib_mem_alloc_aligned (CLIB_CACHE_LINE_BYTES,
1072 CLIB_CACHE_LINE_BYTES);
1073 im->sw_if_counter_lock[0] = 1; /* should be no need */
1075 vec_validate (im->sw_if_counters, VNET_N_SIMPLE_INTERFACE_COUNTER - 1);
1076 im->sw_if_counters[VNET_INTERFACE_COUNTER_DROP].name = "drops";
1077 im->sw_if_counters[VNET_INTERFACE_COUNTER_PUNT].name = "punts";
1078 im->sw_if_counters[VNET_INTERFACE_COUNTER_IP4].name = "ip4";
1079 im->sw_if_counters[VNET_INTERFACE_COUNTER_IP6].name = "ip6";
1080 im->sw_if_counters[VNET_INTERFACE_COUNTER_RX_NO_BUF].name = "rx-no-buf";
1081 im->sw_if_counters[VNET_INTERFACE_COUNTER_RX_MISS].name = "rx-miss";
1082 im->sw_if_counters[VNET_INTERFACE_COUNTER_RX_ERROR].name = "rx-error";
1083 im->sw_if_counters[VNET_INTERFACE_COUNTER_TX_ERROR].name = "tx-error";
1085 vec_validate (im->combined_sw_if_counters,
1086 VNET_N_COMBINED_INTERFACE_COUNTER - 1);
1087 im->combined_sw_if_counters[VNET_INTERFACE_COUNTER_RX].name = "rx";
1088 im->combined_sw_if_counters[VNET_INTERFACE_COUNTER_TX].name = "tx";
1090 im->sw_if_counter_lock[0] = 0;
1092 im->device_class_by_name = hash_create_string ( /* size */ 0,
1095 vnet_device_class_t *c;
1097 c = vnm->device_class_registrations;
1101 c->index = vec_len (im->device_classes);
1102 hash_set_mem (im->device_class_by_name, c->name, c->index);
1103 vec_add1 (im->device_classes, c[0]);
1104 c = c->next_class_registration;
1108 im->hw_interface_class_by_name = hash_create_string ( /* size */ 0,
1111 im->sw_if_index_by_sup_and_sub = hash_create_mem (0, sizeof (u64),
1114 vnet_hw_interface_class_t *c;
1116 c = vnm->hw_interface_class_registrations;
1120 c->index = vec_len (im->hw_interface_classes);
1121 hash_set_mem (im->hw_interface_class_by_name, c->name, c->index);
1122 vec_add1 (im->hw_interface_classes, c[0]);
1123 c = c->next_class_registration;
1128 clib_error_t *error;
1130 if ((error = vlib_call_init_function (vm, vnet_interface_cli_init)))
1137 VLIB_INIT_FUNCTION (vnet_interface_init);
1139 /* Kludge to renumber interface names [only!] */
1141 vnet_interface_name_renumber (u32 sw_if_index, u32 new_show_dev_instance)
1144 vnet_main_t *vnm = vnet_get_main ();
1145 vnet_interface_main_t *im = &vnm->interface_main;
1146 vnet_hw_interface_t *hi = vnet_get_sup_hw_interface (vnm, sw_if_index);
1148 vnet_device_class_t *dev_class = vnet_get_device_class
1149 (vnm, hi->dev_class_index);
1151 if (dev_class->name_renumber == 0 || dev_class->format_device_name == 0)
1152 return VNET_API_ERROR_UNIMPLEMENTED;
1154 rv = dev_class->name_renumber (hi, new_show_dev_instance);
1159 hash_unset_mem (im->hw_interface_by_name, hi->name);
1160 vec_free (hi->name);
1161 /* Use the mapping we set up to call it Ishmael */
1162 hi->name = format (0, "%U", dev_class->format_device_name,
1165 hash_set_mem (im->hw_interface_by_name, hi->name, hi->hw_if_index);
1170 vnet_interface_add_del_feature (vnet_main_t * vnm,
1173 intf_output_feat_t feature, int is_add)
1175 vnet_sw_interface_t *sw;
1177 sw = vnet_get_sw_interface (vnm, sw_if_index);
1182 sw->output_feature_bitmap |= (1 << feature);
1183 sw->output_feature_bitmap |= (1 << INTF_OUTPUT_FEAT_DONE);
1189 sw->output_feature_bitmap &= ~(1 << feature);
1190 if (sw->output_feature_bitmap == (1 << INTF_OUTPUT_FEAT_DONE))
1191 sw->output_feature_bitmap = 0;
1198 vnet_rename_interface (vnet_main_t * vnm, u32 hw_if_index, char *new_name)
1200 vnet_interface_main_t *im = &vnm->interface_main;
1201 vlib_main_t *vm = vnm->vlib_main;
1202 vnet_hw_interface_t *hw;
1204 clib_error_t *error = 0;
1206 hw = vnet_get_hw_interface (vnm, hw_if_index);
1209 return clib_error_return (0,
1210 "unable to find hw interface for index %u",
1214 old_name = hw->name;
1216 /* set new hw->name */
1217 hw->name = format (0, "%s", new_name);
1219 /* remove the old name to hw_if_index mapping and install the new one */
1220 hash_unset_mem (im->hw_interface_by_name, old_name);
1221 hash_set_mem (im->hw_interface_by_name, hw->name, hw_if_index);
1223 /* rename tx/output nodes */
1224 vlib_node_rename (vm, hw->tx_node_index, "%v-tx", hw->name);
1225 vlib_node_rename (vm, hw->output_node_index, "%v-output", hw->name);
1227 /* free the old name vector */
1228 vec_free (old_name);
1233 static clib_error_t *
1234 vnet_hw_interface_change_mac_address_helper (vnet_main_t * vnm,
1235 u32 hw_if_index, u64 mac_address)
1237 clib_error_t *error = 0;
1238 vnet_hw_interface_t *hi = vnet_get_hw_interface (vnm, hw_if_index);
1242 vnet_device_class_t *dev_class =
1243 vnet_get_device_class (vnm, hi->dev_class_index);
1244 if (dev_class->mac_addr_change_function)
1247 dev_class->mac_addr_change_function (vnet_get_hw_interface
1249 (char *) &mac_address);
1253 ethernet_main_t *em = ðernet_main;
1254 ethernet_interface_t *ei =
1255 pool_elt_at_index (em->interfaces, hi->hw_instance);
1257 clib_memcpy (hi->hw_address, (u8 *) & mac_address,
1258 sizeof (hi->hw_address));
1259 clib_memcpy (ei->address, (u8 *) & mac_address,
1260 sizeof (ei->address));
1261 ethernet_arp_change_mac (vnm, hw_if_index);
1262 ethernet_ndp_change_mac (vnm->vlib_main, hw_if_index);
1267 clib_error_return (0,
1268 "MAC Address Change is not supported on this interface");
1274 clib_error_return (0,
1275 "mac address change is not supported for interface index %u",
1282 vnet_hw_interface_change_mac_address (vnet_main_t * vnm, u32 hw_if_index,
1285 return vnet_hw_interface_change_mac_address_helper
1286 (vnm, hw_if_index, mac_address);
1290 * fd.io coding-style-patch-verification: ON
1293 * eval: (c-set-style "gnu")