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