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