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 #include <vlibapi/api_types.h>
36 #include <vnet/ip/ip_types_api.h>
37 #include <vnet/ethernet/ethernet_types_api.h>
39 #define vl_typedefs /* define message structures */
40 #include <vnet/vnet_all_api_h.h>
43 #define vl_endianfun /* define message structures */
44 #include <vnet/vnet_all_api_h.h>
47 /* instantiate all the print functions we know about */
48 #define vl_print(handle, ...) vlib_cli_output (handle, __VA_ARGS__)
50 #include <vnet/vnet_all_api_h.h>
53 #include <vlibapi/api_helper_macros.h>
54 vpe_api_main_t vpe_api_main;
56 #define foreach_vpe_api_msg \
57 _(SW_INTERFACE_SET_FLAGS, sw_interface_set_flags) \
58 _(HW_INTERFACE_SET_MTU, hw_interface_set_mtu) \
59 _(SW_INTERFACE_SET_MTU, sw_interface_set_mtu) \
60 _(WANT_INTERFACE_EVENTS, want_interface_events) \
61 _(SW_INTERFACE_DUMP, sw_interface_dump) \
62 _(SW_INTERFACE_ADD_DEL_ADDRESS, sw_interface_add_del_address) \
63 _(SW_INTERFACE_SET_RX_MODE, sw_interface_set_rx_mode) \
64 _(SW_INTERFACE_RX_PLACEMENT_DUMP, sw_interface_rx_placement_dump) \
65 _(SW_INTERFACE_SET_RX_PLACEMENT, sw_interface_set_rx_placement) \
66 _(SW_INTERFACE_SET_TABLE, sw_interface_set_table) \
67 _(SW_INTERFACE_GET_TABLE, sw_interface_get_table) \
68 _(SW_INTERFACE_SET_UNNUMBERED, sw_interface_set_unnumbered) \
69 _(SW_INTERFACE_CLEAR_STATS, sw_interface_clear_stats) \
70 _(SW_INTERFACE_TAG_ADD_DEL, sw_interface_tag_add_del) \
71 _(SW_INTERFACE_SET_MAC_ADDRESS, sw_interface_set_mac_address) \
72 _(SW_INTERFACE_GET_MAC_ADDRESS, sw_interface_get_mac_address) \
73 _(CREATE_VLAN_SUBIF, create_vlan_subif) \
74 _(CREATE_SUBIF, create_subif) \
75 _(DELETE_SUBIF, delete_subif) \
76 _(CREATE_LOOPBACK, create_loopback) \
77 _(CREATE_LOOPBACK_INSTANCE, create_loopback_instance) \
78 _(DELETE_LOOPBACK, delete_loopback) \
79 _(INTERFACE_NAME_RENUMBER, interface_name_renumber) \
80 _(COLLECT_DETAILED_INTERFACE_STATS, collect_detailed_interface_stats) \
81 _(SW_INTERFACE_SET_IP_DIRECTED_BROADCAST, \
82 sw_interface_set_ip_directed_broadcast)
85 vl_api_sw_interface_set_flags_t_handler (vl_api_sw_interface_set_flags_t * mp)
87 vl_api_sw_interface_set_flags_reply_t *rmp;
88 vnet_main_t *vnm = vnet_get_main ();
93 VALIDATE_SW_IF_INDEX (mp);
96 ((ntohl (mp->flags)) & IF_STATUS_API_FLAG_ADMIN_UP) ?
97 VNET_SW_INTERFACE_FLAG_ADMIN_UP : 0;
99 error = vnet_sw_interface_set_flags (vnm, ntohl (mp->sw_if_index), flags);
103 clib_error_report (error);
106 BAD_SW_IF_INDEX_LABEL;
107 REPLY_MACRO (VL_API_SW_INTERFACE_SET_FLAGS_REPLY);
111 vl_api_hw_interface_set_mtu_t_handler (vl_api_hw_interface_set_mtu_t * mp)
113 vl_api_hw_interface_set_mtu_reply_t *rmp;
114 vnet_main_t *vnm = vnet_get_main ();
115 u32 sw_if_index = ntohl (mp->sw_if_index);
116 u16 mtu = ntohs (mp->mtu);
117 ethernet_main_t *em = ðernet_main;
120 VALIDATE_SW_IF_INDEX (mp);
122 vnet_sw_interface_t *si = vnet_get_sw_interface (vnm, sw_if_index);
123 if (si->type != VNET_SW_INTERFACE_TYPE_HARDWARE)
125 rv = VNET_API_ERROR_INVALID_VALUE;
126 goto bad_sw_if_index;
129 vnet_hw_interface_t *hi = vnet_get_hw_interface (vnm, si->hw_if_index);
130 ethernet_interface_t *eif = ethernet_get_interface (em, si->hw_if_index);
134 rv = VNET_API_ERROR_FEATURE_DISABLED;
135 goto bad_sw_if_index;
138 if (mtu < hi->min_supported_packet_bytes)
140 rv = VNET_API_ERROR_INVALID_VALUE;
141 goto bad_sw_if_index;
144 if (mtu > hi->max_supported_packet_bytes)
146 rv = VNET_API_ERROR_INVALID_VALUE;
147 goto bad_sw_if_index;
150 vnet_hw_interface_set_mtu (vnm, si->hw_if_index, mtu);
152 BAD_SW_IF_INDEX_LABEL;
153 REPLY_MACRO (VL_API_HW_INTERFACE_SET_MTU_REPLY);
157 vl_api_sw_interface_set_mtu_t_handler (vl_api_sw_interface_set_mtu_t * mp)
159 vl_api_sw_interface_set_mtu_reply_t *rmp;
160 vnet_main_t *vnm = vnet_get_main ();
161 u32 sw_if_index = ntohl (mp->sw_if_index);
164 u32 per_protocol_mtu[VNET_N_MTU];
166 VALIDATE_SW_IF_INDEX (mp);
168 for (i = 0; i < VNET_N_MTU; i++)
169 per_protocol_mtu[i] = ntohl (mp->mtu[i]);
171 vnet_sw_interface_set_protocol_mtu (vnm, sw_if_index, per_protocol_mtu);
173 BAD_SW_IF_INDEX_LABEL;
174 REPLY_MACRO (VL_API_SW_INTERFACE_SET_MTU_REPLY);
178 vl_api_sw_interface_set_ip_directed_broadcast_t_handler
179 (vl_api_sw_interface_set_ip_directed_broadcast_t * mp)
181 vl_api_sw_interface_set_ip_directed_broadcast_reply_t *rmp;
182 u32 sw_if_index = ntohl (mp->sw_if_index);
185 VALIDATE_SW_IF_INDEX (mp);
187 vnet_sw_interface_ip_directed_broadcast (vnet_get_main (),
188 sw_if_index, mp->enable);
190 BAD_SW_IF_INDEX_LABEL;
191 REPLY_MACRO (VL_API_SW_INTERFACE_SET_IP_DIRECTED_BROADCAST_REPLY);
195 send_sw_interface_details (vpe_api_main_t * am,
196 vl_api_registration_t * rp,
197 vnet_sw_interface_t * swif,
198 u8 * interface_name, u32 context)
200 vnet_hw_interface_t *hi =
201 vnet_get_sup_hw_interface (am->vnet_main, swif->sw_if_index);
203 uint32_t if_name_len = strlen ((char *) interface_name);
204 u8 *tag = vnet_get_sw_interface_tag (vnet_get_main (), swif->sw_if_index);
205 uint32_t tag_len = 0;
207 tag_len = strlen ((char *) tag);
208 vl_api_sw_interface_details_t *mp =
209 vl_msg_api_alloc (sizeof (*mp) + if_name_len + tag_len);
210 clib_memset (mp, 0, sizeof (*mp) + if_name_len + tag_len);
211 mp->_vl_msg_id = ntohs (VL_API_SW_INTERFACE_DETAILS);
212 mp->sw_if_index = ntohl (swif->sw_if_index);
213 mp->sup_sw_if_index = ntohl (swif->sup_sw_if_index);
215 mp->flags |= (swif->flags & VNET_SW_INTERFACE_FLAG_ADMIN_UP) ?
216 IF_STATUS_API_FLAG_ADMIN_UP : 0;
217 mp->flags |= (hi->flags & VNET_HW_INTERFACE_FLAG_LINK_UP) ?
218 IF_STATUS_API_FLAG_LINK_UP : 0;
219 mp->flags = ntohl (mp->flags);
221 mp->link_duplex = ((hi->flags & VNET_HW_INTERFACE_FLAG_DUPLEX_MASK) >>
222 VNET_HW_INTERFACE_FLAG_DUPLEX_SHIFT);
223 mp->link_speed = ntohl (hi->link_speed);
224 mp->link_mtu = ntohs (hi->max_packet_bytes);
225 mp->mtu[VNET_MTU_L3] = ntohl (swif->mtu[VNET_MTU_L3]);
226 mp->mtu[VNET_MTU_IP4] = ntohl (swif->mtu[VNET_MTU_IP4]);
227 mp->mtu[VNET_MTU_IP6] = ntohl (swif->mtu[VNET_MTU_IP6]);
228 mp->mtu[VNET_MTU_MPLS] = ntohl (swif->mtu[VNET_MTU_MPLS]);
230 mp->context = context;
232 /* Send the L2 address for ethernet physical intfcs */
233 if (swif->sup_sw_if_index == swif->sw_if_index
234 && hi->hw_class_index == ethernet_hw_interface_class.index)
236 ethernet_main_t *em = ethernet_get_main (am->vlib_main);
237 ethernet_interface_t *ei;
239 ei = pool_elt_at_index (em->interfaces, hi->hw_instance);
240 ASSERT (sizeof (mp->l2_address) >= sizeof (ei->address));
241 mac_address_encode ((mac_address_t *) ei->address, mp->l2_address);
243 else if (swif->sup_sw_if_index != swif->sw_if_index)
245 vnet_sub_interface_t *sub = &swif->sub;
246 mp->sub_id = ntohl (sub->id);
247 mp->sub_number_of_tags =
248 sub->eth.flags.one_tag + sub->eth.flags.two_tags * 2;
249 mp->sub_outer_vlan_id = ntohs (sub->eth.outer_vlan_id);
250 mp->sub_inner_vlan_id = ntohs (sub->eth.inner_vlan_id);
252 ntohl (sub->eth.raw_flags & SUB_IF_API_FLAG_MASK_VNET);
254 /* vlan tag rewrite data */
255 u32 vtr_op = L2_VTR_DISABLED;
256 u32 vtr_push_dot1q = 0, vtr_tag1 = 0, vtr_tag2 = 0;
258 if (l2vtr_get (am->vlib_main, am->vnet_main, swif->sw_if_index,
259 &vtr_op, &vtr_push_dot1q, &vtr_tag1, &vtr_tag2) != 0)
261 // error - default to disabled
262 mp->vtr_op = ntohl (L2_VTR_DISABLED);
263 clib_warning ("cannot get vlan tag rewrite for sw_if_index %d",
268 mp->vtr_op = ntohl (vtr_op);
269 mp->vtr_push_dot1q = ntohl (vtr_push_dot1q);
270 mp->vtr_tag1 = ntohl (vtr_tag1);
271 mp->vtr_tag2 = ntohl (vtr_tag2);
275 /* pbb tag rewrite data */
276 ethernet_header_t eth_hdr;
277 u32 vtr_op = L2_VTR_DISABLED;
281 clib_memset (ð_hdr, 0, sizeof (eth_hdr));
283 if (!l2pbb_get (am->vlib_main, am->vnet_main, swif->sw_if_index,
284 &vtr_op, &outer_tag, ð_hdr, &b_vlanid, &i_sid))
286 mp->sub_if_flags |= ntohl (SUB_IF_API_FLAG_DOT1AH);
287 mac_address_encode ((mac_address_t *) eth_hdr.dst_address, mp->b_dmac);
288 mac_address_encode ((mac_address_t *) eth_hdr.src_address, mp->b_smac);
289 mp->b_vlanid = b_vlanid;
293 char *p = (char *) &mp->interface_name;
295 vl_api_to_api_string (if_name_len, (char *) interface_name,
296 (vl_api_string_t *) p);
298 vl_api_to_api_string (tag_len, (char *) tag, (vl_api_string_t *) p);
300 vl_api_send_msg (rp, (u8 *) mp);
304 vl_api_sw_interface_dump_t_handler (vl_api_sw_interface_dump_t * mp)
306 vpe_api_main_t *am = &vpe_api_main;
307 vnet_sw_interface_t *swif;
308 vnet_interface_main_t *im = &am->vnet_main->interface_main;
309 vl_api_registration_t *rp;
311 rp = vl_api_client_index_to_registration (mp->client_index);
315 clib_warning ("Client %d AWOL", mp->client_index);
319 u8 *filter = 0, *name = 0;
320 if (mp->name_filter_valid)
323 format (0, "%s%c", vl_api_from_api_string_c (&mp->name_filter), 0);
326 char *strcasestr (char *, char *); /* lnx hdr file botch */
328 pool_foreach (swif, im->sw_interfaces,
330 if (!vnet_swif_is_api_visible (swif))
332 vec_reset_length(name);
333 name = format (name, "%U%c", format_vnet_sw_interface_name, am->vnet_main,
336 if (filter && !strcasestr((char *) name, (char *) filter))
339 send_sw_interface_details (am, rp, swif, name, mp->context);
348 vl_api_sw_interface_add_del_address_t_handler
349 (vl_api_sw_interface_add_del_address_t * mp)
351 vlib_main_t *vm = vlib_get_main ();
352 vnet_main_t *vnm = vnet_get_main ();
353 vl_api_sw_interface_add_del_address_reply_t *rmp;
356 clib_error_t *error = 0;
357 ip46_address_t address;
359 VALIDATE_SW_IF_INDEX (mp);
361 is_del = mp->is_add == 0;
365 ip_del_all_interface_addresses (vm, ntohl (mp->sw_if_index));
366 else if (ip_address_decode (&mp->prefix.address, &address) == IP46_TYPE_IP6)
367 error = ip6_add_del_interface_address (vm, ntohl (mp->sw_if_index),
368 (void *) &address.ip6,
369 mp->prefix.address_length, is_del);
371 error = ip4_add_del_interface_address (vm, ntohl (mp->sw_if_index),
372 (void *) &address.ip4,
373 mp->prefix.address_length, is_del);
378 clib_error_report (error);
382 BAD_SW_IF_INDEX_LABEL;
385 REPLY_MACRO (VL_API_SW_INTERFACE_ADD_DEL_ADDRESS_REPLY);
388 void stats_dslock_with_hint (int hint, int tag) __attribute__ ((weak));
390 stats_dslock_with_hint (int hint, int tag)
394 void stats_dsunlock (void) __attribute__ ((weak));
396 stats_dsunlock (void)
401 vl_api_sw_interface_set_table_t_handler (vl_api_sw_interface_set_table_t * mp)
403 vl_api_sw_interface_set_table_reply_t *rmp;
404 u32 sw_if_index = ntohl (mp->sw_if_index);
405 u32 table_id = ntohl (mp->vrf_id);
408 VALIDATE_SW_IF_INDEX (mp);
410 stats_dslock_with_hint (1 /* release hint */ , 4 /* tag */ );
413 rv = ip_table_bind (FIB_PROTOCOL_IP6, sw_if_index, table_id, 1);
415 rv = ip_table_bind (FIB_PROTOCOL_IP4, sw_if_index, table_id, 1);
419 BAD_SW_IF_INDEX_LABEL;
421 REPLY_MACRO (VL_API_SW_INTERFACE_SET_TABLE_REPLY);
425 ip_table_bind (fib_protocol_t fproto,
426 u32 sw_if_index, u32 table_id, u8 is_api)
428 CLIB_UNUSED (ip_interface_address_t * ia);
429 u32 fib_index, mfib_index;
435 src = FIB_SOURCE_API;
436 msrc = MFIB_SOURCE_API;
440 src = FIB_SOURCE_CLI;
441 msrc = MFIB_SOURCE_CLI;
445 * This if table does not exist = error is what we want in the end.
447 fib_index = fib_table_find (fproto, table_id);
448 mfib_index = mfib_table_find (fproto, table_id);
450 if (~0 == fib_index || ~0 == mfib_index)
452 return (VNET_API_ERROR_NO_SUCH_FIB);
455 if (FIB_PROTOCOL_IP6 == fproto)
458 * If the interface already has in IP address, then a change int
459 * VRF is not allowed. The IP address applied must first be removed.
460 * We do not do that automatically here, since VPP has no knowledge
461 * of whether those subnets are valid in the destination VRF.
464 foreach_ip_interface_address (&ip6_main.lookup_main,
466 1 /* honor unnumbered */ ,
468 return (VNET_API_ERROR_ADDRESS_FOUND_FOR_INTERFACE);
472 vec_validate (ip6_main.fib_index_by_sw_if_index, sw_if_index);
473 vec_validate (ip6_main.mfib_index_by_sw_if_index, sw_if_index);
476 * tell those that are interested that the binding is changing.
478 ip6_table_bind_callback_t *cb;
479 vec_foreach (cb, ip6_main.table_bind_callbacks)
480 cb->function (&ip6_main, cb->function_opaque,
483 ip6_main.fib_index_by_sw_if_index[sw_if_index]);
487 /* reset back to default */
488 if (0 != ip6_main.fib_index_by_sw_if_index[sw_if_index])
489 fib_table_unlock (ip6_main.fib_index_by_sw_if_index[sw_if_index],
490 FIB_PROTOCOL_IP6, src);
491 if (0 != ip6_main.mfib_index_by_sw_if_index[sw_if_index])
492 mfib_table_unlock (ip6_main.mfib_index_by_sw_if_index
493 [sw_if_index], FIB_PROTOCOL_IP6, msrc);
498 /* we need to lock the table now it's inuse */
499 fib_table_lock (fib_index, FIB_PROTOCOL_IP6, src);
500 mfib_table_lock (mfib_index, FIB_PROTOCOL_IP6, msrc);
503 ip6_main.fib_index_by_sw_if_index[sw_if_index] = fib_index;
504 ip6_main.mfib_index_by_sw_if_index[sw_if_index] = mfib_index;
509 * If the interface already has in IP address, then a change int
510 * VRF is not allowed. The IP address applied must first be removed.
511 * We do not do that automatically here, since VPP has no knowledge
512 * of whether those subnets are valid in the destination VRF.
515 foreach_ip_interface_address (&ip4_main.lookup_main,
517 1 /* honor unnumbered */ ,
519 return (VNET_API_ERROR_ADDRESS_FOUND_FOR_INTERFACE);
523 vec_validate (ip4_main.fib_index_by_sw_if_index, sw_if_index);
524 vec_validate (ip4_main.mfib_index_by_sw_if_index, sw_if_index);
527 * tell those that are interested that the binding is changing.
529 ip4_table_bind_callback_t *cb;
530 vec_foreach (cb, ip4_main.table_bind_callbacks)
531 cb->function (&ip4_main, cb->function_opaque,
534 ip4_main.fib_index_by_sw_if_index[sw_if_index]);
538 /* reset back to default */
539 if (0 != ip4_main.fib_index_by_sw_if_index[sw_if_index])
540 fib_table_unlock (ip4_main.fib_index_by_sw_if_index[sw_if_index],
541 FIB_PROTOCOL_IP4, src);
542 if (0 != ip4_main.mfib_index_by_sw_if_index[sw_if_index])
543 mfib_table_unlock (ip4_main.mfib_index_by_sw_if_index
544 [sw_if_index], FIB_PROTOCOL_IP4, msrc);
549 /* we need to lock the table now it's inuse */
550 fib_index = fib_table_find_or_create_and_lock (FIB_PROTOCOL_IP4,
553 mfib_index = mfib_table_find_or_create_and_lock (FIB_PROTOCOL_IP4,
557 ip4_main.fib_index_by_sw_if_index[sw_if_index] = fib_index;
558 ip4_main.mfib_index_by_sw_if_index[sw_if_index] = mfib_index;
565 send_sw_interface_get_table_reply (vl_api_registration_t * reg,
566 u32 context, int retval, u32 vrf_id)
568 vl_api_sw_interface_get_table_reply_t *mp;
570 mp = vl_msg_api_alloc (sizeof (*mp));
571 clib_memset (mp, 0, sizeof (*mp));
572 mp->_vl_msg_id = ntohs (VL_API_SW_INTERFACE_GET_TABLE_REPLY);
573 mp->context = context;
574 mp->retval = htonl (retval);
575 mp->vrf_id = htonl (vrf_id);
577 vl_api_send_msg (reg, (u8 *) mp);
581 vl_api_sw_interface_get_table_t_handler (vl_api_sw_interface_get_table_t * mp)
583 vl_api_registration_t *reg;
584 fib_table_t *fib_table = 0;
585 u32 sw_if_index = ~0;
588 fib_protocol_t fib_proto = FIB_PROTOCOL_IP4;
591 reg = vl_api_client_index_to_registration (mp->client_index);
595 VALIDATE_SW_IF_INDEX (mp);
597 sw_if_index = ntohl (mp->sw_if_index);
600 fib_proto = FIB_PROTOCOL_IP6;
602 fib_index = fib_table_get_index_for_sw_if_index (fib_proto, sw_if_index);
605 fib_table = fib_table_get (fib_index, fib_proto);
606 table_id = fib_table->ft_table_id;
609 BAD_SW_IF_INDEX_LABEL;
611 send_sw_interface_get_table_reply (reg, mp->context, rv, table_id);
614 static void vl_api_sw_interface_set_unnumbered_t_handler
615 (vl_api_sw_interface_set_unnumbered_t * mp)
617 vl_api_sw_interface_set_unnumbered_reply_t *rmp;
619 vnet_main_t *vnm = vnet_get_main ();
620 u32 sw_if_index = ntohl (mp->sw_if_index);
621 u32 unnumbered_sw_if_index = ntohl (mp->unnumbered_sw_if_index);
624 * The API message field names are backwards from
625 * the underlying data structure names.
626 * It's not worth changing them now.
628 if (!vnet_sw_interface_is_api_valid (vnm, unnumbered_sw_if_index))
630 rv = VNET_API_ERROR_INVALID_SW_IF_INDEX;
634 /* Only check the "use loop0" field when setting the binding */
635 if (mp->is_add && !vnet_sw_interface_is_api_valid (vnm, sw_if_index))
637 rv = VNET_API_ERROR_INVALID_SW_IF_INDEX_2;
641 vnet_sw_interface_update_unnumbered (unnumbered_sw_if_index,
642 sw_if_index, mp->is_add);
644 REPLY_MACRO (VL_API_SW_INTERFACE_SET_UNNUMBERED_REPLY);
648 vl_api_sw_interface_clear_stats_t_handler (vl_api_sw_interface_clear_stats_t *
651 vl_api_sw_interface_clear_stats_reply_t *rmp;
653 vnet_main_t *vnm = vnet_get_main ();
654 vnet_interface_main_t *im = &vnm->interface_main;
655 vlib_simple_counter_main_t *sm;
656 vlib_combined_counter_main_t *cm;
657 static vnet_main_t **my_vnet_mains;
658 int i, j, n_counters;
661 if (mp->sw_if_index != ~0)
662 VALIDATE_SW_IF_INDEX (mp);
664 vec_reset_length (my_vnet_mains);
666 for (i = 0; i < vec_len (vnet_mains); i++)
669 vec_add1 (my_vnet_mains, vnet_mains[i]);
672 if (vec_len (vnet_mains) == 0)
673 vec_add1 (my_vnet_mains, vnm);
675 n_counters = vec_len (im->combined_sw_if_counters);
677 for (j = 0; j < n_counters; j++)
679 for (i = 0; i < vec_len (my_vnet_mains); i++)
681 im = &my_vnet_mains[i]->interface_main;
682 cm = im->combined_sw_if_counters + j;
683 if (mp->sw_if_index == (u32) ~ 0)
684 vlib_clear_combined_counters (cm);
686 vlib_zero_combined_counter (cm, ntohl (mp->sw_if_index));
690 n_counters = vec_len (im->sw_if_counters);
692 for (j = 0; j < n_counters; j++)
694 for (i = 0; i < vec_len (my_vnet_mains); i++)
696 im = &my_vnet_mains[i]->interface_main;
697 sm = im->sw_if_counters + j;
698 if (mp->sw_if_index == (u32) ~ 0)
699 vlib_clear_simple_counters (sm);
701 vlib_zero_simple_counter (sm, ntohl (mp->sw_if_index));
705 BAD_SW_IF_INDEX_LABEL;
707 REPLY_MACRO (VL_API_SW_INTERFACE_CLEAR_STATS_REPLY);
710 #define API_LINK_STATE_EVENT 1
711 #define API_ADMIN_UP_DOWN_EVENT 2
714 event_data_cmp (void *a1, void *a2)
719 return (word) e1[0] - (word) e2[0];
723 send_sw_interface_event (vpe_api_main_t * am,
724 vpe_client_registration_t * reg,
725 vl_api_registration_t * vl_reg,
726 vnet_sw_interface_t * swif)
728 vl_api_sw_interface_event_t *mp;
729 vnet_main_t *vnm = am->vnet_main;
731 vnet_hw_interface_t *hi = vnet_get_sup_hw_interface (vnm,
733 mp = vl_msg_api_alloc (sizeof (*mp));
734 clib_memset (mp, 0, sizeof (*mp));
735 mp->_vl_msg_id = ntohs (VL_API_SW_INTERFACE_EVENT);
736 mp->sw_if_index = ntohl (swif->sw_if_index);
737 mp->client_index = reg->client_index;
738 mp->pid = reg->client_pid;
741 mp->flags |= (swif->flags & VNET_SW_INTERFACE_FLAG_ADMIN_UP) ?
742 IF_STATUS_API_FLAG_ADMIN_UP : 0;
743 mp->flags |= (hi->flags & VNET_HW_INTERFACE_FLAG_LINK_UP) ?
744 IF_STATUS_API_FLAG_LINK_UP : 0;
745 mp->flags = ntohl (mp->flags);
747 vl_api_send_msg (vl_reg, (u8 *) mp);
751 link_state_process (vlib_main_t * vm,
752 vlib_node_runtime_t * rt, vlib_frame_t * f)
754 vpe_api_main_t *vam = &vpe_api_main;
755 vnet_main_t *vnm = vam->vnet_main;
756 vnet_sw_interface_t *swif;
757 uword *event_data = 0;
758 vpe_client_registration_t *reg;
760 u32 prev_sw_if_index;
761 vl_api_registration_t *vl_reg;
763 vam->link_state_process_up = 1;
767 vlib_process_wait_for_event (vm);
769 /* Unified list of changed link or admin state sw_if_indices */
770 vlib_process_get_events_with_type
771 (vm, &event_data, API_LINK_STATE_EVENT);
772 vlib_process_get_events_with_type
773 (vm, &event_data, API_ADMIN_UP_DOWN_EVENT);
775 /* Sort, so we can eliminate duplicates */
776 vec_sort_with_function (event_data, event_data_cmp);
778 prev_sw_if_index = ~0;
780 for (i = 0; i < vec_len (event_data); i++)
782 /* Only one message per swif */
783 if (prev_sw_if_index == event_data[i])
785 prev_sw_if_index = event_data[i];
788 pool_foreach(reg, vam->interface_events_registrations,
790 vl_reg = vl_api_client_index_to_registration (reg->client_index);
793 /* sw_interface may be deleted already */
794 if (!pool_is_free_index (vnm->interface_main.sw_interfaces,
797 swif = vnet_get_sw_interface (vnm, event_data[i]);
798 send_sw_interface_event (vam, reg, vl_reg, swif);
804 vec_reset_length (event_data);
810 static clib_error_t *link_up_down_function (vnet_main_t * vm, u32 hw_if_index,
812 static clib_error_t *admin_up_down_function (vnet_main_t * vm,
813 u32 hw_if_index, u32 flags);
816 VLIB_REGISTER_NODE (link_state_process_node,static) = {
817 .function = link_state_process,
818 .type = VLIB_NODE_TYPE_PROCESS,
819 .name = "vpe-link-state-process",
823 VNET_SW_INTERFACE_ADMIN_UP_DOWN_FUNCTION (admin_up_down_function);
824 VNET_HW_INTERFACE_LINK_UP_DOWN_FUNCTION (link_up_down_function);
826 static clib_error_t *
827 link_up_down_function (vnet_main_t * vm, u32 hw_if_index, u32 flags)
829 vpe_api_main_t *vam = &vpe_api_main;
830 vnet_hw_interface_t *hi = vnet_get_hw_interface (vm, hw_if_index);
832 if (vam->link_state_process_up)
833 vlib_process_signal_event (vam->vlib_main,
834 link_state_process_node.index,
835 API_LINK_STATE_EVENT, hi->sw_if_index);
839 static clib_error_t *
840 admin_up_down_function (vnet_main_t * vm, u32 sw_if_index, u32 flags)
842 vpe_api_main_t *vam = &vpe_api_main;
845 * Note: it's perfectly fair to set a subif admin up / admin down.
846 * Note the subtle distinction between this routine and the previous
849 if (vam->link_state_process_up)
850 vlib_process_signal_event (vam->vlib_main,
851 link_state_process_node.index,
852 API_ADMIN_UP_DOWN_EVENT, sw_if_index);
856 static void vl_api_sw_interface_tag_add_del_t_handler
857 (vl_api_sw_interface_tag_add_del_t * mp)
859 vnet_main_t *vnm = vnet_get_main ();
860 vl_api_sw_interface_tag_add_del_reply_t *rmp;
863 u32 sw_if_index = ntohl (mp->sw_if_index);
865 VALIDATE_SW_IF_INDEX (mp);
869 if (vl_api_from_api_string_c (&mp->tag)[0] == 0)
871 rv = VNET_API_ERROR_INVALID_VALUE;
875 tag = format (0, "%s%c", vl_api_from_api_string_c (&mp->tag), 0);
876 vnet_set_sw_interface_tag (vnm, tag, sw_if_index);
879 vnet_clear_sw_interface_tag (vnm, sw_if_index);
881 BAD_SW_IF_INDEX_LABEL;
883 REPLY_MACRO (VL_API_SW_INTERFACE_TAG_ADD_DEL_REPLY);
886 static void vl_api_sw_interface_set_mac_address_t_handler
887 (vl_api_sw_interface_set_mac_address_t * mp)
889 vl_api_sw_interface_set_mac_address_reply_t *rmp;
890 vnet_main_t *vnm = vnet_get_main ();
891 u32 sw_if_index = ntohl (mp->sw_if_index);
892 vnet_sw_interface_t *si;
897 VALIDATE_SW_IF_INDEX (mp);
899 si = vnet_get_sw_interface (vnm, sw_if_index);
900 mac_address_decode (mp->mac_address, &mac);
902 vnet_hw_interface_change_mac_address (vnm, si->hw_if_index, (u8 *) & mac);
905 rv = VNET_API_ERROR_UNIMPLEMENTED;
906 clib_error_report (error);
910 BAD_SW_IF_INDEX_LABEL;
912 REPLY_MACRO (VL_API_SW_INTERFACE_SET_MAC_ADDRESS_REPLY);
915 static void vl_api_sw_interface_get_mac_address_t_handler
916 (vl_api_sw_interface_get_mac_address_t * mp)
918 vl_api_sw_interface_get_mac_address_reply_t *rmp;
919 vl_api_registration_t *reg;
920 vnet_main_t *vnm = vnet_get_main ();
921 u32 sw_if_index = ntohl (mp->sw_if_index);
922 vnet_sw_interface_t *si;
923 ethernet_interface_t *eth_if = 0;
926 VALIDATE_SW_IF_INDEX (mp);
928 si = vnet_get_sup_sw_interface (vnm, sw_if_index);
929 if (si->type == VNET_SW_INTERFACE_TYPE_HARDWARE)
930 eth_if = ethernet_get_interface (ðernet_main, si->hw_if_index);
932 BAD_SW_IF_INDEX_LABEL;
934 reg = vl_api_client_index_to_registration (mp->client_index);
937 rmp = vl_msg_api_alloc (sizeof (*rmp));
938 rmp->_vl_msg_id = htons (VL_API_SW_INTERFACE_GET_MAC_ADDRESS_REPLY);
939 rmp->context = mp->context;
940 rmp->retval = htonl (rv);
942 mac_address_encode ((mac_address_t *) eth_if->address, rmp->mac_address);
943 vl_api_send_msg (reg, (u8 *) rmp);
946 static void vl_api_sw_interface_set_rx_mode_t_handler
947 (vl_api_sw_interface_set_rx_mode_t * mp)
949 vl_api_sw_interface_set_rx_mode_reply_t *rmp;
950 vnet_main_t *vnm = vnet_get_main ();
951 u32 sw_if_index = ntohl (mp->sw_if_index);
952 vnet_sw_interface_t *si;
956 VALIDATE_SW_IF_INDEX (mp);
958 si = vnet_get_sw_interface (vnm, sw_if_index);
959 if (si->type != VNET_SW_INTERFACE_TYPE_HARDWARE)
961 rv = VNET_API_ERROR_INVALID_VALUE;
962 goto bad_sw_if_index;
965 error = set_hw_interface_change_rx_mode (vnm, si->hw_if_index,
967 ntohl (mp->queue_id), mp->mode);
970 rv = VNET_API_ERROR_UNIMPLEMENTED;
971 clib_error_report (error);
975 BAD_SW_IF_INDEX_LABEL;
977 REPLY_MACRO (VL_API_SW_INTERFACE_SET_RX_MODE_REPLY);
981 send_interface_rx_placement_details (vpe_api_main_t * am,
982 vl_api_registration_t * rp,
983 u32 sw_if_index, u32 worker_id,
984 u32 queue_id, u8 mode, u32 context)
986 vl_api_sw_interface_rx_placement_details_t *mp;
987 mp = vl_msg_api_alloc (sizeof (*mp));
988 clib_memset (mp, 0, sizeof (*mp));
990 mp->_vl_msg_id = htons (VL_API_SW_INTERFACE_RX_PLACEMENT_DETAILS);
991 mp->sw_if_index = htonl (sw_if_index);
992 mp->queue_id = htonl (queue_id);
993 mp->worker_id = htonl (worker_id);
995 mp->context = context;
997 vl_api_send_msg (rp, (u8 *) mp);
1000 static void vl_api_sw_interface_rx_placement_dump_t_handler
1001 (vl_api_sw_interface_rx_placement_dump_t * mp)
1003 vnet_main_t *vnm = vnet_get_main ();
1004 vpe_api_main_t *am = &vpe_api_main;
1005 u32 sw_if_index = ntohl (mp->sw_if_index);
1006 vl_api_registration_t *reg;
1008 reg = vl_api_client_index_to_registration (mp->client_index);
1012 if (sw_if_index == ~0)
1014 vnet_device_input_runtime_t *rt;
1015 vnet_device_and_queue_t *dq;
1016 vlib_node_t *pn = vlib_get_node_by_name (am->vlib_main,
1017 (u8 *) "device-input");
1022 foreach_vlib_main (({
1023 clib_bitmap_foreach (si, pn->sibling_bitmap,
1025 rt = vlib_node_get_runtime_data (this_vlib_main, si);
1026 vec_foreach (dq, rt->devices_and_queues)
1028 vnet_hw_interface_t *hw = vnet_get_hw_interface (vnm,
1030 send_interface_rx_placement_details (am, reg, hw->sw_if_index, index,
1031 dq->queue_id, dq->mode, mp->context);
1041 vnet_sw_interface_t *si;
1043 if (!vnet_sw_if_index_is_api_valid (sw_if_index))
1045 clib_warning ("sw_if_index %u does not exist", sw_if_index);
1046 goto bad_sw_if_index;
1049 si = vnet_get_sw_interface (vnm, sw_if_index);
1050 if (si->type != VNET_SW_INTERFACE_TYPE_HARDWARE)
1052 clib_warning ("interface type is not HARDWARE! P2P, PIPE and SUB"
1053 " interfaces are not supported");
1054 goto bad_sw_if_index;
1057 vnet_hw_interface_t *hw = vnet_get_hw_interface (vnm, si->hw_if_index);
1059 for (i = 0; i < vec_len (hw->dq_runtime_index_by_queue); i++)
1061 send_interface_rx_placement_details (am, reg, hw->sw_if_index,
1062 hw->input_node_thread_index_by_queue
1064 hw->rx_mode_by_queue[i],
1069 BAD_SW_IF_INDEX_LABEL;
1072 static void vl_api_sw_interface_set_rx_placement_t_handler
1073 (vl_api_sw_interface_set_rx_placement_t * mp)
1075 vl_api_sw_interface_set_rx_placement_reply_t *rmp;
1076 vnet_main_t *vnm = vnet_get_main ();
1077 u32 sw_if_index = ntohl (mp->sw_if_index);
1078 vnet_sw_interface_t *si;
1079 clib_error_t *error = 0;
1082 VALIDATE_SW_IF_INDEX (mp);
1084 si = vnet_get_sw_interface (vnm, sw_if_index);
1085 if (si->type != VNET_SW_INTERFACE_TYPE_HARDWARE)
1087 rv = VNET_API_ERROR_INVALID_VALUE;
1088 goto bad_sw_if_index;
1091 error = set_hw_interface_rx_placement (si->hw_if_index,
1092 ntohl (mp->queue_id),
1093 ntohl (mp->worker_id), mp->is_main);
1096 rv = VNET_API_ERROR_UNIMPLEMENTED;
1097 clib_error_report (error);
1101 BAD_SW_IF_INDEX_LABEL;
1103 REPLY_MACRO (VL_API_SW_INTERFACE_SET_RX_PLACEMENT_REPLY);
1107 vl_api_create_vlan_subif_t_handler (vl_api_create_vlan_subif_t * mp)
1109 vl_api_create_vlan_subif_reply_t *rmp;
1110 vnet_main_t *vnm = vnet_get_main ();
1111 u32 sw_if_index = (u32) ~ 0;
1112 vnet_hw_interface_t *hi;
1115 vnet_sw_interface_t template;
1117 vnet_interface_main_t *im = &vnm->interface_main;
1118 u64 sup_and_sub_key;
1119 vl_api_registration_t *reg;
1120 clib_error_t *error;
1122 VALIDATE_SW_IF_INDEX (mp);
1124 hi = vnet_get_sup_hw_interface (vnm, ntohl (mp->sw_if_index));
1126 if (hi->bond_info == VNET_HW_INTERFACE_BOND_INFO_SLAVE)
1128 rv = VNET_API_ERROR_BOND_SLAVE_NOT_ALLOWED;
1132 id = ntohl (mp->vlan_id);
1133 if (id == 0 || id > 4095)
1135 rv = VNET_API_ERROR_INVALID_VLAN;
1139 sup_and_sub_key = ((u64) (hi->sw_if_index) << 32) | (u64) id;
1141 p = hash_get_mem (im->sw_if_index_by_sup_and_sub, &sup_and_sub_key);
1144 rv = VNET_API_ERROR_VLAN_ALREADY_EXISTS;
1148 clib_memset (&template, 0, sizeof (template));
1149 template.type = VNET_SW_INTERFACE_TYPE_SUB;
1150 template.flood_class = VNET_FLOOD_CLASS_NORMAL;
1151 template.sup_sw_if_index = hi->sw_if_index;
1152 template.sub.id = id;
1153 template.sub.eth.raw_flags = 0;
1154 template.sub.eth.flags.one_tag = 1;
1155 template.sub.eth.outer_vlan_id = id;
1156 template.sub.eth.flags.exact_match = 1;
1158 error = vnet_create_sw_interface (vnm, &template, &sw_if_index);
1161 clib_error_report (error);
1162 rv = VNET_API_ERROR_INVALID_REGISTRATION;
1166 u64 *kp = clib_mem_alloc (sizeof (*kp));
1167 *kp = sup_and_sub_key;
1169 hash_set (hi->sub_interface_sw_if_index_by_id, id, sw_if_index);
1170 hash_set_mem (im->sw_if_index_by_sup_and_sub, kp, sw_if_index);
1172 BAD_SW_IF_INDEX_LABEL;
1175 reg = vl_api_client_index_to_registration (mp->client_index);
1179 rmp = vl_msg_api_alloc (sizeof (*rmp));
1180 rmp->_vl_msg_id = htons (VL_API_CREATE_VLAN_SUBIF_REPLY);
1181 rmp->context = mp->context;
1182 rmp->retval = htonl (rv);
1183 rmp->sw_if_index = htonl (sw_if_index);
1184 vl_api_send_msg (reg, (u8 *) rmp);
1188 vl_api_create_subif_t_handler (vl_api_create_subif_t * mp)
1190 vl_api_create_subif_reply_t *rmp;
1191 vnet_main_t *vnm = vnet_get_main ();
1192 u32 sw_if_index = ~0;
1195 vnet_sw_interface_t *si;
1196 vnet_hw_interface_t *hi;
1197 vnet_sw_interface_t template;
1199 vnet_interface_main_t *im = &vnm->interface_main;
1200 u64 sup_and_sub_key;
1201 clib_error_t *error;
1203 VALIDATE_SW_IF_INDEX (mp);
1205 si = vnet_get_sup_sw_interface (vnm, ntohl (mp->sw_if_index));
1206 hi = vnet_get_sup_hw_interface (vnm, ntohl (mp->sw_if_index));
1208 if (hi->bond_info == VNET_HW_INTERFACE_BOND_INFO_SLAVE)
1210 rv = VNET_API_ERROR_BOND_SLAVE_NOT_ALLOWED;
1214 sw_if_index = si->sw_if_index;
1215 sub_id = ntohl (mp->sub_id);
1217 sup_and_sub_key = ((u64) (sw_if_index) << 32) | (u64) sub_id;
1219 p = hash_get_mem (im->sw_if_index_by_sup_and_sub, &sup_and_sub_key);
1223 clib_warning ("sup sw_if_index %d, sub id %d already exists\n",
1224 sw_if_index, sub_id);
1225 rv = VNET_API_ERROR_SUBIF_ALREADY_EXISTS;
1229 clib_memset (&template, 0, sizeof (template));
1230 template.type = VNET_SW_INTERFACE_TYPE_SUB;
1231 template.flood_class = VNET_FLOOD_CLASS_NORMAL;
1232 template.sup_sw_if_index = sw_if_index;
1233 template.sub.id = sub_id;
1234 template.sub.eth.raw_flags = ntohl (mp->sub_if_flags);
1235 template.sub.eth.outer_vlan_id = ntohs (mp->outer_vlan_id);
1236 template.sub.eth.inner_vlan_id = ntohs (mp->inner_vlan_id);
1238 error = vnet_create_sw_interface (vnm, &template, &sw_if_index);
1241 clib_error_report (error);
1242 rv = VNET_API_ERROR_SUBIF_CREATE_FAILED;
1246 u64 *kp = clib_mem_alloc (sizeof (*kp));
1247 *kp = sup_and_sub_key;
1249 hash_set (hi->sub_interface_sw_if_index_by_id, sub_id, sw_if_index);
1250 hash_set_mem (im->sw_if_index_by_sup_and_sub, kp, sw_if_index);
1252 BAD_SW_IF_INDEX_LABEL;
1257 REPLY_MACRO2(VL_API_CREATE_SUBIF_REPLY,
1259 rmp->sw_if_index = ntohl(sw_if_index);
1265 vl_api_delete_subif_t_handler (vl_api_delete_subif_t * mp)
1267 vl_api_delete_subif_reply_t *rmp;
1270 rv = vnet_delete_sub_interface (ntohl (mp->sw_if_index));
1272 REPLY_MACRO (VL_API_DELETE_SUBIF_REPLY);
1276 vl_api_interface_name_renumber_t_handler (vl_api_interface_name_renumber_t *
1279 vl_api_interface_name_renumber_reply_t *rmp;
1282 VALIDATE_SW_IF_INDEX (mp);
1284 rv = vnet_interface_name_renumber
1285 (ntohl (mp->sw_if_index), ntohl (mp->new_show_dev_instance));
1287 BAD_SW_IF_INDEX_LABEL;
1289 REPLY_MACRO (VL_API_INTERFACE_NAME_RENUMBER_REPLY);
1293 vl_api_create_loopback_t_handler (vl_api_create_loopback_t * mp)
1295 vl_api_create_loopback_reply_t *rmp;
1300 mac_address_decode (mp->mac_address, &mac);
1301 rv = vnet_create_loopback_interface (&sw_if_index, (u8 *) & mac, 0, 0);
1304 REPLY_MACRO2(VL_API_CREATE_LOOPBACK_REPLY,
1306 rmp->sw_if_index = ntohl (sw_if_index);
1311 static void vl_api_create_loopback_instance_t_handler
1312 (vl_api_create_loopback_instance_t * mp)
1314 vl_api_create_loopback_instance_reply_t *rmp;
1316 u8 is_specified = mp->is_specified;
1317 u32 user_instance = ntohl (mp->user_instance);
1321 mac_address_decode (mp->mac_address, &mac);
1322 rv = vnet_create_loopback_interface (&sw_if_index, (u8 *) & mac,
1323 is_specified, user_instance);
1326 REPLY_MACRO2(VL_API_CREATE_LOOPBACK_INSTANCE_REPLY,
1328 rmp->sw_if_index = ntohl (sw_if_index);
1334 vl_api_delete_loopback_t_handler (vl_api_delete_loopback_t * mp)
1336 vl_api_delete_loopback_reply_t *rmp;
1340 sw_if_index = ntohl (mp->sw_if_index);
1341 rv = vnet_delete_loopback_interface (sw_if_index);
1343 REPLY_MACRO (VL_API_DELETE_LOOPBACK_REPLY);
1347 vl_api_collect_detailed_interface_stats_t_handler
1348 (vl_api_collect_detailed_interface_stats_t * mp)
1350 vl_api_collect_detailed_interface_stats_reply_t *rmp;
1354 vnet_sw_interface_stats_collect_enable_disable (ntohl (mp->sw_if_index),
1355 mp->enable_disable);
1357 REPLY_MACRO (VL_API_COLLECT_DETAILED_INTERFACE_STATS_REPLY);
1362 * Add vpe's API message handlers to the table.
1363 * vlib has already mapped shared memory and
1364 * added the client registration handlers.
1365 * See .../vlib-api/vlibmemory/memclnt_vlib.c:memclnt_process()
1367 #define vl_msg_name_crc_list
1368 #include <vnet/interface.api.h>
1369 #undef vl_msg_name_crc_list
1372 setup_message_id_table (api_main_t * am)
1374 #define _(id,n,crc) vl_msg_api_add_msg_name_crc (am, #n "_" #crc, id);
1375 foreach_vl_msg_name_crc_interface;
1379 pub_sub_handler (interface_events, INTERFACE_EVENTS);
1381 static clib_error_t *
1382 interface_api_hookup (vlib_main_t * vm)
1384 api_main_t *am = &api_main;
1387 vl_msg_api_set_handlers(VL_API_##N, #n, \
1388 vl_api_##n##_t_handler, \
1390 vl_api_##n##_t_endian, \
1391 vl_api_##n##_t_print, \
1392 sizeof(vl_api_##n##_t), 1);
1393 foreach_vpe_api_msg;
1397 * Set up the (msg_name, crc, message-id) table
1399 setup_message_id_table (am);
1404 VLIB_API_INIT_FUNCTION (interface_api_hookup);
1407 * fd.io coding-style-patch-verification: ON
1410 * eval: (c-set-style "gnu")