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