FD.io VPP  v21.10.1-2-g0a485f517
Vector Packet Processing
tunnel.c
Go to the documentation of this file.
1 /*
2  * tunnel.h: shared definitions for tunnels.
3  *
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:
8  *
9  * http://www.apache.org/licenses/LICENSE-2.0
10  *
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.
16  */
17 
18 #include <vnet/tunnel/tunnel.h>
19 #include <vnet/fib/fib_table.h>
21 
22 #include <vnet/ip/ip6_inlines.h>
23 
25 #define _(a, b, c) TUNNEL_ENCAP_DECAP_FLAG_##a |
27 #undef _
28  0);
30 #define _(a, b, c) TUNNEL_FLAG_##a |
32 #undef _
33  0);
34 
35 u8 *
36 format_tunnel_mode (u8 * s, va_list * args)
37 {
38  tunnel_mode_t mode = va_arg (*args, int);
39 
40  switch (mode)
41  {
42 #define _(n, v) case TUNNEL_MODE_##n: \
43  s = format (s, "%s", v); \
44  break;
46 #undef _
47  }
48 
49  return (s);
50 }
51 
52 uword
53 unformat_tunnel_mode (unformat_input_t * input, va_list * args)
54 {
55  tunnel_mode_t *m = va_arg (*args, tunnel_mode_t *);
56 
57  if (unformat (input, "p2p"))
58  *m = TUNNEL_MODE_P2P;
59  else if (unformat (input, "p2mp") || unformat (input, "mp"))
60  *m = TUNNEL_MODE_MP;
61  else
62  return 0;
63  return 1;
64 }
65 
66 u8 *
67 format_tunnel_encap_decap_flags (u8 * s, va_list * args)
68 {
69  tunnel_encap_decap_flags_t f = va_arg (*args, int);
70 
71  if (f == TUNNEL_ENCAP_DECAP_FLAG_NONE)
72  s = format (s, "none");
73 
74 #define _(a, b, c) \
75  else if (f & TUNNEL_ENCAP_DECAP_FLAG_##a) s = format (s, "%s ", b);
77 #undef _
78  return (s);
79 }
80 
81 uword
83 {
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;\
88  return 1;\
89  }
91 #undef _
92  return 0;
93 }
94 
95 u8 *
96 format_tunnel_flags (u8 *s, va_list *args)
97 {
98  tunnel_flags_t f = va_arg (*args, int);
99 
100  if (f == TUNNEL_FLAG_NONE)
101  s = format (s, "none");
102 
103 #define _(a, b, c) else if (f & TUNNEL_FLAG_##a) s = format (s, "%s ", c);
105 #undef _
106  return (s);
107 }
108 
109 uword
111 {
112  tunnel_flags_t *f = va_arg (*args, tunnel_flags_t *);
113 #define _(a, b, c) \
114  if (unformat (input, c)) \
115  { \
116  *f |= TUNNEL_FLAG_##a; \
117  return 1; \
118  }
120 #undef _
121  return 0;
122 }
123 
126 {
127  return (ip_addr_version (&t->t_src));
128 }
129 
130 void
132 {
133  ip_address_copy (&dst->t_dst, &src->t_dst);
134  ip_address_copy (&dst->t_src, &src->t_src);
135 
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;
143 
144  dst->t_flags &= ~TUNNEL_FLAG_RESOLVED;
145  dst->t_fib_entry_index = FIB_NODE_INDEX_INVALID;
146  dst->t_sibling = ~0;
147 }
148 
149 u8 *
150 format_tunnel (u8 *s, va_list *args)
151 {
152  const tunnel_t *t = va_arg (*args, tunnel_t *);
153  u32 indent = va_arg (*args, u32);
154 
155  s = format (s, "%Utable-ID:%d [%U->%U] hop-limit:%d %U %U [%U] [%U]",
161  if (t->t_flags & TUNNEL_FLAG_RESOLVED)
162  s = format (s, " [resolved via fib-entry: %d]", t->t_fib_entry_index);
163 
164  return (s);
165 }
166 
167 uword
168 unformat_tunnel (unformat_input_t *input, va_list *args)
169 {
170  tunnel_t *t = va_arg (*args, tunnel_t *);
171 
172  if (!unformat (input, "tunnel"))
173  return (0);
174 
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);
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);
184 
185  return (1);
186 }
187 
188 int
189 tunnel_resolve (tunnel_t *t, fib_node_type_t child_type, index_t child_index)
190 {
191  fib_prefix_t pfx;
192 
193  ip_address_to_fib_prefix (&t->t_dst, &pfx);
194 
196 
197  if (t->t_fib_index == ~((u32) 0))
198  return VNET_API_ERROR_NO_SUCH_FIB;
199 
200  t->t_fib_entry_index = fib_entry_track (t->t_fib_index, &pfx, child_type,
201  child_index, &t->t_sibling);
202 
203  t->t_flags |= TUNNEL_FLAG_RESOLVED;
204 
205  return (0);
206 }
207 
208 void
210 {
211  if (t->t_flags & TUNNEL_FLAG_RESOLVED)
213 
214  t->t_flags &= ~TUNNEL_FLAG_RESOLVED;
215 }
216 
217 void
219 {
221 
224 
226 }
227 
228 void
230  ip6_header_t *ip)
231 {
232  ip->ip_version_traffic_class_and_flow_label =
233  clib_host_to_net_u32 (0x60000000);
235 
236  ip->hop_limit = 254;
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));
239 
240  ip->protocol = next_proto;
241  ip->hop_limit = (t->t_hop_limit == 0 ? 254 : t->t_hop_limit);
244 }
245 
246 void
248  ip4_header_t *ip)
249 {
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);
257 }
258 
259 /*
260  * fd.io coding-style-patch-verification: ON
261  *
262  * Local Variables:
263  * eval: (c-set-style "gnu")
264  * End:
265  */
tunnel_t_::t_dscp
ip_dscp_t t_dscp
Definition: tunnel.h:93
tunnel_get_af
ip_address_family_t tunnel_get_af(const tunnel_t *t)
Definition: tunnel.c:125
tunnel_encap_decap_flags_t
enum tunnel_encap_decap_flags_t_ tunnel_encap_decap_flags_t
format_tunnel_flags
u8 * format_tunnel_flags(u8 *s, va_list *args)
Definition: tunnel.c:96
tunnel_t_::t_src
ip_address_t t_src
Definition: tunnel.h:87
f
vlib_frame_t * f
Definition: interface_output.c:1098
tunnel_t_
A representation of an IP tunnel config.
Definition: tunnel.h:85
tunnel_flags_t
enum tunnel_flags_t_ tunnel_flags_t
fib_entry_untrack
void fib_entry_untrack(fib_node_index_t fei, u32 sibling)
Stop tracking a FIB entry.
Definition: fib_entry_track.c:79
tunnel.h
tunnel_t_::t_fib_index
u32 t_fib_index
derived data
Definition: tunnel.h:99
TUNNEL_FLAG_MASK
const u8 TUNNEL_FLAG_MASK
Definition: tunnel.c:29
FIB_NODE_INDEX_INVALID
#define FIB_NODE_INDEX_INVALID
Definition: fib_types.h:30
fib_table.h
mode
vl_api_tunnel_mode_t mode
Definition: gre.api:48
ip_address_to_fib_prefix
void ip_address_to_fib_prefix(const ip_address_t *addr, fib_prefix_t *prefix)
convert from a IP address to a FIB prefix
Definition: ip_types.c:270
unformat_tunnel
uword unformat_tunnel(unformat_input_t *input, va_list *args)
Definition: tunnel.c:168
tunnel_t_::t_fib_entry_index
fib_node_index_t t_fib_entry_index
Definition: tunnel.h:101
tunnel_contribute_forwarding
void tunnel_contribute_forwarding(const tunnel_t *t, dpo_id_t *dpo)
Definition: tunnel.c:218
ip6_set_dscp_network_order
static_always_inline void ip6_set_dscp_network_order(ip6_header_t *ip6, ip_dscp_t dscp)
Definition: ip6_packet.h:357
foreach_tunnel_mode
@ foreach_tunnel_mode
Definition: tunnel.h:31
TUNNEL_FLAG_NONE
@ TUNNEL_FLAG_NONE
Definition: tunnel.h:71
tunnel_copy
void tunnel_copy(const tunnel_t *src, tunnel_t *dst)
Definition: tunnel.c:131
unformat_input_t
struct _unformat_input_t unformat_input_t
fib_node_type_t
enum fib_node_type_t_ fib_node_type_t
The types of nodes in a FIB graph.
foreach_tunnel_encap_decap_flag
@ foreach_tunnel_encap_decap_flag
Definition: tunnel.h:55
ip4_header_t
Definition: ip4_packet.h:87
unformat_tunnel_mode
uword unformat_tunnel_mode(unformat_input_t *input, va_list *args)
Definition: tunnel.c:53
fib_entry_contribute_forwarding
void fib_entry_contribute_forwarding(fib_node_index_t fib_entry_index, fib_forward_chain_type_t fct, dpo_id_t *dpo)
Definition: fib_entry.c:437
unformat
uword unformat(unformat_input_t *i, const char *fmt,...)
Definition: unformat.c:978
format_tunnel_mode
u8 * format_tunnel_mode(u8 *s, va_list *args)
Definition: tunnel.c:36
fib_entry_track
fib_node_index_t fib_entry_track(u32 fib_index, const fib_prefix_t *prefix, fib_node_type_t child_type, index_t child_index, u32 *sibling)
Trackers are used on FIB entries by objects that which to track the changing state of the entry.
Definition: fib_entry_track.c:49
tunnel_build_v4_hdr
void tunnel_build_v4_hdr(const tunnel_t *t, ip_protocol_t next_proto, ip4_header_t *ip)
Definition: tunnel.c:247
ip_addr_version
#define ip_addr_version(_a)
Definition: ip_types.h:93
unformat_ip_address
uword unformat_ip_address(unformat_input_t *input, va_list *args)
Definition: ip_types.c:41
ip6_compute_flow_hash
static u32 ip6_compute_flow_hash(const ip6_header_t *ip, flow_hash_config_t flow_hash_config)
Definition: ip6_inlines.h:49
fib_forward_chain_type_t
enum fib_forward_chain_type_t_ fib_forward_chain_type_t
FIB output chain type.
ip_addr_v6
#define ip_addr_v6(_a)
Definition: ip_types.h:92
index_t
u32 index_t
A Data-Path Object is an object that represents actions that are applied to packets are they are swit...
Definition: dpo.h:43
IP_FLOW_HASH_DEFAULT
#define IP_FLOW_HASH_DEFAULT
Default: 5-tuple + flowlabel without the "reverse" bit.
Definition: ip_flow_hash.h:22
format_ip_address
u8 * format_ip_address(u8 *s, va_list *args)
Definition: ip_types.c:21
uword
u64 uword
Definition: types.h:112
unformat_tunnel_encap_decap_flags
uword unformat_tunnel_encap_decap_flags(unformat_input_t *input, va_list *args)
Definition: tunnel.c:82
src
vl_api_address_t src
Definition: gre.api:54
ip6_address_copy
static void ip6_address_copy(ip6_address_t *dst, const ip6_address_t *src)
Definition: ip6_packet.h:127
ip6_inlines.h
ip6_set_flow_label_network_order
static_always_inline void ip6_set_flow_label_network_order(ip6_header_t *ip6, u32 flow_label)
Definition: ip6_packet.h:385
ip_address_family_to_fib_proto
fib_protocol_t ip_address_family_to_fib_proto(ip_address_family_t af)
Definition: ip_types.c:223
fib_entry_track.h
fib_forw_chain_type_from_fib_proto
fib_forward_chain_type_t fib_forw_chain_type_from_fib_proto(fib_protocol_t proto)
Convert from a fib-protocol to a chain type.
Definition: fib_types.c:449
format
description fragment has unexpected format
Definition: map.api:433
tunnel_build_v6_hdr
void tunnel_build_v6_hdr(const tunnel_t *t, ip_protocol_t next_proto, ip6_header_t *ip)
Definition: tunnel.c:229
u32
unsigned int u32
Definition: types.h:88
tunnel_t_::t_mode
tunnel_mode_t t_mode
Definition: tunnel.h:91
tunnel_t_::t_dst
ip_address_t t_dst
Definition: tunnel.h:88
dst
vl_api_ip4_address_t dst
Definition: pnat.api:41
tunnel_resolve
int tunnel_resolve(tunnel_t *t, fib_node_type_t child_type, index_t child_index)
Definition: tunnel.c:189
unformat_tunnel_flags
uword unformat_tunnel_flags(unformat_input_t *input, va_list *args)
Definition: tunnel.c:110
tunnel_t_::t_table_id
u32 t_table_id
Definition: tunnel.h:92
fib_prefix_t_::fp_proto
fib_protocol_t fp_proto
protocol type
Definition: fib_types.h:211
ip6_header_t
Definition: ip6_packet.h:294
next_proto
vl_api_udp_decap_next_proto_t next_proto
Definition: udp.api:65
format_tunnel_encap_decap_flags
u8 * format_tunnel_encap_decap_flags(u8 *s, va_list *args)
Definition: tunnel.c:67
ip_protocol_t
enum ip_protocol ip_protocol_t
tunnel_mode_t
enum tunnel_mode_t_ tunnel_mode_t
u8
unsigned char u8
Definition: types.h:56
ip
vl_api_address_t ip
Definition: l2.api:558
ip4_header_checksum
static u16 ip4_header_checksum(ip4_header_t *i)
Definition: ip4_packet.h:314
TUNNEL_ENCAP_DECAP_FLAG_MASK
const u8 TUNNEL_ENCAP_DECAP_FLAG_MASK
Definition: tunnel.c:24
tunnel_t_::t_encap_decap_flags
tunnel_encap_decap_flags_t t_encap_decap_flags
Definition: tunnel.h:89
foreach_tunnel_flag
@ foreach_tunnel_flag
Definition: tunnel.h:73
ip_address::ip
ip46_address_t ip
Definition: ip_types.h:81
dpo_id_t_
The identity of a DPO is a combination of its type and its instance number/index of objects of that t...
Definition: dpo.h:172
format_tunnel
u8 * format_tunnel(u8 *s, va_list *args)
Definition: tunnel.c:150
ip_address_copy
void ip_address_copy(ip_address_t *dst, const ip_address_t *src)
Definition: ip_types.c:133
fib_table_find
u32 fib_table_find(fib_protocol_t proto, u32 table_id)
Get the index of the FIB for a Table-ID.
Definition: fib_table.c:1111
unformat_ip_dscp
unformat_function_t unformat_ip_dscp
Definition: ip_packet.h:123
tunnel_t_::t_hop_limit
u8 t_hop_limit
Definition: tunnel.h:94
format_ip_dscp
u8 * format_ip_dscp(u8 *s, va_list *va)
Definition: ip.c:285
fib_prefix_t_
Aggregate type for a prefix.
Definition: fib_types.h:202
tunnel_unresolve
void tunnel_unresolve(tunnel_t *t)
Definition: tunnel.c:209
format_white_space
u8 * format_white_space(u8 *s, va_list *va)
Definition: std-formats.c:129
tunnel_t_::t_sibling
u32 t_sibling
Definition: tunnel.h:102
tunnel_t_::t_flags
tunnel_flags_t t_flags
Definition: tunnel.h:90
ip_address_family_t
enum ip_address_family_t_ ip_address_family_t