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