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:
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 <vlibapi/api_types.h>
17 #include <vnet/ip/ip_types_api.h>
20 ip_address_family_decode (vl_api_address_family_t af,
21 ip_address_family_t * out)
32 return (VNET_API_ERROR_INVALID_ADDRESS_FAMILY);
35 vl_api_address_family_t
36 ip_address_family_encode (ip_address_family_t af)
51 ip_proto_decode (vl_api_ip_proto_t ipp, ip_protocol_t * out)
53 /* Not all protocol are defined in vl_api_ip_proto_t
54 * so we must cast to a different type.
58 #define ip_protocol(n,s) \
59 case IP_PROTOCOL_##s: \
60 *out = IP_PROTOCOL_##s; \
62 #include "protocols.def"
65 return (VNET_API_ERROR_INVALID_PROTOCOL);
69 ip_proto_encode (ip_protocol_t ipp)
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"
81 return (IP_API_PROTO_TCP);
85 ip_dscp_decode (vl_api_ip_dscp_t in)
87 return ((ip_dscp_t) in);
91 ip_dscp_encode (ip_dscp_t dscp)
93 return ((vl_api_ip_dscp_t) dscp);
97 ip_feature_location_decode (vl_api_ip_feature_location_t loc,
98 ip_feature_location_t * out)
100 /* Not all feature_locationcol are defined in vl_api_ip_feature_location_t
101 * so we must cast to a different type.
106 case IP_API_FEATURE_##n: \
107 *out = IP_FEATURE_##n; \
109 foreach_ip_feature_location
112 return (VNET_API_ERROR_FEATURE_DISABLED);
115 vl_api_ip_feature_location_t
116 ip_feature_location_encode (ip_feature_location_t loc)
118 return ((vl_api_ip_feature_location_t) (loc));
123 ip6_address_encode (const ip6_address_t * in, vl_api_ip6_address_t out)
125 clib_memcpy (out, in, sizeof (*in));
129 ip6_address_decode (const vl_api_ip6_address_t in, ip6_address_t * out)
131 clib_memcpy (out, in, sizeof (*out));
135 ip4_address_encode (const ip4_address_t * in, vl_api_ip4_address_t out)
137 clib_memcpy (out, in, sizeof (*in));
141 ip4_address_decode (const vl_api_ip4_address_t in, ip4_address_t * out)
143 clib_memcpy (out, in, sizeof (*out));
147 ip_address_union_decode (const vl_api_address_union_t * in,
148 vl_api_address_family_t af, ip46_address_t * out)
155 clib_memset (out, 0, sizeof (*out));
156 clib_memcpy (&out->ip4, &in->ip4, sizeof (out->ip4));
157 type = IP46_TYPE_IP4;
160 clib_memcpy (&out->ip6, &in->ip6, sizeof (out->ip6));
161 type = IP46_TYPE_IP6;
164 type = IP46_TYPE_ANY;
172 ip_address_decode (const vl_api_address_t * in, ip46_address_t * out)
174 return (ip_address_union_decode (&in->un, in->af, out));
178 ip_address_decode2 (const vl_api_address_t * in, ip_address_t * out)
180 switch (ip_address_union_decode (&in->un, in->af, &out->ip))
183 out->version = AF_IP4;
186 out->version = AF_IP6;
195 ip_address_union_encode (const ip46_address_t * in,
196 vl_api_address_family_t af,
197 vl_api_address_union_t * out)
199 if (ADDRESS_IP6 == af)
200 ip6_address_encode (&in->ip6, out->ip6);
202 ip4_address_encode (&in->ip4, out->ip4);
206 ip_address_encode (const ip46_address_t * in,
207 ip46_type_t type, vl_api_address_t * out)
212 out->af = ADDRESS_IP4;
215 out->af = ADDRESS_IP6;
218 if (ip46_address_is_ip4 (in))
219 out->af = ADDRESS_IP4;
221 out->af = ADDRESS_IP6;
224 ip_address_union_encode (in, out->af, &out->un);
228 ip_address_encode2 (const ip_address_t * in, vl_api_address_t * out)
233 out->af = ADDRESS_IP4;
234 ip4_address_encode (&in->ip.ip4, out->un.ip4);
237 out->af = ADDRESS_IP6;
238 ip6_address_encode (&in->ip.ip6, out->un.ip6);
241 ip_address_union_encode (&in->ip, out->af, &out->un);
245 ip_prefix_decode (const vl_api_prefix_t * in, fib_prefix_t * out)
247 switch (in->address.af)
250 out->fp_proto = FIB_PROTOCOL_IP4;
253 out->fp_proto = FIB_PROTOCOL_IP6;
256 out->fp_len = in->len;
257 out->___fp___pad = 0;
258 ip_address_decode (&in->address, &out->fp_addr);
262 ip_prefix_decode2 (const vl_api_prefix_t * in, ip_prefix_t * out)
265 ip_address_decode2 (&in->address, &out->addr);
267 if (!ip_prefix_validate (out))
268 return (VNET_API_ERROR_IP_PREFIX_INVALID);
273 ip_prefix_encode (const fib_prefix_t * in, vl_api_prefix_t * out)
275 out->len = in->fp_len;
276 ip46_type_t ip46_type;
278 switch (in->fp_proto)
280 case FIB_PROTOCOL_IP4:
281 ip46_type = (IP46_TYPE_IP4);
283 case FIB_PROTOCOL_IP6:
284 ip46_type = (IP46_TYPE_IP6);
286 case FIB_PROTOCOL_MPLS:
287 ip46_type = (IP46_TYPE_ANY);
290 ip46_type = (IP46_TYPE_ANY);
293 ip_address_encode (&in->fp_addr, ip46_type, &out->address);
297 ip_prefix_encode2 (const ip_prefix_t * in, vl_api_prefix_t * out)
300 ip_address_encode2 (&in->addr, &out->address);
304 ip_mprefix_encode (const mfib_prefix_t * in, vl_api_mprefix_t * out)
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);
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);
314 ip_mprefix_decode (const vl_api_mprefix_t * in, mfib_prefix_t * out)
316 out->fp_proto = (ADDRESS_IP6 == in->af ?
317 FIB_PROTOCOL_IP6 : FIB_PROTOCOL_IP4);
318 out->fp_len = clib_net_to_host_u16 (in->grp_address_length);
319 out->___fp___pad = 0;
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);
324 if (!ip46_address_is_zero (&out->fp_src_addr))
325 out->fp_len = (out->fp_proto == FIB_PROTOCOL_IP6 ? 256 : 64);
329 * fd.io coding-style-patch-verification: ON
332 * eval: (c-set-style "gnu")