MTU: Setting of MTU on software interface (instead of hardware interface)
[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 #define vl_typedefs             /* define message structures */
35 #include <vnet/vnet_all_api_h.h>
36 #undef vl_typedefs
37
38 #define vl_endianfun            /* define message structures */
39 #include <vnet/vnet_all_api_h.h>
40 #undef vl_endianfun
41
42 /* instantiate all the print functions we know about */
43 #define vl_print(handle, ...) vlib_cli_output (handle, __VA_ARGS__)
44 #define vl_printfun
45 #include <vnet/vnet_all_api_h.h>
46 #undef vl_printfun
47
48 #include <vlibapi/api_helper_macros.h>
49 vpe_api_main_t vpe_api_main;
50
51 #define foreach_vpe_api_msg                                     \
52 _(SW_INTERFACE_SET_FLAGS, sw_interface_set_flags)               \
53 _(SW_INTERFACE_SET_MTU, sw_interface_set_mtu)                   \
54 _(WANT_INTERFACE_EVENTS, want_interface_events)                 \
55 _(SW_INTERFACE_DUMP, sw_interface_dump)                         \
56 _(SW_INTERFACE_ADD_DEL_ADDRESS, sw_interface_add_del_address)   \
57 _(SW_INTERFACE_SET_RX_MODE, sw_interface_set_rx_mode)           \
58 _(SW_INTERFACE_SET_TABLE, sw_interface_set_table)               \
59 _(SW_INTERFACE_GET_TABLE, sw_interface_get_table)               \
60 _(SW_INTERFACE_SET_UNNUMBERED, sw_interface_set_unnumbered)     \
61 _(SW_INTERFACE_CLEAR_STATS, sw_interface_clear_stats)           \
62 _(SW_INTERFACE_TAG_ADD_DEL, sw_interface_tag_add_del)           \
63 _(SW_INTERFACE_SET_MAC_ADDRESS, sw_interface_set_mac_address)   \
64 _(SW_INTERFACE_GET_MAC_ADDRESS, sw_interface_get_mac_address)   \
65 _(CREATE_VLAN_SUBIF, create_vlan_subif)                         \
66 _(CREATE_SUBIF, create_subif)                                   \
67 _(DELETE_SUBIF, delete_subif)                                   \
68 _(CREATE_LOOPBACK, create_loopback)                             \
69 _(CREATE_LOOPBACK_INSTANCE, create_loopback_instance)           \
70 _(DELETE_LOOPBACK, delete_loopback)                             \
71 _(INTERFACE_NAME_RENUMBER, interface_name_renumber)             \
72 _(COLLECT_DETAILED_INTERFACE_STATS, collect_detailed_interface_stats)
73
74 static void
75 vl_api_sw_interface_set_flags_t_handler (vl_api_sw_interface_set_flags_t * mp)
76 {
77   vl_api_sw_interface_set_flags_reply_t *rmp;
78   vnet_main_t *vnm = vnet_get_main ();
79   int rv = 0;
80   clib_error_t *error;
81   u16 flags;
82
83   VALIDATE_SW_IF_INDEX (mp);
84
85   flags = mp->admin_up_down ? VNET_SW_INTERFACE_FLAG_ADMIN_UP : 0;
86
87   error = vnet_sw_interface_set_flags (vnm, ntohl (mp->sw_if_index), flags);
88   if (error)
89     {
90       rv = -1;
91       clib_error_report (error);
92     }
93
94   BAD_SW_IF_INDEX_LABEL;
95   REPLY_MACRO (VL_API_SW_INTERFACE_SET_FLAGS_REPLY);
96 }
97
98 static void
99 vl_api_sw_interface_set_mtu_t_handler (vl_api_sw_interface_set_mtu_t * mp)
100 {
101   vl_api_sw_interface_set_mtu_reply_t *rmp;
102   vnet_main_t *vnm = vnet_get_main ();
103   u32 sw_if_index = ntohl (mp->sw_if_index);
104   u16 mtu = ntohs (mp->mtu);
105   int rv = 0;
106
107   VALIDATE_SW_IF_INDEX (mp);
108
109   rv = vnet_sw_interface_set_mtu (vnm, sw_if_index, mtu);
110
111   BAD_SW_IF_INDEX_LABEL;
112   REPLY_MACRO (VL_API_SW_INTERFACE_SET_MTU_REPLY);
113 }
114
115 static void
116 send_sw_interface_details (vpe_api_main_t * am,
117                            vl_api_registration_t * rp,
118                            vnet_sw_interface_t * swif,
119                            u8 * interface_name, u32 context)
120 {
121   vnet_hw_interface_t *hi =
122     vnet_get_sup_hw_interface (am->vnet_main, swif->sw_if_index);
123
124   vl_api_sw_interface_details_t *mp = vl_msg_api_alloc (sizeof (*mp));
125   memset (mp, 0, sizeof (*mp));
126   mp->_vl_msg_id = ntohs (VL_API_SW_INTERFACE_DETAILS);
127   mp->sw_if_index = ntohl (swif->sw_if_index);
128   mp->sup_sw_if_index = ntohl (swif->sup_sw_if_index);
129   mp->admin_up_down = (swif->flags & VNET_SW_INTERFACE_FLAG_ADMIN_UP) ? 1 : 0;
130   mp->link_up_down = (hi->flags & VNET_HW_INTERFACE_FLAG_LINK_UP) ? 1 : 0;
131   mp->link_duplex = ((hi->flags & VNET_HW_INTERFACE_FLAG_DUPLEX_MASK) >>
132                      VNET_HW_INTERFACE_FLAG_DUPLEX_SHIFT);
133   mp->link_speed = ((hi->flags & VNET_HW_INTERFACE_FLAG_SPEED_MASK) >>
134                     VNET_HW_INTERFACE_FLAG_SPEED_SHIFT);
135   mp->mtu = ntohs (swif->max_l3_packet_bytes[VLIB_TX]);
136   mp->context = context;
137
138   strncpy ((char *) mp->interface_name,
139            (char *) interface_name, ARRAY_LEN (mp->interface_name) - 1);
140
141   /* Send the L2 address for ethernet physical intfcs */
142   if (swif->sup_sw_if_index == swif->sw_if_index
143       && hi->hw_class_index == ethernet_hw_interface_class.index)
144     {
145       ethernet_main_t *em = ethernet_get_main (am->vlib_main);
146       ethernet_interface_t *ei;
147
148       ei = pool_elt_at_index (em->interfaces, hi->hw_instance);
149       ASSERT (sizeof (mp->l2_address) >= sizeof (ei->address));
150       clib_memcpy (mp->l2_address, ei->address, sizeof (ei->address));
151       mp->l2_address_length = ntohl (sizeof (ei->address));
152     }
153   else if (swif->sup_sw_if_index != swif->sw_if_index)
154     {
155       vnet_sub_interface_t *sub = &swif->sub;
156       mp->sub_id = ntohl (sub->id);
157       mp->sub_dot1ad = sub->eth.flags.dot1ad;
158       mp->sub_number_of_tags =
159         sub->eth.flags.one_tag + sub->eth.flags.two_tags * 2;
160       mp->sub_outer_vlan_id = ntohs (sub->eth.outer_vlan_id);
161       mp->sub_inner_vlan_id = ntohs (sub->eth.inner_vlan_id);
162       mp->sub_exact_match = sub->eth.flags.exact_match;
163       mp->sub_default = sub->eth.flags.default_sub;
164       mp->sub_outer_vlan_id_any = sub->eth.flags.outer_vlan_id_any;
165       mp->sub_inner_vlan_id_any = sub->eth.flags.inner_vlan_id_any;
166
167       /* vlan tag rewrite data */
168       u32 vtr_op = L2_VTR_DISABLED;
169       u32 vtr_push_dot1q = 0, vtr_tag1 = 0, vtr_tag2 = 0;
170
171       if (l2vtr_get (am->vlib_main, am->vnet_main, swif->sw_if_index,
172                      &vtr_op, &vtr_push_dot1q, &vtr_tag1, &vtr_tag2) != 0)
173         {
174           // error - default to disabled
175           mp->vtr_op = ntohl (L2_VTR_DISABLED);
176           clib_warning ("cannot get vlan tag rewrite for sw_if_index %d",
177                         swif->sw_if_index);
178         }
179       else
180         {
181           mp->vtr_op = ntohl (vtr_op);
182           mp->vtr_push_dot1q = ntohl (vtr_push_dot1q);
183           mp->vtr_tag1 = ntohl (vtr_tag1);
184           mp->vtr_tag2 = ntohl (vtr_tag2);
185         }
186     }
187
188   /* pbb tag rewrite data */
189   ethernet_header_t eth_hdr;
190   u32 vtr_op = L2_VTR_DISABLED;
191   u16 outer_tag = 0;
192   u16 b_vlanid = 0;
193   u32 i_sid = 0;
194   memset (&eth_hdr, 0, sizeof (eth_hdr));
195
196   if (!l2pbb_get (am->vlib_main, am->vnet_main, swif->sw_if_index,
197                   &vtr_op, &outer_tag, &eth_hdr, &b_vlanid, &i_sid))
198     {
199       mp->sub_dot1ah = 1;
200       clib_memcpy (mp->b_dmac, eth_hdr.dst_address,
201                    sizeof (eth_hdr.dst_address));
202       clib_memcpy (mp->b_smac, eth_hdr.src_address,
203                    sizeof (eth_hdr.src_address));
204       mp->b_vlanid = b_vlanid;
205       mp->i_sid = i_sid;
206     }
207
208   u8 *tag = vnet_get_sw_interface_tag (vnet_get_main (), swif->sw_if_index);
209   if (tag)
210     strncpy ((char *) mp->tag, (char *) tag, ARRAY_LEN (mp->tag) - 1);
211
212   vl_api_send_msg (rp, (u8 *) mp);
213 }
214
215 static void
216 vl_api_sw_interface_dump_t_handler (vl_api_sw_interface_dump_t * mp)
217 {
218   vpe_api_main_t *am = &vpe_api_main;
219   vnet_sw_interface_t *swif;
220   vnet_interface_main_t *im = &am->vnet_main->interface_main;
221   vl_api_registration_t *rp;
222
223   rp = vl_api_client_index_to_registration (mp->client_index);
224
225   if (rp == 0)
226     {
227       clib_warning ("Client %d AWOL", mp->client_index);
228       return;
229     }
230
231   u8 *filter = 0, *name = 0;
232   if (mp->name_filter_valid)
233     {
234       mp->name_filter[ARRAY_LEN (mp->name_filter) - 1] = 0;
235       filter = format (0, "%s%c", mp->name_filter, 0);
236     }
237
238   char *strcasestr (char *, char *);    /* lnx hdr file botch */
239   /* *INDENT-OFF* */
240   pool_foreach (swif, im->sw_interfaces,
241   ({
242     if (!vnet_swif_is_api_visible (swif))
243         continue;
244     vec_reset_length(name);
245     name = format (name, "%U%c", format_vnet_sw_interface_name, am->vnet_main,
246                    swif, 0);
247
248     if (filter && !strcasestr((char *) name, (char *) filter))
249         continue;
250
251     send_sw_interface_details (am, rp, swif, name, mp->context);
252   }));
253   /* *INDENT-ON* */
254
255   vec_free (name);
256   vec_free (filter);
257 }
258
259 static void
260   vl_api_sw_interface_add_del_address_t_handler
261   (vl_api_sw_interface_add_del_address_t * mp)
262 {
263   vlib_main_t *vm = vlib_get_main ();
264   vnet_main_t *vnm = vnet_get_main ();
265   vl_api_sw_interface_add_del_address_reply_t *rmp;
266   int rv = 0;
267   u32 is_del;
268   clib_error_t *error = 0;
269
270   VALIDATE_SW_IF_INDEX (mp);
271
272   is_del = mp->is_add == 0;
273   vnm->api_errno = 0;
274
275   if (mp->del_all)
276     ip_del_all_interface_addresses (vm, ntohl (mp->sw_if_index));
277   else if (mp->is_ipv6)
278     error = ip6_add_del_interface_address (vm, ntohl (mp->sw_if_index),
279                                            (void *) mp->address,
280                                            mp->address_length, is_del);
281   else
282     error = ip4_add_del_interface_address (vm, ntohl (mp->sw_if_index),
283                                            (void *) mp->address,
284                                            mp->address_length, is_del);
285
286   if (error)
287     {
288       rv = vnm->api_errno;
289       clib_error_report (error);
290       goto done;
291     }
292
293   BAD_SW_IF_INDEX_LABEL;
294
295 done:
296   REPLY_MACRO (VL_API_SW_INTERFACE_ADD_DEL_ADDRESS_REPLY);
297 }
298
299 void stats_dslock_with_hint (int hint, int tag) __attribute__ ((weak));
300 void
301 stats_dslock_with_hint (int hint, int tag)
302 {
303 }
304
305 void stats_dsunlock (void) __attribute__ ((weak));
306 void
307 stats_dsunlock (void)
308 {
309 }
310
311 static void
312 vl_api_sw_interface_set_table_t_handler (vl_api_sw_interface_set_table_t * mp)
313 {
314   vl_api_sw_interface_set_table_reply_t *rmp;
315   u32 sw_if_index = ntohl (mp->sw_if_index);
316   u32 table_id = ntohl (mp->vrf_id);
317   int rv = 0;
318
319   VALIDATE_SW_IF_INDEX (mp);
320
321   stats_dslock_with_hint (1 /* release hint */ , 4 /* tag */ );
322
323   if (mp->is_ipv6)
324     rv = ip_table_bind (FIB_PROTOCOL_IP6, sw_if_index, table_id, 1);
325   else
326     rv = ip_table_bind (FIB_PROTOCOL_IP4, sw_if_index, table_id, 1);
327
328   stats_dsunlock ();
329
330   BAD_SW_IF_INDEX_LABEL;
331
332   REPLY_MACRO (VL_API_SW_INTERFACE_SET_TABLE_REPLY);
333 }
334
335 int
336 ip_table_bind (fib_protocol_t fproto,
337                u32 sw_if_index, u32 table_id, u8 is_api)
338 {
339   CLIB_UNUSED (ip_interface_address_t * ia);
340   u32 fib_index, mfib_index;
341   fib_source_t src;
342   mfib_source_t msrc;
343
344   if (is_api)
345     {
346       src = FIB_SOURCE_API;
347       msrc = MFIB_SOURCE_API;
348     }
349   else
350     {
351       src = FIB_SOURCE_CLI;
352       msrc = MFIB_SOURCE_CLI;
353     }
354
355   /*
356    * This is temporary whilst I do the song and dance with the CSIT version
357    */
358   if (0 != table_id)
359     {
360       fib_index = fib_table_find_or_create_and_lock (fproto, table_id, src);
361       mfib_index =
362         mfib_table_find_or_create_and_lock (fproto, table_id, msrc);
363     }
364   else
365     {
366       fib_index = 0;
367       mfib_index = 0;
368     }
369
370   /*
371    * This if table does not exist = error is what we want in the end.
372    */
373   /* fib_index = fib_table_find (fproto, table_id); */
374   /* mfib_index = mfib_table_find (fproto, table_id); */
375
376   /* if (~0 == fib_index || ~0 == mfib_index) */
377   /*   { */
378   /*     return (VNET_API_ERROR_NO_SUCH_FIB); */
379   /*   } */
380
381   if (FIB_PROTOCOL_IP6 == fproto)
382     {
383       /*
384        * If the interface already has in IP address, then a change int
385        * VRF is not allowed. The IP address applied must first be removed.
386        * We do not do that automatically here, since VPP has no knowledge
387        * of whether thoses subnets are valid in the destination VRF.
388        */
389       /* *INDENT-OFF* */
390       foreach_ip_interface_address (&ip6_main.lookup_main,
391                                     ia, sw_if_index,
392                                     1 /* honor unnumbered */ ,
393       ({
394         return (VNET_API_ERROR_ADDRESS_FOUND_FOR_INTERFACE);
395       }));
396       /* *INDENT-ON* */
397
398       vec_validate (ip6_main.fib_index_by_sw_if_index, sw_if_index);
399       vec_validate (ip6_main.mfib_index_by_sw_if_index, sw_if_index);
400
401       /*
402        * tell those that are interested that the binding is changing.
403        */
404       ip6_table_bind_callback_t *cb;
405       vec_foreach (cb, ip6_main.table_bind_callbacks)
406         cb->function (&ip6_main, cb->function_opaque,
407                       sw_if_index,
408                       fib_index,
409                       ip6_main.fib_index_by_sw_if_index[sw_if_index]);
410
411       if (0 == table_id)
412         {
413           /* reset back to default */
414           if (0 != ip6_main.fib_index_by_sw_if_index[sw_if_index])
415             fib_table_unlock (ip6_main.fib_index_by_sw_if_index[sw_if_index],
416                               FIB_PROTOCOL_IP6, src);
417           if (0 != ip6_main.mfib_index_by_sw_if_index[sw_if_index])
418             mfib_table_unlock (ip6_main.mfib_index_by_sw_if_index
419                                [sw_if_index], FIB_PROTOCOL_IP6, msrc);
420
421         }
422       else
423         {
424           /* we need to lock the table now it's inuse */
425           fib_table_lock (fib_index, FIB_PROTOCOL_IP6, src);
426           mfib_table_lock (mfib_index, FIB_PROTOCOL_IP6, msrc);
427         }
428
429       ip6_main.fib_index_by_sw_if_index[sw_if_index] = fib_index;
430       ip6_main.mfib_index_by_sw_if_index[sw_if_index] = mfib_index;
431     }
432   else
433     {
434       /*
435        * If the interface already has in IP address, then a change int
436        * VRF is not allowed. The IP address applied must first be removed.
437        * We do not do that automatically here, since VPP has no knowledge
438        * of whether thoses subnets are valid in the destination VRF.
439        */
440       /* *INDENT-OFF* */
441       foreach_ip_interface_address (&ip4_main.lookup_main,
442                                     ia, sw_if_index,
443                                     1 /* honor unnumbered */ ,
444       ({
445         return (VNET_API_ERROR_ADDRESS_FOUND_FOR_INTERFACE);
446       }));
447       /* *INDENT-ON* */
448
449       vec_validate (ip4_main.fib_index_by_sw_if_index, sw_if_index);
450       vec_validate (ip4_main.mfib_index_by_sw_if_index, sw_if_index);
451
452       /*
453        * tell those that are interested that the binding is changing.
454        */
455       ip4_table_bind_callback_t *cb;
456       vec_foreach (cb, ip4_main.table_bind_callbacks)
457         cb->function (&ip4_main, cb->function_opaque,
458                       sw_if_index,
459                       fib_index,
460                       ip4_main.fib_index_by_sw_if_index[sw_if_index]);
461
462       if (0 == table_id)
463         {
464           /* reset back to default */
465           if (0 != ip4_main.fib_index_by_sw_if_index[sw_if_index])
466             fib_table_unlock (ip4_main.fib_index_by_sw_if_index[sw_if_index],
467                               FIB_PROTOCOL_IP4, src);
468           if (0 != ip4_main.mfib_index_by_sw_if_index[sw_if_index])
469             mfib_table_unlock (ip4_main.mfib_index_by_sw_if_index
470                                [sw_if_index], FIB_PROTOCOL_IP4, msrc);
471
472         }
473       else
474         {
475           /* we need to lock the table now it's inuse */
476           fib_index = fib_table_find_or_create_and_lock (FIB_PROTOCOL_IP4,
477                                                          table_id, src);
478
479           mfib_index = mfib_table_find_or_create_and_lock (FIB_PROTOCOL_IP4,
480                                                            table_id, msrc);
481         }
482
483       ip4_main.fib_index_by_sw_if_index[sw_if_index] = fib_index;
484       ip4_main.mfib_index_by_sw_if_index[sw_if_index] = mfib_index;
485     }
486
487   /*
488    * Temporary. undo the locks from the find and create at the staart
489    */
490   if (0 != table_id)
491     {
492       fib_table_unlock (fib_index, fproto, src);
493       mfib_table_unlock (mfib_index, fproto, msrc);
494     }
495
496   return (0);
497 }
498
499 static void
500 send_sw_interface_get_table_reply (vl_api_registration_t * reg,
501                                    u32 context, int retval, u32 vrf_id)
502 {
503   vl_api_sw_interface_get_table_reply_t *mp;
504
505   mp = vl_msg_api_alloc (sizeof (*mp));
506   memset (mp, 0, sizeof (*mp));
507   mp->_vl_msg_id = ntohs (VL_API_SW_INTERFACE_GET_TABLE_REPLY);
508   mp->context = context;
509   mp->retval = htonl (retval);
510   mp->vrf_id = htonl (vrf_id);
511
512   vl_api_send_msg (reg, (u8 *) mp);
513 }
514
515 static void
516 vl_api_sw_interface_get_table_t_handler (vl_api_sw_interface_get_table_t * mp)
517 {
518   vl_api_registration_t *reg;
519   fib_table_t *fib_table = 0;
520   u32 sw_if_index = ~0;
521   u32 fib_index = ~0;
522   u32 table_id = ~0;
523   fib_protocol_t fib_proto = FIB_PROTOCOL_IP4;
524   int rv = 0;
525
526   reg = vl_api_client_index_to_registration (mp->client_index);
527   if (!reg)
528     return;
529
530   VALIDATE_SW_IF_INDEX (mp);
531
532   sw_if_index = ntohl (mp->sw_if_index);
533
534   if (mp->is_ipv6)
535     fib_proto = FIB_PROTOCOL_IP6;
536
537   fib_index = fib_table_get_index_for_sw_if_index (fib_proto, sw_if_index);
538   if (fib_index != ~0)
539     {
540       fib_table = fib_table_get (fib_index, fib_proto);
541       table_id = fib_table->ft_table_id;
542     }
543
544   BAD_SW_IF_INDEX_LABEL;
545
546   send_sw_interface_get_table_reply (reg, mp->context, rv, table_id);
547 }
548
549 static void vl_api_sw_interface_set_unnumbered_t_handler
550   (vl_api_sw_interface_set_unnumbered_t * mp)
551 {
552   vl_api_sw_interface_set_unnumbered_reply_t *rmp;
553   int rv = 0;
554   vnet_main_t *vnm = vnet_get_main ();
555   u32 sw_if_index = ntohl (mp->sw_if_index);
556   u32 unnumbered_sw_if_index = ntohl (mp->unnumbered_sw_if_index);
557
558   /*
559    * The API message field names are backwards from
560    * the underlying data structure names.
561    * It's not worth changing them now.
562    */
563   if (!vnet_sw_interface_is_api_valid (vnm, unnumbered_sw_if_index))
564     {
565       rv = VNET_API_ERROR_INVALID_SW_IF_INDEX;
566       goto done;
567     }
568
569   /* Only check the "use loop0" field when setting the binding */
570   if (mp->is_add && !vnet_sw_interface_is_api_valid (vnm, sw_if_index))
571     {
572       rv = VNET_API_ERROR_INVALID_SW_IF_INDEX_2;
573       goto done;
574     }
575
576   vnet_sw_interface_update_unnumbered (unnumbered_sw_if_index,
577                                        sw_if_index, mp->is_add);
578 done:
579   REPLY_MACRO (VL_API_SW_INTERFACE_SET_UNNUMBERED_REPLY);
580 }
581
582 static void
583 vl_api_sw_interface_clear_stats_t_handler (vl_api_sw_interface_clear_stats_t *
584                                            mp)
585 {
586   vl_api_sw_interface_clear_stats_reply_t *rmp;
587
588   vnet_main_t *vnm = vnet_get_main ();
589   vnet_interface_main_t *im = &vnm->interface_main;
590   vlib_simple_counter_main_t *sm;
591   vlib_combined_counter_main_t *cm;
592   static vnet_main_t **my_vnet_mains;
593   int i, j, n_counters;
594   int rv = 0;
595
596   if (mp->sw_if_index != ~0)
597     VALIDATE_SW_IF_INDEX (mp);
598
599   vec_reset_length (my_vnet_mains);
600
601   for (i = 0; i < vec_len (vnet_mains); i++)
602     {
603       if (vnet_mains[i])
604         vec_add1 (my_vnet_mains, vnet_mains[i]);
605     }
606
607   if (vec_len (vnet_mains) == 0)
608     vec_add1 (my_vnet_mains, vnm);
609
610   n_counters = vec_len (im->combined_sw_if_counters);
611
612   for (j = 0; j < n_counters; j++)
613     {
614       for (i = 0; i < vec_len (my_vnet_mains); i++)
615         {
616           im = &my_vnet_mains[i]->interface_main;
617           cm = im->combined_sw_if_counters + j;
618           if (mp->sw_if_index == (u32) ~ 0)
619             vlib_clear_combined_counters (cm);
620           else
621             vlib_zero_combined_counter (cm, ntohl (mp->sw_if_index));
622         }
623     }
624
625   n_counters = vec_len (im->sw_if_counters);
626
627   for (j = 0; j < n_counters; j++)
628     {
629       for (i = 0; i < vec_len (my_vnet_mains); i++)
630         {
631           im = &my_vnet_mains[i]->interface_main;
632           sm = im->sw_if_counters + j;
633           if (mp->sw_if_index == (u32) ~ 0)
634             vlib_clear_simple_counters (sm);
635           else
636             vlib_zero_simple_counter (sm, ntohl (mp->sw_if_index));
637         }
638     }
639
640   BAD_SW_IF_INDEX_LABEL;
641
642   REPLY_MACRO (VL_API_SW_INTERFACE_CLEAR_STATS_REPLY);
643 }
644
645 #define API_LINK_STATE_EVENT 1
646 #define API_ADMIN_UP_DOWN_EVENT 2
647
648 static int
649 event_data_cmp (void *a1, void *a2)
650 {
651   uword *e1 = a1;
652   uword *e2 = a2;
653
654   return (word) e1[0] - (word) e2[0];
655 }
656
657 static void
658 send_sw_interface_event (vpe_api_main_t * am,
659                          vpe_client_registration_t * reg,
660                          vl_api_registration_t * vl_reg,
661                          vnet_sw_interface_t * swif)
662 {
663   vl_api_sw_interface_event_t *mp;
664   vnet_main_t *vnm = am->vnet_main;
665
666   vnet_hw_interface_t *hi = vnet_get_sup_hw_interface (vnm,
667                                                        swif->sw_if_index);
668   mp = vl_msg_api_alloc (sizeof (*mp));
669   memset (mp, 0, sizeof (*mp));
670   mp->_vl_msg_id = ntohs (VL_API_SW_INTERFACE_EVENT);
671   mp->sw_if_index = ntohl (swif->sw_if_index);
672   mp->client_index = reg->client_index;
673   mp->pid = reg->client_pid;
674
675   mp->admin_up_down = (swif->flags & VNET_SW_INTERFACE_FLAG_ADMIN_UP) ? 1 : 0;
676   mp->link_up_down = (hi->flags & VNET_HW_INTERFACE_FLAG_LINK_UP) ? 1 : 0;
677   vl_api_send_msg (vl_reg, (u8 *) mp);
678 }
679
680 static uword
681 link_state_process (vlib_main_t * vm,
682                     vlib_node_runtime_t * rt, vlib_frame_t * f)
683 {
684   vpe_api_main_t *vam = &vpe_api_main;
685   vnet_main_t *vnm = vam->vnet_main;
686   vnet_sw_interface_t *swif;
687   uword *event_data = 0;
688   vpe_client_registration_t *reg;
689   int i;
690   u32 prev_sw_if_index;
691   vl_api_registration_t *vl_reg;
692
693   vam->link_state_process_up = 1;
694
695   while (1)
696     {
697       vlib_process_wait_for_event (vm);
698
699       /* Unified list of changed link or admin state sw_if_indices */
700       vlib_process_get_events_with_type
701         (vm, &event_data, API_LINK_STATE_EVENT);
702       vlib_process_get_events_with_type
703         (vm, &event_data, API_ADMIN_UP_DOWN_EVENT);
704
705       /* Sort, so we can eliminate duplicates */
706       vec_sort_with_function (event_data, event_data_cmp);
707
708       prev_sw_if_index = ~0;
709
710       for (i = 0; i < vec_len (event_data); i++)
711         {
712           /* Only one message per swif */
713           if (prev_sw_if_index == event_data[i])
714             continue;
715           prev_sw_if_index = event_data[i];
716
717           /* *INDENT-OFF* */
718           pool_foreach(reg, vam->interface_events_registrations,
719           ({
720             vl_reg = vl_api_client_index_to_registration (reg->client_index);
721             if (vl_reg)
722               {
723                 /* sw_interface may be deleted already */
724                 if (!pool_is_free_index (vnm->interface_main.sw_interfaces,
725                                          event_data[i]))
726                   {
727                     swif = vnet_get_sw_interface (vnm, event_data[i]);
728                     send_sw_interface_event (vam, reg, vl_reg, swif);
729                   }
730               }
731           }));
732           /* *INDENT-ON* */
733         }
734       vec_reset_length (event_data);
735     }
736
737   return 0;
738 }
739
740 static clib_error_t *link_up_down_function (vnet_main_t * vm, u32 hw_if_index,
741                                             u32 flags);
742 static clib_error_t *admin_up_down_function (vnet_main_t * vm,
743                                              u32 hw_if_index, u32 flags);
744
745 /* *INDENT-OFF* */
746 VLIB_REGISTER_NODE (link_state_process_node,static) = {
747   .function = link_state_process,
748   .type = VLIB_NODE_TYPE_PROCESS,
749   .name = "vpe-link-state-process",
750 };
751 /* *INDENT-ON* */
752
753 VNET_SW_INTERFACE_ADMIN_UP_DOWN_FUNCTION (admin_up_down_function);
754 VNET_HW_INTERFACE_LINK_UP_DOWN_FUNCTION (link_up_down_function);
755
756 static clib_error_t *
757 link_up_down_function (vnet_main_t * vm, u32 hw_if_index, u32 flags)
758 {
759   vpe_api_main_t *vam = &vpe_api_main;
760   vnet_hw_interface_t *hi = vnet_get_hw_interface (vm, hw_if_index);
761
762   if (vam->link_state_process_up)
763     vlib_process_signal_event (vam->vlib_main,
764                                link_state_process_node.index,
765                                API_LINK_STATE_EVENT, hi->sw_if_index);
766   return 0;
767 }
768
769 static clib_error_t *
770 admin_up_down_function (vnet_main_t * vm, u32 sw_if_index, u32 flags)
771 {
772   vpe_api_main_t *vam = &vpe_api_main;
773
774   /*
775    * Note: it's perfectly fair to set a subif admin up / admin down.
776    * Note the subtle distinction between this routine and the previous
777    * routine.
778    */
779   if (vam->link_state_process_up)
780     vlib_process_signal_event (vam->vlib_main,
781                                link_state_process_node.index,
782                                API_ADMIN_UP_DOWN_EVENT, sw_if_index);
783   return 0;
784 }
785
786 static void vl_api_sw_interface_tag_add_del_t_handler
787   (vl_api_sw_interface_tag_add_del_t * mp)
788 {
789   vnet_main_t *vnm = vnet_get_main ();
790   vl_api_sw_interface_tag_add_del_reply_t *rmp;
791   int rv = 0;
792   u8 *tag;
793   u32 sw_if_index = ntohl (mp->sw_if_index);
794
795   VALIDATE_SW_IF_INDEX (mp);
796
797   if (mp->is_add)
798     {
799       if (mp->tag[0] == 0)
800         {
801           rv = VNET_API_ERROR_INVALID_VALUE;
802           goto out;
803         }
804
805       mp->tag[ARRAY_LEN (mp->tag) - 1] = 0;
806       tag = format (0, "%s%c", mp->tag, 0);
807       vnet_set_sw_interface_tag (vnm, tag, sw_if_index);
808     }
809   else
810     vnet_clear_sw_interface_tag (vnm, sw_if_index);
811
812   BAD_SW_IF_INDEX_LABEL;
813 out:
814   REPLY_MACRO (VL_API_SW_INTERFACE_TAG_ADD_DEL_REPLY);
815 }
816
817 static void vl_api_sw_interface_set_mac_address_t_handler
818   (vl_api_sw_interface_set_mac_address_t * mp)
819 {
820   vl_api_sw_interface_set_mac_address_reply_t *rmp;
821   vnet_main_t *vnm = vnet_get_main ();
822   u32 sw_if_index = ntohl (mp->sw_if_index);
823   vnet_sw_interface_t *si;
824   clib_error_t *error;
825   int rv = 0;
826
827   VALIDATE_SW_IF_INDEX (mp);
828
829   si = vnet_get_sw_interface (vnm, sw_if_index);
830   error = vnet_hw_interface_change_mac_address (vnm, si->hw_if_index,
831                                                 mp->mac_address);
832   if (error)
833     {
834       rv = VNET_API_ERROR_UNIMPLEMENTED;
835       clib_error_report (error);
836       goto out;
837     }
838
839   BAD_SW_IF_INDEX_LABEL;
840 out:
841   REPLY_MACRO (VL_API_SW_INTERFACE_SET_MAC_ADDRESS_REPLY);
842 }
843
844 static void vl_api_sw_interface_get_mac_address_t_handler
845   (vl_api_sw_interface_get_mac_address_t * mp)
846 {
847   vl_api_sw_interface_get_mac_address_reply_t *rmp;
848   vl_api_registration_t *reg;
849   vnet_main_t *vnm = vnet_get_main ();
850   u32 sw_if_index = ntohl (mp->sw_if_index);
851   vnet_sw_interface_t *si;
852   ethernet_interface_t *eth_if = 0;
853   int rv = 0;
854
855   VALIDATE_SW_IF_INDEX (mp);
856
857   si = vnet_get_sup_sw_interface (vnm, sw_if_index);
858   if (si->type == VNET_SW_INTERFACE_TYPE_HARDWARE)
859     eth_if = ethernet_get_interface (&ethernet_main, si->hw_if_index);
860
861   BAD_SW_IF_INDEX_LABEL;
862
863   reg = vl_api_client_index_to_registration (mp->client_index);
864   if (!reg)
865     return;
866   rmp = vl_msg_api_alloc (sizeof (*rmp));
867   rmp->_vl_msg_id = htons (VL_API_SW_INTERFACE_GET_MAC_ADDRESS_REPLY);
868   rmp->context = mp->context;
869   rmp->retval = htonl (rv);
870   if (!rv && eth_if)
871     memcpy (rmp->mac_address, eth_if->address, 6);
872   vl_api_send_msg (reg, (u8 *) rmp);
873 }
874
875 static void vl_api_sw_interface_set_rx_mode_t_handler
876   (vl_api_sw_interface_set_rx_mode_t * mp)
877 {
878   vl_api_sw_interface_set_rx_mode_reply_t *rmp;
879   vnet_main_t *vnm = vnet_get_main ();
880   u32 sw_if_index = ntohl (mp->sw_if_index);
881   vnet_sw_interface_t *si;
882   clib_error_t *error;
883   int rv = 0;
884
885   VALIDATE_SW_IF_INDEX (mp);
886
887   si = vnet_get_sw_interface (vnm, sw_if_index);
888   error = set_hw_interface_change_rx_mode (vnm, si->hw_if_index,
889                                            mp->queue_id_valid,
890                                            ntohl (mp->queue_id), mp->mode);
891   if (error)
892     {
893       rv = VNET_API_ERROR_UNIMPLEMENTED;
894       clib_error_report (error);
895       goto out;
896     }
897
898   BAD_SW_IF_INDEX_LABEL;
899 out:
900   REPLY_MACRO (VL_API_SW_INTERFACE_SET_RX_MODE_REPLY);
901 }
902
903 static void
904 vl_api_create_vlan_subif_t_handler (vl_api_create_vlan_subif_t * mp)
905 {
906   vl_api_create_vlan_subif_reply_t *rmp;
907   vnet_main_t *vnm = vnet_get_main ();
908   u32 sw_if_index = (u32) ~ 0;
909   vnet_hw_interface_t *hi;
910   int rv = 0;
911   u32 id;
912   vnet_sw_interface_t template;
913   uword *p;
914   vnet_interface_main_t *im = &vnm->interface_main;
915   u64 sup_and_sub_key;
916   vl_api_registration_t *reg;
917   clib_error_t *error;
918
919   VALIDATE_SW_IF_INDEX (mp);
920
921   hi = vnet_get_sup_hw_interface (vnm, ntohl (mp->sw_if_index));
922
923   if (hi->bond_info == VNET_HW_INTERFACE_BOND_INFO_SLAVE)
924     {
925       rv = VNET_API_ERROR_BOND_SLAVE_NOT_ALLOWED;
926       goto out;
927     }
928
929   id = ntohl (mp->vlan_id);
930   if (id == 0 || id > 4095)
931     {
932       rv = VNET_API_ERROR_INVALID_VLAN;
933       goto out;
934     }
935
936   sup_and_sub_key = ((u64) (hi->sw_if_index) << 32) | (u64) id;
937
938   p = hash_get_mem (im->sw_if_index_by_sup_and_sub, &sup_and_sub_key);
939   if (p)
940     {
941       rv = VNET_API_ERROR_VLAN_ALREADY_EXISTS;
942       goto out;
943     }
944
945   memset (&template, 0, sizeof (template));
946   template.type = VNET_SW_INTERFACE_TYPE_SUB;
947   template.sup_sw_if_index = hi->sw_if_index;
948   template.sub.id = id;
949   template.sub.eth.raw_flags = 0;
950   template.sub.eth.flags.one_tag = 1;
951   template.sub.eth.outer_vlan_id = id;
952   template.sub.eth.flags.exact_match = 1;
953
954   error = vnet_create_sw_interface (vnm, &template, &sw_if_index);
955   if (error)
956     {
957       clib_error_report (error);
958       rv = VNET_API_ERROR_INVALID_REGISTRATION;
959       goto out;
960     }
961
962   u64 *kp = clib_mem_alloc (sizeof (*kp));
963   *kp = sup_and_sub_key;
964
965   hash_set (hi->sub_interface_sw_if_index_by_id, id, sw_if_index);
966   hash_set_mem (im->sw_if_index_by_sup_and_sub, kp, sw_if_index);
967
968   BAD_SW_IF_INDEX_LABEL;
969
970 out:
971   reg = vl_api_client_index_to_registration (mp->client_index);
972   if (!reg)
973     return;
974
975   rmp = vl_msg_api_alloc (sizeof (*rmp));
976   rmp->_vl_msg_id = htons (VL_API_CREATE_VLAN_SUBIF_REPLY);
977   rmp->context = mp->context;
978   rmp->retval = htonl (rv);
979   rmp->sw_if_index = htonl (sw_if_index);
980   vl_api_send_msg (reg, (u8 *) rmp);
981 }
982
983 static void
984 vl_api_create_subif_t_handler (vl_api_create_subif_t * mp)
985 {
986   vl_api_create_subif_reply_t *rmp;
987   vnet_main_t *vnm = vnet_get_main ();
988   u32 sw_if_index = ~0;
989   int rv = 0;
990   u32 sub_id;
991   vnet_sw_interface_t *si;
992   vnet_hw_interface_t *hi;
993   vnet_sw_interface_t template;
994   uword *p;
995   vnet_interface_main_t *im = &vnm->interface_main;
996   u64 sup_and_sub_key;
997   clib_error_t *error;
998
999   VALIDATE_SW_IF_INDEX (mp);
1000
1001   si = vnet_get_sup_sw_interface (vnm, ntohl (mp->sw_if_index));
1002   hi = vnet_get_sup_hw_interface (vnm, ntohl (mp->sw_if_index));
1003
1004   if (hi->bond_info == VNET_HW_INTERFACE_BOND_INFO_SLAVE)
1005     {
1006       rv = VNET_API_ERROR_BOND_SLAVE_NOT_ALLOWED;
1007       goto out;
1008     }
1009
1010   sw_if_index = si->sw_if_index;
1011   sub_id = ntohl (mp->sub_id);
1012
1013   sup_and_sub_key = ((u64) (sw_if_index) << 32) | (u64) sub_id;
1014
1015   p = hash_get_mem (im->sw_if_index_by_sup_and_sub, &sup_and_sub_key);
1016   if (p)
1017     {
1018       if (CLIB_DEBUG > 0)
1019         clib_warning ("sup sw_if_index %d, sub id %d already exists\n",
1020                       sw_if_index, sub_id);
1021       rv = VNET_API_ERROR_SUBIF_ALREADY_EXISTS;
1022       goto out;
1023     }
1024
1025   memset (&template, 0, sizeof (template));
1026   template.type = VNET_SW_INTERFACE_TYPE_SUB;
1027   template.sup_sw_if_index = sw_if_index;
1028   template.sub.id = sub_id;
1029   template.sub.eth.flags.no_tags = mp->no_tags;
1030   template.sub.eth.flags.one_tag = mp->one_tag;
1031   template.sub.eth.flags.two_tags = mp->two_tags;
1032   template.sub.eth.flags.dot1ad = mp->dot1ad;
1033   template.sub.eth.flags.exact_match = mp->exact_match;
1034   template.sub.eth.flags.default_sub = mp->default_sub;
1035   template.sub.eth.flags.outer_vlan_id_any = mp->outer_vlan_id_any;
1036   template.sub.eth.flags.inner_vlan_id_any = mp->inner_vlan_id_any;
1037   template.sub.eth.outer_vlan_id = ntohs (mp->outer_vlan_id);
1038   template.sub.eth.inner_vlan_id = ntohs (mp->inner_vlan_id);
1039
1040   error = vnet_create_sw_interface (vnm, &template, &sw_if_index);
1041   if (error)
1042     {
1043       clib_error_report (error);
1044       rv = VNET_API_ERROR_SUBIF_CREATE_FAILED;
1045       goto out;
1046     }
1047
1048   u64 *kp = clib_mem_alloc (sizeof (*kp));
1049   *kp = sup_and_sub_key;
1050
1051   hash_set (hi->sub_interface_sw_if_index_by_id, sub_id, sw_if_index);
1052   hash_set_mem (im->sw_if_index_by_sup_and_sub, kp, sw_if_index);
1053
1054   BAD_SW_IF_INDEX_LABEL;
1055
1056 out:
1057
1058   /* *INDENT-OFF* */
1059   REPLY_MACRO2(VL_API_CREATE_SUBIF_REPLY,
1060   ({
1061     rmp->sw_if_index = ntohl(sw_if_index);
1062   }));
1063   /* *INDENT-ON* */
1064 }
1065
1066 static void
1067 vl_api_delete_subif_t_handler (vl_api_delete_subif_t * mp)
1068 {
1069   vl_api_delete_subif_reply_t *rmp;
1070   int rv;
1071
1072   rv = vnet_delete_sub_interface (ntohl (mp->sw_if_index));
1073
1074   REPLY_MACRO (VL_API_DELETE_SUBIF_REPLY);
1075 }
1076
1077 static void
1078 vl_api_interface_name_renumber_t_handler (vl_api_interface_name_renumber_t *
1079                                           mp)
1080 {
1081   vl_api_interface_name_renumber_reply_t *rmp;
1082   int rv = 0;
1083
1084   VALIDATE_SW_IF_INDEX (mp);
1085
1086   rv = vnet_interface_name_renumber
1087     (ntohl (mp->sw_if_index), ntohl (mp->new_show_dev_instance));
1088
1089   BAD_SW_IF_INDEX_LABEL;
1090
1091   REPLY_MACRO (VL_API_INTERFACE_NAME_RENUMBER_REPLY);
1092 }
1093
1094 static void
1095 vl_api_create_loopback_t_handler (vl_api_create_loopback_t * mp)
1096 {
1097   vl_api_create_loopback_reply_t *rmp;
1098   u32 sw_if_index;
1099   int rv;
1100
1101   rv = vnet_create_loopback_interface (&sw_if_index, mp->mac_address, 0, 0);
1102
1103   /* *INDENT-OFF* */
1104   REPLY_MACRO2(VL_API_CREATE_LOOPBACK_REPLY,
1105   ({
1106     rmp->sw_if_index = ntohl (sw_if_index);
1107   }));
1108   /* *INDENT-ON* */
1109 }
1110
1111 static void vl_api_create_loopback_instance_t_handler
1112   (vl_api_create_loopback_instance_t * mp)
1113 {
1114   vl_api_create_loopback_instance_reply_t *rmp;
1115   u32 sw_if_index;
1116   u8 is_specified = mp->is_specified;
1117   u32 user_instance = ntohl (mp->user_instance);
1118   int rv;
1119
1120   rv = vnet_create_loopback_interface (&sw_if_index, mp->mac_address,
1121                                        is_specified, user_instance);
1122
1123   /* *INDENT-OFF* */
1124   REPLY_MACRO2(VL_API_CREATE_LOOPBACK_INSTANCE_REPLY,
1125   ({
1126     rmp->sw_if_index = ntohl (sw_if_index);
1127   }));
1128   /* *INDENT-ON* */
1129 }
1130
1131 static void
1132 vl_api_delete_loopback_t_handler (vl_api_delete_loopback_t * mp)
1133 {
1134   vl_api_delete_loopback_reply_t *rmp;
1135   u32 sw_if_index;
1136   int rv;
1137
1138   sw_if_index = ntohl (mp->sw_if_index);
1139   rv = vnet_delete_loopback_interface (sw_if_index);
1140
1141   REPLY_MACRO (VL_API_DELETE_LOOPBACK_REPLY);
1142 }
1143
1144 static void
1145   vl_api_collect_detailed_interface_stats_t_handler
1146   (vl_api_collect_detailed_interface_stats_t * mp)
1147 {
1148   vl_api_collect_detailed_interface_stats_reply_t *rmp;
1149   int rv = 0;
1150
1151   rv =
1152     vnet_sw_interface_stats_collect_enable_disable (ntohl (mp->sw_if_index),
1153                                                     mp->enable_disable);
1154
1155   REPLY_MACRO (VL_API_COLLECT_DETAILED_INTERFACE_STATS_REPLY);
1156 }
1157
1158 /*
1159  * vpe_api_hookup
1160  * Add vpe's API message handlers to the table.
1161  * vlib has alread mapped shared memory and
1162  * added the client registration handlers.
1163  * See .../vlib-api/vlibmemory/memclnt_vlib.c:memclnt_process()
1164  */
1165 #define vl_msg_name_crc_list
1166 #include <vnet/interface.api.h>
1167 #undef vl_msg_name_crc_list
1168
1169 static void
1170 setup_message_id_table (api_main_t * am)
1171 {
1172 #define _(id,n,crc) vl_msg_api_add_msg_name_crc (am, #n "_" #crc, id);
1173   foreach_vl_msg_name_crc_interface;
1174 #undef _
1175 }
1176
1177 pub_sub_handler (interface_events, INTERFACE_EVENTS);
1178
1179 static clib_error_t *
1180 interface_api_hookup (vlib_main_t * vm)
1181 {
1182   api_main_t *am = &api_main;
1183
1184 #define _(N,n)                                                  \
1185     vl_msg_api_set_handlers(VL_API_##N, #n,                     \
1186                            vl_api_##n##_t_handler,              \
1187                            vl_noop_handler,                     \
1188                            vl_api_##n##_t_endian,               \
1189                            vl_api_##n##_t_print,                \
1190                            sizeof(vl_api_##n##_t), 1);
1191   foreach_vpe_api_msg;
1192 #undef _
1193
1194   /*
1195    * Set up the (msg_name, crc, message-id) table
1196    */
1197   setup_message_id_table (am);
1198
1199   return 0;
1200 }
1201
1202 VLIB_API_INIT_FUNCTION (interface_api_hookup);
1203
1204 /*
1205  * fd.io coding-style-patch-verification: ON
1206  *
1207  * Local Variables:
1208  * eval: (c-set-style "gnu")
1209  * End:
1210  */