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 _(HW_INTERFACE_SET_MTU, hw_interface_set_mtu) \
54 _(SW_INTERFACE_SET_MTU, sw_interface_set_mtu) \
55 _(WANT_INTERFACE_EVENTS, want_interface_events) \
56 _(SW_INTERFACE_DUMP, sw_interface_dump) \
57 _(SW_INTERFACE_ADD_DEL_ADDRESS, sw_interface_add_del_address) \
58 _(SW_INTERFACE_SET_RX_MODE, sw_interface_set_rx_mode) \
59 _(SW_INTERFACE_SET_TABLE, sw_interface_set_table) \
60 _(SW_INTERFACE_GET_TABLE, sw_interface_get_table) \
61 _(SW_INTERFACE_SET_UNNUMBERED, sw_interface_set_unnumbered) \
62 _(SW_INTERFACE_CLEAR_STATS, sw_interface_clear_stats) \
63 _(SW_INTERFACE_TAG_ADD_DEL, sw_interface_tag_add_del) \
64 _(SW_INTERFACE_SET_MAC_ADDRESS, sw_interface_set_mac_address) \
65 _(SW_INTERFACE_GET_MAC_ADDRESS, sw_interface_get_mac_address) \
66 _(CREATE_VLAN_SUBIF, create_vlan_subif) \
67 _(CREATE_SUBIF, create_subif) \
68 _(DELETE_SUBIF, delete_subif) \
69 _(CREATE_LOOPBACK, create_loopback) \
70 _(CREATE_LOOPBACK_INSTANCE, create_loopback_instance) \
71 _(DELETE_LOOPBACK, delete_loopback) \
72 _(INTERFACE_NAME_RENUMBER, interface_name_renumber) \
73 _(COLLECT_DETAILED_INTERFACE_STATS, collect_detailed_interface_stats) \
74 _(SW_INTERFACE_SET_IP_DIRECTED_BROADCAST, \
75 sw_interface_set_ip_directed_broadcast)
78 vl_api_sw_interface_set_flags_t_handler (vl_api_sw_interface_set_flags_t * mp)
80 vl_api_sw_interface_set_flags_reply_t *rmp;
81 vnet_main_t *vnm = vnet_get_main ();
86 VALIDATE_SW_IF_INDEX (mp);
88 flags = mp->admin_up_down ? VNET_SW_INTERFACE_FLAG_ADMIN_UP : 0;
90 error = vnet_sw_interface_set_flags (vnm, ntohl (mp->sw_if_index), flags);
94 clib_error_report (error);
97 BAD_SW_IF_INDEX_LABEL;
98 REPLY_MACRO (VL_API_SW_INTERFACE_SET_FLAGS_REPLY);
102 vl_api_hw_interface_set_mtu_t_handler (vl_api_hw_interface_set_mtu_t * mp)
104 vl_api_hw_interface_set_mtu_reply_t *rmp;
105 vnet_main_t *vnm = vnet_get_main ();
106 u32 sw_if_index = ntohl (mp->sw_if_index);
107 u16 mtu = ntohs (mp->mtu);
108 ethernet_main_t *em = ðernet_main;
111 VALIDATE_SW_IF_INDEX (mp);
113 vnet_sw_interface_t *si = vnet_get_sw_interface (vnm, sw_if_index);
114 if (si->type != VNET_SW_INTERFACE_TYPE_HARDWARE)
116 rv = VNET_API_ERROR_INVALID_VALUE;
117 goto bad_sw_if_index;
120 vnet_hw_interface_t *hi = vnet_get_hw_interface (vnm, si->hw_if_index);
121 ethernet_interface_t *eif = ethernet_get_interface (em, si->hw_if_index);
125 rv = VNET_API_ERROR_FEATURE_DISABLED;
126 goto bad_sw_if_index;
129 if (mtu < hi->min_supported_packet_bytes)
131 rv = VNET_API_ERROR_INVALID_VALUE;
132 goto bad_sw_if_index;
135 if (mtu > hi->max_supported_packet_bytes)
137 rv = VNET_API_ERROR_INVALID_VALUE;
138 goto bad_sw_if_index;
141 vnet_hw_interface_set_mtu (vnm, si->hw_if_index, mtu);
143 BAD_SW_IF_INDEX_LABEL;
144 REPLY_MACRO (VL_API_HW_INTERFACE_SET_MTU_REPLY);
148 vl_api_sw_interface_set_mtu_t_handler (vl_api_sw_interface_set_mtu_t * mp)
150 vl_api_sw_interface_set_mtu_reply_t *rmp;
151 vnet_main_t *vnm = vnet_get_main ();
152 u32 sw_if_index = ntohl (mp->sw_if_index);
155 u32 per_protocol_mtu[VNET_N_MTU];
157 VALIDATE_SW_IF_INDEX (mp);
159 for (i = 0; i < VNET_N_MTU; i++)
160 per_protocol_mtu[i] = ntohl (mp->mtu[i]);
162 vnet_sw_interface_set_protocol_mtu (vnm, sw_if_index, per_protocol_mtu);
164 BAD_SW_IF_INDEX_LABEL;
165 REPLY_MACRO (VL_API_SW_INTERFACE_SET_MTU_REPLY);
169 vl_api_sw_interface_set_ip_directed_broadcast_t_handler
170 (vl_api_sw_interface_set_ip_directed_broadcast_t * mp)
172 vl_api_sw_interface_set_ip_directed_broadcast_reply_t *rmp;
173 u32 sw_if_index = ntohl (mp->sw_if_index);
176 VALIDATE_SW_IF_INDEX (mp);
178 vnet_sw_interface_ip_directed_broadcast (vnet_get_main (),
179 sw_if_index, mp->enable);
181 BAD_SW_IF_INDEX_LABEL;
182 REPLY_MACRO (VL_API_SW_INTERFACE_SET_IP_DIRECTED_BROADCAST_REPLY);
186 send_sw_interface_details (vpe_api_main_t * am,
187 vl_api_registration_t * rp,
188 vnet_sw_interface_t * swif,
189 u8 * interface_name, u32 context)
191 vnet_hw_interface_t *hi =
192 vnet_get_sup_hw_interface (am->vnet_main, swif->sw_if_index);
194 vl_api_sw_interface_details_t *mp = vl_msg_api_alloc (sizeof (*mp));
195 memset (mp, 0, sizeof (*mp));
196 mp->_vl_msg_id = ntohs (VL_API_SW_INTERFACE_DETAILS);
197 mp->sw_if_index = ntohl (swif->sw_if_index);
198 mp->sup_sw_if_index = ntohl (swif->sup_sw_if_index);
199 mp->admin_up_down = (swif->flags & VNET_SW_INTERFACE_FLAG_ADMIN_UP) ? 1 : 0;
200 mp->link_up_down = (hi->flags & VNET_HW_INTERFACE_FLAG_LINK_UP) ? 1 : 0;
201 mp->link_duplex = ((hi->flags & VNET_HW_INTERFACE_FLAG_DUPLEX_MASK) >>
202 VNET_HW_INTERFACE_FLAG_DUPLEX_SHIFT);
203 mp->link_speed = ((hi->flags & VNET_HW_INTERFACE_FLAG_SPEED_MASK) >>
204 VNET_HW_INTERFACE_FLAG_SPEED_SHIFT);
205 mp->link_mtu = ntohs (hi->max_packet_bytes);
206 mp->mtu[VNET_MTU_L3] = ntohl (swif->mtu[VNET_MTU_L3]);
207 mp->mtu[VNET_MTU_IP4] = ntohl (swif->mtu[VNET_MTU_IP4]);
208 mp->mtu[VNET_MTU_IP6] = ntohl (swif->mtu[VNET_MTU_IP6]);
209 mp->mtu[VNET_MTU_MPLS] = ntohl (swif->mtu[VNET_MTU_MPLS]);
211 mp->context = context;
213 strncpy ((char *) mp->interface_name,
214 (char *) interface_name, ARRAY_LEN (mp->interface_name) - 1);
216 /* Send the L2 address for ethernet physical intfcs */
217 if (swif->sup_sw_if_index == swif->sw_if_index
218 && hi->hw_class_index == ethernet_hw_interface_class.index)
220 ethernet_main_t *em = ethernet_get_main (am->vlib_main);
221 ethernet_interface_t *ei;
223 ei = pool_elt_at_index (em->interfaces, hi->hw_instance);
224 ASSERT (sizeof (mp->l2_address) >= sizeof (ei->address));
225 clib_memcpy (mp->l2_address, ei->address, sizeof (ei->address));
226 mp->l2_address_length = ntohl (sizeof (ei->address));
228 else if (swif->sup_sw_if_index != swif->sw_if_index)
230 vnet_sub_interface_t *sub = &swif->sub;
231 mp->sub_id = ntohl (sub->id);
232 mp->sub_dot1ad = sub->eth.flags.dot1ad;
233 mp->sub_number_of_tags =
234 sub->eth.flags.one_tag + sub->eth.flags.two_tags * 2;
235 mp->sub_outer_vlan_id = ntohs (sub->eth.outer_vlan_id);
236 mp->sub_inner_vlan_id = ntohs (sub->eth.inner_vlan_id);
237 mp->sub_exact_match = sub->eth.flags.exact_match;
238 mp->sub_default = sub->eth.flags.default_sub;
239 mp->sub_outer_vlan_id_any = sub->eth.flags.outer_vlan_id_any;
240 mp->sub_inner_vlan_id_any = sub->eth.flags.inner_vlan_id_any;
242 /* vlan tag rewrite data */
243 u32 vtr_op = L2_VTR_DISABLED;
244 u32 vtr_push_dot1q = 0, vtr_tag1 = 0, vtr_tag2 = 0;
246 if (l2vtr_get (am->vlib_main, am->vnet_main, swif->sw_if_index,
247 &vtr_op, &vtr_push_dot1q, &vtr_tag1, &vtr_tag2) != 0)
249 // error - default to disabled
250 mp->vtr_op = ntohl (L2_VTR_DISABLED);
251 clib_warning ("cannot get vlan tag rewrite for sw_if_index %d",
256 mp->vtr_op = ntohl (vtr_op);
257 mp->vtr_push_dot1q = ntohl (vtr_push_dot1q);
258 mp->vtr_tag1 = ntohl (vtr_tag1);
259 mp->vtr_tag2 = ntohl (vtr_tag2);
263 /* pbb tag rewrite data */
264 ethernet_header_t eth_hdr;
265 u32 vtr_op = L2_VTR_DISABLED;
269 memset (ð_hdr, 0, sizeof (eth_hdr));
271 if (!l2pbb_get (am->vlib_main, am->vnet_main, swif->sw_if_index,
272 &vtr_op, &outer_tag, ð_hdr, &b_vlanid, &i_sid))
275 clib_memcpy (mp->b_dmac, eth_hdr.dst_address,
276 sizeof (eth_hdr.dst_address));
277 clib_memcpy (mp->b_smac, eth_hdr.src_address,
278 sizeof (eth_hdr.src_address));
279 mp->b_vlanid = b_vlanid;
283 u8 *tag = vnet_get_sw_interface_tag (vnet_get_main (), swif->sw_if_index);
285 strncpy ((char *) mp->tag, (char *) tag, ARRAY_LEN (mp->tag) - 1);
287 vl_api_send_msg (rp, (u8 *) mp);
291 vl_api_sw_interface_dump_t_handler (vl_api_sw_interface_dump_t * mp)
293 vpe_api_main_t *am = &vpe_api_main;
294 vnet_sw_interface_t *swif;
295 vnet_interface_main_t *im = &am->vnet_main->interface_main;
296 vl_api_registration_t *rp;
298 rp = vl_api_client_index_to_registration (mp->client_index);
302 clib_warning ("Client %d AWOL", mp->client_index);
306 u8 *filter = 0, *name = 0;
307 if (mp->name_filter_valid)
309 mp->name_filter[ARRAY_LEN (mp->name_filter) - 1] = 0;
310 filter = format (0, "%s%c", mp->name_filter, 0);
313 char *strcasestr (char *, char *); /* lnx hdr file botch */
315 pool_foreach (swif, im->sw_interfaces,
317 if (!vnet_swif_is_api_visible (swif))
319 vec_reset_length(name);
320 name = format (name, "%U%c", format_vnet_sw_interface_name, am->vnet_main,
323 if (filter && !strcasestr((char *) name, (char *) filter))
326 send_sw_interface_details (am, rp, swif, name, mp->context);
335 vl_api_sw_interface_add_del_address_t_handler
336 (vl_api_sw_interface_add_del_address_t * mp)
338 vlib_main_t *vm = vlib_get_main ();
339 vnet_main_t *vnm = vnet_get_main ();
340 vl_api_sw_interface_add_del_address_reply_t *rmp;
343 clib_error_t *error = 0;
345 VALIDATE_SW_IF_INDEX (mp);
347 is_del = mp->is_add == 0;
351 ip_del_all_interface_addresses (vm, ntohl (mp->sw_if_index));
352 else if (mp->is_ipv6)
353 error = ip6_add_del_interface_address (vm, ntohl (mp->sw_if_index),
354 (void *) mp->address,
355 mp->address_length, is_del);
357 error = ip4_add_del_interface_address (vm, ntohl (mp->sw_if_index),
358 (void *) mp->address,
359 mp->address_length, is_del);
364 clib_error_report (error);
368 BAD_SW_IF_INDEX_LABEL;
371 REPLY_MACRO (VL_API_SW_INTERFACE_ADD_DEL_ADDRESS_REPLY);
374 void stats_dslock_with_hint (int hint, int tag) __attribute__ ((weak));
376 stats_dslock_with_hint (int hint, int tag)
380 void stats_dsunlock (void) __attribute__ ((weak));
382 stats_dsunlock (void)
387 vl_api_sw_interface_set_table_t_handler (vl_api_sw_interface_set_table_t * mp)
389 vl_api_sw_interface_set_table_reply_t *rmp;
390 u32 sw_if_index = ntohl (mp->sw_if_index);
391 u32 table_id = ntohl (mp->vrf_id);
394 VALIDATE_SW_IF_INDEX (mp);
396 stats_dslock_with_hint (1 /* release hint */ , 4 /* tag */ );
399 rv = ip_table_bind (FIB_PROTOCOL_IP6, sw_if_index, table_id, 1);
401 rv = ip_table_bind (FIB_PROTOCOL_IP4, sw_if_index, table_id, 1);
405 BAD_SW_IF_INDEX_LABEL;
407 REPLY_MACRO (VL_API_SW_INTERFACE_SET_TABLE_REPLY);
411 ip_table_bind (fib_protocol_t fproto,
412 u32 sw_if_index, u32 table_id, u8 is_api)
414 CLIB_UNUSED (ip_interface_address_t * ia);
415 u32 fib_index, mfib_index;
421 src = FIB_SOURCE_API;
422 msrc = MFIB_SOURCE_API;
426 src = FIB_SOURCE_CLI;
427 msrc = MFIB_SOURCE_CLI;
431 * This if table does not exist = error is what we want in the end.
433 fib_index = fib_table_find (fproto, table_id);
434 mfib_index = mfib_table_find (fproto, table_id);
436 if (~0 == fib_index || ~0 == mfib_index)
438 return (VNET_API_ERROR_NO_SUCH_FIB);
441 if (FIB_PROTOCOL_IP6 == fproto)
444 * If the interface already has in IP address, then a change int
445 * VRF is not allowed. The IP address applied must first be removed.
446 * We do not do that automatically here, since VPP has no knowledge
447 * of whether thoses subnets are valid in the destination VRF.
450 foreach_ip_interface_address (&ip6_main.lookup_main,
452 1 /* honor unnumbered */ ,
454 return (VNET_API_ERROR_ADDRESS_FOUND_FOR_INTERFACE);
458 vec_validate (ip6_main.fib_index_by_sw_if_index, sw_if_index);
459 vec_validate (ip6_main.mfib_index_by_sw_if_index, sw_if_index);
462 * tell those that are interested that the binding is changing.
464 ip6_table_bind_callback_t *cb;
465 vec_foreach (cb, ip6_main.table_bind_callbacks)
466 cb->function (&ip6_main, cb->function_opaque,
469 ip6_main.fib_index_by_sw_if_index[sw_if_index]);
473 /* reset back to default */
474 if (0 != ip6_main.fib_index_by_sw_if_index[sw_if_index])
475 fib_table_unlock (ip6_main.fib_index_by_sw_if_index[sw_if_index],
476 FIB_PROTOCOL_IP6, src);
477 if (0 != ip6_main.mfib_index_by_sw_if_index[sw_if_index])
478 mfib_table_unlock (ip6_main.mfib_index_by_sw_if_index
479 [sw_if_index], FIB_PROTOCOL_IP6, msrc);
484 /* we need to lock the table now it's inuse */
485 fib_table_lock (fib_index, FIB_PROTOCOL_IP6, src);
486 mfib_table_lock (mfib_index, FIB_PROTOCOL_IP6, msrc);
489 ip6_main.fib_index_by_sw_if_index[sw_if_index] = fib_index;
490 ip6_main.mfib_index_by_sw_if_index[sw_if_index] = mfib_index;
495 * If the interface already has in IP address, then a change int
496 * VRF is not allowed. The IP address applied must first be removed.
497 * We do not do that automatically here, since VPP has no knowledge
498 * of whether thoses subnets are valid in the destination VRF.
501 foreach_ip_interface_address (&ip4_main.lookup_main,
503 1 /* honor unnumbered */ ,
505 return (VNET_API_ERROR_ADDRESS_FOUND_FOR_INTERFACE);
509 vec_validate (ip4_main.fib_index_by_sw_if_index, sw_if_index);
510 vec_validate (ip4_main.mfib_index_by_sw_if_index, sw_if_index);
513 * tell those that are interested that the binding is changing.
515 ip4_table_bind_callback_t *cb;
516 vec_foreach (cb, ip4_main.table_bind_callbacks)
517 cb->function (&ip4_main, cb->function_opaque,
520 ip4_main.fib_index_by_sw_if_index[sw_if_index]);
524 /* reset back to default */
525 if (0 != ip4_main.fib_index_by_sw_if_index[sw_if_index])
526 fib_table_unlock (ip4_main.fib_index_by_sw_if_index[sw_if_index],
527 FIB_PROTOCOL_IP4, src);
528 if (0 != ip4_main.mfib_index_by_sw_if_index[sw_if_index])
529 mfib_table_unlock (ip4_main.mfib_index_by_sw_if_index
530 [sw_if_index], FIB_PROTOCOL_IP4, msrc);
535 /* we need to lock the table now it's inuse */
536 fib_index = fib_table_find_or_create_and_lock (FIB_PROTOCOL_IP4,
539 mfib_index = mfib_table_find_or_create_and_lock (FIB_PROTOCOL_IP4,
543 ip4_main.fib_index_by_sw_if_index[sw_if_index] = fib_index;
544 ip4_main.mfib_index_by_sw_if_index[sw_if_index] = mfib_index;
551 send_sw_interface_get_table_reply (vl_api_registration_t * reg,
552 u32 context, int retval, u32 vrf_id)
554 vl_api_sw_interface_get_table_reply_t *mp;
556 mp = vl_msg_api_alloc (sizeof (*mp));
557 memset (mp, 0, sizeof (*mp));
558 mp->_vl_msg_id = ntohs (VL_API_SW_INTERFACE_GET_TABLE_REPLY);
559 mp->context = context;
560 mp->retval = htonl (retval);
561 mp->vrf_id = htonl (vrf_id);
563 vl_api_send_msg (reg, (u8 *) mp);
567 vl_api_sw_interface_get_table_t_handler (vl_api_sw_interface_get_table_t * mp)
569 vl_api_registration_t *reg;
570 fib_table_t *fib_table = 0;
571 u32 sw_if_index = ~0;
574 fib_protocol_t fib_proto = FIB_PROTOCOL_IP4;
577 reg = vl_api_client_index_to_registration (mp->client_index);
581 VALIDATE_SW_IF_INDEX (mp);
583 sw_if_index = ntohl (mp->sw_if_index);
586 fib_proto = FIB_PROTOCOL_IP6;
588 fib_index = fib_table_get_index_for_sw_if_index (fib_proto, sw_if_index);
591 fib_table = fib_table_get (fib_index, fib_proto);
592 table_id = fib_table->ft_table_id;
595 BAD_SW_IF_INDEX_LABEL;
597 send_sw_interface_get_table_reply (reg, mp->context, rv, table_id);
600 static void vl_api_sw_interface_set_unnumbered_t_handler
601 (vl_api_sw_interface_set_unnumbered_t * mp)
603 vl_api_sw_interface_set_unnumbered_reply_t *rmp;
605 vnet_main_t *vnm = vnet_get_main ();
606 u32 sw_if_index = ntohl (mp->sw_if_index);
607 u32 unnumbered_sw_if_index = ntohl (mp->unnumbered_sw_if_index);
610 * The API message field names are backwards from
611 * the underlying data structure names.
612 * It's not worth changing them now.
614 if (!vnet_sw_interface_is_api_valid (vnm, unnumbered_sw_if_index))
616 rv = VNET_API_ERROR_INVALID_SW_IF_INDEX;
620 /* Only check the "use loop0" field when setting the binding */
621 if (mp->is_add && !vnet_sw_interface_is_api_valid (vnm, sw_if_index))
623 rv = VNET_API_ERROR_INVALID_SW_IF_INDEX_2;
627 vnet_sw_interface_update_unnumbered (unnumbered_sw_if_index,
628 sw_if_index, mp->is_add);
630 REPLY_MACRO (VL_API_SW_INTERFACE_SET_UNNUMBERED_REPLY);
634 vl_api_sw_interface_clear_stats_t_handler (vl_api_sw_interface_clear_stats_t *
637 vl_api_sw_interface_clear_stats_reply_t *rmp;
639 vnet_main_t *vnm = vnet_get_main ();
640 vnet_interface_main_t *im = &vnm->interface_main;
641 vlib_simple_counter_main_t *sm;
642 vlib_combined_counter_main_t *cm;
643 static vnet_main_t **my_vnet_mains;
644 int i, j, n_counters;
647 if (mp->sw_if_index != ~0)
648 VALIDATE_SW_IF_INDEX (mp);
650 vec_reset_length (my_vnet_mains);
652 for (i = 0; i < vec_len (vnet_mains); i++)
655 vec_add1 (my_vnet_mains, vnet_mains[i]);
658 if (vec_len (vnet_mains) == 0)
659 vec_add1 (my_vnet_mains, vnm);
661 n_counters = vec_len (im->combined_sw_if_counters);
663 for (j = 0; j < n_counters; j++)
665 for (i = 0; i < vec_len (my_vnet_mains); i++)
667 im = &my_vnet_mains[i]->interface_main;
668 cm = im->combined_sw_if_counters + j;
669 if (mp->sw_if_index == (u32) ~ 0)
670 vlib_clear_combined_counters (cm);
672 vlib_zero_combined_counter (cm, ntohl (mp->sw_if_index));
676 n_counters = vec_len (im->sw_if_counters);
678 for (j = 0; j < n_counters; j++)
680 for (i = 0; i < vec_len (my_vnet_mains); i++)
682 im = &my_vnet_mains[i]->interface_main;
683 sm = im->sw_if_counters + j;
684 if (mp->sw_if_index == (u32) ~ 0)
685 vlib_clear_simple_counters (sm);
687 vlib_zero_simple_counter (sm, ntohl (mp->sw_if_index));
691 BAD_SW_IF_INDEX_LABEL;
693 REPLY_MACRO (VL_API_SW_INTERFACE_CLEAR_STATS_REPLY);
696 #define API_LINK_STATE_EVENT 1
697 #define API_ADMIN_UP_DOWN_EVENT 2
700 event_data_cmp (void *a1, void *a2)
705 return (word) e1[0] - (word) e2[0];
709 send_sw_interface_event (vpe_api_main_t * am,
710 vpe_client_registration_t * reg,
711 vl_api_registration_t * vl_reg,
712 vnet_sw_interface_t * swif)
714 vl_api_sw_interface_event_t *mp;
715 vnet_main_t *vnm = am->vnet_main;
717 vnet_hw_interface_t *hi = vnet_get_sup_hw_interface (vnm,
719 mp = vl_msg_api_alloc (sizeof (*mp));
720 memset (mp, 0, sizeof (*mp));
721 mp->_vl_msg_id = ntohs (VL_API_SW_INTERFACE_EVENT);
722 mp->sw_if_index = ntohl (swif->sw_if_index);
723 mp->client_index = reg->client_index;
724 mp->pid = reg->client_pid;
726 mp->admin_up_down = (swif->flags & VNET_SW_INTERFACE_FLAG_ADMIN_UP) ? 1 : 0;
727 mp->link_up_down = (hi->flags & VNET_HW_INTERFACE_FLAG_LINK_UP) ? 1 : 0;
728 vl_api_send_msg (vl_reg, (u8 *) mp);
732 link_state_process (vlib_main_t * vm,
733 vlib_node_runtime_t * rt, vlib_frame_t * f)
735 vpe_api_main_t *vam = &vpe_api_main;
736 vnet_main_t *vnm = vam->vnet_main;
737 vnet_sw_interface_t *swif;
738 uword *event_data = 0;
739 vpe_client_registration_t *reg;
741 u32 prev_sw_if_index;
742 vl_api_registration_t *vl_reg;
744 vam->link_state_process_up = 1;
748 vlib_process_wait_for_event (vm);
750 /* Unified list of changed link or admin state sw_if_indices */
751 vlib_process_get_events_with_type
752 (vm, &event_data, API_LINK_STATE_EVENT);
753 vlib_process_get_events_with_type
754 (vm, &event_data, API_ADMIN_UP_DOWN_EVENT);
756 /* Sort, so we can eliminate duplicates */
757 vec_sort_with_function (event_data, event_data_cmp);
759 prev_sw_if_index = ~0;
761 for (i = 0; i < vec_len (event_data); i++)
763 /* Only one message per swif */
764 if (prev_sw_if_index == event_data[i])
766 prev_sw_if_index = event_data[i];
769 pool_foreach(reg, vam->interface_events_registrations,
771 vl_reg = vl_api_client_index_to_registration (reg->client_index);
774 /* sw_interface may be deleted already */
775 if (!pool_is_free_index (vnm->interface_main.sw_interfaces,
778 swif = vnet_get_sw_interface (vnm, event_data[i]);
779 send_sw_interface_event (vam, reg, vl_reg, swif);
785 vec_reset_length (event_data);
791 static clib_error_t *link_up_down_function (vnet_main_t * vm, u32 hw_if_index,
793 static clib_error_t *admin_up_down_function (vnet_main_t * vm,
794 u32 hw_if_index, u32 flags);
797 VLIB_REGISTER_NODE (link_state_process_node,static) = {
798 .function = link_state_process,
799 .type = VLIB_NODE_TYPE_PROCESS,
800 .name = "vpe-link-state-process",
804 VNET_SW_INTERFACE_ADMIN_UP_DOWN_FUNCTION (admin_up_down_function);
805 VNET_HW_INTERFACE_LINK_UP_DOWN_FUNCTION (link_up_down_function);
807 static clib_error_t *
808 link_up_down_function (vnet_main_t * vm, u32 hw_if_index, u32 flags)
810 vpe_api_main_t *vam = &vpe_api_main;
811 vnet_hw_interface_t *hi = vnet_get_hw_interface (vm, hw_if_index);
813 if (vam->link_state_process_up)
814 vlib_process_signal_event (vam->vlib_main,
815 link_state_process_node.index,
816 API_LINK_STATE_EVENT, hi->sw_if_index);
820 static clib_error_t *
821 admin_up_down_function (vnet_main_t * vm, u32 sw_if_index, u32 flags)
823 vpe_api_main_t *vam = &vpe_api_main;
826 * Note: it's perfectly fair to set a subif admin up / admin down.
827 * Note the subtle distinction between this routine and the previous
830 if (vam->link_state_process_up)
831 vlib_process_signal_event (vam->vlib_main,
832 link_state_process_node.index,
833 API_ADMIN_UP_DOWN_EVENT, sw_if_index);
837 static void vl_api_sw_interface_tag_add_del_t_handler
838 (vl_api_sw_interface_tag_add_del_t * mp)
840 vnet_main_t *vnm = vnet_get_main ();
841 vl_api_sw_interface_tag_add_del_reply_t *rmp;
844 u32 sw_if_index = ntohl (mp->sw_if_index);
846 VALIDATE_SW_IF_INDEX (mp);
852 rv = VNET_API_ERROR_INVALID_VALUE;
856 mp->tag[ARRAY_LEN (mp->tag) - 1] = 0;
857 tag = format (0, "%s%c", mp->tag, 0);
858 vnet_set_sw_interface_tag (vnm, tag, sw_if_index);
861 vnet_clear_sw_interface_tag (vnm, sw_if_index);
863 BAD_SW_IF_INDEX_LABEL;
865 REPLY_MACRO (VL_API_SW_INTERFACE_TAG_ADD_DEL_REPLY);
868 static void vl_api_sw_interface_set_mac_address_t_handler
869 (vl_api_sw_interface_set_mac_address_t * mp)
871 vl_api_sw_interface_set_mac_address_reply_t *rmp;
872 vnet_main_t *vnm = vnet_get_main ();
873 u32 sw_if_index = ntohl (mp->sw_if_index);
874 vnet_sw_interface_t *si;
878 VALIDATE_SW_IF_INDEX (mp);
880 si = vnet_get_sw_interface (vnm, sw_if_index);
881 error = vnet_hw_interface_change_mac_address (vnm, si->hw_if_index,
885 rv = VNET_API_ERROR_UNIMPLEMENTED;
886 clib_error_report (error);
890 BAD_SW_IF_INDEX_LABEL;
892 REPLY_MACRO (VL_API_SW_INTERFACE_SET_MAC_ADDRESS_REPLY);
895 static void vl_api_sw_interface_get_mac_address_t_handler
896 (vl_api_sw_interface_get_mac_address_t * mp)
898 vl_api_sw_interface_get_mac_address_reply_t *rmp;
899 vl_api_registration_t *reg;
900 vnet_main_t *vnm = vnet_get_main ();
901 u32 sw_if_index = ntohl (mp->sw_if_index);
902 vnet_sw_interface_t *si;
903 ethernet_interface_t *eth_if = 0;
906 VALIDATE_SW_IF_INDEX (mp);
908 si = vnet_get_sup_sw_interface (vnm, sw_if_index);
909 if (si->type == VNET_SW_INTERFACE_TYPE_HARDWARE)
910 eth_if = ethernet_get_interface (ðernet_main, si->hw_if_index);
912 BAD_SW_IF_INDEX_LABEL;
914 reg = vl_api_client_index_to_registration (mp->client_index);
917 rmp = vl_msg_api_alloc (sizeof (*rmp));
918 rmp->_vl_msg_id = htons (VL_API_SW_INTERFACE_GET_MAC_ADDRESS_REPLY);
919 rmp->context = mp->context;
920 rmp->retval = htonl (rv);
922 memcpy (rmp->mac_address, eth_if->address, 6);
923 vl_api_send_msg (reg, (u8 *) rmp);
926 static void vl_api_sw_interface_set_rx_mode_t_handler
927 (vl_api_sw_interface_set_rx_mode_t * mp)
929 vl_api_sw_interface_set_rx_mode_reply_t *rmp;
930 vnet_main_t *vnm = vnet_get_main ();
931 u32 sw_if_index = ntohl (mp->sw_if_index);
932 vnet_sw_interface_t *si;
936 VALIDATE_SW_IF_INDEX (mp);
938 si = vnet_get_sw_interface (vnm, sw_if_index);
939 error = set_hw_interface_change_rx_mode (vnm, si->hw_if_index,
941 ntohl (mp->queue_id), mp->mode);
944 rv = VNET_API_ERROR_UNIMPLEMENTED;
945 clib_error_report (error);
949 BAD_SW_IF_INDEX_LABEL;
951 REPLY_MACRO (VL_API_SW_INTERFACE_SET_RX_MODE_REPLY);
955 vl_api_create_vlan_subif_t_handler (vl_api_create_vlan_subif_t * mp)
957 vl_api_create_vlan_subif_reply_t *rmp;
958 vnet_main_t *vnm = vnet_get_main ();
959 u32 sw_if_index = (u32) ~ 0;
960 vnet_hw_interface_t *hi;
963 vnet_sw_interface_t template;
965 vnet_interface_main_t *im = &vnm->interface_main;
967 vl_api_registration_t *reg;
970 VALIDATE_SW_IF_INDEX (mp);
972 hi = vnet_get_sup_hw_interface (vnm, ntohl (mp->sw_if_index));
974 if (hi->bond_info == VNET_HW_INTERFACE_BOND_INFO_SLAVE)
976 rv = VNET_API_ERROR_BOND_SLAVE_NOT_ALLOWED;
980 id = ntohl (mp->vlan_id);
981 if (id == 0 || id > 4095)
983 rv = VNET_API_ERROR_INVALID_VLAN;
987 sup_and_sub_key = ((u64) (hi->sw_if_index) << 32) | (u64) id;
989 p = hash_get_mem (im->sw_if_index_by_sup_and_sub, &sup_and_sub_key);
992 rv = VNET_API_ERROR_VLAN_ALREADY_EXISTS;
996 memset (&template, 0, sizeof (template));
997 template.type = VNET_SW_INTERFACE_TYPE_SUB;
998 template.sup_sw_if_index = hi->sw_if_index;
999 template.sub.id = id;
1000 template.sub.eth.raw_flags = 0;
1001 template.sub.eth.flags.one_tag = 1;
1002 template.sub.eth.outer_vlan_id = id;
1003 template.sub.eth.flags.exact_match = 1;
1005 error = vnet_create_sw_interface (vnm, &template, &sw_if_index);
1008 clib_error_report (error);
1009 rv = VNET_API_ERROR_INVALID_REGISTRATION;
1013 u64 *kp = clib_mem_alloc (sizeof (*kp));
1014 *kp = sup_and_sub_key;
1016 hash_set (hi->sub_interface_sw_if_index_by_id, id, sw_if_index);
1017 hash_set_mem (im->sw_if_index_by_sup_and_sub, kp, sw_if_index);
1019 BAD_SW_IF_INDEX_LABEL;
1022 reg = vl_api_client_index_to_registration (mp->client_index);
1026 rmp = vl_msg_api_alloc (sizeof (*rmp));
1027 rmp->_vl_msg_id = htons (VL_API_CREATE_VLAN_SUBIF_REPLY);
1028 rmp->context = mp->context;
1029 rmp->retval = htonl (rv);
1030 rmp->sw_if_index = htonl (sw_if_index);
1031 vl_api_send_msg (reg, (u8 *) rmp);
1035 vl_api_create_subif_t_handler (vl_api_create_subif_t * mp)
1037 vl_api_create_subif_reply_t *rmp;
1038 vnet_main_t *vnm = vnet_get_main ();
1039 u32 sw_if_index = ~0;
1042 vnet_sw_interface_t *si;
1043 vnet_hw_interface_t *hi;
1044 vnet_sw_interface_t template;
1046 vnet_interface_main_t *im = &vnm->interface_main;
1047 u64 sup_and_sub_key;
1048 clib_error_t *error;
1050 VALIDATE_SW_IF_INDEX (mp);
1052 si = vnet_get_sup_sw_interface (vnm, ntohl (mp->sw_if_index));
1053 hi = vnet_get_sup_hw_interface (vnm, ntohl (mp->sw_if_index));
1055 if (hi->bond_info == VNET_HW_INTERFACE_BOND_INFO_SLAVE)
1057 rv = VNET_API_ERROR_BOND_SLAVE_NOT_ALLOWED;
1061 sw_if_index = si->sw_if_index;
1062 sub_id = ntohl (mp->sub_id);
1064 sup_and_sub_key = ((u64) (sw_if_index) << 32) | (u64) sub_id;
1066 p = hash_get_mem (im->sw_if_index_by_sup_and_sub, &sup_and_sub_key);
1070 clib_warning ("sup sw_if_index %d, sub id %d already exists\n",
1071 sw_if_index, sub_id);
1072 rv = VNET_API_ERROR_SUBIF_ALREADY_EXISTS;
1076 memset (&template, 0, sizeof (template));
1077 template.type = VNET_SW_INTERFACE_TYPE_SUB;
1078 template.sup_sw_if_index = sw_if_index;
1079 template.sub.id = sub_id;
1080 template.sub.eth.flags.no_tags = mp->no_tags;
1081 template.sub.eth.flags.one_tag = mp->one_tag;
1082 template.sub.eth.flags.two_tags = mp->two_tags;
1083 template.sub.eth.flags.dot1ad = mp->dot1ad;
1084 template.sub.eth.flags.exact_match = mp->exact_match;
1085 template.sub.eth.flags.default_sub = mp->default_sub;
1086 template.sub.eth.flags.outer_vlan_id_any = mp->outer_vlan_id_any;
1087 template.sub.eth.flags.inner_vlan_id_any = mp->inner_vlan_id_any;
1088 template.sub.eth.outer_vlan_id = ntohs (mp->outer_vlan_id);
1089 template.sub.eth.inner_vlan_id = ntohs (mp->inner_vlan_id);
1091 error = vnet_create_sw_interface (vnm, &template, &sw_if_index);
1094 clib_error_report (error);
1095 rv = VNET_API_ERROR_SUBIF_CREATE_FAILED;
1099 u64 *kp = clib_mem_alloc (sizeof (*kp));
1100 *kp = sup_and_sub_key;
1102 hash_set (hi->sub_interface_sw_if_index_by_id, sub_id, sw_if_index);
1103 hash_set_mem (im->sw_if_index_by_sup_and_sub, kp, sw_if_index);
1105 BAD_SW_IF_INDEX_LABEL;
1110 REPLY_MACRO2(VL_API_CREATE_SUBIF_REPLY,
1112 rmp->sw_if_index = ntohl(sw_if_index);
1118 vl_api_delete_subif_t_handler (vl_api_delete_subif_t * mp)
1120 vl_api_delete_subif_reply_t *rmp;
1123 rv = vnet_delete_sub_interface (ntohl (mp->sw_if_index));
1125 REPLY_MACRO (VL_API_DELETE_SUBIF_REPLY);
1129 vl_api_interface_name_renumber_t_handler (vl_api_interface_name_renumber_t *
1132 vl_api_interface_name_renumber_reply_t *rmp;
1135 VALIDATE_SW_IF_INDEX (mp);
1137 rv = vnet_interface_name_renumber
1138 (ntohl (mp->sw_if_index), ntohl (mp->new_show_dev_instance));
1140 BAD_SW_IF_INDEX_LABEL;
1142 REPLY_MACRO (VL_API_INTERFACE_NAME_RENUMBER_REPLY);
1146 vl_api_create_loopback_t_handler (vl_api_create_loopback_t * mp)
1148 vl_api_create_loopback_reply_t *rmp;
1152 rv = vnet_create_loopback_interface (&sw_if_index, mp->mac_address, 0, 0);
1155 REPLY_MACRO2(VL_API_CREATE_LOOPBACK_REPLY,
1157 rmp->sw_if_index = ntohl (sw_if_index);
1162 static void vl_api_create_loopback_instance_t_handler
1163 (vl_api_create_loopback_instance_t * mp)
1165 vl_api_create_loopback_instance_reply_t *rmp;
1167 u8 is_specified = mp->is_specified;
1168 u32 user_instance = ntohl (mp->user_instance);
1171 rv = vnet_create_loopback_interface (&sw_if_index, mp->mac_address,
1172 is_specified, user_instance);
1175 REPLY_MACRO2(VL_API_CREATE_LOOPBACK_INSTANCE_REPLY,
1177 rmp->sw_if_index = ntohl (sw_if_index);
1183 vl_api_delete_loopback_t_handler (vl_api_delete_loopback_t * mp)
1185 vl_api_delete_loopback_reply_t *rmp;
1189 sw_if_index = ntohl (mp->sw_if_index);
1190 rv = vnet_delete_loopback_interface (sw_if_index);
1192 REPLY_MACRO (VL_API_DELETE_LOOPBACK_REPLY);
1196 vl_api_collect_detailed_interface_stats_t_handler
1197 (vl_api_collect_detailed_interface_stats_t * mp)
1199 vl_api_collect_detailed_interface_stats_reply_t *rmp;
1203 vnet_sw_interface_stats_collect_enable_disable (ntohl (mp->sw_if_index),
1204 mp->enable_disable);
1206 REPLY_MACRO (VL_API_COLLECT_DETAILED_INTERFACE_STATS_REPLY);
1211 * Add vpe's API message handlers to the table.
1212 * vlib has alread mapped shared memory and
1213 * added the client registration handlers.
1214 * See .../vlib-api/vlibmemory/memclnt_vlib.c:memclnt_process()
1216 #define vl_msg_name_crc_list
1217 #include <vnet/interface.api.h>
1218 #undef vl_msg_name_crc_list
1221 setup_message_id_table (api_main_t * am)
1223 #define _(id,n,crc) vl_msg_api_add_msg_name_crc (am, #n "_" #crc, id);
1224 foreach_vl_msg_name_crc_interface;
1228 pub_sub_handler (interface_events, INTERFACE_EVENTS);
1230 static clib_error_t *
1231 interface_api_hookup (vlib_main_t * vm)
1233 api_main_t *am = &api_main;
1236 vl_msg_api_set_handlers(VL_API_##N, #n, \
1237 vl_api_##n##_t_handler, \
1239 vl_api_##n##_t_endian, \
1240 vl_api_##n##_t_print, \
1241 sizeof(vl_api_##n##_t), 1);
1242 foreach_vpe_api_msg;
1246 * Set up the (msg_name, crc, message-id) table
1248 setup_message_id_table (am);
1253 VLIB_API_INIT_FUNCTION (interface_api_hookup);
1256 * fd.io coding-style-patch-verification: ON
1259 * eval: (c-set-style "gnu")