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