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;
643 vlib_register_errors (vm, node_index,
644 dev_class->tx_function_n_errors,
645 dev_class->tx_function_error_strings);
649 setup_output_node (vlib_main_t * vm,
650 u32 node_index, vnet_hw_interface_class_t * hw_class)
652 vlib_node_t *n = vlib_get_node (vm, node_index);
653 n->format_buffer = hw_class->format_header;
654 n->unformat_buffer = hw_class->unformat_header;
657 /* Register an interface instance. */
659 vnet_register_interface (vnet_main_t * vnm,
662 u32 hw_class_index, u32 hw_instance)
664 vnet_interface_main_t *im = &vnm->interface_main;
665 vnet_hw_interface_t *hw;
666 vnet_device_class_t *dev_class =
667 vnet_get_device_class (vnm, dev_class_index);
668 vnet_hw_interface_class_t *hw_class =
669 vnet_get_hw_interface_class (vnm, hw_class_index);
670 vlib_main_t *vm = vnm->vlib_main;
672 char *tx_node_name, *output_node_name;
674 pool_get (im->hw_interfaces, hw);
676 hw_index = hw - im->hw_interfaces;
677 hw->hw_if_index = hw_index;
679 if (dev_class->format_device_name)
680 hw->name = format (0, "%U", dev_class->format_device_name, dev_instance);
681 else if (hw_class->format_interface_name)
682 hw->name = format (0, "%U", hw_class->format_interface_name,
685 hw->name = format (0, "%s%x", hw_class->name, dev_instance);
687 if (!im->hw_interface_by_name)
688 im->hw_interface_by_name = hash_create_vec ( /* size */ 0,
689 sizeof (hw->name[0]),
692 hash_set_mem (im->hw_interface_by_name, hw->name, hw_index);
694 /* Make hardware interface point to software interface. */
696 vnet_sw_interface_t sw;
698 memset (&sw, 0, sizeof (sw));
699 sw.type = VNET_SW_INTERFACE_TYPE_HARDWARE;
700 sw.hw_if_index = hw_index;
701 hw->sw_if_index = vnet_create_sw_interface_no_callbacks (vnm, &sw);
704 hw->dev_class_index = dev_class_index;
705 hw->dev_instance = dev_instance;
706 hw->hw_class_index = hw_class_index;
707 hw->hw_instance = hw_instance;
709 hw->max_rate_bits_per_sec = 0;
710 hw->min_packet_bytes = 0;
711 hw->per_packet_overhead_bytes = 0;
712 hw->max_l3_packet_bytes[VLIB_RX] = ~0;
713 hw->max_l3_packet_bytes[VLIB_TX] = ~0;
715 tx_node_name = (char *) format (0, "%v-tx", hw->name);
716 output_node_name = (char *) format (0, "%v-output", hw->name);
718 /* If we have previously deleted interface nodes, re-use them. */
719 if (vec_len (im->deleted_hw_interface_nodes) > 0)
721 vnet_hw_interface_nodes_t *hn;
722 vnet_interface_output_runtime_t *rt;
724 hn = vec_end (im->deleted_hw_interface_nodes) - 1;
726 hw->tx_node_index = hn->tx_node_index;
727 hw->output_node_index = hn->output_node_index;
729 vlib_node_rename (vm, hw->tx_node_index, "%v", tx_node_name);
730 vlib_node_rename (vm, hw->output_node_index, "%v", output_node_name);
732 rt = vlib_node_get_runtime_data (vm, hw->output_node_index);
733 ASSERT (rt->is_deleted == 1);
735 rt->hw_if_index = hw_index;
736 rt->sw_if_index = hw->sw_if_index;
737 rt->dev_instance = hw->dev_instance;
739 rt = vlib_node_get_runtime_data (vm, hw->tx_node_index);
740 rt->hw_if_index = hw_index;
741 rt->sw_if_index = hw->sw_if_index;
742 rt->dev_instance = hw->dev_instance;
744 vlib_worker_thread_node_runtime_update ();
745 _vec_len (im->deleted_hw_interface_nodes) -= 1;
749 vlib_node_registration_t r;
750 vnet_interface_output_runtime_t rt = {
751 .hw_if_index = hw_index,
752 .sw_if_index = hw->sw_if_index,
753 .dev_instance = hw->dev_instance,
757 memset (&r, 0, sizeof (r));
758 r.type = VLIB_NODE_TYPE_INTERNAL;
759 r.runtime_data = &rt;
760 r.runtime_data_bytes = sizeof (rt);
762 r.vector_size = sizeof (u32);
764 r.flags = VLIB_NODE_FLAG_IS_OUTPUT;
765 r.name = tx_node_name;
766 r.function = dev_class->tx_function;
768 hw->tx_node_index = vlib_register_node (vm, &r);
770 vlib_node_add_named_next_with_slot (vm, hw->tx_node_index,
772 VNET_INTERFACE_TX_NEXT_DROP);
775 r.name = output_node_name;
776 r.function = dev_class->no_flatten_output_chains ?
777 vnet_interface_output_node_no_flatten_multiarch_select () :
778 vnet_interface_output_node_multiarch_select ();
779 r.format_trace = format_vnet_interface_output_trace;
784 "interface is deleted",
787 r.n_errors = ARRAY_LEN (e);
790 hw->output_node_index = vlib_register_node (vm, &r);
792 #define _(sym,str) vlib_node_add_named_next_with_slot (vm, \
793 hw->output_node_index, str, \
794 VNET_INTERFACE_OUTPUT_NEXT_##sym);
795 foreach_intf_output_feat
797 vlib_node_add_named_next_with_slot (vm, hw->output_node_index,
799 VNET_INTERFACE_OUTPUT_NEXT_DROP);
800 vlib_node_add_next_with_slot (vm, hw->output_node_index,
802 VNET_INTERFACE_OUTPUT_NEXT_TX);
805 setup_output_node (vm, hw->output_node_index, hw_class);
806 setup_tx_node (vm, hw->tx_node_index, dev_class);
808 /* Call all up/down callbacks with zero flags when interface is created. */
809 vnet_sw_interface_set_flags_helper (vnm, hw->sw_if_index, /* flags */ 0,
810 VNET_INTERFACE_SET_FLAGS_HELPER_IS_CREATE);
811 vnet_hw_interface_set_flags_helper (vnm, hw_index, /* flags */ 0,
812 VNET_INTERFACE_SET_FLAGS_HELPER_IS_CREATE);
818 vnet_delete_hw_interface (vnet_main_t * vnm, u32 hw_if_index)
820 vnet_interface_main_t *im = &vnm->interface_main;
821 vnet_hw_interface_t *hw = vnet_get_hw_interface (vnm, hw_if_index);
822 vlib_main_t *vm = vnm->vlib_main;
824 /* If it is up, mark it down. */
826 vnet_hw_interface_set_flags (vnm, hw_if_index, /* flags */ 0);
828 /* Call delete callbacks. */
829 call_hw_interface_add_del_callbacks (vnm, hw_if_index, /* is_create */ 0);
831 /* Delete software interface corresponding to hardware interface. */
832 vnet_delete_sw_interface (vnm, hw->sw_if_index);
834 /* Delete any sub-interfaces. */
838 hash_foreach (id, sw_if_index, hw->sub_interface_sw_if_index_by_id, ({
839 vnet_delete_sw_interface (vnm, sw_if_index);
845 vnet_hw_interface_nodes_t *dn;
846 vnet_interface_output_runtime_t *rt =
847 vlib_node_get_runtime_data (vm, hw->output_node_index);
849 /* Mark node runtime as deleted so output node (if called) will drop packets. */
852 vlib_node_rename (vm, hw->output_node_index,
853 "interface-%d-output-deleted", hw_if_index);
854 vlib_node_rename (vm, hw->tx_node_index, "interface-%d-tx-deleted",
856 vec_add2 (im->deleted_hw_interface_nodes, dn, 1);
857 dn->tx_node_index = hw->tx_node_index;
858 dn->output_node_index = hw->output_node_index;
861 hash_unset_mem (im->hw_interface_by_name, hw->name);
864 pool_put (im->hw_interfaces, hw);
868 serialize_vnet_hw_interface_set_class (serialize_main_t * m, va_list * va)
870 u32 hw_if_index = va_arg (*va, u32);
871 char *hw_class_name = va_arg (*va, char *);
872 serialize_integer (m, hw_if_index, sizeof (hw_if_index));
873 serialize_cstring (m, hw_class_name);
877 unserialize_vnet_hw_interface_set_class (serialize_main_t * m, va_list * va)
879 CLIB_UNUSED (mc_main_t * mc) = va_arg (*va, mc_main_t *);
880 vnet_main_t *vnm = vnet_get_main ();
886 unserialize_integer (m, &hw_if_index, sizeof (hw_if_index));
887 unserialize_cstring (m, &hw_class_name);
889 hash_get (vnm->interface_main.hw_interface_class_by_name, hw_class_name);
891 error = vnet_hw_interface_set_class_helper (vnm, hw_if_index, p[0],
892 /* redistribute */ 0);
894 clib_error_report (error);
897 MC_SERIALIZE_MSG (vnet_hw_interface_set_class_msg, static) =
899 .name = "vnet_hw_interface_set_class",.serialize =
900 serialize_vnet_hw_interface_set_class,.unserialize =
901 unserialize_vnet_hw_interface_set_class,};
904 vnet_hw_interface_init_for_class (vnet_main_t * vnm, u32 hw_if_index,
905 u32 hw_class_index, u32 hw_instance)
907 vnet_hw_interface_t *hi = vnet_get_hw_interface (vnm, hw_if_index);
908 vnet_hw_interface_class_t *hc =
909 vnet_get_hw_interface_class (vnm, hw_class_index);
911 hi->hw_class_index = hw_class_index;
912 hi->hw_instance = hw_instance;
913 setup_output_node (vnm->vlib_main, hi->output_node_index, hc);
916 static clib_error_t *
917 vnet_hw_interface_set_class_helper (vnet_main_t * vnm, u32 hw_if_index,
918 u32 hw_class_index, u32 redistribute)
920 vnet_hw_interface_t *hi = vnet_get_hw_interface (vnm, hw_if_index);
921 vnet_sw_interface_t *si = vnet_get_sw_interface (vnm, hi->sw_if_index);
922 vnet_hw_interface_class_t *old_class =
923 vnet_get_hw_interface_class (vnm, hi->hw_class_index);
924 vnet_hw_interface_class_t *new_class =
925 vnet_get_hw_interface_class (vnm, hw_class_index);
926 vnet_device_class_t *dev_class =
927 vnet_get_device_class (vnm, hi->dev_class_index);
928 clib_error_t *error = 0;
930 /* New class equals old class? Nothing to do. */
931 if (hi->hw_class_index == hw_class_index)
934 /* No need (and incorrect since admin up flag may be set) to do error checking when
935 receiving unserialize message. */
938 if (si->flags & VNET_SW_INTERFACE_FLAG_ADMIN_UP)
939 return clib_error_return (0,
940 "%v must be admin down to change class from %s to %s",
941 hi->name, old_class->name, new_class->name);
943 /* Make sure interface supports given class. */
944 if ((new_class->is_valid_class_for_interface
945 && !new_class->is_valid_class_for_interface (vnm, hw_if_index,
947 || (dev_class->is_valid_class_for_interface
948 && !dev_class->is_valid_class_for_interface (vnm, hw_if_index,
950 return clib_error_return (0,
951 "%v class cannot be changed from %s to %s",
952 hi->name, old_class->name, new_class->name);
954 if (vnm->vlib_main->mc_main)
956 mc_serialize (vnm->vlib_main->mc_main,
957 &vnet_hw_interface_set_class_msg, hw_if_index,
963 if (old_class->hw_class_change)
964 old_class->hw_class_change (vnm, hw_if_index, old_class->index,
967 vnet_hw_interface_init_for_class (vnm, hw_if_index, new_class->index,
970 if (new_class->hw_class_change)
971 new_class->hw_class_change (vnm, hw_if_index, old_class->index,
974 if (dev_class->hw_class_change)
975 dev_class->hw_class_change (vnm, hw_if_index, new_class->index);
981 vnet_hw_interface_set_class (vnet_main_t * vnm, u32 hw_if_index,
984 return vnet_hw_interface_set_class_helper (vnm, hw_if_index, hw_class_index,
985 /* redistribute */ 1);
989 vnet_hw_interface_rx_redirect_to_node_helper (vnet_main_t * vnm,
994 vnet_hw_interface_t *hi = vnet_get_hw_interface (vnm, hw_if_index);
995 vnet_device_class_t *dev_class = vnet_get_device_class
996 (vnm, hi->dev_class_index);
1000 /* $$$$ fixme someday maybe */
1001 ASSERT (vnm->vlib_main->mc_main == 0);
1003 if (dev_class->rx_redirect_to_node)
1005 dev_class->rx_redirect_to_node (vnm, hw_if_index, node_index);
1009 return VNET_API_ERROR_UNIMPLEMENTED;
1013 vnet_hw_interface_rx_redirect_to_node (vnet_main_t * vnm, u32 hw_if_index,
1016 return vnet_hw_interface_rx_redirect_to_node_helper (vnm, hw_if_index,
1018 1 /* redistribute */ );
1022 vnet_sw_interface_compare (vnet_main_t * vnm,
1023 uword sw_if_index0, uword sw_if_index1)
1025 vnet_sw_interface_t *sup0 = vnet_get_sup_sw_interface (vnm, sw_if_index0);
1026 vnet_sw_interface_t *sup1 = vnet_get_sup_sw_interface (vnm, sw_if_index1);
1027 vnet_hw_interface_t *h0 = vnet_get_hw_interface (vnm, sup0->hw_if_index);
1028 vnet_hw_interface_t *h1 = vnet_get_hw_interface (vnm, sup1->hw_if_index);
1031 return vec_cmp (h0->name, h1->name);
1032 return (word) h0->hw_instance - (word) h1->hw_instance;
1036 vnet_hw_interface_compare (vnet_main_t * vnm,
1037 uword hw_if_index0, uword hw_if_index1)
1039 vnet_hw_interface_t *h0 = vnet_get_hw_interface (vnm, hw_if_index0);
1040 vnet_hw_interface_t *h1 = vnet_get_hw_interface (vnm, hw_if_index1);
1043 return vec_cmp (h0->name, h1->name);
1044 return (word) h0->hw_instance - (word) h1->hw_instance;
1048 vnet_interface_init (vlib_main_t * vm)
1050 vnet_main_t *vnm = vnet_get_main ();
1051 vnet_interface_main_t *im = &vnm->interface_main;
1052 vlib_buffer_t *b = 0;
1053 vnet_buffer_opaque_t *o = 0;
1056 * Keep people from shooting themselves in the foot.
1058 if (sizeof (b->opaque) != sizeof (vnet_buffer_opaque_t))
1060 #define _(a) if (sizeof(o->a) > sizeof (o->unused)) \
1062 ("FATAL: size of opaque union subtype %s is %d (max %d)", \
1063 #a, sizeof(o->a), sizeof (o->unused));
1064 foreach_buffer_opaque_union_subtype;
1067 return clib_error_return
1068 (0, "FATAL: size of vlib buffer opaque %d, size of vnet opaque %d",
1069 sizeof (b->opaque), sizeof (vnet_buffer_opaque_t));
1072 im->sw_if_counter_lock = clib_mem_alloc_aligned (CLIB_CACHE_LINE_BYTES,
1073 CLIB_CACHE_LINE_BYTES);
1074 im->sw_if_counter_lock[0] = 1; /* should be no need */
1076 vec_validate (im->sw_if_counters, VNET_N_SIMPLE_INTERFACE_COUNTER - 1);
1077 im->sw_if_counters[VNET_INTERFACE_COUNTER_DROP].name = "drops";
1078 im->sw_if_counters[VNET_INTERFACE_COUNTER_PUNT].name = "punts";
1079 im->sw_if_counters[VNET_INTERFACE_COUNTER_IP4].name = "ip4";
1080 im->sw_if_counters[VNET_INTERFACE_COUNTER_IP6].name = "ip6";
1081 im->sw_if_counters[VNET_INTERFACE_COUNTER_RX_NO_BUF].name = "rx-no-buf";
1082 im->sw_if_counters[VNET_INTERFACE_COUNTER_RX_MISS].name = "rx-miss";
1083 im->sw_if_counters[VNET_INTERFACE_COUNTER_RX_ERROR].name = "rx-error";
1084 im->sw_if_counters[VNET_INTERFACE_COUNTER_TX_ERROR].name = "tx-error";
1086 vec_validate (im->combined_sw_if_counters,
1087 VNET_N_COMBINED_INTERFACE_COUNTER - 1);
1088 im->combined_sw_if_counters[VNET_INTERFACE_COUNTER_RX].name = "rx";
1089 im->combined_sw_if_counters[VNET_INTERFACE_COUNTER_TX].name = "tx";
1091 im->sw_if_counter_lock[0] = 0;
1093 im->device_class_by_name = hash_create_string ( /* size */ 0,
1096 vnet_device_class_t *c;
1098 c = vnm->device_class_registrations;
1102 c->index = vec_len (im->device_classes);
1103 hash_set_mem (im->device_class_by_name, c->name, c->index);
1104 vec_add1 (im->device_classes, c[0]);
1105 c = c->next_class_registration;
1109 im->hw_interface_class_by_name = hash_create_string ( /* size */ 0,
1112 im->sw_if_index_by_sup_and_sub = hash_create_mem (0, sizeof (u64),
1115 vnet_hw_interface_class_t *c;
1117 c = vnm->hw_interface_class_registrations;
1121 c->index = vec_len (im->hw_interface_classes);
1122 hash_set_mem (im->hw_interface_class_by_name, c->name, c->index);
1123 vec_add1 (im->hw_interface_classes, c[0]);
1124 c = c->next_class_registration;
1129 clib_error_t *error;
1131 if ((error = vlib_call_init_function (vm, vnet_interface_cli_init)))
1138 VLIB_INIT_FUNCTION (vnet_interface_init);
1140 /* Kludge to renumber interface names [only!] */
1142 vnet_interface_name_renumber (u32 sw_if_index, u32 new_show_dev_instance)
1145 vnet_main_t *vnm = vnet_get_main ();
1146 vnet_interface_main_t *im = &vnm->interface_main;
1147 vnet_hw_interface_t *hi = vnet_get_sup_hw_interface (vnm, sw_if_index);
1149 vnet_device_class_t *dev_class = vnet_get_device_class
1150 (vnm, hi->dev_class_index);
1152 if (dev_class->name_renumber == 0 || dev_class->format_device_name == 0)
1153 return VNET_API_ERROR_UNIMPLEMENTED;
1155 rv = dev_class->name_renumber (hi, new_show_dev_instance);
1160 hash_unset_mem (im->hw_interface_by_name, hi->name);
1161 vec_free (hi->name);
1162 /* Use the mapping we set up to call it Ishmael */
1163 hi->name = format (0, "%U", dev_class->format_device_name,
1166 hash_set_mem (im->hw_interface_by_name, hi->name, hi->hw_if_index);
1171 vnet_interface_add_del_feature (vnet_main_t * vnm,
1174 intf_output_feat_t feature, int is_add)
1176 vnet_sw_interface_t *sw;
1178 sw = vnet_get_sw_interface (vnm, sw_if_index);
1183 sw->output_feature_bitmap |= (1 << feature);
1184 sw->output_feature_bitmap |= (1 << INTF_OUTPUT_FEAT_DONE);
1190 sw->output_feature_bitmap &= ~(1 << feature);
1191 if (sw->output_feature_bitmap == (1 << INTF_OUTPUT_FEAT_DONE))
1192 sw->output_feature_bitmap = 0;
1199 vnet_rename_interface (vnet_main_t * vnm, u32 hw_if_index, char *new_name)
1201 vnet_interface_main_t *im = &vnm->interface_main;
1202 vlib_main_t *vm = vnm->vlib_main;
1203 vnet_hw_interface_t *hw;
1205 clib_error_t *error = 0;
1207 hw = vnet_get_hw_interface (vnm, hw_if_index);
1210 return clib_error_return (0,
1211 "unable to find hw interface for index %u",
1215 old_name = hw->name;
1217 /* set new hw->name */
1218 hw->name = format (0, "%s", new_name);
1220 /* remove the old name to hw_if_index mapping and install the new one */
1221 hash_unset_mem (im->hw_interface_by_name, old_name);
1222 hash_set_mem (im->hw_interface_by_name, hw->name, hw_if_index);
1224 /* rename tx/output nodes */
1225 vlib_node_rename (vm, hw->tx_node_index, "%v-tx", hw->name);
1226 vlib_node_rename (vm, hw->output_node_index, "%v-output", hw->name);
1228 /* free the old name vector */
1229 vec_free (old_name);
1234 static clib_error_t *
1235 vnet_hw_interface_change_mac_address_helper (vnet_main_t * vnm,
1236 u32 hw_if_index, u64 mac_address)
1238 clib_error_t *error = 0;
1239 vnet_hw_interface_t *hi = vnet_get_hw_interface (vnm, hw_if_index);
1243 vnet_device_class_t *dev_class =
1244 vnet_get_device_class (vnm, hi->dev_class_index);
1245 if (dev_class->mac_addr_change_function)
1248 dev_class->mac_addr_change_function (vnet_get_hw_interface
1250 (char *) &mac_address);
1254 ethernet_main_t *em = ðernet_main;
1255 ethernet_interface_t *ei =
1256 pool_elt_at_index (em->interfaces, hi->hw_instance);
1258 clib_memcpy (hi->hw_address, (u8 *) & mac_address,
1259 sizeof (hi->hw_address));
1260 clib_memcpy (ei->address, (u8 *) & mac_address,
1261 sizeof (ei->address));
1262 ethernet_arp_change_mac (vnm, hw_if_index);
1263 ethernet_ndp_change_mac (vnm->vlib_main, hw_if_index);
1268 clib_error_return (0,
1269 "MAC Address Change is not supported on this interface");
1275 clib_error_return (0,
1276 "mac address change is not supported for interface index %u",
1283 vnet_hw_interface_change_mac_address (vnet_main_t * vnm, u32 hw_if_index,
1286 return vnet_hw_interface_change_mac_address_helper
1287 (vnm, hw_if_index, mac_address);
1291 * fd.io coding-style-patch-verification: ON
1294 * eval: (c-set-style "gnu")