2 * Copyright (c) 2015 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.
16 #include <vnet/ip/ip.h>
19 unformat_sw_if_index (unformat_input_t * input, va_list * args)
21 vat_main_t *vam = va_arg (*args, vat_main_t *);
22 u32 *result = va_arg (*args, u32 *);
26 if (!unformat (input, "%s", &if_name))
29 p = hash_get_mem (vam->sw_if_index_by_interface_name, if_name);
36 /* Parse an IP4 address %d.%d.%d.%d. */
38 unformat_ip4_address (unformat_input_t * input, va_list * args)
40 u8 *result = va_arg (*args, u8 *);
43 if (!unformat (input, "%d.%d.%d.%d", &a[0], &a[1], &a[2], &a[3]))
46 if (a[0] >= 256 || a[1] >= 256 || a[2] >= 256 || a[3] >= 256)
58 unformat_ethernet_address (unformat_input_t * input, va_list * args)
60 u8 *result = va_arg (*args, u8 *);
63 if (!unformat (input, "%_%x:%x:%x:%x:%x:%x%_",
64 &a[0], &a[1], &a[2], &a[3], &a[4], &a[5]))
68 for (i = 0; i < 6; i++)
72 for (i = 0; i < 6; i++)
78 /* Returns ethernet type as an int in host byte order. */
80 unformat_ethernet_type_host_byte_order (unformat_input_t * input,
83 u16 *result = va_arg (*args, u16 *);
87 if (unformat (input, "0x%x", &type) || unformat (input, "%d", &type))
89 if (type >= (1 << 16))
97 /* Parse an IP6 address. */
99 unformat_ip6_address (unformat_input_t * input, va_list * args)
101 ip6_address_t *result = va_arg (*args, ip6_address_t *);
103 uword hex_quad, n_hex_quads, hex_digit, n_hex_digits;
104 uword c, n_colon, double_colon_index;
106 n_hex_quads = hex_quad = n_hex_digits = n_colon = 0;
107 double_colon_index = ARRAY_LEN (hex_quads);
108 while ((c = unformat_get_input (input)) != UNFORMAT_END_OF_INPUT)
111 if (c >= '0' && c <= '9')
113 else if (c >= 'a' && c <= 'f')
114 hex_digit = c + 10 - 'a';
115 else if (c >= 'A' && c <= 'F')
116 hex_digit = c + 10 - 'A';
117 else if (c == ':' && n_colon < 2)
121 unformat_put_input (input);
125 /* Too many hex quads. */
126 if (n_hex_quads >= ARRAY_LEN (hex_quads))
131 hex_quad = (hex_quad << 4) | hex_digit;
133 /* Hex quad must fit in 16 bits. */
134 if (n_hex_digits >= 4)
141 /* Save position of :: */
144 /* More than one :: ? */
145 if (double_colon_index < ARRAY_LEN (hex_quads))
147 double_colon_index = n_hex_quads;
150 if (n_colon > 0 && n_hex_digits > 0)
152 hex_quads[n_hex_quads++] = hex_quad;
158 if (n_hex_digits > 0)
159 hex_quads[n_hex_quads++] = hex_quad;
164 /* Expand :: to appropriate number of zero hex quads. */
165 if (double_colon_index < ARRAY_LEN (hex_quads))
167 word n_zero = ARRAY_LEN (hex_quads) - n_hex_quads;
169 for (i = n_hex_quads - 1; i >= (signed) double_colon_index; i--)
170 hex_quads[n_zero + i] = hex_quads[i];
172 for (i = 0; i < n_zero; i++)
173 hex_quads[double_colon_index + i] = 0;
175 n_hex_quads = ARRAY_LEN (hex_quads);
178 /* Too few hex quads given. */
179 if (n_hex_quads < ARRAY_LEN (hex_quads))
182 for (i = 0; i < ARRAY_LEN (hex_quads); i++)
183 result->as_u16[i] = clib_host_to_net_u16 (hex_quads[i]);
190 format_ip4_address (u8 * s, va_list * args)
192 u8 *a = va_arg (*args, u8 *);
193 return format (s, "%d.%d.%d.%d", a[0], a[1], a[2], a[3]);
197 format_ip6_address (u8 * s, va_list * args)
199 ip6_address_t *a = va_arg (*args, ip6_address_t *);
200 u32 i, i_max_n_zero, max_n_zeros, i_first_zero, n_zeros, last_double_colon;
202 i_max_n_zero = ARRAY_LEN (a->as_u16);
204 i_first_zero = i_max_n_zero;
206 for (i = 0; i < ARRAY_LEN (a->as_u16); i++)
208 u32 is_zero = a->as_u16[i] == 0;
209 if (is_zero && i_first_zero >= ARRAY_LEN (a->as_u16))
215 if ((!is_zero && n_zeros > max_n_zeros)
216 || (i + 1 >= ARRAY_LEN (a->as_u16) && n_zeros > max_n_zeros))
218 i_max_n_zero = i_first_zero;
219 max_n_zeros = n_zeros;
220 i_first_zero = ARRAY_LEN (a->as_u16);
225 last_double_colon = 0;
226 for (i = 0; i < ARRAY_LEN (a->as_u16); i++)
228 if (i == i_max_n_zero && max_n_zeros > 1)
230 s = format (s, "::");
231 i += max_n_zeros - 1;
232 last_double_colon = 1;
236 s = format (s, "%s%x",
237 (last_double_colon || i == 0) ? "" : ":",
238 clib_net_to_host_u16 (a->as_u16[i]));
239 last_double_colon = 0;
246 /* Format an IP46 address. */
248 format_ip46_address (u8 * s, va_list * args)
250 ip46_address_t *ip46 = va_arg (*args, ip46_address_t *);
251 ip46_type_t type = va_arg (*args, ip46_type_t);
257 is_ip4 = ip46_address_is_ip4 (ip46);
268 format (s, "%U", format_ip4_address, &ip46->ip4) :
269 format (s, "%U", format_ip6_address, &ip46->ip6);
273 format_ethernet_address (u8 * s, va_list * args)
275 u8 *a = va_arg (*args, u8 *);
277 return format (s, "%02x:%02x:%02x:%02x:%02x:%02x",
278 a[0], a[1], a[2], a[3], a[4], a[5]);
282 vat_plugin_api_reference (void)
287 * fd.io coding-style-patch-verification: ON
290 * eval: (c-set-style "gnu")