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