FD.io VPP  v21.10.1-2-g0a485f517
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 int
20 ip_address_family_decode (vl_api_address_family_t af,
21  ip_address_family_t * out)
22 {
23  switch (af)
24  {
25  case ADDRESS_IP4:
26  *out = AF_IP4;
27  return (0);
28  case ADDRESS_IP6:
29  *out = AF_IP6;
30  return (0);
31  }
32  return (VNET_API_ERROR_INVALID_ADDRESS_FAMILY);
33 }
34 
35 vl_api_address_family_t
37 {
38  switch (af)
39  {
40  case AF_IP4:
41  return (ADDRESS_IP4);
42  case AF_IP6:
43  return (ADDRESS_IP6);
44  }
45 
46  ASSERT (0);
47  return (ADDRESS_IP4);
48 }
49 
50 int
51 ip_proto_decode (vl_api_ip_proto_t ipp, ip_protocol_t * out)
52 {
53  /* Not all protocol are defined in vl_api_ip_proto_t
54  * so we must cast to a different type.
55  */
56  switch ((u8) ipp)
57  {
58 #define ip_protocol(n,s) \
59  case IP_PROTOCOL_##s: \
60  *out = IP_PROTOCOL_##s; \
61  return (0);
62 #include "protocols.def"
63 #undef ip_protocol
64  }
65  return (VNET_API_ERROR_INVALID_PROTOCOL);
66 }
67 
68 vl_api_ip_proto_t
70 {
71  switch (ipp)
72  {
73 #define ip_protocol(n,s) \
74  case IP_PROTOCOL_##s: \
75  return ((vl_api_ip_proto_t) IP_PROTOCOL_##s);
76 #include "protocols.def"
77 #undef ip_protocol
78  }
79 
80  ASSERT (0);
81  return (IP_API_PROTO_TCP);
82 }
83 
85 ip_dscp_decode (vl_api_ip_dscp_t in)
86 {
87  return ((ip_dscp_t) in);
88 }
89 
90 vl_api_ip_dscp_t
92 {
93  return ((vl_api_ip_dscp_t) dscp);
94 }
95 
96 int
97 ip_feature_location_decode (vl_api_ip_feature_location_t loc,
99 {
100  /* Not all feature_locationcol are defined in vl_api_ip_feature_location_t
101  * so we must cast to a different type.
102  */
103  switch (loc)
104  {
105 #define _(n,s) \
106  case IP_API_FEATURE_##n: \
107  *out = IP_FEATURE_##n; \
108  return (0);
110 #undef _
111  }
112  return (VNET_API_ERROR_FEATURE_DISABLED);
113 }
114 
115 vl_api_ip_feature_location_t
117 {
118  return ((vl_api_ip_feature_location_t) (loc));
119 }
120 
121 
122 void
123 ip6_address_encode (const ip6_address_t * in, vl_api_ip6_address_t out)
124 {
125  clib_memcpy (out, in, sizeof (*in));
126 }
127 
128 void
129 ip6_address_decode (const vl_api_ip6_address_t in, ip6_address_t * out)
130 {
131  clib_memcpy (out, in, sizeof (*out));
132 }
133 
134 void
135 ip4_address_encode (const ip4_address_t * in, vl_api_ip4_address_t out)
136 {
137  clib_memcpy (out, in, sizeof (*in));
138 }
139 
140 void
141 ip4_address_decode (const vl_api_ip4_address_t in, ip4_address_t * out)
142 {
143  clib_memcpy (out, in, sizeof (*out));
144 }
145 
146 static ip46_type_t
147 ip_address_union_decode (const vl_api_address_union_t * in,
148  vl_api_address_family_t af, ip46_address_t * out)
149 {
151 
152  switch (af)
153  {
154  case ADDRESS_IP4:
155  clib_memset (out, 0, sizeof (*out));
156  clib_memcpy (&out->ip4, &in->ip4, sizeof (out->ip4));
158  break;
159  case ADDRESS_IP6:
160  clib_memcpy (&out->ip6, &in->ip6, sizeof (out->ip6));
162  break;
163  default:
165  break;
166  }
167 
168  return type;
169 }
170 
172 ip_address_decode (const vl_api_address_t * in, ip46_address_t * out)
173 {
174  return (ip_address_union_decode (&in->un, in->af, out));
175 }
176 
177 void
178 ip_address_decode2 (const vl_api_address_t * in, ip_address_t * out)
179 {
180  switch (ip_address_union_decode (&in->un, in->af, &out->ip))
181  {
182  case IP46_TYPE_IP4:
183  out->version = AF_IP4;
184  break;
185  case IP46_TYPE_IP6:
186  out->version = AF_IP6;
187  break;
188  default:
189  ;
190  break;
191  }
192 }
193 
194 static void
195 ip_address_union_encode (const ip46_address_t * in,
196  vl_api_address_family_t af,
197  vl_api_address_union_t * out)
198 {
199  if (ADDRESS_IP6 == af)
200  ip6_address_encode (&in->ip6, out->ip6);
201  else
202  ip4_address_encode (&in->ip4, out->ip4);
203 }
204 
205 void
206 ip_address_encode (const ip46_address_t * in,
207  ip46_type_t type, vl_api_address_t * out)
208 {
209  switch (type)
210  {
211  case IP46_TYPE_IP4:
212  out->af = ADDRESS_IP4;
213  break;
214  case IP46_TYPE_IP6:
215  out->af = ADDRESS_IP6;
216  break;
217  case IP46_TYPE_ANY:
218  if (ip46_address_is_ip4 (in))
219  out->af = ADDRESS_IP4;
220  else
221  out->af = ADDRESS_IP6;
222  break;
223  }
224  ip_address_union_encode (in, out->af, &out->un);
225 }
226 
227 void
228 ip_address_encode2 (const ip_address_t * in, vl_api_address_t * out)
229 {
230  switch (in->version)
231  {
232  case AF_IP4:
233  out->af = ADDRESS_IP4;
234  ip4_address_encode (&in->ip.ip4, out->un.ip4);
235  break;
236  case AF_IP6:
237  out->af = ADDRESS_IP6;
238  ip6_address_encode (&in->ip.ip6, out->un.ip6);
239  break;
240  }
241  ip_address_union_encode (&in->ip, out->af, &out->un);
242 }
243 
244 void
245 ip_prefix_decode (const vl_api_prefix_t * in, fib_prefix_t * out)
246 {
247  switch (in->address.af)
248  {
249  case ADDRESS_IP4:
250  out->fp_proto = FIB_PROTOCOL_IP4;
251  break;
252  case ADDRESS_IP6:
253  out->fp_proto = FIB_PROTOCOL_IP6;
254  break;
255  }
256  out->fp_len = in->len;
257  out->___fp___pad = 0;
258  ip_address_decode (&in->address, &out->fp_addr);
259 }
260 
261 int
262 ip_prefix_decode2 (const vl_api_prefix_t * in, ip_prefix_t * out)
263 {
264  out->len = in->len;
265  ip_address_decode2 (&in->address, &out->addr);
266 
267  if (!ip_prefix_validate (out))
268  return (VNET_API_ERROR_IP_PREFIX_INVALID);
269  return (0);
270 }
271 
272 void
273 ip_prefix_encode (const fib_prefix_t * in, vl_api_prefix_t * out)
274 {
275  out->len = in->fp_len;
276  ip46_type_t ip46_type;
277 
278  switch (in->fp_proto)
279  {
280  case FIB_PROTOCOL_IP4:
281  ip46_type = (IP46_TYPE_IP4);
282  break;
283  case FIB_PROTOCOL_IP6:
284  ip46_type = (IP46_TYPE_IP6);
285  break;
286  case FIB_PROTOCOL_MPLS:
287  ip46_type = (IP46_TYPE_ANY);
288  break;
289  default:
290  ip46_type = (IP46_TYPE_ANY);
291  }
292 
293  ip_address_encode (&in->fp_addr, ip46_type, &out->address);
294 }
295 
296 void
297 ip_prefix_encode2 (const ip_prefix_t * in, vl_api_prefix_t * out)
298 {
299  out->len = in->len;
300  ip_address_encode2 (&in->addr, &out->address);
301 }
302 
303 void
304 ip_mprefix_encode (const mfib_prefix_t * in, vl_api_mprefix_t * out)
305 {
306  out->af = (FIB_PROTOCOL_IP6 == in->fp_proto ? ADDRESS_IP6 : ADDRESS_IP4);
307  out->grp_address_length = clib_host_to_net_u16 (in->fp_len);
308 
309  ip_address_union_encode (&in->fp_grp_addr, out->af, &out->grp_address);
310  ip_address_union_encode (&in->fp_src_addr, out->af, &out->src_address);
311 }
312 
313 void
314 ip_mprefix_decode (const vl_api_mprefix_t * in, mfib_prefix_t * out)
315 {
316  out->fp_proto = (ADDRESS_IP6 == in->af ?
318  out->fp_len = clib_net_to_host_u16 (in->grp_address_length);
319  out->___fp___pad = 0;
320 
321  ip_address_union_decode (&in->grp_address, in->af, &out->fp_grp_addr);
322  ip_address_union_decode (&in->src_address, in->af, &out->fp_src_addr);
323 
324  if (!ip46_address_is_zero (&out->fp_src_addr))
325  out->fp_len = (out->fp_proto == FIB_PROTOCOL_IP6 ? 256 : 64);
326 }
327 
328 /*
329  * fd.io coding-style-patch-verification: ON
330  *
331  * Local Variables:
332  * eval: (c-set-style "gnu")
333  * End:
334  */
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:297
ip4_address_decode
void ip4_address_decode(const vl_api_ip4_address_t in, ip4_address_t *out)
Definition: ip_types_api.c:141
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:314
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:91
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:304
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:129
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:69
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:147
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:273
ip_address_encode2
void ip_address_encode2(const ip_address_t *in, vl_api_address_t *out)
Definition: ip_types_api.c:228
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:172
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:20
ip6_address_encode
void ip6_address_encode(const ip6_address_t *in, vl_api_ip6_address_t out)
Definition: ip_types_api.c:123
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:245
ip_dscp_decode
ip_dscp_t ip_dscp_decode(vl_api_ip_dscp_t in)
Definition: ip_types_api.c:85
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:116
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:36
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:178
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:195
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:262
af
vl_api_address_family_t af
Definition: ip.api:619
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:135
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:206
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:51
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:97
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