tunnel: support copying TTL and flow label from inner to outer
[vpp.git] / src / vnet / tunnel / tunnel_dp.h
1 /*
2  * tunnel_dp.h: data-plane functions 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 #ifndef __TUNNEL_DP_H__
19 #define __TUNNEL_DP_H__
20
21 #include <vnet/tunnel/tunnel.h>
22 #include <vnet/mpls/mpls_lookup.h>
23
24 static_always_inline void
25 tunnel_encap_fixup_4o4 (tunnel_encap_decap_flags_t flags,
26                         const ip4_header_t * inner, ip4_header_t * outer)
27 {
28   if (PREDICT_FALSE (flags & TUNNEL_ENCAP_DECAP_FLAG_ENCAP_COPY_DSCP))
29     ip4_header_set_dscp (outer, ip4_header_get_dscp (inner));
30   if (PREDICT_FALSE (flags & TUNNEL_ENCAP_DECAP_FLAG_ENCAP_COPY_ECN))
31     ip4_header_set_ecn (outer, ip4_header_get_ecn (inner));
32   if (PREDICT_FALSE ((flags & TUNNEL_ENCAP_DECAP_FLAG_ENCAP_COPY_DF) &&
33                      ip4_header_get_df (inner)))
34     ip4_header_set_df (outer);
35   if (PREDICT_FALSE (flags & TUNNEL_ENCAP_DECAP_FLAG_ENCAP_COPY_HOP_LIMIT))
36     ip4_header_set_ttl (outer, ip4_header_get_ttl (inner));
37 }
38
39 static_always_inline void
40 tunnel_encap_fixup_4o4_w_chksum (tunnel_encap_decap_flags_t flags,
41                                  const ip4_header_t * inner,
42                                  ip4_header_t * outer)
43 {
44   if (PREDICT_FALSE (flags & (TUNNEL_ENCAP_DECAP_FLAG_ENCAP_COPY_DSCP |
45                               TUNNEL_ENCAP_DECAP_FLAG_ENCAP_COPY_ECN)))
46     {
47       ip_csum_t sum = outer->checksum;
48       u8 tos = outer->tos;
49
50       if (flags & TUNNEL_ENCAP_DECAP_FLAG_ENCAP_COPY_DSCP)
51         ip4_header_set_dscp (outer, ip4_header_get_dscp (inner));
52       if (flags & TUNNEL_ENCAP_DECAP_FLAG_ENCAP_COPY_ECN)
53         ip4_header_set_ecn (outer, ip4_header_get_ecn (inner));
54
55       sum =
56         ip_csum_update (outer->checksum, tos, outer->tos, ip4_header_t, tos);
57       outer->checksum = ip_csum_fold (sum);
58     }
59   if (PREDICT_FALSE ((flags & TUNNEL_ENCAP_DECAP_FLAG_ENCAP_COPY_DF) &&
60                      ip4_header_get_df (inner)))
61     {
62       ip_csum_t sum = outer->checksum;
63       u16 tos = outer->flags_and_fragment_offset;
64
65       ip4_header_set_df (outer);
66
67       sum =
68         ip_csum_update (outer->checksum, tos, outer->tos, ip4_header_t,
69                         flags_and_fragment_offset);
70       outer->checksum = ip_csum_fold (sum);
71     }
72 }
73
74 static_always_inline void
75 tunnel_encap_fixup_mplso4_w_chksum (tunnel_encap_decap_flags_t flags,
76                                     const mpls_unicast_header_t *inner,
77                                     ip4_header_t *outer)
78 {
79   if (flags & TUNNEL_ENCAP_DECAP_FLAG_ENCAP_COPY_DSCP)
80     {
81       ip_csum_t sum = outer->checksum;
82       u8 tos = outer->tos;
83
84       if (flags & TUNNEL_ENCAP_DECAP_FLAG_ENCAP_COPY_DSCP)
85         ip4_header_set_dscp (outer,
86                              vnet_mpls_uc_get_exp (inner->label_exp_s_ttl));
87
88       sum =
89         ip_csum_update (outer->checksum, tos, outer->tos, ip4_header_t, tos);
90       outer->checksum = ip_csum_fold (sum);
91     }
92 }
93
94 static_always_inline void
95 tunnel_encap_fixup_6o4 (tunnel_encap_decap_flags_t flags,
96                         const ip6_header_t * inner, ip4_header_t * outer)
97 {
98   if (PREDICT_FALSE (flags & TUNNEL_ENCAP_DECAP_FLAG_ENCAP_COPY_DSCP))
99     ip4_header_set_dscp (outer, ip6_dscp_network_order (inner));
100   if (PREDICT_FALSE (flags & TUNNEL_ENCAP_DECAP_FLAG_ENCAP_COPY_ECN))
101     ip4_header_set_ecn (outer, ip6_ecn_network_order ((inner)));
102   if (PREDICT_FALSE (flags & TUNNEL_ENCAP_DECAP_FLAG_ENCAP_COPY_HOP_LIMIT))
103     ip4_header_set_ttl (outer, ip6_hop_limit_network_order (inner));
104 }
105
106 static_always_inline void
107 tunnel_encap_fixup_6o4_w_chksum (tunnel_encap_decap_flags_t flags,
108                                  const ip6_header_t * inner,
109                                  ip4_header_t * outer)
110 {
111   if (PREDICT_FALSE (flags & (TUNNEL_ENCAP_DECAP_FLAG_ENCAP_COPY_DSCP |
112                               TUNNEL_ENCAP_DECAP_FLAG_ENCAP_COPY_ECN)))
113     {
114       ip_csum_t sum = outer->checksum;
115       u8 tos = outer->tos;
116
117       if (flags & TUNNEL_ENCAP_DECAP_FLAG_ENCAP_COPY_DSCP)
118         ip4_header_set_dscp (outer, ip6_dscp_network_order (inner));
119       if (flags & TUNNEL_ENCAP_DECAP_FLAG_ENCAP_COPY_ECN)
120         ip4_header_set_ecn (outer, ip6_ecn_network_order ((inner)));
121
122       sum =
123         ip_csum_update (outer->checksum, tos, outer->tos, ip4_header_t, tos);
124       outer->checksum = ip_csum_fold (sum);
125     }
126 }
127
128 static_always_inline void
129 tunnel_encap_fixup_6o6 (tunnel_encap_decap_flags_t flags,
130                         const ip6_header_t * inner, ip6_header_t * outer)
131 {
132   if (PREDICT_FALSE (flags & TUNNEL_ENCAP_DECAP_FLAG_ENCAP_COPY_DSCP))
133     ip6_set_dscp_network_order (outer, ip6_dscp_network_order (inner));
134   if (PREDICT_FALSE (flags & TUNNEL_ENCAP_DECAP_FLAG_ENCAP_COPY_ECN))
135     ip6_set_ecn_network_order (outer, ip6_ecn_network_order (inner));
136   if (PREDICT_FALSE (flags & TUNNEL_ENCAP_DECAP_FLAG_ENCAP_COPY_FLOW_LABEL))
137     ip6_set_flow_label_network_order (outer,
138                                       ip6_flow_label_network_order (inner));
139   if (PREDICT_FALSE (flags & TUNNEL_ENCAP_DECAP_FLAG_ENCAP_COPY_HOP_LIMIT))
140     ip6_set_hop_limit_network_order (outer,
141                                      ip6_hop_limit_network_order (inner));
142 }
143
144 static_always_inline void
145 tunnel_encap_fixup_4o6 (tunnel_encap_decap_flags_t flags,
146                         const vlib_buffer_t *b, const ip4_header_t *inner,
147                         ip6_header_t *outer)
148 {
149   if (PREDICT_FALSE (flags & TUNNEL_ENCAP_DECAP_FLAG_ENCAP_COPY_DSCP))
150     ip6_set_dscp_network_order (outer, ip4_header_get_dscp (inner));
151   if (PREDICT_FALSE (flags & TUNNEL_ENCAP_DECAP_FLAG_ENCAP_COPY_ECN))
152     ip6_set_ecn_network_order (outer, ip4_header_get_ecn (inner));
153   if (PREDICT_FALSE (flags & TUNNEL_ENCAP_DECAP_FLAG_ENCAP_COPY_HOP_LIMIT))
154     ip6_set_hop_limit_network_order (outer, ip4_header_get_ttl (inner));
155   if (PREDICT_FALSE (flags & TUNNEL_ENCAP_DECAP_FLAG_ENCAP_COPY_FLOW_LABEL))
156     ip6_set_flow_label_network_order (
157       outer, (0 != vnet_buffer (b)->ip.flow_hash ?
158                 vnet_buffer (b)->ip.flow_hash :
159                 ip4_compute_flow_hash (inner, IP_FLOW_HASH_DEFAULT)));
160 }
161
162 static_always_inline void
163 tunnel_encap_fixup_mplso6 (tunnel_encap_decap_flags_t flags,
164                            const vlib_buffer_t *b,
165                            const mpls_unicast_header_t *inner,
166                            ip6_header_t *outer)
167 {
168   if (flags & TUNNEL_ENCAP_DECAP_FLAG_ENCAP_COPY_DSCP)
169     ip6_set_dscp_network_order (outer,
170                                 vnet_mpls_uc_get_exp (inner->label_exp_s_ttl));
171   if (PREDICT_FALSE (flags & TUNNEL_ENCAP_DECAP_FLAG_ENCAP_COPY_FLOW_LABEL))
172     ip6_set_flow_label_network_order (
173       outer, (0 != vnet_buffer (b)->ip.flow_hash ?
174                 vnet_buffer (b)->ip.flow_hash :
175                 mpls_compute_flow_hash (inner, IP_FLOW_HASH_DEFAULT)));
176 }
177
178 static_always_inline void
179 tunnel_encap_fixup_mplso4 (tunnel_encap_decap_flags_t flags,
180                            const mpls_unicast_header_t *inner,
181                            ip4_header_t *outer)
182 {
183   if (flags & TUNNEL_ENCAP_DECAP_FLAG_ENCAP_COPY_DSCP)
184     ip4_header_set_dscp (outer, vnet_mpls_uc_get_exp (inner->label_exp_s_ttl));
185 }
186
187 static_always_inline void
188 tunnel_decap_fixup_4o6 (tunnel_encap_decap_flags_t flags,
189                         ip4_header_t * inner, const ip6_header_t * outer)
190 {
191   if (PREDICT_FALSE (flags & TUNNEL_ENCAP_DECAP_FLAG_DECAP_COPY_ECN))
192     ip4_header_set_ecn_w_chksum (inner, ip6_ecn_network_order (outer));
193 }
194
195 static_always_inline void
196 tunnel_decap_fixup_6o6 (tunnel_encap_decap_flags_t flags,
197                         ip6_header_t * inner, const ip6_header_t * outer)
198 {
199   if (PREDICT_FALSE (flags & TUNNEL_ENCAP_DECAP_FLAG_DECAP_COPY_ECN))
200     ip6_set_ecn_network_order (inner, ip6_ecn_network_order (outer));
201 }
202
203 static_always_inline void
204 tunnel_decap_fixup_6o4 (tunnel_encap_decap_flags_t flags,
205                         ip6_header_t * inner, const ip4_header_t * outer)
206 {
207   if (PREDICT_FALSE (flags & TUNNEL_ENCAP_DECAP_FLAG_DECAP_COPY_ECN))
208     ip6_set_ecn_network_order (inner, ip4_header_get_ecn (outer));
209 }
210
211 static_always_inline void
212 tunnel_decap_fixup_4o4 (tunnel_encap_decap_flags_t flags,
213                         ip4_header_t * inner, const ip4_header_t * outer)
214 {
215   if (PREDICT_FALSE (flags & TUNNEL_ENCAP_DECAP_FLAG_DECAP_COPY_ECN))
216     ip4_header_set_ecn_w_chksum (inner, ip4_header_get_ecn (outer));
217 }
218
219 static_always_inline void
220 tunnel_decap_fixup_mplso6 (tunnel_encap_decap_flags_t flags,
221                            mpls_unicast_header_t *inner,
222                            const ip6_header_t *outer)
223 {
224 }
225
226 static_always_inline void
227 tunnel_decap_fixup_mplso4 (tunnel_encap_decap_flags_t flags,
228                            mpls_unicast_header_t *inner,
229                            const ip4_header_t *outer)
230 {
231 }
232
233 #endif
234
235 /*
236  * fd.io coding-style-patch-verification: ON
237  *
238  * Local Variables:
239  * eval: (c-set-style "gnu")
240  * End:
241  */