FD.io VPP  v21.06-3-gbb25fbf28
Vector Packet Processing
ip_types_api.c
Go to the documentation of this file.
1 /*
2  * Copyright (c) 2018 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 
16 #include <vlibapi/api_types.h>
17 #include <vnet/ip/ip_types_api.h>
18 
19 #define vl_typedefs /* define message structures */
20 #include <vnet/vnet_all_api_h.h>
21 #undef vl_typedefs
22 
23 #define vl_endianfun /* define message structures */
24 #include <vnet/vnet_all_api_h.h>
25 #undef vl_endianfun
26 
27 /* instantiate all the print functions we know about */
28 #define vl_print(handle, ...) vlib_cli_output (handle, __VA_ARGS__)
29 #define vl_printfun
30 #include <vnet/vnet_all_api_h.h>
31 #undef vl_printfun
32 
33 int
34 ip_address_family_decode (vl_api_address_family_t af,
35  ip_address_family_t * out)
36 {
37  switch (af)
38  {
39  case ADDRESS_IP4:
40  *out = AF_IP4;
41  return (0);
42  case ADDRESS_IP6:
43  *out = AF_IP6;
44  return (0);
45  }
46  return (VNET_API_ERROR_INVALID_ADDRESS_FAMILY);
47 }
48 
49 vl_api_address_family_t
51 {
52  switch (af)
53  {
54  case AF_IP4:
55  return (ADDRESS_IP4);
56  case AF_IP6:
57  return (ADDRESS_IP6);
58  }
59 
60  ASSERT (0);
61  return (ADDRESS_IP4);
62 }
63 
64 int
65 ip_proto_decode (vl_api_ip_proto_t ipp, ip_protocol_t * out)
66 {
67  /* Not all protocol are defined in vl_api_ip_proto_t
68  * so we must cast to a different type.
69  */
70  switch ((u8) ipp)
71  {
72 #define ip_protocol(n,s) \
73  case IP_PROTOCOL_##s: \
74  *out = IP_PROTOCOL_##s; \
75  return (0);
76 #include "protocols.def"
77 #undef ip_protocol
78  }
79  return (VNET_API_ERROR_INVALID_PROTOCOL);
80 }
81 
82 vl_api_ip_proto_t
84 {
85  switch (ipp)
86  {
87 #define ip_protocol(n,s) \
88  case IP_PROTOCOL_##s: \
89  return ((vl_api_ip_proto_t) IP_PROTOCOL_##s);
90 #include "protocols.def"
91 #undef ip_protocol
92  }
93 
94  ASSERT (0);
95  return (IP_API_PROTO_TCP);
96 }
97 
99 ip_dscp_decode (vl_api_ip_dscp_t in)
100 {
101  return ((ip_dscp_t) in);
102 }
103 
104 vl_api_ip_dscp_t
106 {
107  return ((vl_api_ip_dscp_t) dscp);
108 }
109 
110 int
111 ip_feature_location_decode (vl_api_ip_feature_location_t loc,
112  ip_feature_location_t * out)
113 {
114  /* Not all feature_locationcol are defined in vl_api_ip_feature_location_t
115  * so we must cast to a different type.
116  */
117  switch (loc)
118  {
119 #define _(n,s) \
120  case IP_API_FEATURE_##n: \
121  *out = IP_FEATURE_##n; \
122  return (0);
124 #undef _
125  }
126  return (VNET_API_ERROR_FEATURE_DISABLED);
127 }
128 
129 vl_api_ip_feature_location_t
131 {
132  return ((vl_api_ip_feature_location_t) (loc));
133 }
134 
135 
136 void
137 ip6_address_encode (const ip6_address_t * in, vl_api_ip6_address_t out)
138 {
139  clib_memcpy (out, in, sizeof (*in));
140 }
141 
142 void
143 ip6_address_decode (const vl_api_ip6_address_t in, ip6_address_t * out)
144 {
145  clib_memcpy (out, in, sizeof (*out));
146 }
147 
148 void
149 ip4_address_encode (const ip4_address_t * in, vl_api_ip4_address_t out)
150 {
151  clib_memcpy (out, in, sizeof (*in));
152 }
153 
154 void
155 ip4_address_decode (const vl_api_ip4_address_t in, ip4_address_t * out)
156 {
157  clib_memcpy (out, in, sizeof (*out));
158 }
159 
160 static ip46_type_t
161 ip_address_union_decode (const vl_api_address_union_t * in,
162  vl_api_address_family_t af, ip46_address_t * out)
163 {
165 
166  switch (af)
167  {
168  case ADDRESS_IP4:
169  clib_memset (out, 0, sizeof (*out));
170  clib_memcpy (&out->ip4, &in->ip4, sizeof (out->ip4));
172  break;
173  case ADDRESS_IP6:
174  clib_memcpy (&out->ip6, &in->ip6, sizeof (out->ip6));
176  break;
177  default:
179  break;
180  }
181 
182  return type;
183 }
184 
186 ip_address_decode (const vl_api_address_t * in, ip46_address_t * out)
187 {
188  return (ip_address_union_decode (&in->un, in->af, out));
189 }
190 
191 void
192 ip_address_decode2 (const vl_api_address_t * in, ip_address_t * out)
193 {
194  switch (ip_address_union_decode (&in->un, in->af, &out->ip))
195  {
196  case IP46_TYPE_IP4:
197  out->version = AF_IP4;
198  break;
199  case IP46_TYPE_IP6:
200  out->version = AF_IP6;
201  break;
202  default:
203  ;
204  break;
205  }
206 }
207 
208 static void
209 ip_address_union_encode (const ip46_address_t * in,
210  vl_api_address_family_t af,
211  vl_api_address_union_t * out)
212 {
213  if (ADDRESS_IP6 == af)
214  ip6_address_encode (&in->ip6, out->ip6);
215  else
216  ip4_address_encode (&in->ip4, out->ip4);
217 }
218 
219 void
220 ip_address_encode (const ip46_address_t * in,
221  ip46_type_t type, vl_api_address_t * out)
222 {
223  switch (type)
224  {
225  case IP46_TYPE_IP4:
226  out->af = ADDRESS_IP4;
227  break;
228  case IP46_TYPE_IP6:
229  out->af = ADDRESS_IP6;
230  break;
231  case IP46_TYPE_ANY:
232  if (ip46_address_is_ip4 (in))
233  out->af = ADDRESS_IP4;
234  else
235  out->af = ADDRESS_IP6;
236  break;
237  }
238  ip_address_union_encode (in, out->af, &out->un);
239 }
240 
241 void
242 ip_address_encode2 (const ip_address_t * in, vl_api_address_t * out)
243 {
244  switch (in->version)
245  {
246  case AF_IP4:
247  out->af = ADDRESS_IP4;
248  ip4_address_encode (&in->ip.ip4, out->un.ip4);
249  break;
250  case AF_IP6:
251  out->af = ADDRESS_IP6;
252  ip6_address_encode (&in->ip.ip6, out->un.ip6);
253  break;
254  }
255  ip_address_union_encode (&in->ip, out->af, &out->un);
256 }
257 
258 void
259 ip_prefix_decode (const vl_api_prefix_t * in, fib_prefix_t * out)
260 {
261  switch (in->address.af)
262  {
263  case ADDRESS_IP4:
264  out->fp_proto = FIB_PROTOCOL_IP4;
265  break;
266  case ADDRESS_IP6:
267  out->fp_proto = FIB_PROTOCOL_IP6;
268  break;
269  }
270  out->fp_len = in->len;
271  out->___fp___pad = 0;
272  ip_address_decode (&in->address, &out->fp_addr);
273 }
274 
275 int
276 ip_prefix_decode2 (const vl_api_prefix_t * in, ip_prefix_t * out)
277 {
278  out->len = in->len;
279  ip_address_decode2 (&in->address, &out->addr);
280 
281  if (!ip_prefix_validate (out))
282  return (VNET_API_ERROR_IP_PREFIX_INVALID);
283  return (0);
284 }
285 
286 void
287 ip_prefix_encode (const fib_prefix_t * in, vl_api_prefix_t * out)
288 {
289  out->len = in->fp_len;
290  ip46_type_t ip46_type;
291 
292  switch (in->fp_proto)
293  {
294  case FIB_PROTOCOL_IP4:
295  ip46_type = (IP46_TYPE_IP4);
296  break;
297  case FIB_PROTOCOL_IP6:
298  ip46_type = (IP46_TYPE_IP6);
299  break;
300  case FIB_PROTOCOL_MPLS:
301  ip46_type = (IP46_TYPE_ANY);
302  break;
303  default:
304  ip46_type = (IP46_TYPE_ANY);
305  }
306 
307  ip_address_encode (&in->fp_addr, ip46_type, &out->address);
308 }
309 
310 void
311 ip_prefix_encode2 (const ip_prefix_t * in, vl_api_prefix_t * out)
312 {
313  out->len = in->len;
314  ip_address_encode2 (&in->addr, &out->address);
315 }
316 
317 void
318 ip_mprefix_encode (const mfib_prefix_t * in, vl_api_mprefix_t * out)
319 {
320  out->af = (FIB_PROTOCOL_IP6 == in->fp_proto ? ADDRESS_IP6 : ADDRESS_IP4);
321  out->grp_address_length = clib_host_to_net_u16 (in->fp_len);
322 
323  ip_address_union_encode (&in->fp_grp_addr, out->af, &out->grp_address);
324  ip_address_union_encode (&in->fp_src_addr, out->af, &out->src_address);
325 }
326 
327 void
328 ip_mprefix_decode (const vl_api_mprefix_t * in, mfib_prefix_t * out)
329 {
330  out->fp_proto = (ADDRESS_IP6 == in->af ?
332  out->fp_len = clib_net_to_host_u16 (in->grp_address_length);
333  out->___fp___pad = 0;
334 
335  ip_address_union_decode (&in->grp_address, in->af, &out->fp_grp_addr);
336  ip_address_union_decode (&in->src_address, in->af, &out->fp_src_addr);
337 
338  if (!ip46_address_is_zero (&out->fp_src_addr))
339  out->fp_len = (out->fp_proto == FIB_PROTOCOL_IP6 ? 256 : 64);
340 }
341 
342 /*
343  * fd.io coding-style-patch-verification: ON
344  *
345  * Local Variables:
346  * eval: (c-set-style "gnu")
347  * End:
348  */
ip_address
Definition: ip_types.h:79
ip_dscp_t
enum ip_dscp_t_ ip_dscp_t
ip_prefix_encode2
void ip_prefix_encode2(const ip_prefix_t *in, vl_api_prefix_t *out)
Definition: ip_types_api.c:311
ip4_address_decode
void ip4_address_decode(const vl_api_ip4_address_t in, ip4_address_t *out)
Definition: ip_types_api.c:155
clib_memcpy
#define clib_memcpy(d, s, n)
Definition: string.h:197
ip_prefix
Definition: ip_types.h:116
ip_mprefix_decode
void ip_mprefix_decode(const vl_api_mprefix_t *in, mfib_prefix_t *out)
Definition: ip_types_api.c:328
protocols.def
ip_prefix_validate
bool ip_prefix_validate(const ip_prefix_t *ip)
Definition: ip_types.c:447
api_types.h
ip_dscp_encode
vl_api_ip_dscp_t ip_dscp_encode(ip_dscp_t dscp)
Definition: ip_types_api.c:105
mfib_prefix_t_
Aggregate type for a prefix.
Definition: mfib_types.h:24
ip46_address_is_ip4
static u8 ip46_address_is_ip4(const ip46_address_t *ip46)
Definition: ip46_address.h:55
IP46_TYPE_IP4
@ IP46_TYPE_IP4
Definition: ip46_address.h:26
ip_mprefix_encode
void ip_mprefix_encode(const mfib_prefix_t *in, vl_api_mprefix_t *out)
Definition: ip_types_api.c:318
mfib_prefix_t_::fp_len
u16 fp_len
The mask length.
Definition: mfib_types.h:28
fib_prefix_t_::fp_len
u16 fp_len
The mask length.
Definition: fib_types.h:206
ip6_address_decode
void ip6_address_decode(const vl_api_ip6_address_t in, ip6_address_t *out)
Definition: ip_types_api.c:143
AF_IP4
@ AF_IP4
Definition: ip_types.h:23
ip_proto_encode
vl_api_ip_proto_t ip_proto_encode(ip_protocol_t ipp)
Definition: ip_types_api.c:83
ADDRESS_IP6
@ ADDRESS_IP6
Definition: ip_types.api:23
ip_address_union_decode
static ip46_type_t ip_address_union_decode(const vl_api_address_union_t *in, vl_api_address_family_t af, ip46_address_t *out)
Definition: ip_types_api.c:161
ip_prefix::len
u8 len
Definition: ip_types.h:119
ip_prefix_encode
void ip_prefix_encode(const fib_prefix_t *in, vl_api_prefix_t *out)
Definition: ip_types_api.c:287
ip_address_encode2
void ip_address_encode2(const ip_address_t *in, vl_api_address_t *out)
Definition: ip_types_api.c:242
ip_address_decode
ip46_type_t ip_address_decode(const vl_api_address_t *in, ip46_address_t *out)
Decode/Encode for struct/union types.
Definition: ip_types_api.c:186
mfib_prefix_t_::fp_src_addr
ip46_address_t fp_src_addr
Definition: mfib_types.h:47
ip_address_family_decode
int ip_address_family_decode(vl_api_address_family_t af, ip_address_family_t *out)
Conversion functions to/from (decode/encode) API types to VPP internal types.
Definition: ip_types_api.c:34
ip6_address_encode
void ip6_address_encode(const ip6_address_t *in, vl_api_ip6_address_t out)
Definition: ip_types_api.c:137
ip_address::version
ip_address_family_t version
Definition: ip_types.h:82
ip_prefix_decode
void ip_prefix_decode(const vl_api_prefix_t *in, fib_prefix_t *out)
Definition: ip_types_api.c:259
ip_dscp_decode
ip_dscp_t ip_dscp_decode(vl_api_ip_dscp_t in)
Definition: ip_types_api.c:99
IP_API_PROTO_TCP
@ IP_API_PROTO_TCP
Definition: ip_types.api:79
foreach_ip_feature_location
@ foreach_ip_feature_location
Definition: ip_types.h:72
ip_feature_location_encode
vl_api_ip_feature_location_t ip_feature_location_encode(ip_feature_location_t loc)
Definition: ip_types_api.c:130
ip4_address_t
Definition: ip4_packet.h:50
FIB_PROTOCOL_IP4
@ FIB_PROTOCOL_IP4
Definition: fib_types.h:36
IP46_TYPE_IP6
@ IP46_TYPE_IP6
Definition: ip46_address.h:27
ip_address_family_encode
vl_api_address_family_t ip_address_family_encode(ip_address_family_t af)
Definition: ip_types_api.c:50
mfib_prefix_t_::fp_proto
fib_protocol_t fp_proto
protocol type
Definition: mfib_types.h:33
fib_prefix_t_::fp_addr
ip46_address_t fp_addr
The address type is not deriveable from the fp_addr member.
Definition: fib_types.h:225
ip_address_decode2
void ip_address_decode2(const vl_api_address_t *in, ip_address_t *out)
Definition: ip_types_api.c:192
ip46_address_is_zero
static u8 ip46_address_is_zero(const ip46_address_t *ip46)
Definition: ip46_address.h:87
ip_prefix::addr
ip_address_t addr
Definition: ip_types.h:118
ip_address_union_encode
static void ip_address_union_encode(const ip46_address_t *in, vl_api_address_family_t af, vl_api_address_union_t *out)
Definition: ip_types_api.c:209
mfib_prefix_t_::fp_grp_addr
ip46_address_t fp_grp_addr
The address type is not deriveable from the fp_addr member.
Definition: mfib_types.h:46
ASSERT
#define ASSERT(truth)
Definition: error_bootstrap.h:69
FIB_PROTOCOL_MPLS
@ FIB_PROTOCOL_MPLS
Definition: fib_types.h:38
ip_prefix_decode2
int ip_prefix_decode2(const vl_api_prefix_t *in, ip_prefix_t *out)
Definition: ip_types_api.c:276
FIB_PROTOCOL_IP6
@ FIB_PROTOCOL_IP6
Definition: fib_types.h:37
ip4_address_encode
void ip4_address_encode(const ip4_address_t *in, vl_api_ip4_address_t out)
Definition: ip_types_api.c:149
AF_IP6
@ AF_IP6
Definition: ip_types.h:24
fib_prefix_t_::fp_proto
fib_protocol_t fp_proto
protocol type
Definition: fib_types.h:211
ip_protocol_t
enum ip_protocol ip_protocol_t
clib_memset
clib_memset(h->entries, 0, sizeof(h->entries[0]) *entries)
ip_address_encode
void ip_address_encode(const ip46_address_t *in, ip46_type_t type, vl_api_address_t *out)
Definition: ip_types_api.c:220
u8
unsigned char u8
Definition: types.h:56
ip46_type_t
ip46_type_t
Definition: ip46_address.h:22
ip_proto_decode
int ip_proto_decode(vl_api_ip_proto_t ipp, ip_protocol_t *out)
Definition: ip_types_api.c:65
vnet_all_api_h.h
ip_address::ip
ip46_address_t ip
Definition: ip_types.h:81
IP46_TYPE_ANY
@ IP46_TYPE_ANY
Definition: ip46_address.h:24
ip_feature_location_decode
int ip_feature_location_decode(vl_api_ip_feature_location_t loc, ip_feature_location_t *out)
Definition: ip_types_api.c:111
ip_feature_location_t
enum ip_feature_location_t_ ip_feature_location_t
ADDRESS_IP4
@ ADDRESS_IP4
Definition: ip_types.api:22
ip_types_api.h
fib_prefix_t_
Aggregate type for a prefix.
Definition: fib_types.h:202
dscp
vl_api_ip_dscp_t dscp
Definition: dhcp.api:163
type
vl_api_fib_path_type_t type
Definition: fib_types.api:123
ip_address_family_t
enum ip_address_family_t_ ip_address_family_t