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_TABLE, sw_interface_set_table) \
58 _(SW_INTERFACE_GET_TABLE, sw_interface_get_table) \
59 _(SW_INTERFACE_SET_UNNUMBERED, sw_interface_set_unnumbered) \
60 _(SW_INTERFACE_CLEAR_STATS, sw_interface_clear_stats) \
61 _(SW_INTERFACE_TAG_ADD_DEL, sw_interface_tag_add_del) \
62 _(SW_INTERFACE_SET_MAC_ADDRESS, sw_interface_set_mac_address)
65 vl_api_sw_interface_set_flags_t_handler (vl_api_sw_interface_set_flags_t * mp)
67 vl_api_sw_interface_set_flags_reply_t *rmp;
68 vnet_main_t *vnm = vnet_get_main ();
73 VALIDATE_SW_IF_INDEX (mp);
75 flags = mp->admin_up_down ? VNET_SW_INTERFACE_FLAG_ADMIN_UP : 0;
77 error = vnet_sw_interface_set_flags (vnm, ntohl (mp->sw_if_index), flags);
81 clib_error_report (error);
84 BAD_SW_IF_INDEX_LABEL;
85 REPLY_MACRO (VL_API_SW_INTERFACE_SET_FLAGS_REPLY);
89 vl_api_sw_interface_set_mtu_t_handler (vl_api_sw_interface_set_mtu_t * mp)
91 vl_api_sw_interface_set_mtu_reply_t *rmp;
92 vnet_main_t *vnm = vnet_get_main ();
93 u32 flags = ETHERNET_INTERFACE_FLAG_MTU;
94 u32 sw_if_index = ntohl (mp->sw_if_index);
95 u16 mtu = ntohs (mp->mtu);
96 ethernet_main_t *em = ðernet_main;
99 VALIDATE_SW_IF_INDEX (mp);
101 vnet_sw_interface_t *si = vnet_get_sw_interface (vnm, sw_if_index);
102 if (si->type != VNET_SW_INTERFACE_TYPE_HARDWARE)
104 rv = VNET_API_ERROR_INVALID_VALUE;
105 goto bad_sw_if_index;
108 vnet_hw_interface_t *hi = vnet_get_hw_interface (vnm, si->hw_if_index);
109 ethernet_interface_t *eif = ethernet_get_interface (em, si->hw_if_index);
113 rv = VNET_API_ERROR_FEATURE_DISABLED;
114 goto bad_sw_if_index;
117 if (mtu < hi->min_supported_packet_bytes)
119 rv = VNET_API_ERROR_INVALID_VALUE;
120 goto bad_sw_if_index;
123 if (mtu > hi->max_supported_packet_bytes)
125 rv = VNET_API_ERROR_INVALID_VALUE;
126 goto bad_sw_if_index;
129 if (hi->max_packet_bytes != mtu)
131 hi->max_packet_bytes = mtu;
132 ethernet_set_flags (vnm, si->hw_if_index, flags);
135 BAD_SW_IF_INDEX_LABEL;
136 REPLY_MACRO (VL_API_SW_INTERFACE_SET_MTU_REPLY);
140 send_sw_interface_details (vpe_api_main_t * am,
141 unix_shared_memory_queue_t * q,
142 vnet_sw_interface_t * swif,
143 u8 * interface_name, u32 context)
145 vnet_hw_interface_t *hi =
146 vnet_get_sup_hw_interface (am->vnet_main, swif->sw_if_index);
148 vl_api_sw_interface_details_t *mp = vl_msg_api_alloc (sizeof (*mp));
149 memset (mp, 0, sizeof (*mp));
150 mp->_vl_msg_id = ntohs (VL_API_SW_INTERFACE_DETAILS);
151 mp->sw_if_index = ntohl (swif->sw_if_index);
152 mp->sup_sw_if_index = ntohl (swif->sup_sw_if_index);
153 mp->admin_up_down = (swif->flags & VNET_SW_INTERFACE_FLAG_ADMIN_UP) ? 1 : 0;
154 mp->link_up_down = (hi->flags & VNET_HW_INTERFACE_FLAG_LINK_UP) ? 1 : 0;
155 mp->link_duplex = ((hi->flags & VNET_HW_INTERFACE_FLAG_DUPLEX_MASK) >>
156 VNET_HW_INTERFACE_FLAG_DUPLEX_SHIFT);
157 mp->link_speed = ((hi->flags & VNET_HW_INTERFACE_FLAG_SPEED_MASK) >>
158 VNET_HW_INTERFACE_FLAG_SPEED_SHIFT);
159 mp->link_mtu = ntohs (hi->max_packet_bytes);
160 mp->context = context;
162 strncpy ((char *) mp->interface_name,
163 (char *) interface_name, ARRAY_LEN (mp->interface_name) - 1);
165 /* Send the L2 address for ethernet physical intfcs */
166 if (swif->sup_sw_if_index == swif->sw_if_index
167 && hi->hw_class_index == ethernet_hw_interface_class.index)
169 ethernet_main_t *em = ethernet_get_main (am->vlib_main);
170 ethernet_interface_t *ei;
172 ei = pool_elt_at_index (em->interfaces, hi->hw_instance);
173 ASSERT (sizeof (mp->l2_address) >= sizeof (ei->address));
174 clib_memcpy (mp->l2_address, ei->address, sizeof (ei->address));
175 mp->l2_address_length = ntohl (sizeof (ei->address));
177 else if (swif->sup_sw_if_index != swif->sw_if_index)
179 vnet_sub_interface_t *sub = &swif->sub;
180 mp->sub_id = ntohl (sub->id);
181 mp->sub_dot1ad = sub->eth.flags.dot1ad;
182 mp->sub_number_of_tags =
183 sub->eth.flags.one_tag + sub->eth.flags.two_tags * 2;
184 mp->sub_outer_vlan_id = ntohs (sub->eth.outer_vlan_id);
185 mp->sub_inner_vlan_id = ntohs (sub->eth.inner_vlan_id);
186 mp->sub_exact_match = sub->eth.flags.exact_match;
187 mp->sub_default = sub->eth.flags.default_sub;
188 mp->sub_outer_vlan_id_any = sub->eth.flags.outer_vlan_id_any;
189 mp->sub_inner_vlan_id_any = sub->eth.flags.inner_vlan_id_any;
191 /* vlan tag rewrite data */
192 u32 vtr_op = L2_VTR_DISABLED;
193 u32 vtr_push_dot1q = 0, vtr_tag1 = 0, vtr_tag2 = 0;
195 if (l2vtr_get (am->vlib_main, am->vnet_main, swif->sw_if_index,
196 &vtr_op, &vtr_push_dot1q, &vtr_tag1, &vtr_tag2) != 0)
198 // error - default to disabled
199 mp->vtr_op = ntohl (L2_VTR_DISABLED);
200 clib_warning ("cannot get vlan tag rewrite for sw_if_index %d",
205 mp->vtr_op = ntohl (vtr_op);
206 mp->vtr_push_dot1q = ntohl (vtr_push_dot1q);
207 mp->vtr_tag1 = ntohl (vtr_tag1);
208 mp->vtr_tag2 = ntohl (vtr_tag2);
212 /* pbb tag rewrite data */
213 ethernet_header_t eth_hdr;
214 u32 vtr_op = L2_VTR_DISABLED;
218 memset (ð_hdr, 0, sizeof (eth_hdr));
220 if (!l2pbb_get (am->vlib_main, am->vnet_main, swif->sw_if_index,
221 &vtr_op, &outer_tag, ð_hdr, &b_vlanid, &i_sid))
224 clib_memcpy (mp->b_dmac, eth_hdr.dst_address,
225 sizeof (eth_hdr.dst_address));
226 clib_memcpy (mp->b_smac, eth_hdr.src_address,
227 sizeof (eth_hdr.src_address));
228 mp->b_vlanid = b_vlanid;
232 u8 *tag = vnet_get_sw_interface_tag (vnet_get_main (), swif->sw_if_index);
234 strncpy ((char *) mp->tag, (char *) tag, ARRAY_LEN (mp->tag) - 1);
236 vl_msg_api_send_shmem (q, (u8 *) & mp);
240 vl_api_sw_interface_dump_t_handler (vl_api_sw_interface_dump_t * mp)
242 vpe_api_main_t *am = &vpe_api_main;
243 vnet_sw_interface_t *swif;
244 vnet_interface_main_t *im = &am->vnet_main->interface_main;
246 unix_shared_memory_queue_t *q =
247 vl_api_client_index_to_input_queue (mp->client_index);
251 u8 *filter = 0, *name = 0;
252 if (mp->name_filter_valid)
254 mp->name_filter[ARRAY_LEN (mp->name_filter) - 1] = 0;
255 filter = format (0, "%s%c", mp->name_filter, 0);
258 char *strcasestr (char *, char *); /* lnx hdr file botch */
260 pool_foreach (swif, im->sw_interfaces,
262 if (!vnet_swif_is_api_visible (swif))
264 vec_reset_length(name);
265 name = format (name, "%U%c", format_vnet_sw_interface_name, am->vnet_main,
268 if (filter && !strcasestr((char *) name, (char *) filter))
271 send_sw_interface_details (am, q, swif, name, mp->context);
280 vl_api_sw_interface_add_del_address_t_handler
281 (vl_api_sw_interface_add_del_address_t * mp)
283 vlib_main_t *vm = vlib_get_main ();
284 vl_api_sw_interface_add_del_address_reply_t *rmp;
288 VALIDATE_SW_IF_INDEX (mp);
290 is_del = mp->is_add == 0;
293 ip_del_all_interface_addresses (vm, ntohl (mp->sw_if_index));
294 else if (mp->is_ipv6)
295 ip6_add_del_interface_address (vm, ntohl (mp->sw_if_index),
296 (void *) mp->address,
297 mp->address_length, is_del);
299 ip4_add_del_interface_address (vm, ntohl (mp->sw_if_index),
300 (void *) mp->address,
301 mp->address_length, is_del);
303 BAD_SW_IF_INDEX_LABEL;
305 REPLY_MACRO (VL_API_SW_INTERFACE_ADD_DEL_ADDRESS_REPLY);
308 void stats_dslock_with_hint (int hint, int tag) __attribute__ ((weak));
310 stats_dslock_with_hint (int hint, int tag)
314 void stats_dsunlock (void) __attribute__ ((weak));
316 stats_dsunlock (void)
321 vl_api_sw_interface_set_table_t_handler (vl_api_sw_interface_set_table_t * mp)
323 vl_api_sw_interface_set_table_reply_t *rmp;
324 u32 sw_if_index = ntohl (mp->sw_if_index);
325 u32 table_id = ntohl (mp->vrf_id);
328 VALIDATE_SW_IF_INDEX (mp);
330 stats_dslock_with_hint (1 /* release hint */ , 4 /* tag */ );
333 rv = ip_table_bind (FIB_PROTOCOL_IP6, sw_if_index, table_id, 1);
335 rv = ip_table_bind (FIB_PROTOCOL_IP4, sw_if_index, table_id, 1);
339 BAD_SW_IF_INDEX_LABEL;
341 REPLY_MACRO (VL_API_SW_INTERFACE_SET_TABLE_REPLY);
345 ip_table_bind (fib_protocol_t fproto,
346 uint32_t sw_if_index, uint32_t table_id, u8 is_api)
348 CLIB_UNUSED (ip_interface_address_t * ia);
349 u32 fib_index, mfib_index;
355 src = FIB_SOURCE_API;
356 msrc = MFIB_SOURCE_API;
360 src = FIB_SOURCE_CLI;
361 msrc = MFIB_SOURCE_CLI;
365 * This is temporary whilst I do the song and dance with the CSIT version
369 fib_index = fib_table_find_or_create_and_lock (fproto, table_id, src);
371 mfib_table_find_or_create_and_lock (fproto, table_id, msrc);
380 * This if table does not exist = error is what we want in the end.
382 /* fib_index = fib_table_find (fproto, table_id); */
383 /* mfib_index = mfib_table_find (fproto, table_id); */
385 /* if (~0 == fib_index || ~0 == mfib_index) */
387 /* return (VNET_API_ERROR_NO_SUCH_FIB); */
390 if (FIB_PROTOCOL_IP6 == fproto)
393 * If the interface already has in IP address, then a change int
394 * VRF is not allowed. The IP address applied must first be removed.
395 * We do not do that automatically here, since VPP has no knowledge
396 * of whether thoses subnets are valid in the destination VRF.
399 foreach_ip_interface_address (&ip6_main.lookup_main,
401 1 /* honor unnumbered */ ,
403 return (VNET_API_ERROR_ADDRESS_FOUND_FOR_INTERFACE);
407 vec_validate (ip6_main.fib_index_by_sw_if_index, sw_if_index);
408 vec_validate (ip6_main.mfib_index_by_sw_if_index, sw_if_index);
411 * tell those that are interested that the binding is changing.
413 ip6_table_bind_callback_t *cb;
414 vec_foreach (cb, ip6_main.table_bind_callbacks)
415 cb->function (&ip6_main, cb->function_opaque,
418 ip6_main.fib_index_by_sw_if_index[sw_if_index]);
422 /* reset back to default */
423 if (0 != ip6_main.fib_index_by_sw_if_index[sw_if_index])
424 fib_table_unlock (ip6_main.fib_index_by_sw_if_index[sw_if_index],
425 FIB_PROTOCOL_IP6, src);
426 if (0 != ip6_main.mfib_index_by_sw_if_index[sw_if_index])
427 mfib_table_unlock (ip6_main.mfib_index_by_sw_if_index
428 [sw_if_index], FIB_PROTOCOL_IP6, msrc);
433 /* we need to lock the table now it's inuse */
434 fib_table_lock (fib_index, FIB_PROTOCOL_IP6, src);
435 mfib_table_lock (mfib_index, FIB_PROTOCOL_IP6, msrc);
438 ip6_main.fib_index_by_sw_if_index[sw_if_index] = fib_index;
439 ip6_main.mfib_index_by_sw_if_index[sw_if_index] = mfib_index;
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 (&ip4_main.lookup_main,
452 1 /* honor unnumbered */ ,
454 return (VNET_API_ERROR_ADDRESS_FOUND_FOR_INTERFACE);
458 vec_validate (ip4_main.fib_index_by_sw_if_index, sw_if_index);
459 vec_validate (ip4_main.mfib_index_by_sw_if_index, sw_if_index);
462 * tell those that are interested that the binding is changing.
464 ip4_table_bind_callback_t *cb;
465 vec_foreach (cb, ip4_main.table_bind_callbacks)
466 cb->function (&ip4_main, cb->function_opaque,
469 ip4_main.fib_index_by_sw_if_index[sw_if_index]);
473 /* reset back to default */
474 if (0 != ip4_main.fib_index_by_sw_if_index[sw_if_index])
475 fib_table_unlock (ip4_main.fib_index_by_sw_if_index[sw_if_index],
476 FIB_PROTOCOL_IP4, src);
477 if (0 != ip4_main.mfib_index_by_sw_if_index[sw_if_index])
478 mfib_table_unlock (ip4_main.mfib_index_by_sw_if_index
479 [sw_if_index], FIB_PROTOCOL_IP4, msrc);
484 /* we need to lock the table now it's inuse */
485 fib_index = fib_table_find_or_create_and_lock (FIB_PROTOCOL_IP4,
488 mfib_index = mfib_table_find_or_create_and_lock (FIB_PROTOCOL_IP4,
492 ip4_main.fib_index_by_sw_if_index[sw_if_index] = fib_index;
493 ip4_main.mfib_index_by_sw_if_index[sw_if_index] = mfib_index;
497 * Temporary. undo the locks from the find and create at the staart
501 fib_table_unlock (fib_index, fproto, src);
502 mfib_table_unlock (mfib_index, fproto, msrc);
509 send_sw_interface_get_table_reply (unix_shared_memory_queue_t * q,
510 u32 context, int retval, u32 vrf_id)
512 vl_api_sw_interface_get_table_reply_t *mp;
514 mp = vl_msg_api_alloc (sizeof (*mp));
515 memset (mp, 0, sizeof (*mp));
516 mp->_vl_msg_id = ntohs (VL_API_SW_INTERFACE_GET_TABLE_REPLY);
517 mp->context = context;
518 mp->retval = htonl (retval);
519 mp->vrf_id = htonl (vrf_id);
521 vl_msg_api_send_shmem (q, (u8 *) & mp);
525 vl_api_sw_interface_get_table_t_handler (vl_api_sw_interface_get_table_t * mp)
527 unix_shared_memory_queue_t *q;
528 fib_table_t *fib_table = 0;
529 u32 sw_if_index = ~0;
532 fib_protocol_t fib_proto = FIB_PROTOCOL_IP4;
535 q = vl_api_client_index_to_input_queue (mp->client_index);
539 VALIDATE_SW_IF_INDEX (mp);
541 sw_if_index = ntohl (mp->sw_if_index);
544 fib_proto = FIB_PROTOCOL_IP6;
546 fib_index = fib_table_get_index_for_sw_if_index (fib_proto, sw_if_index);
549 fib_table = fib_table_get (fib_index, fib_proto);
550 table_id = fib_table->ft_table_id;
553 BAD_SW_IF_INDEX_LABEL;
555 send_sw_interface_get_table_reply (q, mp->context, rv, table_id);
558 static void vl_api_sw_interface_set_unnumbered_t_handler
559 (vl_api_sw_interface_set_unnumbered_t * mp)
561 vl_api_sw_interface_set_unnumbered_reply_t *rmp;
563 vnet_main_t *vnm = vnet_get_main ();
564 u32 sw_if_index = ntohl (mp->sw_if_index);
565 u32 unnumbered_sw_if_index = ntohl (mp->unnumbered_sw_if_index);
569 * The API message field names are backwards from
570 * the underlying data structure names.
571 * It's not worth changing them now.
573 if (!vnet_sw_interface_is_api_valid (vnm, unnumbered_sw_if_index))
575 rv = VNET_API_ERROR_INVALID_SW_IF_INDEX;
579 /* Only check the "use loop0" field when setting the binding */
580 if (mp->is_add && !vnet_sw_interface_is_api_valid (vnm, sw_if_index))
582 rv = VNET_API_ERROR_INVALID_SW_IF_INDEX_2;
586 vnet_sw_interface_t *si =
587 vnet_get_sw_interface (vnm, unnumbered_sw_if_index);
588 was_unnum = (si->flags & VNET_SW_INTERFACE_FLAG_UNNUMBERED);
592 si->flags |= VNET_SW_INTERFACE_FLAG_UNNUMBERED;
593 si->unnumbered_sw_if_index = sw_if_index;
595 ip4_main.lookup_main.if_address_pool_index_by_sw_if_index
596 [unnumbered_sw_if_index] =
598 lookup_main.if_address_pool_index_by_sw_if_index[sw_if_index];
600 lookup_main.if_address_pool_index_by_sw_if_index
601 [unnumbered_sw_if_index] =
603 lookup_main.if_address_pool_index_by_sw_if_index[sw_if_index];
607 si->flags &= ~(VNET_SW_INTERFACE_FLAG_UNNUMBERED);
608 si->unnumbered_sw_if_index = (u32) ~ 0;
610 ip4_main.lookup_main.if_address_pool_index_by_sw_if_index
611 [unnumbered_sw_if_index] = ~0;
612 ip6_main.lookup_main.if_address_pool_index_by_sw_if_index
613 [unnumbered_sw_if_index] = ~0;
616 if (was_unnum != (si->flags & VNET_SW_INTERFACE_FLAG_UNNUMBERED))
618 ip4_sw_interface_enable_disable (unnumbered_sw_if_index, mp->is_add);
619 ip6_sw_interface_enable_disable (unnumbered_sw_if_index, mp->is_add);
623 REPLY_MACRO (VL_API_SW_INTERFACE_SET_UNNUMBERED_REPLY);
627 vl_api_sw_interface_clear_stats_t_handler (vl_api_sw_interface_clear_stats_t *
630 vl_api_sw_interface_clear_stats_reply_t *rmp;
632 vnet_main_t *vnm = vnet_get_main ();
633 vnet_interface_main_t *im = &vnm->interface_main;
634 vlib_simple_counter_main_t *sm;
635 vlib_combined_counter_main_t *cm;
636 static vnet_main_t **my_vnet_mains;
637 int i, j, n_counters;
640 if (mp->sw_if_index != ~0)
641 VALIDATE_SW_IF_INDEX (mp);
643 vec_reset_length (my_vnet_mains);
645 for (i = 0; i < vec_len (vnet_mains); i++)
648 vec_add1 (my_vnet_mains, vnet_mains[i]);
651 if (vec_len (vnet_mains) == 0)
652 vec_add1 (my_vnet_mains, vnm);
654 n_counters = vec_len (im->combined_sw_if_counters);
656 for (j = 0; j < n_counters; j++)
658 for (i = 0; i < vec_len (my_vnet_mains); i++)
660 im = &my_vnet_mains[i]->interface_main;
661 cm = im->combined_sw_if_counters + j;
662 if (mp->sw_if_index == (u32) ~ 0)
663 vlib_clear_combined_counters (cm);
665 vlib_zero_combined_counter (cm, ntohl (mp->sw_if_index));
669 n_counters = vec_len (im->sw_if_counters);
671 for (j = 0; j < n_counters; j++)
673 for (i = 0; i < vec_len (my_vnet_mains); i++)
675 im = &my_vnet_mains[i]->interface_main;
676 sm = im->sw_if_counters + j;
677 if (mp->sw_if_index == (u32) ~ 0)
678 vlib_clear_simple_counters (sm);
680 vlib_zero_simple_counter (sm, ntohl (mp->sw_if_index));
684 BAD_SW_IF_INDEX_LABEL;
686 REPLY_MACRO (VL_API_SW_INTERFACE_CLEAR_STATS_REPLY);
689 #define API_LINK_STATE_EVENT 1
690 #define API_ADMIN_UP_DOWN_EVENT 2
693 event_data_cmp (void *a1, void *a2)
698 return (word) e1[0] - (word) e2[0];
702 send_sw_interface_event (vpe_api_main_t * am,
703 vpe_client_registration_t * reg,
704 unix_shared_memory_queue_t * q,
705 vnet_sw_interface_t * swif)
707 vl_api_sw_interface_event_t *mp;
708 vnet_main_t *vnm = am->vnet_main;
710 vnet_hw_interface_t *hi = vnet_get_sup_hw_interface (vnm,
712 mp = vl_msg_api_alloc (sizeof (*mp));
713 memset (mp, 0, sizeof (*mp));
714 mp->_vl_msg_id = ntohs (VL_API_SW_INTERFACE_EVENT);
715 mp->sw_if_index = ntohl (swif->sw_if_index);
716 mp->client_index = reg->client_index;
717 mp->pid = reg->client_pid;
719 mp->admin_up_down = (swif->flags & VNET_SW_INTERFACE_FLAG_ADMIN_UP) ? 1 : 0;
720 mp->link_up_down = (hi->flags & VNET_HW_INTERFACE_FLAG_LINK_UP) ? 1 : 0;
721 vl_msg_api_send_shmem (q, (u8 *) & mp);
725 link_state_process (vlib_main_t * vm,
726 vlib_node_runtime_t * rt, vlib_frame_t * f)
728 vpe_api_main_t *vam = &vpe_api_main;
729 vnet_main_t *vnm = vam->vnet_main;
730 vnet_sw_interface_t *swif;
731 uword *event_data = 0;
732 vpe_client_registration_t *reg;
734 u32 prev_sw_if_index;
735 unix_shared_memory_queue_t *q;
737 vam->link_state_process_up = 1;
741 vlib_process_wait_for_event (vm);
743 /* Unified list of changed link or admin state sw_if_indices */
744 vlib_process_get_events_with_type
745 (vm, &event_data, API_LINK_STATE_EVENT);
746 vlib_process_get_events_with_type
747 (vm, &event_data, API_ADMIN_UP_DOWN_EVENT);
749 /* Sort, so we can eliminate duplicates */
750 vec_sort_with_function (event_data, event_data_cmp);
752 prev_sw_if_index = ~0;
754 for (i = 0; i < vec_len (event_data); i++)
756 /* Only one message per swif */
757 if (prev_sw_if_index == event_data[i])
759 prev_sw_if_index = event_data[i];
762 pool_foreach(reg, vam->interface_events_registrations,
764 q = vl_api_client_index_to_input_queue (reg->client_index);
767 /* sw_interface may be deleted already */
768 if (!pool_is_free_index (vnm->interface_main.sw_interfaces,
771 swif = vnet_get_sw_interface (vnm, event_data[i]);
772 send_sw_interface_event (vam, reg, q, swif);
778 vec_reset_length (event_data);
784 static clib_error_t *link_up_down_function (vnet_main_t * vm, u32 hw_if_index,
786 static clib_error_t *admin_up_down_function (vnet_main_t * vm,
787 u32 hw_if_index, u32 flags);
790 VLIB_REGISTER_NODE (link_state_process_node,static) = {
791 .function = link_state_process,
792 .type = VLIB_NODE_TYPE_PROCESS,
793 .name = "vpe-link-state-process",
797 VNET_SW_INTERFACE_ADMIN_UP_DOWN_FUNCTION (admin_up_down_function);
798 VNET_HW_INTERFACE_LINK_UP_DOWN_FUNCTION (link_up_down_function);
800 static clib_error_t *
801 link_up_down_function (vnet_main_t * vm, u32 hw_if_index, u32 flags)
803 vpe_api_main_t *vam = &vpe_api_main;
804 vnet_hw_interface_t *hi = vnet_get_hw_interface (vm, hw_if_index);
806 if (vam->link_state_process_up)
807 vlib_process_signal_event (vam->vlib_main,
808 link_state_process_node.index,
809 API_LINK_STATE_EVENT, hi->sw_if_index);
813 static clib_error_t *
814 admin_up_down_function (vnet_main_t * vm, u32 sw_if_index, u32 flags)
816 vpe_api_main_t *vam = &vpe_api_main;
819 * Note: it's perfectly fair to set a subif admin up / admin down.
820 * Note the subtle distinction between this routine and the previous
823 if (vam->link_state_process_up)
824 vlib_process_signal_event (vam->vlib_main,
825 link_state_process_node.index,
826 API_ADMIN_UP_DOWN_EVENT, sw_if_index);
830 static void vl_api_sw_interface_tag_add_del_t_handler
831 (vl_api_sw_interface_tag_add_del_t * mp)
833 vnet_main_t *vnm = vnet_get_main ();
834 vl_api_sw_interface_tag_add_del_reply_t *rmp;
837 u32 sw_if_index = ntohl (mp->sw_if_index);
839 VALIDATE_SW_IF_INDEX (mp);
845 rv = VNET_API_ERROR_INVALID_VALUE;
849 mp->tag[ARRAY_LEN (mp->tag) - 1] = 0;
850 tag = format (0, "%s%c", mp->tag, 0);
851 vnet_set_sw_interface_tag (vnm, tag, sw_if_index);
854 vnet_clear_sw_interface_tag (vnm, sw_if_index);
856 BAD_SW_IF_INDEX_LABEL;
858 REPLY_MACRO (VL_API_SW_INTERFACE_TAG_ADD_DEL_REPLY);
861 static void vl_api_sw_interface_set_mac_address_t_handler
862 (vl_api_sw_interface_set_mac_address_t * mp)
864 vl_api_sw_interface_set_mac_address_reply_t *rmp;
865 vnet_main_t *vnm = vnet_get_main ();
866 u32 sw_if_index = ntohl (mp->sw_if_index);
871 VALIDATE_SW_IF_INDEX (mp);
873 mac = ((u64) mp->mac_address[0] << (8 * 0)
874 | (u64) mp->mac_address[1] << (8 * 1)
875 | (u64) mp->mac_address[2] << (8 * 2)
876 | (u64) mp->mac_address[3] << (8 * 3)
877 | (u64) mp->mac_address[4] << (8 * 4)
878 | (u64) mp->mac_address[5] << (8 * 5));
880 error = vnet_hw_interface_change_mac_address (vnm, sw_if_index, mac);
883 rv = VNET_API_ERROR_UNIMPLEMENTED;
884 clib_error_report (error);
888 BAD_SW_IF_INDEX_LABEL;
890 REPLY_MACRO (VL_API_SW_INTERFACE_SET_MAC_ADDRESS_REPLY);
895 * Add vpe's API message handlers to the table.
896 * vlib has alread mapped shared memory and
897 * added the client registration handlers.
898 * See .../vlib-api/vlibmemory/memclnt_vlib.c:memclnt_process()
900 #define vl_msg_name_crc_list
901 #include <vnet/interface.api.h>
902 #undef vl_msg_name_crc_list
905 setup_message_id_table (api_main_t * am)
907 #define _(id,n,crc) vl_msg_api_add_msg_name_crc (am, #n "_" #crc, id);
908 foreach_vl_msg_name_crc_interface;
912 pub_sub_handler (interface_events, INTERFACE_EVENTS);
914 static clib_error_t *
915 interface_api_hookup (vlib_main_t * vm)
917 api_main_t *am = &api_main;
920 vl_msg_api_set_handlers(VL_API_##N, #n, \
921 vl_api_##n##_t_handler, \
923 vl_api_##n##_t_endian, \
924 vl_api_##n##_t_print, \
925 sizeof(vl_api_##n##_t), 1);
930 * Set up the (msg_name, crc, message-id) table
932 setup_message_id_table (am);
937 VLIB_API_INIT_FUNCTION (interface_api_hookup);
940 * fd.io coding-style-patch-verification: ON
943 * eval: (c-set-style "gnu")