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