api: enforce vla is last and fixed string type
[vpp.git] / src / vnet / interface_api.c
1 /*
2  *------------------------------------------------------------------
3  * interface_api.c - vnet interface api
4  *
5  * Copyright (c) 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 <vnet/vnet.h>
21 #include <vlibmemory/api.h>
22
23 #include <vnet/interface.h>
24 #include <vnet/api_errno.h>
25 #include <vnet/ethernet/ethernet.h>
26 #include <vnet/ip/ip.h>
27 #include <vnet/fib/fib_table.h>
28 #include <vnet/mfib/mfib_table.h>
29 #include <vnet/l2/l2_vtr.h>
30 #include <vnet/vnet_msg_enum.h>
31 #include <vnet/fib/fib_api.h>
32 #include <vnet/mfib/mfib_table.h>
33
34 #include <vlibapi/api_types.h>
35
36 #include <vnet/ip/ip_types_api.h>
37 #include <vnet/ethernet/ethernet_types_api.h>
38
39 #define vl_typedefs             /* define message structures */
40 #include <vnet/vnet_all_api_h.h>
41 #undef vl_typedefs
42
43 #define vl_endianfun            /* define message structures */
44 #include <vnet/vnet_all_api_h.h>
45 #undef vl_endianfun
46
47 /* instantiate all the print functions we know about */
48 #define vl_print(handle, ...) vlib_cli_output (handle, __VA_ARGS__)
49 #define vl_printfun
50 #include <vnet/vnet_all_api_h.h>
51 #undef vl_printfun
52
53 #include <vlibapi/api_helper_macros.h>
54 vpe_api_main_t vpe_api_main;
55
56 #define foreach_vpe_api_msg                                     \
57 _(SW_INTERFACE_SET_FLAGS, sw_interface_set_flags)               \
58 _(HW_INTERFACE_SET_MTU, hw_interface_set_mtu)                   \
59 _(SW_INTERFACE_SET_MTU, sw_interface_set_mtu)                   \
60 _(WANT_INTERFACE_EVENTS, want_interface_events)                 \
61 _(SW_INTERFACE_DUMP, sw_interface_dump)                         \
62 _(SW_INTERFACE_ADD_DEL_ADDRESS, sw_interface_add_del_address)   \
63 _(SW_INTERFACE_SET_RX_MODE, sw_interface_set_rx_mode)           \
64 _(SW_INTERFACE_RX_PLACEMENT_DUMP, sw_interface_rx_placement_dump) \
65 _(SW_INTERFACE_SET_RX_PLACEMENT, sw_interface_set_rx_placement) \
66 _(SW_INTERFACE_SET_TABLE, sw_interface_set_table)               \
67 _(SW_INTERFACE_GET_TABLE, sw_interface_get_table)               \
68 _(SW_INTERFACE_SET_UNNUMBERED, sw_interface_set_unnumbered)     \
69 _(SW_INTERFACE_CLEAR_STATS, sw_interface_clear_stats)           \
70 _(SW_INTERFACE_TAG_ADD_DEL, sw_interface_tag_add_del)           \
71 _(SW_INTERFACE_SET_MAC_ADDRESS, sw_interface_set_mac_address)   \
72 _(SW_INTERFACE_GET_MAC_ADDRESS, sw_interface_get_mac_address)   \
73 _(CREATE_VLAN_SUBIF, create_vlan_subif)                         \
74 _(CREATE_SUBIF, create_subif)                                   \
75 _(DELETE_SUBIF, delete_subif)                                   \
76 _(CREATE_LOOPBACK, create_loopback)                             \
77 _(CREATE_LOOPBACK_INSTANCE, create_loopback_instance)           \
78 _(DELETE_LOOPBACK, delete_loopback)                             \
79 _(INTERFACE_NAME_RENUMBER, interface_name_renumber)             \
80 _(COLLECT_DETAILED_INTERFACE_STATS, collect_detailed_interface_stats) \
81 _(SW_INTERFACE_SET_IP_DIRECTED_BROADCAST,                            \
82   sw_interface_set_ip_directed_broadcast)
83
84 static void
85 vl_api_sw_interface_set_flags_t_handler (vl_api_sw_interface_set_flags_t * mp)
86 {
87   vl_api_sw_interface_set_flags_reply_t *rmp;
88   vnet_main_t *vnm = vnet_get_main ();
89   int rv = 0;
90   clib_error_t *error;
91   u16 flags;
92
93   VALIDATE_SW_IF_INDEX (mp);
94
95   flags =
96     ((ntohl (mp->flags)) & IF_STATUS_API_FLAG_ADMIN_UP) ?
97     VNET_SW_INTERFACE_FLAG_ADMIN_UP : 0;
98
99   error = vnet_sw_interface_set_flags (vnm, ntohl (mp->sw_if_index), flags);
100   if (error)
101     {
102       rv = -1;
103       clib_error_report (error);
104     }
105
106   BAD_SW_IF_INDEX_LABEL;
107   REPLY_MACRO (VL_API_SW_INTERFACE_SET_FLAGS_REPLY);
108 }
109
110 static void
111 vl_api_hw_interface_set_mtu_t_handler (vl_api_hw_interface_set_mtu_t * mp)
112 {
113   vl_api_hw_interface_set_mtu_reply_t *rmp;
114   vnet_main_t *vnm = vnet_get_main ();
115   u32 sw_if_index = ntohl (mp->sw_if_index);
116   u16 mtu = ntohs (mp->mtu);
117   ethernet_main_t *em = &ethernet_main;
118   int rv = 0;
119
120   VALIDATE_SW_IF_INDEX (mp);
121
122   vnet_sw_interface_t *si = vnet_get_sw_interface (vnm, sw_if_index);
123   if (si->type != VNET_SW_INTERFACE_TYPE_HARDWARE)
124     {
125       rv = VNET_API_ERROR_INVALID_VALUE;
126       goto bad_sw_if_index;
127     }
128
129   vnet_hw_interface_t *hi = vnet_get_hw_interface (vnm, si->hw_if_index);
130   ethernet_interface_t *eif = ethernet_get_interface (em, si->hw_if_index);
131
132   if (!eif)
133     {
134       rv = VNET_API_ERROR_FEATURE_DISABLED;
135       goto bad_sw_if_index;
136     }
137
138   if (mtu < hi->min_supported_packet_bytes)
139     {
140       rv = VNET_API_ERROR_INVALID_VALUE;
141       goto bad_sw_if_index;
142     }
143
144   if (mtu > hi->max_supported_packet_bytes)
145     {
146       rv = VNET_API_ERROR_INVALID_VALUE;
147       goto bad_sw_if_index;
148     }
149
150   vnet_hw_interface_set_mtu (vnm, si->hw_if_index, mtu);
151
152   BAD_SW_IF_INDEX_LABEL;
153   REPLY_MACRO (VL_API_HW_INTERFACE_SET_MTU_REPLY);
154 }
155
156 static void
157 vl_api_sw_interface_set_mtu_t_handler (vl_api_sw_interface_set_mtu_t * mp)
158 {
159   vl_api_sw_interface_set_mtu_reply_t *rmp;
160   vnet_main_t *vnm = vnet_get_main ();
161   u32 sw_if_index = ntohl (mp->sw_if_index);
162   int rv = 0;
163   int i;
164   u32 per_protocol_mtu[VNET_N_MTU];
165
166   VALIDATE_SW_IF_INDEX (mp);
167
168   for (i = 0; i < VNET_N_MTU; i++)
169     per_protocol_mtu[i] = ntohl (mp->mtu[i]);
170
171   vnet_sw_interface_set_protocol_mtu (vnm, sw_if_index, per_protocol_mtu);
172
173   BAD_SW_IF_INDEX_LABEL;
174   REPLY_MACRO (VL_API_SW_INTERFACE_SET_MTU_REPLY);
175 }
176
177 static void
178   vl_api_sw_interface_set_ip_directed_broadcast_t_handler
179   (vl_api_sw_interface_set_ip_directed_broadcast_t * mp)
180 {
181   vl_api_sw_interface_set_ip_directed_broadcast_reply_t *rmp;
182   u32 sw_if_index = ntohl (mp->sw_if_index);
183   int rv = 0;
184
185   VALIDATE_SW_IF_INDEX (mp);
186
187   vnet_sw_interface_ip_directed_broadcast (vnet_get_main (),
188                                            sw_if_index, mp->enable);
189
190   BAD_SW_IF_INDEX_LABEL;
191   REPLY_MACRO (VL_API_SW_INTERFACE_SET_IP_DIRECTED_BROADCAST_REPLY);
192 }
193
194 static void
195 send_sw_interface_details (vpe_api_main_t * am,
196                            vl_api_registration_t * rp,
197                            vnet_sw_interface_t * swif,
198                            u8 * interface_name, u32 context)
199 {
200   vnet_hw_interface_t *hi =
201     vnet_get_sup_hw_interface (am->vnet_main, swif->sw_if_index);
202
203   vl_api_sw_interface_details_t *mp = vl_msg_api_alloc (sizeof (*mp));
204   clib_memset (mp, 0, sizeof (*mp));
205   mp->_vl_msg_id = ntohs (VL_API_SW_INTERFACE_DETAILS);
206   mp->sw_if_index = ntohl (swif->sw_if_index);
207   mp->sup_sw_if_index = ntohl (swif->sup_sw_if_index);
208
209   mp->flags |= (swif->flags & VNET_SW_INTERFACE_FLAG_ADMIN_UP) ?
210     IF_STATUS_API_FLAG_ADMIN_UP : 0;
211   mp->flags |= (hi->flags & VNET_HW_INTERFACE_FLAG_LINK_UP) ?
212     IF_STATUS_API_FLAG_LINK_UP : 0;
213   mp->flags = ntohl (mp->flags);
214
215   switch (swif->type)
216     {
217     case VNET_SW_INTERFACE_TYPE_SUB:
218       mp->type = IF_API_TYPE_SUB;
219       break;
220     case VNET_SW_INTERFACE_TYPE_P2P:
221       mp->type = IF_API_TYPE_P2P;
222       break;
223     case VNET_SW_INTERFACE_TYPE_PIPE:
224       mp->type = IF_API_TYPE_PIPE;
225       break;
226     default:
227       mp->type = IF_API_TYPE_HARDWARE;
228     }
229   mp->type = ntohl (mp->type);
230
231   mp->link_duplex = ((hi->flags & VNET_HW_INTERFACE_FLAG_DUPLEX_MASK) >>
232                      VNET_HW_INTERFACE_FLAG_DUPLEX_SHIFT);
233   mp->link_speed = ntohl (hi->link_speed);
234   mp->link_mtu = ntohs (hi->max_packet_bytes);
235   mp->mtu[VNET_MTU_L3] = ntohl (swif->mtu[VNET_MTU_L3]);
236   mp->mtu[VNET_MTU_IP4] = ntohl (swif->mtu[VNET_MTU_IP4]);
237   mp->mtu[VNET_MTU_IP6] = ntohl (swif->mtu[VNET_MTU_IP6]);
238   mp->mtu[VNET_MTU_MPLS] = ntohl (swif->mtu[VNET_MTU_MPLS]);
239
240   mp->context = context;
241
242   strncpy ((char *) mp->interface_name,
243            (char *) interface_name, ARRAY_LEN (mp->interface_name) - 1);
244
245   /* Send the L2 address for ethernet physical intfcs */
246   if (swif->sup_sw_if_index == swif->sw_if_index
247       && hi->hw_class_index == ethernet_hw_interface_class.index)
248     {
249       ethernet_main_t *em = ethernet_get_main (am->vlib_main);
250       ethernet_interface_t *ei;
251
252       ei = pool_elt_at_index (em->interfaces, hi->hw_instance);
253       ASSERT (sizeof (mp->l2_address) >= sizeof (ei->address));
254       mac_address_encode ((mac_address_t *) ei->address, mp->l2_address);
255     }
256   else if (swif->sup_sw_if_index != swif->sw_if_index)
257     {
258       vnet_sub_interface_t *sub = &swif->sub;
259       mp->sub_id = ntohl (sub->id);
260       mp->sub_number_of_tags =
261         sub->eth.flags.one_tag + sub->eth.flags.two_tags * 2;
262       mp->sub_outer_vlan_id = ntohs (sub->eth.outer_vlan_id);
263       mp->sub_inner_vlan_id = ntohs (sub->eth.inner_vlan_id);
264       mp->sub_if_flags =
265         ntohl (sub->eth.raw_flags & SUB_IF_API_FLAG_MASK_VNET);
266
267       /* vlan tag rewrite data */
268       u32 vtr_op = L2_VTR_DISABLED;
269       u32 vtr_push_dot1q = 0, vtr_tag1 = 0, vtr_tag2 = 0;
270
271       if (l2vtr_get (am->vlib_main, am->vnet_main, swif->sw_if_index,
272                      &vtr_op, &vtr_push_dot1q, &vtr_tag1, &vtr_tag2) != 0)
273         {
274           // error - default to disabled
275           mp->vtr_op = ntohl (L2_VTR_DISABLED);
276           clib_warning ("cannot get vlan tag rewrite for sw_if_index %d",
277                         swif->sw_if_index);
278         }
279       else
280         {
281           mp->vtr_op = ntohl (vtr_op);
282           mp->vtr_push_dot1q = ntohl (vtr_push_dot1q);
283           mp->vtr_tag1 = ntohl (vtr_tag1);
284           mp->vtr_tag2 = ntohl (vtr_tag2);
285         }
286     }
287
288   /* pbb tag rewrite data */
289   ethernet_header_t eth_hdr;
290   u32 pbb_vtr_op = L2_VTR_DISABLED;
291   u16 outer_tag = 0;
292   u16 b_vlanid = 0;
293   u32 i_sid = 0;
294   clib_memset (&eth_hdr, 0, sizeof (eth_hdr));
295
296   if (!l2pbb_get (am->vlib_main, am->vnet_main, swif->sw_if_index,
297                   &pbb_vtr_op, &outer_tag, &eth_hdr, &b_vlanid, &i_sid))
298     {
299       mp->sub_if_flags |= ntohl (SUB_IF_API_FLAG_DOT1AH);
300       mac_address_encode ((mac_address_t *) eth_hdr.dst_address, mp->b_dmac);
301       mac_address_encode ((mac_address_t *) eth_hdr.src_address, mp->b_smac);
302       mp->b_vlanid = b_vlanid;
303       mp->i_sid = i_sid;
304     }
305
306   u8 *tag = vnet_get_sw_interface_tag (vnet_get_main (), swif->sw_if_index);
307   if (tag)
308     strncpy ((char *) mp->tag, (char *) tag, ARRAY_LEN (mp->tag) - 1);
309
310   vl_api_send_msg (rp, (u8 *) mp);
311 }
312
313 static void
314 vl_api_sw_interface_dump_t_handler (vl_api_sw_interface_dump_t * mp)
315 {
316   vpe_api_main_t *am = &vpe_api_main;
317   vnet_sw_interface_t *swif;
318   vnet_interface_main_t *im = &am->vnet_main->interface_main;
319   vl_api_registration_t *rp;
320   u32 sw_if_index;
321
322   rp = vl_api_client_index_to_registration (mp->client_index);
323
324   if (rp == 0)
325     {
326       clib_warning ("Client %d AWOL", mp->client_index);
327       return;
328     }
329
330   u8 *filter = 0, *name = 0;
331   sw_if_index = ntohl (mp->sw_if_index);
332
333   if (!mp->name_filter_valid && sw_if_index != ~0 && sw_if_index != 0)
334     {
335       /* is it a valid sw_if_index? */
336       if (!vnet_sw_if_index_is_api_valid (sw_if_index))
337         return;
338
339       swif = vec_elt_at_index (im->sw_interfaces, sw_if_index);
340
341       vec_reset_length (name);
342       name =
343         format (name, "%U%c", format_vnet_sw_interface_name, am->vnet_main,
344                 swif, 0);
345       send_sw_interface_details (am, rp, swif, name, mp->context);
346       vec_free (name);
347       return;
348     }
349
350   if (mp->name_filter_valid)
351     {
352       filter =
353         format (0, ".*%s", vl_api_string_len (&mp->name_filter),
354                 vl_api_from_api_string (&mp->name_filter), 0);
355     }
356
357   char *strcasestr (char *, char *);    /* lnx hdr file botch */
358   /* *INDENT-OFF* */
359   pool_foreach (swif, im->sw_interfaces,
360   ({
361     if (!vnet_swif_is_api_visible (swif))
362         continue;
363     vec_reset_length(name);
364     name = format (name, "%U%c", format_vnet_sw_interface_name, am->vnet_main,
365                    swif, 0);
366
367     if (filter && !strcasestr((char *) name, (char *) filter))
368         continue;
369
370     send_sw_interface_details (am, rp, swif, name, mp->context);
371   }));
372   /* *INDENT-ON* */
373
374   vec_free (name);
375   vec_free (filter);
376 }
377
378 static void
379   vl_api_sw_interface_add_del_address_t_handler
380   (vl_api_sw_interface_add_del_address_t * mp)
381 {
382   vlib_main_t *vm = vlib_get_main ();
383   vnet_main_t *vnm = vnet_get_main ();
384   vl_api_sw_interface_add_del_address_reply_t *rmp;
385   int rv = 0;
386   u32 is_del;
387   clib_error_t *error = 0;
388   ip46_address_t address;
389
390   VALIDATE_SW_IF_INDEX (mp);
391
392   is_del = mp->is_add == 0;
393   vnm->api_errno = 0;
394
395   if (mp->del_all)
396     ip_del_all_interface_addresses (vm, ntohl (mp->sw_if_index));
397   else if (ip_address_decode (&mp->prefix.address, &address) == IP46_TYPE_IP6)
398     error = ip6_add_del_interface_address (vm, ntohl (mp->sw_if_index),
399                                            (void *) &address.ip6,
400                                            mp->prefix.len, is_del);
401   else
402     error = ip4_add_del_interface_address (vm, ntohl (mp->sw_if_index),
403                                            (void *) &address.ip4,
404                                            mp->prefix.len, is_del);
405
406   if (error)
407     {
408       rv = vnm->api_errno;
409       clib_error_report (error);
410       goto done;
411     }
412
413   BAD_SW_IF_INDEX_LABEL;
414
415 done:
416   REPLY_MACRO (VL_API_SW_INTERFACE_ADD_DEL_ADDRESS_REPLY);
417 }
418
419 static void
420 vl_api_sw_interface_set_table_t_handler (vl_api_sw_interface_set_table_t * mp)
421 {
422   vl_api_sw_interface_set_table_reply_t *rmp;
423   u32 sw_if_index = ntohl (mp->sw_if_index);
424   u32 table_id = ntohl (mp->vrf_id);
425   int rv = 0;
426
427   VALIDATE_SW_IF_INDEX (mp);
428
429   if (mp->is_ipv6)
430     rv = ip_table_bind (FIB_PROTOCOL_IP6, sw_if_index, table_id, 1);
431   else
432     rv = ip_table_bind (FIB_PROTOCOL_IP4, sw_if_index, table_id, 1);
433
434   BAD_SW_IF_INDEX_LABEL;
435
436   REPLY_MACRO (VL_API_SW_INTERFACE_SET_TABLE_REPLY);
437 }
438
439 int
440 ip_table_bind (fib_protocol_t fproto,
441                u32 sw_if_index, u32 table_id, u8 is_api)
442 {
443   CLIB_UNUSED (ip_interface_address_t * ia);
444   u32 fib_index, mfib_index;
445   fib_source_t src;
446   mfib_source_t msrc;
447
448   if (is_api)
449     {
450       src = FIB_SOURCE_API;
451       msrc = MFIB_SOURCE_API;
452     }
453   else
454     {
455       src = FIB_SOURCE_CLI;
456       msrc = MFIB_SOURCE_CLI;
457     }
458
459   /*
460    * This if table does not exist = error is what we want in the end.
461    */
462   fib_index = fib_table_find (fproto, table_id);
463   mfib_index = mfib_table_find (fproto, table_id);
464
465   if (~0 == fib_index || ~0 == mfib_index)
466     {
467       return (VNET_API_ERROR_NO_SUCH_FIB);
468     }
469
470   if (FIB_PROTOCOL_IP6 == fproto)
471     {
472       /*
473        * If the interface already has in IP address, then a change int
474        * VRF is not allowed. The IP address applied must first be removed.
475        * We do not do that automatically here, since VPP has no knowledge
476        * of whether those subnets are valid in the destination VRF.
477        */
478       /* *INDENT-OFF* */
479       foreach_ip_interface_address (&ip6_main.lookup_main,
480                                     ia, sw_if_index,
481                                     1 /* honor unnumbered */ ,
482       ({
483         return (VNET_API_ERROR_ADDRESS_FOUND_FOR_INTERFACE);
484       }));
485       /* *INDENT-ON* */
486
487       vec_validate (ip6_main.fib_index_by_sw_if_index, sw_if_index);
488       vec_validate (ip6_main.mfib_index_by_sw_if_index, sw_if_index);
489
490       /*
491        * tell those that are interested that the binding is changing.
492        */
493       ip6_table_bind_callback_t *cb;
494       vec_foreach (cb, ip6_main.table_bind_callbacks)
495         cb->function (&ip6_main, cb->function_opaque,
496                       sw_if_index,
497                       fib_index,
498                       ip6_main.fib_index_by_sw_if_index[sw_if_index]);
499
500       if (0 == table_id)
501         {
502           /* reset back to default */
503           if (0 != ip6_main.fib_index_by_sw_if_index[sw_if_index])
504             fib_table_unlock (ip6_main.fib_index_by_sw_if_index[sw_if_index],
505                               FIB_PROTOCOL_IP6, src);
506           if (0 != ip6_main.mfib_index_by_sw_if_index[sw_if_index])
507             mfib_table_unlock (ip6_main.mfib_index_by_sw_if_index
508                                [sw_if_index], FIB_PROTOCOL_IP6, msrc);
509
510         }
511       else
512         {
513           /* we need to lock the table now it's inuse */
514           fib_table_lock (fib_index, FIB_PROTOCOL_IP6, src);
515           mfib_table_lock (mfib_index, FIB_PROTOCOL_IP6, msrc);
516         }
517
518       ip6_main.fib_index_by_sw_if_index[sw_if_index] = fib_index;
519       ip6_main.mfib_index_by_sw_if_index[sw_if_index] = mfib_index;
520     }
521   else
522     {
523       /*
524        * If the interface already has in IP address, then a change int
525        * VRF is not allowed. The IP address applied must first be removed.
526        * We do not do that automatically here, since VPP has no knowledge
527        * of whether those subnets are valid in the destination VRF.
528        */
529       /* *INDENT-OFF* */
530       foreach_ip_interface_address (&ip4_main.lookup_main,
531                                     ia, sw_if_index,
532                                     1 /* honor unnumbered */ ,
533       ({
534         return (VNET_API_ERROR_ADDRESS_FOUND_FOR_INTERFACE);
535       }));
536       /* *INDENT-ON* */
537
538       vec_validate (ip4_main.fib_index_by_sw_if_index, sw_if_index);
539       vec_validate (ip4_main.mfib_index_by_sw_if_index, sw_if_index);
540
541       /*
542        * tell those that are interested that the binding is changing.
543        */
544       ip4_table_bind_callback_t *cb;
545       vec_foreach (cb, ip4_main.table_bind_callbacks)
546         cb->function (&ip4_main, cb->function_opaque,
547                       sw_if_index,
548                       fib_index,
549                       ip4_main.fib_index_by_sw_if_index[sw_if_index]);
550
551       if (0 == table_id)
552         {
553           /* reset back to default */
554           if (0 != ip4_main.fib_index_by_sw_if_index[sw_if_index])
555             fib_table_unlock (ip4_main.fib_index_by_sw_if_index[sw_if_index],
556                               FIB_PROTOCOL_IP4, src);
557           if (0 != ip4_main.mfib_index_by_sw_if_index[sw_if_index])
558             mfib_table_unlock (ip4_main.mfib_index_by_sw_if_index
559                                [sw_if_index], FIB_PROTOCOL_IP4, msrc);
560
561         }
562       else
563         {
564           /* we need to lock the table now it's inuse */
565           fib_index = fib_table_find_or_create_and_lock (FIB_PROTOCOL_IP4,
566                                                          table_id, src);
567
568           mfib_index = mfib_table_find_or_create_and_lock (FIB_PROTOCOL_IP4,
569                                                            table_id, msrc);
570         }
571
572       ip4_main.fib_index_by_sw_if_index[sw_if_index] = fib_index;
573       ip4_main.mfib_index_by_sw_if_index[sw_if_index] = mfib_index;
574     }
575
576   return (0);
577 }
578
579 static void
580 send_sw_interface_get_table_reply (vl_api_registration_t * reg,
581                                    u32 context, int retval, u32 vrf_id)
582 {
583   vl_api_sw_interface_get_table_reply_t *mp;
584
585   mp = vl_msg_api_alloc (sizeof (*mp));
586   clib_memset (mp, 0, sizeof (*mp));
587   mp->_vl_msg_id = ntohs (VL_API_SW_INTERFACE_GET_TABLE_REPLY);
588   mp->context = context;
589   mp->retval = htonl (retval);
590   mp->vrf_id = htonl (vrf_id);
591
592   vl_api_send_msg (reg, (u8 *) mp);
593 }
594
595 static void
596 vl_api_sw_interface_get_table_t_handler (vl_api_sw_interface_get_table_t * mp)
597 {
598   vl_api_registration_t *reg;
599   fib_table_t *fib_table = 0;
600   u32 sw_if_index = ~0;
601   u32 fib_index = ~0;
602   u32 table_id = ~0;
603   fib_protocol_t fib_proto = FIB_PROTOCOL_IP4;
604   int rv = 0;
605
606   reg = vl_api_client_index_to_registration (mp->client_index);
607   if (!reg)
608     return;
609
610   VALIDATE_SW_IF_INDEX (mp);
611
612   sw_if_index = ntohl (mp->sw_if_index);
613
614   if (mp->is_ipv6)
615     fib_proto = FIB_PROTOCOL_IP6;
616
617   fib_index = fib_table_get_index_for_sw_if_index (fib_proto, sw_if_index);
618   if (fib_index != ~0)
619     {
620       fib_table = fib_table_get (fib_index, fib_proto);
621       table_id = fib_table->ft_table_id;
622     }
623
624   BAD_SW_IF_INDEX_LABEL;
625
626   send_sw_interface_get_table_reply (reg, mp->context, rv, table_id);
627 }
628
629 static void vl_api_sw_interface_set_unnumbered_t_handler
630   (vl_api_sw_interface_set_unnumbered_t * mp)
631 {
632   vl_api_sw_interface_set_unnumbered_reply_t *rmp;
633   int rv = 0;
634   vnet_main_t *vnm = vnet_get_main ();
635   u32 sw_if_index = ntohl (mp->sw_if_index);
636   u32 unnumbered_sw_if_index = ntohl (mp->unnumbered_sw_if_index);
637
638   /*
639    * The API message field names are backwards from
640    * the underlying data structure names.
641    * It's not worth changing them now.
642    */
643   if (!vnet_sw_interface_is_api_valid (vnm, unnumbered_sw_if_index))
644     {
645       rv = VNET_API_ERROR_INVALID_SW_IF_INDEX;
646       goto done;
647     }
648
649   /* Only check the "use loop0" field when setting the binding */
650   if (mp->is_add && !vnet_sw_interface_is_api_valid (vnm, sw_if_index))
651     {
652       rv = VNET_API_ERROR_INVALID_SW_IF_INDEX_2;
653       goto done;
654     }
655
656   vnet_sw_interface_update_unnumbered (unnumbered_sw_if_index,
657                                        sw_if_index, mp->is_add);
658 done:
659   REPLY_MACRO (VL_API_SW_INTERFACE_SET_UNNUMBERED_REPLY);
660 }
661
662 static void
663 vl_api_sw_interface_clear_stats_t_handler (vl_api_sw_interface_clear_stats_t *
664                                            mp)
665 {
666   vl_api_sw_interface_clear_stats_reply_t *rmp;
667
668   vnet_main_t *vnm = vnet_get_main ();
669   vnet_interface_main_t *im = &vnm->interface_main;
670   vlib_simple_counter_main_t *sm;
671   vlib_combined_counter_main_t *cm;
672   int j, n_counters;
673   int rv = 0;
674
675   if (mp->sw_if_index != ~0)
676     VALIDATE_SW_IF_INDEX (mp);
677
678   n_counters = vec_len (im->combined_sw_if_counters);
679
680   for (j = 0; j < n_counters; j++)
681     {
682       im = &vnm->interface_main;
683       cm = im->combined_sw_if_counters + j;
684       if (mp->sw_if_index == (u32) ~ 0)
685         vlib_clear_combined_counters (cm);
686       else
687         vlib_zero_combined_counter (cm, ntohl (mp->sw_if_index));
688     }
689
690   n_counters = vec_len (im->sw_if_counters);
691
692   for (j = 0; j < n_counters; j++)
693     {
694       im = &vnm->interface_main;
695       sm = im->sw_if_counters + j;
696       if (mp->sw_if_index == (u32) ~ 0)
697         vlib_clear_simple_counters (sm);
698       else
699         vlib_zero_simple_counter (sm, ntohl (mp->sw_if_index));
700     }
701
702   BAD_SW_IF_INDEX_LABEL;
703
704   REPLY_MACRO (VL_API_SW_INTERFACE_CLEAR_STATS_REPLY);
705 }
706
707 /*
708  * Events used for sw_interface_events
709  */
710 enum api_events
711 {
712   API_LINK_STATE_UP_EVENT = 1 << 1,
713   API_LINK_STATE_DOWN_EVENT = 1 << 2,
714   API_ADMIN_UP_EVENT = 1 << 3,
715   API_ADMIN_DOWN_EVENT = 1 << 4,
716   API_SW_INTERFACE_ADD_EVENT = 1 << 5,
717   API_SW_INTERFACE_DEL_EVENT = 1 << 6,
718 };
719
720 static void
721 send_sw_interface_event (vpe_api_main_t * am,
722                          vpe_client_registration_t * reg,
723                          vl_api_registration_t * vl_reg,
724                          u32 sw_if_index, enum api_events events)
725 {
726   vl_api_sw_interface_event_t *mp;
727
728   mp = vl_msg_api_alloc (sizeof (*mp));
729   clib_memset (mp, 0, sizeof (*mp));
730   mp->_vl_msg_id = ntohs (VL_API_SW_INTERFACE_EVENT);
731   mp->sw_if_index = ntohl (sw_if_index);
732   mp->client_index = reg->client_index;
733   mp->pid = reg->client_pid;
734   mp->flags = 0;
735   mp->flags |= (events & API_ADMIN_UP_EVENT) ?
736     IF_STATUS_API_FLAG_ADMIN_UP : 0;
737   mp->flags |= (events & API_LINK_STATE_UP_EVENT) ?
738     IF_STATUS_API_FLAG_LINK_UP : 0;
739   mp->flags = ntohl (mp->flags);
740   mp->deleted = events & API_SW_INTERFACE_DEL_EVENT ? true : false;
741   vl_api_send_msg (vl_reg, (u8 *) mp);
742 }
743
744 static uword
745 link_state_process (vlib_main_t * vm,
746                     vlib_node_runtime_t * rt, vlib_frame_t * f)
747 {
748   vpe_api_main_t *vam = &vpe_api_main;
749   uword *event_by_sw_if_index = 0;
750   vpe_client_registration_t *reg;
751   int i;
752   vl_api_registration_t *vl_reg;
753   uword event_type;
754   uword *event_data = 0;
755   u32 sw_if_index;
756
757   vam->link_state_process_up = 1;
758
759   while (1)
760     {
761       vlib_process_wait_for_event (vm);
762
763       /* Batch up events */
764       while ((event_type = vlib_process_get_events (vm, &event_data)) != ~0)
765         {
766           for (i = 0; i < vec_len (event_data); i++)
767             {
768               sw_if_index = event_data[i];
769               vec_validate_init_empty (event_by_sw_if_index, sw_if_index, 0);
770               event_by_sw_if_index[sw_if_index] |= event_type;
771             }
772           vec_reset_length (event_data);
773         }
774
775       for (i = 0; i < vec_len (event_by_sw_if_index); i++)
776         {
777           if (event_by_sw_if_index[i] == 0)
778             continue;
779
780           /* *INDENT-OFF* */
781           pool_foreach(reg, vam->interface_events_registrations,
782           ({
783             vl_reg = vl_api_client_index_to_registration (reg->client_index);
784             if (vl_reg)
785               send_sw_interface_event (vam, reg, vl_reg, i, event_by_sw_if_index[i]);
786           }));
787           /* *INDENT-ON* */
788         }
789       vec_reset_length (event_by_sw_if_index);
790     }
791
792   return 0;
793 }
794
795 static clib_error_t *link_up_down_function (vnet_main_t * vm, u32 hw_if_index,
796                                             u32 flags);
797 static clib_error_t *admin_up_down_function (vnet_main_t * vm,
798                                              u32 hw_if_index, u32 flags);
799 static clib_error_t *sw_interface_add_del_function (vnet_main_t * vm,
800                                                     u32 sw_if_index,
801                                                     u32 flags);
802
803 /* *INDENT-OFF* */
804 VLIB_REGISTER_NODE (link_state_process_node,static) = {
805   .function = link_state_process,
806   .type = VLIB_NODE_TYPE_PROCESS,
807   .name = "vpe-link-state-process",
808 };
809 /* *INDENT-ON* */
810
811 VNET_SW_INTERFACE_ADMIN_UP_DOWN_FUNCTION (admin_up_down_function);
812 VNET_HW_INTERFACE_LINK_UP_DOWN_FUNCTION (link_up_down_function);
813 VNET_SW_INTERFACE_ADD_DEL_FUNCTION (sw_interface_add_del_function);
814
815 static clib_error_t *
816 link_up_down_function (vnet_main_t * vm, u32 hw_if_index, u32 flags)
817 {
818   vpe_api_main_t *vam = &vpe_api_main;
819   vnet_hw_interface_t *hi = vnet_get_hw_interface (vm, hw_if_index);
820
821   if (vam->link_state_process_up)
822     {
823       enum api_events event =
824         flags ? API_LINK_STATE_UP_EVENT : API_LINK_STATE_DOWN_EVENT;
825       vlib_process_signal_event (vam->vlib_main,
826                                  link_state_process_node.index, event,
827                                  hi->sw_if_index);
828     }
829   return 0;
830 }
831
832 static clib_error_t *
833 admin_up_down_function (vnet_main_t * vm, u32 sw_if_index, u32 flags)
834 {
835   vpe_api_main_t *vam = &vpe_api_main;
836
837   /*
838    * Note: it's perfectly fair to set a subif admin up / admin down.
839    * Note the subtle distinction between this routine and the previous
840    * routine.
841    */
842   if (vam->link_state_process_up)
843     {
844       enum api_events event =
845         flags ? API_ADMIN_UP_EVENT : API_ADMIN_DOWN_EVENT;
846       vlib_process_signal_event (vam->vlib_main,
847                                  link_state_process_node.index, event,
848                                  sw_if_index);
849     }
850   return 0;
851 }
852
853 static clib_error_t *
854 sw_interface_add_del_function (vnet_main_t * vm, u32 sw_if_index, u32 flags)
855 {
856   vpe_api_main_t *vam = &vpe_api_main;
857
858   if (vam->link_state_process_up)
859     {
860       enum api_events event =
861         flags ? API_SW_INTERFACE_ADD_EVENT : API_SW_INTERFACE_DEL_EVENT;
862       vlib_process_signal_event (vam->vlib_main,
863                                  link_state_process_node.index, event,
864                                  sw_if_index);
865     }
866   return 0;
867 }
868
869 static void vl_api_sw_interface_tag_add_del_t_handler
870   (vl_api_sw_interface_tag_add_del_t * mp)
871 {
872   vnet_main_t *vnm = vnet_get_main ();
873   vl_api_sw_interface_tag_add_del_reply_t *rmp;
874   int rv = 0;
875   u8 *tag;
876   u32 sw_if_index = ntohl (mp->sw_if_index);
877
878   VALIDATE_SW_IF_INDEX (mp);
879
880   if (mp->is_add)
881     {
882       if (mp->tag[0] == 0)
883         {
884           rv = VNET_API_ERROR_INVALID_VALUE;
885           goto out;
886         }
887
888       mp->tag[ARRAY_LEN (mp->tag) - 1] = 0;
889       tag = format (0, "%s%c", mp->tag, 0);
890       vnet_set_sw_interface_tag (vnm, tag, sw_if_index);
891     }
892   else
893     vnet_clear_sw_interface_tag (vnm, sw_if_index);
894
895   BAD_SW_IF_INDEX_LABEL;
896 out:
897   REPLY_MACRO (VL_API_SW_INTERFACE_TAG_ADD_DEL_REPLY);
898 }
899
900 static void vl_api_sw_interface_set_mac_address_t_handler
901   (vl_api_sw_interface_set_mac_address_t * mp)
902 {
903   vl_api_sw_interface_set_mac_address_reply_t *rmp;
904   vnet_main_t *vnm = vnet_get_main ();
905   u32 sw_if_index = ntohl (mp->sw_if_index);
906   vnet_sw_interface_t *si;
907   clib_error_t *error;
908   int rv = 0;
909   mac_address_t mac;
910
911   VALIDATE_SW_IF_INDEX (mp);
912
913   si = vnet_get_sw_interface (vnm, sw_if_index);
914   mac_address_decode (mp->mac_address, &mac);
915   error =
916     vnet_hw_interface_change_mac_address (vnm, si->hw_if_index, (u8 *) & mac);
917   if (error)
918     {
919       rv = VNET_API_ERROR_UNIMPLEMENTED;
920       clib_error_report (error);
921       goto out;
922     }
923
924   BAD_SW_IF_INDEX_LABEL;
925 out:
926   REPLY_MACRO (VL_API_SW_INTERFACE_SET_MAC_ADDRESS_REPLY);
927 }
928
929 static void vl_api_sw_interface_get_mac_address_t_handler
930   (vl_api_sw_interface_get_mac_address_t * mp)
931 {
932   vl_api_sw_interface_get_mac_address_reply_t *rmp;
933   vl_api_registration_t *reg;
934   vnet_main_t *vnm = vnet_get_main ();
935   u32 sw_if_index = ntohl (mp->sw_if_index);
936   vnet_sw_interface_t *si;
937   ethernet_interface_t *eth_if = 0;
938   int rv = 0;
939
940   VALIDATE_SW_IF_INDEX (mp);
941
942   si = vnet_get_sup_sw_interface (vnm, sw_if_index);
943   if (si->type == VNET_SW_INTERFACE_TYPE_HARDWARE)
944     eth_if = ethernet_get_interface (&ethernet_main, si->hw_if_index);
945
946   BAD_SW_IF_INDEX_LABEL;
947
948   reg = vl_api_client_index_to_registration (mp->client_index);
949   if (!reg)
950     return;
951   rmp = vl_msg_api_alloc (sizeof (*rmp));
952   rmp->_vl_msg_id = htons (VL_API_SW_INTERFACE_GET_MAC_ADDRESS_REPLY);
953   rmp->context = mp->context;
954   rmp->retval = htonl (rv);
955   if (!rv && eth_if)
956     mac_address_encode ((mac_address_t *) eth_if->address, rmp->mac_address);
957   vl_api_send_msg (reg, (u8 *) rmp);
958 }
959
960 static void vl_api_sw_interface_set_rx_mode_t_handler
961   (vl_api_sw_interface_set_rx_mode_t * mp)
962 {
963   vl_api_sw_interface_set_rx_mode_reply_t *rmp;
964   vnet_main_t *vnm = vnet_get_main ();
965   u32 sw_if_index = ntohl (mp->sw_if_index);
966   vnet_sw_interface_t *si;
967   clib_error_t *error;
968   int rv = 0;
969
970   VALIDATE_SW_IF_INDEX (mp);
971
972   si = vnet_get_sw_interface (vnm, sw_if_index);
973   if (si->type != VNET_SW_INTERFACE_TYPE_HARDWARE)
974     {
975       rv = VNET_API_ERROR_INVALID_VALUE;
976       goto bad_sw_if_index;
977     }
978
979   error = set_hw_interface_change_rx_mode (vnm, si->hw_if_index,
980                                            mp->queue_id_valid,
981                                            ntohl (mp->queue_id),
982                                            (vnet_hw_interface_rx_mode)
983                                            mp->mode);
984
985   if (error)
986     {
987       rv = VNET_API_ERROR_UNIMPLEMENTED;
988       clib_error_report (error);
989       goto out;
990     }
991
992   BAD_SW_IF_INDEX_LABEL;
993 out:
994   REPLY_MACRO (VL_API_SW_INTERFACE_SET_RX_MODE_REPLY);
995 }
996
997 static void
998 send_interface_rx_placement_details (vpe_api_main_t * am,
999                                      vl_api_registration_t * rp,
1000                                      u32 sw_if_index, u32 worker_id,
1001                                      u32 queue_id, u8 mode, u32 context)
1002 {
1003   vl_api_sw_interface_rx_placement_details_t *mp;
1004   mp = vl_msg_api_alloc (sizeof (*mp));
1005   clib_memset (mp, 0, sizeof (*mp));
1006
1007   mp->_vl_msg_id = htons (VL_API_SW_INTERFACE_RX_PLACEMENT_DETAILS);
1008   mp->sw_if_index = htonl (sw_if_index);
1009   mp->queue_id = htonl (queue_id);
1010   mp->worker_id = htonl (worker_id);
1011   mp->mode = mode;
1012   mp->context = context;
1013
1014   vl_api_send_msg (rp, (u8 *) mp);
1015 }
1016
1017 static void vl_api_sw_interface_rx_placement_dump_t_handler
1018   (vl_api_sw_interface_rx_placement_dump_t * mp)
1019 {
1020   vnet_main_t *vnm = vnet_get_main ();
1021   vpe_api_main_t *am = &vpe_api_main;
1022   u32 sw_if_index = ntohl (mp->sw_if_index);
1023   vl_api_registration_t *reg;
1024
1025   reg = vl_api_client_index_to_registration (mp->client_index);
1026   if (!reg)
1027     return;
1028
1029   if (sw_if_index == ~0)
1030     {
1031       vnet_device_input_runtime_t *rt;
1032       vnet_device_and_queue_t *dq;
1033       vlib_node_t *pn = vlib_get_node_by_name (am->vlib_main,
1034                                                (u8 *) "device-input");
1035       uword si;
1036       int index = 0;
1037
1038       /* *INDENT-OFF* */
1039       foreach_vlib_main (({
1040         clib_bitmap_foreach (si, pn->sibling_bitmap,
1041         ({
1042           rt = vlib_node_get_runtime_data (this_vlib_main, si);
1043           vec_foreach (dq, rt->devices_and_queues)
1044             {
1045               vnet_hw_interface_t *hw = vnet_get_hw_interface (vnm,
1046                                                              dq->hw_if_index);
1047               send_interface_rx_placement_details (am, reg, hw->sw_if_index, index,
1048                                           dq->queue_id, dq->mode, mp->context);
1049             }
1050         }));
1051         index++;
1052       }));
1053       /* *INDENT-ON* */
1054     }
1055   else
1056     {
1057       int i;
1058       vnet_sw_interface_t *si;
1059
1060       if (!vnet_sw_if_index_is_api_valid (sw_if_index))
1061         {
1062           clib_warning ("sw_if_index %u does not exist", sw_if_index);
1063           goto bad_sw_if_index;
1064         }
1065
1066       si = vnet_get_sw_interface (vnm, sw_if_index);
1067       if (si->type != VNET_SW_INTERFACE_TYPE_HARDWARE)
1068         {
1069           clib_warning ("interface type is not HARDWARE! P2P, PIPE and SUB"
1070                         " interfaces are not supported");
1071           goto bad_sw_if_index;
1072         }
1073
1074       vnet_hw_interface_t *hw = vnet_get_hw_interface (vnm, si->hw_if_index);
1075
1076       for (i = 0; i < vec_len (hw->dq_runtime_index_by_queue); i++)
1077         {
1078           send_interface_rx_placement_details (am, reg, hw->sw_if_index,
1079                                                hw->input_node_thread_index_by_queue
1080                                                [i], i,
1081                                                hw->rx_mode_by_queue[i],
1082                                                mp->context);
1083         }
1084     }
1085
1086   BAD_SW_IF_INDEX_LABEL;
1087 }
1088
1089 static void vl_api_sw_interface_set_rx_placement_t_handler
1090   (vl_api_sw_interface_set_rx_placement_t * mp)
1091 {
1092   vl_api_sw_interface_set_rx_placement_reply_t *rmp;
1093   vnet_main_t *vnm = vnet_get_main ();
1094   u32 sw_if_index = ntohl (mp->sw_if_index);
1095   vnet_sw_interface_t *si;
1096   clib_error_t *error = 0;
1097   int rv = 0;
1098
1099   VALIDATE_SW_IF_INDEX (mp);
1100
1101   si = vnet_get_sw_interface (vnm, sw_if_index);
1102   if (si->type != VNET_SW_INTERFACE_TYPE_HARDWARE)
1103     {
1104       rv = VNET_API_ERROR_INVALID_VALUE;
1105       goto bad_sw_if_index;
1106     }
1107
1108   error = set_hw_interface_rx_placement (si->hw_if_index,
1109                                          ntohl (mp->queue_id),
1110                                          ntohl (mp->worker_id), mp->is_main);
1111   if (error)
1112     {
1113       rv = VNET_API_ERROR_UNIMPLEMENTED;
1114       clib_error_report (error);
1115       goto out;
1116     }
1117
1118   BAD_SW_IF_INDEX_LABEL;
1119 out:
1120   REPLY_MACRO (VL_API_SW_INTERFACE_SET_RX_PLACEMENT_REPLY);
1121 }
1122
1123 static void
1124 vl_api_create_vlan_subif_t_handler (vl_api_create_vlan_subif_t * mp)
1125 {
1126   vl_api_create_vlan_subif_reply_t *rmp;
1127   vnet_main_t *vnm = vnet_get_main ();
1128   u32 sw_if_index = (u32) ~ 0;
1129   vnet_hw_interface_t *hi;
1130   int rv = 0;
1131   u32 id;
1132   vnet_sw_interface_t template;
1133   uword *p;
1134   vnet_interface_main_t *im = &vnm->interface_main;
1135   u64 sup_and_sub_key;
1136   vl_api_registration_t *reg;
1137   clib_error_t *error;
1138
1139   VALIDATE_SW_IF_INDEX (mp);
1140
1141   hi = vnet_get_sup_hw_interface (vnm, ntohl (mp->sw_if_index));
1142
1143   if (hi->bond_info == VNET_HW_INTERFACE_BOND_INFO_SLAVE)
1144     {
1145       rv = VNET_API_ERROR_BOND_SLAVE_NOT_ALLOWED;
1146       goto out;
1147     }
1148
1149   id = ntohl (mp->vlan_id);
1150   if (id == 0 || id > 4095)
1151     {
1152       rv = VNET_API_ERROR_INVALID_VLAN;
1153       goto out;
1154     }
1155
1156   sup_and_sub_key = ((u64) (hi->sw_if_index) << 32) | (u64) id;
1157
1158   p = hash_get_mem (im->sw_if_index_by_sup_and_sub, &sup_and_sub_key);
1159   if (p)
1160     {
1161       rv = VNET_API_ERROR_VLAN_ALREADY_EXISTS;
1162       goto out;
1163     }
1164
1165   clib_memset (&template, 0, sizeof (template));
1166   template.type = VNET_SW_INTERFACE_TYPE_SUB;
1167   template.flood_class = VNET_FLOOD_CLASS_NORMAL;
1168   template.sup_sw_if_index = hi->sw_if_index;
1169   template.sub.id = id;
1170   template.sub.eth.raw_flags = 0;
1171   template.sub.eth.flags.one_tag = 1;
1172   template.sub.eth.outer_vlan_id = id;
1173   template.sub.eth.flags.exact_match = 1;
1174
1175   error = vnet_create_sw_interface (vnm, &template, &sw_if_index);
1176   if (error)
1177     {
1178       clib_error_report (error);
1179       rv = VNET_API_ERROR_INVALID_REGISTRATION;
1180       goto out;
1181     }
1182
1183   u64 *kp = clib_mem_alloc (sizeof (*kp));
1184   *kp = sup_and_sub_key;
1185
1186   hash_set (hi->sub_interface_sw_if_index_by_id, id, sw_if_index);
1187   hash_set_mem (im->sw_if_index_by_sup_and_sub, kp, sw_if_index);
1188
1189   BAD_SW_IF_INDEX_LABEL;
1190
1191 out:
1192   reg = vl_api_client_index_to_registration (mp->client_index);
1193   if (!reg)
1194     return;
1195
1196   rmp = vl_msg_api_alloc (sizeof (*rmp));
1197   rmp->_vl_msg_id = htons (VL_API_CREATE_VLAN_SUBIF_REPLY);
1198   rmp->context = mp->context;
1199   rmp->retval = htonl (rv);
1200   rmp->sw_if_index = htonl (sw_if_index);
1201   vl_api_send_msg (reg, (u8 *) rmp);
1202 }
1203
1204 static void
1205 vl_api_create_subif_t_handler (vl_api_create_subif_t * mp)
1206 {
1207   vl_api_create_subif_reply_t *rmp;
1208   vnet_main_t *vnm = vnet_get_main ();
1209   u32 sw_if_index = ~0;
1210   int rv = 0;
1211   u32 sub_id;
1212   vnet_sw_interface_t *si;
1213   vnet_hw_interface_t *hi;
1214   vnet_sw_interface_t template;
1215   uword *p;
1216   vnet_interface_main_t *im = &vnm->interface_main;
1217   u64 sup_and_sub_key;
1218   clib_error_t *error;
1219
1220   VALIDATE_SW_IF_INDEX (mp);
1221
1222   si = vnet_get_sup_sw_interface (vnm, ntohl (mp->sw_if_index));
1223   hi = vnet_get_sup_hw_interface (vnm, ntohl (mp->sw_if_index));
1224
1225   if (hi->bond_info == VNET_HW_INTERFACE_BOND_INFO_SLAVE)
1226     {
1227       rv = VNET_API_ERROR_BOND_SLAVE_NOT_ALLOWED;
1228       goto out;
1229     }
1230
1231   sw_if_index = si->sw_if_index;
1232   sub_id = ntohl (mp->sub_id);
1233
1234   sup_and_sub_key = ((u64) (sw_if_index) << 32) | (u64) sub_id;
1235
1236   p = hash_get_mem (im->sw_if_index_by_sup_and_sub, &sup_and_sub_key);
1237   if (p)
1238     {
1239       if (CLIB_DEBUG > 0)
1240         clib_warning ("sup sw_if_index %d, sub id %d already exists\n",
1241                       sw_if_index, sub_id);
1242       rv = VNET_API_ERROR_SUBIF_ALREADY_EXISTS;
1243       goto out;
1244     }
1245
1246   clib_memset (&template, 0, sizeof (template));
1247   template.type = VNET_SW_INTERFACE_TYPE_SUB;
1248   template.flood_class = VNET_FLOOD_CLASS_NORMAL;
1249   template.sup_sw_if_index = sw_if_index;
1250   template.sub.id = sub_id;
1251   template.sub.eth.raw_flags = ntohl (mp->sub_if_flags);
1252   template.sub.eth.outer_vlan_id = ntohs (mp->outer_vlan_id);
1253   template.sub.eth.inner_vlan_id = ntohs (mp->inner_vlan_id);
1254
1255   error = vnet_create_sw_interface (vnm, &template, &sw_if_index);
1256   if (error)
1257     {
1258       clib_error_report (error);
1259       rv = VNET_API_ERROR_SUBIF_CREATE_FAILED;
1260       goto out;
1261     }
1262
1263   u64 *kp = clib_mem_alloc (sizeof (*kp));
1264   *kp = sup_and_sub_key;
1265
1266   hash_set (hi->sub_interface_sw_if_index_by_id, sub_id, sw_if_index);
1267   hash_set_mem (im->sw_if_index_by_sup_and_sub, kp, sw_if_index);
1268
1269   BAD_SW_IF_INDEX_LABEL;
1270
1271 out:
1272
1273   /* *INDENT-OFF* */
1274   REPLY_MACRO2(VL_API_CREATE_SUBIF_REPLY,
1275   ({
1276     rmp->sw_if_index = ntohl(sw_if_index);
1277   }));
1278   /* *INDENT-ON* */
1279 }
1280
1281 static void
1282 vl_api_delete_subif_t_handler (vl_api_delete_subif_t * mp)
1283 {
1284   vl_api_delete_subif_reply_t *rmp;
1285   int rv;
1286
1287   rv = vnet_delete_sub_interface (ntohl (mp->sw_if_index));
1288
1289   REPLY_MACRO (VL_API_DELETE_SUBIF_REPLY);
1290 }
1291
1292 static void
1293 vl_api_interface_name_renumber_t_handler (vl_api_interface_name_renumber_t *
1294                                           mp)
1295 {
1296   vl_api_interface_name_renumber_reply_t *rmp;
1297   int rv = 0;
1298
1299   VALIDATE_SW_IF_INDEX (mp);
1300
1301   rv = vnet_interface_name_renumber
1302     (ntohl (mp->sw_if_index), ntohl (mp->new_show_dev_instance));
1303
1304   BAD_SW_IF_INDEX_LABEL;
1305
1306   REPLY_MACRO (VL_API_INTERFACE_NAME_RENUMBER_REPLY);
1307 }
1308
1309 static void
1310 vl_api_create_loopback_t_handler (vl_api_create_loopback_t * mp)
1311 {
1312   vl_api_create_loopback_reply_t *rmp;
1313   u32 sw_if_index;
1314   int rv;
1315   mac_address_t mac;
1316
1317   mac_address_decode (mp->mac_address, &mac);
1318   rv = vnet_create_loopback_interface (&sw_if_index, (u8 *) & mac, 0, 0);
1319
1320   /* *INDENT-OFF* */
1321   REPLY_MACRO2(VL_API_CREATE_LOOPBACK_REPLY,
1322   ({
1323     rmp->sw_if_index = ntohl (sw_if_index);
1324   }));
1325   /* *INDENT-ON* */
1326 }
1327
1328 static void vl_api_create_loopback_instance_t_handler
1329   (vl_api_create_loopback_instance_t * mp)
1330 {
1331   vl_api_create_loopback_instance_reply_t *rmp;
1332   u32 sw_if_index;
1333   u8 is_specified = mp->is_specified;
1334   u32 user_instance = ntohl (mp->user_instance);
1335   int rv;
1336   mac_address_t mac;
1337
1338   mac_address_decode (mp->mac_address, &mac);
1339   rv = vnet_create_loopback_interface (&sw_if_index, (u8 *) & mac,
1340                                        is_specified, user_instance);
1341
1342   /* *INDENT-OFF* */
1343   REPLY_MACRO2(VL_API_CREATE_LOOPBACK_INSTANCE_REPLY,
1344   ({
1345     rmp->sw_if_index = ntohl (sw_if_index);
1346   }));
1347   /* *INDENT-ON* */
1348 }
1349
1350 static void
1351 vl_api_delete_loopback_t_handler (vl_api_delete_loopback_t * mp)
1352 {
1353   vl_api_delete_loopback_reply_t *rmp;
1354   u32 sw_if_index;
1355   int rv;
1356
1357   sw_if_index = ntohl (mp->sw_if_index);
1358   rv = vnet_delete_loopback_interface (sw_if_index);
1359
1360   REPLY_MACRO (VL_API_DELETE_LOOPBACK_REPLY);
1361 }
1362
1363 static void
1364   vl_api_collect_detailed_interface_stats_t_handler
1365   (vl_api_collect_detailed_interface_stats_t * mp)
1366 {
1367   vl_api_collect_detailed_interface_stats_reply_t *rmp;
1368   int rv = 0;
1369
1370   rv =
1371     vnet_sw_interface_stats_collect_enable_disable (ntohl (mp->sw_if_index),
1372                                                     mp->enable_disable);
1373
1374   REPLY_MACRO (VL_API_COLLECT_DETAILED_INTERFACE_STATS_REPLY);
1375 }
1376
1377 /*
1378  * vpe_api_hookup
1379  * Add vpe's API message handlers to the table.
1380  * vlib has already mapped shared memory and
1381  * added the client registration handlers.
1382  * See .../vlib-api/vlibmemory/memclnt_vlib.c:memclnt_process()
1383  */
1384 #define vl_msg_name_crc_list
1385 #include <vnet/interface.api.h>
1386 #undef vl_msg_name_crc_list
1387
1388 static void
1389 setup_message_id_table (api_main_t * am)
1390 {
1391 #define _(id,n,crc) vl_msg_api_add_msg_name_crc (am, #n "_" #crc, id);
1392   foreach_vl_msg_name_crc_interface;
1393 #undef _
1394 }
1395
1396 pub_sub_handler (interface_events, INTERFACE_EVENTS);
1397
1398 static clib_error_t *
1399 interface_api_hookup (vlib_main_t * vm)
1400 {
1401   api_main_t *am = &api_main;
1402
1403 #define _(N,n)                                                  \
1404     vl_msg_api_set_handlers(VL_API_##N, #n,                     \
1405                            vl_api_##n##_t_handler,              \
1406                            vl_noop_handler,                     \
1407                            vl_api_##n##_t_endian,               \
1408                            vl_api_##n##_t_print,                \
1409                            sizeof(vl_api_##n##_t), 1);
1410   foreach_vpe_api_msg;
1411 #undef _
1412
1413   /* Mark these APIs as mp safe */
1414   am->is_mp_safe[VL_API_SW_INTERFACE_DUMP] = 1;
1415   am->is_mp_safe[VL_API_SW_INTERFACE_DETAILS] = 1;
1416   am->is_mp_safe[VL_API_SW_INTERFACE_TAG_ADD_DEL] = 1;
1417
1418   /* Do not replay VL_API_SW_INTERFACE_DUMP messages */
1419   am->api_trace_cfg[VL_API_SW_INTERFACE_DUMP].replay_enable = 0;
1420
1421   /*
1422    * Set up the (msg_name, crc, message-id) table
1423    */
1424   setup_message_id_table (am);
1425
1426   return 0;
1427 }
1428
1429 VLIB_API_INIT_FUNCTION (interface_api_hookup);
1430
1431 /*
1432  * fd.io coding-style-patch-verification: ON
1433  *
1434  * Local Variables:
1435  * eval: (c-set-style "gnu")
1436  * End:
1437  */