BD:unify bridge domain creation code
[vpp.git] / src / vpp / api / api.c
1 /*
2  *------------------------------------------------------------------
3  * api.c - message handler registration
4  *
5  * Copyright (c) 2010-2016 Cisco and/or its affiliates.
6  * Licensed under the Apache License, Version 2.0 (the "License");
7  * you may not use this file except in compliance with the License.
8  * You may obtain a copy of the License at:
9  *
10  *     http://www.apache.org/licenses/LICENSE-2.0
11  *
12  * Unless required by applicable law or agreed to in writing, software
13  * distributed under the License is distributed on an "AS IS" BASIS,
14  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15  * See the License for the specific language governing permissions and
16  * limitations under the License.
17  *------------------------------------------------------------------
18  */
19
20 #include <stdio.h>
21 #include <stdlib.h>
22 #include <string.h>
23 #include <sys/types.h>
24 #include <sys/mman.h>
25 #include <sys/stat.h>
26 #include <netinet/in.h>
27 #include <signal.h>
28 #include <pthread.h>
29 #include <unistd.h>
30 #include <time.h>
31 #include <fcntl.h>
32 #include <string.h>
33 #include <pwd.h>
34 #include <grp.h>
35
36 #include <vppinfra/clib.h>
37 #include <vppinfra/vec.h>
38 #include <vppinfra/hash.h>
39 #include <vppinfra/bitmap.h>
40 #include <vppinfra/fifo.h>
41 #include <vppinfra/time.h>
42 #include <vppinfra/mheap.h>
43 #include <vppinfra/heap.h>
44 #include <vppinfra/pool.h>
45 #include <vppinfra/format.h>
46 #include <vppinfra/error.h>
47
48 #include <vnet/api_errno.h>
49 #include <vnet/vnet.h>
50 #include <vnet/l2/l2_input.h>
51 #include <vnet/l2/l2_bd.h>
52 #include <vnet/ip/ip.h>
53 #include <vnet/ip/ip6.h>
54 #include <vnet/ip/ip6_neighbor.h>
55 #if WITH_LIBSSL > 0
56 #include <vnet/sr/sr.h>
57 #endif
58 #include <vlib/vlib.h>
59 #include <vlib/unix/unix.h>
60 #include <vlibapi/api.h>
61 #include <vlibmemory/api.h>
62 #include <vnet/classify/input_acl.h>
63 #include <vnet/l2/l2_classify.h>
64 #include <vnet/map/map.h>
65 #include <vnet/ip/ip6_hop_by_hop.h>
66 #include <vnet/ip/ip_source_and_port_range_check.h>
67 #include <vnet/ip/punt.h>
68 #include <vnet/feature/feature.h>
69
70 #undef BIHASH_TYPE
71 #undef __included_bihash_template_h__
72 #include <vnet/l2/l2_fib.h>
73
74 #include <vpp/stats/stats.h>
75 #include <vpp/oam/oam.h>
76
77 #include <vnet/ethernet/ethernet.h>
78 #include <vnet/ethernet/arp_packet.h>
79 #include <vnet/interface.h>
80 #include <vnet/l2/l2_fib.h>
81 #include <vnet/l2/l2_bd.h>
82 #include <vpp/api/vpe_msg_enum.h>
83 #include <vnet/span/span.h>
84 #include <vnet/fib/ip6_fib.h>
85 #include <vnet/fib/ip4_fib.h>
86 #include <vnet/fib/fib_api.h>
87 #include <vnet/dpo/drop_dpo.h>
88 #include <vnet/dpo/receive_dpo.h>
89 #include <vnet/dpo/lookup_dpo.h>
90 #include <vnet/dpo/classify_dpo.h>
91 #include <vnet/dpo/ip_null_dpo.h>
92 #define vl_typedefs             /* define message structures */
93 #include <vpp/api/vpe_all_api_h.h>
94 #undef vl_typedefs
95 #define vl_endianfun            /* define message structures */
96 #include <vpp/api/vpe_all_api_h.h>
97 #undef vl_endianfun
98 /* instantiate all the print functions we know about */
99 #define vl_print(handle, ...) vlib_cli_output (handle, __VA_ARGS__)
100 #define vl_printfun
101 #include <vpp/api/vpe_all_api_h.h>
102 #undef vl_printfun
103 #include <vlibapi/api_helper_macros.h>
104 #define foreach_vpe_api_msg                                             \
105 _(WANT_OAM_EVENTS, want_oam_events)                                     \
106 _(OAM_ADD_DEL, oam_add_del)                                             \
107 _(IS_ADDRESS_REACHABLE, is_address_reachable)                           \
108 _(SW_INTERFACE_SET_MPLS_ENABLE, sw_interface_set_mpls_enable)           \
109 _(SW_INTERFACE_SET_VPATH, sw_interface_set_vpath)                       \
110 _(SW_INTERFACE_SET_L2_XCONNECT, sw_interface_set_l2_xconnect)           \
111 _(SW_INTERFACE_SET_L2_BRIDGE, sw_interface_set_l2_bridge)               \
112 _(CREATE_VLAN_SUBIF, create_vlan_subif)                                 \
113 _(CREATE_SUBIF, create_subif)                                           \
114 _(PROXY_ARP_ADD_DEL, proxy_arp_add_del)                                 \
115 _(PROXY_ARP_INTFC_ENABLE_DISABLE, proxy_arp_intfc_enable_disable)       \
116 _(VNET_GET_SUMMARY_STATS, vnet_get_summary_stats)                       \
117 _(RESET_FIB, reset_fib)                                                 \
118 _(CREATE_LOOPBACK, create_loopback)                                     \
119 _(CREATE_LOOPBACK_INSTANCE, create_loopback_instance)                   \
120 _(CONTROL_PING, control_ping)                                           \
121 _(CLI_REQUEST, cli_request)                                             \
122 _(CLI_INBAND, cli_inband)                                               \
123 _(SET_ARP_NEIGHBOR_LIMIT, set_arp_neighbor_limit)                       \
124 _(L2_PATCH_ADD_DEL, l2_patch_add_del)                                   \
125 _(CLASSIFY_SET_INTERFACE_IP_TABLE, classify_set_interface_ip_table)     \
126 _(CLASSIFY_SET_INTERFACE_L2_TABLES, classify_set_interface_l2_tables)   \
127 _(GET_NODE_INDEX, get_node_index)                                       \
128 _(ADD_NODE_NEXT, add_node_next)                                         \
129 _(L2_INTERFACE_EFP_FILTER, l2_interface_efp_filter)                     \
130 _(SHOW_VERSION, show_version)                                           \
131 _(INTERFACE_NAME_RENUMBER, interface_name_renumber)                     \
132 _(WANT_IP4_ARP_EVENTS, want_ip4_arp_events)                             \
133 _(WANT_IP6_ND_EVENTS, want_ip6_nd_events)                               \
134 _(INPUT_ACL_SET_INTERFACE, input_acl_set_interface)                     \
135 _(DELETE_LOOPBACK, delete_loopback)                                     \
136 _(BD_IP_MAC_ADD_DEL, bd_ip_mac_add_del)                                 \
137 _(GET_NODE_GRAPH, get_node_graph)                                       \
138 _(IOAM_ENABLE, ioam_enable)                                             \
139 _(IOAM_DISABLE, ioam_disable)                                           \
140 _(GET_NEXT_INDEX, get_next_index)                                       \
141 _(PG_CREATE_INTERFACE, pg_create_interface)                             \
142 _(PG_CAPTURE, pg_capture)                                               \
143 _(PG_ENABLE_DISABLE, pg_enable_disable)                                 \
144 _(IP_SOURCE_AND_PORT_RANGE_CHECK_ADD_DEL,                               \
145   ip_source_and_port_range_check_add_del)                               \
146 _(IP_SOURCE_AND_PORT_RANGE_CHECK_INTERFACE_ADD_DEL,                     \
147   ip_source_and_port_range_check_interface_add_del)                     \
148 _(DELETE_SUBIF, delete_subif)                                           \
149 _(PUNT, punt)                                                           \
150 _(FEATURE_ENABLE_DISABLE, feature_enable_disable)
151
152 #define QUOTE_(x) #x
153 #define QUOTE(x) QUOTE_(x)
154 typedef enum
155 {
156   RESOLVE_IP4_ADD_DEL_ROUTE = 1,
157   RESOLVE_IP6_ADD_DEL_ROUTE,
158 } resolve_t;
159
160 static vlib_node_registration_t vpe_resolver_process_node;
161 extern vpe_api_main_t vpe_api_main;
162
163 static int arp_change_delete_callback (u32 pool_index, u8 * notused);
164 static int nd_change_delete_callback (u32 pool_index, u8 * notused);
165
166 /* Clean up all registrations belonging to the indicated client */
167 static clib_error_t *
168 memclnt_delete_callback (u32 client_index)
169 {
170   vpe_api_main_t *vam = &vpe_api_main;
171   vpe_client_registration_t *rp;
172   uword *p;
173   int stats_memclnt_delete_callback (u32 client_index);
174
175   stats_memclnt_delete_callback (client_index);
176
177 #define _(a)                                                    \
178     p = hash_get (vam->a##_registration_hash, client_index);    \
179     if (p) {                                                    \
180         rp = pool_elt_at_index (vam->a##_registrations, p[0]);  \
181         pool_put (vam->a##_registrations, rp);                  \
182         hash_unset (vam->a##_registration_hash, client_index);  \
183     }
184   foreach_registration_hash;
185 #undef _
186   return 0;
187 }
188
189 VL_MSG_API_REAPER_FUNCTION (memclnt_delete_callback);
190
191 pub_sub_handler (oam_events, OAM_EVENTS);
192
193 #define RESOLUTION_EVENT 1
194 #define RESOLUTION_PENDING_EVENT 2
195 #define IP4_ARP_EVENT 3
196 #define IP6_ND_EVENT 4
197
198 int ip4_add_del_route_t_handler (vl_api_ip_add_del_route_t * mp);
199
200 int ip6_add_del_route_t_handler (vl_api_ip_add_del_route_t * mp);
201
202 static void
203 handle_ip4_arp_event (u32 pool_index)
204 {
205   vpe_api_main_t *vam = &vpe_api_main;
206   vnet_main_t *vnm = vam->vnet_main;
207   vlib_main_t *vm = vam->vlib_main;
208   vl_api_ip4_arp_event_t *event;
209   vl_api_ip4_arp_event_t *mp;
210   unix_shared_memory_queue_t *q;
211
212   /* Client can cancel, die, etc. */
213   if (pool_is_free_index (vam->arp_events, pool_index))
214     return;
215
216   event = pool_elt_at_index (vam->arp_events, pool_index);
217
218   q = vl_api_client_index_to_input_queue (event->client_index);
219   if (!q)
220     {
221       (void) vnet_add_del_ip4_arp_change_event
222         (vnm, arp_change_delete_callback,
223          event->pid, &event->address,
224          vpe_resolver_process_node.index, IP4_ARP_EVENT,
225          ~0 /* pool index, notused */ , 0 /* is_add */ );
226       return;
227     }
228
229   if (q->cursize < q->maxsize)
230     {
231       mp = vl_msg_api_alloc (sizeof (*mp));
232       clib_memcpy (mp, event, sizeof (*mp));
233       vl_msg_api_send_shmem (q, (u8 *) & mp);
234     }
235   else
236     {
237       static f64 last_time;
238       /*
239        * Throttle syslog msgs.
240        * It's pretty tempting to just revoke the registration...
241        */
242       if (vlib_time_now (vm) > last_time + 10.0)
243         {
244           clib_warning ("arp event for %U to pid %d: queue stuffed!",
245                         format_ip4_address, &event->address, event->pid);
246           last_time = vlib_time_now (vm);
247         }
248     }
249 }
250
251 void
252 handle_ip6_nd_event (u32 pool_index)
253 {
254   vpe_api_main_t *vam = &vpe_api_main;
255   vnet_main_t *vnm = vam->vnet_main;
256   vlib_main_t *vm = vam->vlib_main;
257   vl_api_ip6_nd_event_t *event;
258   vl_api_ip6_nd_event_t *mp;
259   unix_shared_memory_queue_t *q;
260
261   /* Client can cancel, die, etc. */
262   if (pool_is_free_index (vam->nd_events, pool_index))
263     return;
264
265   event = pool_elt_at_index (vam->nd_events, pool_index);
266
267   q = vl_api_client_index_to_input_queue (event->client_index);
268   if (!q)
269     {
270       (void) vnet_add_del_ip6_nd_change_event
271         (vnm, nd_change_delete_callback,
272          event->pid, &event->address,
273          vpe_resolver_process_node.index, IP6_ND_EVENT,
274          ~0 /* pool index, notused */ , 0 /* is_add */ );
275       return;
276     }
277
278   if (q->cursize < q->maxsize)
279     {
280       mp = vl_msg_api_alloc (sizeof (*mp));
281       clib_memcpy (mp, event, sizeof (*mp));
282       vl_msg_api_send_shmem (q, (u8 *) & mp);
283     }
284   else
285     {
286       static f64 last_time;
287       /*
288        * Throttle syslog msgs.
289        * It's pretty tempting to just revoke the registration...
290        */
291       if (vlib_time_now (vm) > last_time + 10.0)
292         {
293           clib_warning ("ip6 nd event for %U to pid %d: queue stuffed!",
294                         format_ip6_address, &event->address, event->pid);
295           last_time = vlib_time_now (vm);
296         }
297     }
298 }
299
300 static uword
301 resolver_process (vlib_main_t * vm,
302                   vlib_node_runtime_t * rt, vlib_frame_t * f)
303 {
304   uword event_type;
305   uword *event_data = 0;
306   f64 timeout = 100.0;
307   int i;
308
309   while (1)
310     {
311       vlib_process_wait_for_event_or_clock (vm, timeout);
312
313       event_type = vlib_process_get_events (vm, &event_data);
314
315       switch (event_type)
316         {
317         case RESOLUTION_PENDING_EVENT:
318           timeout = 1.0;
319           break;
320
321         case RESOLUTION_EVENT:
322           clib_warning ("resolver: BOGUS TYPE");
323           break;
324
325         case IP4_ARP_EVENT:
326           for (i = 0; i < vec_len (event_data); i++)
327             handle_ip4_arp_event (event_data[i]);
328           break;
329
330         case IP6_ND_EVENT:
331           for (i = 0; i < vec_len (event_data); i++)
332             handle_ip6_nd_event (event_data[i]);
333           break;
334
335         case ~0:                /* timeout */
336           break;
337         }
338
339       vec_reset_length (event_data);
340     }
341   return 0;                     /* or not */
342 }
343
344 /* *INDENT-OFF* */
345 VLIB_REGISTER_NODE (vpe_resolver_process_node,static) = {
346   .function = resolver_process,
347   .type = VLIB_NODE_TYPE_PROCESS,
348   .name = "vpe-route-resolver-process",
349 };
350 /* *INDENT-ON* */
351
352 static void
353 vl_api_sw_interface_set_vpath_t_handler (vl_api_sw_interface_set_vpath_t * mp)
354 {
355   vl_api_sw_interface_set_vpath_reply_t *rmp;
356   int rv = 0;
357   u32 sw_if_index = ntohl (mp->sw_if_index);
358
359   VALIDATE_SW_IF_INDEX (mp);
360
361   l2input_intf_bitmap_enable (sw_if_index, L2INPUT_FEAT_VPATH, mp->enable);
362   vnet_feature_enable_disable ("ip4-unicast", "vpath-input-ip4",
363                                sw_if_index, mp->enable, 0, 0);
364   vnet_feature_enable_disable ("ip4-multicast", "vpath-input-ip4",
365                                sw_if_index, mp->enable, 0, 0);
366   vnet_feature_enable_disable ("ip6-unicast", "vpath-input-ip6",
367                                sw_if_index, mp->enable, 0, 0);
368   vnet_feature_enable_disable ("ip6-multicast", "vpath-input-ip6",
369                                sw_if_index, mp->enable, 0, 0);
370
371   BAD_SW_IF_INDEX_LABEL;
372
373   REPLY_MACRO (VL_API_SW_INTERFACE_SET_VPATH_REPLY);
374 }
375
376 static void
377   vl_api_sw_interface_set_l2_xconnect_t_handler
378   (vl_api_sw_interface_set_l2_xconnect_t * mp)
379 {
380   vl_api_sw_interface_set_l2_xconnect_reply_t *rmp;
381   int rv = 0;
382   u32 rx_sw_if_index = ntohl (mp->rx_sw_if_index);
383   u32 tx_sw_if_index = ntohl (mp->tx_sw_if_index);
384   vlib_main_t *vm = vlib_get_main ();
385   vnet_main_t *vnm = vnet_get_main ();
386
387   VALIDATE_RX_SW_IF_INDEX (mp);
388
389   if (mp->enable)
390     {
391       VALIDATE_TX_SW_IF_INDEX (mp);
392       rv = set_int_l2_mode (vm, vnm, MODE_L2_XC,
393                             rx_sw_if_index, 0, 0, 0, tx_sw_if_index);
394     }
395   else
396     {
397       rv = set_int_l2_mode (vm, vnm, MODE_L3, rx_sw_if_index, 0, 0, 0, 0);
398     }
399
400   BAD_RX_SW_IF_INDEX_LABEL;
401   BAD_TX_SW_IF_INDEX_LABEL;
402
403   REPLY_MACRO (VL_API_SW_INTERFACE_SET_L2_XCONNECT_REPLY);
404 }
405
406 static void
407   vl_api_sw_interface_set_l2_bridge_t_handler
408   (vl_api_sw_interface_set_l2_bridge_t * mp)
409 {
410   bd_main_t *bdm = &bd_main;
411   vl_api_sw_interface_set_l2_bridge_reply_t *rmp;
412   int rv = 0;
413   vlib_main_t *vm = vlib_get_main ();
414   vnet_main_t *vnm = vnet_get_main ();
415
416   VALIDATE_RX_SW_IF_INDEX (mp);
417   u32 rx_sw_if_index = ntohl (mp->rx_sw_if_index);
418
419
420   if (mp->enable)
421     {
422       u32 bd_id = ntohl (mp->bd_id);
423       u32 bd_index = bd_find_or_add_bd_index (bdm, bd_id);
424       u32 bvi = mp->bvi;
425       u8 shg = mp->shg;
426       rv = set_int_l2_mode (vm, vnm, MODE_L2_BRIDGE,
427                             rx_sw_if_index, bd_index, bvi, shg, 0);
428     }
429   else
430     {
431       rv = set_int_l2_mode (vm, vnm, MODE_L3, rx_sw_if_index, 0, 0, 0, 0);
432     }
433
434   BAD_RX_SW_IF_INDEX_LABEL;
435
436   REPLY_MACRO (VL_API_SW_INTERFACE_SET_L2_BRIDGE_REPLY);
437 }
438
439 static void
440 vl_api_bd_ip_mac_add_del_t_handler (vl_api_bd_ip_mac_add_del_t * mp)
441 {
442   bd_main_t *bdm = &bd_main;
443   vl_api_bd_ip_mac_add_del_reply_t *rmp;
444   int rv = 0;
445   u32 bd_id = ntohl (mp->bd_id);
446   u32 bd_index;
447   uword *p;
448
449   p = hash_get (bdm->bd_index_by_bd_id, bd_id);
450   if (p == 0)
451     {
452       rv = VNET_API_ERROR_NO_SUCH_ENTRY;
453       goto out;
454     }
455
456   bd_index = p[0];
457   if (bd_add_del_ip_mac (bd_index, mp->ip_address,
458                          mp->mac_address, mp->is_ipv6, mp->is_add))
459     rv = VNET_API_ERROR_UNSPECIFIED;
460
461 out:
462   REPLY_MACRO (VL_API_BD_IP_MAC_ADD_DEL_REPLY);
463 }
464
465 static void
466 vl_api_create_vlan_subif_t_handler (vl_api_create_vlan_subif_t * mp)
467 {
468   vl_api_create_vlan_subif_reply_t *rmp;
469   vnet_main_t *vnm = vnet_get_main ();
470   u32 sw_if_index = (u32) ~ 0;
471   vnet_hw_interface_t *hi;
472   int rv = 0;
473   u32 id;
474   vnet_sw_interface_t template;
475   uword *p;
476   vnet_interface_main_t *im = &vnm->interface_main;
477   u64 sup_and_sub_key;
478   unix_shared_memory_queue_t *q;
479   clib_error_t *error;
480
481   VALIDATE_SW_IF_INDEX (mp);
482
483   hi = vnet_get_sup_hw_interface (vnm, ntohl (mp->sw_if_index));
484
485   if (hi->bond_info == VNET_HW_INTERFACE_BOND_INFO_SLAVE)
486     {
487       rv = VNET_API_ERROR_BOND_SLAVE_NOT_ALLOWED;
488       goto out;
489     }
490
491   id = ntohl (mp->vlan_id);
492   if (id == 0 || id > 4095)
493     {
494       rv = VNET_API_ERROR_INVALID_VLAN;
495       goto out;
496     }
497
498   sup_and_sub_key = ((u64) (hi->sw_if_index) << 32) | (u64) id;
499
500   p = hash_get_mem (im->sw_if_index_by_sup_and_sub, &sup_and_sub_key);
501   if (p)
502     {
503       rv = VNET_API_ERROR_VLAN_ALREADY_EXISTS;
504       goto out;
505     }
506
507   memset (&template, 0, sizeof (template));
508   template.type = VNET_SW_INTERFACE_TYPE_SUB;
509   template.sup_sw_if_index = hi->sw_if_index;
510   template.sub.id = id;
511   template.sub.eth.raw_flags = 0;
512   template.sub.eth.flags.one_tag = 1;
513   template.sub.eth.outer_vlan_id = id;
514   template.sub.eth.flags.exact_match = 1;
515
516   error = vnet_create_sw_interface (vnm, &template, &sw_if_index);
517   if (error)
518     {
519       clib_error_report (error);
520       rv = VNET_API_ERROR_INVALID_REGISTRATION;
521       goto out;
522     }
523
524   u64 *kp = clib_mem_alloc (sizeof (*kp));
525   *kp = sup_and_sub_key;
526
527   hash_set (hi->sub_interface_sw_if_index_by_id, id, sw_if_index);
528   hash_set_mem (im->sw_if_index_by_sup_and_sub, kp, sw_if_index);
529
530   BAD_SW_IF_INDEX_LABEL;
531
532 out:
533   q = vl_api_client_index_to_input_queue (mp->client_index);
534   if (!q)
535     return;
536
537   rmp = vl_msg_api_alloc (sizeof (*rmp));
538   rmp->_vl_msg_id = htons (VL_API_CREATE_VLAN_SUBIF_REPLY);
539   rmp->context = mp->context;
540   rmp->retval = htonl (rv);
541   rmp->sw_if_index = htonl (sw_if_index);
542   vl_msg_api_send_shmem (q, (u8 *) & rmp);
543 }
544
545 static void
546 vl_api_create_subif_t_handler (vl_api_create_subif_t * mp)
547 {
548   vl_api_create_subif_reply_t *rmp;
549   vnet_main_t *vnm = vnet_get_main ();
550   u32 sw_if_index = ~0;
551   int rv = 0;
552   u32 sub_id;
553   vnet_sw_interface_t *si;
554   vnet_hw_interface_t *hi;
555   vnet_sw_interface_t template;
556   uword *p;
557   vnet_interface_main_t *im = &vnm->interface_main;
558   u64 sup_and_sub_key;
559   clib_error_t *error;
560
561   VALIDATE_SW_IF_INDEX (mp);
562
563   si = vnet_get_sup_sw_interface (vnm, ntohl (mp->sw_if_index));
564   hi = vnet_get_sup_hw_interface (vnm, ntohl (mp->sw_if_index));
565
566   if (hi->bond_info == VNET_HW_INTERFACE_BOND_INFO_SLAVE)
567     {
568       rv = VNET_API_ERROR_BOND_SLAVE_NOT_ALLOWED;
569       goto out;
570     }
571
572   sw_if_index = si->sw_if_index;
573   sub_id = ntohl (mp->sub_id);
574
575   sup_and_sub_key = ((u64) (sw_if_index) << 32) | (u64) sub_id;
576
577   p = hash_get_mem (im->sw_if_index_by_sup_and_sub, &sup_and_sub_key);
578   if (p)
579     {
580       if (CLIB_DEBUG > 0)
581         clib_warning ("sup sw_if_index %d, sub id %d already exists\n",
582                       sw_if_index, sub_id);
583       rv = VNET_API_ERROR_SUBIF_ALREADY_EXISTS;
584       goto out;
585     }
586
587   memset (&template, 0, sizeof (template));
588   template.type = VNET_SW_INTERFACE_TYPE_SUB;
589   template.sup_sw_if_index = sw_if_index;
590   template.sub.id = sub_id;
591   template.sub.eth.flags.no_tags = mp->no_tags;
592   template.sub.eth.flags.one_tag = mp->one_tag;
593   template.sub.eth.flags.two_tags = mp->two_tags;
594   template.sub.eth.flags.dot1ad = mp->dot1ad;
595   template.sub.eth.flags.exact_match = mp->exact_match;
596   template.sub.eth.flags.default_sub = mp->default_sub;
597   template.sub.eth.flags.outer_vlan_id_any = mp->outer_vlan_id_any;
598   template.sub.eth.flags.inner_vlan_id_any = mp->inner_vlan_id_any;
599   template.sub.eth.outer_vlan_id = ntohs (mp->outer_vlan_id);
600   template.sub.eth.inner_vlan_id = ntohs (mp->inner_vlan_id);
601
602   error = vnet_create_sw_interface (vnm, &template, &sw_if_index);
603   if (error)
604     {
605       clib_error_report (error);
606       rv = VNET_API_ERROR_SUBIF_CREATE_FAILED;
607       goto out;
608     }
609
610   u64 *kp = clib_mem_alloc (sizeof (*kp));
611   *kp = sup_and_sub_key;
612
613   hash_set (hi->sub_interface_sw_if_index_by_id, sub_id, sw_if_index);
614   hash_set_mem (im->sw_if_index_by_sup_and_sub, kp, sw_if_index);
615
616   BAD_SW_IF_INDEX_LABEL;
617
618 out:
619
620   /* *INDENT-OFF* */
621   REPLY_MACRO2(VL_API_CREATE_SUBIF_REPLY,
622   ({
623     rmp->sw_if_index = ntohl(sw_if_index);
624   }));
625   /* *INDENT-ON* */
626 }
627
628 static void
629 vl_api_proxy_arp_add_del_t_handler (vl_api_proxy_arp_add_del_t * mp)
630 {
631   vl_api_proxy_arp_add_del_reply_t *rmp;
632   u32 fib_index;
633   int rv;
634   ip4_main_t *im = &ip4_main;
635   stats_main_t *sm = &stats_main;
636   int vnet_proxy_arp_add_del (ip4_address_t * lo_addr,
637                               ip4_address_t * hi_addr,
638                               u32 fib_index, int is_del);
639   uword *p;
640
641   dslock (sm, 1 /* release hint */ , 6 /* tag */ );
642
643   p = hash_get (im->fib_index_by_table_id, ntohl (mp->vrf_id));
644
645   if (!p)
646     {
647       rv = VNET_API_ERROR_NO_SUCH_FIB;
648       goto out;
649     }
650
651   fib_index = p[0];
652
653   rv = vnet_proxy_arp_add_del ((ip4_address_t *) mp->low_address,
654                                (ip4_address_t *) mp->hi_address,
655                                fib_index, mp->is_add == 0);
656
657 out:
658   dsunlock (sm);
659   REPLY_MACRO (VL_API_PROXY_ARP_ADD_DEL_REPLY);
660 }
661
662 static void
663   vl_api_proxy_arp_intfc_enable_disable_t_handler
664   (vl_api_proxy_arp_intfc_enable_disable_t * mp)
665 {
666   int rv = 0;
667   vnet_main_t *vnm = vnet_get_main ();
668   vl_api_proxy_arp_intfc_enable_disable_reply_t *rmp;
669
670   VALIDATE_SW_IF_INDEX (mp);
671
672   vnet_sw_interface_t *si =
673     vnet_get_sw_interface (vnm, ntohl (mp->sw_if_index));
674
675   ASSERT (si);
676
677   if (mp->enable_disable)
678     si->flags |= VNET_SW_INTERFACE_FLAG_PROXY_ARP;
679   else
680     si->flags &= ~VNET_SW_INTERFACE_FLAG_PROXY_ARP;
681
682   BAD_SW_IF_INDEX_LABEL;
683
684 out:
685   REPLY_MACRO (VL_API_PROXY_ARP_INTFC_ENABLE_DISABLE_REPLY);
686 }
687
688 static void
689 vl_api_is_address_reachable_t_handler (vl_api_is_address_reachable_t * mp)
690 {
691 #if 0
692   vpe_main_t *rm = &vpe_main;
693   ip4_main_t *im4 = &ip4_main;
694   ip6_main_t *im6 = &ip6_main;
695   ip_lookup_main_t *lm;
696   union
697   {
698     ip4_address_t ip4;
699     ip6_address_t ip6;
700   } addr;
701   u32 adj_index, sw_if_index;
702   vl_api_is_address_reachable_t *rmp;
703   ip_adjacency_t *adj;
704   unix_shared_memory_queue_t *q;
705
706   q = vl_api_client_index_to_input_queue (mp->client_index);
707   if (!q)
708     {
709       increment_missing_api_client_counter (rm->vlib_main);
710       return;
711     }
712
713   rmp = vl_msg_api_alloc (sizeof (*rmp));
714   clib_memcpy (rmp, mp, sizeof (*rmp));
715
716   sw_if_index = mp->next_hop_sw_if_index;
717   clib_memcpy (&addr, mp->address, sizeof (addr));
718   if (mp->is_ipv6)
719     {
720       lm = &im6->lookup_main;
721       adj_index = ip6_fib_lookup (im6, sw_if_index, &addr.ip6);
722     }
723   else
724     {
725       lm = &im4->lookup_main;
726       // FIXME NOT an ADJ
727       adj_index = ip4_fib_lookup (im4, sw_if_index, &addr.ip4);
728     }
729   if (adj_index == ~0)
730     {
731       rmp->is_error = 1;
732       goto send;
733     }
734   adj = ip_get_adjacency (lm, adj_index);
735
736   if (adj->lookup_next_index == IP_LOOKUP_NEXT_REWRITE
737       && adj->rewrite_header.sw_if_index == sw_if_index)
738     {
739       rmp->is_known = 1;
740     }
741   else
742     {
743       if (adj->lookup_next_index == IP_LOOKUP_NEXT_ARP
744           && adj->rewrite_header.sw_if_index == sw_if_index)
745         {
746           if (mp->is_ipv6)
747             ip6_probe_neighbor (rm->vlib_main, &addr.ip6, sw_if_index);
748           else
749             ip4_probe_neighbor (rm->vlib_main, &addr.ip4, sw_if_index);
750         }
751       else if (adj->lookup_next_index == IP_LOOKUP_NEXT_DROP)
752         {
753           rmp->is_known = 1;
754           goto send;
755         }
756       rmp->is_known = 0;
757     }
758
759 send:
760   vl_msg_api_send_shmem (q, (u8 *) & rmp);
761 #endif
762 }
763
764 static void
765   vl_api_sw_interface_set_mpls_enable_t_handler
766   (vl_api_sw_interface_set_mpls_enable_t * mp)
767 {
768   vl_api_sw_interface_set_mpls_enable_reply_t *rmp;
769   int rv = 0;
770
771   VALIDATE_SW_IF_INDEX (mp);
772
773   mpls_sw_interface_enable_disable (&mpls_main,
774                                     ntohl (mp->sw_if_index), mp->enable);
775
776   BAD_SW_IF_INDEX_LABEL;
777   REPLY_MACRO (VL_API_SW_INTERFACE_SET_MPLS_ENABLE_REPLY);
778 }
779
780 void
781 send_oam_event (oam_target_t * t)
782 {
783   vpe_api_main_t *vam = &vpe_api_main;
784   unix_shared_memory_queue_t *q;
785   vpe_client_registration_t *reg;
786   vl_api_oam_event_t *mp;
787
788   /* *INDENT-OFF* */
789   pool_foreach(reg, vam->oam_events_registrations,
790   ({
791     q = vl_api_client_index_to_input_queue (reg->client_index);
792     if (q)
793       {
794         mp = vl_msg_api_alloc (sizeof (*mp));
795         mp->_vl_msg_id = ntohs (VL_API_OAM_EVENT);
796         clib_memcpy (mp->dst_address, &t->dst_address,
797                      sizeof (mp->dst_address));
798         mp->state = t->state;
799         vl_msg_api_send_shmem (q, (u8 *)&mp);
800       }
801   }));
802   /* *INDENT-ON* */
803 }
804
805 static void
806 vl_api_oam_add_del_t_handler (vl_api_oam_add_del_t * mp)
807 {
808   vl_api_oam_add_del_reply_t *rmp;
809   int rv;
810
811   rv = vpe_oam_add_del_target ((ip4_address_t *) mp->src_address,
812                                (ip4_address_t *) mp->dst_address,
813                                ntohl (mp->vrf_id), (int) (mp->is_add));
814
815   REPLY_MACRO (VL_API_OAM_ADD_DEL_REPLY);
816 }
817
818 static void
819 vl_api_vnet_get_summary_stats_t_handler (vl_api_vnet_get_summary_stats_t * mp)
820 {
821   stats_main_t *sm = &stats_main;
822   vnet_interface_main_t *im = sm->interface_main;
823   vl_api_vnet_summary_stats_reply_t *rmp;
824   vlib_combined_counter_main_t *cm;
825   vlib_counter_t v;
826   int i, which;
827   u64 total_pkts[VLIB_N_RX_TX];
828   u64 total_bytes[VLIB_N_RX_TX];
829
830   unix_shared_memory_queue_t *q =
831     vl_api_client_index_to_input_queue (mp->client_index);
832
833   if (!q)
834     return;
835
836   rmp = vl_msg_api_alloc (sizeof (*rmp));
837   rmp->_vl_msg_id = ntohs (VL_API_VNET_SUMMARY_STATS_REPLY);
838   rmp->context = mp->context;
839   rmp->retval = 0;
840
841   memset (total_pkts, 0, sizeof (total_pkts));
842   memset (total_bytes, 0, sizeof (total_bytes));
843
844   vnet_interface_counter_lock (im);
845
846   vec_foreach (cm, im->combined_sw_if_counters)
847   {
848     which = cm - im->combined_sw_if_counters;
849
850     for (i = 0; i < vlib_combined_counter_n_counters (cm); i++)
851       {
852         vlib_get_combined_counter (cm, i, &v);
853         total_pkts[which] += v.packets;
854         total_bytes[which] += v.bytes;
855       }
856   }
857   vnet_interface_counter_unlock (im);
858
859   rmp->total_pkts[VLIB_RX] = clib_host_to_net_u64 (total_pkts[VLIB_RX]);
860   rmp->total_bytes[VLIB_RX] = clib_host_to_net_u64 (total_bytes[VLIB_RX]);
861   rmp->total_pkts[VLIB_TX] = clib_host_to_net_u64 (total_pkts[VLIB_TX]);
862   rmp->total_bytes[VLIB_TX] = clib_host_to_net_u64 (total_bytes[VLIB_TX]);
863   rmp->vector_rate =
864     clib_host_to_net_u64 (vlib_last_vector_length_per_node (sm->vlib_main));
865
866   vl_msg_api_send_shmem (q, (u8 *) & rmp);
867 }
868
869 /* *INDENT-OFF* */
870 typedef CLIB_PACKED (struct {
871   ip4_address_t address;
872   u32 address_length: 6;
873   u32 index:26;
874 }) ip4_route_t;
875 /* *INDENT-ON* */
876
877 static int
878 ip4_reset_fib_t_handler (vl_api_reset_fib_t * mp)
879 {
880   vnet_main_t *vnm = vnet_get_main ();
881   vnet_interface_main_t *im = &vnm->interface_main;
882   ip4_main_t *im4 = &ip4_main;
883   static u32 *sw_if_indices_to_shut;
884   stats_main_t *sm = &stats_main;
885   fib_table_t *fib_table;
886   ip4_fib_t *fib;
887   u32 sw_if_index;
888   int i;
889   int rv = VNET_API_ERROR_NO_SUCH_FIB;
890   u32 target_fib_id = ntohl (mp->vrf_id);
891
892   dslock (sm, 1 /* release hint */ , 8 /* tag */ );
893
894   /* *INDENT-OFF* */
895   pool_foreach (fib_table, im4->fibs,
896   ({
897     vnet_sw_interface_t * si;
898
899     fib = pool_elt_at_index (im4->v4_fibs, fib_table->ft_index);
900
901     if (fib->table_id != target_fib_id)
902       continue;
903
904     /* remove any mpls encap/decap labels */
905     mpls_fib_reset_labels (fib->table_id);
906
907     /* remove any proxy arps in this fib */
908     vnet_proxy_arp_fib_reset (fib->table_id);
909
910     /* Set the flow hash for this fib to the default */
911     vnet_set_ip4_flow_hash (fib->table_id, IP_FLOW_HASH_DEFAULT);
912
913     vec_reset_length (sw_if_indices_to_shut);
914
915     /* Shut down interfaces in this FIB / clean out intfc routes */
916     pool_foreach (si, im->sw_interfaces,
917     ({
918       u32 sw_if_index = si->sw_if_index;
919
920       if (sw_if_index < vec_len (im4->fib_index_by_sw_if_index)
921           && (im4->fib_index_by_sw_if_index[si->sw_if_index] ==
922               fib->index))
923         vec_add1 (sw_if_indices_to_shut, si->sw_if_index);
924     }));
925
926     for (i = 0; i < vec_len (sw_if_indices_to_shut); i++) {
927       sw_if_index = sw_if_indices_to_shut[i];
928
929       u32 flags = vnet_sw_interface_get_flags (vnm, sw_if_index);
930       flags &= ~(VNET_SW_INTERFACE_FLAG_ADMIN_UP);
931       vnet_sw_interface_set_flags (vnm, sw_if_index, flags);
932     }
933
934     fib_table_flush(fib->index, FIB_PROTOCOL_IP4, FIB_SOURCE_API);
935
936     rv = 0;
937     break;
938     })); /* pool_foreach (fib) */
939     /* *INDENT-ON* */
940
941   dsunlock (sm);
942   return rv;
943 }
944
945 static int
946 ip6_reset_fib_t_handler (vl_api_reset_fib_t * mp)
947 {
948   vnet_main_t *vnm = vnet_get_main ();
949   vnet_interface_main_t *im = &vnm->interface_main;
950   ip6_main_t *im6 = &ip6_main;
951   stats_main_t *sm = &stats_main;
952   static u32 *sw_if_indices_to_shut;
953   fib_table_t *fib_table;
954   ip6_fib_t *fib;
955   u32 sw_if_index;
956   int i;
957   int rv = VNET_API_ERROR_NO_SUCH_FIB;
958   u32 target_fib_id = ntohl (mp->vrf_id);
959
960   dslock (sm, 1 /* release hint */ , 9 /* tag */ );
961
962   /* *INDENT-OFF* */
963   pool_foreach (fib_table, im6->fibs,
964   ({
965     vnet_sw_interface_t * si;
966
967     fib = pool_elt_at_index (im6->v6_fibs, fib_table->ft_index);
968
969     if (fib->table_id != target_fib_id)
970       continue;
971
972     vec_reset_length (sw_if_indices_to_shut);
973
974     /* Shut down interfaces in this FIB / clean out intfc routes */
975     pool_foreach (si, im->sw_interfaces,
976     ({
977       if (im6->fib_index_by_sw_if_index[si->sw_if_index] ==
978           fib->index)
979         vec_add1 (sw_if_indices_to_shut, si->sw_if_index);
980     }));
981
982     for (i = 0; i < vec_len (sw_if_indices_to_shut); i++) {
983       sw_if_index = sw_if_indices_to_shut[i];
984
985       u32 flags = vnet_sw_interface_get_flags (vnm, sw_if_index);
986       flags &= ~(VNET_SW_INTERFACE_FLAG_ADMIN_UP);
987       vnet_sw_interface_set_flags (vnm, sw_if_index, flags);
988     }
989
990     fib_table_flush(fib->index, FIB_PROTOCOL_IP6, FIB_SOURCE_API);
991
992     rv = 0;
993     break;
994   })); /* pool_foreach (fib) */
995   /* *INDENT-ON* */
996
997   dsunlock (sm);
998   return rv;
999 }
1000
1001 static void
1002 vl_api_reset_fib_t_handler (vl_api_reset_fib_t * mp)
1003 {
1004   int rv;
1005   vl_api_reset_fib_reply_t *rmp;
1006
1007   if (mp->is_ipv6)
1008     rv = ip6_reset_fib_t_handler (mp);
1009   else
1010     rv = ip4_reset_fib_t_handler (mp);
1011
1012   REPLY_MACRO (VL_API_RESET_FIB_REPLY);
1013 }
1014
1015 static void
1016 vl_api_create_loopback_t_handler (vl_api_create_loopback_t * mp)
1017 {
1018   vl_api_create_loopback_reply_t *rmp;
1019   u32 sw_if_index;
1020   int rv;
1021
1022   rv = vnet_create_loopback_interface (&sw_if_index, mp->mac_address, 0, 0);
1023
1024   /* *INDENT-OFF* */
1025   REPLY_MACRO2(VL_API_CREATE_LOOPBACK_REPLY,
1026   ({
1027     rmp->sw_if_index = ntohl (sw_if_index);
1028   }));
1029   /* *INDENT-ON* */
1030 }
1031
1032 static void vl_api_create_loopback_instance_t_handler
1033   (vl_api_create_loopback_instance_t * mp)
1034 {
1035   vl_api_create_loopback_instance_reply_t *rmp;
1036   u32 sw_if_index;
1037   u8 is_specified = mp->is_specified;
1038   u32 user_instance = ntohl (mp->user_instance);
1039   int rv;
1040
1041   rv = vnet_create_loopback_interface (&sw_if_index, mp->mac_address,
1042                                        is_specified, user_instance);
1043
1044   /* *INDENT-OFF* */
1045   REPLY_MACRO2(VL_API_CREATE_LOOPBACK_INSTANCE_REPLY,
1046   ({
1047     rmp->sw_if_index = ntohl (sw_if_index);
1048   }));
1049   /* *INDENT-ON* */
1050 }
1051
1052 static void
1053 vl_api_delete_loopback_t_handler (vl_api_delete_loopback_t * mp)
1054 {
1055   vl_api_delete_loopback_reply_t *rmp;
1056   u32 sw_if_index;
1057   int rv;
1058
1059   sw_if_index = ntohl (mp->sw_if_index);
1060   rv = vnet_delete_loopback_interface (sw_if_index);
1061
1062   REPLY_MACRO (VL_API_DELETE_LOOPBACK_REPLY);
1063 }
1064
1065 static void
1066 vl_api_control_ping_t_handler (vl_api_control_ping_t * mp)
1067 {
1068   vl_api_control_ping_reply_t *rmp;
1069   int rv = 0;
1070
1071   /* *INDENT-OFF* */
1072   REPLY_MACRO2(VL_API_CONTROL_PING_REPLY,
1073   ({
1074     rmp->vpe_pid = ntohl (getpid());
1075   }));
1076   /* *INDENT-ON* */
1077 }
1078
1079 static void
1080 shmem_cli_output (uword arg, u8 * buffer, uword buffer_bytes)
1081 {
1082   u8 **shmem_vecp = (u8 **) arg;
1083   u8 *shmem_vec;
1084   void *oldheap;
1085   api_main_t *am = &api_main;
1086   u32 offset;
1087
1088   shmem_vec = *shmem_vecp;
1089
1090   offset = vec_len (shmem_vec);
1091
1092   pthread_mutex_lock (&am->vlib_rp->mutex);
1093   oldheap = svm_push_data_heap (am->vlib_rp);
1094
1095   vec_validate (shmem_vec, offset + buffer_bytes - 1);
1096
1097   clib_memcpy (shmem_vec + offset, buffer, buffer_bytes);
1098
1099   svm_pop_heap (oldheap);
1100   pthread_mutex_unlock (&am->vlib_rp->mutex);
1101
1102   *shmem_vecp = shmem_vec;
1103 }
1104
1105
1106 static void
1107 vl_api_cli_request_t_handler (vl_api_cli_request_t * mp)
1108 {
1109   vl_api_cli_reply_t *rp;
1110   unix_shared_memory_queue_t *q;
1111   vlib_main_t *vm = vlib_get_main ();
1112   api_main_t *am = &api_main;
1113   unformat_input_t input;
1114   u8 *shmem_vec = 0;
1115   void *oldheap;
1116
1117   q = vl_api_client_index_to_input_queue (mp->client_index);
1118   if (!q)
1119     return;
1120
1121   rp = vl_msg_api_alloc (sizeof (*rp));
1122   rp->_vl_msg_id = ntohs (VL_API_CLI_REPLY);
1123   rp->context = mp->context;
1124
1125   unformat_init_vector (&input, (u8 *) (uword) mp->cmd_in_shmem);
1126
1127   vlib_cli_input (vm, &input, shmem_cli_output, (uword) & shmem_vec);
1128
1129   pthread_mutex_lock (&am->vlib_rp->mutex);
1130   oldheap = svm_push_data_heap (am->vlib_rp);
1131
1132   vec_add1 (shmem_vec, 0);
1133
1134   svm_pop_heap (oldheap);
1135   pthread_mutex_unlock (&am->vlib_rp->mutex);
1136
1137   rp->reply_in_shmem = (uword) shmem_vec;
1138
1139   vl_msg_api_send_shmem (q, (u8 *) & rp);
1140 }
1141
1142 static void
1143 inband_cli_output (uword arg, u8 * buffer, uword buffer_bytes)
1144 {
1145   u8 **mem_vecp = (u8 **) arg;
1146   u8 *mem_vec = *mem_vecp;
1147   u32 offset = vec_len (mem_vec);
1148
1149   vec_validate (mem_vec, offset + buffer_bytes - 1);
1150   clib_memcpy (mem_vec + offset, buffer, buffer_bytes);
1151   *mem_vecp = mem_vec;
1152 }
1153
1154 static void
1155 vl_api_cli_inband_t_handler (vl_api_cli_inband_t * mp)
1156 {
1157   vl_api_cli_inband_reply_t *rmp;
1158   int rv = 0;
1159   unix_shared_memory_queue_t *q;
1160   vlib_main_t *vm = vlib_get_main ();
1161   unformat_input_t input;
1162   u8 *out_vec = 0;
1163
1164   q = vl_api_client_index_to_input_queue (mp->client_index);
1165   if (!q)
1166     return;
1167
1168   unformat_init_string (&input, (char *) mp->cmd, ntohl (mp->length));
1169   vlib_cli_input (vm, &input, inband_cli_output, (uword) & out_vec);
1170
1171   u32 len = vec_len (out_vec);
1172   /* *INDENT-OFF* */
1173   REPLY_MACRO3(VL_API_CLI_INBAND_REPLY, len,
1174   ({
1175     rmp->length = htonl (len);
1176     clib_memcpy (rmp->reply, out_vec, len);
1177   }));
1178   /* *INDENT-ON* */
1179   vec_free (out_vec);
1180 }
1181
1182 static void
1183 vl_api_set_arp_neighbor_limit_t_handler (vl_api_set_arp_neighbor_limit_t * mp)
1184 {
1185   int rv;
1186   vl_api_set_arp_neighbor_limit_reply_t *rmp;
1187   vnet_main_t *vnm = vnet_get_main ();
1188   clib_error_t *error;
1189
1190   vnm->api_errno = 0;
1191
1192   if (mp->is_ipv6)
1193     error = ip6_set_neighbor_limit (ntohl (mp->arp_neighbor_limit));
1194   else
1195     error = ip4_set_arp_limit (ntohl (mp->arp_neighbor_limit));
1196
1197   if (error)
1198     {
1199       clib_error_report (error);
1200       rv = VNET_API_ERROR_UNSPECIFIED;
1201     }
1202   else
1203     {
1204       rv = vnm->api_errno;
1205     }
1206
1207   REPLY_MACRO (VL_API_SET_ARP_NEIGHBOR_LIMIT_REPLY);
1208 }
1209
1210 static void vl_api_classify_set_interface_ip_table_t_handler
1211   (vl_api_classify_set_interface_ip_table_t * mp)
1212 {
1213   vlib_main_t *vm = vlib_get_main ();
1214   vl_api_classify_set_interface_ip_table_reply_t *rmp;
1215   int rv;
1216
1217   VALIDATE_SW_IF_INDEX (mp);
1218
1219   u32 table_index = ntohl (mp->table_index);
1220   u32 sw_if_index = ntohl (mp->sw_if_index);
1221
1222   if (mp->is_ipv6)
1223     rv = vnet_set_ip6_classify_intfc (vm, sw_if_index, table_index);
1224   else
1225     rv = vnet_set_ip4_classify_intfc (vm, sw_if_index, table_index);
1226
1227   BAD_SW_IF_INDEX_LABEL;
1228
1229   REPLY_MACRO (VL_API_CLASSIFY_SET_INTERFACE_IP_TABLE_REPLY);
1230 }
1231
1232 static void vl_api_classify_set_interface_l2_tables_t_handler
1233   (vl_api_classify_set_interface_l2_tables_t * mp)
1234 {
1235   vl_api_classify_set_interface_l2_tables_reply_t *rmp;
1236   int rv;
1237   u32 sw_if_index, ip4_table_index, ip6_table_index, other_table_index;
1238   int enable;
1239
1240   ip4_table_index = ntohl (mp->ip4_table_index);
1241   ip6_table_index = ntohl (mp->ip6_table_index);
1242   other_table_index = ntohl (mp->other_table_index);
1243   sw_if_index = ntohl (mp->sw_if_index);
1244
1245   VALIDATE_SW_IF_INDEX (mp);
1246
1247   if (mp->is_input)
1248     rv = vnet_l2_input_classify_set_tables (sw_if_index, ip4_table_index,
1249                                             ip6_table_index,
1250                                             other_table_index);
1251   else
1252     rv = vnet_l2_output_classify_set_tables (sw_if_index, ip4_table_index,
1253                                              ip6_table_index,
1254                                              other_table_index);
1255
1256   if (rv == 0)
1257     {
1258       if (ip4_table_index != ~0 || ip6_table_index != ~0
1259           || other_table_index != ~0)
1260         enable = 1;
1261       else
1262         enable = 0;
1263
1264       if (mp->is_input)
1265         vnet_l2_input_classify_enable_disable (sw_if_index, enable);
1266       else
1267         vnet_l2_output_classify_enable_disable (sw_if_index, enable);
1268     }
1269
1270   BAD_SW_IF_INDEX_LABEL;
1271
1272   REPLY_MACRO (VL_API_CLASSIFY_SET_INTERFACE_L2_TABLES_REPLY);
1273 }
1274
1275 extern void l2_efp_filter_configure (vnet_main_t * vnet_main,
1276                                      u32 sw_if_index, u32 enable);
1277
1278 static void
1279 vl_api_l2_interface_efp_filter_t_handler (vl_api_l2_interface_efp_filter_t *
1280                                           mp)
1281 {
1282   int rv;
1283   vl_api_l2_interface_efp_filter_reply_t *rmp;
1284   vnet_main_t *vnm = vnet_get_main ();
1285
1286   // enable/disable the feature
1287   l2_efp_filter_configure (vnm, mp->sw_if_index, mp->enable_disable);
1288   rv = vnm->api_errno;
1289
1290   REPLY_MACRO (VL_API_L2_INTERFACE_EFP_FILTER_REPLY);
1291 }
1292
1293 static void
1294 vl_api_show_version_t_handler (vl_api_show_version_t * mp)
1295 {
1296   vl_api_show_version_reply_t *rmp;
1297   int rv = 0;
1298   char *vpe_api_get_build_directory (void);
1299   char *vpe_api_get_version (void);
1300   char *vpe_api_get_build_date (void);
1301
1302   unix_shared_memory_queue_t *q =
1303     vl_api_client_index_to_input_queue (mp->client_index);
1304
1305   if (!q)
1306     return;
1307
1308   /* *INDENT-OFF* */
1309   REPLY_MACRO2(VL_API_SHOW_VERSION_REPLY,
1310   ({
1311     strncpy ((char *) rmp->program, "vpe", ARRAY_LEN(rmp->program)-1);
1312     strncpy ((char *) rmp->build_directory, vpe_api_get_build_directory(),
1313              ARRAY_LEN(rmp->build_directory)-1);
1314     strncpy ((char *) rmp->version, vpe_api_get_version(),
1315              ARRAY_LEN(rmp->version)-1);
1316     strncpy ((char *) rmp->build_date, vpe_api_get_build_date(),
1317              ARRAY_LEN(rmp->build_date)-1);
1318   }));
1319   /* *INDENT-ON* */
1320 }
1321
1322 static void
1323 vl_api_get_node_index_t_handler (vl_api_get_node_index_t * mp)
1324 {
1325   vlib_main_t *vm = vlib_get_main ();
1326   vl_api_get_node_index_reply_t *rmp;
1327   vlib_node_t *n;
1328   int rv = 0;
1329   u32 node_index = ~0;
1330
1331   n = vlib_get_node_by_name (vm, mp->node_name);
1332
1333   if (n == 0)
1334     rv = VNET_API_ERROR_NO_SUCH_NODE;
1335   else
1336     node_index = n->index;
1337
1338   /* *INDENT-OFF* */
1339   REPLY_MACRO2(VL_API_GET_NODE_INDEX_REPLY,
1340   ({
1341     rmp->node_index = ntohl(node_index);
1342   }));
1343   /* *INDENT-ON* */
1344 }
1345
1346 static void
1347 vl_api_get_next_index_t_handler (vl_api_get_next_index_t * mp)
1348 {
1349   vlib_main_t *vm = vlib_get_main ();
1350   vl_api_get_next_index_reply_t *rmp;
1351   vlib_node_t *node, *next_node;
1352   int rv = 0;
1353   u32 next_node_index = ~0, next_index = ~0;
1354   uword *p;
1355
1356   node = vlib_get_node_by_name (vm, mp->node_name);
1357
1358   if (node == 0)
1359     {
1360       rv = VNET_API_ERROR_NO_SUCH_NODE;
1361       goto out;
1362     }
1363
1364   next_node = vlib_get_node_by_name (vm, mp->next_name);
1365
1366   if (next_node == 0)
1367     {
1368       rv = VNET_API_ERROR_NO_SUCH_NODE2;
1369       goto out;
1370     }
1371   else
1372     next_node_index = next_node->index;
1373
1374   p = hash_get (node->next_slot_by_node, next_node_index);
1375
1376   if (p == 0)
1377     {
1378       rv = VNET_API_ERROR_NO_SUCH_ENTRY;
1379       goto out;
1380     }
1381   else
1382     next_index = p[0];
1383
1384 out:
1385   /* *INDENT-OFF* */
1386   REPLY_MACRO2(VL_API_GET_NEXT_INDEX_REPLY,
1387   ({
1388     rmp->next_index = ntohl(next_index);
1389   }));
1390   /* *INDENT-ON* */
1391 }
1392
1393 static void
1394 vl_api_add_node_next_t_handler (vl_api_add_node_next_t * mp)
1395 {
1396   vlib_main_t *vm = vlib_get_main ();
1397   vl_api_add_node_next_reply_t *rmp;
1398   vlib_node_t *n, *next;
1399   int rv = 0;
1400   u32 next_index = ~0;
1401
1402   n = vlib_get_node_by_name (vm, mp->node_name);
1403
1404   if (n == 0)
1405     {
1406       rv = VNET_API_ERROR_NO_SUCH_NODE;
1407       goto out;
1408     }
1409
1410   next = vlib_get_node_by_name (vm, mp->next_name);
1411
1412   if (next == 0)
1413     rv = VNET_API_ERROR_NO_SUCH_NODE2;
1414   else
1415     next_index = vlib_node_add_next (vm, n->index, next->index);
1416
1417 out:
1418   /* *INDENT-OFF* */
1419   REPLY_MACRO2(VL_API_GET_NODE_INDEX_REPLY,
1420   ({
1421     rmp->next_index = ntohl(next_index);
1422   }));
1423   /* *INDENT-ON* */
1424 }
1425
1426 static void
1427 vl_api_l2_patch_add_del_t_handler (vl_api_l2_patch_add_del_t * mp)
1428 {
1429   extern int vnet_l2_patch_add_del (u32 rx_sw_if_index, u32 tx_sw_if_index,
1430                                     int is_add);
1431   vl_api_l2_patch_add_del_reply_t *rmp;
1432   int vnet_l2_patch_add_del (u32 rx_sw_if_index, u32 tx_sw_if_index,
1433                              int is_add);
1434   int rv = 0;
1435
1436   VALIDATE_RX_SW_IF_INDEX (mp);
1437   VALIDATE_TX_SW_IF_INDEX (mp);
1438
1439   rv = vnet_l2_patch_add_del (ntohl (mp->rx_sw_if_index),
1440                               ntohl (mp->tx_sw_if_index),
1441                               (int) (mp->is_add != 0));
1442
1443   BAD_RX_SW_IF_INDEX_LABEL;
1444   BAD_TX_SW_IF_INDEX_LABEL;
1445
1446   REPLY_MACRO (VL_API_L2_PATCH_ADD_DEL_REPLY);
1447 }
1448
1449 static void
1450 vl_api_interface_name_renumber_t_handler (vl_api_interface_name_renumber_t *
1451                                           mp)
1452 {
1453   vl_api_interface_name_renumber_reply_t *rmp;
1454   int rv = 0;
1455
1456   VALIDATE_SW_IF_INDEX (mp);
1457
1458   rv = vnet_interface_name_renumber
1459     (ntohl (mp->sw_if_index), ntohl (mp->new_show_dev_instance));
1460
1461   BAD_SW_IF_INDEX_LABEL;
1462
1463   REPLY_MACRO (VL_API_INTERFACE_NAME_RENUMBER_REPLY);
1464 }
1465
1466 static int
1467 arp_change_data_callback (u32 pool_index, u8 * new_mac,
1468                           u32 sw_if_index, u32 address)
1469 {
1470   vpe_api_main_t *am = &vpe_api_main;
1471   vlib_main_t *vm = am->vlib_main;
1472   vl_api_ip4_arp_event_t *event;
1473   static f64 arp_event_last_time;
1474   f64 now = vlib_time_now (vm);
1475
1476   if (pool_is_free_index (am->arp_events, pool_index))
1477     return 1;
1478
1479   event = pool_elt_at_index (am->arp_events, pool_index);
1480   /* *INDENT-OFF* */
1481   if (memcmp (&event->new_mac, new_mac, sizeof (event->new_mac)))
1482     {
1483       clib_memcpy (event->new_mac, new_mac, sizeof (event->new_mac));
1484     }
1485   else
1486     {                           /* same mac */
1487       if (sw_if_index == ntohl(event->sw_if_index) &&
1488           (!event->mac_ip ||
1489            /* for BD case, also check IP address with 10 sec timeout */
1490            (address == event->address &&
1491             (now - arp_event_last_time) < 10.0)))
1492         return 1;
1493     }
1494   /* *INDENT-ON* */
1495
1496   arp_event_last_time = now;
1497   event->sw_if_index = htonl (sw_if_index);
1498   if (event->mac_ip)
1499     event->address = address;
1500   return 0;
1501 }
1502
1503 static int
1504 nd_change_data_callback (u32 pool_index, u8 * new_mac,
1505                          u32 sw_if_index, ip6_address_t * address)
1506 {
1507   vpe_api_main_t *am = &vpe_api_main;
1508   vlib_main_t *vm = am->vlib_main;
1509   vl_api_ip6_nd_event_t *event;
1510   static f64 nd_event_last_time;
1511   f64 now = vlib_time_now (vm);
1512
1513   if (pool_is_free_index (am->nd_events, pool_index))
1514     return 1;
1515
1516   event = pool_elt_at_index (am->nd_events, pool_index);
1517
1518   /* *INDENT-OFF* */
1519   if (memcmp (&event->new_mac, new_mac, sizeof (event->new_mac)))
1520     {
1521       clib_memcpy (event->new_mac, new_mac, sizeof (event->new_mac));
1522     }
1523   else
1524     {                           /* same mac */
1525       if (sw_if_index == ntohl(event->sw_if_index) &&
1526           (!event->mac_ip ||
1527            /* for BD case, also check IP address with 10 sec timeout */
1528            (ip6_address_is_equal (address,
1529                                   (ip6_address_t *) event->address) &&
1530             (now - nd_event_last_time) < 10.0)))
1531         return 1;
1532     }
1533   /* *INDENT-ON* */
1534
1535   nd_event_last_time = now;
1536   event->sw_if_index = htonl (sw_if_index);
1537   if (event->mac_ip)
1538     clib_memcpy (event->address, address, sizeof (event->address));
1539   return 0;
1540 }
1541
1542 static int
1543 arp_change_delete_callback (u32 pool_index, u8 * notused)
1544 {
1545   vpe_api_main_t *am = &vpe_api_main;
1546
1547   if (pool_is_free_index (am->arp_events, pool_index))
1548     return 1;
1549
1550   pool_put_index (am->arp_events, pool_index);
1551   return 0;
1552 }
1553
1554 static int
1555 nd_change_delete_callback (u32 pool_index, u8 * notused)
1556 {
1557   vpe_api_main_t *am = &vpe_api_main;
1558
1559   if (pool_is_free_index (am->nd_events, pool_index))
1560     return 1;
1561
1562   pool_put_index (am->nd_events, pool_index);
1563   return 0;
1564 }
1565
1566 static void
1567 vl_api_want_ip4_arp_events_t_handler (vl_api_want_ip4_arp_events_t * mp)
1568 {
1569   vpe_api_main_t *am = &vpe_api_main;
1570   vnet_main_t *vnm = vnet_get_main ();
1571   vl_api_want_ip4_arp_events_reply_t *rmp;
1572   int rv;
1573
1574   if (mp->enable_disable)
1575     {
1576       vl_api_ip4_arp_event_t *event;
1577       pool_get (am->arp_events, event);
1578       rv = vnet_add_del_ip4_arp_change_event
1579         (vnm, arp_change_data_callback,
1580          mp->pid, &mp->address /* addr, in net byte order */ ,
1581          vpe_resolver_process_node.index,
1582          IP4_ARP_EVENT, event - am->arp_events, 1 /* is_add */ );
1583
1584       if (rv)
1585         {
1586           pool_put (am->arp_events, event);
1587           goto out;
1588         }
1589       memset (event, 0, sizeof (*event));
1590
1591       event->_vl_msg_id = ntohs (VL_API_IP4_ARP_EVENT);
1592       event->client_index = mp->client_index;
1593       event->context = mp->context;
1594       event->address = mp->address;
1595       event->pid = mp->pid;
1596       if (mp->address == 0)
1597         event->mac_ip = 1;
1598     }
1599   else
1600     {
1601       rv = vnet_add_del_ip4_arp_change_event
1602         (vnm, arp_change_delete_callback,
1603          mp->pid, &mp->address /* addr, in net byte order */ ,
1604          vpe_resolver_process_node.index,
1605          IP4_ARP_EVENT, ~0 /* pool index */ , 0 /* is_add */ );
1606     }
1607 out:
1608   REPLY_MACRO (VL_API_WANT_IP4_ARP_EVENTS_REPLY);
1609 }
1610
1611 static void
1612 vl_api_want_ip6_nd_events_t_handler (vl_api_want_ip6_nd_events_t * mp)
1613 {
1614   vpe_api_main_t *am = &vpe_api_main;
1615   vnet_main_t *vnm = vnet_get_main ();
1616   vl_api_want_ip6_nd_events_reply_t *rmp;
1617   int rv;
1618
1619   if (mp->enable_disable)
1620     {
1621       vl_api_ip6_nd_event_t *event;
1622       pool_get (am->nd_events, event);
1623
1624       rv = vnet_add_del_ip6_nd_change_event
1625         (vnm, nd_change_data_callback,
1626          mp->pid, mp->address /* addr, in net byte order */ ,
1627          vpe_resolver_process_node.index,
1628          IP6_ND_EVENT, event - am->nd_events, 1 /* is_add */ );
1629
1630       if (rv)
1631         {
1632           pool_put (am->nd_events, event);
1633           goto out;
1634         }
1635       memset (event, 0, sizeof (*event));
1636
1637       event->_vl_msg_id = ntohs (VL_API_IP6_ND_EVENT);
1638       event->client_index = mp->client_index;
1639       event->context = mp->context;
1640       clib_memcpy (event->address, mp->address, 16);
1641       event->pid = mp->pid;
1642       if (ip6_address_is_zero ((ip6_address_t *) mp->address))
1643         event->mac_ip = 1;
1644
1645     }
1646   else
1647     {
1648       rv = vnet_add_del_ip6_nd_change_event
1649         (vnm, nd_change_delete_callback,
1650          mp->pid, mp->address /* addr, in net byte order */ ,
1651          vpe_resolver_process_node.index,
1652          IP6_ND_EVENT, ~0 /* pool index */ , 0 /* is_add */ );
1653     }
1654 out:
1655   REPLY_MACRO (VL_API_WANT_IP6_ND_EVENTS_REPLY);
1656 }
1657
1658 static void vl_api_input_acl_set_interface_t_handler
1659   (vl_api_input_acl_set_interface_t * mp)
1660 {
1661   vlib_main_t *vm = vlib_get_main ();
1662   vl_api_input_acl_set_interface_reply_t *rmp;
1663   int rv;
1664
1665   VALIDATE_SW_IF_INDEX (mp);
1666
1667   u32 ip4_table_index = ntohl (mp->ip4_table_index);
1668   u32 ip6_table_index = ntohl (mp->ip6_table_index);
1669   u32 l2_table_index = ntohl (mp->l2_table_index);
1670   u32 sw_if_index = ntohl (mp->sw_if_index);
1671
1672   rv = vnet_set_input_acl_intfc (vm, sw_if_index, ip4_table_index,
1673                                  ip6_table_index, l2_table_index, mp->is_add);
1674
1675   BAD_SW_IF_INDEX_LABEL;
1676
1677   REPLY_MACRO (VL_API_INPUT_ACL_SET_INTERFACE_REPLY);
1678 }
1679
1680 static void
1681 vl_api_get_node_graph_t_handler (vl_api_get_node_graph_t * mp)
1682 {
1683   int rv = 0;
1684   u8 *vector = 0;
1685   api_main_t *am = &api_main;
1686   vlib_main_t *vm = vlib_get_main ();
1687   void *oldheap;
1688   vl_api_get_node_graph_reply_t *rmp;
1689
1690   pthread_mutex_lock (&am->vlib_rp->mutex);
1691   oldheap = svm_push_data_heap (am->vlib_rp);
1692
1693   /*
1694    * Keep the number of memcpy ops to a minimum (e.g. 1).
1695    */
1696   vec_validate (vector, 16384);
1697   vec_reset_length (vector);
1698
1699   /* $$$$ FIXME */
1700   vector = vlib_node_serialize (&vm->node_main, vector,
1701                                 (u32) ~ 0 /* all threads */ ,
1702                                 1 /* include nexts */ ,
1703                                 1 /* include stats */ );
1704
1705   svm_pop_heap (oldheap);
1706   pthread_mutex_unlock (&am->vlib_rp->mutex);
1707
1708   /* *INDENT-OFF* */
1709   REPLY_MACRO2(VL_API_GET_NODE_GRAPH_REPLY,
1710   ({
1711     rmp->reply_in_shmem = (uword) vector;
1712   }));
1713   /* *INDENT-ON* */
1714 }
1715
1716 static void
1717 vl_api_ioam_enable_t_handler (vl_api_ioam_enable_t * mp)
1718 {
1719   int rv = 0;
1720   vl_api_ioam_enable_reply_t *rmp;
1721   clib_error_t *error;
1722
1723   /* Ignoring the profile id as currently a single profile
1724    * is supported */
1725   error = ip6_ioam_enable (mp->trace_enable, mp->pot_enable,
1726                            mp->seqno, mp->analyse);
1727   if (error)
1728     {
1729       clib_error_report (error);
1730       rv = clib_error_get_code (error);
1731     }
1732
1733   REPLY_MACRO (VL_API_IOAM_ENABLE_REPLY);
1734 }
1735
1736 static void
1737 vl_api_ioam_disable_t_handler (vl_api_ioam_disable_t * mp)
1738 {
1739   int rv = 0;
1740   vl_api_ioam_disable_reply_t *rmp;
1741   clib_error_t *error;
1742
1743   error = clear_ioam_rewrite_fn ();
1744   if (error)
1745     {
1746       clib_error_report (error);
1747       rv = clib_error_get_code (error);
1748     }
1749
1750   REPLY_MACRO (VL_API_IOAM_DISABLE_REPLY);
1751 }
1752
1753 static void
1754 vl_api_pg_create_interface_t_handler (vl_api_pg_create_interface_t * mp)
1755 {
1756   vl_api_pg_create_interface_reply_t *rmp;
1757   int rv = 0;
1758
1759   pg_main_t *pg = &pg_main;
1760   u32 pg_if_id = pg_interface_add_or_get (pg, ntohl (mp->interface_id));
1761   pg_interface_t *pi = pool_elt_at_index (pg->interfaces, pg_if_id);
1762
1763   /* *INDENT-OFF* */
1764   REPLY_MACRO2(VL_API_PG_CREATE_INTERFACE_REPLY,
1765   ({
1766     rmp->sw_if_index = ntohl(pi->sw_if_index);
1767   }));
1768   /* *INDENT-ON* */
1769 }
1770
1771 static void
1772 vl_api_pg_capture_t_handler (vl_api_pg_capture_t * mp)
1773 {
1774   vl_api_pg_capture_reply_t *rmp;
1775   int rv = 0;
1776
1777   vnet_main_t *vnm = vnet_get_main ();
1778   vnet_interface_main_t *im = &vnm->interface_main;
1779   vnet_hw_interface_t *hi = 0;
1780
1781   u8 *intf_name = format (0, "pg%d", ntohl (mp->interface_id), 0);
1782   u32 hw_if_index = ~0;
1783   uword *p = hash_get_mem (im->hw_interface_by_name, intf_name);
1784   if (p)
1785     hw_if_index = *p;
1786   vec_free (intf_name);
1787
1788   if (hw_if_index != ~0)
1789     {
1790       pg_capture_args_t _a, *a = &_a;
1791
1792       u32 len = ntohl (mp->pcap_name_length);
1793       u8 *pcap_file_name = vec_new (u8, len);
1794       clib_memcpy (pcap_file_name, mp->pcap_file_name, len);
1795
1796       hi = vnet_get_sup_hw_interface (vnm, hw_if_index);
1797       a->hw_if_index = hw_if_index;
1798       a->dev_instance = hi->dev_instance;
1799       a->is_enabled = mp->is_enabled;
1800       a->pcap_file_name = pcap_file_name;
1801       a->count = ntohl (mp->count);
1802
1803       clib_error_t *e = pg_capture (a);
1804       if (e)
1805         {
1806           clib_error_report (e);
1807           rv = VNET_API_ERROR_CANNOT_CREATE_PCAP_FILE;
1808         }
1809
1810       vec_free (pcap_file_name);
1811     }
1812   REPLY_MACRO (VL_API_PG_CAPTURE_REPLY);
1813 }
1814
1815 static void
1816 vl_api_pg_enable_disable_t_handler (vl_api_pg_enable_disable_t * mp)
1817 {
1818   vl_api_pg_enable_disable_reply_t *rmp;
1819   int rv = 0;
1820
1821   pg_main_t *pg = &pg_main;
1822   u32 stream_index = ~0;
1823
1824   int is_enable = mp->is_enabled != 0;
1825   u32 len = ntohl (mp->stream_name_length) - 1;
1826
1827   if (len > 0)
1828     {
1829       u8 *stream_name = vec_new (u8, len);
1830       clib_memcpy (stream_name, mp->stream_name, len);
1831       uword *p = hash_get_mem (pg->stream_index_by_name, stream_name);
1832       if (p)
1833         stream_index = *p;
1834       vec_free (stream_name);
1835     }
1836
1837   pg_enable_disable (stream_index, is_enable);
1838
1839   REPLY_MACRO (VL_API_PG_ENABLE_DISABLE_REPLY);
1840 }
1841
1842 static void
1843   vl_api_ip_source_and_port_range_check_add_del_t_handler
1844   (vl_api_ip_source_and_port_range_check_add_del_t * mp)
1845 {
1846   vl_api_ip_source_and_port_range_check_add_del_reply_t *rmp;
1847   int rv = 0;
1848
1849   u8 is_ipv6 = mp->is_ipv6;
1850   u8 is_add = mp->is_add;
1851   u8 mask_length = mp->mask_length;
1852   ip4_address_t ip4_addr;
1853   ip6_address_t ip6_addr;
1854   u16 *low_ports = 0;
1855   u16 *high_ports = 0;
1856   u32 vrf_id;
1857   u16 tmp_low, tmp_high;
1858   u8 num_ranges;
1859   int i;
1860
1861   // Validate port range
1862   num_ranges = mp->number_of_ranges;
1863   if (num_ranges > 32)
1864     {                           // This is size of array in VPE.API
1865       rv = VNET_API_ERROR_EXCEEDED_NUMBER_OF_RANGES_CAPACITY;
1866       goto reply;
1867     }
1868
1869   vec_reset_length (low_ports);
1870   vec_reset_length (high_ports);
1871
1872   for (i = 0; i < num_ranges; i++)
1873     {
1874       tmp_low = mp->low_ports[i];
1875       tmp_high = mp->high_ports[i];
1876       // If tmp_low <= tmp_high then only need to check tmp_low = 0
1877       // If tmp_low <= tmp_high then only need to check tmp_high > 65535
1878       if (tmp_low > tmp_high || tmp_low == 0 || tmp_high > 65535)
1879         {
1880           rv = VNET_API_ERROR_INVALID_VALUE;
1881           goto reply;
1882         }
1883       vec_add1 (low_ports, tmp_low);
1884       vec_add1 (high_ports, tmp_high + 1);
1885     }
1886
1887   // Validate mask_length
1888   if ((is_ipv6 && mask_length > 128) || (!is_ipv6 && mask_length > 32))
1889     {
1890       rv = VNET_API_ERROR_ADDRESS_LENGTH_MISMATCH;
1891       goto reply;
1892     }
1893
1894   vrf_id = ntohl (mp->vrf_id);
1895
1896   if (vrf_id < 1)
1897     {
1898       rv = VNET_API_ERROR_INVALID_VALUE;
1899       goto reply;
1900     }
1901
1902
1903   if (is_ipv6)
1904     {
1905       clib_memcpy (ip6_addr.as_u8, mp->address, sizeof (ip6_addr.as_u8));
1906       rv = ip6_source_and_port_range_check_add_del (&ip6_addr,
1907                                                     mask_length,
1908                                                     vrf_id,
1909                                                     low_ports,
1910                                                     high_ports, is_add);
1911     }
1912   else
1913     {
1914       clib_memcpy (ip4_addr.data, mp->address, sizeof (ip4_addr));
1915       rv = ip4_source_and_port_range_check_add_del (&ip4_addr,
1916                                                     mask_length,
1917                                                     vrf_id,
1918                                                     low_ports,
1919                                                     high_ports, is_add);
1920     }
1921
1922 reply:
1923   vec_free (low_ports);
1924   vec_free (high_ports);
1925   REPLY_MACRO (VL_API_IP_SOURCE_AND_PORT_RANGE_CHECK_ADD_DEL_REPLY);
1926 }
1927
1928 static void
1929   vl_api_ip_source_and_port_range_check_interface_add_del_t_handler
1930   (vl_api_ip_source_and_port_range_check_interface_add_del_t * mp)
1931 {
1932   vlib_main_t *vm = vlib_get_main ();
1933   vl_api_ip_source_and_port_range_check_interface_add_del_reply_t *rmp;
1934   ip4_main_t *im = &ip4_main;
1935   int rv;
1936   u32 sw_if_index;
1937   u32 fib_index[IP_SOURCE_AND_PORT_RANGE_CHECK_N_PROTOCOLS];
1938   u32 vrf_id[IP_SOURCE_AND_PORT_RANGE_CHECK_N_PROTOCOLS];
1939   uword *p = 0;
1940   int i;
1941
1942   vrf_id[IP_SOURCE_AND_PORT_RANGE_CHECK_PROTOCOL_TCP_OUT] =
1943     ntohl (mp->tcp_out_vrf_id);
1944   vrf_id[IP_SOURCE_AND_PORT_RANGE_CHECK_PROTOCOL_UDP_OUT] =
1945     ntohl (mp->udp_out_vrf_id);
1946   vrf_id[IP_SOURCE_AND_PORT_RANGE_CHECK_PROTOCOL_TCP_IN] =
1947     ntohl (mp->tcp_in_vrf_id);
1948   vrf_id[IP_SOURCE_AND_PORT_RANGE_CHECK_PROTOCOL_UDP_IN] =
1949     ntohl (mp->udp_in_vrf_id);
1950
1951
1952   for (i = 0; i < IP_SOURCE_AND_PORT_RANGE_CHECK_N_PROTOCOLS; i++)
1953     {
1954       if (vrf_id[i] != 0 && vrf_id[i] != ~0)
1955         {
1956           p = hash_get (im->fib_index_by_table_id, vrf_id[i]);
1957
1958           if (p == 0)
1959             {
1960               rv = VNET_API_ERROR_INVALID_VALUE;
1961               goto reply;
1962             }
1963
1964           fib_index[i] = p[0];
1965         }
1966       else
1967         fib_index[i] = ~0;
1968     }
1969   sw_if_index = ntohl (mp->sw_if_index);
1970
1971   VALIDATE_SW_IF_INDEX (mp);
1972
1973   rv =
1974     set_ip_source_and_port_range_check (vm, fib_index, sw_if_index,
1975                                         mp->is_add);
1976
1977   BAD_SW_IF_INDEX_LABEL;
1978 reply:
1979
1980   REPLY_MACRO (VL_API_IP_SOURCE_AND_PORT_RANGE_CHECK_INTERFACE_ADD_DEL_REPLY);
1981 }
1982
1983 static void
1984 vl_api_delete_subif_t_handler (vl_api_delete_subif_t * mp)
1985 {
1986   vl_api_delete_subif_reply_t *rmp;
1987   int rv;
1988
1989   rv = vnet_delete_sub_interface (ntohl (mp->sw_if_index));
1990
1991   REPLY_MACRO (VL_API_DELETE_SUBIF_REPLY);
1992 }
1993
1994 static void
1995 vl_api_punt_t_handler (vl_api_punt_t * mp)
1996 {
1997   vl_api_punt_reply_t *rmp;
1998   vlib_main_t *vm = vlib_get_main ();
1999   int rv = 0;
2000   clib_error_t *error;
2001
2002   error = vnet_punt_add_del (vm, mp->ipv, mp->l4_protocol,
2003                              ntohs (mp->l4_port), mp->is_add);
2004   if (error)
2005     {
2006       rv = -1;
2007       clib_error_report (error);
2008     }
2009
2010   REPLY_MACRO (VL_API_PUNT_REPLY);
2011 }
2012
2013 static void
2014 vl_api_feature_enable_disable_t_handler (vl_api_feature_enable_disable_t * mp)
2015 {
2016   vl_api_feature_enable_disable_reply_t *rmp;
2017   int rv = 0;
2018
2019   VALIDATE_SW_IF_INDEX (mp);
2020
2021   u8 *arc_name = format (0, "%s%c", mp->arc_name, 0);
2022   u8 *feature_name = format (0, "%s%c", mp->feature_name, 0);
2023
2024   vnet_feature_registration_t *reg =
2025     vnet_get_feature_reg ((const char *) arc_name,
2026                           (const char *) feature_name);
2027   if (reg == 0)
2028     rv = VNET_API_ERROR_INVALID_VALUE;
2029   else
2030     {
2031       u32 sw_if_index = ntohl (mp->sw_if_index);
2032       clib_error_t *error = 0;
2033
2034       if (reg->enable_disable_cb)
2035         error = reg->enable_disable_cb (sw_if_index, mp->enable);
2036       if (!error)
2037         vnet_feature_enable_disable ((const char *) arc_name,
2038                                      (const char *) feature_name,
2039                                      sw_if_index, mp->enable, 0, 0);
2040       else
2041         {
2042           clib_error_report (error);
2043           rv = VNET_API_ERROR_CANNOT_ENABLE_DISABLE_FEATURE;
2044         }
2045     }
2046
2047   vec_free (feature_name);
2048   vec_free (arc_name);
2049
2050   BAD_SW_IF_INDEX_LABEL;
2051
2052   REPLY_MACRO (VL_API_FEATURE_ENABLE_DISABLE_REPLY);
2053 }
2054
2055 #define BOUNCE_HANDLER(nn)                                              \
2056 static void vl_api_##nn##_t_handler (                                   \
2057     vl_api_##nn##_t *mp)                                                \
2058 {                                                                       \
2059     vpe_client_registration_t *reg;                                     \
2060     vpe_api_main_t * vam = &vpe_api_main;                               \
2061     unix_shared_memory_queue_t * q;                                     \
2062                                                                         \
2063     /* One registration only... */                                      \
2064     pool_foreach(reg, vam->nn##_registrations,                          \
2065     ({                                                                  \
2066         q = vl_api_client_index_to_input_queue (reg->client_index);     \
2067         if (q) {                                                        \
2068             /*                                                          \
2069              * If the queue is stuffed, turf the msg and complain       \
2070              * It's unlikely that the intended recipient is             \
2071              * alive; avoid deadlock at all costs.                      \
2072              */                                                         \
2073             if (q->cursize == q->maxsize) {                             \
2074                 clib_warning ("ERROR: receiver queue full, drop msg");  \
2075                 vl_msg_api_free (mp);                                   \
2076                 return;                                                 \
2077             }                                                           \
2078             vl_msg_api_send_shmem (q, (u8 *)&mp);                       \
2079             return;                                                     \
2080         }                                                               \
2081     }));                                                                \
2082     vl_msg_api_free (mp);                                               \
2083 }
2084
2085 static void setup_message_id_table (api_main_t * am);
2086
2087 /*
2088  * vpe_api_hookup
2089  * Add vpe's API message handlers to the table.
2090  * vlib has alread mapped shared memory and
2091  * added the client registration handlers.
2092  * See .../open-repo/vlib/memclnt_vlib.c:memclnt_process()
2093  */
2094 static clib_error_t *
2095 vpe_api_hookup (vlib_main_t * vm)
2096 {
2097   api_main_t *am = &api_main;
2098
2099 #define _(N,n)                                                  \
2100     vl_msg_api_set_handlers(VL_API_##N, #n,                     \
2101                            vl_api_##n##_t_handler,              \
2102                            vl_noop_handler,                     \
2103                            vl_api_##n##_t_endian,               \
2104                            vl_api_##n##_t_print,                \
2105                            sizeof(vl_api_##n##_t), 1);
2106   foreach_vpe_api_msg;
2107 #undef _
2108
2109   /*
2110    * Trace space for classifier mask+match
2111    */
2112   am->api_trace_cfg[VL_API_CLASSIFY_ADD_DEL_TABLE].size += 5 * sizeof (u32x4);
2113   am->api_trace_cfg[VL_API_CLASSIFY_ADD_DEL_SESSION].size
2114     += 5 * sizeof (u32x4);
2115
2116   /*
2117    * Thread-safe API messages
2118    */
2119   am->is_mp_safe[VL_API_IP_ADD_DEL_ROUTE] = 1;
2120   am->is_mp_safe[VL_API_GET_NODE_GRAPH] = 1;
2121
2122   /*
2123    * Set up the (msg_name, crc, message-id) table
2124    */
2125   setup_message_id_table (am);
2126
2127   return 0;
2128 }
2129
2130 VLIB_API_INIT_FUNCTION (vpe_api_hookup);
2131
2132 static clib_error_t *
2133 vpe_api_init (vlib_main_t * vm)
2134 {
2135   vpe_api_main_t *am = &vpe_api_main;
2136
2137   am->vlib_main = vm;
2138   am->vnet_main = vnet_get_main ();
2139   am->interface_events_registration_hash = hash_create (0, sizeof (uword));
2140   am->to_netconf_server_registration_hash = hash_create (0, sizeof (uword));
2141   am->from_netconf_server_registration_hash = hash_create (0, sizeof (uword));
2142   am->to_netconf_client_registration_hash = hash_create (0, sizeof (uword));
2143   am->from_netconf_client_registration_hash = hash_create (0, sizeof (uword));
2144   am->oam_events_registration_hash = hash_create (0, sizeof (uword));
2145   am->bfd_events_registration_hash = hash_create (0, sizeof (uword));
2146
2147   vl_set_memory_region_name ("/vpe-api");
2148   vl_enable_disable_memory_api (vm, 1 /* enable it */ );
2149
2150   return 0;
2151 }
2152
2153 VLIB_INIT_FUNCTION (vpe_api_init);
2154
2155
2156 static clib_error_t *
2157 api_segment_config (vlib_main_t * vm, unformat_input_t * input)
2158 {
2159   u8 *chroot_path;
2160   u64 baseva, size, pvt_heap_size;
2161   int uid, gid, rv;
2162   const int max_buf_size = 4096;
2163   char *s, *buf;
2164   struct passwd _pw, *pw;
2165   struct group _grp, *grp;
2166   clib_error_t *e;
2167   buf = vec_new (char, 128);
2168   while (unformat_check_input (input) != UNFORMAT_END_OF_INPUT)
2169     {
2170       if (unformat (input, "prefix %s", &chroot_path))
2171         {
2172           vec_add1 (chroot_path, 0);
2173           vl_set_memory_root_path ((char *) chroot_path);
2174         }
2175       else if (unformat (input, "uid %d", &uid))
2176         vl_set_memory_uid (uid);
2177       else if (unformat (input, "gid %d", &gid))
2178         vl_set_memory_gid (gid);
2179       else if (unformat (input, "baseva %llx", &baseva))
2180         vl_set_global_memory_baseva (baseva);
2181       else if (unformat (input, "global-size %lldM", &size))
2182         vl_set_global_memory_size (size * (1ULL << 20));
2183       else if (unformat (input, "global-size %lldG", &size))
2184         vl_set_global_memory_size (size * (1ULL << 30));
2185       else if (unformat (input, "global-size %lld", &size))
2186         vl_set_global_memory_size (size);
2187       else if (unformat (input, "global-pvt-heap-size %lldM", &pvt_heap_size))
2188         vl_set_global_pvt_heap_size (pvt_heap_size * (1ULL << 20));
2189       else if (unformat (input, "global-pvt-heap-size size %lld",
2190                          &pvt_heap_size))
2191         vl_set_global_pvt_heap_size (pvt_heap_size);
2192       else if (unformat (input, "api-pvt-heap-size %lldM", &pvt_heap_size))
2193         vl_set_api_pvt_heap_size (pvt_heap_size * (1ULL << 20));
2194       else if (unformat (input, "api-pvt-heap-size size %lld",
2195                          &pvt_heap_size))
2196         vl_set_api_pvt_heap_size (pvt_heap_size);
2197       else if (unformat (input, "api-size %lldM", &size))
2198         vl_set_api_memory_size (size * (1ULL << 20));
2199       else if (unformat (input, "api-size %lldG", &size))
2200         vl_set_api_memory_size (size * (1ULL << 30));
2201       else if (unformat (input, "api-size %lld", &size))
2202         vl_set_api_memory_size (size);
2203       else if (unformat (input, "uid %s", &s))
2204         {
2205           /* lookup the username */
2206           pw = NULL;
2207           while (((rv =
2208                    getpwnam_r (s, &_pw, buf, vec_len (buf), &pw)) == ERANGE)
2209                  && (vec_len (buf) <= max_buf_size))
2210             {
2211               vec_resize (buf, vec_len (buf) * 2);
2212             }
2213           if (rv < 0)
2214             {
2215               e = clib_error_return_code (0, rv,
2216                                           CLIB_ERROR_ERRNO_VALID |
2217                                           CLIB_ERROR_FATAL,
2218                                           "cannot fetch username %s", s);
2219               vec_free (s);
2220               vec_free (buf);
2221               return e;
2222             }
2223           if (pw == NULL)
2224             {
2225               e =
2226                 clib_error_return_fatal (0, "username %s does not exist", s);
2227               vec_free (s);
2228               vec_free (buf);
2229               return e;
2230             }
2231           vec_free (s);
2232           vl_set_memory_uid (pw->pw_uid);
2233         }
2234       else if (unformat (input, "gid %s", &s))
2235         {
2236           /* lookup the group name */
2237           grp = NULL;
2238           while (((rv =
2239                    getgrnam_r (s, &_grp, buf, vec_len (buf), &grp)) == ERANGE)
2240                  && (vec_len (buf) <= max_buf_size))
2241             {
2242               vec_resize (buf, vec_len (buf) * 2);
2243             }
2244           if (rv != 0)
2245             {
2246               e = clib_error_return_code (0, rv,
2247                                           CLIB_ERROR_ERRNO_VALID |
2248                                           CLIB_ERROR_FATAL,
2249                                           "cannot fetch group %s", s);
2250               vec_free (s);
2251               vec_free (buf);
2252               return e;
2253             }
2254           if (grp == NULL)
2255             {
2256               e = clib_error_return_fatal (0, "group %s does not exist", s);
2257               vec_free (s);
2258               vec_free (buf);
2259               return e;
2260             }
2261           vec_free (s);
2262           vec_free (buf);
2263           vl_set_memory_gid (grp->gr_gid);
2264         }
2265       else
2266         return clib_error_return (0, "unknown input `%U'",
2267                                   format_unformat_error, input);
2268     }
2269   return 0;
2270 }
2271
2272 VLIB_EARLY_CONFIG_FUNCTION (api_segment_config, "api-segment");
2273
2274 void *
2275 get_unformat_vnet_sw_interface (void)
2276 {
2277   return (void *) &unformat_vnet_sw_interface;
2278 }
2279
2280 static u8 *
2281 format_arp_event (u8 * s, va_list * args)
2282 {
2283   vl_api_ip4_arp_event_t *event = va_arg (*args, vl_api_ip4_arp_event_t *);
2284
2285   s = format (s, "pid %d: ", ntohl (event->pid));
2286   if (event->mac_ip)
2287     s = format (s, "bd mac/ip4 binding events");
2288   else
2289     s = format (s, "resolution for %U", format_ip4_address, &event->address);
2290   return s;
2291 }
2292
2293 static u8 *
2294 format_nd_event (u8 * s, va_list * args)
2295 {
2296   vl_api_ip6_nd_event_t *event = va_arg (*args, vl_api_ip6_nd_event_t *);
2297
2298   s = format (s, "pid %d: ", ntohl (event->pid));
2299   if (event->mac_ip)
2300     s = format (s, "bd mac/ip6 binding events");
2301   else
2302     s = format (s, "resolution for %U", format_ip6_address, event->address);
2303   return s;
2304 }
2305
2306 static clib_error_t *
2307 show_ip_arp_nd_events_fn (vlib_main_t * vm,
2308                           unformat_input_t * input, vlib_cli_command_t * cmd)
2309 {
2310   vpe_api_main_t *am = &vpe_api_main;
2311   vl_api_ip4_arp_event_t *arp_event;
2312   vl_api_ip6_nd_event_t *nd_event;
2313
2314   if ((pool_elts (am->arp_events) == 0) && (pool_elts (am->nd_events) == 0))
2315     {
2316       vlib_cli_output (vm, "No active arp or nd event registrations");
2317       return 0;
2318     }
2319
2320   /* *INDENT-OFF* */
2321   pool_foreach (arp_event, am->arp_events,
2322   ({
2323     vlib_cli_output (vm, "%U", format_arp_event, arp_event);
2324   }));
2325
2326   pool_foreach (nd_event, am->nd_events,
2327   ({
2328     vlib_cli_output (vm, "%U", format_nd_event, nd_event);
2329   }));
2330   /* *INDENT-ON* */
2331
2332   return 0;
2333 }
2334
2335 /* *INDENT-OFF* */
2336 VLIB_CLI_COMMAND (show_ip_arp_nd_events, static) = {
2337   .path = "show arp-nd-event registrations",
2338   .function = show_ip_arp_nd_events_fn,
2339   .short_help = "Show ip4 arp and ip6 nd event registrations",
2340 };
2341 /* *INDENT-ON* */
2342
2343 #define vl_msg_name_crc_list
2344 #include <vpp/api/vpe_all_api_h.h>
2345 #undef vl_msg_name_crc_list
2346
2347 static void
2348 setup_message_id_table (api_main_t * am)
2349 {
2350 #define _(id,n,crc) vl_msg_api_add_msg_name_crc (am, #n "_" #crc, id);
2351   foreach_vl_msg_name_crc_memclnt;
2352   foreach_vl_msg_name_crc_vpe;
2353 #undef _
2354 }
2355
2356
2357 /*
2358  * fd.io coding-style-patch-verification: ON
2359  *
2360  * Local Variables:
2361  * eval: (c-set-style "gnu")
2362  * End:
2363  */