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 _(CREATE_VLAN_SUBIF, create_vlan_subif) \
65 _(CREATE_SUBIF, create_subif) \
66 _(DELETE_SUBIF, delete_subif) \
67 _(CREATE_LOOPBACK, create_loopback) \
68 _(CREATE_LOOPBACK_INSTANCE, create_loopback_instance) \
69 _(DELETE_LOOPBACK, delete_loopback) \
70 _(INTERFACE_NAME_RENUMBER, interface_name_renumber)
73 vl_api_sw_interface_set_flags_t_handler (vl_api_sw_interface_set_flags_t * mp)
75 vl_api_sw_interface_set_flags_reply_t *rmp;
76 vnet_main_t *vnm = vnet_get_main ();
81 VALIDATE_SW_IF_INDEX (mp);
83 flags = mp->admin_up_down ? VNET_SW_INTERFACE_FLAG_ADMIN_UP : 0;
85 error = vnet_sw_interface_set_flags (vnm, ntohl (mp->sw_if_index), flags);
89 clib_error_report (error);
92 BAD_SW_IF_INDEX_LABEL;
93 REPLY_MACRO (VL_API_SW_INTERFACE_SET_FLAGS_REPLY);
97 vl_api_sw_interface_set_mtu_t_handler (vl_api_sw_interface_set_mtu_t * mp)
99 vl_api_sw_interface_set_mtu_reply_t *rmp;
100 vnet_main_t *vnm = vnet_get_main ();
101 u32 flags = ETHERNET_INTERFACE_FLAG_MTU;
102 u32 sw_if_index = ntohl (mp->sw_if_index);
103 u16 mtu = ntohs (mp->mtu);
104 ethernet_main_t *em = ðernet_main;
107 VALIDATE_SW_IF_INDEX (mp);
109 vnet_sw_interface_t *si = vnet_get_sw_interface (vnm, sw_if_index);
110 if (si->type != VNET_SW_INTERFACE_TYPE_HARDWARE)
112 rv = VNET_API_ERROR_INVALID_VALUE;
113 goto bad_sw_if_index;
116 vnet_hw_interface_t *hi = vnet_get_hw_interface (vnm, si->hw_if_index);
117 ethernet_interface_t *eif = ethernet_get_interface (em, si->hw_if_index);
121 rv = VNET_API_ERROR_FEATURE_DISABLED;
122 goto bad_sw_if_index;
125 if (mtu < hi->min_supported_packet_bytes)
127 rv = VNET_API_ERROR_INVALID_VALUE;
128 goto bad_sw_if_index;
131 if (mtu > hi->max_supported_packet_bytes)
133 rv = VNET_API_ERROR_INVALID_VALUE;
134 goto bad_sw_if_index;
137 if (hi->max_packet_bytes != mtu)
139 hi->max_packet_bytes = mtu;
140 ethernet_set_flags (vnm, si->hw_if_index, flags);
143 BAD_SW_IF_INDEX_LABEL;
144 REPLY_MACRO (VL_API_SW_INTERFACE_SET_MTU_REPLY);
148 send_sw_interface_details (vpe_api_main_t * am,
149 vl_api_registration_t * rp,
150 vnet_sw_interface_t * swif,
151 u8 * interface_name, u32 context)
153 vnet_hw_interface_t *hi =
154 vnet_get_sup_hw_interface (am->vnet_main, swif->sw_if_index);
156 vl_api_sw_interface_details_t *mp = vl_msg_api_alloc (sizeof (*mp));
157 memset (mp, 0, sizeof (*mp));
158 mp->_vl_msg_id = ntohs (VL_API_SW_INTERFACE_DETAILS);
159 mp->sw_if_index = ntohl (swif->sw_if_index);
160 mp->sup_sw_if_index = ntohl (swif->sup_sw_if_index);
161 mp->admin_up_down = (swif->flags & VNET_SW_INTERFACE_FLAG_ADMIN_UP) ? 1 : 0;
162 mp->link_up_down = (hi->flags & VNET_HW_INTERFACE_FLAG_LINK_UP) ? 1 : 0;
163 mp->link_duplex = ((hi->flags & VNET_HW_INTERFACE_FLAG_DUPLEX_MASK) >>
164 VNET_HW_INTERFACE_FLAG_DUPLEX_SHIFT);
165 mp->link_speed = ((hi->flags & VNET_HW_INTERFACE_FLAG_SPEED_MASK) >>
166 VNET_HW_INTERFACE_FLAG_SPEED_SHIFT);
167 mp->link_mtu = ntohs (hi->max_packet_bytes);
168 mp->context = context;
170 strncpy ((char *) mp->interface_name,
171 (char *) interface_name, ARRAY_LEN (mp->interface_name) - 1);
173 /* Send the L2 address for ethernet physical intfcs */
174 if (swif->sup_sw_if_index == swif->sw_if_index
175 && hi->hw_class_index == ethernet_hw_interface_class.index)
177 ethernet_main_t *em = ethernet_get_main (am->vlib_main);
178 ethernet_interface_t *ei;
180 ei = pool_elt_at_index (em->interfaces, hi->hw_instance);
181 ASSERT (sizeof (mp->l2_address) >= sizeof (ei->address));
182 clib_memcpy (mp->l2_address, ei->address, sizeof (ei->address));
183 mp->l2_address_length = ntohl (sizeof (ei->address));
185 else if (swif->sup_sw_if_index != swif->sw_if_index)
187 vnet_sub_interface_t *sub = &swif->sub;
188 mp->sub_id = ntohl (sub->id);
189 mp->sub_dot1ad = sub->eth.flags.dot1ad;
190 mp->sub_number_of_tags =
191 sub->eth.flags.one_tag + sub->eth.flags.two_tags * 2;
192 mp->sub_outer_vlan_id = ntohs (sub->eth.outer_vlan_id);
193 mp->sub_inner_vlan_id = ntohs (sub->eth.inner_vlan_id);
194 mp->sub_exact_match = sub->eth.flags.exact_match;
195 mp->sub_default = sub->eth.flags.default_sub;
196 mp->sub_outer_vlan_id_any = sub->eth.flags.outer_vlan_id_any;
197 mp->sub_inner_vlan_id_any = sub->eth.flags.inner_vlan_id_any;
199 /* vlan tag rewrite data */
200 u32 vtr_op = L2_VTR_DISABLED;
201 u32 vtr_push_dot1q = 0, vtr_tag1 = 0, vtr_tag2 = 0;
203 if (l2vtr_get (am->vlib_main, am->vnet_main, swif->sw_if_index,
204 &vtr_op, &vtr_push_dot1q, &vtr_tag1, &vtr_tag2) != 0)
206 // error - default to disabled
207 mp->vtr_op = ntohl (L2_VTR_DISABLED);
208 clib_warning ("cannot get vlan tag rewrite for sw_if_index %d",
213 mp->vtr_op = ntohl (vtr_op);
214 mp->vtr_push_dot1q = ntohl (vtr_push_dot1q);
215 mp->vtr_tag1 = ntohl (vtr_tag1);
216 mp->vtr_tag2 = ntohl (vtr_tag2);
220 /* pbb tag rewrite data */
221 ethernet_header_t eth_hdr;
222 u32 vtr_op = L2_VTR_DISABLED;
226 memset (ð_hdr, 0, sizeof (eth_hdr));
228 if (!l2pbb_get (am->vlib_main, am->vnet_main, swif->sw_if_index,
229 &vtr_op, &outer_tag, ð_hdr, &b_vlanid, &i_sid))
232 clib_memcpy (mp->b_dmac, eth_hdr.dst_address,
233 sizeof (eth_hdr.dst_address));
234 clib_memcpy (mp->b_smac, eth_hdr.src_address,
235 sizeof (eth_hdr.src_address));
236 mp->b_vlanid = b_vlanid;
240 u8 *tag = vnet_get_sw_interface_tag (vnet_get_main (), swif->sw_if_index);
242 strncpy ((char *) mp->tag, (char *) tag, ARRAY_LEN (mp->tag) - 1);
244 vl_api_send_msg (rp, (u8 *) mp);
248 vl_api_sw_interface_dump_t_handler (vl_api_sw_interface_dump_t * mp)
250 vpe_api_main_t *am = &vpe_api_main;
251 vnet_sw_interface_t *swif;
252 vnet_interface_main_t *im = &am->vnet_main->interface_main;
253 vl_api_registration_t *rp;
255 rp = vl_api_client_index_to_registration (mp->client_index);
259 clib_warning ("Client %d AWOL", mp->client_index);
263 u8 *filter = 0, *name = 0;
264 if (mp->name_filter_valid)
266 mp->name_filter[ARRAY_LEN (mp->name_filter) - 1] = 0;
267 filter = format (0, "%s%c", mp->name_filter, 0);
270 char *strcasestr (char *, char *); /* lnx hdr file botch */
272 pool_foreach (swif, im->sw_interfaces,
274 if (!vnet_swif_is_api_visible (swif))
276 vec_reset_length(name);
277 name = format (name, "%U%c", format_vnet_sw_interface_name, am->vnet_main,
280 if (filter && !strcasestr((char *) name, (char *) filter))
283 send_sw_interface_details (am, rp, swif, name, mp->context);
292 vl_api_sw_interface_add_del_address_t_handler
293 (vl_api_sw_interface_add_del_address_t * mp)
295 vlib_main_t *vm = vlib_get_main ();
296 vnet_main_t *vnm = vnet_get_main ();
297 vl_api_sw_interface_add_del_address_reply_t *rmp;
300 clib_error_t *error = 0;
302 VALIDATE_SW_IF_INDEX (mp);
304 is_del = mp->is_add == 0;
308 ip_del_all_interface_addresses (vm, ntohl (mp->sw_if_index));
309 else if (mp->is_ipv6)
310 error = ip6_add_del_interface_address (vm, ntohl (mp->sw_if_index),
311 (void *) mp->address,
312 mp->address_length, is_del);
314 error = ip4_add_del_interface_address (vm, ntohl (mp->sw_if_index),
315 (void *) mp->address,
316 mp->address_length, is_del);
321 clib_error_report (error);
325 BAD_SW_IF_INDEX_LABEL;
328 REPLY_MACRO (VL_API_SW_INTERFACE_ADD_DEL_ADDRESS_REPLY);
331 void stats_dslock_with_hint (int hint, int tag) __attribute__ ((weak));
333 stats_dslock_with_hint (int hint, int tag)
337 void stats_dsunlock (void) __attribute__ ((weak));
339 stats_dsunlock (void)
344 vl_api_sw_interface_set_table_t_handler (vl_api_sw_interface_set_table_t * mp)
346 vl_api_sw_interface_set_table_reply_t *rmp;
347 u32 sw_if_index = ntohl (mp->sw_if_index);
348 u32 table_id = ntohl (mp->vrf_id);
351 VALIDATE_SW_IF_INDEX (mp);
353 stats_dslock_with_hint (1 /* release hint */ , 4 /* tag */ );
356 rv = ip_table_bind (FIB_PROTOCOL_IP6, sw_if_index, table_id, 1);
358 rv = ip_table_bind (FIB_PROTOCOL_IP4, sw_if_index, table_id, 1);
362 BAD_SW_IF_INDEX_LABEL;
364 REPLY_MACRO (VL_API_SW_INTERFACE_SET_TABLE_REPLY);
368 ip_table_bind (fib_protocol_t fproto,
369 u32 sw_if_index, u32 table_id, u8 is_api)
371 CLIB_UNUSED (ip_interface_address_t * ia);
372 u32 fib_index, mfib_index;
378 src = FIB_SOURCE_API;
379 msrc = MFIB_SOURCE_API;
383 src = FIB_SOURCE_CLI;
384 msrc = MFIB_SOURCE_CLI;
388 * This is temporary whilst I do the song and dance with the CSIT version
392 fib_index = fib_table_find_or_create_and_lock (fproto, table_id, src);
394 mfib_table_find_or_create_and_lock (fproto, table_id, msrc);
403 * This if table does not exist = error is what we want in the end.
405 /* fib_index = fib_table_find (fproto, table_id); */
406 /* mfib_index = mfib_table_find (fproto, table_id); */
408 /* if (~0 == fib_index || ~0 == mfib_index) */
410 /* return (VNET_API_ERROR_NO_SUCH_FIB); */
413 if (FIB_PROTOCOL_IP6 == fproto)
416 * If the interface already has in IP address, then a change int
417 * VRF is not allowed. The IP address applied must first be removed.
418 * We do not do that automatically here, since VPP has no knowledge
419 * of whether thoses subnets are valid in the destination VRF.
422 foreach_ip_interface_address (&ip6_main.lookup_main,
424 1 /* honor unnumbered */ ,
426 return (VNET_API_ERROR_ADDRESS_FOUND_FOR_INTERFACE);
430 vec_validate (ip6_main.fib_index_by_sw_if_index, sw_if_index);
431 vec_validate (ip6_main.mfib_index_by_sw_if_index, sw_if_index);
434 * tell those that are interested that the binding is changing.
436 ip6_table_bind_callback_t *cb;
437 vec_foreach (cb, ip6_main.table_bind_callbacks)
438 cb->function (&ip6_main, cb->function_opaque,
441 ip6_main.fib_index_by_sw_if_index[sw_if_index]);
445 /* reset back to default */
446 if (0 != ip6_main.fib_index_by_sw_if_index[sw_if_index])
447 fib_table_unlock (ip6_main.fib_index_by_sw_if_index[sw_if_index],
448 FIB_PROTOCOL_IP6, src);
449 if (0 != ip6_main.mfib_index_by_sw_if_index[sw_if_index])
450 mfib_table_unlock (ip6_main.mfib_index_by_sw_if_index
451 [sw_if_index], FIB_PROTOCOL_IP6, msrc);
456 /* we need to lock the table now it's inuse */
457 fib_table_lock (fib_index, FIB_PROTOCOL_IP6, src);
458 mfib_table_lock (mfib_index, FIB_PROTOCOL_IP6, msrc);
461 ip6_main.fib_index_by_sw_if_index[sw_if_index] = fib_index;
462 ip6_main.mfib_index_by_sw_if_index[sw_if_index] = mfib_index;
467 * If the interface already has in IP address, then a change int
468 * VRF is not allowed. The IP address applied must first be removed.
469 * We do not do that automatically here, since VPP has no knowledge
470 * of whether thoses subnets are valid in the destination VRF.
473 foreach_ip_interface_address (&ip4_main.lookup_main,
475 1 /* honor unnumbered */ ,
477 return (VNET_API_ERROR_ADDRESS_FOUND_FOR_INTERFACE);
481 vec_validate (ip4_main.fib_index_by_sw_if_index, sw_if_index);
482 vec_validate (ip4_main.mfib_index_by_sw_if_index, sw_if_index);
485 * tell those that are interested that the binding is changing.
487 ip4_table_bind_callback_t *cb;
488 vec_foreach (cb, ip4_main.table_bind_callbacks)
489 cb->function (&ip4_main, cb->function_opaque,
492 ip4_main.fib_index_by_sw_if_index[sw_if_index]);
496 /* reset back to default */
497 if (0 != ip4_main.fib_index_by_sw_if_index[sw_if_index])
498 fib_table_unlock (ip4_main.fib_index_by_sw_if_index[sw_if_index],
499 FIB_PROTOCOL_IP4, src);
500 if (0 != ip4_main.mfib_index_by_sw_if_index[sw_if_index])
501 mfib_table_unlock (ip4_main.mfib_index_by_sw_if_index
502 [sw_if_index], FIB_PROTOCOL_IP4, msrc);
507 /* we need to lock the table now it's inuse */
508 fib_index = fib_table_find_or_create_and_lock (FIB_PROTOCOL_IP4,
511 mfib_index = mfib_table_find_or_create_and_lock (FIB_PROTOCOL_IP4,
515 ip4_main.fib_index_by_sw_if_index[sw_if_index] = fib_index;
516 ip4_main.mfib_index_by_sw_if_index[sw_if_index] = mfib_index;
520 * Temporary. undo the locks from the find and create at the staart
524 fib_table_unlock (fib_index, fproto, src);
525 mfib_table_unlock (mfib_index, fproto, msrc);
532 send_sw_interface_get_table_reply (vl_api_registration_t * reg,
533 u32 context, int retval, u32 vrf_id)
535 vl_api_sw_interface_get_table_reply_t *mp;
537 mp = vl_msg_api_alloc (sizeof (*mp));
538 memset (mp, 0, sizeof (*mp));
539 mp->_vl_msg_id = ntohs (VL_API_SW_INTERFACE_GET_TABLE_REPLY);
540 mp->context = context;
541 mp->retval = htonl (retval);
542 mp->vrf_id = htonl (vrf_id);
544 vl_api_send_msg (reg, (u8 *) mp);
548 vl_api_sw_interface_get_table_t_handler (vl_api_sw_interface_get_table_t * mp)
550 vl_api_registration_t *reg;
551 fib_table_t *fib_table = 0;
552 u32 sw_if_index = ~0;
555 fib_protocol_t fib_proto = FIB_PROTOCOL_IP4;
558 reg = vl_api_client_index_to_registration (mp->client_index);
562 VALIDATE_SW_IF_INDEX (mp);
564 sw_if_index = ntohl (mp->sw_if_index);
567 fib_proto = FIB_PROTOCOL_IP6;
569 fib_index = fib_table_get_index_for_sw_if_index (fib_proto, sw_if_index);
572 fib_table = fib_table_get (fib_index, fib_proto);
573 table_id = fib_table->ft_table_id;
576 BAD_SW_IF_INDEX_LABEL;
578 send_sw_interface_get_table_reply (reg, mp->context, rv, table_id);
581 static void vl_api_sw_interface_set_unnumbered_t_handler
582 (vl_api_sw_interface_set_unnumbered_t * mp)
584 vl_api_sw_interface_set_unnumbered_reply_t *rmp;
586 vnet_main_t *vnm = vnet_get_main ();
587 u32 sw_if_index = ntohl (mp->sw_if_index);
588 u32 unnumbered_sw_if_index = ntohl (mp->unnumbered_sw_if_index);
592 * The API message field names are backwards from
593 * the underlying data structure names.
594 * It's not worth changing them now.
596 if (!vnet_sw_interface_is_api_valid (vnm, unnumbered_sw_if_index))
598 rv = VNET_API_ERROR_INVALID_SW_IF_INDEX;
602 /* Only check the "use loop0" field when setting the binding */
603 if (mp->is_add && !vnet_sw_interface_is_api_valid (vnm, sw_if_index))
605 rv = VNET_API_ERROR_INVALID_SW_IF_INDEX_2;
609 vnet_sw_interface_t *si =
610 vnet_get_sw_interface (vnm, unnumbered_sw_if_index);
611 was_unnum = (si->flags & VNET_SW_INTERFACE_FLAG_UNNUMBERED);
615 si->flags |= VNET_SW_INTERFACE_FLAG_UNNUMBERED;
616 si->unnumbered_sw_if_index = sw_if_index;
618 ip4_main.lookup_main.if_address_pool_index_by_sw_if_index
619 [unnumbered_sw_if_index] =
621 lookup_main.if_address_pool_index_by_sw_if_index[sw_if_index];
623 lookup_main.if_address_pool_index_by_sw_if_index
624 [unnumbered_sw_if_index] =
626 lookup_main.if_address_pool_index_by_sw_if_index[sw_if_index];
630 si->flags &= ~(VNET_SW_INTERFACE_FLAG_UNNUMBERED);
631 si->unnumbered_sw_if_index = (u32) ~ 0;
633 ip4_main.lookup_main.if_address_pool_index_by_sw_if_index
634 [unnumbered_sw_if_index] = ~0;
635 ip6_main.lookup_main.if_address_pool_index_by_sw_if_index
636 [unnumbered_sw_if_index] = ~0;
639 if (was_unnum != (si->flags & VNET_SW_INTERFACE_FLAG_UNNUMBERED))
641 ip4_sw_interface_enable_disable (unnumbered_sw_if_index, mp->is_add);
642 ip6_sw_interface_enable_disable (unnumbered_sw_if_index, mp->is_add);
646 REPLY_MACRO (VL_API_SW_INTERFACE_SET_UNNUMBERED_REPLY);
650 vl_api_sw_interface_clear_stats_t_handler (vl_api_sw_interface_clear_stats_t *
653 vl_api_sw_interface_clear_stats_reply_t *rmp;
655 vnet_main_t *vnm = vnet_get_main ();
656 vnet_interface_main_t *im = &vnm->interface_main;
657 vlib_simple_counter_main_t *sm;
658 vlib_combined_counter_main_t *cm;
659 static vnet_main_t **my_vnet_mains;
660 int i, j, n_counters;
663 if (mp->sw_if_index != ~0)
664 VALIDATE_SW_IF_INDEX (mp);
666 vec_reset_length (my_vnet_mains);
668 for (i = 0; i < vec_len (vnet_mains); i++)
671 vec_add1 (my_vnet_mains, vnet_mains[i]);
674 if (vec_len (vnet_mains) == 0)
675 vec_add1 (my_vnet_mains, vnm);
677 n_counters = vec_len (im->combined_sw_if_counters);
679 for (j = 0; j < n_counters; j++)
681 for (i = 0; i < vec_len (my_vnet_mains); i++)
683 im = &my_vnet_mains[i]->interface_main;
684 cm = im->combined_sw_if_counters + j;
685 if (mp->sw_if_index == (u32) ~ 0)
686 vlib_clear_combined_counters (cm);
688 vlib_zero_combined_counter (cm, ntohl (mp->sw_if_index));
692 n_counters = vec_len (im->sw_if_counters);
694 for (j = 0; j < n_counters; j++)
696 for (i = 0; i < vec_len (my_vnet_mains); i++)
698 im = &my_vnet_mains[i]->interface_main;
699 sm = im->sw_if_counters + j;
700 if (mp->sw_if_index == (u32) ~ 0)
701 vlib_clear_simple_counters (sm);
703 vlib_zero_simple_counter (sm, ntohl (mp->sw_if_index));
707 BAD_SW_IF_INDEX_LABEL;
709 REPLY_MACRO (VL_API_SW_INTERFACE_CLEAR_STATS_REPLY);
712 #define API_LINK_STATE_EVENT 1
713 #define API_ADMIN_UP_DOWN_EVENT 2
716 event_data_cmp (void *a1, void *a2)
721 return (word) e1[0] - (word) e2[0];
725 send_sw_interface_event (vpe_api_main_t * am,
726 vpe_client_registration_t * reg,
727 vl_api_registration_t * vl_reg,
728 vnet_sw_interface_t * swif)
730 vl_api_sw_interface_event_t *mp;
731 vnet_main_t *vnm = am->vnet_main;
733 vnet_hw_interface_t *hi = vnet_get_sup_hw_interface (vnm,
735 mp = vl_msg_api_alloc (sizeof (*mp));
736 memset (mp, 0, sizeof (*mp));
737 mp->_vl_msg_id = ntohs (VL_API_SW_INTERFACE_EVENT);
738 mp->sw_if_index = ntohl (swif->sw_if_index);
739 mp->client_index = reg->client_index;
740 mp->pid = reg->client_pid;
742 mp->admin_up_down = (swif->flags & VNET_SW_INTERFACE_FLAG_ADMIN_UP) ? 1 : 0;
743 mp->link_up_down = (hi->flags & VNET_HW_INTERFACE_FLAG_LINK_UP) ? 1 : 0;
744 vl_api_send_msg (vl_reg, (u8 *) mp);
748 link_state_process (vlib_main_t * vm,
749 vlib_node_runtime_t * rt, vlib_frame_t * f)
751 vpe_api_main_t *vam = &vpe_api_main;
752 vnet_main_t *vnm = vam->vnet_main;
753 vnet_sw_interface_t *swif;
754 uword *event_data = 0;
755 vpe_client_registration_t *reg;
757 u32 prev_sw_if_index;
758 vl_api_registration_t *vl_reg;
760 vam->link_state_process_up = 1;
764 vlib_process_wait_for_event (vm);
766 /* Unified list of changed link or admin state sw_if_indices */
767 vlib_process_get_events_with_type
768 (vm, &event_data, API_LINK_STATE_EVENT);
769 vlib_process_get_events_with_type
770 (vm, &event_data, API_ADMIN_UP_DOWN_EVENT);
772 /* Sort, so we can eliminate duplicates */
773 vec_sort_with_function (event_data, event_data_cmp);
775 prev_sw_if_index = ~0;
777 for (i = 0; i < vec_len (event_data); i++)
779 /* Only one message per swif */
780 if (prev_sw_if_index == event_data[i])
782 prev_sw_if_index = event_data[i];
785 pool_foreach(reg, vam->interface_events_registrations,
787 vl_reg = vl_api_client_index_to_registration (reg->client_index);
790 /* sw_interface may be deleted already */
791 if (!pool_is_free_index (vnm->interface_main.sw_interfaces,
794 swif = vnet_get_sw_interface (vnm, event_data[i]);
795 send_sw_interface_event (vam, reg, vl_reg, swif);
801 vec_reset_length (event_data);
807 static clib_error_t *link_up_down_function (vnet_main_t * vm, u32 hw_if_index,
809 static clib_error_t *admin_up_down_function (vnet_main_t * vm,
810 u32 hw_if_index, u32 flags);
813 VLIB_REGISTER_NODE (link_state_process_node,static) = {
814 .function = link_state_process,
815 .type = VLIB_NODE_TYPE_PROCESS,
816 .name = "vpe-link-state-process",
820 VNET_SW_INTERFACE_ADMIN_UP_DOWN_FUNCTION (admin_up_down_function);
821 VNET_HW_INTERFACE_LINK_UP_DOWN_FUNCTION (link_up_down_function);
823 static clib_error_t *
824 link_up_down_function (vnet_main_t * vm, u32 hw_if_index, u32 flags)
826 vpe_api_main_t *vam = &vpe_api_main;
827 vnet_hw_interface_t *hi = vnet_get_hw_interface (vm, hw_if_index);
829 if (vam->link_state_process_up)
830 vlib_process_signal_event (vam->vlib_main,
831 link_state_process_node.index,
832 API_LINK_STATE_EVENT, hi->sw_if_index);
836 static clib_error_t *
837 admin_up_down_function (vnet_main_t * vm, u32 sw_if_index, u32 flags)
839 vpe_api_main_t *vam = &vpe_api_main;
842 * Note: it's perfectly fair to set a subif admin up / admin down.
843 * Note the subtle distinction between this routine and the previous
846 if (vam->link_state_process_up)
847 vlib_process_signal_event (vam->vlib_main,
848 link_state_process_node.index,
849 API_ADMIN_UP_DOWN_EVENT, sw_if_index);
853 static void vl_api_sw_interface_tag_add_del_t_handler
854 (vl_api_sw_interface_tag_add_del_t * mp)
856 vnet_main_t *vnm = vnet_get_main ();
857 vl_api_sw_interface_tag_add_del_reply_t *rmp;
860 u32 sw_if_index = ntohl (mp->sw_if_index);
862 VALIDATE_SW_IF_INDEX (mp);
868 rv = VNET_API_ERROR_INVALID_VALUE;
872 mp->tag[ARRAY_LEN (mp->tag) - 1] = 0;
873 tag = format (0, "%s%c", mp->tag, 0);
874 vnet_set_sw_interface_tag (vnm, tag, sw_if_index);
877 vnet_clear_sw_interface_tag (vnm, sw_if_index);
879 BAD_SW_IF_INDEX_LABEL;
881 REPLY_MACRO (VL_API_SW_INTERFACE_TAG_ADD_DEL_REPLY);
884 static void vl_api_sw_interface_set_mac_address_t_handler
885 (vl_api_sw_interface_set_mac_address_t * mp)
887 vl_api_sw_interface_set_mac_address_reply_t *rmp;
888 vnet_main_t *vnm = vnet_get_main ();
889 u32 sw_if_index = ntohl (mp->sw_if_index);
890 vnet_sw_interface_t *si;
894 VALIDATE_SW_IF_INDEX (mp);
896 si = vnet_get_sw_interface (vnm, sw_if_index);
897 error = vnet_hw_interface_change_mac_address (vnm, si->hw_if_index,
901 rv = VNET_API_ERROR_UNIMPLEMENTED;
902 clib_error_report (error);
906 BAD_SW_IF_INDEX_LABEL;
908 REPLY_MACRO (VL_API_SW_INTERFACE_SET_MAC_ADDRESS_REPLY);
911 static void vl_api_sw_interface_set_rx_mode_t_handler
912 (vl_api_sw_interface_set_rx_mode_t * mp)
914 vl_api_sw_interface_set_rx_mode_reply_t *rmp;
915 vnet_main_t *vnm = vnet_get_main ();
916 u32 sw_if_index = ntohl (mp->sw_if_index);
917 vnet_sw_interface_t *si;
921 VALIDATE_SW_IF_INDEX (mp);
923 si = vnet_get_sw_interface (vnm, sw_if_index);
924 error = set_hw_interface_change_rx_mode (vnm, si->hw_if_index,
926 ntohl (mp->queue_id), mp->mode);
929 rv = VNET_API_ERROR_UNIMPLEMENTED;
930 clib_error_report (error);
934 BAD_SW_IF_INDEX_LABEL;
936 REPLY_MACRO (VL_API_SW_INTERFACE_SET_RX_MODE_REPLY);
940 vl_api_create_vlan_subif_t_handler (vl_api_create_vlan_subif_t * mp)
942 vl_api_create_vlan_subif_reply_t *rmp;
943 vnet_main_t *vnm = vnet_get_main ();
944 u32 sw_if_index = (u32) ~ 0;
945 vnet_hw_interface_t *hi;
948 vnet_sw_interface_t template;
950 vnet_interface_main_t *im = &vnm->interface_main;
952 vl_api_registration_t *reg;
955 VALIDATE_SW_IF_INDEX (mp);
957 hi = vnet_get_sup_hw_interface (vnm, ntohl (mp->sw_if_index));
959 if (hi->bond_info == VNET_HW_INTERFACE_BOND_INFO_SLAVE)
961 rv = VNET_API_ERROR_BOND_SLAVE_NOT_ALLOWED;
965 id = ntohl (mp->vlan_id);
966 if (id == 0 || id > 4095)
968 rv = VNET_API_ERROR_INVALID_VLAN;
972 sup_and_sub_key = ((u64) (hi->sw_if_index) << 32) | (u64) id;
974 p = hash_get_mem (im->sw_if_index_by_sup_and_sub, &sup_and_sub_key);
977 rv = VNET_API_ERROR_VLAN_ALREADY_EXISTS;
981 memset (&template, 0, sizeof (template));
982 template.type = VNET_SW_INTERFACE_TYPE_SUB;
983 template.sup_sw_if_index = hi->sw_if_index;
984 template.sub.id = id;
985 template.sub.eth.raw_flags = 0;
986 template.sub.eth.flags.one_tag = 1;
987 template.sub.eth.outer_vlan_id = id;
988 template.sub.eth.flags.exact_match = 1;
990 error = vnet_create_sw_interface (vnm, &template, &sw_if_index);
993 clib_error_report (error);
994 rv = VNET_API_ERROR_INVALID_REGISTRATION;
998 u64 *kp = clib_mem_alloc (sizeof (*kp));
999 *kp = sup_and_sub_key;
1001 hash_set (hi->sub_interface_sw_if_index_by_id, id, sw_if_index);
1002 hash_set_mem (im->sw_if_index_by_sup_and_sub, kp, sw_if_index);
1004 BAD_SW_IF_INDEX_LABEL;
1007 reg = vl_api_client_index_to_registration (mp->client_index);
1011 rmp = vl_msg_api_alloc (sizeof (*rmp));
1012 rmp->_vl_msg_id = htons (VL_API_CREATE_VLAN_SUBIF_REPLY);
1013 rmp->context = mp->context;
1014 rmp->retval = htonl (rv);
1015 rmp->sw_if_index = htonl (sw_if_index);
1016 vl_api_send_msg (reg, (u8 *) rmp);
1020 vl_api_create_subif_t_handler (vl_api_create_subif_t * mp)
1022 vl_api_create_subif_reply_t *rmp;
1023 vnet_main_t *vnm = vnet_get_main ();
1024 u32 sw_if_index = ~0;
1027 vnet_sw_interface_t *si;
1028 vnet_hw_interface_t *hi;
1029 vnet_sw_interface_t template;
1031 vnet_interface_main_t *im = &vnm->interface_main;
1032 u64 sup_and_sub_key;
1033 clib_error_t *error;
1035 VALIDATE_SW_IF_INDEX (mp);
1037 si = vnet_get_sup_sw_interface (vnm, ntohl (mp->sw_if_index));
1038 hi = vnet_get_sup_hw_interface (vnm, ntohl (mp->sw_if_index));
1040 if (hi->bond_info == VNET_HW_INTERFACE_BOND_INFO_SLAVE)
1042 rv = VNET_API_ERROR_BOND_SLAVE_NOT_ALLOWED;
1046 sw_if_index = si->sw_if_index;
1047 sub_id = ntohl (mp->sub_id);
1049 sup_and_sub_key = ((u64) (sw_if_index) << 32) | (u64) sub_id;
1051 p = hash_get_mem (im->sw_if_index_by_sup_and_sub, &sup_and_sub_key);
1055 clib_warning ("sup sw_if_index %d, sub id %d already exists\n",
1056 sw_if_index, sub_id);
1057 rv = VNET_API_ERROR_SUBIF_ALREADY_EXISTS;
1061 memset (&template, 0, sizeof (template));
1062 template.type = VNET_SW_INTERFACE_TYPE_SUB;
1063 template.sup_sw_if_index = sw_if_index;
1064 template.sub.id = sub_id;
1065 template.sub.eth.flags.no_tags = mp->no_tags;
1066 template.sub.eth.flags.one_tag = mp->one_tag;
1067 template.sub.eth.flags.two_tags = mp->two_tags;
1068 template.sub.eth.flags.dot1ad = mp->dot1ad;
1069 template.sub.eth.flags.exact_match = mp->exact_match;
1070 template.sub.eth.flags.default_sub = mp->default_sub;
1071 template.sub.eth.flags.outer_vlan_id_any = mp->outer_vlan_id_any;
1072 template.sub.eth.flags.inner_vlan_id_any = mp->inner_vlan_id_any;
1073 template.sub.eth.outer_vlan_id = ntohs (mp->outer_vlan_id);
1074 template.sub.eth.inner_vlan_id = ntohs (mp->inner_vlan_id);
1076 error = vnet_create_sw_interface (vnm, &template, &sw_if_index);
1079 clib_error_report (error);
1080 rv = VNET_API_ERROR_SUBIF_CREATE_FAILED;
1084 u64 *kp = clib_mem_alloc (sizeof (*kp));
1085 *kp = sup_and_sub_key;
1087 hash_set (hi->sub_interface_sw_if_index_by_id, sub_id, sw_if_index);
1088 hash_set_mem (im->sw_if_index_by_sup_and_sub, kp, sw_if_index);
1090 BAD_SW_IF_INDEX_LABEL;
1095 REPLY_MACRO2(VL_API_CREATE_SUBIF_REPLY,
1097 rmp->sw_if_index = ntohl(sw_if_index);
1103 vl_api_delete_subif_t_handler (vl_api_delete_subif_t * mp)
1105 vl_api_delete_subif_reply_t *rmp;
1108 rv = vnet_delete_sub_interface (ntohl (mp->sw_if_index));
1110 REPLY_MACRO (VL_API_DELETE_SUBIF_REPLY);
1114 vl_api_interface_name_renumber_t_handler (vl_api_interface_name_renumber_t *
1117 vl_api_interface_name_renumber_reply_t *rmp;
1120 VALIDATE_SW_IF_INDEX (mp);
1122 rv = vnet_interface_name_renumber
1123 (ntohl (mp->sw_if_index), ntohl (mp->new_show_dev_instance));
1125 BAD_SW_IF_INDEX_LABEL;
1127 REPLY_MACRO (VL_API_INTERFACE_NAME_RENUMBER_REPLY);
1131 vl_api_create_loopback_t_handler (vl_api_create_loopback_t * mp)
1133 vl_api_create_loopback_reply_t *rmp;
1137 rv = vnet_create_loopback_interface (&sw_if_index, mp->mac_address, 0, 0);
1140 REPLY_MACRO2(VL_API_CREATE_LOOPBACK_REPLY,
1142 rmp->sw_if_index = ntohl (sw_if_index);
1147 static void vl_api_create_loopback_instance_t_handler
1148 (vl_api_create_loopback_instance_t * mp)
1150 vl_api_create_loopback_instance_reply_t *rmp;
1152 u8 is_specified = mp->is_specified;
1153 u32 user_instance = ntohl (mp->user_instance);
1156 rv = vnet_create_loopback_interface (&sw_if_index, mp->mac_address,
1157 is_specified, user_instance);
1160 REPLY_MACRO2(VL_API_CREATE_LOOPBACK_INSTANCE_REPLY,
1162 rmp->sw_if_index = ntohl (sw_if_index);
1168 vl_api_delete_loopback_t_handler (vl_api_delete_loopback_t * mp)
1170 vl_api_delete_loopback_reply_t *rmp;
1174 sw_if_index = ntohl (mp->sw_if_index);
1175 rv = vnet_delete_loopback_interface (sw_if_index);
1177 REPLY_MACRO (VL_API_DELETE_LOOPBACK_REPLY);
1182 * Add vpe's API message handlers to the table.
1183 * vlib has alread mapped shared memory and
1184 * added the client registration handlers.
1185 * See .../vlib-api/vlibmemory/memclnt_vlib.c:memclnt_process()
1187 #define vl_msg_name_crc_list
1188 #include <vnet/interface.api.h>
1189 #undef vl_msg_name_crc_list
1192 setup_message_id_table (api_main_t * am)
1194 #define _(id,n,crc) vl_msg_api_add_msg_name_crc (am, #n "_" #crc, id);
1195 foreach_vl_msg_name_crc_interface;
1199 pub_sub_handler (interface_events, INTERFACE_EVENTS);
1201 static clib_error_t *
1202 interface_api_hookup (vlib_main_t * vm)
1204 api_main_t *am = &api_main;
1207 vl_msg_api_set_handlers(VL_API_##N, #n, \
1208 vl_api_##n##_t_handler, \
1210 vl_api_##n##_t_endian, \
1211 vl_api_##n##_t_print, \
1212 sizeof(vl_api_##n##_t), 1);
1213 foreach_vpe_api_msg;
1217 * Set up the (msg_name, crc, message-id) table
1219 setup_message_id_table (am);
1224 VLIB_API_INIT_FUNCTION (interface_api_hookup);
1227 * fd.io coding-style-patch-verification: ON
1230 * eval: (c-set-style "gnu")