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