VPP-521: Classify API enhancement to redirect traffic to pre-defined VRF
[vpp.git] / vpp / vpp-api / api.c
1 /*
2  *------------------------------------------------------------------
3  * api.c - message handler registration
4  *
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:
9  *
10  *     http://www.apache.org/licenses/LICENSE-2.0
11  *
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  *------------------------------------------------------------------
18  */
19
20 #include <stdio.h>
21 #include <stdlib.h>
22 #include <string.h>
23 #include <sys/types.h>
24 #include <sys/mman.h>
25 #include <sys/stat.h>
26 #include <netinet/in.h>
27 #include <signal.h>
28 #include <pthread.h>
29 #include <unistd.h>
30 #include <time.h>
31 #include <fcntl.h>
32 #include <string.h>
33 #include <pwd.h>
34 #include <grp.h>
35
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>
47
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/l2tp/l2tp.h>
53 #include <vnet/ip/ip.h>
54 #include <vnet/ip/ip6.h>
55 #include <vnet/unix/tuntap.h>
56 #include <vnet/unix/tapcli.h>
57 #include <vnet/mpls/mpls.h>
58 #include <vnet/dhcp/proxy.h>
59 #include <vnet/dhcp/client.h>
60 #if IPV6SR > 0
61 #include <vnet/sr/sr.h>
62 #endif
63 #include <vnet/dhcpv6/proxy.h>
64 #include <vlib/vlib.h>
65 #include <vlib/unix/unix.h>
66 #include <vlibapi/api.h>
67 #include <vlibmemory/api.h>
68 #include <vnet/classify/vnet_classify.h>
69 #include <vnet/classify/input_acl.h>
70 #include <vnet/classify/policer_classify.h>
71 #include <vnet/classify/flow_classify.h>
72 #include <vnet/l2/l2_classify.h>
73 #include <vnet/vxlan/vxlan.h>
74 #include <vnet/gre/gre.h>
75 #include <vnet/l2/l2_vtr.h>
76 #include <vnet/vxlan-gpe/vxlan_gpe.h>
77 #include <vnet/lisp-gpe/lisp_gpe.h>
78 #include <vnet/lisp-gpe/lisp_gpe_fwd_entry.h>
79 #include <vnet/lisp-gpe/lisp_gpe_tenant.h>
80 #include <vnet/lisp-cp/control.h>
81 #include <vnet/map/map.h>
82 #include <vnet/cop/cop.h>
83 #include <vnet/ip/ip6_hop_by_hop.h>
84 #include <vnet/ip/ip_source_and_port_range_check.h>
85 #include <vnet/devices/af_packet/af_packet.h>
86 #include <vnet/policer/policer.h>
87 #include <vnet/devices/netmap/netmap.h>
88 #include <vnet/flow/flow_report.h>
89 #include <vnet/ipsec-gre/ipsec_gre.h>
90 #include <vnet/flow/flow_report_classify.h>
91 #include <vnet/ip/punt.h>
92
93 #undef BIHASH_TYPE
94 #undef __included_bihash_template_h__
95 #include <vnet/l2/l2_fib.h>
96
97 #if IPSEC > 0
98 #include <vnet/ipsec/ipsec.h>
99 #include <vnet/ipsec/ikev2.h>
100 #endif /* IPSEC */
101 #include <vnet/devices/virtio/vhost-user.h>
102
103 #include <stats/stats.h>
104 #include <oam/oam.h>
105
106 #include <vnet/ethernet/ethernet.h>
107 #include <vnet/ethernet/arp_packet.h>
108 #include <vnet/interface.h>
109
110 #include <vnet/l2/l2_fib.h>
111 #include <vnet/l2/l2_bd.h>
112 #include <vpp-api/vpe_msg_enum.h>
113 #include <vnet/span/span.h>
114
115 #include <vnet/fib/ip6_fib.h>
116 #include <vnet/fib/ip4_fib.h>
117 #include <vnet/dpo/drop_dpo.h>
118 #include <vnet/dpo/receive_dpo.h>
119 #include <vnet/dpo/lookup_dpo.h>
120 #include <vnet/dpo/classify_dpo.h>
121 #include <vnet/dpo/ip_null_dpo.h>
122
123 #define f64_endian(a)
124 #define f64_print(a,b)
125
126 #define vl_typedefs             /* define message structures */
127 #include <vpp-api/vpe_all_api_h.h>
128 #undef vl_typedefs
129
130 #define vl_endianfun            /* define message structures */
131 #include <vpp-api/vpe_all_api_h.h>
132 #undef vl_endianfun
133
134 /* instantiate all the print functions we know about */
135 #define vl_print(handle, ...) vlib_cli_output (handle, __VA_ARGS__)
136 #define vl_printfun
137 #include <vpp-api/vpe_all_api_h.h>
138 #undef vl_printfun
139
140 #define REPLY_MACRO(t)                                          \
141 do {                                                            \
142     unix_shared_memory_queue_t * q;                             \
143     rv = vl_msg_api_pd_handler (mp, rv);                        \
144     q = vl_api_client_index_to_input_queue (mp->client_index);  \
145     if (!q)                                                     \
146         return;                                                 \
147                                                                 \
148     rmp = vl_msg_api_alloc (sizeof (*rmp));                     \
149     rmp->_vl_msg_id = ntohs((t));                               \
150     rmp->context = mp->context;                                 \
151     rmp->retval = ntohl(rv);                                    \
152                                                                 \
153     vl_msg_api_send_shmem (q, (u8 *)&rmp);                      \
154 } while(0);
155
156 #define REPLY_MACRO2(t, body)                                   \
157 do {                                                            \
158     unix_shared_memory_queue_t * q;                             \
159     rv = vl_msg_api_pd_handler (mp, rv);                        \
160     q = vl_api_client_index_to_input_queue (mp->client_index);  \
161     if (!q)                                                     \
162         return;                                                 \
163                                                                 \
164     rmp = vl_msg_api_alloc (sizeof (*rmp));                     \
165     rmp->_vl_msg_id = ntohs((t));                               \
166     rmp->context = mp->context;                                 \
167     rmp->retval = ntohl(rv);                                    \
168     do {body;} while (0);                                       \
169     vl_msg_api_send_shmem (q, (u8 *)&rmp);                      \
170 } while(0);
171
172 #define REPLY_MACRO3(t, n, body)                                \
173 do {                                                            \
174     unix_shared_memory_queue_t * q;                             \
175     rv = vl_msg_api_pd_handler (mp, rv);                        \
176     q = vl_api_client_index_to_input_queue (mp->client_index);  \
177     if (!q)                                                     \
178         return;                                                 \
179                                                                 \
180     rmp = vl_msg_api_alloc (sizeof (*rmp) + n);                 \
181     rmp->_vl_msg_id = ntohs((t));                               \
182     rmp->context = mp->context;                                 \
183     rmp->retval = ntohl(rv);                                    \
184     do {body;} while (0);                                       \
185     vl_msg_api_send_shmem (q, (u8 *)&rmp);                      \
186 } while(0);
187
188 #define REPLY_MACRO4(t, n, body)                                \
189 do {                                                            \
190     unix_shared_memory_queue_t * q;                             \
191     u8 is_error = 0;                                            \
192     rv = vl_msg_api_pd_handler (mp, rv);                        \
193     q = vl_api_client_index_to_input_queue (mp->client_index);  \
194     if (!q)                                                     \
195         return;                                                 \
196                                                                 \
197     rmp = vl_msg_api_alloc_or_null (sizeof (*rmp) + n);         \
198     if (!rmp)                                                   \
199       {                                                         \
200         /* if there isn't enough memory, try to allocate */     \
201         /* some at least for returning an error */              \
202         rmp = vl_msg_api_alloc (sizeof (*rmp));                 \
203         if (!rmp)                                               \
204           return;                                               \
205                                                                 \
206         memset (rmp, 0, sizeof (*rmp));                         \
207         rv = VNET_API_ERROR_TABLE_TOO_BIG;                      \
208         is_error = 1;                                           \
209       }                                                         \
210     rmp->_vl_msg_id = ntohs((t));                               \
211     rmp->context = mp->context;                                 \
212     rmp->retval = ntohl(rv);                                    \
213     if (!is_error)                                              \
214       do {body;} while (0);                                     \
215     vl_msg_api_send_shmem (q, (u8 *)&rmp);                      \
216 } while(0);
217
218 #if (1 || CLIB_DEBUG > 0)       /* "trust, but verify" */
219
220 #define VALIDATE_SW_IF_INDEX(mp)                                \
221  do { u32 __sw_if_index = ntohl(mp->sw_if_index);               \
222     vnet_main_t *__vnm = vnet_get_main();                       \
223     if (pool_is_free_index(__vnm->interface_main.sw_interfaces, \
224                            __sw_if_index)) {                    \
225         rv = VNET_API_ERROR_INVALID_SW_IF_INDEX;                \
226         goto bad_sw_if_index;                                   \
227     }                                                           \
228 } while(0);
229
230 #define BAD_SW_IF_INDEX_LABEL                   \
231 do {                                            \
232 bad_sw_if_index:                                \
233     ;                                           \
234 } while (0);
235
236 #define VALIDATE_RX_SW_IF_INDEX(mp)                             \
237  do { u32 __rx_sw_if_index = ntohl(mp->rx_sw_if_index);         \
238     vnet_main_t *__vnm = vnet_get_main();                       \
239     if (pool_is_free_index(__vnm->interface_main.sw_interfaces, \
240                            __rx_sw_if_index)) {                 \
241         rv = VNET_API_ERROR_INVALID_SW_IF_INDEX;                \
242         goto bad_rx_sw_if_index;                                \
243     }                                                           \
244 } while(0);
245
246 #define BAD_RX_SW_IF_INDEX_LABEL                \
247 do {                                            \
248 bad_rx_sw_if_index:                             \
249     ;                                           \
250 } while (0);
251
252 #define VALIDATE_TX_SW_IF_INDEX(mp)                             \
253  do { u32 __tx_sw_if_index = ntohl(mp->tx_sw_if_index);         \
254     vnet_main_t *__vnm = vnet_get_main();                       \
255     if (pool_is_free_index(__vnm->interface_main.sw_interfaces, \
256                            __tx_sw_if_index)) {                 \
257         rv = VNET_API_ERROR_INVALID_SW_IF_INDEX;                \
258         goto bad_tx_sw_if_index;                                \
259     }                                                           \
260 } while(0);
261
262 #define BAD_TX_SW_IF_INDEX_LABEL                \
263 do {                                            \
264 bad_tx_sw_if_index:                             \
265     ;                                           \
266 } while (0);
267
268 #else
269
270 #define VALIDATE_SW_IF_INDEX(mp)
271 #define BAD_SW_IF_INDEX_LABEL
272 #define VALIDATE_RX_SW_IF_INDEX(mp)
273 #define BAD_RX_SW_IF_INDEX_LABEL
274 #define VALIDATE_TX_SW_IF_INDEX(mp)
275 #define BAD_TX_SW_IF_INDEX_LABEL
276
277 #endif /* CLIB_DEBUG > 0 */
278
279 #define foreach_vpe_api_msg                                             \
280 _(WANT_INTERFACE_EVENTS, want_interface_events)                         \
281 _(WANT_OAM_EVENTS, want_oam_events)                                     \
282 _(OAM_ADD_DEL, oam_add_del)                                             \
283 _(SW_INTERFACE_DUMP, sw_interface_dump)                                 \
284 _(SW_INTERFACE_DETAILS, sw_interface_details)                           \
285 _(SW_INTERFACE_SET_FLAGS, sw_interface_set_flags)                       \
286 _(IP_ADD_DEL_ROUTE, ip_add_del_route)                                   \
287 _(MPLS_ROUTE_ADD_DEL, mpls_route_add_del)                               \
288 _(MPLS_IP_BIND_UNBIND, mpls_ip_bind_unbind)                             \
289 _(IS_ADDRESS_REACHABLE, is_address_reachable)                           \
290 _(SW_INTERFACE_ADD_DEL_ADDRESS, sw_interface_add_del_address)           \
291 _(SW_INTERFACE_SET_TABLE, sw_interface_set_table)                       \
292 _(SW_INTERFACE_SET_MPLS_ENABLE, sw_interface_set_mpls_enable)           \
293 _(SW_INTERFACE_SET_VPATH, sw_interface_set_vpath)                       \
294 _(SW_INTERFACE_SET_L2_XCONNECT, sw_interface_set_l2_xconnect)           \
295 _(SW_INTERFACE_SET_L2_BRIDGE, sw_interface_set_l2_bridge)               \
296 _(SW_INTERFACE_SET_DPDK_HQOS_PIPE, sw_interface_set_dpdk_hqos_pipe)     \
297 _(SW_INTERFACE_SET_DPDK_HQOS_SUBPORT, sw_interface_set_dpdk_hqos_subport) \
298 _(SW_INTERFACE_SET_DPDK_HQOS_TCTBL, sw_interface_set_dpdk_hqos_tctbl)   \
299 _(BRIDGE_DOMAIN_ADD_DEL, bridge_domain_add_del)                         \
300 _(BRIDGE_DOMAIN_DUMP, bridge_domain_dump)                               \
301 _(BRIDGE_DOMAIN_DETAILS, bridge_domain_details)                         \
302 _(BRIDGE_DOMAIN_SW_IF_DETAILS, bridge_domain_sw_if_details)             \
303 _(L2FIB_ADD_DEL, l2fib_add_del)                                         \
304 _(L2_FLAGS, l2_flags)                                                   \
305 _(BRIDGE_FLAGS, bridge_flags)                                           \
306 _(TAP_CONNECT, tap_connect)                                             \
307 _(TAP_MODIFY, tap_modify)                                               \
308 _(TAP_DELETE, tap_delete)                                               \
309 _(SW_INTERFACE_TAP_DUMP, sw_interface_tap_dump)                         \
310 _(CREATE_VLAN_SUBIF, create_vlan_subif)                                 \
311 _(CREATE_SUBIF, create_subif)                                           \
312 _(MPLS_ETHERNET_ADD_DEL_TUNNEL, mpls_ethernet_add_del_tunnel)           \
313 _(MPLS_ETHERNET_ADD_DEL_TUNNEL_2, mpls_ethernet_add_del_tunnel_2)       \
314 _(MPLS_ADD_DEL_ENCAP, mpls_add_del_encap)                               \
315 _(PROXY_ARP_ADD_DEL, proxy_arp_add_del)                                 \
316 _(PROXY_ARP_INTFC_ENABLE_DISABLE, proxy_arp_intfc_enable_disable)       \
317 _(IP_NEIGHBOR_ADD_DEL, ip_neighbor_add_del)                             \
318 _(VNET_GET_SUMMARY_STATS, vnet_get_summary_stats)                       \
319 _(RESET_FIB, reset_fib)                                                 \
320 _(DHCP_PROXY_CONFIG,dhcp_proxy_config)                                  \
321 _(DHCP_PROXY_CONFIG_2,dhcp_proxy_config_2)                              \
322 _(DHCP_PROXY_SET_VSS,dhcp_proxy_set_vss)                                \
323 _(DHCP_CLIENT_CONFIG, dhcp_client_config)                               \
324 _(SET_IP_FLOW_HASH,set_ip_flow_hash)                                    \
325 _(SW_INTERFACE_IP6ND_RA_CONFIG, sw_interface_ip6nd_ra_config)           \
326 _(SW_INTERFACE_IP6ND_RA_PREFIX, sw_interface_ip6nd_ra_prefix)           \
327 _(SW_INTERFACE_IP6_ENABLE_DISABLE, sw_interface_ip6_enable_disable )    \
328 _(SW_INTERFACE_IP6_SET_LINK_LOCAL_ADDRESS,                              \
329   sw_interface_ip6_set_link_local_address)                              \
330 _(SW_INTERFACE_SET_UNNUMBERED, sw_interface_set_unnumbered)             \
331 _(CREATE_LOOPBACK, create_loopback)                                     \
332 _(CONTROL_PING, control_ping)                                           \
333 _(CLI_REQUEST, cli_request)                                             \
334 _(CLI_INBAND, cli_inband)                                               \
335 _(SET_ARP_NEIGHBOR_LIMIT, set_arp_neighbor_limit)                       \
336 _(L2_PATCH_ADD_DEL, l2_patch_add_del)                                   \
337 _(CLASSIFY_ADD_DEL_TABLE, classify_add_del_table)                       \
338 _(CLASSIFY_ADD_DEL_SESSION, classify_add_del_session)                   \
339 _(CLASSIFY_SET_INTERFACE_IP_TABLE, classify_set_interface_ip_table)     \
340 _(CLASSIFY_SET_INTERFACE_L2_TABLES, classify_set_interface_l2_tables)   \
341 _(GET_NODE_INDEX, get_node_index)                                       \
342 _(ADD_NODE_NEXT, add_node_next)                                         \
343 _(L2TPV3_CREATE_TUNNEL, l2tpv3_create_tunnel)                           \
344 _(L2TPV3_SET_TUNNEL_COOKIES, l2tpv3_set_tunnel_cookies)                 \
345 _(L2TPV3_INTERFACE_ENABLE_DISABLE, l2tpv3_interface_enable_disable)     \
346 _(L2TPV3_SET_LOOKUP_KEY, l2tpv3_set_lookup_key)                         \
347 _(SW_IF_L2TPV3_TUNNEL_DUMP, sw_if_l2tpv3_tunnel_dump)                   \
348 _(VXLAN_ADD_DEL_TUNNEL, vxlan_add_del_tunnel)                           \
349 _(VXLAN_TUNNEL_DUMP, vxlan_tunnel_dump)                                 \
350 _(GRE_ADD_DEL_TUNNEL, gre_add_del_tunnel)                               \
351 _(GRE_TUNNEL_DUMP, gre_tunnel_dump)                                     \
352 _(L2_FIB_CLEAR_TABLE, l2_fib_clear_table)                               \
353 _(L2_INTERFACE_EFP_FILTER, l2_interface_efp_filter)                     \
354 _(L2_INTERFACE_VLAN_TAG_REWRITE, l2_interface_vlan_tag_rewrite)         \
355 _(CREATE_VHOST_USER_IF, create_vhost_user_if)                           \
356 _(MODIFY_VHOST_USER_IF, modify_vhost_user_if)                           \
357 _(DELETE_VHOST_USER_IF, delete_vhost_user_if)                           \
358 _(SW_INTERFACE_VHOST_USER_DUMP, sw_interface_vhost_user_dump)           \
359 _(IP_ADDRESS_DUMP, ip_address_dump)                                     \
360 _(IP_DUMP, ip_dump)                                                     \
361 _(SW_INTERFACE_VHOST_USER_DETAILS, sw_interface_vhost_user_details)     \
362 _(SHOW_VERSION, show_version)                                           \
363 _(L2_FIB_TABLE_DUMP, l2_fib_table_dump)                                 \
364 _(L2_FIB_TABLE_ENTRY, l2_fib_table_entry)                               \
365 _(VXLAN_GPE_ADD_DEL_TUNNEL, vxlan_gpe_add_del_tunnel)                   \
366 _(VXLAN_GPE_TUNNEL_DUMP, vxlan_gpe_tunnel_dump)                         \
367 _(INTERFACE_NAME_RENUMBER, interface_name_renumber)                     \
368 _(WANT_IP4_ARP_EVENTS, want_ip4_arp_events)                             \
369 _(WANT_IP6_ND_EVENTS, want_ip6_nd_events)                               \
370 _(INPUT_ACL_SET_INTERFACE, input_acl_set_interface)                     \
371 _(IPSEC_SPD_ADD_DEL, ipsec_spd_add_del)                                 \
372 _(IPSEC_INTERFACE_ADD_DEL_SPD, ipsec_interface_add_del_spd)             \
373 _(IPSEC_SPD_ADD_DEL_ENTRY, ipsec_spd_add_del_entry)                     \
374 _(IPSEC_SAD_ADD_DEL_ENTRY, ipsec_sad_add_del_entry)                     \
375 _(IPSEC_SA_SET_KEY, ipsec_sa_set_key)                                   \
376 _(IKEV2_PROFILE_ADD_DEL, ikev2_profile_add_del)                         \
377 _(IKEV2_PROFILE_SET_AUTH, ikev2_profile_set_auth)                       \
378 _(IKEV2_PROFILE_SET_ID, ikev2_profile_set_id)                           \
379 _(IKEV2_PROFILE_SET_TS, ikev2_profile_set_ts)                           \
380 _(IKEV2_SET_LOCAL_KEY, ikev2_set_local_key)                             \
381 _(DELETE_LOOPBACK, delete_loopback)                                     \
382 _(BD_IP_MAC_ADD_DEL, bd_ip_mac_add_del)                                 \
383 _(MAP_ADD_DOMAIN, map_add_domain)                                       \
384 _(MAP_DEL_DOMAIN, map_del_domain)                                       \
385 _(MAP_ADD_DEL_RULE, map_add_del_rule)                                   \
386 _(MAP_DOMAIN_DUMP, map_domain_dump)                                     \
387 _(MAP_RULE_DUMP, map_rule_dump)                                         \
388 _(MAP_SUMMARY_STATS, map_summary_stats)                                 \
389 _(COP_INTERFACE_ENABLE_DISABLE, cop_interface_enable_disable)           \
390 _(COP_WHITELIST_ENABLE_DISABLE, cop_whitelist_enable_disable)           \
391 _(GET_NODE_GRAPH, get_node_graph)                                       \
392 _(SW_INTERFACE_CLEAR_STATS, sw_interface_clear_stats)                   \
393 _(IOAM_ENABLE, ioam_enable)                                 \
394 _(IOAM_DISABLE, ioam_disable)                                 \
395 _(LISP_ADD_DEL_LOCATOR_SET, lisp_add_del_locator_set)                   \
396 _(LISP_ADD_DEL_LOCATOR, lisp_add_del_locator)                           \
397 _(LISP_ADD_DEL_LOCAL_EID, lisp_add_del_local_eid)                       \
398 _(LISP_GPE_ADD_DEL_FWD_ENTRY, lisp_gpe_add_del_fwd_entry)               \
399 _(LISP_ADD_DEL_MAP_RESOLVER, lisp_add_del_map_resolver)                 \
400 _(LISP_GPE_ENABLE_DISABLE, lisp_gpe_enable_disable)                     \
401 _(LISP_ENABLE_DISABLE, lisp_enable_disable)                             \
402 _(LISP_GPE_ADD_DEL_IFACE, lisp_gpe_add_del_iface)                       \
403 _(LISP_ADD_DEL_REMOTE_MAPPING, lisp_add_del_remote_mapping)             \
404 _(LISP_ADD_DEL_ADJACENCY, lisp_add_del_adjacency)                       \
405 _(LISP_PITR_SET_LOCATOR_SET, lisp_pitr_set_locator_set)                 \
406 _(LISP_MAP_REQUEST_MODE, lisp_map_request_mode)                         \
407 _(LISP_EID_TABLE_ADD_DEL_MAP, lisp_eid_table_add_del_map)               \
408 _(LISP_LOCATOR_SET_DUMP, lisp_locator_set_dump)                         \
409 _(LISP_LOCATOR_DUMP, lisp_locator_dump)                                 \
410 _(LISP_EID_TABLE_DUMP, lisp_eid_table_dump)                             \
411 _(LISP_GPE_TUNNEL_DUMP, lisp_gpe_tunnel_dump)                           \
412 _(LISP_MAP_RESOLVER_DUMP, lisp_map_resolver_dump)                       \
413 _(LISP_EID_TABLE_MAP_DUMP, lisp_eid_table_map_dump)                     \
414 _(LISP_EID_TABLE_VNI_DUMP, lisp_eid_table_vni_dump)                     \
415 _(LISP_ADJACENCIES_GET, lisp_adjacencies_get)                           \
416 _(SHOW_LISP_STATUS, show_lisp_status)                                   \
417 _(LISP_ADD_DEL_MAP_REQUEST_ITR_RLOCS,                                   \
418   lisp_add_del_map_request_itr_rlocs)                                   \
419 _(LISP_GET_MAP_REQUEST_ITR_RLOCS, lisp_get_map_request_itr_rlocs)       \
420 _(SHOW_LISP_PITR, show_lisp_pitr)                                       \
421 _(SHOW_LISP_MAP_REQUEST_MODE, show_lisp_map_request_mode)               \
422 _(SR_MULTICAST_MAP_ADD_DEL, sr_multicast_map_add_del)                   \
423 _(AF_PACKET_CREATE, af_packet_create)                                   \
424 _(AF_PACKET_DELETE, af_packet_delete)                                   \
425 _(POLICER_ADD_DEL, policer_add_del)                                     \
426 _(POLICER_DUMP, policer_dump)                                           \
427 _(POLICER_CLASSIFY_SET_INTERFACE, policer_classify_set_interface)       \
428 _(POLICER_CLASSIFY_DUMP, policer_classify_dump)                         \
429 _(NETMAP_CREATE, netmap_create)                                         \
430 _(NETMAP_DELETE, netmap_delete)                                         \
431 _(MPLS_ETH_TUNNEL_DUMP, mpls_eth_tunnel_dump)                           \
432 _(MPLS_ETH_TUNNEL_DETAILS, mpls_eth_tunnel_details)                     \
433 _(MPLS_FIB_ENCAP_DUMP, mpls_fib_encap_dump)                             \
434 _(MPLS_FIB_ENCAP_DETAILS, mpls_fib_encap_details)                       \
435 _(MPLS_FIB_DUMP, mpls_fib_dump)                                         \
436 _(MPLS_FIB_DETAILS, mpls_fib_details)                                   \
437 _(CLASSIFY_TABLE_IDS,classify_table_ids)                                \
438 _(CLASSIFY_TABLE_BY_INTERFACE, classify_table_by_interface)             \
439 _(CLASSIFY_TABLE_INFO,classify_table_info)                              \
440 _(CLASSIFY_SESSION_DUMP,classify_session_dump)                          \
441 _(CLASSIFY_SESSION_DETAILS,classify_session_details)                    \
442 _(SET_IPFIX_EXPORTER, set_ipfix_exporter)                               \
443 _(IPFIX_EXPORTER_DUMP, ipfix_exporter_dump)                             \
444 _(SET_IPFIX_CLASSIFY_STREAM, set_ipfix_classify_stream)                 \
445 _(IPFIX_CLASSIFY_STREAM_DUMP, ipfix_classify_stream_dump)               \
446 _(IPFIX_CLASSIFY_TABLE_ADD_DEL, ipfix_classify_table_add_del)           \
447 _(IPFIX_CLASSIFY_TABLE_DUMP, ipfix_classify_table_dump)                 \
448 _(SW_INTERFACE_SPAN_ENABLE_DISABLE, sw_interface_span_enable_disable)   \
449 _(SW_INTERFACE_SPAN_DUMP, sw_interface_span_dump)                       \
450 _(GET_NEXT_INDEX, get_next_index)                                       \
451 _(PG_CREATE_INTERFACE, pg_create_interface)                             \
452 _(PG_CAPTURE, pg_capture)                                               \
453 _(PG_ENABLE_DISABLE, pg_enable_disable)                                 \
454 _(IP_SOURCE_AND_PORT_RANGE_CHECK_ADD_DEL,                               \
455   ip_source_and_port_range_check_add_del)                               \
456 _(IP_SOURCE_AND_PORT_RANGE_CHECK_INTERFACE_ADD_DEL,                     \
457   ip_source_and_port_range_check_interface_add_del)                     \
458 _(IPSEC_GRE_ADD_DEL_TUNNEL, ipsec_gre_add_del_tunnel)                   \
459 _(IPSEC_GRE_TUNNEL_DUMP, ipsec_gre_tunnel_dump)                         \
460 _(DELETE_SUBIF, delete_subif)                                           \
461 _(L2_INTERFACE_PBB_TAG_REWRITE, l2_interface_pbb_tag_rewrite)           \
462 _(PUNT, punt)                                                           \
463 _(FLOW_CLASSIFY_SET_INTERFACE, flow_classify_set_interface)             \
464 _(FLOW_CLASSIFY_DUMP, flow_classify_dump)                               \
465 _(IPSEC_SPD_DUMP, ipsec_spd_dump)                                       \
466 _(IP_FIB_DUMP, ip_fib_dump)                                             \
467 _(IP_FIB_DETAILS, ip_fib_details)                                       \
468 _(IP6_FIB_DUMP, ip6_fib_dump)                                           \
469 _(IP6_FIB_DETAILS, ip6_fib_details)
470
471 #define QUOTE_(x) #x
472 #define QUOTE(x) QUOTE_(x)
473
474 #define foreach_registration_hash               \
475 _(interface_events)                             \
476 _(to_netconf_server)                            \
477 _(from_netconf_server)                          \
478 _(to_netconf_client)                            \
479 _(from_netconf_client)                          \
480 _(oam_events)
481
482 typedef enum
483 {
484   RESOLVE_IP4_ADD_DEL_ROUTE = 1,
485   RESOLVE_IP6_ADD_DEL_ROUTE,
486   RESOLVE_MPLS_ETHERNET_ADD_DEL,
487 } resolve_t;
488
489 typedef struct
490 {
491   u8 resolve_type;
492   union
493   {
494     vl_api_ip_add_del_route_t r;
495     vl_api_mpls_ethernet_add_del_tunnel_2_t t;
496   };
497 } pending_route_t;
498
499 typedef struct
500 {
501
502 #define _(a) uword *a##_registration_hash;              \
503     vpe_client_registration_t * a##_registrations;
504   foreach_registration_hash
505 #undef _
506     /* notifications happen really early in the game */
507   u8 link_state_process_up;
508
509   /* ip4 and ip6 pending route adds */
510   pending_route_t *pending_routes;
511
512   /* ip4 arp event registration pool */
513   vl_api_ip4_arp_event_t *arp_events;
514
515   /* ip6 nd event registration pool */
516   vl_api_ip6_nd_event_t *nd_events;
517
518   /* convenience */
519   vlib_main_t *vlib_main;
520   vnet_main_t *vnet_main;
521 } vpe_api_main_t;
522
523 static vlib_node_registration_t vpe_resolver_process_node;
524 static vpe_api_main_t vpe_api_main;
525
526 static void send_sw_interface_flags (vpe_api_main_t * am,
527                                      unix_shared_memory_queue_t * q,
528                                      vnet_sw_interface_t * swif);
529 static void send_sw_interface_flags_deleted (vpe_api_main_t * am,
530                                              unix_shared_memory_queue_t * q,
531                                              u32 sw_if_index);
532
533 static int arp_change_delete_callback (u32 pool_index, u8 * notused);
534 static int nd_change_delete_callback (u32 pool_index, u8 * notused);
535
536
537 /* Clean up all registrations belonging to the indicated client */
538 int
539 vl_api_memclnt_delete_callback (u32 client_index)
540 {
541   vpe_api_main_t *vam = &vpe_api_main;
542   vpe_client_registration_t *rp;
543   uword *p;
544   int stats_memclnt_delete_callback (u32 client_index);
545
546   stats_memclnt_delete_callback (client_index);
547
548 #define _(a)                                                    \
549     p = hash_get (vam->a##_registration_hash, client_index);    \
550     if (p) {                                                    \
551         rp = pool_elt_at_index (vam->a##_registrations, p[0]);  \
552         pool_put (vam->a##_registrations, rp);                  \
553         hash_unset (vam->a##_registration_hash, client_index);  \
554     }
555   foreach_registration_hash;
556 #undef _
557   return 0;
558 }
559
560 #define API_LINK_STATE_EVENT 1
561 #define API_ADMIN_UP_DOWN_EVENT 2
562
563 static int
564 event_data_cmp (void *a1, void *a2)
565 {
566   uword *e1 = a1;
567   uword *e2 = a2;
568
569   return (word) e1[0] - (word) e2[0];
570 }
571
572 static uword
573 link_state_process (vlib_main_t * vm,
574                     vlib_node_runtime_t * rt, vlib_frame_t * f)
575 {
576   vpe_api_main_t *vam = &vpe_api_main;
577   vnet_main_t *vnm = vam->vnet_main;
578   vnet_sw_interface_t *swif;
579   uword *event_data = 0;
580   vpe_client_registration_t *reg;
581   int i;
582   u32 prev_sw_if_index;
583   unix_shared_memory_queue_t *q;
584
585   vam->link_state_process_up = 1;
586
587   while (1)
588     {
589       vlib_process_wait_for_event (vm);
590
591       /* Unified list of changed link or admin state sw_if_indices */
592       vlib_process_get_events_with_type
593         (vm, &event_data, API_LINK_STATE_EVENT);
594       vlib_process_get_events_with_type
595         (vm, &event_data, API_ADMIN_UP_DOWN_EVENT);
596
597       /* Sort, so we can eliminate duplicates */
598       vec_sort_with_function (event_data, event_data_cmp);
599
600       prev_sw_if_index = ~0;
601
602       for (i = 0; i < vec_len (event_data); i++)
603         {
604           /* Only one message per swif */
605           if (prev_sw_if_index == event_data[i])
606             continue;
607           prev_sw_if_index = event_data[i];
608
609           /* *INDENT-OFF* */
610           pool_foreach(reg, vam->interface_events_registrations,
611           ({
612             q = vl_api_client_index_to_input_queue (reg->client_index);
613             if (q)
614               {
615                 /* sw_interface may be deleted already */
616                 if (!pool_is_free_index (vnm->interface_main.sw_interfaces,
617                                          event_data[i]))
618                   {
619                     swif = vnet_get_sw_interface (vnm, event_data[i]);
620                     send_sw_interface_flags (vam, q, swif);
621                   }
622               }
623           }));
624           /* *INDENT-ON* */
625         }
626       vec_reset_length (event_data);
627     }
628
629   return 0;
630 }
631
632 static clib_error_t *link_up_down_function (vnet_main_t * vm, u32 hw_if_index,
633                                             u32 flags);
634 static clib_error_t *admin_up_down_function (vnet_main_t * vm,
635                                              u32 hw_if_index, u32 flags);
636
637 /* *INDENT-OFF* */
638 VLIB_REGISTER_NODE (link_state_process_node,static) = {
639   .function = link_state_process,
640   .type = VLIB_NODE_TYPE_PROCESS,
641   .name = "vpe-link-state-process",
642 };
643 /* *INDENT-ON* */
644
645 VNET_SW_INTERFACE_ADMIN_UP_DOWN_FUNCTION (admin_up_down_function);
646 VNET_HW_INTERFACE_LINK_UP_DOWN_FUNCTION (link_up_down_function);
647
648 static clib_error_t *
649 link_up_down_function (vnet_main_t * vm, u32 hw_if_index, u32 flags)
650 {
651   vpe_api_main_t *vam = &vpe_api_main;
652   vnet_hw_interface_t *hi = vnet_get_hw_interface (vm, hw_if_index);
653
654   if (vam->link_state_process_up)
655     vlib_process_signal_event (vam->vlib_main,
656                                link_state_process_node.index,
657                                API_LINK_STATE_EVENT, hi->sw_if_index);
658   return 0;
659 }
660
661 static clib_error_t *
662 admin_up_down_function (vnet_main_t * vm, u32 sw_if_index, u32 flags)
663 {
664   vpe_api_main_t *vam = &vpe_api_main;
665
666   /*
667    * Note: it's perfectly fair to set a subif admin up / admin down.
668    * Note the subtle distinction between this routine and the previous
669    * routine.
670    */
671   if (vam->link_state_process_up)
672     vlib_process_signal_event (vam->vlib_main,
673                                link_state_process_node.index,
674                                API_ADMIN_UP_DOWN_EVENT, sw_if_index);
675   return 0;
676 }
677
678 #define pub_sub_handler(lca,UCA)                                        \
679 static void vl_api_want_##lca##_t_handler (                             \
680     vl_api_want_##lca##_t *mp)                                          \
681 {                                                                       \
682     vpe_api_main_t *vam = &vpe_api_main;                                \
683     vpe_client_registration_t *rp;                                      \
684     vl_api_want_##lca##_reply_t *rmp;                                   \
685     uword *p;                                                           \
686     i32 rv = 0;                                                         \
687                                                                         \
688     p = hash_get (vam->lca##_registration_hash, mp->client_index);      \
689     if (p) {                                                            \
690         if (mp->enable_disable) {                                       \
691             clib_warning ("pid %d: already enabled...", mp->pid);       \
692             rv = VNET_API_ERROR_INVALID_REGISTRATION;                   \
693             goto reply;                                                 \
694         } else {                                                        \
695             rp = pool_elt_at_index (vam->lca##_registrations, p[0]);    \
696             pool_put (vam->lca##_registrations, rp);                    \
697             hash_unset (vam->lca##_registration_hash,                   \
698                 mp->client_index);                                      \
699             goto reply;                                                 \
700         }                                                               \
701     }                                                                   \
702     if (mp->enable_disable == 0) {                                      \
703         clib_warning ("pid %d: already disabled...", mp->pid);          \
704         rv = VNET_API_ERROR_INVALID_REGISTRATION;                       \
705         goto reply;                                                     \
706     }                                                                   \
707     pool_get (vam->lca##_registrations, rp);                            \
708     rp->client_index = mp->client_index;                                \
709     rp->client_pid = mp->pid;                                           \
710     hash_set (vam->lca##_registration_hash, rp->client_index,           \
711               rp - vam->lca##_registrations);                           \
712                                                                         \
713 reply:                                                                  \
714     REPLY_MACRO (VL_API_WANT_##UCA##_REPLY);                            \
715 }
716
717 /* *INDENT-OFF* */
718 pub_sub_handler (interface_events, INTERFACE_EVENTS)
719 pub_sub_handler (oam_events, OAM_EVENTS)
720 /* *INDENT-ON* */
721
722 #define RESOLUTION_EVENT 1
723 #define RESOLUTION_PENDING_EVENT 2
724 #define IP4_ARP_EVENT 3
725 #define IP6_ND_EVENT 4
726
727 static int ip4_add_del_route_t_handler (vl_api_ip_add_del_route_t * mp);
728
729 static int ip6_add_del_route_t_handler (vl_api_ip_add_del_route_t * mp);
730
731 static int mpls_ethernet_add_del_tunnel_2_t_handler
732   (vl_api_mpls_ethernet_add_del_tunnel_2_t * mp);
733
734 void
735 handle_ip4_arp_event (u32 pool_index)
736 {
737   vpe_api_main_t *vam = &vpe_api_main;
738   vnet_main_t *vnm = vam->vnet_main;
739   vlib_main_t *vm = vam->vlib_main;
740   vl_api_ip4_arp_event_t *event;
741   vl_api_ip4_arp_event_t *mp;
742   unix_shared_memory_queue_t *q;
743
744   /* Client can cancel, die, etc. */
745   if (pool_is_free_index (vam->arp_events, pool_index))
746     return;
747
748   event = pool_elt_at_index (vam->arp_events, pool_index);
749
750   q = vl_api_client_index_to_input_queue (event->client_index);
751   if (!q)
752     {
753       (void) vnet_add_del_ip4_arp_change_event
754         (vnm, arp_change_delete_callback,
755          event->pid, &event->address,
756          vpe_resolver_process_node.index, IP4_ARP_EVENT,
757          ~0 /* pool index, notused */ , 0 /* is_add */ );
758       return;
759     }
760
761   if (q->cursize < q->maxsize)
762     {
763       mp = vl_msg_api_alloc (sizeof (*mp));
764       clib_memcpy (mp, event, sizeof (*mp));
765       vl_msg_api_send_shmem (q, (u8 *) & mp);
766     }
767   else
768     {
769       static f64 last_time;
770       /*
771        * Throttle syslog msgs.
772        * It's pretty tempting to just revoke the registration...
773        */
774       if (vlib_time_now (vm) > last_time + 10.0)
775         {
776           clib_warning ("arp event for %U to pid %d: queue stuffed!",
777                         format_ip4_address, &event->address, event->pid);
778           last_time = vlib_time_now (vm);
779         }
780     }
781 }
782
783 void
784 handle_ip6_nd_event (u32 pool_index)
785 {
786   vpe_api_main_t *vam = &vpe_api_main;
787   vnet_main_t *vnm = vam->vnet_main;
788   vlib_main_t *vm = vam->vlib_main;
789   vl_api_ip6_nd_event_t *event;
790   vl_api_ip6_nd_event_t *mp;
791   unix_shared_memory_queue_t *q;
792
793   /* Client can cancel, die, etc. */
794   if (pool_is_free_index (vam->nd_events, pool_index))
795     return;
796
797   event = pool_elt_at_index (vam->nd_events, pool_index);
798
799   q = vl_api_client_index_to_input_queue (event->client_index);
800   if (!q)
801     {
802       (void) vnet_add_del_ip6_nd_change_event
803         (vnm, nd_change_delete_callback,
804          event->pid, &event->address,
805          vpe_resolver_process_node.index, IP6_ND_EVENT,
806          ~0 /* pool index, notused */ , 0 /* is_add */ );
807       return;
808     }
809
810   if (q->cursize < q->maxsize)
811     {
812       mp = vl_msg_api_alloc (sizeof (*mp));
813       clib_memcpy (mp, event, sizeof (*mp));
814       vl_msg_api_send_shmem (q, (u8 *) & mp);
815     }
816   else
817     {
818       static f64 last_time;
819       /*
820        * Throttle syslog msgs.
821        * It's pretty tempting to just revoke the registration...
822        */
823       if (vlib_time_now (vm) > last_time + 10.0)
824         {
825           clib_warning ("ip6 nd event for %U to pid %d: queue stuffed!",
826                         format_ip6_address, &event->address, event->pid);
827           last_time = vlib_time_now (vm);
828         }
829     }
830 }
831
832 static uword
833 resolver_process (vlib_main_t * vm,
834                   vlib_node_runtime_t * rt, vlib_frame_t * f)
835 {
836   uword event_type;
837   uword *event_data = 0;
838   f64 timeout = 100.0;
839   vpe_api_main_t *vam = &vpe_api_main;
840   pending_route_t *pr;
841   vl_api_ip_add_del_route_t *adr;
842   vl_api_mpls_ethernet_add_del_tunnel_2_t *pme;
843   u32 *resolution_failures = 0;
844   int i, rv;
845   clib_error_t *e;
846
847   while (1)
848     {
849       vlib_process_wait_for_event_or_clock (vm, timeout);
850
851       event_type = vlib_process_get_events (vm, &event_data);
852
853       switch (event_type)
854         {
855         case RESOLUTION_PENDING_EVENT:
856           timeout = 1.0;
857           break;
858
859         case RESOLUTION_EVENT:
860           for (i = 0; i < vec_len (event_data); i++)
861             {
862               /*
863                * Resolution events can occur long after the
864                * original request has timed out. $$$ add a cancel
865                * mechanism..
866                */
867               if (pool_is_free_index (vam->pending_routes, event_data[i]))
868                 continue;
869
870               pr = pool_elt_at_index (vam->pending_routes, event_data[i]);
871               adr = &pr->r;
872               pme = &pr->t;
873
874               switch (pr->resolve_type)
875                 {
876                 case RESOLVE_IP4_ADD_DEL_ROUTE:
877                   rv = ip4_add_del_route_t_handler (adr);
878                   clib_warning ("resolver: add %U/%d via %U %s",
879                                 format_ip4_address,
880                                 (ip4_address_t *) & (adr->dst_address),
881                                 adr->dst_address_length,
882                                 format_ip4_address,
883                                 (ip4_address_t *) & (adr->next_hop_address),
884                                 (rv >= 0) ? "succeeded" : "failed");
885                   break;
886
887                 case RESOLVE_IP6_ADD_DEL_ROUTE:
888                   rv = ip6_add_del_route_t_handler (adr);
889                   clib_warning ("resolver: add %U/%d via %U %s",
890                                 format_ip6_address,
891                                 (ip6_address_t *) & (adr->dst_address),
892                                 adr->dst_address_length,
893                                 format_ip6_address,
894                                 (ip6_address_t *) & (adr->next_hop_address),
895                                 (rv >= 0) ? "succeeded" : "failed");
896                   break;
897
898                 case RESOLVE_MPLS_ETHERNET_ADD_DEL:
899                   rv = mpls_ethernet_add_del_tunnel_2_t_handler (pme);
900                   clib_warning ("resolver: add mpls-o-e via %U %s",
901                                 format_ip4_address,
902                                 (ip4_address_t *) &
903                                 (pme->next_hop_ip4_address_in_outer_vrf),
904                                 (rv >= 0) ? "succeeded" : "failed");
905                   break;
906
907                 default:
908                   clib_warning ("resolver: BOGUS TYPE %d", pr->resolve_type);
909                 }
910               pool_put (vam->pending_routes, pr);
911             }
912           break;
913
914         case IP4_ARP_EVENT:
915           for (i = 0; i < vec_len (event_data); i++)
916             handle_ip4_arp_event (event_data[i]);
917           break;
918
919         case IP6_ND_EVENT:
920           for (i = 0; i < vec_len (event_data); i++)
921             handle_ip6_nd_event (event_data[i]);
922           break;
923
924         case ~0:                /* timeout, retry pending resolutions */
925           /* *INDENT-OFF* */
926           pool_foreach (pr, vam->pending_routes,
927           ({
928             int is_adr = 1;
929             adr = &pr->r;
930             pme = &pr->t;
931
932             /* May fail, e.g. due to interface down */
933             switch (pr->resolve_type)
934               {
935               case RESOLVE_IP4_ADD_DEL_ROUTE:
936                 e = ip4_probe_neighbor
937                   (vm, (ip4_address_t *)&(adr->next_hop_address),
938                    ntohl(adr->next_hop_sw_if_index));
939                 break;
940
941               case RESOLVE_IP6_ADD_DEL_ROUTE:
942                 e = ip6_probe_neighbor
943                   (vm, (ip6_address_t *)&(adr->next_hop_address),
944                    ntohl(adr->next_hop_sw_if_index));
945                 break;
946
947               case RESOLVE_MPLS_ETHERNET_ADD_DEL:
948                 is_adr = 0;
949                 e = ip4_probe_neighbor
950                   (vm,
951                    (ip4_address_t *)&(pme->next_hop_ip4_address_in_outer_vrf),
952                    pme->resolve_opaque);
953                 break;
954
955               default:
956                 e = clib_error_return (0, "resolver: BOGUS TYPE %d",
957                                        pr->resolve_type);
958               }
959             if (e)
960               {
961                 clib_error_report (e);
962                 if (is_adr)
963                   adr->resolve_attempts = 1;
964                 else
965                   pme->resolve_attempts = 1;
966               }
967             if (is_adr)
968               {
969                 adr->resolve_attempts -= 1;
970                 if (adr->resolve_attempts == 0)
971                   vec_add1 (resolution_failures,
972                             pr - vam->pending_routes);
973               }
974             else
975               {
976                 pme->resolve_attempts -= 1;
977                 if (pme->resolve_attempts == 0)
978                   vec_add1 (resolution_failures,
979                             pr - vam->pending_routes);
980               }
981           }));
982           /* *INDENT-ON* */
983           for (i = 0; i < vec_len (resolution_failures); i++)
984             {
985               pr = pool_elt_at_index (vam->pending_routes,
986                                       resolution_failures[i]);
987               adr = &pr->r;
988               pme = &pr->t;
989
990               switch (pr->resolve_type)
991                 {
992                 case RESOLVE_IP4_ADD_DEL_ROUTE:
993                   clib_warning ("resolver: add %U/%d via %U retry failure",
994                                 format_ip4_address,
995                                 (ip4_address_t *) & (adr->dst_address),
996                                 adr->dst_address_length,
997                                 format_ip4_address,
998                                 (ip4_address_t *) & (adr->next_hop_address));
999                   break;
1000
1001                 case RESOLVE_IP6_ADD_DEL_ROUTE:
1002                   clib_warning ("resolver: add %U/%d via %U retry failure",
1003                                 format_ip6_address,
1004                                 (ip6_address_t *) & (adr->dst_address),
1005                                 adr->dst_address_length,
1006                                 format_ip6_address,
1007                                 (ip6_address_t *) & (adr->next_hop_address));
1008                   break;
1009
1010                 case RESOLVE_MPLS_ETHERNET_ADD_DEL:
1011                   clib_warning ("resolver: add mpls-o-e via %U retry failure",
1012                                 format_ip4_address,
1013                                 (ip4_address_t *) &
1014                                 (pme->next_hop_ip4_address_in_outer_vrf));
1015                   break;
1016
1017                 default:
1018                   clib_warning ("BUG");
1019                 }
1020               pool_put (vam->pending_routes, pr);
1021             }
1022           vec_reset_length (resolution_failures);
1023           break;
1024         }
1025       if (pool_elts (vam->pending_routes) == 0)
1026         timeout = 100.0;
1027       vec_reset_length (event_data);
1028     }
1029   return 0;                     /* or not */
1030 }
1031
1032 /* *INDENT-OFF* */
1033 VLIB_REGISTER_NODE (vpe_resolver_process_node,static) = {
1034   .function = resolver_process,
1035   .type = VLIB_NODE_TYPE_PROCESS,
1036   .name = "vpe-route-resolver-process",
1037 };
1038 /* *INDENT-ON* */
1039
1040 static int
1041 add_del_route_t_handler (u8 is_multipath,
1042                          u8 is_add,
1043                          u8 is_drop,
1044                          u8 is_unreach,
1045                          u8 is_prohibit,
1046                          u8 is_local,
1047                          u8 is_classify,
1048                          u32 classify_table_index,
1049                          u8 is_resolve_host,
1050                          u8 is_resolve_attached,
1051                          u32 fib_index,
1052                          const fib_prefix_t * prefix,
1053                          u8 next_hop_proto_is_ip4,
1054                          const ip46_address_t * next_hop,
1055                          u32 next_hop_sw_if_index,
1056                          u8 next_hop_fib_index,
1057                          u32 next_hop_weight, u32 next_hop_out_label)
1058 {
1059   vnet_classify_main_t *cm = &vnet_classify_main;
1060   stats_main_t *sm = &stats_main;
1061
1062   if (is_multipath)
1063     {
1064       fib_route_path_flags_t path_flags = FIB_ROUTE_PATH_FLAG_NONE;
1065
1066       dslock (sm, 1 /* release hint */ , 10 /* tag */ );
1067
1068       if (is_resolve_host)
1069         path_flags |= FIB_ROUTE_PATH_RESOLVE_VIA_HOST;
1070       if (is_resolve_attached)
1071         path_flags |= FIB_ROUTE_PATH_RESOLVE_VIA_ATTACHED;
1072
1073       if (is_add)
1074         fib_table_entry_path_add (fib_index,
1075                                   prefix,
1076                                   FIB_SOURCE_API,
1077                                   FIB_ENTRY_FLAG_NONE,
1078                                   (next_hop_proto_is_ip4 ?
1079                                    FIB_PROTOCOL_IP4 :
1080                                    FIB_PROTOCOL_IP6),
1081                                   next_hop,
1082                                   next_hop_sw_if_index,
1083                                   next_hop_fib_index,
1084                                   next_hop_weight,
1085                                   next_hop_out_label, path_flags);
1086       else
1087         fib_table_entry_path_remove (fib_index,
1088                                      prefix,
1089                                      FIB_SOURCE_API,
1090                                      (next_hop_proto_is_ip4 ?
1091                                       FIB_PROTOCOL_IP4 :
1092                                       FIB_PROTOCOL_IP6),
1093                                      next_hop,
1094                                      next_hop_sw_if_index,
1095                                      next_hop_fib_index,
1096                                      next_hop_weight, path_flags);
1097
1098       dsunlock (sm);
1099       return 0;
1100     }
1101
1102   dslock (sm, 1 /* release hint */ , 2 /* tag */ );
1103
1104   if (is_drop || is_local || is_classify || is_unreach || is_prohibit)
1105     {
1106       /*
1107        * special route types that link directly to the adj
1108        */
1109       if (is_add)
1110         {
1111           dpo_id_t dpo = DPO_INVALID;
1112           dpo_proto_t dproto;
1113
1114           dproto = fib_proto_to_dpo (prefix->fp_proto);
1115
1116           if (is_drop)
1117             ip_null_dpo_add_and_lock (dproto, IP_NULL_ACTION_NONE, &dpo);
1118           else if (is_local)
1119             receive_dpo_add_or_lock (dproto, ~0, NULL, &dpo);
1120           else if (is_unreach)
1121             ip_null_dpo_add_and_lock (dproto,
1122                                       IP_NULL_ACTION_SEND_ICMP_UNREACH, &dpo);
1123           else if (is_prohibit)
1124             ip_null_dpo_add_and_lock (dproto,
1125                                       IP_NULL_ACTION_SEND_ICMP_PROHIBIT,
1126                                       &dpo);
1127           else if (is_classify)
1128             {
1129               if (pool_is_free_index (cm->tables,
1130                                       ntohl (classify_table_index)))
1131                 {
1132                   dsunlock (sm);
1133                   return VNET_API_ERROR_NO_SUCH_TABLE;
1134                 }
1135
1136               dpo_set (&dpo, DPO_CLASSIFY, dproto,
1137                        classify_dpo_create (dproto,
1138                                             ntohl (classify_table_index)));
1139             }
1140           else
1141             {
1142               dsunlock (sm);
1143               return VNET_API_ERROR_NO_SUCH_TABLE;
1144             }
1145
1146           fib_table_entry_special_dpo_update (fib_index,
1147                                               prefix,
1148                                               FIB_SOURCE_API,
1149                                               FIB_ENTRY_FLAG_EXCLUSIVE, &dpo);
1150           dpo_reset (&dpo);
1151         }
1152       else
1153         {
1154           fib_table_entry_special_remove (fib_index, prefix, FIB_SOURCE_API);
1155         }
1156     }
1157   else
1158     {
1159       if (is_add)
1160         {
1161           fib_route_path_flags_t path_flags = FIB_ROUTE_PATH_FLAG_NONE;
1162
1163           if (is_resolve_host)
1164             path_flags |= FIB_ROUTE_PATH_RESOLVE_VIA_HOST;
1165           if (is_resolve_attached)
1166             path_flags |= FIB_ROUTE_PATH_RESOLVE_VIA_ATTACHED;
1167
1168           fib_table_entry_update_one_path (fib_index,
1169                                            prefix,
1170                                            FIB_SOURCE_API,
1171                                            FIB_ENTRY_FLAG_NONE,
1172                                            (next_hop_proto_is_ip4 ?
1173                                             FIB_PROTOCOL_IP4 :
1174                                             FIB_PROTOCOL_IP6),
1175                                            next_hop,
1176                                            next_hop_sw_if_index,
1177                                            next_hop_fib_index,
1178                                            next_hop_weight,
1179                                            next_hop_out_label, path_flags);
1180         }
1181       else
1182         {
1183           fib_table_entry_delete (fib_index, prefix, FIB_SOURCE_API);
1184         }
1185     }
1186
1187   dsunlock (sm);
1188   return (0);
1189 }
1190
1191 static int
1192 add_del_route_check (fib_protocol_t table_proto,
1193                      u32 table_id,
1194                      u32 next_hop_sw_if_index,
1195                      fib_protocol_t next_hop_table_proto,
1196                      u32 next_hop_table_id,
1197                      u8 create_missing_tables,
1198                      u32 * fib_index, u32 * next_hop_fib_index)
1199 {
1200   vnet_main_t *vnm = vnet_get_main ();
1201
1202   *fib_index = fib_table_find (table_proto, ntohl (table_id));
1203   if (~0 == *fib_index)
1204     {
1205       if (create_missing_tables)
1206         {
1207           *fib_index = fib_table_find_or_create_and_lock (table_proto,
1208                                                           ntohl (table_id));
1209         }
1210       else
1211         {
1212           /* No such VRF, and we weren't asked to create one */
1213           return VNET_API_ERROR_NO_SUCH_FIB;
1214         }
1215     }
1216
1217   if (~0 != ntohl (next_hop_sw_if_index))
1218     {
1219       if (pool_is_free_index (vnm->interface_main.sw_interfaces,
1220                               ntohl (next_hop_sw_if_index)))
1221         {
1222           return VNET_API_ERROR_NO_MATCHING_INTERFACE;
1223         }
1224     }
1225   else
1226     {
1227       *next_hop_fib_index = fib_table_find (next_hop_table_proto,
1228                                             ntohl (next_hop_table_id));
1229
1230       if (~0 == *next_hop_fib_index)
1231         {
1232           if (create_missing_tables)
1233             {
1234               *next_hop_fib_index =
1235                 fib_table_find_or_create_and_lock (next_hop_table_proto,
1236                                                    ntohl (next_hop_table_id));
1237             }
1238           else
1239             {
1240               /* No such VRF, and we weren't asked to create one */
1241               return VNET_API_ERROR_NO_SUCH_FIB;
1242             }
1243         }
1244     }
1245
1246   return (0);
1247 }
1248
1249 static int
1250 ip4_add_del_route_t_handler (vl_api_ip_add_del_route_t * mp)
1251 {
1252   u32 fib_index, next_hop_fib_index;
1253   int rv;
1254
1255   rv = add_del_route_check (FIB_PROTOCOL_IP4,
1256                             mp->table_id,
1257                             mp->next_hop_sw_if_index,
1258                             FIB_PROTOCOL_IP4,
1259                             mp->next_hop_table_id,
1260                             mp->create_vrf_if_needed,
1261                             &fib_index, &next_hop_fib_index);
1262
1263   if (0 != rv)
1264     return (rv);
1265
1266   fib_prefix_t pfx = {
1267     .fp_len = mp->dst_address_length,
1268     .fp_proto = FIB_PROTOCOL_IP4,
1269   };
1270   clib_memcpy (&pfx.fp_addr.ip4, mp->dst_address, sizeof (pfx.fp_addr.ip4));
1271
1272   ip46_address_t nh;
1273   memset (&nh, 0, sizeof (nh));
1274   memcpy (&nh.ip4, mp->next_hop_address, sizeof (nh.ip4));
1275
1276   return (add_del_route_t_handler (mp->is_multipath,
1277                                    mp->is_add,
1278                                    mp->is_drop,
1279                                    mp->is_unreach,
1280                                    mp->is_prohibit,
1281                                    mp->is_local,
1282                                    mp->is_classify,
1283                                    mp->classify_table_index,
1284                                    mp->is_resolve_host,
1285                                    mp->is_resolve_attached,
1286                                    fib_index, &pfx, 1,
1287                                    &nh,
1288                                    ntohl (mp->next_hop_sw_if_index),
1289                                    next_hop_fib_index,
1290                                    mp->next_hop_weight,
1291                                    ntohl (mp->next_hop_out_label)));
1292 }
1293
1294 static int
1295 ip6_add_del_route_t_handler (vl_api_ip_add_del_route_t * mp)
1296 {
1297   u32 fib_index, next_hop_fib_index;
1298   int rv;
1299
1300   rv = add_del_route_check (FIB_PROTOCOL_IP6,
1301                             mp->table_id,
1302                             mp->next_hop_sw_if_index,
1303                             FIB_PROTOCOL_IP6,
1304                             mp->next_hop_table_id,
1305                             mp->create_vrf_if_needed,
1306                             &fib_index, &next_hop_fib_index);
1307
1308   if (0 != rv)
1309     return (rv);
1310
1311   fib_prefix_t pfx = {
1312     .fp_len = mp->dst_address_length,
1313     .fp_proto = FIB_PROTOCOL_IP6,
1314   };
1315   clib_memcpy (&pfx.fp_addr.ip6, mp->dst_address, sizeof (pfx.fp_addr.ip6));
1316
1317   ip46_address_t nh;
1318   memset (&nh, 0, sizeof (nh));
1319   memcpy (&nh.ip6, mp->next_hop_address, sizeof (nh.ip6));
1320
1321   return (add_del_route_t_handler (mp->is_multipath,
1322                                    mp->is_add,
1323                                    mp->is_drop,
1324                                    mp->is_unreach,
1325                                    mp->is_prohibit,
1326                                    mp->is_local,
1327                                    mp->is_classify,
1328                                    mp->classify_table_index,
1329                                    mp->is_resolve_host,
1330                                    mp->is_resolve_attached,
1331                                    fib_index, &pfx, 0,
1332                                    &nh, ntohl (mp->next_hop_sw_if_index),
1333                                    next_hop_fib_index,
1334                                    mp->next_hop_weight,
1335                                    ntohl (mp->next_hop_out_label)));
1336 }
1337
1338 static int
1339 mpls_route_add_del_t_handler (vnet_main_t * vnm,
1340                               vl_api_mpls_route_add_del_t * mp)
1341 {
1342   u32 fib_index, next_hop_fib_index;
1343
1344   int rv;
1345
1346   fib_prefix_t pfx = {
1347     .fp_len = 21,
1348     .fp_proto = FIB_PROTOCOL_MPLS,
1349     .fp_eos = mp->mr_eos,
1350     .fp_label = ntohl (mp->mr_label),
1351   };
1352   if (pfx.fp_eos)
1353     {
1354       if (mp->mr_next_hop_proto_is_ip4)
1355         {
1356           pfx.fp_payload_proto = DPO_PROTO_IP4;
1357         }
1358       else
1359         {
1360           pfx.fp_payload_proto = DPO_PROTO_IP6;
1361         }
1362     }
1363   else
1364     {
1365       pfx.fp_payload_proto = DPO_PROTO_MPLS;
1366     }
1367
1368   rv = add_del_route_check (FIB_PROTOCOL_MPLS,
1369                             mp->mr_table_id,
1370                             mp->mr_next_hop_sw_if_index,
1371                             dpo_proto_to_fib (pfx.fp_payload_proto),
1372                             mp->mr_next_hop_table_id,
1373                             mp->mr_create_table_if_needed,
1374                             &fib_index, &next_hop_fib_index);
1375
1376   if (0 != rv)
1377     return (rv);
1378
1379   ip46_address_t nh;
1380   memset (&nh, 0, sizeof (nh));
1381
1382   if (mp->mr_next_hop_proto_is_ip4)
1383     memcpy (&nh.ip4, mp->mr_next_hop, sizeof (nh.ip4));
1384   else
1385     memcpy (&nh.ip6, mp->mr_next_hop, sizeof (nh.ip6));
1386
1387   return (add_del_route_t_handler (mp->mr_is_multipath, mp->mr_is_add, 0,       // mp->is_drop,
1388                                    0,   // mp->is_unreach,
1389                                    0,   // mp->is_prohibit,
1390                                    0,   // mp->is_local,
1391                                    mp->mr_is_classify,
1392                                    mp->mr_classify_table_index,
1393                                    mp->mr_is_resolve_host,
1394                                    mp->mr_is_resolve_attached,
1395                                    fib_index, &pfx,
1396                                    mp->mr_next_hop_proto_is_ip4,
1397                                    &nh, ntohl (mp->mr_next_hop_sw_if_index),
1398                                    next_hop_fib_index,
1399                                    mp->mr_next_hop_weight,
1400                                    ntohl (mp->mr_next_hop_out_label)));
1401 }
1402
1403 void
1404 vl_api_ip_add_del_route_t_handler (vl_api_ip_add_del_route_t * mp)
1405 {
1406   vl_api_ip_add_del_route_reply_t *rmp;
1407   int rv;
1408   vnet_main_t *vnm = vnet_get_main ();
1409
1410   vnm->api_errno = 0;
1411
1412   if (mp->is_ipv6)
1413     rv = ip6_add_del_route_t_handler (mp);
1414   else
1415     rv = ip4_add_del_route_t_handler (mp);
1416
1417   rv = (rv == 0) ? vnm->api_errno : rv;
1418
1419   REPLY_MACRO (VL_API_IP_ADD_DEL_ROUTE_REPLY);
1420 }
1421
1422 void
1423 vl_api_mpls_route_add_del_t_handler (vl_api_mpls_route_add_del_t * mp)
1424 {
1425   vl_api_mpls_route_add_del_reply_t *rmp;
1426   vnet_main_t *vnm;
1427   int rv;
1428
1429   vnm = vnet_get_main ();
1430   vnm->api_errno = 0;
1431
1432   rv = mpls_route_add_del_t_handler (vnm, mp);
1433
1434   rv = (rv == 0) ? vnm->api_errno : rv;
1435
1436   REPLY_MACRO (VL_API_MPLS_ROUTE_ADD_DEL_REPLY);
1437 }
1438
1439 static int
1440 mpls_ip_bind_unbind_handler (vnet_main_t * vnm,
1441                              vl_api_mpls_ip_bind_unbind_t * mp)
1442 {
1443   u32 mpls_fib_index, ip_fib_index;
1444
1445   mpls_fib_index =
1446     fib_table_find (FIB_PROTOCOL_MPLS, ntohl (mp->mb_mpls_table_id));
1447
1448   if (~0 == mpls_fib_index)
1449     {
1450       if (mp->mb_create_table_if_needed)
1451         {
1452           mpls_fib_index =
1453             fib_table_find_or_create_and_lock (FIB_PROTOCOL_MPLS,
1454                                                ntohl (mp->mb_mpls_table_id));
1455         }
1456       else
1457         return VNET_API_ERROR_NO_SUCH_FIB;
1458     }
1459
1460   ip_fib_index = fib_table_find ((mp->mb_is_ip4 ?
1461                                   FIB_PROTOCOL_IP4 :
1462                                   FIB_PROTOCOL_IP6),
1463                                  ntohl (mp->mb_ip_table_id));
1464   if (~0 == ip_fib_index)
1465     return VNET_API_ERROR_NO_SUCH_FIB;
1466
1467   fib_prefix_t pfx = {
1468     .fp_len = mp->mb_address_length,
1469   };
1470
1471   if (mp->mb_is_ip4)
1472     {
1473       pfx.fp_proto = FIB_PROTOCOL_IP4;
1474       clib_memcpy (&pfx.fp_addr.ip4, mp->mb_address,
1475                    sizeof (pfx.fp_addr.ip4));
1476     }
1477   else
1478     {
1479       pfx.fp_proto = FIB_PROTOCOL_IP6;
1480       clib_memcpy (&pfx.fp_addr.ip6, mp->mb_address,
1481                    sizeof (pfx.fp_addr.ip6));
1482     }
1483
1484   if (mp->mb_is_bind)
1485     fib_table_entry_local_label_add (ip_fib_index, &pfx,
1486                                      ntohl (mp->mb_label));
1487   else
1488     fib_table_entry_local_label_remove (ip_fib_index, &pfx,
1489                                         ntohl (mp->mb_label));
1490
1491   return (0);
1492 }
1493
1494 void
1495 vl_api_mpls_ip_bind_unbind_t_handler (vl_api_mpls_ip_bind_unbind_t * mp)
1496 {
1497   vl_api_mpls_route_add_del_reply_t *rmp;
1498   vnet_main_t *vnm;
1499   int rv;
1500
1501   vnm = vnet_get_main ();
1502   vnm->api_errno = 0;
1503
1504   rv = mpls_ip_bind_unbind_handler (vnm, mp);
1505
1506   rv = (rv == 0) ? vnm->api_errno : rv;
1507
1508   REPLY_MACRO (VL_API_MPLS_ROUTE_ADD_DEL_REPLY);
1509 }
1510
1511 static void
1512   vl_api_sw_interface_add_del_address_t_handler
1513   (vl_api_sw_interface_add_del_address_t * mp)
1514 {
1515   vlib_main_t *vm = vlib_get_main ();
1516   vl_api_sw_interface_add_del_address_reply_t *rmp;
1517   int rv = 0;
1518   u32 is_del;
1519
1520   VALIDATE_SW_IF_INDEX (mp);
1521
1522   is_del = mp->is_add == 0;
1523
1524   if (mp->del_all)
1525     ip_del_all_interface_addresses (vm, ntohl (mp->sw_if_index));
1526   else if (mp->is_ipv6)
1527     ip6_add_del_interface_address (vm, ntohl (mp->sw_if_index),
1528                                    (void *) mp->address,
1529                                    mp->address_length, is_del);
1530   else
1531     ip4_add_del_interface_address (vm, ntohl (mp->sw_if_index),
1532                                    (void *) mp->address,
1533                                    mp->address_length, is_del);
1534
1535   BAD_SW_IF_INDEX_LABEL;
1536
1537   REPLY_MACRO (VL_API_SW_INTERFACE_ADD_DEL_ADDRESS_REPLY);
1538 }
1539
1540 static void
1541 vl_api_sw_interface_set_table_t_handler (vl_api_sw_interface_set_table_t * mp)
1542 {
1543   int rv = 0;
1544   u32 table_id = ntohl (mp->vrf_id);
1545   u32 sw_if_index = ntohl (mp->sw_if_index);
1546   vl_api_sw_interface_set_table_reply_t *rmp;
1547   stats_main_t *sm = &stats_main;
1548   u32 fib_index;
1549
1550   VALIDATE_SW_IF_INDEX (mp);
1551
1552   dslock (sm, 1 /* release hint */ , 4 /* tag */ );
1553
1554   if (mp->is_ipv6)
1555     {
1556       fib_index = fib_table_find_or_create_and_lock (FIB_PROTOCOL_IP6,
1557                                                      table_id);
1558
1559       vec_validate (ip6_main.fib_index_by_sw_if_index, sw_if_index);
1560       ip6_main.fib_index_by_sw_if_index[sw_if_index] = fib_index;
1561     }
1562   else
1563     {
1564
1565       fib_index = fib_table_find_or_create_and_lock (FIB_PROTOCOL_IP4,
1566                                                      table_id);
1567
1568       vec_validate (ip4_main.fib_index_by_sw_if_index, sw_if_index);
1569       ip4_main.fib_index_by_sw_if_index[sw_if_index] = fib_index;
1570     }
1571   dsunlock (sm);
1572
1573   BAD_SW_IF_INDEX_LABEL;
1574
1575   REPLY_MACRO (VL_API_SW_INTERFACE_SET_TABLE_REPLY);
1576 }
1577
1578 static void
1579 vl_api_sw_interface_set_vpath_t_handler (vl_api_sw_interface_set_vpath_t * mp)
1580 {
1581   vl_api_sw_interface_set_vpath_reply_t *rmp;
1582   int rv = 0;
1583   u32 sw_if_index = ntohl (mp->sw_if_index);
1584
1585   VALIDATE_SW_IF_INDEX (mp);
1586
1587   l2input_intf_bitmap_enable (sw_if_index, L2INPUT_FEAT_VPATH, mp->enable);
1588   vnet_feature_enable_disable ("ip4-unicast", "vpath-input-ip4",
1589                                sw_if_index, mp->enable, 0, 0);
1590   vnet_feature_enable_disable ("ip4-multicast", "vpath-input-ip4",
1591                                sw_if_index, mp->enable, 0, 0);
1592   vnet_feature_enable_disable ("ip6-unicast", "vpath-input-ip6",
1593                                sw_if_index, mp->enable, 0, 0);
1594   vnet_feature_enable_disable ("ip6-multicast", "vpath-input-ip6",
1595                                sw_if_index, mp->enable, 0, 0);
1596
1597   BAD_SW_IF_INDEX_LABEL;
1598
1599   REPLY_MACRO (VL_API_SW_INTERFACE_SET_VPATH_REPLY);
1600 }
1601
1602 static void
1603   vl_api_sw_interface_set_l2_xconnect_t_handler
1604   (vl_api_sw_interface_set_l2_xconnect_t * mp)
1605 {
1606   vl_api_sw_interface_set_l2_xconnect_reply_t *rmp;
1607   int rv = 0;
1608   u32 rx_sw_if_index = ntohl (mp->rx_sw_if_index);
1609   u32 tx_sw_if_index = ntohl (mp->tx_sw_if_index);
1610   vlib_main_t *vm = vlib_get_main ();
1611   vnet_main_t *vnm = vnet_get_main ();
1612
1613   VALIDATE_RX_SW_IF_INDEX (mp);
1614
1615   if (mp->enable)
1616     {
1617       VALIDATE_TX_SW_IF_INDEX (mp);
1618       rv = set_int_l2_mode (vm, vnm, MODE_L2_XC,
1619                             rx_sw_if_index, 0, 0, 0, tx_sw_if_index);
1620     }
1621   else
1622     {
1623       rv = set_int_l2_mode (vm, vnm, MODE_L3, rx_sw_if_index, 0, 0, 0, 0);
1624     }
1625
1626   BAD_RX_SW_IF_INDEX_LABEL;
1627   BAD_TX_SW_IF_INDEX_LABEL;
1628
1629   REPLY_MACRO (VL_API_SW_INTERFACE_SET_L2_XCONNECT_REPLY);
1630 }
1631
1632 static void
1633   vl_api_sw_interface_set_l2_bridge_t_handler
1634   (vl_api_sw_interface_set_l2_bridge_t * mp)
1635 {
1636   bd_main_t *bdm = &bd_main;
1637   vl_api_sw_interface_set_l2_bridge_reply_t *rmp;
1638   int rv = 0;
1639   u32 rx_sw_if_index = ntohl (mp->rx_sw_if_index);
1640   u32 bd_id = ntohl (mp->bd_id);
1641   u32 bd_index;
1642   u32 bvi = mp->bvi;
1643   u8 shg = mp->shg;
1644   vlib_main_t *vm = vlib_get_main ();
1645   vnet_main_t *vnm = vnet_get_main ();
1646
1647   VALIDATE_RX_SW_IF_INDEX (mp);
1648
1649   bd_index = bd_find_or_add_bd_index (bdm, bd_id);
1650
1651   if (mp->enable)
1652     {
1653       //VALIDATE_TX_SW_IF_INDEX(mp);
1654       rv = set_int_l2_mode (vm, vnm, MODE_L2_BRIDGE,
1655                             rx_sw_if_index, bd_index, bvi, shg, 0);
1656     }
1657   else
1658     {
1659       rv = set_int_l2_mode (vm, vnm, MODE_L3, rx_sw_if_index, 0, 0, 0, 0);
1660     }
1661
1662   BAD_RX_SW_IF_INDEX_LABEL;
1663
1664   REPLY_MACRO (VL_API_SW_INTERFACE_SET_L2_BRIDGE_REPLY);
1665 }
1666
1667 static void
1668   vl_api_sw_interface_set_dpdk_hqos_pipe_t_handler
1669   (vl_api_sw_interface_set_dpdk_hqos_pipe_t * mp)
1670 {
1671   vl_api_sw_interface_set_dpdk_hqos_pipe_reply_t *rmp;
1672   int rv = 0;
1673
1674 #if DPDK > 0
1675   dpdk_main_t *dm = &dpdk_main;
1676   dpdk_device_t *xd;
1677
1678   u32 sw_if_index = ntohl (mp->sw_if_index);
1679   u32 subport = ntohl (mp->subport);
1680   u32 pipe = ntohl (mp->pipe);
1681   u32 profile = ntohl (mp->profile);
1682   vnet_hw_interface_t *hw;
1683
1684   VALIDATE_SW_IF_INDEX (mp);
1685
1686   /* hw_if & dpdk device */
1687   hw = vnet_get_sup_hw_interface (dm->vnet_main, sw_if_index);
1688
1689   xd = vec_elt_at_index (dm->devices, hw->dev_instance);
1690
1691   rv = rte_sched_pipe_config (xd->hqos_ht->hqos, subport, pipe, profile);
1692
1693   BAD_SW_IF_INDEX_LABEL;
1694 #else
1695   clib_warning ("setting HQoS pipe parameters without DPDK not implemented");
1696   rv = VNET_API_ERROR_UNIMPLEMENTED;
1697 #endif /* DPDK */
1698
1699   REPLY_MACRO (VL_API_SW_INTERFACE_SET_DPDK_HQOS_PIPE_REPLY);
1700 }
1701
1702 static void
1703   vl_api_sw_interface_set_dpdk_hqos_subport_t_handler
1704   (vl_api_sw_interface_set_dpdk_hqos_subport_t * mp)
1705 {
1706   vl_api_sw_interface_set_dpdk_hqos_subport_reply_t *rmp;
1707   int rv = 0;
1708
1709 #if DPDK > 0
1710   dpdk_main_t *dm = &dpdk_main;
1711   dpdk_device_t *xd;
1712   struct rte_sched_subport_params p;
1713
1714   u32 sw_if_index = ntohl (mp->sw_if_index);
1715   u32 subport = ntohl (mp->subport);
1716   p.tb_rate = ntohl (mp->tb_rate);
1717   p.tb_size = ntohl (mp->tb_size);
1718   p.tc_rate[0] = ntohl (mp->tc_rate[0]);
1719   p.tc_rate[1] = ntohl (mp->tc_rate[1]);
1720   p.tc_rate[2] = ntohl (mp->tc_rate[2]);
1721   p.tc_rate[3] = ntohl (mp->tc_rate[3]);
1722   p.tc_period = ntohl (mp->tc_period);
1723
1724   vnet_hw_interface_t *hw;
1725
1726   VALIDATE_SW_IF_INDEX (mp);
1727
1728   /* hw_if & dpdk device */
1729   hw = vnet_get_sup_hw_interface (dm->vnet_main, sw_if_index);
1730
1731   xd = vec_elt_at_index (dm->devices, hw->dev_instance);
1732
1733   rv = rte_sched_subport_config (xd->hqos_ht->hqos, subport, &p);
1734
1735   BAD_SW_IF_INDEX_LABEL;
1736 #else
1737   clib_warning
1738     ("setting HQoS subport parameters without DPDK not implemented");
1739   rv = VNET_API_ERROR_UNIMPLEMENTED;
1740 #endif /* DPDK */
1741
1742   REPLY_MACRO (VL_API_SW_INTERFACE_SET_DPDK_HQOS_SUBPORT_REPLY);
1743 }
1744
1745 static void
1746   vl_api_sw_interface_set_dpdk_hqos_tctbl_t_handler
1747   (vl_api_sw_interface_set_dpdk_hqos_tctbl_t * mp)
1748 {
1749   vl_api_sw_interface_set_dpdk_hqos_tctbl_reply_t *rmp;
1750   int rv = 0;
1751
1752 #if DPDK > 0
1753   dpdk_main_t *dm = &dpdk_main;
1754   vlib_thread_main_t *tm = vlib_get_thread_main ();
1755   dpdk_device_t *xd;
1756
1757   u32 sw_if_index = ntohl (mp->sw_if_index);
1758   u32 entry = ntohl (mp->entry);
1759   u32 tc = ntohl (mp->tc);
1760   u32 queue = ntohl (mp->queue);
1761   u32 val, i;
1762
1763   vnet_hw_interface_t *hw;
1764
1765   VALIDATE_SW_IF_INDEX (mp);
1766
1767   /* hw_if & dpdk device */
1768   hw = vnet_get_sup_hw_interface (dm->vnet_main, sw_if_index);
1769
1770   xd = vec_elt_at_index (dm->devices, hw->dev_instance);
1771
1772   if (tc >= RTE_SCHED_TRAFFIC_CLASSES_PER_PIPE)
1773     {
1774       clib_warning ("invalid traffic class !!");
1775       rv = VNET_API_ERROR_INVALID_VALUE;
1776       goto done;
1777     }
1778   if (queue >= RTE_SCHED_QUEUES_PER_TRAFFIC_CLASS)
1779     {
1780       clib_warning ("invalid queue !!");
1781       rv = VNET_API_ERROR_INVALID_VALUE;
1782       goto done;
1783     }
1784
1785   /* Detect the set of worker threads */
1786   uword *p = hash_get_mem (tm->thread_registrations_by_name, "workers");
1787
1788   if (p == 0)
1789     {
1790       clib_warning ("worker thread registration AWOL !!");
1791       rv = VNET_API_ERROR_INVALID_VALUE_2;
1792       goto done;
1793     }
1794
1795   vlib_thread_registration_t *tr = (vlib_thread_registration_t *) p[0];
1796   int worker_thread_first = tr->first_index;
1797   int worker_thread_count = tr->count;
1798
1799   val = tc * RTE_SCHED_QUEUES_PER_TRAFFIC_CLASS + queue;
1800   for (i = 0; i < worker_thread_count; i++)
1801     xd->hqos_wt[worker_thread_first + i].hqos_tc_table[entry] = val;
1802
1803   BAD_SW_IF_INDEX_LABEL;
1804 done:
1805 #else
1806   clib_warning ("setting HQoS DSCP table entry without DPDK not implemented");
1807   rv = VNET_API_ERROR_UNIMPLEMENTED;
1808 #endif /* DPDK */
1809
1810   REPLY_MACRO (VL_API_SW_INTERFACE_SET_DPDK_HQOS_TCTBL_REPLY);
1811 }
1812
1813 static void
1814 vl_api_bridge_domain_add_del_t_handler (vl_api_bridge_domain_add_del_t * mp)
1815 {
1816   vlib_main_t *vm = vlib_get_main ();
1817   bd_main_t *bdm = &bd_main;
1818   vl_api_bridge_domain_add_del_reply_t *rmp;
1819   int rv = 0;
1820   u32 enable_flags = 0, disable_flags = 0;
1821   u32 bd_id = ntohl (mp->bd_id);
1822   u32 bd_index;
1823
1824   if (mp->is_add)
1825     {
1826       bd_index = bd_find_or_add_bd_index (bdm, bd_id);
1827
1828       if (mp->flood)
1829         enable_flags |= L2_FLOOD;
1830       else
1831         disable_flags |= L2_FLOOD;
1832
1833       if (mp->uu_flood)
1834         enable_flags |= L2_UU_FLOOD;
1835       else
1836         disable_flags |= L2_UU_FLOOD;
1837
1838       if (mp->forward)
1839         enable_flags |= L2_FWD;
1840       else
1841         disable_flags |= L2_FWD;
1842
1843       if (mp->arp_term)
1844         enable_flags |= L2_ARP_TERM;
1845       else
1846         disable_flags |= L2_ARP_TERM;
1847
1848       if (mp->learn)
1849         enable_flags |= L2_LEARN;
1850       else
1851         disable_flags |= L2_LEARN;
1852
1853       if (enable_flags)
1854         bd_set_flags (vm, bd_index, enable_flags, 1 /* enable */ );
1855
1856       if (disable_flags)
1857         bd_set_flags (vm, bd_index, disable_flags, 0 /* disable */ );
1858
1859     }
1860   else
1861     rv = bd_delete_bd_index (bdm, bd_id);
1862
1863   REPLY_MACRO (VL_API_BRIDGE_DOMAIN_ADD_DEL_REPLY);
1864 }
1865
1866 static void
1867 vl_api_bridge_domain_details_t_handler (vl_api_bridge_domain_details_t * mp)
1868 {
1869   clib_warning ("BUG");
1870 }
1871
1872 static void
1873   vl_api_bridge_domain_sw_if_details_t_handler
1874   (vl_api_bridge_domain_sw_if_details_t * mp)
1875 {
1876   clib_warning ("BUG");
1877 }
1878
1879 static void
1880 send_bridge_domain_details (unix_shared_memory_queue_t * q,
1881                             l2_bridge_domain_t * bd_config,
1882                             u32 n_sw_ifs, u32 context)
1883 {
1884   vl_api_bridge_domain_details_t *mp;
1885
1886   mp = vl_msg_api_alloc (sizeof (*mp));
1887   memset (mp, 0, sizeof (*mp));
1888   mp->_vl_msg_id = ntohs (VL_API_BRIDGE_DOMAIN_DETAILS);
1889   mp->bd_id = ntohl (bd_config->bd_id);
1890   mp->flood = bd_feature_flood (bd_config);
1891   mp->uu_flood = bd_feature_uu_flood (bd_config);
1892   mp->forward = bd_feature_forward (bd_config);
1893   mp->learn = bd_feature_learn (bd_config);
1894   mp->arp_term = bd_feature_arp_term (bd_config);
1895   mp->bvi_sw_if_index = ntohl (bd_config->bvi_sw_if_index);
1896   mp->n_sw_ifs = ntohl (n_sw_ifs);
1897   mp->context = context;
1898
1899   vl_msg_api_send_shmem (q, (u8 *) & mp);
1900 }
1901
1902 static void
1903 send_bd_sw_if_details (l2input_main_t * l2im,
1904                        unix_shared_memory_queue_t * q,
1905                        l2_flood_member_t * member, u32 bd_id, u32 context)
1906 {
1907   vl_api_bridge_domain_sw_if_details_t *mp;
1908   l2_input_config_t *input_cfg;
1909
1910   mp = vl_msg_api_alloc (sizeof (*mp));
1911   memset (mp, 0, sizeof (*mp));
1912   mp->_vl_msg_id = ntohs (VL_API_BRIDGE_DOMAIN_SW_IF_DETAILS);
1913   mp->bd_id = ntohl (bd_id);
1914   mp->sw_if_index = ntohl (member->sw_if_index);
1915   input_cfg = vec_elt_at_index (l2im->configs, member->sw_if_index);
1916   mp->shg = input_cfg->shg;
1917   mp->context = context;
1918
1919   vl_msg_api_send_shmem (q, (u8 *) & mp);
1920 }
1921
1922 static void
1923 vl_api_bridge_domain_dump_t_handler (vl_api_bridge_domain_dump_t * mp)
1924 {
1925   bd_main_t *bdm = &bd_main;
1926   l2input_main_t *l2im = &l2input_main;
1927   unix_shared_memory_queue_t *q;
1928   l2_bridge_domain_t *bd_config;
1929   u32 bd_id, bd_index;
1930   u32 end;
1931
1932   q = vl_api_client_index_to_input_queue (mp->client_index);
1933
1934   if (q == 0)
1935     return;
1936
1937   bd_id = ntohl (mp->bd_id);
1938
1939   bd_index = (bd_id == ~0) ? 0 : bd_find_or_add_bd_index (bdm, bd_id);
1940   end = (bd_id == ~0) ? vec_len (l2im->bd_configs) : bd_index + 1;
1941   for (; bd_index < end; bd_index++)
1942     {
1943       bd_config = l2input_bd_config_from_index (l2im, bd_index);
1944       /* skip dummy bd_id 0 */
1945       if (bd_config && (bd_config->bd_id > 0))
1946         {
1947           u32 n_sw_ifs;
1948           l2_flood_member_t *m;
1949
1950           n_sw_ifs = vec_len (bd_config->members);
1951           send_bridge_domain_details (q, bd_config, n_sw_ifs, mp->context);
1952
1953           vec_foreach (m, bd_config->members)
1954           {
1955             send_bd_sw_if_details (l2im, q, m, bd_config->bd_id, mp->context);
1956           }
1957         }
1958     }
1959 }
1960
1961 static void
1962 vl_api_l2fib_add_del_t_handler (vl_api_l2fib_add_del_t * mp)
1963 {
1964   bd_main_t *bdm = &bd_main;
1965   l2input_main_t *l2im = &l2input_main;
1966   vl_api_l2fib_add_del_reply_t *rmp;
1967   int rv = 0;
1968   u64 mac = 0;
1969   u32 sw_if_index = ntohl (mp->sw_if_index);
1970   u32 bd_id = ntohl (mp->bd_id);
1971   u32 bd_index;
1972   u32 static_mac;
1973   u32 filter_mac;
1974   u32 bvi_mac;
1975   uword *p;
1976
1977   mac = mp->mac;
1978
1979   p = hash_get (bdm->bd_index_by_bd_id, bd_id);
1980   if (!p)
1981     {
1982       rv = VNET_API_ERROR_NO_SUCH_ENTRY;
1983       goto bad_sw_if_index;
1984     }
1985   bd_index = p[0];
1986
1987   if (mp->is_add)
1988     {
1989       VALIDATE_SW_IF_INDEX (mp);
1990       if (vec_len (l2im->configs) <= sw_if_index)
1991         {
1992           rv = VNET_API_ERROR_INVALID_SW_IF_INDEX;
1993           goto bad_sw_if_index;
1994         }
1995       else
1996         {
1997           l2_input_config_t *config;
1998           config = vec_elt_at_index (l2im->configs, sw_if_index);
1999           if (config->bridge == 0)
2000             {
2001               rv = VNET_API_ERROR_INVALID_SW_IF_INDEX;
2002               goto bad_sw_if_index;
2003             }
2004         }
2005       static_mac = mp->static_mac ? 1 : 0;
2006       filter_mac = mp->filter_mac ? 1 : 0;
2007       bvi_mac = mp->bvi_mac ? 1 : 0;
2008       l2fib_add_entry (mac, bd_index, sw_if_index, static_mac, filter_mac,
2009                        bvi_mac);
2010     }
2011   else
2012     {
2013       l2fib_del_entry (mac, bd_index);
2014     }
2015
2016   BAD_SW_IF_INDEX_LABEL;
2017
2018   REPLY_MACRO (VL_API_L2FIB_ADD_DEL_REPLY);
2019 }
2020
2021 static void
2022 vl_api_l2_flags_t_handler (vl_api_l2_flags_t * mp)
2023 {
2024   vl_api_l2_flags_reply_t *rmp;
2025   int rv = 0;
2026   u32 sw_if_index = ntohl (mp->sw_if_index);
2027   u32 flags = ntohl (mp->feature_bitmap);
2028   u32 rbm = 0;
2029
2030   VALIDATE_SW_IF_INDEX (mp);
2031
2032 #define _(a,b) \
2033     if (flags & L2INPUT_FEAT_ ## a) \
2034         rbm = l2input_intf_bitmap_enable (sw_if_index, L2INPUT_FEAT_ ## a, mp->is_set);
2035   foreach_l2input_feat;
2036 #undef _
2037
2038   BAD_SW_IF_INDEX_LABEL;
2039
2040   /* *INDENT-OFF* */
2041   REPLY_MACRO2(VL_API_L2_FLAGS_REPLY,
2042   ({
2043     rmp->resulting_feature_bitmap = ntohl(rbm);
2044   }));
2045   /* *INDENT-ON* */
2046 }
2047
2048 static void
2049 vl_api_bridge_flags_t_handler (vl_api_bridge_flags_t * mp)
2050 {
2051   vlib_main_t *vm = vlib_get_main ();
2052   bd_main_t *bdm = &bd_main;
2053   vl_api_bridge_flags_reply_t *rmp;
2054   int rv = 0;
2055   u32 bd_id = ntohl (mp->bd_id);
2056   u32 bd_index;
2057   u32 flags = ntohl (mp->feature_bitmap);
2058   uword *p;
2059
2060   p = hash_get (bdm->bd_index_by_bd_id, bd_id);
2061   if (p == 0)
2062     {
2063       rv = VNET_API_ERROR_NO_SUCH_ENTRY;
2064       goto out;
2065     }
2066
2067   bd_index = p[0];
2068
2069   bd_set_flags (vm, bd_index, flags, mp->is_set);
2070
2071 out:
2072   /* *INDENT-OFF* */
2073   REPLY_MACRO2(VL_API_BRIDGE_FLAGS_REPLY,
2074   ({
2075     rmp->resulting_feature_bitmap = ntohl(flags);
2076   }));
2077   /* *INDENT-ON* */
2078 }
2079
2080 static void
2081 vl_api_bd_ip_mac_add_del_t_handler (vl_api_bd_ip_mac_add_del_t * mp)
2082 {
2083   bd_main_t *bdm = &bd_main;
2084   vl_api_bd_ip_mac_add_del_reply_t *rmp;
2085   int rv = 0;
2086   u32 bd_id = ntohl (mp->bd_id);
2087   u32 bd_index;
2088   uword *p;
2089
2090   p = hash_get (bdm->bd_index_by_bd_id, bd_id);
2091   if (p == 0)
2092     {
2093       rv = VNET_API_ERROR_NO_SUCH_ENTRY;
2094       goto out;
2095     }
2096
2097   bd_index = p[0];
2098   if (bd_add_del_ip_mac (bd_index, mp->ip_address,
2099                          mp->mac_address, mp->is_ipv6, mp->is_add))
2100     rv = VNET_API_ERROR_UNSPECIFIED;
2101
2102 out:
2103   REPLY_MACRO (VL_API_BD_IP_MAC_ADD_DEL_REPLY);
2104 }
2105
2106 static void
2107 vl_api_tap_connect_t_handler (vl_api_tap_connect_t * mp, vlib_main_t * vm)
2108 {
2109   int rv;
2110   vl_api_tap_connect_reply_t *rmp;
2111   unix_shared_memory_queue_t *q;
2112   u32 sw_if_index = (u32) ~ 0;
2113
2114   rv = vnet_tap_connect_renumber (vm, mp->tap_name,
2115                                   mp->use_random_mac ? 0 : mp->mac_address,
2116                                   &sw_if_index, mp->renumber,
2117                                   ntohl (mp->custom_dev_instance));
2118
2119   q = vl_api_client_index_to_input_queue (mp->client_index);
2120   if (!q)
2121     return;
2122
2123   rmp = vl_msg_api_alloc (sizeof (*rmp));
2124   rmp->_vl_msg_id = ntohs (VL_API_TAP_CONNECT_REPLY);
2125   rmp->context = mp->context;
2126   rmp->retval = ntohl (rv);
2127   rmp->sw_if_index = ntohl (sw_if_index);
2128
2129   vl_msg_api_send_shmem (q, (u8 *) & rmp);
2130 }
2131
2132 static void
2133 vl_api_tap_modify_t_handler (vl_api_tap_modify_t * mp, vlib_main_t * vm)
2134 {
2135   int rv;
2136   vl_api_tap_modify_reply_t *rmp;
2137   unix_shared_memory_queue_t *q;
2138   u32 sw_if_index = (u32) ~ 0;
2139
2140   rv = vnet_tap_modify (vm, ntohl (mp->sw_if_index), mp->tap_name,
2141                         mp->use_random_mac ? 0 : mp->mac_address,
2142                         &sw_if_index, mp->renumber,
2143                         ntohl (mp->custom_dev_instance));
2144
2145   q = vl_api_client_index_to_input_queue (mp->client_index);
2146   if (!q)
2147     return;
2148
2149   rmp = vl_msg_api_alloc (sizeof (*rmp));
2150   rmp->_vl_msg_id = ntohs (VL_API_TAP_MODIFY_REPLY);
2151   rmp->context = mp->context;
2152   rmp->retval = ntohl (rv);
2153   rmp->sw_if_index = ntohl (sw_if_index);
2154
2155   vl_msg_api_send_shmem (q, (u8 *) & rmp);
2156 }
2157
2158 static void
2159 vl_api_tap_delete_t_handler (vl_api_tap_delete_t * mp, vlib_main_t * vm)
2160 {
2161   int rv;
2162   vpe_api_main_t *vam = &vpe_api_main;
2163   vl_api_tap_delete_reply_t *rmp;
2164   unix_shared_memory_queue_t *q;
2165   u32 sw_if_index = ntohl (mp->sw_if_index);
2166
2167   rv = vnet_tap_delete (vm, sw_if_index);
2168
2169   q = vl_api_client_index_to_input_queue (mp->client_index);
2170   if (!q)
2171     return;
2172
2173   rmp = vl_msg_api_alloc (sizeof (*rmp));
2174   rmp->_vl_msg_id = ntohs (VL_API_TAP_DELETE_REPLY);
2175   rmp->context = mp->context;
2176   rmp->retval = ntohl (rv);
2177
2178   vl_msg_api_send_shmem (q, (u8 *) & rmp);
2179
2180   if (!rv)
2181     send_sw_interface_flags_deleted (vam, q, sw_if_index);
2182 }
2183
2184 static void
2185 vl_api_create_vlan_subif_t_handler (vl_api_create_vlan_subif_t * mp)
2186 {
2187   vl_api_create_vlan_subif_reply_t *rmp;
2188   vnet_main_t *vnm = vnet_get_main ();
2189   u32 hw_if_index, sw_if_index = (u32) ~ 0;
2190   vnet_hw_interface_t *hi;
2191   int rv = 0;
2192   u32 id;
2193   vnet_sw_interface_t template;
2194   uword *p;
2195   vnet_interface_main_t *im = &vnm->interface_main;
2196   u64 sup_and_sub_key;
2197   u64 *kp;
2198   unix_shared_memory_queue_t *q;
2199   clib_error_t *error;
2200
2201   VALIDATE_SW_IF_INDEX (mp);
2202
2203   hw_if_index = ntohl (mp->sw_if_index);
2204   hi = vnet_get_hw_interface (vnm, hw_if_index);
2205
2206   id = ntohl (mp->vlan_id);
2207   if (id == 0 || id > 4095)
2208     {
2209       rv = VNET_API_ERROR_INVALID_VLAN;
2210       goto out;
2211     }
2212
2213   sup_and_sub_key = ((u64) (hi->sw_if_index) << 32) | (u64) id;
2214
2215   p = hash_get_mem (im->sw_if_index_by_sup_and_sub, &sup_and_sub_key);
2216   if (p)
2217     {
2218       rv = VNET_API_ERROR_VLAN_ALREADY_EXISTS;
2219       goto out;
2220     }
2221
2222   kp = clib_mem_alloc (sizeof (*kp));
2223   *kp = sup_and_sub_key;
2224
2225   memset (&template, 0, sizeof (template));
2226   template.type = VNET_SW_INTERFACE_TYPE_SUB;
2227   template.sup_sw_if_index = hi->sw_if_index;
2228   template.sub.id = id;
2229   template.sub.eth.raw_flags = 0;
2230   template.sub.eth.flags.one_tag = 1;
2231   template.sub.eth.outer_vlan_id = id;
2232   template.sub.eth.flags.exact_match = 1;
2233
2234   error = vnet_create_sw_interface (vnm, &template, &sw_if_index);
2235   if (error)
2236     {
2237       clib_error_report (error);
2238       rv = VNET_API_ERROR_INVALID_REGISTRATION;
2239       goto out;
2240     }
2241   hash_set (hi->sub_interface_sw_if_index_by_id, id, sw_if_index);
2242   hash_set_mem (im->sw_if_index_by_sup_and_sub, kp, sw_if_index);
2243
2244   BAD_SW_IF_INDEX_LABEL;
2245
2246 out:
2247   q = vl_api_client_index_to_input_queue (mp->client_index);
2248   if (!q)
2249     return;
2250
2251   rmp = vl_msg_api_alloc (sizeof (*rmp));
2252   rmp->_vl_msg_id = ntohs (VL_API_CREATE_VLAN_SUBIF_REPLY);
2253   rmp->context = mp->context;
2254   rmp->retval = ntohl (rv);
2255   rmp->sw_if_index = ntohl (sw_if_index);
2256   vl_msg_api_send_shmem (q, (u8 *) & rmp);
2257 }
2258
2259 static void
2260 vl_api_create_subif_t_handler (vl_api_create_subif_t * mp)
2261 {
2262   vl_api_create_subif_reply_t *rmp;
2263   vnet_main_t *vnm = vnet_get_main ();
2264   u32 sw_if_index = ~0;
2265   int rv = 0;
2266   u32 sub_id;
2267   vnet_sw_interface_t *si;
2268   vnet_hw_interface_t *hi;
2269   vnet_sw_interface_t template;
2270   uword *p;
2271   vnet_interface_main_t *im = &vnm->interface_main;
2272   u64 sup_and_sub_key;
2273   u64 *kp;
2274   clib_error_t *error;
2275
2276   VALIDATE_SW_IF_INDEX (mp);
2277
2278   si = vnet_get_sup_sw_interface (vnm, ntohl (mp->sw_if_index));
2279   hi = vnet_get_sup_hw_interface (vnm, ntohl (mp->sw_if_index));
2280
2281   if (hi->bond_info == VNET_HW_INTERFACE_BOND_INFO_SLAVE)
2282     {
2283       rv = VNET_API_ERROR_BOND_SLAVE_NOT_ALLOWED;
2284       goto out;
2285     }
2286
2287   sw_if_index = si->sw_if_index;
2288   sub_id = ntohl (mp->sub_id);
2289
2290   sup_and_sub_key = ((u64) (sw_if_index) << 32) | (u64) sub_id;
2291
2292   p = hash_get_mem (im->sw_if_index_by_sup_and_sub, &sup_and_sub_key);
2293   if (p)
2294     {
2295       if (CLIB_DEBUG > 0)
2296         clib_warning ("sup sw_if_index %d, sub id %d already exists\n",
2297                       sw_if_index, sub_id);
2298       rv = VNET_API_ERROR_SUBIF_ALREADY_EXISTS;
2299       goto out;
2300     }
2301
2302   kp = clib_mem_alloc (sizeof (*kp));
2303   *kp = sup_and_sub_key;
2304
2305   memset (&template, 0, sizeof (template));
2306   template.type = VNET_SW_INTERFACE_TYPE_SUB;
2307   template.sup_sw_if_index = sw_if_index;
2308   template.sub.id = sub_id;
2309   template.sub.eth.flags.no_tags = mp->no_tags;
2310   template.sub.eth.flags.one_tag = mp->one_tag;
2311   template.sub.eth.flags.two_tags = mp->two_tags;
2312   template.sub.eth.flags.dot1ad = mp->dot1ad;
2313   template.sub.eth.flags.exact_match = mp->exact_match;
2314   template.sub.eth.flags.default_sub = mp->default_sub;
2315   template.sub.eth.flags.outer_vlan_id_any = mp->outer_vlan_id_any;
2316   template.sub.eth.flags.inner_vlan_id_any = mp->inner_vlan_id_any;
2317   template.sub.eth.outer_vlan_id = ntohs (mp->outer_vlan_id);
2318   template.sub.eth.inner_vlan_id = ntohs (mp->inner_vlan_id);
2319
2320   error = vnet_create_sw_interface (vnm, &template, &sw_if_index);
2321   if (error)
2322     {
2323       clib_error_report (error);
2324       rv = VNET_API_ERROR_SUBIF_CREATE_FAILED;
2325       goto out;
2326     }
2327
2328   hash_set (hi->sub_interface_sw_if_index_by_id, sub_id, sw_if_index);
2329   hash_set_mem (im->sw_if_index_by_sup_and_sub, kp, sw_if_index);
2330
2331   BAD_SW_IF_INDEX_LABEL;
2332
2333 out:
2334
2335   /* *INDENT-OFF* */
2336   REPLY_MACRO2(VL_API_CREATE_SUBIF_REPLY,
2337   ({
2338     rmp->sw_if_index = ntohl(sw_if_index);
2339   }));
2340   /* *INDENT-ON* */
2341 }
2342
2343 static void
2344   vl_api_mpls_ethernet_add_del_tunnel_t_handler
2345   (vl_api_mpls_ethernet_add_del_tunnel_t * mp)
2346 {
2347   vl_api_mpls_ethernet_add_del_tunnel_reply_t *rmp;
2348   int rv = 0;
2349   stats_main_t *sm = &stats_main;
2350   u32 tunnel_sw_if_index;
2351
2352   dslock (sm, 1 /* release hint */ , 5 /* tag */ );
2353
2354   rv = vnet_mpls_ethernet_add_del_tunnel
2355     (mp->dst_mac_address, (ip4_address_t *) (mp->adj_address),
2356      (u32) (mp->adj_address_length), ntohl (mp->vrf_id),
2357      ntohl (mp->tx_sw_if_index),
2358      &tunnel_sw_if_index, mp->l2_only, mp->is_add);
2359
2360   dsunlock (sm);
2361
2362   /* *INDENT-OFF* */
2363   REPLY_MACRO2(VL_API_MPLS_ETHERNET_ADD_DEL_TUNNEL_REPLY,
2364   ({
2365     rmp->tunnel_sw_if_index = ntohl(tunnel_sw_if_index);
2366   }));
2367   /* *INDENT-ON* */
2368 }
2369
2370 /*
2371  * This piece of misery brought to you because the control-plane
2372  * can't figure out the tx interface + dst-mac address all by itself
2373  */
2374 static int mpls_ethernet_add_del_tunnel_2_t_handler
2375   (vl_api_mpls_ethernet_add_del_tunnel_2_t * mp)
2376 {
2377   pending_route_t *pr;
2378   vl_api_mpls_ethernet_add_del_tunnel_2_t *pme;
2379   vnet_main_t *vnm = vnet_get_main ();
2380   vlib_main_t *vm = vlib_get_main ();
2381   stats_main_t *sm = &stats_main;
2382   vpe_api_main_t *vam = &vpe_api_main;
2383   u32 inner_fib_index, outer_fib_index;
2384   ip4_main_t *im = &ip4_main;
2385   ip_lookup_main_t *lm = &im->lookup_main;
2386   ip_adjacency_t *adj = 0;
2387   u32 lookup_result;
2388   u32 tx_sw_if_index;
2389   u8 *dst_mac_address;
2390   clib_error_t *e;
2391   uword *p;
2392   int rv;
2393   u32 tunnel_sw_if_index;
2394
2395   p = hash_get (im->fib_index_by_table_id, ntohl (mp->outer_vrf_id));
2396   if (!p)
2397     return VNET_API_ERROR_NO_SUCH_FIB;
2398   else
2399     outer_fib_index = p[0];
2400
2401
2402   p = hash_get (im->fib_index_by_table_id, ntohl (mp->inner_vrf_id));
2403   if (!p)
2404     return VNET_API_ERROR_NO_SUCH_INNER_FIB;
2405   else
2406     inner_fib_index = p[0];
2407
2408   if (inner_fib_index == outer_fib_index)
2409     return VNET_API_ERROR_INVALID_VALUE;
2410
2411   // FIXME not an ADJ
2412   lookup_result = ip4_fib_table_lookup_lb (ip4_fib_get (outer_fib_index),
2413                                            (ip4_address_t *)
2414                                            mp->next_hop_ip4_address_in_outer_vrf);
2415
2416   adj = ip_get_adjacency (lm, lookup_result);
2417   tx_sw_if_index = adj->rewrite_header.sw_if_index;
2418
2419   if (mp->is_add && mp->resolve_if_needed)
2420     {
2421       if (adj->lookup_next_index == IP_LOOKUP_NEXT_ARP)
2422         {
2423           pool_get (vam->pending_routes, pr);
2424           pr->resolve_type = RESOLVE_MPLS_ETHERNET_ADD_DEL;
2425           pme = &pr->t;
2426           clib_memcpy (pme, mp, sizeof (*pme));
2427           /* recursion block, "just in case" */
2428           pme->resolve_if_needed = 0;
2429           pme->resolve_attempts = ntohl (mp->resolve_attempts);
2430           pme->resolve_opaque = tx_sw_if_index;
2431           vnet_register_ip4_arp_resolution_event
2432             (vnm,
2433              (ip4_address_t *) & (pme->next_hop_ip4_address_in_outer_vrf),
2434              vpe_resolver_process_node.index,
2435              RESOLUTION_EVENT, pr - vam->pending_routes);
2436
2437           vlib_process_signal_event
2438             (vm, vpe_resolver_process_node.index,
2439              RESOLUTION_PENDING_EVENT, 0 /* data */ );
2440
2441           /* The interface may be down, etc. */
2442           e = ip4_probe_neighbor
2443             (vm, (ip4_address_t *) & (mp->next_hop_ip4_address_in_outer_vrf),
2444              tx_sw_if_index);
2445
2446           if (e)
2447             clib_error_report (e);
2448
2449           return VNET_API_ERROR_IN_PROGRESS;
2450         }
2451     }
2452
2453   if (adj->lookup_next_index != IP_LOOKUP_NEXT_REWRITE)
2454     return VNET_API_ERROR_NEXT_HOP_NOT_IN_FIB;
2455
2456   dst_mac_address =
2457     vnet_rewrite_get_data_internal
2458     (&adj->rewrite_header, sizeof (adj->rewrite_data));
2459
2460   dslock (sm, 1 /* release hint */ , 10 /* tag */ );
2461
2462   rv = vnet_mpls_ethernet_add_del_tunnel
2463     (dst_mac_address, (ip4_address_t *) (mp->adj_address),
2464      (u32) (mp->adj_address_length), ntohl (mp->inner_vrf_id),
2465      tx_sw_if_index, &tunnel_sw_if_index, mp->l2_only, mp->is_add);
2466
2467   dsunlock (sm);
2468
2469   return rv;
2470 }
2471
2472 static void
2473   vl_api_mpls_ethernet_add_del_tunnel_2_t_handler
2474   (vl_api_mpls_ethernet_add_del_tunnel_2_t * mp)
2475 {
2476   vl_api_mpls_ethernet_add_del_tunnel_reply_t *rmp;
2477   int rv = 0;
2478
2479   rv = mpls_ethernet_add_del_tunnel_2_t_handler (mp);
2480
2481   REPLY_MACRO (VL_API_MPLS_ETHERNET_ADD_DEL_TUNNEL_2_REPLY);
2482 }
2483
2484
2485 static void
2486 vl_api_mpls_add_del_encap_t_handler (vl_api_mpls_add_del_encap_t * mp)
2487 {
2488   vl_api_mpls_add_del_encap_reply_t *rmp;
2489   int rv;
2490   static u32 *labels;
2491   int i;
2492
2493   vec_reset_length (labels);
2494
2495   for (i = 0; i < mp->nlabels; i++)
2496     vec_add1 (labels, ntohl (mp->labels[i]));
2497
2498   /* $$$$ fixme */
2499   rv = vnet_mpls_add_del_encap ((ip4_address_t *) mp->dst_address,
2500                                 ntohl (mp->vrf_id), labels,
2501                                 ~0 /* policy_tunnel_index */ ,
2502                                 0 /* no_dst_hash */ ,
2503                                 0 /* indexp */ ,
2504                                 mp->is_add);
2505
2506   REPLY_MACRO (VL_API_MPLS_ADD_DEL_ENCAP_REPLY);
2507 }
2508
2509 static void
2510 vl_api_proxy_arp_add_del_t_handler (vl_api_proxy_arp_add_del_t * mp)
2511 {
2512   vl_api_proxy_arp_add_del_reply_t *rmp;
2513   u32 fib_index;
2514   int rv;
2515   ip4_main_t *im = &ip4_main;
2516   stats_main_t *sm = &stats_main;
2517   int vnet_proxy_arp_add_del (ip4_address_t * lo_addr,
2518                               ip4_address_t * hi_addr,
2519                               u32 fib_index, int is_del);
2520   uword *p;
2521
2522   dslock (sm, 1 /* release hint */ , 6 /* tag */ );
2523
2524   p = hash_get (im->fib_index_by_table_id, ntohl (mp->vrf_id));
2525
2526   if (!p)
2527     {
2528       rv = VNET_API_ERROR_NO_SUCH_FIB;
2529       goto out;
2530     }
2531
2532   fib_index = p[0];
2533
2534   rv = vnet_proxy_arp_add_del ((ip4_address_t *) mp->low_address,
2535                                (ip4_address_t *) mp->hi_address,
2536                                fib_index, mp->is_add == 0);
2537
2538 out:
2539   dsunlock (sm);
2540   REPLY_MACRO (VL_API_PROXY_ARP_ADD_DEL_REPLY);
2541 }
2542
2543 static void
2544   vl_api_proxy_arp_intfc_enable_disable_t_handler
2545   (vl_api_proxy_arp_intfc_enable_disable_t * mp)
2546 {
2547   int rv = 0;
2548   vnet_main_t *vnm = vnet_get_main ();
2549   vl_api_proxy_arp_intfc_enable_disable_reply_t *rmp;
2550   vnet_sw_interface_t *si;
2551   u32 sw_if_index;
2552
2553   VALIDATE_SW_IF_INDEX (mp);
2554
2555   sw_if_index = ntohl (mp->sw_if_index);
2556
2557   if (pool_is_free_index (vnm->interface_main.sw_interfaces, sw_if_index))
2558     {
2559       rv = VNET_API_ERROR_INVALID_SW_IF_INDEX;
2560       goto out;
2561     }
2562
2563   si = vnet_get_sw_interface (vnm, sw_if_index);
2564
2565   ASSERT (si);
2566
2567   if (mp->enable_disable)
2568     si->flags |= VNET_SW_INTERFACE_FLAG_PROXY_ARP;
2569   else
2570     si->flags &= ~VNET_SW_INTERFACE_FLAG_PROXY_ARP;
2571
2572   BAD_SW_IF_INDEX_LABEL;
2573
2574 out:
2575   REPLY_MACRO (VL_API_PROXY_ARP_INTFC_ENABLE_DISABLE_REPLY);
2576 }
2577
2578 static void
2579 vl_api_ip_neighbor_add_del_t_handler (vl_api_ip_neighbor_add_del_t * mp,
2580                                       vlib_main_t * vm)
2581 {
2582   vl_api_ip_neighbor_add_del_reply_t *rmp;
2583   vnet_main_t *vnm = vnet_get_main ();
2584   stats_main_t *sm = &stats_main;
2585   int rv = 0;
2586
2587   VALIDATE_SW_IF_INDEX (mp);
2588
2589   dslock (sm, 1 /* release hint */ , 7 /* tag */ );
2590
2591   /*
2592    * there's no validation here of the ND/ARP entry being added.
2593    * The expectation is that the FIB will ensure that nothing bad
2594    * will come of adding bogus entries.
2595    */
2596   if (mp->is_ipv6)
2597     {
2598       if (mp->is_add)
2599         rv = vnet_set_ip6_ethernet_neighbor
2600           (vm, ntohl (mp->sw_if_index),
2601            (ip6_address_t *) (mp->dst_address),
2602            mp->mac_address, sizeof (mp->mac_address), mp->is_static);
2603       else
2604         rv = vnet_unset_ip6_ethernet_neighbor
2605           (vm, ntohl (mp->sw_if_index),
2606            (ip6_address_t *) (mp->dst_address),
2607            mp->mac_address, sizeof (mp->mac_address));
2608     }
2609   else
2610     {
2611       ethernet_arp_ip4_over_ethernet_address_t a;
2612
2613       clib_memcpy (&a.ethernet, mp->mac_address, 6);
2614       clib_memcpy (&a.ip4, mp->dst_address, 4);
2615
2616       if (mp->is_add)
2617         rv = vnet_arp_set_ip4_over_ethernet (vnm, ntohl (mp->sw_if_index),
2618                                              &a, mp->is_static);
2619       else
2620         rv =
2621           vnet_arp_unset_ip4_over_ethernet (vnm, ntohl (mp->sw_if_index), &a);
2622     }
2623
2624   BAD_SW_IF_INDEX_LABEL;
2625
2626   dsunlock (sm);
2627   REPLY_MACRO (VL_API_IP_NEIGHBOR_ADD_DEL_REPLY);
2628 }
2629
2630 static void
2631 vl_api_is_address_reachable_t_handler (vl_api_is_address_reachable_t * mp)
2632 {
2633 #if 0
2634   vpe_main_t *rm = &vpe_main;
2635   ip4_main_t *im4 = &ip4_main;
2636   ip6_main_t *im6 = &ip6_main;
2637   ip_lookup_main_t *lm;
2638   union
2639   {
2640     ip4_address_t ip4;
2641     ip6_address_t ip6;
2642   } addr;
2643   u32 adj_index, sw_if_index;
2644   vl_api_is_address_reachable_t *rmp;
2645   ip_adjacency_t *adj;
2646   unix_shared_memory_queue_t *q;
2647
2648   q = vl_api_client_index_to_input_queue (mp->client_index);
2649   if (!q)
2650     {
2651       increment_missing_api_client_counter (rm->vlib_main);
2652       return;
2653     }
2654
2655   rmp = vl_msg_api_alloc (sizeof (*rmp));
2656   clib_memcpy (rmp, mp, sizeof (*rmp));
2657
2658   sw_if_index = mp->next_hop_sw_if_index;
2659   clib_memcpy (&addr, mp->address, sizeof (addr));
2660   if (mp->is_ipv6)
2661     {
2662       lm = &im6->lookup_main;
2663       adj_index = ip6_fib_lookup (im6, sw_if_index, &addr.ip6);
2664     }
2665   else
2666     {
2667       lm = &im4->lookup_main;
2668       // FIXME NOT an ADJ
2669       adj_index = ip4_fib_lookup (im4, sw_if_index, &addr.ip4);
2670     }
2671   if (adj_index == ~0)
2672     {
2673       rmp->is_error = 1;
2674       goto send;
2675     }
2676   adj = ip_get_adjacency (lm, adj_index);
2677
2678   if (adj->lookup_next_index == IP_LOOKUP_NEXT_REWRITE
2679       && adj->rewrite_header.sw_if_index == sw_if_index)
2680     {
2681       rmp->is_known = 1;
2682     }
2683   else
2684     {
2685       if (adj->lookup_next_index == IP_LOOKUP_NEXT_ARP
2686           && adj->rewrite_header.sw_if_index == sw_if_index)
2687         {
2688           if (mp->is_ipv6)
2689             ip6_probe_neighbor (rm->vlib_main, &addr.ip6, sw_if_index);
2690           else
2691             ip4_probe_neighbor (rm->vlib_main, &addr.ip4, sw_if_index);
2692         }
2693       else if (adj->lookup_next_index == IP_LOOKUP_NEXT_DROP)
2694         {
2695           rmp->is_known = 1;
2696           goto send;
2697         }
2698       rmp->is_known = 0;
2699     }
2700
2701 send:
2702   vl_msg_api_send_shmem (q, (u8 *) & rmp);
2703 #endif
2704 }
2705
2706 static void
2707 vl_api_sw_interface_details_t_handler (vl_api_sw_interface_details_t * mp)
2708 {
2709   clib_warning ("BUG");
2710 }
2711
2712 static void
2713 vl_api_sw_interface_set_flags_t_handler (vl_api_sw_interface_set_flags_t * mp)
2714 {
2715   vl_api_sw_interface_set_flags_reply_t *rmp;
2716   vnet_main_t *vnm = vnet_get_main ();
2717   int rv = 0;
2718   clib_error_t *error;
2719   u16 flags;
2720
2721   VALIDATE_SW_IF_INDEX (mp);
2722
2723   flags = mp->admin_up_down ? VNET_SW_INTERFACE_FLAG_ADMIN_UP : 0;
2724
2725   error = vnet_sw_interface_set_flags (vnm, ntohl (mp->sw_if_index), flags);
2726   if (error)
2727     {
2728       rv = -1;
2729       clib_error_report (error);
2730     }
2731
2732   BAD_SW_IF_INDEX_LABEL;
2733   REPLY_MACRO (VL_API_SW_INTERFACE_SET_FLAGS_REPLY);
2734 }
2735
2736 static void
2737   vl_api_sw_interface_set_mpls_enable_t_handler
2738   (vl_api_sw_interface_set_mpls_enable_t * mp)
2739 {
2740   vl_api_sw_interface_set_mpls_enable_reply_t *rmp;
2741   int rv = 0;
2742
2743   VALIDATE_SW_IF_INDEX (mp);
2744
2745   mpls_sw_interface_enable_disable (&mpls_main,
2746                                     ntohl (mp->sw_if_index), mp->enable);
2747
2748   BAD_SW_IF_INDEX_LABEL;
2749   REPLY_MACRO (VL_API_SW_INTERFACE_SET_MPLS_ENABLE_REPLY);
2750 }
2751
2752 static void
2753 vl_api_sw_interface_clear_stats_t_handler (vl_api_sw_interface_clear_stats_t *
2754                                            mp)
2755 {
2756   vl_api_sw_interface_clear_stats_reply_t *rmp;
2757
2758   vnet_main_t *vnm = vnet_get_main ();
2759   vnet_interface_main_t *im = &vnm->interface_main;
2760   vlib_simple_counter_main_t *sm;
2761   vlib_combined_counter_main_t *cm;
2762   static vnet_main_t **my_vnet_mains;
2763   int i, j, n_counters;
2764   int rv = 0;
2765
2766   if (mp->sw_if_index != ~0)
2767     VALIDATE_SW_IF_INDEX (mp);
2768
2769   vec_reset_length (my_vnet_mains);
2770
2771   for (i = 0; i < vec_len (vnet_mains); i++)
2772     {
2773       if (vnet_mains[i])
2774         vec_add1 (my_vnet_mains, vnet_mains[i]);
2775     }
2776
2777   if (vec_len (vnet_mains) == 0)
2778     vec_add1 (my_vnet_mains, vnm);
2779
2780   n_counters = vec_len (im->combined_sw_if_counters);
2781
2782   for (j = 0; j < n_counters; j++)
2783     {
2784       for (i = 0; i < vec_len (my_vnet_mains); i++)
2785         {
2786           im = &my_vnet_mains[i]->interface_main;
2787           cm = im->combined_sw_if_counters + j;
2788           if (mp->sw_if_index == (u32) ~ 0)
2789             vlib_clear_combined_counters (cm);
2790           else
2791             vlib_zero_combined_counter (cm, ntohl (mp->sw_if_index));
2792         }
2793     }
2794
2795   n_counters = vec_len (im->sw_if_counters);
2796
2797   for (j = 0; j < n_counters; j++)
2798     {
2799       for (i = 0; i < vec_len (my_vnet_mains); i++)
2800         {
2801           im = &my_vnet_mains[i]->interface_main;
2802           sm = im->sw_if_counters + j;
2803           if (mp->sw_if_index == (u32) ~ 0)
2804             vlib_clear_simple_counters (sm);
2805           else
2806             vlib_zero_simple_counter (sm, ntohl (mp->sw_if_index));
2807         }
2808     }
2809
2810   BAD_SW_IF_INDEX_LABEL;
2811
2812   REPLY_MACRO (VL_API_SW_INTERFACE_CLEAR_STATS_REPLY);
2813 }
2814
2815 static void
2816 send_sw_interface_details (vpe_api_main_t * am,
2817                            unix_shared_memory_queue_t * q,
2818                            vnet_sw_interface_t * swif,
2819                            u8 * interface_name, u32 context)
2820 {
2821   vl_api_sw_interface_details_t *mp;
2822   vnet_hw_interface_t *hi;
2823
2824   hi = vnet_get_sup_hw_interface (am->vnet_main, swif->sw_if_index);
2825
2826   mp = vl_msg_api_alloc (sizeof (*mp));
2827   memset (mp, 0, sizeof (*mp));
2828   mp->_vl_msg_id = ntohs (VL_API_SW_INTERFACE_DETAILS);
2829   mp->sw_if_index = ntohl (swif->sw_if_index);
2830   mp->sup_sw_if_index = ntohl (swif->sup_sw_if_index);
2831   mp->admin_up_down = (swif->flags & VNET_SW_INTERFACE_FLAG_ADMIN_UP) ? 1 : 0;
2832   mp->link_up_down = (hi->flags & VNET_HW_INTERFACE_FLAG_LINK_UP) ? 1 : 0;
2833   mp->link_duplex = ((hi->flags & VNET_HW_INTERFACE_FLAG_DUPLEX_MASK) >>
2834                      VNET_HW_INTERFACE_FLAG_DUPLEX_SHIFT);
2835   mp->link_speed = ((hi->flags & VNET_HW_INTERFACE_FLAG_SPEED_MASK) >>
2836                     VNET_HW_INTERFACE_FLAG_SPEED_SHIFT);
2837   mp->link_mtu = ntohs (hi->max_packet_bytes);
2838   mp->context = context;
2839
2840   strncpy ((char *) mp->interface_name,
2841            (char *) interface_name, ARRAY_LEN (mp->interface_name) - 1);
2842
2843   /* Send the L2 address for ethernet physical intfcs */
2844   if (swif->sup_sw_if_index == swif->sw_if_index
2845       && hi->hw_class_index == ethernet_hw_interface_class.index)
2846     {
2847       ethernet_main_t *em = ethernet_get_main (am->vlib_main);
2848       ethernet_interface_t *ei;
2849
2850       ei = pool_elt_at_index (em->interfaces, hi->hw_instance);
2851       ASSERT (sizeof (mp->l2_address) >= sizeof (ei->address));
2852       clib_memcpy (mp->l2_address, ei->address, sizeof (ei->address));
2853       mp->l2_address_length = ntohl (sizeof (ei->address));
2854     }
2855   else if (swif->sup_sw_if_index != swif->sw_if_index)
2856     {
2857       vnet_sub_interface_t *sub = &swif->sub;
2858       mp->sub_id = ntohl (sub->id);
2859       mp->sub_dot1ad = sub->eth.flags.dot1ad;
2860       mp->sub_number_of_tags =
2861         sub->eth.flags.one_tag + sub->eth.flags.two_tags * 2;
2862       mp->sub_outer_vlan_id = ntohs (sub->eth.outer_vlan_id);
2863       mp->sub_inner_vlan_id = ntohs (sub->eth.inner_vlan_id);
2864       mp->sub_exact_match = sub->eth.flags.exact_match;
2865       mp->sub_default = sub->eth.flags.default_sub;
2866       mp->sub_outer_vlan_id_any = sub->eth.flags.outer_vlan_id_any;
2867       mp->sub_inner_vlan_id_any = sub->eth.flags.inner_vlan_id_any;
2868
2869       /* vlan tag rewrite data */
2870       u32 vtr_op = L2_VTR_DISABLED;
2871       u32 vtr_push_dot1q = 0, vtr_tag1 = 0, vtr_tag2 = 0;
2872
2873       if (l2vtr_get (am->vlib_main, am->vnet_main, swif->sw_if_index,
2874                      &vtr_op, &vtr_push_dot1q, &vtr_tag1, &vtr_tag2) != 0)
2875         {
2876           // error - default to disabled
2877           mp->vtr_op = ntohl (L2_VTR_DISABLED);
2878           clib_warning ("cannot get vlan tag rewrite for sw_if_index %d",
2879                         swif->sw_if_index);
2880         }
2881       else
2882         {
2883           mp->vtr_op = ntohl (vtr_op);
2884           mp->vtr_push_dot1q = ntohl (vtr_push_dot1q);
2885           mp->vtr_tag1 = ntohl (vtr_tag1);
2886           mp->vtr_tag2 = ntohl (vtr_tag2);
2887         }
2888     }
2889
2890   vl_msg_api_send_shmem (q, (u8 *) & mp);
2891 }
2892
2893 static void
2894 send_sw_interface_flags (vpe_api_main_t * am,
2895                          unix_shared_memory_queue_t * q,
2896                          vnet_sw_interface_t * swif)
2897 {
2898   vl_api_sw_interface_set_flags_t *mp;
2899   vnet_main_t *vnm = am->vnet_main;
2900
2901   vnet_hw_interface_t *hi = vnet_get_sup_hw_interface (vnm,
2902                                                        swif->sw_if_index);
2903   mp = vl_msg_api_alloc (sizeof (*mp));
2904   memset (mp, 0, sizeof (*mp));
2905   mp->_vl_msg_id = ntohs (VL_API_SW_INTERFACE_SET_FLAGS);
2906   mp->sw_if_index = ntohl (swif->sw_if_index);
2907
2908   mp->admin_up_down = (swif->flags & VNET_SW_INTERFACE_FLAG_ADMIN_UP) ? 1 : 0;
2909   mp->link_up_down = (hi->flags & VNET_HW_INTERFACE_FLAG_LINK_UP) ? 1 : 0;
2910   vl_msg_api_send_shmem (q, (u8 *) & mp);
2911 }
2912
2913 static void send_sw_interface_flags_deleted (vpe_api_main_t * am,
2914                                              unix_shared_memory_queue_t * q,
2915                                              u32 sw_if_index)
2916   __attribute__ ((unused));
2917
2918 static void
2919 send_sw_interface_flags_deleted (vpe_api_main_t * am,
2920                                  unix_shared_memory_queue_t * q,
2921                                  u32 sw_if_index)
2922 {
2923   vl_api_sw_interface_set_flags_t *mp;
2924
2925   mp = vl_msg_api_alloc (sizeof (*mp));
2926   memset (mp, 0, sizeof (*mp));
2927   mp->_vl_msg_id = ntohs (VL_API_SW_INTERFACE_SET_FLAGS);
2928   mp->sw_if_index = ntohl (sw_if_index);
2929
2930   mp->admin_up_down = 0;
2931   mp->link_up_down = 0;
2932   mp->deleted = 1;
2933   vl_msg_api_send_shmem (q, (u8 *) & mp);
2934 }
2935
2936 static void
2937 vl_api_sw_interface_dump_t_handler (vl_api_sw_interface_dump_t * mp)
2938 {
2939   vpe_api_main_t *am = &vpe_api_main;
2940   vnet_sw_interface_t *swif;
2941   vnet_interface_main_t *im = &am->vnet_main->interface_main;
2942   u8 *filter_string = 0, *name_string = 0;
2943   unix_shared_memory_queue_t *q;
2944   char *strcasestr (char *, char *);    /* lnx hdr file botch */
2945
2946   q = vl_api_client_index_to_input_queue (mp->client_index);
2947
2948   if (q == 0)
2949     return;
2950
2951   if (mp->name_filter_valid)
2952     {
2953       mp->name_filter[ARRAY_LEN (mp->name_filter) - 1] = 0;
2954       filter_string = format (0, "%s%c", mp->name_filter, 0);
2955     }
2956
2957   /* *INDENT-OFF* */
2958   pool_foreach (swif, im->sw_interfaces,
2959   ({
2960     name_string = format (name_string, "%U%c",
2961                           format_vnet_sw_interface_name,
2962                           am->vnet_main, swif, 0);
2963
2964     if (mp->name_filter_valid == 0 ||
2965         strcasestr((char *) name_string, (char *) filter_string)) {
2966
2967       send_sw_interface_details (am, q, swif, name_string, mp->context);
2968     }
2969     _vec_len (name_string) = 0;
2970   }));
2971   /* *INDENT-ON* */
2972
2973   vec_free (name_string);
2974   vec_free (filter_string);
2975 }
2976
2977 void
2978 send_oam_event (oam_target_t * t)
2979 {
2980   vpe_api_main_t *vam = &vpe_api_main;
2981   unix_shared_memory_queue_t *q;
2982   vpe_client_registration_t *reg;
2983   vl_api_oam_event_t *mp;
2984
2985   /* *INDENT-OFF* */
2986   pool_foreach(reg, vam->oam_events_registrations,
2987   ({
2988     q = vl_api_client_index_to_input_queue (reg->client_index);
2989     if (q)
2990       {
2991         mp = vl_msg_api_alloc (sizeof (*mp));
2992         mp->_vl_msg_id = ntohs (VL_API_OAM_EVENT);
2993         clib_memcpy (mp->dst_address, &t->dst_address,
2994                      sizeof (mp->dst_address));
2995         mp->state = t->state;
2996         vl_msg_api_send_shmem (q, (u8 *)&mp);
2997       }
2998   }));
2999   /* *INDENT-ON* */
3000 }
3001
3002 static void
3003 vl_api_oam_add_del_t_handler (vl_api_oam_add_del_t * mp)
3004 {
3005   vl_api_oam_add_del_reply_t *rmp;
3006   int rv;
3007
3008   rv = vpe_oam_add_del_target ((ip4_address_t *) mp->src_address,
3009                                (ip4_address_t *) mp->dst_address,
3010                                ntohl (mp->vrf_id), (int) (mp->is_add));
3011
3012   REPLY_MACRO (VL_API_OAM_ADD_DEL_REPLY);
3013 }
3014
3015 static void
3016 vl_api_vnet_get_summary_stats_t_handler (vl_api_vnet_get_summary_stats_t * mp)
3017 {
3018   stats_main_t *sm = &stats_main;
3019   vnet_interface_main_t *im = sm->interface_main;
3020   vl_api_vnet_summary_stats_reply_t *rmp;
3021   vlib_combined_counter_main_t *cm;
3022   vlib_counter_t v;
3023   int i, which;
3024   u64 total_pkts[VLIB_N_RX_TX];
3025   u64 total_bytes[VLIB_N_RX_TX];
3026
3027   unix_shared_memory_queue_t *q =
3028     vl_api_client_index_to_input_queue (mp->client_index);
3029
3030   if (!q)
3031     return;
3032
3033   rmp = vl_msg_api_alloc (sizeof (*rmp));
3034   rmp->_vl_msg_id = ntohs (VL_API_VNET_SUMMARY_STATS_REPLY);
3035   rmp->context = mp->context;
3036   rmp->retval = 0;
3037
3038   memset (total_pkts, 0, sizeof (total_pkts));
3039   memset (total_bytes, 0, sizeof (total_bytes));
3040
3041   vnet_interface_counter_lock (im);
3042
3043   vec_foreach (cm, im->combined_sw_if_counters)
3044   {
3045     which = cm - im->combined_sw_if_counters;
3046
3047     for (i = 0; i < vec_len (cm->maxi); i++)
3048       {
3049         vlib_get_combined_counter (cm, i, &v);
3050         total_pkts[which] += v.packets;
3051         total_bytes[which] += v.bytes;
3052       }
3053   }
3054   vnet_interface_counter_unlock (im);
3055
3056   rmp->total_pkts[VLIB_RX] = clib_host_to_net_u64 (total_pkts[VLIB_RX]);
3057   rmp->total_bytes[VLIB_RX] = clib_host_to_net_u64 (total_bytes[VLIB_RX]);
3058   rmp->total_pkts[VLIB_TX] = clib_host_to_net_u64 (total_pkts[VLIB_TX]);
3059   rmp->total_bytes[VLIB_TX] = clib_host_to_net_u64 (total_bytes[VLIB_TX]);
3060   rmp->vector_rate =
3061     clib_host_to_net_u64 (vlib_last_vector_length_per_node (sm->vlib_main));
3062
3063   vl_msg_api_send_shmem (q, (u8 *) & rmp);
3064 }
3065
3066 /* *INDENT-OFF* */
3067 typedef CLIB_PACKED (struct {
3068   ip4_address_t address;
3069   u32 address_length: 6;
3070   u32 index:26;
3071 }) ip4_route_t;
3072 /* *INDENT-ON* */
3073
3074 static int
3075 ip4_reset_fib_t_handler (vl_api_reset_fib_t * mp)
3076 {
3077   vnet_main_t *vnm = vnet_get_main ();
3078   vnet_interface_main_t *im = &vnm->interface_main;
3079   ip4_main_t *im4 = &ip4_main;
3080   static u32 *sw_if_indices_to_shut;
3081   stats_main_t *sm = &stats_main;
3082   fib_table_t *fib_table;
3083   ip4_fib_t *fib;
3084   u32 sw_if_index;
3085   int i;
3086   int rv = VNET_API_ERROR_NO_SUCH_FIB;
3087   u32 target_fib_id = ntohl (mp->vrf_id);
3088
3089   dslock (sm, 1 /* release hint */ , 8 /* tag */ );
3090
3091   /* *INDENT-OFF* */
3092   pool_foreach (fib_table, im4->fibs,
3093   ({
3094     fib = &fib_table->v4;
3095     vnet_sw_interface_t * si;
3096
3097     if (fib->table_id != target_fib_id)
3098       continue;
3099
3100     /* remove any mpls encap/decap labels */
3101     mpls_fib_reset_labels (fib->table_id);
3102
3103     /* remove any proxy arps in this fib */
3104     vnet_proxy_arp_fib_reset (fib->table_id);
3105
3106     /* Set the flow hash for this fib to the default */
3107     vnet_set_ip4_flow_hash (fib->table_id, IP_FLOW_HASH_DEFAULT);
3108
3109     vec_reset_length (sw_if_indices_to_shut);
3110
3111     /* Shut down interfaces in this FIB / clean out intfc routes */
3112     pool_foreach (si, im->sw_interfaces,
3113     ({
3114       u32 sw_if_index = si->sw_if_index;
3115
3116       if (sw_if_index < vec_len (im4->fib_index_by_sw_if_index)
3117           && (im4->fib_index_by_sw_if_index[si->sw_if_index] ==
3118               fib->index))
3119         vec_add1 (sw_if_indices_to_shut, si->sw_if_index);
3120     }));
3121
3122     for (i = 0; i < vec_len (sw_if_indices_to_shut); i++) {
3123       sw_if_index = sw_if_indices_to_shut[i];
3124       // vec_foreach (sw_if_index, sw_if_indices_to_shut) {
3125
3126       u32 flags = vnet_sw_interface_get_flags (vnm, sw_if_index);
3127       flags &= ~(VNET_SW_INTERFACE_FLAG_ADMIN_UP);
3128       vnet_sw_interface_set_flags (vnm, sw_if_index, flags);
3129     }
3130
3131     fib_table_flush(fib->index, FIB_PROTOCOL_IP4, FIB_SOURCE_API);
3132     fib_table_flush(fib->index, FIB_PROTOCOL_IP4, FIB_SOURCE_INTERFACE);
3133
3134     rv = 0;
3135     break;
3136     })); /* pool_foreach (fib) */
3137     /* *INDENT-ON* */
3138
3139   dsunlock (sm);
3140   return rv;
3141 }
3142
3143 static int
3144 ip6_reset_fib_t_handler (vl_api_reset_fib_t * mp)
3145 {
3146   vnet_main_t *vnm = vnet_get_main ();
3147   vnet_interface_main_t *im = &vnm->interface_main;
3148   ip6_main_t *im6 = &ip6_main;
3149   stats_main_t *sm = &stats_main;
3150   static u32 *sw_if_indices_to_shut;
3151   fib_table_t *fib_table;
3152   ip6_fib_t *fib;
3153   u32 sw_if_index;
3154   int i;
3155   int rv = VNET_API_ERROR_NO_SUCH_FIB;
3156   u32 target_fib_id = ntohl (mp->vrf_id);
3157
3158   dslock (sm, 1 /* release hint */ , 9 /* tag */ );
3159
3160   /* *INDENT-OFF* */
3161   pool_foreach (fib_table, im6->fibs,
3162   ({
3163     vnet_sw_interface_t * si;
3164     fib = &(fib_table->v6);
3165
3166     if (fib->table_id != target_fib_id)
3167       continue;
3168
3169     vec_reset_length (sw_if_indices_to_shut);
3170
3171     /* Shut down interfaces in this FIB / clean out intfc routes */
3172     pool_foreach (si, im->sw_interfaces,
3173                   ({
3174                     if (im6->fib_index_by_sw_if_index[si->sw_if_index] ==
3175                         fib->index)
3176                       vec_add1 (sw_if_indices_to_shut, si->sw_if_index);
3177                   }));
3178
3179     for (i = 0; i < vec_len (sw_if_indices_to_shut); i++) {
3180       sw_if_index = sw_if_indices_to_shut[i];
3181       // vec_foreach (sw_if_index, sw_if_indices_to_shut) {
3182
3183       u32 flags = vnet_sw_interface_get_flags (vnm, sw_if_index);
3184       flags &= ~(VNET_SW_INTERFACE_FLAG_ADMIN_UP);
3185       vnet_sw_interface_set_flags (vnm, sw_if_index, flags);
3186     }
3187
3188     fib_table_flush(fib->index, FIB_PROTOCOL_IP6, FIB_SOURCE_API);
3189     fib_table_flush(fib->index, FIB_PROTOCOL_IP6, FIB_SOURCE_INTERFACE);
3190
3191     rv = 0;
3192     break;
3193   })); /* pool_foreach (fib) */
3194   /* *INDENT-ON* */
3195
3196   dsunlock (sm);
3197   return rv;
3198 }
3199
3200 static void
3201 vl_api_reset_fib_t_handler (vl_api_reset_fib_t * mp)
3202 {
3203   int rv;
3204   vl_api_reset_fib_reply_t *rmp;
3205
3206   if (mp->is_ipv6)
3207     rv = ip6_reset_fib_t_handler (mp);
3208   else
3209     rv = ip4_reset_fib_t_handler (mp);
3210
3211   REPLY_MACRO (VL_API_RESET_FIB_REPLY);
3212 }
3213
3214
3215 static void
3216 dhcpv4_proxy_config (vl_api_dhcp_proxy_config_t * mp)
3217 {
3218   vl_api_dhcp_proxy_config_reply_t *rmp;
3219   int rv;
3220
3221   rv = dhcp_proxy_set_server ((ip4_address_t *) (&mp->dhcp_server),
3222                               (ip4_address_t *) (&mp->dhcp_src_address),
3223                               (u32) ntohl (mp->vrf_id),
3224                               (int) mp->insert_circuit_id,
3225                               (int) (mp->is_add == 0));
3226
3227   REPLY_MACRO (VL_API_DHCP_PROXY_CONFIG_REPLY);
3228 }
3229
3230
3231 static void
3232 dhcpv6_proxy_config (vl_api_dhcp_proxy_config_t * mp)
3233 {
3234   vl_api_dhcp_proxy_config_reply_t *rmp;
3235   int rv = -1;
3236
3237   rv = dhcpv6_proxy_set_server ((ip6_address_t *) (&mp->dhcp_server),
3238                                 (ip6_address_t *) (&mp->dhcp_src_address),
3239                                 (u32) ntohl (mp->vrf_id),
3240                                 (int) mp->insert_circuit_id,
3241                                 (int) (mp->is_add == 0));
3242
3243   REPLY_MACRO (VL_API_DHCP_PROXY_CONFIG_REPLY);
3244 }
3245
3246 static void
3247 dhcpv4_proxy_config_2 (vl_api_dhcp_proxy_config_2_t * mp)
3248 {
3249   vl_api_dhcp_proxy_config_reply_t *rmp;
3250   int rv;
3251
3252   rv = dhcp_proxy_set_server_2 ((ip4_address_t *) (&mp->dhcp_server),
3253                                 (ip4_address_t *) (&mp->dhcp_src_address),
3254                                 (u32) ntohl (mp->rx_vrf_id),
3255                                 (u32) ntohl (mp->server_vrf_id),
3256                                 (int) mp->insert_circuit_id,
3257                                 (int) (mp->is_add == 0));
3258
3259   REPLY_MACRO (VL_API_DHCP_PROXY_CONFIG_2_REPLY);
3260 }
3261
3262
3263 static void
3264 dhcpv6_proxy_config_2 (vl_api_dhcp_proxy_config_2_t * mp)
3265 {
3266   vl_api_dhcp_proxy_config_reply_t *rmp;
3267   int rv = -1;
3268
3269   rv = dhcpv6_proxy_set_server_2 ((ip6_address_t *) (&mp->dhcp_server),
3270                                   (ip6_address_t *) (&mp->dhcp_src_address),
3271                                   (u32) ntohl (mp->rx_vrf_id),
3272                                   (u32) ntohl (mp->server_vrf_id),
3273                                   (int) mp->insert_circuit_id,
3274                                   (int) (mp->is_add == 0));
3275
3276   REPLY_MACRO (VL_API_DHCP_PROXY_CONFIG_2_REPLY);
3277 }
3278
3279
3280 static void
3281 vl_api_dhcp_proxy_set_vss_t_handler (vl_api_dhcp_proxy_set_vss_t * mp)
3282 {
3283   vl_api_dhcp_proxy_set_vss_reply_t *rmp;
3284   int rv;
3285   if (!mp->is_ipv6)
3286     rv = dhcp_proxy_set_option82_vss (ntohl (mp->tbl_id),
3287                                       ntohl (mp->oui),
3288                                       ntohl (mp->fib_id),
3289                                       (int) mp->is_add == 0);
3290   else
3291     rv = dhcpv6_proxy_set_vss (ntohl (mp->tbl_id),
3292                                ntohl (mp->oui),
3293                                ntohl (mp->fib_id), (int) mp->is_add == 0);
3294
3295   REPLY_MACRO (VL_API_DHCP_PROXY_SET_VSS_REPLY);
3296 }
3297
3298
3299 static void vl_api_dhcp_proxy_config_t_handler
3300   (vl_api_dhcp_proxy_config_t * mp)
3301 {
3302   if (mp->is_ipv6 == 0)
3303     dhcpv4_proxy_config (mp);
3304   else
3305     dhcpv6_proxy_config (mp);
3306 }
3307
3308 static void vl_api_dhcp_proxy_config_2_t_handler
3309   (vl_api_dhcp_proxy_config_2_t * mp)
3310 {
3311   if (mp->is_ipv6 == 0)
3312     dhcpv4_proxy_config_2 (mp);
3313   else
3314     dhcpv6_proxy_config_2 (mp);
3315 }
3316
3317 void
3318 dhcp_compl_event_callback (u32 client_index, u32 pid, u8 * hostname,
3319                            u8 is_ipv6, u8 * host_address, u8 * router_address,
3320                            u8 * host_mac)
3321 {
3322   unix_shared_memory_queue_t *q;
3323   vl_api_dhcp_compl_event_t *mp;
3324
3325   q = vl_api_client_index_to_input_queue (client_index);
3326   if (!q)
3327     return;
3328
3329   mp = vl_msg_api_alloc (sizeof (*mp));
3330   mp->client_index = client_index;
3331   mp->pid = pid;
3332   mp->is_ipv6 = is_ipv6;
3333   clib_memcpy (&mp->hostname, hostname, vec_len (hostname));
3334   mp->hostname[vec_len (hostname) + 1] = '\n';
3335   clib_memcpy (&mp->host_address[0], host_address, 16);
3336   clib_memcpy (&mp->router_address[0], router_address, 16);
3337
3338   if (NULL != host_mac)
3339     clib_memcpy (&mp->host_mac[0], host_mac, 6);
3340
3341   mp->_vl_msg_id = ntohs (VL_API_DHCP_COMPL_EVENT);
3342
3343   vl_msg_api_send_shmem (q, (u8 *) & mp);
3344 }
3345
3346 static void vl_api_dhcp_client_config_t_handler
3347   (vl_api_dhcp_client_config_t * mp)
3348 {
3349   vlib_main_t *vm = vlib_get_main ();
3350   vl_api_dhcp_client_config_reply_t *rmp;
3351   int rv = 0;
3352
3353   VALIDATE_SW_IF_INDEX (mp);
3354
3355   rv = dhcp_client_config (vm, ntohl (mp->sw_if_index),
3356                            mp->hostname, mp->is_add, mp->client_index,
3357                            mp->want_dhcp_event ? dhcp_compl_event_callback :
3358                            NULL, mp->pid);
3359
3360   BAD_SW_IF_INDEX_LABEL;
3361
3362   REPLY_MACRO (VL_API_DHCP_CLIENT_CONFIG_REPLY);
3363 }
3364
3365 static void
3366   vl_api_sw_interface_ip6nd_ra_config_t_handler
3367   (vl_api_sw_interface_ip6nd_ra_config_t * mp, vlib_main_t * vm)
3368 {
3369   vl_api_sw_interface_ip6nd_ra_config_reply_t *rmp;
3370   int rv = 0;
3371   u8 is_no, suppress, managed, other, ll_option, send_unicast, cease,
3372     default_router;
3373
3374   is_no = mp->is_no == 1;
3375   suppress = mp->suppress == 1;
3376   managed = mp->managed == 1;
3377   other = mp->other == 1;
3378   ll_option = mp->ll_option == 1;
3379   send_unicast = mp->send_unicast == 1;
3380   cease = mp->cease == 1;
3381   default_router = mp->default_router == 1;
3382
3383   VALIDATE_SW_IF_INDEX (mp);
3384
3385   rv = ip6_neighbor_ra_config (vm, ntohl (mp->sw_if_index),
3386                                suppress, managed, other,
3387                                ll_option, send_unicast, cease,
3388                                default_router, ntohl (mp->lifetime),
3389                                ntohl (mp->initial_count),
3390                                ntohl (mp->initial_interval),
3391                                ntohl (mp->max_interval),
3392                                ntohl (mp->min_interval), is_no);
3393
3394   BAD_SW_IF_INDEX_LABEL;
3395
3396   REPLY_MACRO (VL_API_SW_INTERFACE_IP6ND_RA_CONFIG_REPLY);
3397 }
3398
3399 static void
3400   vl_api_sw_interface_ip6nd_ra_prefix_t_handler
3401   (vl_api_sw_interface_ip6nd_ra_prefix_t * mp, vlib_main_t * vm)
3402 {
3403   vl_api_sw_interface_ip6nd_ra_prefix_reply_t *rmp;
3404   int rv = 0;
3405   u8 is_no, use_default, no_advertise, off_link, no_autoconfig, no_onlink;
3406
3407   VALIDATE_SW_IF_INDEX (mp);
3408
3409   is_no = mp->is_no == 1;
3410   use_default = mp->use_default == 1;
3411   no_advertise = mp->no_advertise == 1;
3412   off_link = mp->off_link == 1;
3413   no_autoconfig = mp->no_autoconfig == 1;
3414   no_onlink = mp->no_onlink == 1;
3415
3416   rv = ip6_neighbor_ra_prefix (vm, ntohl (mp->sw_if_index),
3417                                (ip6_address_t *) mp->address,
3418                                mp->address_length, use_default,
3419                                ntohl (mp->val_lifetime),
3420                                ntohl (mp->pref_lifetime), no_advertise,
3421                                off_link, no_autoconfig, no_onlink, is_no);
3422
3423   BAD_SW_IF_INDEX_LABEL;
3424   REPLY_MACRO (VL_API_SW_INTERFACE_IP6ND_RA_PREFIX_REPLY);
3425 }
3426
3427 static void
3428   vl_api_sw_interface_ip6_enable_disable_t_handler
3429   (vl_api_sw_interface_ip6_enable_disable_t * mp, vlib_main_t * vm)
3430 {
3431   vl_api_sw_interface_ip6_enable_disable_reply_t *rmp;
3432   vnet_main_t *vnm = vnet_get_main ();
3433   int rv = 0;
3434   clib_error_t *error;
3435
3436   vnm->api_errno = 0;
3437
3438   VALIDATE_SW_IF_INDEX (mp);
3439
3440   error =
3441     (mp->enable == 1) ? enable_ip6_interface (vm,
3442                                               ntohl (mp->sw_if_index)) :
3443     disable_ip6_interface (vm, ntohl (mp->sw_if_index));
3444
3445   if (error)
3446     {
3447       clib_error_report (error);
3448       rv = VNET_API_ERROR_UNSPECIFIED;
3449     }
3450   else
3451     {
3452       rv = vnm->api_errno;
3453     }
3454
3455   BAD_SW_IF_INDEX_LABEL;
3456
3457   REPLY_MACRO (VL_API_SW_INTERFACE_IP6_ENABLE_DISABLE_REPLY);
3458 }
3459
3460 static void
3461   vl_api_sw_interface_ip6_set_link_local_address_t_handler
3462   (vl_api_sw_interface_ip6_set_link_local_address_t * mp, vlib_main_t * vm)
3463 {
3464   vl_api_sw_interface_ip6_set_link_local_address_reply_t *rmp;
3465   int rv = 0;
3466   clib_error_t *error;
3467   vnet_main_t *vnm = vnet_get_main ();
3468
3469   vnm->api_errno = 0;
3470
3471   VALIDATE_SW_IF_INDEX (mp);
3472
3473   error = set_ip6_link_local_address (vm,
3474                                       ntohl (mp->sw_if_index),
3475                                       (ip6_address_t *) mp->address,
3476                                       mp->address_length);
3477   if (error)
3478     {
3479       clib_error_report (error);
3480       rv = VNET_API_ERROR_UNSPECIFIED;
3481     }
3482   else
3483     {
3484       rv = vnm->api_errno;
3485     }
3486
3487   BAD_SW_IF_INDEX_LABEL;
3488
3489   REPLY_MACRO (VL_API_SW_INTERFACE_IP6_SET_LINK_LOCAL_ADDRESS_REPLY);
3490 }
3491
3492 static void
3493 set_ip6_flow_hash (vl_api_set_ip_flow_hash_t * mp)
3494 {
3495   vl_api_set_ip_flow_hash_reply_t *rmp;
3496   int rv = VNET_API_ERROR_UNIMPLEMENTED;
3497
3498   clib_warning ("unimplemented...");
3499
3500   REPLY_MACRO (VL_API_SET_IP_FLOW_HASH_REPLY);
3501 }
3502
3503 static void
3504 set_ip4_flow_hash (vl_api_set_ip_flow_hash_t * mp)
3505 {
3506   vl_api_set_ip_flow_hash_reply_t *rmp;
3507   int rv;
3508   u32 table_id;
3509   flow_hash_config_t flow_hash_config = 0;
3510
3511   table_id = ntohl (mp->vrf_id);
3512
3513 #define _(a,b) if (mp->a) flow_hash_config |= b;
3514   foreach_flow_hash_bit;
3515 #undef _
3516
3517   rv = vnet_set_ip4_flow_hash (table_id, flow_hash_config);
3518
3519   REPLY_MACRO (VL_API_SET_IP_FLOW_HASH_REPLY);
3520 }
3521
3522
3523 static void
3524 vl_api_set_ip_flow_hash_t_handler (vl_api_set_ip_flow_hash_t * mp)
3525 {
3526   if (mp->is_ipv6 == 0)
3527     set_ip4_flow_hash (mp);
3528   else
3529     set_ip6_flow_hash (mp);
3530 }
3531
3532 static void vl_api_sw_interface_set_unnumbered_t_handler
3533   (vl_api_sw_interface_set_unnumbered_t * mp)
3534 {
3535   vl_api_sw_interface_set_unnumbered_reply_t *rmp;
3536   int rv = 0;
3537   vnet_sw_interface_t *si;
3538   vnet_main_t *vnm = vnet_get_main ();
3539   u32 sw_if_index, unnumbered_sw_if_index;
3540
3541   sw_if_index = ntohl (mp->sw_if_index);
3542   unnumbered_sw_if_index = ntohl (mp->unnumbered_sw_if_index);
3543
3544   /*
3545    * The API message field names are backwards from
3546    * the underlying data structure names.
3547    * It's not worth changing them now.
3548    */
3549   if (pool_is_free_index (vnm->interface_main.sw_interfaces,
3550                           unnumbered_sw_if_index))
3551     {
3552       rv = VNET_API_ERROR_INVALID_SW_IF_INDEX;
3553       goto done;
3554     }
3555
3556   /* Only check the "use loop0" field when setting the binding */
3557   if (mp->is_add &&
3558       pool_is_free_index (vnm->interface_main.sw_interfaces, sw_if_index))
3559     {
3560       rv = VNET_API_ERROR_INVALID_SW_IF_INDEX_2;
3561       goto done;
3562     }
3563
3564   si = vnet_get_sw_interface (vnm, unnumbered_sw_if_index);
3565
3566   if (mp->is_add)
3567     {
3568       si->flags |= VNET_SW_INTERFACE_FLAG_UNNUMBERED;
3569       si->unnumbered_sw_if_index = sw_if_index;
3570       ip4_sw_interface_enable_disable (unnumbered_sw_if_index, 1);
3571       ip6_sw_interface_enable_disable (unnumbered_sw_if_index, 1);
3572     }
3573   else
3574     {
3575       si->flags &= ~(VNET_SW_INTERFACE_FLAG_UNNUMBERED);
3576       si->unnumbered_sw_if_index = (u32) ~ 0;
3577       ip4_sw_interface_enable_disable (unnumbered_sw_if_index, 0);
3578       ip6_sw_interface_enable_disable (unnumbered_sw_if_index, 0);
3579     }
3580
3581 done:
3582   REPLY_MACRO (VL_API_SW_INTERFACE_SET_UNNUMBERED_REPLY);
3583 }
3584
3585 static void
3586 vl_api_create_loopback_t_handler (vl_api_create_loopback_t * mp)
3587 {
3588   vl_api_create_loopback_reply_t *rmp;
3589   u32 sw_if_index;
3590   int rv;
3591
3592   rv = vnet_create_loopback_interface (&sw_if_index, mp->mac_address);
3593
3594   /* *INDENT-OFF* */
3595   REPLY_MACRO2(VL_API_CREATE_LOOPBACK_REPLY,
3596   ({
3597     rmp->sw_if_index = ntohl (sw_if_index);
3598   }));
3599   /* *INDENT-ON* */
3600 }
3601
3602 static void
3603 vl_api_delete_loopback_t_handler (vl_api_delete_loopback_t * mp)
3604 {
3605   vl_api_delete_loopback_reply_t *rmp;
3606   u32 sw_if_index;
3607   int rv;
3608
3609   sw_if_index = ntohl (mp->sw_if_index);
3610   rv = vnet_delete_loopback_interface (sw_if_index);
3611
3612   REPLY_MACRO (VL_API_DELETE_LOOPBACK_REPLY);
3613 }
3614
3615 static void
3616 vl_api_control_ping_t_handler (vl_api_control_ping_t * mp)
3617 {
3618   vl_api_control_ping_reply_t *rmp;
3619   int rv = 0;
3620
3621   /* *INDENT-OFF* */
3622   REPLY_MACRO2(VL_API_CONTROL_PING_REPLY,
3623   ({
3624     rmp->vpe_pid = ntohl (getpid());
3625   }));
3626   /* *INDENT-ON* */
3627 }
3628
3629 static void
3630 shmem_cli_output (uword arg, u8 * buffer, uword buffer_bytes)
3631 {
3632   u8 **shmem_vecp = (u8 **) arg;
3633   u8 *shmem_vec;
3634   void *oldheap;
3635   api_main_t *am = &api_main;
3636   u32 offset;
3637
3638   shmem_vec = *shmem_vecp;
3639
3640   offset = vec_len (shmem_vec);
3641
3642   pthread_mutex_lock (&am->vlib_rp->mutex);
3643   oldheap = svm_push_data_heap (am->vlib_rp);
3644
3645   vec_validate (shmem_vec, offset + buffer_bytes - 1);
3646
3647   clib_memcpy (shmem_vec + offset, buffer, buffer_bytes);
3648
3649   svm_pop_heap (oldheap);
3650   pthread_mutex_unlock (&am->vlib_rp->mutex);
3651
3652   *shmem_vecp = shmem_vec;
3653 }
3654
3655
3656 static void
3657 vl_api_cli_request_t_handler (vl_api_cli_request_t * mp)
3658 {
3659   vl_api_cli_reply_t *rp;
3660   unix_shared_memory_queue_t *q;
3661   vlib_main_t *vm = vlib_get_main ();
3662   api_main_t *am = &api_main;
3663   unformat_input_t input;
3664   u8 *shmem_vec = 0;
3665   void *oldheap;
3666
3667   q = vl_api_client_index_to_input_queue (mp->client_index);
3668   if (!q)
3669     return;
3670
3671   rp = vl_msg_api_alloc (sizeof (*rp));
3672   rp->_vl_msg_id = ntohs (VL_API_CLI_REPLY);
3673   rp->context = mp->context;
3674
3675   unformat_init_vector (&input, (u8 *) (uword) mp->cmd_in_shmem);
3676
3677   vlib_cli_input (vm, &input, shmem_cli_output, (uword) & shmem_vec);
3678
3679   pthread_mutex_lock (&am->vlib_rp->mutex);
3680   oldheap = svm_push_data_heap (am->vlib_rp);
3681
3682   vec_add1 (shmem_vec, 0);
3683
3684   svm_pop_heap (oldheap);
3685   pthread_mutex_unlock (&am->vlib_rp->mutex);
3686
3687   rp->reply_in_shmem = (uword) shmem_vec;
3688
3689   vl_msg_api_send_shmem (q, (u8 *) & rp);
3690 }
3691
3692 static void
3693 inband_cli_output (uword arg, u8 * buffer, uword buffer_bytes)
3694 {
3695   u8 **mem_vecp = (u8 **) arg;
3696   u8 *mem_vec = *mem_vecp;
3697   u32 offset = vec_len (mem_vec);
3698
3699   vec_validate (mem_vec, offset + buffer_bytes - 1);
3700   clib_memcpy (mem_vec + offset, buffer, buffer_bytes);
3701   *mem_vecp = mem_vec;
3702 }
3703
3704 static void
3705 vl_api_cli_inband_t_handler (vl_api_cli_inband_t * mp)
3706 {
3707   vl_api_cli_inband_reply_t *rmp;
3708   int rv = 0;
3709   unix_shared_memory_queue_t *q;
3710   vlib_main_t *vm = vlib_get_main ();
3711   unformat_input_t input;
3712   u8 *out_vec = 0;
3713
3714   q = vl_api_client_index_to_input_queue (mp->client_index);
3715   if (!q)
3716     return;
3717
3718   unformat_init_string (&input, (char *) mp->cmd, ntohl (mp->length));
3719   vlib_cli_input (vm, &input, inband_cli_output, (uword) & out_vec);
3720
3721   u32 len = vec_len (out_vec);
3722   /* *INDENT-OFF* */
3723   REPLY_MACRO3(VL_API_CLI_INBAND_REPLY, len,
3724   ({
3725     rmp->length = htonl (len);
3726     clib_memcpy (rmp->reply, out_vec, len);
3727   }));
3728   /* *INDENT-ON* */
3729   vec_free (out_vec);
3730 }
3731
3732 static void
3733 vl_api_set_arp_neighbor_limit_t_handler (vl_api_set_arp_neighbor_limit_t * mp)
3734 {
3735   int rv;
3736   vl_api_set_arp_neighbor_limit_reply_t *rmp;
3737   vnet_main_t *vnm = vnet_get_main ();
3738   clib_error_t *error;
3739
3740   vnm->api_errno = 0;
3741
3742   if (mp->is_ipv6)
3743     error = ip6_set_neighbor_limit (ntohl (mp->arp_neighbor_limit));
3744   else
3745     error = ip4_set_arp_limit (ntohl (mp->arp_neighbor_limit));
3746
3747   if (error)
3748     {
3749       clib_error_report (error);
3750       rv = VNET_API_ERROR_UNSPECIFIED;
3751     }
3752   else
3753     {
3754       rv = vnm->api_errno;
3755     }
3756
3757   REPLY_MACRO (VL_API_SET_ARP_NEIGHBOR_LIMIT_REPLY);
3758 }
3759
3760 static void vl_api_sr_tunnel_add_del_t_handler
3761   (vl_api_sr_tunnel_add_del_t * mp)
3762 {
3763 #if IP6SR == 0
3764   clib_warning ("unimplemented");
3765 #else
3766   ip6_sr_add_del_tunnel_args_t _a, *a = &_a;
3767   int rv = 0;
3768   vl_api_sr_tunnel_add_del_reply_t *rmp;
3769   ip6_address_t *segments = 0, *seg;
3770   ip6_address_t *tags = 0, *tag;
3771   ip6_address_t *this_address;
3772   int i;
3773
3774   if (mp->n_segments == 0)
3775     {
3776       rv = -11;
3777       goto out;
3778     }
3779
3780   memset (a, 0, sizeof (*a));
3781   a->src_address = (ip6_address_t *) & mp->src_address;
3782   a->dst_address = (ip6_address_t *) & mp->dst_address;
3783   a->dst_mask_width = mp->dst_mask_width;
3784   a->flags_net_byte_order = mp->flags_net_byte_order;
3785   a->is_del = (mp->is_add == 0);
3786   a->rx_table_id = ntohl (mp->outer_vrf_id);
3787   a->tx_table_id = ntohl (mp->inner_vrf_id);
3788
3789   a->name = format (0, "%s", mp->name);
3790   if (!(vec_len (a->name)))
3791     a->name = 0;
3792
3793   a->policy_name = format (0, "%s", mp->policy_name);
3794   if (!(vec_len (a->policy_name)))
3795     a->policy_name = 0;
3796
3797   /* Yank segments and tags out of the API message */
3798   this_address = (ip6_address_t *) mp->segs_and_tags;
3799   for (i = 0; i < mp->n_segments; i++)
3800     {
3801       vec_add2 (segments, seg, 1);
3802       clib_memcpy (seg->as_u8, this_address->as_u8, sizeof (*this_address));
3803       this_address++;
3804     }
3805   for (i = 0; i < mp->n_tags; i++)
3806     {
3807       vec_add2 (tags, tag, 1);
3808       clib_memcpy (tag->as_u8, this_address->as_u8, sizeof (*this_address));
3809       this_address++;
3810     }
3811
3812   a->segments = segments;
3813   a->tags = tags;
3814
3815   rv = ip6_sr_add_del_tunnel (a);
3816
3817 out:
3818
3819   REPLY_MACRO (VL_API_SR_TUNNEL_ADD_DEL_REPLY);
3820 #endif
3821 }
3822
3823 static void vl_api_sr_policy_add_del_t_handler
3824   (vl_api_sr_policy_add_del_t * mp)
3825 {
3826 #if IP6SR == 0
3827   clib_warning ("unimplemented");
3828 #else
3829   ip6_sr_add_del_policy_args_t _a, *a = &_a;
3830   int rv = 0;
3831   vl_api_sr_policy_add_del_reply_t *rmp;
3832   int i;
3833
3834   memset (a, 0, sizeof (*a));
3835   a->is_del = (mp->is_add == 0);
3836
3837   a->name = format (0, "%s", mp->name);
3838   if (!(vec_len (a->name)))
3839     {
3840       rv = VNET_API_ERROR_NO_SUCH_NODE2;
3841       goto out;
3842     }
3843
3844   if (!(mp->tunnel_names[0]))
3845     {
3846       rv = VNET_API_ERROR_NO_SUCH_NODE2;
3847       goto out;
3848     }
3849
3850   // start deserializing tunnel_names
3851   int num_tunnels = mp->tunnel_names[0];        //number of tunnels
3852   u8 *deser_tun_names = mp->tunnel_names;
3853   deser_tun_names += 1;         //moving along
3854
3855   u8 *tun_name = 0;
3856   int tun_name_len = 0;
3857
3858   for (i = 0; i < num_tunnels; i++)
3859     {
3860       tun_name_len = *deser_tun_names;
3861       deser_tun_names += 1;
3862       vec_resize (tun_name, tun_name_len);
3863       memcpy (tun_name, deser_tun_names, tun_name_len);
3864       vec_add1 (a->tunnel_names, tun_name);
3865       deser_tun_names += tun_name_len;
3866       tun_name = 0;
3867     }
3868
3869   rv = ip6_sr_add_del_policy (a);
3870
3871 out:
3872
3873   REPLY_MACRO (VL_API_SR_POLICY_ADD_DEL_REPLY);
3874 #endif
3875 }
3876
3877 static void vl_api_sr_multicast_map_add_del_t_handler
3878   (vl_api_sr_multicast_map_add_del_t * mp)
3879 {
3880 #if IP6SR == 0
3881   clib_warning ("unimplemented");
3882 #else
3883   ip6_sr_add_del_multicastmap_args_t _a, *a = &_a;
3884   int rv = 0;
3885   vl_api_sr_multicast_map_add_del_reply_t *rmp;
3886
3887   memset (a, 0, sizeof (*a));
3888   a->is_del = (mp->is_add == 0);
3889
3890   a->multicast_address = (ip6_address_t *) & mp->multicast_address;
3891   a->policy_name = format (0, "%s", mp->policy_name);
3892
3893   if (a->multicast_address == 0)
3894     {
3895       rv = -1;
3896       goto out;
3897     }
3898
3899   if (!(a->policy_name))
3900     {
3901       rv = -2;
3902       goto out;
3903     }
3904
3905 #if DPDK > 0                    /* Cannot call replicate without DPDK */
3906   rv = ip6_sr_add_del_multicastmap (a);
3907 #else
3908   clib_warning ("multicast replication without DPDK not implemented");
3909   rv = VNET_API_ERROR_UNIMPLEMENTED;
3910 #endif /* DPDK */
3911
3912 out:
3913
3914   REPLY_MACRO (VL_API_SR_MULTICAST_MAP_ADD_DEL_REPLY);
3915 #endif
3916 }
3917
3918 #define foreach_classify_add_del_table_field    \
3919 _(table_index)                                  \
3920 _(nbuckets)                                     \
3921 _(memory_size)                                  \
3922 _(skip_n_vectors)                               \
3923 _(match_n_vectors)                              \
3924 _(next_table_index)                             \
3925 _(miss_next_index)                              \
3926 _(current_data_flag)                            \
3927 _(current_data_offset)
3928
3929 static void vl_api_classify_add_del_table_t_handler
3930   (vl_api_classify_add_del_table_t * mp)
3931 {
3932   vl_api_classify_add_del_table_reply_t *rmp;
3933   vnet_classify_main_t *cm = &vnet_classify_main;
3934   vnet_classify_table_t *t;
3935   int rv;
3936
3937 #define _(a) u32 a;
3938   foreach_classify_add_del_table_field;
3939 #undef _
3940
3941 #define _(a) a = ntohl(mp->a);
3942   foreach_classify_add_del_table_field;
3943 #undef _
3944
3945   /* The underlying API fails silently, on purpose, so check here */
3946   if (mp->is_add == 0)          /* delete */
3947     {
3948       if (pool_is_free_index (cm->tables, table_index))
3949         {
3950           rv = VNET_API_ERROR_NO_SUCH_TABLE;
3951           goto out;
3952         }
3953     }
3954   else                          /* add or update */
3955     {
3956       if (table_index != ~0 && pool_is_free_index (cm->tables, table_index))
3957         table_index = ~0;
3958     }
3959
3960   rv = vnet_classify_add_del_table
3961     (cm, mp->mask, nbuckets, memory_size,
3962      skip_n_vectors, match_n_vectors,
3963      next_table_index, miss_next_index, &table_index,
3964      current_data_flag, current_data_offset, mp->is_add);
3965
3966 out:
3967   /* *INDENT-OFF* */
3968   REPLY_MACRO2(VL_API_CLASSIFY_ADD_DEL_TABLE_REPLY,
3969   ({
3970     if (rv == 0 && mp->is_add)
3971       {
3972         t = pool_elt_at_index (cm->tables, table_index);
3973         rmp->skip_n_vectors = ntohl(t->skip_n_vectors);
3974         rmp->match_n_vectors = ntohl(t->match_n_vectors);
3975         rmp->new_table_index = ntohl(table_index);
3976       }
3977     else
3978       {
3979         rmp->skip_n_vectors = ~0;
3980         rmp->match_n_vectors = ~0;
3981         rmp->new_table_index = ~0;
3982       }
3983   }));
3984   /* *INDENT-ON* */
3985 }
3986
3987 static void vl_api_classify_add_del_session_t_handler
3988   (vl_api_classify_add_del_session_t * mp)
3989 {
3990   vnet_classify_main_t *cm = &vnet_classify_main;
3991   vl_api_classify_add_del_session_reply_t *rmp;
3992   int rv;
3993   u32 table_index, hit_next_index, opaque_index, metadata;
3994   i32 advance;
3995   u8 action;
3996
3997   table_index = ntohl (mp->table_index);
3998   hit_next_index = ntohl (mp->hit_next_index);
3999   opaque_index = ntohl (mp->opaque_index);
4000   advance = ntohl (mp->advance);
4001   action = mp->action;
4002   metadata = ntohl (mp->metadata);
4003
4004   rv = vnet_classify_add_del_session
4005     (cm, table_index, mp->match, hit_next_index, opaque_index,
4006      advance, action, metadata, mp->is_add);
4007
4008   REPLY_MACRO (VL_API_CLASSIFY_ADD_DEL_SESSION_REPLY);
4009 }
4010
4011 static void vl_api_classify_set_interface_ip_table_t_handler
4012   (vl_api_classify_set_interface_ip_table_t * mp)
4013 {
4014   vlib_main_t *vm = vlib_get_main ();
4015   vl_api_classify_set_interface_ip_table_reply_t *rmp;
4016   int rv;
4017   u32 table_index, sw_if_index;
4018
4019   table_index = ntohl (mp->table_index);
4020   sw_if_index = ntohl (mp->sw_if_index);
4021
4022   VALIDATE_SW_IF_INDEX (mp);
4023
4024   if (mp->is_ipv6)
4025     rv = vnet_set_ip6_classify_intfc (vm, sw_if_index, table_index);
4026   else
4027     rv = vnet_set_ip4_classify_intfc (vm, sw_if_index, table_index);
4028
4029   BAD_SW_IF_INDEX_LABEL;
4030
4031   REPLY_MACRO (VL_API_CLASSIFY_SET_INTERFACE_IP_TABLE_REPLY);
4032 }
4033
4034 static void vl_api_classify_set_interface_l2_tables_t_handler
4035   (vl_api_classify_set_interface_l2_tables_t * mp)
4036 {
4037   vl_api_classify_set_interface_l2_tables_reply_t *rmp;
4038   int rv;
4039   u32 sw_if_index, ip4_table_index, ip6_table_index, other_table_index;
4040   int enable;
4041
4042   ip4_table_index = ntohl (mp->ip4_table_index);
4043   ip6_table_index = ntohl (mp->ip6_table_index);
4044   other_table_index = ntohl (mp->other_table_index);
4045   sw_if_index = ntohl (mp->sw_if_index);
4046
4047   VALIDATE_SW_IF_INDEX (mp);
4048
4049   if (mp->is_input)
4050     rv = vnet_l2_input_classify_set_tables (sw_if_index, ip4_table_index,
4051                                             ip6_table_index,
4052                                             other_table_index);
4053   else
4054     rv = vnet_l2_output_classify_set_tables (sw_if_index, ip4_table_index,
4055                                              ip6_table_index,
4056                                              other_table_index);
4057
4058   if (rv == 0)
4059     {
4060       if (ip4_table_index != ~0 || ip6_table_index != ~0
4061           || other_table_index != ~0)
4062         enable = 1;
4063       else
4064         enable = 0;
4065
4066       if (mp->is_input)
4067         vnet_l2_input_classify_enable_disable (sw_if_index, enable);
4068       else
4069         vnet_l2_output_classify_enable_disable (sw_if_index, enable);
4070     }
4071
4072   BAD_SW_IF_INDEX_LABEL;
4073
4074   REPLY_MACRO (VL_API_CLASSIFY_SET_INTERFACE_L2_TABLES_REPLY);
4075 }
4076
4077 static void
4078 vl_api_l2_fib_clear_table_t_handler (vl_api_l2_fib_clear_table_t * mp)
4079 {
4080   int rv = 0;
4081   vl_api_l2_fib_clear_table_reply_t *rmp;
4082
4083   /* DAW-FIXME: This API should only clear non-static l2fib entries, but
4084    *            that is not currently implemented.  When that TODO is fixed
4085    *            this call should be changed to pass 1 instead of 0.
4086    */
4087   l2fib_clear_table (0);
4088
4089   REPLY_MACRO (VL_API_L2_FIB_CLEAR_TABLE_REPLY);
4090 }
4091
4092 extern void l2_efp_filter_configure (vnet_main_t * vnet_main,
4093                                      u32 sw_if_index, u32 enable);
4094
4095 static void
4096 vl_api_l2_interface_efp_filter_t_handler (vl_api_l2_interface_efp_filter_t *
4097                                           mp)
4098 {
4099   int rv;
4100   vl_api_l2_interface_efp_filter_reply_t *rmp;
4101   vnet_main_t *vnm = vnet_get_main ();
4102
4103   // enable/disable the feature
4104   l2_efp_filter_configure (vnm, mp->sw_if_index, mp->enable_disable);
4105   rv = vnm->api_errno;
4106
4107   REPLY_MACRO (VL_API_L2_INTERFACE_EFP_FILTER_REPLY);
4108 }
4109
4110 static void
4111   vl_api_l2_interface_vlan_tag_rewrite_t_handler
4112   (vl_api_l2_interface_vlan_tag_rewrite_t * mp)
4113 {
4114   int rv = 0;
4115   vl_api_l2_interface_vlan_tag_rewrite_reply_t *rmp;
4116   vnet_main_t *vnm = vnet_get_main ();
4117   vlib_main_t *vm = vlib_get_main ();
4118   u32 vtr_op;
4119
4120   VALIDATE_SW_IF_INDEX (mp);
4121
4122   vtr_op = ntohl (mp->vtr_op);
4123
4124   /* The L2 code is unsuspicious */
4125   switch (vtr_op)
4126     {
4127     case L2_VTR_DISABLED:
4128     case L2_VTR_PUSH_1:
4129     case L2_VTR_PUSH_2:
4130     case L2_VTR_POP_1:
4131     case L2_VTR_POP_2:
4132     case L2_VTR_TRANSLATE_1_1:
4133     case L2_VTR_TRANSLATE_1_2:
4134     case L2_VTR_TRANSLATE_2_1:
4135     case L2_VTR_TRANSLATE_2_2:
4136       break;
4137
4138     default:
4139       rv = VNET_API_ERROR_INVALID_VALUE;
4140       goto bad_sw_if_index;
4141     }
4142
4143   rv = l2vtr_configure (vm, vnm, ntohl (mp->sw_if_index), vtr_op,
4144                         ntohl (mp->push_dot1q), ntohl (mp->tag1),
4145                         ntohl (mp->tag2));
4146
4147   BAD_SW_IF_INDEX_LABEL;
4148
4149   REPLY_MACRO (VL_API_L2_INTERFACE_VLAN_TAG_REWRITE_REPLY);
4150 }
4151
4152 static void
4153 vl_api_create_vhost_user_if_t_handler (vl_api_create_vhost_user_if_t * mp)
4154 {
4155   int rv = 0;
4156   vl_api_create_vhost_user_if_reply_t *rmp;
4157   u32 sw_if_index = (u32) ~ 0;
4158
4159   vnet_main_t *vnm = vnet_get_main ();
4160   vlib_main_t *vm = vlib_get_main ();
4161
4162   rv = vhost_user_create_if (vnm, vm, (char *) mp->sock_filename,
4163                              mp->is_server, &sw_if_index, (u64) ~ 0,
4164                              mp->renumber, ntohl (mp->custom_dev_instance),
4165                              (mp->use_custom_mac) ? mp->mac_address : NULL);
4166
4167   /* *INDENT-OFF* */
4168   REPLY_MACRO2(VL_API_CREATE_VHOST_USER_IF_REPLY,
4169   ({
4170     rmp->sw_if_index = ntohl (sw_if_index);
4171   }));
4172   /* *INDENT-ON* */
4173 }
4174
4175 static void
4176 vl_api_modify_vhost_user_if_t_handler (vl_api_modify_vhost_user_if_t * mp)
4177 {
4178   int rv = 0;
4179   vl_api_modify_vhost_user_if_reply_t *rmp;
4180   u32 sw_if_index = ntohl (mp->sw_if_index);
4181
4182   vnet_main_t *vnm = vnet_get_main ();
4183   vlib_main_t *vm = vlib_get_main ();
4184
4185   rv = vhost_user_modify_if (vnm, vm, (char *) mp->sock_filename,
4186                              mp->is_server, sw_if_index, (u64) ~ 0,
4187                              mp->renumber, ntohl (mp->custom_dev_instance));
4188   REPLY_MACRO (VL_API_MODIFY_VHOST_USER_IF_REPLY);
4189 }
4190
4191 static void
4192 vl_api_delete_vhost_user_if_t_handler (vl_api_delete_vhost_user_if_t * mp)
4193 {
4194   int rv = 0;
4195   vl_api_delete_vhost_user_if_reply_t *rmp;
4196   vpe_api_main_t *vam = &vpe_api_main;
4197   u32 sw_if_index = ntohl (mp->sw_if_index);
4198
4199   vnet_main_t *vnm = vnet_get_main ();
4200   vlib_main_t *vm = vlib_get_main ();
4201
4202   rv = vhost_user_delete_if (vnm, vm, sw_if_index);
4203
4204   REPLY_MACRO (VL_API_DELETE_VHOST_USER_IF_REPLY);
4205   if (!rv)
4206     {
4207       unix_shared_memory_queue_t *q =
4208         vl_api_client_index_to_input_queue (mp->client_index);
4209       if (!q)
4210         return;
4211
4212       send_sw_interface_flags_deleted (vam, q, sw_if_index);
4213     }
4214 }
4215
4216 static void
4217   vl_api_sw_interface_vhost_user_details_t_handler
4218   (vl_api_sw_interface_vhost_user_details_t * mp)
4219 {
4220   clib_warning ("BUG");
4221 }
4222
4223 static void
4224 send_sw_interface_vhost_user_details (vpe_api_main_t * am,
4225                                       unix_shared_memory_queue_t * q,
4226                                       vhost_user_intf_details_t * vui,
4227                                       u32 context)
4228 {
4229   vl_api_sw_interface_vhost_user_details_t *mp;
4230
4231   mp = vl_msg_api_alloc (sizeof (*mp));
4232   memset (mp, 0, sizeof (*mp));
4233   mp->_vl_msg_id = ntohs (VL_API_SW_INTERFACE_VHOST_USER_DETAILS);
4234   mp->sw_if_index = ntohl (vui->sw_if_index);
4235   mp->virtio_net_hdr_sz = ntohl (vui->virtio_net_hdr_sz);
4236   mp->features = clib_net_to_host_u64 (vui->features);
4237   mp->is_server = vui->is_server;
4238   mp->num_regions = ntohl (vui->num_regions);
4239   mp->sock_errno = ntohl (vui->sock_errno);
4240   mp->context = context;
4241
4242   strncpy ((char *) mp->sock_filename,
4243            (char *) vui->sock_filename, ARRAY_LEN (mp->sock_filename) - 1);
4244   strncpy ((char *) mp->interface_name,
4245            (char *) vui->if_name, ARRAY_LEN (mp->interface_name) - 1);
4246
4247   vl_msg_api_send_shmem (q, (u8 *) & mp);
4248 }
4249
4250 static void
4251   vl_api_sw_interface_vhost_user_dump_t_handler
4252   (vl_api_sw_interface_vhost_user_dump_t * mp)
4253 {
4254   int rv = 0;
4255   vpe_api_main_t *am = &vpe_api_main;
4256   vnet_main_t *vnm = vnet_get_main ();
4257   vlib_main_t *vm = vlib_get_main ();
4258   vhost_user_intf_details_t *ifaces = NULL;
4259   vhost_user_intf_details_t *vuid = NULL;
4260   unix_shared_memory_queue_t *q;
4261
4262   q = vl_api_client_index_to_input_queue (mp->client_index);
4263   if (q == 0)
4264     return;
4265
4266   rv = vhost_user_dump_ifs (vnm, vm, &ifaces);
4267   if (rv)
4268     return;
4269
4270   vec_foreach (vuid, ifaces)
4271   {
4272     send_sw_interface_vhost_user_details (am, q, vuid, mp->context);
4273   }
4274   vec_free (ifaces);
4275 }
4276
4277 static void
4278 send_sw_if_l2tpv3_tunnel_details (vpe_api_main_t * am,
4279                                   unix_shared_memory_queue_t * q,
4280                                   l2t_session_t * s,
4281                                   l2t_main_t * lm, u32 context)
4282 {
4283   vl_api_sw_if_l2tpv3_tunnel_details_t *mp;
4284   u8 *if_name = NULL;
4285   vnet_sw_interface_t *si = NULL;
4286
4287   si = vnet_get_hw_sw_interface (lm->vnet_main, s->hw_if_index);
4288
4289   if_name = format (if_name, "%U",
4290                     format_vnet_sw_interface_name, lm->vnet_main, si);
4291
4292   mp = vl_msg_api_alloc (sizeof (*mp));
4293   memset (mp, 0, sizeof (*mp));
4294   mp->_vl_msg_id = ntohs (VL_API_SW_IF_L2TPV3_TUNNEL_DETAILS);
4295   strncpy ((char *) mp->interface_name,
4296            (char *) if_name, ARRAY_LEN (mp->interface_name) - 1);
4297   mp->sw_if_index = ntohl (si->sw_if_index);
4298   mp->local_session_id = s->local_session_id;
4299   mp->remote_session_id = s->remote_session_id;
4300   mp->local_cookie[0] = s->local_cookie[0];
4301   mp->local_cookie[1] = s->local_cookie[1];
4302   mp->remote_cookie = s->remote_cookie;
4303   clib_memcpy (mp->client_address, &s->client_address,
4304                sizeof (s->client_address));
4305   clib_memcpy (mp->our_address, &s->our_address, sizeof (s->our_address));
4306   mp->l2_sublayer_present = s->l2_sublayer_present;
4307   mp->context = context;
4308
4309   vl_msg_api_send_shmem (q, (u8 *) & mp);
4310 }
4311
4312 static void
4313 send_ip_address_details (vpe_api_main_t * am,
4314                          unix_shared_memory_queue_t * q,
4315                          u8 * ip, u16 prefix_length, u8 is_ipv6, u32 context)
4316 {
4317   vl_api_ip_address_details_t *mp;
4318
4319   mp = vl_msg_api_alloc (sizeof (*mp));
4320   memset (mp, 0, sizeof (*mp));
4321   mp->_vl_msg_id = ntohs (VL_API_IP_ADDRESS_DETAILS);
4322
4323   if (is_ipv6)
4324     {
4325       clib_memcpy (&mp->ip, ip, sizeof (mp->ip));
4326     }
4327   else
4328     {
4329       u32 *tp = (u32 *) mp->ip;
4330       *tp = ntohl (*(u32 *) ip);
4331     }
4332   mp->prefix_length = prefix_length;
4333   mp->context = context;
4334
4335   vl_msg_api_send_shmem (q, (u8 *) & mp);
4336 }
4337
4338 static void
4339 vl_api_ip_address_dump_t_handler (vl_api_ip_address_dump_t * mp)
4340 {
4341   vpe_api_main_t *am = &vpe_api_main;
4342   unix_shared_memory_queue_t *q;
4343   ip6_address_t *r6;
4344   ip4_address_t *r4;
4345   ip6_main_t *im6 = &ip6_main;
4346   ip4_main_t *im4 = &ip4_main;
4347   ip_lookup_main_t *lm6 = &im6->lookup_main;
4348   ip_lookup_main_t *lm4 = &im4->lookup_main;
4349   ip_interface_address_t *ia = 0;
4350   u32 sw_if_index = ~0;
4351   int rv __attribute__ ((unused)) = 0;
4352
4353   VALIDATE_SW_IF_INDEX (mp);
4354
4355   sw_if_index = ntohl (mp->sw_if_index);
4356
4357   q = vl_api_client_index_to_input_queue (mp->client_index);
4358   if (q == 0)
4359     {
4360       return;
4361     }
4362
4363   if (mp->is_ipv6)
4364     {
4365       /* *INDENT-OFF* */
4366       foreach_ip_interface_address (lm6, ia, sw_if_index,
4367                                     1 /* honor unnumbered */,
4368       ({
4369         r6 = ip_interface_address_get_address (lm6, ia);
4370         u16 prefix_length = ia->address_length;
4371         send_ip_address_details(am, q, (u8*)r6, prefix_length, 1, mp->context);
4372       }));
4373       /* *INDENT-ON* */
4374     }
4375   else
4376     {
4377       /* *INDENT-OFF* */
4378       foreach_ip_interface_address (lm4, ia, sw_if_index,
4379                                     1 /* honor unnumbered */,
4380       ({
4381         r4 = ip_interface_address_get_address (lm4, ia);
4382         u16 prefix_length = ia->address_length;
4383         send_ip_address_details(am, q, (u8*)r4, prefix_length, 0, mp->context);
4384       }));
4385       /* *INDENT-ON* */
4386     }
4387   BAD_SW_IF_INDEX_LABEL;
4388 }
4389
4390 static void
4391 send_ip_details (vpe_api_main_t * am,
4392                  unix_shared_memory_queue_t * q, u32 sw_if_index, u32 context)
4393 {
4394   vl_api_ip_details_t *mp;
4395
4396   mp = vl_msg_api_alloc (sizeof (*mp));
4397   memset (mp, 0, sizeof (*mp));
4398   mp->_vl_msg_id = ntohs (VL_API_IP_DETAILS);
4399
4400   mp->sw_if_index = ntohl (sw_if_index);
4401   mp->context = context;
4402
4403   vl_msg_api_send_shmem (q, (u8 *) & mp);
4404 }
4405
4406 static void
4407 vl_api_sw_if_l2tpv3_tunnel_dump_t_handler (vl_api_sw_if_l2tpv3_tunnel_dump_t *
4408                                            mp)
4409 {
4410   vpe_api_main_t *am = &vpe_api_main;
4411   l2t_main_t *lm = &l2t_main;
4412   unix_shared_memory_queue_t *q;
4413   l2t_session_t *session;
4414
4415   q = vl_api_client_index_to_input_queue (mp->client_index);
4416   if (q == 0)
4417     return;
4418
4419   /* *INDENT-OFF* */
4420   pool_foreach (session, lm->sessions,
4421   ({
4422     send_sw_if_l2tpv3_tunnel_details (am, q, session, lm, mp->context);
4423   }));
4424   /* *INDENT-ON* */
4425 }
4426
4427
4428 static void
4429 send_sw_interface_tap_details (vpe_api_main_t * am,
4430                                unix_shared_memory_queue_t * q,
4431                                tapcli_interface_details_t * tap_if,
4432                                u32 context)
4433 {
4434   vl_api_sw_interface_tap_details_t *mp;
4435   mp = vl_msg_api_alloc (sizeof (*mp));
4436   memset (mp, 0, sizeof (*mp));
4437   mp->_vl_msg_id = ntohs (VL_API_SW_INTERFACE_TAP_DETAILS);
4438   mp->sw_if_index = ntohl (tap_if->sw_if_index);
4439   strncpy ((char *) mp->dev_name,
4440            (char *) tap_if->dev_name, ARRAY_LEN (mp->dev_name) - 1);
4441   mp->context = context;
4442
4443   vl_msg_api_send_shmem (q, (u8 *) & mp);
4444 }
4445
4446 static void
4447 vl_api_sw_interface_tap_dump_t_handler (vl_api_sw_interface_tap_dump_t * mp)
4448 {
4449   int rv = 0;
4450   vpe_api_main_t *am = &vpe_api_main;
4451   unix_shared_memory_queue_t *q;
4452   tapcli_interface_details_t *tapifs = NULL;
4453   tapcli_interface_details_t *tap_if = NULL;
4454
4455   q = vl_api_client_index_to_input_queue (mp->client_index);
4456   if (q == 0)
4457     return;
4458
4459   rv = vnet_tap_dump_ifs (&tapifs);
4460   if (rv)
4461     return;
4462
4463   vec_foreach (tap_if, tapifs)
4464   {
4465     send_sw_interface_tap_details (am, q, tap_if, mp->context);
4466   }
4467
4468   vec_free (tapifs);
4469 }
4470
4471 static void
4472 vl_api_ip_dump_t_handler (vl_api_ip_dump_t * mp)
4473 {
4474   vpe_api_main_t *am = &vpe_api_main;
4475   vnet_main_t *vnm = vnet_get_main ();
4476   vlib_main_t *vm = vlib_get_main ();
4477   vnet_interface_main_t *im = &vnm->interface_main;
4478   unix_shared_memory_queue_t *q;
4479   vnet_sw_interface_t *si, *sorted_sis;
4480   u32 sw_if_index = ~0;
4481
4482   q = vl_api_client_index_to_input_queue (mp->client_index);
4483   if (q == 0)
4484     {
4485       return;
4486     }
4487
4488   /* Gather interfaces. */
4489   sorted_sis = vec_new (vnet_sw_interface_t, pool_elts (im->sw_interfaces));
4490   _vec_len (sorted_sis) = 0;
4491   /* *INDENT-OFF* */
4492   pool_foreach (si, im->sw_interfaces,
4493   ({
4494     vec_add1 (sorted_sis, si[0]);
4495   }));
4496   /* *INDENT-ON* */
4497
4498   vec_foreach (si, sorted_sis)
4499   {
4500     if (!(si->flags & VNET_SW_INTERFACE_FLAG_UNNUMBERED))
4501       {
4502         if (mp->is_ipv6 && !ip6_interface_enabled (vm, si->sw_if_index))
4503           {
4504             continue;
4505           }
4506         sw_if_index = si->sw_if_index;
4507         send_ip_details (am, q, sw_if_index, mp->context);
4508       }
4509   }
4510 }
4511
4512 static void
4513 vl_api_l2_fib_table_entry_t_handler (vl_api_l2_fib_table_entry_t * mp)
4514 {
4515   clib_warning ("BUG");
4516 }
4517
4518 static void
4519 send_l2fib_table_entry (vpe_api_main_t * am,
4520                         unix_shared_memory_queue_t * q,
4521                         l2fib_entry_key_t * l2fe_key,
4522                         l2fib_entry_result_t * l2fe_res, u32 context)
4523 {
4524   vl_api_l2_fib_table_entry_t *mp;
4525
4526   mp = vl_msg_api_alloc (sizeof (*mp));
4527   memset (mp, 0, sizeof (*mp));
4528   mp->_vl_msg_id = ntohs (VL_API_L2_FIB_TABLE_ENTRY);
4529
4530   mp->bd_id =
4531     ntohl (l2input_main.bd_configs[l2fe_key->fields.bd_index].bd_id);
4532
4533   mp->mac = l2fib_make_key (l2fe_key->fields.mac, 0);
4534   mp->sw_if_index = ntohl (l2fe_res->fields.sw_if_index);
4535   mp->static_mac = l2fe_res->fields.static_mac;
4536   mp->filter_mac = l2fe_res->fields.filter;
4537   mp->bvi_mac = l2fe_res->fields.bvi;
4538   mp->context = context;
4539
4540   vl_msg_api_send_shmem (q, (u8 *) & mp);
4541 }
4542
4543 static void
4544 vl_api_l2_fib_table_dump_t_handler (vl_api_l2_fib_table_dump_t * mp)
4545 {
4546   vpe_api_main_t *am = &vpe_api_main;
4547   bd_main_t *bdm = &bd_main;
4548   l2fib_entry_key_t *l2fe_key = NULL;
4549   l2fib_entry_result_t *l2fe_res = NULL;
4550   u32 ni, bd_id = ntohl (mp->bd_id);
4551   u32 bd_index;
4552   unix_shared_memory_queue_t *q;
4553   uword *p;
4554
4555   q = vl_api_client_index_to_input_queue (mp->client_index);
4556   if (q == 0)
4557     return;
4558
4559   /* see l2fib_table_dump: ~0 means "any" */
4560   if (bd_id == ~0)
4561     bd_index = ~0;
4562   else
4563     {
4564       p = hash_get (bdm->bd_index_by_bd_id, bd_id);
4565       if (p == 0)
4566         return;
4567
4568       bd_index = p[0];
4569     }
4570
4571   l2fib_table_dump (bd_index, &l2fe_key, &l2fe_res);
4572
4573   vec_foreach_index (ni, l2fe_key)
4574   {
4575     send_l2fib_table_entry (am, q, vec_elt_at_index (l2fe_key, ni),
4576                             vec_elt_at_index (l2fe_res, ni), mp->context);
4577   }
4578   vec_free (l2fe_key);
4579   vec_free (l2fe_res);
4580 }
4581
4582 static void
4583 vl_api_show_version_t_handler (vl_api_show_version_t * mp)
4584 {
4585   vl_api_show_version_reply_t *rmp;
4586   int rv = 0;
4587   char *vpe_api_get_build_directory (void);
4588   char *vpe_api_get_version (void);
4589   char *vpe_api_get_build_date (void);
4590
4591   unix_shared_memory_queue_t *q =
4592     vl_api_client_index_to_input_queue (mp->client_index);
4593
4594   if (!q)
4595     return;
4596
4597   /* *INDENT-OFF* */
4598   REPLY_MACRO2(VL_API_SHOW_VERSION_REPLY,
4599   ({
4600     strncpy ((char *) rmp->program, "vpe", ARRAY_LEN(rmp->program)-1);
4601     strncpy ((char *) rmp->build_directory, vpe_api_get_build_directory(),
4602              ARRAY_LEN(rmp->build_directory)-1);
4603     strncpy ((char *) rmp->version, vpe_api_get_version(),
4604              ARRAY_LEN(rmp->version)-1);
4605     strncpy ((char *) rmp->build_date, vpe_api_get_build_date(),
4606              ARRAY_LEN(rmp->build_date)-1);
4607   }));
4608   /* *INDENT-ON* */
4609 }
4610
4611 static void
4612 vl_api_get_node_index_t_handler (vl_api_get_node_index_t * mp)
4613 {
4614   vlib_main_t *vm = vlib_get_main ();
4615   vl_api_get_node_index_reply_t *rmp;
4616   vlib_node_t *n;
4617   int rv = 0;
4618   u32 node_index = ~0;
4619
4620   n = vlib_get_node_by_name (vm, mp->node_name);
4621
4622   if (n == 0)
4623     rv = VNET_API_ERROR_NO_SUCH_NODE;
4624   else
4625     node_index = n->index;
4626
4627   /* *INDENT-OFF* */
4628   REPLY_MACRO2(VL_API_GET_NODE_INDEX_REPLY,
4629   ({
4630     rmp->node_index = ntohl(node_index);
4631   }));
4632   /* *INDENT-ON* */
4633 }
4634
4635 static void
4636 vl_api_get_next_index_t_handler (vl_api_get_next_index_t * mp)
4637 {
4638   vlib_main_t *vm = vlib_get_main ();
4639   vl_api_get_next_index_reply_t *rmp;
4640   vlib_node_t *node, *next_node;
4641   int rv = 0;
4642   u32 next_node_index = ~0, next_index = ~0;
4643   uword *p;
4644
4645   node = vlib_get_node_by_name (vm, mp->node_name);
4646
4647   if (node == 0)
4648     {
4649       rv = VNET_API_ERROR_NO_SUCH_NODE;
4650       goto out;
4651     }
4652
4653   next_node = vlib_get_node_by_name (vm, mp->next_name);
4654
4655   if (next_node == 0)
4656     {
4657       rv = VNET_API_ERROR_NO_SUCH_NODE2;
4658       goto out;
4659     }
4660   else
4661     next_node_index = next_node->index;
4662
4663   p = hash_get (node->next_slot_by_node, next_node_index);
4664
4665   if (p == 0)
4666     {
4667       rv = VNET_API_ERROR_NO_SUCH_ENTRY;
4668       goto out;
4669     }
4670   else
4671     next_index = p[0];
4672
4673 out:
4674   /* *INDENT-OFF* */
4675   REPLY_MACRO2(VL_API_GET_NEXT_INDEX_REPLY,
4676   ({
4677     rmp->next_index = ntohl(next_index);
4678   }));
4679   /* *INDENT-ON* */
4680 }
4681
4682 static void
4683 vl_api_add_node_next_t_handler (vl_api_add_node_next_t * mp)
4684 {
4685   vlib_main_t *vm = vlib_get_main ();
4686   vl_api_add_node_next_reply_t *rmp;
4687   vlib_node_t *n, *next;
4688   int rv = 0;
4689   u32 next_index = ~0;
4690
4691   n = vlib_get_node_by_name (vm, mp->node_name);
4692
4693   if (n == 0)
4694     {
4695       rv = VNET_API_ERROR_NO_SUCH_NODE;
4696       goto out;
4697     }
4698
4699   next = vlib_get_node_by_name (vm, mp->next_name);
4700
4701   if (next == 0)
4702     rv = VNET_API_ERROR_NO_SUCH_NODE2;
4703   else
4704     next_index = vlib_node_add_next (vm, n->index, next->index);
4705
4706 out:
4707   /* *INDENT-OFF* */
4708   REPLY_MACRO2(VL_API_GET_NODE_INDEX_REPLY,
4709   ({
4710     rmp->next_index = ntohl(next_index);
4711   }));
4712   /* *INDENT-ON* */
4713 }
4714
4715 static void vl_api_l2tpv3_create_tunnel_t_handler
4716   (vl_api_l2tpv3_create_tunnel_t * mp)
4717 {
4718   vl_api_l2tpv3_create_tunnel_reply_t *rmp;
4719   l2t_main_t *lm = &l2t_main;
4720   u32 sw_if_index = (u32) ~ 0;
4721   int rv;
4722
4723   if (mp->is_ipv6 != 1)
4724     {
4725       rv = VNET_API_ERROR_UNIMPLEMENTED;
4726       goto out;
4727     }
4728
4729   u32 encap_fib_index;
4730
4731   if (mp->encap_vrf_id != ~0)
4732     {
4733       uword *p;
4734       ip6_main_t *im = &ip6_main;
4735       if (!
4736           (p =
4737            hash_get (im->fib_index_by_table_id, ntohl (mp->encap_vrf_id))))
4738         {
4739           rv = VNET_API_ERROR_NO_SUCH_FIB;
4740           goto out;
4741         }
4742       encap_fib_index = p[0];
4743     }
4744   else
4745     {
4746       encap_fib_index = ~0;
4747     }
4748
4749   rv = create_l2tpv3_ipv6_tunnel (lm,
4750                                   (ip6_address_t *) mp->client_address,
4751                                   (ip6_address_t *) mp->our_address,
4752                                   ntohl (mp->local_session_id),
4753                                   ntohl (mp->remote_session_id),
4754                                   clib_net_to_host_u64 (mp->local_cookie),
4755                                   clib_net_to_host_u64 (mp->remote_cookie),
4756                                   mp->l2_sublayer_present,
4757                                   encap_fib_index, &sw_if_index);
4758
4759 out:
4760   /* *INDENT-OFF* */
4761   REPLY_MACRO2(VL_API_L2TPV3_CREATE_TUNNEL_REPLY,
4762   ({
4763     rmp->sw_if_index = ntohl (sw_if_index);
4764   }));
4765   /* *INDENT-ON* */
4766 }
4767
4768 static void vl_api_l2tpv3_set_tunnel_cookies_t_handler
4769   (vl_api_l2tpv3_set_tunnel_cookies_t * mp)
4770 {
4771   vl_api_l2tpv3_set_tunnel_cookies_reply_t *rmp;
4772   l2t_main_t *lm = &l2t_main;
4773   int rv;
4774
4775   VALIDATE_SW_IF_INDEX (mp);
4776
4777   rv = l2tpv3_set_tunnel_cookies (lm, ntohl (mp->sw_if_index),
4778                                   clib_net_to_host_u64 (mp->new_local_cookie),
4779                                   clib_net_to_host_u64
4780                                   (mp->new_remote_cookie));
4781
4782   BAD_SW_IF_INDEX_LABEL;
4783
4784   REPLY_MACRO (VL_API_L2TPV3_SET_TUNNEL_COOKIES_REPLY);
4785 }
4786
4787 static void vl_api_l2tpv3_interface_enable_disable_t_handler
4788   (vl_api_l2tpv3_interface_enable_disable_t * mp)
4789 {
4790   int rv;
4791   vnet_main_t *vnm = vnet_get_main ();
4792   vl_api_l2tpv3_interface_enable_disable_reply_t *rmp;
4793
4794   VALIDATE_SW_IF_INDEX (mp);
4795
4796   rv = l2tpv3_interface_enable_disable
4797     (vnm, ntohl (mp->sw_if_index), mp->enable_disable);
4798
4799   BAD_SW_IF_INDEX_LABEL;
4800
4801   REPLY_MACRO (VL_API_L2TPV3_INTERFACE_ENABLE_DISABLE_REPLY);
4802 }
4803
4804 static void vl_api_l2tpv3_set_lookup_key_t_handler
4805   (vl_api_l2tpv3_set_lookup_key_t * mp)
4806 {
4807   int rv = 0;
4808   l2t_main_t *lm = &l2t_main;
4809   vl_api_l2tpv3_set_lookup_key_reply_t *rmp;
4810
4811   if (mp->key > L2T_LOOKUP_SESSION_ID)
4812     {
4813       rv = VNET_API_ERROR_INVALID_VALUE;
4814       goto out;
4815     }
4816
4817   lm->lookup_type = mp->key;
4818
4819 out:
4820   REPLY_MACRO (VL_API_L2TPV3_SET_LOOKUP_KEY_REPLY);
4821 }
4822
4823 static void vl_api_vxlan_add_del_tunnel_t_handler
4824   (vl_api_vxlan_add_del_tunnel_t * mp)
4825 {
4826   vl_api_vxlan_add_del_tunnel_reply_t *rmp;
4827   int rv = 0;
4828   vnet_vxlan_add_del_tunnel_args_t _a, *a = &_a;
4829   u32 encap_fib_index;
4830   uword *p;
4831   ip4_main_t *im = &ip4_main;
4832   u32 sw_if_index = ~0;
4833
4834   p = hash_get (im->fib_index_by_table_id, ntohl (mp->encap_vrf_id));
4835   if (!p)
4836     {
4837       rv = VNET_API_ERROR_NO_SUCH_FIB;
4838       goto out;
4839     }
4840   encap_fib_index = p[0];
4841
4842   /* Check src & dst are different */
4843   if ((mp->is_ipv6 && memcmp (mp->src_address, mp->dst_address, 16) == 0) ||
4844       (!mp->is_ipv6 && memcmp (mp->src_address, mp->dst_address, 4) == 0))
4845     {
4846       rv = VNET_API_ERROR_SAME_SRC_DST;
4847       goto out;
4848     }
4849   memset (a, 0, sizeof (*a));
4850
4851   a->is_add = mp->is_add;
4852   a->is_ip6 = mp->is_ipv6;
4853
4854   /* ip addresses sent in network byte order */
4855   if (a->is_ip6)
4856     {
4857       memcpy (&(a->src.ip6), mp->src_address, 16);
4858       memcpy (&(a->dst.ip6), mp->dst_address, 16);
4859     }
4860   else
4861     {
4862       memcpy (&(a->src.ip4), mp->src_address, 4);
4863       memcpy (&(a->dst.ip4), mp->dst_address, 4);
4864     }
4865
4866   a->encap_fib_index = encap_fib_index;
4867   a->decap_next_index = ntohl (mp->decap_next_index);
4868   a->vni = ntohl (mp->vni);
4869   rv = vnet_vxlan_add_del_tunnel (a, &sw_if_index);
4870
4871 out:
4872   /* *INDENT-OFF* */
4873   REPLY_MACRO2(VL_API_VXLAN_ADD_DEL_TUNNEL_REPLY,
4874   ({
4875     rmp->sw_if_index = ntohl (sw_if_index);
4876   }));
4877   /* *INDENT-ON* */
4878 }
4879
4880 static void send_vxlan_tunnel_details
4881   (vxlan_tunnel_t * t, unix_shared_memory_queue_t * q, u32 context)
4882 {
4883   vl_api_vxlan_tunnel_details_t *rmp;
4884   ip4_main_t *im4 = &ip4_main;
4885   ip6_main_t *im6 = &ip6_main;
4886   u8 is_ipv6 = !ip46_address_is_ip4 (&t->dst);
4887
4888   rmp = vl_msg_api_alloc (sizeof (*rmp));
4889   memset (rmp, 0, sizeof (*rmp));
4890   rmp->_vl_msg_id = ntohs (VL_API_VXLAN_TUNNEL_DETAILS);
4891   if (is_ipv6)
4892     {
4893       memcpy (rmp->src_address, &(t->src.ip6), 16);
4894       memcpy (rmp->dst_address, &(t->dst.ip6), 16);
4895       rmp->encap_vrf_id = htonl (im6->fibs[t->encap_fib_index].ft_table_id);
4896     }
4897   else
4898     {
4899       memcpy (rmp->src_address, &(t->src.ip4), 4);
4900       memcpy (rmp->dst_address, &(t->dst.ip4), 4);
4901       rmp->encap_vrf_id = htonl (im4->fibs[t->encap_fib_index].ft_table_id);
4902     }
4903   rmp->vni = htonl (t->vni);
4904   /* decap_next_index is deprecated, hard code to l2-input */
4905   rmp->decap_next_index = htonl (VXLAN_INPUT_NEXT_L2_INPUT);
4906   rmp->sw_if_index = htonl (t->sw_if_index);
4907   rmp->is_ipv6 = is_ipv6;
4908   rmp->context = context;
4909
4910   vl_msg_api_send_shmem (q, (u8 *) & rmp);
4911 }
4912
4913 static void vl_api_vxlan_tunnel_dump_t_handler
4914   (vl_api_vxlan_tunnel_dump_t * mp)
4915 {
4916   unix_shared_memory_queue_t *q;
4917   vxlan_main_t *vxm = &vxlan_main;
4918   vxlan_tunnel_t *t;
4919   u32 sw_if_index;
4920
4921   q = vl_api_client_index_to_input_queue (mp->client_index);
4922   if (q == 0)
4923     {
4924       return;
4925     }
4926
4927   sw_if_index = ntohl (mp->sw_if_index);
4928
4929   if (~0 == sw_if_index)
4930     {
4931       /* *INDENT-OFF* */
4932       pool_foreach (t, vxm->tunnels,
4933       ({
4934         send_vxlan_tunnel_details(t, q, mp->context);
4935       }));
4936       /* *INDENT-ON* */
4937     }
4938   else
4939     {
4940       if ((sw_if_index >= vec_len (vxm->tunnel_index_by_sw_if_index)) ||
4941           (~0 == vxm->tunnel_index_by_sw_if_index[sw_if_index]))
4942         {
4943           return;
4944         }
4945       t = &vxm->tunnels[vxm->tunnel_index_by_sw_if_index[sw_if_index]];
4946       send_vxlan_tunnel_details (t, q, mp->context);
4947     }
4948 }
4949
4950 static void vl_api_gre_add_del_tunnel_t_handler
4951   (vl_api_gre_add_del_tunnel_t * mp)
4952 {
4953   vl_api_gre_add_del_tunnel_reply_t *rmp;
4954   int rv = 0;
4955   vnet_gre_add_del_tunnel_args_t _a, *a = &_a;
4956   u32 outer_fib_id;
4957   uword *p;
4958   ip4_main_t *im = &ip4_main;
4959   u32 sw_if_index = ~0;
4960
4961   p = hash_get (im->fib_index_by_table_id, ntohl (mp->outer_fib_id));
4962   if (!p)
4963     {
4964       rv = VNET_API_ERROR_NO_SUCH_FIB;
4965       goto out;
4966     }
4967   outer_fib_id = p[0];
4968
4969   /* Check src & dst are different */
4970   if ((mp->is_ipv6 && memcmp (mp->src_address, mp->dst_address, 16) == 0) ||
4971       (!mp->is_ipv6 && memcmp (mp->src_address, mp->dst_address, 4) == 0))
4972     {
4973       rv = VNET_API_ERROR_SAME_SRC_DST;
4974       goto out;
4975     }
4976   memset (a, 0, sizeof (*a));
4977
4978   a->is_add = mp->is_add;
4979   a->teb = mp->teb;
4980
4981   /* ip addresses sent in network byte order */
4982   clib_memcpy (&(a->src), mp->src_address, 4);
4983   clib_memcpy (&(a->dst), mp->dst_address, 4);
4984
4985   a->outer_fib_id = outer_fib_id;
4986   rv = vnet_gre_add_del_tunnel (a, &sw_if_index);
4987
4988 out:
4989   /* *INDENT-OFF* */
4990   REPLY_MACRO2(VL_API_GRE_ADD_DEL_TUNNEL_REPLY,
4991   ({
4992     rmp->sw_if_index = ntohl (sw_if_index);
4993   }));
4994   /* *INDENT-ON* */
4995 }
4996
4997 static void send_gre_tunnel_details
4998   (gre_tunnel_t * t, unix_shared_memory_queue_t * q, u32 context)
4999 {
5000   vl_api_gre_tunnel_details_t *rmp;
5001   ip4_main_t *im = &ip4_main;
5002
5003   rmp = vl_msg_api_alloc (sizeof (*rmp));
5004   memset (rmp, 0, sizeof (*rmp));
5005   rmp->_vl_msg_id = ntohs (VL_API_GRE_TUNNEL_DETAILS);
5006   clib_memcpy (rmp->src_address, &(t->tunnel_src), 4);
5007   clib_memcpy (rmp->dst_address, &(t->tunnel_dst), 4);
5008   rmp->outer_fib_id = htonl (im->fibs[t->outer_fib_index].ft_table_id);
5009   rmp->teb = t->teb;
5010   rmp->sw_if_index = htonl (t->sw_if_index);
5011   rmp->context = context;
5012
5013   vl_msg_api_send_shmem (q, (u8 *) & rmp);
5014 }
5015
5016 static void
5017 vl_api_gre_tunnel_dump_t_handler (vl_api_gre_tunnel_dump_t * mp)
5018 {
5019   unix_shared_memory_queue_t *q;
5020   gre_main_t *gm = &gre_main;
5021   gre_tunnel_t *t;
5022   u32 sw_if_index;
5023
5024   q = vl_api_client_index_to_input_queue (mp->client_index);
5025   if (q == 0)
5026     {
5027       return;
5028     }
5029
5030   sw_if_index = ntohl (mp->sw_if_index);
5031
5032   if (~0 == sw_if_index)
5033     {
5034       /* *INDENT-OFF* */
5035       pool_foreach (t, gm->tunnels,
5036       ({
5037         send_gre_tunnel_details(t, q, mp->context);
5038       }));
5039       /* *INDENT-ON* */
5040     }
5041   else
5042     {
5043       if ((sw_if_index >= vec_len (gm->tunnel_index_by_sw_if_index)) ||
5044           (~0 == gm->tunnel_index_by_sw_if_index[sw_if_index]))
5045         {
5046           return;
5047         }
5048       t = &gm->tunnels[gm->tunnel_index_by_sw_if_index[sw_if_index]];
5049       send_gre_tunnel_details (t, q, mp->context);
5050     }
5051 }
5052
5053 static void
5054 vl_api_l2_patch_add_del_t_handler (vl_api_l2_patch_add_del_t * mp)
5055 {
5056   extern int vnet_l2_patch_add_del (u32 rx_sw_if_index, u32 tx_sw_if_index,
5057                                     int is_add);
5058   vl_api_l2_patch_add_del_reply_t *rmp;
5059   int vnet_l2_patch_add_del (u32 rx_sw_if_index, u32 tx_sw_if_index,
5060                              int is_add);
5061   int rv = 0;
5062
5063   VALIDATE_RX_SW_IF_INDEX (mp);
5064   VALIDATE_TX_SW_IF_INDEX (mp);
5065
5066   rv = vnet_l2_patch_add_del (ntohl (mp->rx_sw_if_index),
5067                               ntohl (mp->tx_sw_if_index),
5068                               (int) (mp->is_add != 0));
5069
5070   BAD_RX_SW_IF_INDEX_LABEL;
5071   BAD_TX_SW_IF_INDEX_LABEL;
5072
5073   REPLY_MACRO (VL_API_L2_PATCH_ADD_DEL_REPLY);
5074 }
5075
5076 static void
5077   vl_api_vxlan_gpe_add_del_tunnel_t_handler
5078   (vl_api_vxlan_gpe_add_del_tunnel_t * mp)
5079 {
5080   vl_api_vxlan_gpe_add_del_tunnel_reply_t *rmp;
5081   int rv = 0;
5082   vnet_vxlan_gpe_add_del_tunnel_args_t _a, *a = &_a;
5083   u32 encap_fib_index, decap_fib_index;
5084   u8 protocol;
5085   uword *p;
5086   ip4_main_t *im = &ip4_main;
5087   u32 sw_if_index = ~0;
5088
5089
5090   p = hash_get (im->fib_index_by_table_id, ntohl (mp->encap_vrf_id));
5091   if (!p)
5092     {
5093       rv = VNET_API_ERROR_NO_SUCH_FIB;
5094       goto out;
5095     }
5096   encap_fib_index = p[0];
5097
5098   protocol = mp->protocol;
5099
5100   /* Interpret decap_vrf_id as an opaque if sending to other-than-ip4-input */
5101   if (protocol == VXLAN_GPE_INPUT_NEXT_IP4_INPUT)
5102     {
5103       p = hash_get (im->fib_index_by_table_id, ntohl (mp->decap_vrf_id));
5104       if (!p)
5105         {
5106           rv = VNET_API_ERROR_NO_SUCH_INNER_FIB;
5107           goto out;
5108         }
5109       decap_fib_index = p[0];
5110     }
5111   else
5112     {
5113       decap_fib_index = ntohl (mp->decap_vrf_id);
5114     }
5115
5116   /* Check src & dst are different */
5117   if ((mp->is_ipv6 && memcmp (mp->local, mp->remote, 16) == 0) ||
5118       (!mp->is_ipv6 && memcmp (mp->local, mp->remote, 4) == 0))
5119     {
5120       rv = VNET_API_ERROR_SAME_SRC_DST;
5121       goto out;
5122     }
5123   memset (a, 0, sizeof (*a));
5124
5125   a->is_add = mp->is_add;
5126   a->is_ip6 = mp->is_ipv6;
5127   /* ip addresses sent in network byte order */
5128   if (a->is_ip6)
5129     {
5130       clib_memcpy (&(a->local.ip6), mp->local, 16);
5131       clib_memcpy (&(a->remote.ip6), mp->remote, 16);
5132     }
5133   else
5134     {
5135       clib_memcpy (&(a->local.ip4), mp->local, 4);
5136       clib_memcpy (&(a->remote.ip4), mp->remote, 4);
5137     }
5138   a->encap_fib_index = encap_fib_index;
5139   a->decap_fib_index = decap_fib_index;
5140   a->protocol = protocol;
5141   a->vni = ntohl (mp->vni);
5142   rv = vnet_vxlan_gpe_add_del_tunnel (a, &sw_if_index);
5143
5144 out:
5145   /* *INDENT-OFF* */
5146   REPLY_MACRO2(VL_API_VXLAN_GPE_ADD_DEL_TUNNEL_REPLY,
5147   ({
5148     rmp->sw_if_index = ntohl (sw_if_index);
5149   }));
5150   /* *INDENT-ON* */
5151 }
5152
5153 static void send_vxlan_gpe_tunnel_details
5154   (vxlan_gpe_tunnel_t * t, unix_shared_memory_queue_t * q, u32 context)
5155 {
5156   vl_api_vxlan_gpe_tunnel_details_t *rmp;
5157   ip4_main_t *im4 = &ip4_main;
5158   ip6_main_t *im6 = &ip6_main;
5159   u8 is_ipv6 = !(t->flags & VXLAN_GPE_TUNNEL_IS_IPV4);
5160
5161   rmp = vl_msg_api_alloc (sizeof (*rmp));
5162   memset (rmp, 0, sizeof (*rmp));
5163   rmp->_vl_msg_id = ntohs (VL_API_VXLAN_GPE_TUNNEL_DETAILS);
5164   if (is_ipv6)
5165     {
5166       memcpy (rmp->local, &(t->local.ip6), 16);
5167       memcpy (rmp->remote, &(t->remote.ip6), 16);
5168       rmp->encap_vrf_id = htonl (im6->fibs[t->encap_fib_index].ft_table_id);
5169       rmp->decap_vrf_id = htonl (im6->fibs[t->decap_fib_index].ft_table_id);
5170     }
5171   else
5172     {
5173       memcpy (rmp->local, &(t->local.ip4), 4);
5174       memcpy (rmp->remote, &(t->remote.ip4), 4);
5175       rmp->encap_vrf_id = htonl (im4->fibs[t->encap_fib_index].ft_table_id);
5176       rmp->decap_vrf_id = htonl (im4->fibs[t->decap_fib_index].ft_table_id);
5177     }
5178   rmp->vni = htonl (t->vni);
5179   rmp->protocol = t->protocol;
5180   rmp->sw_if_index = htonl (t->sw_if_index);
5181   rmp->is_ipv6 = is_ipv6;
5182   rmp->context = context;
5183
5184   vl_msg_api_send_shmem (q, (u8 *) & rmp);
5185 }
5186
5187 static void vl_api_vxlan_gpe_tunnel_dump_t_handler
5188   (vl_api_vxlan_gpe_tunnel_dump_t * mp)
5189 {
5190   unix_shared_memory_queue_t *q;
5191   vxlan_gpe_main_t *vgm = &vxlan_gpe_main;
5192   vxlan_gpe_tunnel_t *t;
5193   u32 sw_if_index;
5194
5195   q = vl_api_client_index_to_input_queue (mp->client_index);
5196   if (q == 0)
5197     {
5198       return;
5199     }
5200
5201   sw_if_index = ntohl (mp->sw_if_index);
5202
5203   if (~0 == sw_if_index)
5204     {
5205       /* *INDENT-OFF* */
5206       pool_foreach (t, vgm->tunnels,
5207       ({
5208         send_vxlan_gpe_tunnel_details(t, q, mp->context);
5209       }));
5210       /* *INDENT-ON* */
5211     }
5212   else
5213     {
5214       if ((sw_if_index >= vec_len (vgm->tunnel_index_by_sw_if_index)) ||
5215           (~0 == vgm->tunnel_index_by_sw_if_index[sw_if_index]))
5216         {
5217           return;
5218         }
5219       t = &vgm->tunnels[vgm->tunnel_index_by_sw_if_index[sw_if_index]];
5220       send_vxlan_gpe_tunnel_details (t, q, mp->context);
5221     }
5222 }
5223
5224 /** Used for transferring locators via VPP API */
5225 /* *INDENT-OFF* */
5226 typedef CLIB_PACKED (struct {
5227   u32 sw_if_index; /**< locator sw_if_index */
5228   u8 priority; /**< locator priority */
5229   u8 weight; /**< locator weight */
5230 }) ls_locator_t;
5231 /* *INDENT-ON* */
5232
5233 static void
5234 vl_api_lisp_add_del_locator_set_t_handler (vl_api_lisp_add_del_locator_set_t *
5235                                            mp)
5236 {
5237   vl_api_lisp_add_del_locator_set_reply_t *rmp;
5238   int rv = 0;
5239   vnet_lisp_add_del_locator_set_args_t _a, *a = &_a;
5240   locator_t locator;
5241   ls_locator_t *ls_loc;
5242   u32 ls_index = ~0, locator_num;
5243   u8 *locator_name = NULL;
5244   int i;
5245
5246   memset (a, 0, sizeof (a[0]));
5247
5248   locator_name = format (0, "%s", mp->locator_set_name);
5249
5250   a->name = locator_name;
5251   a->is_add = mp->is_add;
5252   a->local = 1;
5253   locator_num = clib_net_to_host_u32 (mp->locator_num);
5254
5255   memset (&locator, 0, sizeof (locator));
5256   for (i = 0; i < locator_num; i++)
5257     {
5258       ls_loc = &((ls_locator_t *) mp->locators)[i];
5259       VALIDATE_SW_IF_INDEX (ls_loc);
5260
5261       locator.sw_if_index = htonl (ls_loc->sw_if_index);
5262       locator.priority = ls_loc->priority;
5263       locator.weight = ls_loc->weight;
5264       locator.local = 1;
5265       vec_add1 (a->locators, locator);
5266     }
5267
5268   rv = vnet_lisp_add_del_locator_set (a, &ls_index);
5269
5270   BAD_SW_IF_INDEX_LABEL;
5271
5272   vec_free (locator_name);
5273   vec_free (a->locators);
5274
5275   /* *INDENT-OFF* */
5276   REPLY_MACRO2 (VL_API_LISP_ADD_DEL_LOCATOR_SET_REPLY,
5277   ({
5278     rmp->ls_index = clib_host_to_net_u32 (ls_index);
5279   }));
5280   /* *INDENT-ON* */
5281 }
5282
5283 static void
5284 vl_api_lisp_add_del_locator_t_handler (vl_api_lisp_add_del_locator_t * mp)
5285 {
5286   vl_api_lisp_add_del_locator_reply_t *rmp;
5287   int rv = 0;
5288   locator_t locator, *locators = NULL;
5289   vnet_lisp_add_del_locator_set_args_t _a, *a = &_a;
5290   u32 ls_index = ~0;
5291   u8 *locator_name = NULL;
5292
5293   memset (&locator, 0, sizeof (locator));
5294   memset (a, 0, sizeof (a[0]));
5295
5296   locator.sw_if_index = ntohl (mp->sw_if_index);
5297   locator.priority = mp->priority;
5298   locator.weight = mp->weight;
5299   locator.local = 1;
5300   vec_add1 (locators, locator);
5301
5302   locator_name = format (0, "%s", mp->locator_set_name);
5303
5304   a->name = locator_name;
5305   a->locators = locators;
5306   a->is_add = mp->is_add;
5307   a->local = 1;
5308
5309   rv = vnet_lisp_add_del_locator (a, NULL, &ls_index);
5310
5311   vec_free (locators);
5312   vec_free (locator_name);
5313
5314   REPLY_MACRO (VL_API_LISP_ADD_DEL_LOCATOR_REPLY);
5315 }
5316
5317 static int
5318 unformat_lisp_eid_api (gid_address_t * dst, u32 vni, u8 type, void *src,
5319                        u8 len)
5320 {
5321   switch (type)
5322     {
5323     case 0:                     /* ipv4 */
5324       gid_address_type (dst) = GID_ADDR_IP_PREFIX;
5325       gid_address_ip_set (dst, src, IP4);
5326       gid_address_ippref_len (dst) = len;
5327       ip_prefix_normalize (&gid_address_ippref (dst));
5328       break;
5329     case 1:                     /* ipv6 */
5330       gid_address_type (dst) = GID_ADDR_IP_PREFIX;
5331       gid_address_ip_set (dst, src, IP6);
5332       gid_address_ippref_len (dst) = len;
5333       ip_prefix_normalize (&gid_address_ippref (dst));
5334       break;
5335     case 2:                     /* l2 mac */
5336       gid_address_type (dst) = GID_ADDR_MAC;
5337       clib_memcpy (&gid_address_mac (dst), src, 6);
5338       break;
5339     default:
5340       /* unknown type */
5341       return VNET_API_ERROR_INVALID_VALUE;
5342     }
5343
5344   gid_address_vni (dst) = vni;
5345
5346   return 0;
5347 }
5348
5349 static void
5350 vl_api_lisp_add_del_local_eid_t_handler (vl_api_lisp_add_del_local_eid_t * mp)
5351 {
5352   vl_api_lisp_add_del_local_eid_reply_t *rmp;
5353   lisp_cp_main_t *lcm = vnet_lisp_cp_get_main ();
5354   int rv = 0;
5355   gid_address_t _eid, *eid = &_eid;
5356   uword *p = NULL;
5357   u32 locator_set_index = ~0, map_index = ~0;
5358   vnet_lisp_add_del_mapping_args_t _a, *a = &_a;
5359   u8 *name = NULL;
5360   memset (a, 0, sizeof (a[0]));
5361   memset (eid, 0, sizeof (eid[0]));
5362
5363   rv = unformat_lisp_eid_api (eid, clib_net_to_host_u32 (mp->vni),
5364                               mp->eid_type, mp->eid, mp->prefix_len);
5365   if (rv)
5366     goto out;
5367
5368   name = format (0, "%s", mp->locator_set_name);
5369   p = hash_get_mem (lcm->locator_set_index_by_name, name);
5370   if (!p)
5371     {
5372       rv = VNET_API_ERROR_INVALID_VALUE;
5373       goto out;
5374     }
5375   locator_set_index = p[0];
5376
5377   /* XXX treat batch configuration */
5378   a->is_add = mp->is_add;
5379   gid_address_copy (&a->eid, eid);
5380   a->locator_set_index = locator_set_index;
5381   a->local = 1;
5382   rv = vnet_lisp_add_del_local_mapping (a, &map_index);
5383
5384 out:
5385   vec_free (name);
5386   gid_address_free (&a->eid);
5387
5388   REPLY_MACRO (VL_API_LISP_ADD_DEL_LOCAL_EID_REPLY);
5389 }
5390
5391 static void
5392   vl_api_lisp_eid_table_add_del_map_t_handler
5393   (vl_api_lisp_eid_table_add_del_map_t * mp)
5394 {
5395   vl_api_lisp_eid_table_add_del_map_reply_t *rmp;
5396   int rv = 0;
5397   rv = vnet_lisp_eid_table_map (clib_net_to_host_u32 (mp->vni),
5398                                 clib_net_to_host_u32 (mp->dp_table),
5399                                 mp->is_l2, mp->is_add);
5400 REPLY_MACRO (VL_API_LISP_EID_TABLE_ADD_DEL_MAP_REPLY)}
5401
5402 /** Used for transferring locators via VPP API */
5403 /* *INDENT-OFF* */
5404 typedef CLIB_PACKED (struct {
5405   u8 is_ip4; /**< is locator an IPv4 address */
5406   u8 priority; /**< locator priority */
5407   u8 weight; /**< locator weight */
5408   u8 addr[16]; /**< IPv4/IPv6 address */
5409 }) rloc_t;
5410 /* *INDENT-ON* */
5411
5412 static locator_pair_t *
5413 unformat_lisp_loc_pairs (void *lcl_locs, void *rmt_locs, u32 rloc_num)
5414 {
5415   u32 i;
5416   locator_pair_t *pairs = 0, pair;
5417   rloc_t *r;
5418
5419   for (i = 0; i < rloc_num; i++)
5420     {
5421       /* local locator */
5422       r = &((rloc_t *) lcl_locs)[i];
5423       memset (&pair.lcl_loc, 0, sizeof (pair.lcl_loc));
5424       ip_address_set (&pair.lcl_loc, &r->addr, r->is_ip4 ? IP4 : IP6);
5425
5426       /* remote locators */
5427       r = &((rloc_t *) rmt_locs)[i];
5428       memset (&pair.rmt_loc, 0, sizeof (pair.rmt_loc));
5429       ip_address_set (&pair.rmt_loc, &r->addr, r->is_ip4 ? IP4 : IP6);
5430
5431       pair.priority = r->priority;
5432       pair.weight = r->weight;
5433
5434       vec_add1 (pairs, pair);
5435     }
5436   return pairs;
5437 }
5438
5439 static locator_t *
5440 unformat_lisp_locs (void *rmt_locs, u32 rloc_num)
5441 {
5442   u32 i;
5443   locator_t *locs = 0, loc;
5444   rloc_t *r;
5445
5446   for (i = 0; i < rloc_num; i++)
5447     {
5448       /* remote locators */
5449       r = &((rloc_t *) rmt_locs)[i];
5450       memset (&loc, 0, sizeof (loc));
5451       gid_address_ip_set (&loc.address, &r->addr, r->is_ip4 ? IP4 : IP6);
5452
5453       loc.priority = r->priority;
5454       loc.weight = r->weight;
5455
5456       vec_add1 (locs, loc);
5457     }
5458   return locs;
5459 }
5460
5461 static void
5462   vl_api_lisp_gpe_add_del_fwd_entry_t_handler
5463   (vl_api_lisp_gpe_add_del_fwd_entry_t * mp)
5464 {
5465   vl_api_lisp_gpe_add_del_fwd_entry_reply_t *rmp;
5466   vnet_lisp_gpe_add_del_fwd_entry_args_t _a, *a = &_a;
5467   locator_pair_t *pairs = 0;
5468   int rv = 0;
5469
5470   memset (a, 0, sizeof (a[0]));
5471
5472   rv = unformat_lisp_eid_api (&a->rmt_eid, mp->vni, mp->eid_type,
5473                               mp->rmt_eid, mp->rmt_len);
5474   rv |= unformat_lisp_eid_api (&a->lcl_eid, mp->vni, mp->eid_type,
5475                                mp->lcl_eid, mp->lcl_len);
5476
5477   pairs = unformat_lisp_loc_pairs (mp->lcl_locs, mp->rmt_locs, mp->loc_num);
5478
5479   if (rv || 0 == pairs)
5480     goto send_reply;
5481
5482   a->is_add = mp->is_add;
5483   a->locator_pairs = pairs;
5484   a->dp_table = mp->dp_table;
5485   a->vni = mp->vni;
5486   a->action = mp->action;
5487
5488   rv = vnet_lisp_gpe_add_del_fwd_entry (a, 0);
5489   vec_free (pairs);
5490 send_reply:
5491   REPLY_MACRO (VL_API_LISP_GPE_ADD_DEL_FWD_ENTRY_REPLY);
5492 }
5493
5494 static void
5495 vl_api_lisp_add_del_map_resolver_t_handler (vl_api_lisp_add_del_map_resolver_t
5496                                             * mp)
5497 {
5498   vl_api_lisp_add_del_map_resolver_reply_t *rmp;
5499   int rv = 0;
5500   vnet_lisp_add_del_map_resolver_args_t _a, *a = &_a;
5501
5502   memset (a, 0, sizeof (a[0]));
5503
5504   a->is_add = mp->is_add;
5505   ip_address_set (&a->address, mp->ip_address, mp->is_ipv6 ? IP6 : IP4);
5506
5507   rv = vnet_lisp_add_del_map_resolver (a);
5508
5509   REPLY_MACRO (VL_API_LISP_ADD_DEL_MAP_RESOLVER_REPLY);
5510 }
5511
5512 static void
5513 vl_api_lisp_gpe_enable_disable_t_handler (vl_api_lisp_gpe_enable_disable_t *
5514                                           mp)
5515 {
5516   vl_api_lisp_gpe_enable_disable_reply_t *rmp;
5517   int rv = 0;
5518   vnet_lisp_gpe_enable_disable_args_t _a, *a = &_a;
5519
5520   a->is_en = mp->is_en;
5521   vnet_lisp_gpe_enable_disable (a);
5522
5523   REPLY_MACRO (VL_API_LISP_GPE_ENABLE_DISABLE_REPLY);
5524 }
5525
5526 static void
5527 vl_api_lisp_enable_disable_t_handler (vl_api_lisp_enable_disable_t * mp)
5528 {
5529   vl_api_lisp_enable_disable_reply_t *rmp;
5530   int rv = 0;
5531
5532   vnet_lisp_enable_disable (mp->is_en);
5533   REPLY_MACRO (VL_API_LISP_ENABLE_DISABLE_REPLY);
5534 }
5535
5536 static void
5537 vl_api_lisp_gpe_add_del_iface_t_handler (vl_api_lisp_gpe_add_del_iface_t * mp)
5538 {
5539   vl_api_lisp_gpe_add_del_iface_reply_t *rmp;
5540   int rv = 0;
5541
5542   if (mp->is_l2)
5543     {
5544       if (mp->is_add)
5545         {
5546           if (~0 ==
5547               lisp_gpe_tenant_l2_iface_add_or_lock (mp->vni, mp->dp_table))
5548             rv = 1;
5549         }
5550       else
5551         lisp_gpe_tenant_l2_iface_unlock (mp->vni);
5552     }
5553   else
5554     {
5555       if (mp->is_add)
5556         {
5557           if (~0 ==
5558               lisp_gpe_tenant_l3_iface_add_or_lock (mp->vni, mp->dp_table))
5559             rv = 1;
5560         }
5561       else
5562         lisp_gpe_tenant_l3_iface_unlock (mp->vni);
5563     }
5564
5565   REPLY_MACRO (VL_API_LISP_GPE_ADD_DEL_IFACE_REPLY);
5566 }
5567
5568 static void
5569   vl_api_show_lisp_map_request_mode_t_handler
5570   (vl_api_show_lisp_map_request_mode_t * mp)
5571 {
5572   int rv = 0;
5573   vl_api_show_lisp_map_request_mode_reply_t *rmp;
5574
5575   /* *INDENT-OFF* */
5576   REPLY_MACRO2(VL_API_SHOW_LISP_MAP_REQUEST_MODE_REPLY,
5577   ({
5578     rmp->mode = vnet_lisp_get_map_request_mode ();
5579   }));
5580   /* *INDENT-ON* */
5581 }
5582
5583 static void
5584 vl_api_lisp_map_request_mode_t_handler (vl_api_lisp_map_request_mode_t * mp)
5585 {
5586   vl_api_lisp_map_request_mode_reply_t *rmp;
5587   int rv = 0;
5588
5589   rv = vnet_lisp_set_map_request_mode (mp->mode);
5590
5591   REPLY_MACRO (VL_API_LISP_MAP_REQUEST_MODE_REPLY);
5592 }
5593
5594 static void
5595 vl_api_lisp_pitr_set_locator_set_t_handler (vl_api_lisp_pitr_set_locator_set_t
5596                                             * mp)
5597 {
5598   vl_api_lisp_pitr_set_locator_set_reply_t *rmp;
5599   int rv = 0;
5600   u8 *ls_name = 0;
5601
5602   ls_name = format (0, "%s", mp->ls_name);
5603   rv = vnet_lisp_pitr_set_locator_set (ls_name, mp->is_add);
5604   vec_free (ls_name);
5605
5606   REPLY_MACRO (VL_API_LISP_PITR_SET_LOCATOR_SET_REPLY);
5607 }
5608
5609 static void
5610   vl_api_lisp_add_del_map_request_itr_rlocs_t_handler
5611   (vl_api_lisp_add_del_map_request_itr_rlocs_t * mp)
5612 {
5613   vl_api_lisp_add_del_map_request_itr_rlocs_reply_t *rmp;
5614   int rv = 0;
5615   u8 *locator_set_name = NULL;
5616   vnet_lisp_add_del_mreq_itr_rloc_args_t _a, *a = &_a;
5617
5618   locator_set_name = format (0, "%s", mp->locator_set_name);
5619
5620   a->is_add = mp->is_add;
5621   a->locator_set_name = locator_set_name;
5622
5623   rv = vnet_lisp_add_del_mreq_itr_rlocs (a);
5624
5625   vec_free (locator_set_name);
5626
5627   REPLY_MACRO (VL_API_LISP_ADD_DEL_MAP_REQUEST_ITR_RLOCS_REPLY);
5628 }
5629
5630 static void
5631   vl_api_lisp_add_del_remote_mapping_t_handler
5632   (vl_api_lisp_add_del_remote_mapping_t * mp)
5633 {
5634   locator_t *rlocs = 0;
5635   vl_api_lisp_add_del_remote_mapping_reply_t *rmp;
5636   int rv = 0;
5637   gid_address_t _eid, *eid = &_eid;
5638   u32 rloc_num = clib_net_to_host_u32 (mp->rloc_num);
5639
5640   memset (eid, 0, sizeof (eid[0]));
5641
5642   rv = unformat_lisp_eid_api (eid, clib_net_to_host_u32 (mp->vni),
5643                               mp->eid_type, mp->eid, mp->eid_len);
5644   if (rv)
5645     goto send_reply;
5646
5647   rlocs = unformat_lisp_locs (mp->rlocs, rloc_num);
5648
5649   if (!mp->is_add)
5650     {
5651       vnet_lisp_add_del_adjacency_args_t _a, *a = &_a;
5652       gid_address_copy (&a->reid, eid);
5653       a->is_add = 0;
5654       rv = vnet_lisp_add_del_adjacency (a);
5655       if (rv)
5656         {
5657           goto out;
5658         }
5659     }
5660
5661   /* NOTE: for now this works as a static remote mapping, i.e.,
5662    * not authoritative and ttl infinite. */
5663   rv = vnet_lisp_add_del_mapping (eid, rlocs, mp->action, 0, ~0,
5664                                   mp->is_add, 1 /* is_static */ , 0);
5665
5666   if (mp->del_all)
5667     vnet_lisp_clear_all_remote_adjacencies ();
5668
5669 out:
5670   vec_free (rlocs);
5671 send_reply:
5672   REPLY_MACRO (VL_API_LISP_ADD_DEL_REMOTE_MAPPING_REPLY);
5673 }
5674
5675 static void
5676 vl_api_lisp_add_del_adjacency_t_handler (vl_api_lisp_add_del_adjacency_t * mp)
5677 {
5678   vl_api_lisp_add_del_adjacency_reply_t *rmp;
5679   vnet_lisp_add_del_adjacency_args_t _a, *a = &_a;
5680
5681   int rv = 0;
5682   memset (a, 0, sizeof (a[0]));
5683
5684   rv = unformat_lisp_eid_api (&a->leid, clib_net_to_host_u32 (mp->vni),
5685                               mp->eid_type, mp->leid, mp->leid_len);
5686   rv |= unformat_lisp_eid_api (&a->reid, clib_net_to_host_u32 (mp->vni),
5687                                mp->eid_type, mp->reid, mp->reid_len);
5688
5689   if (rv)
5690     goto send_reply;
5691
5692   a->is_add = mp->is_add;
5693   rv = vnet_lisp_add_del_adjacency (a);
5694
5695 send_reply:
5696   REPLY_MACRO (VL_API_LISP_ADD_DEL_ADJACENCY_REPLY);
5697 }
5698
5699 static void
5700 send_lisp_locator_details (lisp_cp_main_t * lcm,
5701                            locator_t * loc,
5702                            unix_shared_memory_queue_t * q, u32 context)
5703 {
5704   vl_api_lisp_locator_details_t *rmp;
5705
5706   rmp = vl_msg_api_alloc (sizeof (*rmp));
5707   memset (rmp, 0, sizeof (*rmp));
5708   rmp->_vl_msg_id = ntohs (VL_API_LISP_LOCATOR_DETAILS);
5709   rmp->context = context;
5710
5711   rmp->local = loc->local;
5712   if (loc->local)
5713     {
5714       rmp->sw_if_index = ntohl (loc->sw_if_index);
5715     }
5716   else
5717     {
5718       rmp->is_ipv6 = gid_address_ip_version (&loc->address);
5719       ip_address_copy_addr (rmp->ip_address, &gid_address_ip (&loc->address));
5720     }
5721   rmp->priority = loc->priority;
5722   rmp->weight = loc->weight;
5723
5724   vl_msg_api_send_shmem (q, (u8 *) & rmp);
5725 }
5726
5727 static void
5728 vl_api_lisp_locator_dump_t_handler (vl_api_lisp_locator_dump_t * mp)
5729 {
5730   u8 *ls_name = 0;
5731   unix_shared_memory_queue_t *q = 0;
5732   lisp_cp_main_t *lcm = vnet_lisp_cp_get_main ();
5733   locator_set_t *lsit = 0;
5734   locator_t *loc = 0;
5735   u32 ls_index = ~0, *locit = 0;
5736   uword *p = 0;
5737
5738   q = vl_api_client_index_to_input_queue (mp->client_index);
5739   if (q == 0)
5740     {
5741       return;
5742     }
5743
5744   if (mp->is_index_set)
5745     ls_index = htonl (mp->ls_index);
5746   else
5747     {
5748       /* make sure we get a proper C-string */
5749       mp->ls_name[sizeof (mp->ls_name) - 1] = 0;
5750       ls_name = format (0, "%s", mp->ls_name);
5751       p = hash_get_mem (lcm->locator_set_index_by_name, ls_name);
5752       if (!p)
5753         goto out;
5754       ls_index = p[0];
5755     }
5756
5757   if (pool_is_free_index (lcm->locator_set_pool, ls_index))
5758     return;
5759
5760   lsit = pool_elt_at_index (lcm->locator_set_pool, ls_index);
5761
5762   vec_foreach (locit, lsit->locator_indices)
5763   {
5764     loc = pool_elt_at_index (lcm->locator_pool, locit[0]);
5765     send_lisp_locator_details (lcm, loc, q, mp->context);
5766   };
5767 out:
5768   vec_free (ls_name);
5769 }
5770
5771 static void
5772 send_lisp_locator_set_details (lisp_cp_main_t * lcm,
5773                                locator_set_t * lsit,
5774                                unix_shared_memory_queue_t * q,
5775                                u32 context, u32 ls_index)
5776 {
5777   vl_api_lisp_locator_set_details_t *rmp;
5778   u8 *str = 0;
5779
5780   rmp = vl_msg_api_alloc (sizeof (*rmp));
5781   memset (rmp, 0, sizeof (*rmp));
5782   rmp->_vl_msg_id = ntohs (VL_API_LISP_LOCATOR_SET_DETAILS);
5783   rmp->context = context;
5784
5785   rmp->ls_index = htonl (ls_index);
5786   if (lsit->local)
5787     {
5788       ASSERT (lsit->name != NULL);
5789       strncpy ((char *) rmp->ls_name, (char *) lsit->name,
5790                vec_len (lsit->name));
5791     }
5792   else
5793     {
5794       str = format (0, "<remote-%d>", ls_index);
5795       strncpy ((char *) rmp->ls_name, (char *) str, vec_len (str));
5796       vec_free (str);
5797     }
5798
5799   vl_msg_api_send_shmem (q, (u8 *) & rmp);
5800 }
5801
5802 static void
5803 vl_api_lisp_locator_set_dump_t_handler (vl_api_lisp_locator_set_dump_t * mp)
5804 {
5805   unix_shared_memory_queue_t *q = NULL;
5806   lisp_cp_main_t *lcm = vnet_lisp_cp_get_main ();
5807   locator_set_t *lsit = NULL;
5808   u8 filter;
5809
5810   q = vl_api_client_index_to_input_queue (mp->client_index);
5811   if (q == 0)
5812     {
5813       return;
5814     }
5815
5816   filter = mp->filter;
5817   /* *INDENT-OFF* */
5818   pool_foreach (lsit, lcm->locator_set_pool,
5819   ({
5820     if (filter && !((1 == filter && lsit->local) ||
5821                     (2 == filter && !lsit->local)))
5822       {
5823         continue;
5824       }
5825     send_lisp_locator_set_details (lcm, lsit, q, mp->context,
5826                                    lsit - lcm->locator_set_pool);
5827   }));
5828   /* *INDENT-ON* */
5829 }
5830
5831 static void
5832 lisp_fid_put_api (u8 * dst, fid_address_t * src, u8 * prefix_length)
5833 {
5834   ASSERT (prefix_length);
5835   ip_prefix_t *ippref = &fid_addr_ippref (src);
5836
5837   switch (fid_addr_type (src))
5838     {
5839     case FID_ADDR_IP_PREF:
5840       if (ip_prefix_version (ippref) == IP4)
5841         clib_memcpy (dst, &ip_prefix_v4 (ippref), 4);
5842       else
5843         clib_memcpy (dst, &ip_prefix_v6 (ippref), 16);
5844       prefix_length[0] = ip_prefix_len (ippref);
5845       break;
5846
5847     case FID_ADDR_MAC:
5848       prefix_length[0] = 0;
5849       clib_memcpy (dst, fid_addr_mac (src), 6);
5850       break;
5851
5852     default:
5853       clib_warning ("Unknown FID type %d!", fid_addr_type (src));
5854       break;
5855     }
5856 }
5857
5858 static u8
5859 fid_type_to_api_type (fid_address_t * fid)
5860 {
5861   ip_prefix_t *ippref;
5862
5863   switch (fid_addr_type (fid))
5864     {
5865     case FID_ADDR_IP_PREF:
5866       ippref = &fid_addr_ippref (fid);
5867       if (ip_prefix_version (ippref) == IP4)
5868         return 0;
5869       else if (ip_prefix_version (ippref) == IP6)
5870         return 1;
5871       else
5872         return ~0;
5873
5874     case FID_ADDR_MAC:
5875       return 2;
5876     }
5877
5878   return ~0;
5879 }
5880
5881 static void
5882 send_lisp_eid_table_details (mapping_t * mapit,
5883                              unix_shared_memory_queue_t * q,
5884                              u32 context, u8 filter)
5885 {
5886   fid_address_t *fid;
5887   lisp_cp_main_t *lcm = vnet_lisp_cp_get_main ();
5888   locator_set_t *ls = 0;
5889   vl_api_lisp_eid_table_details_t *rmp = NULL;
5890   gid_address_t *gid = NULL;
5891   u8 *mac = 0;
5892   ip_prefix_t *ip_prefix = NULL;
5893
5894   switch (filter)
5895     {
5896     case 0:                     /* all mappings */
5897       break;
5898
5899     case 1:                     /* local only */
5900       if (!mapit->local)
5901         return;
5902       break;
5903     case 2:                     /* remote only */
5904       if (mapit->local)
5905         return;
5906       break;
5907     default:
5908       clib_warning ("Filter error, unknown filter: %d", filter);
5909       return;
5910     }
5911
5912   gid = &mapit->eid;
5913   ip_prefix = &gid_address_ippref (gid);
5914   mac = gid_address_mac (gid);
5915
5916   rmp = vl_msg_api_alloc (sizeof (*rmp));
5917   memset (rmp, 0, sizeof (*rmp));
5918   rmp->_vl_msg_id = ntohs (VL_API_LISP_EID_TABLE_DETAILS);
5919
5920   ls = pool_elt_at_index (lcm->locator_set_pool, mapit->locator_set_index);
5921   if (vec_len (ls->locator_indices) == 0)
5922     rmp->locator_set_index = ~0;
5923   else
5924     rmp->locator_set_index = clib_host_to_net_u32 (mapit->locator_set_index);
5925
5926   rmp->is_local = mapit->local;
5927   rmp->ttl = clib_host_to_net_u32 (mapit->ttl);
5928   rmp->action = mapit->action;
5929   rmp->authoritative = mapit->authoritative;
5930
5931   switch (gid_address_type (gid))
5932     {
5933     case GID_ADDR_SRC_DST:
5934       rmp->is_src_dst = 1;
5935       fid = &gid_address_sd_src (gid);
5936       rmp->eid_type = fid_type_to_api_type (fid);
5937       lisp_fid_put_api (rmp->seid, &gid_address_sd_src (gid),
5938                         &rmp->seid_prefix_len);
5939       lisp_fid_put_api (rmp->eid, &gid_address_sd_dst (gid),
5940                         &rmp->eid_prefix_len);
5941       break;
5942     case GID_ADDR_IP_PREFIX:
5943       rmp->eid_prefix_len = ip_prefix_len (ip_prefix);
5944       if (ip_prefix_version (ip_prefix) == IP4)
5945         {
5946           rmp->eid_type = 0;    /* ipv4 type */
5947           clib_memcpy (rmp->eid, &ip_prefix_v4 (ip_prefix),
5948                        sizeof (ip_prefix_v4 (ip_prefix)));
5949         }
5950       else
5951         {
5952           rmp->eid_type = 1;    /* ipv6 type */
5953           clib_memcpy (rmp->eid, &ip_prefix_v6 (ip_prefix),
5954                        sizeof (ip_prefix_v6 (ip_prefix)));
5955         }
5956       break;
5957     case GID_ADDR_MAC:
5958       rmp->eid_type = 2;        /* l2 mac type */
5959       clib_memcpy (rmp->eid, mac, 6);
5960       break;
5961     default:
5962       ASSERT (0);
5963     }
5964   rmp->context = context;
5965   rmp->vni = clib_host_to_net_u32 (gid_address_vni (gid));
5966   vl_msg_api_send_shmem (q, (u8 *) & rmp);
5967 }
5968
5969 static void
5970 vl_api_lisp_eid_table_dump_t_handler (vl_api_lisp_eid_table_dump_t * mp)
5971 {
5972   u32 mi;
5973   unix_shared_memory_queue_t *q = NULL;
5974   lisp_cp_main_t *lcm = vnet_lisp_cp_get_main ();
5975   mapping_t *mapit = NULL;
5976   gid_address_t _eid, *eid = &_eid;
5977
5978   q = vl_api_client_index_to_input_queue (mp->client_index);
5979   if (q == 0)
5980     {
5981       return;
5982     }
5983
5984   if (mp->eid_set)
5985     {
5986       memset (eid, 0, sizeof (*eid));
5987
5988       unformat_lisp_eid_api (eid, clib_net_to_host_u32 (mp->vni),
5989                              mp->eid_type, mp->eid, mp->prefix_length);
5990
5991       mi = gid_dictionary_lookup (&lcm->mapping_index_by_gid, eid);
5992       if ((u32) ~ 0 == mi)
5993         return;
5994
5995       mapit = pool_elt_at_index (lcm->mapping_pool, mi);
5996       send_lisp_eid_table_details (mapit, q, mp->context,
5997                                    0 /* ignore filter */ );
5998     }
5999   else
6000     {
6001       /* *INDENT-OFF* */
6002       pool_foreach (mapit, lcm->mapping_pool,
6003       ({
6004         send_lisp_eid_table_details(mapit, q, mp->context,
6005                                     mp->filter);
6006       }));
6007       /* *INDENT-ON* */
6008     }
6009 }
6010
6011 static void
6012 send_lisp_gpe_fwd_entry_details (lisp_gpe_fwd_entry_t * lfe,
6013                                  unix_shared_memory_queue_t * q, u32 context)
6014 {
6015   vl_api_lisp_gpe_tunnel_details_t *rmp;
6016   lisp_gpe_main_t *lgm = &lisp_gpe_main;
6017
6018   rmp = vl_msg_api_alloc (sizeof (*rmp));
6019   memset (rmp, 0, sizeof (*rmp));
6020   rmp->_vl_msg_id = ntohs (VL_API_LISP_GPE_TUNNEL_DETAILS);
6021
6022   rmp->tunnels = lfe - lgm->lisp_fwd_entry_pool;
6023
6024   rmp->is_ipv6 = ip_prefix_version (&(lfe->key->rmt.ippref)) == IP6 ? 1 : 0;
6025   ip_address_copy_addr (rmp->source_ip,
6026                         &ip_prefix_addr (&(lfe->key->rmt.ippref)));
6027   ip_address_copy_addr (rmp->destination_ip,
6028                         &ip_prefix_addr (&(lfe->key->rmt.ippref)));
6029
6030   rmp->encap_fib_id = htonl (0);
6031   rmp->decap_fib_id = htonl (lfe->eid_fib_index);
6032   rmp->iid = htonl (lfe->key->vni);
6033   rmp->context = context;
6034
6035   vl_msg_api_send_shmem (q, (u8 *) & rmp);
6036 }
6037
6038 static void
6039 vl_api_lisp_gpe_tunnel_dump_t_handler (vl_api_lisp_gpe_tunnel_dump_t * mp)
6040 {
6041   unix_shared_memory_queue_t *q = NULL;
6042   lisp_gpe_main_t *lgm = &lisp_gpe_main;
6043   lisp_gpe_fwd_entry_t *lfe = NULL;
6044
6045   if (pool_elts (lgm->lisp_fwd_entry_pool) == 0)
6046     {
6047       return;
6048     }
6049
6050   q = vl_api_client_index_to_input_queue (mp->client_index);
6051   if (q == 0)
6052     {
6053       return;
6054     }
6055
6056   /* *INDENT-OFF* */
6057   pool_foreach(lfe, lgm->lisp_fwd_entry_pool,
6058   ({
6059     send_lisp_gpe_fwd_entry_details(lfe, q, mp->context);
6060   }));
6061   /* *INDENT-ON* */
6062 }
6063
6064 static void
6065 send_lisp_map_resolver_details (ip_address_t * ip,
6066                                 unix_shared_memory_queue_t * q, u32 context)
6067 {
6068   vl_api_lisp_map_resolver_details_t *rmp = NULL;
6069
6070   rmp = vl_msg_api_alloc (sizeof (*rmp));
6071   memset (rmp, 0, sizeof (*rmp));
6072   rmp->_vl_msg_id = ntohs (VL_API_LISP_MAP_RESOLVER_DETAILS);
6073
6074   switch (ip_addr_version (ip))
6075     {
6076     case IP4:
6077       rmp->is_ipv6 = 0;
6078       clib_memcpy (rmp->ip_address, &ip_addr_v4 (ip),
6079                    sizeof (ip_addr_v4 (ip)));
6080       break;
6081
6082     case IP6:
6083       rmp->is_ipv6 = 1;
6084       clib_memcpy (rmp->ip_address, &ip_addr_v6 (ip),
6085                    sizeof (ip_addr_v6 (ip)));
6086       break;
6087
6088     default:
6089       ASSERT (0);
6090     }
6091   rmp->context = context;
6092
6093   vl_msg_api_send_shmem (q, (u8 *) & rmp);
6094 }
6095
6096 static void
6097 vl_api_lisp_map_resolver_dump_t_handler (vl_api_lisp_map_resolver_dump_t * mp)
6098 {
6099   unix_shared_memory_queue_t *q = NULL;
6100   lisp_cp_main_t *lcm = vnet_lisp_cp_get_main ();
6101   map_resolver_t *mr;
6102
6103   q = vl_api_client_index_to_input_queue (mp->client_index);
6104   if (q == 0)
6105     {
6106       return;
6107     }
6108
6109   vec_foreach (mr, lcm->map_resolvers)
6110   {
6111     send_lisp_map_resolver_details (&mr->address, q, mp->context);
6112   }
6113 }
6114
6115 static void
6116 send_eid_table_map_pair (hash_pair_t * p,
6117                          unix_shared_memory_queue_t * q, u32 context)
6118 {
6119   vl_api_lisp_eid_table_map_details_t *rmp = NULL;
6120
6121   rmp = vl_msg_api_alloc (sizeof (*rmp));
6122   memset (rmp, 0, sizeof (*rmp));
6123   rmp->_vl_msg_id = ntohs (VL_API_LISP_EID_TABLE_MAP_DETAILS);
6124
6125   rmp->vni = clib_host_to_net_u32 (p->key);
6126   rmp->dp_table = clib_host_to_net_u32 (p->value[0]);
6127   rmp->context = context;
6128   vl_msg_api_send_shmem (q, (u8 *) & rmp);
6129 }
6130
6131 static void
6132 vl_api_lisp_eid_table_map_dump_t_handler (vl_api_lisp_eid_table_map_dump_t *
6133                                           mp)
6134 {
6135   unix_shared_memory_queue_t *q = NULL;
6136   lisp_cp_main_t *lcm = vnet_lisp_cp_get_main ();
6137   hash_pair_t *p;
6138   uword *vni_table = 0;
6139
6140   q = vl_api_client_index_to_input_queue (mp->client_index);
6141   if (q == 0)
6142     {
6143       return;
6144     }
6145
6146   if (mp->is_l2)
6147     {
6148       vni_table = lcm->bd_id_by_vni;
6149     }
6150   else
6151     {
6152       vni_table = lcm->table_id_by_vni;
6153     }
6154
6155   /* *INDENT-OFF* */
6156   hash_foreach_pair (p, vni_table,
6157   ({
6158     send_eid_table_map_pair (p, q, mp->context);
6159   }));
6160   /* *INDENT-ON* */
6161 }
6162
6163 static void
6164 send_eid_table_vni (u32 vni, unix_shared_memory_queue_t * q, u32 context)
6165 {
6166   vl_api_lisp_eid_table_vni_details_t *rmp = 0;
6167
6168   rmp = vl_msg_api_alloc (sizeof (*rmp));
6169   memset (rmp, 0, sizeof (*rmp));
6170   rmp->_vl_msg_id = ntohs (VL_API_LISP_EID_TABLE_VNI_DETAILS);
6171   rmp->context = context;
6172   rmp->vni = clib_host_to_net_u32 (vni);
6173   vl_msg_api_send_shmem (q, (u8 *) & rmp);
6174 }
6175
6176 static void
6177 lisp_adjacency_copy (vl_api_lisp_adjacency_t * dst, lisp_adjacency_t * adjs)
6178 {
6179   lisp_adjacency_t *adj;
6180   vl_api_lisp_adjacency_t a;
6181   u32 i, n = vec_len (adjs);
6182
6183   for (i = 0; i < n; i++)
6184     {
6185       adj = vec_elt_at_index (adjs, i);
6186       memset (&a, 0, sizeof (a));
6187
6188       switch (gid_address_type (&adj->reid))
6189         {
6190         case GID_ADDR_IP_PREFIX:
6191           a.reid_prefix_len = gid_address_ippref_len (&adj->reid);
6192           a.leid_prefix_len = gid_address_ippref_len (&adj->leid);
6193           if (gid_address_ip_version (&adj->reid) == IP4)
6194             {
6195               a.eid_type = 0;   /* ipv4 type */
6196               clib_memcpy (a.reid, &gid_address_ip (&adj->reid), 4);
6197               clib_memcpy (a.leid, &gid_address_ip (&adj->leid), 4);
6198             }
6199           else
6200             {
6201               a.eid_type = 1;   /* ipv6 type */
6202               clib_memcpy (a.reid, &gid_address_ip (&adj->reid), 16);
6203               clib_memcpy (a.leid, &gid_address_ip (&adj->leid), 16);
6204             }
6205           break;
6206         case GID_ADDR_MAC:
6207           a.eid_type = 2;       /* l2 mac type */
6208           mac_copy (a.reid, gid_address_mac (&adj->reid));
6209           mac_copy (a.leid, gid_address_mac (&adj->leid));
6210           break;
6211         default:
6212           ASSERT (0);
6213         }
6214       dst[i] = a;
6215     }
6216 }
6217
6218 static void
6219 vl_api_lisp_adjacencies_get_t_handler (vl_api_lisp_adjacencies_get_t * mp)
6220 {
6221   vl_api_lisp_adjacencies_get_reply_t *rmp = 0;
6222   lisp_adjacency_t *adjs = 0;
6223   int rv = 0;
6224   vl_api_lisp_adjacency_t a;
6225   u32 size = ~0;
6226   u32 vni = clib_net_to_host_u32 (mp->vni);
6227
6228   adjs = vnet_lisp_adjacencies_get_by_vni (vni);
6229   size = vec_len (adjs) * sizeof (a);
6230
6231   /* *INDENT-OFF* */
6232   REPLY_MACRO4 (VL_API_LISP_ADJACENCIES_GET_REPLY, size,
6233   {
6234     rmp->count = clib_host_to_net_u32 (vec_len (adjs));
6235     lisp_adjacency_copy (rmp->adjacencies, adjs);
6236   });
6237   /* *INDENT-ON* */
6238
6239   vec_free (adjs);
6240 }
6241
6242 static void
6243 vl_api_lisp_eid_table_vni_dump_t_handler (vl_api_lisp_eid_table_vni_dump_t *
6244                                           mp)
6245 {
6246   hash_pair_t *p;
6247   u32 *vnis = 0;
6248   unix_shared_memory_queue_t *q = 0;
6249   lisp_cp_main_t *lcm = vnet_lisp_cp_get_main ();
6250
6251   q = vl_api_client_index_to_input_queue (mp->client_index);
6252   if (q == 0)
6253     {
6254       return;
6255     }
6256
6257   /* *INDENT-OFF* */
6258   hash_foreach_pair (p, lcm->table_id_by_vni,
6259   ({
6260     hash_set (vnis, p->key, 0);
6261   }));
6262
6263   hash_foreach_pair (p, lcm->bd_id_by_vni,
6264   ({
6265     hash_set (vnis, p->key, 0);
6266   }));
6267
6268   hash_foreach_pair (p, vnis,
6269   ({
6270     send_eid_table_vni (p->key, q, mp->context);
6271   }));
6272   /* *INDENT-ON* */
6273
6274   hash_free (vnis);
6275 }
6276
6277 static void
6278 vl_api_show_lisp_status_t_handler (vl_api_show_lisp_status_t * mp)
6279 {
6280   unix_shared_memory_queue_t *q = NULL;
6281   vl_api_show_lisp_status_reply_t *rmp = NULL;
6282   int rv = 0;
6283
6284   q = vl_api_client_index_to_input_queue (mp->client_index);
6285   if (q == 0)
6286     {
6287       return;
6288     }
6289
6290   /* *INDENT-OFF* */
6291   REPLY_MACRO2(VL_API_SHOW_LISP_STATUS_REPLY,
6292   ({
6293     rmp->gpe_status = vnet_lisp_gpe_enable_disable_status ();
6294     rmp->feature_status = vnet_lisp_enable_disable_status ();
6295   }));
6296   /* *INDENT-ON* */
6297 }
6298
6299 static void
6300   vl_api_lisp_get_map_request_itr_rlocs_t_handler
6301   (vl_api_lisp_get_map_request_itr_rlocs_t * mp)
6302 {
6303   unix_shared_memory_queue_t *q = NULL;
6304   vl_api_lisp_get_map_request_itr_rlocs_reply_t *rmp = NULL;
6305   lisp_cp_main_t *lcm = vnet_lisp_cp_get_main ();
6306   locator_set_t *loc_set = 0;
6307   u8 *tmp_str = 0;
6308   int rv = 0;
6309
6310   q = vl_api_client_index_to_input_queue (mp->client_index);
6311   if (q == 0)
6312     {
6313       return;
6314     }
6315
6316   if (~0 == lcm->mreq_itr_rlocs)
6317     {
6318       tmp_str = format (0, " ");
6319     }
6320   else
6321     {
6322       loc_set =
6323         pool_elt_at_index (lcm->locator_set_pool, lcm->mreq_itr_rlocs);
6324       tmp_str = format (0, "%s", loc_set->name);
6325     }
6326
6327   /* *INDENT-OFF* */
6328   REPLY_MACRO2(VL_API_LISP_GET_MAP_REQUEST_ITR_RLOCS_REPLY,
6329   ({
6330     strncpy((char *) rmp->locator_set_name, (char *) tmp_str,
6331             ARRAY_LEN(rmp->locator_set_name) - 1);
6332   }));
6333   /* *INDENT-ON* */
6334
6335   vec_free (tmp_str);
6336 }
6337
6338 static void
6339 vl_api_show_lisp_pitr_t_handler (vl_api_show_lisp_pitr_t * mp)
6340 {
6341   unix_shared_memory_queue_t *q = NULL;
6342   vl_api_show_lisp_pitr_reply_t *rmp = NULL;
6343   lisp_cp_main_t *lcm = vnet_lisp_cp_get_main ();
6344   mapping_t *m;
6345   locator_set_t *ls = 0;
6346   u8 *tmp_str = 0;
6347   int rv = 0;
6348
6349   q = vl_api_client_index_to_input_queue (mp->client_index);
6350   if (q == 0)
6351     {
6352       return;
6353     }
6354
6355   if (!lcm->lisp_pitr)
6356     {
6357       tmp_str = format (0, "N/A");
6358     }
6359   else
6360     {
6361       m = pool_elt_at_index (lcm->mapping_pool, lcm->pitr_map_index);
6362       if (~0 != m->locator_set_index)
6363         {
6364           ls =
6365             pool_elt_at_index (lcm->locator_set_pool, m->locator_set_index);
6366           tmp_str = format (0, "%s", ls->name);
6367         }
6368       else
6369         {
6370           tmp_str = format (0, "N/A");
6371         }
6372     }
6373   vec_add1 (tmp_str, 0);
6374
6375   /* *INDENT-OFF* */
6376   REPLY_MACRO2(VL_API_SHOW_LISP_PITR_REPLY,
6377   ({
6378     rmp->status = lcm->lisp_pitr;
6379     strncpy((char *) rmp->locator_set_name, (char *) tmp_str,
6380             ARRAY_LEN(rmp->locator_set_name) - 1);
6381   }));
6382   /* *INDENT-ON* */
6383 }
6384
6385 static void
6386 vl_api_interface_name_renumber_t_handler (vl_api_interface_name_renumber_t *
6387                                           mp)
6388 {
6389   vl_api_interface_name_renumber_reply_t *rmp;
6390   int rv = 0;
6391
6392   VALIDATE_SW_IF_INDEX (mp);
6393
6394   rv = vnet_interface_name_renumber
6395     (ntohl (mp->sw_if_index), ntohl (mp->new_show_dev_instance));
6396
6397   BAD_SW_IF_INDEX_LABEL;
6398
6399   REPLY_MACRO (VL_API_INTERFACE_NAME_RENUMBER_REPLY);
6400 }
6401
6402 static int
6403 arp_change_data_callback (u32 pool_index, u8 * new_mac,
6404                           u32 sw_if_index, u32 address)
6405 {
6406   vpe_api_main_t *am = &vpe_api_main;
6407   vlib_main_t *vm = am->vlib_main;
6408   vl_api_ip4_arp_event_t *event;
6409   static f64 arp_event_last_time;
6410   f64 now = vlib_time_now (vm);
6411
6412   if (pool_is_free_index (am->arp_events, pool_index))
6413     return 1;
6414
6415   event = pool_elt_at_index (am->arp_events, pool_index);
6416   /* *INDENT-OFF* */
6417   if (memcmp (&event->new_mac, new_mac, sizeof (event->new_mac)))
6418     {
6419       clib_memcpy (event->new_mac, new_mac, sizeof (event->new_mac));
6420     }
6421   else
6422     {                           /* same mac */
6423       if (sw_if_index == event->sw_if_index &&
6424           (!event->mac_ip ||
6425            /* for BD case, also check IP address with 10 sec timeout */
6426            (address == event->address &&
6427             (now - arp_event_last_time) < 10.0)))
6428         return 1;
6429     }
6430   /* *INDENT-ON* */
6431
6432   arp_event_last_time = now;
6433   event->sw_if_index = sw_if_index;
6434   if (event->mac_ip)
6435     event->address = address;
6436   return 0;
6437 }
6438
6439 static int
6440 nd_change_data_callback (u32 pool_index, u8 * new_mac,
6441                          u32 sw_if_index, ip6_address_t * address)
6442 {
6443   vpe_api_main_t *am = &vpe_api_main;
6444   vlib_main_t *vm = am->vlib_main;
6445   vl_api_ip6_nd_event_t *event;
6446   static f64 nd_event_last_time;
6447   f64 now = vlib_time_now (vm);
6448
6449   if (pool_is_free_index (am->nd_events, pool_index))
6450     return 1;
6451
6452   event = pool_elt_at_index (am->nd_events, pool_index);
6453
6454   /* *INDENT-OFF* */
6455   if (memcmp (&event->new_mac, new_mac, sizeof (event->new_mac)))
6456     {
6457       clib_memcpy (event->new_mac, new_mac, sizeof (event->new_mac));
6458     }
6459   else
6460     {                           /* same mac */
6461       if (sw_if_index == event->sw_if_index &&
6462           (!event->mac_ip ||
6463            /* for BD case, also check IP address with 10 sec timeout */
6464            (ip6_address_is_equal (address,
6465                                   (ip6_address_t *) event->address) &&
6466             (now - nd_event_last_time) < 10.0)))
6467         return 1;
6468     }
6469   /* *INDENT-ON* */
6470
6471   nd_event_last_time = now;
6472   event->sw_if_index = sw_if_index;
6473   if (event->mac_ip)
6474     clib_memcpy (event->address, address, sizeof (event->address));
6475   return 0;
6476 }
6477
6478 static int
6479 arp_change_delete_callback (u32 pool_index, u8 * notused)
6480 {
6481   vpe_api_main_t *am = &vpe_api_main;
6482
6483   if (pool_is_free_index (am->arp_events, pool_index))
6484     return 1;
6485
6486   pool_put_index (am->arp_events, pool_index);
6487   return 0;
6488 }
6489
6490 static int
6491 nd_change_delete_callback (u32 pool_index, u8 * notused)
6492 {
6493   vpe_api_main_t *am = &vpe_api_main;
6494
6495   if (pool_is_free_index (am->nd_events, pool_index))
6496     return 1;
6497
6498   pool_put_index (am->nd_events, pool_index);
6499   return 0;
6500 }
6501
6502 static void
6503 vl_api_want_ip4_arp_events_t_handler (vl_api_want_ip4_arp_events_t * mp)
6504 {
6505   vpe_api_main_t *am = &vpe_api_main;
6506   vnet_main_t *vnm = vnet_get_main ();
6507   vl_api_want_ip4_arp_events_reply_t *rmp;
6508   vl_api_ip4_arp_event_t *event;
6509   int rv;
6510
6511   if (mp->enable_disable)
6512     {
6513       pool_get (am->arp_events, event);
6514       memset (event, 0, sizeof (*event));
6515
6516       event->_vl_msg_id = ntohs (VL_API_IP4_ARP_EVENT);
6517       event->client_index = mp->client_index;
6518       event->context = mp->context;
6519       event->address = mp->address;
6520       event->pid = mp->pid;
6521       if (mp->address == 0)
6522         event->mac_ip = 1;
6523
6524       rv = vnet_add_del_ip4_arp_change_event
6525         (vnm, arp_change_data_callback,
6526          mp->pid, &mp->address /* addr, in net byte order */ ,
6527          vpe_resolver_process_node.index,
6528          IP4_ARP_EVENT, event - am->arp_events, 1 /* is_add */ );
6529     }
6530   else
6531     {
6532       rv = vnet_add_del_ip4_arp_change_event
6533         (vnm, arp_change_delete_callback,
6534          mp->pid, &mp->address /* addr, in net byte order */ ,
6535          vpe_resolver_process_node.index,
6536          IP4_ARP_EVENT, ~0 /* pool index */ , 0 /* is_add */ );
6537     }
6538   REPLY_MACRO (VL_API_WANT_IP4_ARP_EVENTS_REPLY);
6539 }
6540
6541 static void
6542 vl_api_want_ip6_nd_events_t_handler (vl_api_want_ip6_nd_events_t * mp)
6543 {
6544   vpe_api_main_t *am = &vpe_api_main;
6545   vnet_main_t *vnm = vnet_get_main ();
6546   vl_api_want_ip6_nd_events_reply_t *rmp;
6547   vl_api_ip6_nd_event_t *event;
6548   int rv;
6549
6550   if (mp->enable_disable)
6551     {
6552       pool_get (am->nd_events, event);
6553       memset (event, 0, sizeof (*event));
6554
6555       event->_vl_msg_id = ntohs (VL_API_IP6_ND_EVENT);
6556       event->client_index = mp->client_index;
6557       event->context = mp->context;
6558       clib_memcpy (event->address, mp->address, 16);
6559       event->pid = mp->pid;
6560       if (ip6_address_is_zero ((ip6_address_t *) mp->address))
6561         event->mac_ip = 1;
6562
6563       rv = vnet_add_del_ip6_nd_change_event
6564         (vnm, nd_change_data_callback,
6565          mp->pid, mp->address /* addr, in net byte order */ ,
6566          vpe_resolver_process_node.index,
6567          IP6_ND_EVENT, event - am->nd_events, 1 /* is_add */ );
6568     }
6569   else
6570     {
6571       rv = vnet_add_del_ip6_nd_change_event
6572         (vnm, nd_change_delete_callback,
6573          mp->pid, mp->address /* addr, in net byte order */ ,
6574          vpe_resolver_process_node.index,
6575          IP6_ND_EVENT, ~0 /* pool index */ , 0 /* is_add */ );
6576     }
6577   REPLY_MACRO (VL_API_WANT_IP6_ND_EVENTS_REPLY);
6578 }
6579
6580 static void vl_api_input_acl_set_interface_t_handler
6581   (vl_api_input_acl_set_interface_t * mp)
6582 {
6583   vlib_main_t *vm = vlib_get_main ();
6584   vl_api_input_acl_set_interface_reply_t *rmp;
6585   int rv;
6586   u32 sw_if_index, ip4_table_index, ip6_table_index, l2_table_index;
6587
6588   ip4_table_index = ntohl (mp->ip4_table_index);
6589   ip6_table_index = ntohl (mp->ip6_table_index);
6590   l2_table_index = ntohl (mp->l2_table_index);
6591   sw_if_index = ntohl (mp->sw_if_index);
6592
6593   VALIDATE_SW_IF_INDEX (mp);
6594
6595   rv = vnet_set_input_acl_intfc (vm, sw_if_index, ip4_table_index,
6596                                  ip6_table_index, l2_table_index, mp->is_add);
6597
6598   BAD_SW_IF_INDEX_LABEL;
6599
6600   REPLY_MACRO (VL_API_INPUT_ACL_SET_INTERFACE_REPLY);
6601 }
6602
6603 static void vl_api_ipsec_spd_add_del_t_handler
6604   (vl_api_ipsec_spd_add_del_t * mp)
6605 {
6606 #if IPSEC == 0
6607   clib_warning ("unimplemented");
6608 #else
6609
6610   vlib_main_t *vm __attribute__ ((unused)) = vlib_get_main ();
6611   vl_api_ipsec_spd_add_del_reply_t *rmp;
6612   int rv;
6613
6614 #if DPDK > 0
6615   rv = ipsec_add_del_spd (vm, ntohl (mp->spd_id), mp->is_add);
6616 #else
6617   rv = VNET_API_ERROR_UNIMPLEMENTED;
6618 #endif
6619
6620   REPLY_MACRO (VL_API_IPSEC_SPD_ADD_DEL_REPLY);
6621 #endif
6622 }
6623
6624 static void vl_api_ipsec_interface_add_del_spd_t_handler
6625   (vl_api_ipsec_interface_add_del_spd_t * mp)
6626 {
6627   vlib_main_t *vm __attribute__ ((unused)) = vlib_get_main ();
6628   vl_api_ipsec_interface_add_del_spd_reply_t *rmp;
6629   int rv;
6630   u32 sw_if_index __attribute__ ((unused));
6631   u32 spd_id __attribute__ ((unused));
6632
6633   sw_if_index = ntohl (mp->sw_if_index);
6634   spd_id = ntohl (mp->spd_id);
6635
6636   VALIDATE_SW_IF_INDEX (mp);
6637
6638 #if IPSEC > 0
6639   rv = ipsec_set_interface_spd (vm, sw_if_index, spd_id, mp->is_add);
6640 #else
6641   rv = VNET_API_ERROR_UNIMPLEMENTED;
6642 #endif
6643
6644   BAD_SW_IF_INDEX_LABEL;
6645
6646   REPLY_MACRO (VL_API_IPSEC_INTERFACE_ADD_DEL_SPD_REPLY);
6647 }
6648
6649 static void vl_api_ipsec_spd_add_del_entry_t_handler
6650   (vl_api_ipsec_spd_add_del_entry_t * mp)
6651 {
6652   vlib_main_t *vm __attribute__ ((unused)) = vlib_get_main ();
6653   vl_api_ipsec_spd_add_del_entry_reply_t *rmp;
6654   int rv;
6655
6656 #if IPSEC > 0
6657   ipsec_policy_t p;
6658
6659   memset (&p, 0, sizeof (p));
6660
6661   p.id = ntohl (mp->spd_id);
6662   p.priority = ntohl (mp->priority);
6663   p.is_outbound = mp->is_outbound;
6664   p.is_ipv6 = mp->is_ipv6;
6665
6666   if (mp->is_ipv6 || mp->is_ip_any)
6667     {
6668       clib_memcpy (&p.raddr.start, mp->remote_address_start, 16);
6669       clib_memcpy (&p.raddr.stop, mp->remote_address_stop, 16);
6670       clib_memcpy (&p.laddr.start, mp->local_address_start, 16);
6671       clib_memcpy (&p.laddr.stop, mp->local_address_stop, 16);
6672     }
6673   else
6674     {
6675       clib_memcpy (&p.raddr.start.ip4.data, mp->remote_address_start, 4);
6676       clib_memcpy (&p.raddr.stop.ip4.data, mp->remote_address_stop, 4);
6677       clib_memcpy (&p.laddr.start.ip4.data, mp->local_address_start, 4);
6678       clib_memcpy (&p.laddr.stop.ip4.data, mp->local_address_stop, 4);
6679     }
6680   p.protocol = mp->protocol;
6681   p.rport.start = ntohs (mp->remote_port_start);
6682   p.rport.stop = ntohs (mp->remote_port_stop);
6683   p.lport.start = ntohs (mp->local_port_start);
6684   p.lport.stop = ntohs (mp->local_port_stop);
6685   /* policy action resolve unsupported */
6686   if (mp->policy == IPSEC_POLICY_ACTION_RESOLVE)
6687     {
6688       clib_warning ("unsupported action: 'resolve'");
6689       rv = VNET_API_ERROR_UNIMPLEMENTED;
6690       goto out;
6691     }
6692   p.policy = mp->policy;
6693   p.sa_id = ntohl (mp->sa_id);
6694
6695   rv = ipsec_add_del_policy (vm, &p, mp->is_add);
6696   if (rv)
6697     goto out;
6698
6699   if (mp->is_ip_any)
6700     {
6701       p.is_ipv6 = 1;
6702       rv = ipsec_add_del_policy (vm, &p, mp->is_add);
6703     }
6704 #else
6705   rv = VNET_API_ERROR_UNIMPLEMENTED;
6706   goto out;
6707 #endif
6708
6709 out:
6710   REPLY_MACRO (VL_API_IPSEC_SPD_ADD_DEL_ENTRY_REPLY);
6711 }
6712
6713 static void vl_api_ipsec_sad_add_del_entry_t_handler
6714   (vl_api_ipsec_sad_add_del_entry_t * mp)
6715 {
6716   vlib_main_t *vm __attribute__ ((unused)) = vlib_get_main ();
6717   vl_api_ipsec_sad_add_del_entry_reply_t *rmp;
6718   int rv;
6719 #if IPSEC > 0
6720   ipsec_sa_t sa;
6721
6722   memset (&sa, 0, sizeof (sa));
6723
6724   sa.id = ntohl (mp->sad_id);
6725   sa.spi = ntohl (mp->spi);
6726   /* security protocol AH unsupported */
6727   if (mp->protocol == IPSEC_PROTOCOL_AH)
6728     {
6729       clib_warning ("unsupported security protocol 'AH'");
6730       rv = VNET_API_ERROR_UNIMPLEMENTED;
6731       goto out;
6732     }
6733   sa.protocol = mp->protocol;
6734   /* check for unsupported crypto-alg */
6735   if (mp->crypto_algorithm < IPSEC_CRYPTO_ALG_AES_CBC_128 ||
6736       mp->crypto_algorithm > IPSEC_CRYPTO_ALG_AES_CBC_256)
6737     {
6738       clib_warning ("unsupported crypto-alg: '%U'", format_ipsec_crypto_alg,
6739                     mp->crypto_algorithm);
6740       rv = VNET_API_ERROR_UNIMPLEMENTED;
6741       goto out;
6742     }
6743   sa.crypto_alg = mp->crypto_algorithm;
6744   sa.crypto_key_len = mp->crypto_key_length;
6745   clib_memcpy (&sa.crypto_key, mp->crypto_key, sizeof (sa.crypto_key));
6746   /* check for unsupported integ-alg */
6747   if (mp->integrity_algorithm < IPSEC_INTEG_ALG_SHA1_96 ||
6748       mp->integrity_algorithm > IPSEC_INTEG_ALG_SHA_512_256)
6749     {
6750       clib_warning ("unsupported integ-alg: '%U'", format_ipsec_integ_alg,
6751                     mp->integrity_algorithm);
6752       rv = VNET_API_ERROR_UNIMPLEMENTED;
6753       goto out;
6754     }
6755   sa.integ_alg = mp->integrity_algorithm;
6756   sa.integ_key_len = mp->integrity_key_length;
6757   clib_memcpy (&sa.integ_key, mp->integrity_key, sizeof (sa.integ_key));
6758   sa.use_esn = mp->use_extended_sequence_number;
6759   sa.is_tunnel = mp->is_tunnel;
6760   sa.is_tunnel_ip6 = mp->is_tunnel_ipv6;
6761   if (sa.is_tunnel_ip6)
6762     {
6763       clib_memcpy (&sa.tunnel_src_addr, mp->tunnel_src_address, 16);
6764       clib_memcpy (&sa.tunnel_dst_addr, mp->tunnel_dst_address, 16);
6765     }
6766   else
6767     {
6768       clib_memcpy (&sa.tunnel_src_addr.ip4.data, mp->tunnel_src_address, 4);
6769       clib_memcpy (&sa.tunnel_dst_addr.ip4.data, mp->tunnel_dst_address, 4);
6770     }
6771
6772   rv = ipsec_add_del_sa (vm, &sa, mp->is_add);
6773 #else
6774   rv = VNET_API_ERROR_UNIMPLEMENTED;
6775   goto out;
6776 #endif
6777
6778 out:
6779   REPLY_MACRO (VL_API_IPSEC_SAD_ADD_DEL_ENTRY_REPLY);
6780 }
6781
6782 static void
6783 vl_api_ikev2_profile_add_del_t_handler (vl_api_ikev2_profile_add_del_t * mp)
6784 {
6785   vl_api_ikev2_profile_add_del_reply_t *rmp;
6786   int rv = 0;
6787
6788 #if IPSEC > 0
6789   vlib_main_t *vm = vlib_get_main ();
6790   clib_error_t *error;
6791   u8 *tmp = format (0, "%s", mp->name);
6792   error = ikev2_add_del_profile (vm, tmp, mp->is_add);
6793   vec_free (tmp);
6794   if (error)
6795     rv = VNET_API_ERROR_UNSPECIFIED;
6796 #else
6797   rv = VNET_API_ERROR_UNIMPLEMENTED;
6798 #endif
6799
6800   REPLY_MACRO (VL_API_IKEV2_PROFILE_ADD_DEL_REPLY);
6801 }
6802
6803 static void
6804   vl_api_ikev2_profile_set_auth_t_handler
6805   (vl_api_ikev2_profile_set_auth_t * mp)
6806 {
6807   vl_api_ikev2_profile_set_auth_reply_t *rmp;
6808   int rv = 0;
6809
6810 #if IPSEC > 0
6811   vlib_main_t *vm = vlib_get_main ();
6812   clib_error_t *error;
6813   u8 *tmp = format (0, "%s", mp->name);
6814   u8 *data = vec_new (u8, mp->data_len);
6815   clib_memcpy (data, mp->data, mp->data_len);
6816   error = ikev2_set_profile_auth (vm, tmp, mp->auth_method, data, mp->is_hex);
6817   vec_free (tmp);
6818   vec_free (data);
6819   if (error)
6820     rv = VNET_API_ERROR_UNSPECIFIED;
6821 #else
6822   rv = VNET_API_ERROR_UNIMPLEMENTED;
6823 #endif
6824
6825   REPLY_MACRO (VL_API_IKEV2_PROFILE_SET_AUTH_REPLY);
6826 }
6827
6828 static void
6829 vl_api_ikev2_profile_set_id_t_handler (vl_api_ikev2_profile_set_id_t * mp)
6830 {
6831   vl_api_ikev2_profile_add_del_reply_t *rmp;
6832   int rv = 0;
6833
6834 #if IPSEC > 0
6835   vlib_main_t *vm = vlib_get_main ();
6836   clib_error_t *error;
6837   u8 *tmp = format (0, "%s", mp->name);
6838   u8 *data = vec_new (u8, mp->data_len);
6839   clib_memcpy (data, mp->data, mp->data_len);
6840   error = ikev2_set_profile_id (vm, tmp, mp->id_type, data, mp->is_local);
6841   vec_free (tmp);
6842   vec_free (data);
6843   if (error)
6844     rv = VNET_API_ERROR_UNSPECIFIED;
6845 #else
6846   rv = VNET_API_ERROR_UNIMPLEMENTED;
6847 #endif
6848
6849   REPLY_MACRO (VL_API_IKEV2_PROFILE_SET_ID_REPLY);
6850 }
6851
6852 static void
6853 vl_api_ikev2_profile_set_ts_t_handler (vl_api_ikev2_profile_set_ts_t * mp)
6854 {
6855   vl_api_ikev2_profile_set_ts_reply_t *rmp;
6856   int rv = 0;
6857
6858 #if IPSEC > 0
6859   vlib_main_t *vm = vlib_get_main ();
6860   clib_error_t *error;
6861   u8 *tmp = format (0, "%s", mp->name);
6862   error = ikev2_set_profile_ts (vm, tmp, mp->proto, mp->start_port,
6863                                 mp->end_port, (ip4_address_t) mp->start_addr,
6864                                 (ip4_address_t) mp->end_addr, mp->is_local);
6865   vec_free (tmp);
6866   if (error)
6867     rv = VNET_API_ERROR_UNSPECIFIED;
6868 #else
6869   rv = VNET_API_ERROR_UNIMPLEMENTED;
6870 #endif
6871
6872   REPLY_MACRO (VL_API_IKEV2_PROFILE_SET_TS_REPLY);
6873 }
6874
6875 static void
6876 vl_api_ikev2_set_local_key_t_handler (vl_api_ikev2_set_local_key_t * mp)
6877 {
6878   vl_api_ikev2_profile_set_ts_reply_t *rmp;
6879   int rv = 0;
6880
6881 #if IPSEC > 0
6882   vlib_main_t *vm = vlib_get_main ();
6883   clib_error_t *error;
6884
6885   error = ikev2_set_local_key (vm, mp->key_file);
6886   if (error)
6887     rv = VNET_API_ERROR_UNSPECIFIED;
6888 #else
6889   rv = VNET_API_ERROR_UNIMPLEMENTED;
6890 #endif
6891
6892   REPLY_MACRO (VL_API_IKEV2_SET_LOCAL_KEY_REPLY);
6893 }
6894
6895 static void
6896 vl_api_map_add_domain_t_handler (vl_api_map_add_domain_t * mp)
6897 {
6898   vl_api_map_add_domain_reply_t *rmp;
6899   int rv = 0;
6900   u32 index;
6901   u8 flags = mp->is_translation ? MAP_DOMAIN_TRANSLATION : 0;
6902   rv =
6903     map_create_domain ((ip4_address_t *) & mp->ip4_prefix, mp->ip4_prefix_len,
6904                        (ip6_address_t *) & mp->ip6_prefix, mp->ip6_prefix_len,
6905                        (ip6_address_t *) & mp->ip6_src,
6906                        mp->ip6_src_prefix_len, mp->ea_bits_len,
6907                        mp->psid_offset, mp->psid_length, &index,
6908                        ntohs (mp->mtu), flags);
6909
6910   /* *INDENT-OFF* */
6911   REPLY_MACRO2(VL_API_MAP_ADD_DOMAIN_REPLY,
6912   ({
6913     rmp->index = ntohl(index);
6914   }));
6915   /* *INDENT-ON* */
6916 }
6917
6918 static void
6919 vl_api_map_del_domain_t_handler (vl_api_map_del_domain_t * mp)
6920 {
6921   vl_api_map_del_domain_reply_t *rmp;
6922   int rv = 0;
6923
6924   rv = map_delete_domain (ntohl (mp->index));
6925
6926   REPLY_MACRO (VL_API_MAP_DEL_DOMAIN_REPLY);
6927 }
6928
6929 static void
6930 vl_api_map_add_del_rule_t_handler (vl_api_map_add_del_rule_t * mp)
6931 {
6932   vl_api_map_del_domain_reply_t *rmp;
6933   int rv = 0;
6934
6935   rv =
6936     map_add_del_psid (ntohl (mp->index), ntohs (mp->psid),
6937                       (ip6_address_t *) mp->ip6_dst, mp->is_add);
6938
6939   REPLY_MACRO (VL_API_MAP_ADD_DEL_RULE_REPLY);
6940 }
6941
6942 static void
6943 vl_api_map_domain_dump_t_handler (vl_api_map_domain_dump_t * mp)
6944 {
6945   vl_api_map_domain_details_t *rmp;
6946   map_main_t *mm = &map_main;
6947   map_domain_t *d;
6948   unix_shared_memory_queue_t *q;
6949
6950   if (pool_elts (mm->domains) == 0)
6951     return;
6952
6953   q = vl_api_client_index_to_input_queue (mp->client_index);
6954   if (q == 0)
6955     {
6956       return;
6957     }
6958
6959   /* *INDENT-OFF* */
6960   pool_foreach(d, mm->domains,
6961   ({
6962     /* Make sure every field is initiated (or don't skip the memset()) */
6963     rmp = vl_msg_api_alloc (sizeof (*rmp));
6964     rmp->_vl_msg_id = ntohs(VL_API_MAP_DOMAIN_DETAILS);
6965     rmp->domain_index = htonl(d - mm->domains);
6966     rmp->ea_bits_len = d->ea_bits_len;
6967     rmp->psid_offset = d->psid_offset;
6968     rmp->psid_length = d->psid_length;
6969     clib_memcpy(rmp->ip4_prefix, &d->ip4_prefix, sizeof(rmp->ip4_prefix));
6970     rmp->ip4_prefix_len = d->ip4_prefix_len;
6971     clib_memcpy(rmp->ip6_prefix, &d->ip6_prefix, sizeof(rmp->ip6_prefix));
6972     rmp->ip6_prefix_len = d->ip6_prefix_len;
6973     clib_memcpy(rmp->ip6_src, &d->ip6_src, sizeof(rmp->ip6_src));
6974     rmp->ip6_src_len = d->ip6_src_len;
6975     rmp->mtu = htons(d->mtu);
6976     rmp->is_translation = (d->flags & MAP_DOMAIN_TRANSLATION);
6977     rmp->context = mp->context;
6978
6979     vl_msg_api_send_shmem (q, (u8 *)&rmp);
6980   }));
6981   /* *INDENT-ON* */
6982 }
6983
6984 static void
6985 vl_api_map_rule_dump_t_handler (vl_api_map_rule_dump_t * mp)
6986 {
6987   unix_shared_memory_queue_t *q;
6988   u16 i;
6989   ip6_address_t dst;
6990   vl_api_map_rule_details_t *rmp;
6991   map_main_t *mm = &map_main;
6992   u32 domain_index = ntohl (mp->domain_index);
6993   map_domain_t *d;
6994
6995   if (pool_elts (mm->domains) == 0)
6996     return;
6997
6998   d = pool_elt_at_index (mm->domains, domain_index);
6999   if (!d || !d->rules)
7000     {
7001       return;
7002     }
7003
7004   q = vl_api_client_index_to_input_queue (mp->client_index);
7005   if (q == 0)
7006     {
7007       return;
7008     }
7009
7010   for (i = 0; i < (0x1 << d->psid_length); i++)
7011     {
7012       dst = d->rules[i];
7013       if (dst.as_u64[0] == 0 && dst.as_u64[1] == 0)
7014         {
7015           continue;
7016         }
7017       rmp = vl_msg_api_alloc (sizeof (*rmp));
7018       memset (rmp, 0, sizeof (*rmp));
7019       rmp->_vl_msg_id = ntohs (VL_API_MAP_RULE_DETAILS);
7020       rmp->psid = htons (i);
7021       clib_memcpy (rmp->ip6_dst, &dst, sizeof (rmp->ip6_dst));
7022       rmp->context = mp->context;
7023       vl_msg_api_send_shmem (q, (u8 *) & rmp);
7024     }
7025 }
7026
7027 static void
7028 vl_api_map_summary_stats_t_handler (vl_api_map_summary_stats_t * mp)
7029 {
7030   vl_api_map_summary_stats_reply_t *rmp;
7031   vlib_combined_counter_main_t *cm;
7032   vlib_counter_t v;
7033   int i, which;
7034   u64 total_pkts[VLIB_N_RX_TX];
7035   u64 total_bytes[VLIB_N_RX_TX];
7036   map_main_t *mm = &map_main;
7037   unix_shared_memory_queue_t *q =
7038     vl_api_client_index_to_input_queue (mp->client_index);
7039
7040   if (!q)
7041     return;
7042
7043   rmp = vl_msg_api_alloc (sizeof (*rmp));
7044   rmp->_vl_msg_id = ntohs (VL_API_MAP_SUMMARY_STATS_REPLY);
7045   rmp->context = mp->context;
7046   rmp->retval = 0;
7047
7048   memset (total_pkts, 0, sizeof (total_pkts));
7049   memset (total_bytes, 0, sizeof (total_bytes));
7050
7051   map_domain_counter_lock (mm);
7052   vec_foreach (cm, mm->domain_counters)
7053   {
7054     which = cm - mm->domain_counters;
7055
7056     for (i = 0; i < vec_len (cm->maxi); i++)
7057       {
7058         vlib_get_combined_counter (cm, i, &v);
7059         total_pkts[which] += v.packets;
7060         total_bytes[which] += v.bytes;
7061       }
7062   }
7063
7064   map_domain_counter_unlock (mm);
7065
7066   /* Note: in network byte order! */
7067   rmp->total_pkts[MAP_DOMAIN_COUNTER_RX] =
7068     clib_host_to_net_u64 (total_pkts[MAP_DOMAIN_COUNTER_RX]);
7069   rmp->total_bytes[MAP_DOMAIN_COUNTER_RX] =
7070     clib_host_to_net_u64 (total_bytes[MAP_DOMAIN_COUNTER_RX]);
7071   rmp->total_pkts[MAP_DOMAIN_COUNTER_TX] =
7072     clib_host_to_net_u64 (total_pkts[MAP_DOMAIN_COUNTER_TX]);
7073   rmp->total_bytes[MAP_DOMAIN_COUNTER_TX] =
7074     clib_host_to_net_u64 (total_bytes[MAP_DOMAIN_COUNTER_TX]);
7075   rmp->total_bindings = clib_host_to_net_u64 (pool_elts (mm->domains));
7076   rmp->total_ip4_fragments = 0; // Not yet implemented. Should be a simple counter.
7077   rmp->total_security_check[MAP_DOMAIN_COUNTER_TX] =
7078     clib_host_to_net_u64 (map_error_counter_get
7079                           (ip4_map_node.index, MAP_ERROR_ENCAP_SEC_CHECK));
7080   rmp->total_security_check[MAP_DOMAIN_COUNTER_RX] =
7081     clib_host_to_net_u64 (map_error_counter_get
7082                           (ip4_map_node.index, MAP_ERROR_DECAP_SEC_CHECK));
7083
7084   vl_msg_api_send_shmem (q, (u8 *) & rmp);
7085 }
7086
7087 static void
7088 vl_api_ipsec_sa_set_key_t_handler (vl_api_ipsec_sa_set_key_t * mp)
7089 {
7090   vlib_main_t *vm __attribute__ ((unused)) = vlib_get_main ();
7091   vl_api_ipsec_sa_set_key_reply_t *rmp;
7092   int rv;
7093 #if IPSEC > 0
7094   ipsec_sa_t sa;
7095   sa.id = ntohl (mp->sa_id);
7096   sa.crypto_key_len = mp->crypto_key_length;
7097   clib_memcpy (&sa.crypto_key, mp->crypto_key, sizeof (sa.crypto_key));
7098   sa.integ_key_len = mp->integrity_key_length;
7099   clib_memcpy (&sa.integ_key, mp->integrity_key, sizeof (sa.integ_key));
7100
7101   rv = ipsec_set_sa_key (vm, &sa);
7102 #else
7103   rv = VNET_API_ERROR_UNIMPLEMENTED;
7104 #endif
7105
7106   REPLY_MACRO (VL_API_IPSEC_SA_SET_KEY_REPLY);
7107 }
7108
7109 static void vl_api_cop_interface_enable_disable_t_handler
7110   (vl_api_cop_interface_enable_disable_t * mp)
7111 {
7112   vl_api_cop_interface_enable_disable_reply_t *rmp;
7113   int rv;
7114   u32 sw_if_index = ntohl (mp->sw_if_index);
7115   int enable_disable;
7116
7117   VALIDATE_SW_IF_INDEX (mp);
7118
7119   enable_disable = (int) mp->enable_disable;
7120
7121   rv = cop_interface_enable_disable (sw_if_index, enable_disable);
7122
7123   BAD_SW_IF_INDEX_LABEL;
7124
7125   REPLY_MACRO (VL_API_COP_INTERFACE_ENABLE_DISABLE_REPLY);
7126 }
7127
7128 static void vl_api_cop_whitelist_enable_disable_t_handler
7129   (vl_api_cop_whitelist_enable_disable_t * mp)
7130 {
7131   vl_api_cop_whitelist_enable_disable_reply_t *rmp;
7132   cop_whitelist_enable_disable_args_t _a, *a = &_a;
7133   u32 sw_if_index = ntohl (mp->sw_if_index);
7134   int rv;
7135
7136   VALIDATE_SW_IF_INDEX (mp);
7137
7138   a->sw_if_index = sw_if_index;
7139   a->ip4 = mp->ip4;
7140   a->ip6 = mp->ip6;
7141   a->default_cop = mp->default_cop;
7142   a->fib_id = ntohl (mp->fib_id);
7143
7144   rv = cop_whitelist_enable_disable (a);
7145
7146   BAD_SW_IF_INDEX_LABEL;
7147
7148   REPLY_MACRO (VL_API_COP_WHITELIST_ENABLE_DISABLE_REPLY);
7149 }
7150
7151 static void
7152 vl_api_get_node_graph_t_handler (vl_api_get_node_graph_t * mp)
7153 {
7154   int rv = 0;
7155   u8 *vector = 0;
7156   api_main_t *am = &api_main;
7157   vlib_main_t *vm = vlib_get_main ();
7158   void *oldheap;
7159   vl_api_get_node_graph_reply_t *rmp;
7160
7161   pthread_mutex_lock (&am->vlib_rp->mutex);
7162   oldheap = svm_push_data_heap (am->vlib_rp);
7163
7164   /*
7165    * Keep the number of memcpy ops to a minimum (e.g. 1).
7166    */
7167   vec_validate (vector, 16384);
7168   vec_reset_length (vector);
7169
7170   /* $$$$ FIXME */
7171   vector = vlib_node_serialize (&vm->node_main, vector,
7172                                 (u32) ~ 0 /* all threads */ ,
7173                                 1 /* include nexts */ ,
7174                                 1 /* include stats */ );
7175
7176   svm_pop_heap (oldheap);
7177   pthread_mutex_unlock (&am->vlib_rp->mutex);
7178
7179   /* *INDENT-OFF* */
7180   REPLY_MACRO2(VL_API_GET_NODE_GRAPH_REPLY,
7181   ({
7182     rmp->reply_in_shmem = (uword) vector;
7183   }));
7184   /* *INDENT-ON* */
7185 }
7186
7187 static void
7188 vl_api_ioam_enable_t_handler (vl_api_ioam_enable_t * mp)
7189 {
7190   int rv = 0;
7191   vl_api_ioam_enable_reply_t *rmp;
7192   clib_error_t *error;
7193
7194   /* Ignoring the profile id as currently a single profile
7195    * is supported */
7196   error = ip6_ioam_enable (mp->trace_enable, mp->pot_enable,
7197                            mp->seqno, mp->analyse);
7198   if (error)
7199     {
7200       clib_error_report (error);
7201       rv = clib_error_get_code (error);
7202     }
7203
7204   REPLY_MACRO (VL_API_IOAM_ENABLE_REPLY);
7205 }
7206
7207 static void
7208 vl_api_ioam_disable_t_handler (vl_api_ioam_disable_t * mp)
7209 {
7210   int rv = 0;
7211   vl_api_ioam_disable_reply_t *rmp;
7212   clib_error_t *error;
7213
7214   error = clear_ioam_rewrite_fn ();
7215   if (error)
7216     {
7217       clib_error_report (error);
7218       rv = clib_error_get_code (error);
7219     }
7220
7221   REPLY_MACRO (VL_API_IOAM_DISABLE_REPLY);
7222 }
7223
7224 static void
7225 vl_api_af_packet_create_t_handler (vl_api_af_packet_create_t * mp)
7226 {
7227   vlib_main_t *vm = vlib_get_main ();
7228   vl_api_af_packet_create_reply_t *rmp;
7229   int rv = 0;
7230   u8 *host_if_name = NULL;
7231   u32 sw_if_index;
7232
7233   host_if_name = format (0, "%s", mp->host_if_name);
7234   vec_add1 (host_if_name, 0);
7235
7236   rv = af_packet_create_if (vm, host_if_name,
7237                             mp->use_random_hw_addr ? 0 : mp->hw_addr,
7238                             &sw_if_index);
7239
7240   vec_free (host_if_name);
7241
7242   /* *INDENT-OFF* */
7243   REPLY_MACRO2(VL_API_AF_PACKET_CREATE_REPLY,
7244   ({
7245     rmp->sw_if_index = clib_host_to_net_u32(sw_if_index);
7246   }));
7247   /* *INDENT-ON* */
7248 }
7249
7250 static void
7251 vl_api_af_packet_delete_t_handler (vl_api_af_packet_delete_t * mp)
7252 {
7253   vlib_main_t *vm = vlib_get_main ();
7254   vl_api_af_packet_delete_reply_t *rmp;
7255   int rv = 0;
7256   u8 *host_if_name = NULL;
7257
7258   host_if_name = format (0, "%s", mp->host_if_name);
7259   vec_add1 (host_if_name, 0);
7260
7261   rv = af_packet_delete_if (vm, host_if_name);
7262
7263   vec_free (host_if_name);
7264
7265   REPLY_MACRO (VL_API_AF_PACKET_DELETE_REPLY);
7266 }
7267
7268 static void
7269 vl_api_policer_add_del_t_handler (vl_api_policer_add_del_t * mp)
7270 {
7271   vlib_main_t *vm = vlib_get_main ();
7272   vl_api_policer_add_del_reply_t *rmp;
7273   int rv = 0;
7274   u8 *name = NULL;
7275   sse2_qos_pol_cfg_params_st cfg;
7276   clib_error_t *error;
7277   u32 policer_index;
7278
7279   name = format (0, "%s", mp->name);
7280
7281   memset (&cfg, 0, sizeof (cfg));
7282   cfg.rfc = mp->type;
7283   cfg.rnd_type = mp->round_type;
7284   cfg.rate_type = mp->rate_type;
7285   cfg.rb.kbps.cir_kbps = mp->cir;
7286   cfg.rb.kbps.eir_kbps = mp->eir;
7287   cfg.rb.kbps.cb_bytes = mp->cb;
7288   cfg.rb.kbps.eb_bytes = mp->eb;
7289   cfg.conform_action.action_type = mp->conform_action_type;
7290   cfg.conform_action.dscp = mp->conform_dscp;
7291   cfg.exceed_action.action_type = mp->exceed_action_type;
7292   cfg.exceed_action.dscp = mp->exceed_dscp;
7293   cfg.violate_action.action_type = mp->violate_action_type;
7294   cfg.violate_action.dscp = mp->violate_dscp;
7295   cfg.color_aware = mp->color_aware;
7296
7297   error = policer_add_del (vm, name, &cfg, &policer_index, mp->is_add);
7298
7299   if (error)
7300     rv = VNET_API_ERROR_UNSPECIFIED;
7301
7302   /* *INDENT-OFF* */
7303   REPLY_MACRO2(VL_API_POLICER_ADD_DEL_REPLY,
7304   ({
7305     if (rv == 0 &&  mp->is_add)
7306       rmp->policer_index = ntohl(policer_index);
7307     else
7308       rmp->policer_index = ~0;
7309   }));
7310   /* *INDENT-ON* */
7311 }
7312
7313 static void
7314 send_policer_details (u8 * name,
7315                       sse2_qos_pol_cfg_params_st * config,
7316                       policer_read_response_type_st * templ,
7317                       unix_shared_memory_queue_t * q, u32 context)
7318 {
7319   vl_api_policer_details_t *mp;
7320
7321   mp = vl_msg_api_alloc (sizeof (*mp));
7322   memset (mp, 0, sizeof (*mp));
7323   mp->_vl_msg_id = ntohs (VL_API_POLICER_DETAILS);
7324   mp->context = context;
7325   mp->cir = htonl (config->rb.kbps.cir_kbps);
7326   mp->eir = htonl (config->rb.kbps.eir_kbps);
7327   mp->cb = htonl (config->rb.kbps.cb_bytes);
7328   mp->eb = htonl (config->rb.kbps.eb_bytes);
7329   mp->rate_type = config->rate_type;
7330   mp->round_type = config->rnd_type;
7331   mp->type = config->rfc;
7332   mp->conform_action_type = config->conform_action.action_type;
7333   mp->conform_dscp = config->conform_action.dscp;
7334   mp->exceed_action_type = config->exceed_action.action_type;
7335   mp->exceed_dscp = config->exceed_action.dscp;
7336   mp->violate_action_type = config->violate_action.action_type;
7337   mp->violate_dscp = config->violate_action.dscp;
7338   mp->single_rate = templ->single_rate ? 1 : 0;
7339   mp->color_aware = templ->color_aware ? 1 : 0;
7340   mp->scale = htonl (templ->scale);
7341   mp->cir_tokens_per_period = htonl (templ->cir_tokens_per_period);
7342   mp->pir_tokens_per_period = htonl (templ->pir_tokens_per_period);
7343   mp->current_limit = htonl (templ->current_limit);
7344   mp->current_bucket = htonl (templ->current_bucket);
7345   mp->extended_limit = htonl (templ->extended_limit);
7346   mp->extended_bucket = htonl (templ->extended_bucket);
7347   mp->last_update_time = clib_host_to_net_u64 (templ->last_update_time);
7348
7349   strncpy ((char *) mp->name, (char *) name, ARRAY_LEN (mp->name) - 1);
7350
7351   vl_msg_api_send_shmem (q, (u8 *) & mp);
7352 }
7353
7354 static void
7355 vl_api_policer_dump_t_handler (vl_api_policer_dump_t * mp)
7356 {
7357   unix_shared_memory_queue_t *q;
7358   vnet_policer_main_t *pm = &vnet_policer_main;
7359   hash_pair_t *hp;
7360   uword *p;
7361   u32 pool_index;
7362   u8 *match_name = 0;
7363   u8 *name;
7364   sse2_qos_pol_cfg_params_st *config;
7365   policer_read_response_type_st *templ;
7366
7367   q = vl_api_client_index_to_input_queue (mp->client_index);
7368   if (q == 0)
7369     return;
7370
7371   if (mp->match_name_valid)
7372     {
7373       match_name = format (0, "%s%c", mp->match_name, 0);
7374     }
7375
7376   if (mp->match_name_valid)
7377     {
7378       p = hash_get_mem (pm->policer_config_by_name, match_name);
7379       if (p)
7380         {
7381           pool_index = p[0];
7382           config = pool_elt_at_index (pm->configs, pool_index);
7383           templ = pool_elt_at_index (pm->policer_templates, pool_index);
7384           send_policer_details (match_name, config, templ, q, mp->context);
7385         }
7386     }
7387   else
7388     {
7389       /* *INDENT-OFF* */
7390       hash_foreach_pair (hp, pm->policer_config_by_name,
7391       ({
7392         name = (u8 *) hp->key;
7393         pool_index = hp->value[0];
7394         config = pool_elt_at_index (pm->configs, pool_index);
7395         templ = pool_elt_at_index (pm->policer_templates, pool_index);
7396         send_policer_details(name, config, templ, q, mp->context);
7397       }));
7398       /* *INDENT-ON* */
7399     }
7400 }
7401
7402 static void
7403   vl_api_policer_classify_set_interface_t_handler
7404   (vl_api_policer_classify_set_interface_t * mp)
7405 {
7406   vlib_main_t *vm = vlib_get_main ();
7407   vl_api_policer_classify_set_interface_reply_t *rmp;
7408   int rv;
7409   u32 sw_if_index, ip4_table_index, ip6_table_index, l2_table_index;
7410
7411   ip4_table_index = ntohl (mp->ip4_table_index);
7412   ip6_table_index = ntohl (mp->ip6_table_index);
7413   l2_table_index = ntohl (mp->l2_table_index);
7414   sw_if_index = ntohl (mp->sw_if_index);
7415
7416   VALIDATE_SW_IF_INDEX (mp);
7417
7418   rv = vnet_set_policer_classify_intfc (vm, sw_if_index, ip4_table_index,
7419                                         ip6_table_index, l2_table_index,
7420                                         mp->is_add);
7421
7422   BAD_SW_IF_INDEX_LABEL;
7423
7424   REPLY_MACRO (VL_API_POLICER_CLASSIFY_SET_INTERFACE_REPLY);
7425 }
7426
7427 static void
7428 send_policer_classify_details (u32 sw_if_index,
7429                                u32 table_index,
7430                                unix_shared_memory_queue_t * q, u32 context)
7431 {
7432   vl_api_policer_classify_details_t *mp;
7433
7434   mp = vl_msg_api_alloc (sizeof (*mp));
7435   memset (mp, 0, sizeof (*mp));
7436   mp->_vl_msg_id = ntohs (VL_API_POLICER_CLASSIFY_DETAILS);
7437   mp->context = context;
7438   mp->sw_if_index = htonl (sw_if_index);
7439   mp->table_index = htonl (table_index);
7440
7441   vl_msg_api_send_shmem (q, (u8 *) & mp);
7442 }
7443
7444 static void
7445 vl_api_policer_classify_dump_t_handler (vl_api_policer_classify_dump_t * mp)
7446 {
7447   unix_shared_memory_queue_t *q;
7448   policer_classify_main_t *pcm = &policer_classify_main;
7449   u32 *vec_tbl;
7450   int i;
7451
7452   q = vl_api_client_index_to_input_queue (mp->client_index);
7453   if (q == 0)
7454     return;
7455
7456   vec_tbl = pcm->classify_table_index_by_sw_if_index[mp->type];
7457
7458   if (vec_len (vec_tbl))
7459     {
7460       for (i = 0; i < vec_len (vec_tbl); i++)
7461         {
7462           if (vec_elt (vec_tbl, i) == ~0)
7463             continue;
7464
7465           send_policer_classify_details (i, vec_elt (vec_tbl, i), q,
7466                                          mp->context);
7467         }
7468     }
7469 }
7470
7471 static void
7472 vl_api_netmap_create_t_handler (vl_api_netmap_create_t * mp)
7473 {
7474   vlib_main_t *vm = vlib_get_main ();
7475   vl_api_netmap_create_reply_t *rmp;
7476   int rv = 0;
7477   u8 *if_name = NULL;
7478
7479   if_name = format (0, "%s", mp->netmap_if_name);
7480   vec_add1 (if_name, 0);
7481
7482   rv =
7483     netmap_create_if (vm, if_name, mp->use_random_hw_addr ? 0 : mp->hw_addr,
7484                       mp->is_pipe, mp->is_master, 0);
7485
7486   vec_free (if_name);
7487
7488   REPLY_MACRO (VL_API_NETMAP_CREATE_REPLY);
7489 }
7490
7491 static void
7492 vl_api_netmap_delete_t_handler (vl_api_netmap_delete_t * mp)
7493 {
7494   vlib_main_t *vm = vlib_get_main ();
7495   vl_api_netmap_delete_reply_t *rmp;
7496   int rv = 0;
7497   u8 *if_name = NULL;
7498
7499   if_name = format (0, "%s", mp->netmap_if_name);
7500   vec_add1 (if_name, 0);
7501
7502   rv = netmap_delete_if (vm, if_name);
7503
7504   vec_free (if_name);
7505
7506   REPLY_MACRO (VL_API_NETMAP_DELETE_REPLY);
7507 }
7508
7509 static void
7510 vl_api_mpls_eth_tunnel_details_t_handler (vl_api_mpls_eth_tunnel_details_t *
7511                                           mp)
7512 {
7513   clib_warning ("BUG");
7514 }
7515
7516 static void
7517 send_mpls_eth_tunnel_entry (vpe_api_main_t * am,
7518                             unix_shared_memory_queue_t * q,
7519                             mpls_eth_tunnel_t * et, u32 index, u32 context)
7520 {
7521   mpls_main_t *mm = &mpls_main;
7522   mpls_encap_t *e;
7523   int i;
7524   u32 nlabels;
7525   vl_api_mpls_eth_tunnel_details_t *mp;
7526
7527   e = pool_elt_at_index (mm->encaps, et->encap_index);
7528   nlabels = vec_len (e->labels);
7529
7530   mp = vl_msg_api_alloc (sizeof (*mp) + nlabels * sizeof (u32));
7531   memset (mp, 0, sizeof (*mp));
7532   mp->_vl_msg_id = ntohs (VL_API_MPLS_ETH_TUNNEL_DETAILS);
7533   mp->context = context;
7534
7535   mp->tunnel_index = htonl (index);
7536   memcpy (mp->tunnel_dst_mac, et->tunnel_dst, 6);
7537   mp->intfc_address = et->intfc_address.as_u32;
7538   mp->tx_sw_if_index = htonl (et->tx_sw_if_index);
7539   mp->inner_fib_index = htonl (et->inner_fib_index);
7540   mp->mask_width = htonl (et->mask_width);
7541   mp->encap_index = htonl (et->encap_index);
7542   mp->hw_if_index = htonl (et->hw_if_index);
7543   mp->l2_only = htonl (et->l2_only);
7544   mp->nlabels = htonl (nlabels);
7545
7546   for (i = 0; i < nlabels; i++)
7547     {
7548       mp->labels[i] =
7549         htonl (vnet_mpls_uc_get_label
7550                (clib_host_to_net_u32 (e->labels[i].label_exp_s_ttl)));
7551     }
7552
7553   vl_msg_api_send_shmem (q, (u8 *) & mp);
7554 }
7555
7556 static void
7557 vl_api_mpls_eth_tunnel_dump_t_handler (vl_api_mpls_eth_tunnel_dump_t * mp)
7558 {
7559   vpe_api_main_t *am = &vpe_api_main;
7560   unix_shared_memory_queue_t *q;
7561   mpls_main_t *mm = &mpls_main;
7562   mpls_eth_tunnel_t *et;
7563   u32 index = ntohl (mp->tunnel_index);
7564
7565   q = vl_api_client_index_to_input_queue (mp->client_index);
7566   if (q == 0)
7567     return;
7568
7569   if (index != ~0)
7570     {
7571       if (!pool_is_free_index (mm->eth_tunnels, index))
7572         {
7573           et = pool_elt_at_index (mm->eth_tunnels, index);
7574           send_mpls_eth_tunnel_entry (am, q, et, et - mm->eth_tunnels,
7575                                       mp->context);
7576         }
7577     }
7578   else
7579     {
7580       /* *INDENT-OFF* */
7581       pool_foreach (et, mm->eth_tunnels,
7582       ({
7583         send_mpls_eth_tunnel_entry (am, q, et, et - mm->eth_tunnels,
7584                                     mp->context);
7585       }));
7586       /* *INDENT-ON* */
7587     }
7588 }
7589
7590 static void
7591 vl_api_mpls_fib_details_t_handler (vl_api_mpls_fib_details_t * mp)
7592 {
7593   clib_warning ("BUG");
7594 }
7595
7596 static void
7597 vl_api_mpls_fib_details_t_endian (vl_api_mpls_fib_details_t * mp)
7598 {
7599   clib_warning ("BUG");
7600 }
7601
7602 static void
7603 vl_api_mpls_fib_details_t_print (vl_api_mpls_fib_details_t * mp)
7604 {
7605   clib_warning ("BUG");
7606 }
7607
7608 static void
7609 send_mpls_fib_details (vpe_api_main_t * am,
7610                        unix_shared_memory_queue_t * q,
7611                        u32 table_id, u32 label, u32 eos,
7612                        fib_route_path_encode_t * api_rpaths, u32 context)
7613 {
7614   vl_api_mpls_fib_details_t *mp;
7615   fib_route_path_encode_t *api_rpath;
7616   vl_api_fib_path_t *fp;
7617   int is_ip4, path_count;
7618
7619   path_count = vec_len (api_rpaths);
7620   mp = vl_msg_api_alloc (sizeof (*mp) + path_count * sizeof (*fp));
7621   if (!mp)
7622     return;
7623   memset (mp, 0, sizeof (*mp));
7624   mp->_vl_msg_id = ntohs (VL_API_MPLS_FIB_DETAILS);
7625   mp->context = context;
7626
7627   mp->table_id = htonl (table_id);
7628   mp->eos_bit = eos;
7629   mp->label = htonl (label);
7630
7631   mp->count = htonl (path_count);
7632   fp = mp->path;
7633   vec_foreach (api_rpath, api_rpaths)
7634   {
7635     memset (fp, 0, sizeof (*fp));
7636     fp->weight = htonl (api_rpath->rpath.frp_weight);
7637     fp->sw_if_index = htonl (api_rpath->rpath.frp_sw_if_index);
7638     memcpy (fp->next_hop, &api_rpath->rpath.frp_addr, sizeof (fp->next_hop));
7639     if (api_rpath->rpath.frp_proto == FIB_PROTOCOL_IP4)
7640       fp->afi = IP46_TYPE_IP4;
7641     else if (api_rpath->rpath.frp_proto == FIB_PROTOCOL_IP6)
7642       fp->afi = IP46_TYPE_IP6;
7643     else
7644       {
7645         is_ip4 = ip46_address_is_ip4 (&api_rpath->rpath.frp_addr);
7646         if (is_ip4)
7647           fp->afi = IP46_TYPE_IP4;
7648         else
7649           fp->afi = IP46_TYPE_IP6;
7650       }
7651     fp++;
7652   }
7653
7654   vl_msg_api_send_shmem (q, (u8 *) & mp);
7655 }
7656
7657 static void
7658 vl_api_mpls_fib_dump_t_handler (vl_api_mpls_fib_dump_t * mp)
7659 {
7660   vpe_api_main_t *am = &vpe_api_main;
7661   unix_shared_memory_queue_t *q;
7662   mpls_main_t *mm = &mpls_main;
7663   fib_table_t *fib_table;
7664   fib_node_index_t lfei, *lfeip, *lfeis = NULL;
7665   mpls_label_t key;
7666   fib_prefix_t pfx;
7667   u32 fib_index;
7668   fib_route_path_encode_t *api_rpaths;
7669
7670   q = vl_api_client_index_to_input_queue (mp->client_index);
7671   if (q == 0)
7672     return;
7673
7674   /* *INDENT-OFF* */
7675   pool_foreach (fib_table, mm->fibs,
7676   ({
7677     hash_foreach(key, lfei, fib_table->mpls.mf_entries,
7678     ({
7679         vec_add1(lfeis, lfei);
7680     }));
7681   }));
7682   vec_sort_with_function(lfeis, fib_entry_cmp_for_sort);
7683
7684   vec_foreach(lfeip, lfeis)
7685   {
7686     fib_entry_get_prefix(*lfeip, &pfx);
7687     fib_index = fib_entry_get_fib_index(*lfeip);
7688     fib_table = fib_table_get(fib_index, pfx.fp_proto);
7689     api_rpaths = NULL;
7690     fib_entry_encode(*lfeip, &api_rpaths);
7691     send_mpls_fib_details (am, q,
7692                            fib_table->ft_table_id,
7693                            pfx.fp_label,
7694                            pfx.fp_eos,
7695                            api_rpaths,
7696                            mp->context);
7697     vec_free(api_rpaths);
7698   }
7699
7700   vec_free (lfeis);
7701 }
7702
7703 static void
7704 vl_api_ip_fib_details_t_handler (vl_api_ip_fib_details_t * mp)
7705 {
7706   clib_warning ("BUG");
7707 }
7708
7709 static void
7710 vl_api_ip_fib_details_t_endian (vl_api_ip_fib_details_t * mp)
7711 {
7712   clib_warning ("BUG");
7713 }
7714
7715 static void
7716 vl_api_ip_fib_details_t_print (vl_api_ip_fib_details_t * mp)
7717 {
7718   clib_warning ("BUG");
7719 }
7720
7721 static void
7722 send_ip_fib_details (vpe_api_main_t * am,
7723                      unix_shared_memory_queue_t * q,
7724                      u32 table_id, fib_prefix_t *pfx,
7725                      fib_route_path_encode_t *api_rpaths, u32 context)
7726 {
7727   vl_api_ip_fib_details_t *mp;
7728   fib_route_path_encode_t *api_rpath;
7729   vl_api_fib_path_t *fp;
7730   int is_ip4, path_count;
7731
7732   path_count = vec_len(api_rpaths);
7733   mp = vl_msg_api_alloc (sizeof (*mp) + path_count * sizeof (*fp));
7734   if (!mp)
7735     return;
7736   memset (mp, 0, sizeof (*mp));
7737   mp->_vl_msg_id = ntohs (VL_API_IP_FIB_DETAILS);
7738   mp->context = context;
7739
7740   mp->table_id = htonl (table_id);
7741   mp->address_length = pfx->fp_len;
7742   memcpy(mp->address, &pfx->fp_addr.ip4, sizeof(pfx->fp_addr.ip4));
7743
7744   mp->count = htonl (path_count);
7745   fp = mp->path;
7746   vec_foreach(api_rpath, api_rpaths)
7747   {
7748     memset (fp, 0, sizeof (*fp));
7749     switch (api_rpath->dpo.dpoi_type)
7750       {
7751       case DPO_RECEIVE:
7752         fp->is_local = true;
7753         break;
7754       case DPO_DROP:
7755         fp->is_drop = true;
7756         break;
7757       case DPO_IP_NULL:
7758         switch (api_rpath->dpo.dpoi_index)
7759           {
7760           case IP_NULL_ACTION_NONE:
7761             fp->is_drop = true;
7762             break;
7763           case IP_NULL_ACTION_SEND_ICMP_UNREACH:
7764             fp->is_unreach = true;
7765             break;
7766           case IP_NULL_ACTION_SEND_ICMP_PROHIBIT:
7767             fp->is_prohibit = true;
7768             break;
7769           default:
7770             break;
7771           }
7772         break;
7773       default:
7774         break;
7775       }
7776     fp->weight = htonl(api_rpath->rpath.frp_weight);
7777     fp->sw_if_index = htonl(api_rpath->rpath.frp_sw_if_index);
7778     memcpy(fp->next_hop, &api_rpath->rpath.frp_addr, sizeof(fp->next_hop));
7779     if (api_rpath->rpath.frp_proto == FIB_PROTOCOL_IP4)
7780         fp->afi = IP46_TYPE_IP4;
7781     else if (api_rpath->rpath.frp_proto == FIB_PROTOCOL_IP6)
7782         fp->afi = IP46_TYPE_IP6;
7783     else
7784       {
7785         is_ip4 = ip46_address_is_ip4 (&api_rpath->rpath.frp_addr);
7786         if (is_ip4)
7787             fp->afi = IP46_TYPE_IP4;
7788         else
7789             fp->afi = IP46_TYPE_IP6;
7790       }
7791     fp++;
7792   }
7793
7794   vl_msg_api_send_shmem (q, (u8 *) & mp);
7795 }
7796
7797 static void
7798 vl_api_ip_fib_dump_t_handler (vl_api_ip_fib_dump_t * mp)
7799 {
7800   vpe_api_main_t *am = &vpe_api_main;
7801   unix_shared_memory_queue_t *q;
7802   ip4_main_t *im = &ip4_main;
7803   fib_table_t *fib_table;
7804   fib_node_index_t lfei, *lfeip, *lfeis = NULL;
7805   mpls_label_t key;
7806   fib_prefix_t pfx;
7807   u32 fib_index;
7808   fib_route_path_encode_t *api_rpaths;
7809   int i;
7810
7811   q = vl_api_client_index_to_input_queue (mp->client_index);
7812   if (q == 0)
7813     return;
7814
7815   /* *INDENT-OFF* */
7816   pool_foreach (fib_table, im->fibs,
7817   ({
7818     for (i = 0; i < ARRAY_LEN (fib_table->v4.fib_entry_by_dst_address); i++)
7819       {
7820         hash_foreach(key, lfei, fib_table->v4.fib_entry_by_dst_address[i],
7821         ({
7822           vec_add1(lfeis, lfei);
7823         }));
7824       }
7825   }));
7826
7827   vec_sort_with_function(lfeis, fib_entry_cmp_for_sort);
7828
7829   vec_foreach(lfeip, lfeis)
7830   {
7831     fib_entry_get_prefix(*lfeip, &pfx);
7832     fib_index = fib_entry_get_fib_index(*lfeip);
7833     fib_table = fib_table_get(fib_index, pfx.fp_proto);
7834     api_rpaths = NULL;
7835     fib_entry_encode(*lfeip, &api_rpaths);
7836     send_ip_fib_details (am, q,
7837                          fib_table->ft_table_id,
7838                          &pfx,
7839                          api_rpaths,
7840                          mp->context);
7841     vec_free(api_rpaths);
7842   }
7843
7844   vec_free (lfeis);
7845 }
7846
7847 static void
7848 vl_api_ip6_fib_details_t_handler (vl_api_ip6_fib_details_t * mp)
7849 {
7850   clib_warning ("BUG");
7851 }
7852
7853 static void
7854 vl_api_ip6_fib_details_t_endian (vl_api_ip6_fib_details_t * mp)
7855 {
7856   clib_warning ("BUG");
7857 }
7858
7859 static void
7860 vl_api_ip6_fib_details_t_print (vl_api_ip6_fib_details_t * mp)
7861 {
7862   clib_warning ("BUG");
7863 }
7864
7865 static void
7866 send_ip6_fib_details (vpe_api_main_t * am,
7867                       unix_shared_memory_queue_t * q,
7868                       u32 table_id, fib_prefix_t *pfx,
7869                       fib_route_path_encode_t *api_rpaths, u32 context)
7870 {
7871   vl_api_ip6_fib_details_t *mp;
7872   fib_route_path_encode_t *api_rpath;
7873   vl_api_fib_path_t *fp;
7874   int is_ip4, path_count;
7875
7876   path_count = vec_len(api_rpaths);
7877   mp = vl_msg_api_alloc (sizeof (*mp) + path_count * sizeof (*fp));
7878   if (!mp)
7879     return;
7880   memset (mp, 0, sizeof (*mp));
7881   mp->_vl_msg_id = ntohs (VL_API_IP6_FIB_DETAILS);
7882   mp->context = context;
7883
7884   mp->table_id = htonl (table_id);
7885   mp->address_length = pfx->fp_len;
7886   memcpy(mp->address, &pfx->fp_addr.ip6, sizeof(pfx->fp_addr.ip6));
7887
7888   mp->count = htonl (path_count);
7889   fp = mp->path;
7890   vec_foreach(api_rpath, api_rpaths)
7891   {
7892     memset (fp, 0, sizeof (*fp));
7893     switch (api_rpath->dpo.dpoi_type)
7894       {
7895       case DPO_RECEIVE:
7896         fp->is_local = true;
7897         break;
7898       case DPO_DROP:
7899         fp->is_drop = true;
7900         break;
7901       case DPO_IP_NULL:
7902         switch (api_rpath->dpo.dpoi_index)
7903           {
7904           case IP_NULL_DPO_ACTION_NUM+IP_NULL_ACTION_NONE:
7905             fp->is_drop = true;
7906             break;
7907           case IP_NULL_DPO_ACTION_NUM+IP_NULL_ACTION_SEND_ICMP_UNREACH:
7908             fp->is_unreach = true;
7909             break;
7910           case IP_NULL_DPO_ACTION_NUM+IP_NULL_ACTION_SEND_ICMP_PROHIBIT:
7911             fp->is_prohibit = true;
7912             break;
7913           default:
7914             break;
7915           }
7916         break;
7917       default:
7918         break;
7919       }
7920     fp->weight = htonl(api_rpath->rpath.frp_weight);
7921     fp->sw_if_index = htonl(api_rpath->rpath.frp_sw_if_index);
7922     memcpy(fp->next_hop, &api_rpath->rpath.frp_addr, sizeof(fp->next_hop));
7923     if (api_rpath->rpath.frp_proto == FIB_PROTOCOL_IP4)
7924       fp->afi = IP46_TYPE_IP4;
7925     else if (api_rpath->rpath.frp_proto == FIB_PROTOCOL_IP6)
7926       fp->afi = IP46_TYPE_IP6;
7927     else
7928       {
7929         is_ip4 = ip46_address_is_ip4 (&api_rpath->rpath.frp_addr);
7930         if (is_ip4)
7931           fp->afi = IP46_TYPE_IP4;
7932         else
7933           fp->afi = IP46_TYPE_IP6;
7934       }
7935     fp++;
7936   }
7937
7938   vl_msg_api_send_shmem (q, (u8 *) & mp);
7939 }
7940
7941 typedef struct apt_ip6_fib_show_ctx_t_ {
7942     u32 fib_index;
7943     fib_node_index_t *entries;
7944 } api_ip6_fib_show_ctx_t;
7945
7946 static void
7947 api_ip6_fib_table_put_entries (clib_bihash_kv_24_8_t * kvp,
7948                                void *arg)
7949 {
7950   api_ip6_fib_show_ctx_t *ctx = arg;
7951
7952   if ((kvp->key[2] >> 32) == ctx->fib_index)
7953     {
7954       vec_add1(ctx->entries, kvp->value);
7955     }
7956 }
7957
7958 static void
7959 api_ip6_fib_table_get_all (unix_shared_memory_queue_t *q,
7960                            vl_api_ip6_fib_dump_t *mp,
7961                            fib_table_t *fib_table)
7962 {
7963   vpe_api_main_t *am = &vpe_api_main;
7964   ip6_main_t *im6 = &ip6_main;
7965   ip6_fib_t *fib = &fib_table->v6;
7966   fib_node_index_t *fib_entry_index;
7967   api_ip6_fib_show_ctx_t ctx = {
7968     .fib_index = fib->index,
7969     .entries = NULL,
7970   };
7971   fib_route_path_encode_t *api_rpaths;
7972   fib_prefix_t pfx;
7973
7974   BV(clib_bihash_foreach_key_value_pair)
7975     ((BVT(clib_bihash) *) &im6->ip6_table[IP6_FIB_TABLE_NON_FWDING].ip6_hash,
7976      api_ip6_fib_table_put_entries,
7977      &ctx);
7978
7979   vec_sort_with_function(ctx.entries, fib_entry_cmp_for_sort);
7980
7981   vec_foreach(fib_entry_index, ctx.entries)
7982     {
7983       fib_entry_get_prefix(*fib_entry_index, &pfx);
7984       api_rpaths = NULL;
7985       fib_entry_encode(*fib_entry_index, &api_rpaths);
7986       send_ip6_fib_details (am, q,
7987                             fib_table->ft_table_id,
7988                             &pfx,
7989                             api_rpaths,
7990                             mp->context);
7991       vec_free(api_rpaths);
7992     }
7993
7994   vec_free(ctx.entries);
7995 }
7996
7997 static void
7998 vl_api_ip6_fib_dump_t_handler (vl_api_ip6_fib_dump_t * mp)
7999 {
8000   unix_shared_memory_queue_t *q;
8001   ip6_main_t *im6 = &ip6_main;
8002   fib_table_t *fib_table;
8003
8004   q = vl_api_client_index_to_input_queue (mp->client_index);
8005   if (q == 0)
8006     return;
8007
8008   /* *INDENT-OFF* */
8009   pool_foreach (fib_table, im6->fibs,
8010   ({
8011     api_ip6_fib_table_get_all(q, mp, fib_table);
8012   }));
8013 }
8014
8015 static void
8016 vl_api_mpls_fib_encap_details_t_handler (vl_api_mpls_fib_encap_details_t * mp)
8017 {
8018   clib_warning ("BUG");
8019 }
8020
8021 static void
8022 send_mpls_fib_encap_details (vpe_api_main_t * am,
8023                              unix_shared_memory_queue_t * q,
8024                              show_mpls_fib_t * s, u32 context)
8025 {
8026   vl_api_mpls_fib_encap_details_t *mp;
8027   mpls_main_t *mm = &mpls_main;
8028   mpls_encap_t *e;
8029   int i;
8030   u32 nlabels;
8031
8032   e = pool_elt_at_index (mm->encaps, s->entry_index);
8033   nlabels = vec_len (e->labels);
8034
8035   mp = vl_msg_api_alloc (sizeof (*mp) + nlabels * sizeof (u32));
8036   memset (mp, 0, sizeof (*mp));
8037   mp->_vl_msg_id = ntohs (VL_API_MPLS_FIB_ENCAP_DETAILS);
8038   mp->context = context;
8039
8040   mp->fib_index = htonl (s->fib_index);
8041   mp->entry_index = htonl (s->entry_index);
8042   mp->dest = s->dest;
8043   mp->s_bit = htonl (s->s_bit);
8044
8045   mp->nlabels = htonl (nlabels);
8046
8047   for (i = 0; i < nlabels; i++)
8048     {
8049       mp->labels[i] =
8050         htonl (vnet_mpls_uc_get_label
8051                (clib_host_to_net_u32 (e->labels[i].label_exp_s_ttl)));
8052     }
8053
8054   vl_msg_api_send_shmem (q, (u8 *) & mp);
8055 }
8056
8057 static void
8058 vl_api_mpls_fib_encap_dump_t_handler (vl_api_mpls_fib_encap_dump_t * mp)
8059 {
8060   vpe_api_main_t *am = &vpe_api_main;
8061   unix_shared_memory_queue_t *q;
8062   vlib_main_t *vm = &vlib_global_main;
8063   u64 key;
8064   u32 value;
8065   show_mpls_fib_t *records = 0;
8066   show_mpls_fib_t *s;
8067   mpls_main_t *mm = &mpls_main;
8068   ip4_fib_t *rx_fib;
8069
8070   q = vl_api_client_index_to_input_queue (mp->client_index);
8071   if (q == 0)
8072     return;
8073
8074   /* *INDENT-OFF* */
8075   hash_foreach (key, value, mm->mpls_encap_by_fib_and_dest,
8076   ({
8077     vec_add2 (records, s, 1);
8078     s->fib_index = (u32)(key>>32);
8079     s->dest = (u32)(key & 0xFFFFFFFF);
8080     s->entry_index = (u32) value;
8081   }));
8082   /* *INDENT-ON* */
8083
8084   if (0 == vec_len (records))
8085     {
8086       vlib_cli_output (vm, "MPLS encap table empty");
8087       goto out;
8088     }
8089
8090   /* sort output by dst address within fib */
8091   vec_sort_with_function (records, mpls_dest_cmp);
8092   vec_sort_with_function (records, mpls_fib_index_cmp);
8093   vlib_cli_output (vm, "MPLS encap table");
8094   vlib_cli_output (vm, "%=6s%=16s%=16s", "Table", "Dest address", "Labels");
8095   vec_foreach (s, records)
8096   {
8097     rx_fib = ip4_fib_get (s->fib_index);
8098     vlib_cli_output (vm, "%=6d%=16U%=16U", rx_fib->table_id,
8099                      format_ip4_address, &s->dest, format_mpls_encap_index,
8100                      mm, s->entry_index);
8101     send_mpls_fib_encap_details (am, q, s, mp->context);
8102   }
8103
8104 out:
8105   vec_free (records);
8106 }
8107
8108 static void
8109 vl_api_classify_table_ids_t_handler (vl_api_classify_table_ids_t * mp)
8110 {
8111   unix_shared_memory_queue_t *q;
8112
8113   q = vl_api_client_index_to_input_queue (mp->client_index);
8114   if (q == 0)
8115     return;
8116
8117   vnet_classify_main_t *cm = &vnet_classify_main;
8118   vnet_classify_table_t *t;
8119   u32 *table_ids = 0;
8120   u32 count;
8121
8122   /* *INDENT-OFF* */
8123   pool_foreach (t, cm->tables,
8124   ({
8125     vec_add1 (table_ids, ntohl(t - cm->tables));
8126   }));
8127   /* *INDENT-ON* */
8128   count = vec_len (table_ids);
8129
8130   vl_api_classify_table_ids_reply_t *rmp;
8131   rmp = vl_msg_api_alloc_as_if_client (sizeof (*rmp) + count * sizeof (u32));
8132   rmp->_vl_msg_id = ntohs (VL_API_CLASSIFY_TABLE_IDS_REPLY);
8133   rmp->context = mp->context;
8134   rmp->count = ntohl (count);
8135   clib_memcpy (rmp->ids, table_ids, count * sizeof (u32));
8136   rmp->retval = 0;
8137
8138   vl_msg_api_send_shmem (q, (u8 *) & rmp);
8139
8140   vec_free (table_ids);
8141 }
8142
8143 static void
8144   vl_api_classify_table_by_interface_t_handler
8145   (vl_api_classify_table_by_interface_t * mp)
8146 {
8147   vl_api_classify_table_by_interface_reply_t *rmp;
8148   int rv = 0;
8149
8150   u32 sw_if_index = ntohl (mp->sw_if_index);
8151   u32 *acl = 0;
8152
8153   vec_validate (acl, INPUT_ACL_N_TABLES - 1);
8154   vec_set (acl, ~0);
8155
8156   VALIDATE_SW_IF_INDEX (mp);
8157
8158   input_acl_main_t *am = &input_acl_main;
8159
8160   int if_idx;
8161   u32 type;
8162
8163   for (type = 0; type < INPUT_ACL_N_TABLES; type++)
8164     {
8165       u32 *vec_tbl = am->classify_table_index_by_sw_if_index[type];
8166       if (vec_len (vec_tbl))
8167         {
8168           for (if_idx = 0; if_idx < vec_len (vec_tbl); if_idx++)
8169             {
8170               if (vec_elt (vec_tbl, if_idx) == ~0 || sw_if_index != if_idx)
8171                 {
8172                   continue;
8173                 }
8174               acl[type] = vec_elt (vec_tbl, if_idx);
8175             }
8176         }
8177     }
8178
8179   BAD_SW_IF_INDEX_LABEL;
8180
8181   /* *INDENT-OFF* */
8182   REPLY_MACRO2(VL_API_CLASSIFY_TABLE_BY_INTERFACE_REPLY,
8183   ({
8184     rmp->sw_if_index = ntohl(sw_if_index);
8185     rmp->l2_table_id = ntohl(acl[INPUT_ACL_TABLE_L2]);
8186     rmp->ip4_table_id = ntohl(acl[INPUT_ACL_TABLE_IP4]);
8187     rmp->ip6_table_id = ntohl(acl[INPUT_ACL_TABLE_IP6]);
8188   }));
8189   /* *INDENT-ON* */
8190   vec_free (acl);
8191 }
8192
8193 static void
8194 vl_api_classify_table_info_t_handler (vl_api_classify_table_info_t * mp)
8195 {
8196   unix_shared_memory_queue_t *q;
8197
8198   q = vl_api_client_index_to_input_queue (mp->client_index);
8199   if (q == 0)
8200     return;
8201
8202   vl_api_classify_table_info_reply_t *rmp = 0;
8203
8204   vnet_classify_main_t *cm = &vnet_classify_main;
8205   u32 table_id = ntohl (mp->table_id);
8206   vnet_classify_table_t *t;
8207
8208   /* *INDENT-OFF* */
8209   pool_foreach (t, cm->tables,
8210   ({
8211     if (table_id == t - cm->tables)
8212       {
8213         rmp = vl_msg_api_alloc_as_if_client
8214           (sizeof (*rmp) + t->match_n_vectors * sizeof (u32x4));
8215         rmp->_vl_msg_id = ntohs (VL_API_CLASSIFY_TABLE_INFO_REPLY);
8216         rmp->context = mp->context;
8217         rmp->table_id = ntohl(table_id);
8218         rmp->nbuckets = ntohl(t->nbuckets);
8219         rmp->match_n_vectors = ntohl(t->match_n_vectors);
8220         rmp->skip_n_vectors = ntohl(t->skip_n_vectors);
8221         rmp->active_sessions = ntohl(t->active_elements);
8222         rmp->next_table_index = ntohl(t->next_table_index);
8223         rmp->miss_next_index = ntohl(t->miss_next_index);
8224         rmp->mask_length = ntohl(t->match_n_vectors * sizeof (u32x4));
8225         clib_memcpy(rmp->mask, t->mask, t->match_n_vectors * sizeof(u32x4));
8226         rmp->retval = 0;
8227         break;
8228       }
8229   }));
8230   /* *INDENT-ON* */
8231
8232   if (rmp == 0)
8233     {
8234       rmp = vl_msg_api_alloc (sizeof (*rmp));
8235       rmp->_vl_msg_id = ntohs ((VL_API_CLASSIFY_TABLE_INFO_REPLY));
8236       rmp->context = mp->context;
8237       rmp->retval = ntohl (VNET_API_ERROR_CLASSIFY_TABLE_NOT_FOUND);
8238     }
8239
8240   vl_msg_api_send_shmem (q, (u8 *) & rmp);
8241 }
8242
8243 static void
8244 vl_api_classify_session_details_t_handler (vl_api_classify_session_details_t *
8245                                            mp)
8246 {
8247   clib_warning ("BUG");
8248 }
8249
8250 static void
8251 send_classify_session_details (unix_shared_memory_queue_t * q,
8252                                u32 table_id,
8253                                u32 match_length,
8254                                vnet_classify_entry_t * e, u32 context)
8255 {
8256   vl_api_classify_session_details_t *rmp;
8257
8258   rmp = vl_msg_api_alloc (sizeof (*rmp));
8259   memset (rmp, 0, sizeof (*rmp));
8260   rmp->_vl_msg_id = ntohs (VL_API_CLASSIFY_SESSION_DETAILS);
8261   rmp->context = context;
8262   rmp->table_id = ntohl (table_id);
8263   rmp->hit_next_index = ntohl (e->next_index);
8264   rmp->advance = ntohl (e->advance);
8265   rmp->opaque_index = ntohl (e->opaque_index);
8266   rmp->match_length = ntohl (match_length);
8267   clib_memcpy (rmp->match, e->key, match_length);
8268
8269   vl_msg_api_send_shmem (q, (u8 *) & rmp);
8270 }
8271
8272 static void
8273 vl_api_classify_session_dump_t_handler (vl_api_classify_session_dump_t * mp)
8274 {
8275   vnet_classify_main_t *cm = &vnet_classify_main;
8276   unix_shared_memory_queue_t *q;
8277
8278   u32 table_id = ntohl (mp->table_id);
8279   vnet_classify_table_t *t;
8280
8281   q = vl_api_client_index_to_input_queue (mp->client_index);
8282   if (!q)
8283     return;
8284
8285   /* *INDENT-OFF* */
8286   pool_foreach (t, cm->tables,
8287   ({
8288     if (table_id == t - cm->tables)
8289       {
8290         vnet_classify_bucket_t * b;
8291         vnet_classify_entry_t * v, * save_v;
8292         int i, j, k;
8293
8294         for (i = 0; i < t->nbuckets; i++)
8295           {
8296             b = &t->buckets [i];
8297             if (b->offset == 0)
8298               continue;
8299
8300             save_v = vnet_classify_get_entry (t, b->offset);
8301             for (j = 0; j < (1<<b->log2_pages); j++)
8302               {
8303                 for (k = 0; k < t->entries_per_page; k++)
8304                   {
8305                     v = vnet_classify_entry_at_index
8306                       (t, save_v, j*t->entries_per_page + k);
8307                     if (vnet_classify_entry_is_free (v))
8308                       continue;
8309
8310                     send_classify_session_details
8311                       (q, table_id, t->match_n_vectors * sizeof (u32x4),
8312                        v, mp->context);
8313                   }
8314               }
8315           }
8316         break;
8317       }
8318   }));
8319   /* *INDENT-ON* */
8320 }
8321
8322 static void
8323 vl_api_set_ipfix_exporter_t_handler (vl_api_set_ipfix_exporter_t * mp)
8324 {
8325   vlib_main_t *vm = vlib_get_main ();
8326   flow_report_main_t *frm = &flow_report_main;
8327   vl_api_set_ipfix_exporter_reply_t *rmp;
8328   ip4_address_t collector, src;
8329   u16 collector_port = UDP_DST_PORT_ipfix;
8330   u32 path_mtu;
8331   u32 template_interval;
8332   u8 udp_checksum;
8333   u32 fib_id;
8334   u32 fib_index = ~0;
8335   int rv = 0;
8336
8337   memcpy (collector.data, mp->collector_address, sizeof (collector.data));
8338   collector_port = ntohs (mp->collector_port);
8339   if (collector_port == (u16) ~ 0)
8340     collector_port = UDP_DST_PORT_ipfix;
8341   memcpy (src.data, mp->src_address, sizeof (src.data));
8342   fib_id = ntohl (mp->vrf_id);
8343
8344   ip4_main_t *im = &ip4_main;
8345   if (fib_id == ~0)
8346     {
8347       fib_index = ~0;
8348     }
8349   else
8350     {
8351       uword *p = hash_get (im->fib_index_by_table_id, fib_id);
8352       if (!p)
8353         {
8354           rv = VNET_API_ERROR_NO_SUCH_FIB;
8355           goto out;
8356         }
8357       fib_index = p[0];
8358     }
8359
8360   path_mtu = ntohl (mp->path_mtu);
8361   if (path_mtu == ~0)
8362     path_mtu = 512;             // RFC 7011 section 10.3.3.
8363   template_interval = ntohl (mp->template_interval);
8364   if (template_interval == ~0)
8365     template_interval = 20;
8366   udp_checksum = mp->udp_checksum;
8367
8368   if (collector.as_u32 == 0)
8369     {
8370       rv = VNET_API_ERROR_INVALID_VALUE;
8371       goto out;
8372     }
8373
8374   if (src.as_u32 == 0)
8375     {
8376       rv = VNET_API_ERROR_INVALID_VALUE;
8377       goto out;
8378     }
8379
8380   if (path_mtu > 1450 /* vpp does not support fragmentation */ )
8381     {
8382       rv = VNET_API_ERROR_INVALID_VALUE;
8383       goto out;
8384     }
8385
8386   if (path_mtu < 68)
8387     {
8388       rv = VNET_API_ERROR_INVALID_VALUE;
8389       goto out;
8390     }
8391
8392   /* Reset report streams if we are reconfiguring IP addresses */
8393   if (frm->ipfix_collector.as_u32 != collector.as_u32 ||
8394       frm->src_address.as_u32 != src.as_u32 ||
8395       frm->collector_port != collector_port)
8396     vnet_flow_reports_reset (frm);
8397
8398   frm->ipfix_collector.as_u32 = collector.as_u32;
8399   frm->collector_port = collector_port;
8400   frm->src_address.as_u32 = src.as_u32;
8401   frm->fib_index = fib_index;
8402   frm->path_mtu = path_mtu;
8403   frm->template_interval = template_interval;
8404   frm->udp_checksum = udp_checksum;
8405
8406   /* Turn on the flow reporting process */
8407   vlib_process_signal_event (vm, flow_report_process_node.index, 1, 0);
8408
8409 out:
8410   REPLY_MACRO (VL_API_SET_IPFIX_EXPORTER_REPLY);
8411 }
8412
8413 static void
8414 vl_api_ipfix_exporter_dump_t_handler (vl_api_ipfix_exporter_dump_t * mp)
8415 {
8416   flow_report_main_t *frm = &flow_report_main;
8417   unix_shared_memory_queue_t *q;
8418   vl_api_ipfix_exporter_details_t *rmp;
8419   ip4_main_t *im = &ip4_main;
8420   u32 vrf_id;
8421
8422   q = vl_api_client_index_to_input_queue (mp->client_index);
8423   if (!q)
8424     return;
8425
8426   rmp = vl_msg_api_alloc (sizeof (*rmp));
8427   memset (rmp, 0, sizeof (*rmp));
8428   rmp->_vl_msg_id = ntohs (VL_API_IPFIX_EXPORTER_DETAILS);
8429   rmp->context = mp->context;
8430   memcpy (rmp->collector_address, frm->ipfix_collector.data,
8431           sizeof (frm->ipfix_collector.data));
8432   rmp->collector_port = htons (frm->collector_port);
8433   memcpy (rmp->src_address, frm->src_address.data,
8434           sizeof (frm->src_address.data));
8435   if (frm->fib_index == ~0)
8436     vrf_id = ~0;
8437   else
8438     vrf_id = im->fibs[frm->fib_index].ft_table_id;
8439   rmp->vrf_id = htonl (vrf_id);
8440   rmp->path_mtu = htonl (frm->path_mtu);
8441   rmp->template_interval = htonl (frm->template_interval);
8442   rmp->udp_checksum = (frm->udp_checksum != 0);
8443
8444   vl_msg_api_send_shmem (q, (u8 *) & rmp);
8445 }
8446
8447 static void
8448   vl_api_set_ipfix_classify_stream_t_handler
8449   (vl_api_set_ipfix_classify_stream_t * mp)
8450 {
8451   vl_api_set_ipfix_classify_stream_reply_t *rmp;
8452   flow_report_classify_main_t *fcm = &flow_report_classify_main;
8453   flow_report_main_t *frm = &flow_report_main;
8454   u32 domain_id = 0;
8455   u32 src_port = UDP_DST_PORT_ipfix;
8456   int rv = 0;
8457
8458   domain_id = ntohl (mp->domain_id);
8459   src_port = ntohs (mp->src_port);
8460
8461   if (fcm->src_port != 0 &&
8462       (fcm->domain_id != domain_id || fcm->src_port != (u16) src_port))
8463     {
8464       int rv = vnet_stream_change (frm, fcm->domain_id, fcm->src_port,
8465                                    domain_id, (u16) src_port);
8466       ASSERT (rv == 0);
8467     }
8468
8469   fcm->domain_id = domain_id;
8470   fcm->src_port = (u16) src_port;
8471
8472   REPLY_MACRO (VL_API_SET_IPFIX_CLASSIFY_STREAM_REPLY);
8473 }
8474
8475 static void
8476   vl_api_ipfix_classify_stream_dump_t_handler
8477   (vl_api_ipfix_classify_stream_dump_t * mp)
8478 {
8479   flow_report_classify_main_t *fcm = &flow_report_classify_main;
8480   unix_shared_memory_queue_t *q;
8481   vl_api_ipfix_classify_stream_details_t *rmp;
8482
8483   q = vl_api_client_index_to_input_queue (mp->client_index);
8484   if (!q)
8485     return;
8486
8487   rmp = vl_msg_api_alloc (sizeof (*rmp));
8488   memset (rmp, 0, sizeof (*rmp));
8489   rmp->_vl_msg_id = ntohs (VL_API_IPFIX_CLASSIFY_STREAM_DETAILS);
8490   rmp->context = mp->context;
8491   rmp->domain_id = htonl (fcm->domain_id);
8492   rmp->src_port = htons (fcm->src_port);
8493
8494   vl_msg_api_send_shmem (q, (u8 *) & rmp);
8495 }
8496
8497 static void
8498   vl_api_ipfix_classify_table_add_del_t_handler
8499   (vl_api_ipfix_classify_table_add_del_t * mp)
8500 {
8501   vl_api_ipfix_classify_table_add_del_reply_t *rmp;
8502   flow_report_classify_main_t *fcm = &flow_report_classify_main;
8503   flow_report_main_t *frm = &flow_report_main;
8504   vnet_flow_report_add_del_args_t args;
8505   ipfix_classify_table_t *table;
8506   int is_add;
8507   u32 classify_table_index;
8508   u8 ip_version;
8509   u8 transport_protocol;
8510   int rv = 0;
8511
8512   classify_table_index = ntohl (mp->table_id);
8513   ip_version = mp->ip_version;
8514   transport_protocol = mp->transport_protocol;
8515   is_add = mp->is_add;
8516
8517   if (fcm->src_port == 0)
8518     {
8519       /* call set_ipfix_classify_stream first */
8520       rv = VNET_API_ERROR_UNSPECIFIED;
8521       goto out;
8522     }
8523
8524   memset (&args, 0, sizeof (args));
8525
8526   table = 0;
8527   int i;
8528   for (i = 0; i < vec_len (fcm->tables); i++)
8529     if (ipfix_classify_table_index_valid (i))
8530       if (fcm->tables[i].classify_table_index == classify_table_index)
8531         {
8532           table = &fcm->tables[i];
8533           break;
8534         }
8535
8536   if (is_add)
8537     {
8538       if (table)
8539         {
8540           rv = VNET_API_ERROR_VALUE_EXIST;
8541           goto out;
8542         }
8543       table = ipfix_classify_add_table ();
8544       table->classify_table_index = classify_table_index;
8545     }
8546   else
8547     {
8548       if (!table)
8549         {
8550           rv = VNET_API_ERROR_NO_SUCH_ENTRY;
8551           goto out;
8552         }
8553     }
8554
8555   table->ip_version = ip_version;
8556   table->transport_protocol = transport_protocol;
8557
8558   args.opaque.as_uword = table - fcm->tables;
8559   args.rewrite_callback = ipfix_classify_template_rewrite;
8560   args.flow_data_callback = ipfix_classify_send_flows;
8561   args.is_add = is_add;
8562   args.domain_id = fcm->domain_id;
8563   args.src_port = fcm->src_port;
8564
8565   rv = vnet_flow_report_add_del (frm, &args);
8566
8567   /* If deleting, or add failed */
8568   if (is_add == 0 || (rv && is_add))
8569     ipfix_classify_delete_table (table - fcm->tables);
8570
8571 out:
8572   REPLY_MACRO (VL_API_SET_IPFIX_CLASSIFY_STREAM_REPLY);
8573 }
8574
8575 static void
8576 send_ipfix_classify_table_details (u32 table_index,
8577                                    unix_shared_memory_queue_t * q,
8578                                    u32 context)
8579 {
8580   flow_report_classify_main_t *fcm = &flow_report_classify_main;
8581   vl_api_ipfix_classify_table_details_t *mp;
8582
8583   ipfix_classify_table_t *table = &fcm->tables[table_index];
8584
8585   mp = vl_msg_api_alloc (sizeof (*mp));
8586   memset (mp, 0, sizeof (*mp));
8587   mp->_vl_msg_id = ntohs (VL_API_IPFIX_CLASSIFY_TABLE_DETAILS);
8588   mp->context = context;
8589   mp->table_id = htonl (table->classify_table_index);
8590   mp->ip_version = table->ip_version;
8591   mp->transport_protocol = table->transport_protocol;
8592
8593   vl_msg_api_send_shmem (q, (u8 *) & mp);
8594 }
8595
8596 static void
8597   vl_api_ipfix_classify_table_dump_t_handler
8598   (vl_api_ipfix_classify_table_dump_t * mp)
8599 {
8600   flow_report_classify_main_t *fcm = &flow_report_classify_main;
8601   unix_shared_memory_queue_t *q;
8602   u32 i;
8603
8604   q = vl_api_client_index_to_input_queue (mp->client_index);
8605   if (!q)
8606     return;
8607
8608   for (i = 0; i < vec_len (fcm->tables); i++)
8609     if (ipfix_classify_table_index_valid (i))
8610       send_ipfix_classify_table_details (i, q, mp->context);
8611 }
8612
8613 static void
8614   vl_api_sw_interface_span_enable_disable_t_handler
8615   (vl_api_sw_interface_span_enable_disable_t * mp)
8616 {
8617   vl_api_sw_interface_span_enable_disable_reply_t *rmp;
8618   int rv;
8619
8620   vlib_main_t *vm = vlib_get_main ();
8621
8622   rv = span_add_delete_entry (vm, ntohl (mp->sw_if_index_from),
8623                               ntohl (mp->sw_if_index_to), mp->enable);
8624
8625   REPLY_MACRO (VL_API_SW_INTERFACE_SPAN_ENABLE_DISABLE_REPLY);
8626 }
8627
8628 static void
8629 vl_api_sw_interface_span_dump_t_handler (vl_api_sw_interface_span_dump_t * mp)
8630 {
8631
8632   unix_shared_memory_queue_t *q;
8633   vl_api_sw_interface_span_details_t *rmp;
8634   span_main_t *sm = &span_main;
8635   u32 src_sw_if_index = 0, *dst_sw_if_index;
8636
8637   q = vl_api_client_index_to_input_queue (mp->client_index);
8638   vec_foreach (dst_sw_if_index, sm->dst_by_src_sw_if_index)
8639   {
8640     if (*dst_sw_if_index > 0)
8641       {
8642         rmp = vl_msg_api_alloc (sizeof (*rmp));
8643         memset (rmp, 0, sizeof (*rmp));
8644         rmp->_vl_msg_id = ntohs (VL_API_SW_INTERFACE_SPAN_DETAILS);
8645         rmp->context = mp->context;
8646
8647         rmp->sw_if_index_from = htonl (src_sw_if_index);
8648         rmp->sw_if_index_to = htonl (*dst_sw_if_index);
8649
8650         vl_msg_api_send_shmem (q, (u8 *) & rmp);
8651       }
8652     src_sw_if_index++;
8653   }
8654 }
8655
8656 static void
8657 vl_api_pg_create_interface_t_handler (vl_api_pg_create_interface_t * mp)
8658 {
8659   vl_api_pg_create_interface_reply_t *rmp;
8660   int rv = 0;
8661
8662   pg_main_t *pg = &pg_main;
8663   u32 pg_if_id = pg_interface_add_or_get (pg, ntohl (mp->interface_id));
8664   pg_interface_t *pi = pool_elt_at_index (pg->interfaces, pg_if_id);
8665
8666   /* *INDENT-OFF* */
8667   REPLY_MACRO2(VL_API_PG_CREATE_INTERFACE_REPLY,
8668   ({
8669     rmp->sw_if_index = ntohl(pi->sw_if_index);
8670   }));
8671   /* *INDENT-ON* */
8672 }
8673
8674 static void
8675 vl_api_pg_capture_t_handler (vl_api_pg_capture_t * mp)
8676 {
8677   vl_api_pg_capture_reply_t *rmp;
8678   int rv = 0;
8679
8680   vnet_main_t *vnm = vnet_get_main ();
8681   vnet_interface_main_t *im = &vnm->interface_main;
8682   vnet_hw_interface_t *hi = 0;
8683
8684   u8 *intf_name = format (0, "pg%d", ntohl (mp->interface_id), 0);
8685   u32 hw_if_index = ~0;
8686   uword *p = hash_get_mem (im->hw_interface_by_name, intf_name);
8687   if (p)
8688     hw_if_index = *p;
8689   vec_free (intf_name);
8690
8691   if (hw_if_index != ~0)
8692     {
8693       pg_capture_args_t _a, *a = &_a;
8694
8695       u32 len = ntohl (mp->pcap_name_length);
8696       u8 *pcap_file_name = vec_new (u8, len);
8697       clib_memcpy (pcap_file_name, mp->pcap_file_name, len);
8698
8699       hi = vnet_get_sup_hw_interface (vnm, hw_if_index);
8700       a->hw_if_index = hw_if_index;
8701       a->dev_instance = hi->dev_instance;
8702       a->is_enabled = mp->is_enabled;
8703       a->pcap_file_name = pcap_file_name;
8704       a->count = ntohl (mp->count);
8705
8706       clib_error_t *e = pg_capture (a);
8707       if (e)
8708         {
8709           clib_error_report (e);
8710           rv = VNET_API_ERROR_CANNOT_CREATE_PCAP_FILE;
8711         }
8712
8713       vec_free (pcap_file_name);
8714     }
8715   REPLY_MACRO (VL_API_PG_CAPTURE_REPLY);
8716 }
8717
8718 static void
8719 vl_api_pg_enable_disable_t_handler (vl_api_pg_enable_disable_t * mp)
8720 {
8721   vl_api_pg_enable_disable_reply_t *rmp;
8722   int rv = 0;
8723
8724   pg_main_t *pg = &pg_main;
8725   u32 stream_index = ~0;
8726
8727   int is_enable = mp->is_enabled != 0;
8728   u32 len = ntohl (mp->stream_name_length) - 1;
8729
8730   if (len > 0)
8731     {
8732       u8 *stream_name = vec_new (u8, len);
8733       clib_memcpy (stream_name, mp->stream_name, len);
8734       uword *p = hash_get_mem (pg->stream_index_by_name, stream_name);
8735       if (p)
8736         stream_index = *p;
8737       vec_free (stream_name);
8738     }
8739
8740   pg_enable_disable (stream_index, is_enable);
8741
8742   REPLY_MACRO (VL_API_PG_ENABLE_DISABLE_REPLY);
8743 }
8744
8745 static void
8746   vl_api_ip_source_and_port_range_check_add_del_t_handler
8747   (vl_api_ip_source_and_port_range_check_add_del_t * mp)
8748 {
8749   vl_api_ip_source_and_port_range_check_add_del_reply_t *rmp;
8750   int rv = 0;
8751
8752   u8 is_ipv6 = mp->is_ipv6;
8753   u8 is_add = mp->is_add;
8754   u8 mask_length = mp->mask_length;
8755   ip4_address_t ip4_addr;
8756   ip6_address_t ip6_addr;
8757   u16 *low_ports = 0;
8758   u16 *high_ports = 0;
8759   u32 vrf_id;
8760   u16 tmp_low, tmp_high;
8761   u8 num_ranges;
8762   int i;
8763
8764   // Validate port range
8765   num_ranges = mp->number_of_ranges;
8766   if (num_ranges > 32)
8767     {                           // This is size of array in VPE.API
8768       rv = VNET_API_ERROR_EXCEEDED_NUMBER_OF_RANGES_CAPACITY;
8769       goto reply;
8770     }
8771
8772   vec_reset_length (low_ports);
8773   vec_reset_length (high_ports);
8774
8775   for (i = 0; i < num_ranges; i++)
8776     {
8777       tmp_low = mp->low_ports[i];
8778       tmp_high = mp->high_ports[i];
8779       // If tmp_low <= tmp_high then only need to check tmp_low = 0
8780       // If tmp_low <= tmp_high then only need to check tmp_high > 65535
8781       if (tmp_low > tmp_high || tmp_low == 0 || tmp_high > 65535)
8782         {
8783           rv = VNET_API_ERROR_INVALID_VALUE;
8784           goto reply;
8785         }
8786       vec_add1 (low_ports, tmp_low);
8787       vec_add1 (high_ports, tmp_high + 1);
8788     }
8789
8790   // Validate mask_length
8791   if ((is_ipv6 && mask_length > 128) || (!is_ipv6 && mask_length > 32))
8792     {
8793       rv = VNET_API_ERROR_ADDRESS_LENGTH_MISMATCH;
8794       goto reply;
8795     }
8796
8797   vrf_id = ntohl (mp->vrf_id);
8798
8799   if (vrf_id < 1)
8800     {
8801       rv = VNET_API_ERROR_INVALID_VALUE;
8802       goto reply;
8803     }
8804
8805
8806   if (is_ipv6)
8807     {
8808       clib_memcpy (ip6_addr.as_u8, mp->address, sizeof (ip6_addr.as_u8));
8809       rv = ip6_source_and_port_range_check_add_del (&ip6_addr,
8810                                                     mask_length,
8811                                                     vrf_id,
8812                                                     low_ports,
8813                                                     high_ports, is_add);
8814     }
8815   else
8816     {
8817       clib_memcpy (ip4_addr.data, mp->address, sizeof (ip4_addr));
8818       rv = ip4_source_and_port_range_check_add_del (&ip4_addr,
8819                                                     mask_length,
8820                                                     vrf_id,
8821                                                     low_ports,
8822                                                     high_ports, is_add);
8823     }
8824
8825 reply:
8826   vec_free (low_ports);
8827   vec_free (high_ports);
8828   REPLY_MACRO (VL_API_IP_SOURCE_AND_PORT_RANGE_CHECK_ADD_DEL_REPLY);
8829 }
8830
8831 static void
8832   vl_api_ip_source_and_port_range_check_interface_add_del_t_handler
8833   (vl_api_ip_source_and_port_range_check_interface_add_del_t * mp)
8834 {
8835   vlib_main_t *vm = vlib_get_main ();
8836   vl_api_ip_source_and_port_range_check_interface_add_del_reply_t *rmp;
8837   ip4_main_t *im = &ip4_main;
8838   int rv;
8839   u32 sw_if_index;
8840   u32 fib_index[IP_SOURCE_AND_PORT_RANGE_CHECK_N_PROTOCOLS];
8841   u32 vrf_id[IP_SOURCE_AND_PORT_RANGE_CHECK_N_PROTOCOLS];
8842   uword *p = 0;
8843   int i;
8844
8845   vrf_id[IP_SOURCE_AND_PORT_RANGE_CHECK_PROTOCOL_TCP_OUT] =
8846     ntohl (mp->tcp_out_vrf_id);
8847   vrf_id[IP_SOURCE_AND_PORT_RANGE_CHECK_PROTOCOL_UDP_OUT] =
8848     ntohl (mp->udp_out_vrf_id);
8849   vrf_id[IP_SOURCE_AND_PORT_RANGE_CHECK_PROTOCOL_TCP_IN] =
8850     ntohl (mp->tcp_in_vrf_id);
8851   vrf_id[IP_SOURCE_AND_PORT_RANGE_CHECK_PROTOCOL_UDP_IN] =
8852     ntohl (mp->udp_in_vrf_id);
8853
8854
8855   for (i = 0; i < IP_SOURCE_AND_PORT_RANGE_CHECK_N_PROTOCOLS; i++)
8856     {
8857       if (vrf_id[i] != 0 && vrf_id[i] != ~0)
8858         {
8859           p = hash_get (im->fib_index_by_table_id, vrf_id[i]);
8860
8861           if (p == 0)
8862             {
8863               rv = VNET_API_ERROR_INVALID_VALUE;
8864               goto reply;
8865             }
8866
8867           fib_index[i] = p[0];
8868         }
8869       else
8870         fib_index[i] = ~0;
8871     }
8872   sw_if_index = ntohl (mp->sw_if_index);
8873
8874   VALIDATE_SW_IF_INDEX (mp);
8875
8876   rv =
8877     set_ip_source_and_port_range_check (vm, fib_index, sw_if_index,
8878                                         mp->is_add);
8879
8880   BAD_SW_IF_INDEX_LABEL;
8881 reply:
8882
8883   REPLY_MACRO (VL_API_IP_SOURCE_AND_PORT_RANGE_CHECK_INTERFACE_ADD_DEL_REPLY);
8884 }
8885
8886 static void
8887 vl_api_ipsec_gre_add_del_tunnel_t_handler (vl_api_ipsec_gre_add_del_tunnel_t *
8888                                            mp)
8889 {
8890   vl_api_ipsec_gre_add_del_tunnel_reply_t *rmp;
8891   int rv = 0;
8892   vnet_ipsec_gre_add_del_tunnel_args_t _a, *a = &_a;
8893   u32 sw_if_index = ~0;
8894
8895   /* Check src & dst are different */
8896   if (memcmp (mp->src_address, mp->dst_address, 4) == 0)
8897     {
8898       rv = VNET_API_ERROR_SAME_SRC_DST;
8899       goto out;
8900     }
8901
8902   memset (a, 0, sizeof (*a));
8903
8904   /* ip addresses sent in network byte order */
8905   clib_memcpy (&(a->src), mp->src_address, 4);
8906   clib_memcpy (&(a->dst), mp->dst_address, 4);
8907   a->is_add = mp->is_add;
8908   a->lsa = ntohl (mp->local_sa_id);
8909   a->rsa = ntohl (mp->remote_sa_id);
8910
8911   rv = vnet_ipsec_gre_add_del_tunnel (a, &sw_if_index);
8912
8913 out:
8914     /* *INDENT-OFF* */
8915     REPLY_MACRO2(VL_API_GRE_ADD_DEL_TUNNEL_REPLY,
8916     ({
8917         rmp->sw_if_index = ntohl (sw_if_index);
8918     }));
8919     /* *INDENT-ON* */
8920 }
8921
8922 static void send_ipsec_gre_tunnel_details
8923   (ipsec_gre_tunnel_t * t, unix_shared_memory_queue_t * q, u32 context)
8924 {
8925   vl_api_ipsec_gre_tunnel_details_t *rmp;
8926
8927   rmp = vl_msg_api_alloc (sizeof (*rmp));
8928   memset (rmp, 0, sizeof (*rmp));
8929   rmp->_vl_msg_id = ntohs (VL_API_IPSEC_GRE_TUNNEL_DETAILS);
8930   clib_memcpy (rmp->src_address, &(t->tunnel_src), 4);
8931   clib_memcpy (rmp->dst_address, &(t->tunnel_dst), 4);
8932   rmp->sw_if_index = htonl (t->sw_if_index);
8933   rmp->local_sa_id = htonl (t->local_sa_id);
8934   rmp->remote_sa_id = htonl (t->remote_sa_id);
8935   rmp->context = context;
8936
8937   vl_msg_api_send_shmem (q, (u8 *) & rmp);
8938 }
8939
8940 static void vl_api_ipsec_gre_tunnel_dump_t_handler
8941   (vl_api_ipsec_gre_tunnel_dump_t * mp)
8942 {
8943   unix_shared_memory_queue_t *q;
8944   ipsec_gre_main_t *igm = &ipsec_gre_main;
8945   ipsec_gre_tunnel_t *t;
8946   u32 sw_if_index;
8947
8948   q = vl_api_client_index_to_input_queue (mp->client_index);
8949   if (q == 0)
8950     {
8951       return;
8952     }
8953
8954   sw_if_index = ntohl (mp->sw_if_index);
8955
8956   if (~0 == sw_if_index)
8957     {
8958         /* *INDENT-OFF* */
8959         pool_foreach (t, igm->tunnels,
8960         ({
8961             send_ipsec_gre_tunnel_details(t, q, mp->context);
8962         }));
8963         /* *INDENT-ON* */
8964     }
8965   else
8966     {
8967       if ((sw_if_index >= vec_len (igm->tunnel_index_by_sw_if_index)) ||
8968           (~0 == igm->tunnel_index_by_sw_if_index[sw_if_index]))
8969         {
8970           return;
8971         }
8972       t = &igm->tunnels[igm->tunnel_index_by_sw_if_index[sw_if_index]];
8973       send_ipsec_gre_tunnel_details (t, q, mp->context);
8974     }
8975 }
8976
8977 static void
8978 vl_api_delete_subif_t_handler (vl_api_delete_subif_t * mp)
8979 {
8980   vl_api_delete_subif_reply_t *rmp;
8981   int rv;
8982
8983   rv = vnet_delete_sub_interface (ntohl (mp->sw_if_index));
8984
8985   REPLY_MACRO (VL_API_DELETE_SUBIF_REPLY);
8986 }
8987
8988 static void
8989   vl_api_l2_interface_pbb_tag_rewrite_t_handler
8990   (vl_api_l2_interface_pbb_tag_rewrite_t * mp)
8991 {
8992   vl_api_l2_interface_pbb_tag_rewrite_reply_t *rmp;
8993   vnet_main_t *vnm = vnet_get_main ();
8994   vlib_main_t *vm = vlib_get_main ();
8995   u32 vtr_op;
8996   int rv = 0;
8997
8998   VALIDATE_SW_IF_INDEX (mp);
8999
9000   vtr_op = ntohl (mp->vtr_op);
9001
9002   switch (vtr_op)
9003     {
9004     case L2_VTR_DISABLED:
9005     case L2_VTR_PUSH_2:
9006     case L2_VTR_POP_2:
9007     case L2_VTR_TRANSLATE_2_1:
9008       break;
9009
9010     default:
9011       rv = VNET_API_ERROR_INVALID_VALUE;
9012       goto bad_sw_if_index;
9013     }
9014
9015   rv = l2pbb_configure (vm, vnm, ntohl (mp->sw_if_index), vtr_op,
9016                         mp->b_dmac, mp->b_smac, ntohs (mp->b_vlanid),
9017                         ntohl (mp->i_sid), ntohs (mp->outer_tag));
9018
9019   BAD_SW_IF_INDEX_LABEL;
9020
9021   REPLY_MACRO (VL_API_L2_INTERFACE_PBB_TAG_REWRITE_REPLY);
9022 }
9023
9024 static void
9025 vl_api_punt_t_handler (vl_api_punt_t * mp)
9026 {
9027   vl_api_punt_reply_t *rmp;
9028   vlib_main_t *vm = vlib_get_main ();
9029   int rv = 0;
9030   clib_error_t *error;
9031
9032   error = vnet_punt_add_del (vm, mp->ipv, mp->l4_protocol,
9033                              ntohs (mp->l4_port), mp->is_add);
9034   if (error)
9035     {
9036       rv = -1;
9037       clib_error_report (error);
9038     }
9039
9040   REPLY_MACRO (VL_API_PUNT_REPLY);
9041 }
9042
9043 static void
9044   vl_api_flow_classify_set_interface_t_handler
9045   (vl_api_flow_classify_set_interface_t * mp)
9046 {
9047   vlib_main_t *vm = vlib_get_main ();
9048   vl_api_flow_classify_set_interface_reply_t *rmp;
9049   int rv;
9050   u32 sw_if_index, ip4_table_index, ip6_table_index;
9051
9052   ip4_table_index = ntohl (mp->ip4_table_index);
9053   ip6_table_index = ntohl (mp->ip6_table_index);
9054   sw_if_index = ntohl (mp->sw_if_index);
9055
9056   VALIDATE_SW_IF_INDEX (mp);
9057
9058   rv = vnet_set_flow_classify_intfc (vm, sw_if_index, ip4_table_index,
9059                                      ip6_table_index, mp->is_add);
9060
9061   BAD_SW_IF_INDEX_LABEL;
9062
9063   REPLY_MACRO (VL_API_FLOW_CLASSIFY_SET_INTERFACE_REPLY);
9064 }
9065
9066 static void
9067 send_flow_classify_details (u32 sw_if_index,
9068                             u32 table_index,
9069                             unix_shared_memory_queue_t * q, u32 context)
9070 {
9071   vl_api_flow_classify_details_t *mp;
9072
9073   mp = vl_msg_api_alloc (sizeof (*mp));
9074   memset (mp, 0, sizeof (*mp));
9075   mp->_vl_msg_id = ntohs (VL_API_FLOW_CLASSIFY_DETAILS);
9076   mp->context = context;
9077   mp->sw_if_index = htonl (sw_if_index);
9078   mp->table_index = htonl (table_index);
9079
9080   vl_msg_api_send_shmem (q, (u8 *) & mp);
9081 }
9082
9083 static void
9084 vl_api_flow_classify_dump_t_handler (vl_api_flow_classify_dump_t * mp)
9085 {
9086   unix_shared_memory_queue_t *q;
9087   flow_classify_main_t *pcm = &flow_classify_main;
9088   u32 *vec_tbl;
9089   int i;
9090
9091   q = vl_api_client_index_to_input_queue (mp->client_index);
9092   if (q == 0)
9093     return;
9094
9095   vec_tbl = pcm->classify_table_index_by_sw_if_index[mp->type];
9096
9097   if (vec_len (vec_tbl))
9098     {
9099       for (i = 0; i < vec_len (vec_tbl); i++)
9100         {
9101           if (vec_elt (vec_tbl, i) == ~0)
9102             continue;
9103
9104           send_flow_classify_details (i, vec_elt (vec_tbl, i), q,
9105                                       mp->context);
9106         }
9107     }
9108 }
9109
9110 static void
9111 send_ipsec_spd_details (ipsec_policy_t * p, unix_shared_memory_queue_t * q,
9112                         u32 context)
9113 {
9114   vl_api_ipsec_spd_details_t *mp;
9115
9116   mp = vl_msg_api_alloc (sizeof (*mp));
9117   memset (mp, 0, sizeof (*mp));
9118   mp->_vl_msg_id = ntohs (VL_API_IPSEC_SPD_DETAILS);
9119   mp->context = context;
9120
9121   mp->spd_id = htonl (p->id);
9122   mp->priority = htonl (p->priority);
9123   mp->is_outbound = p->is_outbound;
9124   mp->is_ipv6 = p->is_ipv6;
9125   if (p->is_ipv6)
9126     {
9127       memcpy (mp->local_start_addr, &p->laddr.start.ip6, 16);
9128       memcpy (mp->local_stop_addr, &p->laddr.stop.ip6, 16);
9129       memcpy (mp->remote_start_addr, &p->raddr.start.ip6, 16);
9130       memcpy (mp->remote_stop_addr, &p->raddr.stop.ip6, 16);
9131     }
9132   else
9133     {
9134       memcpy (mp->local_start_addr, &p->laddr.start.ip4, 4);
9135       memcpy (mp->local_stop_addr, &p->laddr.stop.ip4, 4);
9136       memcpy (mp->remote_start_addr, &p->raddr.start.ip4, 4);
9137       memcpy (mp->remote_stop_addr, &p->raddr.stop.ip4, 4);
9138     }
9139   mp->local_start_port = htons (p->lport.start);
9140   mp->local_stop_port = htons (p->lport.stop);
9141   mp->remote_start_port = htons (p->rport.start);
9142   mp->remote_stop_port = htons (p->rport.stop);
9143   mp->protocol = p->protocol;
9144   mp->policy = p->policy;
9145   mp->sa_id = htonl (p->sa_id);
9146   mp->bytes = clib_host_to_net_u64 (p->counter.bytes);
9147   mp->packets = clib_host_to_net_u64 (p->counter.packets);
9148
9149   vl_msg_api_send_shmem (q, (u8 *) & mp);
9150 }
9151
9152 static void
9153 vl_api_ipsec_spd_dump_t_handler (vl_api_ipsec_spd_dump_t * mp)
9154 {
9155   unix_shared_memory_queue_t *q;
9156   ipsec_main_t *im = &ipsec_main;
9157   ipsec_policy_t *policy;
9158   ipsec_spd_t *spd;
9159   uword *p;
9160   u32 spd_index;
9161 #if IPSEC > 0
9162   q = vl_api_client_index_to_input_queue (mp->client_index);
9163   if (q == 0)
9164     return;
9165
9166   p = hash_get (im->spd_index_by_spd_id, ntohl (mp->spd_id));
9167   if (!p)
9168     return;
9169
9170   spd_index = p[0];
9171   spd = pool_elt_at_index (im->spds, spd_index);
9172
9173   pool_foreach (policy, spd->policies, (
9174                                          {
9175                                          if (mp->sa_id == ~(0)
9176                                              || ntohl (mp->sa_id) ==
9177                                              policy->sa_id)
9178                                          send_ipsec_spd_details (policy, q,
9179                                                                  mp->context);}
9180                 ));
9181 #else
9182   clib_warning ("unimplemented");
9183 #endif
9184 }
9185
9186 #define BOUNCE_HANDLER(nn)                                              \
9187 static void vl_api_##nn##_t_handler (                                   \
9188     vl_api_##nn##_t *mp)                                                \
9189 {                                                                       \
9190     vpe_client_registration_t *reg;                                     \
9191     vpe_api_main_t * vam = &vpe_api_main;                               \
9192     unix_shared_memory_queue_t * q;                                     \
9193                                                                         \
9194     /* One registration only... */                                      \
9195     pool_foreach(reg, vam->nn##_registrations,                          \
9196     ({                                                                  \
9197         q = vl_api_client_index_to_input_queue (reg->client_index);     \
9198         if (q) {                                                        \
9199             /*                                                          \
9200              * If the queue is stuffed, turf the msg and complain       \
9201              * It's unlikely that the intended recipient is             \
9202              * alive; avoid deadlock at all costs.                      \
9203              */                                                         \
9204             if (q->cursize == q->maxsize) {                             \
9205                 clib_warning ("ERROR: receiver queue full, drop msg");  \
9206                 vl_msg_api_free (mp);                                   \
9207                 return;                                                 \
9208             }                                                           \
9209             vl_msg_api_send_shmem (q, (u8 *)&mp);                       \
9210             return;                                                     \
9211         }                                                               \
9212     }));                                                                \
9213     vl_msg_api_free (mp);                                               \
9214 }
9215
9216 /*
9217  * vpe_api_hookup
9218  * Add vpe's API message handlers to the table.
9219  * vlib has alread mapped shared memory and
9220  * added the client registration handlers.
9221  * See .../open-repo/vlib/memclnt_vlib.c:memclnt_process()
9222  */
9223
9224 static clib_error_t *
9225 vpe_api_hookup (vlib_main_t * vm)
9226 {
9227   api_main_t *am = &api_main;
9228
9229 #define _(N,n)                                                  \
9230     vl_msg_api_set_handlers(VL_API_##N, #n,                     \
9231                            vl_api_##n##_t_handler,              \
9232                            vl_noop_handler,                     \
9233                            vl_api_##n##_t_endian,               \
9234                            vl_api_##n##_t_print,                \
9235                            sizeof(vl_api_##n##_t), 1);
9236   foreach_vpe_api_msg;
9237 #undef _
9238
9239   /*
9240    * Manually register the sr tunnel add del msg, so we trace
9241    * enough bytes to capture a typical segment list
9242    */
9243   vl_msg_api_set_handlers (VL_API_SR_TUNNEL_ADD_DEL,
9244                            "sr_tunnel_add_del",
9245                            vl_api_sr_tunnel_add_del_t_handler,
9246                            vl_noop_handler,
9247                            vl_api_sr_tunnel_add_del_t_endian,
9248                            vl_api_sr_tunnel_add_del_t_print, 256, 1);
9249
9250
9251   /*
9252    * Manually register the sr policy add del msg, so we trace
9253    * enough bytes to capture a typical tunnel name list
9254    */
9255   vl_msg_api_set_handlers (VL_API_SR_POLICY_ADD_DEL,
9256                            "sr_policy_add_del",
9257                            vl_api_sr_policy_add_del_t_handler,
9258                            vl_noop_handler,
9259                            vl_api_sr_policy_add_del_t_endian,
9260                            vl_api_sr_policy_add_del_t_print, 256, 1);
9261
9262   /*
9263    * Trace space for 8 MPLS encap labels, classifier mask+match
9264    */
9265   am->api_trace_cfg[VL_API_MPLS_ADD_DEL_ENCAP].size += 8 * sizeof (u32);
9266   am->api_trace_cfg[VL_API_CLASSIFY_ADD_DEL_TABLE].size += 5 * sizeof (u32x4);
9267   am->api_trace_cfg[VL_API_CLASSIFY_ADD_DEL_SESSION].size
9268     += 5 * sizeof (u32x4);
9269   am->api_trace_cfg[VL_API_VXLAN_ADD_DEL_TUNNEL].size += 16 * sizeof (u32);
9270
9271   /*
9272    * Thread-safe API messages
9273    */
9274   am->is_mp_safe[VL_API_IP_ADD_DEL_ROUTE] = 1;
9275   am->is_mp_safe[VL_API_GET_NODE_GRAPH] = 1;
9276
9277   return 0;
9278 }
9279
9280 VLIB_API_INIT_FUNCTION (vpe_api_hookup);
9281
9282 static clib_error_t *
9283 vpe_api_init (vlib_main_t * vm)
9284 {
9285   vpe_api_main_t *am = &vpe_api_main;
9286
9287   am->vlib_main = vm;
9288   am->vnet_main = vnet_get_main ();
9289   am->interface_events_registration_hash = hash_create (0, sizeof (uword));
9290   am->to_netconf_server_registration_hash = hash_create (0, sizeof (uword));
9291   am->from_netconf_server_registration_hash = hash_create (0, sizeof (uword));
9292   am->to_netconf_client_registration_hash = hash_create (0, sizeof (uword));
9293   am->from_netconf_client_registration_hash = hash_create (0, sizeof (uword));
9294   am->oam_events_registration_hash = hash_create (0, sizeof (uword));
9295
9296   vl_api_init (vm);
9297   vl_set_memory_region_name ("/vpe-api");
9298   vl_enable_disable_memory_api (vm, 1 /* enable it */ );
9299
9300   return 0;
9301 }
9302
9303 VLIB_INIT_FUNCTION (vpe_api_init);
9304
9305
9306 static clib_error_t *
9307 api_segment_config (vlib_main_t * vm, unformat_input_t * input)
9308 {
9309   u8 *chroot_path;
9310   u64 baseva, size, pvt_heap_size;
9311   int uid, gid, rv;
9312   const int max_buf_size = 4096;
9313   char *s, *buf;
9314   struct passwd _pw, *pw;
9315   struct group _grp, *grp;
9316   clib_error_t *e;
9317   buf = vec_new (char, 128);
9318   while (unformat_check_input (input) != UNFORMAT_END_OF_INPUT)
9319     {
9320       if (unformat (input, "prefix %s", &chroot_path))
9321         {
9322           vec_add1 (chroot_path, 0);
9323           vl_set_memory_root_path ((char *) chroot_path);
9324         }
9325       else if (unformat (input, "uid %d", &uid))
9326         vl_set_memory_uid (uid);
9327       else if (unformat (input, "gid %d", &gid))
9328         vl_set_memory_gid (gid);
9329       else if (unformat (input, "baseva %llx", &baseva))
9330         vl_set_global_memory_baseva (baseva);
9331       else if (unformat (input, "global-size %lldM", &size))
9332         vl_set_global_memory_size (size * (1ULL << 20));
9333       else if (unformat (input, "global-size %lldG", &size))
9334         vl_set_global_memory_size (size * (1ULL << 30));
9335       else if (unformat (input, "global-size %lld", &size))
9336         vl_set_global_memory_size (size);
9337       else if (unformat (input, "global-pvt-heap-size %lldM", &pvt_heap_size))
9338         vl_set_global_pvt_heap_size (pvt_heap_size * (1ULL << 20));
9339       else if (unformat (input, "global-pvt-heap-size size %lld",
9340                          &pvt_heap_size))
9341         vl_set_global_pvt_heap_size (pvt_heap_size);
9342       else if (unformat (input, "api-pvt-heap-size %lldM", &pvt_heap_size))
9343         vl_set_api_pvt_heap_size (pvt_heap_size * (1ULL << 20));
9344       else if (unformat (input, "api-pvt-heap-size size %lld",
9345                          &pvt_heap_size))
9346         vl_set_api_pvt_heap_size (pvt_heap_size);
9347       else if (unformat (input, "api-size %lldM", &size))
9348         vl_set_api_memory_size (size * (1ULL << 20));
9349       else if (unformat (input, "api-size %lldG", &size))
9350         vl_set_api_memory_size (size * (1ULL << 30));
9351       else if (unformat (input, "api-size %lld", &size))
9352         vl_set_api_memory_size (size);
9353       else if (unformat (input, "uid %s", &s))
9354         {
9355           /* lookup the username */
9356           pw = NULL;
9357           while (((rv =
9358                    getpwnam_r (s, &_pw, buf, vec_len (buf), &pw)) == ERANGE)
9359                  && (vec_len (buf) <= max_buf_size))
9360             {
9361               vec_resize (buf, vec_len (buf) * 2);
9362             }
9363           if (rv < 0)
9364             {
9365               e = clib_error_return_code (0, rv,
9366                                           CLIB_ERROR_ERRNO_VALID |
9367                                           CLIB_ERROR_FATAL,
9368                                           "cannot fetch username %s", s);
9369               vec_free (s);
9370               vec_free (buf);
9371               return e;
9372             }
9373           if (pw == NULL)
9374             {
9375               e =
9376                 clib_error_return_fatal (0, "username %s does not exist", s);
9377               vec_free (s);
9378               vec_free (buf);
9379               return e;
9380             }
9381           vec_free (s);
9382           vl_set_memory_uid (pw->pw_uid);
9383         }
9384       else if (unformat (input, "gid %s", &s))
9385         {
9386           /* lookup the group name */
9387           grp = NULL;
9388           while (((rv =
9389                    getgrnam_r (s, &_grp, buf, vec_len (buf), &grp)) == ERANGE)
9390                  && (vec_len (buf) <= max_buf_size))
9391             {
9392               vec_resize (buf, vec_len (buf) * 2);
9393             }
9394           if (rv != 0)
9395             {
9396               e = clib_error_return_code (0, rv,
9397                                           CLIB_ERROR_ERRNO_VALID |
9398                                           CLIB_ERROR_FATAL,
9399                                           "cannot fetch group %s", s);
9400               vec_free (s);
9401               vec_free (buf);
9402               return e;
9403             }
9404           if (grp == NULL)
9405             {
9406               e = clib_error_return_fatal (0, "group %s does not exist", s);
9407               vec_free (s);
9408               vec_free (buf);
9409               return e;
9410             }
9411           vec_free (s);
9412           vec_free (buf);
9413           vl_set_memory_gid (grp->gr_gid);
9414         }
9415       else
9416         return clib_error_return (0, "unknown input `%U'",
9417                                   format_unformat_error, input);
9418     }
9419   return 0;
9420 }
9421
9422 VLIB_EARLY_CONFIG_FUNCTION (api_segment_config, "api-segment");
9423
9424 void *
9425 get_unformat_vnet_sw_interface (void)
9426 {
9427   return (void *) &unformat_vnet_sw_interface;
9428 }
9429
9430 #undef vl_api_version
9431 #define vl_api_version(n,v) static u32 vpe_api_version = v;
9432 #include <vpp-api/vpe.api.h>
9433 #undef vl_api_version
9434
9435 int
9436 vl_msg_api_version_check (vl_api_memclnt_create_t * mp)
9437 {
9438   if (clib_host_to_net_u32 (mp->api_versions[0]) != vpe_api_version)
9439     {
9440       clib_warning ("vpe API mismatch: 0x%08x instead of 0x%08x",
9441                     clib_host_to_net_u32 (mp->api_versions[0]),
9442                     vpe_api_version);
9443       return -1;
9444     }
9445   return 0;
9446 }
9447
9448 static u8 *
9449 format_arp_event (u8 * s, va_list * args)
9450 {
9451   vl_api_ip4_arp_event_t *event = va_arg (*args, vl_api_ip4_arp_event_t *);
9452
9453   s = format (s, "pid %d: ", event->pid);
9454   if (event->mac_ip)
9455     s = format (s, "bd mac/ip4 binding events");
9456   else
9457     s = format (s, "resolution for %U", format_ip4_address, &event->address);
9458   return s;
9459 }
9460
9461 static u8 *
9462 format_nd_event (u8 * s, va_list * args)
9463 {
9464   vl_api_ip6_nd_event_t *event = va_arg (*args, vl_api_ip6_nd_event_t *);
9465
9466   s = format (s, "pid %d: ", event->pid);
9467   if (event->mac_ip)
9468     s = format (s, "bd mac/ip6 binding events");
9469   else
9470     s = format (s, "resolution for %U", format_ip6_address, event->address);
9471   return s;
9472 }
9473
9474 static clib_error_t *
9475 show_ip_arp_nd_events_fn (vlib_main_t * vm,
9476                           unformat_input_t * input, vlib_cli_command_t * cmd)
9477 {
9478   vpe_api_main_t *am = &vpe_api_main;
9479   vl_api_ip4_arp_event_t *arp_event;
9480   vl_api_ip6_nd_event_t *nd_event;
9481
9482   if ((pool_elts (am->arp_events) == 0) && (pool_elts (am->nd_events) == 0))
9483     {
9484       vlib_cli_output (vm, "No active arp or nd event registrations");
9485       return 0;
9486     }
9487
9488   /* *INDENT-OFF* */
9489   pool_foreach (arp_event, am->arp_events,
9490   ({
9491     vlib_cli_output (vm, "%U", format_arp_event, arp_event);
9492   }));
9493
9494   pool_foreach (nd_event, am->nd_events,
9495   ({
9496     vlib_cli_output (vm, "%U", format_nd_event, nd_event);
9497   }));
9498   /* *INDENT-ON* */
9499
9500   return 0;
9501 }
9502
9503 /* *INDENT-OFF* */
9504 VLIB_CLI_COMMAND (show_ip_arp_nd_events, static) = {
9505   .path = "show arp-nd-event registrations",
9506   .function = show_ip_arp_nd_events_fn,
9507   .short_help = "Show ip4 arp and ip6 nd event registrations",
9508 };
9509 /* *INDENT-ON* */
9510
9511 /*
9512  * fd.io coding-style-patch-verification: ON
9513  *
9514  * Local Variables:
9515  * eval: (c-set-style "gnu")
9516  * End:
9517  */