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