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