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 f)
65 return ((vl_api_tunnel_flags_t) f);
69 tunnel_mode_decode (vl_api_tunnel_mode_t in, tunnel_mode_t * out)
74 case TUNNEL_API_MODE_##n: \
75 *out = TUNNEL_MODE_##n; \
81 return (VNET_API_ERROR_INVALID_VALUE_2);
85 tunnel_mode_encode (tunnel_mode_t in)
87 vl_api_tunnel_mode_t out = TUNNEL_API_MODE_P2P;
92 case TUNNEL_MODE_##n: \
93 out = TUNNEL_API_MODE_##n; \
103 tunnel_decode (const vl_api_tunnel_t *in, tunnel_t *out)
107 ip_address_decode2 (&in->src, &out->t_src);
108 ip_address_decode2 (&in->dst, &out->t_dst);
110 if (ip_addr_version (&out->t_src) != ip_addr_version (&out->t_dst))
111 return (VNET_API_ERROR_INVALID_PROTOCOL);
113 if (0 == ip_address_cmp (&out->t_src, &out->t_dst))
114 return (VNET_API_ERROR_SAME_SRC_DST);
116 rv = tunnel_encap_decap_flags_decode (in->encap_decap_flags,
117 &out->t_encap_decap_flags);
122 rv = tunnel_mode_decode (in->mode, &out->t_mode);
127 rv = tunnel_flags_decode (in->flags, &out->t_flags);
132 out->t_table_id = clib_net_to_host_u32 (in->table_id);
133 out->t_fib_index = fib_table_find (
134 ip_address_family_to_fib_proto (ip_addr_version (&out->t_dst)),
137 if (~0 == out->t_fib_index)
138 return (VNET_API_ERROR_NO_SUCH_FIB);
140 out->t_dscp = ip_dscp_decode (in->dscp);
141 out->t_hop_limit = in->hop_limit;
147 tunnel_encode (const tunnel_t *in, vl_api_tunnel_t *out)
149 ip_address_encode2 (&in->t_src, &out->src);
150 ip_address_encode2 (&in->t_dst, &out->dst);
152 out->encap_decap_flags =
153 tunnel_encap_decap_flags_encode (in->t_encap_decap_flags);
154 out->mode = tunnel_mode_encode (in->t_mode);
155 out->flags = tunnel_flags_encode (in->t_flags);
156 out->table_id = clib_host_to_net_u32 (in->t_table_id);
157 out->dscp = ip_dscp_encode (in->t_dscp);
158 out->hop_limit = in->t_hop_limit;
162 * fd.io coding-style-patch-verification: ON
165 * eval: (c-set-style "gnu")