1cb9cadadSEd Warnicke/*
2cb9cadadSEd Warnicke *------------------------------------------------------------------
311bfc2f7SHongjun Ni * api_format.c
411bfc2f7SHongjun Ni *
54a7e58bfSAlexander Popovsky (apopovsk) * Copyright (c) 2014-2016 Cisco and/or its affiliates.
6cb9cadadSEd Warnicke * Licensed under the Apache License, Version 2.0 (the "License");
7cb9cadadSEd Warnicke * you may not use this file except in compliance with the License.
8cb9cadadSEd Warnicke * You may obtain a copy of the License at:
9cb9cadadSEd Warnicke *
10cb9cadadSEd Warnicke *     http://www.apache.org/licenses/LICENSE-2.0
11cb9cadadSEd Warnicke *
12cb9cadadSEd Warnicke * Unless required by applicable law or agreed to in writing, software
13cb9cadadSEd Warnicke * distributed under the License is distributed on an "AS IS" BASIS,
14cb9cadadSEd Warnicke * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15cb9cadadSEd Warnicke * See the License for the specific language governing permissions and
1611bfc2f7SHongjun Ni * limitations under the License.
17cb9cadadSEd Warnicke *------------------------------------------------------------------
18cb9cadadSEd Warnicke */
19cb9cadadSEd Warnicke
207cd468a3SDamjan Marion#include <vat/vat.h>
21082ebebaSjialv#include <vlib/pci/pci.h>
2286327be9SNeale Ranns#include <vpp/api/types.h>
2359b2565cSDave Barach#include <vppinfra/socket.h>
24cb9cadadSEd Warnicke#include <vlibapi/api.h>
25cb9cadadSEd Warnicke#include <vlibmemory/api.h>
26cb9cadadSEd Warnicke#include <vnet/ip/ip.h>
27cbe25aabSNeale Ranns#include <vnet/ip-neighbor/ip_neighbor.h>
2837029305SNeale Ranns#include <vnet/ip/ip_types_api.h>
29cb9cadadSEd Warnicke#include <vnet/l2/l2_input.h>
30cb9cadadSEd Warnicke#include <vnet/l2tp/l2tp.h>
31cb9cadadSEd Warnicke#include <vnet/vxlan/vxlan.h>
32b598f1d3SMarco Varlese#include <vnet/geneve/geneve.h>
3327fe48f6SChris Luke#include <vnet/gre/gre.h>
3494b1442aSKeith Burns (alagalah)#include <vnet/vxlan-gpe/vxlan_gpe.h>
35cb9cadadSEd Warnicke#include <vnet/lisp-gpe/lisp_gpe.h>
36cb9cadadSEd Warnicke
377cd468a3SDamjan Marion#include <vpp/api/vpe_msg_enum.h>
3811bfc2f7SHongjun Ni#include <vnet/l2/l2_classify.h>
39cb9cadadSEd Warnicke#include <vnet/l2/l2_vtr.h>
40815d7d56SAndrew Yourtchenko#include <vnet/classify/in_out_acl.h>
4170e6a8ddSMatus Fabian#include <vnet/classify/policer_classify.h>
42506b2456SJuraj Sloboda#include <vnet/classify/flow_classify.h>
430bfe5d8cSNeale Ranns#include <vnet/mpls/mpls.h>
44cb9cadadSEd Warnicke#include <vnet/ipsec/ipsec.h>
45bfdedbd5SDave Barach#include <inttypes.h>
46c07bf5d5SDave Barach#include <vnet/cop/cop.h>
4720a64f5fSShwetha#include <vnet/ip/ip6_hop_by_hop.h>
486f9bca21SDave Barach#include <vnet/ip/ip_source_and_port_range_check.h>
4965fcd4d7SMatus Fabian#include <vnet/policer/xlate.h>
50f6e3dc47SPavel Kotucek#include <vnet/span/span.h>
514ac74c95SMatus Fabian#include <vnet/policer/policer.h>
5270e6a8ddSMatus Fabian#include <vnet/policer/police.h>
5332e1c010SNeale Ranns#include <vnet/mfib/mfib_types.h>
549cd2d7a5SSteven#include <vnet/bonding/node.h>
55582caa3bSIgor Mikhailov (imichail)#include <vnet/qos/qos_types.h>
5637029305SNeale Ranns#include <vnet/ethernet/ethernet_types_api.h>
5737029305SNeale Ranns#include <vnet/ip/ip_types_api.h>
587cd468a3SDamjan Marion#include "vat/json_format.h"
5986327be9SNeale Ranns#include <vnet/ip/ip_types_api.h>
6086327be9SNeale Ranns#include <vnet/ethernet/ethernet_types_api.h>
617cd468a3SDamjan Marion
6267655493SDamjan Marion#include <inttypes.h>
639e6ed6e2SPavel Kotucek#include <sys/stat.h>
649e6ed6e2SPavel Kotucek
6572d72231SDave Barach#define vl_typedefs		/* define message structures */
667cd468a3SDamjan Marion#include <vpp/api/vpe_all_api_h.h>
67cb9cadadSEd Warnicke#undef vl_typedefs
68cb9cadadSEd Warnicke
69cb9cadadSEd Warnicke/* declare message handlers for each api */
70cb9cadadSEd Warnicke
7172d72231SDave Barach#define vl_endianfun		/* define message structures */
727cd468a3SDamjan Marion#include <vpp/api/vpe_all_api_h.h>
73cb9cadadSEd Warnicke#undef vl_endianfun
74cb9cadadSEd Warnicke
75cb9cadadSEd Warnicke/* instantiate all the print functions we know about */
76f35a0727SDave Barach#if VPP_API_TEST_BUILTIN == 0
77cb9cadadSEd Warnicke#define vl_print(handle, ...)
78f35a0727SDave Barach#else
79f35a0727SDave Barach#define vl_print(handle, ...) vlib_cli_output (handle, __VA_ARGS__)
80f35a0727SDave Barach#endif
81cb9cadadSEd Warnicke#define vl_printfun
827cd468a3SDamjan Marion#include <vpp/api/vpe_all_api_h.h>
83cb9cadadSEd Warnicke#undef vl_printfun
84cb9cadadSEd Warnicke
852d6b2d6dSDave Barach#define __plugin_msg_base 0
86fe6bdfd8SDave Barach#include <vlibapi/vat_helper_macros.h>
87fe6bdfd8SDave Barach
8833a58171SOle Troan#include <vnet/format_fns.h>
8933a58171SOle Troan
90b09f4d0aSDave Barachvoid vl_api_set_elog_main (elog_main_t * m);
91b09f4d0aSDave Barachint vl_api_set_elog_trace_api_messages (int enable);
92b09f4d0aSDave Barach
9359b2565cSDave Barach#if VPP_API_TEST_BUILTIN == 0
9459b2565cSDave Barach#include <netdb.h>
9559b2565cSDave Barach
9659b2565cSDave Barachu32
9759b2565cSDave Barachvl (void *p)
9859b2565cSDave Barach{
9959b2565cSDave Barach  return vec_len (p);
10059b2565cSDave Barach}
10159b2565cSDave Barach
10259b2565cSDave Barachint
10359b2565cSDave Barachvat_socket_connect (vat_main_t * vam)
10459b2565cSDave Barach{
10566a1003fSFlorin Coras  int rv;
10669eeadc0SDave Barach  api_main_t *am = vlibapi_get_main ();
10790a63988SFlorin Coras  vam->socket_client_main = &socket_client_main;
10866a1003fSFlorin Coras  if ((rv = vl_socket_client_connect ((char *) vam->socket_name,
10966a1003fSFlorin Coras				      "vpp_api_test",
11066a1003fSFlorin Coras				      0 /* default socket rx, tx buffer */ )))
11166a1003fSFlorin Coras    return rv;
11269eeadc0SDave Barach
11366a1003fSFlorin Coras  /* vpp expects the client index in network order */
11466a1003fSFlorin Coras  vam->my_client_index = htonl (socket_client_main.client_index);
11569eeadc0SDave Barach  am->my_client_index = vam->my_client_index;
11666a1003fSFlorin Coras  return 0;
11759b2565cSDave Barach}
11859b2565cSDave Barach#else /* vpp built-in case, we don't do sockets... */
11959b2565cSDave Barachint
12059b2565cSDave Barachvat_socket_connect (vat_main_t * vam)
12159b2565cSDave Barach{
12259b2565cSDave Barach  return 0;
12359b2565cSDave Barach}
12459b2565cSDave Barach
12590a63988SFlorin Corasint
12690a63988SFlorin Corasvl_socket_client_read (int wait)
12790a63988SFlorin Coras{
12890a63988SFlorin Coras  return -1;
12990a63988SFlorin Coras};
13090a63988SFlorin Coras
13190a63988SFlorin Corasint
13290a63988SFlorin Corasvl_socket_client_write ()
13359b2565cSDave Barach{
13490a63988SFlorin Coras  return -1;
13559b2565cSDave Barach};
13690a63988SFlorin Coras
13790a63988SFlorin Corasvoid *
13890a63988SFlorin Corasvl_socket_client_msg_alloc (int nbytes)
13990a63988SFlorin Coras{
14090a63988SFlorin Coras  return 0;
14190a63988SFlorin Coras}
14259b2565cSDave Barach#endif
14359b2565cSDave Barach
14459b2565cSDave Barach
145fe6bdfd8SDave Barachf64
146fe6bdfd8SDave Barachvat_time_now (vat_main_t * vam)
147fe6bdfd8SDave Barach{
148fe6bdfd8SDave Barach#if VPP_API_TEST_BUILTIN
149fe6bdfd8SDave Barach  return vlib_time_now (vam->vlib_main);
150fe6bdfd8SDave Barach#else
151fe6bdfd8SDave Barach  return clib_time_now (&vam->clib_time);
152fe6bdfd8SDave Barach#endif
153fe6bdfd8SDave Barach}
154fe6bdfd8SDave Barach
155fe6bdfd8SDave Barachvoid
156fe6bdfd8SDave Baracherrmsg (char *fmt, ...)
157fe6bdfd8SDave Barach{
158fe6bdfd8SDave Barach  vat_main_t *vam = &vat_main;
159fe6bdfd8SDave Barach  va_list va;
160fe6bdfd8SDave Barach  u8 *s;
161fe6bdfd8SDave Barach
162fe6bdfd8SDave Barach  va_start (va, fmt);
163fe6bdfd8SDave Barach  s = va_format (0, fmt, &va);
164fe6bdfd8SDave Barach  va_end (va);
165fe6bdfd8SDave Barach
166fe6bdfd8SDave Barach  vec_add1 (s, 0);
167fe6bdfd8SDave Barach
168fe6bdfd8SDave Barach#if VPP_API_TEST_BUILTIN
169fe6bdfd8SDave Barach  vlib_cli_output (vam->vlib_main, (char *) s);
170fe6bdfd8SDave Barach#else
171fe6bdfd8SDave Barach  {
172fe6bdfd8SDave Barach    if (vam->ifp != stdin)
173fe6bdfd8SDave Barach      fformat (vam->ofp, "%s(%d): \n", vam->current_file,
174fe6bdfd8SDave Barach	       vam->input_line_number);
175b09f4d0aSDave Barach    else
176b09f4d0aSDave Barach      fformat (vam->ofp, "%s\n", (char *) s);
177fe6bdfd8SDave Barach    fflush (vam->ofp);
178fe6bdfd8SDave Barach  }
179fe6bdfd8SDave Barach#endif
180fe6bdfd8SDave Barach
181fe6bdfd8SDave Barach  vec_free (s);
182fe6bdfd8SDave Barach}
183fe6bdfd8SDave Barach
1844a3f69c1SDave Barach#if VPP_API_TEST_BUILTIN == 0
185fc262a0cSDave Barachstatic uword
186fc262a0cSDave Barachapi_unformat_sw_if_index (unformat_input_t * input, va_list * args)
187cb9cadadSEd Warnicke{
18872d72231SDave Barach  vat_main_t *vam = va_arg (*args, vat_main_t *);
18972d72231SDave Barach  u32 *result = va_arg (*args, u32 *);
19072d72231SDave Barach  u8 *if_name;
19172d72231SDave Barach  uword *p;
192cb9cadadSEd Warnicke
193cb9cadadSEd Warnicke  if (!unformat (input, "%s", &if_name))
19472d72231SDave Barach    return 0;
195cb9cadadSEd Warnicke
196cb9cadadSEd Warnicke  p = hash_get_mem (vam->sw_if_index_by_interface_name, if_name);
197cb9cadadSEd Warnicke  if (p == 0)
19872d72231SDave Barach    return 0;
199cb9cadadSEd Warnicke  *result = p[0];
200cb9cadadSEd Warnicke  return 1;
201cb9cadadSEd Warnicke}
202cb9cadadSEd Warnicke
203af86a487Seyal baristatic uword
204af86a487Seyal bariapi_unformat_hw_if_index (unformat_input_t * input, va_list * args)
205af86a487Seyal bari{
206af86a487Seyal bari  return 0;
207af86a487Seyal bari}
208af86a487Seyal bari
209cb9cadadSEd Warnicke/* Parse an IP4 address %d.%d.%d.%d. */
21072d72231SDave Barachuword
21172d72231SDave Barachunformat_ip4_address (unformat_input_t * input, va_list * args)
212cb9cadadSEd Warnicke{
21372d72231SDave Barach  u8 *result = va_arg (*args, u8 *);
214cb9cadadSEd Warnicke  unsigned a[4];
215cb9cadadSEd Warnicke
21672d72231SDave Barach  if (!unformat (input, "%d.%d.%d.%d", &a[0], &a[1], &a[2], &a[3]))
217cb9cadadSEd Warnicke    return 0;
218cb9cadadSEd Warnicke
219cb9cadadSEd Warnicke  if (a[0] >= 256 || a[1] >= 256 || a[2] >= 256 || a[3] >= 256)
220cb9cadadSEd Warnicke    return 0;
221cb9cadadSEd Warnicke
222cb9cadadSEd Warnicke  result[0] = a[0];
223cb9cadadSEd Warnicke  result[1] = a[1];
224cb9cadadSEd Warnicke  result[2] = a[2];
225cb9cadadSEd Warnicke  result[3] = a[3];
226cb9cadadSEd Warnicke
227cb9cadadSEd Warnicke  return 1;
228cb9cadadSEd Warnicke}
229cb9cadadSEd Warnicke
230cb9cadadSEd Warnickeuword
231cb9cadadSEd Warnickeunformat_ethernet_address (unformat_input_t * input, va_list * args)
232cb9cadadSEd Warnicke{
23372d72231SDave Barach  u8 *result = va_arg (*args, u8 *);
234cb9cadadSEd Warnicke  u32 i, a[6];
235cb9cadadSEd Warnicke
23672d72231SDave Barach  if (!unformat (input, "%_%x:%x:%x:%x:%x:%x%_",
23772d72231SDave Barach		 &a[0], &a[1], &a[2], &a[3], &a[4], &a[5]))
238cb9cadadSEd Warnicke    return 0;
239cb9cadadSEd Warnicke
240cb9cadadSEd Warnicke  /* Check range. */
241cb9cadadSEd Warnicke  for (i = 0; i < 6; i++)
242cb9cadadSEd Warnicke    if (a[i] >= (1 << 8))
243cb9cadadSEd Warnicke      return 0;
244cb9cadadSEd Warnicke
245cb9cadadSEd Warnicke  for (i = 0; i < 6; i++)
246cb9cadadSEd Warnicke    result[i] = a[i];
247cb9cadadSEd Warnicke
248cb9cadadSEd Warnicke  return 1;
249cb9cadadSEd Warnicke}
250cb9cadadSEd Warnicke
251cb9cadadSEd Warnicke/* Returns ethernet type as an int in host byte order. */
252cb9cadadSEd Warnickeuword
253cb9cadadSEd Warnickeunformat_ethernet_type_host_byte_order (unformat_input_t * input,
254cb9cadadSEd Warnicke					va_list * args)
255cb9cadadSEd Warnicke{
25672d72231SDave Barach  u16 *result = va_arg (*args, u16 *);
257cb9cadadSEd Warnicke  int type;
258cb9cadadSEd Warnicke
259cb9cadadSEd Warnicke  /* Numeric type. */
26072d72231SDave Barach  if (unformat (input, "0x%x", &type) || unformat (input, "%d", &type))
261cb9cadadSEd Warnicke    {
262cb9cadadSEd Warnicke      if (type >= (1 << 16))
263cb9cadadSEd Warnicke	return 0;
264cb9cadadSEd Warnicke      *result = type;
265cb9cadadSEd Warnicke      return 1;
266cb9cadadSEd Warnicke    }
267cb9cadadSEd Warnicke  return 0;
268cb9cadadSEd Warnicke}
269cb9cadadSEd Warnicke
27023a386b7SJakub Grajciar/* Parse an IP46 address. */
27123a386b7SJakub Grajciaruword
27223a386b7SJakub Grajciarunformat_ip46_address (unformat_input_t * input, va_list * args)
27323a386b7SJakub Grajciar{
27423a386b7SJakub Grajciar  ip46_address_t *ip46 = va_arg (*args, ip46_address_t *);
27523a386b7SJakub Grajciar  ip46_type_t type = va_arg (*args, ip46_type_t);
27623a386b7SJakub Grajciar  if ((type != IP46_TYPE_IP6) &&
27723a386b7SJakub Grajciar      unformat (input, "%U", unformat_ip4_address, &ip46->ip4))
27823a386b7SJakub Grajciar    {
27923a386b7SJakub Grajciar      ip46_address_mask_ip4 (ip46);
28023a386b7SJakub Grajciar      return 1;
28123a386b7SJakub Grajciar    }
28223a386b7SJakub Grajciar  else if ((type != IP46_TYPE_IP4) &&
28323a386b7SJakub Grajciar	   unformat (input, "%U", unformat_ip6_address, &ip46->ip6))
28423a386b7SJakub Grajciar    {
28523a386b7SJakub Grajciar      return 1;
28623a386b7SJakub Grajciar    }
28723a386b7SJakub Grajciar  return 0;
28823a386b7SJakub Grajciar}
28923a386b7SJakub Grajciar
290cb9cadadSEd Warnicke/* Parse an IP6 address. */
29172d72231SDave Barachuword
29272d72231SDave Barachunformat_ip6_address (unformat_input_t * input, va_list * args)
293cb9cadadSEd Warnicke{
29472d72231SDave Barach  ip6_address_t *result = va_arg (*args, ip6_address_t *);
295cb9cadadSEd Warnicke  u16 hex_quads[8];
296cb9cadadSEd Warnicke  uword hex_quad, n_hex_quads, hex_digit, n_hex_digits;
297cb9cadadSEd Warnicke  uword c, n_colon, double_colon_index;
298cb9cadadSEd Warnicke
299cb9cadadSEd Warnicke  n_hex_quads = hex_quad = n_hex_digits = n_colon = 0;
300cb9cadadSEd Warnicke  double_colon_index = ARRAY_LEN (hex_quads);
301cb9cadadSEd Warnicke  while ((c = unformat_get_input (input)) != UNFORMAT_END_OF_INPUT)
302cb9cadadSEd Warnicke    {
303cb9cadadSEd Warnicke      hex_digit = 16;
304cb9cadadSEd Warnicke      if (c >= '0' && c <= '9')
305cb9cadadSEd Warnicke	hex_digit = c - '0';
306cb9cadadSEd Warnicke      else if (c >= 'a' && c <= 'f')
307cb9cadadSEd Warnicke	hex_digit = c + 10 - 'a';
308cb9cadadSEd Warnicke      else if (c >= 'A' && c <= 'F')
309cb9cadadSEd Warnicke	hex_digit = c + 10 - 'A';
310cb9cadadSEd Warnicke      else if (c == ':' && n_colon < 2)
311cb9cadadSEd Warnicke	n_colon++;
312cb9cadadSEd Warnicke      else
313cb9cadadSEd Warnicke	{
314cb9cadadSEd Warnicke	  unformat_put_input (input);
315cb9cadadSEd Warnicke	  break;
316cb9cadadSEd Warnicke	}
317cb9cadadSEd Warnicke
318cb9cadadSEd Warnicke      /* Too many hex quads. */
319cb9cadadSEd Warnicke      if (n_hex_quads >= ARRAY_LEN (hex_quads))
320cb9cadadSEd Warnicke	return 0;
321cb9cadadSEd Warnicke
322cb9cadadSEd Warnicke      if (hex_digit < 16)
323cb9cadadSEd Warnicke	{
324cb9cadadSEd Warnicke	  hex_quad = (hex_quad << 4) | hex_digit;
325cb9cadadSEd Warnicke
326cb9cadadSEd Warnicke	  /* Hex quad must fit in 16 bits. */
327cb9cadadSEd Warnicke	  if (n_hex_digits >= 4)
328cb9cadadSEd Warnicke	    return 0;
329cb9cadadSEd Warnicke
330cb9cadadSEd Warnicke	  n_colon = 0;
331cb9cadadSEd Warnicke	  n_hex_digits++;
332cb9cadadSEd Warnicke	}
33311bfc2f7SHongjun Ni
334cb9cadadSEd Warnicke      /* Save position of :: */
335cb9cadadSEd Warnicke      if (n_colon == 2)
336cb9cadadSEd Warnicke	{
337cb9cadadSEd Warnicke	  /* More than one :: ? */
338cb9cadadSEd Warnicke	  if (double_colon_index < ARRAY_LEN (hex_quads))
339cb9cadadSEd Warnicke	    return 0;
340cb9cadadSEd Warnicke	  double_colon_index = n_hex_quads;
341cb9cadadSEd Warnicke	}
342cb9cadadSEd Warnicke
343cb9cadadSEd Warnicke      if (n_colon > 0 && n_hex_digits > 0)
344cb9cadadSEd Warnicke	{
345cb9cadadSEd Warnicke	  hex_quads[n_hex_quads++] = hex_quad;
346cb9cadadSEd Warnicke	  hex_quad = 0;
347cb9cadadSEd Warnicke	  n_hex_digits = 0;
348cb9cadadSEd Warnicke	}
349cb9cadadSEd Warnicke    }
350cb9cadadSEd Warnicke
351cb9cadadSEd Warnicke  if (n_hex_digits > 0)
352cb9cadadSEd Warnicke    hex_quads[n_hex_quads++] = hex_quad;
353cb9cadadSEd Warnicke
354cb9cadadSEd Warnicke  {
355cb9cadadSEd Warnicke    word i;
356cb9cadadSEd Warnicke
357cb9cadadSEd Warnicke    /* Expand :: to appropriate number of zero hex quads. */
358cb9cadadSEd Warnicke    if (double_colon_index < ARRAY_LEN (hex_quads))
359cb9cadadSEd Warnicke      {
360cb9cadadSEd Warnicke	word n_zero = ARRAY_LEN (hex_quads) - n_hex_quads;
361cb9cadadSEd Warnicke
362cb9cadadSEd Warnicke	for (i = n_hex_quads - 1; i >= (signed) double_colon_index; i--)
363cb9cadadSEd Warnicke	  hex_quads[n_zero + i] = hex_quads[i];
364cb9cadadSEd Warnicke
365cb9cadadSEd Warnicke	for (i = 0; i < n_zero; i++)
366cb9cadadSEd Warnicke	  hex_quads[double_colon_index + i] = 0;
367cb9cadadSEd Warnicke
368cb9cadadSEd Warnicke	n_hex_quads = ARRAY_LEN (hex_quads);
369cb9cadadSEd Warnicke      }
370cb9cadadSEd Warnicke
371cb9cadadSEd Warnicke    /* Too few hex quads given. */
372cb9cadadSEd Warnicke    if (n_hex_quads < ARRAY_LEN (hex_quads))
373cb9cadadSEd Warnicke      return 0;
374cb9cadadSEd Warnicke
375cb9cadadSEd Warnicke    for (i = 0; i < ARRAY_LEN (hex_quads); i++)
376cb9cadadSEd Warnicke      result->as_u16[i] = clib_host_to_net_u16 (hex_quads[i]);
377cb9cadadSEd Warnicke
378cb9cadadSEd Warnicke    return 1;
379cb9cadadSEd Warnicke  }
380cb9cadadSEd Warnicke}
381cb9cadadSEd Warnicke
382cb9cadadSEd Warnickeuword
383cb9cadadSEd Warnickeunformat_ipsec_policy_action (unformat_input_t * input, va_list * args)
384cb9cadadSEd Warnicke{
38572d72231SDave Barach  u32 *r = va_arg (*args, u32 *);
386cb9cadadSEd Warnicke
38772d72231SDave Barach  if (0);
388cb9cadadSEd Warnicke#define _(v,f,s) else if (unformat (input, s)) *r = IPSEC_POLICY_ACTION_##f;
389cb9cadadSEd Warnicke  foreach_ipsec_policy_action
390cb9cadadSEd Warnicke#undef _
39172d72231SDave Barach    else
392cb9cadadSEd Warnicke    return 0;
393cb9cadadSEd Warnicke  return 1;
394cb9cadadSEd Warnicke}
395cb9cadadSEd Warnicke
396cb9cadadSEd Warnickeu8 *
397cb9cadadSEd Warnickeformat_ipsec_crypto_alg (u8 * s, va_list * args)
398cb9cadadSEd Warnicke{
399cb9cadadSEd Warnicke  u32 i = va_arg (*args, u32);
40072d72231SDave Barach  u8 *t = 0;
401cb9cadadSEd Warnicke
402cb9cadadSEd Warnicke  switch (i)
403cb9cadadSEd Warnicke    {
404cb9cadadSEd Warnicke#define _(v,f,str) case IPSEC_CRYPTO_ALG_##f: t = (u8 *) str; break;
40572d72231SDave Barach      foreach_ipsec_crypto_alg
406cb9cadadSEd Warnicke#undef _
40772d72231SDave Barach    default:
40872d72231SDave Barach      return format (s, "unknown");
409cb9cadadSEd Warnicke    }
410cb9cadadSEd Warnicke  return format (s, "%s", t);
411cb9cadadSEd Warnicke}
412cb9cadadSEd Warnicke
413cb9cadadSEd Warnickeu8 *
414cb9cadadSEd Warnickeformat_ipsec_integ_alg (u8 * s, va_list * args)
415cb9cadadSEd Warnicke{
416cb9cadadSEd Warnicke  u32 i = va_arg (*args, u32);
41772d72231SDave Barach  u8 *t = 0;
418cb9cadadSEd Warnicke
419cb9cadadSEd Warnicke  switch (i)
420cb9cadadSEd Warnicke    {
421cb9cadadSEd Warnicke#define _(v,f,str) case IPSEC_INTEG_ALG_##f: t = (u8 *) str; break;
42272d72231SDave Barach      foreach_ipsec_integ_alg
423cb9cadadSEd Warnicke#undef _
42472d72231SDave Barach    default:
42572d72231SDave Barach      return format (s, "unknown");
426cb9cadadSEd Warnicke    }
427cb9cadadSEd Warnicke  return format (s, "%s", t);
428cb9cadadSEd Warnicke}
429cb9cadadSEd Warnicke
4304a3f69c1SDave Barach#else /* VPP_API_TEST_BUILTIN == 1 */
4314a3f69c1SDave Barachstatic uword
4324a3f69c1SDave Barachapi_unformat_sw_if_index (unformat_input_t * input, va_list * args)
4334a3f69c1SDave Barach{
43449ee6844SBenoît Ganne  vat_main_t *vam __clib_unused = va_arg (*args, vat_main_t *);
4354a3f69c1SDave Barach  vnet_main_t *vnm = vnet_get_main ();
4364a3f69c1SDave Barach  u32 *result = va_arg (*args, u32 *);
4374a3f69c1SDave Barach
438af86a487Seyal bari  return unformat (input, "%U", unformat_vnet_sw_interface, vnm, result);
439af86a487Seyal bari}
4404a3f69c1SDave Barach
441af86a487Seyal baristatic uword
442af86a487Seyal bariapi_unformat_hw_if_index (unformat_input_t * input, va_list * args)
443af86a487Seyal bari{
44449ee6844SBenoît Ganne  vat_main_t *vam __clib_unused = va_arg (*args, vat_main_t *);
445af86a487Seyal bari  vnet_main_t *vnm = vnet_get_main ();
446af86a487Seyal bari  u32 *result = va_arg (*args, u32 *);
447af86a487Seyal bari
448af86a487Seyal bari  return unformat (input, "%U", unformat_vnet_hw_interface, vnm, result);
4494a3f69c1SDave Barach}
450af86a487Seyal bari
451fc262a0cSDave Barach#endif /* VPP_API_TEST_BUILTIN */
452e5f42febSMatus Fabian
45317dcec0bSNeale Rannsuword
45417dcec0bSNeale Rannsunformat_ipsec_api_crypto_alg (unformat_input_t * input, va_list * args)
45517dcec0bSNeale Ranns{
45617dcec0bSNeale Ranns  u32 *r = va_arg (*args, u32 *);
45717dcec0bSNeale Ranns
45817dcec0bSNeale Ranns  if (0);
45917dcec0bSNeale Ranns#define _(v,f,s) else if (unformat (input, s)) *r = IPSEC_API_CRYPTO_ALG_##f;
46017dcec0bSNeale Ranns  foreach_ipsec_crypto_alg
46117dcec0bSNeale Ranns#undef _
46217dcec0bSNeale Ranns    else
46317dcec0bSNeale Ranns    return 0;
46417dcec0bSNeale Ranns  return 1;
46517dcec0bSNeale Ranns}
46617dcec0bSNeale Ranns
46717dcec0bSNeale Rannsuword
46817dcec0bSNeale Rannsunformat_ipsec_api_integ_alg (unformat_input_t * input, va_list * args)
46917dcec0bSNeale Ranns{
47017dcec0bSNeale Ranns  u32 *r = va_arg (*args, u32 *);
47117dcec0bSNeale Ranns
47217dcec0bSNeale Ranns  if (0);
47317dcec0bSNeale Ranns#define _(v,f,s) else if (unformat (input, s)) *r = IPSEC_API_INTEG_ALG_##f;
47417dcec0bSNeale Ranns  foreach_ipsec_integ_alg
47517dcec0bSNeale Ranns#undef _
47617dcec0bSNeale Ranns    else
47717dcec0bSNeale Ranns    return 0;
47817dcec0bSNeale Ranns  return 1;
47917dcec0bSNeale Ranns}
48017dcec0bSNeale Ranns
481fc262a0cSDave Barachstatic uword
48265fcd4d7SMatus Fabianunformat_policer_rate_type (unformat_input_t * input, va_list * args)
48365fcd4d7SMatus Fabian{
48472d72231SDave Barach  u8 *r = va_arg (*args, u8 *);
48565fcd4d7SMatus Fabian
48665fcd4d7SMatus Fabian  if (unformat (input, "kbps"))
48765fcd4d7SMatus Fabian    *r = SSE2_QOS_RATE_KBPS;
48872d72231SDave Barach  else if (unformat (input, "pps"))
48965fcd4d7SMatus Fabian    *r = SSE2_QOS_RATE_PPS;
49065fcd4d7SMatus Fabian  else
49165fcd4d7SMatus Fabian    return 0;
49265fcd4d7SMatus Fabian  return 1;
49365fcd4d7SMatus Fabian}
49465fcd4d7SMatus Fabian
495fc262a0cSDave Barachstatic uword
49665fcd4d7SMatus Fabianunformat_policer_round_type (unformat_input_t * input, va_list * args)
49765fcd4d7SMatus Fabian{
49872d72231SDave Barach  u8 *r = va_arg (*args, u8 *);
49965fcd4d7SMatus Fabian
50072d72231SDave Barach  if (unformat (input, "closest"))
50165fcd4d7SMatus Fabian    *r = SSE2_QOS_ROUND_TO_CLOSEST;
50265fcd4d7SMatus Fabian  else if (unformat (input, "up"))
50365fcd4d7SMatus Fabian    *r = SSE2_QOS_ROUND_TO_UP;
50465fcd4d7SMatus Fabian  else if (unformat (input, "down"))
50565fcd4d7SMatus Fabian    *r = SSE2_QOS_ROUND_TO_DOWN;
50665fcd4d7SMatus Fabian  else
50765fcd4d7SMatus Fabian    return 0;
50865fcd4d7SMatus Fabian  return 1;
50965fcd4d7SMatus Fabian}
51065fcd4d7SMatus Fabian
511fc262a0cSDave Barachstatic uword
51265fcd4d7SMatus Fabianunformat_policer_type (unformat_input_t * input, va_list * args)
51365fcd4d7SMatus Fabian{
51472d72231SDave Barach  u8 *r = va_arg (*args, u8 *);
51565fcd4d7SMatus Fabian
51665fcd4d7SMatus Fabian  if (unformat (input, "1r2c"))
51765fcd4d7SMatus Fabian    *r = SSE2_QOS_POLICER_TYPE_1R2C;
51865fcd4d7SMatus Fabian  else if (unformat (input, "1r3c"))
51965fcd4d7SMatus Fabian    *r = SSE2_QOS_POLICER_TYPE_1R3C_RFC_2697;
52065fcd4d7SMatus Fabian  else if (unformat (input, "2r3c-2698"))
52165fcd4d7SMatus Fabian    *r = SSE2_QOS_POLICER_TYPE_2R3C_RFC_2698;
52265fcd4d7SMatus Fabian  else if (unformat (input, "2r3c-4115"))
52365fcd4d7SMatus Fabian    *r = SSE2_QOS_POLICER_TYPE_2R3C_RFC_4115;
52465fcd4d7SMatus Fabian  else if (unformat (input, "2r3c-mef5cf1"))
52565fcd4d7SMatus Fabian    *r = SSE2_QOS_POLICER_TYPE_2R3C_RFC_MEF5CF1;
52665fcd4d7SMatus Fabian  else
52765fcd4d7SMatus Fabian    return 0;
52865fcd4d7SMatus Fabian  return 1;
52965fcd4d7SMatus Fabian}
53065fcd4d7SMatus Fabian
531fc262a0cSDave Barachstatic uword
5324ac74c95SMatus Fabianunformat_dscp (unformat_input_t * input, va_list * va)
5334ac74c95SMatus Fabian{
53472d72231SDave Barach  u8 *r = va_arg (*va, u8 *);
5354ac74c95SMatus Fabian
53672d72231SDave Barach  if (0);
5374ac74c95SMatus Fabian#define _(v,f,str) else if (unformat (input, str)) *r = VNET_DSCP_##f;
53872d72231SDave Barach  foreach_vnet_dscp
5394ac74c95SMatus Fabian#undef _
54072d72231SDave Barach    else
5414ac74c95SMatus Fabian    return 0;
5424ac74c95SMatus Fabian  return 1;
5434ac74c95SMatus Fabian}
5444ac74c95SMatus Fabian
545fc262a0cSDave Barachstatic uword
5464ac74c95SMatus Fabianunformat_policer_action_type (unformat_input_t * input, va_list * va)
5474ac74c95SMatus Fabian{
54872d72231SDave Barach  sse2_qos_pol_action_params_st *a
5494ac74c95SMatus Fabian    = va_arg (*va, sse2_qos_pol_action_params_st *);
5504ac74c95SMatus Fabian
5514ac74c95SMatus Fabian  if (unformat (input, "drop"))
5524ac74c95SMatus Fabian    a->action_type = SSE2_QOS_ACTION_DROP;
5534ac74c95SMatus Fabian  else if (unformat (input, "transmit"))
5544ac74c95SMatus Fabian    a->action_type = SSE2_QOS_ACTION_TRANSMIT;
5554ac74c95SMatus Fabian  else if (unformat (input, "mark-and-transmit %U", unformat_dscp, &a->dscp))
5564ac74c95SMatus Fabian    a->action_type = SSE2_QOS_ACTION_MARK_AND_TRANSMIT;
5574ac74c95SMatus Fabian  else
5584ac74c95SMatus Fabian    return 0;
5594ac74c95SMatus Fabian  return 1;
5604ac74c95SMatus Fabian}
5614ac74c95SMatus Fabian
562fc262a0cSDave Barachstatic uword
563506b2456SJuraj Slobodaunformat_policer_classify_table_type (unformat_input_t * input, va_list * va)
56470e6a8ddSMatus Fabian{
56572d72231SDave Barach  u32 *r = va_arg (*va, u32 *);
56670e6a8ddSMatus Fabian  u32 tid;
56770e6a8ddSMatus Fabian
56870e6a8ddSMatus Fabian  if (unformat (input, "ip4"))
56970e6a8ddSMatus Fabian    tid = POLICER_CLASSIFY_TABLE_IP4;
57070e6a8ddSMatus Fabian  else if (unformat (input, "ip6"))
57170e6a8ddSMatus Fabian    tid = POLICER_CLASSIFY_TABLE_IP6;
57270e6a8ddSMatus Fabian  else if (unformat (input, "l2"))
57370e6a8ddSMatus Fabian    tid = POLICER_CLASSIFY_TABLE_L2;
57470e6a8ddSMatus Fabian  else
57570e6a8ddSMatus Fabian    return 0;
57670e6a8ddSMatus Fabian
57770e6a8ddSMatus Fabian  *r = tid;
57870e6a8ddSMatus Fabian  return 1;
57970e6a8ddSMatus Fabian}
58070e6a8ddSMatus Fabian
581fc262a0cSDave Barachstatic uword
582506b2456SJuraj Slobodaunformat_flow_classify_table_type (unformat_input_t * input, va_list * va)
583506b2456SJuraj Sloboda{
584506b2456SJuraj Sloboda  u32 *r = va_arg (*va, u32 *);
585506b2456SJuraj Sloboda  u32 tid;
586506b2456SJuraj Sloboda
587506b2456SJuraj Sloboda  if (unformat (input, "ip4"))
588506b2456SJuraj Sloboda    tid = FLOW_CLASSIFY_TABLE_IP4;
589506b2456SJuraj Sloboda  else if (unformat (input, "ip6"))
590506b2456SJuraj Sloboda    tid = FLOW_CLASSIFY_TABLE_IP6;
591506b2456SJuraj Sloboda  else
592506b2456SJuraj Sloboda    return 0;
593506b2456SJuraj Sloboda
594506b2456SJuraj Sloboda  *r = tid;
595506b2456SJuraj Sloboda  return 1;
596506b2456SJuraj Sloboda}
597506b2456SJuraj Sloboda
59849ee6844SBenoît Ganne#if (VPP_API_TEST_BUILTIN==0)
59949ee6844SBenoît Ganne
60032e1c010SNeale Rannsstatic const char *mfib_flag_names[] = MFIB_ENTRY_NAMES_SHORT;
60132e1c010SNeale Rannsstatic const char *mfib_flag_long_names[] = MFIB_ENTRY_NAMES_LONG;
60232e1c010SNeale Rannsstatic const char *mfib_itf_flag_long_names[] = MFIB_ITF_NAMES_LONG;
60332e1c010SNeale Rannsstatic const char *mfib_itf_flag_names[] = MFIB_ITF_NAMES_SHORT;
60432e1c010SNeale Ranns
60532e1c010SNeale Rannsuword
60632e1c010SNeale Rannsunformat_mfib_itf_flags (unformat_input_t * input, va_list * args)
60732e1c010SNeale Ranns{
60832e1c010SNeale Ranns  mfib_itf_flags_t old, *iflags = va_arg (*args, mfib_itf_flags_t *);
60932e1c010SNeale Ranns  mfib_itf_attribute_t attr;
61032e1c010SNeale Ranns
61132e1c010SNeale Ranns  old = *iflags;
61232e1c010SNeale Ranns  FOR_EACH_MFIB_ITF_ATTRIBUTE (attr)
61332e1c010SNeale Ranns  {
61432e1c010SNeale Ranns    if (unformat (input, mfib_itf_flag_long_names[attr]))
61532e1c010SNeale Ranns      *iflags |= (1 << attr);
61632e1c010SNeale Ranns  }
61732e1c010SNeale Ranns  FOR_EACH_MFIB_ITF_ATTRIBUTE (attr)
61832e1c010SNeale Ranns  {
61932e1c010SNeale Ranns    if (unformat (input, mfib_itf_flag_names[attr]))
62032e1c010SNeale Ranns      *iflags |= (1 << attr);
62132e1c010SNeale Ranns  }
62232e1c010SNeale Ranns
62332e1c010SNeale Ranns  return (old == *iflags ? 0 : 1);
62432e1c010SNeale Ranns}
62532e1c010SNeale Ranns
62632e1c010SNeale Rannsuword
62732e1c010SNeale Rannsunformat_mfib_entry_flags (unformat_input_t * input, va_list * args)
62832e1c010SNeale Ranns{
62932e1c010SNeale Ranns  mfib_entry_flags_t old, *eflags = va_arg (*args, mfib_entry_flags_t *);
63032e1c010SNeale Ranns  mfib_entry_attribute_t attr;
63132e1c010SNeale Ranns
63232e1c010SNeale Ranns  old = *eflags;
63332e1c010SNeale Ranns  FOR_EACH_MFIB_ATTRIBUTE (attr)
63432e1c010SNeale Ranns  {
63532e1c010SNeale Ranns    if (unformat (input, mfib_flag_long_names[attr]))
63632e1c010SNeale Ranns      *eflags |= (1 << attr);
63732e1c010SNeale Ranns  }
63832e1c010SNeale Ranns  FOR_EACH_MFIB_ATTRIBUTE (attr)
63932e1c010SNeale Ranns  {
64032e1c010SNeale Ranns    if (unformat (input, mfib_flag_names[attr]))
64132e1c010SNeale Ranns      *eflags |= (1 << attr);
64232e1c010SNeale Ranns  }
64332e1c010SNeale Ranns
64432e1c010SNeale Ranns  return (old == *eflags ? 0 : 1);
64532e1c010SNeale Ranns}
64632e1c010SNeale Ranns
64772d72231SDave Barachu8 *
64872d72231SDave Barachformat_ip4_address (u8 * s, va_list * args)
649cb9cadadSEd Warnicke{
65072d72231SDave Barach  u8 *a = va_arg (*args, u8 *);
651cb9cadadSEd Warnicke  return format (s, "%d.%d.%d.%d", a[0], a[1], a[2], a[3]);
652cb9cadadSEd Warnicke}
653cb9cadadSEd Warnicke
65472d72231SDave Barachu8 *
65572d72231SDave Barachformat_ip6_address (u8 * s, va_list * args)
656cb9cadadSEd Warnicke{
65772d72231SDave Barach  ip6_address_t *a = va_arg (*args, ip6_address_t *);
65872d72231SDave Barach  u32 i, i_max_n_zero, max_n_zeros, i_first_zero, n_zeros, last_double_colon;
659cb9cadadSEd Warnicke
66072d72231SDave Barach  i_max_n_zero = ARRAY_LEN (a->as_u16);
66172d72231SDave Barach  max_n_zeros = 0;
66272d72231SDave Barach  i_first_zero = i_max_n_zero;
66372d72231SDave Barach  n_zeros = 0;
66472d72231SDave Barach  for (i = 0; i < ARRAY_LEN (a->as_u16); i++)
66572d72231SDave Barach    {
66672d72231SDave Barach      u32 is_zero = a->as_u16[i] == 0;
66772d72231SDave Barach      if (is_zero && i_first_zero >= ARRAY_LEN (a->as_u16))
66872d72231SDave Barach	{
66972d72231SDave Barach	  i_first_zero = i;
67072d72231SDave Barach	  n_zeros = 0;
67172d72231SDave Barach	}
67272d72231SDave Barach      n_zeros += is_zero;
67372d72231SDave Barach      if ((!is_zero && n_zeros > max_n_zeros)
67472d72231SDave Barach	  || (i + 1 >= ARRAY_LEN (a->as_u16) && n_zeros > max_n_zeros))
67572d72231SDave Barach	{
67672d72231SDave Barach	  i_max_n_zero = i_first_zero;
67772d72231SDave Barach	  max_n_zeros = n_zeros;
67872d72231SDave Barach	  i_first_zero = ARRAY_LEN (a->as_u16);
67972d72231SDave Barach	  n_zeros = 0;
68072d72231SDave Barach	}
68172d72231SDave Barach    }
682cb9cadadSEd Warnicke
68372d72231SDave Barach  last_double_colon = 0;
68472d72231SDave Barach  for (i = 0; i < ARRAY_LEN (a->as_u16); i++)
68572d72231SDave Barach    {
68672d72231SDave Barach      if (i == i_max_n_zero && max_n_zeros > 1)
68772d72231SDave Barach	{
68872d72231SDave Barach	  s = format (s, "::");
68972d72231SDave Barach	  i += max_n_zeros - 1;
69072d72231SDave Barach	  last_double_colon = 1;
69172d72231SDave Barach	}
69272d72231SDave Barach      else
69372d72231SDave Barach	{
69472d72231SDave Barach	  s = format (s, "%s%x",
69572d72231SDave Barach		      (last_double_colon || i == 0) ? "" : ":",
69672d72231SDave Barach		      clib_net_to_host_u16 (a->as_u16[i]));
69772d72231SDave Barach	  last_double_colon = 0;
69872d72231SDave Barach	}
69972d72231SDave Barach    }
700cb9cadadSEd Warnicke
70172d72231SDave Barach  return s;
702cb9cadadSEd Warnicke}
703cb9cadadSEd Warnicke
70499cb335aSChris Luke/* Format an IP46 address. */
70572d72231SDave Barachu8 *
70672d72231SDave Barachformat_ip46_address (u8 * s, va_list * args)
70799cb335aSChris Luke{
70899cb335aSChris Luke  ip46_address_t *ip46 = va_arg (*args, ip46_address_t *);
70986be4877SDamjan Marion  ip46_type_t type = va_arg (*args, ip46_type_t);
71086be4877SDamjan Marion  int is_ip4 = 1;
71186be4877SDamjan Marion
71286be4877SDamjan Marion  switch (type)
71386be4877SDamjan Marion    {
71472d72231SDave Barach    case IP46_TYPE_ANY:
71572d72231SDave Barach      is_ip4 = ip46_address_is_ip4 (ip46);
71672d72231SDave Barach      break;
71772d72231SDave Barach    case IP46_TYPE_IP4:
71872d72231SDave Barach      is_ip4 = 1;
71972d72231SDave Barach      break;
72072d72231SDave Barach    case IP46_TYPE_IP6:
72172d72231SDave Barach      is_ip4 = 0;
72272d72231SDave Barach      break;
72386be4877SDamjan Marion    }
72486be4877SDamjan Marion
72586be4877SDamjan Marion  return is_ip4 ?
72672d72231SDave Barach    format (s, "%U", format_ip4_address, &ip46->ip4) :
72772d72231SDave Barach    format (s, "%U", format_ip6_address, &ip46->ip6);
72899cb335aSChris Luke}
72999cb335aSChris Luke
73072d72231SDave Barachu8 *
73172d72231SDave Barachformat_ethernet_address (u8 * s, va_list * args)
732cb9cadadSEd Warnicke{
73372d72231SDave Barach  u8 *a = va_arg (*args, u8 *);
734cb9cadadSEd Warnicke
735cb9cadadSEd Warnicke  return format (s, "%02x:%02x:%02x:%02x:%02x:%02x",
73672d72231SDave Barach		 a[0], a[1], a[2], a[3], a[4], a[5]);
737cb9cadadSEd Warnicke}
738fc262a0cSDave Barach#endif
739cb9cadadSEd Warnicke
740fc262a0cSDave Barachstatic void
741097fa66bSNeale Rannsincrement_v4_address (vl_api_ip4_address_t * i)
742cb9cadadSEd Warnicke{
743097fa66bSNeale Ranns  ip4_address_t *a = (ip4_address_t *) i;
74472d72231SDave Barach  u32 v;
745cb9cadadSEd Warnicke
74672d72231SDave Barach  v = ntohl (a->as_u32) + 1;
74772d72231SDave Barach  a->as_u32 = ntohl (v);
748cb9cadadSEd Warnicke}
749cb9cadadSEd Warnicke
7502b5ba950SNeale Rannsstatic void
751097fa66bSNeale Rannsincrement_v6_address (vl_api_ip6_address_t * i)
752cb9cadadSEd Warnicke{
753097fa66bSNeale Ranns  ip6_address_t *a = (ip6_address_t *) i;
75472d72231SDave Barach  u64 v0, v1;
755cb9cadadSEd Warnicke
75672d72231SDave Barach  v0 = clib_net_to_host_u64 (a->as_u64[0]);
75772d72231SDave Barach  v1 = clib_net_to_host_u64 (a->as_u64[1]);
758cb9cadadSEd Warnicke
75972d72231SDave Barach  v1 += 1;
76072d72231SDave Barach  if (v1 == 0)
76172d72231SDave Barach    v0 += 1;
76272d72231SDave Barach  a->as_u64[0] = clib_net_to_host_u64 (v0);
76372d72231SDave Barach  a->as_u64[1] = clib_net_to_host_u64 (v1);
764cb9cadadSEd Warnicke}
765cb9cadadSEd Warnicke
766097fa66bSNeale Rannsstatic void
767097fa66bSNeale Rannsincrement_address (vl_api_address_t * a)
768097fa66bSNeale Ranns{
76954582663SDave Barach  if (a->af == ADDRESS_IP4)
770097fa66bSNeale Ranns    increment_v4_address (&a->un.ip4);
77154582663SDave Barach  else if (a->af == ADDRESS_IP6)
772097fa66bSNeale Ranns    increment_v6_address (&a->un.ip6);
773097fa66bSNeale Ranns}
774097fa66bSNeale Ranns
775097fa66bSNeale Rannsstatic void
776097fa66bSNeale Rannsset_ip4_address (vl_api_address_t * a, u32 v)
777097fa66bSNeale Ranns{
778097fa66bSNeale Ranns  if (a->af == ADDRESS_IP4)
779097fa66bSNeale Ranns    {
780097fa66bSNeale Ranns      ip4_address_t *i = (ip4_address_t *) & a->un.ip4;
781097fa66bSNeale Ranns      i->as_u32 = v;
782097fa66bSNeale Ranns    }
783097fa66bSNeale Ranns}
784097fa66bSNeale Ranns
78523a386b7SJakub Grajciarvoid
78623a386b7SJakub Grajciarip_set (ip46_address_t * dst, void *src, u8 is_ip4)
78723a386b7SJakub Grajciar{
78823a386b7SJakub Grajciar  if (is_ip4)
78923a386b7SJakub Grajciar    dst->ip4.as_u32 = ((ip4_address_t *) src)->as_u32;
79023a386b7SJakub Grajciar  else
79123a386b7SJakub Grajciar    clib_memcpy_fast (&dst->ip6, (ip6_address_t *) src,
79223a386b7SJakub Grajciar		      sizeof (ip6_address_t));
79323a386b7SJakub Grajciar}
79423a386b7SJakub Grajciar
795fc262a0cSDave Barachstatic void
79657938f63SMohsin Kazmiincrement_mac_address (u8 * mac)
79741da02d9SDave Barach{
79857938f63SMohsin Kazmi  u64 tmp = *((u64 *) mac);
79972d72231SDave Barach  tmp = clib_net_to_host_u64 (tmp);
80072d72231SDave Barach  tmp += 1 << 16;		/* skip unused (least significant) octets */
80172d72231SDave Barach  tmp = clib_host_to_net_u64 (tmp);
80257938f63SMohsin Kazmi
80357938f63SMohsin Kazmi  clib_memcpy (mac, &tmp, 6);
80441da02d9SDave Barach}
805cb9cadadSEd Warnicke
806097fa66bSNeale Rannsstatic void
807097fa66bSNeale Rannsvat_json_object_add_address (vat_json_node_t * node,
808097fa66bSNeale Ranns			     const char *str, const vl_api_address_t * addr)
809097fa66bSNeale Ranns{
810097fa66bSNeale Ranns  if (ADDRESS_IP6 == addr->af)
811097fa66bSNeale Ranns    {
812097fa66bSNeale Ranns      struct in6_addr ip6;
813097fa66bSNeale Ranns
814097fa66bSNeale Ranns      clib_memcpy (&ip6, &addr->un.ip6, sizeof (ip6));
815097fa66bSNeale Ranns      vat_json_object_add_ip6 (node, str, ip6);
816097fa66bSNeale Ranns    }
817097fa66bSNeale Ranns  else
818097fa66bSNeale Ranns    {
819097fa66bSNeale Ranns      struct in_addr ip4;
820097fa66bSNeale Ranns
821097fa66bSNeale Ranns      clib_memcpy (&ip4, &addr->un.ip4, sizeof (ip4));
822097fa66bSNeale Ranns      vat_json_object_add_ip4 (node, str, ip4);
823097fa66bSNeale Ranns    }
824097fa66bSNeale Ranns}
825097fa66bSNeale Ranns
826097fa66bSNeale Rannsstatic void
827097fa66bSNeale Rannsvat_json_object_add_prefix (vat_json_node_t * node,
828097fa66bSNeale Ranns			    const vl_api_prefix_t * prefix)
829097fa66bSNeale Ranns{
830ab05508eSPaul Vinciguerra  vat_json_object_add_uint (node, "len", prefix->len);
831ab05508eSPaul Vinciguerra  vat_json_object_add_address (node, "address", &prefix->address);
832097fa66bSNeale Ranns}
833097fa66bSNeale Ranns
83411bfc2f7SHongjun Nistatic void vl_api_create_loopback_reply_t_handler
83572d72231SDave Barach  (vl_api_create_loopback_reply_t * mp)
836cb9cadadSEd Warnicke{
83772d72231SDave Barach  vat_main_t *vam = &vat_main;
83872d72231SDave Barach  i32 retval = ntohl (mp->retval);
839cb9cadadSEd Warnicke
84072d72231SDave Barach  vam->retval = retval;
84172d72231SDave Barach  vam->regenerate_interface_table = 1;
84272d72231SDave Barach  vam->sw_if_index = ntohl (mp->sw_if_index);
84372d72231SDave Barach  vam->result_ready = 1;
844cb9cadadSEd Warnicke}
845cb9cadadSEd Warnicke
846cb9cadadSEd Warnickestatic void vl_api_create_loopback_reply_t_handler_json
84772d72231SDave Barach  (vl_api_create_loopback_reply_t * mp)
848cb9cadadSEd Warnicke{
84972d72231SDave Barach  vat_main_t *vam = &vat_main;
85072d72231SDave Barach  vat_json_node_t node;
851cb9cadadSEd Warnicke
85272d72231SDave Barach  vat_json_init_object (&node);
85372d72231SDave Barach  vat_json_object_add_int (&node, "retval", ntohl (mp->retval));
85472d72231SDave Barach  vat_json_object_add_uint (&node, "sw_if_index", ntohl (mp->sw_if_index));
855cb9cadadSEd Warnicke
85672d72231SDave Barach  vat_json_print (vam->ofp, &node);
85772d72231SDave Barach  vat_json_free (&node);
85872d72231SDave Barach  vam->retval = ntohl (mp->retval);
85972d72231SDave Barach  vam->result_ready = 1;
860802255c9SKeith Burns (alagalah)}
861802255c9SKeith Burns (alagalah)
862c83c3b7fSJon Loeligerstatic void vl_api_create_loopback_instance_reply_t_handler
863c83c3b7fSJon Loeliger  (vl_api_create_loopback_instance_reply_t * mp)
864c83c3b7fSJon Loeliger{
865c83c3b7fSJon Loeliger  vat_main_t *vam = &vat_main;
866c83c3b7fSJon Loeliger  i32 retval = ntohl (mp->retval);
867c83c3b7fSJon Loeliger
868c83c3b7fSJon Loeliger  vam->retval = retval;
869c83c3b7fSJon Loeliger  vam->regenerate_interface_table = 1;
870c83c3b7fSJon Loeliger  vam->sw_if_index = ntohl (mp->sw_if_index);
871c83c3b7fSJon Loeliger  vam->result_ready = 1;
872c83c3b7fSJon Loeliger}
873c83c3b7fSJon Loeliger
874c83c3b7fSJon Loeligerstatic void vl_api_create_loopback_instance_reply_t_handler_json
875c83c3b7fSJon Loeliger  (vl_api_create_loopback_instance_reply_t * mp)
876c83c3b7fSJon Loeliger{
877c83c3b7fSJon Loeliger  vat_main_t *vam = &vat_main;
878c83c3b7fSJon Loeliger  vat_json_node_t node;
879c83c3b7fSJon Loeliger
880c83c3b7fSJon Loeliger  vat_json_init_object (&node);
881c83c3b7fSJon Loeliger  vat_json_object_add_int (&node, "retval", ntohl (mp->retval));
882c83c3b7fSJon Loeliger  vat_json_object_add_uint (&node, "sw_if_index", ntohl (mp->sw_if_index));
883c83c3b7fSJon Loeliger
884c83c3b7fSJon Loeliger  vat_json_print (vam->ofp, &node);
885c83c3b7fSJon Loeliger  vat_json_free (&node);
886c83c3b7fSJon Loeliger  vam->retval = ntohl (mp->retval);
887