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