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