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