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