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