2 * Copyright (c) 2015 Cisco and/or its affiliates.
3 * Licensed under the Apache License, Version 2.0 (the "License");
4 * you may not use this file except in compliance with the License.
5 * You may obtain a copy of the License at:
7 * http://www.apache.org/licenses/LICENSE-2.0
9 * Unless required by applicable law or agreed to in writing, software
10 * distributed under the License is distributed on an "AS IS" BASIS,
11 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 * See the License for the specific language governing permissions and
13 * limitations under the License.
16 * interface.c: VNET interfaces/sub-interfaces
18 * Copyright (c) 2008 Eliot Dresselhaus
20 * Permission is hereby granted, free of charge, to any person obtaining
21 * a copy of this software and associated documentation files (the
22 * "Software"), to deal in the Software without restriction, including
23 * without limitation the rights to use, copy, modify, merge, publish,
24 * distribute, sublicense, and/or sell copies of the Software, and to
25 * permit persons to whom the Software is furnished to do so, subject to
26 * the following conditions:
28 * The above copyright notice and this permission notice shall be
29 * included in all copies or substantial portions of the Software.
31 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
32 * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
33 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
34 * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
35 * LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
36 * OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
37 * WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
40 #include <vnet/vnet.h>
41 #include <vnet/plugin/plugin.h>
43 #define VNET_INTERFACE_SET_FLAGS_HELPER_IS_CREATE (1 << 0)
44 #define VNET_INTERFACE_SET_FLAGS_HELPER_WANT_REDISTRIBUTE (1 << 1)
46 static clib_error_t *vnet_hw_interface_set_flags_helper (vnet_main_t * vnm,
51 static clib_error_t *vnet_sw_interface_set_flags_helper (vnet_main_t * vnm,
56 static clib_error_t *vnet_hw_interface_set_class_helper (vnet_main_t * vnm,
63 /* Either sw or hw interface index. */
68 } vnet_sw_hw_interface_state_t;
71 serialize_vec_vnet_sw_hw_interface_state (serialize_main_t * m, va_list * va)
73 vnet_sw_hw_interface_state_t *s =
74 va_arg (*va, vnet_sw_hw_interface_state_t *);
75 u32 n = va_arg (*va, u32);
77 for (i = 0; i < n; i++)
79 serialize_integer (m, s[i].sw_hw_if_index,
80 sizeof (s[i].sw_hw_if_index));
81 serialize_integer (m, s[i].flags, sizeof (s[i].flags));
86 unserialize_vec_vnet_sw_hw_interface_state (serialize_main_t * m,
89 vnet_sw_hw_interface_state_t *s =
90 va_arg (*va, vnet_sw_hw_interface_state_t *);
91 u32 n = va_arg (*va, u32);
93 for (i = 0; i < n; i++)
95 unserialize_integer (m, &s[i].sw_hw_if_index,
96 sizeof (s[i].sw_hw_if_index));
97 unserialize_integer (m, &s[i].flags, sizeof (s[i].flags));
102 serialize_vnet_sw_hw_interface_set_flags (serialize_main_t * m, va_list * va)
104 vnet_sw_hw_interface_state_t *s =
105 va_arg (*va, vnet_sw_hw_interface_state_t *);
106 serialize (m, serialize_vec_vnet_sw_hw_interface_state, s, 1);
110 unserialize_vnet_sw_interface_set_flags (serialize_main_t * m, va_list * va)
112 CLIB_UNUSED (mc_main_t * mc) = va_arg (*va, mc_main_t *);
113 vnet_sw_hw_interface_state_t s;
115 unserialize (m, unserialize_vec_vnet_sw_hw_interface_state, &s, 1);
117 vnet_sw_interface_set_flags_helper
118 (vnet_get_main (), s.sw_hw_if_index, s.flags,
119 /* helper_flags no redistribution */ 0);
123 unserialize_vnet_hw_interface_set_flags (serialize_main_t * m, va_list * va)
125 CLIB_UNUSED (mc_main_t * mc) = va_arg (*va, mc_main_t *);
126 vnet_sw_hw_interface_state_t s;
128 unserialize (m, unserialize_vec_vnet_sw_hw_interface_state, &s, 1);
130 vnet_hw_interface_set_flags_helper
131 (vnet_get_main (), s.sw_hw_if_index, s.flags,
132 /* helper_flags no redistribution */ 0);
135 MC_SERIALIZE_MSG (vnet_sw_interface_set_flags_msg, static) =
137 .name = "vnet_sw_interface_set_flags",.serialize =
138 serialize_vnet_sw_hw_interface_set_flags,.unserialize =
139 unserialize_vnet_sw_interface_set_flags,};
141 MC_SERIALIZE_MSG (vnet_hw_interface_set_flags_msg, static) =
143 .name = "vnet_hw_interface_set_flags",.serialize =
144 serialize_vnet_sw_hw_interface_set_flags,.unserialize =
145 unserialize_vnet_hw_interface_set_flags,};
148 serialize_vnet_interface_state (serialize_main_t * m, va_list * va)
150 vnet_main_t *vnm = va_arg (*va, vnet_main_t *);
151 vnet_sw_hw_interface_state_t *sts = 0, *st;
152 vnet_sw_interface_t *sif;
153 vnet_hw_interface_t *hif;
154 vnet_interface_main_t *im = &vnm->interface_main;
156 /* Serialize hardware interface classes since they may have changed.
157 Must do this before sending up/down flags. */
159 pool_foreach (hif, im->hw_interfaces, ({
160 vnet_hw_interface_class_t * hw_class = vnet_get_hw_interface_class (vnm, hif->hw_class_index);
161 serialize_cstring (m, hw_class->name);
165 /* Send sw/hw interface state when non-zero. */
167 pool_foreach (sif, im->sw_interfaces, ({
170 vec_add2 (sts, st, 1);
171 st->sw_hw_if_index = sif->sw_if_index;
172 st->flags = sif->flags;
177 vec_serialize (m, sts, serialize_vec_vnet_sw_hw_interface_state);
183 pool_foreach (hif, im->hw_interfaces, ({
186 vec_add2 (sts, st, 1);
187 st->sw_hw_if_index = hif->hw_if_index;
188 st->flags = hif->flags;
193 vec_serialize (m, sts, serialize_vec_vnet_sw_hw_interface_state);
199 unserialize_vnet_interface_state (serialize_main_t * m, va_list * va)
201 vnet_main_t *vnm = va_arg (*va, vnet_main_t *);
202 vnet_sw_hw_interface_state_t *sts = 0, *st;
204 /* First set interface hardware class. */
206 vnet_interface_main_t *im = &vnm->interface_main;
207 vnet_hw_interface_t *hif;
213 pool_foreach (hif, im->hw_interfaces, ({
214 unserialize_cstring (m, &class_name);
215 p = hash_get_mem (im->hw_interface_class_by_name, class_name);
217 error = vnet_hw_interface_set_class_helper (vnm, hif->hw_if_index, p[0], /* redistribute */ 0);
219 clib_error_report (error);
220 vec_free (class_name);
225 vec_unserialize (m, &sts, unserialize_vec_vnet_sw_hw_interface_state);
226 vec_foreach (st, sts)
227 vnet_sw_interface_set_flags_helper (vnm, st->sw_hw_if_index, st->flags,
228 /* no distribute */ 0);
231 vec_unserialize (m, &sts, unserialize_vec_vnet_sw_hw_interface_state);
232 vec_foreach (st, sts)
233 vnet_hw_interface_set_flags_helper (vnm, st->sw_hw_if_index, st->flags,
234 /* no distribute */ 0);
238 static clib_error_t *
239 call_elf_section_interface_callbacks (vnet_main_t * vnm, u32 if_index,
241 _vnet_interface_function_list_elt_t *
244 clib_error_t *error = 0;
248 error = elt->fp (vnm, if_index, flags);
251 elt = elt->next_interface_function;
256 static clib_error_t *
257 call_hw_interface_add_del_callbacks (vnet_main_t * vnm, u32 hw_if_index,
260 vnet_hw_interface_t *hi = vnet_get_hw_interface (vnm, hw_if_index);
261 vnet_hw_interface_class_t *hw_class =
262 vnet_get_hw_interface_class (vnm, hi->hw_class_index);
263 vnet_device_class_t *dev_class =
264 vnet_get_device_class (vnm, hi->dev_class_index);
265 clib_error_t *error = 0;
267 if (hw_class->interface_add_del_function
269 hw_class->interface_add_del_function (vnm, hw_if_index, is_create)))
272 if (dev_class->interface_add_del_function
274 dev_class->interface_add_del_function (vnm, hw_if_index,
278 error = call_elf_section_interface_callbacks
279 (vnm, hw_if_index, is_create, vnm->hw_interface_add_del_functions);
284 static clib_error_t *
285 call_sw_interface_add_del_callbacks (vnet_main_t * vnm, u32 sw_if_index,
288 return call_elf_section_interface_callbacks
289 (vnm, sw_if_index, is_create, vnm->sw_interface_add_del_functions);
292 #define VNET_INTERFACE_SET_FLAGS_HELPER_IS_CREATE (1 << 0)
293 #define VNET_INTERFACE_SET_FLAGS_HELPER_WANT_REDISTRIBUTE (1 << 1)
295 static clib_error_t *
296 vnet_hw_interface_set_flags_helper (vnet_main_t * vnm, u32 hw_if_index,
297 u32 flags, u32 helper_flags)
299 vnet_hw_interface_t *hi = vnet_get_hw_interface (vnm, hw_if_index);
300 vnet_hw_interface_class_t *hw_class =
301 vnet_get_hw_interface_class (vnm, hi->hw_class_index);
302 vnet_device_class_t *dev_class =
303 vnet_get_device_class (vnm, hi->dev_class_index);
304 vlib_main_t *vm = vnm->vlib_main;
306 clib_error_t *error = 0;
308 (helper_flags & VNET_INTERFACE_SET_FLAGS_HELPER_IS_CREATE) != 0;
311 (VNET_HW_INTERFACE_FLAG_LINK_UP | VNET_HW_INTERFACE_FLAG_DUPLEX_MASK |
312 VNET_HW_INTERFACE_FLAG_SPEED_MASK);
315 /* Call hardware interface add/del callbacks. */
317 call_hw_interface_add_del_callbacks (vnm, hw_if_index, is_create);
319 /* Already in the desired state? */
320 if (!is_create && (hi->flags & mask) == flags)
323 /* Some interface classes do not redistribute (e.g. are local). */
324 if (!dev_class->redistribute)
325 helper_flags &= ~VNET_INTERFACE_SET_FLAGS_HELPER_WANT_REDISTRIBUTE;
328 && (helper_flags & VNET_INTERFACE_SET_FLAGS_HELPER_WANT_REDISTRIBUTE))
330 vnet_sw_hw_interface_state_t s;
331 s.sw_hw_if_index = hw_if_index;
333 mc_serialize (vm->mc_main, &vnet_hw_interface_set_flags_msg, &s);
336 if ((hi->flags & VNET_HW_INTERFACE_FLAG_LINK_UP) !=
337 (flags & VNET_HW_INTERFACE_FLAG_LINK_UP))
339 /* Do hardware class (e.g. ethernet). */
340 if (hw_class->link_up_down_function
341 && (error = hw_class->link_up_down_function (vnm, hw_if_index,
345 error = call_elf_section_interface_callbacks
346 (vnm, hw_if_index, flags, vnm->hw_interface_link_up_down_functions);
359 static clib_error_t *
360 vnet_sw_interface_set_flags_helper (vnet_main_t * vnm, u32 sw_if_index,
361 u32 flags, u32 helper_flags)
363 vnet_sw_interface_t *si = vnet_get_sw_interface (vnm, sw_if_index);
364 vlib_main_t *vm = vnm->vlib_main;
366 clib_error_t *error = 0;
368 (helper_flags & VNET_INTERFACE_SET_FLAGS_HELPER_IS_CREATE) != 0;
371 mask = VNET_SW_INTERFACE_FLAG_ADMIN_UP | VNET_SW_INTERFACE_FLAG_PUNT;
377 call_sw_interface_add_del_callbacks (vnm, sw_if_index, is_create);
381 if (flags & VNET_SW_INTERFACE_FLAG_ADMIN_UP)
383 /* Notify everyone when the interface is created as admin up */
384 error = call_elf_section_interface_callbacks (vnm, sw_if_index,
387 sw_interface_admin_up_down_functions);
394 vnet_sw_interface_t *si_sup = si;
396 /* Check that super interface is in correct state. */
397 if (si->type == VNET_SW_INTERFACE_TYPE_SUB)
399 si_sup = vnet_get_sw_interface (vnm, si->sup_sw_if_index);
401 /* Check to see if we're bringing down the soft interface and if it's parent is up */
402 if ((flags != (si_sup->flags & mask)) &&
404 && ((si_sup->flags & mask) ==
405 VNET_SW_INTERFACE_FLAG_ADMIN_UP))))
407 error = clib_error_return (0, "super-interface %U must be %U",
408 format_vnet_sw_interface_name, vnm,
410 format_vnet_sw_interface_flags,
416 /* Donot change state for slave link of bonded interfaces */
417 if (si->flags & VNET_SW_INTERFACE_FLAG_BOND_SLAVE)
419 error = clib_error_return
420 (0, "not allowed as %U belong to a BondEthernet interface",
421 format_vnet_sw_interface_name, vnm, si);
425 /* Already in the desired state? */
426 if ((si->flags & mask) == flags)
429 /* Sub-interfaces of hardware interfaces that do no redistribute,
430 do not redistribute themselves. */
431 if (si_sup->type == VNET_SW_INTERFACE_TYPE_HARDWARE)
433 vnet_hw_interface_t *hi =
434 vnet_get_hw_interface (vnm, si_sup->hw_if_index);
435 vnet_device_class_t *dev_class =
436 vnet_get_device_class (vnm, hi->dev_class_index);
437 if (!dev_class->redistribute)
439 ~VNET_INTERFACE_SET_FLAGS_HELPER_WANT_REDISTRIBUTE;
444 VNET_INTERFACE_SET_FLAGS_HELPER_WANT_REDISTRIBUTE))
446 vnet_sw_hw_interface_state_t s;
447 s.sw_hw_if_index = sw_if_index;
449 mc_serialize (vm->mc_main, &vnet_sw_interface_set_flags_msg, &s);
452 /* set the flags now before invoking the registered clients
453 * so that the state they query is consistent with the state here notified */
454 old_flags = si->flags;
457 if ((flags | old_flags) & VNET_SW_INTERFACE_FLAG_ADMIN_UP)
458 error = call_elf_section_interface_callbacks
459 (vnm, sw_if_index, flags,
460 vnm->sw_interface_admin_up_down_functions);
461 si->flags = old_flags;
466 if (si->type == VNET_SW_INTERFACE_TYPE_HARDWARE)
468 vnet_hw_interface_t *hi =
469 vnet_get_hw_interface (vnm, si->hw_if_index);
470 vnet_hw_interface_class_t *hw_class =
471 vnet_get_hw_interface_class (vnm, hi->hw_class_index);
472 vnet_device_class_t *dev_class =
473 vnet_get_device_class (vnm, hi->dev_class_index);
475 /* save the si admin up flag */
476 old_flags = si->flags;
478 /* update si admin up flag in advance if we are going admin down */
479 if (!(flags & VNET_SW_INTERFACE_FLAG_ADMIN_UP))
480 si->flags &= ~VNET_SW_INTERFACE_FLAG_ADMIN_UP;
482 if (dev_class->admin_up_down_function
483 && (error = dev_class->admin_up_down_function (vnm,
487 /* restore si admin up flag to it's original state on errors */
488 si->flags = old_flags;
492 if (hw_class->admin_up_down_function
493 && (error = hw_class->admin_up_down_function (vnm,
497 /* restore si admin up flag to it's original state on errors */
498 si->flags = old_flags;
502 /* Admin down implies link down. */
503 if (!(flags & VNET_SW_INTERFACE_FLAG_ADMIN_UP)
504 && (hi->flags & VNET_HW_INTERFACE_FLAG_LINK_UP))
505 vnet_hw_interface_set_flags_helper (vnm, si->hw_if_index,
507 ~VNET_HW_INTERFACE_FLAG_LINK_UP,
520 vnet_hw_interface_set_flags (vnet_main_t * vnm, u32 hw_if_index, u32 flags)
522 return vnet_hw_interface_set_flags_helper
523 (vnm, hw_if_index, flags,
524 VNET_INTERFACE_SET_FLAGS_HELPER_WANT_REDISTRIBUTE);
528 vnet_sw_interface_set_flags (vnet_main_t * vnm, u32 sw_if_index, u32 flags)
530 return vnet_sw_interface_set_flags_helper
531 (vnm, sw_if_index, flags,
532 VNET_INTERFACE_SET_FLAGS_HELPER_WANT_REDISTRIBUTE);
536 vnet_create_sw_interface_no_callbacks (vnet_main_t * vnm,
537 vnet_sw_interface_t * template)
539 vnet_interface_main_t *im = &vnm->interface_main;
540 vnet_sw_interface_t *sw;
543 pool_get (im->sw_interfaces, sw);
544 sw_if_index = sw - im->sw_interfaces;
549 sw->sw_if_index = sw_if_index;
550 if (sw->type == VNET_SW_INTERFACE_TYPE_HARDWARE)
551 sw->sup_sw_if_index = sw->sw_if_index;
553 /* Allocate counters for this interface. */
557 vnet_interface_counter_lock (im);
559 for (i = 0; i < vec_len (im->sw_if_counters); i++)
561 vlib_validate_simple_counter (&im->sw_if_counters[i], sw_if_index);
562 vlib_zero_simple_counter (&im->sw_if_counters[i], sw_if_index);
565 for (i = 0; i < vec_len (im->combined_sw_if_counters); i++)
567 vlib_validate_combined_counter (&im->combined_sw_if_counters[i],
569 vlib_zero_combined_counter (&im->combined_sw_if_counters[i],
573 vnet_interface_counter_unlock (im);
580 vnet_create_sw_interface (vnet_main_t * vnm, vnet_sw_interface_t * template,
584 vnet_hw_interface_t *hi;
585 vnet_device_class_t *dev_class;
587 hi = vnet_get_sup_hw_interface (vnm, template->sup_sw_if_index);
588 dev_class = vnet_get_device_class (vnm, hi->dev_class_index);
590 if (template->type == VNET_SW_INTERFACE_TYPE_SUB &&
591 dev_class->subif_add_del_function)
593 error = dev_class->subif_add_del_function (vnm, hi->hw_if_index,
594 (struct vnet_sw_interface_t
600 *sw_if_index = vnet_create_sw_interface_no_callbacks (vnm, template);
601 error = vnet_sw_interface_set_flags_helper
602 (vnm, *sw_if_index, template->flags,
603 VNET_INTERFACE_SET_FLAGS_HELPER_IS_CREATE);
607 /* undo the work done by vnet_create_sw_interface_no_callbacks() */
608 vnet_interface_main_t *im = &vnm->interface_main;
609 vnet_sw_interface_t *sw =
610 pool_elt_at_index (im->sw_interfaces, *sw_if_index);
611 pool_put (im->sw_interfaces, sw);
618 vnet_delete_sw_interface (vnet_main_t * vnm, u32 sw_if_index)
620 vnet_interface_main_t *im = &vnm->interface_main;
621 vnet_sw_interface_t *sw =
622 pool_elt_at_index (im->sw_interfaces, sw_if_index);
624 /* Bring down interface in case it is up. */
626 vnet_sw_interface_set_flags (vnm, sw_if_index, /* flags */ 0);
628 call_sw_interface_add_del_callbacks (vnm, sw_if_index, /* is_create */ 0);
630 pool_put (im->sw_interfaces, sw);
634 setup_tx_node (vlib_main_t * vm,
635 u32 node_index, vnet_device_class_t * dev_class)
637 vlib_node_t *n = vlib_get_node (vm, node_index);
639 n->function = dev_class->tx_function;
640 n->format_trace = dev_class->format_tx_trace;
641 vlib_register_errors (vm, node_index,
642 dev_class->tx_function_n_errors,
643 dev_class->tx_function_error_strings);
647 setup_output_node (vlib_main_t * vm,
648 u32 node_index, vnet_hw_interface_class_t * hw_class)
650 vlib_node_t *n = vlib_get_node (vm, node_index);
651 n->format_buffer = hw_class->format_header;
652 n->unformat_buffer = hw_class->unformat_header;
655 /* Register an interface instance. */
657 vnet_register_interface (vnet_main_t * vnm,
660 u32 hw_class_index, u32 hw_instance)
662 vnet_interface_main_t *im = &vnm->interface_main;
663 vnet_hw_interface_t *hw;
664 vnet_device_class_t *dev_class =
665 vnet_get_device_class (vnm, dev_class_index);
666 vnet_hw_interface_class_t *hw_class =
667 vnet_get_hw_interface_class (vnm, hw_class_index);
668 vlib_main_t *vm = vnm->vlib_main;
670 char *tx_node_name, *output_node_name;
672 pool_get (im->hw_interfaces, hw);
674 hw_index = hw - im->hw_interfaces;
675 hw->hw_if_index = hw_index;
677 if (dev_class->format_device_name)
678 hw->name = format (0, "%U", dev_class->format_device_name, dev_instance);
679 else if (hw_class->format_interface_name)
680 hw->name = format (0, "%U", hw_class->format_interface_name,
683 hw->name = format (0, "%s%x", hw_class->name, dev_instance);
685 if (!im->hw_interface_by_name)
686 im->hw_interface_by_name = hash_create_vec ( /* size */ 0,
687 sizeof (hw->name[0]),
690 hash_set_mem (im->hw_interface_by_name, hw->name, hw_index);
692 /* Make hardware interface point to software interface. */
694 vnet_sw_interface_t sw;
696 memset (&sw, 0, sizeof (sw));
697 sw.type = VNET_SW_INTERFACE_TYPE_HARDWARE;
698 sw.hw_if_index = hw_index;
699 hw->sw_if_index = vnet_create_sw_interface_no_callbacks (vnm, &sw);
702 hw->dev_class_index = dev_class_index;
703 hw->dev_instance = dev_instance;
704 hw->hw_class_index = hw_class_index;
705 hw->hw_instance = hw_instance;
707 hw->max_rate_bits_per_sec = 0;
708 hw->min_packet_bytes = 0;
709 hw->per_packet_overhead_bytes = 0;
710 hw->max_l3_packet_bytes[VLIB_RX] = ~0;
711 hw->max_l3_packet_bytes[VLIB_TX] = ~0;
713 tx_node_name = (char *) format (0, "%v-tx", hw->name);
714 output_node_name = (char *) format (0, "%v-output", hw->name);
716 /* If we have previously deleted interface nodes, re-use them. */
717 if (vec_len (im->deleted_hw_interface_nodes) > 0)
719 vnet_hw_interface_nodes_t *hn;
720 vnet_interface_output_runtime_t *rt;
722 hn = vec_end (im->deleted_hw_interface_nodes) - 1;
724 hw->tx_node_index = hn->tx_node_index;
725 hw->output_node_index = hn->output_node_index;
727 vlib_node_rename (vm, hw->tx_node_index, "%v", tx_node_name);
728 vlib_node_rename (vm, hw->output_node_index, "%v", output_node_name);
730 rt = vlib_node_get_runtime_data (vm, hw->output_node_index);
731 ASSERT (rt->is_deleted == 1);
733 rt->hw_if_index = hw_index;
734 rt->sw_if_index = hw->sw_if_index;
735 rt->dev_instance = hw->dev_instance;
737 rt = vlib_node_get_runtime_data (vm, hw->tx_node_index);
738 rt->hw_if_index = hw_index;
739 rt->sw_if_index = hw->sw_if_index;
740 rt->dev_instance = hw->dev_instance;
742 vlib_worker_thread_node_runtime_update ();
743 _vec_len (im->deleted_hw_interface_nodes) -= 1;
747 vlib_node_registration_t r;
748 vnet_interface_output_runtime_t rt = {
749 .hw_if_index = hw_index,
750 .sw_if_index = hw->sw_if_index,
751 .dev_instance = hw->dev_instance,
755 memset (&r, 0, sizeof (r));
756 r.type = VLIB_NODE_TYPE_INTERNAL;
757 r.runtime_data = &rt;
758 r.runtime_data_bytes = sizeof (rt);
760 r.vector_size = sizeof (u32);
762 r.flags = VLIB_NODE_FLAG_IS_OUTPUT;
763 r.name = tx_node_name;
764 r.function = dev_class->tx_function;
766 hw->tx_node_index = vlib_register_node (vm, &r);
768 vlib_node_add_named_next_with_slot (vm, hw->tx_node_index,
770 VNET_INTERFACE_TX_NEXT_DROP);
773 r.name = output_node_name;
774 r.function = dev_class->no_flatten_output_chains ?
775 vnet_interface_output_node_no_flatten_multiarch_select () :
776 vnet_interface_output_node_multiarch_select ();
777 r.format_trace = format_vnet_interface_output_trace;
782 "interface is deleted",
785 r.n_errors = ARRAY_LEN (e);
788 hw->output_node_index = vlib_register_node (vm, &r);
790 #define _(sym,str) vlib_node_add_named_next_with_slot (vm, \
791 hw->output_node_index, str, \
792 VNET_INTERFACE_OUTPUT_NEXT_##sym);
793 foreach_intf_output_feat
795 vlib_node_add_named_next_with_slot (vm, hw->output_node_index,
797 VNET_INTERFACE_OUTPUT_NEXT_DROP);
798 vlib_node_add_next_with_slot (vm, hw->output_node_index,
800 VNET_INTERFACE_OUTPUT_NEXT_TX);
803 setup_output_node (vm, hw->output_node_index, hw_class);
804 setup_tx_node (vm, hw->tx_node_index, dev_class);
806 /* Call all up/down callbacks with zero flags when interface is created. */
807 vnet_sw_interface_set_flags_helper (vnm, hw->sw_if_index, /* flags */ 0,
808 VNET_INTERFACE_SET_FLAGS_HELPER_IS_CREATE);
809 vnet_hw_interface_set_flags_helper (vnm, hw_index, /* flags */ 0,
810 VNET_INTERFACE_SET_FLAGS_HELPER_IS_CREATE);
816 vnet_delete_hw_interface (vnet_main_t * vnm, u32 hw_if_index)
818 vnet_interface_main_t *im = &vnm->interface_main;
819 vnet_hw_interface_t *hw = vnet_get_hw_interface (vnm, hw_if_index);
820 vlib_main_t *vm = vnm->vlib_main;
822 /* If it is up, mark it down. */
824 vnet_hw_interface_set_flags (vnm, hw_if_index, /* flags */ 0);
826 /* Call delete callbacks. */
827 call_hw_interface_add_del_callbacks (vnm, hw_if_index, /* is_create */ 0);
829 /* Delete software interface corresponding to hardware interface. */
830 vnet_delete_sw_interface (vnm, hw->sw_if_index);
832 /* Delete any sub-interfaces. */
836 hash_foreach (id, sw_if_index, hw->sub_interface_sw_if_index_by_id, ({
837 vnet_delete_sw_interface (vnm, sw_if_index);
843 vnet_hw_interface_nodes_t *dn;
844 vnet_interface_output_runtime_t *rt =
845 vlib_node_get_runtime_data (vm, hw->output_node_index);
847 /* Mark node runtime as deleted so output node (if called) will drop packets. */
850 vlib_node_rename (vm, hw->output_node_index,
851 "interface-%d-output-deleted", hw_if_index);
852 vlib_node_rename (vm, hw->tx_node_index, "interface-%d-tx-deleted",
854 vec_add2 (im->deleted_hw_interface_nodes, dn, 1);
855 dn->tx_node_index = hw->tx_node_index;
856 dn->output_node_index = hw->output_node_index;
859 hash_unset_mem (im->hw_interface_by_name, hw->name);
862 pool_put (im->hw_interfaces, hw);
866 serialize_vnet_hw_interface_set_class (serialize_main_t * m, va_list * va)
868 u32 hw_if_index = va_arg (*va, u32);
869 char *hw_class_name = va_arg (*va, char *);
870 serialize_integer (m, hw_if_index, sizeof (hw_if_index));
871 serialize_cstring (m, hw_class_name);
875 unserialize_vnet_hw_interface_set_class (serialize_main_t * m, va_list * va)
877 CLIB_UNUSED (mc_main_t * mc) = va_arg (*va, mc_main_t *);
878 vnet_main_t *vnm = vnet_get_main ();
884 unserialize_integer (m, &hw_if_index, sizeof (hw_if_index));
885 unserialize_cstring (m, &hw_class_name);
887 hash_get (vnm->interface_main.hw_interface_class_by_name, hw_class_name);
889 error = vnet_hw_interface_set_class_helper (vnm, hw_if_index, p[0],
890 /* redistribute */ 0);
892 clib_error_report (error);
895 MC_SERIALIZE_MSG (vnet_hw_interface_set_class_msg, static) =
897 .name = "vnet_hw_interface_set_class",.serialize =
898 serialize_vnet_hw_interface_set_class,.unserialize =
899 unserialize_vnet_hw_interface_set_class,};
902 vnet_hw_interface_init_for_class (vnet_main_t * vnm, u32 hw_if_index,
903 u32 hw_class_index, u32 hw_instance)
905 vnet_hw_interface_t *hi = vnet_get_hw_interface (vnm, hw_if_index);
906 vnet_hw_interface_class_t *hc =
907 vnet_get_hw_interface_class (vnm, hw_class_index);
909 hi->hw_class_index = hw_class_index;
910 hi->hw_instance = hw_instance;
911 setup_output_node (vnm->vlib_main, hi->output_node_index, hc);
914 static clib_error_t *
915 vnet_hw_interface_set_class_helper (vnet_main_t * vnm, u32 hw_if_index,
916 u32 hw_class_index, u32 redistribute)
918 vnet_hw_interface_t *hi = vnet_get_hw_interface (vnm, hw_if_index);
919 vnet_sw_interface_t *si = vnet_get_sw_interface (vnm, hi->sw_if_index);
920 vnet_hw_interface_class_t *old_class =
921 vnet_get_hw_interface_class (vnm, hi->hw_class_index);
922 vnet_hw_interface_class_t *new_class =
923 vnet_get_hw_interface_class (vnm, hw_class_index);
924 vnet_device_class_t *dev_class =
925 vnet_get_device_class (vnm, hi->dev_class_index);
926 clib_error_t *error = 0;
928 /* New class equals old class? Nothing to do. */
929 if (hi->hw_class_index == hw_class_index)
932 /* No need (and incorrect since admin up flag may be set) to do error checking when
933 receiving unserialize message. */
936 if (si->flags & VNET_SW_INTERFACE_FLAG_ADMIN_UP)
937 return clib_error_return (0,
938 "%v must be admin down to change class from %s to %s",
939 hi->name, old_class->name, new_class->name);
941 /* Make sure interface supports given class. */
942 if ((new_class->is_valid_class_for_interface
943 && !new_class->is_valid_class_for_interface (vnm, hw_if_index,
945 || (dev_class->is_valid_class_for_interface
946 && !dev_class->is_valid_class_for_interface (vnm, hw_if_index,
948 return clib_error_return (0,
949 "%v class cannot be changed from %s to %s",
950 hi->name, old_class->name, new_class->name);
952 if (vnm->vlib_main->mc_main)
954 mc_serialize (vnm->vlib_main->mc_main,
955 &vnet_hw_interface_set_class_msg, hw_if_index,
961 if (old_class->hw_class_change)
962 old_class->hw_class_change (vnm, hw_if_index, old_class->index,
965 vnet_hw_interface_init_for_class (vnm, hw_if_index, new_class->index,
968 if (new_class->hw_class_change)
969 new_class->hw_class_change (vnm, hw_if_index, old_class->index,
972 if (dev_class->hw_class_change)
973 dev_class->hw_class_change (vnm, hw_if_index, new_class->index);
979 vnet_hw_interface_set_class (vnet_main_t * vnm, u32 hw_if_index,
982 return vnet_hw_interface_set_class_helper (vnm, hw_if_index, hw_class_index,
983 /* redistribute */ 1);
987 vnet_hw_interface_rx_redirect_to_node_helper (vnet_main_t * vnm,
992 vnet_hw_interface_t *hi = vnet_get_hw_interface (vnm, hw_if_index);
993 vnet_device_class_t *dev_class = vnet_get_device_class
994 (vnm, hi->dev_class_index);
998 /* $$$$ fixme someday maybe */
999 ASSERT (vnm->vlib_main->mc_main == 0);
1001 if (dev_class->rx_redirect_to_node)
1003 dev_class->rx_redirect_to_node (vnm, hw_if_index, node_index);
1007 return VNET_API_ERROR_UNIMPLEMENTED;
1011 vnet_hw_interface_rx_redirect_to_node (vnet_main_t * vnm, u32 hw_if_index,
1014 return vnet_hw_interface_rx_redirect_to_node_helper (vnm, hw_if_index,
1016 1 /* redistribute */ );
1020 vnet_sw_interface_compare (vnet_main_t * vnm,
1021 uword sw_if_index0, uword sw_if_index1)
1023 vnet_sw_interface_t *sup0 = vnet_get_sup_sw_interface (vnm, sw_if_index0);
1024 vnet_sw_interface_t *sup1 = vnet_get_sup_sw_interface (vnm, sw_if_index1);
1025 vnet_hw_interface_t *h0 = vnet_get_hw_interface (vnm, sup0->hw_if_index);
1026 vnet_hw_interface_t *h1 = vnet_get_hw_interface (vnm, sup1->hw_if_index);
1029 return vec_cmp (h0->name, h1->name);
1030 return (word) h0->hw_instance - (word) h1->hw_instance;
1034 vnet_hw_interface_compare (vnet_main_t * vnm,
1035 uword hw_if_index0, uword hw_if_index1)
1037 vnet_hw_interface_t *h0 = vnet_get_hw_interface (vnm, hw_if_index0);
1038 vnet_hw_interface_t *h1 = vnet_get_hw_interface (vnm, hw_if_index1);
1041 return vec_cmp (h0->name, h1->name);
1042 return (word) h0->hw_instance - (word) h1->hw_instance;
1046 vnet_interface_init (vlib_main_t * vm)
1048 vnet_main_t *vnm = vnet_get_main ();
1049 vnet_interface_main_t *im = &vnm->interface_main;
1050 vlib_buffer_t *b = 0;
1051 vnet_buffer_opaque_t *o = 0;
1054 * Keep people from shooting themselves in the foot.
1056 if (sizeof (b->opaque) != sizeof (vnet_buffer_opaque_t))
1058 #define _(a) if (sizeof(o->a) > sizeof (o->unused)) \
1060 ("FATAL: size of opaque union subtype %s is %d (max %d)", \
1061 #a, sizeof(o->a), sizeof (o->unused));
1062 foreach_buffer_opaque_union_subtype;
1065 return clib_error_return
1066 (0, "FATAL: size of vlib buffer opaque %d, size of vnet opaque %d",
1067 sizeof (b->opaque), sizeof (vnet_buffer_opaque_t));
1070 im->sw_if_counter_lock = clib_mem_alloc_aligned (CLIB_CACHE_LINE_BYTES,
1071 CLIB_CACHE_LINE_BYTES);
1072 im->sw_if_counter_lock[0] = 1; /* should be no need */
1074 vec_validate (im->sw_if_counters, VNET_N_SIMPLE_INTERFACE_COUNTER - 1);
1075 im->sw_if_counters[VNET_INTERFACE_COUNTER_DROP].name = "drops";
1076 im->sw_if_counters[VNET_INTERFACE_COUNTER_PUNT].name = "punts";
1077 im->sw_if_counters[VNET_INTERFACE_COUNTER_IP4].name = "ip4";
1078 im->sw_if_counters[VNET_INTERFACE_COUNTER_IP6].name = "ip6";
1079 im->sw_if_counters[VNET_INTERFACE_COUNTER_RX_NO_BUF].name = "rx-no-buf";
1080 im->sw_if_counters[VNET_INTERFACE_COUNTER_RX_MISS].name = "rx-miss";
1081 im->sw_if_counters[VNET_INTERFACE_COUNTER_RX_ERROR].name = "rx-error";
1082 im->sw_if_counters[VNET_INTERFACE_COUNTER_TX_ERROR].name = "tx-error";
1084 vec_validate (im->combined_sw_if_counters,
1085 VNET_N_COMBINED_INTERFACE_COUNTER - 1);
1086 im->combined_sw_if_counters[VNET_INTERFACE_COUNTER_RX].name = "rx";
1087 im->combined_sw_if_counters[VNET_INTERFACE_COUNTER_TX].name = "tx";
1089 im->sw_if_counter_lock[0] = 0;
1091 im->device_class_by_name = hash_create_string ( /* size */ 0,
1094 vnet_device_class_t *c;
1096 c = vnm->device_class_registrations;
1100 c->index = vec_len (im->device_classes);
1101 hash_set_mem (im->device_class_by_name, c->name, c->index);
1102 vec_add1 (im->device_classes, c[0]);
1103 c = c->next_class_registration;
1107 im->hw_interface_class_by_name = hash_create_string ( /* size */ 0,
1110 im->sw_if_index_by_sup_and_sub = hash_create_mem (0, sizeof (u64),
1113 vnet_hw_interface_class_t *c;
1115 c = vnm->hw_interface_class_registrations;
1119 c->index = vec_len (im->hw_interface_classes);
1120 hash_set_mem (im->hw_interface_class_by_name, c->name, c->index);
1121 vec_add1 (im->hw_interface_classes, c[0]);
1122 c = c->next_class_registration;
1127 clib_error_t *error;
1129 if ((error = vlib_call_init_function (vm, vnet_interface_cli_init)))
1136 VLIB_INIT_FUNCTION (vnet_interface_init);
1138 /* Kludge to renumber interface names [only!] */
1140 vnet_interface_name_renumber (u32 sw_if_index, u32 new_show_dev_instance)
1143 vnet_main_t *vnm = vnet_get_main ();
1144 vnet_interface_main_t *im = &vnm->interface_main;
1145 vnet_hw_interface_t *hi = vnet_get_sup_hw_interface (vnm, sw_if_index);
1147 vnet_device_class_t *dev_class = vnet_get_device_class
1148 (vnm, hi->dev_class_index);
1150 if (dev_class->name_renumber == 0 || dev_class->format_device_name == 0)
1151 return VNET_API_ERROR_UNIMPLEMENTED;
1153 rv = dev_class->name_renumber (hi, new_show_dev_instance);
1158 hash_unset_mem (im->hw_interface_by_name, hi->name);
1159 vec_free (hi->name);
1160 /* Use the mapping we set up to call it Ishmael */
1161 hi->name = format (0, "%U", dev_class->format_device_name,
1164 hash_set_mem (im->hw_interface_by_name, hi->name, hi->hw_if_index);
1169 vnet_interface_add_del_feature (vnet_main_t * vnm,
1172 intf_output_feat_t feature, int is_add)
1174 vnet_sw_interface_t *sw;
1176 sw = vnet_get_sw_interface (vnm, sw_if_index);
1181 sw->output_feature_bitmap |= (1 << feature);
1182 sw->output_feature_bitmap |= (1 << INTF_OUTPUT_FEAT_DONE);
1188 sw->output_feature_bitmap &= ~(1 << feature);
1189 if (sw->output_feature_bitmap == (1 << INTF_OUTPUT_FEAT_DONE))
1190 sw->output_feature_bitmap = 0;
1197 vnet_rename_interface (vnet_main_t * vnm, u32 hw_if_index, char *new_name)
1199 vnet_interface_main_t *im = &vnm->interface_main;
1200 vlib_main_t *vm = vnm->vlib_main;
1201 vnet_hw_interface_t *hw;
1203 clib_error_t *error = 0;
1205 hw = vnet_get_hw_interface (vnm, hw_if_index);
1208 return clib_error_return (0,
1209 "unable to find hw interface for index %u",
1213 old_name = hw->name;
1215 /* set new hw->name */
1216 hw->name = format (0, "%s", new_name);
1218 /* remove the old name to hw_if_index mapping and install the new one */
1219 hash_unset_mem (im->hw_interface_by_name, old_name);
1220 hash_set_mem (im->hw_interface_by_name, hw->name, hw_if_index);
1222 /* rename tx/output nodes */
1223 vlib_node_rename (vm, hw->tx_node_index, "%v-tx", hw->name);
1224 vlib_node_rename (vm, hw->output_node_index, "%v-output", hw->name);
1226 /* free the old name vector */
1227 vec_free (old_name);
1233 * fd.io coding-style-patch-verification: ON
1236 * eval: (c-set-style "gnu")