blob: 3db22a838b223b95c101c357ba6bbb00e8fc5645 [file] [log] [blame]
Neale Rannse5b94dd2019-12-31 05:13:14 +00001/*
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
Neale Ranns4ec36c52020-03-31 09:21:29 -040021#include <vnet/tunnel/tunnel.h>
Neale Rannsa91cb452021-02-04 11:02:52 +000022#include <vnet/mpls/mpls_lookup.h>
Neale Ranns4ec36c52020-03-31 09:21:29 -040023
Neale Rannse5b94dd2019-12-31 05:13:14 +000024static_always_inline void
25tunnel_encap_fixup_4o4 (tunnel_encap_decap_flags_t flags,
26 const ip4_header_t * inner, ip4_header_t * outer)
27{
Neale Rannsa91cb452021-02-04 11:02:52 +000028 if (PREDICT_FALSE (flags & TUNNEL_ENCAP_DECAP_FLAG_ENCAP_COPY_DSCP))
Neale Rannse5b94dd2019-12-31 05:13:14 +000029 ip4_header_set_dscp (outer, ip4_header_get_dscp (inner));
Neale Rannsa91cb452021-02-04 11:02:52 +000030 if (PREDICT_FALSE (flags & TUNNEL_ENCAP_DECAP_FLAG_ENCAP_COPY_ECN))
Neale Rannse5b94dd2019-12-31 05:13:14 +000031 ip4_header_set_ecn (outer, ip4_header_get_ecn (inner));
Neale Rannsa91cb452021-02-04 11:02:52 +000032 if (PREDICT_FALSE ((flags & TUNNEL_ENCAP_DECAP_FLAG_ENCAP_COPY_DF) &&
33 ip4_header_get_df (inner)))
Neale Rannse5b94dd2019-12-31 05:13:14 +000034 ip4_header_set_df (outer);
Neale Rannsa91cb452021-02-04 11:02:52 +000035 if (PREDICT_FALSE (flags & TUNNEL_ENCAP_DECAP_FLAG_ENCAP_COPY_HOP_LIMIT))
36 ip4_header_set_ttl (outer, ip4_header_get_ttl (inner));
Neale Rannse5b94dd2019-12-31 05:13:14 +000037}
38
39static_always_inline void
Neale Ranns041add72020-01-02 04:06:10 +000040tunnel_encap_fixup_4o4_w_chksum (tunnel_encap_decap_flags_t flags,
41 const ip4_header_t * inner,
42 ip4_header_t * outer)
43{
Neale Rannsa91cb452021-02-04 11:02:52 +000044 if (PREDICT_FALSE (flags & (TUNNEL_ENCAP_DECAP_FLAG_ENCAP_COPY_DSCP |
45 TUNNEL_ENCAP_DECAP_FLAG_ENCAP_COPY_ECN)))
Neale Ranns041add72020-01-02 04:06:10 +000046 {
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 }
Neale Rannsa91cb452021-02-04 11:02:52 +000059 if (PREDICT_FALSE ((flags & TUNNEL_ENCAP_DECAP_FLAG_ENCAP_COPY_DF) &&
60 ip4_header_get_df (inner)))
Neale Ranns041add72020-01-02 04:06:10 +000061 {
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
74static_always_inline void
Neale Ranns4a58e492020-12-21 13:19:10 +000075tunnel_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
94static_always_inline void
Neale Rannse5b94dd2019-12-31 05:13:14 +000095tunnel_encap_fixup_6o4 (tunnel_encap_decap_flags_t flags,
96 const ip6_header_t * inner, ip4_header_t * outer)
97{
Neale Rannsa91cb452021-02-04 11:02:52 +000098 if (PREDICT_FALSE (flags & TUNNEL_ENCAP_DECAP_FLAG_ENCAP_COPY_DSCP))
Neale Rannse5b94dd2019-12-31 05:13:14 +000099 ip4_header_set_dscp (outer, ip6_dscp_network_order (inner));
Neale Rannsa91cb452021-02-04 11:02:52 +0000100 if (PREDICT_FALSE (flags & TUNNEL_ENCAP_DECAP_FLAG_ENCAP_COPY_ECN))
Neale Rannse5b94dd2019-12-31 05:13:14 +0000101 ip4_header_set_ecn (outer, ip6_ecn_network_order ((inner)));
Neale Rannsa91cb452021-02-04 11:02:52 +0000102 if (PREDICT_FALSE (flags & TUNNEL_ENCAP_DECAP_FLAG_ENCAP_COPY_HOP_LIMIT))
103 ip4_header_set_ttl (outer, ip6_hop_limit_network_order (inner));
Neale Rannse5b94dd2019-12-31 05:13:14 +0000104}
105
106static_always_inline void
Neale Ranns041add72020-01-02 04:06:10 +0000107tunnel_encap_fixup_6o4_w_chksum (tunnel_encap_decap_flags_t flags,
108 const ip6_header_t * inner,
109 ip4_header_t * outer)
110{
Neale Rannsa91cb452021-02-04 11:02:52 +0000111 if (PREDICT_FALSE (flags & (TUNNEL_ENCAP_DECAP_FLAG_ENCAP_COPY_DSCP |
112 TUNNEL_ENCAP_DECAP_FLAG_ENCAP_COPY_ECN)))
Neale Ranns041add72020-01-02 04:06:10 +0000113 {
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
128static_always_inline void
Neale Rannse5b94dd2019-12-31 05:13:14 +0000129tunnel_encap_fixup_6o6 (tunnel_encap_decap_flags_t flags,
130 const ip6_header_t * inner, ip6_header_t * outer)
131{
Neale Rannsa91cb452021-02-04 11:02:52 +0000132 if (PREDICT_FALSE (flags & TUNNEL_ENCAP_DECAP_FLAG_ENCAP_COPY_DSCP))
Neale Rannse5b94dd2019-12-31 05:13:14 +0000133 ip6_set_dscp_network_order (outer, ip6_dscp_network_order (inner));
Neale Rannsa91cb452021-02-04 11:02:52 +0000134 if (PREDICT_FALSE (flags & TUNNEL_ENCAP_DECAP_FLAG_ENCAP_COPY_ECN))
Neale Rannse5b94dd2019-12-31 05:13:14 +0000135 ip6_set_ecn_network_order (outer, ip6_ecn_network_order (inner));
Neale Rannsa91cb452021-02-04 11:02:52 +0000136 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));
Neale Rannse5b94dd2019-12-31 05:13:14 +0000142}
143
144static_always_inline void
145tunnel_encap_fixup_4o6 (tunnel_encap_decap_flags_t flags,
Neale Rannsa91cb452021-02-04 11:02:52 +0000146 const vlib_buffer_t *b, const ip4_header_t *inner,
147 ip6_header_t *outer)
Neale Rannse5b94dd2019-12-31 05:13:14 +0000148{
Neale Rannsa91cb452021-02-04 11:02:52 +0000149 if (PREDICT_FALSE (flags & TUNNEL_ENCAP_DECAP_FLAG_ENCAP_COPY_DSCP))
Neale Rannse5b94dd2019-12-31 05:13:14 +0000150 ip6_set_dscp_network_order (outer, ip4_header_get_dscp (inner));
Neale Rannsa91cb452021-02-04 11:02:52 +0000151 if (PREDICT_FALSE (flags & TUNNEL_ENCAP_DECAP_FLAG_ENCAP_COPY_ECN))
Neale Rannse5b94dd2019-12-31 05:13:14 +0000152 ip6_set_ecn_network_order (outer, ip4_header_get_ecn (inner));
Neale Rannsa91cb452021-02-04 11:02:52 +0000153 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)));
Neale Rannse5b94dd2019-12-31 05:13:14 +0000160}
161
162static_always_inline void
Neale Rannse294de62020-12-21 09:52:24 +0000163tunnel_encap_fixup_mplso6 (tunnel_encap_decap_flags_t flags,
Neale Rannsa91cb452021-02-04 11:02:52 +0000164 const vlib_buffer_t *b,
Neale Rannse294de62020-12-21 09:52:24 +0000165 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));
Neale Rannsa91cb452021-02-04 11:02:52 +0000171 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)));
Neale Rannse294de62020-12-21 09:52:24 +0000176}
177
178static_always_inline void
179tunnel_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
187static_always_inline void
Neale Rannse5b94dd2019-12-31 05:13:14 +0000188tunnel_decap_fixup_4o6 (tunnel_encap_decap_flags_t flags,
189 ip4_header_t * inner, const ip6_header_t * outer)
190{
Neale Rannsa91cb452021-02-04 11:02:52 +0000191 if (PREDICT_FALSE (flags & TUNNEL_ENCAP_DECAP_FLAG_DECAP_COPY_ECN))
Neale Rannse5b94dd2019-12-31 05:13:14 +0000192 ip4_header_set_ecn_w_chksum (inner, ip6_ecn_network_order (outer));
193}
194
195static_always_inline void
196tunnel_decap_fixup_6o6 (tunnel_encap_decap_flags_t flags,
197 ip6_header_t * inner, const ip6_header_t * outer)
198{
Neale Rannsa91cb452021-02-04 11:02:52 +0000199 if (PREDICT_FALSE (flags & TUNNEL_ENCAP_DECAP_FLAG_DECAP_COPY_ECN))
Neale Rannse5b94dd2019-12-31 05:13:14 +0000200 ip6_set_ecn_network_order (inner, ip6_ecn_network_order (outer));
201}
202
203static_always_inline void
204tunnel_decap_fixup_6o4 (tunnel_encap_decap_flags_t flags,
205 ip6_header_t * inner, const ip4_header_t * outer)
206{
Neale Rannsa91cb452021-02-04 11:02:52 +0000207 if (PREDICT_FALSE (flags & TUNNEL_ENCAP_DECAP_FLAG_DECAP_COPY_ECN))
Neale Rannse5b94dd2019-12-31 05:13:14 +0000208 ip6_set_ecn_network_order (inner, ip4_header_get_ecn (outer));
209}
210
211static_always_inline void
212tunnel_decap_fixup_4o4 (tunnel_encap_decap_flags_t flags,
213 ip4_header_t * inner, const ip4_header_t * outer)
214{
Neale Rannsa91cb452021-02-04 11:02:52 +0000215 if (PREDICT_FALSE (flags & TUNNEL_ENCAP_DECAP_FLAG_DECAP_COPY_ECN))
Neale Rannse5b94dd2019-12-31 05:13:14 +0000216 ip4_header_set_ecn_w_chksum (inner, ip4_header_get_ecn (outer));
217}
218
Neale Rannse294de62020-12-21 09:52:24 +0000219static_always_inline void
220tunnel_decap_fixup_mplso6 (tunnel_encap_decap_flags_t flags,
221 mpls_unicast_header_t *inner,
222 const ip6_header_t *outer)
223{
224}
225
226static_always_inline void
227tunnel_decap_fixup_mplso4 (tunnel_encap_decap_flags_t flags,
228 mpls_unicast_header_t *inner,
229 const ip4_header_t *outer)
230{
231}
232
Neale Rannse5b94dd2019-12-31 05:13:14 +0000233#endif
234
235/*
236 * fd.io coding-style-patch-verification: ON
237 *
238 * Local Variables:
239 * eval: (c-set-style "gnu")
240 * End:
241 */