blob: 4e1eb897844b04c26671eca26aa15239053d2c6d [file] [log] [blame]
Ed Warnickecb9cada2015-12-08 15:45:58 -07001/*
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:
6 *
7 * http://www.apache.org/licenses/LICENSE-2.0
8 *
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.
14 */
15#include <vat/vat.h>
16#include <vnet/ip/ip.h>
17
Dave Barach72d72232016-08-04 10:15:08 -040018uword
19unformat_sw_if_index (unformat_input_t * input, va_list * args)
Ed Warnickecb9cada2015-12-08 15:45:58 -070020{
Dave Barach72d72232016-08-04 10:15:08 -040021 vat_main_t *vam = va_arg (*args, vat_main_t *);
22 u32 *result = va_arg (*args, u32 *);
23 u8 *if_name;
24 uword *p;
Ed Warnickecb9cada2015-12-08 15:45:58 -070025
26 if (!unformat (input, "%s", &if_name))
Dave Barach72d72232016-08-04 10:15:08 -040027 return 0;
Ed Warnickecb9cada2015-12-08 15:45:58 -070028
29 p = hash_get_mem (vam->sw_if_index_by_interface_name, if_name);
30 if (p == 0)
Dave Barach72d72232016-08-04 10:15:08 -040031 return 0;
Ed Warnickecb9cada2015-12-08 15:45:58 -070032 *result = p[0];
33 return 1;
34}
35
36/* Parse an IP4 address %d.%d.%d.%d. */
Dave Barach72d72232016-08-04 10:15:08 -040037uword
38unformat_ip4_address (unformat_input_t * input, va_list * args)
Ed Warnickecb9cada2015-12-08 15:45:58 -070039{
Dave Barach72d72232016-08-04 10:15:08 -040040 u8 *result = va_arg (*args, u8 *);
Ed Warnickecb9cada2015-12-08 15:45:58 -070041 unsigned a[4];
42
Dave Barach72d72232016-08-04 10:15:08 -040043 if (!unformat (input, "%d.%d.%d.%d", &a[0], &a[1], &a[2], &a[3]))
Ed Warnickecb9cada2015-12-08 15:45:58 -070044 return 0;
45
46 if (a[0] >= 256 || a[1] >= 256 || a[2] >= 256 || a[3] >= 256)
47 return 0;
48
49 result[0] = a[0];
50 result[1] = a[1];
51 result[2] = a[2];
52 result[3] = a[3];
53
54 return 1;
55}
56
57uword
58unformat_ethernet_address (unformat_input_t * input, va_list * args)
59{
Dave Barach72d72232016-08-04 10:15:08 -040060 u8 *result = va_arg (*args, u8 *);
Ed Warnickecb9cada2015-12-08 15:45:58 -070061 u32 i, a[6];
62
Dave Barach72d72232016-08-04 10:15:08 -040063 if (!unformat (input, "%_%x:%x:%x:%x:%x:%x%_",
64 &a[0], &a[1], &a[2], &a[3], &a[4], &a[5]))
Ed Warnickecb9cada2015-12-08 15:45:58 -070065 return 0;
66
67 /* Check range. */
68 for (i = 0; i < 6; i++)
69 if (a[i] >= (1 << 8))
70 return 0;
71
72 for (i = 0; i < 6; i++)
73 result[i] = a[i];
74
75 return 1;
76}
77
78/* Returns ethernet type as an int in host byte order. */
79uword
80unformat_ethernet_type_host_byte_order (unformat_input_t * input,
81 va_list * args)
82{
Dave Barach72d72232016-08-04 10:15:08 -040083 u16 *result = va_arg (*args, u16 *);
Ed Warnickecb9cada2015-12-08 15:45:58 -070084 int type;
85
86 /* Numeric type. */
Dave Barach72d72232016-08-04 10:15:08 -040087 if (unformat (input, "0x%x", &type) || unformat (input, "%d", &type))
Ed Warnickecb9cada2015-12-08 15:45:58 -070088 {
89 if (type >= (1 << 16))
90 return 0;
91 *result = type;
92 return 1;
93 }
94 return 0;
95}
96
97/* Parse an IP6 address. */
Dave Barach72d72232016-08-04 10:15:08 -040098uword
99unformat_ip6_address (unformat_input_t * input, va_list * args)
Ed Warnickecb9cada2015-12-08 15:45:58 -0700100{
Dave Barach72d72232016-08-04 10:15:08 -0400101 ip6_address_t *result = va_arg (*args, ip6_address_t *);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700102 u16 hex_quads[8];
103 uword hex_quad, n_hex_quads, hex_digit, n_hex_digits;
104 uword c, n_colon, double_colon_index;
105
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)
109 {
110 hex_digit = 16;
111 if (c >= '0' && c <= '9')
112 hex_digit = c - '0';
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)
118 n_colon++;
119 else
120 {
121 unformat_put_input (input);
122 break;
123 }
124
125 /* Too many hex quads. */
126 if (n_hex_quads >= ARRAY_LEN (hex_quads))
127 return 0;
128
129 if (hex_digit < 16)
130 {
131 hex_quad = (hex_quad << 4) | hex_digit;
132
133 /* Hex quad must fit in 16 bits. */
134 if (n_hex_digits >= 4)
135 return 0;
136
137 n_colon = 0;
138 n_hex_digits++;
139 }
Dave Barach72d72232016-08-04 10:15:08 -0400140
Ed Warnickecb9cada2015-12-08 15:45:58 -0700141 /* Save position of :: */
142 if (n_colon == 2)
143 {
144 /* More than one :: ? */
145 if (double_colon_index < ARRAY_LEN (hex_quads))
146 return 0;
147 double_colon_index = n_hex_quads;
148 }
149
150 if (n_colon > 0 && n_hex_digits > 0)
151 {
152 hex_quads[n_hex_quads++] = hex_quad;
153 hex_quad = 0;
154 n_hex_digits = 0;
155 }
156 }
157
158 if (n_hex_digits > 0)
159 hex_quads[n_hex_quads++] = hex_quad;
160
161 {
162 word i;
163
164 /* Expand :: to appropriate number of zero hex quads. */
165 if (double_colon_index < ARRAY_LEN (hex_quads))
166 {
167 word n_zero = ARRAY_LEN (hex_quads) - n_hex_quads;
168
169 for (i = n_hex_quads - 1; i >= (signed) double_colon_index; i--)
170 hex_quads[n_zero + i] = hex_quads[i];
171
172 for (i = 0; i < n_zero; i++)
173 hex_quads[double_colon_index + i] = 0;
174
175 n_hex_quads = ARRAY_LEN (hex_quads);
176 }
177
178 /* Too few hex quads given. */
179 if (n_hex_quads < ARRAY_LEN (hex_quads))
180 return 0;
181
182 for (i = 0; i < ARRAY_LEN (hex_quads); i++)
183 result->as_u16[i] = clib_host_to_net_u16 (hex_quads[i]);
184
185 return 1;
186 }
187}
188
Dave Barach72d72232016-08-04 10:15:08 -0400189u8 *
190format_ip4_address (u8 * s, va_list * args)
Ed Warnickecb9cada2015-12-08 15:45:58 -0700191{
Dave Barach72d72232016-08-04 10:15:08 -0400192 u8 *a = va_arg (*args, u8 *);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700193 return format (s, "%d.%d.%d.%d", a[0], a[1], a[2], a[3]);
194}
195
Dave Barach72d72232016-08-04 10:15:08 -0400196u8 *
197format_ethernet_address (u8 * s, va_list * args)
Ed Warnickecb9cada2015-12-08 15:45:58 -0700198{
Dave Barach72d72232016-08-04 10:15:08 -0400199 u8 *a = va_arg (*args, u8 *);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700200
201 return format (s, "%02x:%02x:%02x:%02x:%02x:%02x",
Dave Barach72d72232016-08-04 10:15:08 -0400202 a[0], a[1], a[2], a[3], a[4], a[5]);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700203}
204
Dave Barach72d72232016-08-04 10:15:08 -0400205void
206vat_plugin_api_reference (void)
207{
208}
209
210/*
211 * fd.io coding-style-patch-verification: ON
212 *
213 * Local Variables:
214 * eval: (c-set-style "gnu")
215 * End:
216 */