span: add feature (rx only) (VPP-185)
[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
3927 static void vl_api_classify_add_del_table_t_handler
3928   (vl_api_classify_add_del_table_t * mp)
3929 {
3930   vl_api_classify_add_del_table_reply_t *rmp;
3931   vnet_classify_main_t *cm = &vnet_classify_main;
3932   vnet_classify_table_t *t;
3933   int rv;
3934
3935 #define _(a) u32 a;
3936   foreach_classify_add_del_table_field;
3937 #undef _
3938
3939 #define _(a) a = ntohl(mp->a);
3940   foreach_classify_add_del_table_field;
3941 #undef _
3942
3943   /* The underlying API fails silently, on purpose, so check here */
3944   if (mp->is_add == 0)
3945     if (pool_is_free_index (cm->tables, table_index))
3946       {
3947         rv = VNET_API_ERROR_NO_SUCH_TABLE;
3948         goto out;
3949       }
3950
3951   rv = vnet_classify_add_del_table
3952     (cm, mp->mask, nbuckets, memory_size,
3953      skip_n_vectors, match_n_vectors,
3954      next_table_index, miss_next_index, &table_index, mp->is_add);
3955
3956 out:
3957   /* *INDENT-OFF* */
3958   REPLY_MACRO2(VL_API_CLASSIFY_ADD_DEL_TABLE_REPLY,
3959   ({
3960     if (rv == 0 && mp->is_add)
3961       {
3962         t = pool_elt_at_index (cm->tables, table_index);
3963         rmp->skip_n_vectors = ntohl(t->skip_n_vectors);
3964         rmp->match_n_vectors = ntohl(t->match_n_vectors);
3965         rmp->new_table_index = ntohl(table_index);
3966       }
3967     else
3968       {
3969         rmp->skip_n_vectors = ~0;
3970         rmp->match_n_vectors = ~0;
3971         rmp->new_table_index = ~0;
3972       }
3973   }));
3974   /* *INDENT-ON* */
3975 }
3976
3977 static void vl_api_classify_add_del_session_t_handler
3978   (vl_api_classify_add_del_session_t * mp)
3979 {
3980   vnet_classify_main_t *cm = &vnet_classify_main;
3981   vl_api_classify_add_del_session_reply_t *rmp;
3982   int rv;
3983   u32 table_index, hit_next_index, opaque_index;
3984   i32 advance;
3985
3986   table_index = ntohl (mp->table_index);
3987   hit_next_index = ntohl (mp->hit_next_index);
3988   opaque_index = ntohl (mp->opaque_index);
3989   advance = ntohl (mp->advance);
3990
3991   rv = vnet_classify_add_del_session
3992     (cm, table_index, mp->match, hit_next_index, opaque_index,
3993      advance, mp->is_add);
3994
3995   REPLY_MACRO (VL_API_CLASSIFY_ADD_DEL_SESSION_REPLY);
3996 }
3997
3998 static void vl_api_classify_set_interface_ip_table_t_handler
3999   (vl_api_classify_set_interface_ip_table_t * mp)
4000 {
4001   vlib_main_t *vm = vlib_get_main ();
4002   vl_api_classify_set_interface_ip_table_reply_t *rmp;
4003   int rv;
4004   u32 table_index, sw_if_index;
4005
4006   table_index = ntohl (mp->table_index);
4007   sw_if_index = ntohl (mp->sw_if_index);
4008
4009   VALIDATE_SW_IF_INDEX (mp);
4010
4011   if (mp->is_ipv6)
4012     rv = vnet_set_ip6_classify_intfc (vm, sw_if_index, table_index);
4013   else
4014     rv = vnet_set_ip4_classify_intfc (vm, sw_if_index, table_index);
4015
4016   BAD_SW_IF_INDEX_LABEL;
4017
4018   REPLY_MACRO (VL_API_CLASSIFY_SET_INTERFACE_IP_TABLE_REPLY);
4019 }
4020
4021 static void vl_api_classify_set_interface_l2_tables_t_handler
4022   (vl_api_classify_set_interface_l2_tables_t * mp)
4023 {
4024   vl_api_classify_set_interface_l2_tables_reply_t *rmp;
4025   int rv;
4026   u32 sw_if_index, ip4_table_index, ip6_table_index, other_table_index;
4027   int enable;
4028
4029   ip4_table_index = ntohl (mp->ip4_table_index);
4030   ip6_table_index = ntohl (mp->ip6_table_index);
4031   other_table_index = ntohl (mp->other_table_index);
4032   sw_if_index = ntohl (mp->sw_if_index);
4033
4034   VALIDATE_SW_IF_INDEX (mp);
4035
4036   if (mp->is_input)
4037     rv = vnet_l2_input_classify_set_tables (sw_if_index, ip4_table_index,
4038                                             ip6_table_index,
4039                                             other_table_index);
4040   else
4041     rv = vnet_l2_output_classify_set_tables (sw_if_index, ip4_table_index,
4042                                              ip6_table_index,
4043                                              other_table_index);
4044
4045   if (rv == 0)
4046     {
4047       if (ip4_table_index != ~0 || ip6_table_index != ~0
4048           || other_table_index != ~0)
4049         enable = 1;
4050       else
4051         enable = 0;
4052
4053       if (mp->is_input)
4054         vnet_l2_input_classify_enable_disable (sw_if_index, enable);
4055       else
4056         vnet_l2_output_classify_enable_disable (sw_if_index, enable);
4057     }
4058
4059   BAD_SW_IF_INDEX_LABEL;
4060
4061   REPLY_MACRO (VL_API_CLASSIFY_SET_INTERFACE_L2_TABLES_REPLY);
4062 }
4063
4064 static void
4065 vl_api_l2_fib_clear_table_t_handler (vl_api_l2_fib_clear_table_t * mp)
4066 {
4067   int rv = 0;
4068   vl_api_l2_fib_clear_table_reply_t *rmp;
4069
4070   /* DAW-FIXME: This API should only clear non-static l2fib entries, but
4071    *            that is not currently implemented.  When that TODO is fixed
4072    *            this call should be changed to pass 1 instead of 0.
4073    */
4074   l2fib_clear_table (0);
4075
4076   REPLY_MACRO (VL_API_L2_FIB_CLEAR_TABLE_REPLY);
4077 }
4078
4079 extern void l2_efp_filter_configure (vnet_main_t * vnet_main,
4080                                      u32 sw_if_index, u32 enable);
4081
4082 static void
4083 vl_api_l2_interface_efp_filter_t_handler (vl_api_l2_interface_efp_filter_t *
4084                                           mp)
4085 {
4086   int rv;
4087   vl_api_l2_interface_efp_filter_reply_t *rmp;
4088   vnet_main_t *vnm = vnet_get_main ();
4089
4090   // enable/disable the feature
4091   l2_efp_filter_configure (vnm, mp->sw_if_index, mp->enable_disable);
4092   rv = vnm->api_errno;
4093
4094   REPLY_MACRO (VL_API_L2_INTERFACE_EFP_FILTER_REPLY);
4095 }
4096
4097 static void
4098   vl_api_l2_interface_vlan_tag_rewrite_t_handler
4099   (vl_api_l2_interface_vlan_tag_rewrite_t * mp)
4100 {
4101   int rv = 0;
4102   vl_api_l2_interface_vlan_tag_rewrite_reply_t *rmp;
4103   vnet_main_t *vnm = vnet_get_main ();
4104   vlib_main_t *vm = vlib_get_main ();
4105   u32 vtr_op;
4106
4107   VALIDATE_SW_IF_INDEX (mp);
4108
4109   vtr_op = ntohl (mp->vtr_op);
4110
4111   /* The L2 code is unsuspicious */
4112   switch (vtr_op)
4113     {
4114     case L2_VTR_DISABLED:
4115     case L2_VTR_PUSH_1:
4116     case L2_VTR_PUSH_2:
4117     case L2_VTR_POP_1:
4118     case L2_VTR_POP_2:
4119     case L2_VTR_TRANSLATE_1_1:
4120     case L2_VTR_TRANSLATE_1_2:
4121     case L2_VTR_TRANSLATE_2_1:
4122     case L2_VTR_TRANSLATE_2_2:
4123       break;
4124
4125     default:
4126       rv = VNET_API_ERROR_INVALID_VALUE;
4127       goto bad_sw_if_index;
4128     }
4129
4130   rv = l2vtr_configure (vm, vnm, ntohl (mp->sw_if_index), vtr_op,
4131                         ntohl (mp->push_dot1q), ntohl (mp->tag1),
4132                         ntohl (mp->tag2));
4133
4134   BAD_SW_IF_INDEX_LABEL;
4135
4136   REPLY_MACRO (VL_API_L2_INTERFACE_VLAN_TAG_REWRITE_REPLY);
4137 }
4138
4139 static void
4140 vl_api_create_vhost_user_if_t_handler (vl_api_create_vhost_user_if_t * mp)
4141 {
4142   int rv = 0;
4143   vl_api_create_vhost_user_if_reply_t *rmp;
4144   u32 sw_if_index = (u32) ~ 0;
4145
4146   vnet_main_t *vnm = vnet_get_main ();
4147   vlib_main_t *vm = vlib_get_main ();
4148
4149   rv = vhost_user_create_if (vnm, vm, (char *) mp->sock_filename,
4150                              mp->is_server, &sw_if_index, (u64) ~ 0,
4151                              mp->renumber, ntohl (mp->custom_dev_instance),
4152                              (mp->use_custom_mac) ? mp->mac_address : NULL);
4153
4154   /* *INDENT-OFF* */
4155   REPLY_MACRO2(VL_API_CREATE_VHOST_USER_IF_REPLY,
4156   ({
4157     rmp->sw_if_index = ntohl (sw_if_index);
4158   }));
4159   /* *INDENT-ON* */
4160 }
4161
4162 static void
4163 vl_api_modify_vhost_user_if_t_handler (vl_api_modify_vhost_user_if_t * mp)
4164 {
4165   int rv = 0;
4166   vl_api_modify_vhost_user_if_reply_t *rmp;
4167   u32 sw_if_index = ntohl (mp->sw_if_index);
4168
4169   vnet_main_t *vnm = vnet_get_main ();
4170   vlib_main_t *vm = vlib_get_main ();
4171
4172   rv = vhost_user_modify_if (vnm, vm, (char *) mp->sock_filename,
4173                              mp->is_server, sw_if_index, (u64) ~ 0,
4174                              mp->renumber, ntohl (mp->custom_dev_instance));
4175   REPLY_MACRO (VL_API_MODIFY_VHOST_USER_IF_REPLY);
4176 }
4177
4178 static void
4179 vl_api_delete_vhost_user_if_t_handler (vl_api_delete_vhost_user_if_t * mp)
4180 {
4181   int rv = 0;
4182   vl_api_delete_vhost_user_if_reply_t *rmp;
4183   vpe_api_main_t *vam = &vpe_api_main;
4184   u32 sw_if_index = ntohl (mp->sw_if_index);
4185
4186   vnet_main_t *vnm = vnet_get_main ();
4187   vlib_main_t *vm = vlib_get_main ();
4188
4189   rv = vhost_user_delete_if (vnm, vm, sw_if_index);
4190
4191   REPLY_MACRO (VL_API_DELETE_VHOST_USER_IF_REPLY);
4192   if (!rv)
4193     {
4194       unix_shared_memory_queue_t *q =
4195         vl_api_client_index_to_input_queue (mp->client_index);
4196       if (!q)
4197         return;
4198
4199       send_sw_interface_flags_deleted (vam, q, sw_if_index);
4200     }
4201 }
4202
4203 static void
4204   vl_api_sw_interface_vhost_user_details_t_handler
4205   (vl_api_sw_interface_vhost_user_details_t * mp)
4206 {
4207   clib_warning ("BUG");
4208 }
4209
4210 static void
4211 send_sw_interface_vhost_user_details (vpe_api_main_t * am,
4212                                       unix_shared_memory_queue_t * q,
4213                                       vhost_user_intf_details_t * vui,
4214                                       u32 context)
4215 {
4216   vl_api_sw_interface_vhost_user_details_t *mp;
4217
4218   mp = vl_msg_api_alloc (sizeof (*mp));
4219   memset (mp, 0, sizeof (*mp));
4220   mp->_vl_msg_id = ntohs (VL_API_SW_INTERFACE_VHOST_USER_DETAILS);
4221   mp->sw_if_index = ntohl (vui->sw_if_index);
4222   mp->virtio_net_hdr_sz = ntohl (vui->virtio_net_hdr_sz);
4223   mp->features = clib_net_to_host_u64 (vui->features);
4224   mp->is_server = vui->is_server;
4225   mp->num_regions = ntohl (vui->num_regions);
4226   mp->sock_errno = ntohl (vui->sock_errno);
4227   mp->context = context;
4228
4229   strncpy ((char *) mp->sock_filename,
4230            (char *) vui->sock_filename, ARRAY_LEN (mp->sock_filename) - 1);
4231   strncpy ((char *) mp->interface_name,
4232            (char *) vui->if_name, ARRAY_LEN (mp->interface_name) - 1);
4233
4234   vl_msg_api_send_shmem (q, (u8 *) & mp);
4235 }
4236
4237 static void
4238   vl_api_sw_interface_vhost_user_dump_t_handler
4239   (vl_api_sw_interface_vhost_user_dump_t * mp)
4240 {
4241   int rv = 0;
4242   vpe_api_main_t *am = &vpe_api_main;
4243   vnet_main_t *vnm = vnet_get_main ();
4244   vlib_main_t *vm = vlib_get_main ();
4245   vhost_user_intf_details_t *ifaces = NULL;
4246   vhost_user_intf_details_t *vuid = NULL;
4247   unix_shared_memory_queue_t *q;
4248
4249   q = vl_api_client_index_to_input_queue (mp->client_index);
4250   if (q == 0)
4251     return;
4252
4253   rv = vhost_user_dump_ifs (vnm, vm, &ifaces);
4254   if (rv)
4255     return;
4256
4257   vec_foreach (vuid, ifaces)
4258   {
4259     send_sw_interface_vhost_user_details (am, q, vuid, mp->context);
4260   }
4261   vec_free (ifaces);
4262 }
4263
4264 static void
4265 send_sw_if_l2tpv3_tunnel_details (vpe_api_main_t * am,
4266                                   unix_shared_memory_queue_t * q,
4267                                   l2t_session_t * s,
4268                                   l2t_main_t * lm, u32 context)
4269 {
4270   vl_api_sw_if_l2tpv3_tunnel_details_t *mp;
4271   u8 *if_name = NULL;
4272   vnet_sw_interface_t *si = NULL;
4273
4274   si = vnet_get_hw_sw_interface (lm->vnet_main, s->hw_if_index);
4275
4276   if_name = format (if_name, "%U",
4277                     format_vnet_sw_interface_name, lm->vnet_main, si);
4278
4279   mp = vl_msg_api_alloc (sizeof (*mp));
4280   memset (mp, 0, sizeof (*mp));
4281   mp->_vl_msg_id = ntohs (VL_API_SW_IF_L2TPV3_TUNNEL_DETAILS);
4282   strncpy ((char *) mp->interface_name,
4283            (char *) if_name, ARRAY_LEN (mp->interface_name) - 1);
4284   mp->sw_if_index = ntohl (si->sw_if_index);
4285   mp->local_session_id = s->local_session_id;
4286   mp->remote_session_id = s->remote_session_id;
4287   mp->local_cookie[0] = s->local_cookie[0];
4288   mp->local_cookie[1] = s->local_cookie[1];
4289   mp->remote_cookie = s->remote_cookie;
4290   clib_memcpy (mp->client_address, &s->client_address,
4291                sizeof (s->client_address));
4292   clib_memcpy (mp->our_address, &s->our_address, sizeof (s->our_address));
4293   mp->l2_sublayer_present = s->l2_sublayer_present;
4294   mp->context = context;
4295
4296   vl_msg_api_send_shmem (q, (u8 *) & mp);
4297 }
4298
4299 static void
4300 send_ip_address_details (vpe_api_main_t * am,
4301                          unix_shared_memory_queue_t * q,
4302                          u8 * ip, u16 prefix_length, u8 is_ipv6, u32 context)
4303 {
4304   vl_api_ip_address_details_t *mp;
4305
4306   mp = vl_msg_api_alloc (sizeof (*mp));
4307   memset (mp, 0, sizeof (*mp));
4308   mp->_vl_msg_id = ntohs (VL_API_IP_ADDRESS_DETAILS);
4309
4310   if (is_ipv6)
4311     {
4312       clib_memcpy (&mp->ip, ip, sizeof (mp->ip));
4313     }
4314   else
4315     {
4316       u32 *tp = (u32 *) mp->ip;
4317       *tp = ntohl (*(u32 *) ip);
4318     }
4319   mp->prefix_length = prefix_length;
4320   mp->context = context;
4321
4322   vl_msg_api_send_shmem (q, (u8 *) & mp);
4323 }
4324
4325 static void
4326 vl_api_ip_address_dump_t_handler (vl_api_ip_address_dump_t * mp)
4327 {
4328   vpe_api_main_t *am = &vpe_api_main;
4329   unix_shared_memory_queue_t *q;
4330   ip6_address_t *r6;
4331   ip4_address_t *r4;
4332   ip6_main_t *im6 = &ip6_main;
4333   ip4_main_t *im4 = &ip4_main;
4334   ip_lookup_main_t *lm6 = &im6->lookup_main;
4335   ip_lookup_main_t *lm4 = &im4->lookup_main;
4336   ip_interface_address_t *ia = 0;
4337   u32 sw_if_index = ~0;
4338   int rv __attribute__ ((unused)) = 0;
4339
4340   VALIDATE_SW_IF_INDEX (mp);
4341
4342   sw_if_index = ntohl (mp->sw_if_index);
4343
4344   q = vl_api_client_index_to_input_queue (mp->client_index);
4345   if (q == 0)
4346     {
4347       return;
4348     }
4349
4350   if (mp->is_ipv6)
4351     {
4352       /* *INDENT-OFF* */
4353       foreach_ip_interface_address (lm6, ia, sw_if_index,
4354                                     1 /* honor unnumbered */,
4355       ({
4356         r6 = ip_interface_address_get_address (lm6, ia);
4357         u16 prefix_length = ia->address_length;
4358         send_ip_address_details(am, q, (u8*)r6, prefix_length, 1, mp->context);
4359       }));
4360       /* *INDENT-ON* */
4361     }
4362   else
4363     {
4364       /* *INDENT-OFF* */
4365       foreach_ip_interface_address (lm4, ia, sw_if_index,
4366                                     1 /* honor unnumbered */,
4367       ({
4368         r4 = ip_interface_address_get_address (lm4, ia);
4369         u16 prefix_length = ia->address_length;
4370         send_ip_address_details(am, q, (u8*)r4, prefix_length, 0, mp->context);
4371       }));
4372       /* *INDENT-ON* */
4373     }
4374   BAD_SW_IF_INDEX_LABEL;
4375 }
4376
4377 static void
4378 send_ip_details (vpe_api_main_t * am,
4379                  unix_shared_memory_queue_t * q, u32 sw_if_index, u32 context)
4380 {
4381   vl_api_ip_details_t *mp;
4382
4383   mp = vl_msg_api_alloc (sizeof (*mp));
4384   memset (mp, 0, sizeof (*mp));
4385   mp->_vl_msg_id = ntohs (VL_API_IP_DETAILS);
4386
4387   mp->sw_if_index = ntohl (sw_if_index);
4388   mp->context = context;
4389
4390   vl_msg_api_send_shmem (q, (u8 *) & mp);
4391 }
4392
4393 static void
4394 vl_api_sw_if_l2tpv3_tunnel_dump_t_handler (vl_api_sw_if_l2tpv3_tunnel_dump_t *
4395                                            mp)
4396 {
4397   vpe_api_main_t *am = &vpe_api_main;
4398   l2t_main_t *lm = &l2t_main;
4399   unix_shared_memory_queue_t *q;
4400   l2t_session_t *session;
4401
4402   q = vl_api_client_index_to_input_queue (mp->client_index);
4403   if (q == 0)
4404     return;
4405
4406   /* *INDENT-OFF* */
4407   pool_foreach (session, lm->sessions,
4408   ({
4409     send_sw_if_l2tpv3_tunnel_details (am, q, session, lm, mp->context);
4410   }));
4411   /* *INDENT-ON* */
4412 }
4413
4414
4415 static void
4416 send_sw_interface_tap_details (vpe_api_main_t * am,
4417                                unix_shared_memory_queue_t * q,
4418                                tapcli_interface_details_t * tap_if,
4419                                u32 context)
4420 {
4421   vl_api_sw_interface_tap_details_t *mp;
4422   mp = vl_msg_api_alloc (sizeof (*mp));
4423   memset (mp, 0, sizeof (*mp));
4424   mp->_vl_msg_id = ntohs (VL_API_SW_INTERFACE_TAP_DETAILS);
4425   mp->sw_if_index = ntohl (tap_if->sw_if_index);
4426   strncpy ((char *) mp->dev_name,
4427            (char *) tap_if->dev_name, ARRAY_LEN (mp->dev_name) - 1);
4428   mp->context = context;
4429
4430   vl_msg_api_send_shmem (q, (u8 *) & mp);
4431 }
4432
4433 static void
4434 vl_api_sw_interface_tap_dump_t_handler (vl_api_sw_interface_tap_dump_t * mp)
4435 {
4436   int rv = 0;
4437   vpe_api_main_t *am = &vpe_api_main;
4438   unix_shared_memory_queue_t *q;
4439   tapcli_interface_details_t *tapifs = NULL;
4440   tapcli_interface_details_t *tap_if = NULL;
4441
4442   q = vl_api_client_index_to_input_queue (mp->client_index);
4443   if (q == 0)
4444     return;
4445
4446   rv = vnet_tap_dump_ifs (&tapifs);
4447   if (rv)
4448     return;
4449
4450   vec_foreach (tap_if, tapifs)
4451   {
4452     send_sw_interface_tap_details (am, q, tap_if, mp->context);
4453   }
4454
4455   vec_free (tapifs);
4456 }
4457
4458 static void
4459 vl_api_ip_dump_t_handler (vl_api_ip_dump_t * mp)
4460 {
4461   vpe_api_main_t *am = &vpe_api_main;
4462   vnet_main_t *vnm = vnet_get_main ();
4463   vlib_main_t *vm = vlib_get_main ();
4464   vnet_interface_main_t *im = &vnm->interface_main;
4465   unix_shared_memory_queue_t *q;
4466   vnet_sw_interface_t *si, *sorted_sis;
4467   u32 sw_if_index = ~0;
4468
4469   q = vl_api_client_index_to_input_queue (mp->client_index);
4470   if (q == 0)
4471     {
4472       return;
4473     }
4474
4475   /* Gather interfaces. */
4476   sorted_sis = vec_new (vnet_sw_interface_t, pool_elts (im->sw_interfaces));
4477   _vec_len (sorted_sis) = 0;
4478   /* *INDENT-OFF* */
4479   pool_foreach (si, im->sw_interfaces,
4480   ({
4481     vec_add1 (sorted_sis, si[0]);
4482   }));
4483   /* *INDENT-ON* */
4484
4485   vec_foreach (si, sorted_sis)
4486   {
4487     if (!(si->flags & VNET_SW_INTERFACE_FLAG_UNNUMBERED))
4488       {
4489         if (mp->is_ipv6 && !ip6_interface_enabled (vm, si->sw_if_index))
4490           {
4491             continue;
4492           }
4493         sw_if_index = si->sw_if_index;
4494         send_ip_details (am, q, sw_if_index, mp->context);
4495       }
4496   }
4497 }
4498
4499 static void
4500 vl_api_l2_fib_table_entry_t_handler (vl_api_l2_fib_table_entry_t * mp)
4501 {
4502   clib_warning ("BUG");
4503 }
4504
4505 static void
4506 send_l2fib_table_entry (vpe_api_main_t * am,
4507                         unix_shared_memory_queue_t * q,
4508                         l2fib_entry_key_t * l2fe_key,
4509                         l2fib_entry_result_t * l2fe_res, u32 context)
4510 {
4511   vl_api_l2_fib_table_entry_t *mp;
4512
4513   mp = vl_msg_api_alloc (sizeof (*mp));
4514   memset (mp, 0, sizeof (*mp));
4515   mp->_vl_msg_id = ntohs (VL_API_L2_FIB_TABLE_ENTRY);
4516
4517   mp->bd_id =
4518     ntohl (l2input_main.bd_configs[l2fe_key->fields.bd_index].bd_id);
4519
4520   mp->mac = l2fib_make_key (l2fe_key->fields.mac, 0);
4521   mp->sw_if_index = ntohl (l2fe_res->fields.sw_if_index);
4522   mp->static_mac = l2fe_res->fields.static_mac;
4523   mp->filter_mac = l2fe_res->fields.filter;
4524   mp->bvi_mac = l2fe_res->fields.bvi;
4525   mp->context = context;
4526
4527   vl_msg_api_send_shmem (q, (u8 *) & mp);
4528 }
4529
4530 static void
4531 vl_api_l2_fib_table_dump_t_handler (vl_api_l2_fib_table_dump_t * mp)
4532 {
4533   vpe_api_main_t *am = &vpe_api_main;
4534   bd_main_t *bdm = &bd_main;
4535   l2fib_entry_key_t *l2fe_key = NULL;
4536   l2fib_entry_result_t *l2fe_res = NULL;
4537   u32 ni, bd_id = ntohl (mp->bd_id);
4538   u32 bd_index;
4539   unix_shared_memory_queue_t *q;
4540   uword *p;
4541
4542   q = vl_api_client_index_to_input_queue (mp->client_index);
4543   if (q == 0)
4544     return;
4545
4546   /* see l2fib_table_dump: ~0 means "any" */
4547   if (bd_id == ~0)
4548     bd_index = ~0;
4549   else
4550     {
4551       p = hash_get (bdm->bd_index_by_bd_id, bd_id);
4552       if (p == 0)
4553         return;
4554
4555       bd_index = p[0];
4556     }
4557
4558   l2fib_table_dump (bd_index, &l2fe_key, &l2fe_res);
4559
4560   vec_foreach_index (ni, l2fe_key)
4561   {
4562     send_l2fib_table_entry (am, q, vec_elt_at_index (l2fe_key, ni),
4563                             vec_elt_at_index (l2fe_res, ni), mp->context);
4564   }
4565   vec_free (l2fe_key);
4566   vec_free (l2fe_res);
4567 }
4568
4569 static void
4570 vl_api_show_version_t_handler (vl_api_show_version_t * mp)
4571 {
4572   vl_api_show_version_reply_t *rmp;
4573   int rv = 0;
4574   char *vpe_api_get_build_directory (void);
4575   char *vpe_api_get_version (void);
4576   char *vpe_api_get_build_date (void);
4577
4578   unix_shared_memory_queue_t *q =
4579     vl_api_client_index_to_input_queue (mp->client_index);
4580
4581   if (!q)
4582     return;
4583
4584   /* *INDENT-OFF* */
4585   REPLY_MACRO2(VL_API_SHOW_VERSION_REPLY,
4586   ({
4587     strncpy ((char *) rmp->program, "vpe", ARRAY_LEN(rmp->program)-1);
4588     strncpy ((char *) rmp->build_directory, vpe_api_get_build_directory(),
4589              ARRAY_LEN(rmp->build_directory)-1);
4590     strncpy ((char *) rmp->version, vpe_api_get_version(),
4591              ARRAY_LEN(rmp->version)-1);
4592     strncpy ((char *) rmp->build_date, vpe_api_get_build_date(),
4593              ARRAY_LEN(rmp->build_date)-1);
4594   }));
4595   /* *INDENT-ON* */
4596 }
4597
4598 static void
4599 vl_api_get_node_index_t_handler (vl_api_get_node_index_t * mp)
4600 {
4601   vlib_main_t *vm = vlib_get_main ();
4602   vl_api_get_node_index_reply_t *rmp;
4603   vlib_node_t *n;
4604   int rv = 0;
4605   u32 node_index = ~0;
4606
4607   n = vlib_get_node_by_name (vm, mp->node_name);
4608
4609   if (n == 0)
4610     rv = VNET_API_ERROR_NO_SUCH_NODE;
4611   else
4612     node_index = n->index;
4613
4614   /* *INDENT-OFF* */
4615   REPLY_MACRO2(VL_API_GET_NODE_INDEX_REPLY,
4616   ({
4617     rmp->node_index = ntohl(node_index);
4618   }));
4619   /* *INDENT-ON* */
4620 }
4621
4622 static void
4623 vl_api_get_next_index_t_handler (vl_api_get_next_index_t * mp)
4624 {
4625   vlib_main_t *vm = vlib_get_main ();
4626   vl_api_get_next_index_reply_t *rmp;
4627   vlib_node_t *node, *next_node;
4628   int rv = 0;
4629   u32 next_node_index = ~0, next_index = ~0;
4630   uword *p;
4631
4632   node = vlib_get_node_by_name (vm, mp->node_name);
4633
4634   if (node == 0)
4635     {
4636       rv = VNET_API_ERROR_NO_SUCH_NODE;
4637       goto out;
4638     }
4639
4640   next_node = vlib_get_node_by_name (vm, mp->next_name);
4641
4642   if (next_node == 0)
4643     {
4644       rv = VNET_API_ERROR_NO_SUCH_NODE2;
4645       goto out;
4646     }
4647   else
4648     next_node_index = next_node->index;
4649
4650   p = hash_get (node->next_slot_by_node, next_node_index);
4651
4652   if (p == 0)
4653     {
4654       rv = VNET_API_ERROR_NO_SUCH_ENTRY;
4655       goto out;
4656     }
4657   else
4658     next_index = p[0];
4659
4660 out:
4661   /* *INDENT-OFF* */
4662   REPLY_MACRO2(VL_API_GET_NEXT_INDEX_REPLY,
4663   ({
4664     rmp->next_index = ntohl(next_index);
4665   }));
4666   /* *INDENT-ON* */
4667 }
4668
4669 static void
4670 vl_api_add_node_next_t_handler (vl_api_add_node_next_t * mp)
4671 {
4672   vlib_main_t *vm = vlib_get_main ();
4673   vl_api_add_node_next_reply_t *rmp;
4674   vlib_node_t *n, *next;
4675   int rv = 0;
4676   u32 next_index = ~0;
4677
4678   n = vlib_get_node_by_name (vm, mp->node_name);
4679
4680   if (n == 0)
4681     {
4682       rv = VNET_API_ERROR_NO_SUCH_NODE;
4683       goto out;
4684     }
4685
4686   next = vlib_get_node_by_name (vm, mp->next_name);
4687
4688   if (next == 0)
4689     rv = VNET_API_ERROR_NO_SUCH_NODE2;
4690   else
4691     next_index = vlib_node_add_next (vm, n->index, next->index);
4692
4693 out:
4694   /* *INDENT-OFF* */
4695   REPLY_MACRO2(VL_API_GET_NODE_INDEX_REPLY,
4696   ({
4697     rmp->next_index = ntohl(next_index);
4698   }));
4699   /* *INDENT-ON* */
4700 }
4701
4702 static void vl_api_l2tpv3_create_tunnel_t_handler
4703   (vl_api_l2tpv3_create_tunnel_t * mp)
4704 {
4705   vl_api_l2tpv3_create_tunnel_reply_t *rmp;
4706   l2t_main_t *lm = &l2t_main;
4707   u32 sw_if_index = (u32) ~ 0;
4708   int rv;
4709
4710   if (mp->is_ipv6 != 1)
4711     {
4712       rv = VNET_API_ERROR_UNIMPLEMENTED;
4713       goto out;
4714     }
4715
4716   u32 encap_fib_index;
4717
4718   if (mp->encap_vrf_id != ~0)
4719     {
4720       uword *p;
4721       ip6_main_t *im = &ip6_main;
4722       if (!
4723           (p =
4724            hash_get (im->fib_index_by_table_id, ntohl (mp->encap_vrf_id))))
4725         {
4726           rv = VNET_API_ERROR_NO_SUCH_FIB;
4727           goto out;
4728         }
4729       encap_fib_index = p[0];
4730     }
4731   else
4732     {
4733       encap_fib_index = ~0;
4734     }
4735
4736   rv = create_l2tpv3_ipv6_tunnel (lm,
4737                                   (ip6_address_t *) mp->client_address,
4738                                   (ip6_address_t *) mp->our_address,
4739                                   ntohl (mp->local_session_id),
4740                                   ntohl (mp->remote_session_id),
4741                                   clib_net_to_host_u64 (mp->local_cookie),
4742                                   clib_net_to_host_u64 (mp->remote_cookie),
4743                                   mp->l2_sublayer_present,
4744                                   encap_fib_index, &sw_if_index);
4745
4746 out:
4747   /* *INDENT-OFF* */
4748   REPLY_MACRO2(VL_API_L2TPV3_CREATE_TUNNEL_REPLY,
4749   ({
4750     rmp->sw_if_index = ntohl (sw_if_index);
4751   }));
4752   /* *INDENT-ON* */
4753 }
4754
4755 static void vl_api_l2tpv3_set_tunnel_cookies_t_handler
4756   (vl_api_l2tpv3_set_tunnel_cookies_t * mp)
4757 {
4758   vl_api_l2tpv3_set_tunnel_cookies_reply_t *rmp;
4759   l2t_main_t *lm = &l2t_main;
4760   int rv;
4761
4762   VALIDATE_SW_IF_INDEX (mp);
4763
4764   rv = l2tpv3_set_tunnel_cookies (lm, ntohl (mp->sw_if_index),
4765                                   clib_net_to_host_u64 (mp->new_local_cookie),
4766                                   clib_net_to_host_u64
4767                                   (mp->new_remote_cookie));
4768
4769   BAD_SW_IF_INDEX_LABEL;
4770
4771   REPLY_MACRO (VL_API_L2TPV3_SET_TUNNEL_COOKIES_REPLY);
4772 }
4773
4774 static void vl_api_l2tpv3_interface_enable_disable_t_handler
4775   (vl_api_l2tpv3_interface_enable_disable_t * mp)
4776 {
4777   int rv;
4778   vnet_main_t *vnm = vnet_get_main ();
4779   vl_api_l2tpv3_interface_enable_disable_reply_t *rmp;
4780
4781   VALIDATE_SW_IF_INDEX (mp);
4782
4783   rv = l2tpv3_interface_enable_disable
4784     (vnm, ntohl (mp->sw_if_index), mp->enable_disable);
4785
4786   BAD_SW_IF_INDEX_LABEL;
4787
4788   REPLY_MACRO (VL_API_L2TPV3_INTERFACE_ENABLE_DISABLE_REPLY);
4789 }
4790
4791 static void vl_api_l2tpv3_set_lookup_key_t_handler
4792   (vl_api_l2tpv3_set_lookup_key_t * mp)
4793 {
4794   int rv = 0;
4795   l2t_main_t *lm = &l2t_main;
4796   vl_api_l2tpv3_set_lookup_key_reply_t *rmp;
4797
4798   if (mp->key > L2T_LOOKUP_SESSION_ID)
4799     {
4800       rv = VNET_API_ERROR_INVALID_VALUE;
4801       goto out;
4802     }
4803
4804   lm->lookup_type = mp->key;
4805
4806 out:
4807   REPLY_MACRO (VL_API_L2TPV3_SET_LOOKUP_KEY_REPLY);
4808 }
4809
4810 static void vl_api_vxlan_add_del_tunnel_t_handler
4811   (vl_api_vxlan_add_del_tunnel_t * mp)
4812 {
4813   vl_api_vxlan_add_del_tunnel_reply_t *rmp;
4814   int rv = 0;
4815   vnet_vxlan_add_del_tunnel_args_t _a, *a = &_a;
4816   u32 encap_fib_index;
4817   uword *p;
4818   ip4_main_t *im = &ip4_main;
4819   u32 sw_if_index = ~0;
4820
4821   p = hash_get (im->fib_index_by_table_id, ntohl (mp->encap_vrf_id));
4822   if (!p)
4823     {
4824       rv = VNET_API_ERROR_NO_SUCH_FIB;
4825       goto out;
4826     }
4827   encap_fib_index = p[0];
4828
4829   /* Check src & dst are different */
4830   if ((mp->is_ipv6 && memcmp (mp->src_address, mp->dst_address, 16) == 0) ||
4831       (!mp->is_ipv6 && memcmp (mp->src_address, mp->dst_address, 4) == 0))
4832     {
4833       rv = VNET_API_ERROR_SAME_SRC_DST;
4834       goto out;
4835     }
4836   memset (a, 0, sizeof (*a));
4837
4838   a->is_add = mp->is_add;
4839   a->is_ip6 = mp->is_ipv6;
4840
4841   /* ip addresses sent in network byte order */
4842   if (a->is_ip6)
4843     {
4844       memcpy (&(a->src.ip6), mp->src_address, 16);
4845       memcpy (&(a->dst.ip6), mp->dst_address, 16);
4846     }
4847   else
4848     {
4849       memcpy (&(a->src.ip4), mp->src_address, 4);
4850       memcpy (&(a->dst.ip4), mp->dst_address, 4);
4851     }
4852
4853   a->encap_fib_index = encap_fib_index;
4854   a->decap_next_index = ntohl (mp->decap_next_index);
4855   a->vni = ntohl (mp->vni);
4856   rv = vnet_vxlan_add_del_tunnel (a, &sw_if_index);
4857
4858 out:
4859   /* *INDENT-OFF* */
4860   REPLY_MACRO2(VL_API_VXLAN_ADD_DEL_TUNNEL_REPLY,
4861   ({
4862     rmp->sw_if_index = ntohl (sw_if_index);
4863   }));
4864   /* *INDENT-ON* */
4865 }
4866
4867 static void send_vxlan_tunnel_details
4868   (vxlan_tunnel_t * t, unix_shared_memory_queue_t * q, u32 context)
4869 {
4870   vl_api_vxlan_tunnel_details_t *rmp;
4871   ip4_main_t *im4 = &ip4_main;
4872   ip6_main_t *im6 = &ip6_main;
4873   u8 is_ipv6 = !ip46_address_is_ip4 (&t->dst);
4874
4875   rmp = vl_msg_api_alloc (sizeof (*rmp));
4876   memset (rmp, 0, sizeof (*rmp));
4877   rmp->_vl_msg_id = ntohs (VL_API_VXLAN_TUNNEL_DETAILS);
4878   if (is_ipv6)
4879     {
4880       memcpy (rmp->src_address, &(t->src.ip6), 16);
4881       memcpy (rmp->dst_address, &(t->dst.ip6), 16);
4882       rmp->encap_vrf_id = htonl (im6->fibs[t->encap_fib_index].ft_table_id);
4883     }
4884   else
4885     {
4886       memcpy (rmp->src_address, &(t->src.ip4), 4);
4887       memcpy (rmp->dst_address, &(t->dst.ip4), 4);
4888       rmp->encap_vrf_id = htonl (im4->fibs[t->encap_fib_index].ft_table_id);
4889     }
4890   rmp->vni = htonl (t->vni);
4891   /* decap_next_index is deprecated, hard code to l2-input */
4892   rmp->decap_next_index = htonl (VXLAN_INPUT_NEXT_L2_INPUT);
4893   rmp->sw_if_index = htonl (t->sw_if_index);
4894   rmp->is_ipv6 = is_ipv6;
4895   rmp->context = context;
4896
4897   vl_msg_api_send_shmem (q, (u8 *) & rmp);
4898 }
4899
4900 static void vl_api_vxlan_tunnel_dump_t_handler
4901   (vl_api_vxlan_tunnel_dump_t * mp)
4902 {
4903   unix_shared_memory_queue_t *q;
4904   vxlan_main_t *vxm = &vxlan_main;
4905   vxlan_tunnel_t *t;
4906   u32 sw_if_index;
4907
4908   q = vl_api_client_index_to_input_queue (mp->client_index);
4909   if (q == 0)
4910     {
4911       return;
4912     }
4913
4914   sw_if_index = ntohl (mp->sw_if_index);
4915
4916   if (~0 == sw_if_index)
4917     {
4918       /* *INDENT-OFF* */
4919       pool_foreach (t, vxm->tunnels,
4920       ({
4921         send_vxlan_tunnel_details(t, q, mp->context);
4922       }));
4923       /* *INDENT-ON* */
4924     }
4925   else
4926     {
4927       if ((sw_if_index >= vec_len (vxm->tunnel_index_by_sw_if_index)) ||
4928           (~0 == vxm->tunnel_index_by_sw_if_index[sw_if_index]))
4929         {
4930           return;
4931         }
4932       t = &vxm->tunnels[vxm->tunnel_index_by_sw_if_index[sw_if_index]];
4933       send_vxlan_tunnel_details (t, q, mp->context);
4934     }
4935 }
4936
4937 static void vl_api_gre_add_del_tunnel_t_handler
4938   (vl_api_gre_add_del_tunnel_t * mp)
4939 {
4940   vl_api_gre_add_del_tunnel_reply_t *rmp;
4941   int rv = 0;
4942   vnet_gre_add_del_tunnel_args_t _a, *a = &_a;
4943   u32 outer_fib_id;
4944   uword *p;
4945   ip4_main_t *im = &ip4_main;
4946   u32 sw_if_index = ~0;
4947
4948   p = hash_get (im->fib_index_by_table_id, ntohl (mp->outer_fib_id));
4949   if (!p)
4950     {
4951       rv = VNET_API_ERROR_NO_SUCH_FIB;
4952       goto out;
4953     }
4954   outer_fib_id = p[0];
4955
4956   /* Check src & dst are different */
4957   if ((mp->is_ipv6 && memcmp (mp->src_address, mp->dst_address, 16) == 0) ||
4958       (!mp->is_ipv6 && memcmp (mp->src_address, mp->dst_address, 4) == 0))
4959     {
4960       rv = VNET_API_ERROR_SAME_SRC_DST;
4961       goto out;
4962     }
4963   memset (a, 0, sizeof (*a));
4964
4965   a->is_add = mp->is_add;
4966   a->teb = mp->teb;
4967
4968   /* ip addresses sent in network byte order */
4969   clib_memcpy (&(a->src), mp->src_address, 4);
4970   clib_memcpy (&(a->dst), mp->dst_address, 4);
4971
4972   a->outer_fib_id = outer_fib_id;
4973   rv = vnet_gre_add_del_tunnel (a, &sw_if_index);
4974
4975 out:
4976   /* *INDENT-OFF* */
4977   REPLY_MACRO2(VL_API_GRE_ADD_DEL_TUNNEL_REPLY,
4978   ({
4979     rmp->sw_if_index = ntohl (sw_if_index);
4980   }));
4981   /* *INDENT-ON* */
4982 }
4983
4984 static void send_gre_tunnel_details
4985   (gre_tunnel_t * t, unix_shared_memory_queue_t * q, u32 context)
4986 {
4987   vl_api_gre_tunnel_details_t *rmp;
4988   ip4_main_t *im = &ip4_main;
4989
4990   rmp = vl_msg_api_alloc (sizeof (*rmp));
4991   memset (rmp, 0, sizeof (*rmp));
4992   rmp->_vl_msg_id = ntohs (VL_API_GRE_TUNNEL_DETAILS);
4993   clib_memcpy (rmp->src_address, &(t->tunnel_src), 4);
4994   clib_memcpy (rmp->dst_address, &(t->tunnel_dst), 4);
4995   rmp->outer_fib_id = htonl (im->fibs[t->outer_fib_index].ft_table_id);
4996   rmp->teb = t->teb;
4997   rmp->sw_if_index = htonl (t->sw_if_index);
4998   rmp->context = context;
4999
5000   vl_msg_api_send_shmem (q, (u8 *) & rmp);
5001 }
5002
5003 static void
5004 vl_api_gre_tunnel_dump_t_handler (vl_api_gre_tunnel_dump_t * mp)
5005 {
5006   unix_shared_memory_queue_t *q;
5007   gre_main_t *gm = &gre_main;
5008   gre_tunnel_t *t;
5009   u32 sw_if_index;
5010
5011   q = vl_api_client_index_to_input_queue (mp->client_index);
5012   if (q == 0)
5013     {
5014       return;
5015     }
5016
5017   sw_if_index = ntohl (mp->sw_if_index);
5018
5019   if (~0 == sw_if_index)
5020     {
5021       /* *INDENT-OFF* */
5022       pool_foreach (t, gm->tunnels,
5023       ({
5024         send_gre_tunnel_details(t, q, mp->context);
5025       }));
5026       /* *INDENT-ON* */
5027     }
5028   else
5029     {
5030       if ((sw_if_index >= vec_len (gm->tunnel_index_by_sw_if_index)) ||
5031           (~0 == gm->tunnel_index_by_sw_if_index[sw_if_index]))
5032         {
5033           return;
5034         }
5035       t = &gm->tunnels[gm->tunnel_index_by_sw_if_index[sw_if_index]];
5036       send_gre_tunnel_details (t, q, mp->context);
5037     }
5038 }
5039
5040 static void
5041 vl_api_l2_patch_add_del_t_handler (vl_api_l2_patch_add_del_t * mp)
5042 {
5043   extern int vnet_l2_patch_add_del (u32 rx_sw_if_index, u32 tx_sw_if_index,
5044                                     int is_add);
5045   vl_api_l2_patch_add_del_reply_t *rmp;
5046   int vnet_l2_patch_add_del (u32 rx_sw_if_index, u32 tx_sw_if_index,
5047                              int is_add);
5048   int rv = 0;
5049
5050   VALIDATE_RX_SW_IF_INDEX (mp);
5051   VALIDATE_TX_SW_IF_INDEX (mp);
5052
5053   rv = vnet_l2_patch_add_del (ntohl (mp->rx_sw_if_index),
5054                               ntohl (mp->tx_sw_if_index),
5055                               (int) (mp->is_add != 0));
5056
5057   BAD_RX_SW_IF_INDEX_LABEL;
5058   BAD_TX_SW_IF_INDEX_LABEL;
5059
5060   REPLY_MACRO (VL_API_L2_PATCH_ADD_DEL_REPLY);
5061 }
5062
5063 static void
5064   vl_api_vxlan_gpe_add_del_tunnel_t_handler
5065   (vl_api_vxlan_gpe_add_del_tunnel_t * mp)
5066 {
5067   vl_api_vxlan_gpe_add_del_tunnel_reply_t *rmp;
5068   int rv = 0;
5069   vnet_vxlan_gpe_add_del_tunnel_args_t _a, *a = &_a;
5070   u32 encap_fib_index, decap_fib_index;
5071   u8 protocol;
5072   uword *p;
5073   ip4_main_t *im = &ip4_main;
5074   u32 sw_if_index = ~0;
5075
5076
5077   p = hash_get (im->fib_index_by_table_id, ntohl (mp->encap_vrf_id));
5078   if (!p)
5079     {
5080       rv = VNET_API_ERROR_NO_SUCH_FIB;
5081       goto out;
5082     }
5083   encap_fib_index = p[0];
5084
5085   protocol = mp->protocol;
5086
5087   /* Interpret decap_vrf_id as an opaque if sending to other-than-ip4-input */
5088   if (protocol == VXLAN_GPE_INPUT_NEXT_IP4_INPUT)
5089     {
5090       p = hash_get (im->fib_index_by_table_id, ntohl (mp->decap_vrf_id));
5091       if (!p)
5092         {
5093           rv = VNET_API_ERROR_NO_SUCH_INNER_FIB;
5094           goto out;
5095         }
5096       decap_fib_index = p[0];
5097     }
5098   else
5099     {
5100       decap_fib_index = ntohl (mp->decap_vrf_id);
5101     }
5102
5103   /* Check src & dst are different */
5104   if ((mp->is_ipv6 && memcmp (mp->local, mp->remote, 16) == 0) ||
5105       (!mp->is_ipv6 && memcmp (mp->local, mp->remote, 4) == 0))
5106     {
5107       rv = VNET_API_ERROR_SAME_SRC_DST;
5108       goto out;
5109     }
5110   memset (a, 0, sizeof (*a));
5111
5112   a->is_add = mp->is_add;
5113   a->is_ip6 = mp->is_ipv6;
5114   /* ip addresses sent in network byte order */
5115   if (a->is_ip6)
5116     {
5117       clib_memcpy (&(a->local.ip6), mp->local, 16);
5118       clib_memcpy (&(a->remote.ip6), mp->remote, 16);
5119     }
5120   else
5121     {
5122       clib_memcpy (&(a->local.ip4), mp->local, 4);
5123       clib_memcpy (&(a->remote.ip4), mp->remote, 4);
5124     }
5125   a->encap_fib_index = encap_fib_index;
5126   a->decap_fib_index = decap_fib_index;
5127   a->protocol = protocol;
5128   a->vni = ntohl (mp->vni);
5129   rv = vnet_vxlan_gpe_add_del_tunnel (a, &sw_if_index);
5130
5131 out:
5132   /* *INDENT-OFF* */
5133   REPLY_MACRO2(VL_API_VXLAN_GPE_ADD_DEL_TUNNEL_REPLY,
5134   ({
5135     rmp->sw_if_index = ntohl (sw_if_index);
5136   }));
5137   /* *INDENT-ON* */
5138 }
5139
5140 static void send_vxlan_gpe_tunnel_details
5141   (vxlan_gpe_tunnel_t * t, unix_shared_memory_queue_t * q, u32 context)
5142 {
5143   vl_api_vxlan_gpe_tunnel_details_t *rmp;
5144   ip4_main_t *im4 = &ip4_main;
5145   ip6_main_t *im6 = &ip6_main;
5146   u8 is_ipv6 = !(t->flags & VXLAN_GPE_TUNNEL_IS_IPV4);
5147
5148   rmp = vl_msg_api_alloc (sizeof (*rmp));
5149   memset (rmp, 0, sizeof (*rmp));
5150   rmp->_vl_msg_id = ntohs (VL_API_VXLAN_GPE_TUNNEL_DETAILS);
5151   if (is_ipv6)
5152     {
5153       memcpy (rmp->local, &(t->local.ip6), 16);
5154       memcpy (rmp->remote, &(t->remote.ip6), 16);
5155       rmp->encap_vrf_id = htonl (im6->fibs[t->encap_fib_index].ft_table_id);
5156       rmp->decap_vrf_id = htonl (im6->fibs[t->decap_fib_index].ft_table_id);
5157     }
5158   else
5159     {
5160       memcpy (rmp->local, &(t->local.ip4), 4);
5161       memcpy (rmp->remote, &(t->remote.ip4), 4);
5162       rmp->encap_vrf_id = htonl (im4->fibs[t->encap_fib_index].ft_table_id);
5163       rmp->decap_vrf_id = htonl (im4->fibs[t->decap_fib_index].ft_table_id);
5164     }
5165   rmp->vni = htonl (t->vni);
5166   rmp->protocol = t->protocol;
5167   rmp->sw_if_index = htonl (t->sw_if_index);
5168   rmp->is_ipv6 = is_ipv6;
5169   rmp->context = context;
5170
5171   vl_msg_api_send_shmem (q, (u8 *) & rmp);
5172 }
5173
5174 static void vl_api_vxlan_gpe_tunnel_dump_t_handler
5175   (vl_api_vxlan_gpe_tunnel_dump_t * mp)
5176 {
5177   unix_shared_memory_queue_t *q;
5178   vxlan_gpe_main_t *vgm = &vxlan_gpe_main;
5179   vxlan_gpe_tunnel_t *t;
5180   u32 sw_if_index;
5181
5182   q = vl_api_client_index_to_input_queue (mp->client_index);
5183   if (q == 0)
5184     {
5185       return;
5186     }
5187
5188   sw_if_index = ntohl (mp->sw_if_index);
5189
5190   if (~0 == sw_if_index)
5191     {
5192       /* *INDENT-OFF* */
5193       pool_foreach (t, vgm->tunnels,
5194       ({
5195         send_vxlan_gpe_tunnel_details(t, q, mp->context);
5196       }));
5197       /* *INDENT-ON* */
5198     }
5199   else
5200     {
5201       if ((sw_if_index >= vec_len (vgm->tunnel_index_by_sw_if_index)) ||
5202           (~0 == vgm->tunnel_index_by_sw_if_index[sw_if_index]))
5203         {
5204           return;
5205         }
5206       t = &vgm->tunnels[vgm->tunnel_index_by_sw_if_index[sw_if_index]];
5207       send_vxlan_gpe_tunnel_details (t, q, mp->context);
5208     }
5209 }
5210
5211 /** Used for transferring locators via VPP API */
5212 /* *INDENT-OFF* */
5213 typedef CLIB_PACKED (struct {
5214   u32 sw_if_index; /**< locator sw_if_index */
5215   u8 priority; /**< locator priority */
5216   u8 weight; /**< locator weight */
5217 }) ls_locator_t;
5218 /* *INDENT-ON* */
5219
5220 static void
5221 vl_api_lisp_add_del_locator_set_t_handler (vl_api_lisp_add_del_locator_set_t *
5222                                            mp)
5223 {
5224   vl_api_lisp_add_del_locator_set_reply_t *rmp;
5225   int rv = 0;
5226   vnet_lisp_add_del_locator_set_args_t _a, *a = &_a;
5227   locator_t locator;
5228   ls_locator_t *ls_loc;
5229   u32 ls_index = ~0, locator_num;
5230   u8 *locator_name = NULL;
5231   int i;
5232
5233   memset (a, 0, sizeof (a[0]));
5234
5235   locator_name = format (0, "%s", mp->locator_set_name);
5236
5237   a->name = locator_name;
5238   a->is_add = mp->is_add;
5239   a->local = 1;
5240   locator_num = clib_net_to_host_u32 (mp->locator_num);
5241
5242   memset (&locator, 0, sizeof (locator));
5243   for (i = 0; i < locator_num; i++)
5244     {
5245       ls_loc = &((ls_locator_t *) mp->locators)[i];
5246       VALIDATE_SW_IF_INDEX (ls_loc);
5247
5248       locator.sw_if_index = htonl (ls_loc->sw_if_index);
5249       locator.priority = ls_loc->priority;
5250       locator.weight = ls_loc->weight;
5251       locator.local = 1;
5252       vec_add1 (a->locators, locator);
5253     }
5254
5255   rv = vnet_lisp_add_del_locator_set (a, &ls_index);
5256
5257   BAD_SW_IF_INDEX_LABEL;
5258
5259   vec_free (locator_name);
5260   vec_free (a->locators);
5261
5262   /* *INDENT-OFF* */
5263   REPLY_MACRO2 (VL_API_LISP_ADD_DEL_LOCATOR_SET_REPLY,
5264   ({
5265     rmp->ls_index = clib_host_to_net_u32 (ls_index);
5266   }));
5267   /* *INDENT-ON* */
5268 }
5269
5270 static void
5271 vl_api_lisp_add_del_locator_t_handler (vl_api_lisp_add_del_locator_t * mp)
5272 {
5273   vl_api_lisp_add_del_locator_reply_t *rmp;
5274   int rv = 0;
5275   locator_t locator, *locators = NULL;
5276   vnet_lisp_add_del_locator_set_args_t _a, *a = &_a;
5277   u32 ls_index = ~0;
5278   u8 *locator_name = NULL;
5279
5280   memset (&locator, 0, sizeof (locator));
5281   memset (a, 0, sizeof (a[0]));
5282
5283   locator.sw_if_index = ntohl (mp->sw_if_index);
5284   locator.priority = mp->priority;
5285   locator.weight = mp->weight;
5286   locator.local = 1;
5287   vec_add1 (locators, locator);
5288
5289   locator_name = format (0, "%s", mp->locator_set_name);
5290
5291   a->name = locator_name;
5292   a->locators = locators;
5293   a->is_add = mp->is_add;
5294   a->local = 1;
5295
5296   rv = vnet_lisp_add_del_locator (a, NULL, &ls_index);
5297
5298   vec_free (locators);
5299   vec_free (locator_name);
5300
5301   REPLY_MACRO (VL_API_LISP_ADD_DEL_LOCATOR_REPLY);
5302 }
5303
5304 static int
5305 unformat_lisp_eid_api (gid_address_t * dst, u32 vni, u8 type, void *src,
5306                        u8 len)
5307 {
5308   switch (type)
5309     {
5310     case 0:                     /* ipv4 */
5311       gid_address_type (dst) = GID_ADDR_IP_PREFIX;
5312       gid_address_ip_set (dst, src, IP4);
5313       gid_address_ippref_len (dst) = len;
5314       ip_prefix_normalize (&gid_address_ippref (dst));
5315       break;
5316     case 1:                     /* ipv6 */
5317       gid_address_type (dst) = GID_ADDR_IP_PREFIX;
5318       gid_address_ip_set (dst, src, IP6);
5319       gid_address_ippref_len (dst) = len;
5320       ip_prefix_normalize (&gid_address_ippref (dst));
5321       break;
5322     case 2:                     /* l2 mac */
5323       gid_address_type (dst) = GID_ADDR_MAC;
5324       clib_memcpy (&gid_address_mac (dst), src, 6);
5325       break;
5326     default:
5327       /* unknown type */
5328       return VNET_API_ERROR_INVALID_VALUE;
5329     }
5330
5331   gid_address_vni (dst) = vni;
5332
5333   return 0;
5334 }
5335
5336 static void
5337 vl_api_lisp_add_del_local_eid_t_handler (vl_api_lisp_add_del_local_eid_t * mp)
5338 {
5339   vl_api_lisp_add_del_local_eid_reply_t *rmp;
5340   lisp_cp_main_t *lcm = vnet_lisp_cp_get_main ();
5341   int rv = 0;
5342   gid_address_t _eid, *eid = &_eid;
5343   uword *p = NULL;
5344   u32 locator_set_index = ~0, map_index = ~0;
5345   vnet_lisp_add_del_mapping_args_t _a, *a = &_a;
5346   u8 *name = NULL;
5347   memset (a, 0, sizeof (a[0]));
5348   memset (eid, 0, sizeof (eid[0]));
5349
5350   rv = unformat_lisp_eid_api (eid, clib_net_to_host_u32 (mp->vni),
5351                               mp->eid_type, mp->eid, mp->prefix_len);
5352   if (rv)
5353     goto out;
5354
5355   name = format (0, "%s", mp->locator_set_name);
5356   p = hash_get_mem (lcm->locator_set_index_by_name, name);
5357   if (!p)
5358     {
5359       rv = VNET_API_ERROR_INVALID_VALUE;
5360       goto out;
5361     }
5362   locator_set_index = p[0];
5363
5364   /* XXX treat batch configuration */
5365   a->is_add = mp->is_add;
5366   gid_address_copy (&a->eid, eid);
5367   a->locator_set_index = locator_set_index;
5368   a->local = 1;
5369   rv = vnet_lisp_add_del_local_mapping (a, &map_index);
5370
5371 out:
5372   vec_free (name);
5373   gid_address_free (&a->eid);
5374
5375   REPLY_MACRO (VL_API_LISP_ADD_DEL_LOCAL_EID_REPLY);
5376 }
5377
5378 static void
5379   vl_api_lisp_eid_table_add_del_map_t_handler
5380   (vl_api_lisp_eid_table_add_del_map_t * mp)
5381 {
5382   vl_api_lisp_eid_table_add_del_map_reply_t *rmp;
5383   int rv = 0;
5384   rv = vnet_lisp_eid_table_map (clib_net_to_host_u32 (mp->vni),
5385                                 clib_net_to_host_u32 (mp->dp_table),
5386                                 mp->is_l2, mp->is_add);
5387 REPLY_MACRO (VL_API_LISP_EID_TABLE_ADD_DEL_MAP_REPLY)}
5388
5389 /** Used for transferring locators via VPP API */
5390 /* *INDENT-OFF* */
5391 typedef CLIB_PACKED (struct {
5392   u8 is_ip4; /**< is locator an IPv4 address */
5393   u8 priority; /**< locator priority */
5394   u8 weight; /**< locator weight */
5395   u8 addr[16]; /**< IPv4/IPv6 address */
5396 }) rloc_t;
5397 /* *INDENT-ON* */
5398
5399 static locator_pair_t *
5400 unformat_lisp_loc_pairs (void *lcl_locs, void *rmt_locs, u32 rloc_num)
5401 {
5402   u32 i;
5403   locator_pair_t *pairs = 0, pair;
5404   rloc_t *r;
5405
5406   for (i = 0; i < rloc_num; i++)
5407     {
5408       /* local locator */
5409       r = &((rloc_t *) lcl_locs)[i];
5410       memset (&pair.lcl_loc, 0, sizeof (pair.lcl_loc));
5411       ip_address_set (&pair.lcl_loc, &r->addr, r->is_ip4 ? IP4 : IP6);
5412
5413       /* remote locators */
5414       r = &((rloc_t *) rmt_locs)[i];
5415       memset (&pair.rmt_loc, 0, sizeof (pair.rmt_loc));
5416       ip_address_set (&pair.rmt_loc, &r->addr, r->is_ip4 ? IP4 : IP6);
5417
5418       pair.priority = r->priority;
5419       pair.weight = r->weight;
5420
5421       vec_add1 (pairs, pair);
5422     }
5423   return pairs;
5424 }
5425
5426 static locator_t *
5427 unformat_lisp_locs (void *rmt_locs, u32 rloc_num)
5428 {
5429   u32 i;
5430   locator_t *locs = 0, loc;
5431   rloc_t *r;
5432
5433   for (i = 0; i < rloc_num; i++)
5434     {
5435       /* remote locators */
5436       r = &((rloc_t *) rmt_locs)[i];
5437       memset (&loc, 0, sizeof (loc));
5438       gid_address_ip_set (&loc.address, &r->addr, r->is_ip4 ? IP4 : IP6);
5439
5440       loc.priority = r->priority;
5441       loc.weight = r->weight;
5442
5443       vec_add1 (locs, loc);
5444     }
5445   return locs;
5446 }
5447
5448 static void
5449   vl_api_lisp_gpe_add_del_fwd_entry_t_handler
5450   (vl_api_lisp_gpe_add_del_fwd_entry_t * mp)
5451 {
5452   vl_api_lisp_gpe_add_del_fwd_entry_reply_t *rmp;
5453   vnet_lisp_gpe_add_del_fwd_entry_args_t _a, *a = &_a;
5454   locator_pair_t *pairs = 0;
5455   int rv = 0;
5456
5457   memset (a, 0, sizeof (a[0]));
5458
5459   rv = unformat_lisp_eid_api (&a->rmt_eid, mp->vni, mp->eid_type,
5460                               mp->rmt_eid, mp->rmt_len);
5461   rv |= unformat_lisp_eid_api (&a->lcl_eid, mp->vni, mp->eid_type,
5462                                mp->lcl_eid, mp->lcl_len);
5463
5464   pairs = unformat_lisp_loc_pairs (mp->lcl_locs, mp->rmt_locs, mp->loc_num);
5465
5466   if (rv || 0 == pairs)
5467     goto send_reply;
5468
5469   a->is_add = mp->is_add;
5470   a->locator_pairs = pairs;
5471   a->dp_table = mp->dp_table;
5472   a->vni = mp->vni;
5473   a->action = mp->action;
5474
5475   rv = vnet_lisp_gpe_add_del_fwd_entry (a, 0);
5476   vec_free (pairs);
5477 send_reply:
5478   REPLY_MACRO (VL_API_LISP_GPE_ADD_DEL_FWD_ENTRY_REPLY);
5479 }
5480
5481 static void
5482 vl_api_lisp_add_del_map_resolver_t_handler (vl_api_lisp_add_del_map_resolver_t
5483                                             * mp)
5484 {
5485   vl_api_lisp_add_del_map_resolver_reply_t *rmp;
5486   int rv = 0;
5487   vnet_lisp_add_del_map_resolver_args_t _a, *a = &_a;
5488
5489   memset (a, 0, sizeof (a[0]));
5490
5491   a->is_add = mp->is_add;
5492   ip_address_set (&a->address, mp->ip_address, mp->is_ipv6 ? IP6 : IP4);
5493
5494   rv = vnet_lisp_add_del_map_resolver (a);
5495
5496   REPLY_MACRO (VL_API_LISP_ADD_DEL_MAP_RESOLVER_REPLY);
5497 }
5498
5499 static void
5500 vl_api_lisp_gpe_enable_disable_t_handler (vl_api_lisp_gpe_enable_disable_t *
5501                                           mp)
5502 {
5503   vl_api_lisp_gpe_enable_disable_reply_t *rmp;
5504   int rv = 0;
5505   vnet_lisp_gpe_enable_disable_args_t _a, *a = &_a;
5506
5507   a->is_en = mp->is_en;
5508   vnet_lisp_gpe_enable_disable (a);
5509
5510   REPLY_MACRO (VL_API_LISP_GPE_ENABLE_DISABLE_REPLY);
5511 }
5512
5513 static void
5514 vl_api_lisp_enable_disable_t_handler (vl_api_lisp_enable_disable_t * mp)
5515 {
5516   vl_api_lisp_enable_disable_reply_t *rmp;
5517   int rv = 0;
5518
5519   vnet_lisp_enable_disable (mp->is_en);
5520   REPLY_MACRO (VL_API_LISP_ENABLE_DISABLE_REPLY);
5521 }
5522
5523 static void
5524 vl_api_lisp_gpe_add_del_iface_t_handler (vl_api_lisp_gpe_add_del_iface_t * mp)
5525 {
5526   vl_api_lisp_gpe_add_del_iface_reply_t *rmp;
5527   int rv = 0;
5528
5529   if (mp->is_l2)
5530     {
5531       if (mp->is_add)
5532         {
5533           if (~0 ==
5534               lisp_gpe_tenant_l2_iface_add_or_lock (mp->vni, mp->dp_table))
5535             rv = 1;
5536         }
5537       else
5538         lisp_gpe_tenant_l2_iface_unlock (mp->vni);
5539     }
5540   else
5541     {
5542       if (mp->is_add)
5543         {
5544           if (~0 ==
5545               lisp_gpe_tenant_l3_iface_add_or_lock (mp->vni, mp->dp_table))
5546             rv = 1;
5547         }
5548       else
5549         lisp_gpe_tenant_l3_iface_unlock (mp->vni);
5550     }
5551
5552   REPLY_MACRO (VL_API_LISP_GPE_ADD_DEL_IFACE_REPLY);
5553 }
5554
5555 static void
5556   vl_api_show_lisp_map_request_mode_t_handler
5557   (vl_api_show_lisp_map_request_mode_t * mp)
5558 {
5559   int rv = 0;
5560   vl_api_show_lisp_map_request_mode_reply_t *rmp;
5561
5562   /* *INDENT-OFF* */
5563   REPLY_MACRO2(VL_API_SHOW_LISP_MAP_REQUEST_MODE_REPLY,
5564   ({
5565     rmp->mode = vnet_lisp_get_map_request_mode ();
5566   }));
5567   /* *INDENT-ON* */
5568 }
5569
5570 static void
5571 vl_api_lisp_map_request_mode_t_handler (vl_api_lisp_map_request_mode_t * mp)
5572 {
5573   vl_api_lisp_map_request_mode_reply_t *rmp;
5574   int rv = 0;
5575
5576   rv = vnet_lisp_set_map_request_mode (mp->mode);
5577
5578   REPLY_MACRO (VL_API_LISP_MAP_REQUEST_MODE_REPLY);
5579 }
5580
5581 static void
5582 vl_api_lisp_pitr_set_locator_set_t_handler (vl_api_lisp_pitr_set_locator_set_t
5583                                             * mp)
5584 {
5585   vl_api_lisp_pitr_set_locator_set_reply_t *rmp;
5586   int rv = 0;
5587   u8 *ls_name = 0;
5588
5589   ls_name = format (0, "%s", mp->ls_name);
5590   rv = vnet_lisp_pitr_set_locator_set (ls_name, mp->is_add);
5591   vec_free (ls_name);
5592
5593   REPLY_MACRO (VL_API_LISP_PITR_SET_LOCATOR_SET_REPLY);
5594 }
5595
5596 static void
5597   vl_api_lisp_add_del_map_request_itr_rlocs_t_handler
5598   (vl_api_lisp_add_del_map_request_itr_rlocs_t * mp)
5599 {
5600   vl_api_lisp_add_del_map_request_itr_rlocs_reply_t *rmp;
5601   int rv = 0;
5602   u8 *locator_set_name = NULL;
5603   vnet_lisp_add_del_mreq_itr_rloc_args_t _a, *a = &_a;
5604
5605   locator_set_name = format (0, "%s", mp->locator_set_name);
5606
5607   a->is_add = mp->is_add;
5608   a->locator_set_name = locator_set_name;
5609
5610   rv = vnet_lisp_add_del_mreq_itr_rlocs (a);
5611
5612   vec_free (locator_set_name);
5613
5614   REPLY_MACRO (VL_API_LISP_ADD_DEL_MAP_REQUEST_ITR_RLOCS_REPLY);
5615 }
5616
5617 static void
5618   vl_api_lisp_add_del_remote_mapping_t_handler
5619   (vl_api_lisp_add_del_remote_mapping_t * mp)
5620 {
5621   locator_t *rlocs = 0;
5622   vl_api_lisp_add_del_remote_mapping_reply_t *rmp;
5623   int rv = 0;
5624   gid_address_t _eid, *eid = &_eid;
5625   u32 rloc_num = clib_net_to_host_u32 (mp->rloc_num);
5626
5627   memset (eid, 0, sizeof (eid[0]));
5628
5629   rv = unformat_lisp_eid_api (eid, clib_net_to_host_u32 (mp->vni),
5630                               mp->eid_type, mp->eid, mp->eid_len);
5631   if (rv)
5632     goto send_reply;
5633
5634   rlocs = unformat_lisp_locs (mp->rlocs, rloc_num);
5635
5636   if (!mp->is_add)
5637     {
5638       vnet_lisp_add_del_adjacency_args_t _a, *a = &_a;
5639       gid_address_copy (&a->reid, eid);
5640       a->is_add = 0;
5641       rv = vnet_lisp_add_del_adjacency (a);
5642       if (rv)
5643         {
5644           goto out;
5645         }
5646     }
5647
5648   /* NOTE: for now this works as a static remote mapping, i.e.,
5649    * not authoritative and ttl infinite. */
5650   rv = vnet_lisp_add_del_mapping (eid, rlocs, mp->action, 0, ~0,
5651                                   mp->is_add, 1 /* is_static */ , 0);
5652
5653   if (mp->del_all)
5654     vnet_lisp_clear_all_remote_adjacencies ();
5655
5656 out:
5657   vec_free (rlocs);
5658 send_reply:
5659   REPLY_MACRO (VL_API_LISP_ADD_DEL_REMOTE_MAPPING_REPLY);
5660 }
5661
5662 static void
5663 vl_api_lisp_add_del_adjacency_t_handler (vl_api_lisp_add_del_adjacency_t * mp)
5664 {
5665   vl_api_lisp_add_del_adjacency_reply_t *rmp;
5666   vnet_lisp_add_del_adjacency_args_t _a, *a = &_a;
5667
5668   int rv = 0;
5669   memset (a, 0, sizeof (a[0]));
5670
5671   rv = unformat_lisp_eid_api (&a->leid, clib_net_to_host_u32 (mp->vni),
5672                               mp->eid_type, mp->leid, mp->leid_len);
5673   rv |= unformat_lisp_eid_api (&a->reid, clib_net_to_host_u32 (mp->vni),
5674                                mp->eid_type, mp->reid, mp->reid_len);
5675
5676   if (rv)
5677     goto send_reply;
5678
5679   a->is_add = mp->is_add;
5680   rv = vnet_lisp_add_del_adjacency (a);
5681
5682 send_reply:
5683   REPLY_MACRO (VL_API_LISP_ADD_DEL_ADJACENCY_REPLY);
5684 }
5685
5686 static void
5687 send_lisp_locator_details (lisp_cp_main_t * lcm,
5688                            locator_t * loc,
5689                            unix_shared_memory_queue_t * q, u32 context)
5690 {
5691   vl_api_lisp_locator_details_t *rmp;
5692
5693   rmp = vl_msg_api_alloc (sizeof (*rmp));
5694   memset (rmp, 0, sizeof (*rmp));
5695   rmp->_vl_msg_id = ntohs (VL_API_LISP_LOCATOR_DETAILS);
5696   rmp->context = context;
5697
5698   rmp->local = loc->local;
5699   if (loc->local)
5700     {
5701       rmp->sw_if_index = ntohl (loc->sw_if_index);
5702     }
5703   else
5704     {
5705       rmp->is_ipv6 = gid_address_ip_version (&loc->address);
5706       ip_address_copy_addr (rmp->ip_address, &gid_address_ip (&loc->address));
5707     }
5708   rmp->priority = loc->priority;
5709   rmp->weight = loc->weight;
5710
5711   vl_msg_api_send_shmem (q, (u8 *) & rmp);
5712 }
5713
5714 static void
5715 vl_api_lisp_locator_dump_t_handler (vl_api_lisp_locator_dump_t * mp)
5716 {
5717   u8 *ls_name = 0;
5718   unix_shared_memory_queue_t *q = 0;
5719   lisp_cp_main_t *lcm = vnet_lisp_cp_get_main ();
5720   locator_set_t *lsit = 0;
5721   locator_t *loc = 0;
5722   u32 ls_index = ~0, *locit = 0;
5723   uword *p = 0;
5724
5725   q = vl_api_client_index_to_input_queue (mp->client_index);
5726   if (q == 0)
5727     {
5728       return;
5729     }
5730
5731   if (mp->is_index_set)
5732     ls_index = htonl (mp->ls_index);
5733   else
5734     {
5735       /* make sure we get a proper C-string */
5736       mp->ls_name[sizeof (mp->ls_name) - 1] = 0;
5737       ls_name = format (0, "%s", mp->ls_name);
5738       p = hash_get_mem (lcm->locator_set_index_by_name, ls_name);
5739       if (!p)
5740         goto out;
5741       ls_index = p[0];
5742     }
5743
5744   if (pool_is_free_index (lcm->locator_set_pool, ls_index))
5745     return;
5746
5747   lsit = pool_elt_at_index (lcm->locator_set_pool, ls_index);
5748
5749   vec_foreach (locit, lsit->locator_indices)
5750   {
5751     loc = pool_elt_at_index (lcm->locator_pool, locit[0]);
5752     send_lisp_locator_details (lcm, loc, q, mp->context);
5753   };
5754 out:
5755   vec_free (ls_name);
5756 }
5757
5758 static void
5759 send_lisp_locator_set_details (lisp_cp_main_t * lcm,
5760                                locator_set_t * lsit,
5761                                unix_shared_memory_queue_t * q,
5762                                u32 context, u32 ls_index)
5763 {
5764   vl_api_lisp_locator_set_details_t *rmp;
5765   u8 *str = 0;
5766
5767   rmp = vl_msg_api_alloc (sizeof (*rmp));
5768   memset (rmp, 0, sizeof (*rmp));
5769   rmp->_vl_msg_id = ntohs (VL_API_LISP_LOCATOR_SET_DETAILS);
5770   rmp->context = context;
5771
5772   rmp->ls_index = htonl (ls_index);
5773   if (lsit->local)
5774     {
5775       ASSERT (lsit->name != NULL);
5776       strncpy ((char *) rmp->ls_name, (char *) lsit->name,
5777                vec_len (lsit->name));
5778     }
5779   else
5780     {
5781       str = format (0, "<remote-%d>", ls_index);
5782       strncpy ((char *) rmp->ls_name, (char *) str, vec_len (str));
5783       vec_free (str);
5784     }
5785
5786   vl_msg_api_send_shmem (q, (u8 *) & rmp);
5787 }
5788
5789 static void
5790 vl_api_lisp_locator_set_dump_t_handler (vl_api_lisp_locator_set_dump_t * mp)
5791 {
5792   unix_shared_memory_queue_t *q = NULL;
5793   lisp_cp_main_t *lcm = vnet_lisp_cp_get_main ();
5794   locator_set_t *lsit = NULL;
5795   u8 filter;
5796
5797   q = vl_api_client_index_to_input_queue (mp->client_index);
5798   if (q == 0)
5799     {
5800       return;
5801     }
5802
5803   filter = mp->filter;
5804   /* *INDENT-OFF* */
5805   pool_foreach (lsit, lcm->locator_set_pool,
5806   ({
5807     if (filter && !((1 == filter && lsit->local) ||
5808                     (2 == filter && !lsit->local)))
5809       {
5810         continue;
5811       }
5812     send_lisp_locator_set_details (lcm, lsit, q, mp->context,
5813                                    lsit - lcm->locator_set_pool);
5814   }));
5815   /* *INDENT-ON* */
5816 }
5817
5818 static void
5819 lisp_fid_put_api (u8 * dst, fid_address_t * src, u8 * prefix_length)
5820 {
5821   ASSERT (prefix_length);
5822   ip_prefix_t *ippref = &fid_addr_ippref (src);
5823
5824   switch (fid_addr_type (src))
5825     {
5826     case FID_ADDR_IP_PREF:
5827       if (ip_prefix_version (ippref) == IP4)
5828         clib_memcpy (dst, &ip_prefix_v4 (ippref), 4);
5829       else
5830         clib_memcpy (dst, &ip_prefix_v6 (ippref), 16);
5831       prefix_length[0] = ip_prefix_len (ippref);
5832       break;
5833
5834     case FID_ADDR_MAC:
5835       prefix_length[0] = 0;
5836       clib_memcpy (dst, fid_addr_mac (src), 6);
5837       break;
5838
5839     default:
5840       clib_warning ("Unknown FID type %d!", fid_addr_type (src));
5841       break;
5842     }
5843 }
5844
5845 static u8
5846 fid_type_to_api_type (fid_address_t * fid)
5847 {
5848   ip_prefix_t *ippref;
5849
5850   switch (fid_addr_type (fid))
5851     {
5852     case FID_ADDR_IP_PREF:
5853       ippref = &fid_addr_ippref (fid);
5854       if (ip_prefix_version (ippref) == IP4)
5855         return 0;
5856       else if (ip_prefix_version (ippref) == IP6)
5857         return 1;
5858       else
5859         return ~0;
5860
5861     case FID_ADDR_MAC:
5862       return 2;
5863     }
5864
5865   return ~0;
5866 }
5867
5868 static void
5869 send_lisp_eid_table_details (mapping_t * mapit,
5870                              unix_shared_memory_queue_t * q,
5871                              u32 context, u8 filter)
5872 {
5873   fid_address_t *fid;
5874   lisp_cp_main_t *lcm = vnet_lisp_cp_get_main ();
5875   locator_set_t *ls = 0;
5876   vl_api_lisp_eid_table_details_t *rmp = NULL;
5877   gid_address_t *gid = NULL;
5878   u8 *mac = 0;
5879   ip_prefix_t *ip_prefix = NULL;
5880
5881   switch (filter)
5882     {
5883     case 0:                     /* all mappings */
5884       break;
5885
5886     case 1:                     /* local only */
5887       if (!mapit->local)
5888         return;
5889       break;
5890     case 2:                     /* remote only */
5891       if (mapit->local)
5892         return;
5893       break;
5894     default:
5895       clib_warning ("Filter error, unknown filter: %d", filter);
5896       return;
5897     }
5898
5899   gid = &mapit->eid;
5900   ip_prefix = &gid_address_ippref (gid);
5901   mac = gid_address_mac (gid);
5902
5903   rmp = vl_msg_api_alloc (sizeof (*rmp));
5904   memset (rmp, 0, sizeof (*rmp));
5905   rmp->_vl_msg_id = ntohs (VL_API_LISP_EID_TABLE_DETAILS);
5906
5907   ls = pool_elt_at_index (lcm->locator_set_pool, mapit->locator_set_index);
5908   if (vec_len (ls->locator_indices) == 0)
5909     rmp->locator_set_index = ~0;
5910   else
5911     rmp->locator_set_index = clib_host_to_net_u32 (mapit->locator_set_index);
5912
5913   rmp->is_local = mapit->local;
5914   rmp->ttl = clib_host_to_net_u32 (mapit->ttl);
5915   rmp->action = mapit->action;
5916   rmp->authoritative = mapit->authoritative;
5917
5918   switch (gid_address_type (gid))
5919     {
5920     case GID_ADDR_SRC_DST:
5921       rmp->is_src_dst = 1;
5922       fid = &gid_address_sd_src (gid);
5923       rmp->eid_type = fid_type_to_api_type (fid);
5924       lisp_fid_put_api (rmp->seid, &gid_address_sd_src (gid),
5925                         &rmp->seid_prefix_len);
5926       lisp_fid_put_api (rmp->eid, &gid_address_sd_dst (gid),
5927                         &rmp->eid_prefix_len);
5928       break;
5929     case GID_ADDR_IP_PREFIX:
5930       rmp->eid_prefix_len = ip_prefix_len (ip_prefix);
5931       if (ip_prefix_version (ip_prefix) == IP4)
5932         {
5933           rmp->eid_type = 0;    /* ipv4 type */
5934           clib_memcpy (rmp->eid, &ip_prefix_v4 (ip_prefix),
5935                        sizeof (ip_prefix_v4 (ip_prefix)));
5936         }
5937       else
5938         {
5939           rmp->eid_type = 1;    /* ipv6 type */
5940           clib_memcpy (rmp->eid, &ip_prefix_v6 (ip_prefix),
5941                        sizeof (ip_prefix_v6 (ip_prefix)));
5942         }
5943       break;
5944     case GID_ADDR_MAC:
5945       rmp->eid_type = 2;        /* l2 mac type */
5946       clib_memcpy (rmp->eid, mac, 6);
5947       break;
5948     default:
5949       ASSERT (0);
5950     }
5951   rmp->context = context;
5952   rmp->vni = clib_host_to_net_u32 (gid_address_vni (gid));
5953   vl_msg_api_send_shmem (q, (u8 *) & rmp);
5954 }
5955
5956 static void
5957 vl_api_lisp_eid_table_dump_t_handler (vl_api_lisp_eid_table_dump_t * mp)
5958 {
5959   u32 mi;
5960   unix_shared_memory_queue_t *q = NULL;
5961   lisp_cp_main_t *lcm = vnet_lisp_cp_get_main ();
5962   mapping_t *mapit = NULL;
5963   gid_address_t _eid, *eid = &_eid;
5964
5965   q = vl_api_client_index_to_input_queue (mp->client_index);
5966   if (q == 0)
5967     {
5968       return;
5969     }
5970
5971   if (mp->eid_set)
5972     {
5973       memset (eid, 0, sizeof (*eid));
5974
5975       unformat_lisp_eid_api (eid, clib_net_to_host_u32 (mp->vni),
5976                              mp->eid_type, mp->eid, mp->prefix_length);
5977
5978       mi = gid_dictionary_lookup (&lcm->mapping_index_by_gid, eid);
5979       if ((u32) ~ 0 == mi)
5980         return;
5981
5982       mapit = pool_elt_at_index (lcm->mapping_pool, mi);
5983       send_lisp_eid_table_details (mapit, q, mp->context,
5984                                    0 /* ignore filter */ );
5985     }
5986   else
5987     {
5988       /* *INDENT-OFF* */
5989       pool_foreach (mapit, lcm->mapping_pool,
5990       ({
5991         send_lisp_eid_table_details(mapit, q, mp->context,
5992                                     mp->filter);
5993       }));
5994       /* *INDENT-ON* */
5995     }
5996 }
5997
5998 static void
5999 send_lisp_gpe_fwd_entry_details (lisp_gpe_fwd_entry_t * lfe,
6000                                  unix_shared_memory_queue_t * q, u32 context)
6001 {
6002   vl_api_lisp_gpe_tunnel_details_t *rmp;
6003   lisp_gpe_main_t *lgm = &lisp_gpe_main;
6004
6005   rmp = vl_msg_api_alloc (sizeof (*rmp));
6006   memset (rmp, 0, sizeof (*rmp));
6007   rmp->_vl_msg_id = ntohs (VL_API_LISP_GPE_TUNNEL_DETAILS);
6008
6009   rmp->tunnels = lfe - lgm->lisp_fwd_entry_pool;
6010
6011   rmp->is_ipv6 = ip_prefix_version (&(lfe->key->rmt.ippref)) == IP6 ? 1 : 0;
6012   ip_address_copy_addr (rmp->source_ip,
6013                         &ip_prefix_addr (&(lfe->key->rmt.ippref)));
6014   ip_address_copy_addr (rmp->destination_ip,
6015                         &ip_prefix_addr (&(lfe->key->rmt.ippref)));
6016
6017   rmp->encap_fib_id = htonl (0);
6018   rmp->decap_fib_id = htonl (lfe->eid_fib_index);
6019   rmp->iid = htonl (lfe->key->vni);
6020   rmp->context = context;
6021
6022   vl_msg_api_send_shmem (q, (u8 *) & rmp);
6023 }
6024
6025 static void
6026 vl_api_lisp_gpe_tunnel_dump_t_handler (vl_api_lisp_gpe_tunnel_dump_t * mp)
6027 {
6028   unix_shared_memory_queue_t *q = NULL;
6029   lisp_gpe_main_t *lgm = &lisp_gpe_main;
6030   lisp_gpe_fwd_entry_t *lfe = NULL;
6031
6032   if (pool_elts (lgm->lisp_fwd_entry_pool) == 0)
6033     {
6034       return;
6035     }
6036
6037   q = vl_api_client_index_to_input_queue (mp->client_index);
6038   if (q == 0)
6039     {
6040       return;
6041     }
6042
6043   /* *INDENT-OFF* */
6044   pool_foreach(lfe, lgm->lisp_fwd_entry_pool,
6045   ({
6046     send_lisp_gpe_fwd_entry_details(lfe, q, mp->context);
6047   }));
6048   /* *INDENT-ON* */
6049 }
6050
6051 static void
6052 send_lisp_map_resolver_details (ip_address_t * ip,
6053                                 unix_shared_memory_queue_t * q, u32 context)
6054 {
6055   vl_api_lisp_map_resolver_details_t *rmp = NULL;
6056
6057   rmp = vl_msg_api_alloc (sizeof (*rmp));
6058   memset (rmp, 0, sizeof (*rmp));
6059   rmp->_vl_msg_id = ntohs (VL_API_LISP_MAP_RESOLVER_DETAILS);
6060
6061   switch (ip_addr_version (ip))
6062     {
6063     case IP4:
6064       rmp->is_ipv6 = 0;
6065       clib_memcpy (rmp->ip_address, &ip_addr_v4 (ip),
6066                    sizeof (ip_addr_v4 (ip)));
6067       break;
6068
6069     case IP6:
6070       rmp->is_ipv6 = 1;
6071       clib_memcpy (rmp->ip_address, &ip_addr_v6 (ip),
6072                    sizeof (ip_addr_v6 (ip)));
6073       break;
6074
6075     default:
6076       ASSERT (0);
6077     }
6078   rmp->context = context;
6079
6080   vl_msg_api_send_shmem (q, (u8 *) & rmp);
6081 }
6082
6083 static void
6084 vl_api_lisp_map_resolver_dump_t_handler (vl_api_lisp_map_resolver_dump_t * mp)
6085 {
6086   unix_shared_memory_queue_t *q = NULL;
6087   lisp_cp_main_t *lcm = vnet_lisp_cp_get_main ();
6088   map_resolver_t *mr;
6089
6090   q = vl_api_client_index_to_input_queue (mp->client_index);
6091   if (q == 0)
6092     {
6093       return;
6094     }
6095
6096   vec_foreach (mr, lcm->map_resolvers)
6097   {
6098     send_lisp_map_resolver_details (&mr->address, q, mp->context);
6099   }
6100 }
6101
6102 static void
6103 send_eid_table_map_pair (hash_pair_t * p,
6104                          unix_shared_memory_queue_t * q, u32 context)
6105 {
6106   vl_api_lisp_eid_table_map_details_t *rmp = NULL;
6107
6108   rmp = vl_msg_api_alloc (sizeof (*rmp));
6109   memset (rmp, 0, sizeof (*rmp));
6110   rmp->_vl_msg_id = ntohs (VL_API_LISP_EID_TABLE_MAP_DETAILS);
6111
6112   rmp->vni = clib_host_to_net_u32 (p->key);
6113   rmp->dp_table = clib_host_to_net_u32 (p->value[0]);
6114   rmp->context = context;
6115   vl_msg_api_send_shmem (q, (u8 *) & rmp);
6116 }
6117
6118 static void
6119 vl_api_lisp_eid_table_map_dump_t_handler (vl_api_lisp_eid_table_map_dump_t *
6120                                           mp)
6121 {
6122   unix_shared_memory_queue_t *q = NULL;
6123   lisp_cp_main_t *lcm = vnet_lisp_cp_get_main ();
6124   hash_pair_t *p;
6125   uword *vni_table = 0;
6126
6127   q = vl_api_client_index_to_input_queue (mp->client_index);
6128   if (q == 0)
6129     {
6130       return;
6131     }
6132
6133   if (mp->is_l2)
6134     {
6135       vni_table = lcm->bd_id_by_vni;
6136     }
6137   else
6138     {
6139       vni_table = lcm->table_id_by_vni;
6140     }
6141
6142   /* *INDENT-OFF* */
6143   hash_foreach_pair (p, vni_table,
6144   ({
6145     send_eid_table_map_pair (p, q, mp->context);
6146   }));
6147   /* *INDENT-ON* */
6148 }
6149
6150 static void
6151 send_eid_table_vni (u32 vni, unix_shared_memory_queue_t * q, u32 context)
6152 {
6153   vl_api_lisp_eid_table_vni_details_t *rmp = 0;
6154
6155   rmp = vl_msg_api_alloc (sizeof (*rmp));
6156   memset (rmp, 0, sizeof (*rmp));
6157   rmp->_vl_msg_id = ntohs (VL_API_LISP_EID_TABLE_VNI_DETAILS);
6158   rmp->context = context;
6159   rmp->vni = clib_host_to_net_u32 (vni);
6160   vl_msg_api_send_shmem (q, (u8 *) & rmp);
6161 }
6162
6163 static void
6164 lisp_adjacency_copy (vl_api_lisp_adjacency_t * dst, lisp_adjacency_t * adjs)
6165 {
6166   lisp_adjacency_t *adj;
6167   vl_api_lisp_adjacency_t a;
6168   u32 i, n = vec_len (adjs);
6169
6170   for (i = 0; i < n; i++)
6171     {
6172       adj = vec_elt_at_index (adjs, i);
6173       memset (&a, 0, sizeof (a));
6174
6175       switch (gid_address_type (&adj->reid))
6176         {
6177         case GID_ADDR_IP_PREFIX:
6178           a.reid_prefix_len = gid_address_ippref_len (&adj->reid);
6179           a.leid_prefix_len = gid_address_ippref_len (&adj->leid);
6180           if (gid_address_ip_version (&adj->reid) == IP4)
6181             {
6182               a.eid_type = 0;   /* ipv4 type */
6183               clib_memcpy (a.reid, &gid_address_ip (&adj->reid), 4);
6184               clib_memcpy (a.leid, &gid_address_ip (&adj->leid), 4);
6185             }
6186           else
6187             {
6188               a.eid_type = 1;   /* ipv6 type */
6189               clib_memcpy (a.reid, &gid_address_ip (&adj->reid), 16);
6190               clib_memcpy (a.leid, &gid_address_ip (&adj->leid), 16);
6191             }
6192           break;
6193         case GID_ADDR_MAC:
6194           a.eid_type = 2;       /* l2 mac type */
6195           mac_copy (a.reid, gid_address_mac (&adj->reid));
6196           mac_copy (a.leid, gid_address_mac (&adj->leid));
6197           break;
6198         default:
6199           ASSERT (0);
6200         }
6201       dst[i] = a;
6202     }
6203 }
6204
6205 static void
6206 vl_api_lisp_adjacencies_get_t_handler (vl_api_lisp_adjacencies_get_t * mp)
6207 {
6208   vl_api_lisp_adjacencies_get_reply_t *rmp = 0;
6209   lisp_adjacency_t *adjs = 0;
6210   int rv = 0;
6211   vl_api_lisp_adjacency_t a;
6212   u32 size = ~0;
6213   u32 vni = clib_net_to_host_u32 (mp->vni);
6214
6215   adjs = vnet_lisp_adjacencies_get_by_vni (vni);
6216   size = vec_len (adjs) * sizeof (a);
6217
6218   /* *INDENT-OFF* */
6219   REPLY_MACRO4 (VL_API_LISP_ADJACENCIES_GET_REPLY, size,
6220   {
6221     rmp->count = clib_host_to_net_u32 (vec_len (adjs));
6222     lisp_adjacency_copy (rmp->adjacencies, adjs);
6223   });
6224   /* *INDENT-ON* */
6225
6226   vec_free (adjs);
6227 }
6228
6229 static void
6230 vl_api_lisp_eid_table_vni_dump_t_handler (vl_api_lisp_eid_table_vni_dump_t *
6231                                           mp)
6232 {
6233   hash_pair_t *p;
6234   u32 *vnis = 0;
6235   unix_shared_memory_queue_t *q = 0;
6236   lisp_cp_main_t *lcm = vnet_lisp_cp_get_main ();
6237
6238   q = vl_api_client_index_to_input_queue (mp->client_index);
6239   if (q == 0)
6240     {
6241       return;
6242     }
6243
6244   /* *INDENT-OFF* */
6245   hash_foreach_pair (p, lcm->table_id_by_vni,
6246   ({
6247     hash_set (vnis, p->key, 0);
6248   }));
6249
6250   hash_foreach_pair (p, lcm->bd_id_by_vni,
6251   ({
6252     hash_set (vnis, p->key, 0);
6253   }));
6254
6255   hash_foreach_pair (p, vnis,
6256   ({
6257     send_eid_table_vni (p->key, q, mp->context);
6258   }));
6259   /* *INDENT-ON* */
6260
6261   hash_free (vnis);
6262 }
6263
6264 static void
6265 vl_api_show_lisp_status_t_handler (vl_api_show_lisp_status_t * mp)
6266 {
6267   unix_shared_memory_queue_t *q = NULL;
6268   vl_api_show_lisp_status_reply_t *rmp = NULL;
6269   int rv = 0;
6270
6271   q = vl_api_client_index_to_input_queue (mp->client_index);
6272   if (q == 0)
6273     {
6274       return;
6275     }
6276
6277   /* *INDENT-OFF* */
6278   REPLY_MACRO2(VL_API_SHOW_LISP_STATUS_REPLY,
6279   ({
6280     rmp->gpe_status = vnet_lisp_gpe_enable_disable_status ();
6281     rmp->feature_status = vnet_lisp_enable_disable_status ();
6282   }));
6283   /* *INDENT-ON* */
6284 }
6285
6286 static void
6287   vl_api_lisp_get_map_request_itr_rlocs_t_handler
6288   (vl_api_lisp_get_map_request_itr_rlocs_t * mp)
6289 {
6290   unix_shared_memory_queue_t *q = NULL;
6291   vl_api_lisp_get_map_request_itr_rlocs_reply_t *rmp = NULL;
6292   lisp_cp_main_t *lcm = vnet_lisp_cp_get_main ();
6293   locator_set_t *loc_set = 0;
6294   u8 *tmp_str = 0;
6295   int rv = 0;
6296
6297   q = vl_api_client_index_to_input_queue (mp->client_index);
6298   if (q == 0)
6299     {
6300       return;
6301     }
6302
6303   if (~0 == lcm->mreq_itr_rlocs)
6304     {
6305       tmp_str = format (0, " ");
6306     }
6307   else
6308     {
6309       loc_set =
6310         pool_elt_at_index (lcm->locator_set_pool, lcm->mreq_itr_rlocs);
6311       tmp_str = format (0, "%s", loc_set->name);
6312     }
6313
6314   /* *INDENT-OFF* */
6315   REPLY_MACRO2(VL_API_LISP_GET_MAP_REQUEST_ITR_RLOCS_REPLY,
6316   ({
6317     strncpy((char *) rmp->locator_set_name, (char *) tmp_str,
6318             ARRAY_LEN(rmp->locator_set_name) - 1);
6319   }));
6320   /* *INDENT-ON* */
6321
6322   vec_free (tmp_str);
6323 }
6324
6325 static void
6326 vl_api_show_lisp_pitr_t_handler (vl_api_show_lisp_pitr_t * mp)
6327 {
6328   unix_shared_memory_queue_t *q = NULL;
6329   vl_api_show_lisp_pitr_reply_t *rmp = NULL;
6330   lisp_cp_main_t *lcm = vnet_lisp_cp_get_main ();
6331   mapping_t *m;
6332   locator_set_t *ls = 0;
6333   u8 *tmp_str = 0;
6334   int rv = 0;
6335
6336   q = vl_api_client_index_to_input_queue (mp->client_index);
6337   if (q == 0)
6338     {
6339       return;
6340     }
6341
6342   if (!lcm->lisp_pitr)
6343     {
6344       tmp_str = format (0, "N/A");
6345     }
6346   else
6347     {
6348       m = pool_elt_at_index (lcm->mapping_pool, lcm->pitr_map_index);
6349       if (~0 != m->locator_set_index)
6350         {
6351           ls =
6352             pool_elt_at_index (lcm->locator_set_pool, m->locator_set_index);
6353           tmp_str = format (0, "%s", ls->name);
6354         }
6355       else
6356         {
6357           tmp_str = format (0, "N/A");
6358         }
6359     }
6360   vec_add1 (tmp_str, 0);
6361
6362   /* *INDENT-OFF* */
6363   REPLY_MACRO2(VL_API_SHOW_LISP_PITR_REPLY,
6364   ({
6365     rmp->status = lcm->lisp_pitr;
6366     strncpy((char *) rmp->locator_set_name, (char *) tmp_str,
6367             ARRAY_LEN(rmp->locator_set_name) - 1);
6368   }));
6369   /* *INDENT-ON* */
6370 }
6371
6372 static void
6373 vl_api_interface_name_renumber_t_handler (vl_api_interface_name_renumber_t *
6374                                           mp)
6375 {
6376   vl_api_interface_name_renumber_reply_t *rmp;
6377   int rv = 0;
6378
6379   VALIDATE_SW_IF_INDEX (mp);
6380
6381   rv = vnet_interface_name_renumber
6382     (ntohl (mp->sw_if_index), ntohl (mp->new_show_dev_instance));
6383
6384   BAD_SW_IF_INDEX_LABEL;
6385
6386   REPLY_MACRO (VL_API_INTERFACE_NAME_RENUMBER_REPLY);
6387 }
6388
6389 static int
6390 arp_change_data_callback (u32 pool_index, u8 * new_mac,
6391                           u32 sw_if_index, u32 address)
6392 {
6393   vpe_api_main_t *am = &vpe_api_main;
6394   vlib_main_t *vm = am->vlib_main;
6395   vl_api_ip4_arp_event_t *event;
6396   static f64 arp_event_last_time;
6397   f64 now = vlib_time_now (vm);
6398
6399   if (pool_is_free_index (am->arp_events, pool_index))
6400     return 1;
6401
6402   event = pool_elt_at_index (am->arp_events, pool_index);
6403   /* *INDENT-OFF* */
6404   if (memcmp (&event->new_mac, new_mac, sizeof (event->new_mac)))
6405     {
6406       clib_memcpy (event->new_mac, new_mac, sizeof (event->new_mac));
6407     }
6408   else
6409     {                           /* same mac */
6410       if (sw_if_index == event->sw_if_index &&
6411           (!event->mac_ip ||
6412            /* for BD case, also check IP address with 10 sec timeout */
6413            (address == event->address &&
6414             (now - arp_event_last_time) < 10.0)))
6415         return 1;
6416     }
6417   /* *INDENT-ON* */
6418
6419   arp_event_last_time = now;
6420   event->sw_if_index = sw_if_index;
6421   if (event->mac_ip)
6422     event->address = address;
6423   return 0;
6424 }
6425
6426 static int
6427 nd_change_data_callback (u32 pool_index, u8 * new_mac,
6428                          u32 sw_if_index, ip6_address_t * address)
6429 {
6430   vpe_api_main_t *am = &vpe_api_main;
6431   vlib_main_t *vm = am->vlib_main;
6432   vl_api_ip6_nd_event_t *event;
6433   static f64 nd_event_last_time;
6434   f64 now = vlib_time_now (vm);
6435
6436   if (pool_is_free_index (am->nd_events, pool_index))
6437     return 1;
6438
6439   event = pool_elt_at_index (am->nd_events, pool_index);
6440
6441   /* *INDENT-OFF* */
6442   if (memcmp (&event->new_mac, new_mac, sizeof (event->new_mac)))
6443     {
6444       clib_memcpy (event->new_mac, new_mac, sizeof (event->new_mac));
6445     }
6446   else
6447     {                           /* same mac */
6448       if (sw_if_index == event->sw_if_index &&
6449           (!event->mac_ip ||
6450            /* for BD case, also check IP address with 10 sec timeout */
6451            (ip6_address_is_equal (address,
6452                                   (ip6_address_t *) event->address) &&
6453             (now - nd_event_last_time) < 10.0)))
6454         return 1;
6455     }
6456   /* *INDENT-ON* */
6457
6458   nd_event_last_time = now;
6459   event->sw_if_index = sw_if_index;
6460   if (event->mac_ip)
6461     clib_memcpy (event->address, address, sizeof (event->address));
6462   return 0;
6463 }
6464
6465 static int
6466 arp_change_delete_callback (u32 pool_index, u8 * notused)
6467 {
6468   vpe_api_main_t *am = &vpe_api_main;
6469
6470   if (pool_is_free_index (am->arp_events, pool_index))
6471     return 1;
6472
6473   pool_put_index (am->arp_events, pool_index);
6474   return 0;
6475 }
6476
6477 static int
6478 nd_change_delete_callback (u32 pool_index, u8 * notused)
6479 {
6480   vpe_api_main_t *am = &vpe_api_main;
6481
6482   if (pool_is_free_index (am->nd_events, pool_index))
6483     return 1;
6484
6485   pool_put_index (am->nd_events, pool_index);
6486   return 0;
6487 }
6488
6489 static void
6490 vl_api_want_ip4_arp_events_t_handler (vl_api_want_ip4_arp_events_t * mp)
6491 {
6492   vpe_api_main_t *am = &vpe_api_main;
6493   vnet_main_t *vnm = vnet_get_main ();
6494   vl_api_want_ip4_arp_events_reply_t *rmp;
6495   vl_api_ip4_arp_event_t *event;
6496   int rv;
6497
6498   if (mp->enable_disable)
6499     {
6500       pool_get (am->arp_events, event);
6501       memset (event, 0, sizeof (*event));
6502
6503       event->_vl_msg_id = ntohs (VL_API_IP4_ARP_EVENT);
6504       event->client_index = mp->client_index;
6505       event->context = mp->context;
6506       event->address = mp->address;
6507       event->pid = mp->pid;
6508       if (mp->address == 0)
6509         event->mac_ip = 1;
6510
6511       rv = vnet_add_del_ip4_arp_change_event
6512         (vnm, arp_change_data_callback,
6513          mp->pid, &mp->address /* addr, in net byte order */ ,
6514          vpe_resolver_process_node.index,
6515          IP4_ARP_EVENT, event - am->arp_events, 1 /* is_add */ );
6516     }
6517   else
6518     {
6519       rv = vnet_add_del_ip4_arp_change_event
6520         (vnm, arp_change_delete_callback,
6521          mp->pid, &mp->address /* addr, in net byte order */ ,
6522          vpe_resolver_process_node.index,
6523          IP4_ARP_EVENT, ~0 /* pool index */ , 0 /* is_add */ );
6524     }
6525   REPLY_MACRO (VL_API_WANT_IP4_ARP_EVENTS_REPLY);
6526 }
6527
6528 static void
6529 vl_api_want_ip6_nd_events_t_handler (vl_api_want_ip6_nd_events_t * mp)
6530 {
6531   vpe_api_main_t *am = &vpe_api_main;
6532   vnet_main_t *vnm = vnet_get_main ();
6533   vl_api_want_ip6_nd_events_reply_t *rmp;
6534   vl_api_ip6_nd_event_t *event;
6535   int rv;
6536
6537   if (mp->enable_disable)
6538     {
6539       pool_get (am->nd_events, event);
6540       memset (event, 0, sizeof (*event));
6541
6542       event->_vl_msg_id = ntohs (VL_API_IP6_ND_EVENT);
6543       event->client_index = mp->client_index;
6544       event->context = mp->context;
6545       clib_memcpy (event->address, mp->address, 16);
6546       event->pid = mp->pid;
6547       if (ip6_address_is_zero ((ip6_address_t *) mp->address))
6548         event->mac_ip = 1;
6549
6550       rv = vnet_add_del_ip6_nd_change_event
6551         (vnm, nd_change_data_callback,
6552          mp->pid, mp->address /* addr, in net byte order */ ,
6553          vpe_resolver_process_node.index,
6554          IP6_ND_EVENT, event - am->nd_events, 1 /* is_add */ );
6555     }
6556   else
6557     {
6558       rv = vnet_add_del_ip6_nd_change_event
6559         (vnm, nd_change_delete_callback,
6560          mp->pid, mp->address /* addr, in net byte order */ ,
6561          vpe_resolver_process_node.index,
6562          IP6_ND_EVENT, ~0 /* pool index */ , 0 /* is_add */ );
6563     }
6564   REPLY_MACRO (VL_API_WANT_IP6_ND_EVENTS_REPLY);
6565 }
6566
6567 static void vl_api_input_acl_set_interface_t_handler
6568   (vl_api_input_acl_set_interface_t * mp)
6569 {
6570   vlib_main_t *vm = vlib_get_main ();
6571   vl_api_input_acl_set_interface_reply_t *rmp;
6572   int rv;
6573   u32 sw_if_index, ip4_table_index, ip6_table_index, l2_table_index;
6574
6575   ip4_table_index = ntohl (mp->ip4_table_index);
6576   ip6_table_index = ntohl (mp->ip6_table_index);
6577   l2_table_index = ntohl (mp->l2_table_index);
6578   sw_if_index = ntohl (mp->sw_if_index);
6579
6580   VALIDATE_SW_IF_INDEX (mp);
6581
6582   rv = vnet_set_input_acl_intfc (vm, sw_if_index, ip4_table_index,
6583                                  ip6_table_index, l2_table_index, mp->is_add);
6584
6585   BAD_SW_IF_INDEX_LABEL;
6586
6587   REPLY_MACRO (VL_API_INPUT_ACL_SET_INTERFACE_REPLY);
6588 }
6589
6590 static void vl_api_ipsec_spd_add_del_t_handler
6591   (vl_api_ipsec_spd_add_del_t * mp)
6592 {
6593 #if IPSEC == 0
6594   clib_warning ("unimplemented");
6595 #else
6596
6597   vlib_main_t *vm __attribute__ ((unused)) = vlib_get_main ();
6598   vl_api_ipsec_spd_add_del_reply_t *rmp;
6599   int rv;
6600
6601 #if DPDK > 0
6602   rv = ipsec_add_del_spd (vm, ntohl (mp->spd_id), mp->is_add);
6603 #else
6604   rv = VNET_API_ERROR_UNIMPLEMENTED;
6605 #endif
6606
6607   REPLY_MACRO (VL_API_IPSEC_SPD_ADD_DEL_REPLY);
6608 #endif
6609 }
6610
6611 static void vl_api_ipsec_interface_add_del_spd_t_handler
6612   (vl_api_ipsec_interface_add_del_spd_t * mp)
6613 {
6614   vlib_main_t *vm __attribute__ ((unused)) = vlib_get_main ();
6615   vl_api_ipsec_interface_add_del_spd_reply_t *rmp;
6616   int rv;
6617   u32 sw_if_index __attribute__ ((unused));
6618   u32 spd_id __attribute__ ((unused));
6619
6620   sw_if_index = ntohl (mp->sw_if_index);
6621   spd_id = ntohl (mp->spd_id);
6622
6623   VALIDATE_SW_IF_INDEX (mp);
6624
6625 #if IPSEC > 0
6626   rv = ipsec_set_interface_spd (vm, sw_if_index, spd_id, mp->is_add);
6627 #else
6628   rv = VNET_API_ERROR_UNIMPLEMENTED;
6629 #endif
6630
6631   BAD_SW_IF_INDEX_LABEL;
6632
6633   REPLY_MACRO (VL_API_IPSEC_INTERFACE_ADD_DEL_SPD_REPLY);
6634 }
6635
6636 static void vl_api_ipsec_spd_add_del_entry_t_handler
6637   (vl_api_ipsec_spd_add_del_entry_t * mp)
6638 {
6639   vlib_main_t *vm __attribute__ ((unused)) = vlib_get_main ();
6640   vl_api_ipsec_spd_add_del_entry_reply_t *rmp;
6641   int rv;
6642
6643 #if IPSEC > 0
6644   ipsec_policy_t p;
6645
6646   memset (&p, 0, sizeof (p));
6647
6648   p.id = ntohl (mp->spd_id);
6649   p.priority = ntohl (mp->priority);
6650   p.is_outbound = mp->is_outbound;
6651   p.is_ipv6 = mp->is_ipv6;
6652
6653   if (mp->is_ipv6 || mp->is_ip_any)
6654     {
6655       clib_memcpy (&p.raddr.start, mp->remote_address_start, 16);
6656       clib_memcpy (&p.raddr.stop, mp->remote_address_stop, 16);
6657       clib_memcpy (&p.laddr.start, mp->local_address_start, 16);
6658       clib_memcpy (&p.laddr.stop, mp->local_address_stop, 16);
6659     }
6660   else
6661     {
6662       clib_memcpy (&p.raddr.start.ip4.data, mp->remote_address_start, 4);
6663       clib_memcpy (&p.raddr.stop.ip4.data, mp->remote_address_stop, 4);
6664       clib_memcpy (&p.laddr.start.ip4.data, mp->local_address_start, 4);
6665       clib_memcpy (&p.laddr.stop.ip4.data, mp->local_address_stop, 4);
6666     }
6667   p.protocol = mp->protocol;
6668   p.rport.start = ntohs (mp->remote_port_start);
6669   p.rport.stop = ntohs (mp->remote_port_stop);
6670   p.lport.start = ntohs (mp->local_port_start);
6671   p.lport.stop = ntohs (mp->local_port_stop);
6672   /* policy action resolve unsupported */
6673   if (mp->policy == IPSEC_POLICY_ACTION_RESOLVE)
6674     {
6675       clib_warning ("unsupported action: 'resolve'");
6676       rv = VNET_API_ERROR_UNIMPLEMENTED;
6677       goto out;
6678     }
6679   p.policy = mp->policy;
6680   p.sa_id = ntohl (mp->sa_id);
6681
6682   rv = ipsec_add_del_policy (vm, &p, mp->is_add);
6683   if (rv)
6684     goto out;
6685
6686   if (mp->is_ip_any)
6687     {
6688       p.is_ipv6 = 1;
6689       rv = ipsec_add_del_policy (vm, &p, mp->is_add);
6690     }
6691 #else
6692   rv = VNET_API_ERROR_UNIMPLEMENTED;
6693   goto out;
6694 #endif
6695
6696 out:
6697   REPLY_MACRO (VL_API_IPSEC_SPD_ADD_DEL_ENTRY_REPLY);
6698 }
6699
6700 static void vl_api_ipsec_sad_add_del_entry_t_handler
6701   (vl_api_ipsec_sad_add_del_entry_t * mp)
6702 {
6703   vlib_main_t *vm __attribute__ ((unused)) = vlib_get_main ();
6704   vl_api_ipsec_sad_add_del_entry_reply_t *rmp;
6705   int rv;
6706 #if IPSEC > 0
6707   ipsec_sa_t sa;
6708
6709   memset (&sa, 0, sizeof (sa));
6710
6711   sa.id = ntohl (mp->sad_id);
6712   sa.spi = ntohl (mp->spi);
6713   /* security protocol AH unsupported */
6714   if (mp->protocol == IPSEC_PROTOCOL_AH)
6715     {
6716       clib_warning ("unsupported security protocol 'AH'");
6717       rv = VNET_API_ERROR_UNIMPLEMENTED;
6718       goto out;
6719     }
6720   sa.protocol = mp->protocol;
6721   /* check for unsupported crypto-alg */
6722   if (mp->crypto_algorithm < IPSEC_CRYPTO_ALG_AES_CBC_128 ||
6723       mp->crypto_algorithm > IPSEC_CRYPTO_ALG_AES_CBC_256)
6724     {
6725       clib_warning ("unsupported crypto-alg: '%U'", format_ipsec_crypto_alg,
6726                     mp->crypto_algorithm);
6727       rv = VNET_API_ERROR_UNIMPLEMENTED;
6728       goto out;
6729     }
6730   sa.crypto_alg = mp->crypto_algorithm;
6731   sa.crypto_key_len = mp->crypto_key_length;
6732   clib_memcpy (&sa.crypto_key, mp->crypto_key, sizeof (sa.crypto_key));
6733   /* check for unsupported integ-alg */
6734   if (mp->integrity_algorithm < IPSEC_INTEG_ALG_SHA1_96 ||
6735       mp->integrity_algorithm > IPSEC_INTEG_ALG_SHA_512_256)
6736     {
6737       clib_warning ("unsupported integ-alg: '%U'", format_ipsec_integ_alg,
6738                     mp->integrity_algorithm);
6739       rv = VNET_API_ERROR_UNIMPLEMENTED;
6740       goto out;
6741     }
6742   sa.integ_alg = mp->integrity_algorithm;
6743   sa.integ_key_len = mp->integrity_key_length;
6744   clib_memcpy (&sa.integ_key, mp->integrity_key, sizeof (sa.integ_key));
6745   sa.use_esn = mp->use_extended_sequence_number;
6746   sa.is_tunnel = mp->is_tunnel;
6747   sa.is_tunnel_ip6 = mp->is_tunnel_ipv6;
6748   if (sa.is_tunnel_ip6)
6749     {
6750       clib_memcpy (&sa.tunnel_src_addr, mp->tunnel_src_address, 16);
6751       clib_memcpy (&sa.tunnel_dst_addr, mp->tunnel_dst_address, 16);
6752     }
6753   else
6754     {
6755       clib_memcpy (&sa.tunnel_src_addr.ip4.data, mp->tunnel_src_address, 4);
6756       clib_memcpy (&sa.tunnel_dst_addr.ip4.data, mp->tunnel_dst_address, 4);
6757     }
6758
6759   rv = ipsec_add_del_sa (vm, &sa, mp->is_add);
6760 #else
6761   rv = VNET_API_ERROR_UNIMPLEMENTED;
6762   goto out;
6763 #endif
6764
6765 out:
6766   REPLY_MACRO (VL_API_IPSEC_SAD_ADD_DEL_ENTRY_REPLY);
6767 }
6768
6769 static void
6770 vl_api_ikev2_profile_add_del_t_handler (vl_api_ikev2_profile_add_del_t * mp)
6771 {
6772   vl_api_ikev2_profile_add_del_reply_t *rmp;
6773   int rv = 0;
6774
6775 #if IPSEC > 0
6776   vlib_main_t *vm = vlib_get_main ();
6777   clib_error_t *error;
6778   u8 *tmp = format (0, "%s", mp->name);
6779   error = ikev2_add_del_profile (vm, tmp, mp->is_add);
6780   vec_free (tmp);
6781   if (error)
6782     rv = VNET_API_ERROR_UNSPECIFIED;
6783 #else
6784   rv = VNET_API_ERROR_UNIMPLEMENTED;
6785 #endif
6786
6787   REPLY_MACRO (VL_API_IKEV2_PROFILE_ADD_DEL_REPLY);
6788 }
6789
6790 static void
6791   vl_api_ikev2_profile_set_auth_t_handler
6792   (vl_api_ikev2_profile_set_auth_t * mp)
6793 {
6794   vl_api_ikev2_profile_set_auth_reply_t *rmp;
6795   int rv = 0;
6796
6797 #if IPSEC > 0
6798   vlib_main_t *vm = vlib_get_main ();
6799   clib_error_t *error;
6800   u8 *tmp = format (0, "%s", mp->name);
6801   u8 *data = vec_new (u8, mp->data_len);
6802   clib_memcpy (data, mp->data, mp->data_len);
6803   error = ikev2_set_profile_auth (vm, tmp, mp->auth_method, data, mp->is_hex);
6804   vec_free (tmp);
6805   vec_free (data);
6806   if (error)
6807     rv = VNET_API_ERROR_UNSPECIFIED;
6808 #else
6809   rv = VNET_API_ERROR_UNIMPLEMENTED;
6810 #endif
6811
6812   REPLY_MACRO (VL_API_IKEV2_PROFILE_SET_AUTH_REPLY);
6813 }
6814
6815 static void
6816 vl_api_ikev2_profile_set_id_t_handler (vl_api_ikev2_profile_set_id_t * mp)
6817 {
6818   vl_api_ikev2_profile_add_del_reply_t *rmp;
6819   int rv = 0;
6820
6821 #if IPSEC > 0
6822   vlib_main_t *vm = vlib_get_main ();
6823   clib_error_t *error;
6824   u8 *tmp = format (0, "%s", mp->name);
6825   u8 *data = vec_new (u8, mp->data_len);
6826   clib_memcpy (data, mp->data, mp->data_len);
6827   error = ikev2_set_profile_id (vm, tmp, mp->id_type, data, mp->is_local);
6828   vec_free (tmp);
6829   vec_free (data);
6830   if (error)
6831     rv = VNET_API_ERROR_UNSPECIFIED;
6832 #else
6833   rv = VNET_API_ERROR_UNIMPLEMENTED;
6834 #endif
6835
6836   REPLY_MACRO (VL_API_IKEV2_PROFILE_SET_ID_REPLY);
6837 }
6838
6839 static void
6840 vl_api_ikev2_profile_set_ts_t_handler (vl_api_ikev2_profile_set_ts_t * mp)
6841 {
6842   vl_api_ikev2_profile_set_ts_reply_t *rmp;
6843   int rv = 0;
6844
6845 #if IPSEC > 0
6846   vlib_main_t *vm = vlib_get_main ();
6847   clib_error_t *error;
6848   u8 *tmp = format (0, "%s", mp->name);
6849   error = ikev2_set_profile_ts (vm, tmp, mp->proto, mp->start_port,
6850                                 mp->end_port, (ip4_address_t) mp->start_addr,
6851                                 (ip4_address_t) mp->end_addr, mp->is_local);
6852   vec_free (tmp);
6853   if (error)
6854     rv = VNET_API_ERROR_UNSPECIFIED;
6855 #else
6856   rv = VNET_API_ERROR_UNIMPLEMENTED;
6857 #endif
6858
6859   REPLY_MACRO (VL_API_IKEV2_PROFILE_SET_TS_REPLY);
6860 }
6861
6862 static void
6863 vl_api_ikev2_set_local_key_t_handler (vl_api_ikev2_set_local_key_t * mp)
6864 {
6865   vl_api_ikev2_profile_set_ts_reply_t *rmp;
6866   int rv = 0;
6867
6868 #if IPSEC > 0
6869   vlib_main_t *vm = vlib_get_main ();
6870   clib_error_t *error;
6871
6872   error = ikev2_set_local_key (vm, mp->key_file);
6873   if (error)
6874     rv = VNET_API_ERROR_UNSPECIFIED;
6875 #else
6876   rv = VNET_API_ERROR_UNIMPLEMENTED;
6877 #endif
6878
6879   REPLY_MACRO (VL_API_IKEV2_SET_LOCAL_KEY_REPLY);
6880 }
6881
6882 static void
6883 vl_api_map_add_domain_t_handler (vl_api_map_add_domain_t * mp)
6884 {
6885   vl_api_map_add_domain_reply_t *rmp;
6886   int rv = 0;
6887   u32 index;
6888   u8 flags = mp->is_translation ? MAP_DOMAIN_TRANSLATION : 0;
6889   rv =
6890     map_create_domain ((ip4_address_t *) & mp->ip4_prefix, mp->ip4_prefix_len,
6891                        (ip6_address_t *) & mp->ip6_prefix, mp->ip6_prefix_len,
6892                        (ip6_address_t *) & mp->ip6_src,
6893                        mp->ip6_src_prefix_len, mp->ea_bits_len,
6894                        mp->psid_offset, mp->psid_length, &index,
6895                        ntohs (mp->mtu), flags);
6896
6897   /* *INDENT-OFF* */
6898   REPLY_MACRO2(VL_API_MAP_ADD_DOMAIN_REPLY,
6899   ({
6900     rmp->index = ntohl(index);
6901   }));
6902   /* *INDENT-ON* */
6903 }
6904
6905 static void
6906 vl_api_map_del_domain_t_handler (vl_api_map_del_domain_t * mp)
6907 {
6908   vl_api_map_del_domain_reply_t *rmp;
6909   int rv = 0;
6910
6911   rv = map_delete_domain (ntohl (mp->index));
6912
6913   REPLY_MACRO (VL_API_MAP_DEL_DOMAIN_REPLY);
6914 }
6915
6916 static void
6917 vl_api_map_add_del_rule_t_handler (vl_api_map_add_del_rule_t * mp)
6918 {
6919   vl_api_map_del_domain_reply_t *rmp;
6920   int rv = 0;
6921
6922   rv =
6923     map_add_del_psid (ntohl (mp->index), ntohs (mp->psid),
6924                       (ip6_address_t *) mp->ip6_dst, mp->is_add);
6925
6926   REPLY_MACRO (VL_API_MAP_ADD_DEL_RULE_REPLY);
6927 }
6928
6929 static void
6930 vl_api_map_domain_dump_t_handler (vl_api_map_domain_dump_t * mp)
6931 {
6932   vl_api_map_domain_details_t *rmp;
6933   map_main_t *mm = &map_main;
6934   map_domain_t *d;
6935   unix_shared_memory_queue_t *q;
6936
6937   if (pool_elts (mm->domains) == 0)
6938     return;
6939
6940   q = vl_api_client_index_to_input_queue (mp->client_index);
6941   if (q == 0)
6942     {
6943       return;
6944     }
6945
6946   /* *INDENT-OFF* */
6947   pool_foreach(d, mm->domains,
6948   ({
6949     /* Make sure every field is initiated (or don't skip the memset()) */
6950     rmp = vl_msg_api_alloc (sizeof (*rmp));
6951     rmp->_vl_msg_id = ntohs(VL_API_MAP_DOMAIN_DETAILS);
6952     rmp->domain_index = htonl(d - mm->domains);
6953     rmp->ea_bits_len = d->ea_bits_len;
6954     rmp->psid_offset = d->psid_offset;
6955     rmp->psid_length = d->psid_length;
6956     clib_memcpy(rmp->ip4_prefix, &d->ip4_prefix, sizeof(rmp->ip4_prefix));
6957     rmp->ip4_prefix_len = d->ip4_prefix_len;
6958     clib_memcpy(rmp->ip6_prefix, &d->ip6_prefix, sizeof(rmp->ip6_prefix));
6959     rmp->ip6_prefix_len = d->ip6_prefix_len;
6960     clib_memcpy(rmp->ip6_src, &d->ip6_src, sizeof(rmp->ip6_src));
6961     rmp->ip6_src_len = d->ip6_src_len;
6962     rmp->mtu = htons(d->mtu);
6963     rmp->is_translation = (d->flags & MAP_DOMAIN_TRANSLATION);
6964     rmp->context = mp->context;
6965
6966     vl_msg_api_send_shmem (q, (u8 *)&rmp);
6967   }));
6968   /* *INDENT-ON* */
6969 }
6970
6971 static void
6972 vl_api_map_rule_dump_t_handler (vl_api_map_rule_dump_t * mp)
6973 {
6974   unix_shared_memory_queue_t *q;
6975   u16 i;
6976   ip6_address_t dst;
6977   vl_api_map_rule_details_t *rmp;
6978   map_main_t *mm = &map_main;
6979   u32 domain_index = ntohl (mp->domain_index);
6980   map_domain_t *d;
6981
6982   if (pool_elts (mm->domains) == 0)
6983     return;
6984
6985   d = pool_elt_at_index (mm->domains, domain_index);
6986   if (!d || !d->rules)
6987     {
6988       return;
6989     }
6990
6991   q = vl_api_client_index_to_input_queue (mp->client_index);
6992   if (q == 0)
6993     {
6994       return;
6995     }
6996
6997   for (i = 0; i < (0x1 << d->psid_length); i++)
6998     {
6999       dst = d->rules[i];
7000       if (dst.as_u64[0] == 0 && dst.as_u64[1] == 0)
7001         {
7002           continue;
7003         }
7004       rmp = vl_msg_api_alloc (sizeof (*rmp));
7005       memset (rmp, 0, sizeof (*rmp));
7006       rmp->_vl_msg_id = ntohs (VL_API_MAP_RULE_DETAILS);
7007       rmp->psid = htons (i);
7008       clib_memcpy (rmp->ip6_dst, &dst, sizeof (rmp->ip6_dst));
7009       rmp->context = mp->context;
7010       vl_msg_api_send_shmem (q, (u8 *) & rmp);
7011     }
7012 }
7013
7014 static void
7015 vl_api_map_summary_stats_t_handler (vl_api_map_summary_stats_t * mp)
7016 {
7017   vl_api_map_summary_stats_reply_t *rmp;
7018   vlib_combined_counter_main_t *cm;
7019   vlib_counter_t v;
7020   int i, which;
7021   u64 total_pkts[VLIB_N_RX_TX];
7022   u64 total_bytes[VLIB_N_RX_TX];
7023   map_main_t *mm = &map_main;
7024   unix_shared_memory_queue_t *q =
7025     vl_api_client_index_to_input_queue (mp->client_index);
7026
7027   if (!q)
7028     return;
7029
7030   rmp = vl_msg_api_alloc (sizeof (*rmp));
7031   rmp->_vl_msg_id = ntohs (VL_API_MAP_SUMMARY_STATS_REPLY);
7032   rmp->context = mp->context;
7033   rmp->retval = 0;
7034
7035   memset (total_pkts, 0, sizeof (total_pkts));
7036   memset (total_bytes, 0, sizeof (total_bytes));
7037
7038   map_domain_counter_lock (mm);
7039   vec_foreach (cm, mm->domain_counters)
7040   {
7041     which = cm - mm->domain_counters;
7042
7043     for (i = 0; i < vec_len (cm->maxi); i++)
7044       {
7045         vlib_get_combined_counter (cm, i, &v);
7046         total_pkts[which] += v.packets;
7047         total_bytes[which] += v.bytes;
7048       }
7049   }
7050
7051   map_domain_counter_unlock (mm);
7052
7053   /* Note: in network byte order! */
7054   rmp->total_pkts[MAP_DOMAIN_COUNTER_RX] =
7055     clib_host_to_net_u64 (total_pkts[MAP_DOMAIN_COUNTER_RX]);
7056   rmp->total_bytes[MAP_DOMAIN_COUNTER_RX] =
7057     clib_host_to_net_u64 (total_bytes[MAP_DOMAIN_COUNTER_RX]);
7058   rmp->total_pkts[MAP_DOMAIN_COUNTER_TX] =
7059     clib_host_to_net_u64 (total_pkts[MAP_DOMAIN_COUNTER_TX]);
7060   rmp->total_bytes[MAP_DOMAIN_COUNTER_TX] =
7061     clib_host_to_net_u64 (total_bytes[MAP_DOMAIN_COUNTER_TX]);
7062   rmp->total_bindings = clib_host_to_net_u64 (pool_elts (mm->domains));
7063   rmp->total_ip4_fragments = 0; // Not yet implemented. Should be a simple counter.
7064   rmp->total_security_check[MAP_DOMAIN_COUNTER_TX] =
7065     clib_host_to_net_u64 (map_error_counter_get
7066                           (ip4_map_node.index, MAP_ERROR_ENCAP_SEC_CHECK));
7067   rmp->total_security_check[MAP_DOMAIN_COUNTER_RX] =
7068     clib_host_to_net_u64 (map_error_counter_get
7069                           (ip4_map_node.index, MAP_ERROR_DECAP_SEC_CHECK));
7070
7071   vl_msg_api_send_shmem (q, (u8 *) & rmp);
7072 }
7073
7074 static void
7075 vl_api_ipsec_sa_set_key_t_handler (vl_api_ipsec_sa_set_key_t * mp)
7076 {
7077   vlib_main_t *vm __attribute__ ((unused)) = vlib_get_main ();
7078   vl_api_ipsec_sa_set_key_reply_t *rmp;
7079   int rv;
7080 #if IPSEC > 0
7081   ipsec_sa_t sa;
7082   sa.id = ntohl (mp->sa_id);
7083   sa.crypto_key_len = mp->crypto_key_length;
7084   clib_memcpy (&sa.crypto_key, mp->crypto_key, sizeof (sa.crypto_key));
7085   sa.integ_key_len = mp->integrity_key_length;
7086   clib_memcpy (&sa.integ_key, mp->integrity_key, sizeof (sa.integ_key));
7087
7088   rv = ipsec_set_sa_key (vm, &sa);
7089 #else
7090   rv = VNET_API_ERROR_UNIMPLEMENTED;
7091 #endif
7092
7093   REPLY_MACRO (VL_API_IPSEC_SA_SET_KEY_REPLY);
7094 }
7095
7096 static void vl_api_cop_interface_enable_disable_t_handler
7097   (vl_api_cop_interface_enable_disable_t * mp)
7098 {
7099   vl_api_cop_interface_enable_disable_reply_t *rmp;
7100   int rv;
7101   u32 sw_if_index = ntohl (mp->sw_if_index);
7102   int enable_disable;
7103
7104   VALIDATE_SW_IF_INDEX (mp);
7105
7106   enable_disable = (int) mp->enable_disable;
7107
7108   rv = cop_interface_enable_disable (sw_if_index, enable_disable);
7109
7110   BAD_SW_IF_INDEX_LABEL;
7111
7112   REPLY_MACRO (VL_API_COP_INTERFACE_ENABLE_DISABLE_REPLY);
7113 }
7114
7115 static void vl_api_cop_whitelist_enable_disable_t_handler
7116   (vl_api_cop_whitelist_enable_disable_t * mp)
7117 {
7118   vl_api_cop_whitelist_enable_disable_reply_t *rmp;
7119   cop_whitelist_enable_disable_args_t _a, *a = &_a;
7120   u32 sw_if_index = ntohl (mp->sw_if_index);
7121   int rv;
7122
7123   VALIDATE_SW_IF_INDEX (mp);
7124
7125   a->sw_if_index = sw_if_index;
7126   a->ip4 = mp->ip4;
7127   a->ip6 = mp->ip6;
7128   a->default_cop = mp->default_cop;
7129   a->fib_id = ntohl (mp->fib_id);
7130
7131   rv = cop_whitelist_enable_disable (a);
7132
7133   BAD_SW_IF_INDEX_LABEL;
7134
7135   REPLY_MACRO (VL_API_COP_WHITELIST_ENABLE_DISABLE_REPLY);
7136 }
7137
7138 static void
7139 vl_api_get_node_graph_t_handler (vl_api_get_node_graph_t * mp)
7140 {
7141   int rv = 0;
7142   u8 *vector = 0;
7143   api_main_t *am = &api_main;
7144   vlib_main_t *vm = vlib_get_main ();
7145   void *oldheap;
7146   vl_api_get_node_graph_reply_t *rmp;
7147
7148   pthread_mutex_lock (&am->vlib_rp->mutex);
7149   oldheap = svm_push_data_heap (am->vlib_rp);
7150
7151   /*
7152    * Keep the number of memcpy ops to a minimum (e.g. 1).
7153    */
7154   vec_validate (vector, 16384);
7155   vec_reset_length (vector);
7156
7157   /* $$$$ FIXME */
7158   vector = vlib_node_serialize (&vm->node_main, vector,
7159                                 (u32) ~ 0 /* all threads */ ,
7160                                 1 /* include nexts */ ,
7161                                 1 /* include stats */ );
7162
7163   svm_pop_heap (oldheap);
7164   pthread_mutex_unlock (&am->vlib_rp->mutex);
7165
7166   /* *INDENT-OFF* */
7167   REPLY_MACRO2(VL_API_GET_NODE_GRAPH_REPLY,
7168   ({
7169     rmp->reply_in_shmem = (uword) vector;
7170   }));
7171   /* *INDENT-ON* */
7172 }
7173
7174 static void
7175 vl_api_ioam_enable_t_handler (vl_api_ioam_enable_t * mp)
7176 {
7177   int rv = 0;
7178   vl_api_ioam_enable_reply_t *rmp;
7179   clib_error_t *error;
7180
7181   /* Ignoring the profile id as currently a single profile
7182    * is supported */
7183   error = ip6_ioam_enable (mp->trace_enable, mp->pot_enable,
7184                            mp->seqno, mp->analyse);
7185   if (error)
7186     {
7187       clib_error_report (error);
7188       rv = clib_error_get_code (error);
7189     }
7190
7191   REPLY_MACRO (VL_API_IOAM_ENABLE_REPLY);
7192 }
7193
7194 static void
7195 vl_api_ioam_disable_t_handler (vl_api_ioam_disable_t * mp)
7196 {
7197   int rv = 0;
7198   vl_api_ioam_disable_reply_t *rmp;
7199   clib_error_t *error;
7200
7201   error = clear_ioam_rewrite_fn ();
7202   if (error)
7203     {
7204       clib_error_report (error);
7205       rv = clib_error_get_code (error);
7206     }
7207
7208   REPLY_MACRO (VL_API_IOAM_DISABLE_REPLY);
7209 }
7210
7211 static void
7212 vl_api_af_packet_create_t_handler (vl_api_af_packet_create_t * mp)
7213 {
7214   vlib_main_t *vm = vlib_get_main ();
7215   vl_api_af_packet_create_reply_t *rmp;
7216   int rv = 0;
7217   u8 *host_if_name = NULL;
7218   u32 sw_if_index;
7219
7220   host_if_name = format (0, "%s", mp->host_if_name);
7221   vec_add1 (host_if_name, 0);
7222
7223   rv = af_packet_create_if (vm, host_if_name,
7224                             mp->use_random_hw_addr ? 0 : mp->hw_addr,
7225                             &sw_if_index);
7226
7227   vec_free (host_if_name);
7228
7229   /* *INDENT-OFF* */
7230   REPLY_MACRO2(VL_API_AF_PACKET_CREATE_REPLY,
7231   ({
7232     rmp->sw_if_index = clib_host_to_net_u32(sw_if_index);
7233   }));
7234   /* *INDENT-ON* */
7235 }
7236
7237 static void
7238 vl_api_af_packet_delete_t_handler (vl_api_af_packet_delete_t * mp)
7239 {
7240   vlib_main_t *vm = vlib_get_main ();
7241   vl_api_af_packet_delete_reply_t *rmp;
7242   int rv = 0;
7243   u8 *host_if_name = NULL;
7244
7245   host_if_name = format (0, "%s", mp->host_if_name);
7246   vec_add1 (host_if_name, 0);
7247
7248   rv = af_packet_delete_if (vm, host_if_name);
7249
7250   vec_free (host_if_name);
7251
7252   REPLY_MACRO (VL_API_AF_PACKET_DELETE_REPLY);
7253 }
7254
7255 static void
7256 vl_api_policer_add_del_t_handler (vl_api_policer_add_del_t * mp)
7257 {
7258   vlib_main_t *vm = vlib_get_main ();
7259   vl_api_policer_add_del_reply_t *rmp;
7260   int rv = 0;
7261   u8 *name = NULL;
7262   sse2_qos_pol_cfg_params_st cfg;
7263   clib_error_t *error;
7264   u32 policer_index;
7265
7266   name = format (0, "%s", mp->name);
7267
7268   memset (&cfg, 0, sizeof (cfg));
7269   cfg.rfc = mp->type;
7270   cfg.rnd_type = mp->round_type;
7271   cfg.rate_type = mp->rate_type;
7272   cfg.rb.kbps.cir_kbps = mp->cir;
7273   cfg.rb.kbps.eir_kbps = mp->eir;
7274   cfg.rb.kbps.cb_bytes = mp->cb;
7275   cfg.rb.kbps.eb_bytes = mp->eb;
7276   cfg.conform_action.action_type = mp->conform_action_type;
7277   cfg.conform_action.dscp = mp->conform_dscp;
7278   cfg.exceed_action.action_type = mp->exceed_action_type;
7279   cfg.exceed_action.dscp = mp->exceed_dscp;
7280   cfg.violate_action.action_type = mp->violate_action_type;
7281   cfg.violate_action.dscp = mp->violate_dscp;
7282   cfg.color_aware = mp->color_aware;
7283
7284   error = policer_add_del (vm, name, &cfg, &policer_index, mp->is_add);
7285
7286   if (error)
7287     rv = VNET_API_ERROR_UNSPECIFIED;
7288
7289   /* *INDENT-OFF* */
7290   REPLY_MACRO2(VL_API_POLICER_ADD_DEL_REPLY,
7291   ({
7292     if (rv == 0 &&  mp->is_add)
7293       rmp->policer_index = ntohl(policer_index);
7294     else
7295       rmp->policer_index = ~0;
7296   }));
7297   /* *INDENT-ON* */
7298 }
7299
7300 static void
7301 send_policer_details (u8 * name,
7302                       sse2_qos_pol_cfg_params_st * config,
7303                       policer_read_response_type_st * templ,
7304                       unix_shared_memory_queue_t * q, u32 context)
7305 {
7306   vl_api_policer_details_t *mp;
7307
7308   mp = vl_msg_api_alloc (sizeof (*mp));
7309   memset (mp, 0, sizeof (*mp));
7310   mp->_vl_msg_id = ntohs (VL_API_POLICER_DETAILS);
7311   mp->context = context;
7312   mp->cir = htonl (config->rb.kbps.cir_kbps);
7313   mp->eir = htonl (config->rb.kbps.eir_kbps);
7314   mp->cb = htonl (config->rb.kbps.cb_bytes);
7315   mp->eb = htonl (config->rb.kbps.eb_bytes);
7316   mp->rate_type = config->rate_type;
7317   mp->round_type = config->rnd_type;
7318   mp->type = config->rfc;
7319   mp->conform_action_type = config->conform_action.action_type;
7320   mp->conform_dscp = config->conform_action.dscp;
7321   mp->exceed_action_type = config->exceed_action.action_type;
7322   mp->exceed_dscp = config->exceed_action.dscp;
7323   mp->violate_action_type = config->violate_action.action_type;
7324   mp->violate_dscp = config->violate_action.dscp;
7325   mp->single_rate = templ->single_rate ? 1 : 0;
7326   mp->color_aware = templ->color_aware ? 1 : 0;
7327   mp->scale = htonl (templ->scale);
7328   mp->cir_tokens_per_period = htonl (templ->cir_tokens_per_period);
7329   mp->pir_tokens_per_period = htonl (templ->pir_tokens_per_period);
7330   mp->current_limit = htonl (templ->current_limit);
7331   mp->current_bucket = htonl (templ->current_bucket);
7332   mp->extended_limit = htonl (templ->extended_limit);
7333   mp->extended_bucket = htonl (templ->extended_bucket);
7334   mp->last_update_time = clib_host_to_net_u64 (templ->last_update_time);
7335
7336   strncpy ((char *) mp->name, (char *) name, ARRAY_LEN (mp->name) - 1);
7337
7338   vl_msg_api_send_shmem (q, (u8 *) & mp);
7339 }
7340
7341 static void
7342 vl_api_policer_dump_t_handler (vl_api_policer_dump_t * mp)
7343 {
7344   unix_shared_memory_queue_t *q;
7345   vnet_policer_main_t *pm = &vnet_policer_main;
7346   hash_pair_t *hp;
7347   uword *p;
7348   u32 pool_index;
7349   u8 *match_name = 0;
7350   u8 *name;
7351   sse2_qos_pol_cfg_params_st *config;
7352   policer_read_response_type_st *templ;
7353
7354   q = vl_api_client_index_to_input_queue (mp->client_index);
7355   if (q == 0)
7356     return;
7357
7358   if (mp->match_name_valid)
7359     {
7360       match_name = format (0, "%s%c", mp->match_name, 0);
7361     }
7362
7363   if (mp->match_name_valid)
7364     {
7365       p = hash_get_mem (pm->policer_config_by_name, match_name);
7366       if (p)
7367         {
7368           pool_index = p[0];
7369           config = pool_elt_at_index (pm->configs, pool_index);
7370           templ = pool_elt_at_index (pm->policer_templates, pool_index);
7371           send_policer_details (match_name, config, templ, q, mp->context);
7372         }
7373     }
7374   else
7375     {
7376       /* *INDENT-OFF* */
7377       hash_foreach_pair (hp, pm->policer_config_by_name,
7378       ({
7379         name = (u8 *) hp->key;
7380         pool_index = hp->value[0];
7381         config = pool_elt_at_index (pm->configs, pool_index);
7382         templ = pool_elt_at_index (pm->policer_templates, pool_index);
7383         send_policer_details(name, config, templ, q, mp->context);
7384       }));
7385       /* *INDENT-ON* */
7386     }
7387 }
7388
7389 static void
7390   vl_api_policer_classify_set_interface_t_handler
7391   (vl_api_policer_classify_set_interface_t * mp)
7392 {
7393   vlib_main_t *vm = vlib_get_main ();
7394   vl_api_policer_classify_set_interface_reply_t *rmp;
7395   int rv;
7396   u32 sw_if_index, ip4_table_index, ip6_table_index, l2_table_index;
7397
7398   ip4_table_index = ntohl (mp->ip4_table_index);
7399   ip6_table_index = ntohl (mp->ip6_table_index);
7400   l2_table_index = ntohl (mp->l2_table_index);
7401   sw_if_index = ntohl (mp->sw_if_index);
7402
7403   VALIDATE_SW_IF_INDEX (mp);
7404
7405   rv = vnet_set_policer_classify_intfc (vm, sw_if_index, ip4_table_index,
7406                                         ip6_table_index, l2_table_index,
7407                                         mp->is_add);
7408
7409   BAD_SW_IF_INDEX_LABEL;
7410
7411   REPLY_MACRO (VL_API_POLICER_CLASSIFY_SET_INTERFACE_REPLY);
7412 }
7413
7414 static void
7415 send_policer_classify_details (u32 sw_if_index,
7416                                u32 table_index,
7417                                unix_shared_memory_queue_t * q, u32 context)
7418 {
7419   vl_api_policer_classify_details_t *mp;
7420
7421   mp = vl_msg_api_alloc (sizeof (*mp));
7422   memset (mp, 0, sizeof (*mp));
7423   mp->_vl_msg_id = ntohs (VL_API_POLICER_CLASSIFY_DETAILS);
7424   mp->context = context;
7425   mp->sw_if_index = htonl (sw_if_index);
7426   mp->table_index = htonl (table_index);
7427
7428   vl_msg_api_send_shmem (q, (u8 *) & mp);
7429 }
7430
7431 static void
7432 vl_api_policer_classify_dump_t_handler (vl_api_policer_classify_dump_t * mp)
7433 {
7434   unix_shared_memory_queue_t *q;
7435   policer_classify_main_t *pcm = &policer_classify_main;
7436   u32 *vec_tbl;
7437   int i;
7438
7439   q = vl_api_client_index_to_input_queue (mp->client_index);
7440   if (q == 0)
7441     return;
7442
7443   vec_tbl = pcm->classify_table_index_by_sw_if_index[mp->type];
7444
7445   if (vec_len (vec_tbl))
7446     {
7447       for (i = 0; i < vec_len (vec_tbl); i++)
7448         {
7449           if (vec_elt (vec_tbl, i) == ~0)
7450             continue;
7451
7452           send_policer_classify_details (i, vec_elt (vec_tbl, i), q,
7453                                          mp->context);
7454         }
7455     }
7456 }
7457
7458 static void
7459 vl_api_netmap_create_t_handler (vl_api_netmap_create_t * mp)
7460 {
7461   vlib_main_t *vm = vlib_get_main ();
7462   vl_api_netmap_create_reply_t *rmp;
7463   int rv = 0;
7464   u8 *if_name = NULL;
7465
7466   if_name = format (0, "%s", mp->netmap_if_name);
7467   vec_add1 (if_name, 0);
7468
7469   rv =
7470     netmap_create_if (vm, if_name, mp->use_random_hw_addr ? 0 : mp->hw_addr,
7471                       mp->is_pipe, mp->is_master, 0);
7472
7473   vec_free (if_name);
7474
7475   REPLY_MACRO (VL_API_NETMAP_CREATE_REPLY);
7476 }
7477
7478 static void
7479 vl_api_netmap_delete_t_handler (vl_api_netmap_delete_t * mp)
7480 {
7481   vlib_main_t *vm = vlib_get_main ();
7482   vl_api_netmap_delete_reply_t *rmp;
7483   int rv = 0;
7484   u8 *if_name = NULL;
7485
7486   if_name = format (0, "%s", mp->netmap_if_name);
7487   vec_add1 (if_name, 0);
7488
7489   rv = netmap_delete_if (vm, if_name);
7490
7491   vec_free (if_name);
7492
7493   REPLY_MACRO (VL_API_NETMAP_DELETE_REPLY);
7494 }
7495
7496 static void
7497 vl_api_mpls_eth_tunnel_details_t_handler (vl_api_mpls_eth_tunnel_details_t *
7498                                           mp)
7499 {
7500   clib_warning ("BUG");
7501 }
7502
7503 static void
7504 send_mpls_eth_tunnel_entry (vpe_api_main_t * am,
7505                             unix_shared_memory_queue_t * q,
7506                             mpls_eth_tunnel_t * et, u32 index, u32 context)
7507 {
7508   mpls_main_t *mm = &mpls_main;
7509   mpls_encap_t *e;
7510   int i;
7511   u32 nlabels;
7512   vl_api_mpls_eth_tunnel_details_t *mp;
7513
7514   e = pool_elt_at_index (mm->encaps, et->encap_index);
7515   nlabels = vec_len (e->labels);
7516
7517   mp = vl_msg_api_alloc (sizeof (*mp) + nlabels * sizeof (u32));
7518   memset (mp, 0, sizeof (*mp));
7519   mp->_vl_msg_id = ntohs (VL_API_MPLS_ETH_TUNNEL_DETAILS);
7520   mp->context = context;
7521
7522   mp->tunnel_index = htonl (index);
7523   memcpy (mp->tunnel_dst_mac, et->tunnel_dst, 6);
7524   mp->intfc_address = et->intfc_address.as_u32;
7525   mp->tx_sw_if_index = htonl (et->tx_sw_if_index);
7526   mp->inner_fib_index = htonl (et->inner_fib_index);
7527   mp->mask_width = htonl (et->mask_width);
7528   mp->encap_index = htonl (et->encap_index);
7529   mp->hw_if_index = htonl (et->hw_if_index);
7530   mp->l2_only = htonl (et->l2_only);
7531   mp->nlabels = htonl (nlabels);
7532
7533   for (i = 0; i < nlabels; i++)
7534     {
7535       mp->labels[i] =
7536         htonl (vnet_mpls_uc_get_label
7537                (clib_host_to_net_u32 (e->labels[i].label_exp_s_ttl)));
7538     }
7539
7540   vl_msg_api_send_shmem (q, (u8 *) & mp);
7541 }
7542
7543 static void
7544 vl_api_mpls_eth_tunnel_dump_t_handler (vl_api_mpls_eth_tunnel_dump_t * mp)
7545 {
7546   vpe_api_main_t *am = &vpe_api_main;
7547   unix_shared_memory_queue_t *q;
7548   mpls_main_t *mm = &mpls_main;
7549   mpls_eth_tunnel_t *et;
7550   u32 index = ntohl (mp->tunnel_index);
7551
7552   q = vl_api_client_index_to_input_queue (mp->client_index);
7553   if (q == 0)
7554     return;
7555
7556   if (index != ~0)
7557     {
7558       if (!pool_is_free_index (mm->eth_tunnels, index))
7559         {
7560           et = pool_elt_at_index (mm->eth_tunnels, index);
7561           send_mpls_eth_tunnel_entry (am, q, et, et - mm->eth_tunnels,
7562                                       mp->context);
7563         }
7564     }
7565   else
7566     {
7567       /* *INDENT-OFF* */
7568       pool_foreach (et, mm->eth_tunnels,
7569       ({
7570         send_mpls_eth_tunnel_entry (am, q, et, et - mm->eth_tunnels,
7571                                     mp->context);
7572       }));
7573       /* *INDENT-ON* */
7574     }
7575 }
7576
7577 static void
7578 vl_api_mpls_fib_details_t_handler (vl_api_mpls_fib_details_t * mp)
7579 {
7580   clib_warning ("BUG");
7581 }
7582
7583 static void
7584 vl_api_mpls_fib_details_t_endian (vl_api_mpls_fib_details_t * mp)
7585 {
7586   clib_warning ("BUG");
7587 }
7588
7589 static void
7590 vl_api_mpls_fib_details_t_print (vl_api_mpls_fib_details_t * mp)
7591 {
7592   clib_warning ("BUG");
7593 }
7594
7595 static void
7596 send_mpls_fib_details (vpe_api_main_t * am,
7597                        unix_shared_memory_queue_t * q,
7598                        u32 table_id, u32 label, u32 eos,
7599                        fib_route_path_encode_t * api_rpaths, u32 context)
7600 {
7601   vl_api_mpls_fib_details_t *mp;
7602   fib_route_path_encode_t *api_rpath;
7603   vl_api_fib_path_t *fp;
7604   int is_ip4, path_count;
7605
7606   path_count = vec_len (api_rpaths);
7607   mp = vl_msg_api_alloc (sizeof (*mp) + path_count * sizeof (*fp));
7608   if (!mp)
7609     return;
7610   memset (mp, 0, sizeof (*mp));
7611   mp->_vl_msg_id = ntohs (VL_API_MPLS_FIB_DETAILS);
7612   mp->context = context;
7613
7614   mp->table_id = htonl (table_id);
7615   mp->eos_bit = eos;
7616   mp->label = htonl (label);
7617
7618   mp->count = htonl (path_count);
7619   fp = mp->path;
7620   vec_foreach (api_rpath, api_rpaths)
7621   {
7622     memset (fp, 0, sizeof (*fp));
7623     fp->weight = htonl (api_rpath->rpath.frp_weight);
7624     fp->sw_if_index = htonl (api_rpath->rpath.frp_sw_if_index);
7625     memcpy (fp->next_hop, &api_rpath->rpath.frp_addr, sizeof (fp->next_hop));
7626     if (api_rpath->rpath.frp_proto == FIB_PROTOCOL_IP4)
7627       fp->afi = IP46_TYPE_IP4;
7628     else if (api_rpath->rpath.frp_proto == FIB_PROTOCOL_IP6)
7629       fp->afi = IP46_TYPE_IP6;
7630     else
7631       {
7632         is_ip4 = ip46_address_is_ip4 (&api_rpath->rpath.frp_addr);
7633         if (is_ip4)
7634           fp->afi = IP46_TYPE_IP4;
7635         else
7636           fp->afi = IP46_TYPE_IP6;
7637       }
7638     fp++;
7639   }
7640
7641   vl_msg_api_send_shmem (q, (u8 *) & mp);
7642 }
7643
7644 static void
7645 vl_api_mpls_fib_dump_t_handler (vl_api_mpls_fib_dump_t * mp)
7646 {
7647   vpe_api_main_t *am = &vpe_api_main;
7648   unix_shared_memory_queue_t *q;
7649   mpls_main_t *mm = &mpls_main;
7650   fib_table_t *fib_table;
7651   fib_node_index_t lfei, *lfeip, *lfeis = NULL;
7652   mpls_label_t key;
7653   fib_prefix_t pfx;
7654   u32 fib_index;
7655   fib_route_path_encode_t *api_rpaths;
7656
7657   q = vl_api_client_index_to_input_queue (mp->client_index);
7658   if (q == 0)
7659     return;
7660
7661   /* *INDENT-OFF* */
7662   pool_foreach (fib_table, mm->fibs,
7663   ({
7664     hash_foreach(key, lfei, fib_table->mpls.mf_entries,
7665     ({
7666         vec_add1(lfeis, lfei);
7667     }));
7668   }));
7669   vec_sort_with_function(lfeis, fib_entry_cmp_for_sort);
7670
7671   vec_foreach(lfeip, lfeis)
7672   {
7673     fib_entry_get_prefix(*lfeip, &pfx);
7674     fib_index = fib_entry_get_fib_index(*lfeip);
7675     fib_table = fib_table_get(fib_index, pfx.fp_proto);
7676     api_rpaths = NULL;
7677     fib_entry_encode(*lfeip, &api_rpaths);
7678     send_mpls_fib_details (am, q,
7679                            fib_table->ft_table_id,
7680                            pfx.fp_label,
7681                            pfx.fp_eos,
7682                            api_rpaths,
7683                            mp->context);
7684     vec_free(api_rpaths);
7685   }
7686
7687   vec_free (lfeis);
7688 }
7689
7690 static void
7691 vl_api_ip_fib_details_t_handler (vl_api_ip_fib_details_t * mp)
7692 {
7693   clib_warning ("BUG");
7694 }
7695
7696 static void
7697 vl_api_ip_fib_details_t_endian (vl_api_ip_fib_details_t * mp)
7698 {
7699   clib_warning ("BUG");
7700 }
7701
7702 static void
7703 vl_api_ip_fib_details_t_print (vl_api_ip_fib_details_t * mp)
7704 {
7705   clib_warning ("BUG");
7706 }
7707
7708 static void
7709 send_ip_fib_details (vpe_api_main_t * am,
7710                      unix_shared_memory_queue_t * q,
7711                      u32 table_id, fib_prefix_t *pfx,
7712                      fib_route_path_encode_t *api_rpaths, u32 context)
7713 {
7714   vl_api_ip_fib_details_t *mp;
7715   fib_route_path_encode_t *api_rpath;
7716   vl_api_fib_path_t *fp;
7717   int is_ip4, path_count;
7718
7719   path_count = vec_len(api_rpaths);
7720   mp = vl_msg_api_alloc (sizeof (*mp) + path_count * sizeof (*fp));
7721   if (!mp)
7722     return;
7723   memset (mp, 0, sizeof (*mp));
7724   mp->_vl_msg_id = ntohs (VL_API_IP_FIB_DETAILS);
7725   mp->context = context;
7726
7727   mp->table_id = htonl (table_id);
7728   mp->address_length = pfx->fp_len;
7729   memcpy(mp->address, &pfx->fp_addr.ip4, sizeof(pfx->fp_addr.ip4));
7730
7731   mp->count = htonl (path_count);
7732   fp = mp->path;
7733   vec_foreach(api_rpath, api_rpaths)
7734   {
7735     memset (fp, 0, sizeof (*fp));
7736     switch (api_rpath->dpo.dpoi_type)
7737       {
7738       case DPO_RECEIVE:
7739         fp->is_local = true;
7740         break;
7741       case DPO_DROP:
7742         fp->is_drop = true;
7743         break;
7744       case DPO_IP_NULL:
7745         switch (api_rpath->dpo.dpoi_index)
7746           {
7747           case IP_NULL_ACTION_NONE:
7748             fp->is_drop = true;
7749             break;
7750           case IP_NULL_ACTION_SEND_ICMP_UNREACH:
7751             fp->is_unreach = true;
7752             break;
7753           case IP_NULL_ACTION_SEND_ICMP_PROHIBIT:
7754             fp->is_prohibit = true;
7755             break;
7756           default:
7757             break;
7758           }
7759         break;
7760       default:
7761         break;
7762       }
7763     fp->weight = htonl(api_rpath->rpath.frp_weight);
7764     fp->sw_if_index = htonl(api_rpath->rpath.frp_sw_if_index);
7765     memcpy(fp->next_hop, &api_rpath->rpath.frp_addr, sizeof(fp->next_hop));
7766     if (api_rpath->rpath.frp_proto == FIB_PROTOCOL_IP4)
7767         fp->afi = IP46_TYPE_IP4;
7768     else if (api_rpath->rpath.frp_proto == FIB_PROTOCOL_IP6)
7769         fp->afi = IP46_TYPE_IP6;
7770     else
7771       {
7772         is_ip4 = ip46_address_is_ip4 (&api_rpath->rpath.frp_addr);
7773         if (is_ip4)
7774             fp->afi = IP46_TYPE_IP4;
7775         else
7776             fp->afi = IP46_TYPE_IP6;
7777       }
7778     fp++;
7779   }
7780
7781   vl_msg_api_send_shmem (q, (u8 *) & mp);
7782 }
7783
7784 static void
7785 vl_api_ip_fib_dump_t_handler (vl_api_ip_fib_dump_t * mp)
7786 {
7787   vpe_api_main_t *am = &vpe_api_main;
7788   unix_shared_memory_queue_t *q;
7789   ip4_main_t *im = &ip4_main;
7790   fib_table_t *fib_table;
7791   fib_node_index_t lfei, *lfeip, *lfeis = NULL;
7792   mpls_label_t key;
7793   fib_prefix_t pfx;
7794   u32 fib_index;
7795   fib_route_path_encode_t *api_rpaths;
7796   int i;
7797
7798   q = vl_api_client_index_to_input_queue (mp->client_index);
7799   if (q == 0)
7800     return;
7801
7802   /* *INDENT-OFF* */
7803   pool_foreach (fib_table, im->fibs,
7804   ({
7805     for (i = 0; i < ARRAY_LEN (fib_table->v4.fib_entry_by_dst_address); i++)
7806       {
7807         hash_foreach(key, lfei, fib_table->v4.fib_entry_by_dst_address[i],
7808         ({
7809           vec_add1(lfeis, lfei);
7810         }));
7811       }
7812   }));
7813
7814   vec_sort_with_function(lfeis, fib_entry_cmp_for_sort);
7815
7816   vec_foreach(lfeip, lfeis)
7817   {
7818     fib_entry_get_prefix(*lfeip, &pfx);
7819     fib_index = fib_entry_get_fib_index(*lfeip);
7820     fib_table = fib_table_get(fib_index, pfx.fp_proto);
7821     api_rpaths = NULL;
7822     fib_entry_encode(*lfeip, &api_rpaths);
7823     send_ip_fib_details (am, q,
7824                          fib_table->ft_table_id,
7825                          &pfx,
7826                          api_rpaths,
7827                          mp->context);
7828     vec_free(api_rpaths);
7829   }
7830
7831   vec_free (lfeis);
7832 }
7833
7834 static void
7835 vl_api_ip6_fib_details_t_handler (vl_api_ip6_fib_details_t * mp)
7836 {
7837   clib_warning ("BUG");
7838 }
7839
7840 static void
7841 vl_api_ip6_fib_details_t_endian (vl_api_ip6_fib_details_t * mp)
7842 {
7843   clib_warning ("BUG");
7844 }
7845
7846 static void
7847 vl_api_ip6_fib_details_t_print (vl_api_ip6_fib_details_t * mp)
7848 {
7849   clib_warning ("BUG");
7850 }
7851
7852 static void
7853 send_ip6_fib_details (vpe_api_main_t * am,
7854                       unix_shared_memory_queue_t * q,
7855                       u32 table_id, fib_prefix_t *pfx,
7856                       fib_route_path_encode_t *api_rpaths, u32 context)
7857 {
7858   vl_api_ip6_fib_details_t *mp;
7859   fib_route_path_encode_t *api_rpath;
7860   vl_api_fib_path_t *fp;
7861   int is_ip4, path_count;
7862
7863   path_count = vec_len(api_rpaths);
7864   mp = vl_msg_api_alloc (sizeof (*mp) + path_count * sizeof (*fp));
7865   if (!mp)
7866     return;
7867   memset (mp, 0, sizeof (*mp));
7868   mp->_vl_msg_id = ntohs (VL_API_IP6_FIB_DETAILS);
7869   mp->context = context;
7870
7871   mp->table_id = htonl (table_id);
7872   mp->address_length = pfx->fp_len;
7873   memcpy(mp->address, &pfx->fp_addr.ip6, sizeof(pfx->fp_addr.ip6));
7874
7875   mp->count = htonl (path_count);
7876   fp = mp->path;
7877   vec_foreach(api_rpath, api_rpaths)
7878   {
7879     memset (fp, 0, sizeof (*fp));
7880     switch (api_rpath->dpo.dpoi_type)
7881       {
7882       case DPO_RECEIVE:
7883         fp->is_local = true;
7884         break;
7885       case DPO_DROP:
7886         fp->is_drop = true;
7887         break;
7888       case DPO_IP_NULL:
7889         switch (api_rpath->dpo.dpoi_index)
7890           {
7891           case IP_NULL_DPO_ACTION_NUM+IP_NULL_ACTION_NONE:
7892             fp->is_drop = true;
7893             break;
7894           case IP_NULL_DPO_ACTION_NUM+IP_NULL_ACTION_SEND_ICMP_UNREACH:
7895             fp->is_unreach = true;
7896             break;
7897           case IP_NULL_DPO_ACTION_NUM+IP_NULL_ACTION_SEND_ICMP_PROHIBIT:
7898             fp->is_prohibit = true;
7899             break;
7900           default:
7901             break;
7902           }
7903         break;
7904       default:
7905         break;
7906       }
7907     fp->weight = htonl(api_rpath->rpath.frp_weight);
7908     fp->sw_if_index = htonl(api_rpath->rpath.frp_sw_if_index);
7909     memcpy(fp->next_hop, &api_rpath->rpath.frp_addr, sizeof(fp->next_hop));
7910     if (api_rpath->rpath.frp_proto == FIB_PROTOCOL_IP4)
7911       fp->afi = IP46_TYPE_IP4;
7912     else if (api_rpath->rpath.frp_proto == FIB_PROTOCOL_IP6)
7913       fp->afi = IP46_TYPE_IP6;
7914     else
7915       {
7916         is_ip4 = ip46_address_is_ip4 (&api_rpath->rpath.frp_addr);
7917         if (is_ip4)
7918           fp->afi = IP46_TYPE_IP4;
7919         else
7920           fp->afi = IP46_TYPE_IP6;
7921       }
7922     fp++;
7923   }
7924
7925   vl_msg_api_send_shmem (q, (u8 *) & mp);
7926 }
7927
7928 typedef struct apt_ip6_fib_show_ctx_t_ {
7929     u32 fib_index;
7930     fib_node_index_t *entries;
7931 } api_ip6_fib_show_ctx_t;
7932
7933 static void
7934 api_ip6_fib_table_put_entries (clib_bihash_kv_24_8_t * kvp,
7935                                void *arg)
7936 {
7937   api_ip6_fib_show_ctx_t *ctx = arg;
7938
7939   if ((kvp->key[2] >> 32) == ctx->fib_index)
7940     {
7941       vec_add1(ctx->entries, kvp->value);
7942     }
7943 }
7944
7945 static void
7946 api_ip6_fib_table_get_all (unix_shared_memory_queue_t *q,
7947                            vl_api_ip6_fib_dump_t *mp,
7948                            fib_table_t *fib_table)
7949 {
7950   vpe_api_main_t *am = &vpe_api_main;
7951   ip6_main_t *im6 = &ip6_main;
7952   ip6_fib_t *fib = &fib_table->v6;
7953   fib_node_index_t *fib_entry_index;
7954   api_ip6_fib_show_ctx_t ctx = {
7955     .fib_index = fib->index,
7956     .entries = NULL,
7957   };
7958   fib_route_path_encode_t *api_rpaths;
7959   fib_prefix_t pfx;
7960
7961   BV(clib_bihash_foreach_key_value_pair)
7962     ((BVT(clib_bihash) *) &im6->ip6_table[IP6_FIB_TABLE_NON_FWDING].ip6_hash,
7963      api_ip6_fib_table_put_entries,
7964      &ctx);
7965
7966   vec_sort_with_function(ctx.entries, fib_entry_cmp_for_sort);
7967
7968   vec_foreach(fib_entry_index, ctx.entries)
7969     {
7970       fib_entry_get_prefix(*fib_entry_index, &pfx);
7971       api_rpaths = NULL;
7972       fib_entry_encode(*fib_entry_index, &api_rpaths);
7973       send_ip6_fib_details (am, q,
7974                             fib_table->ft_table_id,
7975                             &pfx,
7976                             api_rpaths,
7977                             mp->context);
7978       vec_free(api_rpaths);
7979     }
7980
7981   vec_free(ctx.entries);
7982 }
7983
7984 static void
7985 vl_api_ip6_fib_dump_t_handler (vl_api_ip6_fib_dump_t * mp)
7986 {
7987   unix_shared_memory_queue_t *q;
7988   ip6_main_t *im6 = &ip6_main;
7989   fib_table_t *fib_table;
7990
7991   q = vl_api_client_index_to_input_queue (mp->client_index);
7992   if (q == 0)
7993     return;
7994
7995   /* *INDENT-OFF* */
7996   pool_foreach (fib_table, im6->fibs,
7997   ({
7998     api_ip6_fib_table_get_all(q, mp, fib_table);
7999   }));
8000 }
8001
8002 static void
8003 vl_api_mpls_fib_encap_details_t_handler (vl_api_mpls_fib_encap_details_t * mp)
8004 {
8005   clib_warning ("BUG");
8006 }
8007
8008 static void
8009 send_mpls_fib_encap_details (vpe_api_main_t * am,
8010                              unix_shared_memory_queue_t * q,
8011                              show_mpls_fib_t * s, u32 context)
8012 {
8013   vl_api_mpls_fib_encap_details_t *mp;
8014   mpls_main_t *mm = &mpls_main;
8015   mpls_encap_t *e;
8016   int i;
8017   u32 nlabels;
8018
8019   e = pool_elt_at_index (mm->encaps, s->entry_index);
8020   nlabels = vec_len (e->labels);
8021
8022   mp = vl_msg_api_alloc (sizeof (*mp) + nlabels * sizeof (u32));
8023   memset (mp, 0, sizeof (*mp));
8024   mp->_vl_msg_id = ntohs (VL_API_MPLS_FIB_ENCAP_DETAILS);
8025   mp->context = context;
8026
8027   mp->fib_index = htonl (s->fib_index);
8028   mp->entry_index = htonl (s->entry_index);
8029   mp->dest = s->dest;
8030   mp->s_bit = htonl (s->s_bit);
8031
8032   mp->nlabels = htonl (nlabels);
8033
8034   for (i = 0; i < nlabels; i++)
8035     {
8036       mp->labels[i] =
8037         htonl (vnet_mpls_uc_get_label
8038                (clib_host_to_net_u32 (e->labels[i].label_exp_s_ttl)));
8039     }
8040
8041   vl_msg_api_send_shmem (q, (u8 *) & mp);
8042 }
8043
8044 static void
8045 vl_api_mpls_fib_encap_dump_t_handler (vl_api_mpls_fib_encap_dump_t * mp)
8046 {
8047   vpe_api_main_t *am = &vpe_api_main;
8048   unix_shared_memory_queue_t *q;
8049   vlib_main_t *vm = &vlib_global_main;
8050   u64 key;
8051   u32 value;
8052   show_mpls_fib_t *records = 0;
8053   show_mpls_fib_t *s;
8054   mpls_main_t *mm = &mpls_main;
8055   ip4_fib_t *rx_fib;
8056
8057   q = vl_api_client_index_to_input_queue (mp->client_index);
8058   if (q == 0)
8059     return;
8060
8061   /* *INDENT-OFF* */
8062   hash_foreach (key, value, mm->mpls_encap_by_fib_and_dest,
8063   ({
8064     vec_add2 (records, s, 1);
8065     s->fib_index = (u32)(key>>32);
8066     s->dest = (u32)(key & 0xFFFFFFFF);
8067     s->entry_index = (u32) value;
8068   }));
8069   /* *INDENT-ON* */
8070
8071   if (0 == vec_len (records))
8072     {
8073       vlib_cli_output (vm, "MPLS encap table empty");
8074       goto out;
8075     }
8076
8077   /* sort output by dst address within fib */
8078   vec_sort_with_function (records, mpls_dest_cmp);
8079   vec_sort_with_function (records, mpls_fib_index_cmp);
8080   vlib_cli_output (vm, "MPLS encap table");
8081   vlib_cli_output (vm, "%=6s%=16s%=16s", "Table", "Dest address", "Labels");
8082   vec_foreach (s, records)
8083   {
8084     rx_fib = ip4_fib_get (s->fib_index);
8085     vlib_cli_output (vm, "%=6d%=16U%=16U", rx_fib->table_id,
8086                      format_ip4_address, &s->dest, format_mpls_encap_index,
8087                      mm, s->entry_index);
8088     send_mpls_fib_encap_details (am, q, s, mp->context);
8089   }
8090
8091 out:
8092   vec_free (records);
8093 }
8094
8095 static void
8096 vl_api_classify_table_ids_t_handler (vl_api_classify_table_ids_t * mp)
8097 {
8098   unix_shared_memory_queue_t *q;
8099
8100   q = vl_api_client_index_to_input_queue (mp->client_index);
8101   if (q == 0)
8102     return;
8103
8104   vnet_classify_main_t *cm = &vnet_classify_main;
8105   vnet_classify_table_t *t;
8106   u32 *table_ids = 0;
8107   u32 count;
8108
8109   /* *INDENT-OFF* */
8110   pool_foreach (t, cm->tables,
8111   ({
8112     vec_add1 (table_ids, ntohl(t - cm->tables));
8113   }));
8114   /* *INDENT-ON* */
8115   count = vec_len (table_ids);
8116
8117   vl_api_classify_table_ids_reply_t *rmp;
8118   rmp = vl_msg_api_alloc_as_if_client (sizeof (*rmp) + count * sizeof (u32));
8119   rmp->_vl_msg_id = ntohs (VL_API_CLASSIFY_TABLE_IDS_REPLY);
8120   rmp->context = mp->context;
8121   rmp->count = ntohl (count);
8122   clib_memcpy (rmp->ids, table_ids, count * sizeof (u32));
8123   rmp->retval = 0;
8124
8125   vl_msg_api_send_shmem (q, (u8 *) & rmp);
8126
8127   vec_free (table_ids);
8128 }
8129
8130 static void
8131   vl_api_classify_table_by_interface_t_handler
8132   (vl_api_classify_table_by_interface_t * mp)
8133 {
8134   vl_api_classify_table_by_interface_reply_t *rmp;
8135   int rv = 0;
8136
8137   u32 sw_if_index = ntohl (mp->sw_if_index);
8138   u32 *acl = 0;
8139
8140   vec_validate (acl, INPUT_ACL_N_TABLES - 1);
8141   vec_set (acl, ~0);
8142
8143   VALIDATE_SW_IF_INDEX (mp);
8144
8145   input_acl_main_t *am = &input_acl_main;
8146
8147   int if_idx;
8148   u32 type;
8149
8150   for (type = 0; type < INPUT_ACL_N_TABLES; type++)
8151     {
8152       u32 *vec_tbl = am->classify_table_index_by_sw_if_index[type];
8153       if (vec_len (vec_tbl))
8154         {
8155           for (if_idx = 0; if_idx < vec_len (vec_tbl); if_idx++)
8156             {
8157               if (vec_elt (vec_tbl, if_idx) == ~0 || sw_if_index != if_idx)
8158                 {
8159                   continue;
8160                 }
8161               acl[type] = vec_elt (vec_tbl, if_idx);
8162             }
8163         }
8164     }
8165
8166   BAD_SW_IF_INDEX_LABEL;
8167
8168   /* *INDENT-OFF* */
8169   REPLY_MACRO2(VL_API_CLASSIFY_TABLE_BY_INTERFACE_REPLY,
8170   ({
8171     rmp->sw_if_index = ntohl(sw_if_index);
8172     rmp->l2_table_id = ntohl(acl[INPUT_ACL_TABLE_L2]);
8173     rmp->ip4_table_id = ntohl(acl[INPUT_ACL_TABLE_IP4]);
8174     rmp->ip6_table_id = ntohl(acl[INPUT_ACL_TABLE_IP6]);
8175   }));
8176   /* *INDENT-ON* */
8177   vec_free (acl);
8178 }
8179
8180 static void
8181 vl_api_classify_table_info_t_handler (vl_api_classify_table_info_t * mp)
8182 {
8183   unix_shared_memory_queue_t *q;
8184
8185   q = vl_api_client_index_to_input_queue (mp->client_index);
8186   if (q == 0)
8187     return;
8188
8189   vl_api_classify_table_info_reply_t *rmp = 0;
8190
8191   vnet_classify_main_t *cm = &vnet_classify_main;
8192   u32 table_id = ntohl (mp->table_id);
8193   vnet_classify_table_t *t;
8194
8195   /* *INDENT-OFF* */
8196   pool_foreach (t, cm->tables,
8197   ({
8198     if (table_id == t - cm->tables)
8199       {
8200         rmp = vl_msg_api_alloc_as_if_client
8201           (sizeof (*rmp) + t->match_n_vectors * sizeof (u32x4));
8202         rmp->_vl_msg_id = ntohs (VL_API_CLASSIFY_TABLE_INFO_REPLY);
8203         rmp->context = mp->context;
8204         rmp->table_id = ntohl(table_id);
8205         rmp->nbuckets = ntohl(t->nbuckets);
8206         rmp->match_n_vectors = ntohl(t->match_n_vectors);
8207         rmp->skip_n_vectors = ntohl(t->skip_n_vectors);
8208         rmp->active_sessions = ntohl(t->active_elements);
8209         rmp->next_table_index = ntohl(t->next_table_index);
8210         rmp->miss_next_index = ntohl(t->miss_next_index);
8211         rmp->mask_length = ntohl(t->match_n_vectors * sizeof (u32x4));
8212         clib_memcpy(rmp->mask, t->mask, t->match_n_vectors * sizeof(u32x4));
8213         rmp->retval = 0;
8214         break;
8215       }
8216   }));
8217   /* *INDENT-ON* */
8218
8219   if (rmp == 0)
8220     {
8221       rmp = vl_msg_api_alloc (sizeof (*rmp));
8222       rmp->_vl_msg_id = ntohs ((VL_API_CLASSIFY_TABLE_INFO_REPLY));
8223       rmp->context = mp->context;
8224       rmp->retval = ntohl (VNET_API_ERROR_CLASSIFY_TABLE_NOT_FOUND);
8225     }
8226
8227   vl_msg_api_send_shmem (q, (u8 *) & rmp);
8228 }
8229
8230 static void
8231 vl_api_classify_session_details_t_handler (vl_api_classify_session_details_t *
8232                                            mp)
8233 {
8234   clib_warning ("BUG");
8235 }
8236
8237 static void
8238 send_classify_session_details (unix_shared_memory_queue_t * q,
8239                                u32 table_id,
8240                                u32 match_length,
8241                                vnet_classify_entry_t * e, u32 context)
8242 {
8243   vl_api_classify_session_details_t *rmp;
8244
8245   rmp = vl_msg_api_alloc (sizeof (*rmp));
8246   memset (rmp, 0, sizeof (*rmp));
8247   rmp->_vl_msg_id = ntohs (VL_API_CLASSIFY_SESSION_DETAILS);
8248   rmp->context = context;
8249   rmp->table_id = ntohl (table_id);
8250   rmp->hit_next_index = ntohl (e->next_index);
8251   rmp->advance = ntohl (e->advance);
8252   rmp->opaque_index = ntohl (e->opaque_index);
8253   rmp->match_length = ntohl (match_length);
8254   clib_memcpy (rmp->match, e->key, match_length);
8255
8256   vl_msg_api_send_shmem (q, (u8 *) & rmp);
8257 }
8258
8259 static void
8260 vl_api_classify_session_dump_t_handler (vl_api_classify_session_dump_t * mp)
8261 {
8262   vnet_classify_main_t *cm = &vnet_classify_main;
8263   unix_shared_memory_queue_t *q;
8264
8265   u32 table_id = ntohl (mp->table_id);
8266   vnet_classify_table_t *t;
8267
8268   q = vl_api_client_index_to_input_queue (mp->client_index);
8269   if (!q)
8270     return;
8271
8272   /* *INDENT-OFF* */
8273   pool_foreach (t, cm->tables,
8274   ({
8275     if (table_id == t - cm->tables)
8276       {
8277         vnet_classify_bucket_t * b;
8278         vnet_classify_entry_t * v, * save_v;
8279         int i, j, k;
8280
8281         for (i = 0; i < t->nbuckets; i++)
8282           {
8283             b = &t->buckets [i];
8284             if (b->offset == 0)
8285               continue;
8286
8287             save_v = vnet_classify_get_entry (t, b->offset);
8288             for (j = 0; j < (1<<b->log2_pages); j++)
8289               {
8290                 for (k = 0; k < t->entries_per_page; k++)
8291                   {
8292                     v = vnet_classify_entry_at_index
8293                       (t, save_v, j*t->entries_per_page + k);
8294                     if (vnet_classify_entry_is_free (v))
8295                       continue;
8296
8297                     send_classify_session_details
8298                       (q, table_id, t->match_n_vectors * sizeof (u32x4),
8299                        v, mp->context);
8300                   }
8301               }
8302           }
8303         break;
8304       }
8305   }));
8306   /* *INDENT-ON* */
8307 }
8308
8309 static void
8310 vl_api_set_ipfix_exporter_t_handler (vl_api_set_ipfix_exporter_t * mp)
8311 {
8312   vlib_main_t *vm = vlib_get_main ();
8313   flow_report_main_t *frm = &flow_report_main;
8314   vl_api_set_ipfix_exporter_reply_t *rmp;
8315   ip4_address_t collector, src;
8316   u16 collector_port = UDP_DST_PORT_ipfix;
8317   u32 path_mtu;
8318   u32 template_interval;
8319   u8 udp_checksum;
8320   u32 fib_id;
8321   u32 fib_index = ~0;
8322   int rv = 0;
8323
8324   memcpy (collector.data, mp->collector_address, sizeof (collector.data));
8325   collector_port = ntohs (mp->collector_port);
8326   if (collector_port == (u16) ~ 0)
8327     collector_port = UDP_DST_PORT_ipfix;
8328   memcpy (src.data, mp->src_address, sizeof (src.data));
8329   fib_id = ntohl (mp->vrf_id);
8330
8331   ip4_main_t *im = &ip4_main;
8332   if (fib_id == ~0)
8333     {
8334       fib_index = ~0;
8335     }
8336   else
8337     {
8338       uword *p = hash_get (im->fib_index_by_table_id, fib_id);
8339       if (!p)
8340         {
8341           rv = VNET_API_ERROR_NO_SUCH_FIB;
8342           goto out;
8343         }
8344       fib_index = p[0];
8345     }
8346
8347   path_mtu = ntohl (mp->path_mtu);
8348   if (path_mtu == ~0)
8349     path_mtu = 512;             // RFC 7011 section 10.3.3.
8350   template_interval = ntohl (mp->template_interval);
8351   if (template_interval == ~0)
8352     template_interval = 20;
8353   udp_checksum = mp->udp_checksum;
8354
8355   if (collector.as_u32 == 0)
8356     {
8357       rv = VNET_API_ERROR_INVALID_VALUE;
8358       goto out;
8359     }
8360
8361   if (src.as_u32 == 0)
8362     {
8363       rv = VNET_API_ERROR_INVALID_VALUE;
8364       goto out;
8365     }
8366
8367   if (path_mtu > 1450 /* vpp does not support fragmentation */ )
8368     {
8369       rv = VNET_API_ERROR_INVALID_VALUE;
8370       goto out;
8371     }
8372
8373   if (path_mtu < 68)
8374     {
8375       rv = VNET_API_ERROR_INVALID_VALUE;
8376       goto out;
8377     }
8378
8379   /* Reset report streams if we are reconfiguring IP addresses */
8380   if (frm->ipfix_collector.as_u32 != collector.as_u32 ||
8381       frm->src_address.as_u32 != src.as_u32 ||
8382       frm->collector_port != collector_port)
8383     vnet_flow_reports_reset (frm);
8384
8385   frm->ipfix_collector.as_u32 = collector.as_u32;
8386   frm->collector_port = collector_port;
8387   frm->src_address.as_u32 = src.as_u32;
8388   frm->fib_index = fib_index;
8389   frm->path_mtu = path_mtu;
8390   frm->template_interval = template_interval;
8391   frm->udp_checksum = udp_checksum;
8392
8393   /* Turn on the flow reporting process */
8394   vlib_process_signal_event (vm, flow_report_process_node.index, 1, 0);
8395
8396 out:
8397   REPLY_MACRO (VL_API_SET_IPFIX_EXPORTER_REPLY);
8398 }
8399
8400 static void
8401 vl_api_ipfix_exporter_dump_t_handler (vl_api_ipfix_exporter_dump_t * mp)
8402 {
8403   flow_report_main_t *frm = &flow_report_main;
8404   unix_shared_memory_queue_t *q;
8405   vl_api_ipfix_exporter_details_t *rmp;
8406   ip4_main_t *im = &ip4_main;
8407   u32 vrf_id;
8408
8409   q = vl_api_client_index_to_input_queue (mp->client_index);
8410   if (!q)
8411     return;
8412
8413   rmp = vl_msg_api_alloc (sizeof (*rmp));
8414   memset (rmp, 0, sizeof (*rmp));
8415   rmp->_vl_msg_id = ntohs (VL_API_IPFIX_EXPORTER_DETAILS);
8416   rmp->context = mp->context;
8417   memcpy (rmp->collector_address, frm->ipfix_collector.data,
8418           sizeof (frm->ipfix_collector.data));
8419   rmp->collector_port = htons (frm->collector_port);
8420   memcpy (rmp->src_address, frm->src_address.data,
8421           sizeof (frm->src_address.data));
8422   if (frm->fib_index == ~0)
8423     vrf_id = ~0;
8424   else
8425     vrf_id = im->fibs[frm->fib_index].ft_table_id;
8426   rmp->vrf_id = htonl (vrf_id);
8427   rmp->path_mtu = htonl (frm->path_mtu);
8428   rmp->template_interval = htonl (frm->template_interval);
8429   rmp->udp_checksum = (frm->udp_checksum != 0);
8430
8431   vl_msg_api_send_shmem (q, (u8 *) & rmp);
8432 }
8433
8434 static void
8435   vl_api_set_ipfix_classify_stream_t_handler
8436   (vl_api_set_ipfix_classify_stream_t * mp)
8437 {
8438   vl_api_set_ipfix_classify_stream_reply_t *rmp;
8439   flow_report_classify_main_t *fcm = &flow_report_classify_main;
8440   flow_report_main_t *frm = &flow_report_main;
8441   u32 domain_id = 0;
8442   u32 src_port = UDP_DST_PORT_ipfix;
8443   int rv = 0;
8444
8445   domain_id = ntohl (mp->domain_id);
8446   src_port = ntohs (mp->src_port);
8447
8448   if (fcm->src_port != 0 &&
8449       (fcm->domain_id != domain_id || fcm->src_port != (u16) src_port))
8450     {
8451       int rv = vnet_stream_change (frm, fcm->domain_id, fcm->src_port,
8452                                    domain_id, (u16) src_port);
8453       ASSERT (rv == 0);
8454     }
8455
8456   fcm->domain_id = domain_id;
8457   fcm->src_port = (u16) src_port;
8458
8459   REPLY_MACRO (VL_API_SET_IPFIX_CLASSIFY_STREAM_REPLY);
8460 }
8461
8462 static void
8463   vl_api_ipfix_classify_stream_dump_t_handler
8464   (vl_api_ipfix_classify_stream_dump_t * mp)
8465 {
8466   flow_report_classify_main_t *fcm = &flow_report_classify_main;
8467   unix_shared_memory_queue_t *q;
8468   vl_api_ipfix_classify_stream_details_t *rmp;
8469
8470   q = vl_api_client_index_to_input_queue (mp->client_index);
8471   if (!q)
8472     return;
8473
8474   rmp = vl_msg_api_alloc (sizeof (*rmp));
8475   memset (rmp, 0, sizeof (*rmp));
8476   rmp->_vl_msg_id = ntohs (VL_API_IPFIX_CLASSIFY_STREAM_DETAILS);
8477   rmp->context = mp->context;
8478   rmp->domain_id = htonl (fcm->domain_id);
8479   rmp->src_port = htons (fcm->src_port);
8480
8481   vl_msg_api_send_shmem (q, (u8 *) & rmp);
8482 }
8483
8484 static void
8485   vl_api_ipfix_classify_table_add_del_t_handler
8486   (vl_api_ipfix_classify_table_add_del_t * mp)
8487 {
8488   vl_api_ipfix_classify_table_add_del_reply_t *rmp;
8489   flow_report_classify_main_t *fcm = &flow_report_classify_main;
8490   flow_report_main_t *frm = &flow_report_main;
8491   vnet_flow_report_add_del_args_t args;
8492   ipfix_classify_table_t *table;
8493   int is_add;
8494   u32 classify_table_index;
8495   u8 ip_version;
8496   u8 transport_protocol;
8497   int rv = 0;
8498
8499   classify_table_index = ntohl (mp->table_id);
8500   ip_version = mp->ip_version;
8501   transport_protocol = mp->transport_protocol;
8502   is_add = mp->is_add;
8503
8504   if (fcm->src_port == 0)
8505     {
8506       /* call set_ipfix_classify_stream first */
8507       rv = VNET_API_ERROR_UNSPECIFIED;
8508       goto out;
8509     }
8510
8511   memset (&args, 0, sizeof (args));
8512
8513   table = 0;
8514   int i;
8515   for (i = 0; i < vec_len (fcm->tables); i++)
8516     if (ipfix_classify_table_index_valid (i))
8517       if (fcm->tables[i].classify_table_index == classify_table_index)
8518         {
8519           table = &fcm->tables[i];
8520           break;
8521         }
8522
8523   if (is_add)
8524     {
8525       if (table)
8526         {
8527           rv = VNET_API_ERROR_VALUE_EXIST;
8528           goto out;
8529         }
8530       table = ipfix_classify_add_table ();
8531       table->classify_table_index = classify_table_index;
8532     }
8533   else
8534     {
8535       if (!table)
8536         {
8537           rv = VNET_API_ERROR_NO_SUCH_ENTRY;
8538           goto out;
8539         }
8540     }
8541
8542   table->ip_version = ip_version;
8543   table->transport_protocol = transport_protocol;
8544
8545   args.opaque.as_uword = table - fcm->tables;
8546   args.rewrite_callback = ipfix_classify_template_rewrite;
8547   args.flow_data_callback = ipfix_classify_send_flows;
8548   args.is_add = is_add;
8549   args.domain_id = fcm->domain_id;
8550   args.src_port = fcm->src_port;
8551
8552   rv = vnet_flow_report_add_del (frm, &args);
8553
8554   /* If deleting, or add failed */
8555   if (is_add == 0 || (rv && is_add))
8556     ipfix_classify_delete_table (table - fcm->tables);
8557
8558 out:
8559   REPLY_MACRO (VL_API_SET_IPFIX_CLASSIFY_STREAM_REPLY);
8560 }
8561
8562 static void
8563 send_ipfix_classify_table_details (u32 table_index,
8564                                    unix_shared_memory_queue_t * q,
8565                                    u32 context)
8566 {
8567   flow_report_classify_main_t *fcm = &flow_report_classify_main;
8568   vl_api_ipfix_classify_table_details_t *mp;
8569
8570   ipfix_classify_table_t *table = &fcm->tables[table_index];
8571
8572   mp = vl_msg_api_alloc (sizeof (*mp));
8573   memset (mp, 0, sizeof (*mp));
8574   mp->_vl_msg_id = ntohs (VL_API_IPFIX_CLASSIFY_TABLE_DETAILS);
8575   mp->context = context;
8576   mp->table_id = htonl (table->classify_table_index);
8577   mp->ip_version = table->ip_version;
8578   mp->transport_protocol = table->transport_protocol;
8579
8580   vl_msg_api_send_shmem (q, (u8 *) & mp);
8581 }
8582
8583 static void
8584   vl_api_ipfix_classify_table_dump_t_handler
8585   (vl_api_ipfix_classify_table_dump_t * mp)
8586 {
8587   flow_report_classify_main_t *fcm = &flow_report_classify_main;
8588   unix_shared_memory_queue_t *q;
8589   u32 i;
8590
8591   q = vl_api_client_index_to_input_queue (mp->client_index);
8592   if (!q)
8593     return;
8594
8595   for (i = 0; i < vec_len (fcm->tables); i++)
8596     if (ipfix_classify_table_index_valid (i))
8597       send_ipfix_classify_table_details (i, q, mp->context);
8598 }
8599
8600 static void
8601   vl_api_sw_interface_span_enable_disable_t_handler
8602   (vl_api_sw_interface_span_enable_disable_t * mp)
8603 {
8604   vl_api_sw_interface_span_enable_disable_reply_t *rmp;
8605   int rv;
8606
8607   vlib_main_t *vm = vlib_get_main ();
8608
8609   rv = span_add_delete_entry (vm, ntohl (mp->sw_if_index_from),
8610                               ntohl (mp->sw_if_index_to), mp->enable);
8611
8612   REPLY_MACRO (VL_API_SW_INTERFACE_SPAN_ENABLE_DISABLE_REPLY);
8613 }
8614
8615 static void
8616 vl_api_sw_interface_span_dump_t_handler (vl_api_sw_interface_span_dump_t * mp)
8617 {
8618
8619   unix_shared_memory_queue_t *q;
8620   vl_api_sw_interface_span_details_t *rmp;
8621   span_main_t *sm = &span_main;
8622   u32 src_sw_if_index = 0, *dst_sw_if_index;
8623
8624   q = vl_api_client_index_to_input_queue (mp->client_index);
8625   vec_foreach (dst_sw_if_index, sm->dst_by_src_sw_if_index)
8626   {
8627     if (*dst_sw_if_index > 0)
8628       {
8629         rmp = vl_msg_api_alloc (sizeof (*rmp));
8630         memset (rmp, 0, sizeof (*rmp));
8631         rmp->_vl_msg_id = ntohs (VL_API_SW_INTERFACE_SPAN_DETAILS);
8632         rmp->context = mp->context;
8633
8634         rmp->sw_if_index_from = htonl (src_sw_if_index);
8635         rmp->sw_if_index_to = htonl (*dst_sw_if_index);
8636
8637         vl_msg_api_send_shmem (q, (u8 *) & rmp);
8638       }
8639     src_sw_if_index++;
8640   }
8641 }
8642
8643 static void
8644 vl_api_pg_create_interface_t_handler (vl_api_pg_create_interface_t * mp)
8645 {
8646   vl_api_pg_create_interface_reply_t *rmp;
8647   int rv = 0;
8648
8649   pg_main_t *pg = &pg_main;
8650   u32 pg_if_id = pg_interface_add_or_get (pg, ntohl (mp->interface_id));
8651   pg_interface_t *pi = pool_elt_at_index (pg->interfaces, pg_if_id);
8652
8653   /* *INDENT-OFF* */
8654   REPLY_MACRO2(VL_API_PG_CREATE_INTERFACE_REPLY,
8655   ({
8656     rmp->sw_if_index = ntohl(pi->sw_if_index);
8657   }));
8658   /* *INDENT-ON* */
8659 }
8660
8661 static void
8662 vl_api_pg_capture_t_handler (vl_api_pg_capture_t * mp)
8663 {
8664   vl_api_pg_capture_reply_t *rmp;
8665   int rv = 0;
8666
8667   vnet_main_t *vnm = vnet_get_main ();
8668   vnet_interface_main_t *im = &vnm->interface_main;
8669   vnet_hw_interface_t *hi = 0;
8670
8671   u8 *intf_name = format (0, "pg%d", ntohl (mp->interface_id), 0);
8672   u32 hw_if_index = ~0;
8673   uword *p = hash_get_mem (im->hw_interface_by_name, intf_name);
8674   if (p)
8675     hw_if_index = *p;
8676   vec_free (intf_name);
8677
8678   if (hw_if_index != ~0)
8679     {
8680       pg_capture_args_t _a, *a = &_a;
8681
8682       u32 len = ntohl (mp->pcap_name_length);
8683       u8 *pcap_file_name = vec_new (u8, len);
8684       clib_memcpy (pcap_file_name, mp->pcap_file_name, len);
8685
8686       hi = vnet_get_sup_hw_interface (vnm, hw_if_index);
8687       a->hw_if_index = hw_if_index;
8688       a->dev_instance = hi->dev_instance;
8689       a->is_enabled = mp->is_enabled;
8690       a->pcap_file_name = pcap_file_name;
8691       a->count = ntohl (mp->count);
8692
8693       clib_error_t *e = pg_capture (a);
8694       if (e)
8695         {
8696           clib_error_report (e);
8697           rv = VNET_API_ERROR_CANNOT_CREATE_PCAP_FILE;
8698         }
8699
8700       vec_free (pcap_file_name);
8701     }
8702   REPLY_MACRO (VL_API_PG_CAPTURE_REPLY);
8703 }
8704
8705 static void
8706 vl_api_pg_enable_disable_t_handler (vl_api_pg_enable_disable_t * mp)
8707 {
8708   vl_api_pg_enable_disable_reply_t *rmp;
8709   int rv = 0;
8710
8711   pg_main_t *pg = &pg_main;
8712   u32 stream_index = ~0;
8713
8714   int is_enable = mp->is_enabled != 0;
8715   u32 len = ntohl (mp->stream_name_length) - 1;
8716
8717   if (len > 0)
8718     {
8719       u8 *stream_name = vec_new (u8, len);
8720       clib_memcpy (stream_name, mp->stream_name, len);
8721       uword *p = hash_get_mem (pg->stream_index_by_name, stream_name);
8722       if (p)
8723         stream_index = *p;
8724       vec_free (stream_name);
8725     }
8726
8727   pg_enable_disable (stream_index, is_enable);
8728
8729   REPLY_MACRO (VL_API_PG_ENABLE_DISABLE_REPLY);
8730 }
8731
8732 static void
8733   vl_api_ip_source_and_port_range_check_add_del_t_handler
8734   (vl_api_ip_source_and_port_range_check_add_del_t * mp)
8735 {
8736   vl_api_ip_source_and_port_range_check_add_del_reply_t *rmp;
8737   int rv = 0;
8738
8739   u8 is_ipv6 = mp->is_ipv6;
8740   u8 is_add = mp->is_add;
8741   u8 mask_length = mp->mask_length;
8742   ip4_address_t ip4_addr;
8743   ip6_address_t ip6_addr;
8744   u16 *low_ports = 0;
8745   u16 *high_ports = 0;
8746   u32 vrf_id;
8747   u16 tmp_low, tmp_high;
8748   u8 num_ranges;
8749   int i;
8750
8751   // Validate port range
8752   num_ranges = mp->number_of_ranges;
8753   if (num_ranges > 32)
8754     {                           // This is size of array in VPE.API
8755       rv = VNET_API_ERROR_EXCEEDED_NUMBER_OF_RANGES_CAPACITY;
8756       goto reply;
8757     }
8758
8759   vec_reset_length (low_ports);
8760   vec_reset_length (high_ports);
8761
8762   for (i = 0; i < num_ranges; i++)
8763     {
8764       tmp_low = mp->low_ports[i];
8765       tmp_high = mp->high_ports[i];
8766       // If tmp_low <= tmp_high then only need to check tmp_low = 0
8767       // If tmp_low <= tmp_high then only need to check tmp_high > 65535
8768       if (tmp_low > tmp_high || tmp_low == 0 || tmp_high > 65535)
8769         {
8770           rv = VNET_API_ERROR_INVALID_VALUE;
8771           goto reply;
8772         }
8773       vec_add1 (low_ports, tmp_low);
8774       vec_add1 (high_ports, tmp_high + 1);
8775     }
8776
8777   // Validate mask_length
8778   if ((is_ipv6 && mask_length > 128) || (!is_ipv6 && mask_length > 32))
8779     {
8780       rv = VNET_API_ERROR_ADDRESS_LENGTH_MISMATCH;
8781       goto reply;
8782     }
8783
8784   vrf_id = ntohl (mp->vrf_id);
8785
8786   if (vrf_id < 1)
8787     {
8788       rv = VNET_API_ERROR_INVALID_VALUE;
8789       goto reply;
8790     }
8791
8792
8793   if (is_ipv6)
8794     {
8795       clib_memcpy (ip6_addr.as_u8, mp->address, sizeof (ip6_addr.as_u8));
8796       rv = ip6_source_and_port_range_check_add_del (&ip6_addr,
8797                                                     mask_length,
8798                                                     vrf_id,
8799                                                     low_ports,
8800                                                     high_ports, is_add);
8801     }
8802   else
8803     {
8804       clib_memcpy (ip4_addr.data, mp->address, sizeof (ip4_addr));
8805       rv = ip4_source_and_port_range_check_add_del (&ip4_addr,
8806                                                     mask_length,
8807                                                     vrf_id,
8808                                                     low_ports,
8809                                                     high_ports, is_add);
8810     }
8811
8812 reply:
8813   vec_free (low_ports);
8814   vec_free (high_ports);
8815   REPLY_MACRO (VL_API_IP_SOURCE_AND_PORT_RANGE_CHECK_ADD_DEL_REPLY);
8816 }
8817
8818 static void
8819   vl_api_ip_source_and_port_range_check_interface_add_del_t_handler
8820   (vl_api_ip_source_and_port_range_check_interface_add_del_t * mp)
8821 {
8822   vlib_main_t *vm = vlib_get_main ();
8823   vl_api_ip_source_and_port_range_check_interface_add_del_reply_t *rmp;
8824   ip4_main_t *im = &ip4_main;
8825   int rv;
8826   u32 sw_if_index;
8827   u32 fib_index[IP_SOURCE_AND_PORT_RANGE_CHECK_N_PROTOCOLS];
8828   u32 vrf_id[IP_SOURCE_AND_PORT_RANGE_CHECK_N_PROTOCOLS];
8829   uword *p = 0;
8830   int i;
8831
8832   vrf_id[IP_SOURCE_AND_PORT_RANGE_CHECK_PROTOCOL_TCP_OUT] =
8833     ntohl (mp->tcp_out_vrf_id);
8834   vrf_id[IP_SOURCE_AND_PORT_RANGE_CHECK_PROTOCOL_UDP_OUT] =
8835     ntohl (mp->udp_out_vrf_id);
8836   vrf_id[IP_SOURCE_AND_PORT_RANGE_CHECK_PROTOCOL_TCP_IN] =
8837     ntohl (mp->tcp_in_vrf_id);
8838   vrf_id[IP_SOURCE_AND_PORT_RANGE_CHECK_PROTOCOL_UDP_IN] =
8839     ntohl (mp->udp_in_vrf_id);
8840
8841
8842   for (i = 0; i < IP_SOURCE_AND_PORT_RANGE_CHECK_N_PROTOCOLS; i++)
8843     {
8844       if (vrf_id[i] != 0 && vrf_id[i] != ~0)
8845         {
8846           p = hash_get (im->fib_index_by_table_id, vrf_id[i]);
8847
8848           if (p == 0)
8849             {
8850               rv = VNET_API_ERROR_INVALID_VALUE;
8851               goto reply;
8852             }
8853
8854           fib_index[i] = p[0];
8855         }
8856       else
8857         fib_index[i] = ~0;
8858     }
8859   sw_if_index = ntohl (mp->sw_if_index);
8860
8861   VALIDATE_SW_IF_INDEX (mp);
8862
8863   rv =
8864     set_ip_source_and_port_range_check (vm, fib_index, sw_if_index,
8865                                         mp->is_add);
8866
8867   BAD_SW_IF_INDEX_LABEL;
8868 reply:
8869
8870   REPLY_MACRO (VL_API_IP_SOURCE_AND_PORT_RANGE_CHECK_INTERFACE_ADD_DEL_REPLY);
8871 }
8872
8873 static void
8874 vl_api_ipsec_gre_add_del_tunnel_t_handler (vl_api_ipsec_gre_add_del_tunnel_t *
8875                                            mp)
8876 {
8877   vl_api_ipsec_gre_add_del_tunnel_reply_t *rmp;
8878   int rv = 0;
8879   vnet_ipsec_gre_add_del_tunnel_args_t _a, *a = &_a;
8880   u32 sw_if_index = ~0;
8881
8882   /* Check src & dst are different */
8883   if (memcmp (mp->src_address, mp->dst_address, 4) == 0)
8884     {
8885       rv = VNET_API_ERROR_SAME_SRC_DST;
8886       goto out;
8887     }
8888
8889   memset (a, 0, sizeof (*a));
8890
8891   /* ip addresses sent in network byte order */
8892   clib_memcpy (&(a->src), mp->src_address, 4);
8893   clib_memcpy (&(a->dst), mp->dst_address, 4);
8894   a->is_add = mp->is_add;
8895   a->lsa = ntohl (mp->local_sa_id);
8896   a->rsa = ntohl (mp->remote_sa_id);
8897
8898   rv = vnet_ipsec_gre_add_del_tunnel (a, &sw_if_index);
8899
8900 out:
8901     /* *INDENT-OFF* */
8902     REPLY_MACRO2(VL_API_GRE_ADD_DEL_TUNNEL_REPLY,
8903     ({
8904         rmp->sw_if_index = ntohl (sw_if_index);
8905     }));
8906     /* *INDENT-ON* */
8907 }
8908
8909 static void send_ipsec_gre_tunnel_details
8910   (ipsec_gre_tunnel_t * t, unix_shared_memory_queue_t * q, u32 context)
8911 {
8912   vl_api_ipsec_gre_tunnel_details_t *rmp;
8913
8914   rmp = vl_msg_api_alloc (sizeof (*rmp));
8915   memset (rmp, 0, sizeof (*rmp));
8916   rmp->_vl_msg_id = ntohs (VL_API_IPSEC_GRE_TUNNEL_DETAILS);
8917   clib_memcpy (rmp->src_address, &(t->tunnel_src), 4);
8918   clib_memcpy (rmp->dst_address, &(t->tunnel_dst), 4);
8919   rmp->sw_if_index = htonl (t->sw_if_index);
8920   rmp->local_sa_id = htonl (t->local_sa_id);
8921   rmp->remote_sa_id = htonl (t->remote_sa_id);
8922   rmp->context = context;
8923
8924   vl_msg_api_send_shmem (q, (u8 *) & rmp);
8925 }
8926
8927 static void vl_api_ipsec_gre_tunnel_dump_t_handler
8928   (vl_api_ipsec_gre_tunnel_dump_t * mp)
8929 {
8930   unix_shared_memory_queue_t *q;
8931   ipsec_gre_main_t *igm = &ipsec_gre_main;
8932   ipsec_gre_tunnel_t *t;
8933   u32 sw_if_index;
8934
8935   q = vl_api_client_index_to_input_queue (mp->client_index);
8936   if (q == 0)
8937     {
8938       return;
8939     }
8940
8941   sw_if_index = ntohl (mp->sw_if_index);
8942
8943   if (~0 == sw_if_index)
8944     {
8945         /* *INDENT-OFF* */
8946         pool_foreach (t, igm->tunnels,
8947         ({
8948             send_ipsec_gre_tunnel_details(t, q, mp->context);
8949         }));
8950         /* *INDENT-ON* */
8951     }
8952   else
8953     {
8954       if ((sw_if_index >= vec_len (igm->tunnel_index_by_sw_if_index)) ||
8955           (~0 == igm->tunnel_index_by_sw_if_index[sw_if_index]))
8956         {
8957           return;
8958         }
8959       t = &igm->tunnels[igm->tunnel_index_by_sw_if_index[sw_if_index]];
8960       send_ipsec_gre_tunnel_details (t, q, mp->context);
8961     }
8962 }
8963
8964 static void
8965 vl_api_delete_subif_t_handler (vl_api_delete_subif_t * mp)
8966 {
8967   vl_api_delete_subif_reply_t *rmp;
8968   int rv;
8969
8970   rv = vnet_delete_sub_interface (ntohl (mp->sw_if_index));
8971
8972   REPLY_MACRO (VL_API_DELETE_SUBIF_REPLY);
8973 }
8974
8975 static void
8976   vl_api_l2_interface_pbb_tag_rewrite_t_handler
8977   (vl_api_l2_interface_pbb_tag_rewrite_t * mp)
8978 {
8979   vl_api_l2_interface_pbb_tag_rewrite_reply_t *rmp;
8980   vnet_main_t *vnm = vnet_get_main ();
8981   vlib_main_t *vm = vlib_get_main ();
8982   u32 vtr_op;
8983   int rv = 0;
8984
8985   VALIDATE_SW_IF_INDEX (mp);
8986
8987   vtr_op = ntohl (mp->vtr_op);
8988
8989   switch (vtr_op)
8990     {
8991     case L2_VTR_DISABLED:
8992     case L2_VTR_PUSH_2:
8993     case L2_VTR_POP_2:
8994     case L2_VTR_TRANSLATE_2_1:
8995       break;
8996
8997     default:
8998       rv = VNET_API_ERROR_INVALID_VALUE;
8999       goto bad_sw_if_index;
9000     }
9001
9002   rv = l2pbb_configure (vm, vnm, ntohl (mp->sw_if_index), vtr_op,
9003                         mp->b_dmac, mp->b_smac, ntohs (mp->b_vlanid),
9004                         ntohl (mp->i_sid), ntohs (mp->outer_tag));
9005
9006   BAD_SW_IF_INDEX_LABEL;
9007
9008   REPLY_MACRO (VL_API_L2_INTERFACE_PBB_TAG_REWRITE_REPLY);
9009 }
9010
9011 static void
9012 vl_api_punt_t_handler (vl_api_punt_t * mp)
9013 {
9014   vl_api_punt_reply_t *rmp;
9015   vlib_main_t *vm = vlib_get_main ();
9016   int rv = 0;
9017   clib_error_t *error;
9018
9019   error = vnet_punt_add_del (vm, mp->ipv, mp->l4_protocol,
9020                              ntohs (mp->l4_port), mp->is_add);
9021   if (error)
9022     {
9023       rv = -1;
9024       clib_error_report (error);
9025     }
9026
9027   REPLY_MACRO (VL_API_PUNT_REPLY);
9028 }
9029
9030 static void
9031   vl_api_flow_classify_set_interface_t_handler
9032   (vl_api_flow_classify_set_interface_t * mp)
9033 {
9034   vlib_main_t *vm = vlib_get_main ();
9035   vl_api_flow_classify_set_interface_reply_t *rmp;
9036   int rv;
9037   u32 sw_if_index, ip4_table_index, ip6_table_index;
9038
9039   ip4_table_index = ntohl (mp->ip4_table_index);
9040   ip6_table_index = ntohl (mp->ip6_table_index);
9041   sw_if_index = ntohl (mp->sw_if_index);
9042
9043   VALIDATE_SW_IF_INDEX (mp);
9044
9045   rv = vnet_set_flow_classify_intfc (vm, sw_if_index, ip4_table_index,
9046                                      ip6_table_index, mp->is_add);
9047
9048   BAD_SW_IF_INDEX_LABEL;
9049
9050   REPLY_MACRO (VL_API_FLOW_CLASSIFY_SET_INTERFACE_REPLY);
9051 }
9052
9053 static void
9054 send_flow_classify_details (u32 sw_if_index,
9055                             u32 table_index,
9056                             unix_shared_memory_queue_t * q, u32 context)
9057 {
9058   vl_api_flow_classify_details_t *mp;
9059
9060   mp = vl_msg_api_alloc (sizeof (*mp));
9061   memset (mp, 0, sizeof (*mp));
9062   mp->_vl_msg_id = ntohs (VL_API_FLOW_CLASSIFY_DETAILS);
9063   mp->context = context;
9064   mp->sw_if_index = htonl (sw_if_index);
9065   mp->table_index = htonl (table_index);
9066
9067   vl_msg_api_send_shmem (q, (u8 *) & mp);
9068 }
9069
9070 static void
9071 vl_api_flow_classify_dump_t_handler (vl_api_flow_classify_dump_t * mp)
9072 {
9073   unix_shared_memory_queue_t *q;
9074   flow_classify_main_t *pcm = &flow_classify_main;
9075   u32 *vec_tbl;
9076   int i;
9077
9078   q = vl_api_client_index_to_input_queue (mp->client_index);
9079   if (q == 0)
9080     return;
9081
9082   vec_tbl = pcm->classify_table_index_by_sw_if_index[mp->type];
9083
9084   if (vec_len (vec_tbl))
9085     {
9086       for (i = 0; i < vec_len (vec_tbl); i++)
9087         {
9088           if (vec_elt (vec_tbl, i) == ~0)
9089             continue;
9090
9091           send_flow_classify_details (i, vec_elt (vec_tbl, i), q,
9092                                       mp->context);
9093         }
9094     }
9095 }
9096
9097 static void
9098 send_ipsec_spd_details (ipsec_policy_t * p, unix_shared_memory_queue_t * q,
9099                         u32 context)
9100 {
9101   vl_api_ipsec_spd_details_t *mp;
9102
9103   mp = vl_msg_api_alloc (sizeof (*mp));
9104   memset (mp, 0, sizeof (*mp));
9105   mp->_vl_msg_id = ntohs (VL_API_IPSEC_SPD_DETAILS);
9106   mp->context = context;
9107
9108   mp->spd_id = htonl (p->id);
9109   mp->priority = htonl (p->priority);
9110   mp->is_outbound = p->is_outbound;
9111   mp->is_ipv6 = p->is_ipv6;
9112   if (p->is_ipv6)
9113     {
9114       memcpy (mp->local_start_addr, &p->laddr.start.ip6, 16);
9115       memcpy (mp->local_stop_addr, &p->laddr.stop.ip6, 16);
9116       memcpy (mp->remote_start_addr, &p->raddr.start.ip6, 16);
9117       memcpy (mp->remote_stop_addr, &p->raddr.stop.ip6, 16);
9118     }
9119   else
9120     {
9121       memcpy (mp->local_start_addr, &p->laddr.start.ip4, 4);
9122       memcpy (mp->local_stop_addr, &p->laddr.stop.ip4, 4);
9123       memcpy (mp->remote_start_addr, &p->raddr.start.ip4, 4);
9124       memcpy (mp->remote_stop_addr, &p->raddr.stop.ip4, 4);
9125     }
9126   mp->local_start_port = htons (p->lport.start);
9127   mp->local_stop_port = htons (p->lport.stop);
9128   mp->remote_start_port = htons (p->rport.start);
9129   mp->remote_stop_port = htons (p->rport.stop);
9130   mp->protocol = p->protocol;
9131   mp->policy = p->policy;
9132   mp->sa_id = htonl (p->sa_id);
9133   mp->bytes = clib_host_to_net_u64 (p->counter.bytes);
9134   mp->packets = clib_host_to_net_u64 (p->counter.packets);
9135
9136   vl_msg_api_send_shmem (q, (u8 *) & mp);
9137 }
9138
9139 static void
9140 vl_api_ipsec_spd_dump_t_handler (vl_api_ipsec_spd_dump_t * mp)
9141 {
9142   unix_shared_memory_queue_t *q;
9143   ipsec_main_t *im = &ipsec_main;
9144   ipsec_policy_t *policy;
9145   ipsec_spd_t *spd;
9146   uword *p;
9147   u32 spd_index;
9148 #if IPSEC > 0
9149   q = vl_api_client_index_to_input_queue (mp->client_index);
9150   if (q == 0)
9151     return;
9152
9153   p = hash_get (im->spd_index_by_spd_id, ntohl (mp->spd_id));
9154   if (!p)
9155     return;
9156
9157   spd_index = p[0];
9158   spd = pool_elt_at_index (im->spds, spd_index);
9159
9160   pool_foreach (policy, spd->policies, (
9161                                          {
9162                                          if (mp->sa_id == ~(0)
9163                                              || ntohl (mp->sa_id) ==
9164                                              policy->sa_id)
9165                                          send_ipsec_spd_details (policy, q,
9166                                                                  mp->context);}
9167                 ));
9168 #else
9169   clib_warning ("unimplemented");
9170 #endif
9171 }
9172
9173 #define BOUNCE_HANDLER(nn)                                              \
9174 static void vl_api_##nn##_t_handler (                                   \
9175     vl_api_##nn##_t *mp)                                                \
9176 {                                                                       \
9177     vpe_client_registration_t *reg;                                     \
9178     vpe_api_main_t * vam = &vpe_api_main;                               \
9179     unix_shared_memory_queue_t * q;                                     \
9180                                                                         \
9181     /* One registration only... */                                      \
9182     pool_foreach(reg, vam->nn##_registrations,                          \
9183     ({                                                                  \
9184         q = vl_api_client_index_to_input_queue (reg->client_index);     \
9185         if (q) {                                                        \
9186             /*                                                          \
9187              * If the queue is stuffed, turf the msg and complain       \
9188              * It's unlikely that the intended recipient is             \
9189              * alive; avoid deadlock at all costs.                      \
9190              */                                                         \
9191             if (q->cursize == q->maxsize) {                             \
9192                 clib_warning ("ERROR: receiver queue full, drop msg");  \
9193                 vl_msg_api_free (mp);                                   \
9194                 return;                                                 \
9195             }                                                           \
9196             vl_msg_api_send_shmem (q, (u8 *)&mp);                       \
9197             return;                                                     \
9198         }                                                               \
9199     }));                                                                \
9200     vl_msg_api_free (mp);                                               \
9201 }
9202
9203 /*
9204  * vpe_api_hookup
9205  * Add vpe's API message handlers to the table.
9206  * vlib has alread mapped shared memory and
9207  * added the client registration handlers.
9208  * See .../open-repo/vlib/memclnt_vlib.c:memclnt_process()
9209  */
9210
9211 static clib_error_t *
9212 vpe_api_hookup (vlib_main_t * vm)
9213 {
9214   api_main_t *am = &api_main;
9215
9216 #define _(N,n)                                                  \
9217     vl_msg_api_set_handlers(VL_API_##N, #n,                     \
9218                            vl_api_##n##_t_handler,              \
9219                            vl_noop_handler,                     \
9220                            vl_api_##n##_t_endian,               \
9221                            vl_api_##n##_t_print,                \
9222                            sizeof(vl_api_##n##_t), 1);
9223   foreach_vpe_api_msg;
9224 #undef _
9225
9226   /*
9227    * Manually register the sr tunnel add del msg, so we trace
9228    * enough bytes to capture a typical segment list
9229    */
9230   vl_msg_api_set_handlers (VL_API_SR_TUNNEL_ADD_DEL,
9231                            "sr_tunnel_add_del",
9232                            vl_api_sr_tunnel_add_del_t_handler,
9233                            vl_noop_handler,
9234                            vl_api_sr_tunnel_add_del_t_endian,
9235                            vl_api_sr_tunnel_add_del_t_print, 256, 1);
9236
9237
9238   /*
9239    * Manually register the sr policy add del msg, so we trace
9240    * enough bytes to capture a typical tunnel name list
9241    */
9242   vl_msg_api_set_handlers (VL_API_SR_POLICY_ADD_DEL,
9243                            "sr_policy_add_del",
9244                            vl_api_sr_policy_add_del_t_handler,
9245                            vl_noop_handler,
9246                            vl_api_sr_policy_add_del_t_endian,
9247                            vl_api_sr_policy_add_del_t_print, 256, 1);
9248
9249   /*
9250    * Trace space for 8 MPLS encap labels, classifier mask+match
9251    */
9252   am->api_trace_cfg[VL_API_MPLS_ADD_DEL_ENCAP].size += 8 * sizeof (u32);
9253   am->api_trace_cfg[VL_API_CLASSIFY_ADD_DEL_TABLE].size += 5 * sizeof (u32x4);
9254   am->api_trace_cfg[VL_API_CLASSIFY_ADD_DEL_SESSION].size
9255     += 5 * sizeof (u32x4);
9256   am->api_trace_cfg[VL_API_VXLAN_ADD_DEL_TUNNEL].size += 16 * sizeof (u32);
9257
9258   /*
9259    * Thread-safe API messages
9260    */
9261   am->is_mp_safe[VL_API_IP_ADD_DEL_ROUTE] = 1;
9262   am->is_mp_safe[VL_API_GET_NODE_GRAPH] = 1;
9263
9264   return 0;
9265 }
9266
9267 VLIB_API_INIT_FUNCTION (vpe_api_hookup);
9268
9269 static clib_error_t *
9270 vpe_api_init (vlib_main_t * vm)
9271 {
9272   vpe_api_main_t *am = &vpe_api_main;
9273
9274   am->vlib_main = vm;
9275   am->vnet_main = vnet_get_main ();
9276   am->interface_events_registration_hash = hash_create (0, sizeof (uword));
9277   am->to_netconf_server_registration_hash = hash_create (0, sizeof (uword));
9278   am->from_netconf_server_registration_hash = hash_create (0, sizeof (uword));
9279   am->to_netconf_client_registration_hash = hash_create (0, sizeof (uword));
9280   am->from_netconf_client_registration_hash = hash_create (0, sizeof (uword));
9281   am->oam_events_registration_hash = hash_create (0, sizeof (uword));
9282
9283   vl_api_init (vm);
9284   vl_set_memory_region_name ("/vpe-api");
9285   vl_enable_disable_memory_api (vm, 1 /* enable it */ );
9286
9287   return 0;
9288 }
9289
9290 VLIB_INIT_FUNCTION (vpe_api_init);
9291
9292
9293 static clib_error_t *
9294 api_segment_config (vlib_main_t * vm, unformat_input_t * input)
9295 {
9296   u8 *chroot_path;
9297   u64 baseva, size, pvt_heap_size;
9298   int uid, gid, rv;
9299   const int max_buf_size = 4096;
9300   char *s, *buf;
9301   struct passwd _pw, *pw;
9302   struct group _grp, *grp;
9303   clib_error_t *e;
9304   buf = vec_new (char, 128);
9305   while (unformat_check_input (input) != UNFORMAT_END_OF_INPUT)
9306     {
9307       if (unformat (input, "prefix %s", &chroot_path))
9308         {
9309           vec_add1 (chroot_path, 0);
9310           vl_set_memory_root_path ((char *) chroot_path);
9311         }
9312       else if (unformat (input, "uid %d", &uid))
9313         vl_set_memory_uid (uid);
9314       else if (unformat (input, "gid %d", &gid))
9315         vl_set_memory_gid (gid);
9316       else if (unformat (input, "baseva %llx", &baseva))
9317         vl_set_global_memory_baseva (baseva);
9318       else if (unformat (input, "global-size %lldM", &size))
9319         vl_set_global_memory_size (size * (1ULL << 20));
9320       else if (unformat (input, "global-size %lldG", &size))
9321         vl_set_global_memory_size (size * (1ULL << 30));
9322       else if (unformat (input, "global-size %lld", &size))
9323         vl_set_global_memory_size (size);
9324       else if (unformat (input, "global-pvt-heap-size %lldM", &pvt_heap_size))
9325         vl_set_global_pvt_heap_size (pvt_heap_size * (1ULL << 20));
9326       else if (unformat (input, "global-pvt-heap-size size %lld",
9327                          &pvt_heap_size))
9328         vl_set_global_pvt_heap_size (pvt_heap_size);
9329       else if (unformat (input, "api-pvt-heap-size %lldM", &pvt_heap_size))
9330         vl_set_api_pvt_heap_size (pvt_heap_size * (1ULL << 20));
9331       else if (unformat (input, "api-pvt-heap-size size %lld",
9332                          &pvt_heap_size))
9333         vl_set_api_pvt_heap_size (pvt_heap_size);
9334       else if (unformat (input, "api-size %lldM", &size))
9335         vl_set_api_memory_size (size * (1ULL << 20));
9336       else if (unformat (input, "api-size %lldG", &size))
9337         vl_set_api_memory_size (size * (1ULL << 30));
9338       else if (unformat (input, "api-size %lld", &size))
9339         vl_set_api_memory_size (size);
9340       else if (unformat (input, "uid %s", &s))
9341         {
9342           /* lookup the username */
9343           pw = NULL;
9344           while (((rv =
9345                    getpwnam_r (s, &_pw, buf, vec_len (buf), &pw)) == ERANGE)
9346                  && (vec_len (buf) <= max_buf_size))
9347             {
9348               vec_resize (buf, vec_len (buf) * 2);
9349             }
9350           if (rv < 0)
9351             {
9352               e = clib_error_return_code (0, rv,
9353                                           CLIB_ERROR_ERRNO_VALID |
9354                                           CLIB_ERROR_FATAL,
9355                                           "cannot fetch username %s", s);
9356               vec_free (s);
9357               vec_free (buf);
9358               return e;
9359             }
9360           if (pw == NULL)
9361             {
9362               e =
9363                 clib_error_return_fatal (0, "username %s does not exist", s);
9364               vec_free (s);
9365               vec_free (buf);
9366               return e;
9367             }
9368           vec_free (s);
9369           vl_set_memory_uid (pw->pw_uid);
9370         }
9371       else if (unformat (input, "gid %s", &s))
9372         {
9373           /* lookup the group name */
9374           grp = NULL;
9375           while (((rv =
9376                    getgrnam_r (s, &_grp, buf, vec_len (buf), &grp)) == ERANGE)
9377                  && (vec_len (buf) <= max_buf_size))
9378             {
9379               vec_resize (buf, vec_len (buf) * 2);
9380             }
9381           if (rv != 0)
9382             {
9383               e = clib_error_return_code (0, rv,
9384                                           CLIB_ERROR_ERRNO_VALID |
9385                                           CLIB_ERROR_FATAL,
9386                                           "cannot fetch group %s", s);
9387               vec_free (s);
9388               vec_free (buf);
9389               return e;
9390             }
9391           if (grp == NULL)
9392             {
9393               e = clib_error_return_fatal (0, "group %s does not exist", s);
9394               vec_free (s);
9395               vec_free (buf);
9396               return e;
9397             }
9398           vec_free (s);
9399           vec_free (buf);
9400           vl_set_memory_gid (grp->gr_gid);
9401         }
9402       else
9403         return clib_error_return (0, "unknown input `%U'",
9404                                   format_unformat_error, input);
9405     }
9406   return 0;
9407 }
9408
9409 VLIB_EARLY_CONFIG_FUNCTION (api_segment_config, "api-segment");
9410
9411 void *
9412 get_unformat_vnet_sw_interface (void)
9413 {
9414   return (void *) &unformat_vnet_sw_interface;
9415 }
9416
9417 #undef vl_api_version
9418 #define vl_api_version(n,v) static u32 vpe_api_version = v;
9419 #include <vpp-api/vpe.api.h>
9420 #undef vl_api_version
9421
9422 int
9423 vl_msg_api_version_check (vl_api_memclnt_create_t * mp)
9424 {
9425   if (clib_host_to_net_u32 (mp->api_versions[0]) != vpe_api_version)
9426     {
9427       clib_warning ("vpe API mismatch: 0x%08x instead of 0x%08x",
9428                     clib_host_to_net_u32 (mp->api_versions[0]),
9429                     vpe_api_version);
9430       return -1;
9431     }
9432   return 0;
9433 }
9434
9435 static u8 *
9436 format_arp_event (u8 * s, va_list * args)
9437 {
9438   vl_api_ip4_arp_event_t *event = va_arg (*args, vl_api_ip4_arp_event_t *);
9439
9440   s = format (s, "pid %d: ", event->pid);
9441   if (event->mac_ip)
9442     s = format (s, "bd mac/ip4 binding events");
9443   else
9444     s = format (s, "resolution for %U", format_ip4_address, &event->address);
9445   return s;
9446 }
9447
9448 static u8 *
9449 format_nd_event (u8 * s, va_list * args)
9450 {
9451   vl_api_ip6_nd_event_t *event = va_arg (*args, vl_api_ip6_nd_event_t *);
9452
9453   s = format (s, "pid %d: ", event->pid);
9454   if (event->mac_ip)
9455     s = format (s, "bd mac/ip6 binding events");
9456   else
9457     s = format (s, "resolution for %U", format_ip6_address, event->address);
9458   return s;
9459 }
9460
9461 static clib_error_t *
9462 show_ip_arp_nd_events_fn (vlib_main_t * vm,
9463                           unformat_input_t * input, vlib_cli_command_t * cmd)
9464 {
9465   vpe_api_main_t *am = &vpe_api_main;
9466   vl_api_ip4_arp_event_t *arp_event;
9467   vl_api_ip6_nd_event_t *nd_event;
9468
9469   if ((pool_elts (am->arp_events) == 0) && (pool_elts (am->nd_events) == 0))
9470     {
9471       vlib_cli_output (vm, "No active arp or nd event registrations");
9472       return 0;
9473     }
9474
9475   /* *INDENT-OFF* */
9476   pool_foreach (arp_event, am->arp_events,
9477   ({
9478     vlib_cli_output (vm, "%U", format_arp_event, arp_event);
9479   }));
9480
9481   pool_foreach (nd_event, am->nd_events,
9482   ({
9483     vlib_cli_output (vm, "%U", format_nd_event, nd_event);
9484   }));
9485   /* *INDENT-ON* */
9486
9487   return 0;
9488 }
9489
9490 /* *INDENT-OFF* */
9491 VLIB_CLI_COMMAND (show_ip_arp_nd_events, static) = {
9492   .path = "show arp-nd-event registrations",
9493   .function = show_ip_arp_nd_events_fn,
9494   .short_help = "Show ip4 arp and ip6 nd event registrations",
9495 };
9496 /* *INDENT-ON* */
9497
9498 /*
9499  * fd.io coding-style-patch-verification: ON
9500  *
9501  * Local Variables:
9502  * eval: (c-set-style "gnu")
9503  * End:
9504  */