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>
19 #define vl_typedefs /* define message structures */
20 #include <vnet/vnet_all_api_h.h>
23 #define vl_endianfun /* define message structures */
24 #include <vnet/vnet_all_api_h.h>
27 /* instantiate all the print functions we know about */
28 #define vl_print(handle, ...) vlib_cli_output (handle, __VA_ARGS__)
30 #include <vnet/vnet_all_api_h.h>
34 ip_address_family_decode (vl_api_address_family_t af,
35 ip_address_family_t * out)
46 return (VNET_API_ERROR_INVALID_ADDRESS_FAMILY);
49 vl_api_address_family_t
50 ip_address_family_encode (ip_address_family_t af)
65 ip_proto_decode (vl_api_ip_proto_t ipp, ip_protocol_t * out)
67 /* Not all protocol are defined in vl_api_ip_proto_t
68 * so we must cast to a different type.
72 #define ip_protocol(n,s) \
73 case IP_PROTOCOL_##s: \
74 *out = IP_PROTOCOL_##s; \
76 #include "protocols.def"
79 return (VNET_API_ERROR_INVALID_PROTOCOL);
83 ip_proto_encode (ip_protocol_t ipp)
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"
95 return (IP_API_PROTO_TCP);
99 ip_dscp_decode (vl_api_ip_dscp_t in)
101 return ((ip_dscp_t) in);
105 ip_dscp_encode (ip_dscp_t dscp)
107 return ((vl_api_ip_dscp_t) dscp);
111 ip_feature_location_decode (vl_api_ip_feature_location_t loc,
112 ip_feature_location_t * out)
114 /* Not all feature_locationcol are defined in vl_api_ip_feature_location_t
115 * so we must cast to a different type.
120 case IP_API_FEATURE_##n: \
121 *out = IP_FEATURE_##n; \
123 foreach_ip_feature_location
126 return (VNET_API_ERROR_FEATURE_DISABLED);
129 vl_api_ip_feature_location_t
130 ip_feature_location_encode (ip_feature_location_t loc)
132 return ((vl_api_ip_feature_location_t) (loc));
137 ip6_address_encode (const ip6_address_t * in, vl_api_ip6_address_t out)
139 clib_memcpy (out, in, sizeof (*in));
143 ip6_address_decode (const vl_api_ip6_address_t in, ip6_address_t * out)
145 clib_memcpy (out, in, sizeof (*out));
149 ip4_address_encode (const ip4_address_t * in, vl_api_ip4_address_t out)
151 clib_memcpy (out, in, sizeof (*in));
155 ip4_address_decode (const vl_api_ip4_address_t in, ip4_address_t * out)
157 clib_memcpy (out, in, sizeof (*out));
161 ip_address_union_decode (const vl_api_address_union_t * in,
162 vl_api_address_family_t af, ip46_address_t * out)
169 clib_memset (out, 0, sizeof (*out));
170 clib_memcpy (&out->ip4, &in->ip4, sizeof (out->ip4));
171 type = IP46_TYPE_IP4;
174 clib_memcpy (&out->ip6, &in->ip6, sizeof (out->ip6));
175 type = IP46_TYPE_IP6;
178 type = IP46_TYPE_ANY;
186 ip_address_decode (const vl_api_address_t * in, ip46_address_t * out)
188 return (ip_address_union_decode (&in->un, in->af, out));
192 ip_address_decode2 (const vl_api_address_t * in, ip_address_t * out)
194 switch (ip_address_union_decode (&in->un, in->af, &out->ip))
197 out->version = AF_IP4;
200 out->version = AF_IP6;
209 ip_address_union_encode (const ip46_address_t * in,
210 vl_api_address_family_t af,
211 vl_api_address_union_t * out)
213 if (ADDRESS_IP6 == af)
214 ip6_address_encode (&in->ip6, out->ip6);
216 ip4_address_encode (&in->ip4, out->ip4);
220 ip_address_encode (const ip46_address_t * in,
221 ip46_type_t type, vl_api_address_t * out)
226 out->af = ADDRESS_IP4;
229 out->af = ADDRESS_IP6;
232 if (ip46_address_is_ip4 (in))
233 out->af = ADDRESS_IP4;
235 out->af = ADDRESS_IP6;
238 ip_address_union_encode (in, out->af, &out->un);
242 ip_address_encode2 (const ip_address_t * in, vl_api_address_t * out)
247 out->af = ADDRESS_IP4;
248 ip4_address_encode (&in->ip.ip4, out->un.ip4);
251 out->af = ADDRESS_IP6;
252 ip6_address_encode (&in->ip.ip6, out->un.ip6);
255 ip_address_union_encode (&in->ip, out->af, &out->un);
259 ip_prefix_decode (const vl_api_prefix_t * in, fib_prefix_t * out)
261 switch (in->address.af)
264 out->fp_proto = FIB_PROTOCOL_IP4;
267 out->fp_proto = FIB_PROTOCOL_IP6;
270 out->fp_len = in->len;
271 out->___fp___pad = 0;
272 ip_address_decode (&in->address, &out->fp_addr);
276 ip_prefix_decode2 (const vl_api_prefix_t * in, ip_prefix_t * out)
279 ip_address_decode2 (&in->address, &out->addr);
281 if (!ip_prefix_validate (out))
282 return (VNET_API_ERROR_IP_PREFIX_INVALID);
287 ip_prefix_encode (const fib_prefix_t * in, vl_api_prefix_t * out)
289 out->len = in->fp_len;
290 ip46_type_t ip46_type;
292 switch (in->fp_proto)
294 case FIB_PROTOCOL_IP4:
295 ip46_type = (IP46_TYPE_IP4);
297 case FIB_PROTOCOL_IP6:
298 ip46_type = (IP46_TYPE_IP6);
300 case FIB_PROTOCOL_MPLS:
301 ip46_type = (IP46_TYPE_ANY);
304 ip46_type = (IP46_TYPE_ANY);
307 ip_address_encode (&in->fp_addr, ip46_type, &out->address);
311 ip_prefix_encode2 (const ip_prefix_t * in, vl_api_prefix_t * out)
314 ip_address_encode2 (&in->addr, &out->address);
318 ip_mprefix_encode (const mfib_prefix_t * in, vl_api_mprefix_t * out)
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);
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);
328 ip_mprefix_decode (const vl_api_mprefix_t * in, mfib_prefix_t * out)
330 out->fp_proto = (ADDRESS_IP6 == in->af ?
331 FIB_PROTOCOL_IP6 : FIB_PROTOCOL_IP4);
332 out->fp_len = clib_net_to_host_u16 (in->grp_address_length);
333 out->___fp___pad = 0;
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);
338 if (!ip46_address_is_zero (&out->fp_src_addr))
339 out->fp_len = (out->fp_proto == FIB_PROTOCOL_IP6 ? 256 : 64);
343 * fd.io coding-style-patch-verification: ON
346 * eval: (c-set-style "gnu")