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)
47 vnet_hw_interface_set_flags_helper (vnet_main_t * vnm, u32 hw_if_index, u32 flags,
51 vnet_sw_interface_set_flags_helper (vnet_main_t * vnm, u32 sw_if_index, u32 flags,
55 vnet_hw_interface_set_class_helper (vnet_main_t * vnm, u32 hw_if_index, u32 hw_class_index, u32 redistribute);
58 /* Either sw or hw interface index. */
63 } vnet_sw_hw_interface_state_t;
65 static void serialize_vec_vnet_sw_hw_interface_state (serialize_main_t * m, va_list * va)
67 vnet_sw_hw_interface_state_t * s = va_arg (*va, vnet_sw_hw_interface_state_t *);
68 u32 n = va_arg (*va, u32);
70 for (i = 0; i < n; i++) {
71 serialize_integer (m, s[i].sw_hw_if_index, sizeof (s[i].sw_hw_if_index));
72 serialize_integer (m, s[i].flags, sizeof (s[i].flags));
76 static void unserialize_vec_vnet_sw_hw_interface_state (serialize_main_t * m, va_list * va)
78 vnet_sw_hw_interface_state_t * s = va_arg (*va, vnet_sw_hw_interface_state_t *);
79 u32 n = va_arg (*va, u32);
81 for (i = 0; i < n; i++) {
82 unserialize_integer (m, &s[i].sw_hw_if_index, sizeof (s[i].sw_hw_if_index));
83 unserialize_integer (m, &s[i].flags, sizeof (s[i].flags));
87 static void serialize_vnet_sw_hw_interface_set_flags (serialize_main_t * m, va_list * va)
89 vnet_sw_hw_interface_state_t * s = va_arg (*va, vnet_sw_hw_interface_state_t *);
90 serialize (m, serialize_vec_vnet_sw_hw_interface_state, s, 1);
93 static void unserialize_vnet_sw_interface_set_flags (serialize_main_t * m, va_list * va)
95 CLIB_UNUSED (mc_main_t * mc) = va_arg (*va, mc_main_t *);
96 vnet_sw_hw_interface_state_t s;
98 unserialize (m, unserialize_vec_vnet_sw_hw_interface_state, &s, 1);
100 vnet_sw_interface_set_flags_helper
101 (vnet_get_main(), s.sw_hw_if_index, s.flags,
102 /* helper_flags no redistribution */ 0);
105 static void unserialize_vnet_hw_interface_set_flags (serialize_main_t * m, va_list * va)
107 CLIB_UNUSED (mc_main_t * mc) = va_arg (*va, mc_main_t *);
108 vnet_sw_hw_interface_state_t s;
110 unserialize (m, unserialize_vec_vnet_sw_hw_interface_state, &s, 1);
112 vnet_hw_interface_set_flags_helper
113 (vnet_get_main(), s.sw_hw_if_index, s.flags,
114 /* helper_flags no redistribution */ 0);
117 MC_SERIALIZE_MSG (vnet_sw_interface_set_flags_msg, static) = {
118 .name = "vnet_sw_interface_set_flags",
119 .serialize = serialize_vnet_sw_hw_interface_set_flags,
120 .unserialize = unserialize_vnet_sw_interface_set_flags,
123 MC_SERIALIZE_MSG (vnet_hw_interface_set_flags_msg, static) = {
124 .name = "vnet_hw_interface_set_flags",
125 .serialize = serialize_vnet_sw_hw_interface_set_flags,
126 .unserialize = unserialize_vnet_hw_interface_set_flags,
129 void serialize_vnet_interface_state (serialize_main_t * m, va_list * va)
131 vnet_main_t * vnm = va_arg (*va, vnet_main_t *);
132 vnet_sw_hw_interface_state_t * sts = 0, * st;
133 vnet_sw_interface_t * sif;
134 vnet_hw_interface_t * hif;
135 vnet_interface_main_t * im = &vnm->interface_main;
137 /* Serialize hardware interface classes since they may have changed.
138 Must do this before sending up/down flags. */
139 pool_foreach (hif, im->hw_interfaces, ({
140 vnet_hw_interface_class_t * hw_class = vnet_get_hw_interface_class (vnm, hif->hw_class_index);
141 serialize_cstring (m, hw_class->name);
144 /* Send sw/hw interface state when non-zero. */
145 pool_foreach (sif, im->sw_interfaces, ({
148 vec_add2 (sts, st, 1);
149 st->sw_hw_if_index = sif->sw_if_index;
150 st->flags = sif->flags;
154 vec_serialize (m, sts, serialize_vec_vnet_sw_hw_interface_state);
159 pool_foreach (hif, im->hw_interfaces, ({
162 vec_add2 (sts, st, 1);
163 st->sw_hw_if_index = hif->hw_if_index;
164 st->flags = hif->flags;
168 vec_serialize (m, sts, serialize_vec_vnet_sw_hw_interface_state);
173 void unserialize_vnet_interface_state (serialize_main_t * m, va_list * va)
175 vnet_main_t * vnm = va_arg (*va, vnet_main_t *);
176 vnet_sw_hw_interface_state_t * sts = 0, * st;
178 /* First set interface hardware class. */
180 vnet_interface_main_t * im = &vnm->interface_main;
181 vnet_hw_interface_t * hif;
184 clib_error_t * error;
186 pool_foreach (hif, im->hw_interfaces, ({
187 unserialize_cstring (m, &class_name);
188 p = hash_get_mem (im->hw_interface_class_by_name, class_name);
190 error = vnet_hw_interface_set_class_helper (vnm, hif->hw_if_index, p[0], /* redistribute */ 0);
192 clib_error_report (error);
193 vec_free (class_name);
197 vec_unserialize (m, &sts, unserialize_vec_vnet_sw_hw_interface_state);
198 vec_foreach (st, sts)
199 vnet_sw_interface_set_flags_helper (vnm, st->sw_hw_if_index, st->flags,
200 /* no distribute */ 0);
203 vec_unserialize (m, &sts, unserialize_vec_vnet_sw_hw_interface_state);
204 vec_foreach (st, sts)
205 vnet_hw_interface_set_flags_helper (vnm, st->sw_hw_if_index, st->flags,
206 /* no distribute */ 0);
210 static clib_error_t *
211 call_elf_section_interface_callbacks (vnet_main_t * vnm, u32 if_index,
213 _vnet_interface_function_list_elt_t *elt)
215 clib_error_t * error = 0;
219 error = elt->fp(vnm, if_index, flags);
222 elt = elt->next_interface_function;
227 static clib_error_t *
228 call_hw_interface_add_del_callbacks (vnet_main_t * vnm, u32 hw_if_index, u32 is_create)
230 vnet_hw_interface_t * hi = vnet_get_hw_interface (vnm, hw_if_index);
231 vnet_hw_interface_class_t * hw_class = vnet_get_hw_interface_class (vnm, hi->hw_class_index);
232 vnet_device_class_t * dev_class = vnet_get_device_class (vnm, hi->dev_class_index);
233 clib_error_t * error = 0;
235 if (hw_class->interface_add_del_function
236 && (error = hw_class->interface_add_del_function (vnm, hw_if_index, is_create)))
239 if (dev_class->interface_add_del_function
240 && (error = dev_class->interface_add_del_function (vnm, hw_if_index, is_create)))
243 error = call_elf_section_interface_callbacks
244 (vnm, hw_if_index, is_create, vnm->hw_interface_add_del_functions);
249 static clib_error_t *
250 call_sw_interface_add_del_callbacks (vnet_main_t * vnm, u32 sw_if_index, u32 is_create)
252 return call_elf_section_interface_callbacks
253 (vnm, sw_if_index, is_create, vnm->sw_interface_add_del_functions);
256 #define VNET_INTERFACE_SET_FLAGS_HELPER_IS_CREATE (1 << 0)
257 #define VNET_INTERFACE_SET_FLAGS_HELPER_WANT_REDISTRIBUTE (1 << 1)
259 static clib_error_t *
260 vnet_hw_interface_set_flags_helper (vnet_main_t * vnm, u32 hw_if_index, u32 flags,
263 vnet_hw_interface_t * hi = vnet_get_hw_interface (vnm, hw_if_index);
264 vnet_hw_interface_class_t * hw_class = vnet_get_hw_interface_class (vnm, hi->hw_class_index);
265 vnet_device_class_t * dev_class = vnet_get_device_class (vnm, hi->dev_class_index);
266 vlib_main_t * vm = vnm->vlib_main;
268 clib_error_t * error = 0;
269 u32 is_create = (helper_flags & VNET_INTERFACE_SET_FLAGS_HELPER_IS_CREATE) != 0;
271 mask = (VNET_HW_INTERFACE_FLAG_LINK_UP | VNET_HW_INTERFACE_FLAG_DUPLEX_MASK |
272 VNET_HW_INTERFACE_FLAG_SPEED_MASK);
275 /* Call hardware interface add/del callbacks. */
277 call_hw_interface_add_del_callbacks (vnm, hw_if_index, is_create);
279 /* Already in the desired state? */
280 if (! is_create && (hi->flags & mask) == flags)
283 /* Some interface classes do not redistribute (e.g. are local). */
284 if (! dev_class->redistribute)
285 helper_flags &= ~ VNET_INTERFACE_SET_FLAGS_HELPER_WANT_REDISTRIBUTE;
288 && (helper_flags & VNET_INTERFACE_SET_FLAGS_HELPER_WANT_REDISTRIBUTE))
290 vnet_sw_hw_interface_state_t s;
291 s.sw_hw_if_index = hw_if_index;
293 mc_serialize (vm->mc_main, &vnet_hw_interface_set_flags_msg, &s);
296 if ((hi->flags & VNET_HW_INTERFACE_FLAG_LINK_UP) !=
297 (flags & VNET_HW_INTERFACE_FLAG_LINK_UP))
299 /* Do hardware class (e.g. ethernet). */
300 if (hw_class->link_up_down_function
301 && (error = hw_class->link_up_down_function (vnm, hw_if_index,
305 error = call_elf_section_interface_callbacks
306 (vnm, hw_if_index, is_create, vnm->hw_interface_link_up_down_functions);
319 static clib_error_t *
320 vnet_sw_interface_set_flags_helper (vnet_main_t * vnm, u32 sw_if_index, u32 flags,
323 vnet_sw_interface_t * si = vnet_get_sw_interface (vnm, sw_if_index);
324 vlib_main_t * vm = vnm->vlib_main;
326 clib_error_t * error = 0;
327 u32 is_create = (helper_flags & VNET_INTERFACE_SET_FLAGS_HELPER_IS_CREATE) != 0;
330 mask = VNET_SW_INTERFACE_FLAG_ADMIN_UP | VNET_SW_INTERFACE_FLAG_PUNT;
335 error = call_sw_interface_add_del_callbacks (vnm, sw_if_index, is_create);
339 if (flags & VNET_SW_INTERFACE_FLAG_ADMIN_UP)
341 /* Notify everyone when the interface is created as admin up */
342 error = call_elf_section_interface_callbacks (vnm, sw_if_index,
343 flags, vnm->sw_interface_admin_up_down_functions);
350 vnet_sw_interface_t * si_sup = si;
352 /* Check that super interface is in correct state. */
353 if (si->type == VNET_SW_INTERFACE_TYPE_SUB)
355 si_sup = vnet_get_sw_interface (vnm, si->sup_sw_if_index);
357 if (flags != (si_sup->flags & mask))
359 error = clib_error_return (0, "super-interface %U must be %U",
360 format_vnet_sw_interface_name, vnm, si_sup,
361 format_vnet_sw_interface_flags, flags);
366 /* Already in the desired state? */
367 if ((si->flags & mask) == flags)
370 /* Sub-interfaces of hardware interfaces that do no redistribute,
371 do not redistribute themselves. */
372 if (si_sup->type == VNET_SW_INTERFACE_TYPE_HARDWARE)
374 vnet_hw_interface_t * hi = vnet_get_hw_interface (vnm, si_sup->hw_if_index);
375 vnet_device_class_t * dev_class = vnet_get_device_class (vnm, hi->dev_class_index);
376 if (! dev_class->redistribute)
377 helper_flags &= ~ VNET_INTERFACE_SET_FLAGS_HELPER_WANT_REDISTRIBUTE;
381 && (helper_flags & VNET_INTERFACE_SET_FLAGS_HELPER_WANT_REDISTRIBUTE))
383 vnet_sw_hw_interface_state_t s;
384 s.sw_hw_if_index = sw_if_index;
386 mc_serialize (vm->mc_main, &vnet_sw_interface_set_flags_msg, &s);
389 error = call_elf_section_interface_callbacks
390 (vnm, sw_if_index, flags, vnm->sw_interface_admin_up_down_functions);
395 if (si->type == VNET_SW_INTERFACE_TYPE_HARDWARE)
397 vnet_hw_interface_t * hi = vnet_get_hw_interface (vnm, si->hw_if_index);
398 vnet_hw_interface_class_t * hw_class = vnet_get_hw_interface_class (vnm, hi->hw_class_index);
399 vnet_device_class_t * dev_class = vnet_get_device_class (vnm, hi->dev_class_index);
401 /* save the si admin up flag */
402 old_flags = si->flags;
404 /* update si admin up flag in advance if we are going admin down */
405 if (!(flags & VNET_SW_INTERFACE_FLAG_ADMIN_UP))
406 si->flags &= ~VNET_SW_INTERFACE_FLAG_ADMIN_UP;
408 if (dev_class->admin_up_down_function
409 && (error = dev_class->admin_up_down_function(vnm,
413 /* restore si admin up flag to it's original state on errors */
414 si->flags = old_flags;
418 if (hw_class->admin_up_down_function
419 && (error = hw_class->admin_up_down_function(vnm,
423 /* restore si admin up flag to it's original state on errors */
424 si->flags = old_flags;
428 /* Admin down implies link down. */
429 if (! (flags & VNET_SW_INTERFACE_FLAG_ADMIN_UP)
430 && (hi->flags & VNET_HW_INTERFACE_FLAG_LINK_UP))
431 vnet_hw_interface_set_flags_helper (vnm, si->hw_if_index,
432 hi->flags &~ VNET_HW_INTERFACE_FLAG_LINK_UP,
445 vnet_hw_interface_set_flags (vnet_main_t * vnm, u32 hw_if_index, u32 flags)
447 return vnet_hw_interface_set_flags_helper
448 (vnm, hw_if_index, flags,
449 VNET_INTERFACE_SET_FLAGS_HELPER_WANT_REDISTRIBUTE);
453 vnet_sw_interface_set_flags (vnet_main_t * vnm, u32 sw_if_index, u32 flags)
455 return vnet_sw_interface_set_flags_helper
456 (vnm, sw_if_index, flags,
457 VNET_INTERFACE_SET_FLAGS_HELPER_WANT_REDISTRIBUTE);
461 vnet_create_sw_interface_no_callbacks (vnet_main_t * vnm, vnet_sw_interface_t * template)
463 vnet_interface_main_t * im = &vnm->interface_main;
464 vnet_sw_interface_t * sw;
467 pool_get (im->sw_interfaces, sw);
468 sw_if_index = sw - im->sw_interfaces;
473 sw->sw_if_index = sw_if_index;
474 if (sw->type == VNET_SW_INTERFACE_TYPE_HARDWARE)
475 sw->sup_sw_if_index = sw->sw_if_index;
477 /* Allocate counters for this interface. */
481 vnet_interface_counter_lock(im);
483 for (i = 0; i < vec_len (im->sw_if_counters); i++)
485 vlib_validate_simple_counter (&im->sw_if_counters[i], sw_if_index);
486 vlib_zero_simple_counter (&im->sw_if_counters[i], sw_if_index);
489 for (i = 0; i < vec_len (im->combined_sw_if_counters); i++)
491 vlib_validate_combined_counter (&im->combined_sw_if_counters[i],
493 vlib_zero_combined_counter (&im->combined_sw_if_counters[i],
497 vnet_interface_counter_unlock(im);
504 vnet_create_sw_interface (vnet_main_t * vnm, vnet_sw_interface_t * template, u32 * sw_if_index)
506 clib_error_t * error;
507 vnet_hw_interface_t * hi;
508 vnet_device_class_t * dev_class;
510 hi = vnet_get_sup_hw_interface (vnm, template->sup_sw_if_index);
511 dev_class = vnet_get_device_class (vnm, hi->dev_class_index);
513 if (template->type == VNET_SW_INTERFACE_TYPE_SUB &&
514 dev_class->subif_add_del_function) {
515 error = dev_class->subif_add_del_function (vnm, hi->hw_if_index,
516 (struct vnet_sw_interface_t *) template, 1);
521 *sw_if_index = vnet_create_sw_interface_no_callbacks (vnm, template);
522 error = vnet_sw_interface_set_flags_helper
523 (vnm, *sw_if_index, template->flags,
524 VNET_INTERFACE_SET_FLAGS_HELPER_IS_CREATE);
527 // undo the work done by vnet_create_sw_interface_no_callbacks()
528 vnet_interface_main_t * im = &vnm->interface_main;
529 vnet_sw_interface_t * sw = pool_elt_at_index (im->sw_interfaces, *sw_if_index);
530 pool_put (im->sw_interfaces, sw);
536 void vnet_delete_sw_interface (vnet_main_t * vnm, u32 sw_if_index)
538 vnet_interface_main_t * im = &vnm->interface_main;
539 vnet_sw_interface_t * sw = pool_elt_at_index (im->sw_interfaces, sw_if_index);
541 /* Bring down interface in case it is up. */
543 vnet_sw_interface_set_flags (vnm, sw_if_index, /* flags */ 0);
545 call_sw_interface_add_del_callbacks (vnm, sw_if_index, /* is_create */ 0);
547 pool_put (im->sw_interfaces, sw);
550 static void setup_tx_node (vlib_main_t * vm,
552 vnet_device_class_t * dev_class)
554 vlib_node_t * n = vlib_get_node (vm, node_index);
556 n->function = dev_class->tx_function;
557 n->format_trace = dev_class->format_tx_trace;
558 vlib_register_errors (vm, node_index,
559 dev_class->tx_function_n_errors,
560 dev_class->tx_function_error_strings);
563 static void setup_output_node (vlib_main_t * vm,
565 vnet_hw_interface_class_t * hw_class)
567 vlib_node_t * n = vlib_get_node (vm, node_index);
568 n->format_buffer = hw_class->format_header;
569 n->unformat_buffer = hw_class->unformat_header;
572 /* Register an interface instance. */
574 vnet_register_interface (vnet_main_t * vnm,
580 vnet_interface_main_t * im = &vnm->interface_main;
581 vnet_hw_interface_t * hw;
582 vnet_device_class_t * dev_class = vnet_get_device_class (vnm, dev_class_index);
583 vnet_hw_interface_class_t * hw_class = vnet_get_hw_interface_class (vnm, hw_class_index);
584 vlib_main_t * vm = vnm->vlib_main;
586 char * tx_node_name, * output_node_name;
588 pool_get (im->hw_interfaces, hw);
590 hw_index = hw - im->hw_interfaces;
591 hw->hw_if_index = hw_index;
593 if (dev_class->format_device_name)
594 hw->name = format (0, "%U",
595 dev_class->format_device_name, dev_instance);
596 else if (hw_class->format_interface_name)
597 hw->name = format (0, "%U", hw_class->format_interface_name,
600 hw->name = format (0, "%s%x", hw_class->name, dev_instance);
602 if (! im->hw_interface_by_name)
603 im->hw_interface_by_name = hash_create_vec (/* size */ 0,
604 sizeof (hw->name[0]),
607 hash_set_mem (im->hw_interface_by_name, hw->name, hw_index);
609 /* Make hardware interface point to software interface. */
611 vnet_sw_interface_t sw;
613 memset (&sw, 0, sizeof (sw));
614 sw.type = VNET_SW_INTERFACE_TYPE_HARDWARE;
615 sw.hw_if_index = hw_index;
616 hw->sw_if_index = vnet_create_sw_interface_no_callbacks (vnm, &sw);
619 hw->dev_class_index = dev_class_index;
620 hw->dev_instance = dev_instance;
621 hw->hw_class_index = hw_class_index;
622 hw->hw_instance = hw_instance;
624 hw->max_rate_bits_per_sec = 0;
625 hw->min_packet_bytes = 0;
626 hw->per_packet_overhead_bytes = 0;
627 hw->max_l3_packet_bytes[VLIB_RX] = ~0;
628 hw->max_l3_packet_bytes[VLIB_TX] = ~0;
630 tx_node_name = (char *) format (0, "%v-tx", hw->name);
631 output_node_name = (char *) format (0, "%v-output", hw->name);
633 /* If we have previously deleted interface nodes, re-use them. */
634 if (vec_len (im->deleted_hw_interface_nodes) > 0)
636 vnet_hw_interface_nodes_t * hn;
637 vnet_interface_output_runtime_t * rt;
639 hn = vec_end (im->deleted_hw_interface_nodes) - 1;
641 hw->tx_node_index = hn->tx_node_index;
642 hw->output_node_index = hn->output_node_index;
644 vlib_node_rename (vm, hw->tx_node_index, "%v", tx_node_name);
645 vlib_node_rename (vm, hw->output_node_index, "%v", output_node_name);
647 rt = vlib_node_get_runtime_data (vm, hw->output_node_index);
648 ASSERT (rt->is_deleted == 1);
651 _vec_len (im->deleted_hw_interface_nodes) -= 1;
655 vlib_node_registration_t r;
656 vnet_interface_output_runtime_t rt = {
657 .hw_if_index = hw_index,
658 .sw_if_index = hw->sw_if_index,
659 .dev_instance = hw->dev_instance,
663 memset (&r, 0, sizeof (r));
664 r.type = VLIB_NODE_TYPE_INTERNAL;
665 r.runtime_data = &rt;
666 r.runtime_data_bytes = sizeof (rt);
668 r.vector_size = sizeof (u32);
670 r.flags = VLIB_NODE_FLAG_IS_OUTPUT;
671 r.name = tx_node_name;
672 r.function = dev_class->tx_function;
674 hw->tx_node_index = vlib_register_node (vm, &r);
676 vlib_node_add_named_next_with_slot (vm, hw->tx_node_index,
678 VNET_INTERFACE_TX_NEXT_DROP);
681 r.name = output_node_name;
682 r.function = dev_class->no_flatten_output_chains ?
683 vnet_interface_output_node_no_flatten :
684 vnet_interface_output_node;
685 r.format_trace = format_vnet_interface_output_trace;
688 static char * e[] = {
690 "interface is deleted",
693 r.n_errors = ARRAY_LEN (e);
697 hw->output_node_index = vlib_register_node (vm, &r);
699 #define _(sym,str) vlib_node_add_named_next_with_slot (vm, \
700 hw->output_node_index, str, \
701 VNET_INTERFACE_OUTPUT_NEXT_##sym);
702 foreach_intf_output_feat
705 vlib_node_add_named_next_with_slot (vm, hw->output_node_index,
707 VNET_INTERFACE_OUTPUT_NEXT_DROP);
708 vlib_node_add_next_with_slot (vm, hw->output_node_index,
710 VNET_INTERFACE_OUTPUT_NEXT_TX);
713 setup_output_node (vm, hw->output_node_index, hw_class);
714 setup_tx_node (vm, hw->tx_node_index, dev_class);
716 /* Call all up/down callbacks with zero flags when interface is created. */
717 vnet_sw_interface_set_flags_helper
718 (vnm, hw->sw_if_index, /* flags */ 0,
719 VNET_INTERFACE_SET_FLAGS_HELPER_IS_CREATE);
720 vnet_hw_interface_set_flags_helper
721 (vnm, hw_index, /* flags */ 0,
722 VNET_INTERFACE_SET_FLAGS_HELPER_IS_CREATE);
727 void vnet_delete_hw_interface (vnet_main_t * vnm, u32 hw_if_index)
729 vnet_interface_main_t * im = &vnm->interface_main;
730 vnet_hw_interface_t * hw = vnet_get_hw_interface (vnm, hw_if_index);
731 vlib_main_t * vm = vnm->vlib_main;
733 /* If it is up, mark it down. */
735 vnet_hw_interface_set_flags (vnm, hw_if_index, /* flags */ 0);
737 /* Call delete callbacks. */
738 call_hw_interface_add_del_callbacks (vnm, hw_if_index, /* is_create */ 0);
740 /* Delete software interface corresponding to hardware interface. */
741 vnet_delete_sw_interface (vnm, hw->sw_if_index);
743 /* Delete any sub-interfaces. */
746 hash_foreach (id, sw_if_index, hw->sub_interface_sw_if_index_by_id, ({
747 vnet_delete_sw_interface (vnm, sw_if_index);
752 vnet_hw_interface_nodes_t * dn;
753 vnet_interface_output_runtime_t * rt = vlib_node_get_runtime_data (vm, hw->output_node_index);
755 /* Mark node runtime as deleted so output node (if called) will drop packets. */
758 vlib_node_rename (vm, hw->output_node_index, "interface-%d-output-deleted", hw_if_index);
759 vlib_node_rename (vm, hw->tx_node_index, "interface-%d-tx-deleted", hw_if_index);
760 vec_add2 (im->deleted_hw_interface_nodes, dn, 1);
761 dn->tx_node_index = hw->tx_node_index;
762 dn->output_node_index = hw->output_node_index;
765 hash_unset_mem (im->hw_interface_by_name, hw->name);
768 pool_put (im->hw_interfaces, hw);
771 static void serialize_vnet_hw_interface_set_class (serialize_main_t * m, va_list * va)
773 u32 hw_if_index = va_arg (*va, u32);
774 char * hw_class_name = va_arg (*va, char *);
775 serialize_integer (m, hw_if_index, sizeof (hw_if_index));
776 serialize_cstring (m, hw_class_name);
779 static void unserialize_vnet_hw_interface_set_class (serialize_main_t * m, va_list * va)
781 CLIB_UNUSED (mc_main_t * mc) = va_arg (*va, mc_main_t *);
782 vnet_main_t * vnm = vnet_get_main();
784 char * hw_class_name;
786 clib_error_t * error;
788 unserialize_integer (m, &hw_if_index, sizeof (hw_if_index));
789 unserialize_cstring (m, &hw_class_name);
790 p = hash_get (vnm->interface_main.hw_interface_class_by_name, hw_class_name);
792 error = vnet_hw_interface_set_class_helper (vnm, hw_if_index, p[0], /* redistribute */ 0);
794 clib_error_report (error);
797 MC_SERIALIZE_MSG (vnet_hw_interface_set_class_msg, static) = {
798 .name = "vnet_hw_interface_set_class",
799 .serialize = serialize_vnet_hw_interface_set_class,
800 .unserialize = unserialize_vnet_hw_interface_set_class,
803 void vnet_hw_interface_init_for_class (vnet_main_t * vnm, u32 hw_if_index, u32 hw_class_index, u32 hw_instance)
805 vnet_hw_interface_t * hi = vnet_get_hw_interface (vnm, hw_if_index);
806 vnet_hw_interface_class_t * hc = vnet_get_hw_interface_class (vnm, hw_class_index);
808 hi->hw_class_index = hw_class_index;
809 hi->hw_instance = hw_instance;
810 setup_output_node (vnm->vlib_main, hi->output_node_index, hc);
813 static clib_error_t *
814 vnet_hw_interface_set_class_helper (vnet_main_t * vnm, u32 hw_if_index, u32 hw_class_index, u32 redistribute)
816 vnet_hw_interface_t * hi = vnet_get_hw_interface (vnm, hw_if_index);
817 vnet_sw_interface_t * si = vnet_get_sw_interface (vnm, hi->sw_if_index);
818 vnet_hw_interface_class_t * old_class = vnet_get_hw_interface_class (vnm, hi->hw_class_index);
819 vnet_hw_interface_class_t * new_class = vnet_get_hw_interface_class (vnm, hw_class_index);
820 vnet_device_class_t * dev_class = vnet_get_device_class (vnm, hi->dev_class_index);
821 clib_error_t * error = 0;
823 /* New class equals old class? Nothing to do. */
824 if (hi->hw_class_index == hw_class_index)
827 /* No need (and incorrect since admin up flag may be set) to do error checking when
828 receiving unserialize message. */
831 if (si->flags & VNET_SW_INTERFACE_FLAG_ADMIN_UP)
832 return clib_error_return (0, "%v must be admin down to change class from %s to %s",
833 hi->name, old_class->name, new_class->name);
835 /* Make sure interface supports given class. */
836 if ((new_class->is_valid_class_for_interface
837 && ! new_class->is_valid_class_for_interface (vnm, hw_if_index, hw_class_index))
838 || (dev_class ->is_valid_class_for_interface
839 && ! dev_class->is_valid_class_for_interface (vnm, hw_if_index, hw_class_index)))
840 return clib_error_return (0, "%v class cannot be changed from %s to %s",
841 hi->name, old_class->name, new_class->name);
843 if (vnm->vlib_main->mc_main)
845 mc_serialize (vnm->vlib_main->mc_main, &vnet_hw_interface_set_class_msg, hw_if_index, new_class->name);
850 if (old_class->hw_class_change)
851 old_class->hw_class_change (vnm, hw_if_index, old_class->index, new_class->index);
853 vnet_hw_interface_init_for_class (vnm, hw_if_index, new_class->index, /* instance */ ~0);
855 if (new_class->hw_class_change)
856 new_class->hw_class_change (vnm, hw_if_index, old_class->index, new_class->index);
858 if (dev_class->hw_class_change)
859 dev_class->hw_class_change (vnm, hw_if_index, new_class->index);
865 vnet_hw_interface_set_class (vnet_main_t * vnm, u32 hw_if_index, u32 hw_class_index)
866 { return vnet_hw_interface_set_class_helper (vnm, hw_if_index, hw_class_index, /* redistribute */ 1); }
869 vnet_hw_interface_rx_redirect_to_node_helper (vnet_main_t * vnm,
874 vnet_hw_interface_t * hi = vnet_get_hw_interface (vnm, hw_if_index);
875 vnet_device_class_t * dev_class = vnet_get_device_class
876 (vnm, hi->dev_class_index);
880 /* $$$$ fixme someday maybe */
881 ASSERT(vnm->vlib_main->mc_main == 0);
883 if (dev_class->rx_redirect_to_node)
885 dev_class->rx_redirect_to_node (vnm, hw_if_index, node_index);
889 return VNET_API_ERROR_UNIMPLEMENTED;
892 int vnet_hw_interface_rx_redirect_to_node (vnet_main_t * vnm, u32 hw_if_index,
894 { return vnet_hw_interface_rx_redirect_to_node_helper (vnm, hw_if_index,
896 1 /* redistribute */); }
899 vnet_sw_interface_compare (vnet_main_t * vnm,
900 uword sw_if_index0, uword sw_if_index1)
902 vnet_sw_interface_t * sup0 = vnet_get_sup_sw_interface (vnm, sw_if_index0);
903 vnet_sw_interface_t * sup1 = vnet_get_sup_sw_interface (vnm, sw_if_index1);
904 vnet_hw_interface_t * h0 = vnet_get_hw_interface (vnm, sup0->hw_if_index);
905 vnet_hw_interface_t * h1 = vnet_get_hw_interface (vnm, sup1->hw_if_index);
908 return vec_cmp (h0->name, h1->name);
909 return (word) h0->hw_instance - (word) h1->hw_instance;
913 vnet_hw_interface_compare (vnet_main_t * vnm,
914 uword hw_if_index0, uword hw_if_index1)
916 vnet_hw_interface_t * h0 = vnet_get_hw_interface (vnm, hw_if_index0);
917 vnet_hw_interface_t * h1 = vnet_get_hw_interface (vnm, hw_if_index1);
920 return vec_cmp (h0->name, h1->name);
921 return (word) h0->hw_instance - (word) h1->hw_instance;
925 vnet_interface_init (vlib_main_t * vm)
927 vnet_main_t * vnm = vnet_get_main();
928 vnet_interface_main_t * im = &vnm->interface_main;
929 vlib_buffer_t * b = 0;
930 vnet_buffer_opaque_t * o = 0;
933 * Keep people from shooting themselves in the foot.
935 if (sizeof(b->opaque) != sizeof (vnet_buffer_opaque_t))
937 #define _(a) if (sizeof(o->a) > sizeof (o->unused)) \
939 ("FATAL: size of opaque union subtype %s is %d (max %d)", \
940 #a, sizeof(o->a), sizeof (o->unused));
941 foreach_buffer_opaque_union_subtype;
944 return clib_error_return
945 (0, "FATAL: size of vlib buffer opaque %d, size of vnet opaque %d",
946 sizeof(b->opaque), sizeof (vnet_buffer_opaque_t));
949 im->sw_if_counter_lock = clib_mem_alloc_aligned (CLIB_CACHE_LINE_BYTES,
950 CLIB_CACHE_LINE_BYTES);
951 im->sw_if_counter_lock[0] = 1; /* should be no need */
953 vec_validate (im->sw_if_counters,
954 VNET_N_SIMPLE_INTERFACE_COUNTER - 1);
955 im->sw_if_counters[VNET_INTERFACE_COUNTER_DROP].name = "drops";
956 im->sw_if_counters[VNET_INTERFACE_COUNTER_PUNT].name = "punts";
957 im->sw_if_counters[VNET_INTERFACE_COUNTER_IP4].name = "ip4";
958 im->sw_if_counters[VNET_INTERFACE_COUNTER_IP6].name = "ip6";
959 im->sw_if_counters[VNET_INTERFACE_COUNTER_RX_NO_BUF].name = "rx-no-buf";
960 im->sw_if_counters[VNET_INTERFACE_COUNTER_RX_MISS].name = "rx-miss";
961 im->sw_if_counters[VNET_INTERFACE_COUNTER_RX_ERROR].name = "rx-error";
962 im->sw_if_counters[VNET_INTERFACE_COUNTER_TX_ERROR].name = "tx-error";
964 vec_validate (im->combined_sw_if_counters,
965 VNET_N_COMBINED_INTERFACE_COUNTER - 1);
966 im->combined_sw_if_counters[VNET_INTERFACE_COUNTER_RX].name = "rx";
967 im->combined_sw_if_counters[VNET_INTERFACE_COUNTER_TX].name = "tx";
969 im->sw_if_counter_lock[0] = 0;
971 im->device_class_by_name = hash_create_string (/* size */ 0,
974 vnet_device_class_t * c;
976 c = vnm->device_class_registrations;
980 c->index = vec_len (im->device_classes);
981 hash_set_mem (im->device_class_by_name, c->name, c->index);
982 vec_add1 (im->device_classes, c[0]);
983 c = c->next_class_registration;
987 im->hw_interface_class_by_name = hash_create_string (/* size */ 0,
990 im->sw_if_index_by_sup_and_sub = hash_create_mem (0, sizeof(u64),
993 vnet_hw_interface_class_t * c;
995 c = vnm->hw_interface_class_registrations;
999 c->index = vec_len (im->hw_interface_classes);
1000 hash_set_mem (im->hw_interface_class_by_name, c->name, c->index);
1001 vec_add1 (im->hw_interface_classes, c[0]);
1002 c = c->next_class_registration;
1007 clib_error_t * error;
1009 if ((error = vlib_call_init_function (vm, vnet_interface_cli_init)))
1016 VLIB_INIT_FUNCTION (vnet_interface_init);
1018 /* Kludge to renumber interface names [only!] */
1019 int vnet_interface_name_renumber (u32 sw_if_index, u32 new_show_dev_instance)
1022 vnet_main_t * vnm = vnet_get_main();
1023 vnet_interface_main_t * im = &vnm->interface_main;
1024 vnet_hw_interface_t * hi = vnet_get_sup_hw_interface (vnm, sw_if_index);
1026 vnet_device_class_t * dev_class = vnet_get_device_class
1027 (vnm, hi->dev_class_index);
1029 if (dev_class->name_renumber == 0 || dev_class->format_device_name == 0)
1030 return VNET_API_ERROR_UNIMPLEMENTED;
1032 rv = dev_class->name_renumber (hi, new_show_dev_instance);
1037 hash_unset_mem (im->hw_interface_by_name, hi->name);
1038 vec_free (hi->name);
1039 /* Use the mapping we set up to call it Ishmael */
1040 hi->name = format (0, "%U", dev_class->format_device_name,
1043 hash_set_mem (im->hw_interface_by_name, hi->name, hi->hw_if_index);
1047 int vnet_interface_add_del_feature(vnet_main_t * vnm,
1050 intf_output_feat_t feature,
1053 vnet_sw_interface_t * sw;
1055 sw = vnet_get_sw_interface(vnm, sw_if_index);
1059 sw->output_feature_bitmap |= (1 << feature);
1060 sw->output_feature_bitmap |= (1<< INTF_OUTPUT_FEAT_DONE);
1062 } else { /* delete */
1064 sw->output_feature_bitmap &= ~(1<<feature);
1065 if (sw->output_feature_bitmap == (1 << INTF_OUTPUT_FEAT_DONE))
1066 sw->output_feature_bitmap = 0;