2 * tunnel_api.c - tunnel api
4 * Copyright (c) 2018 Cisco and/or its affiliates.
5 * Licensed under the Apache License, Version 2.0 (the "License");
6 * you may not use this file except in compliance with the License.
7 * You may obtain a copy of the License at:
9 * http://www.apache.org/licenses/LICENSE-2.0
11 * Unless required by applicable law or agreed to in writing, software
12 * distributed under the License is distributed on an "AS IS" BASIS,
13 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 * See the License for the specific language governing permissions and
15 * limitations under the License.
18 #include <vnet/api_errno.h>
19 #include <vnet/tunnel/tunnel_types_api.h>
20 #include <vnet/ip/ip_types_api.h>
21 #include <vnet/fib/fib_table.h>
23 #include <vnet/tunnel/tunnel_types.api_enum.h>
24 #include <vnet/tunnel/tunnel_types.api_types.h>
27 STATIC_ASSERT (sizeof (vl_api_tunnel_encap_decap_flags_t) ==
28 sizeof (tunnel_encap_decap_flags_t),
29 "tunnel API and internal flags enum size differ");
30 STATIC_ASSERT (sizeof (vl_api_tunnel_flags_t) == sizeof (tunnel_flags_t),
31 "tunnel API and internal flags enum size differ");
34 tunnel_encap_decap_flags_decode (vl_api_tunnel_encap_decap_flags_t f,
35 tunnel_encap_decap_flags_t * o)
37 if (f & ~TUNNEL_ENCAP_DECAP_FLAG_MASK)
38 /* unknown flags set */
39 return (VNET_API_ERROR_INVALID_VALUE_2);
41 *o = (tunnel_encap_decap_flags_t) f;
45 vl_api_tunnel_encap_decap_flags_t
46 tunnel_encap_decap_flags_encode (tunnel_encap_decap_flags_t f)
48 return ((vl_api_tunnel_encap_decap_flags_t) f);
52 tunnel_flags_decode (vl_api_tunnel_flags_t f, tunnel_flags_t *o)
54 if (f & ~TUNNEL_FLAG_MASK)
55 /* unknown flags set */
56 return (VNET_API_ERROR_INVALID_VALUE_2);
58 *o = (tunnel_flags_t) f;
63 tunnel_flags_encode (tunnel_flags_t in)
65 vl_api_tunnel_flags_t out = 0;
67 if (in & TUNNEL_FLAG_TRACK_MTU)
68 out |= TUNNEL_API_FLAG_TRACK_MTU;
74 tunnel_mode_decode (vl_api_tunnel_mode_t in, tunnel_mode_t * out)
79 case TUNNEL_API_MODE_##n: \
80 *out = TUNNEL_MODE_##n; \
86 return (VNET_API_ERROR_INVALID_VALUE_2);
90 tunnel_mode_encode (tunnel_mode_t in)
92 vl_api_tunnel_mode_t out = TUNNEL_API_MODE_P2P;
97 case TUNNEL_MODE_##n: \
98 out = TUNNEL_API_MODE_##n; \
108 tunnel_decode (const vl_api_tunnel_t *in, tunnel_t *out)
112 ip_address_decode2 (&in->src, &out->t_src);
113 ip_address_decode2 (&in->dst, &out->t_dst);
115 if (ip_addr_version (&out->t_src) != ip_addr_version (&out->t_dst))
116 return (VNET_API_ERROR_INVALID_PROTOCOL);
118 if (0 == ip_address_cmp (&out->t_src, &out->t_dst))
119 return (VNET_API_ERROR_SAME_SRC_DST);
121 rv = tunnel_encap_decap_flags_decode (in->encap_decap_flags,
122 &out->t_encap_decap_flags);
127 rv = tunnel_mode_decode (in->mode, &out->t_mode);
132 rv = tunnel_flags_decode (in->flags, &out->t_flags);
137 out->t_table_id = clib_net_to_host_u32 (in->table_id);
138 out->t_fib_index = fib_table_find (
139 ip_address_family_to_fib_proto (ip_addr_version (&out->t_dst)),
142 if (~0 == out->t_fib_index)
143 return (VNET_API_ERROR_NO_SUCH_FIB);
145 out->t_dscp = ip_dscp_decode (in->dscp);
146 out->t_hop_limit = in->hop_limit;
152 tunnel_encode (const tunnel_t *in, vl_api_tunnel_t *out)
154 ip_address_encode2 (&in->t_src, &out->src);
155 ip_address_encode2 (&in->t_dst, &out->dst);
157 out->encap_decap_flags =
158 tunnel_encap_decap_flags_encode (in->t_encap_decap_flags);
159 out->mode = tunnel_mode_encode (in->t_mode);
160 out->flags = tunnel_flags_encode (in->t_flags);
161 out->table_id = clib_host_to_net_u32 (in->t_table_id);
162 out->dscp = ip_dscp_encode (in->t_dscp);
163 out->hop_limit = in->t_hop_limit;
167 * fd.io coding-style-patch-verification: ON
170 * eval: (c-set-style "gnu")