2 * tunnel_dp.h: data-plane functions 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 #ifndef __TUNNEL_DP_H__
19 #define __TUNNEL_DP_H__
21 #include <vnet/tunnel/tunnel.h>
23 static_always_inline void
24 tunnel_encap_fixup_4o4 (tunnel_encap_decap_flags_t flags,
25 const ip4_header_t * inner, ip4_header_t * outer)
27 if (flags & TUNNEL_ENCAP_DECAP_FLAG_ENCAP_COPY_DSCP)
28 ip4_header_set_dscp (outer, ip4_header_get_dscp (inner));
29 if (flags & TUNNEL_ENCAP_DECAP_FLAG_ENCAP_COPY_ECN)
30 ip4_header_set_ecn (outer, ip4_header_get_ecn (inner));
31 if ((flags & TUNNEL_ENCAP_DECAP_FLAG_ENCAP_COPY_DF) &&
32 ip4_header_get_df (inner))
33 ip4_header_set_df (outer);
36 static_always_inline void
37 tunnel_encap_fixup_4o4_w_chksum (tunnel_encap_decap_flags_t flags,
38 const ip4_header_t * inner,
41 if (flags & (TUNNEL_ENCAP_DECAP_FLAG_ENCAP_COPY_DSCP |
42 TUNNEL_ENCAP_DECAP_FLAG_ENCAP_COPY_ECN))
44 ip_csum_t sum = outer->checksum;
47 if (flags & TUNNEL_ENCAP_DECAP_FLAG_ENCAP_COPY_DSCP)
48 ip4_header_set_dscp (outer, ip4_header_get_dscp (inner));
49 if (flags & TUNNEL_ENCAP_DECAP_FLAG_ENCAP_COPY_ECN)
50 ip4_header_set_ecn (outer, ip4_header_get_ecn (inner));
53 ip_csum_update (outer->checksum, tos, outer->tos, ip4_header_t, tos);
54 outer->checksum = ip_csum_fold (sum);
56 if ((flags & TUNNEL_ENCAP_DECAP_FLAG_ENCAP_COPY_DF) &&
57 ip4_header_get_df (inner))
59 ip_csum_t sum = outer->checksum;
60 u16 tos = outer->flags_and_fragment_offset;
62 ip4_header_set_df (outer);
65 ip_csum_update (outer->checksum, tos, outer->tos, ip4_header_t,
66 flags_and_fragment_offset);
67 outer->checksum = ip_csum_fold (sum);
71 static_always_inline void
72 tunnel_encap_fixup_mplso4_w_chksum (tunnel_encap_decap_flags_t flags,
73 const mpls_unicast_header_t *inner,
76 if (flags & TUNNEL_ENCAP_DECAP_FLAG_ENCAP_COPY_DSCP)
78 ip_csum_t sum = outer->checksum;
81 if (flags & TUNNEL_ENCAP_DECAP_FLAG_ENCAP_COPY_DSCP)
82 ip4_header_set_dscp (outer,
83 vnet_mpls_uc_get_exp (inner->label_exp_s_ttl));
86 ip_csum_update (outer->checksum, tos, outer->tos, ip4_header_t, tos);
87 outer->checksum = ip_csum_fold (sum);
91 static_always_inline void
92 tunnel_encap_fixup_6o4 (tunnel_encap_decap_flags_t flags,
93 const ip6_header_t * inner, ip4_header_t * outer)
95 if (flags & TUNNEL_ENCAP_DECAP_FLAG_ENCAP_COPY_DSCP)
96 ip4_header_set_dscp (outer, ip6_dscp_network_order (inner));
97 if (flags & TUNNEL_ENCAP_DECAP_FLAG_ENCAP_COPY_ECN)
98 ip4_header_set_ecn (outer, ip6_ecn_network_order ((inner)));
101 static_always_inline void
102 tunnel_encap_fixup_6o4_w_chksum (tunnel_encap_decap_flags_t flags,
103 const ip6_header_t * inner,
104 ip4_header_t * outer)
106 if (flags & (TUNNEL_ENCAP_DECAP_FLAG_ENCAP_COPY_DSCP |
107 TUNNEL_ENCAP_DECAP_FLAG_ENCAP_COPY_ECN))
109 ip_csum_t sum = outer->checksum;
112 if (flags & TUNNEL_ENCAP_DECAP_FLAG_ENCAP_COPY_DSCP)
113 ip4_header_set_dscp (outer, ip6_dscp_network_order (inner));
114 if (flags & TUNNEL_ENCAP_DECAP_FLAG_ENCAP_COPY_ECN)
115 ip4_header_set_ecn (outer, ip6_ecn_network_order ((inner)));
118 ip_csum_update (outer->checksum, tos, outer->tos, ip4_header_t, tos);
119 outer->checksum = ip_csum_fold (sum);
123 static_always_inline void
124 tunnel_encap_fixup_6o6 (tunnel_encap_decap_flags_t flags,
125 const ip6_header_t * inner, ip6_header_t * outer)
127 if (flags & TUNNEL_ENCAP_DECAP_FLAG_ENCAP_COPY_DSCP)
128 ip6_set_dscp_network_order (outer, ip6_dscp_network_order (inner));
129 if (flags & TUNNEL_ENCAP_DECAP_FLAG_ENCAP_COPY_ECN)
130 ip6_set_ecn_network_order (outer, ip6_ecn_network_order (inner));
133 static_always_inline void
134 tunnel_encap_fixup_4o6 (tunnel_encap_decap_flags_t flags,
135 const ip4_header_t * inner, ip6_header_t * outer)
137 if (flags & TUNNEL_ENCAP_DECAP_FLAG_ENCAP_COPY_DSCP)
138 ip6_set_dscp_network_order (outer, ip4_header_get_dscp (inner));
139 if (flags & TUNNEL_ENCAP_DECAP_FLAG_ENCAP_COPY_ECN)
140 ip6_set_ecn_network_order (outer, ip4_header_get_ecn (inner));
143 static_always_inline void
144 tunnel_encap_fixup_mplso6 (tunnel_encap_decap_flags_t flags,
145 const mpls_unicast_header_t *inner,
148 if (flags & TUNNEL_ENCAP_DECAP_FLAG_ENCAP_COPY_DSCP)
149 ip6_set_dscp_network_order (outer,
150 vnet_mpls_uc_get_exp (inner->label_exp_s_ttl));
153 static_always_inline void
154 tunnel_encap_fixup_mplso4 (tunnel_encap_decap_flags_t flags,
155 const mpls_unicast_header_t *inner,
158 if (flags & TUNNEL_ENCAP_DECAP_FLAG_ENCAP_COPY_DSCP)
159 ip4_header_set_dscp (outer, vnet_mpls_uc_get_exp (inner->label_exp_s_ttl));
162 static_always_inline void
163 tunnel_decap_fixup_4o6 (tunnel_encap_decap_flags_t flags,
164 ip4_header_t * inner, const ip6_header_t * outer)
166 if (flags & TUNNEL_ENCAP_DECAP_FLAG_DECAP_COPY_ECN)
167 ip4_header_set_ecn_w_chksum (inner, ip6_ecn_network_order (outer));
170 static_always_inline void
171 tunnel_decap_fixup_6o6 (tunnel_encap_decap_flags_t flags,
172 ip6_header_t * inner, const ip6_header_t * outer)
174 if (flags & TUNNEL_ENCAP_DECAP_FLAG_DECAP_COPY_ECN)
175 ip6_set_ecn_network_order (inner, ip6_ecn_network_order (outer));
178 static_always_inline void
179 tunnel_decap_fixup_6o4 (tunnel_encap_decap_flags_t flags,
180 ip6_header_t * inner, const ip4_header_t * outer)
182 if (flags & TUNNEL_ENCAP_DECAP_FLAG_DECAP_COPY_ECN)
183 ip6_set_ecn_network_order (inner, ip4_header_get_ecn (outer));
186 static_always_inline void
187 tunnel_decap_fixup_4o4 (tunnel_encap_decap_flags_t flags,
188 ip4_header_t * inner, const ip4_header_t * outer)
190 if (flags & TUNNEL_ENCAP_DECAP_FLAG_DECAP_COPY_ECN)
191 ip4_header_set_ecn_w_chksum (inner, ip4_header_get_ecn (outer));
194 static_always_inline void
195 tunnel_decap_fixup_mplso6 (tunnel_encap_decap_flags_t flags,
196 mpls_unicast_header_t *inner,
197 const ip6_header_t *outer)
201 static_always_inline void
202 tunnel_decap_fixup_mplso4 (tunnel_encap_decap_flags_t flags,
203 mpls_unicast_header_t *inner,
204 const ip4_header_t *outer)
211 * fd.io coding-style-patch-verification: ON
214 * eval: (c-set-style "gnu")