f6b1b7392510466076ec3bb818da975dc11720d1
[vpp.git] / vpp / vpp-api / api.c
1 /*
2  *------------------------------------------------------------------
3  * api.c - message handler registration
4  *
5  * Copyright (c) 2010 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> // alagalah TODO : committers please pay note, is this ok?
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/unix/tuntap.h>
55 #include <vnet/unix/tapcli.h>
56 #include <vnet/mpls-gre/mpls.h>
57 #include <vnet/dhcp/proxy.h>
58 #include <vnet/dhcp/client.h>
59 #if IPV6SR > 0
60 #include <vnet/sr/sr.h>
61 #endif
62 #include <vnet/dhcpv6/proxy.h>
63 #include <vlib/vlib.h>
64 #include <vlib/unix/unix.h>
65 #include <vlibapi/api.h>
66 #include <vlibmemory/api.h>
67 #include <vnet/classify/vnet_classify.h>
68 #include <vnet/classify/input_acl.h>
69 #include <vnet/l2/l2_classify.h>
70 #include <vnet/vxlan/vxlan.h>
71 #include <vnet/gre/gre.h>
72 #include <vnet/l2/l2_vtr.h>
73 #include <vnet/vxlan-gpe/vxlan_gpe.h>
74 #include <vnet/lisp-gpe/lisp_gpe.h>
75 #include <vnet/lisp-cp/control.h>
76 #include <vnet/map/map.h>
77 #include <vnet/cop/cop.h>
78 #include <vnet/ip/ip6_hop_by_hop.h>
79 #include <vnet/devices/af_packet/af_packet.h>
80 #include <vnet/policer/policer.h>
81 #include <vnet/devices/netmap/netmap.h>
82 #include <vnet/flow/flow_report.h>
83
84 #undef BIHASH_TYPE
85 #undef __included_bihash_template_h__
86 #include <vnet/l2/l2_fib.h>
87
88 #if IPSEC > 0
89 #include <vnet/ipsec/ipsec.h>
90 #include <vnet/ipsec/ikev2.h>
91 #endif /* IPSEC */
92 #if DPDK > 0
93 #include <vnet/devices/virtio/vhost-user.h>
94 #endif
95
96 #include <stats/stats.h>
97 #include <oam/oam.h>
98
99 #include <vnet/ethernet/ethernet.h>
100 #include <vnet/ethernet/arp_packet.h>
101 #include <vnet/interface.h>
102
103 #include <vnet/l2/l2_fib.h>
104 #include <vnet/l2/l2_bd.h>
105 #include <vpp-api/vpe_msg_enum.h>
106
107 #define f64_endian(a)
108 #define f64_print(a,b)
109
110 #define vl_typedefs             /* define message structures */
111 #include <vpp-api/vpe_all_api_h.h>
112 #undef vl_typedefs
113
114 #define vl_endianfun             /* define message structures */
115 #include <vpp-api/vpe_all_api_h.h>
116 #undef vl_endianfun
117
118 /* instantiate all the print functions we know about */
119 #define vl_print(handle, ...) vlib_cli_output (handle, __VA_ARGS__)
120 #define vl_printfun
121 #include <vpp-api/vpe_all_api_h.h>
122 #undef vl_printfun
123
124 #define REPLY_MACRO(t)                                          \
125 do {                                                            \
126     unix_shared_memory_queue_t * q;                             \
127     rv = vl_msg_api_pd_handler (mp, rv);                        \
128     q = vl_api_client_index_to_input_queue (mp->client_index);  \
129     if (!q)                                                     \
130         return;                                                 \
131                                                                 \
132     rmp = vl_msg_api_alloc (sizeof (*rmp));                     \
133     rmp->_vl_msg_id = ntohs((t));                               \
134     rmp->context = mp->context;                                 \
135     rmp->retval = ntohl(rv);                                    \
136                                                                 \
137     vl_msg_api_send_shmem (q, (u8 *)&rmp);                      \
138 } while(0);
139
140 #define REPLY_MACRO2(t, body)                                   \
141 do {                                                            \
142     unix_shared_memory_queue_t * q;                             \
143     rv = vl_msg_api_pd_handler (mp, rv);                        \
144     q = vl_api_client_index_to_input_queue (mp->client_index);  \
145     if (!q)                                                     \
146         return;                                                 \
147                                                                 \
148     rmp = vl_msg_api_alloc (sizeof (*rmp));                     \
149     rmp->_vl_msg_id = ntohs((t));                               \
150     rmp->context = mp->context;                                 \
151     rmp->retval = ntohl(rv);                                    \
152     do {body;} while (0);                                       \
153     vl_msg_api_send_shmem (q, (u8 *)&rmp);                      \
154 } while(0);
155
156 #if (1 || CLIB_DEBUG > 0)       /* "trust, but verify" */
157
158 #define VALIDATE_SW_IF_INDEX(mp)                                \
159  do { u32 __sw_if_index = ntohl(mp->sw_if_index);               \
160     vnet_main_t *__vnm = vnet_get_main();                       \
161     if (pool_is_free_index(__vnm->interface_main.sw_interfaces, \
162                            __sw_if_index)) {                    \
163         rv = VNET_API_ERROR_INVALID_SW_IF_INDEX;                \
164         goto bad_sw_if_index;                                   \
165     }                                                           \
166 } while(0);
167
168 #define BAD_SW_IF_INDEX_LABEL                   \
169 do {                                            \
170 bad_sw_if_index:                                \
171     ;                                           \
172 } while (0);
173
174 #define VALIDATE_RX_SW_IF_INDEX(mp)                             \
175  do { u32 __rx_sw_if_index = ntohl(mp->rx_sw_if_index);         \
176     vnet_main_t *__vnm = vnet_get_main();                       \
177     if (pool_is_free_index(__vnm->interface_main.sw_interfaces, \
178                            __rx_sw_if_index)) {                 \
179         rv = VNET_API_ERROR_INVALID_SW_IF_INDEX;                \
180         goto bad_rx_sw_if_index;                                \
181     }                                                           \
182 } while(0);
183
184 #define BAD_RX_SW_IF_INDEX_LABEL                \
185 do {                                            \
186 bad_rx_sw_if_index:                             \
187     ;                                           \
188 } while (0);
189
190 #define VALIDATE_TX_SW_IF_INDEX(mp)                             \
191  do { u32 __tx_sw_if_index = ntohl(mp->tx_sw_if_index);         \
192     vnet_main_t *__vnm = vnet_get_main();                       \
193     if (pool_is_free_index(__vnm->interface_main.sw_interfaces, \
194                            __tx_sw_if_index)) {                 \
195         rv = VNET_API_ERROR_INVALID_SW_IF_INDEX;                \
196         goto bad_tx_sw_if_index;                                \
197     }                                                           \
198 } while(0);
199
200 #define BAD_TX_SW_IF_INDEX_LABEL                \
201 do {                                            \
202 bad_tx_sw_if_index:                             \
203     ;                                           \
204 } while (0);
205
206 #else
207
208 #define VALIDATE_SW_IF_INDEX(mp)
209 #define BAD_SW_IF_INDEX_LABEL
210 #define VALIDATE_RX_SW_IF_INDEX(mp)
211 #define BAD_RX_SW_IF_INDEX_LABEL
212 #define VALIDATE_TX_SW_IF_INDEX(mp)
213 #define BAD_TX_SW_IF_INDEX_LABEL
214
215 #endif  /* CLIB_DEBUG > 0 */
216
217 #define foreach_vpe_api_msg                                             \
218 _(WANT_INTERFACE_EVENTS, want_interface_events)                         \
219 _(WANT_OAM_EVENTS, want_oam_events)                                     \
220 _(OAM_ADD_DEL, oam_add_del)                                             \
221 _(SW_INTERFACE_DUMP, sw_interface_dump)                                 \
222 _(SW_INTERFACE_DETAILS, sw_interface_details)                           \
223 _(SW_INTERFACE_SET_FLAGS, sw_interface_set_flags)                       \
224 _(IP_ADD_DEL_ROUTE, ip_add_del_route)                                   \
225 _(IS_ADDRESS_REACHABLE, is_address_reachable)                           \
226 _(SW_INTERFACE_ADD_DEL_ADDRESS, sw_interface_add_del_address)           \
227 _(SW_INTERFACE_SET_TABLE, sw_interface_set_table)                       \
228 _(SW_INTERFACE_SET_VPATH, sw_interface_set_vpath)                       \
229 _(SW_INTERFACE_SET_L2_XCONNECT, sw_interface_set_l2_xconnect)           \
230 _(SW_INTERFACE_SET_L2_BRIDGE, sw_interface_set_l2_bridge)               \
231 _(BRIDGE_DOMAIN_ADD_DEL, bridge_domain_add_del)                         \
232 _(BRIDGE_DOMAIN_DUMP, bridge_domain_dump)                               \
233 _(BRIDGE_DOMAIN_DETAILS, bridge_domain_details)                         \
234 _(BRIDGE_DOMAIN_SW_IF_DETAILS, bridge_domain_sw_if_details)             \
235 _(L2FIB_ADD_DEL, l2fib_add_del)                                         \
236 _(L2_FLAGS, l2_flags)                                                   \
237 _(BRIDGE_FLAGS, bridge_flags)                                           \
238 _(TAP_CONNECT, tap_connect)                                             \
239 _(TAP_MODIFY, tap_modify)                                               \
240 _(TAP_DELETE, tap_delete)                                               \
241 _(SW_INTERFACE_TAP_DUMP, sw_interface_tap_dump)                         \
242 _(CREATE_VLAN_SUBIF, create_vlan_subif)                                 \
243 _(CREATE_SUBIF, create_subif)                                           \
244 _(MPLS_GRE_ADD_DEL_TUNNEL, mpls_gre_add_del_tunnel)                     \
245 _(MPLS_ETHERNET_ADD_DEL_TUNNEL, mpls_ethernet_add_del_tunnel)           \
246 _(MPLS_ETHERNET_ADD_DEL_TUNNEL_2, mpls_ethernet_add_del_tunnel_2)       \
247 _(MPLS_ADD_DEL_ENCAP, mpls_add_del_encap)                               \
248 _(MPLS_ADD_DEL_DECAP, mpls_add_del_decap)                               \
249 _(PROXY_ARP_ADD_DEL, proxy_arp_add_del)                                 \
250 _(PROXY_ARP_INTFC_ENABLE_DISABLE, proxy_arp_intfc_enable_disable)       \
251 _(IP_NEIGHBOR_ADD_DEL, ip_neighbor_add_del)                             \
252 _(VNET_GET_SUMMARY_STATS, vnet_get_summary_stats)                       \
253 _(RESET_FIB, reset_fib)                                                 \
254 _(DHCP_PROXY_CONFIG,dhcp_proxy_config)                                  \
255 _(DHCP_PROXY_CONFIG_2,dhcp_proxy_config_2)                              \
256 _(DHCP_PROXY_SET_VSS,dhcp_proxy_set_vss)                                \
257 _(DHCP_CLIENT_CONFIG, dhcp_client_config)                               \
258 _(SET_IP_FLOW_HASH,set_ip_flow_hash)                                    \
259 _(SW_INTERFACE_IP6ND_RA_CONFIG, sw_interface_ip6nd_ra_config)           \
260 _(SW_INTERFACE_IP6ND_RA_PREFIX, sw_interface_ip6nd_ra_prefix)           \
261 _(SW_INTERFACE_IP6_ENABLE_DISABLE, sw_interface_ip6_enable_disable )    \
262 _(SW_INTERFACE_IP6_SET_LINK_LOCAL_ADDRESS,                              \
263   sw_interface_ip6_set_link_local_address)                              \
264 _(SW_INTERFACE_SET_UNNUMBERED, sw_interface_set_unnumbered)             \
265 _(CREATE_LOOPBACK, create_loopback)                                     \
266 _(CONTROL_PING, control_ping)                                           \
267 _(CLI_REQUEST, cli_request)                                             \
268 _(SET_ARP_NEIGHBOR_LIMIT, set_arp_neighbor_limit)                       \
269 _(L2_PATCH_ADD_DEL, l2_patch_add_del)                                   \
270 _(CLASSIFY_ADD_DEL_TABLE, classify_add_del_table)                       \
271 _(CLASSIFY_ADD_DEL_SESSION, classify_add_del_session)                   \
272 _(CLASSIFY_SET_INTERFACE_IP_TABLE, classify_set_interface_ip_table)     \
273 _(CLASSIFY_SET_INTERFACE_L2_TABLES, classify_set_interface_l2_tables)   \
274 _(GET_NODE_INDEX, get_node_index)                                       \
275 _(ADD_NODE_NEXT, add_node_next)                                         \
276 _(L2TPV3_CREATE_TUNNEL, l2tpv3_create_tunnel)                           \
277 _(L2TPV3_SET_TUNNEL_COOKIES, l2tpv3_set_tunnel_cookies)                 \
278 _(L2TPV3_INTERFACE_ENABLE_DISABLE, l2tpv3_interface_enable_disable)     \
279 _(L2TPV3_SET_LOOKUP_KEY, l2tpv3_set_lookup_key)                         \
280 _(SW_IF_L2TPV3_TUNNEL_DUMP, sw_if_l2tpv3_tunnel_dump)                   \
281 _(VXLAN_ADD_DEL_TUNNEL, vxlan_add_del_tunnel)                           \
282 _(VXLAN_TUNNEL_DUMP, vxlan_tunnel_dump)                                 \
283 _(GRE_ADD_DEL_TUNNEL, gre_add_del_tunnel)                               \
284 _(GRE_TUNNEL_DUMP, gre_tunnel_dump)                                     \
285 _(L2_FIB_CLEAR_TABLE, l2_fib_clear_table)                               \
286 _(L2_INTERFACE_EFP_FILTER, l2_interface_efp_filter)                     \
287 _(L2_INTERFACE_VLAN_TAG_REWRITE, l2_interface_vlan_tag_rewrite)         \
288 _(CREATE_VHOST_USER_IF, create_vhost_user_if)                           \
289 _(MODIFY_VHOST_USER_IF, modify_vhost_user_if)                           \
290 _(DELETE_VHOST_USER_IF, delete_vhost_user_if)                           \
291 _(SW_INTERFACE_VHOST_USER_DUMP, sw_interface_vhost_user_dump)           \
292 _(IP_ADDRESS_DUMP, ip_address_dump)                                     \
293 _(IP_DUMP, ip_dump)                                                     \
294 _(SW_INTERFACE_VHOST_USER_DETAILS, sw_interface_vhost_user_details)     \
295 _(SHOW_VERSION, show_version)                                           \
296 _(L2_FIB_TABLE_DUMP, l2_fib_table_dump)                                 \
297 _(L2_FIB_TABLE_ENTRY, l2_fib_table_entry)                               \
298 _(VXLAN_GPE_ADD_DEL_TUNNEL, vxlan_gpe_add_del_tunnel)                   \
299 _(VXLAN_GPE_TUNNEL_DUMP, vxlan_gpe_tunnel_dump)                         \
300 _(INTERFACE_NAME_RENUMBER, interface_name_renumber)                     \
301 _(WANT_IP4_ARP_EVENTS, want_ip4_arp_events)                             \
302 _(INPUT_ACL_SET_INTERFACE, input_acl_set_interface)                     \
303 _(IPSEC_SPD_ADD_DEL, ipsec_spd_add_del)                                 \
304 _(IPSEC_INTERFACE_ADD_DEL_SPD, ipsec_interface_add_del_spd)             \
305 _(IPSEC_SPD_ADD_DEL_ENTRY, ipsec_spd_add_del_entry)                     \
306 _(IPSEC_SAD_ADD_DEL_ENTRY, ipsec_sad_add_del_entry)                     \
307 _(IPSEC_SA_SET_KEY, ipsec_sa_set_key)                                   \
308 _(IKEV2_PROFILE_ADD_DEL, ikev2_profile_add_del)                         \
309 _(IKEV2_PROFILE_SET_AUTH, ikev2_profile_set_auth)                       \
310 _(IKEV2_PROFILE_SET_ID, ikev2_profile_set_id)                           \
311 _(IKEV2_PROFILE_SET_TS, ikev2_profile_set_ts)                           \
312 _(IKEV2_SET_LOCAL_KEY, ikev2_set_local_key)                             \
313 _(DELETE_LOOPBACK, delete_loopback)                                     \
314 _(BD_IP_MAC_ADD_DEL, bd_ip_mac_add_del)                                 \
315 _(MAP_ADD_DOMAIN, map_add_domain)                                       \
316 _(MAP_DEL_DOMAIN, map_del_domain)                                       \
317 _(MAP_ADD_DEL_RULE, map_add_del_rule)                                   \
318 _(MAP_DOMAIN_DUMP, map_domain_dump)                                     \
319 _(MAP_RULE_DUMP, map_rule_dump)                                         \
320 _(MAP_SUMMARY_STATS, map_summary_stats)                                 \
321 _(COP_INTERFACE_ENABLE_DISABLE, cop_interface_enable_disable)           \
322 _(COP_WHITELIST_ENABLE_DISABLE, cop_whitelist_enable_disable)           \
323 _(GET_NODE_GRAPH, get_node_graph)                                       \
324 _(SW_INTERFACE_CLEAR_STATS, sw_interface_clear_stats)                   \
325 _(TRACE_PROFILE_ADD, trace_profile_add)                                 \
326 _(TRACE_PROFILE_APPLY, trace_profile_apply)                             \
327 _(TRACE_PROFILE_DEL, trace_profile_del)                                 \
328 _(LISP_ADD_DEL_LOCATOR_SET, lisp_add_del_locator_set)                   \
329 _(LISP_ADD_DEL_LOCATOR, lisp_add_del_locator)                           \
330 _(LISP_ADD_DEL_LOCAL_EID, lisp_add_del_local_eid)                       \
331 _(LISP_GPE_ADD_DEL_FWD_ENTRY, lisp_gpe_add_del_fwd_entry)               \
332 _(LISP_ADD_DEL_MAP_RESOLVER, lisp_add_del_map_resolver)                 \
333 _(LISP_GPE_ENABLE_DISABLE, lisp_gpe_enable_disable)                     \
334 _(LISP_ENABLE_DISABLE, lisp_enable_disable)                             \
335 _(LISP_GPE_ADD_DEL_IFACE, lisp_gpe_add_del_iface)                       \
336 _(LISP_ADD_DEL_REMOTE_MAPPING, lisp_add_del_remote_mapping)             \
337 _(LISP_ADD_DEL_ADJACENCY, lisp_add_del_adjacency)                       \
338 _(LISP_PITR_SET_LOCATOR_SET, lisp_pitr_set_locator_set)                 \
339 _(LISP_EID_TABLE_ADD_DEL_MAP, lisp_eid_table_add_del_map)               \
340 _(LISP_LOCATOR_SET_DUMP, lisp_locator_set_dump)                         \
341 _(LISP_LOCAL_EID_TABLE_DUMP, lisp_local_eid_table_dump)                 \
342 _(LISP_GPE_TUNNEL_DUMP, lisp_gpe_tunnel_dump)                           \
343 _(LISP_MAP_RESOLVER_DUMP, lisp_map_resolver_dump)                       \
344 _(LISP_EID_TABLE_MAP_DUMP, lisp_eid_table_map_dump)                     \
345 _(LISP_ENABLE_DISABLE_STATUS_DUMP,                                      \
346   lisp_enable_disable_status_dump)                                      \
347 _(LISP_ADD_DEL_MAP_REQUEST_ITR_RLOCS,                                   \
348   lisp_add_del_map_request_itr_rlocs)                                   \
349 _(LISP_GET_MAP_REQUEST_ITR_RLOCS, lisp_get_map_request_itr_rlocs)       \
350 _(SR_MULTICAST_MAP_ADD_DEL, sr_multicast_map_add_del)                   \
351 _(AF_PACKET_CREATE, af_packet_create)                                   \
352 _(AF_PACKET_DELETE, af_packet_delete)                                   \
353 _(POLICER_ADD_DEL, policer_add_del)                                     \
354 _(POLICER_DUMP, policer_dump)                                           \
355 _(NETMAP_CREATE, netmap_create)                                         \
356 _(NETMAP_DELETE, netmap_delete)                                         \
357 _(MPLS_GRE_TUNNEL_DUMP, mpls_gre_tunnel_dump)                           \
358 _(MPLS_GRE_TUNNEL_DETAILS, mpls_gre_tunnel_details)                     \
359 _(MPLS_ETH_TUNNEL_DUMP, mpls_eth_tunnel_dump)                           \
360 _(MPLS_ETH_TUNNEL_DETAILS, mpls_eth_tunnel_details)                     \
361 _(MPLS_FIB_ENCAP_DUMP, mpls_fib_encap_dump)                             \
362 _(MPLS_FIB_ENCAP_DETAILS, mpls_fib_encap_details)                       \
363 _(MPLS_FIB_DECAP_DUMP, mpls_fib_decap_dump)                             \
364 _(MPLS_FIB_DECAP_DETAILS, mpls_fib_decap_details)                       \
365 _(CLASSIFY_TABLE_IDS,classify_table_ids)                                \
366 _(CLASSIFY_TABLE_BY_INTERFACE, classify_table_by_interface)             \
367 _(CLASSIFY_TABLE_INFO,classify_table_info)                              \
368 _(CLASSIFY_SESSION_DUMP,classify_session_dump)                          \
369 _(CLASSIFY_SESSION_DETAILS,classify_session_details)                    \
370 _(IPFIX_ENABLE,ipfix_enable)                                            \
371 _(IPFIX_DUMP,ipfix_dump)                                                \
372 _(GET_NEXT_INDEX, get_next_index)
373
374 #define QUOTE_(x) #x
375 #define QUOTE(x) QUOTE_(x)
376
377 #define foreach_registration_hash               \
378 _(interface_events)                             \
379 _(to_netconf_server)                            \
380 _(from_netconf_server)                          \
381 _(to_netconf_client)                            \
382 _(from_netconf_client)                          \
383 _(oam_events)
384
385 typedef enum {
386     RESOLVE_IP4_ADD_DEL_ROUTE=1,
387     RESOLVE_IP6_ADD_DEL_ROUTE,
388     RESOLVE_MPLS_ETHERNET_ADD_DEL,
389 } resolve_t;
390
391 typedef struct {
392     u8 resolve_type;
393     union {
394         vl_api_ip_add_del_route_t r;
395         vl_api_mpls_ethernet_add_del_tunnel_2_t t;
396     };
397 } pending_route_t;
398
399 typedef struct {
400
401 #define _(a) uword *a##_registration_hash;              \
402     vpe_client_registration_t * a##_registrations;
403 foreach_registration_hash
404 #undef _
405
406     /* notifications happen really early in the game */
407     u8 link_state_process_up;
408
409     /* ip4 pending route adds */
410     pending_route_t * pending_routes;
411
412     /* ip4 arp event registration pool */
413     vl_api_ip4_arp_event_t * arp_events;
414
415     /* convenience */
416     vlib_main_t * vlib_main;
417     vnet_main_t * vnet_main;
418 } vpe_api_main_t;
419
420 static vlib_node_registration_t vpe_resolver_process_node;
421 static vpe_api_main_t vpe_api_main;
422
423 static void send_sw_interface_flags (vpe_api_main_t * am,
424                                      unix_shared_memory_queue_t *q,
425                                      vnet_sw_interface_t * swif);
426 static void send_sw_interface_flags_deleted (vpe_api_main_t * am,
427                                      unix_shared_memory_queue_t *q,
428                                      u32 sw_if_index);
429
430 static int arp_change_delete_callback (u32 pool_index, u8 * notused);
431
432
433 /* Clean up all registrations belonging to the indicated client */
434 int vl_api_memclnt_delete_callback (u32 client_index)
435 {
436     vpe_api_main_t * vam = &vpe_api_main;
437     vpe_client_registration_t *rp;
438     uword * p;
439     int stats_memclnt_delete_callback (u32 client_index);
440
441     stats_memclnt_delete_callback (client_index);
442
443 #define _(a)                                                    \
444     p = hash_get (vam->a##_registration_hash, client_index);    \
445     if (p) {                                                    \
446         rp = pool_elt_at_index (vam->a##_registrations, p[0]);  \
447         pool_put (vam->a##_registrations, rp);                  \
448         hash_unset (vam->a##_registration_hash, client_index);  \
449     }
450     foreach_registration_hash;
451 #undef _
452     return 0;
453 }
454
455 #define API_LINK_STATE_EVENT 1
456 #define API_ADMIN_UP_DOWN_EVENT 2
457
458 static int
459 event_data_cmp (void * a1, void * a2)
460 {
461   uword * e1 = a1;
462   uword * e2 = a2;
463
464   return (word) e1[0] - (word) e2[0];
465 }
466
467 static uword
468 link_state_process (vlib_main_t * vm,
469                     vlib_node_runtime_t * rt,
470                     vlib_frame_t * f)
471 {
472     vpe_api_main_t * vam = &vpe_api_main;
473     vnet_main_t * vnm = vam->vnet_main;
474     vnet_sw_interface_t * swif;
475     uword * event_data = 0;
476     vpe_client_registration_t *reg;
477     int i;
478     u32 prev_sw_if_index;
479     unix_shared_memory_queue_t * q;
480
481     vam->link_state_process_up = 1;
482
483     while (1) {
484         vlib_process_wait_for_event (vm);
485
486         /* Unified list of changed link or admin state sw_if_indices */
487         vlib_process_get_events_with_type
488             (vm, &event_data, API_LINK_STATE_EVENT);
489         vlib_process_get_events_with_type
490             (vm, &event_data, API_ADMIN_UP_DOWN_EVENT);
491
492         /* Sort, so we can eliminate duplicates */
493         vec_sort_with_function (event_data, event_data_cmp);
494
495         prev_sw_if_index = ~0;
496
497         for (i = 0; i < vec_len(event_data); i++) {
498             /* Only one message per swif */
499             if (prev_sw_if_index == event_data[i])
500                 continue;
501             prev_sw_if_index = event_data[i];
502
503             pool_foreach(reg, vam->interface_events_registrations,
504             ({
505                 q = vl_api_client_index_to_input_queue (reg->client_index);
506                 if (q) {
507                     // sw_interface may be deleted already
508                     if (!pool_is_free_index (vnm->interface_main.sw_interfaces,
509                             event_data[i]))
510                     {
511                         swif = vnet_get_sw_interface (vnm, event_data[i]);
512                         send_sw_interface_flags (vam, q, swif);
513                     }
514                 }
515             }));
516         }
517         vec_reset_length (event_data);
518     }
519
520     return 0;
521 }
522
523 static clib_error_t *
524 link_up_down_function (vnet_main_t *vm, u32 hw_if_index, u32 flags);
525 static clib_error_t *
526 admin_up_down_function (vnet_main_t *vm, u32 hw_if_index, u32 flags);
527
528 VLIB_REGISTER_NODE (link_state_process_node,static) = {
529     .function = link_state_process,
530     .type = VLIB_NODE_TYPE_PROCESS,
531     .name = "vpe-link-state-process",
532 };
533
534 VNET_SW_INTERFACE_ADMIN_UP_DOWN_FUNCTION (admin_up_down_function);
535 VNET_HW_INTERFACE_LINK_UP_DOWN_FUNCTION (link_up_down_function);
536
537 static clib_error_t *
538 link_up_down_function (vnet_main_t *vm, u32 hw_if_index, u32 flags)
539 {
540     vpe_api_main_t * vam = &vpe_api_main;
541     vnet_hw_interface_t *hi = vnet_get_hw_interface (vm, hw_if_index);
542
543     if (vam->link_state_process_up)
544         vlib_process_signal_event (vam->vlib_main,
545                                    link_state_process_node.index,
546                                    API_LINK_STATE_EVENT,
547                                    hi->sw_if_index);
548     return 0;
549 }
550
551 static clib_error_t *
552 admin_up_down_function (vnet_main_t *vm, u32 sw_if_index, u32 flags)
553 {
554     vpe_api_main_t * vam = &vpe_api_main;
555
556     /*
557      * Note: it's perfectly fair to set a subif admin up / admin down.
558      * Note the subtle distinction between this routine and the previous
559      * routine.
560      */
561     if (vam->link_state_process_up)
562         vlib_process_signal_event (vam->vlib_main,
563                                    link_state_process_node.index,
564                                    API_ADMIN_UP_DOWN_EVENT,
565                                    sw_if_index);
566     return 0;
567 }
568
569 #define pub_sub_handler(lca,UCA)                                        \
570 static void vl_api_want_##lca##_t_handler (                             \
571     vl_api_want_##lca##_t *mp)                                          \
572 {                                                                       \
573     vpe_api_main_t *vam = &vpe_api_main;                                \
574     vpe_client_registration_t *rp;                                      \
575     vl_api_want_##lca##_reply_t *rmp;                                   \
576     uword *p;                                                           \
577     i32 rv = 0;                                                         \
578                                                                         \
579     p = hash_get (vam->lca##_registration_hash, mp->client_index);      \
580     if (p) {                                                            \
581         if (mp->enable_disable) {                                       \
582             clib_warning ("pid %d: already enabled...", mp->pid);       \
583             rv = VNET_API_ERROR_INVALID_REGISTRATION;                   \
584             goto reply;                                                 \
585         } else {                                                        \
586             rp = pool_elt_at_index (vam->lca##_registrations, p[0]);    \
587             pool_put (vam->lca##_registrations, rp);                    \
588             hash_unset (vam->lca##_registration_hash,                   \
589                 mp->client_index);                                      \
590             goto reply;                                                 \
591         }                                                               \
592     }                                                                   \
593     if (mp->enable_disable == 0) {                                      \
594         clib_warning ("pid %d: already disabled...", mp->pid);          \
595         rv = VNET_API_ERROR_INVALID_REGISTRATION;                       \
596         goto reply;                                                     \
597     }                                                                   \
598     pool_get (vam->lca##_registrations, rp);                            \
599     rp->client_index = mp->client_index;                                \
600     rp->client_pid = mp->pid;                                           \
601     hash_set (vam->lca##_registration_hash, rp->client_index,           \
602               rp - vam->lca##_registrations);                           \
603                                                                         \
604 reply:                                                                  \
605     REPLY_MACRO (VL_API_WANT_##UCA##_REPLY);                            \
606 }
607
608 pub_sub_handler (interface_events,INTERFACE_EVENTS)
609 pub_sub_handler (oam_events,OAM_EVENTS)
610
611 #define RESOLUTION_EVENT 1
612 #define RESOLUTION_PENDING_EVENT 2
613 #define IP4_ARP_EVENT 3
614
615 static int ip4_add_del_route_t_handler (vl_api_ip_add_del_route_t *mp);
616 static int ip6_add_del_route_t_handler (vl_api_ip_add_del_route_t *mp);
617 static int mpls_ethernet_add_del_tunnel_2_t_handler
618 (vl_api_mpls_ethernet_add_del_tunnel_2_t *mp);
619
620 void handle_ip4_arp_event (u32 pool_index)
621 {
622     vpe_api_main_t * vam = &vpe_api_main;
623     vnet_main_t * vnm = vam->vnet_main;
624     vlib_main_t * vm = vam->vlib_main;
625     vl_api_ip4_arp_event_t * event;
626     vl_api_ip4_arp_event_t * mp;
627     unix_shared_memory_queue_t * q;
628
629     /* Client can cancel, die, etc. */
630     if (pool_is_free_index (vam->arp_events, pool_index))
631         return;
632
633     event = pool_elt_at_index (vam->arp_events, pool_index);
634
635     q = vl_api_client_index_to_input_queue (event->client_index);
636     if (!q) {
637         (void) vnet_add_del_ip4_arp_change_event
638             (vnm, arp_change_delete_callback,
639              event->pid, &event->address,
640              vpe_resolver_process_node.index, IP4_ARP_EVENT,
641              ~0 /* pool index, notused */, 0 /* is_add */);
642         return;
643     }
644
645     if (q->cursize < q->maxsize) {
646         mp =  vl_msg_api_alloc (sizeof (*mp));
647         clib_memcpy (mp, event, sizeof (*mp));
648         vl_msg_api_send_shmem (q, (u8 *)&mp);
649     } else {
650         static f64 last_time;
651         /*
652          * Throttle syslog msgs.
653          * It's pretty tempting to just revoke the registration...
654          */
655         if (vlib_time_now (vm) > last_time + 10.0) {
656             clib_warning ("arp event for %U to pid %d: queue stuffed!",
657                           format_ip4_address, &event->address, event->pid);
658             last_time = vlib_time_now(vm);
659         }
660     }
661 }
662
663 static uword
664 resolver_process (vlib_main_t * vm,
665                     vlib_node_runtime_t * rt,
666                     vlib_frame_t * f)
667 {
668     uword event_type;
669     uword *event_data = 0;
670     f64 timeout = 100.0;
671     vpe_api_main_t * vam = &vpe_api_main;
672     pending_route_t * pr;
673     vl_api_ip_add_del_route_t * adr;
674     vl_api_mpls_ethernet_add_del_tunnel_2_t *pme;
675     u32 * resolution_failures = 0;
676     int i, rv;
677     clib_error_t * e;
678
679     while (1) {
680         vlib_process_wait_for_event_or_clock (vm, timeout);
681
682         event_type = vlib_process_get_events (vm, &event_data);
683
684         switch (event_type) {
685         case RESOLUTION_PENDING_EVENT:
686             timeout = 1.0;
687             break;
688
689         case RESOLUTION_EVENT:
690             for (i = 0; i < vec_len(event_data); i++) {
691                 /*
692                  * Resolution events can occur long after the
693                  * original request has timed out. $$$ add a cancel
694                  * mechanism..
695                  */
696                 if (pool_is_free_index (vam->pending_routes, event_data[i]))
697                     continue;
698
699                 pr = pool_elt_at_index (vam->pending_routes, event_data[i]);
700                 adr = &pr->r;
701                 pme = &pr->t;
702
703                 switch (pr->resolve_type) {
704                 case RESOLVE_IP4_ADD_DEL_ROUTE:
705                     rv = ip4_add_del_route_t_handler (adr);
706                     clib_warning ("resolver: add %U/%d via %U %s",
707                                   format_ip4_address,
708                                   (ip4_address_t *)&(adr->dst_address),
709                                   adr->dst_address_length,
710                                   format_ip4_address,
711                                   (ip4_address_t *)&(adr->next_hop_address),
712                                   (rv >= 0) ? "succeeded" : "failed");
713                     break;
714
715                 case RESOLVE_IP6_ADD_DEL_ROUTE:
716                     rv = ip6_add_del_route_t_handler (adr);
717                     clib_warning ("resolver: add %U/%d via %U %s",
718                                   format_ip6_address,
719                                   (ip6_address_t *)&(adr->dst_address),
720                                   adr->dst_address_length,
721                                   format_ip6_address,
722                                   (ip6_address_t *)&(adr->next_hop_address),
723                                   (rv >= 0) ? "succeeded" : "failed");
724                     break;
725
726                 case RESOLVE_MPLS_ETHERNET_ADD_DEL:
727                     rv = mpls_ethernet_add_del_tunnel_2_t_handler (pme);
728                     clib_warning ("resolver: add mpls-o-e via %U %s",
729                                   format_ip4_address,
730             (ip4_address_t *)&(pme->next_hop_ip4_address_in_outer_vrf),
731                                   (rv >= 0) ? "succeeded" : "failed");
732                     break;
733
734                 default:
735                     clib_warning ("resolver: BOGUS TYPE %d", pr->resolve_type);
736                 }
737                 pool_put (vam->pending_routes, pr);
738             }
739             break;
740
741         case IP4_ARP_EVENT:
742             for (i = 0; i < vec_len(event_data); i++)
743                 handle_ip4_arp_event (event_data[i]);
744             break;
745
746         case ~0:                /* timeout, retry pending resolutions */
747             pool_foreach (pr, vam->pending_routes,
748             ({
749                 int is_adr = 1;
750                 adr = &pr->r;
751                 pme = &pr->t;
752
753                 /* May fail, e.g. due to interface down */
754                 switch (pr->resolve_type) {
755                 case RESOLVE_IP4_ADD_DEL_ROUTE:
756                     e = ip4_probe_neighbor
757                         (vm, (ip4_address_t *)&(adr->next_hop_address),
758                          ntohl(adr->next_hop_sw_if_index));
759                     break;
760
761                 case RESOLVE_IP6_ADD_DEL_ROUTE:
762                     e = ip6_probe_neighbor
763                         (vm, (ip6_address_t *)&(adr->next_hop_address),
764                          ntohl(adr->next_hop_sw_if_index));
765                     break;
766
767                 case RESOLVE_MPLS_ETHERNET_ADD_DEL:
768                     is_adr = 0;
769                     e = ip4_probe_neighbor
770                         (vm,
771             (ip4_address_t *)&(pme->next_hop_ip4_address_in_outer_vrf),
772                          pme->resolve_opaque);
773                     break;
774
775                 default:
776                     e = clib_error_return (0, "resolver: BOGUS TYPE %d",
777                                            pr->resolve_type);
778                 }
779                 if (e) {
780                     clib_error_report (e);
781                     if (is_adr)
782                         adr->resolve_attempts = 1;
783                     else
784                         pme->resolve_attempts = 1;
785
786                 }
787                 if (is_adr) {
788                     adr->resolve_attempts -= 1;
789                     if (adr->resolve_attempts == 0)
790                         vec_add1 (resolution_failures,
791                                   pr - vam->pending_routes);
792                 } else {
793                     pme->resolve_attempts -= 1;
794                     if (pme->resolve_attempts == 0)
795                         vec_add1 (resolution_failures,
796                                   pr - vam->pending_routes);
797                 }
798
799             }));
800             for (i = 0; i < vec_len (resolution_failures); i++) {
801                 pr = pool_elt_at_index (vam->pending_routes,
802                                         resolution_failures[i]);
803                 adr = &pr->r;
804                 pme = &pr->t;
805
806                 switch (pr->resolve_type) {
807                 case RESOLVE_IP4_ADD_DEL_ROUTE:
808                     clib_warning ("resolver: add %U/%d via %U retry failure",
809                                   format_ip4_address,
810                                   (ip4_address_t *)&(adr->dst_address),
811                                   adr->dst_address_length,
812                                   format_ip4_address,
813                                   (ip4_address_t *)&(adr->next_hop_address));
814                     break;
815
816                 case RESOLVE_IP6_ADD_DEL_ROUTE:
817                     clib_warning ("resolver: add %U/%d via %U retry failure",
818                                   format_ip6_address,
819                                   (ip6_address_t *)&(adr->dst_address),
820                                   adr->dst_address_length,
821                                   format_ip6_address,
822                                   (ip6_address_t *)&(adr->next_hop_address));
823                     break;
824
825                 case RESOLVE_MPLS_ETHERNET_ADD_DEL:
826                     clib_warning ("resolver: add mpls-o-e via %U retry failure",
827                                   format_ip4_address,
828                    (ip4_address_t *)&(pme->next_hop_ip4_address_in_outer_vrf));
829                     break;
830
831                 default:
832                     clib_warning ("BUG");
833                 }
834                 pool_put(vam->pending_routes, pr);
835             }
836             vec_reset_length (resolution_failures);
837             break;
838         }
839         if (pool_elts (vam->pending_routes) == 0)
840             timeout = 100.0;
841         vec_reset_length (event_data);
842     }
843     return 0; /* or not */
844 }
845
846 VLIB_REGISTER_NODE (vpe_resolver_process_node,static) = {
847     .function = resolver_process,
848     .type = VLIB_NODE_TYPE_PROCESS,
849     .name = "vpe-route-resolver-process",
850 };
851
852 static int ip4_add_del_route_t_handler (vl_api_ip_add_del_route_t *mp)
853 {
854     ip4_main_t * im = &ip4_main;
855     ip_lookup_main_t * lm = &im->lookup_main;
856     vnet_classify_main_t * cm = &vnet_classify_main;
857     stats_main_t * sm = &stats_main;
858     ip4_add_del_route_args_t a;
859     ip4_address_t next_hop_address;
860     u32 fib_index;
861     vpe_api_main_t * vam = &vpe_api_main;
862     vnet_main_t * vnm = vam->vnet_main;
863     vlib_main_t * vm = vlib_get_main();
864     pending_route_t * pr;
865     vl_api_ip_add_del_route_t * adr;
866     uword * p;
867     clib_error_t * e;
868     u32 ai;
869     ip_adjacency_t *adj;
870
871     p = hash_get (im->fib_index_by_table_id, ntohl(mp->vrf_id));
872     if (!p) {
873         if (mp->create_vrf_if_needed) {
874             ip4_fib_t * f;
875             f = find_ip4_fib_by_table_index_or_id (im, ntohl(mp->vrf_id),
876                                                    0 /* flags */);
877             fib_index = f->index;
878         } else {
879             /* No such VRF, and we weren't asked to create one */
880             return VNET_API_ERROR_NO_SUCH_FIB;
881         }
882     } else {
883         fib_index = p[0];
884     }
885
886     if (~0 != mp->next_hop_sw_if_index &&
887         pool_is_free_index (vnm->interface_main.sw_interfaces,
888                             ntohl(mp->next_hop_sw_if_index)))
889         return VNET_API_ERROR_NO_MATCHING_INTERFACE;
890
891     clib_memcpy (next_hop_address.data, mp->next_hop_address,
892             sizeof (next_hop_address.data));
893
894     /* Arp for the next_hop if necessary */
895     if (mp->is_add && mp->resolve_if_needed && ~0 != mp->next_hop_sw_if_index) {
896         u32 lookup_result;
897         ip_adjacency_t * adj;
898
899         lookup_result = ip4_fib_lookup_with_table
900             (im, fib_index, &next_hop_address, 1 /* disable default route */);
901
902         adj = ip_get_adjacency (lm, lookup_result);
903
904         if (adj->lookup_next_index == IP_LOOKUP_NEXT_ARP) {
905             pool_get (vam->pending_routes, pr);
906             pr->resolve_type = RESOLVE_IP4_ADD_DEL_ROUTE;
907             adr = &pr->r;
908             clib_memcpy (adr, mp, sizeof (*adr));
909             /* recursion block, "just in case" */
910             adr->resolve_if_needed = 0;
911             adr->resolve_attempts = ntohl(mp->resolve_attempts);
912             vnet_register_ip4_arp_resolution_event
913                 (vnm, &next_hop_address, vpe_resolver_process_node.index,
914                  RESOLUTION_EVENT, pr - vam->pending_routes);
915
916             vlib_process_signal_event
917                 (vm, vpe_resolver_process_node.index,
918                  RESOLUTION_PENDING_EVENT, 0 /* data */);
919
920             /* The interface may be down, etc. */
921             e = ip4_probe_neighbor
922                 (vm, (ip4_address_t *)&(mp->next_hop_address),
923                  ntohl(mp->next_hop_sw_if_index));
924
925             if (e)
926                 clib_error_report(e);
927
928             return VNET_API_ERROR_IN_PROGRESS;
929         }
930     }
931
932     if (mp->is_multipath) {
933         u32 flags;
934
935         dslock (sm, 1 /* release hint */, 10 /* tag */);
936
937         if (mp->is_add)
938            flags = IP4_ROUTE_FLAG_ADD;
939         else
940            flags = IP4_ROUTE_FLAG_DEL;
941
942         if (mp->not_last)
943             flags |= IP4_ROUTE_FLAG_NOT_LAST_IN_GROUP;
944
945         ip4_add_del_route_next_hop (im, flags,
946                                     (ip4_address_t *) mp->dst_address,
947                                     (u32) mp->dst_address_length,
948                                     (ip4_address_t *) mp->next_hop_address,
949                                     ntohl(mp->next_hop_sw_if_index),
950                                     (u32) mp->next_hop_weight,
951                                     ~0 /* adj_index */,
952                                     fib_index);
953         dsunlock(sm);
954         return 0;
955     }
956
957     memset (&a, 0, sizeof (a));
958     clib_memcpy (a.dst_address.data, mp->dst_address, sizeof (a.dst_address.data));
959
960     a.dst_address_length = mp->dst_address_length;
961
962     a.flags = (mp->is_add ? IP4_ROUTE_FLAG_ADD : IP4_ROUTE_FLAG_DEL);
963     a.flags |= IP4_ROUTE_FLAG_FIB_INDEX;
964     a.table_index_or_table_id = fib_index;
965     a.add_adj = 0;
966     a.n_add_adj = 0;
967
968     if (mp->not_last)
969         a.flags |= IP4_ROUTE_FLAG_NOT_LAST_IN_GROUP;
970
971     dslock (sm, 1 /* release hint */, 2 /* tag */);
972
973     if (mp->is_add) {
974         if (mp->is_drop)
975             ai = lm->drop_adj_index;
976         else if (mp->is_local)
977             ai = lm->local_adj_index;
978         else if (mp->is_classify) {
979             if (pool_is_free_index (cm->tables, ntohl(mp->classify_table_index))) {
980                 dsunlock(sm);
981                 return VNET_API_ERROR_NO_SUCH_TABLE;
982             }
983             adj = ip_add_adjacency (lm,
984                                     /* template */ 0,
985                                     /* block size */ 1,
986                                     &ai);
987
988             adj->lookup_next_index = IP_LOOKUP_NEXT_CLASSIFY;
989             adj->classify.table_index = ntohl(mp->classify_table_index);
990         }
991         else if (mp->lookup_in_vrf) {
992             p = hash_get (im->fib_index_by_table_id, ntohl(mp->lookup_in_vrf));
993             if (p) {
994                 adj = ip_add_adjacency (lm,
995                                         /* template */ 0,
996                                         /* block size */ 1,
997                                         &ai);
998                 adj->explicit_fib_index = p[0];
999             }
1000             else {
1001                 dsunlock(sm);
1002                 return VNET_API_ERROR_NO_SUCH_INNER_FIB;
1003             }
1004         }
1005         else
1006             ai = ip4_route_get_next_hop_adj (im,
1007                                              fib_index,
1008                                              &next_hop_address,
1009                                              ntohl(mp->next_hop_sw_if_index),
1010                                              fib_index);
1011
1012         if (ai == lm->miss_adj_index) {
1013             dsunlock(sm);
1014             return VNET_API_ERROR_NO_SUCH_INNER_FIB;
1015        }
1016     } else {
1017         ip_adjacency_t * adj;
1018         int disable_default_route = 1;
1019
1020         /* Trying to delete the default route? */
1021         if (a.dst_address.as_u32 == 0 &&
1022             a.dst_address_length == 0)
1023             disable_default_route = 0;
1024
1025         ai = ip4_fib_lookup_with_table
1026             (im, fib_index, &a.dst_address, disable_default_route);
1027         if (ai == lm->miss_adj_index) {
1028             dsunlock(sm);
1029             return VNET_API_ERROR_UNKNOWN_DESTINATION;
1030         }
1031
1032         adj = ip_get_adjacency (lm, ai);
1033         if (adj->lookup_next_index == IP_LOOKUP_NEXT_ARP) {
1034             dsunlock(sm);
1035             return VNET_API_ERROR_ADDRESS_MATCHES_INTERFACE_ADDRESS;
1036         }
1037     }
1038
1039     a.adj_index = ai;
1040     ip4_add_del_route (im, &a);
1041
1042     dsunlock (sm);
1043     return 0;
1044 }
1045
1046 static int ip6_add_del_route_t_handler (vl_api_ip_add_del_route_t *mp)
1047 {
1048     ip6_main_t * im = &ip6_main;
1049     ip_lookup_main_t * lm = &im->lookup_main;
1050     vnet_main_t * vnm = vnet_get_main();
1051     vlib_main_t * vm = vlib_get_main();
1052     vpe_api_main_t * vam = &vpe_api_main;
1053     stats_main_t * sm = &stats_main;
1054     ip6_add_del_route_args_t a;
1055     ip6_address_t next_hop_address;
1056     pending_route_t * pr;
1057     vl_api_ip_add_del_route_t * adr;
1058
1059     u32 fib_index;
1060     uword * p;
1061     clib_error_t * e;
1062     ip_adjacency_t *adj = 0;
1063     u32 ai;
1064
1065     p = hash_get (im->fib_index_by_table_id, ntohl(mp->vrf_id));
1066
1067     if (!p) {
1068         if (mp->create_vrf_if_needed) {
1069             ip6_fib_t * f;
1070             f = find_ip6_fib_by_table_index_or_id (im, ntohl(mp->vrf_id),
1071                                                    0 /* flags */);
1072             fib_index = f->index;
1073         } else {
1074             /* No such VRF, and we weren't asked to create one */
1075             return VNET_API_ERROR_NO_SUCH_FIB;
1076         }
1077     } else {
1078         fib_index = p[0];
1079     }
1080
1081     if (~0 != mp->next_hop_sw_if_index &&
1082         pool_is_free_index (vnm->interface_main.sw_interfaces,
1083                             ntohl(mp->next_hop_sw_if_index)))
1084         return VNET_API_ERROR_NO_MATCHING_INTERFACE;
1085
1086     clib_memcpy (next_hop_address.as_u8, mp->next_hop_address,
1087             sizeof (next_hop_address.as_u8));
1088
1089     /* Arp for the next_hop if necessary */
1090     if (mp->is_add && mp->resolve_if_needed && ~0 != mp->next_hop_sw_if_index) {
1091         u32 lookup_result;
1092         ip_adjacency_t * adj;
1093
1094         lookup_result = ip6_fib_lookup_with_table
1095             (im, fib_index, &next_hop_address);
1096
1097         adj = ip_get_adjacency (lm, lookup_result);
1098
1099         if (adj->lookup_next_index == IP_LOOKUP_NEXT_ARP) {
1100             pool_get (vam->pending_routes, pr);
1101             adr = &pr->r;
1102             pr->resolve_type = RESOLVE_IP6_ADD_DEL_ROUTE;
1103             clib_memcpy (adr, mp, sizeof (*adr));
1104             /* recursion block, "just in case" */
1105             adr->resolve_if_needed = 0;
1106             adr->resolve_attempts = ntohl(mp->resolve_attempts);
1107             vnet_register_ip6_neighbor_resolution_event
1108                 (vnm, &next_hop_address, vpe_resolver_process_node.index,
1109                  RESOLUTION_EVENT, pr - vam->pending_routes);
1110
1111             vlib_process_signal_event
1112                 (vm, vpe_resolver_process_node.index,
1113                  RESOLUTION_PENDING_EVENT, 0 /* data */);
1114
1115             /* The interface may be down, etc. */
1116             e = ip6_probe_neighbor
1117                 (vm, (ip6_address_t *)&(mp->next_hop_address),
1118                  ntohl(mp->next_hop_sw_if_index));
1119
1120             if (e)
1121                 clib_error_report(e);
1122
1123             return VNET_API_ERROR_IN_PROGRESS;
1124         }
1125     }
1126
1127     if (mp->is_multipath) {
1128         u32 flags;
1129
1130         dslock (sm, 1 /* release hint */, 11 /* tag */);
1131
1132         if (mp->is_add)
1133             flags = IP6_ROUTE_FLAG_ADD;
1134         else
1135             flags = IP6_ROUTE_FLAG_DEL;
1136
1137         if (mp->not_last)
1138             flags |= IP6_ROUTE_FLAG_NOT_LAST_IN_GROUP;
1139
1140         ip6_add_del_route_next_hop (im, flags, (ip6_address_t *)mp->dst_address,
1141                                     (u32) mp->dst_address_length,
1142                                     (ip6_address_t *)mp->next_hop_address,
1143                                     ntohl(mp->next_hop_sw_if_index),
1144                                     (u32) mp->next_hop_weight,
1145                                     ~0 /* adj_index */,
1146                                     fib_index);
1147         dsunlock(sm);
1148         return 0;
1149     }
1150
1151     memset (&a, 0, sizeof (a));
1152     clib_memcpy (a.dst_address.as_u8, mp->dst_address, sizeof (a.dst_address.as_u8));
1153
1154     a.dst_address_length = mp->dst_address_length;
1155
1156     a.flags = (mp->is_add ? IP6_ROUTE_FLAG_ADD : IP6_ROUTE_FLAG_DEL);
1157     a.flags |= IP6_ROUTE_FLAG_FIB_INDEX;
1158     a.table_index_or_table_id = fib_index;
1159     a.add_adj = 0;
1160     a.n_add_adj = 0;
1161
1162     if (mp->not_last)
1163         a.flags |= IP6_ROUTE_FLAG_NOT_LAST_IN_GROUP;
1164
1165     dslock (sm, 1 /* release hint */, 3 /* tag */);
1166
1167     if (mp->is_add) {
1168         if (mp->is_drop)
1169             ai = lm->drop_adj_index;
1170         else if (mp->is_local)
1171             ai = lm->local_adj_index;
1172         else if (mp->lookup_in_vrf) {
1173             p = hash_get (im->fib_index_by_table_id, ntohl(mp->lookup_in_vrf));
1174             if (p) {
1175                 adj = ip_add_adjacency (lm,
1176                                         /* template */ 0,
1177                                         /* block size */ 1,
1178                                         &ai);
1179                 adj->explicit_fib_index = p[0];
1180             }
1181             else {
1182                 dsunlock(sm);
1183                 return VNET_API_ERROR_NO_SUCH_INNER_FIB;
1184             }
1185         }
1186        else
1187            ai = ip6_route_get_next_hop_adj (im,
1188                                             fib_index,
1189                                             &next_hop_address,
1190                                             ntohl(mp->next_hop_sw_if_index),
1191                                             fib_index);
1192       if (ai == lm->miss_adj_index) {
1193           dsunlock(sm);
1194           return VNET_API_ERROR_NEXT_HOP_NOT_IN_FIB;
1195       }
1196     } else {
1197         ip_adjacency_t * adj;
1198
1199         ai = ip6_fib_lookup_with_table
1200             (im, fib_index, &a.dst_address);
1201         if (ai == lm->miss_adj_index) {
1202             dsunlock(sm);
1203             return VNET_API_ERROR_UNKNOWN_DESTINATION;
1204         }
1205         adj = ip_get_adjacency (lm, ai);
1206         if (adj->lookup_next_index == IP_LOOKUP_NEXT_ARP) {
1207             dsunlock(sm);
1208             return VNET_API_ERROR_ADDRESS_MATCHES_INTERFACE_ADDRESS;
1209         }
1210     }
1211
1212     a.adj_index = ai;
1213     ip6_add_del_route (im, &a);
1214
1215     dsunlock (sm);
1216     return 0;
1217 }
1218
1219 void vl_api_ip_add_del_route_t_handler (
1220     vl_api_ip_add_del_route_t *mp)
1221 {
1222     vl_api_ip_add_del_route_reply_t * rmp;
1223     int rv;
1224     vnet_main_t * vnm = vnet_get_main();
1225
1226     vnm->api_errno = 0;
1227
1228     if (mp->is_ipv6)
1229         rv = ip6_add_del_route_t_handler (mp);
1230     else
1231         rv = ip4_add_del_route_t_handler (mp);
1232
1233     rv = (rv == 0) ? vnm->api_errno : rv;
1234
1235     REPLY_MACRO(VL_API_IP_ADD_DEL_ROUTE_REPLY);
1236 }
1237
1238 void api_config_default_ip_route (u8 is_ipv6, u8 is_add, u32 vrf_id,
1239                                   u32 sw_if_index, u8 *next_hop_addr)
1240 {
1241     vl_api_ip_add_del_route_t mp;
1242     int rv;
1243
1244     memset (&mp, 0, sizeof(vl_api_ip_add_del_route_t));
1245
1246     /*
1247      * Configure default IP route:
1248      *  - ip route add 0.0.0.0/1 via <GW IP>
1249      *  - ip route add 128.0.0.0/1 via <GW IP>
1250      */
1251     mp.next_hop_sw_if_index = ntohl(sw_if_index);
1252     mp.vrf_id = vrf_id;
1253     mp.resolve_attempts = ~0;
1254     mp.resolve_if_needed = 1;
1255     mp.is_add = is_add;
1256     mp.is_ipv6 = is_ipv6;
1257     mp.next_hop_weight = 1;
1258
1259     clib_memcpy (&mp.next_hop_address[0], next_hop_addr, 16);
1260
1261     if (is_ipv6)
1262         rv = ip6_add_del_route_t_handler (&mp);
1263     else
1264       {
1265         mp.dst_address_length = 1;
1266
1267         mp.dst_address[0] = 0;
1268         rv = ip4_add_del_route_t_handler (&mp);
1269
1270         mp.dst_address[0] = 128;
1271         rv |= ip4_add_del_route_t_handler (&mp);
1272       }
1273
1274     if (rv)
1275         clib_error_return (0, "failed to config default IP route");
1276
1277 }
1278
1279 static void
1280 vl_api_sw_interface_add_del_address_t_handler
1281 (vl_api_sw_interface_add_del_address_t *mp)
1282 {
1283     vlib_main_t *vm = vlib_get_main();
1284     vl_api_sw_interface_add_del_address_reply_t * rmp;
1285     int rv = 0;
1286     u32 is_del;
1287
1288     VALIDATE_SW_IF_INDEX(mp);
1289
1290     is_del = mp->is_add == 0;
1291
1292     if (mp->del_all)
1293         ip_del_all_interface_addresses (vm, ntohl(mp->sw_if_index));
1294     else if (mp->is_ipv6)
1295         ip6_add_del_interface_address (vm, ntohl(mp->sw_if_index),
1296                                        (void *)mp->address,
1297                                        mp->address_length, is_del);
1298     else
1299         ip4_add_del_interface_address (vm, ntohl(mp->sw_if_index),
1300                                        (void *) mp->address,
1301                                        mp->address_length, is_del);
1302
1303     BAD_SW_IF_INDEX_LABEL;
1304
1305     REPLY_MACRO(VL_API_SW_INTERFACE_ADD_DEL_ADDRESS_REPLY);
1306 }
1307
1308 static void
1309 vl_api_sw_interface_set_table_t_handler (vl_api_sw_interface_set_table_t *mp)
1310 {
1311     int rv = 0;
1312     u32 table_id = ntohl(mp->vrf_id);
1313     u32 sw_if_index = ntohl(mp->sw_if_index);
1314     vl_api_sw_interface_set_table_reply_t * rmp;
1315     stats_main_t * sm = &stats_main;
1316
1317     VALIDATE_SW_IF_INDEX(mp);
1318
1319     dslock (sm, 1 /* release hint */, 4 /* tag */);
1320
1321     if (mp->is_ipv6) {
1322         ip6_main_t * im = &ip6_main;
1323         ip6_fib_t * fib =
1324             find_ip6_fib_by_table_index_or_id (im, table_id,
1325                                                IP6_ROUTE_FLAG_TABLE_ID);
1326         if (fib) {
1327             vec_validate (im->fib_index_by_sw_if_index, sw_if_index);
1328             im->fib_index_by_sw_if_index[sw_if_index] = fib->index;
1329         } else {
1330             rv = VNET_API_ERROR_NO_SUCH_FIB;
1331         }
1332     } else {
1333         ip4_main_t * im = &ip4_main;
1334         ip4_fib_t * fib = find_ip4_fib_by_table_index_or_id
1335             (im, table_id, IP4_ROUTE_FLAG_TABLE_ID);
1336
1337         /* Truthfully this can't fail */
1338         if (fib) {
1339             vec_validate (im->fib_index_by_sw_if_index, sw_if_index);
1340             im->fib_index_by_sw_if_index[sw_if_index] = fib->index;
1341         } else {
1342             rv = VNET_API_ERROR_NO_SUCH_FIB;
1343         }
1344     }
1345     dsunlock(sm);
1346
1347     BAD_SW_IF_INDEX_LABEL;
1348
1349     REPLY_MACRO(VL_API_SW_INTERFACE_SET_TABLE_REPLY);
1350 }
1351
1352 static void
1353 vl_api_sw_interface_set_vpath_t_handler (vl_api_sw_interface_set_vpath_t *mp)
1354 {
1355     vlib_main_t *vm = vlib_get_main();
1356     ip4_main_t * im4 = &ip4_main;
1357     ip6_main_t * im6 = &ip6_main;
1358     vl_api_sw_interface_set_vpath_reply_t * rmp;
1359     int rv = 0;
1360     u32 ci;
1361     u32 sw_if_index = ntohl(mp->sw_if_index);
1362     ip4_main_t   *ip4m = &ip4_main;
1363     ip6_main_t   *ip6m = &ip6_main;
1364     ip_lookup_main_t *ip4lm = &ip4m->lookup_main;
1365     ip_lookup_main_t *ip6lm = &ip6m->lookup_main;
1366     ip_config_main_t *rx_cm4u = &ip4lm->rx_config_mains[VNET_UNICAST];
1367     ip_config_main_t *rx_cm4m = &ip4lm->rx_config_mains[VNET_MULTICAST];
1368     ip_config_main_t *rx_cm6u = &ip6lm->rx_config_mains[VNET_UNICAST];
1369     ip_config_main_t *rx_cm6m = &ip6lm->rx_config_mains[VNET_MULTICAST];
1370
1371     VALIDATE_SW_IF_INDEX(mp);
1372
1373     l2input_intf_bitmap_enable(sw_if_index, L2INPUT_FEAT_VPATH, mp->enable);
1374     if (mp->enable) {
1375         ci = rx_cm4u->config_index_by_sw_if_index[sw_if_index]; //IP4 unicast
1376         ci = vnet_config_add_feature(vm, &rx_cm4u->config_main,
1377                                      ci, 
1378                                      im4->ip4_unicast_rx_feature_vpath,
1379                                      0, 0);
1380         rx_cm4u->config_index_by_sw_if_index[sw_if_index] = ci;
1381         ci = rx_cm4m->config_index_by_sw_if_index[sw_if_index]; //IP4 mcast
1382         ci = vnet_config_add_feature(vm, &rx_cm4m->config_main,
1383                                      ci, 
1384                                      im4->ip4_multicast_rx_feature_vpath,
1385                                      0, 0);
1386         rx_cm4m->config_index_by_sw_if_index[sw_if_index] = ci;
1387         ci = rx_cm6u->config_index_by_sw_if_index[sw_if_index]; //IP6 unicast
1388         ci = vnet_config_add_feature(vm, &rx_cm6u->config_main,
1389                                      ci, 
1390                                      im6->ip6_unicast_rx_feature_vpath,
1391                                      0, 0);
1392         rx_cm6u->config_index_by_sw_if_index[sw_if_index] = ci;
1393         ci = rx_cm6m->config_index_by_sw_if_index[sw_if_index]; //IP6 mcast
1394         ci = vnet_config_add_feature(vm, &rx_cm6m->config_main,
1395                                      ci, 
1396                                      im6->ip6_multicast_rx_feature_vpath,
1397                                      0, 0);
1398         rx_cm6m->config_index_by_sw_if_index[sw_if_index] = ci;
1399     } else {
1400         ci = rx_cm4u->config_index_by_sw_if_index[sw_if_index]; //IP4 unicast
1401         ci = vnet_config_del_feature(vm, &rx_cm4u->config_main,
1402                                      ci, 
1403                                      im4->ip4_unicast_rx_feature_vpath,
1404                                      0, 0);
1405         rx_cm4u->config_index_by_sw_if_index[sw_if_index] = ci;
1406         ci = rx_cm4m->config_index_by_sw_if_index[sw_if_index]; //IP4 mcast
1407         ci = vnet_config_del_feature(vm, &rx_cm4m->config_main,
1408                                      ci, 
1409                                      im4->ip4_multicast_rx_feature_vpath,
1410                                      0, 0);
1411         rx_cm4m->config_index_by_sw_if_index[sw_if_index] = ci;
1412         ci = rx_cm6u->config_index_by_sw_if_index[sw_if_index]; //IP6 unicast
1413         ci = vnet_config_del_feature(vm, &rx_cm6u->config_main,
1414                                      ci, 
1415                                      im6->ip6_unicast_rx_feature_vpath,
1416                                      0, 0);
1417         rx_cm6u->config_index_by_sw_if_index[sw_if_index] = ci;
1418         ci = rx_cm6m->config_index_by_sw_if_index[sw_if_index]; //IP6 mcast
1419         ci = vnet_config_del_feature(vm, &rx_cm6m->config_main,
1420                                      ci, 
1421                                      im6->ip6_multicast_rx_feature_vpath,
1422                                      0, 0);
1423         rx_cm6m->config_index_by_sw_if_index[sw_if_index] = ci;
1424     }
1425
1426     BAD_SW_IF_INDEX_LABEL;
1427
1428     REPLY_MACRO(VL_API_SW_INTERFACE_SET_VPATH_REPLY);
1429 }
1430
1431 static void
1432 vl_api_sw_interface_set_l2_xconnect_t_handler (
1433     vl_api_sw_interface_set_l2_xconnect_t *mp)
1434 {
1435     vl_api_sw_interface_set_l2_xconnect_reply_t * rmp;
1436     int rv = 0;
1437     u32 rx_sw_if_index = ntohl(mp->rx_sw_if_index);
1438     u32 tx_sw_if_index = ntohl(mp->tx_sw_if_index);
1439     vlib_main_t *vm  = vlib_get_main();
1440     vnet_main_t *vnm = vnet_get_main();
1441
1442     VALIDATE_RX_SW_IF_INDEX(mp);
1443
1444     if (mp->enable) {
1445         VALIDATE_TX_SW_IF_INDEX(mp);
1446         rv = set_int_l2_mode(vm, vnm, MODE_L2_XC,
1447                              rx_sw_if_index, 0, 0, 0, tx_sw_if_index);
1448     } else {
1449         rv = set_int_l2_mode(vm, vnm, MODE_L3, rx_sw_if_index, 0, 0, 0, 0);
1450     }
1451
1452     BAD_RX_SW_IF_INDEX_LABEL;
1453     BAD_TX_SW_IF_INDEX_LABEL;
1454
1455     REPLY_MACRO(VL_API_SW_INTERFACE_SET_L2_XCONNECT_REPLY);
1456 }
1457
1458 static void
1459 vl_api_sw_interface_set_l2_bridge_t_handler (
1460     vl_api_sw_interface_set_l2_bridge_t *mp)
1461 {
1462     bd_main_t * bdm = &bd_main;
1463     vl_api_sw_interface_set_l2_bridge_reply_t * rmp;
1464     int rv = 0;
1465     u32 rx_sw_if_index = ntohl(mp->rx_sw_if_index);
1466     u32 bd_id = ntohl(mp->bd_id);
1467     u32 bd_index;
1468     u32 bvi = mp->bvi;
1469     u8 shg = mp->shg;
1470     vlib_main_t *vm  = vlib_get_main();
1471     vnet_main_t *vnm = vnet_get_main();
1472
1473     VALIDATE_RX_SW_IF_INDEX(mp);
1474
1475     bd_index = bd_find_or_add_bd_index (bdm, bd_id);
1476
1477     if (mp->enable) {
1478         //VALIDATE_TX_SW_IF_INDEX(mp);
1479         rv = set_int_l2_mode(vm, vnm, MODE_L2_BRIDGE,
1480                              rx_sw_if_index, bd_index, bvi, shg, 0);
1481     } else {
1482         rv = set_int_l2_mode(vm, vnm, MODE_L3, rx_sw_if_index, 0, 0, 0, 0);
1483     }
1484
1485     BAD_RX_SW_IF_INDEX_LABEL;
1486
1487     REPLY_MACRO(VL_API_SW_INTERFACE_SET_L2_BRIDGE_REPLY);
1488 }
1489
1490 static void
1491 vl_api_bridge_domain_add_del_t_handler (
1492     vl_api_bridge_domain_add_del_t *mp)
1493 {
1494     vlib_main_t * vm = vlib_get_main ();
1495     bd_main_t * bdm = &bd_main;
1496     vl_api_bridge_domain_add_del_reply_t * rmp;
1497     int rv = 0;
1498     u32 enable_flags = 0, disable_flags = 0;
1499     u32 bd_id = ntohl(mp->bd_id);
1500     u32 bd_index;
1501
1502     if (mp->is_add) {
1503         bd_index = bd_find_or_add_bd_index (bdm, bd_id);
1504
1505         if (mp->flood)
1506             enable_flags |= L2_FLOOD;
1507         else
1508             disable_flags |= L2_FLOOD;
1509
1510         if (mp->uu_flood)
1511             enable_flags |= L2_UU_FLOOD;
1512         else
1513             disable_flags |= L2_UU_FLOOD;
1514
1515         if (mp->forward)
1516             enable_flags |= L2_FWD;
1517         else
1518             disable_flags |= L2_FWD;
1519
1520         if (mp->arp_term)
1521             enable_flags |= L2_ARP_TERM;
1522         else
1523             disable_flags |= L2_ARP_TERM;
1524
1525         if (mp->learn)
1526             enable_flags |= L2_LEARN;
1527         else
1528             disable_flags |= L2_LEARN;
1529
1530         if (enable_flags)
1531             bd_set_flags (vm, bd_index, enable_flags, 1 /* enable */);
1532
1533         if (disable_flags)
1534             bd_set_flags (vm, bd_index, disable_flags, 0 /* disable */);
1535
1536     } else
1537         rv = bd_delete_bd_index(bdm, bd_id);
1538
1539     REPLY_MACRO(VL_API_BRIDGE_DOMAIN_ADD_DEL_REPLY);
1540 }
1541
1542 static void vl_api_bridge_domain_details_t_handler (
1543     vl_api_bridge_domain_details_t * mp)
1544 {
1545     clib_warning ("BUG");
1546 }
1547
1548 static void vl_api_bridge_domain_sw_if_details_t_handler (
1549     vl_api_bridge_domain_sw_if_details_t * mp)
1550 {
1551     clib_warning ("BUG");
1552 }
1553
1554 static void send_bridge_domain_details (unix_shared_memory_queue_t *q,
1555                                         l2_bridge_domain_t * bd_config,
1556                                         u32 n_sw_ifs,
1557                                         u32 context)
1558 {
1559     vl_api_bridge_domain_details_t * mp;
1560
1561     mp = vl_msg_api_alloc (sizeof (*mp));
1562     memset (mp, 0, sizeof (*mp));
1563     mp->_vl_msg_id = ntohs (VL_API_BRIDGE_DOMAIN_DETAILS);
1564     mp->bd_id = ntohl (bd_config->bd_id);
1565     mp->flood = bd_feature_flood (bd_config);
1566     mp->uu_flood = bd_feature_uu_flood (bd_config);
1567     mp->forward = bd_feature_forward (bd_config);
1568     mp->learn = bd_feature_learn (bd_config);
1569     mp->arp_term = bd_feature_arp_term (bd_config);
1570     mp->bvi_sw_if_index = ntohl (bd_config->bvi_sw_if_index);
1571     mp->n_sw_ifs = ntohl (n_sw_ifs);
1572     mp->context = context;
1573
1574     vl_msg_api_send_shmem (q, (u8 *)&mp);
1575 }
1576
1577 static void send_bd_sw_if_details (l2input_main_t * l2im,
1578                                    unix_shared_memory_queue_t *q,
1579                                    l2_flood_member_t * member, u32 bd_id,
1580                                    u32 context)
1581 {
1582     vl_api_bridge_domain_sw_if_details_t * mp;
1583     l2_input_config_t * input_cfg;
1584
1585     mp = vl_msg_api_alloc (sizeof (*mp));
1586     memset (mp, 0, sizeof (*mp));
1587     mp->_vl_msg_id = ntohs (VL_API_BRIDGE_DOMAIN_SW_IF_DETAILS);
1588     mp->bd_id = ntohl (bd_id);
1589     mp->sw_if_index = ntohl (member->sw_if_index);
1590     input_cfg = vec_elt_at_index (l2im->configs, member->sw_if_index);
1591     mp->shg = input_cfg->shg;
1592     mp->context = context;
1593
1594     vl_msg_api_send_shmem (q, (u8 *)&mp);
1595 }
1596
1597 static void vl_api_bridge_domain_dump_t_handler (
1598     vl_api_bridge_domain_dump_t *mp)
1599 {
1600     bd_main_t * bdm = &bd_main;
1601     l2input_main_t * l2im = &l2input_main;
1602     unix_shared_memory_queue_t * q;
1603     l2_bridge_domain_t * bd_config;
1604     u32 bd_id, bd_index;
1605     u32 end;
1606
1607     q = vl_api_client_index_to_input_queue (mp->client_index);
1608
1609     if (q == 0)
1610         return;
1611
1612     bd_id = ntohl(mp->bd_id);
1613
1614     bd_index = (bd_id == ~0) ? 0 : bd_find_or_add_bd_index (bdm, bd_id);
1615     end = (bd_id == ~0) ? vec_len (l2im->bd_configs) : bd_index + 1;
1616     for (; bd_index < end; bd_index++) {
1617         bd_config = l2input_bd_config_from_index (l2im, bd_index);
1618         /* skip dummy bd_id 0 */
1619         if (bd_config && (bd_config->bd_id > 0)) {
1620             u32 n_sw_ifs;
1621             l2_flood_member_t * m;
1622
1623             n_sw_ifs = vec_len (bd_config->members);
1624             send_bridge_domain_details (q, bd_config, n_sw_ifs, mp->context);
1625
1626             vec_foreach (m, bd_config->members) {
1627                 send_bd_sw_if_details (l2im, q, m, bd_config->bd_id, mp->context);
1628             }
1629         }
1630     }
1631 }
1632
1633 static void
1634 vl_api_l2fib_add_del_t_handler (
1635     vl_api_l2fib_add_del_t *mp)
1636 {
1637     bd_main_t * bdm = &bd_main;
1638     l2input_main_t * l2im = &l2input_main;
1639     vl_api_l2fib_add_del_reply_t * rmp;
1640     int rv = 0;
1641     u64 mac = 0;
1642     u32 sw_if_index = ntohl(mp->sw_if_index);
1643     u32 bd_id = ntohl(mp->bd_id);
1644     u32 bd_index;
1645     u32 static_mac;
1646     u32 filter_mac;
1647     u32 bvi_mac;
1648     uword * p;
1649
1650     mac = mp->mac;
1651
1652     p = hash_get (bdm->bd_index_by_bd_id, bd_id);
1653     if (!p) {
1654         rv = VNET_API_ERROR_NO_SUCH_ENTRY;
1655         goto bad_sw_if_index;
1656     }
1657     bd_index = p[0];
1658
1659     if (mp->is_add) {
1660         VALIDATE_SW_IF_INDEX(mp);
1661         if (vec_len(l2im->configs) <= sw_if_index) {
1662             rv = VNET_API_ERROR_INVALID_SW_IF_INDEX;
1663             goto bad_sw_if_index;
1664         } else {
1665             l2_input_config_t * config;
1666             config = vec_elt_at_index(l2im->configs, sw_if_index);
1667             if (config->bridge == 0) {
1668                 rv = VNET_API_ERROR_INVALID_SW_IF_INDEX;
1669                 goto bad_sw_if_index;
1670             }
1671         }
1672         static_mac = mp->static_mac ? 1 : 0;
1673         filter_mac = mp->filter_mac ? 1 : 0;
1674     bvi_mac = mp->bvi_mac ? 1 : 0;
1675         l2fib_add_entry(mac, bd_index, sw_if_index, static_mac, filter_mac,
1676                         bvi_mac);
1677     } else {
1678         l2fib_del_entry(mac, bd_index);
1679     }
1680
1681     BAD_SW_IF_INDEX_LABEL;
1682
1683     REPLY_MACRO(VL_API_L2FIB_ADD_DEL_REPLY);
1684 }
1685
1686 static void
1687 vl_api_l2_flags_t_handler (
1688     vl_api_l2_flags_t *mp)
1689 {
1690     vl_api_l2_flags_reply_t * rmp;
1691     int rv = 0;
1692     u32 sw_if_index = ntohl(mp->sw_if_index);
1693     u32 flags = ntohl(mp->feature_bitmap);
1694     u32 rbm = 0;
1695
1696     VALIDATE_SW_IF_INDEX(mp);
1697
1698 #define _(a,b) \
1699     if (flags & L2INPUT_FEAT_ ## a) \
1700         rbm = l2input_intf_bitmap_enable (sw_if_index, L2INPUT_FEAT_ ## a, mp->is_set);
1701     foreach_l2input_feat;
1702 #undef _
1703
1704     BAD_SW_IF_INDEX_LABEL;
1705
1706     REPLY_MACRO2(VL_API_L2_FLAGS_REPLY, rmp->resulting_feature_bitmap = ntohl(rbm));
1707 }
1708
1709 static void
1710 vl_api_bridge_flags_t_handler (
1711     vl_api_bridge_flags_t *mp)
1712 {
1713     vlib_main_t *vm  = vlib_get_main();
1714     bd_main_t * bdm = &bd_main;
1715     vl_api_bridge_flags_reply_t * rmp;
1716     int rv = 0;
1717     u32 bd_id = ntohl(mp->bd_id);
1718     u32 bd_index;
1719     u32 flags = ntohl(mp->feature_bitmap);
1720     uword * p;
1721
1722     p = hash_get (bdm->bd_index_by_bd_id, bd_id);
1723     if (p == 0) {
1724         rv = VNET_API_ERROR_NO_SUCH_ENTRY;
1725         goto out;
1726     }
1727
1728     bd_index = p[0];
1729
1730     bd_set_flags(vm, bd_index, flags, mp->is_set);
1731
1732 out:
1733     REPLY_MACRO2(VL_API_BRIDGE_FLAGS_REPLY,
1734                  rmp->resulting_feature_bitmap = ntohl(flags));
1735 }
1736
1737 static void
1738 vl_api_bd_ip_mac_add_del_t_handler (
1739     vl_api_bd_ip_mac_add_del_t *mp)
1740 {
1741     bd_main_t * bdm = &bd_main;
1742     vl_api_bd_ip_mac_add_del_reply_t * rmp;
1743     int rv = 0;
1744     u32 bd_id = ntohl(mp->bd_id);
1745     u32 bd_index;
1746     uword * p;
1747
1748     p = hash_get (bdm->bd_index_by_bd_id, bd_id);
1749     if (p == 0) {
1750         rv = VNET_API_ERROR_NO_SUCH_ENTRY;
1751         goto out;
1752     }
1753
1754     bd_index = p[0];
1755     if (bd_add_del_ip_mac(bd_index,  mp->ip_address,
1756                           mp->mac_address, mp->is_ipv6, mp->is_add))
1757         rv = VNET_API_ERROR_UNSPECIFIED;
1758
1759 out:
1760     REPLY_MACRO(VL_API_BD_IP_MAC_ADD_DEL_REPLY);
1761 }
1762
1763 static void
1764 vl_api_tap_connect_t_handler (vl_api_tap_connect_t *mp, vlib_main_t *vm)
1765 {
1766     int rv;
1767     vl_api_tap_connect_reply_t * rmp;
1768     unix_shared_memory_queue_t * q;
1769     u32 sw_if_index = (u32)~0;
1770
1771     rv = vnet_tap_connect_renumber (vm, mp->tap_name,
1772                            mp->use_random_mac ? 0 : mp->mac_address,
1773                            &sw_if_index, mp->renumber,
1774                            ntohl(mp->custom_dev_instance));
1775
1776     q = vl_api_client_index_to_input_queue (mp->client_index);
1777     if (!q)
1778         return;
1779
1780     rmp = vl_msg_api_alloc (sizeof (*rmp));
1781     rmp->_vl_msg_id = ntohs(VL_API_TAP_CONNECT_REPLY);
1782     rmp->context = mp->context;
1783     rmp->retval = ntohl(rv);
1784     rmp->sw_if_index = ntohl(sw_if_index);
1785
1786     vl_msg_api_send_shmem (q, (u8 *)&rmp);
1787 }
1788
1789 static void
1790 vl_api_tap_modify_t_handler (vl_api_tap_modify_t *mp, vlib_main_t *vm)
1791 {
1792     int rv;
1793     vl_api_tap_modify_reply_t * rmp;
1794     unix_shared_memory_queue_t * q;
1795     u32 sw_if_index = (u32)~0;
1796
1797     rv = vnet_tap_modify (vm, ntohl(mp->sw_if_index), mp->tap_name,
1798                            mp->use_random_mac ? 0 : mp->mac_address,
1799                            &sw_if_index, mp->renumber,
1800                            ntohl(mp->custom_dev_instance));
1801
1802     q = vl_api_client_index_to_input_queue (mp->client_index);
1803     if (!q)
1804         return;
1805
1806     rmp = vl_msg_api_alloc (sizeof (*rmp));
1807     rmp->_vl_msg_id = ntohs(VL_API_TAP_MODIFY_REPLY);
1808     rmp->context = mp->context;
1809     rmp->retval = ntohl(rv);
1810     rmp->sw_if_index = ntohl(sw_if_index);
1811
1812     vl_msg_api_send_shmem (q, (u8 *)&rmp);
1813 }
1814
1815 static void
1816 vl_api_tap_delete_t_handler (vl_api_tap_delete_t *mp, vlib_main_t *vm)
1817 {
1818     int rv;
1819     vpe_api_main_t * vam = &vpe_api_main;
1820     vl_api_tap_delete_reply_t * rmp;
1821     unix_shared_memory_queue_t * q;
1822     u32 sw_if_index = ntohl(mp->sw_if_index);
1823
1824     rv = vnet_tap_delete (vm, sw_if_index);
1825
1826     q = vl_api_client_index_to_input_queue (mp->client_index);
1827     if (!q)
1828         return;
1829
1830     rmp = vl_msg_api_alloc (sizeof (*rmp));
1831     rmp->_vl_msg_id = ntohs(VL_API_TAP_DELETE_REPLY);
1832     rmp->context = mp->context;
1833     rmp->retval = ntohl(rv);
1834
1835     vl_msg_api_send_shmem (q, (u8 *)&rmp);
1836
1837     if (!rv)
1838         send_sw_interface_flags_deleted (vam, q, sw_if_index);
1839 }
1840
1841 static void
1842 vl_api_create_vlan_subif_t_handler (vl_api_create_vlan_subif_t * mp)
1843 {
1844     vl_api_create_vlan_subif_reply_t * rmp;
1845     vnet_main_t * vnm = vnet_get_main();
1846     u32 hw_if_index, sw_if_index = (u32)~0;
1847     vnet_hw_interface_t * hi;
1848     int rv = 0;
1849     u32 id;
1850     vnet_sw_interface_t template;
1851     uword * p;
1852     vnet_interface_main_t * im = &vnm->interface_main;
1853     u64 sup_and_sub_key;
1854     u64 * kp;
1855     unix_shared_memory_queue_t * q;
1856     clib_error_t * error;
1857
1858     VALIDATE_SW_IF_INDEX(mp);
1859
1860     hw_if_index = ntohl(mp->sw_if_index);
1861     hi = vnet_get_hw_interface (vnm, hw_if_index);
1862
1863     id = ntohl(mp->vlan_id);
1864     if (id == 0 || id > 4095) {
1865         rv = VNET_API_ERROR_INVALID_VLAN;
1866         goto out;
1867     }
1868
1869     sup_and_sub_key = ((u64)(hi->sw_if_index) << 32) | (u64) id;
1870
1871     p = hash_get_mem (im->sw_if_index_by_sup_and_sub, &sup_and_sub_key);
1872     if (p) {
1873         rv = VNET_API_ERROR_VLAN_ALREADY_EXISTS;
1874         goto out;
1875     }
1876
1877     kp = clib_mem_alloc (sizeof (*kp));
1878     *kp = sup_and_sub_key;
1879
1880     memset (&template, 0, sizeof (template));
1881     template.type = VNET_SW_INTERFACE_TYPE_SUB;
1882     template.sup_sw_if_index = hi->sw_if_index;
1883     template.sub.id = id;
1884     template.sub.eth.raw_flags = 0;
1885     template.sub.eth.flags.one_tag = 1;
1886     template.sub.eth.outer_vlan_id = id;
1887     template.sub.eth.flags.exact_match = 1;
1888
1889     error = vnet_create_sw_interface (vnm, &template, &sw_if_index);
1890     if (error) {
1891         clib_error_report(error);
1892         rv = VNET_API_ERROR_INVALID_REGISTRATION;
1893         goto out;
1894     }
1895     hash_set (hi->sub_interface_sw_if_index_by_id, id, sw_if_index);
1896     hash_set_mem (im->sw_if_index_by_sup_and_sub, kp, sw_if_index);
1897
1898     BAD_SW_IF_INDEX_LABEL;
1899
1900 out:
1901     q = vl_api_client_index_to_input_queue (mp->client_index);
1902     if (!q)
1903         return;
1904
1905     rmp = vl_msg_api_alloc (sizeof (*rmp));
1906     rmp->_vl_msg_id = ntohs(VL_API_CREATE_VLAN_SUBIF_REPLY);
1907     rmp->context = mp->context;
1908     rmp->retval = ntohl(rv);
1909     rmp->sw_if_index = ntohl(sw_if_index);
1910     vl_msg_api_send_shmem (q, (u8 *)&rmp);
1911 }
1912
1913 static void
1914 vl_api_create_subif_t_handler (vl_api_create_subif_t * mp)
1915 {
1916     vl_api_create_subif_reply_t * rmp;
1917     vnet_main_t * vnm = vnet_get_main();
1918     u32 sw_if_index = ~0;
1919     int rv = 0;
1920     u32 sub_id;
1921     vnet_sw_interface_t *si;
1922     vnet_hw_interface_t *hi;
1923     vnet_sw_interface_t template;
1924     uword * p;
1925     vnet_interface_main_t * im = &vnm->interface_main;
1926     u64 sup_and_sub_key;
1927     u64 * kp;
1928     clib_error_t * error;
1929
1930     VALIDATE_SW_IF_INDEX(mp);
1931
1932     si = vnet_get_sup_sw_interface (vnm, ntohl(mp->sw_if_index));
1933     hi = vnet_get_sup_hw_interface (vnm, ntohl(mp->sw_if_index));
1934
1935     if (hi->bond_info == VNET_HW_INTERFACE_BOND_INFO_SLAVE) {
1936          rv = VNET_API_ERROR_BOND_SLAVE_NOT_ALLOWED;
1937         goto out;
1938     }
1939
1940     sw_if_index = si->sw_if_index;
1941     sub_id = ntohl(mp->sub_id);
1942
1943     sup_and_sub_key = ((u64)(sw_if_index) << 32) | (u64) sub_id;
1944
1945     p = hash_get_mem (im->sw_if_index_by_sup_and_sub, &sup_and_sub_key);
1946     if (p) {
1947         if (CLIB_DEBUG > 0)
1948             clib_warning ("sup sw_if_index %d, sub id %d already exists\n",
1949                           sw_if_index, sub_id);
1950         rv = VNET_API_ERROR_SUBIF_ALREADY_EXISTS;
1951         goto out;
1952     }
1953
1954     kp = clib_mem_alloc (sizeof (*kp));
1955     *kp = sup_and_sub_key;
1956
1957     memset (&template, 0, sizeof (template));
1958     template.type = VNET_SW_INTERFACE_TYPE_SUB;
1959     template.sup_sw_if_index = sw_if_index;
1960     template.sub.id = sub_id;
1961     template.sub.eth.flags.no_tags = mp->no_tags;
1962     template.sub.eth.flags.one_tag = mp->one_tag;
1963     template.sub.eth.flags.two_tags = mp->two_tags;
1964     template.sub.eth.flags.dot1ad = mp->dot1ad;
1965     template.sub.eth.flags.exact_match = mp->exact_match;
1966     template.sub.eth.flags.default_sub = mp->default_sub;
1967     template.sub.eth.flags.outer_vlan_id_any = mp->outer_vlan_id_any;
1968     template.sub.eth.flags.inner_vlan_id_any = mp->inner_vlan_id_any;
1969     template.sub.eth.outer_vlan_id = ntohs(mp->outer_vlan_id);
1970     template.sub.eth.inner_vlan_id = ntohs(mp->inner_vlan_id);
1971
1972     error = vnet_create_sw_interface (vnm, &template, &sw_if_index);
1973     if (error) {
1974         clib_error_report (error);
1975         rv = VNET_API_ERROR_SUBIF_CREATE_FAILED;
1976         goto out;
1977     }
1978
1979     hash_set (hi->sub_interface_sw_if_index_by_id, sub_id, sw_if_index);
1980     hash_set_mem (im->sw_if_index_by_sup_and_sub, kp, sw_if_index);
1981
1982     BAD_SW_IF_INDEX_LABEL;
1983
1984 out:
1985
1986     REPLY_MACRO2(VL_API_CREATE_SUBIF_REPLY,
1987     ({
1988         rmp->sw_if_index = ntohl(sw_if_index);
1989     }));
1990 }
1991
1992 static void
1993 vl_api_mpls_gre_add_del_tunnel_t_handler (vl_api_mpls_gre_add_del_tunnel_t *mp)
1994 {
1995     vl_api_mpls_gre_add_del_tunnel_reply_t * rmp;
1996     int rv = 0;
1997     stats_main_t * sm = &stats_main;
1998     u32 tunnel_sw_if_index = ~0;
1999
2000     dslock (sm, 1 /* release hint */, 5 /* tag */);
2001
2002     rv = vnet_mpls_gre_add_del_tunnel ((ip4_address_t *)(mp->src_address),
2003                                        (ip4_address_t *)(mp->dst_address),
2004                                        (ip4_address_t *)(mp->intfc_address),
2005                                        (u32)(mp->intfc_address_length),
2006                                        ntohl(mp->inner_vrf_id),
2007                                        ntohl(mp->outer_vrf_id),
2008                                        &tunnel_sw_if_index,
2009                                        mp->l2_only,
2010                                        mp->is_add);
2011     dsunlock (sm);
2012
2013     REPLY_MACRO2(VL_API_MPLS_GRE_ADD_DEL_TUNNEL_REPLY,
2014     ({
2015         rmp->tunnel_sw_if_index = ntohl(tunnel_sw_if_index);
2016     }));
2017 }
2018
2019 static void
2020 vl_api_mpls_ethernet_add_del_tunnel_t_handler
2021 (vl_api_mpls_ethernet_add_del_tunnel_t *mp)
2022 {
2023     vl_api_mpls_ethernet_add_del_tunnel_reply_t * rmp;
2024     int rv = 0;
2025     stats_main_t * sm = &stats_main;
2026     u32 tunnel_sw_if_index;
2027
2028     dslock (sm, 1 /* release hint */, 5 /* tag */);
2029
2030     rv = vnet_mpls_ethernet_add_del_tunnel
2031         (mp->dst_mac_address, (ip4_address_t *)(mp->adj_address),
2032          (u32)(mp->adj_address_length), ntohl(mp->vrf_id),
2033          ntohl(mp->tx_sw_if_index),
2034          &tunnel_sw_if_index,
2035          mp->l2_only,
2036          mp->is_add);
2037
2038     dsunlock (sm);
2039
2040     REPLY_MACRO2(VL_API_MPLS_ETHERNET_ADD_DEL_TUNNEL_REPLY,
2041     ({
2042         rmp->tunnel_sw_if_index = ntohl(tunnel_sw_if_index);
2043     }));
2044 }
2045
2046 /*
2047  * This piece of misery brought to you because the control-plane
2048  * can't figure out the tx interface + dst-mac address all by itself
2049  */
2050 static int mpls_ethernet_add_del_tunnel_2_t_handler
2051 (vl_api_mpls_ethernet_add_del_tunnel_2_t *mp)
2052 {
2053     pending_route_t * pr;
2054     vl_api_mpls_ethernet_add_del_tunnel_2_t *pme;
2055     vnet_main_t * vnm = vnet_get_main();
2056     vlib_main_t * vm = vlib_get_main();
2057     stats_main_t * sm = &stats_main;
2058     vpe_api_main_t * vam = &vpe_api_main;
2059     u32 inner_fib_index, outer_fib_index;
2060     ip4_main_t * im = &ip4_main;
2061     ip_lookup_main_t * lm = &im->lookup_main;
2062     ip_adjacency_t * adj = 0;
2063     u32 lookup_result;
2064     u32 tx_sw_if_index;
2065     u8 * dst_mac_address;
2066     clib_error_t * e;
2067     uword * p;
2068     int rv;
2069     u32 tunnel_sw_if_index;
2070
2071     p = hash_get (im->fib_index_by_table_id, ntohl(mp->outer_vrf_id));
2072     if (!p)
2073         return VNET_API_ERROR_NO_SUCH_FIB;
2074     else
2075         outer_fib_index = p[0];
2076
2077
2078     p = hash_get (im->fib_index_by_table_id, ntohl(mp->inner_vrf_id));
2079     if (!p)
2080         return VNET_API_ERROR_NO_SUCH_INNER_FIB;
2081     else
2082         inner_fib_index = p[0];
2083
2084     if (inner_fib_index == outer_fib_index)
2085         return VNET_API_ERROR_INVALID_VALUE;
2086
2087     lookup_result = ip4_fib_lookup_with_table
2088         (im, outer_fib_index,
2089          (ip4_address_t *)mp->next_hop_ip4_address_in_outer_vrf,
2090          1 /* disable default route */);
2091
2092     adj = ip_get_adjacency (lm, lookup_result);
2093     tx_sw_if_index = adj->rewrite_header.sw_if_index;
2094
2095     if (mp->is_add && mp->resolve_if_needed) {
2096         if (adj->lookup_next_index == IP_LOOKUP_NEXT_ARP) {
2097             pool_get (vam->pending_routes, pr);
2098             pr->resolve_type = RESOLVE_MPLS_ETHERNET_ADD_DEL;
2099             pme = &pr->t;
2100             clib_memcpy (pme, mp, sizeof (*pme));
2101             /* recursion block, "just in case" */
2102             pme->resolve_if_needed = 0;
2103             pme->resolve_attempts = ntohl(mp->resolve_attempts);
2104             pme->resolve_opaque = tx_sw_if_index;
2105             vnet_register_ip4_arp_resolution_event
2106                 (vnm,
2107                  (ip4_address_t *)&(pme->next_hop_ip4_address_in_outer_vrf),
2108                  vpe_resolver_process_node.index,
2109                  RESOLUTION_EVENT, pr - vam->pending_routes);
2110
2111             vlib_process_signal_event
2112                 (vm, vpe_resolver_process_node.index,
2113                  RESOLUTION_PENDING_EVENT, 0 /* data */);
2114
2115             /* The interface may be down, etc. */
2116             e = ip4_probe_neighbor
2117                 (vm, (ip4_address_t *)&(mp->next_hop_ip4_address_in_outer_vrf),
2118                  tx_sw_if_index);
2119
2120             if (e)
2121                 clib_error_report(e);
2122
2123             return VNET_API_ERROR_IN_PROGRESS;
2124         }
2125     }
2126
2127     if (adj->lookup_next_index != IP_LOOKUP_NEXT_REWRITE)
2128         return VNET_API_ERROR_NEXT_HOP_NOT_IN_FIB;
2129
2130     dst_mac_address =
2131         vnet_rewrite_get_data_internal
2132         (&adj->rewrite_header, sizeof (adj->rewrite_data));
2133
2134     dslock (sm, 1 /* release hint */, 10 /* tag */);
2135
2136     rv = vnet_mpls_ethernet_add_del_tunnel
2137         (dst_mac_address, (ip4_address_t *)(mp->adj_address),
2138          (u32)(mp->adj_address_length), ntohl(mp->inner_vrf_id),
2139          tx_sw_if_index, &tunnel_sw_if_index, mp->l2_only, mp->is_add);
2140
2141     dsunlock (sm);
2142
2143     return rv;
2144 }
2145
2146 static void
2147 vl_api_mpls_ethernet_add_del_tunnel_2_t_handler
2148 (vl_api_mpls_ethernet_add_del_tunnel_2_t *mp)
2149 {
2150     vl_api_mpls_ethernet_add_del_tunnel_reply_t * rmp;
2151     int rv = 0;
2152
2153     rv = mpls_ethernet_add_del_tunnel_2_t_handler (mp);
2154
2155     REPLY_MACRO(VL_API_MPLS_ETHERNET_ADD_DEL_TUNNEL_2_REPLY);
2156 }
2157
2158
2159 static void
2160 vl_api_mpls_add_del_encap_t_handler (vl_api_mpls_add_del_encap_t *mp)
2161 {
2162     vl_api_mpls_add_del_encap_reply_t * rmp;
2163     int rv;
2164     static u32 * labels;
2165     int i;
2166
2167     vec_reset_length (labels);
2168
2169     for (i = 0; i < mp->nlabels; i++)
2170         vec_add1 (labels, ntohl(mp->labels[i]));
2171
2172     /* $$$$ fixme */
2173     rv = vnet_mpls_add_del_encap ((ip4_address_t *)mp->dst_address,
2174                                   ntohl(mp->vrf_id), labels,
2175                                   ~0 /* policy_tunnel_index */,
2176                                   0 /* no_dst_hash */,
2177                                   0 /* indexp */,
2178                                   mp->is_add);
2179
2180     REPLY_MACRO(VL_API_MPLS_ADD_DEL_ENCAP_REPLY);
2181 }
2182
2183 static void
2184 vl_api_mpls_add_del_decap_t_handler
2185 (vl_api_mpls_add_del_decap_t *mp)
2186 {
2187     vl_api_mpls_add_del_decap_reply_t * rmp;
2188     int rv;
2189
2190     rv = vnet_mpls_add_del_decap (ntohl(mp->rx_vrf_id), ntohl(mp->tx_vrf_id),
2191                                   ntohl(mp->label), ntohl(mp->next_index),
2192                                   mp->s_bit, mp->is_add);
2193
2194     REPLY_MACRO(VL_API_MPLS_ADD_DEL_DECAP_REPLY);
2195 }
2196
2197 static void
2198 vl_api_proxy_arp_add_del_t_handler (vl_api_proxy_arp_add_del_t *mp)
2199 {
2200     vl_api_proxy_arp_add_del_reply_t * rmp;
2201     u32 fib_index;
2202     int rv;
2203     ip4_main_t * im = &ip4_main;
2204     stats_main_t * sm = &stats_main;
2205     int vnet_proxy_arp_add_del (ip4_address_t *lo_addr,
2206                                 ip4_address_t *hi_addr,
2207                                 u32 fib_index, int is_del);
2208     uword * p;
2209
2210     dslock (sm, 1 /* release hint */, 6 /* tag */);
2211
2212     p = hash_get (im->fib_index_by_table_id, ntohl(mp->vrf_id));
2213
2214     if (! p) {
2215         rv = VNET_API_ERROR_NO_SUCH_FIB;
2216         goto out;
2217     }
2218
2219     fib_index = p[0];
2220
2221     rv = vnet_proxy_arp_add_del ((ip4_address_t *)mp->low_address,
2222                                  (ip4_address_t *)mp->hi_address,
2223                                  fib_index, mp->is_add == 0);
2224
2225 out:
2226     dsunlock (sm);
2227     REPLY_MACRO(VL_API_PROXY_ARP_ADD_DEL_REPLY);
2228 }
2229
2230 static void
2231 vl_api_proxy_arp_intfc_enable_disable_t_handler
2232 (vl_api_proxy_arp_intfc_enable_disable_t *mp)
2233 {
2234     int rv = 0;
2235     vnet_main_t * vnm = vnet_get_main();
2236     vl_api_proxy_arp_intfc_enable_disable_reply_t *rmp;
2237     vnet_sw_interface_t * si;
2238     u32 sw_if_index;
2239
2240     VALIDATE_SW_IF_INDEX(mp);
2241
2242     sw_if_index = ntohl(mp->sw_if_index);
2243
2244     if (pool_is_free_index (vnm->interface_main.sw_interfaces,
2245                             sw_if_index)) {
2246         rv = VNET_API_ERROR_INVALID_SW_IF_INDEX;
2247         goto out;
2248     }
2249
2250     si = vnet_get_sw_interface (vnm, sw_if_index);
2251
2252     ASSERT(si);
2253
2254     if (mp->enable_disable)
2255         si->flags |= VNET_SW_INTERFACE_FLAG_PROXY_ARP;
2256     else
2257         si->flags &= ~VNET_SW_INTERFACE_FLAG_PROXY_ARP;
2258
2259     BAD_SW_IF_INDEX_LABEL;
2260
2261     out:
2262     REPLY_MACRO(VL_API_PROXY_ARP_INTFC_ENABLE_DISABLE_REPLY);
2263 }
2264
2265 static void
2266 vl_api_ip_neighbor_add_del_t_handler (vl_api_ip_neighbor_add_del_t *mp, vlib_main_t * vm)
2267 {
2268     vl_api_ip_neighbor_add_del_reply_t * rmp;
2269     vnet_main_t * vnm = vnet_get_main();
2270     u32 fib_index;
2271     int rv=0;
2272     stats_main_t * sm = &stats_main;
2273
2274     VALIDATE_SW_IF_INDEX(mp);
2275
2276     dslock (sm, 1 /* release hint */, 7 /* tag */);
2277
2278     if (mp->is_ipv6) {
2279         if (mp->is_add)
2280             rv = vnet_set_ip6_ethernet_neighbor
2281                 (vm, ntohl(mp->sw_if_index),
2282                  (ip6_address_t *)(mp->dst_address),
2283                  mp->mac_address, sizeof (mp->mac_address), mp->is_static);
2284         else
2285             rv = vnet_unset_ip6_ethernet_neighbor
2286                 (vm, ntohl(mp->sw_if_index),
2287                  (ip6_address_t *)(mp->dst_address),
2288                  mp->mac_address, sizeof(mp->mac_address));
2289     } else {
2290         ip4_main_t * im = &ip4_main;
2291         ip_lookup_main_t * lm = &im->lookup_main;
2292         ethernet_arp_ip4_over_ethernet_address_t a;
2293         u32 ai;
2294         ip_adjacency_t *nh_adj;
2295
2296         uword * p = hash_get (im->fib_index_by_table_id, ntohl(mp->vrf_id));
2297         if (! p) {
2298             rv = VNET_API_ERROR_NO_SUCH_FIB;
2299             goto out;
2300         }
2301         fib_index = p[0];
2302
2303         /*
2304          * Unfortunately, folks have a penchant for
2305          * adding interface addresses to the ARP cache, and
2306          * wondering why the forwarder eventually ASSERTs...
2307          */
2308         ai = ip4_fib_lookup_with_table
2309             (im, fib_index, (ip4_address_t *)(mp->dst_address),
2310              1 /* disable default route */);
2311
2312         if (ai != 0) {
2313             nh_adj = ip_get_adjacency (lm, ai);
2314             /* Never allow manipulation of a local adj! */
2315             if (nh_adj->lookup_next_index == IP_LOOKUP_NEXT_LOCAL) {
2316                 clib_warning("%U matches local adj",
2317                              format_ip4_address,
2318                              (ip4_address_t *)(mp->dst_address));
2319                 rv = VNET_API_ERROR_ADDRESS_MATCHES_INTERFACE_ADDRESS;
2320                 goto out;
2321             }
2322         }
2323
2324         clib_memcpy (&a.ethernet, mp->mac_address, 6);
2325         clib_memcpy (&a.ip4, mp->dst_address, 4);
2326
2327         if (mp->is_add)
2328             rv = vnet_arp_set_ip4_over_ethernet (vnm, ntohl(mp->sw_if_index),
2329                                                  fib_index, &a, mp->is_static);
2330         else
2331             rv = vnet_arp_unset_ip4_over_ethernet (vnm, ntohl(mp->sw_if_index),
2332                                                    fib_index, &a);
2333     }
2334
2335     BAD_SW_IF_INDEX_LABEL;
2336     out:
2337     dsunlock (sm);
2338     REPLY_MACRO(VL_API_IP_NEIGHBOR_ADD_DEL_REPLY);
2339 }
2340
2341 static void
2342 vl_api_is_address_reachable_t_handler (vl_api_is_address_reachable_t *mp)
2343 {
2344 #if 0
2345     vpe_main_t *rm = &vpe_main;
2346     ip4_main_t *im4 = &ip4_main;
2347     ip6_main_t *im6 = &ip6_main;
2348     ip_lookup_main_t * lm;
2349     union {
2350         ip4_address_t ip4;
2351         ip6_address_t ip6;
2352     } addr;
2353     u32 adj_index, sw_if_index;
2354     vl_api_is_address_reachable_t *rmp;
2355     ip_adjacency_t * adj;
2356     unix_shared_memory_queue_t *q;
2357
2358     q = vl_api_client_index_to_input_queue (mp->client_index);
2359     if (!q) {
2360         increment_missing_api_client_counter (rm->vlib_main);
2361         return;
2362     }
2363
2364     rmp = vl_msg_api_alloc (sizeof (*rmp));
2365     clib_memcpy (rmp, mp, sizeof (*rmp));
2366
2367     sw_if_index = mp->next_hop_sw_if_index;
2368     clib_memcpy (&addr, mp->address, sizeof (addr));
2369     if (mp->is_ipv6) {
2370         lm = &im6->lookup_main;
2371         adj_index =
2372             ip6_fib_lookup (im6, sw_if_index, &addr.ip6);
2373     } else {
2374         lm = &im4->lookup_main;
2375         adj_index =
2376             ip4_fib_lookup (im4, sw_if_index, &addr.ip4);
2377     }
2378     if (adj_index == ~0) {
2379         rmp->is_error = 1;
2380         goto send;
2381     }
2382     adj = ip_get_adjacency (lm, adj_index);
2383
2384     if (adj->lookup_next_index == IP_LOOKUP_NEXT_REWRITE
2385         && adj->rewrite_header.sw_if_index == sw_if_index) {
2386         rmp->is_known = 1;
2387     } else {
2388         if (adj->lookup_next_index == IP_LOOKUP_NEXT_ARP
2389             && adj->rewrite_header.sw_if_index == sw_if_index) {
2390             if (mp->is_ipv6)
2391                 ip6_probe_neighbor (rm->vlib_main, &addr.ip6, sw_if_index);
2392             else
2393                 ip4_probe_neighbor (rm->vlib_main, &addr.ip4, sw_if_index);
2394         } else if (adj->lookup_next_index == IP_LOOKUP_NEXT_DROP) {
2395             rmp->is_known = 1;
2396             goto send;
2397         }
2398         rmp->is_known = 0;
2399     }
2400
2401 send:
2402     vl_msg_api_send_shmem (q, (u8 *)&rmp);
2403 #endif
2404 }
2405
2406 static void vl_api_sw_interface_details_t_handler (
2407     vl_api_sw_interface_details_t * mp)
2408 {
2409     clib_warning ("BUG");
2410 }
2411
2412 static void vl_api_sw_interface_set_flags_t_handler (
2413     vl_api_sw_interface_set_flags_t * mp)
2414 {
2415    vl_api_sw_interface_set_flags_reply_t *rmp;
2416    vnet_main_t * vnm = vnet_get_main();
2417    int rv = 0;
2418    clib_error_t * error;
2419    u16 flags;
2420
2421    VALIDATE_SW_IF_INDEX(mp);
2422
2423    flags = mp->admin_up_down ? VNET_SW_INTERFACE_FLAG_ADMIN_UP : 0;
2424
2425    error = vnet_sw_interface_set_flags (vnm,
2426                                         ntohl(mp->sw_if_index),
2427                                         flags);
2428    if (error) {
2429        rv = -1;
2430        clib_error_report (error);
2431    }
2432
2433    BAD_SW_IF_INDEX_LABEL;
2434    REPLY_MACRO(VL_API_SW_INTERFACE_SET_FLAGS_REPLY);
2435 }
2436
2437 static void vl_api_sw_interface_clear_stats_t_handler (
2438     vl_api_sw_interface_clear_stats_t * mp)
2439 {
2440    vl_api_sw_interface_clear_stats_reply_t *rmp;
2441
2442    vnet_main_t * vnm = vnet_get_main();
2443    vnet_interface_main_t * im = &vnm->interface_main;
2444    vlib_simple_counter_main_t * sm;
2445    vlib_combined_counter_main_t * cm;
2446    static vnet_main_t ** my_vnet_mains;
2447    int i, j, n_counters;
2448
2449    int rv = 0;
2450
2451    vec_reset_length (my_vnet_mains);
2452
2453    for (i = 0; i < vec_len (vnet_mains); i++)
2454      {
2455        if (vnet_mains[i])
2456          vec_add1 (my_vnet_mains, vnet_mains[i]);
2457      }
2458
2459    if (vec_len (vnet_mains) == 0)
2460      vec_add1 (my_vnet_mains, vnm);
2461
2462    n_counters = vec_len (im->combined_sw_if_counters);
2463
2464    for (j = 0; j < n_counters; j++)
2465      {
2466        for (i = 0; i < vec_len(my_vnet_mains); i++)
2467          {
2468            im = &my_vnet_mains[i]->interface_main;
2469            cm = im->combined_sw_if_counters + j;
2470            if (mp->sw_if_index == (u32)~0)
2471              vlib_clear_combined_counters (cm);
2472            else
2473              vlib_zero_combined_counter (cm, ntohl(mp->sw_if_index));
2474          }
2475      }
2476
2477    n_counters = vec_len (im->sw_if_counters);
2478
2479    for (j = 0; j < n_counters; j++)
2480      {
2481        for (i = 0; i < vec_len(my_vnet_mains); i++)
2482          {
2483            im = &my_vnet_mains[i]->interface_main;
2484            sm = im->sw_if_counters + j;
2485            if (mp->sw_if_index == (u32)~0)
2486              vlib_clear_simple_counters (sm);
2487            else
2488              vlib_zero_simple_counter (sm, ntohl(mp->sw_if_index));
2489          }
2490      }
2491
2492    REPLY_MACRO(VL_API_SW_INTERFACE_CLEAR_STATS_REPLY);
2493 }
2494
2495 static void send_sw_interface_details (vpe_api_main_t * am,
2496                                        unix_shared_memory_queue_t *q,
2497                                        vnet_sw_interface_t * swif,
2498                                        u8 * interface_name,
2499                                        u32 context)
2500 {
2501     vl_api_sw_interface_details_t * mp;
2502     vnet_hw_interface_t * hi;
2503
2504     hi = vnet_get_sup_hw_interface (am->vnet_main, swif->sw_if_index);
2505
2506     mp = vl_msg_api_alloc (sizeof (*mp));
2507     memset (mp, 0, sizeof (*mp));
2508     mp->_vl_msg_id = ntohs(VL_API_SW_INTERFACE_DETAILS);
2509     mp->sw_if_index = ntohl(swif->sw_if_index);
2510     mp->sup_sw_if_index = ntohl(swif->sup_sw_if_index);
2511     mp->admin_up_down = (swif->flags & VNET_SW_INTERFACE_FLAG_ADMIN_UP) ?
2512         1 : 0;
2513     mp->link_up_down = (hi->flags & VNET_HW_INTERFACE_FLAG_LINK_UP) ?
2514         1 : 0;
2515     mp->link_duplex = ((hi->flags & VNET_HW_INTERFACE_FLAG_DUPLEX_MASK) >>
2516                        VNET_HW_INTERFACE_FLAG_DUPLEX_SHIFT);
2517     mp->link_speed = ((hi->flags & VNET_HW_INTERFACE_FLAG_SPEED_MASK) >>
2518                       VNET_HW_INTERFACE_FLAG_SPEED_SHIFT);
2519     mp->link_mtu = ntohs(hi->max_packet_bytes);
2520     mp->context = context;
2521
2522     strncpy ((char *) mp->interface_name,
2523              (char *) interface_name, ARRAY_LEN(mp->interface_name)-1);
2524
2525     /* Send the L2 address for ethernet physical intfcs */
2526     if (swif->sup_sw_if_index == swif->sw_if_index
2527         && hi->hw_class_index == ethernet_hw_interface_class.index) {
2528         ethernet_main_t *em = ethernet_get_main (am->vlib_main);
2529         ethernet_interface_t *ei;
2530
2531         ei = pool_elt_at_index (em->interfaces, hi->hw_instance);
2532         ASSERT (sizeof (mp->l2_address) >= sizeof (ei->address));
2533         clib_memcpy (mp->l2_address, ei->address, sizeof (ei->address));
2534         mp->l2_address_length = ntohl(sizeof (ei->address));
2535     } else if (swif->sup_sw_if_index != swif->sw_if_index) {
2536         vnet_sub_interface_t *sub = &swif->sub;
2537         mp->sub_id = ntohl(sub->id);
2538         mp->sub_dot1ad = sub->eth.flags.dot1ad;
2539         mp->sub_number_of_tags = sub->eth.flags.one_tag + sub->eth.flags.two_tags*2;
2540         mp->sub_outer_vlan_id = ntohs(sub->eth.outer_vlan_id);
2541         mp->sub_inner_vlan_id = ntohs(sub->eth.inner_vlan_id);
2542         mp->sub_exact_match = sub->eth.flags.exact_match;
2543         mp->sub_default = sub->eth.flags.default_sub;
2544         mp->sub_outer_vlan_id_any = sub->eth.flags.outer_vlan_id_any;
2545         mp->sub_inner_vlan_id_any = sub->eth.flags.inner_vlan_id_any;
2546
2547         /* vlan tag rewrite data */
2548         u32 vtr_op = L2_VTR_DISABLED;
2549         u32 vtr_push_dot1q = 0, vtr_tag1 = 0, vtr_tag2 = 0;
2550
2551         if (l2vtr_get(am->vlib_main, am->vnet_main, swif->sw_if_index,
2552                       &vtr_op, &vtr_push_dot1q, &vtr_tag1, &vtr_tag2) != 0) {
2553             // error - default to disabled
2554             mp->vtr_op = ntohl(L2_VTR_DISABLED);
2555             clib_warning("cannot get vlan tag rewrite for sw_if_index %d",
2556                     swif->sw_if_index);
2557         } else {
2558             mp->vtr_op = ntohl(vtr_op);
2559             mp->vtr_push_dot1q = ntohl(vtr_push_dot1q);
2560             mp->vtr_tag1 = ntohl(vtr_tag1);
2561             mp->vtr_tag2 = ntohl(vtr_tag2);
2562         }
2563     }
2564
2565     vl_msg_api_send_shmem (q, (u8 *)&mp);
2566 }
2567
2568 static void send_sw_interface_flags (vpe_api_main_t * am,
2569                                      unix_shared_memory_queue_t *q,
2570                                      vnet_sw_interface_t * swif)
2571 {
2572     vl_api_sw_interface_set_flags_t *mp;
2573     vnet_main_t * vnm = am->vnet_main;
2574
2575     vnet_hw_interface_t *hi = vnet_get_sup_hw_interface (vnm,
2576                                                          swif->sw_if_index);
2577     mp = vl_msg_api_alloc (sizeof (*mp));
2578     memset (mp, 0, sizeof (*mp));
2579     mp->_vl_msg_id = ntohs(VL_API_SW_INTERFACE_SET_FLAGS);
2580     mp->sw_if_index = ntohl(swif->sw_if_index);
2581
2582     mp->admin_up_down = (swif->flags & VNET_SW_INTERFACE_FLAG_ADMIN_UP) ?
2583         1 : 0;
2584     mp->link_up_down = (hi->flags & VNET_HW_INTERFACE_FLAG_LINK_UP) ?
2585         1 : 0;
2586     vl_msg_api_send_shmem (q, (u8 *)&mp);
2587 }
2588
2589 static void send_sw_interface_flags_deleted (vpe_api_main_t * am,
2590                                      unix_shared_memory_queue_t *q,
2591                                              u32 sw_if_index)
2592     __attribute__((unused));
2593
2594 static void send_sw_interface_flags_deleted (vpe_api_main_t * am,
2595                                      unix_shared_memory_queue_t *q,
2596                                      u32 sw_if_index)
2597 {
2598     vl_api_sw_interface_set_flags_t *mp;
2599
2600     mp = vl_msg_api_alloc (sizeof (*mp));
2601     memset (mp, 0, sizeof (*mp));
2602     mp->_vl_msg_id = ntohs(VL_API_SW_INTERFACE_SET_FLAGS);
2603     mp->sw_if_index = ntohl(sw_if_index);
2604
2605     mp->admin_up_down = 0;
2606     mp->link_up_down = 0;
2607     mp->deleted = 1;
2608     vl_msg_api_send_shmem (q, (u8 *)&mp);
2609 }
2610
2611 static void vl_api_sw_interface_dump_t_handler (
2612     vl_api_sw_interface_dump_t *mp)
2613 {
2614     vpe_api_main_t * am = &vpe_api_main;
2615     vnet_sw_interface_t * swif;
2616     vnet_interface_main_t * im = &am->vnet_main->interface_main;
2617     u8 * filter_string = 0, * name_string = 0;
2618     unix_shared_memory_queue_t * q;
2619     char * strcasestr (char *, char *); /* lnx hdr file botch */
2620
2621     q = vl_api_client_index_to_input_queue (mp->client_index);
2622
2623     if (q == 0)
2624         return;
2625
2626     if (mp->name_filter_valid) {
2627         mp->name_filter [ARRAY_LEN(mp->name_filter)-1] = 0;
2628         filter_string = format (0, "%s%c", mp->name_filter, 0);
2629     }
2630
2631     pool_foreach (swif, im->sw_interfaces,
2632     ({
2633         name_string = format (name_string, "%U%c",
2634                               format_vnet_sw_interface_name,
2635                               am->vnet_main, swif, 0);
2636
2637         if (mp->name_filter_valid == 0 ||
2638             strcasestr((char *) name_string, (char *) filter_string)) {
2639
2640             send_sw_interface_details (am, q, swif, name_string, mp->context);
2641         }
2642         _vec_len (name_string) = 0;
2643     }));
2644
2645     vec_free (name_string);
2646     vec_free (filter_string);
2647 }
2648
2649 void send_oam_event (oam_target_t * t)
2650 {
2651     vpe_api_main_t * vam = &vpe_api_main;
2652     unix_shared_memory_queue_t * q;
2653     vpe_client_registration_t *reg;
2654     vl_api_oam_event_t * mp;
2655
2656     pool_foreach(reg, vam->oam_events_registrations,
2657     ({
2658         q = vl_api_client_index_to_input_queue (reg->client_index);
2659         if (q) {
2660             mp = vl_msg_api_alloc (sizeof (*mp));
2661             mp->_vl_msg_id = ntohs (VL_API_OAM_EVENT);
2662             clib_memcpy (mp->dst_address, &t->dst_address, sizeof (mp->dst_address));
2663             mp->state = t->state;
2664             vl_msg_api_send_shmem (q, (u8 *)&mp);
2665         }
2666     }));
2667 }
2668
2669 static void
2670 vl_api_oam_add_del_t_handler (vl_api_oam_add_del_t *mp)
2671 {
2672     vl_api_oam_add_del_reply_t * rmp;
2673     int rv;
2674
2675     rv = vpe_oam_add_del_target ((ip4_address_t *)mp->src_address,
2676                                  (ip4_address_t *)mp->dst_address,
2677                                  ntohl(mp->vrf_id),
2678                                  (int)(mp->is_add));
2679
2680     REPLY_MACRO(VL_API_OAM_ADD_DEL_REPLY);
2681 }
2682
2683 static void
2684 vl_api_vnet_get_summary_stats_t_handler (
2685     vl_api_vnet_get_summary_stats_t *mp)
2686 {
2687     stats_main_t * sm = &stats_main;
2688     vnet_interface_main_t * im = sm->interface_main;
2689     vl_api_vnet_summary_stats_reply_t *rmp;
2690     vlib_combined_counter_main_t * cm;
2691     vlib_counter_t v;
2692     int i, which;
2693     u64 total_pkts[VLIB_N_RX_TX];
2694     u64 total_bytes[VLIB_N_RX_TX];
2695
2696     unix_shared_memory_queue_t * q =
2697         vl_api_client_index_to_input_queue (mp->client_index);
2698
2699     if (!q)
2700         return;
2701
2702     rmp = vl_msg_api_alloc (sizeof (*rmp));
2703     rmp->_vl_msg_id = ntohs(VL_API_VNET_SUMMARY_STATS_REPLY);
2704     rmp->context = mp->context;
2705     rmp->retval = 0;
2706
2707     memset (total_pkts, 0, sizeof (total_pkts));
2708     memset (total_bytes, 0, sizeof (total_bytes));
2709
2710     vnet_interface_counter_lock (im);
2711
2712     vec_foreach (cm, im->combined_sw_if_counters) {
2713         which = cm - im->combined_sw_if_counters;
2714
2715         for (i = 0; i < vec_len (cm->maxi); i++) {
2716             vlib_get_combined_counter (cm, i, &v);
2717             total_pkts[which] += v.packets;
2718             total_bytes[which] += v.bytes;
2719         }
2720     }
2721     vnet_interface_counter_unlock (im);
2722
2723     /* Note: in HOST byte order! */
2724     rmp->total_pkts[VLIB_RX] = total_pkts[VLIB_RX];
2725     rmp->total_bytes[VLIB_RX] = total_bytes[VLIB_RX];
2726     rmp->total_pkts[VLIB_TX] = total_pkts[VLIB_TX];
2727     rmp->total_bytes[VLIB_TX] = total_bytes[VLIB_TX];
2728     rmp->vector_rate = vlib_last_vector_length_per_node (sm->vlib_main);
2729
2730     vl_msg_api_send_shmem (q, (u8 *)&rmp);
2731 }
2732
2733 typedef CLIB_PACKED (struct {
2734   ip4_address_t address;
2735
2736   u32 address_length : 6;
2737
2738   u32 index : 26;
2739 }) ip4_route_t;
2740
2741 static int ip4_reset_fib_t_handler (vl_api_reset_fib_t *mp)
2742 {
2743     vnet_main_t * vnm = vnet_get_main();
2744     vnet_interface_main_t * im = &vnm->interface_main;
2745     ip4_main_t * im4 = &ip4_main;
2746     static ip4_route_t * routes;
2747     static u32 * sw_if_indices_to_shut;
2748     stats_main_t * sm = &stats_main;
2749     ip4_route_t * r;
2750     ip4_fib_t * fib;
2751     u32 sw_if_index;
2752     int i;
2753     int rv = VNET_API_ERROR_NO_SUCH_FIB;
2754     u32 target_fib_id = ntohl(mp->vrf_id);
2755
2756     dslock (sm, 1 /* release hint */, 8 /* tag */);
2757
2758     vec_foreach (fib, im4->fibs) {
2759         vnet_sw_interface_t * si;
2760
2761         if (fib->table_id != target_fib_id)
2762             continue;
2763
2764         /* remove any mpls/gre tunnels in this fib */
2765         vnet_mpls_gre_delete_fib_tunnels (fib->table_id);
2766
2767         /* remove any mpls encap/decap labels */
2768         mpls_fib_reset_labels (fib->table_id);
2769
2770         /* remove any proxy arps in this fib */
2771         vnet_proxy_arp_fib_reset (fib->table_id);
2772
2773         /* Set the flow hash for this fib to the default */
2774         vnet_set_ip4_flow_hash (fib->table_id, IP_FLOW_HASH_DEFAULT);
2775
2776         vec_reset_length (sw_if_indices_to_shut);
2777
2778         /* Shut down interfaces in this FIB / clean out intfc routes */
2779         pool_foreach (si, im->sw_interfaces,
2780         ({
2781             u32 sw_if_index = si->sw_if_index;
2782
2783             if (sw_if_index < vec_len (im4->fib_index_by_sw_if_index)
2784                 && (im4->fib_index_by_sw_if_index[si->sw_if_index] ==
2785                     fib - im4->fibs))
2786                 vec_add1 (sw_if_indices_to_shut, si->sw_if_index);
2787         }));
2788
2789         for (i = 0; i < vec_len (sw_if_indices_to_shut); i++) {
2790             sw_if_index = sw_if_indices_to_shut[i];
2791             // vec_foreach (sw_if_index, sw_if_indices_to_shut) {
2792
2793             u32 flags = vnet_sw_interface_get_flags (vnm, sw_if_index);
2794             flags &= ~(VNET_SW_INTERFACE_FLAG_ADMIN_UP);
2795             vnet_sw_interface_set_flags (vnm, sw_if_index, flags);
2796         }
2797
2798         vec_reset_length (routes);
2799
2800         for (i = 0; i < ARRAY_LEN (fib->adj_index_by_dst_address); i++) {
2801             uword * hash = fib->adj_index_by_dst_address[i];
2802             hash_pair_t * p;
2803             ip4_route_t x;
2804
2805             x.address_length = i;
2806
2807             hash_foreach_pair (p, hash,
2808             ({
2809                 x.address.data_u32 = p->key;
2810                 vec_add1 (routes, x);
2811             }));
2812         }
2813
2814         vec_foreach (r, routes) {
2815             ip4_add_del_route_args_t a;
2816
2817             memset (&a, 0, sizeof (a));
2818             a.flags = IP4_ROUTE_FLAG_FIB_INDEX | IP4_ROUTE_FLAG_DEL;
2819             a.table_index_or_table_id = fib - im4->fibs;
2820             a.dst_address = r->address;
2821             a.dst_address_length = r->address_length;
2822             a.adj_index = ~0;
2823
2824             ip4_add_del_route (im4, &a);
2825             ip4_maybe_remap_adjacencies (im4, fib - im4->fibs,
2826                                          IP4_ROUTE_FLAG_FIB_INDEX);
2827         }
2828         rv = 0;
2829         break;
2830     } /* vec_foreach (fib) */
2831
2832     dsunlock(sm);
2833     return rv;
2834 }
2835
2836 typedef struct {
2837   ip6_address_t address;
2838   u32 address_length;
2839   u32 index;
2840 } ip6_route_t;
2841
2842 typedef struct {
2843   u32 fib_index;
2844   ip6_route_t ** routep;
2845 } add_routes_in_fib_arg_t;
2846
2847 static void add_routes_in_fib (clib_bihash_kv_24_8_t * kvp, void *arg)
2848 {
2849   add_routes_in_fib_arg_t * ap = arg;
2850
2851   if (kvp->key[2]>>32 == ap->fib_index)
2852     {
2853       ip6_address_t *addr;
2854       ip6_route_t * r;
2855       addr = (ip6_address_t *) kvp;
2856       vec_add2 (*ap->routep, r, 1);
2857       r->address = addr[0];
2858       r->address_length = kvp->key[2] & 0xFF;
2859       r->index = kvp->value;
2860     }
2861 }
2862
2863 static int ip6_reset_fib_t_handler (vl_api_reset_fib_t *mp)
2864 {
2865     vnet_main_t * vnm = vnet_get_main();
2866     vnet_interface_main_t * im = &vnm->interface_main;
2867     ip6_main_t * im6 = &ip6_main;
2868     stats_main_t * sm = &stats_main;
2869     static ip6_route_t * routes;
2870     static u32 * sw_if_indices_to_shut;
2871     ip6_route_t * r;
2872     ip6_fib_t * fib;
2873     u32 sw_if_index;
2874     int i;
2875     int rv = VNET_API_ERROR_NO_SUCH_FIB;
2876     u32 target_fib_id = ntohl(mp->vrf_id);
2877     add_routes_in_fib_arg_t _a, *a=&_a;
2878     clib_bihash_24_8_t * h = &im6->ip6_lookup_table;
2879
2880     dslock (sm, 1 /* release hint */, 9 /* tag */);
2881
2882     vec_foreach (fib, im6->fibs) {
2883         vnet_sw_interface_t * si;
2884
2885         if (fib->table_id != target_fib_id)
2886             continue;
2887
2888         vec_reset_length (sw_if_indices_to_shut);
2889
2890         /* Shut down interfaces in this FIB / clean out intfc routes */
2891         pool_foreach (si, im->sw_interfaces,
2892         ({
2893             if (im6->fib_index_by_sw_if_index[si->sw_if_index] ==
2894                 fib - im6->fibs)
2895                 vec_add1 (sw_if_indices_to_shut, si->sw_if_index);
2896         }));
2897
2898         for (i = 0; i < vec_len (sw_if_indices_to_shut); i++) {
2899             sw_if_index = sw_if_indices_to_shut[i];
2900             // vec_foreach (sw_if_index, sw_if_indices_to_shut) {
2901
2902             u32 flags = vnet_sw_interface_get_flags (vnm, sw_if_index);
2903             flags &= ~(VNET_SW_INTERFACE_FLAG_ADMIN_UP);
2904             vnet_sw_interface_set_flags (vnm, sw_if_index, flags);
2905         }
2906
2907         vec_reset_length (routes);
2908
2909         a->fib_index = fib - im6->fibs;
2910         a->routep = &routes;
2911
2912         clib_bihash_foreach_key_value_pair_24_8 (h, add_routes_in_fib, a);
2913
2914         vec_foreach (r, routes) {
2915             ip6_add_del_route_args_t a;
2916
2917             memset (&a, 0, sizeof (a));
2918             a.flags = IP6_ROUTE_FLAG_FIB_INDEX | IP6_ROUTE_FLAG_DEL;
2919             a.table_index_or_table_id = fib - im6->fibs;
2920             a.dst_address = r->address;
2921             a.dst_address_length = r->address_length;
2922             a.adj_index = ~0;
2923
2924             ip6_add_del_route (im6, &a);
2925             ip6_maybe_remap_adjacencies (im6, fib - im6->fibs,
2926                                          IP6_ROUTE_FLAG_FIB_INDEX);
2927         }
2928         rv = 0;
2929         /* Reinstall the neighbor / router discovery routes */
2930         vnet_ip6_fib_init (im6, fib - im6->fibs);
2931         break;
2932     } /* vec_foreach (fib) */
2933
2934     dsunlock(sm);
2935     return rv;
2936 }
2937
2938 static void vl_api_reset_fib_t_handler (vl_api_reset_fib_t *mp)
2939 {
2940     int rv;
2941     vl_api_reset_fib_reply_t * rmp;
2942
2943     if (mp->is_ipv6)
2944         rv = ip6_reset_fib_t_handler (mp);
2945     else
2946         rv = ip4_reset_fib_t_handler (mp);
2947
2948     REPLY_MACRO(VL_API_RESET_FIB_REPLY);
2949 }
2950
2951
2952 static void
2953 dhcpv4_proxy_config (vl_api_dhcp_proxy_config_t *mp)
2954 {
2955     vl_api_dhcp_proxy_config_reply_t * rmp;
2956     int rv;
2957
2958     rv = dhcp_proxy_set_server ((ip4_address_t *)(&mp->dhcp_server),
2959                                 (ip4_address_t *)(&mp->dhcp_src_address),
2960                                 (u32) ntohl(mp->vrf_id),
2961                                 (int) mp->insert_circuit_id,
2962                                 (int) (mp->is_add == 0));
2963
2964     REPLY_MACRO(VL_API_DHCP_PROXY_CONFIG_REPLY);
2965 }
2966
2967
2968 static void
2969 dhcpv6_proxy_config (vl_api_dhcp_proxy_config_t *mp)
2970 {
2971     vl_api_dhcp_proxy_config_reply_t * rmp;
2972     int rv = -1;
2973
2974     rv = dhcpv6_proxy_set_server ((ip6_address_t *)(&mp->dhcp_server),
2975                                 (ip6_address_t *)(&mp->dhcp_src_address),
2976                                 (u32) ntohl(mp->vrf_id),
2977                                 (int) mp->insert_circuit_id,
2978                                 (int) (mp->is_add == 0));
2979
2980     REPLY_MACRO(VL_API_DHCP_PROXY_CONFIG_REPLY);
2981 }
2982
2983 static void
2984 dhcpv4_proxy_config_2 (vl_api_dhcp_proxy_config_2_t *mp)
2985 {
2986     vl_api_dhcp_proxy_config_reply_t * rmp;
2987     int rv;
2988
2989     rv = dhcp_proxy_set_server_2 ((ip4_address_t *)(&mp->dhcp_server),
2990                                 (ip4_address_t *)(&mp->dhcp_src_address),
2991                                 (u32) ntohl(mp->rx_vrf_id),
2992                                 (u32) ntohl(mp->server_vrf_id),
2993                                 (int) mp->insert_circuit_id,
2994                                 (int) (mp->is_add == 0));
2995
2996     REPLY_MACRO(VL_API_DHCP_PROXY_CONFIG_2_REPLY);
2997 }
2998
2999
3000 static void
3001 dhcpv6_proxy_config_2 (vl_api_dhcp_proxy_config_2_t *mp)
3002 {
3003     vl_api_dhcp_proxy_config_reply_t * rmp;
3004     int rv = -1;
3005
3006 #if 0 // $$$$ FIXME
3007     rv = dhcpv6_proxy_set_server_2 ((ip6_address_t *)(&mp->dhcp_server),
3008                                 (ip6_address_t *)(&mp->dhcp_src_address),
3009                                 (u32) ntohl(mp->rx_vrf_id),
3010                                 (u32) ntohl(mp->server_vrf_id),
3011                                 (int) mp->insert_circuit_id,
3012                                 (int) (mp->is_add == 0));
3013 #else
3014     rv = VNET_API_ERROR_UNIMPLEMENTED;
3015 #endif
3016
3017     REPLY_MACRO(VL_API_DHCP_PROXY_CONFIG_2_REPLY);
3018 }
3019
3020
3021 static void
3022 vl_api_dhcp_proxy_set_vss_t_handler (vl_api_dhcp_proxy_set_vss_t *mp)
3023 {
3024     vl_api_dhcp_proxy_set_vss_reply_t *rmp;
3025     int rv;
3026     if (!mp->is_ipv6)
3027         rv = dhcp_proxy_set_option82_vss(ntohl(mp->tbl_id),
3028                                          ntohl(mp->oui),
3029                                          ntohl(mp->fib_id),
3030                                          (int)mp->is_add == 0);
3031     else
3032          rv = dhcpv6_proxy_set_vss( ntohl(mp->tbl_id),
3033                                          ntohl(mp->oui),
3034                                          ntohl(mp->fib_id),
3035                                          (int)mp->is_add == 0);
3036
3037     REPLY_MACRO(VL_API_DHCP_PROXY_SET_VSS_REPLY);
3038 }
3039
3040
3041 static void vl_api_dhcp_proxy_config_t_handler
3042 (vl_api_dhcp_proxy_config_t *mp)
3043 {
3044     if (mp->is_ipv6 == 0)
3045         dhcpv4_proxy_config (mp);
3046     else
3047         dhcpv6_proxy_config (mp);
3048 }
3049
3050 static void vl_api_dhcp_proxy_config_2_t_handler
3051 (vl_api_dhcp_proxy_config_2_t *mp)
3052 {
3053     if (mp->is_ipv6 == 0)
3054         dhcpv4_proxy_config_2 (mp);
3055     else
3056         dhcpv6_proxy_config_2 (mp);
3057 }
3058
3059 void dhcp_compl_event_callback (u32 client_index, u32 pid, u8 * hostname,
3060        u8 is_ipv6, u8 * host_address, u8 * router_address, u8 * host_mac)
3061 {
3062     unix_shared_memory_queue_t * q;
3063     vl_api_dhcp_compl_event_t * mp;
3064
3065     q = vl_api_client_index_to_input_queue (client_index);
3066     if (!q)
3067         return;
3068
3069     mp = vl_msg_api_alloc (sizeof (*mp));
3070     mp->client_index = client_index;
3071     mp->pid = pid;
3072     mp->is_ipv6 = is_ipv6;
3073     clib_memcpy (&mp->hostname, hostname, vec_len(hostname));
3074     mp->hostname[vec_len(hostname) + 1] = '\n';
3075     clib_memcpy (&mp->host_address[0], host_address, 16);
3076     clib_memcpy (&mp->router_address[0], router_address, 16);
3077     clib_memcpy (&mp->host_mac[0], host_mac, 6);
3078
3079     mp->_vl_msg_id = ntohs (VL_API_DHCP_COMPL_EVENT);
3080
3081     vl_msg_api_send_shmem (q, (u8 *)&mp);
3082 }
3083
3084 static void vl_api_dhcp_client_config_t_handler
3085 (vl_api_dhcp_client_config_t *mp)
3086 {
3087     vlib_main_t *vm = vlib_get_main();
3088     vl_api_dhcp_client_config_reply_t * rmp;
3089     int rv = 0;
3090
3091     VALIDATE_SW_IF_INDEX(mp);
3092
3093     rv = dhcp_client_config(vm, ntohl(mp->sw_if_index),
3094              mp->hostname, mp->is_add, mp->client_index,
3095              mp->want_dhcp_event ? dhcp_compl_event_callback : NULL,
3096              mp->pid);
3097
3098     BAD_SW_IF_INDEX_LABEL;
3099
3100     REPLY_MACRO(VL_API_DHCP_CLIENT_CONFIG_REPLY);
3101 }
3102
3103 static void
3104 vl_api_sw_interface_ip6nd_ra_config_t_handler
3105 (vl_api_sw_interface_ip6nd_ra_config_t *mp, vlib_main_t *vm)
3106 {
3107    vl_api_sw_interface_ip6nd_ra_config_reply_t * rmp;
3108     int rv = 0;
3109     u8  is_no,  suppress, managed, other, ll_option, send_unicast, cease, default_router;
3110
3111     is_no = mp->is_no == 1;
3112     suppress = mp->suppress == 1;
3113     managed = mp->managed == 1;
3114    other = mp->other == 1;
3115     ll_option = mp->ll_option == 1;
3116     send_unicast = mp->send_unicast == 1;
3117     cease = mp->cease == 1;
3118     default_router = mp->default_router  == 1;
3119
3120     VALIDATE_SW_IF_INDEX(mp);
3121
3122     rv = ip6_neighbor_ra_config(vm, ntohl(mp->sw_if_index),
3123                                 suppress,  managed,  other,
3124                                 ll_option,  send_unicast,  cease,
3125                                 default_router, ntohl (mp->lifetime),
3126                                 ntohl(mp->initial_count),  ntohl(mp->initial_interval),
3127                                 ntohl(mp->max_interval), ntohl( mp->min_interval),
3128                                 is_no);
3129
3130     BAD_SW_IF_INDEX_LABEL;
3131
3132     REPLY_MACRO(VL_API_SW_INTERFACE_IP6ND_RA_CONFIG_REPLY);
3133 }
3134
3135 static void
3136 vl_api_sw_interface_ip6nd_ra_prefix_t_handler
3137 (vl_api_sw_interface_ip6nd_ra_prefix_t *mp, vlib_main_t *vm)
3138 {
3139    vl_api_sw_interface_ip6nd_ra_prefix_reply_t * rmp;
3140     int rv = 0;
3141     u8  is_no,  use_default,  no_advertise, off_link, no_autoconfig, no_onlink;
3142
3143     VALIDATE_SW_IF_INDEX(mp);
3144
3145     is_no = mp->is_no == 1;
3146     use_default = mp->use_default == 1;
3147     no_advertise = mp->no_advertise == 1;
3148     off_link = mp->off_link == 1;
3149     no_autoconfig = mp->no_autoconfig == 1;
3150     no_onlink = mp->no_onlink == 1;
3151
3152     rv = ip6_neighbor_ra_prefix(vm,  ntohl(mp->sw_if_index),
3153                                 (ip6_address_t *)mp->address,  mp->address_length,
3154                                 use_default,  ntohl(mp->val_lifetime), ntohl(mp->pref_lifetime),
3155                                 no_advertise,  off_link, no_autoconfig, no_onlink,
3156                                 is_no);
3157
3158     BAD_SW_IF_INDEX_LABEL;
3159     REPLY_MACRO(VL_API_SW_INTERFACE_IP6ND_RA_PREFIX_REPLY);
3160 }
3161
3162 static void
3163 vl_api_sw_interface_ip6_enable_disable_t_handler
3164 (vl_api_sw_interface_ip6_enable_disable_t *mp, vlib_main_t *vm)
3165 {
3166     vl_api_sw_interface_ip6_enable_disable_reply_t * rmp;
3167     vnet_main_t * vnm = vnet_get_main();
3168     int rv = 0;
3169     clib_error_t * error;
3170
3171     vnm->api_errno = 0;
3172
3173     VALIDATE_SW_IF_INDEX(mp);
3174
3175     error = ( mp->enable == 1) ? enable_ip6_interface(vm,ntohl(mp->sw_if_index)) :
3176         disable_ip6_interface(vm,ntohl(mp->sw_if_index));
3177
3178     if (error)  {
3179         clib_error_report(error);
3180         rv = VNET_API_ERROR_UNSPECIFIED;
3181     } else {
3182         rv = vnm->api_errno;
3183     }
3184
3185     BAD_SW_IF_INDEX_LABEL;
3186
3187     REPLY_MACRO(VL_API_SW_INTERFACE_IP6_ENABLE_DISABLE_REPLY);
3188 }
3189
3190 static void
3191 vl_api_sw_interface_ip6_set_link_local_address_t_handler
3192 (vl_api_sw_interface_ip6_set_link_local_address_t *mp, vlib_main_t *vm)
3193 {
3194     vl_api_sw_interface_ip6_set_link_local_address_reply_t * rmp;
3195     int rv = 0;
3196     clib_error_t * error;
3197     vnet_main_t * vnm = vnet_get_main();
3198
3199     vnm->api_errno = 0;
3200
3201     VALIDATE_SW_IF_INDEX(mp);
3202
3203     error = set_ip6_link_local_address(vm,
3204                                        ntohl(mp->sw_if_index),
3205                                        (ip6_address_t *)mp->address,
3206                                        mp->address_length);
3207     if (error)  {
3208       clib_error_report(error);
3209       rv = VNET_API_ERROR_UNSPECIFIED;
3210     } else {
3211         rv = vnm->api_errno;
3212     }
3213
3214     BAD_SW_IF_INDEX_LABEL;
3215
3216     REPLY_MACRO(VL_API_SW_INTERFACE_IP6_SET_LINK_LOCAL_ADDRESS_REPLY);
3217 }
3218
3219 static void set_ip6_flow_hash (vl_api_set_ip_flow_hash_t *mp)
3220 {
3221     vl_api_set_ip_flow_hash_reply_t *rmp;
3222     int rv = VNET_API_ERROR_UNIMPLEMENTED;
3223
3224     clib_warning ("unimplemented...");
3225
3226     REPLY_MACRO(VL_API_SET_IP_FLOW_HASH_REPLY);
3227 }
3228
3229 static void set_ip4_flow_hash (vl_api_set_ip_flow_hash_t *mp)
3230 {
3231     vl_api_set_ip_flow_hash_reply_t *rmp;
3232     int rv;
3233     u32 table_id;
3234     u32 flow_hash_config = 0;
3235
3236     table_id = ntohl(mp->vrf_id);
3237
3238 #define _(a,b) if (mp->a) flow_hash_config |= b;
3239     foreach_flow_hash_bit;
3240 #undef _
3241
3242     rv = vnet_set_ip4_flow_hash (table_id, flow_hash_config);
3243
3244     REPLY_MACRO(VL_API_SET_IP_FLOW_HASH_REPLY);
3245 }
3246
3247
3248 static void vl_api_set_ip_flow_hash_t_handler
3249 (vl_api_set_ip_flow_hash_t *mp)
3250 {
3251     if (mp->is_ipv6 == 0)
3252         set_ip4_flow_hash (mp);
3253     else
3254         set_ip6_flow_hash (mp);
3255 }
3256
3257 static void vl_api_sw_interface_set_unnumbered_t_handler
3258 (vl_api_sw_interface_set_unnumbered_t *mp)
3259 {
3260     vl_api_sw_interface_set_unnumbered_reply_t * rmp;
3261     int rv = 0;
3262     vnet_sw_interface_t * si;
3263     vnet_main_t *vnm = vnet_get_main();
3264     u32 sw_if_index, unnumbered_sw_if_index;
3265
3266     sw_if_index = ntohl(mp->sw_if_index);
3267     unnumbered_sw_if_index = ntohl(mp->unnumbered_sw_if_index);
3268
3269     /*
3270      * The API message field names are backwards from
3271      * the underlying data structure names.
3272      * It's not worth changing them now.
3273      */
3274     if (pool_is_free_index (vnm->interface_main.sw_interfaces,
3275                             unnumbered_sw_if_index)) {
3276         rv = VNET_API_ERROR_INVALID_SW_IF_INDEX;
3277         goto done;
3278     }
3279
3280     /* Only check the "use loop0" field when setting the binding */
3281     if (mp->is_add &&
3282         pool_is_free_index (vnm->interface_main.sw_interfaces, sw_if_index)) {
3283         rv = VNET_API_ERROR_INVALID_SW_IF_INDEX_2;
3284         goto done;
3285     }
3286
3287     si = vnet_get_sw_interface (vnm, unnumbered_sw_if_index);
3288
3289     if (mp->is_add) {
3290         si->flags |= VNET_SW_INTERFACE_FLAG_UNNUMBERED;
3291         si->unnumbered_sw_if_index = sw_if_index;
3292     } else {
3293         si->flags &= ~(VNET_SW_INTERFACE_FLAG_UNNUMBERED);
3294         si->unnumbered_sw_if_index = (u32)~0;
3295     }
3296
3297  done:
3298     REPLY_MACRO(VL_API_SW_INTERFACE_SET_UNNUMBERED_REPLY);
3299 }
3300
3301 static void vl_api_create_loopback_t_handler
3302 (vl_api_create_loopback_t *mp)
3303 {
3304     vl_api_create_loopback_reply_t * rmp;
3305     u32 sw_if_index;
3306     int rv;
3307
3308     rv = vnet_create_loopback_interface (&sw_if_index, mp->mac_address);
3309
3310     REPLY_MACRO2(VL_API_CREATE_LOOPBACK_REPLY,
3311     ({
3312         rmp->sw_if_index = ntohl (sw_if_index);
3313     }));
3314 }
3315
3316 static void vl_api_delete_loopback_t_handler
3317 (vl_api_delete_loopback_t *mp)
3318 {
3319     vl_api_delete_loopback_reply_t * rmp;
3320     u32 sw_if_index;
3321     int rv;
3322
3323     sw_if_index = ntohl (mp->sw_if_index);
3324     rv = vnet_delete_loopback_interface (sw_if_index);
3325
3326     REPLY_MACRO(VL_API_DELETE_LOOPBACK_REPLY);
3327 }
3328
3329 static void vl_api_control_ping_t_handler
3330 (vl_api_control_ping_t *mp)
3331 {
3332     vl_api_control_ping_reply_t * rmp;
3333     int rv = 0;
3334
3335     REPLY_MACRO2(VL_API_CONTROL_PING_REPLY,
3336     ({
3337         rmp->vpe_pid = ntohl (getpid());
3338     }));
3339 }
3340
3341 static void shmem_cli_output (uword arg, u8 * buffer, uword buffer_bytes)
3342 {
3343     u8 **shmem_vecp = (u8 **)arg;
3344     u8 *shmem_vec;
3345     void *oldheap;
3346     api_main_t * am = &api_main;
3347     u32 offset;
3348
3349     shmem_vec = *shmem_vecp;
3350
3351     offset = vec_len (shmem_vec);
3352
3353     pthread_mutex_lock (&am->vlib_rp->mutex);
3354     oldheap = svm_push_data_heap (am->vlib_rp);
3355
3356     vec_validate (shmem_vec, offset + buffer_bytes - 1);
3357
3358     clib_memcpy (shmem_vec + offset, buffer, buffer_bytes);
3359
3360     svm_pop_heap (oldheap);
3361     pthread_mutex_unlock (&am->vlib_rp->mutex);
3362
3363     *shmem_vecp = shmem_vec;
3364 }
3365
3366
3367 static void vl_api_cli_request_t_handler
3368 (vl_api_cli_request_t *mp)
3369 {
3370     vl_api_cli_reply_t *rp;
3371     unix_shared_memory_queue_t *q;
3372     vlib_main_t * vm = vlib_get_main();
3373     api_main_t * am = &api_main;
3374     unformat_input_t input;
3375     u8 *shmem_vec=0;
3376     void *oldheap;
3377
3378     q = vl_api_client_index_to_input_queue (mp->client_index);
3379     if (!q)
3380         return;
3381
3382     rp = vl_msg_api_alloc (sizeof (*rp));
3383     rp->_vl_msg_id = ntohs(VL_API_CLI_REPLY);
3384     rp->context = mp->context;
3385
3386     unformat_init_vector (&input, (u8 *)(uword)mp->cmd_in_shmem);
3387
3388     vlib_cli_input (vm, &input, shmem_cli_output,
3389                     (uword)&shmem_vec);
3390
3391     pthread_mutex_lock (&am->vlib_rp->mutex);
3392     oldheap = svm_push_data_heap (am->vlib_rp);
3393
3394     vec_add1(shmem_vec, 0);
3395
3396     svm_pop_heap (oldheap);
3397     pthread_mutex_unlock (&am->vlib_rp->mutex);
3398
3399     rp->reply_in_shmem = (uword)shmem_vec;
3400
3401     vl_msg_api_send_shmem (q, (u8 *)&rp);
3402 }
3403
3404 static void vl_api_set_arp_neighbor_limit_t_handler (vl_api_set_arp_neighbor_limit_t *mp)
3405 {
3406     int rv;
3407     vl_api_set_arp_neighbor_limit_reply_t * rmp;
3408     vnet_main_t *vnm = vnet_get_main();
3409     clib_error_t * error;
3410
3411     vnm->api_errno = 0;
3412
3413     if (mp->is_ipv6)
3414         error = ip6_set_neighbor_limit (ntohl(mp->arp_neighbor_limit));
3415     else
3416         error = ip4_set_arp_limit (ntohl(mp->arp_neighbor_limit));
3417
3418     if (error)  {
3419         clib_error_report(error);
3420         rv = VNET_API_ERROR_UNSPECIFIED;
3421     } else {
3422         rv = vnm->api_errno;
3423     }
3424
3425     REPLY_MACRO(VL_API_SET_ARP_NEIGHBOR_LIMIT_REPLY);
3426 }
3427
3428 static void vl_api_sr_tunnel_add_del_t_handler
3429 (vl_api_sr_tunnel_add_del_t *mp)
3430 {
3431 #if IPV6SR == 0
3432     clib_warning ("unimplemented");
3433 #else
3434     ip6_sr_add_del_tunnel_args_t _a, *a=&_a;
3435     int rv = 0;
3436     vl_api_sr_tunnel_add_del_reply_t * rmp;
3437     ip6_address_t * segments = 0, * seg;
3438     ip6_address_t * tags = 0, *tag;
3439     ip6_address_t * this_address;
3440     int i;
3441
3442     if (mp->n_segments == 0) {
3443         rv = -11;
3444         goto out;
3445     }
3446
3447     memset (a, 0, sizeof (*a));
3448     a->src_address = (ip6_address_t *)&mp->src_address;
3449     a->dst_address = (ip6_address_t *)&mp->dst_address;
3450     a->dst_mask_width = mp->dst_mask_width;
3451     a->flags_net_byte_order = mp->flags_net_byte_order;
3452     a->is_del = (mp->is_add == 0);
3453     a->rx_table_id = ntohl(mp->outer_vrf_id);
3454     a->tx_table_id = ntohl(mp->inner_vrf_id);
3455
3456     a->name = format(0, "%s", mp->name);
3457     if (!(vec_len(a->name)))
3458       a->name = 0;
3459
3460     a->policy_name = format(0, "%s", mp->policy_name);
3461     if (!(vec_len(a->policy_name)))
3462       a->policy_name = 0;
3463
3464     /* Yank segments and tags out of the API message */
3465     this_address = (ip6_address_t *)mp->segs_and_tags;
3466     for (i = 0; i < mp->n_segments; i++) {
3467         vec_add2 (segments, seg, 1);
3468         clib_memcpy (seg->as_u8, this_address->as_u8, sizeof (*this_address));
3469         this_address++;
3470     }
3471     for (i = 0; i < mp->n_tags; i++) {
3472         vec_add2 (tags, tag, 1);
3473         clib_memcpy (tag->as_u8, this_address->as_u8, sizeof (*this_address));
3474         this_address++;
3475     }
3476
3477     a->segments = segments;
3478     a->tags = tags;
3479
3480     rv = ip6_sr_add_del_tunnel (a);
3481
3482 out:
3483
3484     REPLY_MACRO(VL_API_SR_TUNNEL_ADD_DEL_REPLY);
3485 #endif
3486 }
3487
3488 static void vl_api_sr_policy_add_del_t_handler
3489 (vl_api_sr_policy_add_del_t *mp)
3490 {
3491 #if IPV6SR == 0
3492     clib_warning ("unimplemented");
3493 #else
3494     ip6_sr_add_del_policy_args_t _a, *a=&_a;
3495     int rv = 0;
3496     vl_api_sr_policy_add_del_reply_t * rmp;
3497     int i;
3498
3499     memset (a, 0, sizeof (*a));
3500     a->is_del = (mp->is_add == 0);
3501
3502     a->name = format(0, "%s", mp->name);
3503     if (!(vec_len(a->name)))
3504       {
3505         rv = VNET_API_ERROR_NO_SUCH_NODE2;
3506         goto out;
3507       }
3508
3509     if (!(mp->tunnel_names[0]))
3510       {
3511         rv = VNET_API_ERROR_NO_SUCH_NODE2;
3512         goto out;
3513       }
3514
3515     // start deserializing tunnel_names
3516     int num_tunnels = mp->tunnel_names[0]; //number of tunnels
3517     u8 * deser_tun_names = mp->tunnel_names;
3518     deser_tun_names += 1; //moving along
3519
3520     u8 * tun_name = 0;
3521     int tun_name_len = 0;
3522
3523     for (i=0; i < num_tunnels; i++)
3524       {
3525         tun_name_len= *deser_tun_names;
3526         deser_tun_names += 1;
3527         vec_resize (tun_name, tun_name_len);
3528         memcpy(tun_name, deser_tun_names, tun_name_len);
3529         vec_add1 (a->tunnel_names, tun_name);
3530         deser_tun_names += tun_name_len;
3531         tun_name = 0;
3532       }
3533
3534     rv = ip6_sr_add_del_policy (a);
3535
3536 out:
3537
3538     REPLY_MACRO(VL_API_SR_POLICY_ADD_DEL_REPLY);
3539 #endif
3540 }
3541
3542 static void vl_api_sr_multicast_map_add_del_t_handler
3543 (vl_api_sr_multicast_map_add_del_t *mp)
3544 {
3545 #if IPV6SR == 0
3546     clib_warning ("unimplemented");
3547 #else
3548     ip6_sr_add_del_multicastmap_args_t _a, *a=&_a;
3549     int rv = 0;
3550     vl_api_sr_multicast_map_add_del_reply_t * rmp;
3551
3552     memset (a, 0, sizeof (*a));
3553     a->is_del = (mp->is_add == 0);
3554
3555     a->multicast_address = (ip6_address_t *)&mp->multicast_address;
3556     a->policy_name = format(0, "%s", mp->policy_name);
3557
3558     if (a->multicast_address == 0)
3559       {
3560         rv = -1 ;
3561         goto out;
3562       }
3563
3564     if (!(a->policy_name))
3565       {
3566         rv = -2 ;
3567         goto out;
3568       }
3569
3570 #if DPDK > 0 /* Cannot call replicate without DPDK */
3571     rv = ip6_sr_add_del_multicastmap (a);
3572 #else
3573     clib_warning ("multicast replication without DPDK not implemented");
3574     rv = VNET_API_ERROR_UNIMPLEMENTED;
3575 #endif /* DPDK */
3576
3577 out:
3578
3579     REPLY_MACRO(VL_API_SR_MULTICAST_MAP_ADD_DEL_REPLY);
3580 #endif
3581 }
3582
3583 #define foreach_classify_add_del_table_field    \
3584 _(table_index)                                  \
3585 _(nbuckets)                                     \
3586 _(memory_size)                                  \
3587 _(skip_n_vectors)                               \
3588 _(match_n_vectors)                              \
3589 _(next_table_index)                             \
3590 _(miss_next_index)
3591
3592 static void vl_api_classify_add_del_table_t_handler
3593 (vl_api_classify_add_del_table_t * mp)
3594 {
3595     vl_api_classify_add_del_table_reply_t * rmp;
3596     vnet_classify_main_t * cm = &vnet_classify_main;
3597     vnet_classify_table_t * t;
3598     int rv;
3599
3600 #define _(a) u32 a;
3601     foreach_classify_add_del_table_field;
3602 #undef _
3603
3604 #define _(a) a = ntohl(mp->a);
3605     foreach_classify_add_del_table_field;
3606 #undef _
3607
3608     /* The underlying API fails silently, on purpose, so check here */
3609     if (mp->is_add == 0)
3610         if (pool_is_free_index (cm->tables, table_index)) {
3611             rv = VNET_API_ERROR_NO_SUCH_TABLE;
3612             goto out;
3613         }
3614
3615     rv = vnet_classify_add_del_table
3616         (cm, mp->mask, nbuckets, memory_size,
3617          skip_n_vectors, match_n_vectors,
3618          next_table_index, miss_next_index,
3619          &table_index, mp->is_add);
3620
3621 out:
3622     REPLY_MACRO2(VL_API_CLASSIFY_ADD_DEL_TABLE_REPLY,
3623     ({
3624         if (rv == 0 && mp->is_add) {
3625             t = pool_elt_at_index (cm->tables, table_index);
3626             rmp->skip_n_vectors = ntohl(t->skip_n_vectors);
3627             rmp->match_n_vectors = ntohl(t->match_n_vectors);
3628             rmp->new_table_index = ntohl(table_index);
3629         } else {
3630             rmp->skip_n_vectors = ~0;
3631             rmp->match_n_vectors = ~0;
3632             rmp->new_table_index = ~0;
3633         }
3634     }));
3635 }
3636
3637 static void vl_api_classify_add_del_session_t_handler
3638 (vl_api_classify_add_del_session_t * mp)
3639 {
3640     vnet_classify_main_t * cm = &vnet_classify_main;
3641     vl_api_classify_add_del_session_reply_t * rmp;
3642     int rv;
3643     u32 table_index, hit_next_index, opaque_index;
3644     i32 advance;
3645
3646     table_index = ntohl (mp->table_index);
3647     hit_next_index = ntohl (mp->hit_next_index);
3648     opaque_index = ntohl (mp->opaque_index);
3649     advance = ntohl (mp->advance);
3650
3651     rv = vnet_classify_add_del_session
3652         (cm, table_index, mp->match, hit_next_index, opaque_index,
3653          advance, mp->is_add);
3654
3655     REPLY_MACRO(VL_API_CLASSIFY_ADD_DEL_SESSION_REPLY);
3656 }
3657
3658 static void vl_api_classify_set_interface_ip_table_t_handler
3659 (vl_api_classify_set_interface_ip_table_t * mp)
3660 {
3661     vlib_main_t * vm = vlib_get_main();
3662     vl_api_classify_set_interface_ip_table_reply_t * rmp;
3663     int rv;
3664     u32 table_index, sw_if_index;
3665
3666     table_index = ntohl (mp->table_index);
3667     sw_if_index = ntohl (mp->sw_if_index);
3668
3669     VALIDATE_SW_IF_INDEX(mp);
3670
3671     if (mp->is_ipv6)
3672         rv = vnet_set_ip6_classify_intfc (vm, sw_if_index, table_index);
3673     else
3674         rv = vnet_set_ip4_classify_intfc (vm, sw_if_index, table_index);
3675
3676     BAD_SW_IF_INDEX_LABEL;
3677
3678     REPLY_MACRO(VL_API_CLASSIFY_SET_INTERFACE_IP_TABLE_REPLY);
3679 }
3680
3681 static void vl_api_classify_set_interface_l2_tables_t_handler
3682 (vl_api_classify_set_interface_l2_tables_t * mp)
3683 {
3684     vl_api_classify_set_interface_l2_tables_reply_t * rmp;
3685     int rv;
3686     u32 sw_if_index, ip4_table_index, ip6_table_index, other_table_index;
3687     int enable;
3688
3689     ip4_table_index = ntohl(mp->ip4_table_index);
3690     ip6_table_index = ntohl(mp->ip6_table_index);
3691     other_table_index = ntohl(mp->other_table_index);
3692     sw_if_index = ntohl(mp->sw_if_index);
3693
3694     VALIDATE_SW_IF_INDEX(mp);
3695
3696     rv = vnet_l2_classify_set_tables (sw_if_index, ip4_table_index,
3697                                       ip6_table_index, other_table_index);
3698
3699     if (rv == 0) {
3700         if (ip4_table_index != ~0 || ip6_table_index != ~0
3701             || other_table_index != ~0)
3702             enable = 1;
3703         else
3704             enable = 0;
3705
3706         vnet_l2_classify_enable_disable (sw_if_index, enable);
3707     }
3708
3709     BAD_SW_IF_INDEX_LABEL;
3710
3711     REPLY_MACRO(VL_API_CLASSIFY_SET_INTERFACE_L2_TABLES_REPLY);
3712 }
3713
3714 static void
3715 vl_api_l2_fib_clear_table_t_handler (vl_api_l2_fib_clear_table_t *mp)
3716 {
3717     int rv = 0;
3718     vl_api_l2_fib_clear_table_reply_t * rmp;
3719
3720     /* DAW-FIXME: This API should only clear non-static l2fib entries, but
3721      *            that is not currently implemented.  When that TODO is fixed
3722      *            this call should be changed to pass 1 instead of 0.
3723      */
3724     l2fib_clear_table (0);
3725
3726     REPLY_MACRO(VL_API_L2_FIB_CLEAR_TABLE_REPLY);
3727 }
3728
3729 extern void l2_efp_filter_configure(vnet_main_t * vnet_main,
3730                                     u32           sw_if_index,
3731                                     u32           enable);
3732
3733 static void
3734 vl_api_l2_interface_efp_filter_t_handler (vl_api_l2_interface_efp_filter_t *mp)
3735 {
3736     int rv;
3737     vl_api_l2_interface_efp_filter_reply_t * rmp;
3738     vnet_main_t *vnm = vnet_get_main();
3739
3740     // enable/disable the feature
3741     l2_efp_filter_configure (vnm, mp->sw_if_index, mp->enable_disable);
3742     rv = vnm->api_errno;
3743
3744     REPLY_MACRO(VL_API_L2_INTERFACE_EFP_FILTER_REPLY);
3745 }
3746
3747 static void
3748 vl_api_l2_interface_vlan_tag_rewrite_t_handler (vl_api_l2_interface_vlan_tag_rewrite_t *mp)
3749 {
3750     int rv = 0;
3751     vl_api_l2_interface_vlan_tag_rewrite_reply_t * rmp;
3752     vnet_main_t * vnm = vnet_get_main();
3753     vlib_main_t * vm = vlib_get_main();
3754     u32 vtr_op;
3755
3756     VALIDATE_SW_IF_INDEX(mp);
3757
3758     vtr_op = ntohl(mp->vtr_op);
3759
3760     /* The L2 code is unsuspicious */
3761     switch(vtr_op) {
3762     case L2_VTR_DISABLED:
3763     case L2_VTR_PUSH_1:
3764     case L2_VTR_PUSH_2:
3765     case L2_VTR_POP_1:
3766     case L2_VTR_POP_2:
3767     case L2_VTR_TRANSLATE_1_1:
3768     case L2_VTR_TRANSLATE_1_2:
3769     case L2_VTR_TRANSLATE_2_1:
3770     case L2_VTR_TRANSLATE_2_2:
3771         break;
3772
3773     default:
3774         rv = VNET_API_ERROR_INVALID_VALUE;
3775         goto bad_sw_if_index;
3776     }
3777
3778     rv = l2vtr_configure (vm, vnm, ntohl(mp->sw_if_index), vtr_op,
3779                           ntohl(mp->push_dot1q), ntohl(mp->tag1),
3780                           ntohl(mp->tag2));
3781
3782     BAD_SW_IF_INDEX_LABEL;
3783
3784     REPLY_MACRO(VL_API_L2_INTERFACE_VLAN_TAG_REWRITE_REPLY);
3785 }
3786
3787 static void
3788 vl_api_create_vhost_user_if_t_handler (vl_api_create_vhost_user_if_t *mp)
3789 {
3790 #if DPDK > 0
3791     int rv = 0;
3792     vl_api_create_vhost_user_if_reply_t * rmp;
3793     u32 sw_if_index = (u32)~0;
3794
3795     vnet_main_t * vnm = vnet_get_main();
3796     vlib_main_t * vm = vlib_get_main();
3797
3798     rv = dpdk_vhost_user_create_if(vnm, vm, (char *)mp->sock_filename,
3799                               mp->is_server, &sw_if_index, (u64)~0,
3800                               mp->renumber, ntohl(mp->custom_dev_instance),
3801                               (mp->use_custom_mac)?mp->mac_address:NULL);
3802
3803     REPLY_MACRO2(VL_API_CREATE_VHOST_USER_IF_REPLY,
3804     ({
3805       rmp->sw_if_index = ntohl (sw_if_index);
3806     }));
3807 #endif
3808 }
3809
3810 static void
3811 vl_api_modify_vhost_user_if_t_handler (vl_api_modify_vhost_user_if_t *mp)
3812 {
3813 #if DPDK > 0
3814     int rv = 0;
3815     vl_api_modify_vhost_user_if_reply_t * rmp;
3816     u32 sw_if_index = ntohl(mp->sw_if_index);
3817
3818     vnet_main_t * vnm = vnet_get_main();
3819     vlib_main_t * vm = vlib_get_main();
3820
3821     rv = dpdk_vhost_user_modify_if(vnm, vm, (char *)mp->sock_filename,
3822                               mp->is_server, sw_if_index, (u64)~0,
3823                               mp->renumber, ntohl(mp->custom_dev_instance));
3824
3825     REPLY_MACRO(VL_API_MODIFY_VHOST_USER_IF_REPLY);
3826 #endif
3827 }
3828
3829 static void
3830 vl_api_delete_vhost_user_if_t_handler (vl_api_delete_vhost_user_if_t *mp)
3831 {
3832 #if DPDK > 0
3833     int rv = 0;
3834     vpe_api_main_t * vam = &vpe_api_main;
3835     vl_api_delete_vhost_user_if_reply_t * rmp;
3836     u32 sw_if_index = ntohl(mp->sw_if_index);
3837
3838     vnet_main_t * vnm = vnet_get_main();
3839     vlib_main_t * vm = vlib_get_main();
3840
3841     rv = dpdk_vhost_user_delete_if(vnm, vm, sw_if_index);
3842
3843     REPLY_MACRO(VL_API_DELETE_VHOST_USER_IF_REPLY);
3844     if (!rv) {
3845         unix_shared_memory_queue_t * q =
3846             vl_api_client_index_to_input_queue (mp->client_index);
3847         if (!q)
3848             return;
3849
3850         send_sw_interface_flags_deleted (vam, q, sw_if_index);
3851     }
3852 #endif
3853 }
3854
3855 static void vl_api_sw_interface_vhost_user_details_t_handler (
3856     vl_api_sw_interface_vhost_user_details_t * mp)
3857 {
3858     clib_warning ("BUG");
3859 }
3860
3861 #if DPDK > 0
3862 static void send_sw_interface_vhost_user_details (vpe_api_main_t * am,
3863                                        unix_shared_memory_queue_t *q,
3864                                        vhost_user_intf_details_t * vui,
3865                                        u32 context)
3866 {
3867     vl_api_sw_interface_vhost_user_details_t * mp;
3868
3869     mp = vl_msg_api_alloc (sizeof (*mp));
3870     memset (mp, 0, sizeof (*mp));
3871     mp->_vl_msg_id = ntohs(VL_API_SW_INTERFACE_VHOST_USER_DETAILS);
3872     mp->sw_if_index = ntohl(vui->sw_if_index);
3873     mp->virtio_net_hdr_sz = ntohl (vui->virtio_net_hdr_sz);
3874     mp->features = clib_net_to_host_u64 (vui->features);
3875     mp->is_server = vui->is_server;
3876     mp->num_regions = ntohl(vui->num_regions);
3877     mp->sock_errno = ntohl(vui->sock_errno);
3878     mp->context = context;
3879
3880     strncpy ((char *) mp->sock_filename,
3881              (char *) vui->sock_filename, ARRAY_LEN(mp->sock_filename)-1);
3882     strncpy ((char *) mp->interface_name,
3883              (char *) vui->if_name, ARRAY_LEN(mp->interface_name)-1);
3884
3885     vl_msg_api_send_shmem (q, (u8 *)&mp);
3886 }
3887 #endif
3888
3889 static void
3890 vl_api_sw_interface_vhost_user_dump_t_handler (
3891         vl_api_sw_interface_vhost_user_dump_t *mp)
3892 {
3893 #if DPDK > 0
3894     int rv = 0;
3895     vpe_api_main_t * am = &vpe_api_main;
3896     vnet_main_t * vnm = vnet_get_main();
3897     vlib_main_t * vm = vlib_get_main();
3898     vhost_user_intf_details_t *ifaces = NULL;
3899     vhost_user_intf_details_t *vuid = NULL;
3900     unix_shared_memory_queue_t * q;
3901
3902     q = vl_api_client_index_to_input_queue (mp->client_index);
3903     if (q == 0)
3904         return;
3905
3906     rv = dpdk_vhost_user_dump_ifs(vnm, vm, &ifaces);
3907     if (rv)
3908         return;
3909
3910     vec_foreach (vuid, ifaces) {
3911         send_sw_interface_vhost_user_details (am, q, vuid, mp->context);
3912     }
3913     vec_free(ifaces);
3914 #endif
3915 }
3916
3917 static void send_sw_if_l2tpv3_tunnel_details (vpe_api_main_t * am,
3918                                        unix_shared_memory_queue_t *q,
3919                                        l2t_session_t *s,
3920                                        l2t_main_t * lm,
3921                                        u32 context)
3922 {
3923     vl_api_sw_if_l2tpv3_tunnel_details_t * mp;
3924     u8 * if_name = NULL;
3925     vnet_sw_interface_t * si = NULL;
3926
3927     si = vnet_get_hw_sw_interface (lm->vnet_main, s->hw_if_index);
3928
3929     if_name = format(if_name, "%U",
3930                      format_vnet_sw_interface_name, lm->vnet_main, si);
3931
3932     mp = vl_msg_api_alloc (sizeof (*mp));
3933     memset (mp, 0, sizeof (*mp));
3934     mp->_vl_msg_id = ntohs(VL_API_SW_IF_L2TPV3_TUNNEL_DETAILS);
3935     strncpy((char *)mp->interface_name,
3936             (char *)if_name, ARRAY_LEN(mp->interface_name)-1);
3937     mp->sw_if_index = ntohl(si->sw_if_index);
3938     mp->local_session_id = s->local_session_id;
3939     mp->remote_session_id = s->remote_session_id;
3940     mp->local_cookie[0] = s->local_cookie[0];
3941     mp->local_cookie[1] = s->local_cookie[1];
3942     mp->remote_cookie = s->remote_cookie;
3943     clib_memcpy(mp->client_address, &s->client_address, sizeof(s->client_address));
3944     clib_memcpy(mp->our_address, &s->our_address, sizeof(s->our_address));
3945     mp->l2_sublayer_present = s->l2_sublayer_present;
3946     mp->context = context;
3947
3948     vl_msg_api_send_shmem (q, (u8 *)&mp);
3949 }
3950
3951 static void send_ip_address_details (vpe_api_main_t * am,
3952                                      unix_shared_memory_queue_t * q,
3953                                      u8 * ip,
3954                                      u16 prefix_length,
3955                                      u8 is_ipv6,
3956                                      u32 context)
3957 {
3958     vl_api_ip_address_details_t * mp;
3959
3960     mp = vl_msg_api_alloc (sizeof (*mp));
3961     memset (mp, 0, sizeof (*mp));
3962     mp->_vl_msg_id = ntohs(VL_API_IP_ADDRESS_DETAILS);
3963
3964     if (is_ipv6) {
3965         clib_memcpy(&mp->ip, ip, sizeof(mp->ip));
3966     } else {
3967         u32 * tp = (u32 *)mp->ip;
3968         *tp = ntohl(*(u32*)ip);
3969     }
3970     mp->prefix_length = prefix_length;
3971     mp->context = context;
3972
3973     vl_msg_api_send_shmem (q, (u8 *)&mp);
3974 }
3975
3976 static void
3977 vl_api_ip_address_dump_t_handler (vl_api_ip_address_dump_t *mp)
3978 {
3979     vpe_api_main_t * am = &vpe_api_main;
3980     unix_shared_memory_queue_t * q;
3981     ip6_address_t * r6;
3982     ip4_address_t * r4;
3983     ip6_main_t * im6 = &ip6_main;
3984     ip4_main_t * im4 = &ip4_main;
3985     ip_lookup_main_t * lm6 = &im6->lookup_main;
3986     ip_lookup_main_t * lm4 = &im4->lookup_main;
3987     ip_interface_address_t * ia = 0;
3988     u32 sw_if_index = ~0;
3989     int rv __attribute__ ((unused)) = 0;
3990
3991     VALIDATE_SW_IF_INDEX(mp);
3992
3993     sw_if_index = ntohl(mp->sw_if_index);
3994
3995     q = vl_api_client_index_to_input_queue (mp->client_index);
3996     if (q == 0) {
3997         return;
3998     }
3999
4000     if (mp->is_ipv6) {
4001         foreach_ip_interface_address (lm6, ia, sw_if_index,
4002                                       1 /* honor unnumbered */,
4003         ({
4004             r6 = ip_interface_address_get_address (lm6, ia);
4005             u16 prefix_length = ia->address_length;
4006             send_ip_address_details(am, q, (u8*)r6, prefix_length, 1, mp->context);
4007         }));
4008     } else {
4009         foreach_ip_interface_address (lm4, ia, sw_if_index,
4010                                       1 /* honor unnumbered */,
4011         ({
4012             r4 = ip_interface_address_get_address (lm4, ia);
4013             u16 prefix_length = ia->address_length;
4014             send_ip_address_details(am, q, (u8*)r4, prefix_length, 0, mp->context);
4015         }));
4016     }
4017     BAD_SW_IF_INDEX_LABEL;
4018 }
4019
4020 static void send_ip_details (vpe_api_main_t * am,
4021                                unix_shared_memory_queue_t *q,
4022                                u32 sw_if_index,
4023                                u32 context)
4024 {
4025     vl_api_ip_details_t * mp;
4026
4027     mp = vl_msg_api_alloc (sizeof (*mp));
4028     memset (mp, 0, sizeof (*mp));
4029     mp->_vl_msg_id = ntohs(VL_API_IP_DETAILS);
4030
4031     mp->sw_if_index = ntohl(sw_if_index);
4032     mp->context = context;
4033
4034     vl_msg_api_send_shmem (q, (u8 *)&mp);
4035 }
4036
4037 static void
4038 vl_api_sw_if_l2tpv3_tunnel_dump_t_handler (
4039         vl_api_sw_if_l2tpv3_tunnel_dump_t *mp)
4040 {
4041     vpe_api_main_t * am = &vpe_api_main;
4042     l2t_main_t * lm = &l2t_main;
4043     unix_shared_memory_queue_t * q;
4044     l2t_session_t *session;
4045
4046     q = vl_api_client_index_to_input_queue (mp->client_index);
4047     if (q == 0)
4048         return;
4049
4050     pool_foreach (session, lm->sessions,
4051     ({
4052         send_sw_if_l2tpv3_tunnel_details (am, q, session, lm, mp->context);
4053     }));
4054 }
4055
4056
4057 static void send_sw_interface_tap_details (vpe_api_main_t * am,
4058                                        unix_shared_memory_queue_t *q,
4059                                        tapcli_interface_details_t *tap_if,
4060                                        u32 context)
4061 {
4062     vl_api_sw_interface_tap_details_t * mp;
4063     mp = vl_msg_api_alloc (sizeof (*mp));
4064     memset (mp, 0, sizeof (*mp));
4065     mp->_vl_msg_id = ntohs(VL_API_SW_INTERFACE_TAP_DETAILS);
4066     mp->sw_if_index = ntohl(tap_if->sw_if_index);
4067     strncpy((char *)mp->dev_name,
4068             (char *)tap_if->dev_name, ARRAY_LEN(mp->dev_name)-1);
4069     mp->context = context;
4070
4071     vl_msg_api_send_shmem (q, (u8 *)&mp);
4072 }
4073
4074 static void
4075 vl_api_sw_interface_tap_dump_t_handler (
4076         vl_api_sw_interface_tap_dump_t *mp)
4077 {
4078     int rv = 0;
4079     vpe_api_main_t * am = &vpe_api_main;
4080     unix_shared_memory_queue_t * q;
4081     tapcli_interface_details_t *tapifs = NULL;
4082     tapcli_interface_details_t *tap_if = NULL;
4083
4084     q = vl_api_client_index_to_input_queue (mp->client_index);
4085     if (q == 0)
4086         return;
4087
4088     rv = vnet_tap_dump_ifs(&tapifs);
4089     if (rv)
4090         return;
4091
4092     vec_foreach(tap_if, tapifs) {
4093         send_sw_interface_tap_details(am, q, tap_if, mp->context);
4094     }
4095
4096     vec_free(tapifs);
4097 }
4098
4099 static void
4100 vl_api_ip_dump_t_handler (vl_api_ip_dump_t *mp)
4101 {
4102     vpe_api_main_t * am = &vpe_api_main;
4103     vnet_main_t * vnm = vnet_get_main();
4104     vlib_main_t * vm = vlib_get_main();
4105     vnet_interface_main_t * im = &vnm->interface_main;
4106     unix_shared_memory_queue_t * q;
4107     vnet_sw_interface_t * si, * sorted_sis;
4108     u32 sw_if_index = ~0;
4109
4110     q = vl_api_client_index_to_input_queue (mp->client_index);
4111     if (q == 0) {
4112         return;
4113     }
4114
4115     /* Gather interfaces. */
4116     sorted_sis = vec_new (vnet_sw_interface_t, pool_elts (im->sw_interfaces));
4117     _vec_len (sorted_sis) = 0;
4118     pool_foreach (si, im->sw_interfaces, ({ vec_add1 (sorted_sis, si[0]); }));
4119
4120     vec_foreach (si, sorted_sis) {
4121         if (!(si->flags & VNET_SW_INTERFACE_FLAG_UNNUMBERED)) {
4122             if (mp->is_ipv6 && !ip6_interface_enabled(vm, si->sw_if_index)) {
4123                 continue;
4124             }
4125             sw_if_index = si->sw_if_index;
4126             send_ip_details(am, q, sw_if_index, mp->context);
4127         }
4128     }
4129 }
4130
4131 static void vl_api_l2_fib_table_entry_t_handler (
4132     vl_api_l2_fib_table_entry_t * mp)
4133 {
4134     clib_warning ("BUG");
4135 }
4136
4137 static void send_l2fib_table_entry (vpe_api_main_t * am,
4138                                     unix_shared_memory_queue_t *q,
4139                                     l2fib_entry_key_t * l2fe_key,
4140                                     l2fib_entry_result_t * l2fe_res,
4141                                     u32 context)
4142 {
4143     vl_api_l2_fib_table_entry_t * mp;
4144
4145     mp = vl_msg_api_alloc (sizeof (*mp));
4146     memset (mp, 0, sizeof (*mp));
4147     mp->_vl_msg_id = ntohs(VL_API_L2_FIB_TABLE_ENTRY);
4148
4149     mp->bd_id = ntohl(l2input_main.bd_configs[l2fe_key->fields.bd_index].bd_id);
4150
4151     mp->mac = l2fib_make_key (l2fe_key->fields.mac, 0);
4152     mp->sw_if_index = ntohl(l2fe_res->fields.sw_if_index);
4153     mp->static_mac = l2fe_res->fields.static_mac;
4154     mp->filter_mac = l2fe_res->fields.filter;
4155     mp->bvi_mac = l2fe_res->fields.bvi;
4156     mp->context = context;
4157
4158     vl_msg_api_send_shmem (q, (u8 *)&mp);
4159 }
4160
4161 static void
4162 vl_api_l2_fib_table_dump_t_handler (vl_api_l2_fib_table_dump_t *mp)
4163 {
4164     vpe_api_main_t * am = &vpe_api_main;
4165     bd_main_t * bdm = &bd_main;
4166     l2fib_entry_key_t *l2fe_key = NULL;
4167     l2fib_entry_result_t *l2fe_res = NULL;
4168     u32 ni, bd_id = ntohl (mp->bd_id);
4169     u32 bd_index;
4170     unix_shared_memory_queue_t * q;
4171     uword * p;
4172
4173     q = vl_api_client_index_to_input_queue (mp->client_index);
4174     if (q == 0)
4175         return;
4176
4177     /* see l2fib_table_dump: ~0 means "any" */
4178     if (bd_id == ~0)
4179         bd_index = ~0;
4180     else {
4181         p = hash_get (bdm->bd_index_by_bd_id, bd_id);
4182         if (p == 0)
4183             return;
4184
4185         bd_index = p[0];
4186     }
4187
4188     l2fib_table_dump (bd_index, &l2fe_key, &l2fe_res);
4189
4190     vec_foreach_index (ni, l2fe_key) {
4191         send_l2fib_table_entry (am, q, vec_elt_at_index(l2fe_key, ni),
4192                                 vec_elt_at_index(l2fe_res, ni), mp->context);
4193     }
4194     vec_free(l2fe_key);
4195     vec_free(l2fe_res);
4196 }
4197
4198 static void
4199 vl_api_show_version_t_handler (vl_api_show_version_t *mp)
4200 {
4201     vl_api_show_version_reply_t *rmp;
4202     int rv = 0;
4203     char * vpe_api_get_build_directory(void);
4204     char * vpe_api_get_version(void);
4205     char * vpe_api_get_build_date(void);
4206
4207     unix_shared_memory_queue_t * q =
4208         vl_api_client_index_to_input_queue (mp->client_index);
4209
4210     if (!q)
4211         return;
4212
4213     REPLY_MACRO2(VL_API_SHOW_VERSION_REPLY,
4214     ({
4215         strncpy ((char *) rmp->program, "vpe", ARRAY_LEN(rmp->program)-1);
4216         strncpy ((char *) rmp->build_directory, vpe_api_get_build_directory(),
4217                  ARRAY_LEN(rmp->build_directory)-1);
4218         strncpy ((char *) rmp->version, vpe_api_get_version(),
4219                  ARRAY_LEN(rmp->version)-1);
4220         strncpy ((char *) rmp->build_date, vpe_api_get_build_date(),
4221                  ARRAY_LEN(rmp->build_date)-1);
4222     }));
4223 }
4224
4225 static void vl_api_get_node_index_t_handler
4226 (vl_api_get_node_index_t * mp)
4227 {
4228     vlib_main_t * vm = vlib_get_main();
4229     vl_api_get_node_index_reply_t * rmp;
4230     vlib_node_t * n;
4231     int rv = 0;
4232     u32 node_index = ~0;
4233
4234     n = vlib_get_node_by_name (vm, mp->node_name);
4235
4236     if (n == 0)
4237         rv = VNET_API_ERROR_NO_SUCH_NODE;
4238     else
4239         node_index = n->index;
4240
4241     REPLY_MACRO2(VL_API_GET_NODE_INDEX_REPLY,
4242     ({
4243         rmp->node_index = ntohl(node_index);
4244     }))
4245 }
4246
4247 static void vl_api_get_next_index_t_handler
4248 (vl_api_get_next_index_t * mp)
4249 {
4250     vlib_main_t * vm = vlib_get_main();
4251     vl_api_get_next_index_reply_t * rmp;
4252     vlib_node_t * node, * next_node;
4253     int rv = 0;
4254     u32 next_node_index = ~0, next_index = ~0;
4255     uword * p;
4256
4257     node = vlib_get_node_by_name (vm, mp->node_name);
4258
4259     if (node == 0) {
4260         rv = VNET_API_ERROR_NO_SUCH_NODE;
4261         goto out;
4262     }
4263
4264     next_node = vlib_get_node_by_name (vm, mp->next_name);
4265
4266     if (next_node == 0) {
4267         rv = VNET_API_ERROR_NO_SUCH_NODE2;
4268         goto out;
4269     }
4270     else
4271         next_node_index = next_node->index;
4272
4273     p = hash_get (node->next_slot_by_node, next_node_index);
4274
4275     if (p == 0) {
4276         rv = VNET_API_ERROR_NO_SUCH_ENTRY;
4277         goto out;
4278     }
4279     else
4280         next_index = p[0];
4281
4282  out:
4283     REPLY_MACRO2(VL_API_GET_NEXT_INDEX_REPLY,
4284     ({
4285         rmp->next_index = ntohl(next_index);
4286     }));
4287 }
4288
4289 static void vl_api_add_node_next_t_handler
4290 (vl_api_add_node_next_t * mp)
4291 {
4292     vlib_main_t * vm = vlib_get_main();
4293     vl_api_add_node_next_reply_t * rmp;
4294     vlib_node_t * n, * next;
4295     int rv = 0;
4296     u32 next_index = ~0;
4297
4298     n = vlib_get_node_by_name (vm, mp->node_name);
4299
4300     if (n == 0) {
4301         rv = VNET_API_ERROR_NO_SUCH_NODE;
4302         goto out;
4303     }
4304
4305     next = vlib_get_node_by_name (vm, mp->next_name);
4306
4307     if (next == 0)
4308         rv = VNET_API_ERROR_NO_SUCH_NODE2;
4309     else
4310         next_index = vlib_node_add_next (vm, n->index, next->index);
4311
4312 out:
4313     REPLY_MACRO2(VL_API_GET_NODE_INDEX_REPLY,
4314     ({
4315         rmp->next_index = ntohl(next_index);
4316     }))
4317 }
4318
4319 static void vl_api_l2tpv3_create_tunnel_t_handler
4320 (vl_api_l2tpv3_create_tunnel_t *mp)
4321 {
4322     vl_api_l2tpv3_create_tunnel_reply_t * rmp;
4323     l2t_main_t *lm = &l2t_main;
4324     u32 sw_if_index = (u32)~0;
4325     int rv;
4326
4327     if (mp->is_ipv6 != 1) {
4328         rv = VNET_API_ERROR_UNIMPLEMENTED;
4329         goto out;
4330     }
4331
4332     rv = create_l2tpv3_ipv6_tunnel (lm,
4333                                (ip6_address_t *) mp->client_address,
4334                                (ip6_address_t *) mp->our_address,
4335                                ntohl(mp->local_session_id),
4336                                ntohl(mp->remote_session_id),
4337                                clib_net_to_host_u64(mp->local_cookie),
4338                                clib_net_to_host_u64(mp->remote_cookie),
4339                                mp->l2_sublayer_present,
4340                                &sw_if_index);
4341
4342 out:
4343     REPLY_MACRO2(VL_API_L2TPV3_CREATE_TUNNEL_REPLY,
4344     ({
4345         rmp->sw_if_index = ntohl (sw_if_index);
4346     }))
4347 }
4348
4349 static void vl_api_l2tpv3_set_tunnel_cookies_t_handler
4350 (vl_api_l2tpv3_set_tunnel_cookies_t *mp)
4351 {
4352     vl_api_l2tpv3_set_tunnel_cookies_reply_t * rmp;
4353     l2t_main_t *lm = &l2t_main;
4354     int rv;
4355
4356     VALIDATE_SW_IF_INDEX(mp);
4357
4358     rv = l2tpv3_set_tunnel_cookies (lm, ntohl(mp->sw_if_index),
4359                                   clib_net_to_host_u64(mp->new_local_cookie),
4360                                   clib_net_to_host_u64(mp->new_remote_cookie));
4361
4362     BAD_SW_IF_INDEX_LABEL;
4363
4364     REPLY_MACRO (VL_API_L2TPV3_SET_TUNNEL_COOKIES_REPLY);
4365 }
4366
4367 static void vl_api_l2tpv3_interface_enable_disable_t_handler
4368 (vl_api_l2tpv3_interface_enable_disable_t * mp)
4369 {
4370     int rv;
4371     vnet_main_t * vnm = vnet_get_main();
4372     vl_api_l2tpv3_interface_enable_disable_reply_t * rmp;
4373
4374     VALIDATE_SW_IF_INDEX(mp);
4375
4376     rv = l2tpv3_interface_enable_disable
4377         (vnm, ntohl(mp->sw_if_index), mp->enable_disable);
4378
4379     BAD_SW_IF_INDEX_LABEL;
4380
4381     REPLY_MACRO (VL_API_L2TPV3_INTERFACE_ENABLE_DISABLE_REPLY);
4382 }
4383
4384 static void vl_api_l2tpv3_set_lookup_key_t_handler
4385 (vl_api_l2tpv3_set_lookup_key_t * mp)
4386 {
4387     int rv = 0;
4388     l2t_main_t *lm = &l2t_main;
4389     vl_api_l2tpv3_set_lookup_key_reply_t * rmp;
4390
4391     if (mp->key > L2T_LOOKUP_SESSION_ID) {
4392         rv = VNET_API_ERROR_INVALID_VALUE;
4393         goto out;
4394     }
4395
4396     lm->lookup_type = mp->key;
4397
4398 out:
4399     REPLY_MACRO (VL_API_L2TPV3_SET_LOOKUP_KEY_REPLY);
4400 }
4401
4402 static void vl_api_vxlan_add_del_tunnel_t_handler
4403 (vl_api_vxlan_add_del_tunnel_t * mp)
4404 {
4405     vl_api_vxlan_add_del_tunnel_reply_t * rmp;
4406     int rv = 0;
4407     vnet_vxlan_add_del_tunnel_args_t _a, *a = &_a;
4408     u32 encap_fib_index;
4409     uword * p;
4410     ip4_main_t * im = &ip4_main;
4411     u32 sw_if_index = ~0;
4412
4413     p = hash_get (im->fib_index_by_table_id, ntohl(mp->encap_vrf_id));
4414     if (! p) {
4415         rv = VNET_API_ERROR_NO_SUCH_FIB;
4416         goto out;
4417     }
4418     encap_fib_index = p[0];
4419
4420     /* Check src & dst are different */
4421     if ((mp->is_ipv6 && memcmp(mp->src_address, mp->dst_address, 16) == 0) ||
4422        (!mp->is_ipv6 && memcmp(mp->src_address, mp->dst_address, 4) == 0)) {
4423         rv = VNET_API_ERROR_SAME_SRC_DST;
4424         goto out;
4425     }
4426     memset (a, 0, sizeof (*a));
4427
4428     a->is_add = mp->is_add;
4429     a->is_ip6 = mp->is_ipv6;
4430
4431     /* ip addresses sent in network byte order */
4432     if (a->is_ip6) {
4433         memcpy(&(a->src.ip6), mp->src_address, 16);
4434         memcpy(&(a->dst.ip6), mp->dst_address, 16);
4435     } else {
4436         memcpy(&(a->src.ip4), mp->src_address, 4);
4437         memcpy(&(a->dst.ip4), mp->dst_address, 4);
4438     }
4439
4440     a->encap_fib_index = encap_fib_index;
4441     a->decap_next_index = ntohl(mp->decap_next_index);
4442     a->vni = ntohl(mp->vni);
4443     rv = vnet_vxlan_add_del_tunnel (a, &sw_if_index);
4444
4445 out:
4446     REPLY_MACRO2(VL_API_VXLAN_ADD_DEL_TUNNEL_REPLY,
4447     ({
4448         rmp->sw_if_index = ntohl (sw_if_index);
4449     }));
4450 }
4451
4452 static void send_vxlan_tunnel_details
4453 (vxlan_tunnel_t * t, unix_shared_memory_queue_t * q, u32 context)
4454 {
4455     vl_api_vxlan_tunnel_details_t * rmp;
4456     ip4_main_t * im4 = &ip4_main;
4457     ip6_main_t * im6 = &ip6_main;
4458     u8 is_ipv6 = !(t->flags & VXLAN_TUNNEL_IS_IPV4);
4459
4460     rmp = vl_msg_api_alloc (sizeof (*rmp));
4461     memset (rmp, 0, sizeof (*rmp));
4462     rmp->_vl_msg_id = ntohs(VL_API_VXLAN_TUNNEL_DETAILS);
4463     if (is_ipv6) {
4464         memcpy(rmp->src_address, &(t->src.ip6), 16);
4465         memcpy(rmp->dst_address, &(t->dst.ip6), 16);
4466         rmp->encap_vrf_id = htonl(im6->fibs[t->encap_fib_index].table_id);
4467     } else {
4468         memcpy(rmp->src_address, &(t->src.ip4), 4);
4469         memcpy(rmp->dst_address, &(t->dst.ip4), 4);
4470         rmp->encap_vrf_id = htonl(im4->fibs[t->encap_fib_index].table_id);
4471     }
4472     rmp->vni = htonl(t->vni);
4473     rmp->decap_next_index = htonl(t->decap_next_index);
4474     rmp->sw_if_index = htonl(t->sw_if_index);
4475     rmp->is_ipv6 = is_ipv6;
4476     rmp->context = context;
4477
4478     vl_msg_api_send_shmem (q, (u8 *)&rmp);
4479 }
4480
4481 static void vl_api_vxlan_tunnel_dump_t_handler
4482 (vl_api_vxlan_tunnel_dump_t * mp)
4483 {
4484     unix_shared_memory_queue_t * q;
4485     vxlan_main_t * vxm = &vxlan_main;
4486     vxlan_tunnel_t * t;
4487     u32 sw_if_index;
4488
4489     q = vl_api_client_index_to_input_queue (mp->client_index);
4490     if (q == 0) {
4491         return;
4492     }
4493
4494     sw_if_index = ntohl(mp->sw_if_index);
4495
4496     if (~0 == sw_if_index) {
4497         pool_foreach (t, vxm->tunnels,
4498         ({
4499             send_vxlan_tunnel_details(t, q, mp->context);
4500         }));
4501     } else {
4502         if ((sw_if_index >= vec_len(vxm->tunnel_index_by_sw_if_index)) ||
4503                 (~0 == vxm->tunnel_index_by_sw_if_index[sw_if_index])) {
4504             return;
4505         }
4506         t = &vxm->tunnels[vxm->tunnel_index_by_sw_if_index[sw_if_index]];
4507         send_vxlan_tunnel_details(t, q, mp->context);
4508     }
4509 }
4510
4511 static void vl_api_gre_add_del_tunnel_t_handler
4512 (vl_api_gre_add_del_tunnel_t * mp)
4513 {
4514     vl_api_gre_add_del_tunnel_reply_t * rmp;
4515     int rv = 0;
4516     vnet_gre_add_del_tunnel_args_t _a, *a = &_a;
4517     u32 outer_table_id;
4518     uword * p;
4519     ip4_main_t * im = &ip4_main;
4520     u32 sw_if_index = ~0;
4521
4522     p = hash_get (im->fib_index_by_table_id, ntohl(mp->outer_table_id));
4523     if (! p) {
4524         rv = VNET_API_ERROR_NO_SUCH_FIB;
4525         goto out;
4526     }
4527     outer_table_id = p[0];
4528
4529     /* Check src & dst are different */
4530     if (memcmp(&mp->src_address, &mp->dst_address, 4) == 0) {
4531         rv = VNET_API_ERROR_SAME_SRC_DST;
4532         goto out;
4533     }
4534
4535     memset (a, 0, sizeof (*a));
4536
4537     a->is_add = mp->is_add;
4538
4539     /* ip addresses sent in network byte order */
4540     a->src.as_u32 = mp->src_address;
4541     a->dst.as_u32 = mp->dst_address;
4542
4543     a->outer_table_id = outer_table_id;
4544     rv = vnet_gre_add_del_tunnel (a, &sw_if_index);
4545
4546 out:
4547     REPLY_MACRO2(VL_API_GRE_ADD_DEL_TUNNEL_REPLY,
4548     ({
4549         rmp->sw_if_index = ntohl (sw_if_index);
4550     }));
4551 }
4552
4553 static void send_gre_tunnel_details
4554 (gre_tunnel_t * t, unix_shared_memory_queue_t * q, u32 context)
4555 {
4556     vl_api_gre_tunnel_details_t * rmp;
4557     ip4_main_t * im = &ip4_main;
4558
4559     rmp = vl_msg_api_alloc (sizeof (*rmp));
4560     memset (rmp, 0, sizeof (*rmp));
4561     rmp->_vl_msg_id = ntohs(VL_API_GRE_TUNNEL_DETAILS);
4562     rmp->src_address = t->tunnel_src.data_u32;
4563     rmp->dst_address = t->tunnel_dst.data_u32;
4564     rmp->outer_table_id = htonl(im->fibs[t->outer_fib_index].table_id);
4565     rmp->sw_if_index = htonl(t->sw_if_index);
4566     rmp->context = context;
4567
4568     vl_msg_api_send_shmem (q, (u8 *)&rmp);
4569 }
4570
4571 static void vl_api_gre_tunnel_dump_t_handler
4572 (vl_api_gre_tunnel_dump_t * mp)
4573 {
4574     unix_shared_memory_queue_t * q;
4575     gre_main_t * gm = &gre_main;
4576     gre_tunnel_t * t;
4577     u32 sw_if_index;
4578
4579     q = vl_api_client_index_to_input_queue (mp->client_index);
4580     if (q == 0) {
4581         return;
4582     }
4583
4584     sw_if_index = ntohl(mp->sw_if_index);
4585
4586     if (~0 == sw_if_index) {
4587         pool_foreach (t, gm->tunnels,
4588         ({
4589             send_gre_tunnel_details(t, q, mp->context);
4590         }));
4591     } else {
4592         if ((sw_if_index >= vec_len(gm->tunnel_index_by_sw_if_index)) ||
4593                 (~0 == gm->tunnel_index_by_sw_if_index[sw_if_index])) {
4594             return;
4595         }
4596         t = &gm->tunnels[gm->tunnel_index_by_sw_if_index[sw_if_index]];
4597         send_gre_tunnel_details(t, q, mp->context);
4598     }
4599 }
4600
4601 static void
4602 vl_api_l2_patch_add_del_t_handler (vl_api_l2_patch_add_del_t *mp)
4603 {
4604     extern int vnet_l2_patch_add_del (u32 rx_sw_if_index, u32 tx_sw_if_index,
4605                                       int is_add);
4606     vl_api_l2_patch_add_del_reply_t * rmp;
4607     int vnet_l2_patch_add_del (u32 rx_sw_if_index, u32 tx_sw_if_index,
4608                                int is_add);
4609     int rv = 0;
4610
4611     VALIDATE_RX_SW_IF_INDEX(mp);
4612     VALIDATE_TX_SW_IF_INDEX(mp);
4613
4614     rv = vnet_l2_patch_add_del (ntohl(mp->rx_sw_if_index),
4615                                 ntohl(mp->tx_sw_if_index),
4616                                 (int)(mp->is_add != 0));
4617
4618     BAD_RX_SW_IF_INDEX_LABEL;
4619     BAD_TX_SW_IF_INDEX_LABEL;
4620
4621     REPLY_MACRO(VL_API_L2_PATCH_ADD_DEL_REPLY);
4622 }
4623
4624 static void
4625 vl_api_vxlan_gpe_add_del_tunnel_t_handler
4626 (vl_api_vxlan_gpe_add_del_tunnel_t * mp)
4627 {
4628     vl_api_vxlan_gpe_add_del_tunnel_reply_t * rmp;
4629     int rv = 0;
4630     vnet_vxlan_gpe_add_del_tunnel_args_t _a, *a = &_a;
4631     u32 encap_fib_index, decap_fib_index;
4632     u8 protocol;
4633     uword * p;
4634     ip4_main_t * im = &ip4_main;
4635     u32 sw_if_index = ~0;
4636
4637
4638     p = hash_get (im->fib_index_by_table_id, ntohl(mp->encap_vrf_id));
4639     if (! p) {
4640         rv = VNET_API_ERROR_NO_SUCH_FIB;
4641         goto out;
4642     }
4643     encap_fib_index = p[0];
4644
4645     protocol = mp->protocol;
4646
4647     /* Interpret decap_vrf_id as an opaque if sending to other-than-ip4-input */
4648     if (protocol == VXLAN_GPE_INPUT_NEXT_IP4_INPUT) {
4649         p = hash_get (im->fib_index_by_table_id, ntohl(mp->decap_vrf_id));
4650         if (! p) {
4651             rv = VNET_API_ERROR_NO_SUCH_INNER_FIB;
4652             goto out;
4653         }
4654         decap_fib_index = p[0];
4655     } else {
4656         decap_fib_index = ntohl(mp->decap_vrf_id);
4657     }
4658
4659     /* Check src & dst are different */
4660     if ((mp->is_ipv6 && memcmp(mp->local, mp->remote, 16) == 0) ||
4661        (!mp->is_ipv6 && memcmp(mp->local, mp->remote, 4) == 0)) {
4662         rv = VNET_API_ERROR_SAME_SRC_DST;
4663         goto out;
4664     }
4665     memset (a, 0, sizeof (*a));
4666
4667     a->is_add = mp->is_add;
4668     a->is_ip6 = mp->is_ipv6;
4669     /* ip addresses sent in network byte order */
4670     if (a->is_ip6) {
4671       clib_memcpy(&(a->local.ip6), mp->local, 16);
4672       clib_memcpy(&(a->remote.ip6), mp->remote, 16);
4673     } else {
4674       clib_memcpy(&(a->local.ip4), mp->local, 4);
4675       clib_memcpy(&(a->remote.ip4), mp->remote, 4);
4676     }
4677     a->encap_fib_index = encap_fib_index;
4678     a->decap_fib_index = decap_fib_index;
4679     a->protocol = protocol;
4680     a->vni = ntohl(mp->vni);
4681     rv = vnet_vxlan_gpe_add_del_tunnel (a, &sw_if_index);
4682
4683 out:
4684     REPLY_MACRO2(VL_API_VXLAN_GPE_ADD_DEL_TUNNEL_REPLY,
4685     ({
4686         rmp->sw_if_index = ntohl (sw_if_index);
4687     }));
4688 }
4689
4690 static void send_vxlan_gpe_tunnel_details
4691 (vxlan_gpe_tunnel_t * t, unix_shared_memory_queue_t * q, u32 context)
4692 {
4693     vl_api_vxlan_gpe_tunnel_details_t * rmp;
4694     ip4_main_t * im4 = &ip4_main;
4695     ip6_main_t * im6 = &ip6_main;
4696     u8 is_ipv6 = !(t->flags & VXLAN_GPE_TUNNEL_IS_IPV4);
4697
4698     rmp = vl_msg_api_alloc (sizeof (*rmp));
4699     memset (rmp, 0, sizeof (*rmp));
4700     rmp->_vl_msg_id = ntohs(VL_API_VXLAN_GPE_TUNNEL_DETAILS);
4701     if (is_ipv6) {
4702         memcpy(rmp->local, &(t->local.ip6), 16);
4703         memcpy(rmp->remote, &(t->remote.ip6), 16);
4704         rmp->encap_vrf_id = htonl(im6->fibs[t->encap_fib_index].table_id);
4705         rmp->decap_vrf_id = htonl(im6->fibs[t->decap_fib_index].table_id);
4706     } else {
4707         memcpy(rmp->local, &(t->local.ip4), 4);
4708         memcpy(rmp->remote, &(t->remote.ip4), 4);
4709         rmp->encap_vrf_id = htonl(im4->fibs[t->encap_fib_index].table_id);
4710         rmp->decap_vrf_id = htonl(im4->fibs[t->decap_fib_index].table_id);
4711     }
4712     rmp->vni = htonl(t->vni);
4713     rmp->protocol = t->protocol;
4714     rmp->sw_if_index = htonl(t->sw_if_index);
4715     rmp->is_ipv6 = is_ipv6;
4716     rmp->context = context;
4717
4718     vl_msg_api_send_shmem (q, (u8 *)&rmp);
4719 }
4720
4721 static void vl_api_vxlan_gpe_tunnel_dump_t_handler
4722 (vl_api_vxlan_gpe_tunnel_dump_t * mp)
4723 {
4724     unix_shared_memory_queue_t * q;
4725     vxlan_gpe_main_t * vgm = &vxlan_gpe_main;
4726     vxlan_gpe_tunnel_t * t;
4727     u32 sw_if_index;
4728
4729     q = vl_api_client_index_to_input_queue (mp->client_index);
4730     if (q == 0) {
4731         return;
4732     }
4733
4734     sw_if_index = ntohl(mp->sw_if_index);
4735
4736     if (~0 == sw_if_index) {
4737         pool_foreach (t, vgm->tunnels,
4738         ({
4739             send_vxlan_gpe_tunnel_details(t, q, mp->context);
4740         }));
4741     } else {
4742         if ((sw_if_index >= vec_len(vgm->tunnel_index_by_sw_if_index)) ||
4743                 (~0 == vgm->tunnel_index_by_sw_if_index[sw_if_index])) {
4744             return;
4745         }
4746         t = &vgm->tunnels[vgm->tunnel_index_by_sw_if_index[sw_if_index]];
4747         send_vxlan_gpe_tunnel_details(t, q, mp->context);
4748     }
4749 }
4750
4751 static void
4752 vl_api_lisp_add_del_locator_set_t_handler(vl_api_lisp_add_del_locator_set_t *mp)
4753 {
4754     vl_api_lisp_add_del_locator_set_reply_t *rmp;
4755     int rv = 0;
4756     vnet_lisp_add_del_locator_set_args_t _a, *a = &_a;
4757     u32 ls_index = ~0;
4758     u8 *locator_name = NULL;
4759
4760     memset(a, 0, sizeof(a[0]));
4761
4762     locator_name = format(0, "%s", mp->locator_set_name);
4763
4764     a->name = locator_name;
4765     a->locators = NULL;
4766     a->is_add = mp->is_add;
4767     a->local = 1;
4768
4769     rv = vnet_lisp_add_del_locator_set(a, &ls_index);
4770
4771     vec_free(locator_name);
4772
4773     REPLY_MACRO(VL_API_LISP_ADD_DEL_LOCATOR_SET_REPLY);
4774 }
4775
4776 static void
4777 vl_api_lisp_add_del_locator_t_handler(
4778     vl_api_lisp_add_del_locator_t *mp)
4779 {
4780     vl_api_lisp_add_del_locator_reply_t *rmp;
4781     int rv = 0;
4782     locator_t locator, *locators = NULL;
4783     vnet_lisp_add_del_locator_set_args_t _a, *a = &_a;
4784     u32 ls_index = ~0;
4785     u8 *locator_name = NULL;
4786
4787     memset(&locator, 0, sizeof(locator));
4788     memset(a, 0, sizeof(a[0]));
4789
4790     locator.sw_if_index = ntohl(mp->sw_if_index);
4791     locator.priority = mp->priority;
4792     locator.weight = mp->weight;
4793     locator.local = 1;
4794     vec_add1(locators, locator);
4795
4796     locator_name = format(0, "%s", mp->locator_set_name);
4797
4798     a->name = locator_name;
4799     a->locators = locators;
4800     a->is_add = mp->is_add;
4801     a->local = 1;
4802
4803     rv = vnet_lisp_add_del_locator(a, NULL, &ls_index);
4804
4805     vec_free(locators);
4806     vec_free(locator_name);
4807
4808     REPLY_MACRO(VL_API_LISP_ADD_DEL_LOCATOR_REPLY);
4809 }
4810
4811 static void
4812 vl_api_lisp_add_del_local_eid_t_handler(
4813     vl_api_lisp_add_del_local_eid_t *mp)
4814 {
4815     vl_api_lisp_add_del_local_eid_reply_t *rmp;
4816     lisp_cp_main_t * lcm = vnet_lisp_cp_get_main();
4817     int rv = 0;
4818     ip_prefix_t  *prefp = NULL;
4819     ip_address_t *ip_eid = NULL;
4820     gid_address_t eid;
4821     u8 * mac = gid_address_mac (&eid);
4822     uword * p = NULL;
4823     u32 locator_set_index = ~0, map_index = ~0;
4824     vnet_lisp_add_del_mapping_args_t _a, *a = &_a;
4825     u8 *name = NULL;
4826     memset (a, 0, sizeof (a[0]));
4827     memset (&eid, 0, sizeof (eid));
4828
4829     prefp = &gid_address_ippref(&eid);
4830     ip_eid = &ip_prefix_addr(prefp);
4831
4832     switch (mp->eid_type)
4833       {
4834       case 0: /* ipv4*/
4835         gid_address_type (&eid) = GID_ADDR_IP_PREFIX;
4836         ip_address_set(ip_eid, mp->eid, IP4);
4837         ip_prefix_len(prefp) = mp->prefix_len;
4838         break;
4839       case 1: /* ipv6 */
4840         gid_address_type (&eid) = GID_ADDR_IP_PREFIX;
4841         ip_address_set(ip_eid, mp->eid, IP6);
4842         ip_prefix_len(prefp) = mp->prefix_len;
4843         break;
4844       case 2: /* l2 mac */
4845         gid_address_type (&eid) = GID_ADDR_MAC;
4846         clib_memcpy(mac, mp->eid, 6);
4847         break;
4848       default:
4849         rv = VNET_API_ERROR_INVALID_EID_TYPE;
4850         goto out;
4851       }
4852
4853     name = format(0, "%s", mp->locator_set_name);
4854     p = hash_get_mem(lcm->locator_set_index_by_name, name);
4855     if (!p) {
4856         rv = VNET_API_ERROR_INVALID_VALUE;
4857         goto out;
4858     }
4859     locator_set_index = p[0];
4860
4861   /* XXX treat batch configuration */
4862     a->is_add = mp->is_add;
4863     gid_address_vni (&eid) = clib_net_to_host_u32 (mp->vni);
4864     gid_address_copy (&a->eid, &eid);
4865     a->locator_set_index = locator_set_index;
4866     a->local = 1;
4867     rv = vnet_lisp_add_del_local_mapping(a, &map_index);
4868
4869 out:
4870     vec_free(name);
4871     gid_address_free (&a->eid);
4872
4873     REPLY_MACRO(VL_API_LISP_ADD_DEL_LOCAL_EID_REPLY);
4874 }
4875
4876 static void
4877 vl_api_lisp_eid_table_add_del_map_t_handler(
4878     vl_api_lisp_eid_table_add_del_map_t *mp)
4879 {
4880     vl_api_lisp_eid_table_add_del_map_reply_t *rmp;
4881     int rv = 0;
4882     rv = vnet_lisp_eid_table_map (clib_net_to_host_u32 (mp->vni),
4883                                   clib_net_to_host_u32 (mp->vrf), mp->is_add);
4884     REPLY_MACRO(VL_API_LISP_EID_TABLE_ADD_DEL_MAP_REPLY)
4885 }
4886
4887 static void
4888 lisp_gpe_add_del_fwd_entry_set_address(
4889     vl_api_lisp_gpe_add_del_fwd_entry_t *mp,
4890     ip_address_t                        *slocator,
4891     ip_address_t                        *dlocator,
4892     gid_address_t                       *eid)
4893 {
4894     ip_address_t *ip_eid = NULL;
4895     ip_prefix_t *prefp = NULL;
4896
4897     prefp = &gid_address_ippref(eid);
4898     ip_eid = &ip_prefix_addr(prefp);
4899
4900     if (mp->eid_is_ipv6) {
4901         clib_memcpy(&ip_addr_v6(ip_eid), mp->eid_ip_address,
4902                sizeof(ip_addr_v6(ip_eid)));
4903         ip_addr_version(ip_eid) = IP6;
4904     } else {
4905         clib_memcpy(&ip_addr_v4(ip_eid), mp->eid_ip_address,
4906                sizeof(ip_addr_v4(ip_eid)));
4907         ip_addr_version(ip_eid) = IP4;
4908     }
4909     ip_prefix_len(prefp) = mp->eid_prefix_len;
4910
4911     if (mp->address_is_ipv6) {
4912         clib_memcpy(&ip_addr_v6(slocator), mp->source_ip_address,
4913                sizeof(ip_addr_v6(slocator)));
4914         ip_addr_version(slocator) = IP6;
4915         clib_memcpy(&ip_addr_v6(dlocator), mp->destination_ip_address,
4916                sizeof(ip_addr_v6(dlocator)));
4917         ip_addr_version(dlocator) = IP6;
4918     } else {
4919         clib_memcpy(&ip_addr_v4(slocator), mp->source_ip_address,
4920                sizeof(ip_addr_v4(slocator)));
4921         ip_addr_version(slocator) = IP4;
4922         clib_memcpy(&ip_addr_v4(dlocator), mp->destination_ip_address,
4923                sizeof(ip_addr_v4(dlocator)));
4924         ip_addr_version(dlocator) = IP4;
4925     }
4926 }
4927
4928 static void
4929 vl_api_lisp_gpe_add_del_fwd_entry_t_handler(
4930     vl_api_lisp_gpe_add_del_fwd_entry_t *mp)
4931 {
4932     vl_api_lisp_gpe_add_del_fwd_entry_reply_t *rmp;
4933     int rv = 0;
4934     ip_address_t slocator, dlocator;
4935     gid_address_t eid;
4936     vnet_lisp_gpe_add_del_fwd_entry_args_t a;
4937
4938     lisp_gpe_add_del_fwd_entry_set_address(mp, &slocator, &dlocator, &eid);
4939
4940     memset (&a, 0, sizeof(a));
4941
4942     a.is_add = mp->is_add;
4943     a.deid = eid;
4944     a.slocator = slocator;
4945     a.dlocator = dlocator;
4946     rv = vnet_lisp_gpe_add_del_fwd_entry (&a, 0);
4947
4948     REPLY_MACRO(VL_API_LISP_GPE_ADD_DEL_FWD_ENTRY_REPLY);
4949 }
4950
4951 static void
4952 vl_api_lisp_add_del_map_resolver_t_handler(
4953     vl_api_lisp_add_del_map_resolver_t *mp)
4954 {
4955     vl_api_lisp_add_del_map_resolver_reply_t *rmp;
4956     int rv = 0;
4957     ip_address_t *ip_addr = NULL;
4958     vnet_lisp_add_del_map_resolver_args_t _a, * a = &_a;
4959
4960     a->is_add = mp->is_add;
4961     ip_addr = &a->address;
4962
4963     if (mp->is_ipv6) {
4964         clib_memcpy(&ip_addr_v6(ip_addr), mp->ip_address,
4965                sizeof(ip_addr_v6(ip_addr)));
4966         ip_addr_version(ip_addr) = IP6;
4967     } else {
4968         clib_memcpy(&ip_addr_v4(ip_addr), mp->ip_address,
4969                sizeof(ip_addr_v4(ip_addr)));
4970         ip_addr_version(ip_addr) = IP4;
4971     }
4972
4973     rv = vnet_lisp_add_del_map_resolver (a);
4974
4975     REPLY_MACRO(VL_API_LISP_ADD_DEL_MAP_RESOLVER_REPLY);
4976 }
4977
4978 static void
4979 vl_api_lisp_gpe_enable_disable_t_handler(
4980     vl_api_lisp_gpe_enable_disable_t *mp)
4981 {
4982     vl_api_lisp_gpe_enable_disable_reply_t *rmp;
4983     int rv = 0;
4984     vnet_lisp_gpe_enable_disable_args_t _a, * a = &_a;
4985
4986     a->is_en = mp->is_en;
4987     vnet_lisp_gpe_enable_disable (a);
4988
4989     REPLY_MACRO(VL_API_LISP_GPE_ENABLE_DISABLE_REPLY);
4990 }
4991
4992 static void
4993 vl_api_lisp_enable_disable_t_handler(
4994     vl_api_lisp_enable_disable_t *mp)
4995 {
4996     vl_api_lisp_enable_disable_reply_t *rmp;
4997     int rv = 0;
4998
4999     vnet_lisp_enable_disable (mp->is_en);
5000     REPLY_MACRO(VL_API_LISP_ENABLE_DISABLE_REPLY);
5001 }
5002
5003 static void
5004 vl_api_lisp_gpe_add_del_iface_t_handler(
5005     vl_api_lisp_gpe_add_del_iface_t *mp)
5006 {
5007     vl_api_lisp_gpe_add_del_iface_reply_t *rmp;
5008     int rv = 0;
5009     vnet_lisp_gpe_add_del_iface_args_t _a, * a = &_a;
5010
5011     a->is_add = mp->is_add;
5012     a->table_id = mp->table_id;
5013     a->vni = mp->vni;
5014     rv = vnet_lisp_gpe_add_del_iface (a, 0);
5015
5016     REPLY_MACRO(VL_API_LISP_GPE_ADD_DEL_IFACE_REPLY);
5017 }
5018
5019 static void
5020 vl_api_lisp_pitr_set_locator_set_t_handler(
5021   vl_api_lisp_pitr_set_locator_set_t *mp)
5022 {
5023     vl_api_lisp_pitr_set_locator_set_reply_t *rmp;
5024     int rv = 0;
5025     u8 * ls_name = 0;
5026
5027     ls_name = format (0, "%s", mp->ls_name);
5028     rv = vnet_lisp_pitr_set_locator_set (ls_name, mp->is_add);
5029     vec_free (ls_name);
5030
5031     REPLY_MACRO(VL_API_LISP_PITR_SET_LOCATOR_SET_REPLY);
5032 }
5033
5034 static void
5035 vl_api_lisp_add_del_map_request_itr_rlocs_t_handler
5036 (vl_api_lisp_add_del_map_request_itr_rlocs_t *mp)
5037 {
5038     vl_api_lisp_add_del_map_request_itr_rlocs_reply_t *rmp;
5039     int rv = 0;
5040     u8 * locator_set_name = NULL;
5041     vnet_lisp_add_del_mreq_itr_rloc_args_t _a, * a = &_a;
5042
5043     locator_set_name = format (0, "%s", mp->locator_set_name);
5044
5045     a->is_add = mp->is_add;
5046     a->locator_set_name = locator_set_name;
5047
5048     rv = vnet_lisp_add_del_mreq_itr_rlocs(a);
5049
5050     vec_free(locator_set_name);
5051
5052     REPLY_MACRO(VL_API_LISP_ADD_DEL_MAP_REQUEST_ITR_RLOCS_REPLY);
5053 }
5054
5055 /** Used for transferring locators via VPP API */
5056 typedef CLIB_PACKED(struct
5057 {
5058   u8 is_ip4; /**< is locator an IPv4 address */
5059   u8 priority; /**< locator priority */
5060   u8 weight;   /**< locator weight */
5061   u8 addr[16]; /**< IPv4/IPv6 address */
5062 }) rloc_t;
5063
5064 static void
5065 send_lisp_locator_set_details_set_address
5066 (vl_api_lisp_locator_set_details_t *rmp,
5067  gid_address_t *gid_address)
5068 {
5069     ip_prefix_t *ip_addr;
5070
5071     if (gid_address_type(gid_address) != GID_ADDR_IP_PREFIX) {
5072         return;
5073     }
5074
5075     ip_addr = &gid_address_ippref(gid_address);
5076     rmp->prefix_len = ip_prefix_len(ip_addr);
5077     rmp->is_ipv6 = ip_prefix_version(ip_addr);
5078     ip_address_copy_addr(rmp->ip_address, &ip_prefix_addr(ip_addr));
5079 }
5080
5081 static void
5082 vl_api_lisp_add_del_remote_mapping_t_handler (
5083     vl_api_lisp_add_del_remote_mapping_t *mp)
5084 {
5085     u32 i;
5086     locator_t rloc, * rlocs = 0;
5087     vl_api_lisp_add_del_remote_mapping_reply_t * rmp;
5088     int rv = 0;
5089     gid_address_t _seid, * seid = &_seid;
5090     gid_address_t _deid, * deid = &_deid;
5091     ip_prefix_t * seid_pref = &gid_address_ippref(seid);
5092     ip_prefix_t * deid_pref = &gid_address_ippref(deid);
5093
5094     /* TODO remove seid from API */
5095     memset (seid, 0, sizeof (seid[0]));
5096     memset (deid, 0, sizeof (deid[0]));
5097     ip_address_t * seid_addr = &ip_prefix_addr(seid_pref);
5098     ip_address_t * deid_addr = &ip_prefix_addr(deid_pref);
5099     ip_prefix_len(seid_pref) = mp->seid_len;
5100     ip_prefix_len(deid_pref) = mp->deid_len;
5101     u8 * seid_mac = gid_address_mac (seid);
5102     u8 * deid_mac = gid_address_mac (deid);
5103     gid_address_vni (seid) = ntohl (mp->vni);
5104     gid_address_vni (deid) = ntohl (mp->vni);
5105
5106     switch (mp->eid_type)
5107       {
5108       case 0: /* ipv4 */
5109         gid_address_type(seid) = GID_ADDR_IP_PREFIX;
5110         gid_address_type(deid) = GID_ADDR_IP_PREFIX;
5111         ip_address_set (seid_addr, mp->seid, IP4);
5112         ip_address_set (deid_addr, mp->deid, IP4);
5113         break;
5114       case 1: /* ipv6 */
5115         gid_address_type(seid) = GID_ADDR_IP_PREFIX;
5116         gid_address_type(deid) = GID_ADDR_IP_PREFIX;
5117         ip_address_set (seid_addr, mp->seid, IP6);
5118         ip_address_set (deid_addr, mp->deid, IP6);
5119         break;
5120       case 2: /* l2 mac */
5121         gid_address_type(seid) = GID_ADDR_MAC;
5122         gid_address_type(deid) = GID_ADDR_MAC;
5123         clib_memcpy (seid_mac, mp->seid, 6);
5124         clib_memcpy (deid_mac, mp->deid, 6);
5125         break;
5126       default:
5127         rv = VNET_API_ERROR_INVALID_EID_TYPE;
5128         goto send_reply;
5129       }
5130
5131     for (i = 0; i < mp->rloc_num; i++) {
5132         rloc_t * r = &((rloc_t *) mp->rlocs)[i];
5133         memset(&rloc, 0, sizeof(rloc));
5134         ip_address_set(&gid_address_ip(&rloc.address), &r->addr,
5135                        r->is_ip4 ? IP4 : IP6);
5136         gid_address_ippref_len(&rloc.address) = r->is_ip4 ? 32: 128;
5137         gid_address_type(&rloc.address) = GID_ADDR_IP_PREFIX;
5138         rloc.priority = r->priority;
5139         rloc.weight = r->weight;
5140         vec_add1 (rlocs, rloc);
5141     }
5142
5143     if (!mp->is_add) {
5144         vnet_lisp_add_del_adjacency_args_t _a, * a = &_a;
5145         gid_address_copy(&a->deid, deid);
5146         a->is_add = 0;
5147         rv = vnet_lisp_add_del_adjacency (a);
5148     } else {
5149         /* NOTE: for now this works as a static remote mapping, i.e.,
5150          * not authoritative and ttl infinite. */
5151         rv = vnet_lisp_add_del_mapping (deid, rlocs, mp->action, 0, ~0,
5152                                         mp->is_add, 0);
5153
5154         /* TODO remove once CSIT switched to lisp_add_del_adjacency */
5155         vnet_lisp_add_del_adjacency_args_t _a, * a = &_a;
5156         gid_address_copy(&a->seid, seid);
5157         gid_address_copy(&a->deid, deid);
5158         a->is_add = 1;
5159         vnet_lisp_add_del_adjacency (a);
5160     }
5161
5162     if (mp->del_all)
5163       vnet_lisp_clear_all_remote_adjacencies ();
5164
5165     vec_free (rlocs);
5166 send_reply:
5167     REPLY_MACRO(VL_API_LISP_ADD_DEL_REMOTE_MAPPING_REPLY);
5168 }
5169
5170 static void
5171 vl_api_lisp_add_del_adjacency_t_handler (
5172     vl_api_lisp_add_del_adjacency_t *mp)
5173 {
5174     vl_api_lisp_add_del_adjacency_reply_t * rmp;
5175     vnet_lisp_add_del_adjacency_args_t _a, * a = &_a;
5176
5177     int rv = 0;
5178     memset(a, 0, sizeof(a[0]));
5179
5180     ip_prefix_t * seid_pref = &gid_address_ippref(&a->seid);
5181     ip_prefix_t * deid_pref = &gid_address_ippref(&a->deid);
5182
5183     ip_address_t * seid_addr = &ip_prefix_addr(seid_pref);
5184     ip_address_t * deid_addr = &ip_prefix_addr(deid_pref);
5185     ip_prefix_len(seid_pref) = mp->seid_len;
5186     ip_prefix_len(deid_pref) = mp->deid_len;
5187     u8 * seid_mac = gid_address_mac (&a->seid);
5188     u8 * deid_mac = gid_address_mac (&a->deid);
5189     gid_address_vni(&a->seid) = ntohl (mp->vni);
5190     gid_address_vni(&a->deid) = ntohl (mp->vni);
5191
5192     switch (mp->eid_type)
5193       {
5194       case 0: /* ipv4 */
5195         gid_address_type(&a->seid) = GID_ADDR_IP_PREFIX;
5196         gid_address_type(&a->deid) = GID_ADDR_IP_PREFIX;
5197         ip_address_set (seid_addr, mp->seid, IP4);
5198         ip_address_set (deid_addr, mp->deid, IP4);
5199         break;
5200       case 1: /* ipv6 */
5201         gid_address_type(&a->seid) = GID_ADDR_IP_PREFIX;
5202         gid_address_type(&a->deid) = GID_ADDR_IP_PREFIX;
5203         ip_address_set (seid_addr, mp->seid, IP6);
5204         ip_address_set (deid_addr, mp->deid, IP6);
5205         break;
5206       case 2: /* l2 mac */
5207         gid_address_type(&a->seid) = GID_ADDR_MAC;
5208         gid_address_type(&a->deid) = GID_ADDR_MAC;
5209         clib_memcpy (seid_mac, mp->seid, 6);
5210         clib_memcpy (deid_mac, mp->deid, 6);
5211         break;
5212       default:
5213         rv = VNET_API_ERROR_INVALID_EID_TYPE;
5214         goto send_reply;
5215       }
5216
5217     a->is_add = mp->is_add;
5218     rv = vnet_lisp_add_del_adjacency (a);
5219
5220 send_reply:
5221     REPLY_MACRO(VL_API_LISP_ADD_DEL_ADJACENCY_REPLY);
5222 }
5223
5224 static void
5225 send_lisp_locator_set_details (lisp_cp_main_t *lcm,
5226                                locator_set_t *lsit,
5227                                unix_shared_memory_queue_t *q,
5228                                u32 context,
5229                                u32 index)
5230 {
5231     vl_api_lisp_locator_set_details_t *rmp;
5232     locator_t *loc = NULL;
5233     u32 * locit = NULL;
5234     u8 * str = NULL;
5235
5236     vec_foreach (locit, lsit->locator_indices) {
5237         loc = pool_elt_at_index (lcm->locator_pool, locit[0]);
5238         rmp = vl_msg_api_alloc (sizeof (*rmp));
5239         memset (rmp, 0, sizeof (*rmp));
5240         rmp->_vl_msg_id = ntohs(VL_API_LISP_LOCATOR_SET_DETAILS);
5241         rmp->local = lsit->local;
5242         if (lsit->local) {
5243             ASSERT(lsit->name != NULL);
5244             strncpy((char *) rmp->locator_set_name,
5245                     (char *) lsit->name, ARRAY_LEN(rmp->locator_set_name) - 1);
5246             rmp->sw_if_index = htonl(loc->sw_if_index);
5247         } else {
5248             str = format(0, "remote-%d", index);
5249             strncpy((char *) rmp->locator_set_name, (char *) str,
5250                     ARRAY_LEN(rmp->locator_set_name) - 1);
5251             send_lisp_locator_set_details_set_address(rmp, &loc->address);
5252
5253             vec_free(str);
5254         }
5255         rmp->priority = loc->priority;
5256         rmp->weight = loc->weight;
5257         rmp->context = context;
5258
5259         vl_msg_api_send_shmem (q, (u8 *)&rmp);
5260     }
5261 }
5262
5263 static void
5264 vl_api_lisp_locator_set_dump_t_handler (vl_api_lisp_locator_set_dump_t *mp)
5265 {
5266     unix_shared_memory_queue_t * q = NULL;
5267     lisp_cp_main_t * lcm = vnet_lisp_cp_get_main();
5268     locator_set_t * lsit = NULL;
5269     u32 index;
5270
5271     q = vl_api_client_index_to_input_queue (mp->client_index);
5272     if (q == 0) {
5273         return;
5274     }
5275
5276     index = 0;
5277     pool_foreach (lsit, lcm->locator_set_pool,
5278         ({
5279             send_lisp_locator_set_details(lcm, lsit, q, mp->context, index++);
5280         }));
5281 }
5282
5283 static void
5284 send_lisp_local_eid_table_details (mapping_t *mapit,
5285                                    unix_shared_memory_queue_t *q,
5286                                    u32 context)
5287 {
5288     vl_api_lisp_local_eid_table_details_t *rmp = NULL;
5289     lisp_cp_main_t * lcm = vnet_lisp_cp_get_main();
5290     locator_set_t *ls = NULL;
5291     gid_address_t *gid = NULL;
5292     u8 * mac = 0;
5293     ip_prefix_t *ip_prefix = NULL;
5294     u8 * str = NULL;
5295
5296     ls = pool_elt_at_index (lcm->locator_set_pool,
5297                             mapit->locator_set_index);
5298     gid = &mapit->eid;
5299     ip_prefix = &gid_address_ippref(gid);
5300     mac = gid_address_mac(gid);
5301
5302     rmp = vl_msg_api_alloc (sizeof (*rmp));
5303     memset (rmp, 0, sizeof (*rmp));
5304     rmp->_vl_msg_id = ntohs(VL_API_LISP_LOCAL_EID_TABLE_DETAILS);
5305     if (ls->local) {
5306         ASSERT(ls->name != NULL);
5307         strncpy((char *) rmp->locator_set_name,
5308                 (char *) ls->name, ARRAY_LEN(rmp->locator_set_name) - 1);
5309     } else {
5310             str = format(0, "remote-%d", mapit->locator_set_index);
5311             strncpy((char *) rmp->locator_set_name, (char *) str,
5312                     ARRAY_LEN(rmp->locator_set_name) - 1);
5313             vec_free(str);
5314     }
5315
5316     switch (gid_address_type (gid))
5317       {
5318       case GID_ADDR_IP_PREFIX:
5319         rmp->eid_prefix_len = ip_prefix_len(ip_prefix);
5320         if (ip_prefix_version(ip_prefix) == IP4)
5321           {
5322             rmp->eid_type = 0; /* ipv4 type */
5323             clib_memcpy(rmp->eid, &ip_prefix_v4(ip_prefix),
5324                    sizeof(ip_prefix_v4(ip_prefix)));
5325           }
5326         else
5327           {
5328             rmp->eid_type = 1; /* ipv6 type */
5329             clib_memcpy(rmp->eid, &ip_prefix_v6(ip_prefix),
5330                    sizeof(ip_prefix_v6(ip_prefix)));
5331           }
5332         break;
5333       case GID_ADDR_MAC:
5334         rmp->eid_type = 2; /* l2 mac type */
5335         clib_memcpy(rmp->eid, mac, 6);
5336         break;
5337       default:
5338         ASSERT(0);
5339       }
5340     rmp->context = context;
5341     rmp->vni = clib_host_to_net_u32 (gid_address_vni (gid));
5342     vl_msg_api_send_shmem (q, (u8 *)&rmp);
5343 }
5344
5345 static void
5346 vl_api_lisp_local_eid_table_dump_t_handler (
5347     vl_api_lisp_local_eid_table_dump_t *mp)
5348 {
5349     u32 mi;
5350     unix_shared_memory_queue_t * q = NULL;
5351     lisp_cp_main_t * lcm = vnet_lisp_cp_get_main();
5352     mapping_t * mapit = NULL;
5353     gid_address_t _eid, * eid = &_eid;
5354
5355     q = vl_api_client_index_to_input_queue (mp->client_index);
5356     if (q == 0) {
5357         return;
5358     }
5359
5360     if (mp->eid_set) {
5361         memset (eid, 0, sizeof (*eid));
5362         gid_address_vni(eid) = ntohl(mp->vni);
5363         switch (mp->eid_type) {
5364         case 0:
5365             gid_address_type(eid) = GID_ADDR_IP_PREFIX;
5366             gid_address_ippref_len(eid) = mp->prefix_length;
5367             gid_address_ip_version(eid) = IP4;
5368             clib_memcpy (&gid_address_ippref(eid), mp->eid, 4);
5369             break;
5370         case 1:
5371             gid_address_type(eid) = GID_ADDR_IP_PREFIX;
5372             gid_address_ippref_len(eid) = mp->prefix_length;
5373             gid_address_ip_version(eid) = IP6;
5374             clib_memcpy (&gid_address_ippref(eid), mp->eid, 16);
5375             break;
5376         case 2:
5377             gid_address_type(eid) = GID_ADDR_MAC;
5378             clib_memcpy (gid_address_mac(eid), mp->eid, 6);
5379             break;
5380         }
5381         mi = gid_dictionary_lookup (&lcm->mapping_index_by_gid, eid);
5382         if ((u32)~0 == mi)
5383           return;
5384
5385         mapit = pool_elt_at_index (lcm->mapping_pool, mi);
5386         send_lisp_local_eid_table_details(mapit, q, mp->context);
5387     } else {
5388         pool_foreach (mapit, lcm->mapping_pool,
5389         ({
5390             send_lisp_local_eid_table_details(mapit, q, mp->context);
5391         }));
5392     }
5393 }
5394
5395 static void
5396 send_lisp_gpe_tunnel_details (lisp_gpe_tunnel_t *tunnel,
5397                               unix_shared_memory_queue_t *q,
5398                               u32 context)
5399 {
5400     vl_api_lisp_gpe_tunnel_details_t *rmp;
5401     lisp_gpe_main_t * lgm = &lisp_gpe_main;
5402
5403     rmp = vl_msg_api_alloc (sizeof (*rmp));
5404     memset (rmp, 0, sizeof (*rmp));
5405     rmp->_vl_msg_id = ntohs(VL_API_LISP_GPE_TUNNEL_DETAILS);
5406
5407     rmp->tunnels = tunnel - lgm->tunnels;
5408
5409     rmp->is_ipv6 = ip_addr_version(&tunnel->src) == IP6 ? 1 : 0;
5410     ip_address_copy_addr(rmp->source_ip, &tunnel->src);
5411     ip_address_copy_addr(rmp->destination_ip, &tunnel->dst);
5412
5413     rmp->encap_fib_id = htonl(tunnel->encap_fib_index);
5414     rmp->decap_fib_id = htonl(tunnel->decap_fib_index);
5415     rmp->dcap_next = htonl(tunnel->decap_next_index);
5416     rmp->lisp_ver = tunnel->ver_res;
5417     rmp->next_protocol = tunnel->next_protocol;
5418     rmp->flags = tunnel->flags;
5419     rmp->ver_res = tunnel->ver_res;
5420     rmp->res = tunnel->res;
5421     rmp->iid = htonl(tunnel->vni);
5422     rmp->context = context;
5423
5424     vl_msg_api_send_shmem (q, (u8 *)&rmp);
5425 }
5426
5427 static void
5428 vl_api_lisp_gpe_tunnel_dump_t_handler (
5429     vl_api_lisp_gpe_tunnel_dump_t *mp)
5430 {
5431     unix_shared_memory_queue_t * q = NULL;
5432     lisp_gpe_main_t * lgm = &lisp_gpe_main;
5433     lisp_gpe_tunnel_t * tunnel = NULL;
5434
5435     if (pool_elts(lgm->tunnels) == 0) {
5436         return;
5437     }
5438
5439     q = vl_api_client_index_to_input_queue (mp->client_index);
5440     if (q == 0) {
5441         return;
5442     }
5443
5444     pool_foreach(tunnel, lgm->tunnels,
5445                  ({
5446                      send_lisp_gpe_tunnel_details(tunnel, q, mp->context);
5447                 }));
5448 }
5449
5450 static void
5451 send_lisp_map_resolver_details (ip_address_t *ip,
5452                                 unix_shared_memory_queue_t *q,
5453                                 u32 context)
5454 {
5455     vl_api_lisp_map_resolver_details_t *rmp = NULL;
5456
5457     rmp = vl_msg_api_alloc (sizeof (*rmp));
5458     memset (rmp, 0, sizeof (*rmp));
5459     rmp->_vl_msg_id = ntohs(VL_API_LISP_MAP_RESOLVER_DETAILS);
5460
5461     switch (ip_addr_version(ip)) {
5462         case IP4:
5463             rmp->is_ipv6 = 0;
5464             clib_memcpy(rmp->ip_address, &ip_addr_v4(ip), sizeof(ip_addr_v4(ip)));
5465             break;
5466
5467         case IP6:
5468             rmp->is_ipv6 = 1;
5469             clib_memcpy(rmp->ip_address, &ip_addr_v6(ip), sizeof(ip_addr_v6(ip)));
5470             break;
5471
5472         default:
5473             ASSERT(0);
5474     }
5475     rmp->context = context;
5476
5477     vl_msg_api_send_shmem (q, (u8 *)&rmp);
5478 }
5479
5480 static void
5481 vl_api_lisp_map_resolver_dump_t_handler (
5482     vl_api_lisp_map_resolver_dump_t *mp)
5483 {
5484     unix_shared_memory_queue_t * q = NULL;
5485     lisp_cp_main_t * lcm = vnet_lisp_cp_get_main();
5486     ip_address_t *ip = NULL;
5487
5488     q = vl_api_client_index_to_input_queue (mp->client_index);
5489     if (q == 0) {
5490         return;
5491     }
5492
5493     vec_foreach(ip, lcm->map_resolvers) {
5494         send_lisp_map_resolver_details(ip, q, mp->context);
5495     }
5496
5497 }
5498
5499 static void
5500 send_eid_table_map_pair (hash_pair_t * p,
5501                          unix_shared_memory_queue_t * q,
5502                          u32 context)
5503 {
5504     vl_api_lisp_eid_table_map_details_t * rmp = NULL;
5505
5506     rmp = vl_msg_api_alloc (sizeof (*rmp));
5507     memset (rmp, 0, sizeof (*rmp));
5508     rmp->_vl_msg_id = ntohs(VL_API_LISP_EID_TABLE_MAP_DETAILS);
5509
5510     rmp->vni = clib_host_to_net_u32 (p->key);
5511     rmp->vrf = clib_host_to_net_u32 (p->value[0]);
5512     rmp->context = context;
5513     vl_msg_api_send_shmem (q, (u8 *)&rmp);
5514 }
5515
5516 static void
5517 vl_api_lisp_eid_table_map_dump_t_handler (
5518     vl_api_lisp_eid_table_map_dump_t *mp)
5519 {
5520     unix_shared_memory_queue_t * q = NULL;
5521     lisp_cp_main_t * lcm = vnet_lisp_cp_get_main();
5522     hash_pair_t * p;
5523
5524     q = vl_api_client_index_to_input_queue (mp->client_index);
5525     if (q == 0) {
5526         return;
5527     }
5528     hash_foreach_pair (p, lcm->table_id_by_vni, {
5529         send_eid_table_map_pair (p, q, mp->context);
5530     });
5531 }
5532
5533 static void
5534 send_lisp_enable_disable_details (unix_shared_memory_queue_t *q,
5535                                       u32 context)
5536 {
5537     vl_api_lisp_enable_disable_status_details_t *rmp = NULL;
5538
5539     rmp = vl_msg_api_alloc (sizeof (*rmp));
5540     memset (rmp, 0, sizeof (*rmp));
5541     rmp->_vl_msg_id = ntohs(VL_API_LISP_ENABLE_DISABLE_STATUS_DETAILS);
5542
5543     rmp->gpe_status = vnet_lisp_gpe_enable_disable_status ();
5544     rmp->feature_status = vnet_lisp_enable_disable_status ();
5545     rmp->context = context;
5546
5547     vl_msg_api_send_shmem (q, (u8 *)&rmp);
5548 }
5549
5550 static void
5551 vl_api_lisp_enable_disable_status_dump_t_handler
5552 (vl_api_lisp_enable_disable_status_dump_t *mp)
5553 {
5554     unix_shared_memory_queue_t * q = NULL;
5555
5556     q = vl_api_client_index_to_input_queue (mp->client_index);
5557     if (q == 0) {
5558         return;
5559     }
5560
5561     send_lisp_enable_disable_details(q, mp->context);
5562 }
5563
5564 static void
5565 vl_api_lisp_get_map_request_itr_rlocs_t_handler (
5566     vl_api_lisp_get_map_request_itr_rlocs_t *mp)
5567 {
5568     unix_shared_memory_queue_t * q = NULL;
5569     vl_api_lisp_get_map_request_itr_rlocs_reply_t *rmp = NULL;
5570     lisp_cp_main_t * lcm = vnet_lisp_cp_get_main();
5571     locator_set_t * loc_set = 0;
5572     u8 * tmp_str = 0;
5573     int rv = 0;
5574
5575     q = vl_api_client_index_to_input_queue (mp->client_index);
5576     if (q == 0) {
5577         return;
5578     }
5579
5580     if (~0 == lcm->mreq_itr_rlocs) {
5581       tmp_str = format(0, " ");
5582     } else {
5583       loc_set = pool_elt_at_index (lcm->locator_set_pool, lcm->mreq_itr_rlocs);
5584       tmp_str = format(0, "%s", loc_set->name);
5585     }
5586
5587     REPLY_MACRO2(VL_API_LISP_GET_MAP_REQUEST_ITR_RLOCS_REPLY,
5588     ({
5589       strncpy((char *) rmp->locator_set_name, (char *) tmp_str,
5590               ARRAY_LEN(rmp->locator_set_name) - 1);
5591     }));
5592
5593     vec_free(tmp_str);
5594 }
5595
5596 static void
5597 vl_api_interface_name_renumber_t_handler (vl_api_interface_name_renumber_t *mp)
5598 {
5599     vl_api_interface_name_renumber_reply_t * rmp;
5600     int rv = 0;
5601
5602     VALIDATE_SW_IF_INDEX(mp);
5603
5604     rv = vnet_interface_name_renumber
5605         (ntohl(mp->sw_if_index), ntohl(mp->new_show_dev_instance));
5606
5607     BAD_SW_IF_INDEX_LABEL;
5608
5609     REPLY_MACRO(VL_API_INTERFACE_NAME_RENUMBER_REPLY);
5610 }
5611
5612 static int arp_change_data_callback (u32 pool_index, u8 * new_mac,
5613                                      u32 sw_if_index, u32 address)
5614 {
5615     vpe_api_main_t * am = &vpe_api_main;
5616     vlib_main_t * vm = am->vlib_main;
5617     vl_api_ip4_arp_event_t * event;
5618     static f64 arp_event_last_time;
5619     f64 now = vlib_time_now (vm);
5620
5621     if (pool_is_free_index (am->arp_events, pool_index))
5622         return 1;
5623
5624     event = pool_elt_at_index (am->arp_events, pool_index);
5625     if (memcmp (&event->new_mac, new_mac, sizeof (event->new_mac))) {
5626         clib_memcpy (event->new_mac, new_mac, sizeof(event->new_mac));
5627     } else { /* same mac */
5628         if ((sw_if_index == event->sw_if_index) &&
5629             ((address == 0) ||
5630              /* for BD case, also check IP address with 10 sec timeout */
5631              ((address == event->address) &&
5632               ((now - arp_event_last_time) < 10.0))))
5633             return 1;
5634     }
5635
5636     arp_event_last_time = now;
5637     event->sw_if_index = sw_if_index;
5638     if (address) event->address = address;
5639     return 0;
5640 }
5641
5642 static int arp_change_delete_callback (u32 pool_index, u8 * notused)
5643 {
5644     vpe_api_main_t * am = &vpe_api_main;
5645
5646     if (pool_is_free_index (am->arp_events, pool_index))
5647         return 1;
5648
5649     pool_put_index (am->arp_events, pool_index);
5650     return 0;
5651 }
5652
5653 static void
5654 vl_api_want_ip4_arp_events_t_handler
5655 (vl_api_want_ip4_arp_events_t * mp)
5656 {
5657     vpe_api_main_t * am = &vpe_api_main;
5658     vnet_main_t * vnm = vnet_get_main();
5659     vl_api_want_ip4_arp_events_reply_t *rmp;
5660     vl_api_ip4_arp_event_t * event;
5661     int rv;
5662
5663     if (mp->enable_disable) {
5664         pool_get (am->arp_events, event);
5665         memset (event, 0, sizeof (*event));
5666
5667         event->_vl_msg_id = ntohs(VL_API_IP4_ARP_EVENT);
5668         event->client_index = mp->client_index;
5669         event->context = mp->context;
5670         event->address = mp->address;
5671         event->pid = mp->pid;
5672
5673         rv = vnet_add_del_ip4_arp_change_event
5674             (vnm, arp_change_data_callback,
5675              mp->pid,
5676              &mp->address /* addr, in net byte order */,
5677              vpe_resolver_process_node.index,
5678              IP4_ARP_EVENT, event - am->arp_events, 1 /* is_add */);
5679     } else {
5680         rv = vnet_add_del_ip4_arp_change_event
5681             (vnm, arp_change_delete_callback,
5682              mp->pid,
5683              &mp->address /* addr, in net byte order */,
5684              vpe_resolver_process_node.index,
5685              IP4_ARP_EVENT, ~0 /* pool index */, 0 /* is_add */);
5686     }
5687     REPLY_MACRO(VL_API_WANT_IP4_ARP_EVENTS_REPLY);
5688 }
5689
5690 static void vl_api_input_acl_set_interface_t_handler
5691 (vl_api_input_acl_set_interface_t * mp)
5692 {
5693     vlib_main_t *vm = vlib_get_main();
5694     vl_api_input_acl_set_interface_reply_t * rmp;
5695     int rv;
5696     u32 sw_if_index, ip4_table_index, ip6_table_index, l2_table_index;
5697
5698     ip4_table_index = ntohl(mp->ip4_table_index);
5699     ip6_table_index = ntohl(mp->ip6_table_index);
5700     l2_table_index = ntohl(mp->l2_table_index);
5701     sw_if_index = ntohl(mp->sw_if_index);
5702
5703     VALIDATE_SW_IF_INDEX(mp);
5704
5705     rv = vnet_set_input_acl_intfc (vm, sw_if_index, ip4_table_index,
5706                                    ip6_table_index, l2_table_index,
5707                                    mp->is_add);
5708
5709     BAD_SW_IF_INDEX_LABEL;
5710
5711     REPLY_MACRO(VL_API_INPUT_ACL_SET_INTERFACE_REPLY);
5712 }
5713
5714 static void vl_api_ipsec_spd_add_del_t_handler
5715 (vl_api_ipsec_spd_add_del_t * mp)
5716 {
5717 #if IPSEC == 0
5718     clib_warning ("unimplemented");
5719 #else
5720
5721     vlib_main_t *vm __attribute__((unused)) = vlib_get_main();
5722     vl_api_ipsec_spd_add_del_reply_t * rmp;
5723     int rv;
5724
5725 #if DPDK > 0
5726     rv = ipsec_add_del_spd (vm, ntohl(mp->spd_id), mp->is_add);
5727 #else
5728     rv = VNET_API_ERROR_UNIMPLEMENTED;
5729 #endif
5730
5731     REPLY_MACRO(VL_API_IPSEC_SPD_ADD_DEL_REPLY);
5732 #endif
5733 }
5734
5735 static void vl_api_ipsec_interface_add_del_spd_t_handler
5736 (vl_api_ipsec_interface_add_del_spd_t * mp)
5737 {
5738     vlib_main_t *vm __attribute__((unused)) = vlib_get_main();
5739     vl_api_ipsec_interface_add_del_spd_reply_t * rmp;
5740     int rv;
5741     u32 sw_if_index __attribute__((unused));
5742     u32 spd_id __attribute__((unused));
5743
5744     sw_if_index = ntohl(mp->sw_if_index);
5745     spd_id = ntohl(mp->spd_id);
5746
5747     VALIDATE_SW_IF_INDEX(mp);
5748
5749 #if IPSEC > 0
5750     rv = ipsec_set_interface_spd(vm, sw_if_index, spd_id, mp->is_add);
5751 #else
5752     rv = VNET_API_ERROR_UNIMPLEMENTED;
5753 #endif
5754
5755     BAD_SW_IF_INDEX_LABEL;
5756
5757     REPLY_MACRO(VL_API_IPSEC_INTERFACE_ADD_DEL_SPD_REPLY);
5758 }
5759
5760 static void vl_api_ipsec_spd_add_del_entry_t_handler
5761 (vl_api_ipsec_spd_add_del_entry_t * mp)
5762 {
5763     vlib_main_t *vm __attribute__((unused)) = vlib_get_main();
5764     vl_api_ipsec_spd_add_del_entry_reply_t * rmp;
5765     int rv;
5766
5767 #if IPSEC > 0
5768     ipsec_policy_t p;
5769
5770     memset(&p, 0, sizeof(p));
5771
5772     p.id = ntohl(mp->spd_id);
5773     p.priority = ntohl(mp->priority);
5774     p.is_outbound = mp->is_outbound;
5775     p.is_ipv6 = mp->is_ipv6;
5776
5777     if (mp->is_ipv6 || mp->is_ip_any) {
5778         clib_memcpy(&p.raddr.start, mp->remote_address_start, 16);
5779         clib_memcpy(&p.raddr.stop, mp->remote_address_stop, 16);
5780         clib_memcpy(&p.laddr.start, mp->local_address_start, 16);
5781         clib_memcpy(&p.laddr.stop, mp->local_address_stop, 16);
5782     } else {
5783         clib_memcpy(&p.raddr.start.ip4.data, mp->remote_address_start, 4);
5784         clib_memcpy(&p.raddr.stop.ip4.data, mp->remote_address_stop, 4);
5785         clib_memcpy(&p.laddr.start.ip4.data, mp->local_address_start, 4);
5786         clib_memcpy(&p.laddr.stop.ip4.data, mp->local_address_stop, 4);
5787     }
5788     p.protocol = mp->protocol;
5789     p.rport.start = ntohs(mp->remote_port_start);
5790     p.rport.stop  = ntohs(mp->remote_port_stop);
5791     p.lport.start = ntohs(mp->local_port_start);
5792     p.lport.stop  = ntohs(mp->local_port_stop);
5793     /* policy action resolve unsupported */
5794     if (mp->policy == IPSEC_POLICY_ACTION_RESOLVE) {
5795         clib_warning("unsupported action: 'resolve'");
5796         rv = VNET_API_ERROR_UNIMPLEMENTED;
5797         goto out;
5798     }
5799     p.policy = mp->policy;
5800     p.sa_id = ntohl(mp->sa_id);
5801
5802     rv = ipsec_add_del_policy(vm, &p, mp->is_add);
5803     if (rv)
5804       goto out;
5805
5806     if (mp->is_ip_any) {
5807       p.is_ipv6 = 1;
5808       rv = ipsec_add_del_policy(vm, &p, mp->is_add);
5809     }
5810 #else
5811     rv = VNET_API_ERROR_UNIMPLEMENTED;
5812     goto out;
5813 #endif
5814
5815 out:
5816     REPLY_MACRO(VL_API_IPSEC_SPD_ADD_DEL_ENTRY_REPLY);
5817 }
5818
5819 static void vl_api_ipsec_sad_add_del_entry_t_handler
5820 (vl_api_ipsec_sad_add_del_entry_t * mp)
5821 {
5822     vlib_main_t *vm __attribute__((unused)) = vlib_get_main();
5823     vl_api_ipsec_sad_add_del_entry_reply_t * rmp;
5824     int rv;
5825 #if IPSEC > 0
5826     ipsec_sa_t sa;
5827
5828     memset(&sa, 0, sizeof(sa));
5829
5830     sa.id = ntohl(mp->sad_id);
5831     sa.spi = ntohl(mp->spi);
5832     /* security protocol AH unsupported */
5833     if (mp->protocol == IPSEC_PROTOCOL_AH) {
5834         clib_warning("unsupported security protocol 'AH'");
5835         rv = VNET_API_ERROR_UNIMPLEMENTED;
5836         goto out;
5837     }
5838     sa.protocol = mp->protocol;
5839     /* check for unsupported crypto-alg */
5840     if (mp->crypto_algorithm < IPSEC_CRYPTO_ALG_AES_CBC_128 ||
5841         mp->crypto_algorithm > IPSEC_CRYPTO_ALG_AES_CBC_256) {
5842         clib_warning("unsupported crypto-alg: '%U'", format_ipsec_crypto_alg,
5843                      mp->crypto_algorithm);
5844         rv = VNET_API_ERROR_UNIMPLEMENTED;
5845         goto out;
5846     }
5847     sa.crypto_alg = mp->crypto_algorithm;
5848     sa.crypto_key_len = mp->crypto_key_length;
5849     clib_memcpy(&sa.crypto_key, mp->crypto_key, sizeof(sa.crypto_key));
5850     /* check for unsupported integ-alg */
5851     if (mp->integrity_algorithm < IPSEC_INTEG_ALG_SHA1_96 ||
5852         mp->integrity_algorithm > IPSEC_INTEG_ALG_SHA_512_256) {
5853         clib_warning("unsupported integ-alg: '%U'", format_ipsec_integ_alg,
5854                      mp->integrity_algorithm);
5855         rv = VNET_API_ERROR_UNIMPLEMENTED;
5856         goto out;
5857     }
5858     sa.integ_alg = mp->integrity_algorithm;
5859     sa.integ_key_len = mp->integrity_key_length;
5860     clib_memcpy(&sa.integ_key, mp->integrity_key, sizeof(sa.integ_key));
5861     sa.use_esn = mp->use_extended_sequence_number;
5862     sa.is_tunnel = mp->is_tunnel;
5863     sa.is_tunnel_ip6 = mp->is_tunnel_ipv6;
5864     if (sa.is_tunnel_ip6) {
5865         clib_memcpy(&sa.tunnel_src_addr, mp->tunnel_src_address, 16);
5866         clib_memcpy(&sa.tunnel_dst_addr, mp->tunnel_dst_address, 16);
5867     } else {
5868         clib_memcpy(&sa.tunnel_src_addr.ip4.data, mp->tunnel_src_address, 4);
5869         clib_memcpy(&sa.tunnel_dst_addr.ip4.data, mp->tunnel_dst_address, 4);
5870     }
5871
5872     rv = ipsec_add_del_sa(vm, &sa, mp->is_add);
5873 #else
5874     rv = VNET_API_ERROR_UNIMPLEMENTED;
5875     goto out;
5876 #endif
5877
5878 out:
5879     REPLY_MACRO(VL_API_IPSEC_SAD_ADD_DEL_ENTRY_REPLY);
5880 }
5881
5882 static void
5883 vl_api_ikev2_profile_add_del_t_handler
5884 (vl_api_ikev2_profile_add_del_t * mp)
5885 {
5886     vl_api_ikev2_profile_add_del_reply_t * rmp;
5887     int rv = 0;
5888
5889 #if IPSEC > 0
5890     vlib_main_t * vm = vlib_get_main();
5891     clib_error_t * error;
5892     u8 * tmp = format(0, "%s", mp->name);
5893     error = ikev2_add_del_profile(vm, tmp, mp->is_add);
5894     vec_free (tmp);
5895     if (error)
5896       rv = VNET_API_ERROR_UNSPECIFIED;
5897 #else
5898     rv = VNET_API_ERROR_UNIMPLEMENTED;
5899 #endif
5900
5901    REPLY_MACRO(VL_API_IKEV2_PROFILE_ADD_DEL_REPLY);
5902 }
5903
5904 static void
5905 vl_api_ikev2_profile_set_auth_t_handler
5906 (vl_api_ikev2_profile_set_auth_t * mp)
5907 {
5908     vl_api_ikev2_profile_set_auth_reply_t * rmp;
5909     int rv = 0;
5910
5911 #if IPSEC > 0
5912     vlib_main_t * vm = vlib_get_main();
5913     clib_error_t * error;
5914     u8 * tmp = format(0, "%s", mp->name);
5915     u8 * data = vec_new (u8, mp->data_len);
5916     clib_memcpy(data, mp->data, mp->data_len);
5917     error = ikev2_set_profile_auth(vm, tmp, mp->auth_method, data, mp->is_hex);
5918     vec_free (tmp);
5919     vec_free (data);
5920     if (error)
5921       rv = VNET_API_ERROR_UNSPECIFIED;
5922 #else
5923     rv = VNET_API_ERROR_UNIMPLEMENTED;
5924 #endif
5925
5926    REPLY_MACRO(VL_API_IKEV2_PROFILE_SET_AUTH_REPLY);
5927 }
5928
5929 static void
5930 vl_api_ikev2_profile_set_id_t_handler
5931 (vl_api_ikev2_profile_set_id_t * mp)
5932 {
5933     vl_api_ikev2_profile_add_del_reply_t * rmp;
5934     int rv = 0;
5935
5936 #if IPSEC > 0
5937     vlib_main_t * vm = vlib_get_main();
5938     clib_error_t * error;
5939     u8 * tmp = format(0, "%s", mp->name);
5940     u8 * data = vec_new (u8, mp->data_len);
5941     clib_memcpy(data, mp->data, mp->data_len);
5942     error = ikev2_set_profile_id(vm, tmp, mp->id_type, data, mp->is_local);
5943     vec_free (tmp);
5944     vec_free (data);
5945     if (error)
5946       rv = VNET_API_ERROR_UNSPECIFIED;
5947 #else
5948     rv = VNET_API_ERROR_UNIMPLEMENTED;
5949 #endif
5950
5951    REPLY_MACRO(VL_API_IKEV2_PROFILE_SET_ID_REPLY);
5952 }
5953
5954 static void
5955 vl_api_ikev2_profile_set_ts_t_handler
5956 (vl_api_ikev2_profile_set_ts_t * mp)
5957 {
5958     vl_api_ikev2_profile_set_ts_reply_t * rmp;
5959     int rv = 0;
5960
5961 #if IPSEC > 0
5962     vlib_main_t * vm = vlib_get_main();
5963     clib_error_t * error;
5964     u8 * tmp = format(0, "%s", mp->name);
5965     error = ikev2_set_profile_ts(vm, tmp, mp->proto, mp->start_port,
5966                                  mp->end_port, (ip4_address_t) mp->start_addr,
5967                                  (ip4_address_t) mp->end_addr, mp->is_local);
5968     vec_free (tmp);
5969     if (error)
5970       rv = VNET_API_ERROR_UNSPECIFIED;
5971 #else
5972     rv = VNET_API_ERROR_UNIMPLEMENTED;
5973 #endif
5974
5975    REPLY_MACRO(VL_API_IKEV2_PROFILE_SET_TS_REPLY);
5976 }
5977
5978 static void
5979 vl_api_ikev2_set_local_key_t_handler
5980 (vl_api_ikev2_set_local_key_t * mp)
5981 {
5982     vl_api_ikev2_profile_set_ts_reply_t * rmp;
5983     int rv = 0;
5984
5985 #if IPSEC > 0
5986     vlib_main_t * vm = vlib_get_main();
5987     clib_error_t * error;
5988
5989     error = ikev2_set_local_key(vm, mp->key_file);
5990     if (error)
5991       rv = VNET_API_ERROR_UNSPECIFIED;
5992 #else
5993     rv = VNET_API_ERROR_UNIMPLEMENTED;
5994 #endif
5995
5996    REPLY_MACRO(VL_API_IKEV2_SET_LOCAL_KEY_REPLY);
5997 }
5998
5999 static void
6000 vl_api_map_add_domain_t_handler
6001 (vl_api_map_add_domain_t * mp)
6002 {
6003   vl_api_map_add_domain_reply_t * rmp;
6004   int rv = 0;
6005   u32 index;
6006   u8 flags = mp->is_translation ? MAP_DOMAIN_TRANSLATION : 0;
6007   rv = map_create_domain((ip4_address_t *)&mp->ip4_prefix, mp->ip4_prefix_len,
6008                          (ip6_address_t *)&mp->ip6_prefix, mp->ip6_prefix_len,
6009                          (ip6_address_t *)&mp->ip6_src, mp->ip6_src_prefix_len,
6010                          mp->ea_bits_len, mp->psid_offset, mp->psid_length, &index, ntohs(mp->mtu), flags);
6011
6012   REPLY_MACRO2(VL_API_MAP_ADD_DOMAIN_REPLY,
6013                ({
6014                  rmp->index = ntohl(index);
6015                }));
6016 }
6017
6018 static void
6019 vl_api_map_del_domain_t_handler
6020 (vl_api_map_del_domain_t * mp)
6021 {
6022   vl_api_map_del_domain_reply_t * rmp;
6023   int rv = 0;
6024
6025   rv = map_delete_domain(ntohl(mp->index));
6026
6027   REPLY_MACRO(VL_API_MAP_DEL_DOMAIN_REPLY);
6028 }
6029
6030 static void
6031 vl_api_map_add_del_rule_t_handler
6032 (vl_api_map_add_del_rule_t * mp)
6033 {
6034   vl_api_map_del_domain_reply_t * rmp;
6035   int rv = 0;
6036
6037   rv = map_add_del_psid(ntohl(mp->index), ntohs(mp->psid), (ip6_address_t *)mp->ip6_dst, mp->is_add);
6038
6039   REPLY_MACRO(VL_API_MAP_ADD_DEL_RULE_REPLY);
6040 }
6041
6042 static void
6043 vl_api_map_domain_dump_t_handler
6044 (vl_api_map_domain_dump_t * mp)
6045 {
6046   vl_api_map_domain_details_t * rmp;
6047   map_main_t *mm = &map_main;
6048   map_domain_t *d;
6049   unix_shared_memory_queue_t * q;
6050
6051   if (pool_elts (mm->domains) == 0)
6052       return;
6053
6054   q = vl_api_client_index_to_input_queue (mp->client_index);
6055   if (q == 0) {
6056     return;
6057   }
6058
6059   pool_foreach(d, mm->domains, ({
6060     /* Make sure every field is initiated (or don't skip the memset()) */
6061     rmp = vl_msg_api_alloc (sizeof (*rmp));
6062     rmp->_vl_msg_id = ntohs(VL_API_MAP_DOMAIN_DETAILS);
6063     rmp->domain_index = htonl(d - mm->domains);
6064     rmp->ea_bits_len = d->ea_bits_len;
6065     rmp->psid_offset = d->psid_offset;
6066     rmp->psid_length = d->psid_length;
6067     clib_memcpy(rmp->ip4_prefix, &d->ip4_prefix, sizeof(rmp->ip4_prefix));
6068     rmp->ip4_prefix_len = d->ip4_prefix_len;
6069     clib_memcpy(rmp->ip6_prefix, &d->ip6_prefix, sizeof(rmp->ip6_prefix));
6070     rmp->ip6_prefix_len = d->ip6_prefix_len;
6071     clib_memcpy(rmp->ip6_src, &d->ip6_src, sizeof(rmp->ip6_src));
6072     rmp->ip6_src_len = d->ip6_src_len;
6073     rmp->mtu = htons(d->mtu);
6074     rmp->is_translation = (d->flags & MAP_DOMAIN_TRANSLATION);
6075     rmp->context = mp->context;
6076
6077     vl_msg_api_send_shmem (q, (u8 *)&rmp);
6078   }));
6079 }
6080
6081 static void
6082 vl_api_map_rule_dump_t_handler
6083 (vl_api_map_rule_dump_t * mp)
6084 {
6085   unix_shared_memory_queue_t * q;
6086   u16 i;
6087   ip6_address_t dst;
6088   vl_api_map_rule_details_t * rmp;
6089   map_main_t *mm = &map_main;
6090   u32 domain_index = ntohl(mp->domain_index);
6091   map_domain_t *d;
6092
6093   if (pool_elts (mm->domains) == 0)
6094    return;
6095
6096   d = pool_elt_at_index(mm->domains, domain_index);
6097   if (!d || !d->rules) {
6098     return;
6099   }
6100
6101   q = vl_api_client_index_to_input_queue (mp->client_index);
6102   if (q == 0) {
6103     return;
6104   }
6105
6106   for (i = 0; i < (0x1 << d->psid_length); i++) {
6107     dst = d->rules[i];
6108     if (dst.as_u64[0] == 0 && dst.as_u64[1] == 0) {
6109       continue;
6110     }
6111     rmp = vl_msg_api_alloc(sizeof(*rmp));
6112     memset(rmp, 0, sizeof(*rmp));
6113     rmp->_vl_msg_id = ntohs(VL_API_MAP_RULE_DETAILS);
6114     rmp->psid = htons(i);
6115     clib_memcpy(rmp->ip6_dst, &dst, sizeof(rmp->ip6_dst));
6116     rmp->context = mp->context;
6117     vl_msg_api_send_shmem(q, (u8 *)&rmp);
6118   }
6119 }
6120
6121 static void
6122 vl_api_map_summary_stats_t_handler (
6123     vl_api_map_summary_stats_t *mp)
6124 {
6125     vl_api_map_summary_stats_reply_t *rmp;
6126     vlib_combined_counter_main_t *cm;
6127     vlib_counter_t v;
6128     int i, which;
6129     u64 total_pkts[VLIB_N_RX_TX];
6130     u64 total_bytes[VLIB_N_RX_TX];
6131     map_main_t *mm = &map_main;
6132     unix_shared_memory_queue_t *q =
6133         vl_api_client_index_to_input_queue(mp->client_index);
6134
6135     if (!q)
6136         return;
6137
6138     rmp = vl_msg_api_alloc (sizeof (*rmp));
6139     rmp->_vl_msg_id = ntohs(VL_API_MAP_SUMMARY_STATS_REPLY);
6140     rmp->context = mp->context;
6141     rmp->retval = 0;
6142
6143     memset (total_pkts, 0, sizeof (total_pkts));
6144     memset (total_bytes, 0, sizeof (total_bytes));
6145
6146     map_domain_counter_lock (mm);
6147     vec_foreach(cm, mm->domain_counters) {
6148       which = cm - mm->domain_counters;
6149
6150       for (i = 0; i < vec_len(cm->maxi); i++) {
6151         vlib_get_combined_counter (cm, i, &v);
6152         total_pkts[which] += v.packets;
6153         total_bytes[which] += v.bytes;
6154       }
6155     }
6156
6157     map_domain_counter_unlock (mm);
6158
6159     /* Note: in network byte order! */
6160     rmp->total_pkts[MAP_DOMAIN_COUNTER_RX] = clib_host_to_net_u64(total_pkts[MAP_DOMAIN_COUNTER_RX]);
6161     rmp->total_bytes[MAP_DOMAIN_COUNTER_RX] = clib_host_to_net_u64(total_bytes[MAP_DOMAIN_COUNTER_RX]);
6162     rmp->total_pkts[MAP_DOMAIN_COUNTER_TX] = clib_host_to_net_u64(total_pkts[MAP_DOMAIN_COUNTER_TX]);
6163     rmp->total_bytes[MAP_DOMAIN_COUNTER_TX] = clib_host_to_net_u64(total_bytes[MAP_DOMAIN_COUNTER_TX]);
6164     rmp->total_bindings = clib_host_to_net_u64(pool_elts(mm->domains));
6165     rmp->total_ip4_fragments = 0; // Not yet implemented. Should be a simple counter.
6166     rmp->total_security_check[MAP_DOMAIN_COUNTER_TX] = clib_host_to_net_u64(map_error_counter_get(ip4_map_node.index, MAP_ERROR_ENCAP_SEC_CHECK));
6167     rmp->total_security_check[MAP_DOMAIN_COUNTER_RX] = clib_host_to_net_u64(map_error_counter_get(ip4_map_node.index, MAP_ERROR_DECAP_SEC_CHECK));
6168
6169     vl_msg_api_send_shmem(q, (u8 *)&rmp);
6170 }
6171
6172 static void vl_api_ipsec_sa_set_key_t_handler
6173 (vl_api_ipsec_sa_set_key_t * mp)
6174 {
6175     vlib_main_t *vm __attribute__((unused)) = vlib_get_main();
6176     vl_api_ipsec_sa_set_key_reply_t *rmp;
6177     int rv;
6178 #if IPSEC > 0
6179     ipsec_sa_t sa;
6180     sa.id = ntohl(mp->sa_id);
6181     sa.crypto_key_len = mp->crypto_key_length;
6182     clib_memcpy(&sa.crypto_key, mp->crypto_key, sizeof(sa.crypto_key));
6183     sa.integ_key_len = mp->integrity_key_length;
6184     clib_memcpy(&sa.integ_key, mp->integrity_key, sizeof(sa.integ_key));
6185
6186     rv = ipsec_set_sa_key(vm, &sa);
6187 #else
6188     rv = VNET_API_ERROR_UNIMPLEMENTED;
6189 #endif
6190
6191     REPLY_MACRO(VL_API_IPSEC_SA_SET_KEY_REPLY);
6192 }
6193
6194 static void vl_api_cop_interface_enable_disable_t_handler
6195 (vl_api_cop_interface_enable_disable_t * mp)
6196 {
6197     vl_api_cop_interface_enable_disable_reply_t * rmp;
6198     int rv;
6199     u32 sw_if_index = ntohl(mp->sw_if_index);
6200     int enable_disable;
6201
6202     VALIDATE_SW_IF_INDEX(mp);
6203
6204     enable_disable = (int) mp->enable_disable;
6205
6206     rv = cop_interface_enable_disable (sw_if_index, enable_disable);
6207
6208     BAD_SW_IF_INDEX_LABEL;
6209
6210     REPLY_MACRO(VL_API_COP_INTERFACE_ENABLE_DISABLE_REPLY);
6211 }
6212
6213 static void vl_api_cop_whitelist_enable_disable_t_handler
6214 (vl_api_cop_whitelist_enable_disable_t * mp)
6215 {
6216     vl_api_cop_whitelist_enable_disable_reply_t * rmp;
6217     cop_whitelist_enable_disable_args_t _a, *a=&_a;
6218     u32 sw_if_index = ntohl(mp->sw_if_index);
6219     int rv;
6220
6221     VALIDATE_SW_IF_INDEX(mp);
6222
6223     a->sw_if_index = sw_if_index;
6224     a->ip4 = mp->ip4;
6225     a->ip6 = mp->ip6;
6226     a->default_cop = mp->default_cop;
6227     a->fib_id = ntohl(mp->fib_id);
6228
6229     rv = cop_whitelist_enable_disable (a);
6230
6231     BAD_SW_IF_INDEX_LABEL;
6232
6233     REPLY_MACRO(VL_API_COP_WHITELIST_ENABLE_DISABLE_REPLY);
6234 }
6235
6236 static void vl_api_get_node_graph_t_handler
6237 (vl_api_get_node_graph_t * mp)
6238 {
6239     int rv = 0;
6240     u8 * vector = 0;
6241     api_main_t * am = &api_main;
6242     vlib_main_t * vm = vlib_get_main();
6243     void * oldheap;
6244     vl_api_get_node_graph_reply_t * rmp;
6245
6246     pthread_mutex_lock (&am->vlib_rp->mutex);
6247     oldheap = svm_push_data_heap (am->vlib_rp);
6248
6249     /*
6250      * Keep the number of memcpy ops to a minimum (e.g. 1).
6251      */
6252     vec_validate (vector, 16384);
6253     vec_reset_length (vector);
6254
6255     /* $$$$ FIXME */
6256     vector = vlib_node_serialize (&vm->node_main, vector,
6257                                   (u32)~0 /* all threads */,
6258                                   1 /* include nexts */,
6259                                   1 /* include stats */);
6260
6261     svm_pop_heap (oldheap);
6262     pthread_mutex_unlock (&am->vlib_rp->mutex);
6263
6264     REPLY_MACRO2(VL_API_GET_NODE_GRAPH_REPLY,
6265                  rmp->reply_in_shmem = (uword) vector);
6266 }
6267
6268 static void vl_api_trace_profile_add_t_handler
6269 (vl_api_trace_profile_add_t *mp)
6270 {
6271     int rv = 0;
6272     vl_api_trace_profile_add_reply_t * rmp;
6273     clib_error_t *error;
6274
6275     /* Ignoring the profile id as currently a single profile
6276      * is supported */
6277     error = ip6_ioam_trace_profile_set(mp->trace_num_elt, mp->trace_type,
6278                                ntohl(mp->node_id), ntohl(mp->trace_app_data),
6279                                mp->pow_enable, mp->trace_tsp,
6280                                mp->trace_ppc);
6281     if (error) {
6282       clib_error_report(error);
6283       rv = clib_error_get_code(error);
6284     }
6285
6286     REPLY_MACRO(VL_API_TRACE_PROFILE_ADD_REPLY);
6287 }
6288
6289 static void vl_api_trace_profile_apply_t_handler
6290 (vl_api_trace_profile_apply_t *mp)
6291 {
6292     int rv = 0;
6293     vl_api_trace_profile_apply_reply_t * rmp;
6294
6295     if (mp->enable != 0) {
6296       rv = ip6_ioam_set_destination ((ip6_address_t *)(&mp->dest_ipv6),
6297                                 ntohl(mp->prefix_length),
6298                                 ntohl(mp->vrf_id),
6299                         mp->trace_op == IOAM_HBYH_ADD,
6300                         mp->trace_op == IOAM_HBYH_POP,
6301                         mp->trace_op == IOAM_HBYH_MOD);
6302     } else {
6303       //ip6_ioam_clear_destination(&ip6, mp->prefix_length, mp->vrf_id);
6304     }
6305     REPLY_MACRO(VL_API_TRACE_PROFILE_APPLY_REPLY);
6306 }
6307
6308 static void vl_api_trace_profile_del_t_handler
6309 (vl_api_trace_profile_del_t *mp)
6310 {
6311     int rv = 0;
6312     vl_api_trace_profile_del_reply_t * rmp;
6313     clib_error_t *error;
6314
6315     error = clear_ioam_rewrite_fn();
6316     if (error) {
6317       clib_error_report(error);
6318       rv = clib_error_get_code(error);
6319     }
6320
6321     REPLY_MACRO(VL_API_TRACE_PROFILE_DEL_REPLY);
6322 }
6323
6324 static void
6325 vl_api_af_packet_create_t_handler
6326 (vl_api_af_packet_create_t *mp)
6327 {
6328     vlib_main_t *vm = vlib_get_main();
6329     vl_api_af_packet_create_reply_t *rmp;
6330     int rv = 0;
6331     u8 *host_if_name = NULL;
6332     u32 sw_if_index;
6333
6334     host_if_name = format(0, "%s", mp->host_if_name);
6335     vec_add1 (host_if_name, 0);
6336
6337     rv = af_packet_create_if(vm, host_if_name,
6338                              mp->use_random_hw_addr ? 0 : mp->hw_addr, &sw_if_index);
6339
6340     vec_free(host_if_name);
6341
6342     REPLY_MACRO2(VL_API_AF_PACKET_CREATE_REPLY,
6343                  rmp->sw_if_index = clib_host_to_net_u32(sw_if_index));
6344 }
6345
6346 static void
6347 vl_api_af_packet_delete_t_handler
6348 (vl_api_af_packet_delete_t *mp)
6349 {
6350     vlib_main_t * vm = vlib_get_main();
6351     vl_api_af_packet_delete_reply_t *rmp;
6352     int rv = 0;
6353     u8 *host_if_name = NULL;
6354
6355     host_if_name = format(0, "%s", mp->host_if_name);
6356     vec_add1 (host_if_name, 0);
6357
6358     rv = af_packet_delete_if(vm, host_if_name);
6359
6360     vec_free(host_if_name);
6361
6362     REPLY_MACRO(VL_API_AF_PACKET_DELETE_REPLY);
6363 }
6364
6365 static void
6366 vl_api_policer_add_del_t_handler
6367 (vl_api_policer_add_del_t *mp)
6368 {
6369     vlib_main_t * vm = vlib_get_main();
6370     vl_api_policer_add_del_reply_t *rmp;
6371     int rv = 0;
6372     u8 *name = NULL;
6373     sse2_qos_pol_cfg_params_st cfg;
6374     clib_error_t * error;
6375
6376     name = format(0, "%s", mp->name);
6377
6378     memset (&cfg, 0, sizeof (cfg));
6379     cfg.rfc = mp->type;
6380     cfg.rnd_type = mp->round_type;
6381     cfg.rate_type = mp->rate_type;
6382     cfg.rb.kbps.cir_kbps = mp->cir;
6383     cfg.rb.kbps.eir_kbps = mp->eir;
6384     cfg.rb.kbps.cb_bytes = mp->cb;
6385     cfg.rb.kbps.eb_bytes = mp->eb;
6386     cfg.conform_action.action_type = mp->conform_action_type;
6387     cfg.conform_action.dscp = mp->conform_dscp;
6388     cfg.exceed_action.action_type = mp->exceed_action_type;
6389     cfg.exceed_action.dscp = mp->exceed_dscp;
6390     cfg.violate_action.action_type = mp->violate_action_type;
6391     cfg.violate_action.dscp = mp->violate_dscp;
6392
6393     error = policer_add_del(vm, name, &cfg, mp->is_add);
6394
6395     if (error)
6396       rv = VNET_API_ERROR_UNSPECIFIED;
6397
6398     REPLY_MACRO(VL_API_POLICER_ADD_DEL_REPLY);
6399 }
6400
6401 static void
6402 send_policer_details (u8 *name,
6403                       sse2_qos_pol_cfg_params_st *config,
6404                       policer_read_response_type_st *templ,
6405                       unix_shared_memory_queue_t *q,
6406                       u32 context)
6407 {
6408     vl_api_policer_details_t * mp;
6409
6410     mp = vl_msg_api_alloc (sizeof (*mp));
6411     memset (mp, 0, sizeof (*mp));
6412     mp->_vl_msg_id = ntohs (VL_API_POLICER_DETAILS);
6413     mp->context = context;
6414     mp->cir = htonl(config->rb.kbps.cir_kbps);
6415     mp->eir = htonl(config->rb.kbps.eir_kbps);
6416     mp->cb = htonl(config->rb.kbps.cb_bytes);
6417     mp->eb = htonl(config->rb.kbps.eb_bytes);
6418     mp->rate_type = config->rate_type;
6419     mp->round_type = config->rnd_type;
6420     mp->type = config->rfc;
6421     mp->conform_action_type = config->conform_action.action_type;
6422     mp->conform_dscp = config->conform_action.dscp;
6423     mp->exceed_action_type = config->exceed_action.action_type;
6424     mp->exceed_dscp = config->exceed_action.dscp;
6425     mp->violate_action_type = config->violate_action.action_type;
6426     mp->violate_dscp = config->violate_action.dscp;
6427     mp->single_rate = templ->single_rate ? 1 : 0;
6428     mp->color_aware = templ->color_aware ? 1 : 0;
6429     mp->scale = htonl(templ->scale);
6430     mp->cir_tokens_per_period = htonl(templ->cir_tokens_per_period);
6431     mp->pir_tokens_per_period = htonl(templ->pir_tokens_per_period);
6432     mp->current_limit = htonl(templ->current_limit);
6433     mp->current_bucket = htonl(templ->current_bucket);
6434     mp->extended_limit = htonl(templ->extended_limit);
6435     mp->extended_bucket = htonl(templ->extended_bucket);
6436     mp->last_update_time = clib_host_to_net_u64(templ->last_update_time);
6437
6438     strncpy ((char *) mp->name, (char *) name, ARRAY_LEN(mp->name) - 1);
6439
6440     vl_msg_api_send_shmem (q, (u8 *)&mp);
6441 }
6442
6443 static void
6444 vl_api_policer_dump_t_handler
6445 (vl_api_policer_dump_t *mp)
6446 {
6447     unix_shared_memory_queue_t * q;
6448     vnet_policer_main_t * pm = &vnet_policer_main;
6449     hash_pair_t * hp;
6450     uword * p;
6451     u32 pool_index;
6452     u8 * match_name = 0;
6453     u8 * name;
6454     sse2_qos_pol_cfg_params_st *config;
6455     policer_read_response_type_st *templ;
6456
6457     q = vl_api_client_index_to_input_queue (mp->client_index);
6458     if (q == 0)
6459         return;
6460
6461     if (mp->match_name_valid) {
6462         match_name = format(0, "%s%c", mp->match_name, 0);
6463     }
6464
6465     if (mp->match_name_valid) {
6466         p = hash_get_mem (pm->policer_config_by_name, match_name);
6467         if (p) {
6468             pool_index = p[0];
6469             config = pool_elt_at_index (pm->configs, pool_index);
6470             templ = pool_elt_at_index (pm->policer_templates, pool_index);
6471             send_policer_details(match_name, config, templ, q, mp->context);
6472         }
6473     } else {
6474         hash_foreach_pair (hp, pm->policer_config_by_name,
6475         ({
6476             name = (u8 *) hp->key;
6477             pool_index = hp->value[0];
6478             config = pool_elt_at_index (pm->configs, pool_index);
6479             templ = pool_elt_at_index (pm->policer_templates, pool_index);
6480             send_policer_details(name, config, templ, q, mp->context);
6481         }));
6482     }
6483 }
6484
6485 static void
6486 vl_api_netmap_create_t_handler
6487 (vl_api_netmap_create_t *mp)
6488 {
6489     vlib_main_t *vm = vlib_get_main();
6490     vl_api_netmap_create_reply_t *rmp;
6491     int rv = 0;
6492     u8 *if_name = NULL;
6493
6494     if_name = format(0, "%s", mp->netmap_if_name);
6495     vec_add1 (if_name, 0);
6496
6497     rv = netmap_create_if(vm, if_name, mp->use_random_hw_addr ? 0 : mp->hw_addr,
6498                           mp->is_pipe, mp->is_master, 0);
6499
6500     vec_free(if_name);
6501
6502     REPLY_MACRO(VL_API_NETMAP_CREATE_REPLY);
6503 }
6504
6505 static void
6506 vl_api_netmap_delete_t_handler
6507 (vl_api_netmap_delete_t *mp)
6508 {
6509     vlib_main_t * vm = vlib_get_main();
6510     vl_api_netmap_delete_reply_t *rmp;
6511     int rv = 0;
6512     u8 *if_name = NULL;
6513
6514     if_name = format(0, "%s", mp->netmap_if_name);
6515     vec_add1 (if_name, 0);
6516
6517     rv = netmap_delete_if(vm, if_name);
6518
6519     vec_free(if_name);
6520
6521     REPLY_MACRO(VL_API_NETMAP_DELETE_REPLY);
6522 }
6523
6524 static void vl_api_mpls_gre_tunnel_details_t_handler (
6525     vl_api_mpls_gre_tunnel_details_t * mp)
6526 {
6527     clib_warning ("BUG");
6528 }
6529
6530 static void send_mpls_gre_tunnel_entry (vpe_api_main_t * am,
6531                                     unix_shared_memory_queue_t *q,
6532                                     mpls_gre_tunnel_t * gt,
6533                                     u32 index,
6534                                     u32 context)
6535 {
6536     vl_api_mpls_gre_tunnel_details_t * mp;
6537
6538     mp = vl_msg_api_alloc (sizeof (*mp));
6539     memset (mp, 0, sizeof (*mp));
6540     mp->_vl_msg_id = ntohs(VL_API_MPLS_GRE_TUNNEL_DETAILS);
6541     mp->context = context;
6542
6543     if (gt != NULL) {
6544         mp->tunnel_index    = htonl(index);
6545         mp->tunnel_src      = gt->tunnel_src.as_u32;
6546         mp->tunnel_dst      = gt->tunnel_dst.as_u32;
6547         mp->intfc_address   = gt->intfc_address.as_u32;
6548         mp->mask_width      = htonl(gt->mask_width);
6549         mp->inner_fib_index = htonl(gt->inner_fib_index);
6550         mp->outer_fib_index = htonl(gt->outer_fib_index);
6551         mp->encap_index     = htonl(gt->encap_index);
6552         mp->hw_if_index     = htonl(gt->hw_if_index);
6553         mp->l2_only         = htonl(gt->l2_only);
6554     }
6555
6556     mpls_main_t * mm = &mpls_main;
6557     mpls_encap_t * e;
6558     int i;
6559     u32 len = 0;
6560
6561     e = pool_elt_at_index (mm->encaps, gt->encap_index);
6562     len = vec_len (e->labels);
6563     mp->nlabels = htonl(len);
6564
6565     for (i = 0; i < len; i++) {
6566         mp->labels[i] = htonl(vnet_mpls_uc_get_label(
6567                 clib_host_to_net_u32(e->labels[i].label_exp_s_ttl)));
6568     }
6569
6570     vl_msg_api_send_shmem (q, (u8 *)&mp);
6571 }
6572
6573 static void
6574 vl_api_mpls_gre_tunnel_dump_t_handler (vl_api_mpls_gre_tunnel_dump_t *mp)
6575 {
6576     vpe_api_main_t * am = &vpe_api_main;
6577     unix_shared_memory_queue_t * q;
6578     vlib_main_t * vm = &vlib_global_main;
6579     mpls_main_t * mm = &mpls_main;
6580     mpls_gre_tunnel_t * gt;
6581     u32 index = ntohl(mp->tunnel_index);
6582
6583     q = vl_api_client_index_to_input_queue (mp->client_index);
6584     if (q == 0)
6585         return;
6586
6587     if (pool_elts (mm->gre_tunnels)) {
6588         if(mp->tunnel_index >= 0) {
6589             vlib_cli_output (vm, "MPLS-GRE tunnel %u", index);
6590             gt = pool_elt_at_index (mm->gre_tunnels, index);
6591             send_mpls_gre_tunnel_entry (am, q, gt, gt - mm->gre_tunnels, mp->context);
6592         } else {
6593             vlib_cli_output (vm, "MPLS-GRE tunnels");
6594             pool_foreach (gt, mm->gre_tunnels,
6595             ({
6596               send_mpls_gre_tunnel_entry (am, q, gt, gt - mm->gre_tunnels, mp->context);
6597             }));
6598         }
6599     } else {
6600         vlib_cli_output (vm, "No MPLS-GRE tunnels");
6601     }
6602 }
6603
6604 static void vl_api_mpls_eth_tunnel_details_t_handler (
6605     vl_api_mpls_eth_tunnel_details_t * mp)
6606 {
6607     clib_warning ("BUG");
6608 }
6609
6610 static void send_mpls_eth_tunnel_entry (vpe_api_main_t * am,
6611                                     unix_shared_memory_queue_t *q,
6612                                     mpls_eth_tunnel_t * et,
6613                                     u32 index,
6614                                     u32 context)
6615 {
6616     vl_api_mpls_eth_tunnel_details_t * mp;
6617
6618     mp = vl_msg_api_alloc (sizeof (*mp));
6619     memset (mp, 0, sizeof (*mp));
6620     mp->_vl_msg_id = ntohs(VL_API_MPLS_ETH_TUNNEL_DETAILS);
6621     mp->context = context;
6622
6623     if (et != NULL) {
6624         mp->tunnel_index    = htonl(index);
6625         memcpy(mp->tunnel_dst_mac, et->tunnel_dst, 6);
6626         mp->intfc_address   = et->intfc_address.as_u32;
6627         mp->tx_sw_if_index  = htonl(et->tx_sw_if_index);
6628         mp->inner_fib_index = htonl(et->inner_fib_index);
6629         mp->mask_width      = htonl(et->mask_width);
6630         mp->encap_index     = htonl(et->encap_index);
6631         mp->hw_if_index     = htonl(et->hw_if_index);
6632         mp->l2_only         = htonl(et->l2_only);
6633     }
6634
6635     mpls_main_t * mm = &mpls_main;
6636     mpls_encap_t * e;
6637     int i;
6638     u32 len = 0;
6639
6640     e = pool_elt_at_index (mm->encaps, et->encap_index);
6641     len = vec_len (e->labels);
6642     mp->nlabels = htonl(len);
6643
6644     for (i = 0; i < len; i++) {
6645         mp->labels[i] = htonl(vnet_mpls_uc_get_label(
6646                 clib_host_to_net_u32(e->labels[i].label_exp_s_ttl)));
6647     }
6648
6649     vl_msg_api_send_shmem (q, (u8 *)&mp);
6650 }
6651
6652 static void
6653 vl_api_mpls_eth_tunnel_dump_t_handler (vl_api_mpls_eth_tunnel_dump_t *mp)
6654 {
6655     vpe_api_main_t * am = &vpe_api_main;
6656     unix_shared_memory_queue_t * q;
6657     vlib_main_t * vm = &vlib_global_main;
6658     mpls_main_t * mm = &mpls_main;
6659     mpls_eth_tunnel_t * et;
6660     u32 index = ntohl(mp->tunnel_index);
6661
6662     q = vl_api_client_index_to_input_queue (mp->client_index);
6663     if (q == 0)
6664         return;
6665
6666     clib_warning("Received mpls_eth_tunnel_dump");
6667     clib_warning("Received tunnel index: %u from client %u", index, mp->client_index);
6668
6669     if (pool_elts (mm->eth_tunnels)) {
6670         if(mp->tunnel_index >= 0) {
6671             vlib_cli_output (vm, "MPLS-Ethernet tunnel %u", index);
6672             et = pool_elt_at_index (mm->eth_tunnels, index);
6673             send_mpls_eth_tunnel_entry (am, q, et, et - mm->eth_tunnels, mp->context);
6674         } else {
6675             clib_warning("MPLS-Ethernet tunnels");
6676             pool_foreach (et, mm->eth_tunnels,
6677             ({
6678                 send_mpls_eth_tunnel_entry (am, q, et, et - mm->eth_tunnels, mp->context);
6679             }));
6680         }
6681     } else {
6682         clib_warning("No MPLS-Ethernet tunnels");
6683     }
6684 }
6685
6686 static void vl_api_mpls_fib_encap_details_t_handler (
6687     vl_api_mpls_fib_encap_details_t * mp)
6688 {
6689     clib_warning ("BUG");
6690 }
6691
6692 static void send_mpls_fib_encap_details (vpe_api_main_t * am,
6693                                     unix_shared_memory_queue_t *q,
6694                                     show_mpls_fib_t *s,
6695                                     u32 context)
6696 {
6697     vl_api_mpls_fib_encap_details_t * mp;
6698
6699     mp = vl_msg_api_alloc (sizeof (*mp));
6700     memset (mp, 0, sizeof (*mp));
6701     mp->_vl_msg_id = ntohs(VL_API_MPLS_FIB_ENCAP_DETAILS);
6702     mp->context = context;
6703
6704     mp->fib_index   = htonl(s->fib_index);
6705     mp->entry_index = htonl(s->entry_index);
6706     mp->dest        = s->dest;
6707     mp->s_bit       = htonl(s->s_bit);
6708
6709     mpls_main_t * mm = &mpls_main;
6710     mpls_encap_t * e;
6711     int i;
6712     u32 len = 0;
6713
6714     e = pool_elt_at_index (mm->encaps, s->entry_index);
6715     len = vec_len (e->labels);
6716     mp->nlabels = htonl(len);
6717
6718     for (i = 0; i < len; i++) {
6719         mp->labels[i] = htonl(vnet_mpls_uc_get_label(
6720                 clib_host_to_net_u32(e->labels[i].label_exp_s_ttl)));
6721     }
6722
6723     vl_msg_api_send_shmem (q, (u8 *)&mp);
6724 }
6725
6726 static void
6727 vl_api_mpls_fib_encap_dump_t_handler (vl_api_mpls_fib_encap_dump_t *mp)
6728 {
6729     vpe_api_main_t * am = &vpe_api_main;
6730     unix_shared_memory_queue_t * q;
6731     vlib_main_t * vm = &vlib_global_main;
6732     u64 key;
6733     u32 value;
6734     show_mpls_fib_t *records = 0;
6735     show_mpls_fib_t *s;
6736     mpls_main_t * mm = &mpls_main;
6737     ip4_main_t * im = &ip4_main;
6738     ip4_fib_t * rx_fib;
6739
6740     q = vl_api_client_index_to_input_queue (mp->client_index);
6741     if (q == 0)
6742         return;
6743
6744     hash_foreach (key, value, mm->mpls_encap_by_fib_and_dest,
6745     ({
6746         vec_add2 (records, s, 1);
6747         s->fib_index = (u32)(key>>32);
6748         s->dest = (u32)(key & 0xFFFFFFFF);
6749         s->entry_index = (u32) value;
6750     }));
6751
6752     if (0 == vec_len(records)) {
6753         vlib_cli_output(vm, "MPLS encap table empty");
6754         goto out;
6755     }
6756
6757     /* sort output by dst address within fib */
6758     vec_sort_with_function(records, mpls_dest_cmp);
6759     vec_sort_with_function(records, mpls_fib_index_cmp);
6760     vlib_cli_output(vm, "MPLS encap table");
6761     vlib_cli_output(vm, "%=6s%=16s%=16s", "Table", "Dest address", "Labels");
6762     vec_foreach (s, records)
6763     {
6764         rx_fib = vec_elt_at_index(im->fibs, s->fib_index);
6765         vlib_cli_output(vm, "%=6d%=16U%=16U", rx_fib->table_id,
6766                 format_ip4_address, &s->dest, format_mpls_encap_index, mm,
6767                 s->entry_index);
6768         send_mpls_fib_encap_details (am, q, s, mp->context);
6769     }
6770
6771 out:
6772     vec_free(records);
6773 }
6774
6775 static void vl_api_mpls_fib_decap_details_t_handler (
6776     vl_api_mpls_fib_decap_details_t * mp)
6777 {
6778     clib_warning ("BUG");
6779 }
6780
6781 static void send_mpls_fib_decap_details (vpe_api_main_t * am,
6782                                     unix_shared_memory_queue_t *q,
6783                                     show_mpls_fib_t *s,
6784                                     u32 rx_table_id,
6785                                     u32 tx_table_id,
6786                                     char *swif_tag,
6787                                     u32 context)
6788 {
6789     vl_api_mpls_fib_decap_details_t * mp;
6790
6791     mp = vl_msg_api_alloc (sizeof (*mp));
6792     memset (mp, 0, sizeof (*mp));
6793     mp->_vl_msg_id = ntohs(VL_API_MPLS_FIB_DECAP_DETAILS);
6794     mp->context = context;
6795
6796     mp->fib_index   = htonl(s->fib_index);
6797     mp->entry_index = htonl(s->entry_index);
6798     mp->dest        = s->dest;
6799     mp->s_bit       = htonl(s->s_bit);
6800     mp->label       = htonl(s->label);
6801     mp->rx_table_id = htonl(rx_table_id);
6802     mp->tx_table_id = htonl(tx_table_id);
6803     strncpy ((char *) mp->swif_tag,
6804              (char *) swif_tag, ARRAY_LEN(mp->swif_tag)-1);
6805
6806     vl_msg_api_send_shmem (q, (u8 *)&mp);
6807 }
6808
6809 static void
6810 vl_api_mpls_fib_decap_dump_t_handler (vl_api_mpls_fib_decap_dump_t *mp)
6811 {
6812     vpe_api_main_t * am = &vpe_api_main;
6813     unix_shared_memory_queue_t * q;
6814     vlib_main_t * vm = &vlib_global_main;
6815     u64 key;
6816     u32 value;
6817     show_mpls_fib_t *records = 0;
6818     show_mpls_fib_t *s;
6819     mpls_main_t * mm = &mpls_main;
6820     ip4_main_t * im = &ip4_main;
6821     ip4_fib_t * rx_fib;
6822     ip4_fib_t *tx_fib;
6823     u32 tx_table_id;
6824     char *swif_tag;
6825
6826     q = vl_api_client_index_to_input_queue (mp->client_index);
6827     if (q == 0)
6828         return;
6829
6830     hash_foreach (key, value, mm->mpls_decap_by_rx_fib_and_label,
6831     ({
6832         vec_add2 (records, s, 1);
6833         s->fib_index = (u32)(key>>32);
6834         s->entry_index = (u32) value;
6835         s->label = ((u32) key)>>12;
6836         s->s_bit = (key & (1<<8)) != 0;
6837     }));
6838
6839     if (!vec_len(records)) {
6840         vlib_cli_output(vm, "MPLS decap table empty");
6841         goto out;
6842     }
6843
6844     vec_sort_with_function(records, mpls_label_cmp);
6845     vlib_cli_output(vm, "MPLS decap table");
6846     vlib_cli_output(vm, "%=10s%=15s%=6s%=6s", "RX Table", "TX Table/Intfc",
6847             "Label", "S-bit");
6848     vec_foreach (s, records)
6849     {
6850         mpls_decap_t * d;
6851         d = pool_elt_at_index(mm->decaps, s->entry_index);
6852         if (d->next_index == MPLS_INPUT_NEXT_IP4_INPUT) {
6853             tx_fib = vec_elt_at_index(im->fibs, d->tx_fib_index);
6854             tx_table_id = tx_fib->table_id;
6855             swif_tag = "     ";
6856         } else {
6857             tx_table_id = d->tx_fib_index;
6858             swif_tag = "(i)  ";
6859         }
6860         rx_fib = vec_elt_at_index(im->fibs, s->fib_index);
6861
6862         vlib_cli_output(vm, "%=10d%=10d%=5s%=6d%=6d", rx_fib->table_id,
6863                 tx_table_id, swif_tag, s->label, s->s_bit);
6864
6865         send_mpls_fib_decap_details (am, q, s, rx_fib->table_id,
6866                 tx_table_id, swif_tag, mp->context);
6867     }
6868
6869 out:
6870     vec_free(records);
6871 }
6872
6873 static void vl_api_classify_table_ids_t_handler (vl_api_classify_table_ids_t *mp)
6874 {
6875     unix_shared_memory_queue_t * q;
6876
6877     q = vl_api_client_index_to_input_queue (mp->client_index);
6878     if (q == 0)
6879         return;
6880
6881     vnet_classify_main_t * cm = &vnet_classify_main;
6882     vnet_classify_table_t * t;
6883     u32 * table_ids = 0;
6884     u32 count;
6885
6886     pool_foreach (t, cm->tables,
6887     ({
6888         vec_add1 (table_ids, ntohl(t - cm->tables));
6889     }));
6890     count = vec_len(table_ids);
6891
6892     vl_api_classify_table_ids_reply_t *rmp;
6893     rmp = vl_msg_api_alloc_as_if_client(sizeof (*rmp) + count);
6894     rmp->_vl_msg_id = ntohs (VL_API_CLASSIFY_TABLE_IDS_REPLY);
6895     rmp->context = mp->context;
6896     rmp->count = ntohl(count);
6897     clib_memcpy(rmp->ids, table_ids, count * sizeof(u32));
6898     rmp->retval = 0;
6899
6900     vl_msg_api_send_shmem (q, (u8 *)&rmp);
6901
6902     vec_free (table_ids);
6903 }
6904
6905 static void vl_api_classify_table_by_interface_t_handler (vl_api_classify_table_by_interface_t *mp)
6906 {
6907     vl_api_classify_table_by_interface_reply_t *rmp;
6908     int rv = 0;
6909
6910     u32 sw_if_index = ntohl(mp->sw_if_index);
6911     u32 * acl = 0;
6912
6913     vec_validate (acl, INPUT_ACL_N_TABLES - 1);
6914     vec_set (acl, ~0);
6915
6916     VALIDATE_SW_IF_INDEX(mp);
6917
6918     input_acl_main_t * am = &input_acl_main;
6919
6920     int if_idx;
6921     u32 type;
6922
6923     for (type = 0; type < INPUT_ACL_N_TABLES; type++)
6924     {
6925         u32 * vec_tbl = am->classify_table_index_by_sw_if_index[type];
6926                 if (vec_len(vec_tbl)) {
6927                         for (if_idx = 0; if_idx < vec_len (vec_tbl); if_idx++)
6928                         {
6929                             if (vec_elt(vec_tbl, if_idx) == ~0 || sw_if_index != if_idx) {
6930                                     continue;
6931                             }
6932                             acl[type] = vec_elt(vec_tbl, if_idx);
6933                         }
6934                 }
6935     }
6936
6937     BAD_SW_IF_INDEX_LABEL;
6938
6939     REPLY_MACRO2(VL_API_CLASSIFY_TABLE_BY_INTERFACE_REPLY,
6940     ({
6941        rmp->sw_if_index = ntohl(sw_if_index);
6942        rmp->l2_table_id = ntohl(acl[INPUT_ACL_TABLE_L2]);
6943        rmp->ip4_table_id = ntohl(acl[INPUT_ACL_TABLE_IP4]);
6944        rmp->ip6_table_id = ntohl(acl[INPUT_ACL_TABLE_IP6]);
6945     }));
6946     vec_free(acl);
6947 }
6948
6949 static void vl_api_classify_table_info_t_handler (vl_api_classify_table_info_t *mp)
6950 {
6951     unix_shared_memory_queue_t * q;
6952
6953     q = vl_api_client_index_to_input_queue (mp->client_index);
6954     if (q == 0)
6955         return;
6956
6957     vl_api_classify_table_info_reply_t *rmp = 0;
6958
6959     vnet_classify_main_t * cm = &vnet_classify_main;
6960     u32 table_id = ntohl(mp->table_id);
6961     vnet_classify_table_t * t;
6962
6963     pool_foreach (t, cm->tables,
6964     ({
6965         if (table_id == t - cm->tables) {
6966             rmp = vl_msg_api_alloc_as_if_client(sizeof (*rmp) + t->match_n_vectors * sizeof (u32x4));
6967             rmp->_vl_msg_id = ntohs (VL_API_CLASSIFY_TABLE_INFO_REPLY);
6968             rmp->context = mp->context;
6969             rmp->table_id = ntohl(table_id);
6970             rmp->nbuckets = ntohl(t->nbuckets);
6971             rmp->match_n_vectors = ntohl(t->match_n_vectors);
6972             rmp->skip_n_vectors = ntohl(t->skip_n_vectors);
6973             rmp->active_sessions = ntohl(t->active_elements);
6974             rmp->next_table_index = ntohl(t->next_table_index);
6975             rmp->miss_next_index = ntohl(t->miss_next_index);
6976             rmp->mask_length = ntohl(t->match_n_vectors * sizeof (u32x4));
6977             clib_memcpy(rmp->mask, t->mask, t->match_n_vectors * sizeof(u32x4));
6978             rmp->retval = 0;
6979             break;
6980         }
6981     }));
6982
6983     if (rmp == 0) {
6984         rmp = vl_msg_api_alloc (sizeof (*rmp));
6985         rmp->_vl_msg_id = ntohs((VL_API_CLASSIFY_TABLE_INFO_REPLY));
6986         rmp->context = mp->context;
6987         rmp->retval = ntohl(VNET_API_ERROR_CLASSIFY_TABLE_NOT_FOUND);
6988     }
6989
6990     vl_msg_api_send_shmem (q, (u8 *)&rmp);
6991 }
6992
6993 static void vl_api_classify_session_details_t_handler (vl_api_classify_session_details_t * mp)
6994 {
6995     clib_warning ("BUG");
6996 }
6997
6998 static void send_classify_session_details (unix_shared_memory_queue_t * q,
6999                                            u32 table_id,
7000                                            u32 match_length,
7001                                            vnet_classify_entry_t * e,
7002                                            u32 context)
7003 {
7004     vl_api_classify_session_details_t *rmp;
7005
7006     rmp = vl_msg_api_alloc (sizeof (*rmp));
7007     memset (rmp, 0, sizeof (*rmp));
7008     rmp->_vl_msg_id = ntohs(VL_API_CLASSIFY_SESSION_DETAILS);
7009     rmp->context = context;
7010     rmp->table_id = ntohl(table_id);
7011     rmp->hit_next_index = ntohl(e->next_index);
7012     rmp->advance = ntohl(e->advance);
7013     rmp->opaque_index = ntohl(e->opaque_index);
7014     rmp->match_length = ntohl(match_length);
7015     clib_memcpy(rmp->match, e->key, match_length);
7016
7017     vl_msg_api_send_shmem (q, (u8 *)&rmp);
7018 }
7019
7020 static void vl_api_classify_session_dump_t_handler (vl_api_classify_session_dump_t *mp)
7021 {
7022     vnet_classify_main_t * cm = &vnet_classify_main;
7023     unix_shared_memory_queue_t * q;
7024
7025     u32 table_id = ntohl(mp->table_id);
7026     vnet_classify_table_t * t;
7027
7028     q = vl_api_client_index_to_input_queue (mp->client_index);
7029
7030     pool_foreach (t, cm->tables,
7031     ({
7032         if (table_id == t - cm->tables) {
7033             vnet_classify_bucket_t * b;
7034             vnet_classify_entry_t * v, * save_v;
7035             int i, j, k;
7036
7037             for (i = 0; i < t->nbuckets; i++)
7038             {
7039                 b = &t->buckets [i];
7040                 if (b->offset == 0)
7041                    continue;
7042
7043                 save_v = vnet_classify_get_entry (t, b->offset);
7044                 for (j = 0; j < (1<<b->log2_pages); j++)
7045                 {
7046                         for (k = 0; k < t->entries_per_page; k++)
7047                         {
7048                             v = vnet_classify_entry_at_index (t, save_v, j*t->entries_per_page + k);
7049                             if (vnet_classify_entry_is_free (v))
7050                                 continue;
7051
7052                                 send_classify_session_details(q, table_id,
7053                                                 t->match_n_vectors * sizeof (u32x4), v, mp->context);
7054                         }
7055                 }
7056             }
7057             break;
7058         }
7059     }));
7060 }
7061
7062 static void vl_api_ipfix_enable_t_handler (vl_api_ipfix_enable_t *mp)
7063 {
7064     vlib_main_t *vm = vlib_get_main();
7065         flow_report_main_t * frm = &flow_report_main;
7066         vl_api_ipfix_enable_reply_t *rmp;
7067         ip4_address_t collector, src;
7068         u16 collector_port = UDP_DST_PORT_ipfix;
7069     u32 path_mtu;
7070     u32 template_interval;
7071         u32 fib_id;
7072         u32 fib_index = ~0;
7073         int rv = 0;
7074
7075     memcpy(collector.data, mp->collector_address, sizeof(collector.data));
7076     collector_port = ntohs(mp->collector_port);
7077     if (collector_port == (u16)~0)
7078         collector_port = UDP_DST_PORT_ipfix;
7079     memcpy(src.data, mp->src_address, sizeof(src.data));
7080     fib_id = ntohl(mp->vrf_id);
7081
7082     ip4_main_t * im = &ip4_main;
7083     uword * p = hash_get (im->fib_index_by_table_id, fib_id);
7084     if (! p) {
7085         rv = VNET_API_ERROR_NO_SUCH_FIB;
7086         goto out;
7087     }
7088     fib_index = p[0];
7089
7090     path_mtu = ntohl(mp->path_mtu);
7091     if (path_mtu == ~0)
7092         path_mtu = 512; // RFC 7011 section 10.3.3.
7093     template_interval = ntohl(mp->template_interval);
7094     if (template_interval == ~0)
7095         template_interval = 20;
7096
7097     if (collector.as_u32 == 0) {
7098         rv = VNET_API_ERROR_INVALID_VALUE;
7099         goto out;
7100     }
7101
7102     if (src.as_u32 == 0) {
7103         rv = VNET_API_ERROR_INVALID_VALUE;
7104         goto out;
7105     }
7106
7107     if (path_mtu > 1450 /* vpp does not support fragmentation */) {
7108         rv = VNET_API_ERROR_INVALID_VALUE;
7109         goto out;
7110     }
7111
7112     if (path_mtu < 68) {
7113         rv = VNET_API_ERROR_INVALID_VALUE;
7114         goto out;
7115     }
7116
7117     /* Reset report streams if we are reconfiguring IP addresses */
7118     if (frm->ipfix_collector.as_u32 != collector.as_u32 ||
7119         frm->src_address.as_u32 != src.as_u32 ||
7120         frm->collector_port != collector_port)
7121             vnet_flow_reports_reset(frm);
7122
7123     frm->ipfix_collector.as_u32 = collector.as_u32;
7124     frm->collector_port = collector_port;
7125     frm->src_address.as_u32 = src.as_u32;
7126     frm->fib_index = fib_index;
7127     frm->path_mtu = path_mtu;
7128     frm->template_interval = template_interval;
7129
7130     /* Turn on the flow reporting process */
7131     vlib_process_signal_event (vm, flow_report_process_node.index,
7132                                1, 0);
7133
7134 out:
7135     REPLY_MACRO(VL_API_IPFIX_ENABLE_REPLY);
7136 }
7137
7138 static void vl_api_ipfix_dump_t_handler (vl_api_ipfix_dump_t *mp)
7139 {
7140     flow_report_main_t * frm = &flow_report_main;
7141     unix_shared_memory_queue_t * q;
7142     vl_api_ipfix_details_t *rmp;
7143
7144     q = vl_api_client_index_to_input_queue (mp->client_index);
7145
7146     rmp = vl_msg_api_alloc (sizeof (*rmp));
7147     memset (rmp, 0, sizeof (*rmp));
7148     rmp->_vl_msg_id = ntohs(VL_API_IPFIX_DETAILS);
7149     rmp->context = mp->context;
7150     memcpy(rmp->collector_address, frm->ipfix_collector.data,
7151            sizeof(frm->ipfix_collector.data));
7152     rmp->collector_port = htons(frm->collector_port);
7153     memcpy(rmp->src_address, frm->src_address.data,
7154            sizeof(frm->src_address.data));
7155     rmp->fib_index = htonl(frm->fib_index);
7156     rmp->path_mtu = htonl(frm->path_mtu);
7157     rmp->template_interval = htonl(frm->template_interval);
7158
7159     vl_msg_api_send_shmem (q, (u8 *)&rmp);
7160 }
7161
7162 #define BOUNCE_HANDLER(nn)                                              \
7163 static void vl_api_##nn##_t_handler (                                   \
7164     vl_api_##nn##_t *mp)                                                \
7165 {                                                                       \
7166     vpe_client_registration_t *reg;                                     \
7167     vpe_api_main_t * vam = &vpe_api_main;                               \
7168     unix_shared_memory_queue_t * q;                                     \
7169                                                                         \
7170     /* One registration only... */                                      \
7171     pool_foreach(reg, vam->nn##_registrations,                          \
7172     ({                                                                  \
7173         q = vl_api_client_index_to_input_queue (reg->client_index);     \
7174         if (q) {                                                        \
7175             /*                                                          \
7176              * If the queue is stuffed, turf the msg and complain       \
7177              * It's unlikely that the intended recipient is             \
7178              * alive; avoid deadlock at all costs.                      \
7179              */                                                         \
7180             if (q->cursize == q->maxsize) {                             \
7181                 clib_warning ("ERROR: receiver queue full, drop msg");  \
7182                 vl_msg_api_free (mp);                                   \
7183                 return;                                                 \
7184             }                                                           \
7185             vl_msg_api_send_shmem (q, (u8 *)&mp);                       \
7186             return;                                                     \
7187         }                                                               \
7188     }));                                                                \
7189     vl_msg_api_free (mp);                                               \
7190 }
7191
7192 /*
7193  * vpe_api_hookup
7194  * Add vpe's API message handlers to the table.
7195  * vlib has alread mapped shared memory and
7196  * added the client registration handlers.
7197  * See .../open-repo/vlib/memclnt_vlib.c:memclnt_process()
7198  */
7199
7200 static clib_error_t *
7201 vpe_api_hookup (vlib_main_t *vm)
7202 {
7203     api_main_t * am = &api_main;
7204
7205 #define _(N,n)                                                  \
7206     vl_msg_api_set_handlers(VL_API_##N, #n,                     \
7207                            vl_api_##n##_t_handler,              \
7208                            vl_noop_handler,                     \
7209                            vl_api_##n##_t_endian,               \
7210                            vl_api_##n##_t_print,                \
7211                            sizeof(vl_api_##n##_t), 1);
7212     foreach_vpe_api_msg;
7213 #undef _
7214
7215     /*
7216      * Manually register the sr tunnel add del msg, so we trace
7217      * enough bytes to capture a typical segment list
7218      */
7219     vl_msg_api_set_handlers (VL_API_SR_TUNNEL_ADD_DEL,
7220                              "sr_tunnel_add_del",
7221                              vl_api_sr_tunnel_add_del_t_handler,
7222                              vl_noop_handler,
7223                              vl_api_sr_tunnel_add_del_t_endian,
7224                              vl_api_sr_tunnel_add_del_t_print,
7225                              256, 1);
7226
7227
7228     /*
7229      * Manually register the sr policy add del msg, so we trace
7230      * enough bytes to capture a typical tunnel name list
7231      */
7232     vl_msg_api_set_handlers (VL_API_SR_POLICY_ADD_DEL,
7233                              "sr_policy_add_del",
7234                              vl_api_sr_policy_add_del_t_handler,
7235                              vl_noop_handler,
7236                              vl_api_sr_policy_add_del_t_endian,
7237                              vl_api_sr_policy_add_del_t_print,
7238                              256, 1);
7239
7240     /*
7241      * Trace space for 8 MPLS encap labels, classifier mask+match
7242      */
7243     am->api_trace_cfg [VL_API_MPLS_ADD_DEL_ENCAP].size += 8 * sizeof(u32);
7244     am->api_trace_cfg [VL_API_CLASSIFY_ADD_DEL_TABLE].size
7245         += 5 * sizeof (u32x4);
7246     am->api_trace_cfg [VL_API_CLASSIFY_ADD_DEL_SESSION].size
7247         += 5 * sizeof (u32x4);
7248     am->api_trace_cfg [VL_API_VXLAN_ADD_DEL_TUNNEL].size
7249         += 16 * sizeof (u32);
7250
7251     /*
7252      * Thread-safe API messages
7253      */
7254     am->is_mp_safe [VL_API_IP_ADD_DEL_ROUTE] = 1;
7255     am->is_mp_safe [VL_API_GET_NODE_GRAPH] = 1;
7256
7257     return 0;
7258 }
7259
7260 VLIB_API_INIT_FUNCTION(vpe_api_hookup);
7261
7262 static clib_error_t *
7263 vpe_api_init (vlib_main_t *vm)
7264 {
7265     vpe_api_main_t *am = &vpe_api_main;
7266
7267     am->vlib_main = vm;
7268     am->vnet_main = vnet_get_main();
7269     am->interface_events_registration_hash = hash_create (0, sizeof (uword));
7270     am->to_netconf_server_registration_hash = hash_create (0, sizeof (uword));
7271     am->from_netconf_server_registration_hash = hash_create (0, sizeof (uword));
7272     am->to_netconf_client_registration_hash = hash_create (0, sizeof (uword));
7273     am->from_netconf_client_registration_hash = hash_create (0, sizeof (uword));
7274     am->oam_events_registration_hash = hash_create (0, sizeof (uword));
7275
7276     vl_api_init (vm);
7277     vl_set_memory_region_name ("/vpe-api");
7278     vl_enable_disable_memory_api (vm, 1 /* enable it */);
7279
7280     return 0;
7281 }
7282
7283 VLIB_INIT_FUNCTION(vpe_api_init);
7284
7285
7286 static clib_error_t *
7287 api_segment_config (vlib_main_t * vm, unformat_input_t * input)
7288 {
7289   u8 * chroot_path;
7290   int uid, gid, rv;
7291   char *s, buf[128];
7292   struct passwd _pw, *pw;
7293   struct group _grp, *grp;
7294   clib_error_t *e;
7295
7296   while (unformat_check_input (input) != UNFORMAT_END_OF_INPUT)
7297     {
7298       if (unformat (input, "prefix %s", &chroot_path))
7299         {
7300           vec_add1 (chroot_path, 0);
7301           vl_set_memory_root_path ((char *)chroot_path);
7302         }
7303       else if (unformat (input, "uid %d", &uid))
7304         vl_set_memory_uid (uid);
7305       else if (unformat (input, "gid %d", &gid))
7306         vl_set_memory_gid (gid);
7307       else if (unformat (input, "uid %s", &s))
7308         {
7309           /* lookup the username */
7310           pw = NULL;
7311           rv = getpwnam_r(s, &_pw, buf, sizeof(buf), &pw);
7312           if (rv < 0)
7313             {
7314               e = clib_error_return_code(0, rv,
7315                        CLIB_ERROR_ERRNO_VALID | CLIB_ERROR_FATAL,
7316                        "cannot fetch username %s", s);
7317               vec_free (s);
7318               return e;
7319             }
7320           if (pw == NULL)
7321             {
7322               e = clib_error_return_fatal(0, "username %s does not exist", s);
7323               vec_free (s);
7324               return e;
7325             }
7326           vec_free (s);
7327           vl_set_memory_uid (pw->pw_uid);
7328         }
7329       else if (unformat (input, "gid %s", &s))
7330         {
7331           /* lookup the group name */
7332           grp = NULL;
7333           rv = getgrnam_r(s, &_grp, buf, sizeof(buf), &grp);
7334           if (rv != 0)
7335             {
7336               e = clib_error_return_code(0, rv,
7337                        CLIB_ERROR_ERRNO_VALID | CLIB_ERROR_FATAL,
7338                        "cannot fetch group %s", s);
7339               vec_free (s);
7340               return e;
7341             }
7342           if (grp == NULL)
7343             {
7344               e = clib_error_return_fatal(0, "group %s does not exist", s);
7345               vec_free (s);
7346               return e;
7347             }
7348           vec_free (s);
7349           vl_set_memory_gid (grp->gr_gid);
7350         }
7351       else
7352         return clib_error_return (0, "unknown input `%U'",
7353                                   format_unformat_error, input);
7354     }
7355   return 0;
7356 }
7357 VLIB_EARLY_CONFIG_FUNCTION (api_segment_config, "api-segment");
7358
7359 void * get_unformat_vnet_sw_interface (void)
7360 {
7361     return (void *) &unformat_vnet_sw_interface;
7362 }
7363
7364 #undef vl_api_version
7365 #define vl_api_version(n,v) static u32 vpe_api_version = v;
7366 #include <vpp-api/vpe.api.h>
7367 #undef vl_api_version
7368
7369 int vl_msg_api_version_check (vl_api_memclnt_create_t * mp)
7370 {
7371     if (clib_host_to_net_u32(mp->api_versions[0]) != vpe_api_version) {
7372         clib_warning ("vpe API mismatch: 0x%08x instead of 0x%08x",
7373                       clib_host_to_net_u32 (mp->api_versions[0]),
7374                       vpe_api_version);
7375         return -1;
7376     }
7377     return 0;
7378 }
7379
7380 static u8 * format_arp_event (u8 * s, va_list * args)
7381 {
7382     vl_api_ip4_arp_event_t * event = va_arg (*args, vl_api_ip4_arp_event_t *);
7383
7384     s = format (s, "pid %d: %U", event->pid,
7385                 format_ip4_address, &event->address);
7386     return s;
7387 }
7388
7389 static clib_error_t *
7390 show_ip4_arp_events_fn (vlib_main_t * vm,
7391                         unformat_input_t * input,
7392                         vlib_cli_command_t * cmd)
7393 {
7394     vpe_api_main_t * am = &vpe_api_main;
7395     vl_api_ip4_arp_event_t * event;
7396
7397     if (pool_elts (am->arp_events) == 0) {
7398         vlib_cli_output (vm, "No active arp event registrations");
7399         return 0;
7400     }
7401
7402     pool_foreach (event, am->arp_events,
7403     ({
7404         vlib_cli_output (vm, "%U", format_arp_event, event);
7405     }));
7406
7407     return 0;
7408 }
7409
7410 VLIB_CLI_COMMAND (show_ip4_arp_events, static) = {
7411   .path = "show arp event registrations",
7412   .function = show_ip4_arp_events_fn,
7413   .short_help = "Show arp event registrations",
7414 };