2 *------------------------------------------------------------------
3 * api.c - message handler registration
5 * Copyright (c) 2010-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 *------------------------------------------------------------------
23 #include <sys/types.h>
26 #include <netinet/in.h>
36 #include <vppinfra/clib.h>
37 #include <vppinfra/vec.h>
38 #include <vppinfra/hash.h>
39 #include <vppinfra/bitmap.h>
40 #include <vppinfra/fifo.h>
41 #include <vppinfra/time.h>
42 #include <vppinfra/mheap.h>
43 #include <vppinfra/heap.h>
44 #include <vppinfra/pool.h>
45 #include <vppinfra/format.h>
46 #include <vppinfra/error.h>
48 #include <vnet/api_errno.h>
49 #include <vnet/vnet.h>
50 #include <vnet/l2/l2_input.h>
51 #include <vnet/l2/l2_bd.h>
52 #include <vnet/ip/ip.h>
53 #include <vnet/ip/ip6.h>
54 #include <vnet/ip/ip6_neighbor.h>
56 #include <vnet/srv6/sr.h>
58 #include <vlib/vlib.h>
59 #include <vlib/unix/unix.h>
60 #include <vlibapi/api.h>
61 #include <vlibmemory/api.h>
62 #include <vnet/classify/input_acl.h>
63 #include <vnet/l2/l2_classify.h>
64 #include <vnet/map/map.h>
65 #include <vnet/ip/ip6_hop_by_hop.h>
66 #include <vnet/ip/ip_source_and_port_range_check.h>
67 #include <vnet/ip/punt.h>
68 #include <vnet/feature/feature.h>
71 #undef __included_bihash_template_h__
72 #include <vnet/l2/l2_fib.h>
74 #include <vpp/stats/stats.h>
75 #include <vpp/oam/oam.h>
77 #include <vnet/ethernet/ethernet.h>
78 #include <vnet/ethernet/arp_packet.h>
79 #include <vnet/interface.h>
80 #include <vnet/l2/l2_fib.h>
81 #include <vnet/l2/l2_bd.h>
82 #include <vpp/api/vpe_msg_enum.h>
83 #include <vnet/span/span.h>
84 #include <vnet/fib/fib_api.h>
85 #include <vnet/dpo/drop_dpo.h>
86 #include <vnet/dpo/receive_dpo.h>
87 #include <vnet/dpo/lookup_dpo.h>
88 #include <vnet/dpo/classify_dpo.h>
89 #include <vnet/dpo/ip_null_dpo.h>
90 #define vl_typedefs /* define message structures */
91 #include <vpp/api/vpe_all_api_h.h>
93 #define vl_endianfun /* define message structures */
94 #include <vpp/api/vpe_all_api_h.h>
96 /* instantiate all the print functions we know about */
97 #define vl_print(handle, ...) vlib_cli_output (handle, __VA_ARGS__)
99 #include <vpp/api/vpe_all_api_h.h>
101 #include <vlibapi/api_helper_macros.h>
102 #define foreach_vpe_api_msg \
103 _(WANT_OAM_EVENTS, want_oam_events) \
104 _(OAM_ADD_DEL, oam_add_del) \
105 _(SW_INTERFACE_SET_MPLS_ENABLE, sw_interface_set_mpls_enable) \
106 _(SW_INTERFACE_SET_VPATH, sw_interface_set_vpath) \
107 _(SW_INTERFACE_SET_L2_XCONNECT, sw_interface_set_l2_xconnect) \
108 _(SW_INTERFACE_SET_L2_BRIDGE, sw_interface_set_l2_bridge) \
109 _(CREATE_VLAN_SUBIF, create_vlan_subif) \
110 _(CREATE_SUBIF, create_subif) \
111 _(PROXY_ARP_ADD_DEL, proxy_arp_add_del) \
112 _(PROXY_ARP_INTFC_ENABLE_DISABLE, proxy_arp_intfc_enable_disable) \
113 _(RESET_FIB, reset_fib) \
114 _(CREATE_LOOPBACK, create_loopback) \
115 _(CREATE_LOOPBACK_INSTANCE, create_loopback_instance) \
116 _(CONTROL_PING, control_ping) \
118 _(CLI_INBAND, cli_inband) \
119 _(SET_ARP_NEIGHBOR_LIMIT, set_arp_neighbor_limit) \
120 _(L2_PATCH_ADD_DEL, l2_patch_add_del) \
121 _(CLASSIFY_SET_INTERFACE_IP_TABLE, classify_set_interface_ip_table) \
122 _(CLASSIFY_SET_INTERFACE_L2_TABLES, classify_set_interface_l2_tables) \
123 _(GET_NODE_INDEX, get_node_index) \
124 _(ADD_NODE_NEXT, add_node_next) \
125 _(L2_INTERFACE_EFP_FILTER, l2_interface_efp_filter) \
126 _(SHOW_VERSION, show_version) \
127 _(INTERFACE_NAME_RENUMBER, interface_name_renumber) \
128 _(WANT_IP4_ARP_EVENTS, want_ip4_arp_events) \
129 _(WANT_IP6_ND_EVENTS, want_ip6_nd_events) \
130 _(INPUT_ACL_SET_INTERFACE, input_acl_set_interface) \
131 _(DELETE_LOOPBACK, delete_loopback) \
132 _(BD_IP_MAC_ADD_DEL, bd_ip_mac_add_del) \
133 _(GET_NODE_GRAPH, get_node_graph) \
134 _(IOAM_ENABLE, ioam_enable) \
135 _(IOAM_DISABLE, ioam_disable) \
136 _(GET_NEXT_INDEX, get_next_index) \
137 _(PG_CREATE_INTERFACE, pg_create_interface) \
138 _(PG_CAPTURE, pg_capture) \
139 _(PG_ENABLE_DISABLE, pg_enable_disable) \
140 _(IP_SOURCE_AND_PORT_RANGE_CHECK_ADD_DEL, \
141 ip_source_and_port_range_check_add_del) \
142 _(IP_SOURCE_AND_PORT_RANGE_CHECK_INTERFACE_ADD_DEL, \
143 ip_source_and_port_range_check_interface_add_del) \
144 _(DELETE_SUBIF, delete_subif) \
146 _(PUNT_SOCKET_REGISTER, punt_socket_register) \
147 _(PUNT_SOCKET_DEREGISTER, punt_socket_deregister) \
148 _(FEATURE_ENABLE_DISABLE, feature_enable_disable)
151 #define QUOTE(x) QUOTE_(x)
154 RESOLVE_IP4_ADD_DEL_ROUTE = 1,
155 RESOLVE_IP6_ADD_DEL_ROUTE,
158 static vlib_node_registration_t vpe_resolver_process_node;
159 extern vpe_api_main_t vpe_api_main;
161 static int arp_change_delete_callback (u32 pool_index, u8 * notused);
162 static int nd_change_delete_callback (u32 pool_index, u8 * notused);
164 /* Clean up all registrations belonging to the indicated client */
165 static clib_error_t *
166 memclnt_delete_callback (u32 client_index)
168 vpe_api_main_t *vam = &vpe_api_main;
169 vpe_client_registration_t *rp;
171 int stats_memclnt_delete_callback (u32 client_index);
173 stats_memclnt_delete_callback (client_index);
176 p = hash_get (vam->a##_registration_hash, client_index); \
178 rp = pool_elt_at_index (vam->a##_registrations, p[0]); \
179 pool_put (vam->a##_registrations, rp); \
180 hash_unset (vam->a##_registration_hash, client_index); \
182 foreach_registration_hash;
187 VL_MSG_API_REAPER_FUNCTION (memclnt_delete_callback);
189 pub_sub_handler (oam_events, OAM_EVENTS);
191 #define RESOLUTION_EVENT 1
192 #define RESOLUTION_PENDING_EVENT 2
193 #define IP4_ARP_EVENT 3
194 #define IP6_ND_EVENT 4
196 int ip4_add_del_route_t_handler (vl_api_ip_add_del_route_t * mp);
198 int ip6_add_del_route_t_handler (vl_api_ip_add_del_route_t * mp);
201 handle_ip4_arp_event (u32 pool_index)
203 vpe_api_main_t *vam = &vpe_api_main;
204 vnet_main_t *vnm = vam->vnet_main;
205 vlib_main_t *vm = vam->vlib_main;
206 vl_api_ip4_arp_event_t *event;
207 vl_api_ip4_arp_event_t *mp;
208 unix_shared_memory_queue_t *q;
210 /* Client can cancel, die, etc. */
211 if (pool_is_free_index (vam->arp_events, pool_index))
214 event = pool_elt_at_index (vam->arp_events, pool_index);
216 q = vl_api_client_index_to_input_queue (event->client_index);
219 (void) vnet_add_del_ip4_arp_change_event
220 (vnm, arp_change_delete_callback,
221 event->pid, &event->address,
222 vpe_resolver_process_node.index, IP4_ARP_EVENT,
223 ~0 /* pool index, notused */ , 0 /* is_add */ );
227 if (q->cursize < q->maxsize)
229 mp = vl_msg_api_alloc (sizeof (*mp));
230 clib_memcpy (mp, event, sizeof (*mp));
231 vl_msg_api_send_shmem (q, (u8 *) & mp);
235 static f64 last_time;
237 * Throttle syslog msgs.
238 * It's pretty tempting to just revoke the registration...
240 if (vlib_time_now (vm) > last_time + 10.0)
242 clib_warning ("arp event for %U to pid %d: queue stuffed!",
243 format_ip4_address, &event->address, event->pid);
244 last_time = vlib_time_now (vm);
250 handle_ip6_nd_event (u32 pool_index)
252 vpe_api_main_t *vam = &vpe_api_main;
253 vnet_main_t *vnm = vam->vnet_main;
254 vlib_main_t *vm = vam->vlib_main;
255 vl_api_ip6_nd_event_t *event;
256 vl_api_ip6_nd_event_t *mp;
257 unix_shared_memory_queue_t *q;
259 /* Client can cancel, die, etc. */
260 if (pool_is_free_index (vam->nd_events, pool_index))
263 event = pool_elt_at_index (vam->nd_events, pool_index);
265 q = vl_api_client_index_to_input_queue (event->client_index);
268 (void) vnet_add_del_ip6_nd_change_event
269 (vnm, nd_change_delete_callback,
270 event->pid, &event->address,
271 vpe_resolver_process_node.index, IP6_ND_EVENT,
272 ~0 /* pool index, notused */ , 0 /* is_add */ );
276 if (q->cursize < q->maxsize)
278 mp = vl_msg_api_alloc (sizeof (*mp));
279 clib_memcpy (mp, event, sizeof (*mp));
280 vl_msg_api_send_shmem (q, (u8 *) & mp);
284 static f64 last_time;
286 * Throttle syslog msgs.
287 * It's pretty tempting to just revoke the registration...
289 if (vlib_time_now (vm) > last_time + 10.0)
291 clib_warning ("ip6 nd event for %U to pid %d: queue stuffed!",
292 format_ip6_address, &event->address, event->pid);
293 last_time = vlib_time_now (vm);
299 resolver_process (vlib_main_t * vm,
300 vlib_node_runtime_t * rt, vlib_frame_t * f)
303 uword *event_data = 0;
309 vlib_process_wait_for_event_or_clock (vm, timeout);
311 event_type = vlib_process_get_events (vm, &event_data);
315 case RESOLUTION_PENDING_EVENT:
319 case RESOLUTION_EVENT:
320 clib_warning ("resolver: BOGUS TYPE");
324 for (i = 0; i < vec_len (event_data); i++)
325 handle_ip4_arp_event (event_data[i]);
329 for (i = 0; i < vec_len (event_data); i++)
330 handle_ip6_nd_event (event_data[i]);
333 case ~0: /* timeout */
337 vec_reset_length (event_data);
339 return 0; /* or not */
343 VLIB_REGISTER_NODE (vpe_resolver_process_node,static) = {
344 .function = resolver_process,
345 .type = VLIB_NODE_TYPE_PROCESS,
346 .name = "vpe-route-resolver-process",
351 vl_api_sw_interface_set_vpath_t_handler (vl_api_sw_interface_set_vpath_t * mp)
353 vl_api_sw_interface_set_vpath_reply_t *rmp;
355 u32 sw_if_index = ntohl (mp->sw_if_index);
357 VALIDATE_SW_IF_INDEX (mp);
359 l2input_intf_bitmap_enable (sw_if_index, L2INPUT_FEAT_VPATH, mp->enable);
360 vnet_feature_enable_disable ("ip4-unicast", "vpath-input-ip4",
361 sw_if_index, mp->enable, 0, 0);
362 vnet_feature_enable_disable ("ip4-multicast", "vpath-input-ip4",
363 sw_if_index, mp->enable, 0, 0);
364 vnet_feature_enable_disable ("ip6-unicast", "vpath-input-ip6",
365 sw_if_index, mp->enable, 0, 0);
366 vnet_feature_enable_disable ("ip6-multicast", "vpath-input-ip6",
367 sw_if_index, mp->enable, 0, 0);
369 BAD_SW_IF_INDEX_LABEL;
371 REPLY_MACRO (VL_API_SW_INTERFACE_SET_VPATH_REPLY);
375 vl_api_sw_interface_set_l2_xconnect_t_handler
376 (vl_api_sw_interface_set_l2_xconnect_t * mp)
378 vl_api_sw_interface_set_l2_xconnect_reply_t *rmp;
380 u32 rx_sw_if_index = ntohl (mp->rx_sw_if_index);
381 u32 tx_sw_if_index = ntohl (mp->tx_sw_if_index);
382 vlib_main_t *vm = vlib_get_main ();
383 vnet_main_t *vnm = vnet_get_main ();
385 VALIDATE_RX_SW_IF_INDEX (mp);
389 VALIDATE_TX_SW_IF_INDEX (mp);
390 rv = set_int_l2_mode (vm, vnm, MODE_L2_XC,
391 rx_sw_if_index, 0, 0, 0, tx_sw_if_index);
395 rv = set_int_l2_mode (vm, vnm, MODE_L3, rx_sw_if_index, 0, 0, 0, 0);
398 BAD_RX_SW_IF_INDEX_LABEL;
399 BAD_TX_SW_IF_INDEX_LABEL;
401 REPLY_MACRO (VL_API_SW_INTERFACE_SET_L2_XCONNECT_REPLY);
405 vl_api_sw_interface_set_l2_bridge_t_handler
406 (vl_api_sw_interface_set_l2_bridge_t * mp)
408 bd_main_t *bdm = &bd_main;
409 vl_api_sw_interface_set_l2_bridge_reply_t *rmp;
411 vlib_main_t *vm = vlib_get_main ();
412 vnet_main_t *vnm = vnet_get_main ();
414 VALIDATE_RX_SW_IF_INDEX (mp);
415 u32 rx_sw_if_index = ntohl (mp->rx_sw_if_index);
421 u32 bd_id = ntohl (mp->bd_id);
422 u32 bd_index = bd_find_or_add_bd_index (bdm, bd_id);
425 rv = set_int_l2_mode (vm, vnm, MODE_L2_BRIDGE,
426 rx_sw_if_index, bd_index, bvi, shg, 0);
430 rv = set_int_l2_mode (vm, vnm, MODE_L3, rx_sw_if_index, 0, 0, 0, 0);
433 BAD_RX_SW_IF_INDEX_LABEL;
436 REPLY_MACRO (VL_API_SW_INTERFACE_SET_L2_BRIDGE_REPLY);
440 vl_api_bd_ip_mac_add_del_t_handler (vl_api_bd_ip_mac_add_del_t * mp)
442 bd_main_t *bdm = &bd_main;
443 vl_api_bd_ip_mac_add_del_reply_t *rmp;
445 u32 bd_id = ntohl (mp->bd_id);
451 rv = VNET_API_ERROR_BD_NOT_MODIFIABLE;
455 p = hash_get (bdm->bd_index_by_bd_id, bd_id);
458 rv = VNET_API_ERROR_NO_SUCH_ENTRY;
463 if (bd_add_del_ip_mac (bd_index, mp->ip_address,
464 mp->mac_address, mp->is_ipv6, mp->is_add))
465 rv = VNET_API_ERROR_UNSPECIFIED;
468 REPLY_MACRO (VL_API_BD_IP_MAC_ADD_DEL_REPLY);
472 vl_api_create_vlan_subif_t_handler (vl_api_create_vlan_subif_t * mp)
474 vl_api_create_vlan_subif_reply_t *rmp;
475 vnet_main_t *vnm = vnet_get_main ();
476 u32 sw_if_index = (u32) ~ 0;
477 vnet_hw_interface_t *hi;
480 vnet_sw_interface_t template;
482 vnet_interface_main_t *im = &vnm->interface_main;
484 unix_shared_memory_queue_t *q;
487 VALIDATE_SW_IF_INDEX (mp);
489 hi = vnet_get_sup_hw_interface (vnm, ntohl (mp->sw_if_index));
491 if (hi->bond_info == VNET_HW_INTERFACE_BOND_INFO_SLAVE)
493 rv = VNET_API_ERROR_BOND_SLAVE_NOT_ALLOWED;
497 id = ntohl (mp->vlan_id);
498 if (id == 0 || id > 4095)
500 rv = VNET_API_ERROR_INVALID_VLAN;
504 sup_and_sub_key = ((u64) (hi->sw_if_index) << 32) | (u64) id;
506 p = hash_get_mem (im->sw_if_index_by_sup_and_sub, &sup_and_sub_key);
509 rv = VNET_API_ERROR_VLAN_ALREADY_EXISTS;
513 memset (&template, 0, sizeof (template));
514 template.type = VNET_SW_INTERFACE_TYPE_SUB;
515 template.sup_sw_if_index = hi->sw_if_index;
516 template.sub.id = id;
517 template.sub.eth.raw_flags = 0;
518 template.sub.eth.flags.one_tag = 1;
519 template.sub.eth.outer_vlan_id = id;
520 template.sub.eth.flags.exact_match = 1;
522 error = vnet_create_sw_interface (vnm, &template, &sw_if_index);
525 clib_error_report (error);
526 rv = VNET_API_ERROR_INVALID_REGISTRATION;
530 u64 *kp = clib_mem_alloc (sizeof (*kp));
531 *kp = sup_and_sub_key;
533 hash_set (hi->sub_interface_sw_if_index_by_id, id, sw_if_index);
534 hash_set_mem (im->sw_if_index_by_sup_and_sub, kp, sw_if_index);
536 BAD_SW_IF_INDEX_LABEL;
539 q = vl_api_client_index_to_input_queue (mp->client_index);
543 rmp = vl_msg_api_alloc (sizeof (*rmp));
544 rmp->_vl_msg_id = htons (VL_API_CREATE_VLAN_SUBIF_REPLY);
545 rmp->context = mp->context;
546 rmp->retval = htonl (rv);
547 rmp->sw_if_index = htonl (sw_if_index);
548 vl_msg_api_send_shmem (q, (u8 *) & rmp);
552 vl_api_create_subif_t_handler (vl_api_create_subif_t * mp)
554 vl_api_create_subif_reply_t *rmp;
555 vnet_main_t *vnm = vnet_get_main ();
556 u32 sw_if_index = ~0;
559 vnet_sw_interface_t *si;
560 vnet_hw_interface_t *hi;
561 vnet_sw_interface_t template;
563 vnet_interface_main_t *im = &vnm->interface_main;
567 VALIDATE_SW_IF_INDEX (mp);
569 si = vnet_get_sup_sw_interface (vnm, ntohl (mp->sw_if_index));
570 hi = vnet_get_sup_hw_interface (vnm, ntohl (mp->sw_if_index));
572 if (hi->bond_info == VNET_HW_INTERFACE_BOND_INFO_SLAVE)
574 rv = VNET_API_ERROR_BOND_SLAVE_NOT_ALLOWED;
578 sw_if_index = si->sw_if_index;
579 sub_id = ntohl (mp->sub_id);
581 sup_and_sub_key = ((u64) (sw_if_index) << 32) | (u64) sub_id;
583 p = hash_get_mem (im->sw_if_index_by_sup_and_sub, &sup_and_sub_key);
587 clib_warning ("sup sw_if_index %d, sub id %d already exists\n",
588 sw_if_index, sub_id);
589 rv = VNET_API_ERROR_SUBIF_ALREADY_EXISTS;
593 memset (&template, 0, sizeof (template));
594 template.type = VNET_SW_INTERFACE_TYPE_SUB;
595 template.sup_sw_if_index = sw_if_index;
596 template.sub.id = sub_id;
597 template.sub.eth.flags.no_tags = mp->no_tags;
598 template.sub.eth.flags.one_tag = mp->one_tag;
599 template.sub.eth.flags.two_tags = mp->two_tags;
600 template.sub.eth.flags.dot1ad = mp->dot1ad;
601 template.sub.eth.flags.exact_match = mp->exact_match;
602 template.sub.eth.flags.default_sub = mp->default_sub;
603 template.sub.eth.flags.outer_vlan_id_any = mp->outer_vlan_id_any;
604 template.sub.eth.flags.inner_vlan_id_any = mp->inner_vlan_id_any;
605 template.sub.eth.outer_vlan_id = ntohs (mp->outer_vlan_id);
606 template.sub.eth.inner_vlan_id = ntohs (mp->inner_vlan_id);
608 error = vnet_create_sw_interface (vnm, &template, &sw_if_index);
611 clib_error_report (error);
612 rv = VNET_API_ERROR_SUBIF_CREATE_FAILED;
616 u64 *kp = clib_mem_alloc (sizeof (*kp));
617 *kp = sup_and_sub_key;
619 hash_set (hi->sub_interface_sw_if_index_by_id, sub_id, sw_if_index);
620 hash_set_mem (im->sw_if_index_by_sup_and_sub, kp, sw_if_index);
622 BAD_SW_IF_INDEX_LABEL;
627 REPLY_MACRO2(VL_API_CREATE_SUBIF_REPLY,
629 rmp->sw_if_index = ntohl(sw_if_index);
635 vl_api_proxy_arp_add_del_t_handler (vl_api_proxy_arp_add_del_t * mp)
637 vl_api_proxy_arp_add_del_reply_t *rmp;
640 ip4_main_t *im = &ip4_main;
641 stats_main_t *sm = &stats_main;
642 int vnet_proxy_arp_add_del (ip4_address_t * lo_addr,
643 ip4_address_t * hi_addr,
644 u32 fib_index, int is_del);
647 dslock (sm, 1 /* release hint */ , 6 /* tag */ );
649 p = hash_get (im->fib_index_by_table_id, ntohl (mp->vrf_id));
653 rv = VNET_API_ERROR_NO_SUCH_FIB;
659 rv = vnet_proxy_arp_add_del ((ip4_address_t *) mp->low_address,
660 (ip4_address_t *) mp->hi_address,
661 fib_index, mp->is_add == 0);
665 REPLY_MACRO (VL_API_PROXY_ARP_ADD_DEL_REPLY);
669 vl_api_proxy_arp_intfc_enable_disable_t_handler
670 (vl_api_proxy_arp_intfc_enable_disable_t * mp)
673 vnet_main_t *vnm = vnet_get_main ();
674 vl_api_proxy_arp_intfc_enable_disable_reply_t *rmp;
676 VALIDATE_SW_IF_INDEX (mp);
678 vnet_sw_interface_t *si =
679 vnet_get_sw_interface (vnm, ntohl (mp->sw_if_index));
683 if (mp->enable_disable)
684 si->flags |= VNET_SW_INTERFACE_FLAG_PROXY_ARP;
686 si->flags &= ~VNET_SW_INTERFACE_FLAG_PROXY_ARP;
688 BAD_SW_IF_INDEX_LABEL;
690 REPLY_MACRO (VL_API_PROXY_ARP_INTFC_ENABLE_DISABLE_REPLY);
694 vl_api_sw_interface_set_mpls_enable_t_handler
695 (vl_api_sw_interface_set_mpls_enable_t * mp)
697 vl_api_sw_interface_set_mpls_enable_reply_t *rmp;
700 VALIDATE_SW_IF_INDEX (mp);
702 rv = mpls_sw_interface_enable_disable (&mpls_main,
703 ntohl (mp->sw_if_index),
706 BAD_SW_IF_INDEX_LABEL;
707 REPLY_MACRO (VL_API_SW_INTERFACE_SET_MPLS_ENABLE_REPLY);
711 send_oam_event (oam_target_t * t)
713 vpe_api_main_t *vam = &vpe_api_main;
714 unix_shared_memory_queue_t *q;
715 vpe_client_registration_t *reg;
716 vl_api_oam_event_t *mp;
719 pool_foreach(reg, vam->oam_events_registrations,
721 q = vl_api_client_index_to_input_queue (reg->client_index);
724 mp = vl_msg_api_alloc (sizeof (*mp));
725 mp->_vl_msg_id = ntohs (VL_API_OAM_EVENT);
726 clib_memcpy (mp->dst_address, &t->dst_address,
727 sizeof (mp->dst_address));
728 mp->state = t->state;
729 vl_msg_api_send_shmem (q, (u8 *)&mp);
736 vl_api_oam_add_del_t_handler (vl_api_oam_add_del_t * mp)
738 vl_api_oam_add_del_reply_t *rmp;
741 rv = vpe_oam_add_del_target ((ip4_address_t *) mp->src_address,
742 (ip4_address_t *) mp->dst_address,
743 ntohl (mp->vrf_id), (int) (mp->is_add));
745 REPLY_MACRO (VL_API_OAM_ADD_DEL_REPLY);
750 ip4_reset_fib_t_handler (vl_api_reset_fib_t * mp)
752 vnet_main_t *vnm = vnet_get_main ();
753 vnet_interface_main_t *im = &vnm->interface_main;
754 ip4_main_t *im4 = &ip4_main;
755 static u32 *sw_if_indices_to_shut;
756 stats_main_t *sm = &stats_main;
757 fib_table_t *fib_table;
761 int rv = VNET_API_ERROR_NO_SUCH_FIB;
762 u32 target_fib_id = ntohl (mp->vrf_id);
764 dslock (sm, 1 /* release hint */ , 8 /* tag */ );
767 pool_foreach (fib_table, im4->fibs,
769 vnet_sw_interface_t * si;
771 fib = pool_elt_at_index (im4->v4_fibs, fib_table->ft_index);
773 if (fib->table_id != target_fib_id)
776 /* remove any mpls encap/decap labels */
777 mpls_fib_reset_labels (fib->table_id);
779 /* remove any proxy arps in this fib */
780 vnet_proxy_arp_fib_reset (fib->table_id);
782 /* Set the flow hash for this fib to the default */
783 vnet_set_ip4_flow_hash (fib->table_id, IP_FLOW_HASH_DEFAULT);
785 vec_reset_length (sw_if_indices_to_shut);
787 /* Shut down interfaces in this FIB / clean out intfc routes */
788 pool_foreach (si, im->sw_interfaces,
790 u32 sw_if_index = si->sw_if_index;
792 if (sw_if_index < vec_len (im4->fib_index_by_sw_if_index)
793 && (im4->fib_index_by_sw_if_index[si->sw_if_index] ==
795 vec_add1 (sw_if_indices_to_shut, si->sw_if_index);
798 for (i = 0; i < vec_len (sw_if_indices_to_shut); i++) {
799 sw_if_index = sw_if_indices_to_shut[i];
801 u32 flags = vnet_sw_interface_get_flags (vnm, sw_if_index);
802 flags &= ~(VNET_SW_INTERFACE_FLAG_ADMIN_UP);
803 vnet_sw_interface_set_flags (vnm, sw_if_index, flags);
806 fib_table_flush(fib->index, FIB_PROTOCOL_IP4, FIB_SOURCE_API);
810 })); /* pool_foreach (fib) */
818 ip6_reset_fib_t_handler (vl_api_reset_fib_t * mp)
820 vnet_main_t *vnm = vnet_get_main ();
821 vnet_interface_main_t *im = &vnm->interface_main;
822 ip6_main_t *im6 = &ip6_main;
823 stats_main_t *sm = &stats_main;
824 static u32 *sw_if_indices_to_shut;
825 fib_table_t *fib_table;
829 int rv = VNET_API_ERROR_NO_SUCH_FIB;
830 u32 target_fib_id = ntohl (mp->vrf_id);
832 dslock (sm, 1 /* release hint */ , 9 /* tag */ );
835 pool_foreach (fib_table, im6->fibs,
837 vnet_sw_interface_t * si;
839 fib = pool_elt_at_index (im6->v6_fibs, fib_table->ft_index);
841 if (fib->table_id != target_fib_id)
844 vec_reset_length (sw_if_indices_to_shut);
846 /* Set the flow hash for this fib to the default */
847 vnet_set_ip6_flow_hash (fib->table_id, IP_FLOW_HASH_DEFAULT);
849 /* Shut down interfaces in this FIB / clean out intfc routes */
850 pool_foreach (si, im->sw_interfaces,
852 if (im6->fib_index_by_sw_if_index[si->sw_if_index] ==
854 vec_add1 (sw_if_indices_to_shut, si->sw_if_index);
857 for (i = 0; i < vec_len (sw_if_indices_to_shut); i++) {
858 sw_if_index = sw_if_indices_to_shut[i];
860 u32 flags = vnet_sw_interface_get_flags (vnm, sw_if_index);
861 flags &= ~(VNET_SW_INTERFACE_FLAG_ADMIN_UP);
862 vnet_sw_interface_set_flags (vnm, sw_if_index, flags);
865 fib_table_flush(fib->index, FIB_PROTOCOL_IP6, FIB_SOURCE_API);
869 })); /* pool_foreach (fib) */
877 vl_api_reset_fib_t_handler (vl_api_reset_fib_t * mp)
880 vl_api_reset_fib_reply_t *rmp;
883 rv = ip6_reset_fib_t_handler (mp);
885 rv = ip4_reset_fib_t_handler (mp);
887 REPLY_MACRO (VL_API_RESET_FIB_REPLY);
891 vl_api_create_loopback_t_handler (vl_api_create_loopback_t * mp)
893 vl_api_create_loopback_reply_t *rmp;
897 rv = vnet_create_loopback_interface (&sw_if_index, mp->mac_address, 0, 0);
900 REPLY_MACRO2(VL_API_CREATE_LOOPBACK_REPLY,
902 rmp->sw_if_index = ntohl (sw_if_index);
907 static void vl_api_create_loopback_instance_t_handler
908 (vl_api_create_loopback_instance_t * mp)
910 vl_api_create_loopback_instance_reply_t *rmp;
912 u8 is_specified = mp->is_specified;
913 u32 user_instance = ntohl (mp->user_instance);
916 rv = vnet_create_loopback_interface (&sw_if_index, mp->mac_address,
917 is_specified, user_instance);
920 REPLY_MACRO2(VL_API_CREATE_LOOPBACK_INSTANCE_REPLY,
922 rmp->sw_if_index = ntohl (sw_if_index);
928 vl_api_delete_loopback_t_handler (vl_api_delete_loopback_t * mp)
930 vl_api_delete_loopback_reply_t *rmp;
934 sw_if_index = ntohl (mp->sw_if_index);
935 rv = vnet_delete_loopback_interface (sw_if_index);
937 REPLY_MACRO (VL_API_DELETE_LOOPBACK_REPLY);
941 vl_api_control_ping_t_handler (vl_api_control_ping_t * mp)
943 vl_api_control_ping_reply_t *rmp;
947 REPLY_MACRO2(VL_API_CONTROL_PING_REPLY,
949 rmp->vpe_pid = ntohl (getpid());
955 shmem_cli_output (uword arg, u8 * buffer, uword buffer_bytes)
957 u8 **shmem_vecp = (u8 **) arg;
960 api_main_t *am = &api_main;
963 shmem_vec = *shmem_vecp;
965 offset = vec_len (shmem_vec);
967 pthread_mutex_lock (&am->vlib_rp->mutex);
968 oldheap = svm_push_data_heap (am->vlib_rp);
970 vec_validate (shmem_vec, offset + buffer_bytes - 1);
972 clib_memcpy (shmem_vec + offset, buffer, buffer_bytes);
974 svm_pop_heap (oldheap);
975 pthread_mutex_unlock (&am->vlib_rp->mutex);
977 *shmem_vecp = shmem_vec;
982 vl_api_cli_t_handler (vl_api_cli_t * mp)
984 vl_api_cli_reply_t *rp;
985 unix_shared_memory_queue_t *q;
986 vlib_main_t *vm = vlib_get_main ();
987 api_main_t *am = &api_main;
988 unformat_input_t input;
992 q = vl_api_client_index_to_input_queue (mp->client_index);
996 rp = vl_msg_api_alloc (sizeof (*rp));
997 rp->_vl_msg_id = ntohs (VL_API_CLI_REPLY);
998 rp->context = mp->context;
1000 unformat_init_vector (&input, (u8 *) (uword) mp->cmd_in_shmem);
1002 vlib_cli_input (vm, &input, shmem_cli_output, (uword) & shmem_vec);
1004 pthread_mutex_lock (&am->vlib_rp->mutex);
1005 oldheap = svm_push_data_heap (am->vlib_rp);
1007 vec_add1 (shmem_vec, 0);
1009 svm_pop_heap (oldheap);
1010 pthread_mutex_unlock (&am->vlib_rp->mutex);
1012 rp->reply_in_shmem = (uword) shmem_vec;
1014 vl_msg_api_send_shmem (q, (u8 *) & rp);
1018 inband_cli_output (uword arg, u8 * buffer, uword buffer_bytes)
1020 u8 **mem_vecp = (u8 **) arg;
1021 u8 *mem_vec = *mem_vecp;
1022 u32 offset = vec_len (mem_vec);
1024 vec_validate (mem_vec, offset + buffer_bytes - 1);
1025 clib_memcpy (mem_vec + offset, buffer, buffer_bytes);
1026 *mem_vecp = mem_vec;
1030 vl_api_cli_inband_t_handler (vl_api_cli_inband_t * mp)
1032 vl_api_cli_inband_reply_t *rmp;
1034 unix_shared_memory_queue_t *q;
1035 vlib_main_t *vm = vlib_get_main ();
1036 unformat_input_t input;
1039 q = vl_api_client_index_to_input_queue (mp->client_index);
1043 unformat_init_string (&input, (char *) mp->cmd, ntohl (mp->length));
1044 vlib_cli_input (vm, &input, inband_cli_output, (uword) & out_vec);
1046 u32 len = vec_len (out_vec);
1048 REPLY_MACRO3(VL_API_CLI_INBAND_REPLY, len,
1050 rmp->length = htonl (len);
1051 clib_memcpy (rmp->reply, out_vec, len);
1058 vl_api_set_arp_neighbor_limit_t_handler (vl_api_set_arp_neighbor_limit_t * mp)
1061 vl_api_set_arp_neighbor_limit_reply_t *rmp;
1062 vnet_main_t *vnm = vnet_get_main ();
1063 clib_error_t *error;
1068 error = ip6_set_neighbor_limit (ntohl (mp->arp_neighbor_limit));
1070 error = ip4_set_arp_limit (ntohl (mp->arp_neighbor_limit));
1074 clib_error_report (error);
1075 rv = VNET_API_ERROR_UNSPECIFIED;
1079 rv = vnm->api_errno;
1082 REPLY_MACRO (VL_API_SET_ARP_NEIGHBOR_LIMIT_REPLY);
1085 static void vl_api_classify_set_interface_ip_table_t_handler
1086 (vl_api_classify_set_interface_ip_table_t * mp)
1088 vlib_main_t *vm = vlib_get_main ();
1089 vl_api_classify_set_interface_ip_table_reply_t *rmp;
1092 VALIDATE_SW_IF_INDEX (mp);
1094 u32 table_index = ntohl (mp->table_index);
1095 u32 sw_if_index = ntohl (mp->sw_if_index);
1098 rv = vnet_set_ip6_classify_intfc (vm, sw_if_index, table_index);
1100 rv = vnet_set_ip4_classify_intfc (vm, sw_if_index, table_index);
1102 BAD_SW_IF_INDEX_LABEL;
1104 REPLY_MACRO (VL_API_CLASSIFY_SET_INTERFACE_IP_TABLE_REPLY);
1107 static void vl_api_classify_set_interface_l2_tables_t_handler
1108 (vl_api_classify_set_interface_l2_tables_t * mp)
1110 vl_api_classify_set_interface_l2_tables_reply_t *rmp;
1112 u32 sw_if_index, ip4_table_index, ip6_table_index, other_table_index;
1115 ip4_table_index = ntohl (mp->ip4_table_index);
1116 ip6_table_index = ntohl (mp->ip6_table_index);
1117 other_table_index = ntohl (mp->other_table_index);
1118 sw_if_index = ntohl (mp->sw_if_index);
1120 VALIDATE_SW_IF_INDEX (mp);
1123 rv = vnet_l2_input_classify_set_tables (sw_if_index, ip4_table_index,
1127 rv = vnet_l2_output_classify_set_tables (sw_if_index, ip4_table_index,
1133 if (ip4_table_index != ~0 || ip6_table_index != ~0
1134 || other_table_index != ~0)
1140 vnet_l2_input_classify_enable_disable (sw_if_index, enable);
1142 vnet_l2_output_classify_enable_disable (sw_if_index, enable);
1145 BAD_SW_IF_INDEX_LABEL;
1147 REPLY_MACRO (VL_API_CLASSIFY_SET_INTERFACE_L2_TABLES_REPLY);
1150 extern void l2_efp_filter_configure (vnet_main_t * vnet_main,
1151 u32 sw_if_index, u32 enable);
1154 vl_api_l2_interface_efp_filter_t_handler (vl_api_l2_interface_efp_filter_t *
1158 vl_api_l2_interface_efp_filter_reply_t *rmp;
1159 vnet_main_t *vnm = vnet_get_main ();
1161 // enable/disable the feature
1162 l2_efp_filter_configure (vnm, mp->sw_if_index, mp->enable_disable);
1163 rv = vnm->api_errno;
1165 REPLY_MACRO (VL_API_L2_INTERFACE_EFP_FILTER_REPLY);
1169 vl_api_show_version_t_handler (vl_api_show_version_t * mp)
1171 vl_api_show_version_reply_t *rmp;
1173 char *vpe_api_get_build_directory (void);
1174 char *vpe_api_get_version (void);
1175 char *vpe_api_get_build_date (void);
1177 unix_shared_memory_queue_t *q =
1178 vl_api_client_index_to_input_queue (mp->client_index);
1184 REPLY_MACRO2(VL_API_SHOW_VERSION_REPLY,
1186 strncpy ((char *) rmp->program, "vpe", ARRAY_LEN(rmp->program)-1);
1187 strncpy ((char *) rmp->build_directory, vpe_api_get_build_directory(),
1188 ARRAY_LEN(rmp->build_directory)-1);
1189 strncpy ((char *) rmp->version, vpe_api_get_version(),
1190 ARRAY_LEN(rmp->version)-1);
1191 strncpy ((char *) rmp->build_date, vpe_api_get_build_date(),
1192 ARRAY_LEN(rmp->build_date)-1);
1198 vl_api_get_node_index_t_handler (vl_api_get_node_index_t * mp)
1200 vlib_main_t *vm = vlib_get_main ();
1201 vl_api_get_node_index_reply_t *rmp;
1204 u32 node_index = ~0;
1206 n = vlib_get_node_by_name (vm, mp->node_name);
1209 rv = VNET_API_ERROR_NO_SUCH_NODE;
1211 node_index = n->index;
1214 REPLY_MACRO2(VL_API_GET_NODE_INDEX_REPLY,
1216 rmp->node_index = ntohl(node_index);
1222 vl_api_get_next_index_t_handler (vl_api_get_next_index_t * mp)
1224 vlib_main_t *vm = vlib_get_main ();
1225 vl_api_get_next_index_reply_t *rmp;
1226 vlib_node_t *node, *next_node;
1228 u32 next_node_index = ~0, next_index = ~0;
1231 node = vlib_get_node_by_name (vm, mp->node_name);
1235 rv = VNET_API_ERROR_NO_SUCH_NODE;
1239 next_node = vlib_get_node_by_name (vm, mp->next_name);
1243 rv = VNET_API_ERROR_NO_SUCH_NODE2;
1247 next_node_index = next_node->index;
1249 p = hash_get (node->next_slot_by_node, next_node_index);
1253 rv = VNET_API_ERROR_NO_SUCH_ENTRY;
1261 REPLY_MACRO2(VL_API_GET_NEXT_INDEX_REPLY,
1263 rmp->next_index = ntohl(next_index);
1269 vl_api_add_node_next_t_handler (vl_api_add_node_next_t * mp)
1271 vlib_main_t *vm = vlib_get_main ();
1272 vl_api_add_node_next_reply_t *rmp;
1273 vlib_node_t *n, *next;
1275 u32 next_index = ~0;
1277 n = vlib_get_node_by_name (vm, mp->node_name);
1281 rv = VNET_API_ERROR_NO_SUCH_NODE;
1285 next = vlib_get_node_by_name (vm, mp->next_name);
1288 rv = VNET_API_ERROR_NO_SUCH_NODE2;
1290 next_index = vlib_node_add_next (vm, n->index, next->index);
1294 REPLY_MACRO2(VL_API_GET_NODE_INDEX_REPLY,
1296 rmp->next_index = ntohl(next_index);
1302 vl_api_l2_patch_add_del_t_handler (vl_api_l2_patch_add_del_t * mp)
1304 extern int vnet_l2_patch_add_del (u32 rx_sw_if_index, u32 tx_sw_if_index,
1306 vl_api_l2_patch_add_del_reply_t *rmp;
1307 int vnet_l2_patch_add_del (u32 rx_sw_if_index, u32 tx_sw_if_index,
1311 VALIDATE_RX_SW_IF_INDEX (mp);
1312 VALIDATE_TX_SW_IF_INDEX (mp);
1314 rv = vnet_l2_patch_add_del (ntohl (mp->rx_sw_if_index),
1315 ntohl (mp->tx_sw_if_index),
1316 (int) (mp->is_add != 0));
1318 BAD_RX_SW_IF_INDEX_LABEL;
1319 BAD_TX_SW_IF_INDEX_LABEL;
1321 REPLY_MACRO (VL_API_L2_PATCH_ADD_DEL_REPLY);
1325 vl_api_interface_name_renumber_t_handler (vl_api_interface_name_renumber_t *
1328 vl_api_interface_name_renumber_reply_t *rmp;
1331 VALIDATE_SW_IF_INDEX (mp);
1333 rv = vnet_interface_name_renumber
1334 (ntohl (mp->sw_if_index), ntohl (mp->new_show_dev_instance));
1336 BAD_SW_IF_INDEX_LABEL;
1338 REPLY_MACRO (VL_API_INTERFACE_NAME_RENUMBER_REPLY);
1342 arp_change_data_callback (u32 pool_index, u8 * new_mac,
1343 u32 sw_if_index, u32 address)
1345 vpe_api_main_t *am = &vpe_api_main;
1346 vlib_main_t *vm = am->vlib_main;
1347 vl_api_ip4_arp_event_t *event;
1348 static f64 arp_event_last_time;
1349 f64 now = vlib_time_now (vm);
1351 if (pool_is_free_index (am->arp_events, pool_index))
1354 event = pool_elt_at_index (am->arp_events, pool_index);
1356 if (memcmp (&event->new_mac, new_mac, sizeof (event->new_mac)))
1358 clib_memcpy (event->new_mac, new_mac, sizeof (event->new_mac));
1362 if (sw_if_index == ntohl(event->sw_if_index) &&
1364 /* for BD case, also check IP address with 10 sec timeout */
1365 (address == event->address &&
1366 (now - arp_event_last_time) < 10.0)))
1371 arp_event_last_time = now;
1372 event->sw_if_index = htonl (sw_if_index);
1374 event->address = address;
1379 nd_change_data_callback (u32 pool_index, u8 * new_mac,
1380 u32 sw_if_index, ip6_address_t * address)
1382 vpe_api_main_t *am = &vpe_api_main;
1383 vlib_main_t *vm = am->vlib_main;
1384 vl_api_ip6_nd_event_t *event;
1385 static f64 nd_event_last_time;
1386 f64 now = vlib_time_now (vm);
1388 if (pool_is_free_index (am->nd_events, pool_index))
1391 event = pool_elt_at_index (am->nd_events, pool_index);
1394 if (memcmp (&event->new_mac, new_mac, sizeof (event->new_mac)))
1396 clib_memcpy (event->new_mac, new_mac, sizeof (event->new_mac));
1400 if (sw_if_index == ntohl(event->sw_if_index) &&
1402 /* for BD case, also check IP address with 10 sec timeout */
1403 (ip6_address_is_equal (address,
1404 (ip6_address_t *) event->address) &&
1405 (now - nd_event_last_time) < 10.0)))
1410 nd_event_last_time = now;
1411 event->sw_if_index = htonl (sw_if_index);
1413 clib_memcpy (event->address, address, sizeof (event->address));
1418 arp_change_delete_callback (u32 pool_index, u8 * notused)
1420 vpe_api_main_t *am = &vpe_api_main;
1422 if (pool_is_free_index (am->arp_events, pool_index))
1425 pool_put_index (am->arp_events, pool_index);
1430 nd_change_delete_callback (u32 pool_index, u8 * notused)
1432 vpe_api_main_t *am = &vpe_api_main;
1434 if (pool_is_free_index (am->nd_events, pool_index))
1437 pool_put_index (am->nd_events, pool_index);
1442 vl_api_want_ip4_arp_events_t_handler (vl_api_want_ip4_arp_events_t * mp)
1444 vpe_api_main_t *am = &vpe_api_main;
1445 vnet_main_t *vnm = vnet_get_main ();
1446 vl_api_want_ip4_arp_events_reply_t *rmp;
1449 if (mp->enable_disable)
1451 vl_api_ip4_arp_event_t *event;
1452 pool_get (am->arp_events, event);
1453 rv = vnet_add_del_ip4_arp_change_event
1454 (vnm, arp_change_data_callback,
1455 mp->pid, &mp->address /* addr, in net byte order */ ,
1456 vpe_resolver_process_node.index,
1457 IP4_ARP_EVENT, event - am->arp_events, 1 /* is_add */ );
1461 pool_put (am->arp_events, event);
1464 memset (event, 0, sizeof (*event));
1466 /* Python API expects events to have no context */
1467 event->_vl_msg_id = ntohs (VL_API_IP4_ARP_EVENT);
1468 event->client_index = mp->client_index;
1469 event->address = mp->address;
1470 event->pid = mp->pid;
1471 if (mp->address == 0)
1476 rv = vnet_add_del_ip4_arp_change_event
1477 (vnm, arp_change_delete_callback,
1478 mp->pid, &mp->address /* addr, in net byte order */ ,
1479 vpe_resolver_process_node.index,
1480 IP4_ARP_EVENT, ~0 /* pool index */ , 0 /* is_add */ );
1483 REPLY_MACRO (VL_API_WANT_IP4_ARP_EVENTS_REPLY);
1487 vl_api_want_ip6_nd_events_t_handler (vl_api_want_ip6_nd_events_t * mp)
1489 vpe_api_main_t *am = &vpe_api_main;
1490 vnet_main_t *vnm = vnet_get_main ();
1491 vl_api_want_ip6_nd_events_reply_t *rmp;
1494 if (mp->enable_disable)
1496 vl_api_ip6_nd_event_t *event;
1497 pool_get (am->nd_events, event);
1499 rv = vnet_add_del_ip6_nd_change_event
1500 (vnm, nd_change_data_callback,
1501 mp->pid, mp->address /* addr, in net byte order */ ,
1502 vpe_resolver_process_node.index,
1503 IP6_ND_EVENT, event - am->nd_events, 1 /* is_add */ );
1507 pool_put (am->nd_events, event);
1510 memset (event, 0, sizeof (*event));
1512 event->_vl_msg_id = ntohs (VL_API_IP6_ND_EVENT);
1513 event->client_index = mp->client_index;
1514 clib_memcpy (event->address, mp->address, 16);
1515 event->pid = mp->pid;
1516 if (ip6_address_is_zero ((ip6_address_t *) mp->address))
1522 rv = vnet_add_del_ip6_nd_change_event
1523 (vnm, nd_change_delete_callback,
1524 mp->pid, mp->address /* addr, in net byte order */ ,
1525 vpe_resolver_process_node.index,
1526 IP6_ND_EVENT, ~0 /* pool index */ , 0 /* is_add */ );
1529 REPLY_MACRO (VL_API_WANT_IP6_ND_EVENTS_REPLY);
1532 static void vl_api_input_acl_set_interface_t_handler
1533 (vl_api_input_acl_set_interface_t * mp)
1535 vlib_main_t *vm = vlib_get_main ();
1536 vl_api_input_acl_set_interface_reply_t *rmp;
1539 VALIDATE_SW_IF_INDEX (mp);
1541 u32 ip4_table_index = ntohl (mp->ip4_table_index);
1542 u32 ip6_table_index = ntohl (mp->ip6_table_index);
1543 u32 l2_table_index = ntohl (mp->l2_table_index);
1544 u32 sw_if_index = ntohl (mp->sw_if_index);
1546 rv = vnet_set_input_acl_intfc (vm, sw_if_index, ip4_table_index,
1547 ip6_table_index, l2_table_index, mp->is_add);
1549 BAD_SW_IF_INDEX_LABEL;
1551 REPLY_MACRO (VL_API_INPUT_ACL_SET_INTERFACE_REPLY);
1555 vl_api_get_node_graph_t_handler (vl_api_get_node_graph_t * mp)
1559 api_main_t *am = &api_main;
1560 vlib_main_t *vm = vlib_get_main ();
1562 vl_api_get_node_graph_reply_t *rmp;
1564 pthread_mutex_lock (&am->vlib_rp->mutex);
1565 oldheap = svm_push_data_heap (am->vlib_rp);
1568 * Keep the number of memcpy ops to a minimum (e.g. 1).
1570 vec_validate (vector, 16384);
1571 vec_reset_length (vector);
1574 vector = vlib_node_serialize (&vm->node_main, vector,
1575 (u32) ~ 0 /* all threads */ ,
1576 1 /* include nexts */ ,
1577 1 /* include stats */ );
1579 svm_pop_heap (oldheap);
1580 pthread_mutex_unlock (&am->vlib_rp->mutex);
1583 REPLY_MACRO2(VL_API_GET_NODE_GRAPH_REPLY,
1585 rmp->reply_in_shmem = (uword) vector;
1591 vl_api_ioam_enable_t_handler (vl_api_ioam_enable_t * mp)
1594 vl_api_ioam_enable_reply_t *rmp;
1595 clib_error_t *error;
1597 /* Ignoring the profile id as currently a single profile
1599 error = ip6_ioam_enable (mp->trace_enable, mp->pot_enable,
1600 mp->seqno, mp->analyse);
1603 clib_error_report (error);
1604 rv = clib_error_get_code (error);
1607 REPLY_MACRO (VL_API_IOAM_ENABLE_REPLY);
1611 vl_api_ioam_disable_t_handler (vl_api_ioam_disable_t * mp)
1614 vl_api_ioam_disable_reply_t *rmp;
1615 clib_error_t *error;
1617 error = clear_ioam_rewrite_fn ();
1620 clib_error_report (error);
1621 rv = clib_error_get_code (error);
1624 REPLY_MACRO (VL_API_IOAM_DISABLE_REPLY);
1628 vl_api_pg_create_interface_t_handler (vl_api_pg_create_interface_t * mp)
1630 vl_api_pg_create_interface_reply_t *rmp;
1633 pg_main_t *pg = &pg_main;
1634 u32 pg_if_id = pg_interface_add_or_get (pg, ntohl (mp->interface_id));
1635 pg_interface_t *pi = pool_elt_at_index (pg->interfaces, pg_if_id);
1638 REPLY_MACRO2(VL_API_PG_CREATE_INTERFACE_REPLY,
1640 rmp->sw_if_index = ntohl(pi->sw_if_index);
1646 vl_api_pg_capture_t_handler (vl_api_pg_capture_t * mp)
1648 vl_api_pg_capture_reply_t *rmp;
1651 vnet_main_t *vnm = vnet_get_main ();
1652 vnet_interface_main_t *im = &vnm->interface_main;
1653 vnet_hw_interface_t *hi = 0;
1655 u8 *intf_name = format (0, "pg%d", ntohl (mp->interface_id), 0);
1656 u32 hw_if_index = ~0;
1657 uword *p = hash_get_mem (im->hw_interface_by_name, intf_name);
1660 vec_free (intf_name);
1662 if (hw_if_index != ~0)
1664 pg_capture_args_t _a, *a = &_a;
1666 u32 len = ntohl (mp->pcap_name_length);
1667 u8 *pcap_file_name = vec_new (u8, len);
1668 clib_memcpy (pcap_file_name, mp->pcap_file_name, len);
1670 hi = vnet_get_sup_hw_interface (vnm, hw_if_index);
1671 a->hw_if_index = hw_if_index;
1672 a->dev_instance = hi->dev_instance;
1673 a->is_enabled = mp->is_enabled;
1674 a->pcap_file_name = pcap_file_name;
1675 a->count = ntohl (mp->count);
1677 clib_error_t *e = pg_capture (a);
1680 clib_error_report (e);
1681 rv = VNET_API_ERROR_CANNOT_CREATE_PCAP_FILE;
1684 vec_free (pcap_file_name);
1686 REPLY_MACRO (VL_API_PG_CAPTURE_REPLY);
1690 vl_api_pg_enable_disable_t_handler (vl_api_pg_enable_disable_t * mp)
1692 vl_api_pg_enable_disable_reply_t *rmp;
1695 pg_main_t *pg = &pg_main;
1696 u32 stream_index = ~0;
1698 int is_enable = mp->is_enabled != 0;
1699 u32 len = ntohl (mp->stream_name_length) - 1;
1703 u8 *stream_name = vec_new (u8, len);
1704 clib_memcpy (stream_name, mp->stream_name, len);
1705 uword *p = hash_get_mem (pg->stream_index_by_name, stream_name);
1708 vec_free (stream_name);
1711 pg_enable_disable (stream_index, is_enable);
1713 REPLY_MACRO (VL_API_PG_ENABLE_DISABLE_REPLY);
1717 vl_api_ip_source_and_port_range_check_add_del_t_handler
1718 (vl_api_ip_source_and_port_range_check_add_del_t * mp)
1720 vl_api_ip_source_and_port_range_check_add_del_reply_t *rmp;
1723 u8 is_ipv6 = mp->is_ipv6;
1724 u8 is_add = mp->is_add;
1725 u8 mask_length = mp->mask_length;
1726 ip4_address_t ip4_addr;
1727 ip6_address_t ip6_addr;
1729 u16 *high_ports = 0;
1731 u16 tmp_low, tmp_high;
1735 // Validate port range
1736 num_ranges = mp->number_of_ranges;
1737 if (num_ranges > 32)
1738 { // This is size of array in VPE.API
1739 rv = VNET_API_ERROR_EXCEEDED_NUMBER_OF_RANGES_CAPACITY;
1743 vec_reset_length (low_ports);
1744 vec_reset_length (high_ports);
1746 for (i = 0; i < num_ranges; i++)
1748 tmp_low = mp->low_ports[i];
1749 tmp_high = mp->high_ports[i];
1750 // If tmp_low <= tmp_high then only need to check tmp_low = 0
1751 // If tmp_low <= tmp_high then only need to check tmp_high > 65535
1752 if (tmp_low > tmp_high || tmp_low == 0 || tmp_high > 65535)
1754 rv = VNET_API_ERROR_INVALID_VALUE;
1757 vec_add1 (low_ports, tmp_low);
1758 vec_add1 (high_ports, tmp_high + 1);
1761 // Validate mask_length
1762 if ((is_ipv6 && mask_length > 128) || (!is_ipv6 && mask_length > 32))
1764 rv = VNET_API_ERROR_ADDRESS_LENGTH_MISMATCH;
1768 vrf_id = ntohl (mp->vrf_id);
1772 rv = VNET_API_ERROR_INVALID_VALUE;
1779 clib_memcpy (ip6_addr.as_u8, mp->address, sizeof (ip6_addr.as_u8));
1780 rv = ip6_source_and_port_range_check_add_del (&ip6_addr,
1784 high_ports, is_add);
1788 clib_memcpy (ip4_addr.data, mp->address, sizeof (ip4_addr));
1789 rv = ip4_source_and_port_range_check_add_del (&ip4_addr,
1793 high_ports, is_add);
1797 vec_free (low_ports);
1798 vec_free (high_ports);
1799 REPLY_MACRO (VL_API_IP_SOURCE_AND_PORT_RANGE_CHECK_ADD_DEL_REPLY);
1803 vl_api_ip_source_and_port_range_check_interface_add_del_t_handler
1804 (vl_api_ip_source_and_port_range_check_interface_add_del_t * mp)
1806 vlib_main_t *vm = vlib_get_main ();
1807 vl_api_ip_source_and_port_range_check_interface_add_del_reply_t *rmp;
1808 ip4_main_t *im = &ip4_main;
1811 u32 fib_index[IP_SOURCE_AND_PORT_RANGE_CHECK_N_PROTOCOLS];
1812 u32 vrf_id[IP_SOURCE_AND_PORT_RANGE_CHECK_N_PROTOCOLS];
1816 vrf_id[IP_SOURCE_AND_PORT_RANGE_CHECK_PROTOCOL_TCP_OUT] =
1817 ntohl (mp->tcp_out_vrf_id);
1818 vrf_id[IP_SOURCE_AND_PORT_RANGE_CHECK_PROTOCOL_UDP_OUT] =
1819 ntohl (mp->udp_out_vrf_id);
1820 vrf_id[IP_SOURCE_AND_PORT_RANGE_CHECK_PROTOCOL_TCP_IN] =
1821 ntohl (mp->tcp_in_vrf_id);
1822 vrf_id[IP_SOURCE_AND_PORT_RANGE_CHECK_PROTOCOL_UDP_IN] =
1823 ntohl (mp->udp_in_vrf_id);
1826 for (i = 0; i < IP_SOURCE_AND_PORT_RANGE_CHECK_N_PROTOCOLS; i++)
1828 if (vrf_id[i] != 0 && vrf_id[i] != ~0)
1830 p = hash_get (im->fib_index_by_table_id, vrf_id[i]);
1834 rv = VNET_API_ERROR_INVALID_VALUE;
1838 fib_index[i] = p[0];
1843 sw_if_index = ntohl (mp->sw_if_index);
1845 VALIDATE_SW_IF_INDEX (mp);
1848 set_ip_source_and_port_range_check (vm, fib_index, sw_if_index,
1851 BAD_SW_IF_INDEX_LABEL;
1854 REPLY_MACRO (VL_API_IP_SOURCE_AND_PORT_RANGE_CHECK_INTERFACE_ADD_DEL_REPLY);
1858 vl_api_delete_subif_t_handler (vl_api_delete_subif_t * mp)
1860 vl_api_delete_subif_reply_t *rmp;
1863 rv = vnet_delete_sub_interface (ntohl (mp->sw_if_index));
1865 REPLY_MACRO (VL_API_DELETE_SUBIF_REPLY);
1869 vl_api_punt_t_handler (vl_api_punt_t * mp)
1871 vl_api_punt_reply_t *rmp;
1872 vlib_main_t *vm = vlib_get_main ();
1874 clib_error_t *error;
1876 error = vnet_punt_add_del (vm, mp->ipv, mp->l4_protocol,
1877 ntohs (mp->l4_port), mp->is_add);
1881 clib_error_report (error);
1884 REPLY_MACRO (VL_API_PUNT_REPLY);
1888 vl_api_punt_socket_register_t_handler (vl_api_punt_socket_register_t * mp)
1890 vl_api_punt_socket_register_reply_t *rmp;
1891 vlib_main_t *vm = vlib_get_main ();
1893 clib_error_t *error;
1894 unix_shared_memory_queue_t *q;
1897 error = vnet_punt_socket_add (vm, ntohl (mp->header_version),
1898 mp->is_ip4, mp->l4_protocol,
1899 ntohs (mp->l4_port), (char *) mp->pathname);
1903 clib_error_report (error);
1906 q = vl_api_client_index_to_input_queue (mp->client_index);
1910 rmp = vl_msg_api_alloc (sizeof (*rmp));
1911 rmp->_vl_msg_id = htons (VL_API_PUNT_SOCKET_REGISTER_REPLY);
1912 rmp->context = mp->context;
1913 rmp->retval = htonl (rv);
1914 char *p = vnet_punt_get_server_pathname ();
1915 /* Abstract pathnames start with \0 */
1916 memcpy ((char *) rmp->pathname, p, sizeof (rmp->pathname));
1917 vl_msg_api_send_shmem (q, (u8 *) & rmp);
1921 vl_api_punt_socket_deregister_t_handler (vl_api_punt_socket_deregister_t * mp)
1923 vl_api_punt_socket_deregister_reply_t *rmp;
1924 vlib_main_t *vm = vlib_get_main ();
1926 clib_error_t *error;
1927 unix_shared_memory_queue_t *q;
1930 error = vnet_punt_socket_del (vm, mp->is_ip4, mp->l4_protocol,
1931 ntohs (mp->l4_port));
1935 clib_error_report (error);
1938 q = vl_api_client_index_to_input_queue (mp->client_index);
1942 rmp = vl_msg_api_alloc (sizeof (*rmp));
1943 rmp->_vl_msg_id = htons (VL_API_PUNT_SOCKET_DEREGISTER_REPLY);
1944 rmp->context = mp->context;
1945 rmp->retval = htonl (rv);
1946 vl_msg_api_send_shmem (q, (u8 *) & rmp);
1950 vl_api_feature_enable_disable_t_handler (vl_api_feature_enable_disable_t * mp)
1952 vl_api_feature_enable_disable_reply_t *rmp;
1955 VALIDATE_SW_IF_INDEX (mp);
1957 u8 *arc_name = format (0, "%s%c", mp->arc_name, 0);
1958 u8 *feature_name = format (0, "%s%c", mp->feature_name, 0);
1960 vnet_feature_registration_t *reg =
1961 vnet_get_feature_reg ((const char *) arc_name,
1962 (const char *) feature_name);
1964 rv = VNET_API_ERROR_INVALID_VALUE;
1967 u32 sw_if_index = ntohl (mp->sw_if_index);
1968 clib_error_t *error = 0;
1970 if (reg->enable_disable_cb)
1971 error = reg->enable_disable_cb (sw_if_index, mp->enable);
1973 vnet_feature_enable_disable ((const char *) arc_name,
1974 (const char *) feature_name,
1975 sw_if_index, mp->enable, 0, 0);
1978 clib_error_report (error);
1979 rv = VNET_API_ERROR_CANNOT_ENABLE_DISABLE_FEATURE;
1983 vec_free (feature_name);
1984 vec_free (arc_name);
1986 BAD_SW_IF_INDEX_LABEL;
1988 REPLY_MACRO (VL_API_FEATURE_ENABLE_DISABLE_REPLY);
1991 #define BOUNCE_HANDLER(nn) \
1992 static void vl_api_##nn##_t_handler ( \
1993 vl_api_##nn##_t *mp) \
1995 vpe_client_registration_t *reg; \
1996 vpe_api_main_t * vam = &vpe_api_main; \
1997 unix_shared_memory_queue_t * q; \
1999 /* One registration only... */ \
2000 pool_foreach(reg, vam->nn##_registrations, \
2002 q = vl_api_client_index_to_input_queue (reg->client_index); \
2005 * If the queue is stuffed, turf the msg and complain \
2006 * It's unlikely that the intended recipient is \
2007 * alive; avoid deadlock at all costs. \
2009 if (q->cursize == q->maxsize) { \
2010 clib_warning ("ERROR: receiver queue full, drop msg"); \
2011 vl_msg_api_free (mp); \
2014 vl_msg_api_send_shmem (q, (u8 *)&mp); \
2018 vl_msg_api_free (mp); \
2021 static void setup_message_id_table (api_main_t * am);
2025 * Add vpe's API message handlers to the table.
2026 * vlib has alread mapped shared memory and
2027 * added the client registration handlers.
2028 * See .../open-repo/vlib/memclnt_vlib.c:memclnt_process()
2030 static clib_error_t *
2031 vpe_api_hookup (vlib_main_t * vm)
2033 api_main_t *am = &api_main;
2036 vl_msg_api_set_handlers(VL_API_##N, #n, \
2037 vl_api_##n##_t_handler, \
2039 vl_api_##n##_t_endian, \
2040 vl_api_##n##_t_print, \
2041 sizeof(vl_api_##n##_t), 1);
2042 foreach_vpe_api_msg;
2046 * Trace space for classifier mask+match
2048 am->api_trace_cfg[VL_API_CLASSIFY_ADD_DEL_TABLE].size += 5 * sizeof (u32x4);
2049 am->api_trace_cfg[VL_API_CLASSIFY_ADD_DEL_SESSION].size
2050 += 5 * sizeof (u32x4);
2053 * Thread-safe API messages
2055 am->is_mp_safe[VL_API_IP_ADD_DEL_ROUTE] = 1;
2056 am->is_mp_safe[VL_API_GET_NODE_GRAPH] = 1;
2059 * Set up the (msg_name, crc, message-id) table
2061 setup_message_id_table (am);
2066 VLIB_API_INIT_FUNCTION (vpe_api_hookup);
2068 static clib_error_t *
2069 vpe_api_init (vlib_main_t * vm)
2071 vpe_api_main_t *am = &vpe_api_main;
2074 am->vnet_main = vnet_get_main ();
2075 am->interface_events_registration_hash = hash_create (0, sizeof (uword));
2076 am->to_netconf_server_registration_hash = hash_create (0, sizeof (uword));
2077 am->from_netconf_server_registration_hash = hash_create (0, sizeof (uword));
2078 am->to_netconf_client_registration_hash = hash_create (0, sizeof (uword));
2079 am->from_netconf_client_registration_hash = hash_create (0, sizeof (uword));
2080 am->oam_events_registration_hash = hash_create (0, sizeof (uword));
2081 am->bfd_events_registration_hash = hash_create (0, sizeof (uword));
2083 vl_set_memory_region_name ("/vpe-api");
2084 vl_enable_disable_memory_api (vm, 1 /* enable it */ );
2089 VLIB_INIT_FUNCTION (vpe_api_init);
2092 static clib_error_t *
2093 api_segment_config (vlib_main_t * vm, unformat_input_t * input)
2096 u64 baseva, size, pvt_heap_size;
2098 const int max_buf_size = 4096;
2100 struct passwd _pw, *pw;
2101 struct group _grp, *grp;
2103 buf = vec_new (char, 128);
2104 while (unformat_check_input (input) != UNFORMAT_END_OF_INPUT)
2106 if (unformat (input, "prefix %s", &chroot_path))
2108 vec_add1 (chroot_path, 0);
2109 vl_set_memory_root_path ((char *) chroot_path);
2111 else if (unformat (input, "uid %d", &uid))
2112 vl_set_memory_uid (uid);
2113 else if (unformat (input, "gid %d", &gid))
2114 vl_set_memory_gid (gid);
2115 else if (unformat (input, "baseva %llx", &baseva))
2116 vl_set_global_memory_baseva (baseva);
2117 else if (unformat (input, "global-size %lldM", &size))
2118 vl_set_global_memory_size (size * (1ULL << 20));
2119 else if (unformat (input, "global-size %lldG", &size))
2120 vl_set_global_memory_size (size * (1ULL << 30));
2121 else if (unformat (input, "global-size %lld", &size))
2122 vl_set_global_memory_size (size);
2123 else if (unformat (input, "global-pvt-heap-size %lldM", &pvt_heap_size))
2124 vl_set_global_pvt_heap_size (pvt_heap_size * (1ULL << 20));
2125 else if (unformat (input, "global-pvt-heap-size size %lld",
2127 vl_set_global_pvt_heap_size (pvt_heap_size);
2128 else if (unformat (input, "api-pvt-heap-size %lldM", &pvt_heap_size))
2129 vl_set_api_pvt_heap_size (pvt_heap_size * (1ULL << 20));
2130 else if (unformat (input, "api-pvt-heap-size size %lld",
2132 vl_set_api_pvt_heap_size (pvt_heap_size);
2133 else if (unformat (input, "api-size %lldM", &size))
2134 vl_set_api_memory_size (size * (1ULL << 20));
2135 else if (unformat (input, "api-size %lldG", &size))
2136 vl_set_api_memory_size (size * (1ULL << 30));
2137 else if (unformat (input, "api-size %lld", &size))
2138 vl_set_api_memory_size (size);
2139 else if (unformat (input, "uid %s", &s))
2141 /* lookup the username */
2144 getpwnam_r (s, &_pw, buf, vec_len (buf), &pw)) == ERANGE)
2145 && (vec_len (buf) <= max_buf_size))
2147 vec_resize (buf, vec_len (buf) * 2);
2151 e = clib_error_return_code (0, rv,
2152 CLIB_ERROR_ERRNO_VALID |
2154 "cannot fetch username %s", s);
2162 clib_error_return_fatal (0, "username %s does not exist", s);
2168 vl_set_memory_uid (pw->pw_uid);
2170 else if (unformat (input, "gid %s", &s))
2172 /* lookup the group name */
2175 getgrnam_r (s, &_grp, buf, vec_len (buf), &grp)) == ERANGE)
2176 && (vec_len (buf) <= max_buf_size))
2178 vec_resize (buf, vec_len (buf) * 2);
2182 e = clib_error_return_code (0, rv,
2183 CLIB_ERROR_ERRNO_VALID |
2185 "cannot fetch group %s", s);
2192 e = clib_error_return_fatal (0, "group %s does not exist", s);
2199 vl_set_memory_gid (grp->gr_gid);
2202 return clib_error_return (0, "unknown input `%U'",
2203 format_unformat_error, input);
2208 VLIB_EARLY_CONFIG_FUNCTION (api_segment_config, "api-segment");
2211 get_unformat_vnet_sw_interface (void)
2213 return (void *) &unformat_vnet_sw_interface;
2217 format_arp_event (u8 * s, va_list * args)
2219 vl_api_ip4_arp_event_t *event = va_arg (*args, vl_api_ip4_arp_event_t *);
2221 s = format (s, "pid %d: ", ntohl (event->pid));
2223 s = format (s, "bd mac/ip4 binding events");
2225 s = format (s, "resolution for %U", format_ip4_address, &event->address);
2230 format_nd_event (u8 * s, va_list * args)
2232 vl_api_ip6_nd_event_t *event = va_arg (*args, vl_api_ip6_nd_event_t *);
2234 s = format (s, "pid %d: ", ntohl (event->pid));
2236 s = format (s, "bd mac/ip6 binding events");
2238 s = format (s, "resolution for %U", format_ip6_address, event->address);
2242 static clib_error_t *
2243 show_ip_arp_nd_events_fn (vlib_main_t * vm,
2244 unformat_input_t * input, vlib_cli_command_t * cmd)
2246 vpe_api_main_t *am = &vpe_api_main;
2247 vl_api_ip4_arp_event_t *arp_event;
2248 vl_api_ip6_nd_event_t *nd_event;
2250 if ((pool_elts (am->arp_events) == 0) && (pool_elts (am->nd_events) == 0))
2252 vlib_cli_output (vm, "No active arp or nd event registrations");
2257 pool_foreach (arp_event, am->arp_events,
2259 vlib_cli_output (vm, "%U", format_arp_event, arp_event);
2262 pool_foreach (nd_event, am->nd_events,
2264 vlib_cli_output (vm, "%U", format_nd_event, nd_event);
2272 VLIB_CLI_COMMAND (show_ip_arp_nd_events, static) = {
2273 .path = "show arp-nd-event registrations",
2274 .function = show_ip_arp_nd_events_fn,
2275 .short_help = "Show ip4 arp and ip6 nd event registrations",
2279 #define vl_msg_name_crc_list
2280 #include <vpp/api/vpe_all_api_h.h>
2281 #undef vl_msg_name_crc_list
2284 setup_message_id_table (api_main_t * am)
2286 #define _(id,n,crc) vl_msg_api_add_msg_name_crc (am, #n "_" #crc, id);
2287 foreach_vl_msg_name_crc_memclnt;
2288 foreach_vl_msg_name_crc_vpe;
2294 * fd.io coding-style-patch-verification: ON
2297 * eval: (c-set-style "gnu")