FD.io VPP  v20.09-64-g4f7b92f0a
Vector Packet Processing
types.c
Go to the documentation of this file.
1 /*
2  *------------------------------------------------------------------
3  * Copyright (c) 2018 Cisco and/or its affiliates.
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at:
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  *------------------------------------------------------------------
16  */
17 
18 #include <vlib/vlib.h>
19 #include <vpp/api/types.h>
20 #include <vat/vat.h>
21 
22 const vl_api_mac_address_t VL_API_ZERO_MAC_ADDRESS;
23 const vl_api_address_t VL_API_ZERO_ADDRESS;
24 
25 u8 *
26 format_vl_api_address_family (u8 * s, va_list * args)
27 {
28  vl_api_address_family_t af = va_arg (*args, int);
29 
30  if (ADDRESS_IP6 == af)
31  s = format (s, "ip4");
32  else
33  s = format (s, "ip6");
34 
35  return s;
36 }
37 
38 u8 *
39 format_vl_api_address (u8 * s, va_list * args)
40 {
41  const vl_api_address_t *addr = va_arg (*args, vl_api_address_t *);
42 
43  if (ADDRESS_IP6 == addr->af)
44  s = format (s, "%U", format_ip6_address, addr->un.ip6);
45  else
46  s = format (s, "%U", format_ip4_address, addr->un.ip4);
47 
48  return s;
49 }
50 
51 u8 *
52 format_vl_api_address_union (u8 * s, va_list * args)
53 {
54  const vl_api_address_union_t *addr =
55  va_arg (*args, vl_api_address_union_t *);
56  vl_api_address_family_t af = va_arg (*args, int);
57 
58  if (ADDRESS_IP6 == af)
59  s = format (s, "%U", format_ip6_address, addr->ip6);
60  else
61  s = format (s, "%U", format_ip4_address, addr->ip4);
62 
63  return s;
64 }
65 
66 u8 *
67 format_vl_api_ip4_address (u8 * s, va_list * args)
68 {
69  const vl_api_ip4_address_t *addr = va_arg (*args, vl_api_ip4_address_t *);
70 
71  s = format (s, "%U", format_ip4_address, addr);
72 
73  return s;
74 }
75 
76 u8 *
77 format_vl_api_ip6_address (u8 * s, va_list * args)
78 {
79  const vl_api_ip6_address_t *addr = va_arg (*args, vl_api_ip6_address_t *);
80 
81  s = format (s, "%U", format_ip6_address, addr);
82 
83  return s;
84 }
85 
86 u8 *
87 format_vl_api_prefix (u8 * s, va_list * args)
88 {
89  const vl_api_prefix_t *pfx = va_arg (*args, vl_api_prefix_t *);
90 
91  s = format (s, "%U/%d", format_vl_api_address,
92  &pfx->address, pfx->len);
93 
94  return s;
95 }
96 
97 u8 *
98 format_vl_api_mac_address (u8 * s, va_list * args)
99 {
100  vl_api_mac_address_t *mac = va_arg (*args, vl_api_mac_address_t *);
101 
102  return (format (s, "%U", format_ethernet_address, mac));
103 }
104 
105 u8 *
106 format_vl_api_version (u8 * s, va_list * args)
107 {
108  vl_api_version_t *ver = va_arg (*args, vl_api_version_t *);
109  s = format(s, "%d.%d.%d", ver->major, ver->minor, ver->patch);
110  if (ver->pre_release[0] != 0)
111  {
112  s = format(s, "-%v", ver->pre_release);
113  if (ver->build_metadata[0] != 0)
114  s = format(s, "+%v", ver->build_metadata);
115  }
116  return s;
117 }
118 
119 uword
121 {
122  vl_api_mac_address_t *mac = va_arg (*args, vl_api_mac_address_t *);
123 
124  return (unformat (input, "%U",unformat_ethernet_address, mac));
125 }
126 
127 uword
128 unformat_vl_api_address (unformat_input_t * input, va_list * args)
129 {
130  vl_api_address_t *ip = va_arg (*args, vl_api_address_t *);
131 
132  if (unformat (input, "%U", unformat_ip4_address, &ip->un.ip4))
133  ip->af = ADDRESS_IP4;
134  else if (unformat (input, "%U", unformat_ip6_address, &ip->un.ip6))
135  ip->af = ADDRESS_IP6;
136  else
137  return (0);
138 
139  return (1);
140 }
141 
142 uword
144  va_list * args)
145 {
146  vl_api_address_family_t *af = va_arg (*args, vl_api_address_family_t *);
147 
148  if (unformat (input, "ip4") || unformat (input, "ipv4"))
149  *af = ADDRESS_IP4;
150  else if (unformat (input, "ip6") || unformat (input, "ipv6"))
151  *af = ADDRESS_IP6;
152  else
153  return (0);
154 
155  return (1);
156 }
157 
158 uword
160 {
161  vl_api_ip4_address_t *ip = va_arg (*args, vl_api_ip4_address_t *);
162 
163  if (unformat (input, "%U", unformat_ip4_address, ip))
164  return (1);
165  return (0);
166 }
167 
168 uword
170 {
171  vl_api_ip6_address_t *ip = va_arg (*args, vl_api_ip6_address_t *);
172 
173  if (unformat (input, "%U", unformat_ip6_address, ip))
174  return (1);
175  return (0);
176 }
177 
178 uword
179 unformat_vl_api_prefix (unformat_input_t * input, va_list * args)
180 {
181  vl_api_prefix_t *pfx = va_arg (*args, vl_api_prefix_t *);
182 
183  if (unformat (input, "%U/%d", unformat_vl_api_address, &pfx->address,
184  &pfx->len))
185  return (1);
186  return (0);
187 }
188 
189 uword
190 unformat_vl_api_mprefix (unformat_input_t * input, va_list * args)
191 {
192  vl_api_mprefix_t *pfx = va_arg (*args, vl_api_mprefix_t *);
193 
194  if (unformat (input, "%U/%d",
195  unformat_vl_api_ip4_address, &pfx->grp_address.ip4,
196  &pfx->grp_address_length))
197  pfx->af = ADDRESS_IP4;
198  else if (unformat (input, "%U/%d",
199  unformat_vl_api_ip6_address, &pfx->grp_address.ip6,
200  &pfx->grp_address_length))
201  pfx->af = ADDRESS_IP6;
202  else if (unformat (input, "%U %U",
203  unformat_vl_api_ip4_address, &pfx->src_address.ip4,
204  unformat_vl_api_ip4_address, &pfx->grp_address.ip4))
205  {
206  pfx->af = ADDRESS_IP4;
207  pfx->grp_address_length = 64;
208  }
209  else if (unformat (input, "%U %U",
210  unformat_vl_api_ip6_address, &pfx->src_address.ip6,
211  unformat_vl_api_ip6_address, &pfx->grp_address.ip6))
212  {
213  pfx->af = ADDRESS_IP6;
214  pfx->grp_address_length = 256;
215  }
216  else if (unformat (input, "%U",
217  unformat_vl_api_ip4_address, &pfx->grp_address.ip4))
218  {
219  pfx->af = ADDRESS_IP4;
220  pfx->grp_address_length = 32;
221  clib_memset(&pfx->src_address, 0, sizeof(pfx->src_address));
222  }
223  else if (unformat (input, "%U",
224  unformat_vl_api_ip6_address, &pfx->grp_address.ip6))
225  {
226  pfx->af = ADDRESS_IP6;
227  pfx->grp_address_length = 128;
228  clib_memset(&pfx->src_address, 0, sizeof(pfx->src_address));
229  }
230  else
231  return (0);
232 
233  return (1);
234 }
235 
237 {
238 vl_api_version_t *ver = va_arg (*args, vl_api_version_t *);
239 
240 if (unformat (input, "%d.%d.%d-%s+%s", ver->major, ver->minor, ver->patch, ver->pre_release, ver->build_metadata
241  ))
242  return (1);
243 else if (unformat (input, "%d.%d.%d-%s", ver->major, ver->minor, ver->patch, ver->pre_release
244  ))
245  return (1);
246 else if (unformat (input, "%d.%d.%d", ver->major, ver->minor, ver->patch
247  ))
248  return (1);
249 
250  return (0);
251 }
uword unformat_vl_api_ip4_address(unformat_input_t *input, va_list *args)
Definition: types.c:159
u8 * format_vl_api_mac_address(u8 *s, va_list *args)
Definition: types.c:98
vl_api_mac_address_t mac
Definition: l2.api:502
clib_memset(h->entries, 0, sizeof(h->entries[0]) *entries)
u8 * format_vl_api_address(u8 *s, va_list *args)
Definition: types.c:39
uword unformat_vl_api_ip6_address(unformat_input_t *input, va_list *args)
Definition: types.c:169
u8 * format(u8 *s, const char *fmt,...)
Definition: format.c:424
vhost_vring_addr_t addr
Definition: vhost_user.h:111
u8 * format_vl_api_ip6_address(u8 *s, va_list *args)
Definition: types.c:77
unsigned char u8
Definition: types.h:56
format_function_t format_ip4_address
Definition: format.h:73
u8 * format_ethernet_address(u8 *s, va_list *args)
Definition: format.c:44
unformat_function_t unformat_ip4_address
Definition: format.h:68
u8 * format_vl_api_address_family(u8 *s, va_list *args)
Definition: types.c:26
u8 * format_vl_api_prefix(u8 *s, va_list *args)
Definition: types.c:87
struct _unformat_input_t unformat_input_t
u8 * format_vl_api_version(u8 *s, va_list *args)
Definition: types.c:106
uword unformat_vl_api_prefix(unformat_input_t *input, va_list *args)
Definition: types.c:179
unformat_function_t unformat_ip6_address
Definition: format.h:89
format_function_t format_ip6_address
Definition: format.h:91
const vl_api_mac_address_t VL_API_ZERO_MAC_ADDRESS
Definition: types.c:22
uword unformat_ethernet_address(unformat_input_t *input, va_list *args)
Definition: format.c:233
u8 * format_vl_api_ip4_address(u8 *s, va_list *args)
Definition: types.c:67
uword unformat_vl_api_address(unformat_input_t *input, va_list *args)
Definition: types.c:128
uword unformat_vl_api_mac_address(unformat_input_t *input, va_list *args)
Definition: types.c:120
uword unformat_vl_api_mprefix(unformat_input_t *input, va_list *args)
Definition: types.c:190
vl_api_address_t ip
Definition: l2.api:501
u64 uword
Definition: types.h:112
uword unformat_vl_api_version(unformat_input_t *input, va_list *args)
Definition: types.c:236
const vl_api_address_t VL_API_ZERO_ADDRESS
Definition: types.c:23
uword unformat_vl_api_address_family(unformat_input_t *input, va_list *args)
Definition: types.c:143
uword unformat(unformat_input_t *i, const char *fmt,...)
Definition: unformat.c:978
u8 * format_vl_api_address_union(u8 *s, va_list *args)
Definition: types.c:52