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