2 * tunnel.h: shared definitions for tunnels.
4 * Copyright (c) 2019 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/tunnel/tunnel.h>
19 #include <vnet/fib/fib_table.h>
20 #include <vnet/fib/fib_entry_track.h>
22 #include <vnet/ip/ip6_inlines.h>
24 const u8 TUNNEL_ENCAP_DECAP_FLAG_MASK = (
25 #define _(a, b, c) TUNNEL_ENCAP_DECAP_FLAG_##a |
26 foreach_tunnel_encap_decap_flag
29 const u8 TUNNEL_FLAG_MASK = (
30 #define _(a, b, c) TUNNEL_FLAG_##a |
36 format_tunnel_mode (u8 * s, va_list * args)
38 tunnel_mode_t mode = va_arg (*args, int);
42 #define _(n, v) case TUNNEL_MODE_##n: \
43 s = format (s, "%s", v); \
53 unformat_tunnel_mode (unformat_input_t * input, va_list * args)
55 tunnel_mode_t *m = va_arg (*args, tunnel_mode_t *);
57 if (unformat (input, "p2p"))
59 else if (unformat (input, "p2mp") || unformat (input, "mp"))
67 format_tunnel_encap_decap_flags (u8 * s, va_list * args)
69 tunnel_encap_decap_flags_t f = va_arg (*args, int);
71 if (f == TUNNEL_ENCAP_DECAP_FLAG_NONE)
72 s = format (s, "none");
75 else if (f & TUNNEL_ENCAP_DECAP_FLAG_##a) s = format (s, "%s ", b);
76 foreach_tunnel_encap_decap_flag
82 unformat_tunnel_encap_decap_flags (unformat_input_t * input, va_list * args)
84 tunnel_encap_decap_flags_t *f =
85 va_arg (*args, tunnel_encap_decap_flags_t *);
86 #define _(a,b,c) if (unformat(input, b)) {\
87 *f |= TUNNEL_ENCAP_DECAP_FLAG_##a;\
90 foreach_tunnel_encap_decap_flag;
96 format_tunnel_flags (u8 *s, va_list *args)
98 tunnel_flags_t f = va_arg (*args, int);
100 if (f == TUNNEL_FLAG_NONE)
101 s = format (s, "none");
103 #define _(a, b, c) else if (f & TUNNEL_FLAG_##a) s = format (s, "%s ", c);
110 unformat_tunnel_flags (unformat_input_t *input, va_list *args)
112 tunnel_flags_t *f = va_arg (*args, tunnel_flags_t *);
114 if (unformat (input, c)) \
116 *f |= TUNNEL_FLAG_##a; \
125 tunnel_get_af (const tunnel_t *t)
127 return (ip_addr_version (&t->t_src));
131 tunnel_copy (const tunnel_t *src, tunnel_t *dst)
133 ip_address_copy (&dst->t_dst, &src->t_dst);
134 ip_address_copy (&dst->t_src, &src->t_src);
136 dst->t_encap_decap_flags = src->t_encap_decap_flags;
137 dst->t_flags = src->t_flags;
138 dst->t_mode = src->t_mode;
139 dst->t_table_id = src->t_table_id;
140 dst->t_dscp = src->t_dscp;
141 dst->t_hop_limit = src->t_hop_limit;
142 dst->t_fib_index = src->t_fib_index;
144 dst->t_flags &= ~TUNNEL_FLAG_RESOLVED;
145 dst->t_fib_entry_index = FIB_NODE_INDEX_INVALID;
150 format_tunnel (u8 *s, va_list *args)
152 const tunnel_t *t = va_arg (*args, tunnel_t *);
153 u32 indent = va_arg (*args, u32);
155 s = format (s, "%Utable-ID:%d [%U->%U] hop-limit:%d %U %U [%U] [%U]",
156 format_white_space, indent, t->t_table_id, format_ip_address,
157 &t->t_src, format_ip_address, &t->t_dst, t->t_hop_limit,
158 format_tunnel_mode, t->t_mode, format_ip_dscp, t->t_dscp,
159 format_tunnel_flags, t->t_flags, format_tunnel_encap_decap_flags,
160 t->t_encap_decap_flags);
161 if (t->t_flags & TUNNEL_FLAG_RESOLVED)
162 s = format (s, " [resolved via fib-entry: %d]", t->t_fib_entry_index);
168 unformat_tunnel (unformat_input_t *input, va_list *args)
170 tunnel_t *t = va_arg (*args, tunnel_t *);
172 if (!unformat (input, "tunnel"))
175 unformat (input, "src %U", unformat_ip_address, &t->t_src);
176 unformat (input, "dst %U", unformat_ip_address, &t->t_dst);
177 unformat (input, "table-id %d", &t->t_table_id);
178 unformat (input, "hop-limit %d", &t->t_hop_limit);
179 unformat (input, "%U", unformat_ip_dscp, &t->t_dscp);
180 unformat (input, "%U", unformat_tunnel_encap_decap_flags,
181 &t->t_encap_decap_flags);
182 unformat (input, "%U", unformat_tunnel_flags, &t->t_flags);
183 unformat (input, "%U", unformat_tunnel_mode, &t->t_mode);
189 tunnel_resolve (tunnel_t *t, fib_node_type_t child_type, index_t child_index)
193 ip_address_to_fib_prefix (&t->t_dst, &pfx);
195 t->t_fib_index = fib_table_find (pfx.fp_proto, t->t_table_id);
197 if (t->t_fib_index == ~((u32) 0))
198 return VNET_API_ERROR_NO_SUCH_FIB;
200 t->t_fib_entry_index = fib_entry_track (t->t_fib_index, &pfx, child_type,
201 child_index, &t->t_sibling);
203 t->t_flags |= TUNNEL_FLAG_RESOLVED;
209 tunnel_unresolve (tunnel_t *t)
211 if (t->t_flags & TUNNEL_FLAG_RESOLVED)
212 fib_entry_untrack (t->t_fib_entry_index, t->t_sibling);
214 t->t_flags &= ~TUNNEL_FLAG_RESOLVED;
218 tunnel_contribute_forwarding (const tunnel_t *t, dpo_id_t *dpo)
220 fib_forward_chain_type_t fct;
222 fct = fib_forw_chain_type_from_fib_proto (
223 ip_address_family_to_fib_proto (ip_addr_version (&t->t_src)));
225 fib_entry_contribute_forwarding (t->t_fib_entry_index, fct, dpo);
229 tunnel_build_v6_hdr (const tunnel_t *t, ip_protocol_t next_proto,
232 ip->ip_version_traffic_class_and_flow_label =
233 clib_host_to_net_u32 (0x60000000);
234 ip6_set_dscp_network_order (ip, t->t_dscp);
237 ip6_address_copy (&ip->src_address, &ip_addr_v6 (&t->t_src));
238 ip6_address_copy (&ip->dst_address, &ip_addr_v6 (&t->t_dst));
240 ip->protocol = next_proto;
241 ip->hop_limit = (t->t_hop_limit == 0 ? 254 : t->t_hop_limit);
242 ip6_set_flow_label_network_order (
243 ip, ip6_compute_flow_hash (ip, IP_FLOW_HASH_DEFAULT));
247 tunnel_build_v4_hdr (const tunnel_t *t, ip_protocol_t next_proto,
250 ip->ip_version_and_header_length = 0x45;
251 ip->ttl = (t->t_hop_limit == 0 ? 254 : t->t_hop_limit);
252 ip->src_address.as_u32 = t->t_src.ip.ip4.as_u32;
253 ip->dst_address.as_u32 = t->t_dst.ip.ip4.as_u32;
254 ip->tos = t->t_dscp << 2;
255 ip->protocol = next_proto;
256 ip->checksum = ip4_header_checksum (ip);
260 * fd.io coding-style-patch-verification: ON
263 * eval: (c-set-style "gnu")