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