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/l2/l2_vtr.h>
29 #include <vnet/vnet_msg_enum.h>
30 #include <vnet/fib/fib_api.h>
32 #define vl_typedefs /* define message structures */
33 #include <vnet/vnet_all_api_h.h>
36 #define vl_endianfun /* define message structures */
37 #include <vnet/vnet_all_api_h.h>
40 /* instantiate all the print functions we know about */
41 #define vl_print(handle, ...) vlib_cli_output (handle, __VA_ARGS__)
43 #include <vnet/vnet_all_api_h.h>
46 #include <vlibapi/api_helper_macros.h>
48 #define foreach_vpe_api_msg \
49 _(SW_INTERFACE_SET_FLAGS, sw_interface_set_flags) \
50 _(SW_INTERFACE_SET_MTU, sw_interface_set_mtu) \
51 _(WANT_INTERFACE_EVENTS, want_interface_events) \
52 _(SW_INTERFACE_DUMP, sw_interface_dump) \
53 _(SW_INTERFACE_DETAILS, sw_interface_details) \
54 _(SW_INTERFACE_ADD_DEL_ADDRESS, sw_interface_add_del_address) \
55 _(SW_INTERFACE_SET_TABLE, sw_interface_set_table) \
56 _(SW_INTERFACE_GET_TABLE, sw_interface_get_table) \
57 _(SW_INTERFACE_SET_UNNUMBERED, sw_interface_set_unnumbered) \
58 _(SW_INTERFACE_CLEAR_STATS, sw_interface_clear_stats) \
59 _(SW_INTERFACE_TAG_ADD_DEL, sw_interface_tag_add_del)
62 vl_api_sw_interface_set_flags_t_handler (vl_api_sw_interface_set_flags_t * mp)
64 vl_api_sw_interface_set_flags_reply_t *rmp;
65 vnet_main_t *vnm = vnet_get_main ();
70 VALIDATE_SW_IF_INDEX (mp);
72 flags = mp->admin_up_down ? VNET_SW_INTERFACE_FLAG_ADMIN_UP : 0;
74 error = vnet_sw_interface_set_flags (vnm, ntohl (mp->sw_if_index), flags);
78 clib_error_report (error);
81 BAD_SW_IF_INDEX_LABEL;
82 REPLY_MACRO (VL_API_SW_INTERFACE_SET_FLAGS_REPLY);
86 vl_api_sw_interface_set_mtu_t_handler (vl_api_sw_interface_set_mtu_t * mp)
88 vl_api_sw_interface_set_mtu_reply_t *rmp;
89 vnet_main_t *vnm = vnet_get_main ();
90 u32 flags = ETHERNET_INTERFACE_FLAG_MTU;
91 u32 sw_if_index = ntohl (mp->sw_if_index);
92 u16 mtu = ntohs (mp->mtu);
93 ethernet_main_t *em = ðernet_main;
96 VALIDATE_SW_IF_INDEX (mp);
98 vnet_hw_interface_t *hi = vnet_get_hw_interface (vnm, sw_if_index);
99 ethernet_interface_t *eif = ethernet_get_interface (em, sw_if_index);
103 rv = VNET_API_ERROR_FEATURE_DISABLED;
104 goto bad_sw_if_index;
107 if (mtu < hi->min_supported_packet_bytes)
109 rv = VNET_API_ERROR_INVALID_VALUE;
110 goto bad_sw_if_index;
113 if (mtu > hi->max_supported_packet_bytes)
115 rv = VNET_API_ERROR_INVALID_VALUE;
116 goto bad_sw_if_index;
119 if (hi->max_packet_bytes != mtu)
121 hi->max_packet_bytes = mtu;
122 ethernet_set_flags (vnm, sw_if_index, flags);
125 BAD_SW_IF_INDEX_LABEL;
126 REPLY_MACRO (VL_API_SW_INTERFACE_SET_MTU_REPLY);
130 send_sw_interface_details (vpe_api_main_t * am,
131 unix_shared_memory_queue_t * q,
132 vnet_sw_interface_t * swif,
133 u8 * interface_name, u32 context)
135 vl_api_sw_interface_details_t *mp;
136 vnet_main_t *vnm = vnet_get_main ();
137 vnet_hw_interface_t *hi;
140 hi = vnet_get_sup_hw_interface (am->vnet_main, swif->sw_if_index);
142 mp = vl_msg_api_alloc (sizeof (*mp));
143 memset (mp, 0, sizeof (*mp));
144 mp->_vl_msg_id = ntohs (VL_API_SW_INTERFACE_DETAILS);
145 mp->sw_if_index = ntohl (swif->sw_if_index);
146 mp->sup_sw_if_index = ntohl (swif->sup_sw_if_index);
147 mp->admin_up_down = (swif->flags & VNET_SW_INTERFACE_FLAG_ADMIN_UP) ? 1 : 0;
148 mp->link_up_down = (hi->flags & VNET_HW_INTERFACE_FLAG_LINK_UP) ? 1 : 0;
149 mp->link_duplex = ((hi->flags & VNET_HW_INTERFACE_FLAG_DUPLEX_MASK) >>
150 VNET_HW_INTERFACE_FLAG_DUPLEX_SHIFT);
151 mp->link_speed = ((hi->flags & VNET_HW_INTERFACE_FLAG_SPEED_MASK) >>
152 VNET_HW_INTERFACE_FLAG_SPEED_SHIFT);
153 mp->link_mtu = ntohs (hi->max_packet_bytes);
154 mp->context = context;
156 strncpy ((char *) mp->interface_name,
157 (char *) interface_name, ARRAY_LEN (mp->interface_name) - 1);
159 /* Send the L2 address for ethernet physical intfcs */
160 if (swif->sup_sw_if_index == swif->sw_if_index
161 && hi->hw_class_index == ethernet_hw_interface_class.index)
163 ethernet_main_t *em = ethernet_get_main (am->vlib_main);
164 ethernet_interface_t *ei;
166 ei = pool_elt_at_index (em->interfaces, hi->hw_instance);
167 ASSERT (sizeof (mp->l2_address) >= sizeof (ei->address));
168 clib_memcpy (mp->l2_address, ei->address, sizeof (ei->address));
169 mp->l2_address_length = ntohl (sizeof (ei->address));
171 else if (swif->sup_sw_if_index != swif->sw_if_index)
173 vnet_sub_interface_t *sub = &swif->sub;
174 mp->sub_id = ntohl (sub->id);
175 mp->sub_dot1ad = sub->eth.flags.dot1ad;
176 mp->sub_number_of_tags =
177 sub->eth.flags.one_tag + sub->eth.flags.two_tags * 2;
178 mp->sub_outer_vlan_id = ntohs (sub->eth.outer_vlan_id);
179 mp->sub_inner_vlan_id = ntohs (sub->eth.inner_vlan_id);
180 mp->sub_exact_match = sub->eth.flags.exact_match;
181 mp->sub_default = sub->eth.flags.default_sub;
182 mp->sub_outer_vlan_id_any = sub->eth.flags.outer_vlan_id_any;
183 mp->sub_inner_vlan_id_any = sub->eth.flags.inner_vlan_id_any;
185 /* vlan tag rewrite data */
186 u32 vtr_op = L2_VTR_DISABLED;
187 u32 vtr_push_dot1q = 0, vtr_tag1 = 0, vtr_tag2 = 0;
189 if (l2vtr_get (am->vlib_main, am->vnet_main, swif->sw_if_index,
190 &vtr_op, &vtr_push_dot1q, &vtr_tag1, &vtr_tag2) != 0)
192 // error - default to disabled
193 mp->vtr_op = ntohl (L2_VTR_DISABLED);
194 clib_warning ("cannot get vlan tag rewrite for sw_if_index %d",
199 mp->vtr_op = ntohl (vtr_op);
200 mp->vtr_push_dot1q = ntohl (vtr_push_dot1q);
201 mp->vtr_tag1 = ntohl (vtr_tag1);
202 mp->vtr_tag2 = ntohl (vtr_tag2);
206 tag = vnet_get_sw_interface_tag (vnm, swif->sw_if_index);
208 strncpy ((char *) mp->tag, (char *) tag, ARRAY_LEN (mp->tag) - 1);
210 vl_msg_api_send_shmem (q, (u8 *) & mp);
214 vl_api_sw_interface_dump_t_handler (vl_api_sw_interface_dump_t * mp)
216 vpe_api_main_t *am = &vpe_api_main;
217 vnet_sw_interface_t *swif;
218 vnet_interface_main_t *im = &am->vnet_main->interface_main;
219 u8 *filter_string = 0, *name_string = 0;
220 unix_shared_memory_queue_t *q;
221 char *strcasestr (char *, char *); /* lnx hdr file botch */
223 q = vl_api_client_index_to_input_queue (mp->client_index);
228 if (mp->name_filter_valid)
230 mp->name_filter[ARRAY_LEN (mp->name_filter) - 1] = 0;
231 filter_string = format (0, "%s%c", mp->name_filter, 0);
235 pool_foreach (swif, im->sw_interfaces,
237 name_string = format (name_string, "%U%c",
238 format_vnet_sw_interface_name,
239 am->vnet_main, swif, 0);
241 if (mp->name_filter_valid == 0 ||
242 strcasestr((char *) name_string, (char *) filter_string)) {
244 send_sw_interface_details (am, q, swif, name_string, mp->context);
246 _vec_len (name_string) = 0;
250 vec_free (name_string);
251 vec_free (filter_string);
255 vl_api_sw_interface_add_del_address_t_handler
256 (vl_api_sw_interface_add_del_address_t * mp)
258 vlib_main_t *vm = vlib_get_main ();
259 vl_api_sw_interface_add_del_address_reply_t *rmp;
263 VALIDATE_SW_IF_INDEX (mp);
265 is_del = mp->is_add == 0;
268 ip_del_all_interface_addresses (vm, ntohl (mp->sw_if_index));
269 else if (mp->is_ipv6)
270 ip6_add_del_interface_address (vm, ntohl (mp->sw_if_index),
271 (void *) mp->address,
272 mp->address_length, is_del);
274 ip4_add_del_interface_address (vm, ntohl (mp->sw_if_index),
275 (void *) mp->address,
276 mp->address_length, is_del);
278 BAD_SW_IF_INDEX_LABEL;
280 REPLY_MACRO (VL_API_SW_INTERFACE_ADD_DEL_ADDRESS_REPLY);
283 void stats_dslock_with_hint (int hint, int tag) __attribute__ ((weak));
285 stats_dslock_with_hint (int hint, int tag)
289 void stats_dsunlock (void) __attribute__ ((weak));
291 stats_dsunlock (void)
296 vl_api_sw_interface_set_table_t_handler (vl_api_sw_interface_set_table_t * mp)
299 u32 table_id = ntohl (mp->vrf_id);
300 u32 sw_if_index = ntohl (mp->sw_if_index);
301 vl_api_sw_interface_set_table_reply_t *rmp;
304 VALIDATE_SW_IF_INDEX (mp);
306 stats_dslock_with_hint (1 /* release hint */ , 4 /* tag */ );
310 fib_index = fib_table_find_or_create_and_lock (FIB_PROTOCOL_IP6,
313 vec_validate (ip6_main.fib_index_by_sw_if_index, sw_if_index);
314 ip6_main.fib_index_by_sw_if_index[sw_if_index] = fib_index;
319 fib_index = fib_table_find_or_create_and_lock (FIB_PROTOCOL_IP4,
322 vec_validate (ip4_main.fib_index_by_sw_if_index, sw_if_index);
323 ip4_main.fib_index_by_sw_if_index[sw_if_index] = fib_index;
327 BAD_SW_IF_INDEX_LABEL;
329 REPLY_MACRO (VL_API_SW_INTERFACE_SET_TABLE_REPLY);
333 send_sw_interface_get_table_reply (unix_shared_memory_queue_t * q,
334 u32 context, int retval, u32 vrf_id)
336 vl_api_sw_interface_get_table_reply_t *mp;
338 mp = vl_msg_api_alloc (sizeof (*mp));
339 memset (mp, 0, sizeof (*mp));
340 mp->_vl_msg_id = ntohs (VL_API_SW_INTERFACE_GET_TABLE_REPLY);
341 mp->context = context;
342 mp->retval = htonl (retval);
343 mp->vrf_id = htonl (vrf_id);
345 vl_msg_api_send_shmem (q, (u8 *) & mp);
349 vl_api_sw_interface_get_table_t_handler (vl_api_sw_interface_get_table_t * mp)
351 unix_shared_memory_queue_t *q;
352 fib_table_t *fib_table = 0;
353 u32 sw_if_index = ~0;
356 fib_protocol_t fib_proto = FIB_PROTOCOL_IP4;
359 q = vl_api_client_index_to_input_queue (mp->client_index);
363 VALIDATE_SW_IF_INDEX (mp);
365 sw_if_index = ntohl (mp->sw_if_index);
368 fib_proto = FIB_PROTOCOL_IP6;
370 fib_index = fib_table_get_index_for_sw_if_index (fib_proto, sw_if_index);
373 fib_table = fib_table_get (fib_index, fib_proto);
374 table_id = fib_table->ft_table_id;
377 BAD_SW_IF_INDEX_LABEL;
379 send_sw_interface_get_table_reply (q, mp->context, rv, table_id);
382 static void vl_api_sw_interface_set_unnumbered_t_handler
383 (vl_api_sw_interface_set_unnumbered_t * mp)
385 vl_api_sw_interface_set_unnumbered_reply_t *rmp;
387 vnet_sw_interface_t *si;
388 vnet_main_t *vnm = vnet_get_main ();
389 u32 sw_if_index, unnumbered_sw_if_index;
391 sw_if_index = ntohl (mp->sw_if_index);
392 unnumbered_sw_if_index = ntohl (mp->unnumbered_sw_if_index);
395 * The API message field names are backwards from
396 * the underlying data structure names.
397 * It's not worth changing them now.
399 if (pool_is_free_index (vnm->interface_main.sw_interfaces,
400 unnumbered_sw_if_index))
402 rv = VNET_API_ERROR_INVALID_SW_IF_INDEX;
406 /* Only check the "use loop0" field when setting the binding */
408 pool_is_free_index (vnm->interface_main.sw_interfaces, sw_if_index))
410 rv = VNET_API_ERROR_INVALID_SW_IF_INDEX_2;
414 si = vnet_get_sw_interface (vnm, unnumbered_sw_if_index);
418 si->flags |= VNET_SW_INTERFACE_FLAG_UNNUMBERED;
419 si->unnumbered_sw_if_index = sw_if_index;
420 ip4_sw_interface_enable_disable (unnumbered_sw_if_index, 1);
421 ip6_sw_interface_enable_disable (unnumbered_sw_if_index, 1);
425 si->flags &= ~(VNET_SW_INTERFACE_FLAG_UNNUMBERED);
426 si->unnumbered_sw_if_index = (u32) ~ 0;
427 ip4_sw_interface_enable_disable (unnumbered_sw_if_index, 0);
428 ip6_sw_interface_enable_disable (unnumbered_sw_if_index, 0);
432 REPLY_MACRO (VL_API_SW_INTERFACE_SET_UNNUMBERED_REPLY);
436 vl_api_sw_interface_clear_stats_t_handler (vl_api_sw_interface_clear_stats_t *
439 vl_api_sw_interface_clear_stats_reply_t *rmp;
441 vnet_main_t *vnm = vnet_get_main ();
442 vnet_interface_main_t *im = &vnm->interface_main;
443 vlib_simple_counter_main_t *sm;
444 vlib_combined_counter_main_t *cm;
445 static vnet_main_t **my_vnet_mains;
446 int i, j, n_counters;
449 if (mp->sw_if_index != ~0)
450 VALIDATE_SW_IF_INDEX (mp);
452 vec_reset_length (my_vnet_mains);
454 for (i = 0; i < vec_len (vnet_mains); i++)
457 vec_add1 (my_vnet_mains, vnet_mains[i]);
460 if (vec_len (vnet_mains) == 0)
461 vec_add1 (my_vnet_mains, vnm);
463 n_counters = vec_len (im->combined_sw_if_counters);
465 for (j = 0; j < n_counters; j++)
467 for (i = 0; i < vec_len (my_vnet_mains); i++)
469 im = &my_vnet_mains[i]->interface_main;
470 cm = im->combined_sw_if_counters + j;
471 if (mp->sw_if_index == (u32) ~ 0)
472 vlib_clear_combined_counters (cm);
474 vlib_zero_combined_counter (cm, ntohl (mp->sw_if_index));
478 n_counters = vec_len (im->sw_if_counters);
480 for (j = 0; j < n_counters; j++)
482 for (i = 0; i < vec_len (my_vnet_mains); i++)
484 im = &my_vnet_mains[i]->interface_main;
485 sm = im->sw_if_counters + j;
486 if (mp->sw_if_index == (u32) ~ 0)
487 vlib_clear_simple_counters (sm);
489 vlib_zero_simple_counter (sm, ntohl (mp->sw_if_index));
493 BAD_SW_IF_INDEX_LABEL;
495 REPLY_MACRO (VL_API_SW_INTERFACE_CLEAR_STATS_REPLY);
498 #define API_LINK_STATE_EVENT 1
499 #define API_ADMIN_UP_DOWN_EVENT 2
502 event_data_cmp (void *a1, void *a2)
507 return (word) e1[0] - (word) e2[0];
511 send_sw_interface_flags (vpe_api_main_t * am,
512 unix_shared_memory_queue_t * q,
513 vnet_sw_interface_t * swif)
515 vl_api_sw_interface_set_flags_t *mp;
516 vnet_main_t *vnm = am->vnet_main;
518 vnet_hw_interface_t *hi = vnet_get_sup_hw_interface (vnm,
520 mp = vl_msg_api_alloc (sizeof (*mp));
521 memset (mp, 0, sizeof (*mp));
522 mp->_vl_msg_id = ntohs (VL_API_SW_INTERFACE_SET_FLAGS);
523 mp->sw_if_index = ntohl (swif->sw_if_index);
525 mp->admin_up_down = (swif->flags & VNET_SW_INTERFACE_FLAG_ADMIN_UP) ? 1 : 0;
526 mp->link_up_down = (hi->flags & VNET_HW_INTERFACE_FLAG_LINK_UP) ? 1 : 0;
527 vl_msg_api_send_shmem (q, (u8 *) & mp);
531 link_state_process (vlib_main_t * vm,
532 vlib_node_runtime_t * rt, vlib_frame_t * f)
534 vpe_api_main_t *vam = &vpe_api_main;
535 vnet_main_t *vnm = vam->vnet_main;
536 vnet_sw_interface_t *swif;
537 uword *event_data = 0;
538 vpe_client_registration_t *reg;
540 u32 prev_sw_if_index;
541 unix_shared_memory_queue_t *q;
543 vam->link_state_process_up = 1;
547 vlib_process_wait_for_event (vm);
549 /* Unified list of changed link or admin state sw_if_indices */
550 vlib_process_get_events_with_type
551 (vm, &event_data, API_LINK_STATE_EVENT);
552 vlib_process_get_events_with_type
553 (vm, &event_data, API_ADMIN_UP_DOWN_EVENT);
555 /* Sort, so we can eliminate duplicates */
556 vec_sort_with_function (event_data, event_data_cmp);
558 prev_sw_if_index = ~0;
560 for (i = 0; i < vec_len (event_data); i++)
562 /* Only one message per swif */
563 if (prev_sw_if_index == event_data[i])
565 prev_sw_if_index = event_data[i];
568 pool_foreach(reg, vam->interface_events_registrations,
570 q = vl_api_client_index_to_input_queue (reg->client_index);
573 /* sw_interface may be deleted already */
574 if (!pool_is_free_index (vnm->interface_main.sw_interfaces,
577 swif = vnet_get_sw_interface (vnm, event_data[i]);
578 send_sw_interface_flags (vam, q, swif);
584 vec_reset_length (event_data);
590 static clib_error_t *link_up_down_function (vnet_main_t * vm, u32 hw_if_index,
592 static clib_error_t *admin_up_down_function (vnet_main_t * vm,
593 u32 hw_if_index, u32 flags);
596 VLIB_REGISTER_NODE (link_state_process_node,static) = {
597 .function = link_state_process,
598 .type = VLIB_NODE_TYPE_PROCESS,
599 .name = "vpe-link-state-process",
603 VNET_SW_INTERFACE_ADMIN_UP_DOWN_FUNCTION (admin_up_down_function);
604 VNET_HW_INTERFACE_LINK_UP_DOWN_FUNCTION (link_up_down_function);
606 static clib_error_t *
607 link_up_down_function (vnet_main_t * vm, u32 hw_if_index, u32 flags)
609 vpe_api_main_t *vam = &vpe_api_main;
610 vnet_hw_interface_t *hi = vnet_get_hw_interface (vm, hw_if_index);
612 if (vam->link_state_process_up)
613 vlib_process_signal_event (vam->vlib_main,
614 link_state_process_node.index,
615 API_LINK_STATE_EVENT, hi->sw_if_index);
619 static clib_error_t *
620 admin_up_down_function (vnet_main_t * vm, u32 sw_if_index, u32 flags)
622 vpe_api_main_t *vam = &vpe_api_main;
625 * Note: it's perfectly fair to set a subif admin up / admin down.
626 * Note the subtle distinction between this routine and the previous
629 if (vam->link_state_process_up)
630 vlib_process_signal_event (vam->vlib_main,
631 link_state_process_node.index,
632 API_ADMIN_UP_DOWN_EVENT, sw_if_index);
636 static void vl_api_sw_interface_tag_add_del_t_handler
637 (vl_api_sw_interface_tag_add_del_t * mp)
639 vnet_main_t *vnm = vnet_get_main ();
640 vl_api_sw_interface_tag_add_del_reply_t *rmp;
643 u32 sw_if_index = ntohl (mp->sw_if_index);
645 VALIDATE_SW_IF_INDEX (mp);
651 rv = VNET_API_ERROR_INVALID_VALUE;
655 mp->tag[ARRAY_LEN (mp->tag) - 1] = 0;
656 tag = format (0, "%s%c", mp->tag, 0);
657 vnet_set_sw_interface_tag (vnm, tag, sw_if_index);
660 vnet_clear_sw_interface_tag (vnm, sw_if_index);
662 BAD_SW_IF_INDEX_LABEL;
664 REPLY_MACRO (VL_API_SW_INTERFACE_TAG_ADD_DEL_REPLY);
668 vl_api_sw_interface_details_t_handler (vl_api_sw_interface_details_t * mp)
670 clib_warning ("BUG");
675 * Add vpe's API message handlers to the table.
676 * vlib has alread mapped shared memory and
677 * added the client registration handlers.
678 * See .../vlib-api/vlibmemory/memclnt_vlib.c:memclnt_process()
680 #define vl_msg_name_crc_list
681 #include <vnet/interface.api.h>
682 #undef vl_msg_name_crc_list
685 setup_message_id_table (api_main_t * am)
687 #define _(id,n,crc) vl_msg_api_add_msg_name_crc (am, #n "_" #crc, id);
688 foreach_vl_msg_name_crc_interface;
692 pub_sub_handler (interface_events, INTERFACE_EVENTS);
694 static clib_error_t *
695 interface_api_hookup (vlib_main_t * vm)
697 api_main_t *am = &api_main;
700 vl_msg_api_set_handlers(VL_API_##N, #n, \
701 vl_api_##n##_t_handler, \
703 vl_api_##n##_t_endian, \
704 vl_api_##n##_t_print, \
705 sizeof(vl_api_##n##_t), 1);
710 * Set up the (msg_name, crc, message-id) table
712 setup_message_id_table (am);
717 VLIB_API_INIT_FUNCTION (interface_api_hookup);
720 * fd.io coding-style-patch-verification: ON
723 * eval: (c-set-style "gnu")