2 *------------------------------------------------------------------
3 * interface_api.c - vnet interface api
5 * Copyright (c) 2016 Cisco and/or its affiliates.
6 * Licensed under the Apache License, Version 2.0 (the "License");
7 * you may not use this file except in compliance with the License.
8 * You may obtain a copy of the License at:
10 * http://www.apache.org/licenses/LICENSE-2.0
12 * Unless required by applicable law or agreed to in writing, software
13 * distributed under the License is distributed on an "AS IS" BASIS,
14 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15 * See the License for the specific language governing permissions and
16 * limitations under the License.
17 *------------------------------------------------------------------
20 #include <vnet/vnet.h>
21 #include <vlibmemory/api.h>
23 #include <vnet/interface.h>
24 #include <vnet/api_errno.h>
25 #include <vnet/ethernet/ethernet.h>
26 #include <vnet/ip/ip.h>
27 #include <vnet/fib/fib_table.h>
28 #include <vnet/mfib/mfib_table.h>
29 #include <vnet/l2/l2_vtr.h>
30 #include <vnet/vnet_msg_enum.h>
31 #include <vnet/fib/fib_api.h>
32 #include <vnet/mfib/mfib_table.h>
34 #define vl_typedefs /* define message structures */
35 #include <vnet/vnet_all_api_h.h>
38 #define vl_endianfun /* define message structures */
39 #include <vnet/vnet_all_api_h.h>
42 /* instantiate all the print functions we know about */
43 #define vl_print(handle, ...) vlib_cli_output (handle, __VA_ARGS__)
45 #include <vnet/vnet_all_api_h.h>
48 #include <vlibapi/api_helper_macros.h>
49 vpe_api_main_t vpe_api_main;
51 #define foreach_vpe_api_msg \
52 _(SW_INTERFACE_SET_FLAGS, sw_interface_set_flags) \
53 _(SW_INTERFACE_SET_MTU, sw_interface_set_mtu) \
54 _(WANT_INTERFACE_EVENTS, want_interface_events) \
55 _(SW_INTERFACE_DUMP, sw_interface_dump) \
56 _(SW_INTERFACE_ADD_DEL_ADDRESS, sw_interface_add_del_address) \
57 _(SW_INTERFACE_SET_RX_MODE, sw_interface_set_rx_mode) \
58 _(SW_INTERFACE_SET_TABLE, sw_interface_set_table) \
59 _(SW_INTERFACE_GET_TABLE, sw_interface_get_table) \
60 _(SW_INTERFACE_SET_UNNUMBERED, sw_interface_set_unnumbered) \
61 _(SW_INTERFACE_CLEAR_STATS, sw_interface_clear_stats) \
62 _(SW_INTERFACE_TAG_ADD_DEL, sw_interface_tag_add_del) \
63 _(SW_INTERFACE_SET_MAC_ADDRESS, sw_interface_set_mac_address) \
64 _(SW_INTERFACE_GET_MAC_ADDRESS, sw_interface_get_mac_address) \
65 _(CREATE_VLAN_SUBIF, create_vlan_subif) \
66 _(CREATE_SUBIF, create_subif) \
67 _(DELETE_SUBIF, delete_subif) \
68 _(CREATE_LOOPBACK, create_loopback) \
69 _(CREATE_LOOPBACK_INSTANCE, create_loopback_instance) \
70 _(DELETE_LOOPBACK, delete_loopback) \
71 _(INTERFACE_NAME_RENUMBER, interface_name_renumber) \
72 _(COLLECT_DETAILED_INTERFACE_STATS, collect_detailed_interface_stats)
75 vl_api_sw_interface_set_flags_t_handler (vl_api_sw_interface_set_flags_t * mp)
77 vl_api_sw_interface_set_flags_reply_t *rmp;
78 vnet_main_t *vnm = vnet_get_main ();
83 VALIDATE_SW_IF_INDEX (mp);
85 flags = mp->admin_up_down ? VNET_SW_INTERFACE_FLAG_ADMIN_UP : 0;
87 error = vnet_sw_interface_set_flags (vnm, ntohl (mp->sw_if_index), flags);
91 clib_error_report (error);
94 BAD_SW_IF_INDEX_LABEL;
95 REPLY_MACRO (VL_API_SW_INTERFACE_SET_FLAGS_REPLY);
99 vl_api_sw_interface_set_mtu_t_handler (vl_api_sw_interface_set_mtu_t * mp)
101 vl_api_sw_interface_set_mtu_reply_t *rmp;
102 vnet_main_t *vnm = vnet_get_main ();
103 u32 sw_if_index = ntohl (mp->sw_if_index);
104 u16 mtu = ntohs (mp->mtu);
105 ethernet_main_t *em = ðernet_main;
108 VALIDATE_SW_IF_INDEX (mp);
110 vnet_sw_interface_t *si = vnet_get_sw_interface (vnm, sw_if_index);
111 if (si->type != VNET_SW_INTERFACE_TYPE_HARDWARE)
113 rv = VNET_API_ERROR_INVALID_VALUE;
114 goto bad_sw_if_index;
117 vnet_hw_interface_t *hi = vnet_get_hw_interface (vnm, si->hw_if_index);
118 ethernet_interface_t *eif = ethernet_get_interface (em, si->hw_if_index);
122 rv = VNET_API_ERROR_FEATURE_DISABLED;
123 goto bad_sw_if_index;
126 if (mtu < hi->min_supported_packet_bytes)
128 rv = VNET_API_ERROR_INVALID_VALUE;
129 goto bad_sw_if_index;
132 if (mtu > hi->max_supported_packet_bytes)
134 rv = VNET_API_ERROR_INVALID_VALUE;
135 goto bad_sw_if_index;
138 vnet_hw_interface_set_mtu (vnm, si->hw_if_index, mtu);
140 BAD_SW_IF_INDEX_LABEL;
141 REPLY_MACRO (VL_API_SW_INTERFACE_SET_MTU_REPLY);
145 send_sw_interface_details (vpe_api_main_t * am,
146 vl_api_registration_t * rp,
147 vnet_sw_interface_t * swif,
148 u8 * interface_name, u32 context)
150 vnet_hw_interface_t *hi =
151 vnet_get_sup_hw_interface (am->vnet_main, swif->sw_if_index);
153 vl_api_sw_interface_details_t *mp = vl_msg_api_alloc (sizeof (*mp));
154 memset (mp, 0, sizeof (*mp));
155 mp->_vl_msg_id = ntohs (VL_API_SW_INTERFACE_DETAILS);
156 mp->sw_if_index = ntohl (swif->sw_if_index);
157 mp->sup_sw_if_index = ntohl (swif->sup_sw_if_index);
158 mp->admin_up_down = (swif->flags & VNET_SW_INTERFACE_FLAG_ADMIN_UP) ? 1 : 0;
159 mp->link_up_down = (hi->flags & VNET_HW_INTERFACE_FLAG_LINK_UP) ? 1 : 0;
160 mp->link_duplex = ((hi->flags & VNET_HW_INTERFACE_FLAG_DUPLEX_MASK) >>
161 VNET_HW_INTERFACE_FLAG_DUPLEX_SHIFT);
162 mp->link_speed = ((hi->flags & VNET_HW_INTERFACE_FLAG_SPEED_MASK) >>
163 VNET_HW_INTERFACE_FLAG_SPEED_SHIFT);
164 mp->link_mtu = ntohs (hi->max_packet_bytes);
165 mp->context = context;
167 strncpy ((char *) mp->interface_name,
168 (char *) interface_name, ARRAY_LEN (mp->interface_name) - 1);
170 /* Send the L2 address for ethernet physical intfcs */
171 if (swif->sup_sw_if_index == swif->sw_if_index
172 && hi->hw_class_index == ethernet_hw_interface_class.index)
174 ethernet_main_t *em = ethernet_get_main (am->vlib_main);
175 ethernet_interface_t *ei;
177 ei = pool_elt_at_index (em->interfaces, hi->hw_instance);
178 ASSERT (sizeof (mp->l2_address) >= sizeof (ei->address));
179 clib_memcpy (mp->l2_address, ei->address, sizeof (ei->address));
180 mp->l2_address_length = ntohl (sizeof (ei->address));
182 else if (swif->sup_sw_if_index != swif->sw_if_index)
184 vnet_sub_interface_t *sub = &swif->sub;
185 mp->sub_id = ntohl (sub->id);
186 mp->sub_dot1ad = sub->eth.flags.dot1ad;
187 mp->sub_number_of_tags =
188 sub->eth.flags.one_tag + sub->eth.flags.two_tags * 2;
189 mp->sub_outer_vlan_id = ntohs (sub->eth.outer_vlan_id);
190 mp->sub_inner_vlan_id = ntohs (sub->eth.inner_vlan_id);
191 mp->sub_exact_match = sub->eth.flags.exact_match;
192 mp->sub_default = sub->eth.flags.default_sub;
193 mp->sub_outer_vlan_id_any = sub->eth.flags.outer_vlan_id_any;
194 mp->sub_inner_vlan_id_any = sub->eth.flags.inner_vlan_id_any;
196 /* vlan tag rewrite data */
197 u32 vtr_op = L2_VTR_DISABLED;
198 u32 vtr_push_dot1q = 0, vtr_tag1 = 0, vtr_tag2 = 0;
200 if (l2vtr_get (am->vlib_main, am->vnet_main, swif->sw_if_index,
201 &vtr_op, &vtr_push_dot1q, &vtr_tag1, &vtr_tag2) != 0)
203 // error - default to disabled
204 mp->vtr_op = ntohl (L2_VTR_DISABLED);
205 clib_warning ("cannot get vlan tag rewrite for sw_if_index %d",
210 mp->vtr_op = ntohl (vtr_op);
211 mp->vtr_push_dot1q = ntohl (vtr_push_dot1q);
212 mp->vtr_tag1 = ntohl (vtr_tag1);
213 mp->vtr_tag2 = ntohl (vtr_tag2);
217 /* pbb tag rewrite data */
218 ethernet_header_t eth_hdr;
219 u32 vtr_op = L2_VTR_DISABLED;
223 memset (ð_hdr, 0, sizeof (eth_hdr));
225 if (!l2pbb_get (am->vlib_main, am->vnet_main, swif->sw_if_index,
226 &vtr_op, &outer_tag, ð_hdr, &b_vlanid, &i_sid))
229 clib_memcpy (mp->b_dmac, eth_hdr.dst_address,
230 sizeof (eth_hdr.dst_address));
231 clib_memcpy (mp->b_smac, eth_hdr.src_address,
232 sizeof (eth_hdr.src_address));
233 mp->b_vlanid = b_vlanid;
237 u8 *tag = vnet_get_sw_interface_tag (vnet_get_main (), swif->sw_if_index);
239 strncpy ((char *) mp->tag, (char *) tag, ARRAY_LEN (mp->tag) - 1);
241 vl_api_send_msg (rp, (u8 *) mp);
245 vl_api_sw_interface_dump_t_handler (vl_api_sw_interface_dump_t * mp)
247 vpe_api_main_t *am = &vpe_api_main;
248 vnet_sw_interface_t *swif;
249 vnet_interface_main_t *im = &am->vnet_main->interface_main;
250 vl_api_registration_t *rp;
252 rp = vl_api_client_index_to_registration (mp->client_index);
256 clib_warning ("Client %d AWOL", mp->client_index);
260 u8 *filter = 0, *name = 0;
261 if (mp->name_filter_valid)
263 mp->name_filter[ARRAY_LEN (mp->name_filter) - 1] = 0;
264 filter = format (0, "%s%c", mp->name_filter, 0);
267 char *strcasestr (char *, char *); /* lnx hdr file botch */
269 pool_foreach (swif, im->sw_interfaces,
271 if (!vnet_swif_is_api_visible (swif))
273 vec_reset_length(name);
274 name = format (name, "%U%c", format_vnet_sw_interface_name, am->vnet_main,
277 if (filter && !strcasestr((char *) name, (char *) filter))
280 send_sw_interface_details (am, rp, swif, name, mp->context);
289 vl_api_sw_interface_add_del_address_t_handler
290 (vl_api_sw_interface_add_del_address_t * mp)
292 vlib_main_t *vm = vlib_get_main ();
293 vnet_main_t *vnm = vnet_get_main ();
294 vl_api_sw_interface_add_del_address_reply_t *rmp;
297 clib_error_t *error = 0;
299 VALIDATE_SW_IF_INDEX (mp);
301 is_del = mp->is_add == 0;
305 ip_del_all_interface_addresses (vm, ntohl (mp->sw_if_index));
306 else if (mp->is_ipv6)
307 error = ip6_add_del_interface_address (vm, ntohl (mp->sw_if_index),
308 (void *) mp->address,
309 mp->address_length, is_del);
311 error = ip4_add_del_interface_address (vm, ntohl (mp->sw_if_index),
312 (void *) mp->address,
313 mp->address_length, is_del);
318 clib_error_report (error);
322 BAD_SW_IF_INDEX_LABEL;
325 REPLY_MACRO (VL_API_SW_INTERFACE_ADD_DEL_ADDRESS_REPLY);
328 void stats_dslock_with_hint (int hint, int tag) __attribute__ ((weak));
330 stats_dslock_with_hint (int hint, int tag)
334 void stats_dsunlock (void) __attribute__ ((weak));
336 stats_dsunlock (void)
341 vl_api_sw_interface_set_table_t_handler (vl_api_sw_interface_set_table_t * mp)
343 vl_api_sw_interface_set_table_reply_t *rmp;
344 u32 sw_if_index = ntohl (mp->sw_if_index);
345 u32 table_id = ntohl (mp->vrf_id);
348 VALIDATE_SW_IF_INDEX (mp);
350 stats_dslock_with_hint (1 /* release hint */ , 4 /* tag */ );
353 rv = ip_table_bind (FIB_PROTOCOL_IP6, sw_if_index, table_id, 1);
355 rv = ip_table_bind (FIB_PROTOCOL_IP4, sw_if_index, table_id, 1);
359 BAD_SW_IF_INDEX_LABEL;
361 REPLY_MACRO (VL_API_SW_INTERFACE_SET_TABLE_REPLY);
365 ip_table_bind (fib_protocol_t fproto,
366 u32 sw_if_index, u32 table_id, u8 is_api)
368 CLIB_UNUSED (ip_interface_address_t * ia);
369 u32 fib_index, mfib_index;
375 src = FIB_SOURCE_API;
376 msrc = MFIB_SOURCE_API;
380 src = FIB_SOURCE_CLI;
381 msrc = MFIB_SOURCE_CLI;
385 * This if table does not exist = error is what we want in the end.
387 fib_index = fib_table_find (fproto, table_id);
388 mfib_index = mfib_table_find (fproto, table_id);
390 if (~0 == fib_index || ~0 == mfib_index)
392 return (VNET_API_ERROR_NO_SUCH_FIB);
395 if (FIB_PROTOCOL_IP6 == fproto)
398 * If the interface already has in IP address, then a change int
399 * VRF is not allowed. The IP address applied must first be removed.
400 * We do not do that automatically here, since VPP has no knowledge
401 * of whether thoses subnets are valid in the destination VRF.
404 foreach_ip_interface_address (&ip6_main.lookup_main,
406 1 /* honor unnumbered */ ,
408 return (VNET_API_ERROR_ADDRESS_FOUND_FOR_INTERFACE);
412 vec_validate (ip6_main.fib_index_by_sw_if_index, sw_if_index);
413 vec_validate (ip6_main.mfib_index_by_sw_if_index, sw_if_index);
416 * tell those that are interested that the binding is changing.
418 ip6_table_bind_callback_t *cb;
419 vec_foreach (cb, ip6_main.table_bind_callbacks)
420 cb->function (&ip6_main, cb->function_opaque,
423 ip6_main.fib_index_by_sw_if_index[sw_if_index]);
427 /* reset back to default */
428 if (0 != ip6_main.fib_index_by_sw_if_index[sw_if_index])
429 fib_table_unlock (ip6_main.fib_index_by_sw_if_index[sw_if_index],
430 FIB_PROTOCOL_IP6, src);
431 if (0 != ip6_main.mfib_index_by_sw_if_index[sw_if_index])
432 mfib_table_unlock (ip6_main.mfib_index_by_sw_if_index
433 [sw_if_index], FIB_PROTOCOL_IP6, msrc);
438 /* we need to lock the table now it's inuse */
439 fib_table_lock (fib_index, FIB_PROTOCOL_IP6, src);
440 mfib_table_lock (mfib_index, FIB_PROTOCOL_IP6, msrc);
443 ip6_main.fib_index_by_sw_if_index[sw_if_index] = fib_index;
444 ip6_main.mfib_index_by_sw_if_index[sw_if_index] = mfib_index;
449 * If the interface already has in IP address, then a change int
450 * VRF is not allowed. The IP address applied must first be removed.
451 * We do not do that automatically here, since VPP has no knowledge
452 * of whether thoses subnets are valid in the destination VRF.
455 foreach_ip_interface_address (&ip4_main.lookup_main,
457 1 /* honor unnumbered */ ,
459 return (VNET_API_ERROR_ADDRESS_FOUND_FOR_INTERFACE);
463 vec_validate (ip4_main.fib_index_by_sw_if_index, sw_if_index);
464 vec_validate (ip4_main.mfib_index_by_sw_if_index, sw_if_index);
467 * tell those that are interested that the binding is changing.
469 ip4_table_bind_callback_t *cb;
470 vec_foreach (cb, ip4_main.table_bind_callbacks)
471 cb->function (&ip4_main, cb->function_opaque,
474 ip4_main.fib_index_by_sw_if_index[sw_if_index]);
478 /* reset back to default */
479 if (0 != ip4_main.fib_index_by_sw_if_index[sw_if_index])
480 fib_table_unlock (ip4_main.fib_index_by_sw_if_index[sw_if_index],
481 FIB_PROTOCOL_IP4, src);
482 if (0 != ip4_main.mfib_index_by_sw_if_index[sw_if_index])
483 mfib_table_unlock (ip4_main.mfib_index_by_sw_if_index
484 [sw_if_index], FIB_PROTOCOL_IP4, msrc);
489 /* we need to lock the table now it's inuse */
490 fib_index = fib_table_find_or_create_and_lock (FIB_PROTOCOL_IP4,
493 mfib_index = mfib_table_find_or_create_and_lock (FIB_PROTOCOL_IP4,
497 ip4_main.fib_index_by_sw_if_index[sw_if_index] = fib_index;
498 ip4_main.mfib_index_by_sw_if_index[sw_if_index] = mfib_index;
505 send_sw_interface_get_table_reply (vl_api_registration_t * reg,
506 u32 context, int retval, u32 vrf_id)
508 vl_api_sw_interface_get_table_reply_t *mp;
510 mp = vl_msg_api_alloc (sizeof (*mp));
511 memset (mp, 0, sizeof (*mp));
512 mp->_vl_msg_id = ntohs (VL_API_SW_INTERFACE_GET_TABLE_REPLY);
513 mp->context = context;
514 mp->retval = htonl (retval);
515 mp->vrf_id = htonl (vrf_id);
517 vl_api_send_msg (reg, (u8 *) mp);
521 vl_api_sw_interface_get_table_t_handler (vl_api_sw_interface_get_table_t * mp)
523 vl_api_registration_t *reg;
524 fib_table_t *fib_table = 0;
525 u32 sw_if_index = ~0;
528 fib_protocol_t fib_proto = FIB_PROTOCOL_IP4;
531 reg = vl_api_client_index_to_registration (mp->client_index);
535 VALIDATE_SW_IF_INDEX (mp);
537 sw_if_index = ntohl (mp->sw_if_index);
540 fib_proto = FIB_PROTOCOL_IP6;
542 fib_index = fib_table_get_index_for_sw_if_index (fib_proto, sw_if_index);
545 fib_table = fib_table_get (fib_index, fib_proto);
546 table_id = fib_table->ft_table_id;
549 BAD_SW_IF_INDEX_LABEL;
551 send_sw_interface_get_table_reply (reg, mp->context, rv, table_id);
554 static void vl_api_sw_interface_set_unnumbered_t_handler
555 (vl_api_sw_interface_set_unnumbered_t * mp)
557 vl_api_sw_interface_set_unnumbered_reply_t *rmp;
559 vnet_main_t *vnm = vnet_get_main ();
560 u32 sw_if_index = ntohl (mp->sw_if_index);
561 u32 unnumbered_sw_if_index = ntohl (mp->unnumbered_sw_if_index);
564 * The API message field names are backwards from
565 * the underlying data structure names.
566 * It's not worth changing them now.
568 if (!vnet_sw_interface_is_api_valid (vnm, unnumbered_sw_if_index))
570 rv = VNET_API_ERROR_INVALID_SW_IF_INDEX;
574 /* Only check the "use loop0" field when setting the binding */
575 if (mp->is_add && !vnet_sw_interface_is_api_valid (vnm, sw_if_index))
577 rv = VNET_API_ERROR_INVALID_SW_IF_INDEX_2;
581 vnet_sw_interface_update_unnumbered (unnumbered_sw_if_index,
582 sw_if_index, mp->is_add);
584 REPLY_MACRO (VL_API_SW_INTERFACE_SET_UNNUMBERED_REPLY);
588 vl_api_sw_interface_clear_stats_t_handler (vl_api_sw_interface_clear_stats_t *
591 vl_api_sw_interface_clear_stats_reply_t *rmp;
593 vnet_main_t *vnm = vnet_get_main ();
594 vnet_interface_main_t *im = &vnm->interface_main;
595 vlib_simple_counter_main_t *sm;
596 vlib_combined_counter_main_t *cm;
597 static vnet_main_t **my_vnet_mains;
598 int i, j, n_counters;
601 if (mp->sw_if_index != ~0)
602 VALIDATE_SW_IF_INDEX (mp);
604 vec_reset_length (my_vnet_mains);
606 for (i = 0; i < vec_len (vnet_mains); i++)
609 vec_add1 (my_vnet_mains, vnet_mains[i]);
612 if (vec_len (vnet_mains) == 0)
613 vec_add1 (my_vnet_mains, vnm);
615 n_counters = vec_len (im->combined_sw_if_counters);
617 for (j = 0; j < n_counters; j++)
619 for (i = 0; i < vec_len (my_vnet_mains); i++)
621 im = &my_vnet_mains[i]->interface_main;
622 cm = im->combined_sw_if_counters + j;
623 if (mp->sw_if_index == (u32) ~ 0)
624 vlib_clear_combined_counters (cm);
626 vlib_zero_combined_counter (cm, ntohl (mp->sw_if_index));
630 n_counters = vec_len (im->sw_if_counters);
632 for (j = 0; j < n_counters; j++)
634 for (i = 0; i < vec_len (my_vnet_mains); i++)
636 im = &my_vnet_mains[i]->interface_main;
637 sm = im->sw_if_counters + j;
638 if (mp->sw_if_index == (u32) ~ 0)
639 vlib_clear_simple_counters (sm);
641 vlib_zero_simple_counter (sm, ntohl (mp->sw_if_index));
645 BAD_SW_IF_INDEX_LABEL;
647 REPLY_MACRO (VL_API_SW_INTERFACE_CLEAR_STATS_REPLY);
650 #define API_LINK_STATE_EVENT 1
651 #define API_ADMIN_UP_DOWN_EVENT 2
654 event_data_cmp (void *a1, void *a2)
659 return (word) e1[0] - (word) e2[0];
663 send_sw_interface_event (vpe_api_main_t * am,
664 vpe_client_registration_t * reg,
665 vl_api_registration_t * vl_reg,
666 vnet_sw_interface_t * swif)
668 vl_api_sw_interface_event_t *mp;
669 vnet_main_t *vnm = am->vnet_main;
671 vnet_hw_interface_t *hi = vnet_get_sup_hw_interface (vnm,
673 mp = vl_msg_api_alloc (sizeof (*mp));
674 memset (mp, 0, sizeof (*mp));
675 mp->_vl_msg_id = ntohs (VL_API_SW_INTERFACE_EVENT);
676 mp->sw_if_index = ntohl (swif->sw_if_index);
677 mp->client_index = reg->client_index;
678 mp->pid = reg->client_pid;
680 mp->admin_up_down = (swif->flags & VNET_SW_INTERFACE_FLAG_ADMIN_UP) ? 1 : 0;
681 mp->link_up_down = (hi->flags & VNET_HW_INTERFACE_FLAG_LINK_UP) ? 1 : 0;
682 vl_api_send_msg (vl_reg, (u8 *) mp);
686 link_state_process (vlib_main_t * vm,
687 vlib_node_runtime_t * rt, vlib_frame_t * f)
689 vpe_api_main_t *vam = &vpe_api_main;
690 vnet_main_t *vnm = vam->vnet_main;
691 vnet_sw_interface_t *swif;
692 uword *event_data = 0;
693 vpe_client_registration_t *reg;
695 u32 prev_sw_if_index;
696 vl_api_registration_t *vl_reg;
698 vam->link_state_process_up = 1;
702 vlib_process_wait_for_event (vm);
704 /* Unified list of changed link or admin state sw_if_indices */
705 vlib_process_get_events_with_type
706 (vm, &event_data, API_LINK_STATE_EVENT);
707 vlib_process_get_events_with_type
708 (vm, &event_data, API_ADMIN_UP_DOWN_EVENT);
710 /* Sort, so we can eliminate duplicates */
711 vec_sort_with_function (event_data, event_data_cmp);
713 prev_sw_if_index = ~0;
715 for (i = 0; i < vec_len (event_data); i++)
717 /* Only one message per swif */
718 if (prev_sw_if_index == event_data[i])
720 prev_sw_if_index = event_data[i];
723 pool_foreach(reg, vam->interface_events_registrations,
725 vl_reg = vl_api_client_index_to_registration (reg->client_index);
728 /* sw_interface may be deleted already */
729 if (!pool_is_free_index (vnm->interface_main.sw_interfaces,
732 swif = vnet_get_sw_interface (vnm, event_data[i]);
733 send_sw_interface_event (vam, reg, vl_reg, swif);
739 vec_reset_length (event_data);
745 static clib_error_t *link_up_down_function (vnet_main_t * vm, u32 hw_if_index,
747 static clib_error_t *admin_up_down_function (vnet_main_t * vm,
748 u32 hw_if_index, u32 flags);
751 VLIB_REGISTER_NODE (link_state_process_node,static) = {
752 .function = link_state_process,
753 .type = VLIB_NODE_TYPE_PROCESS,
754 .name = "vpe-link-state-process",
758 VNET_SW_INTERFACE_ADMIN_UP_DOWN_FUNCTION (admin_up_down_function);
759 VNET_HW_INTERFACE_LINK_UP_DOWN_FUNCTION (link_up_down_function);
761 static clib_error_t *
762 link_up_down_function (vnet_main_t * vm, u32 hw_if_index, u32 flags)
764 vpe_api_main_t *vam = &vpe_api_main;
765 vnet_hw_interface_t *hi = vnet_get_hw_interface (vm, hw_if_index);
767 if (vam->link_state_process_up)
768 vlib_process_signal_event (vam->vlib_main,
769 link_state_process_node.index,
770 API_LINK_STATE_EVENT, hi->sw_if_index);
774 static clib_error_t *
775 admin_up_down_function (vnet_main_t * vm, u32 sw_if_index, u32 flags)
777 vpe_api_main_t *vam = &vpe_api_main;
780 * Note: it's perfectly fair to set a subif admin up / admin down.
781 * Note the subtle distinction between this routine and the previous
784 if (vam->link_state_process_up)
785 vlib_process_signal_event (vam->vlib_main,
786 link_state_process_node.index,
787 API_ADMIN_UP_DOWN_EVENT, sw_if_index);
791 static void vl_api_sw_interface_tag_add_del_t_handler
792 (vl_api_sw_interface_tag_add_del_t * mp)
794 vnet_main_t *vnm = vnet_get_main ();
795 vl_api_sw_interface_tag_add_del_reply_t *rmp;
798 u32 sw_if_index = ntohl (mp->sw_if_index);
800 VALIDATE_SW_IF_INDEX (mp);
806 rv = VNET_API_ERROR_INVALID_VALUE;
810 mp->tag[ARRAY_LEN (mp->tag) - 1] = 0;
811 tag = format (0, "%s%c", mp->tag, 0);
812 vnet_set_sw_interface_tag (vnm, tag, sw_if_index);
815 vnet_clear_sw_interface_tag (vnm, sw_if_index);
817 BAD_SW_IF_INDEX_LABEL;
819 REPLY_MACRO (VL_API_SW_INTERFACE_TAG_ADD_DEL_REPLY);
822 static void vl_api_sw_interface_set_mac_address_t_handler
823 (vl_api_sw_interface_set_mac_address_t * mp)
825 vl_api_sw_interface_set_mac_address_reply_t *rmp;
826 vnet_main_t *vnm = vnet_get_main ();
827 u32 sw_if_index = ntohl (mp->sw_if_index);
828 vnet_sw_interface_t *si;
832 VALIDATE_SW_IF_INDEX (mp);
834 si = vnet_get_sw_interface (vnm, sw_if_index);
835 error = vnet_hw_interface_change_mac_address (vnm, si->hw_if_index,
839 rv = VNET_API_ERROR_UNIMPLEMENTED;
840 clib_error_report (error);
844 BAD_SW_IF_INDEX_LABEL;
846 REPLY_MACRO (VL_API_SW_INTERFACE_SET_MAC_ADDRESS_REPLY);
849 static void vl_api_sw_interface_get_mac_address_t_handler
850 (vl_api_sw_interface_get_mac_address_t * mp)
852 vl_api_sw_interface_get_mac_address_reply_t *rmp;
853 vl_api_registration_t *reg;
854 vnet_main_t *vnm = vnet_get_main ();
855 u32 sw_if_index = ntohl (mp->sw_if_index);
856 vnet_sw_interface_t *si;
857 ethernet_interface_t *eth_if = 0;
860 VALIDATE_SW_IF_INDEX (mp);
862 si = vnet_get_sup_sw_interface (vnm, sw_if_index);
863 if (si->type == VNET_SW_INTERFACE_TYPE_HARDWARE)
864 eth_if = ethernet_get_interface (ðernet_main, si->hw_if_index);
866 BAD_SW_IF_INDEX_LABEL;
868 reg = vl_api_client_index_to_registration (mp->client_index);
871 rmp = vl_msg_api_alloc (sizeof (*rmp));
872 rmp->_vl_msg_id = htons (VL_API_SW_INTERFACE_GET_MAC_ADDRESS_REPLY);
873 rmp->context = mp->context;
874 rmp->retval = htonl (rv);
876 memcpy (rmp->mac_address, eth_if->address, 6);
877 vl_api_send_msg (reg, (u8 *) rmp);
880 static void vl_api_sw_interface_set_rx_mode_t_handler
881 (vl_api_sw_interface_set_rx_mode_t * mp)
883 vl_api_sw_interface_set_rx_mode_reply_t *rmp;
884 vnet_main_t *vnm = vnet_get_main ();
885 u32 sw_if_index = ntohl (mp->sw_if_index);
886 vnet_sw_interface_t *si;
890 VALIDATE_SW_IF_INDEX (mp);
892 si = vnet_get_sw_interface (vnm, sw_if_index);
893 error = set_hw_interface_change_rx_mode (vnm, si->hw_if_index,
895 ntohl (mp->queue_id), mp->mode);
898 rv = VNET_API_ERROR_UNIMPLEMENTED;
899 clib_error_report (error);
903 BAD_SW_IF_INDEX_LABEL;
905 REPLY_MACRO (VL_API_SW_INTERFACE_SET_RX_MODE_REPLY);
909 vl_api_create_vlan_subif_t_handler (vl_api_create_vlan_subif_t * mp)
911 vl_api_create_vlan_subif_reply_t *rmp;
912 vnet_main_t *vnm = vnet_get_main ();
913 u32 sw_if_index = (u32) ~ 0;
914 vnet_hw_interface_t *hi;
917 vnet_sw_interface_t template;
919 vnet_interface_main_t *im = &vnm->interface_main;
921 vl_api_registration_t *reg;
924 VALIDATE_SW_IF_INDEX (mp);
926 hi = vnet_get_sup_hw_interface (vnm, ntohl (mp->sw_if_index));
928 if (hi->bond_info == VNET_HW_INTERFACE_BOND_INFO_SLAVE)
930 rv = VNET_API_ERROR_BOND_SLAVE_NOT_ALLOWED;
934 id = ntohl (mp->vlan_id);
935 if (id == 0 || id > 4095)
937 rv = VNET_API_ERROR_INVALID_VLAN;
941 sup_and_sub_key = ((u64) (hi->sw_if_index) << 32) | (u64) id;
943 p = hash_get_mem (im->sw_if_index_by_sup_and_sub, &sup_and_sub_key);
946 rv = VNET_API_ERROR_VLAN_ALREADY_EXISTS;
950 memset (&template, 0, sizeof (template));
951 template.type = VNET_SW_INTERFACE_TYPE_SUB;
952 template.sup_sw_if_index = hi->sw_if_index;
953 template.sub.id = id;
954 template.sub.eth.raw_flags = 0;
955 template.sub.eth.flags.one_tag = 1;
956 template.sub.eth.outer_vlan_id = id;
957 template.sub.eth.flags.exact_match = 1;
959 error = vnet_create_sw_interface (vnm, &template, &sw_if_index);
962 clib_error_report (error);
963 rv = VNET_API_ERROR_INVALID_REGISTRATION;
967 u64 *kp = clib_mem_alloc (sizeof (*kp));
968 *kp = sup_and_sub_key;
970 hash_set (hi->sub_interface_sw_if_index_by_id, id, sw_if_index);
971 hash_set_mem (im->sw_if_index_by_sup_and_sub, kp, sw_if_index);
973 BAD_SW_IF_INDEX_LABEL;
976 reg = vl_api_client_index_to_registration (mp->client_index);
980 rmp = vl_msg_api_alloc (sizeof (*rmp));
981 rmp->_vl_msg_id = htons (VL_API_CREATE_VLAN_SUBIF_REPLY);
982 rmp->context = mp->context;
983 rmp->retval = htonl (rv);
984 rmp->sw_if_index = htonl (sw_if_index);
985 vl_api_send_msg (reg, (u8 *) rmp);
989 vl_api_create_subif_t_handler (vl_api_create_subif_t * mp)
991 vl_api_create_subif_reply_t *rmp;
992 vnet_main_t *vnm = vnet_get_main ();
993 u32 sw_if_index = ~0;
996 vnet_sw_interface_t *si;
997 vnet_hw_interface_t *hi;
998 vnet_sw_interface_t template;
1000 vnet_interface_main_t *im = &vnm->interface_main;
1001 u64 sup_and_sub_key;
1002 clib_error_t *error;
1004 VALIDATE_SW_IF_INDEX (mp);
1006 si = vnet_get_sup_sw_interface (vnm, ntohl (mp->sw_if_index));
1007 hi = vnet_get_sup_hw_interface (vnm, ntohl (mp->sw_if_index));
1009 if (hi->bond_info == VNET_HW_INTERFACE_BOND_INFO_SLAVE)
1011 rv = VNET_API_ERROR_BOND_SLAVE_NOT_ALLOWED;
1015 sw_if_index = si->sw_if_index;
1016 sub_id = ntohl (mp->sub_id);
1018 sup_and_sub_key = ((u64) (sw_if_index) << 32) | (u64) sub_id;
1020 p = hash_get_mem (im->sw_if_index_by_sup_and_sub, &sup_and_sub_key);
1024 clib_warning ("sup sw_if_index %d, sub id %d already exists\n",
1025 sw_if_index, sub_id);
1026 rv = VNET_API_ERROR_SUBIF_ALREADY_EXISTS;
1030 memset (&template, 0, sizeof (template));
1031 template.type = VNET_SW_INTERFACE_TYPE_SUB;
1032 template.sup_sw_if_index = sw_if_index;
1033 template.sub.id = sub_id;
1034 template.sub.eth.flags.no_tags = mp->no_tags;
1035 template.sub.eth.flags.one_tag = mp->one_tag;
1036 template.sub.eth.flags.two_tags = mp->two_tags;
1037 template.sub.eth.flags.dot1ad = mp->dot1ad;
1038 template.sub.eth.flags.exact_match = mp->exact_match;
1039 template.sub.eth.flags.default_sub = mp->default_sub;
1040 template.sub.eth.flags.outer_vlan_id_any = mp->outer_vlan_id_any;
1041 template.sub.eth.flags.inner_vlan_id_any = mp->inner_vlan_id_any;
1042 template.sub.eth.outer_vlan_id = ntohs (mp->outer_vlan_id);
1043 template.sub.eth.inner_vlan_id = ntohs (mp->inner_vlan_id);
1045 error = vnet_create_sw_interface (vnm, &template, &sw_if_index);
1048 clib_error_report (error);
1049 rv = VNET_API_ERROR_SUBIF_CREATE_FAILED;
1053 u64 *kp = clib_mem_alloc (sizeof (*kp));
1054 *kp = sup_and_sub_key;
1056 hash_set (hi->sub_interface_sw_if_index_by_id, sub_id, sw_if_index);
1057 hash_set_mem (im->sw_if_index_by_sup_and_sub, kp, sw_if_index);
1059 BAD_SW_IF_INDEX_LABEL;
1064 REPLY_MACRO2(VL_API_CREATE_SUBIF_REPLY,
1066 rmp->sw_if_index = ntohl(sw_if_index);
1072 vl_api_delete_subif_t_handler (vl_api_delete_subif_t * mp)
1074 vl_api_delete_subif_reply_t *rmp;
1077 rv = vnet_delete_sub_interface (ntohl (mp->sw_if_index));
1079 REPLY_MACRO (VL_API_DELETE_SUBIF_REPLY);
1083 vl_api_interface_name_renumber_t_handler (vl_api_interface_name_renumber_t *
1086 vl_api_interface_name_renumber_reply_t *rmp;
1089 VALIDATE_SW_IF_INDEX (mp);
1091 rv = vnet_interface_name_renumber
1092 (ntohl (mp->sw_if_index), ntohl (mp->new_show_dev_instance));
1094 BAD_SW_IF_INDEX_LABEL;
1096 REPLY_MACRO (VL_API_INTERFACE_NAME_RENUMBER_REPLY);
1100 vl_api_create_loopback_t_handler (vl_api_create_loopback_t * mp)
1102 vl_api_create_loopback_reply_t *rmp;
1106 rv = vnet_create_loopback_interface (&sw_if_index, mp->mac_address, 0, 0);
1109 REPLY_MACRO2(VL_API_CREATE_LOOPBACK_REPLY,
1111 rmp->sw_if_index = ntohl (sw_if_index);
1116 static void vl_api_create_loopback_instance_t_handler
1117 (vl_api_create_loopback_instance_t * mp)
1119 vl_api_create_loopback_instance_reply_t *rmp;
1121 u8 is_specified = mp->is_specified;
1122 u32 user_instance = ntohl (mp->user_instance);
1125 rv = vnet_create_loopback_interface (&sw_if_index, mp->mac_address,
1126 is_specified, user_instance);
1129 REPLY_MACRO2(VL_API_CREATE_LOOPBACK_INSTANCE_REPLY,
1131 rmp->sw_if_index = ntohl (sw_if_index);
1137 vl_api_delete_loopback_t_handler (vl_api_delete_loopback_t * mp)
1139 vl_api_delete_loopback_reply_t *rmp;
1143 sw_if_index = ntohl (mp->sw_if_index);
1144 rv = vnet_delete_loopback_interface (sw_if_index);
1146 REPLY_MACRO (VL_API_DELETE_LOOPBACK_REPLY);
1150 vl_api_collect_detailed_interface_stats_t_handler
1151 (vl_api_collect_detailed_interface_stats_t * mp)
1153 vl_api_collect_detailed_interface_stats_reply_t *rmp;
1157 vnet_sw_interface_stats_collect_enable_disable (ntohl (mp->sw_if_index),
1158 mp->enable_disable);
1160 REPLY_MACRO (VL_API_COLLECT_DETAILED_INTERFACE_STATS_REPLY);
1165 * Add vpe's API message handlers to the table.
1166 * vlib has alread mapped shared memory and
1167 * added the client registration handlers.
1168 * See .../vlib-api/vlibmemory/memclnt_vlib.c:memclnt_process()
1170 #define vl_msg_name_crc_list
1171 #include <vnet/interface.api.h>
1172 #undef vl_msg_name_crc_list
1175 setup_message_id_table (api_main_t * am)
1177 #define _(id,n,crc) vl_msg_api_add_msg_name_crc (am, #n "_" #crc, id);
1178 foreach_vl_msg_name_crc_interface;
1182 pub_sub_handler (interface_events, INTERFACE_EVENTS);
1184 static clib_error_t *
1185 interface_api_hookup (vlib_main_t * vm)
1187 api_main_t *am = &api_main;
1190 vl_msg_api_set_handlers(VL_API_##N, #n, \
1191 vl_api_##n##_t_handler, \
1193 vl_api_##n##_t_endian, \
1194 vl_api_##n##_t_print, \
1195 sizeof(vl_api_##n##_t), 1);
1196 foreach_vpe_api_msg;
1200 * Set up the (msg_name, crc, message-id) table
1202 setup_message_id_table (am);
1207 VLIB_API_INIT_FUNCTION (interface_api_hookup);
1210 * fd.io coding-style-patch-verification: ON
1213 * eval: (c-set-style "gnu")