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 sw_if_index = ntohl (mp->sw_if_index);
102 u16 mtu = ntohs (mp->mtu);
103 ethernet_main_t *em = ðernet_main;
106 VALIDATE_SW_IF_INDEX (mp);
108 vnet_sw_interface_t *si = vnet_get_sw_interface (vnm, sw_if_index);
109 if (si->type != VNET_SW_INTERFACE_TYPE_HARDWARE)
111 rv = VNET_API_ERROR_INVALID_VALUE;
112 goto bad_sw_if_index;
115 vnet_hw_interface_t *hi = vnet_get_hw_interface (vnm, si->hw_if_index);
116 ethernet_interface_t *eif = ethernet_get_interface (em, si->hw_if_index);
120 rv = VNET_API_ERROR_FEATURE_DISABLED;
121 goto bad_sw_if_index;
124 if (mtu < hi->min_supported_packet_bytes)
126 rv = VNET_API_ERROR_INVALID_VALUE;
127 goto bad_sw_if_index;
130 if (mtu > hi->max_supported_packet_bytes)
132 rv = VNET_API_ERROR_INVALID_VALUE;
133 goto bad_sw_if_index;
136 vnet_hw_interface_set_mtu (vnm, si->hw_if_index, mtu);
138 BAD_SW_IF_INDEX_LABEL;
139 REPLY_MACRO (VL_API_SW_INTERFACE_SET_MTU_REPLY);
143 send_sw_interface_details (vpe_api_main_t * am,
144 vl_api_registration_t * rp,
145 vnet_sw_interface_t * swif,
146 u8 * interface_name, u32 context)
148 vnet_hw_interface_t *hi =
149 vnet_get_sup_hw_interface (am->vnet_main, swif->sw_if_index);
151 vl_api_sw_interface_details_t *mp = vl_msg_api_alloc (sizeof (*mp));
152 memset (mp, 0, sizeof (*mp));
153 mp->_vl_msg_id = ntohs (VL_API_SW_INTERFACE_DETAILS);
154 mp->sw_if_index = ntohl (swif->sw_if_index);
155 mp->sup_sw_if_index = ntohl (swif->sup_sw_if_index);
156 mp->admin_up_down = (swif->flags & VNET_SW_INTERFACE_FLAG_ADMIN_UP) ? 1 : 0;
157 mp->link_up_down = (hi->flags & VNET_HW_INTERFACE_FLAG_LINK_UP) ? 1 : 0;
158 mp->link_duplex = ((hi->flags & VNET_HW_INTERFACE_FLAG_DUPLEX_MASK) >>
159 VNET_HW_INTERFACE_FLAG_DUPLEX_SHIFT);
160 mp->link_speed = ((hi->flags & VNET_HW_INTERFACE_FLAG_SPEED_MASK) >>
161 VNET_HW_INTERFACE_FLAG_SPEED_SHIFT);
162 mp->link_mtu = ntohs (hi->max_packet_bytes);
163 mp->context = context;
165 strncpy ((char *) mp->interface_name,
166 (char *) interface_name, ARRAY_LEN (mp->interface_name) - 1);
168 /* Send the L2 address for ethernet physical intfcs */
169 if (swif->sup_sw_if_index == swif->sw_if_index
170 && hi->hw_class_index == ethernet_hw_interface_class.index)
172 ethernet_main_t *em = ethernet_get_main (am->vlib_main);
173 ethernet_interface_t *ei;
175 ei = pool_elt_at_index (em->interfaces, hi->hw_instance);
176 ASSERT (sizeof (mp->l2_address) >= sizeof (ei->address));
177 clib_memcpy (mp->l2_address, ei->address, sizeof (ei->address));
178 mp->l2_address_length = ntohl (sizeof (ei->address));
180 else if (swif->sup_sw_if_index != swif->sw_if_index)
182 vnet_sub_interface_t *sub = &swif->sub;
183 mp->sub_id = ntohl (sub->id);
184 mp->sub_dot1ad = sub->eth.flags.dot1ad;
185 mp->sub_number_of_tags =
186 sub->eth.flags.one_tag + sub->eth.flags.two_tags * 2;
187 mp->sub_outer_vlan_id = ntohs (sub->eth.outer_vlan_id);
188 mp->sub_inner_vlan_id = ntohs (sub->eth.inner_vlan_id);
189 mp->sub_exact_match = sub->eth.flags.exact_match;
190 mp->sub_default = sub->eth.flags.default_sub;
191 mp->sub_outer_vlan_id_any = sub->eth.flags.outer_vlan_id_any;
192 mp->sub_inner_vlan_id_any = sub->eth.flags.inner_vlan_id_any;
194 /* vlan tag rewrite data */
195 u32 vtr_op = L2_VTR_DISABLED;
196 u32 vtr_push_dot1q = 0, vtr_tag1 = 0, vtr_tag2 = 0;
198 if (l2vtr_get (am->vlib_main, am->vnet_main, swif->sw_if_index,
199 &vtr_op, &vtr_push_dot1q, &vtr_tag1, &vtr_tag2) != 0)
201 // error - default to disabled
202 mp->vtr_op = ntohl (L2_VTR_DISABLED);
203 clib_warning ("cannot get vlan tag rewrite for sw_if_index %d",
208 mp->vtr_op = ntohl (vtr_op);
209 mp->vtr_push_dot1q = ntohl (vtr_push_dot1q);
210 mp->vtr_tag1 = ntohl (vtr_tag1);
211 mp->vtr_tag2 = ntohl (vtr_tag2);
215 /* pbb tag rewrite data */
216 ethernet_header_t eth_hdr;
217 u32 vtr_op = L2_VTR_DISABLED;
221 memset (ð_hdr, 0, sizeof (eth_hdr));
223 if (!l2pbb_get (am->vlib_main, am->vnet_main, swif->sw_if_index,
224 &vtr_op, &outer_tag, ð_hdr, &b_vlanid, &i_sid))
227 clib_memcpy (mp->b_dmac, eth_hdr.dst_address,
228 sizeof (eth_hdr.dst_address));
229 clib_memcpy (mp->b_smac, eth_hdr.src_address,
230 sizeof (eth_hdr.src_address));
231 mp->b_vlanid = b_vlanid;
235 u8 *tag = vnet_get_sw_interface_tag (vnet_get_main (), swif->sw_if_index);
237 strncpy ((char *) mp->tag, (char *) tag, ARRAY_LEN (mp->tag) - 1);
239 vl_api_send_msg (rp, (u8 *) mp);
243 vl_api_sw_interface_dump_t_handler (vl_api_sw_interface_dump_t * mp)
245 vpe_api_main_t *am = &vpe_api_main;
246 vnet_sw_interface_t *swif;
247 vnet_interface_main_t *im = &am->vnet_main->interface_main;
248 vl_api_registration_t *rp;
250 rp = vl_api_client_index_to_registration (mp->client_index);
254 clib_warning ("Client %d AWOL", mp->client_index);
258 u8 *filter = 0, *name = 0;
259 if (mp->name_filter_valid)
261 mp->name_filter[ARRAY_LEN (mp->name_filter) - 1] = 0;
262 filter = format (0, "%s%c", mp->name_filter, 0);
265 char *strcasestr (char *, char *); /* lnx hdr file botch */
267 pool_foreach (swif, im->sw_interfaces,
269 if (!vnet_swif_is_api_visible (swif))
271 vec_reset_length(name);
272 name = format (name, "%U%c", format_vnet_sw_interface_name, am->vnet_main,
275 if (filter && !strcasestr((char *) name, (char *) filter))
278 send_sw_interface_details (am, rp, swif, name, mp->context);
287 vl_api_sw_interface_add_del_address_t_handler
288 (vl_api_sw_interface_add_del_address_t * mp)
290 vlib_main_t *vm = vlib_get_main ();
291 vnet_main_t *vnm = vnet_get_main ();
292 vl_api_sw_interface_add_del_address_reply_t *rmp;
295 clib_error_t *error = 0;
297 VALIDATE_SW_IF_INDEX (mp);
299 is_del = mp->is_add == 0;
303 ip_del_all_interface_addresses (vm, ntohl (mp->sw_if_index));
304 else if (mp->is_ipv6)
305 error = ip6_add_del_interface_address (vm, ntohl (mp->sw_if_index),
306 (void *) mp->address,
307 mp->address_length, is_del);
309 error = ip4_add_del_interface_address (vm, ntohl (mp->sw_if_index),
310 (void *) mp->address,
311 mp->address_length, is_del);
316 clib_error_report (error);
320 BAD_SW_IF_INDEX_LABEL;
323 REPLY_MACRO (VL_API_SW_INTERFACE_ADD_DEL_ADDRESS_REPLY);
326 void stats_dslock_with_hint (int hint, int tag) __attribute__ ((weak));
328 stats_dslock_with_hint (int hint, int tag)
332 void stats_dsunlock (void) __attribute__ ((weak));
334 stats_dsunlock (void)
339 vl_api_sw_interface_set_table_t_handler (vl_api_sw_interface_set_table_t * mp)
341 vl_api_sw_interface_set_table_reply_t *rmp;
342 u32 sw_if_index = ntohl (mp->sw_if_index);
343 u32 table_id = ntohl (mp->vrf_id);
346 VALIDATE_SW_IF_INDEX (mp);
348 stats_dslock_with_hint (1 /* release hint */ , 4 /* tag */ );
351 rv = ip_table_bind (FIB_PROTOCOL_IP6, sw_if_index, table_id, 1);
353 rv = ip_table_bind (FIB_PROTOCOL_IP4, sw_if_index, table_id, 1);
357 BAD_SW_IF_INDEX_LABEL;
359 REPLY_MACRO (VL_API_SW_INTERFACE_SET_TABLE_REPLY);
363 ip_table_bind (fib_protocol_t fproto,
364 u32 sw_if_index, u32 table_id, u8 is_api)
366 CLIB_UNUSED (ip_interface_address_t * ia);
367 u32 fib_index, mfib_index;
373 src = FIB_SOURCE_API;
374 msrc = MFIB_SOURCE_API;
378 src = FIB_SOURCE_CLI;
379 msrc = MFIB_SOURCE_CLI;
383 * This is temporary whilst I do the song and dance with the CSIT version
387 fib_index = fib_table_find_or_create_and_lock (fproto, table_id, src);
389 mfib_table_find_or_create_and_lock (fproto, table_id, msrc);
398 * This if table does not exist = error is what we want in the end.
400 /* fib_index = fib_table_find (fproto, table_id); */
401 /* mfib_index = mfib_table_find (fproto, table_id); */
403 /* if (~0 == fib_index || ~0 == mfib_index) */
405 /* return (VNET_API_ERROR_NO_SUCH_FIB); */
408 if (FIB_PROTOCOL_IP6 == fproto)
411 * If the interface already has in IP address, then a change int
412 * VRF is not allowed. The IP address applied must first be removed.
413 * We do not do that automatically here, since VPP has no knowledge
414 * of whether thoses subnets are valid in the destination VRF.
417 foreach_ip_interface_address (&ip6_main.lookup_main,
419 1 /* honor unnumbered */ ,
421 return (VNET_API_ERROR_ADDRESS_FOUND_FOR_INTERFACE);
425 vec_validate (ip6_main.fib_index_by_sw_if_index, sw_if_index);
426 vec_validate (ip6_main.mfib_index_by_sw_if_index, sw_if_index);
429 * tell those that are interested that the binding is changing.
431 ip6_table_bind_callback_t *cb;
432 vec_foreach (cb, ip6_main.table_bind_callbacks)
433 cb->function (&ip6_main, cb->function_opaque,
436 ip6_main.fib_index_by_sw_if_index[sw_if_index]);
440 /* reset back to default */
441 if (0 != ip6_main.fib_index_by_sw_if_index[sw_if_index])
442 fib_table_unlock (ip6_main.fib_index_by_sw_if_index[sw_if_index],
443 FIB_PROTOCOL_IP6, src);
444 if (0 != ip6_main.mfib_index_by_sw_if_index[sw_if_index])
445 mfib_table_unlock (ip6_main.mfib_index_by_sw_if_index
446 [sw_if_index], FIB_PROTOCOL_IP6, msrc);
451 /* we need to lock the table now it's inuse */
452 fib_table_lock (fib_index, FIB_PROTOCOL_IP6, src);
453 mfib_table_lock (mfib_index, FIB_PROTOCOL_IP6, msrc);
456 ip6_main.fib_index_by_sw_if_index[sw_if_index] = fib_index;
457 ip6_main.mfib_index_by_sw_if_index[sw_if_index] = mfib_index;
462 * If the interface already has in IP address, then a change int
463 * VRF is not allowed. The IP address applied must first be removed.
464 * We do not do that automatically here, since VPP has no knowledge
465 * of whether thoses subnets are valid in the destination VRF.
468 foreach_ip_interface_address (&ip4_main.lookup_main,
470 1 /* honor unnumbered */ ,
472 return (VNET_API_ERROR_ADDRESS_FOUND_FOR_INTERFACE);
476 vec_validate (ip4_main.fib_index_by_sw_if_index, sw_if_index);
477 vec_validate (ip4_main.mfib_index_by_sw_if_index, sw_if_index);
480 * tell those that are interested that the binding is changing.
482 ip4_table_bind_callback_t *cb;
483 vec_foreach (cb, ip4_main.table_bind_callbacks)
484 cb->function (&ip4_main, cb->function_opaque,
487 ip4_main.fib_index_by_sw_if_index[sw_if_index]);
491 /* reset back to default */
492 if (0 != ip4_main.fib_index_by_sw_if_index[sw_if_index])
493 fib_table_unlock (ip4_main.fib_index_by_sw_if_index[sw_if_index],
494 FIB_PROTOCOL_IP4, src);
495 if (0 != ip4_main.mfib_index_by_sw_if_index[sw_if_index])
496 mfib_table_unlock (ip4_main.mfib_index_by_sw_if_index
497 [sw_if_index], FIB_PROTOCOL_IP4, msrc);
502 /* we need to lock the table now it's inuse */
503 fib_index = fib_table_find_or_create_and_lock (FIB_PROTOCOL_IP4,
506 mfib_index = mfib_table_find_or_create_and_lock (FIB_PROTOCOL_IP4,
510 ip4_main.fib_index_by_sw_if_index[sw_if_index] = fib_index;
511 ip4_main.mfib_index_by_sw_if_index[sw_if_index] = mfib_index;
515 * Temporary. undo the locks from the find and create at the staart
519 fib_table_unlock (fib_index, fproto, src);
520 mfib_table_unlock (mfib_index, fproto, msrc);
527 send_sw_interface_get_table_reply (vl_api_registration_t * reg,
528 u32 context, int retval, u32 vrf_id)
530 vl_api_sw_interface_get_table_reply_t *mp;
532 mp = vl_msg_api_alloc (sizeof (*mp));
533 memset (mp, 0, sizeof (*mp));
534 mp->_vl_msg_id = ntohs (VL_API_SW_INTERFACE_GET_TABLE_REPLY);
535 mp->context = context;
536 mp->retval = htonl (retval);
537 mp->vrf_id = htonl (vrf_id);
539 vl_api_send_msg (reg, (u8 *) mp);
543 vl_api_sw_interface_get_table_t_handler (vl_api_sw_interface_get_table_t * mp)
545 vl_api_registration_t *reg;
546 fib_table_t *fib_table = 0;
547 u32 sw_if_index = ~0;
550 fib_protocol_t fib_proto = FIB_PROTOCOL_IP4;
553 reg = vl_api_client_index_to_registration (mp->client_index);
557 VALIDATE_SW_IF_INDEX (mp);
559 sw_if_index = ntohl (mp->sw_if_index);
562 fib_proto = FIB_PROTOCOL_IP6;
564 fib_index = fib_table_get_index_for_sw_if_index (fib_proto, sw_if_index);
567 fib_table = fib_table_get (fib_index, fib_proto);
568 table_id = fib_table->ft_table_id;
571 BAD_SW_IF_INDEX_LABEL;
573 send_sw_interface_get_table_reply (reg, mp->context, rv, table_id);
576 static void vl_api_sw_interface_set_unnumbered_t_handler
577 (vl_api_sw_interface_set_unnumbered_t * mp)
579 vl_api_sw_interface_set_unnumbered_reply_t *rmp;
581 vnet_main_t *vnm = vnet_get_main ();
582 u32 sw_if_index = ntohl (mp->sw_if_index);
583 u32 unnumbered_sw_if_index = ntohl (mp->unnumbered_sw_if_index);
587 * The API message field names are backwards from
588 * the underlying data structure names.
589 * It's not worth changing them now.
591 if (!vnet_sw_interface_is_api_valid (vnm, unnumbered_sw_if_index))
593 rv = VNET_API_ERROR_INVALID_SW_IF_INDEX;
597 /* Only check the "use loop0" field when setting the binding */
598 if (mp->is_add && !vnet_sw_interface_is_api_valid (vnm, sw_if_index))
600 rv = VNET_API_ERROR_INVALID_SW_IF_INDEX_2;
604 vnet_sw_interface_t *si =
605 vnet_get_sw_interface (vnm, unnumbered_sw_if_index);
606 was_unnum = (si->flags & VNET_SW_INTERFACE_FLAG_UNNUMBERED);
610 si->flags |= VNET_SW_INTERFACE_FLAG_UNNUMBERED;
611 si->unnumbered_sw_if_index = sw_if_index;
613 ip4_main.lookup_main.if_address_pool_index_by_sw_if_index
614 [unnumbered_sw_if_index] =
616 lookup_main.if_address_pool_index_by_sw_if_index[sw_if_index];
618 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];
625 si->flags &= ~(VNET_SW_INTERFACE_FLAG_UNNUMBERED);
626 si->unnumbered_sw_if_index = (u32) ~ 0;
628 ip4_main.lookup_main.if_address_pool_index_by_sw_if_index
629 [unnumbered_sw_if_index] = ~0;
630 ip6_main.lookup_main.if_address_pool_index_by_sw_if_index
631 [unnumbered_sw_if_index] = ~0;
634 if (was_unnum != (si->flags & VNET_SW_INTERFACE_FLAG_UNNUMBERED))
636 ip4_sw_interface_enable_disable (unnumbered_sw_if_index, mp->is_add);
637 ip6_sw_interface_enable_disable (unnumbered_sw_if_index, mp->is_add);
641 REPLY_MACRO (VL_API_SW_INTERFACE_SET_UNNUMBERED_REPLY);
645 vl_api_sw_interface_clear_stats_t_handler (vl_api_sw_interface_clear_stats_t *
648 vl_api_sw_interface_clear_stats_reply_t *rmp;
650 vnet_main_t *vnm = vnet_get_main ();
651 vnet_interface_main_t *im = &vnm->interface_main;
652 vlib_simple_counter_main_t *sm;
653 vlib_combined_counter_main_t *cm;
654 static vnet_main_t **my_vnet_mains;
655 int i, j, n_counters;
658 if (mp->sw_if_index != ~0)
659 VALIDATE_SW_IF_INDEX (mp);
661 vec_reset_length (my_vnet_mains);
663 for (i = 0; i < vec_len (vnet_mains); i++)
666 vec_add1 (my_vnet_mains, vnet_mains[i]);
669 if (vec_len (vnet_mains) == 0)
670 vec_add1 (my_vnet_mains, vnm);
672 n_counters = vec_len (im->combined_sw_if_counters);
674 for (j = 0; j < n_counters; j++)
676 for (i = 0; i < vec_len (my_vnet_mains); i++)
678 im = &my_vnet_mains[i]->interface_main;
679 cm = im->combined_sw_if_counters + j;
680 if (mp->sw_if_index == (u32) ~ 0)
681 vlib_clear_combined_counters (cm);
683 vlib_zero_combined_counter (cm, ntohl (mp->sw_if_index));
687 n_counters = vec_len (im->sw_if_counters);
689 for (j = 0; j < n_counters; j++)
691 for (i = 0; i < vec_len (my_vnet_mains); i++)
693 im = &my_vnet_mains[i]->interface_main;
694 sm = im->sw_if_counters + j;
695 if (mp->sw_if_index == (u32) ~ 0)
696 vlib_clear_simple_counters (sm);
698 vlib_zero_simple_counter (sm, ntohl (mp->sw_if_index));
702 BAD_SW_IF_INDEX_LABEL;
704 REPLY_MACRO (VL_API_SW_INTERFACE_CLEAR_STATS_REPLY);
707 #define API_LINK_STATE_EVENT 1
708 #define API_ADMIN_UP_DOWN_EVENT 2
711 event_data_cmp (void *a1, void *a2)
716 return (word) e1[0] - (word) e2[0];
720 send_sw_interface_event (vpe_api_main_t * am,
721 vpe_client_registration_t * reg,
722 vl_api_registration_t * vl_reg,
723 vnet_sw_interface_t * swif)
725 vl_api_sw_interface_event_t *mp;
726 vnet_main_t *vnm = am->vnet_main;
728 vnet_hw_interface_t *hi = vnet_get_sup_hw_interface (vnm,
730 mp = vl_msg_api_alloc (sizeof (*mp));
731 memset (mp, 0, sizeof (*mp));
732 mp->_vl_msg_id = ntohs (VL_API_SW_INTERFACE_EVENT);
733 mp->sw_if_index = ntohl (swif->sw_if_index);
734 mp->client_index = reg->client_index;
735 mp->pid = reg->client_pid;
737 mp->admin_up_down = (swif->flags & VNET_SW_INTERFACE_FLAG_ADMIN_UP) ? 1 : 0;
738 mp->link_up_down = (hi->flags & VNET_HW_INTERFACE_FLAG_LINK_UP) ? 1 : 0;
739 vl_api_send_msg (vl_reg, (u8 *) mp);
743 link_state_process (vlib_main_t * vm,
744 vlib_node_runtime_t * rt, vlib_frame_t * f)
746 vpe_api_main_t *vam = &vpe_api_main;
747 vnet_main_t *vnm = vam->vnet_main;
748 vnet_sw_interface_t *swif;
749 uword *event_data = 0;
750 vpe_client_registration_t *reg;
752 u32 prev_sw_if_index;
753 vl_api_registration_t *vl_reg;
755 vam->link_state_process_up = 1;
759 vlib_process_wait_for_event (vm);
761 /* Unified list of changed link or admin state sw_if_indices */
762 vlib_process_get_events_with_type
763 (vm, &event_data, API_LINK_STATE_EVENT);
764 vlib_process_get_events_with_type
765 (vm, &event_data, API_ADMIN_UP_DOWN_EVENT);
767 /* Sort, so we can eliminate duplicates */
768 vec_sort_with_function (event_data, event_data_cmp);
770 prev_sw_if_index = ~0;
772 for (i = 0; i < vec_len (event_data); i++)
774 /* Only one message per swif */
775 if (prev_sw_if_index == event_data[i])
777 prev_sw_if_index = event_data[i];
780 pool_foreach(reg, vam->interface_events_registrations,
782 vl_reg = vl_api_client_index_to_registration (reg->client_index);
785 /* sw_interface may be deleted already */
786 if (!pool_is_free_index (vnm->interface_main.sw_interfaces,
789 swif = vnet_get_sw_interface (vnm, event_data[i]);
790 send_sw_interface_event (vam, reg, vl_reg, swif);
796 vec_reset_length (event_data);
802 static clib_error_t *link_up_down_function (vnet_main_t * vm, u32 hw_if_index,
804 static clib_error_t *admin_up_down_function (vnet_main_t * vm,
805 u32 hw_if_index, u32 flags);
808 VLIB_REGISTER_NODE (link_state_process_node,static) = {
809 .function = link_state_process,
810 .type = VLIB_NODE_TYPE_PROCESS,
811 .name = "vpe-link-state-process",
815 VNET_SW_INTERFACE_ADMIN_UP_DOWN_FUNCTION (admin_up_down_function);
816 VNET_HW_INTERFACE_LINK_UP_DOWN_FUNCTION (link_up_down_function);
818 static clib_error_t *
819 link_up_down_function (vnet_main_t * vm, u32 hw_if_index, u32 flags)
821 vpe_api_main_t *vam = &vpe_api_main;
822 vnet_hw_interface_t *hi = vnet_get_hw_interface (vm, hw_if_index);
824 if (vam->link_state_process_up)
825 vlib_process_signal_event (vam->vlib_main,
826 link_state_process_node.index,
827 API_LINK_STATE_EVENT, hi->sw_if_index);
831 static clib_error_t *
832 admin_up_down_function (vnet_main_t * vm, u32 sw_if_index, u32 flags)
834 vpe_api_main_t *vam = &vpe_api_main;
837 * Note: it's perfectly fair to set a subif admin up / admin down.
838 * Note the subtle distinction between this routine and the previous
841 if (vam->link_state_process_up)
842 vlib_process_signal_event (vam->vlib_main,
843 link_state_process_node.index,
844 API_ADMIN_UP_DOWN_EVENT, sw_if_index);
848 static void vl_api_sw_interface_tag_add_del_t_handler
849 (vl_api_sw_interface_tag_add_del_t * mp)
851 vnet_main_t *vnm = vnet_get_main ();
852 vl_api_sw_interface_tag_add_del_reply_t *rmp;
855 u32 sw_if_index = ntohl (mp->sw_if_index);
857 VALIDATE_SW_IF_INDEX (mp);
863 rv = VNET_API_ERROR_INVALID_VALUE;
867 mp->tag[ARRAY_LEN (mp->tag) - 1] = 0;
868 tag = format (0, "%s%c", mp->tag, 0);
869 vnet_set_sw_interface_tag (vnm, tag, sw_if_index);
872 vnet_clear_sw_interface_tag (vnm, sw_if_index);
874 BAD_SW_IF_INDEX_LABEL;
876 REPLY_MACRO (VL_API_SW_INTERFACE_TAG_ADD_DEL_REPLY);
879 static void vl_api_sw_interface_set_mac_address_t_handler
880 (vl_api_sw_interface_set_mac_address_t * mp)
882 vl_api_sw_interface_set_mac_address_reply_t *rmp;
883 vnet_main_t *vnm = vnet_get_main ();
884 u32 sw_if_index = ntohl (mp->sw_if_index);
885 vnet_sw_interface_t *si;
889 VALIDATE_SW_IF_INDEX (mp);
891 si = vnet_get_sw_interface (vnm, sw_if_index);
892 error = vnet_hw_interface_change_mac_address (vnm, si->hw_if_index,
896 rv = VNET_API_ERROR_UNIMPLEMENTED;
897 clib_error_report (error);
901 BAD_SW_IF_INDEX_LABEL;
903 REPLY_MACRO (VL_API_SW_INTERFACE_SET_MAC_ADDRESS_REPLY);
906 static void vl_api_sw_interface_set_rx_mode_t_handler
907 (vl_api_sw_interface_set_rx_mode_t * mp)
909 vl_api_sw_interface_set_rx_mode_reply_t *rmp;
910 vnet_main_t *vnm = vnet_get_main ();
911 u32 sw_if_index = ntohl (mp->sw_if_index);
912 vnet_sw_interface_t *si;
916 VALIDATE_SW_IF_INDEX (mp);
918 si = vnet_get_sw_interface (vnm, sw_if_index);
919 error = set_hw_interface_change_rx_mode (vnm, si->hw_if_index,
921 ntohl (mp->queue_id), mp->mode);
924 rv = VNET_API_ERROR_UNIMPLEMENTED;
925 clib_error_report (error);
929 BAD_SW_IF_INDEX_LABEL;
931 REPLY_MACRO (VL_API_SW_INTERFACE_SET_RX_MODE_REPLY);
935 vl_api_create_vlan_subif_t_handler (vl_api_create_vlan_subif_t * mp)
937 vl_api_create_vlan_subif_reply_t *rmp;
938 vnet_main_t *vnm = vnet_get_main ();
939 u32 sw_if_index = (u32) ~ 0;
940 vnet_hw_interface_t *hi;
943 vnet_sw_interface_t template;
945 vnet_interface_main_t *im = &vnm->interface_main;
947 vl_api_registration_t *reg;
950 VALIDATE_SW_IF_INDEX (mp);
952 hi = vnet_get_sup_hw_interface (vnm, ntohl (mp->sw_if_index));
954 if (hi->bond_info == VNET_HW_INTERFACE_BOND_INFO_SLAVE)
956 rv = VNET_API_ERROR_BOND_SLAVE_NOT_ALLOWED;
960 id = ntohl (mp->vlan_id);
961 if (id == 0 || id > 4095)
963 rv = VNET_API_ERROR_INVALID_VLAN;
967 sup_and_sub_key = ((u64) (hi->sw_if_index) << 32) | (u64) id;
969 p = hash_get_mem (im->sw_if_index_by_sup_and_sub, &sup_and_sub_key);
972 rv = VNET_API_ERROR_VLAN_ALREADY_EXISTS;
976 memset (&template, 0, sizeof (template));
977 template.type = VNET_SW_INTERFACE_TYPE_SUB;
978 template.sup_sw_if_index = hi->sw_if_index;
979 template.sub.id = id;
980 template.sub.eth.raw_flags = 0;
981 template.sub.eth.flags.one_tag = 1;
982 template.sub.eth.outer_vlan_id = id;
983 template.sub.eth.flags.exact_match = 1;
985 error = vnet_create_sw_interface (vnm, &template, &sw_if_index);
988 clib_error_report (error);
989 rv = VNET_API_ERROR_INVALID_REGISTRATION;
993 u64 *kp = clib_mem_alloc (sizeof (*kp));
994 *kp = sup_and_sub_key;
996 hash_set (hi->sub_interface_sw_if_index_by_id, id, sw_if_index);
997 hash_set_mem (im->sw_if_index_by_sup_and_sub, kp, sw_if_index);
999 BAD_SW_IF_INDEX_LABEL;
1002 reg = vl_api_client_index_to_registration (mp->client_index);
1006 rmp = vl_msg_api_alloc (sizeof (*rmp));
1007 rmp->_vl_msg_id = htons (VL_API_CREATE_VLAN_SUBIF_REPLY);
1008 rmp->context = mp->context;
1009 rmp->retval = htonl (rv);
1010 rmp->sw_if_index = htonl (sw_if_index);
1011 vl_api_send_msg (reg, (u8 *) rmp);
1015 vl_api_create_subif_t_handler (vl_api_create_subif_t * mp)
1017 vl_api_create_subif_reply_t *rmp;
1018 vnet_main_t *vnm = vnet_get_main ();
1019 u32 sw_if_index = ~0;
1022 vnet_sw_interface_t *si;
1023 vnet_hw_interface_t *hi;
1024 vnet_sw_interface_t template;
1026 vnet_interface_main_t *im = &vnm->interface_main;
1027 u64 sup_and_sub_key;
1028 clib_error_t *error;
1030 VALIDATE_SW_IF_INDEX (mp);
1032 si = vnet_get_sup_sw_interface (vnm, ntohl (mp->sw_if_index));
1033 hi = vnet_get_sup_hw_interface (vnm, ntohl (mp->sw_if_index));
1035 if (hi->bond_info == VNET_HW_INTERFACE_BOND_INFO_SLAVE)
1037 rv = VNET_API_ERROR_BOND_SLAVE_NOT_ALLOWED;
1041 sw_if_index = si->sw_if_index;
1042 sub_id = ntohl (mp->sub_id);
1044 sup_and_sub_key = ((u64) (sw_if_index) << 32) | (u64) sub_id;
1046 p = hash_get_mem (im->sw_if_index_by_sup_and_sub, &sup_and_sub_key);
1050 clib_warning ("sup sw_if_index %d, sub id %d already exists\n",
1051 sw_if_index, sub_id);
1052 rv = VNET_API_ERROR_SUBIF_ALREADY_EXISTS;
1056 memset (&template, 0, sizeof (template));
1057 template.type = VNET_SW_INTERFACE_TYPE_SUB;
1058 template.sup_sw_if_index = sw_if_index;
1059 template.sub.id = sub_id;
1060 template.sub.eth.flags.no_tags = mp->no_tags;
1061 template.sub.eth.flags.one_tag = mp->one_tag;
1062 template.sub.eth.flags.two_tags = mp->two_tags;
1063 template.sub.eth.flags.dot1ad = mp->dot1ad;
1064 template.sub.eth.flags.exact_match = mp->exact_match;
1065 template.sub.eth.flags.default_sub = mp->default_sub;
1066 template.sub.eth.flags.outer_vlan_id_any = mp->outer_vlan_id_any;
1067 template.sub.eth.flags.inner_vlan_id_any = mp->inner_vlan_id_any;
1068 template.sub.eth.outer_vlan_id = ntohs (mp->outer_vlan_id);
1069 template.sub.eth.inner_vlan_id = ntohs (mp->inner_vlan_id);
1071 error = vnet_create_sw_interface (vnm, &template, &sw_if_index);
1074 clib_error_report (error);
1075 rv = VNET_API_ERROR_SUBIF_CREATE_FAILED;
1079 u64 *kp = clib_mem_alloc (sizeof (*kp));
1080 *kp = sup_and_sub_key;
1082 hash_set (hi->sub_interface_sw_if_index_by_id, sub_id, sw_if_index);
1083 hash_set_mem (im->sw_if_index_by_sup_and_sub, kp, sw_if_index);
1085 BAD_SW_IF_INDEX_LABEL;
1090 REPLY_MACRO2(VL_API_CREATE_SUBIF_REPLY,
1092 rmp->sw_if_index = ntohl(sw_if_index);
1098 vl_api_delete_subif_t_handler (vl_api_delete_subif_t * mp)
1100 vl_api_delete_subif_reply_t *rmp;
1103 rv = vnet_delete_sub_interface (ntohl (mp->sw_if_index));
1105 REPLY_MACRO (VL_API_DELETE_SUBIF_REPLY);
1109 vl_api_interface_name_renumber_t_handler (vl_api_interface_name_renumber_t *
1112 vl_api_interface_name_renumber_reply_t *rmp;
1115 VALIDATE_SW_IF_INDEX (mp);
1117 rv = vnet_interface_name_renumber
1118 (ntohl (mp->sw_if_index), ntohl (mp->new_show_dev_instance));
1120 BAD_SW_IF_INDEX_LABEL;
1122 REPLY_MACRO (VL_API_INTERFACE_NAME_RENUMBER_REPLY);
1126 vl_api_create_loopback_t_handler (vl_api_create_loopback_t * mp)
1128 vl_api_create_loopback_reply_t *rmp;
1132 rv = vnet_create_loopback_interface (&sw_if_index, mp->mac_address, 0, 0);
1135 REPLY_MACRO2(VL_API_CREATE_LOOPBACK_REPLY,
1137 rmp->sw_if_index = ntohl (sw_if_index);
1142 static void vl_api_create_loopback_instance_t_handler
1143 (vl_api_create_loopback_instance_t * mp)
1145 vl_api_create_loopback_instance_reply_t *rmp;
1147 u8 is_specified = mp->is_specified;
1148 u32 user_instance = ntohl (mp->user_instance);
1151 rv = vnet_create_loopback_interface (&sw_if_index, mp->mac_address,
1152 is_specified, user_instance);
1155 REPLY_MACRO2(VL_API_CREATE_LOOPBACK_INSTANCE_REPLY,
1157 rmp->sw_if_index = ntohl (sw_if_index);
1163 vl_api_delete_loopback_t_handler (vl_api_delete_loopback_t * mp)
1165 vl_api_delete_loopback_reply_t *rmp;
1169 sw_if_index = ntohl (mp->sw_if_index);
1170 rv = vnet_delete_loopback_interface (sw_if_index);
1172 REPLY_MACRO (VL_API_DELETE_LOOPBACK_REPLY);
1177 * Add vpe's API message handlers to the table.
1178 * vlib has alread mapped shared memory and
1179 * added the client registration handlers.
1180 * See .../vlib-api/vlibmemory/memclnt_vlib.c:memclnt_process()
1182 #define vl_msg_name_crc_list
1183 #include <vnet/interface.api.h>
1184 #undef vl_msg_name_crc_list
1187 setup_message_id_table (api_main_t * am)
1189 #define _(id,n,crc) vl_msg_api_add_msg_name_crc (am, #n "_" #crc, id);
1190 foreach_vl_msg_name_crc_interface;
1194 pub_sub_handler (interface_events, INTERFACE_EVENTS);
1196 static clib_error_t *
1197 interface_api_hookup (vlib_main_t * vm)
1199 api_main_t *am = &api_main;
1202 vl_msg_api_set_handlers(VL_API_##N, #n, \
1203 vl_api_##n##_t_handler, \
1205 vl_api_##n##_t_endian, \
1206 vl_api_##n##_t_print, \
1207 sizeof(vl_api_##n##_t), 1);
1208 foreach_vpe_api_msg;
1212 * Set up the (msg_name, crc, message-id) table
1214 setup_message_id_table (am);
1219 VLIB_API_INIT_FUNCTION (interface_api_hookup);
1222 * fd.io coding-style-patch-verification: ON
1225 * eval: (c-set-style "gnu")