2 * Copyright (c) 2019 Cisco and/or its affiliates.
3 * Licensed under the Apache License, Version 2.0 (the "License");
4 * you may not use this file except in compliance with the License.
5 * You may obtain a copy of the License at:
7 * http://www.apache.org/licenses/LICENSE-2.0
9 * Unless required by applicable law or agreed to in writing, software
10 * distributed under the License is distributed on an "AS IS" BASIS,
11 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 * See the License for the specific language governing permissions and
13 * limitations under the License.
19 #include <vnet/session/application_interface.h>
23 /* VNET_API_ERROR_FOO -> "Foo" hash table */
24 uword *error_string_by_error_number;
27 echo_common_main_t echo_common_main;
30 format_ip4_address (u8 * s, va_list * args)
32 u8 *a = va_arg (*args, u8 *);
33 return format (s, "%d.%d.%d.%d", a[0], a[1], a[2], a[3]);
37 format_ip6_address (u8 * s, va_list * args)
39 ip6_address_t *a = va_arg (*args, ip6_address_t *);
40 u32 i, i_max_n_zero, max_n_zeros, i_first_zero, n_zeros, last_double_colon;
42 i_max_n_zero = ARRAY_LEN (a->as_u16);
44 i_first_zero = i_max_n_zero;
46 for (i = 0; i < ARRAY_LEN (a->as_u16); i++)
48 u32 is_zero = a->as_u16[i] == 0;
49 if (is_zero && i_first_zero >= ARRAY_LEN (a->as_u16))
55 if ((!is_zero && n_zeros > max_n_zeros)
56 || (i + 1 >= ARRAY_LEN (a->as_u16) && n_zeros > max_n_zeros))
58 i_max_n_zero = i_first_zero;
59 max_n_zeros = n_zeros;
60 i_first_zero = ARRAY_LEN (a->as_u16);
65 last_double_colon = 0;
66 for (i = 0; i < ARRAY_LEN (a->as_u16); i++)
68 if (i == i_max_n_zero && max_n_zeros > 1)
72 last_double_colon = 1;
76 s = format (s, "%s%x",
77 (last_double_colon || i == 0) ? "" : ":",
78 clib_net_to_host_u16 (a->as_u16[i]));
79 last_double_colon = 0;
86 /* Format an IP46 address. */
88 format_ip46_address (u8 * s, va_list * args)
90 ip46_address_t *ip46 = va_arg (*args, ip46_address_t *);
91 ip46_type_t type = va_arg (*args, ip46_type_t);
97 is_ip4 = ip46_address_is_ip4 (ip46);
108 format (s, "%U", format_ip4_address, &ip46->ip4) :
109 format (s, "%U", format_ip6_address, &ip46->ip6);
113 unformat_data (unformat_input_t * input, va_list * args)
116 u64 *a = va_arg (*args, u64 *);
117 if (unformat (input, "%lluGb", &_a))
119 else if (unformat (input, "%lluMb", &_a))
121 else if (unformat (input, "%lluKb", &_a))
123 else if (unformat (input, "%llu", a))
131 format_api_error (u8 * s, va_list * args)
133 echo_common_main_t *ecm = &echo_common_main;
134 i32 error = va_arg (*args, u32);
137 p = hash_get (ecm->error_string_by_error_number, -error);
140 s = format (s, "%s", p[0]);
142 s = format (s, "%d", error);
147 init_error_string_table ()
149 echo_common_main_t *ecm = &echo_common_main;
150 ecm->error_string_by_error_number = hash_create (0, sizeof (uword));
152 #define _(n,v,s) hash_set (ecm->error_string_by_error_number, -v, s);
153 foreach_vnet_api_error;
156 hash_set (ecm->error_string_by_error_number, 99, "Misc");