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>
18 uword unformat_sw_if_index (unformat_input_t * input, va_list * args)
20 vat_main_t * vam = va_arg (*args, vat_main_t *);
21 u32 * result = va_arg (*args, u32 *);
25 if (!unformat (input, "%s", &if_name))
28 p = hash_get_mem (vam->sw_if_index_by_interface_name, if_name);
35 /* Parse an IP4 address %d.%d.%d.%d. */
36 uword unformat_ip4_address (unformat_input_t * input, va_list * args)
38 u8 * result = va_arg (*args, u8 *);
41 if (! unformat (input, "%d.%d.%d.%d", &a[0], &a[1], &a[2], &a[3]))
44 if (a[0] >= 256 || a[1] >= 256 || a[2] >= 256 || a[3] >= 256)
56 unformat_ethernet_address (unformat_input_t * input, va_list * args)
58 u8 * result = va_arg (*args, u8 *);
61 if (! unformat (input, "%_%x:%x:%x:%x:%x:%x%_",
62 &a[0], &a[1], &a[2], &a[3], &a[4], &a[5]))
66 for (i = 0; i < 6; i++)
70 for (i = 0; i < 6; i++)
76 /* Returns ethernet type as an int in host byte order. */
78 unformat_ethernet_type_host_byte_order (unformat_input_t * input,
81 u16 * result = va_arg (*args, u16 *);
85 if (unformat (input, "0x%x", &type)
86 || unformat (input, "%d", &type))
88 if (type >= (1 << 16))
96 /* Parse an IP6 address. */
97 uword unformat_ip6_address (unformat_input_t * input, va_list * args)
99 ip6_address_t * result = va_arg (*args, ip6_address_t *);
101 uword hex_quad, n_hex_quads, hex_digit, n_hex_digits;
102 uword c, n_colon, double_colon_index;
104 n_hex_quads = hex_quad = n_hex_digits = n_colon = 0;
105 double_colon_index = ARRAY_LEN (hex_quads);
106 while ((c = unformat_get_input (input)) != UNFORMAT_END_OF_INPUT)
109 if (c >= '0' && c <= '9')
111 else if (c >= 'a' && c <= 'f')
112 hex_digit = c + 10 - 'a';
113 else if (c >= 'A' && c <= 'F')
114 hex_digit = c + 10 - 'A';
115 else if (c == ':' && n_colon < 2)
119 unformat_put_input (input);
123 /* Too many hex quads. */
124 if (n_hex_quads >= ARRAY_LEN (hex_quads))
129 hex_quad = (hex_quad << 4) | hex_digit;
131 /* Hex quad must fit in 16 bits. */
132 if (n_hex_digits >= 4)
139 /* Save position of :: */
142 /* More than one :: ? */
143 if (double_colon_index < ARRAY_LEN (hex_quads))
145 double_colon_index = n_hex_quads;
148 if (n_colon > 0 && n_hex_digits > 0)
150 hex_quads[n_hex_quads++] = hex_quad;
156 if (n_hex_digits > 0)
157 hex_quads[n_hex_quads++] = hex_quad;
162 /* Expand :: to appropriate number of zero hex quads. */
163 if (double_colon_index < ARRAY_LEN (hex_quads))
165 word n_zero = ARRAY_LEN (hex_quads) - n_hex_quads;
167 for (i = n_hex_quads - 1; i >= (signed) double_colon_index; i--)
168 hex_quads[n_zero + i] = hex_quads[i];
170 for (i = 0; i < n_zero; i++)
171 hex_quads[double_colon_index + i] = 0;
173 n_hex_quads = ARRAY_LEN (hex_quads);
176 /* Too few hex quads given. */
177 if (n_hex_quads < ARRAY_LEN (hex_quads))
180 for (i = 0; i < ARRAY_LEN (hex_quads); i++)
181 result->as_u16[i] = clib_host_to_net_u16 (hex_quads[i]);
187 u8 * format_ip4_address (u8 * s, va_list * args)
189 u8 * a = va_arg (*args, u8 *);
190 return format (s, "%d.%d.%d.%d", a[0], a[1], a[2], a[3]);
193 u8 * format_ethernet_address (u8 * s, va_list * args)
195 u8 * a = va_arg (*args, u8 *);
197 return format (s, "%02x:%02x:%02x:%02x:%02x:%02x",
198 a[0], a[1], a[2], a[3], a[4], a[5]);
201 void vat_plugin_api_reference(void) { }