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_6o4 (tunnel_encap_decap_flags_t flags,
73 const ip6_header_t * inner, ip4_header_t * outer)
75 if (flags & TUNNEL_ENCAP_DECAP_FLAG_ENCAP_COPY_DSCP)
76 ip4_header_set_dscp (outer, ip6_dscp_network_order (inner));
77 if (flags & TUNNEL_ENCAP_DECAP_FLAG_ENCAP_COPY_ECN)
78 ip4_header_set_ecn (outer, ip6_ecn_network_order ((inner)));
81 static_always_inline void
82 tunnel_encap_fixup_6o4_w_chksum (tunnel_encap_decap_flags_t flags,
83 const ip6_header_t * inner,
86 if (flags & (TUNNEL_ENCAP_DECAP_FLAG_ENCAP_COPY_DSCP |
87 TUNNEL_ENCAP_DECAP_FLAG_ENCAP_COPY_ECN))
89 ip_csum_t sum = outer->checksum;
92 if (flags & TUNNEL_ENCAP_DECAP_FLAG_ENCAP_COPY_DSCP)
93 ip4_header_set_dscp (outer, ip6_dscp_network_order (inner));
94 if (flags & TUNNEL_ENCAP_DECAP_FLAG_ENCAP_COPY_ECN)
95 ip4_header_set_ecn (outer, ip6_ecn_network_order ((inner)));
98 ip_csum_update (outer->checksum, tos, outer->tos, ip4_header_t, tos);
99 outer->checksum = ip_csum_fold (sum);
103 static_always_inline void
104 tunnel_encap_fixup_6o6 (tunnel_encap_decap_flags_t flags,
105 const ip6_header_t * inner, ip6_header_t * outer)
107 if (flags & TUNNEL_ENCAP_DECAP_FLAG_ENCAP_COPY_DSCP)
108 ip6_set_dscp_network_order (outer, ip6_dscp_network_order (inner));
109 if (flags & TUNNEL_ENCAP_DECAP_FLAG_ENCAP_COPY_ECN)
110 ip6_set_ecn_network_order (outer, ip6_ecn_network_order (inner));
113 static_always_inline void
114 tunnel_encap_fixup_4o6 (tunnel_encap_decap_flags_t flags,
115 const ip4_header_t * inner, ip6_header_t * outer)
117 if (flags & TUNNEL_ENCAP_DECAP_FLAG_ENCAP_COPY_DSCP)
118 ip6_set_dscp_network_order (outer, ip4_header_get_dscp (inner));
119 if (flags & TUNNEL_ENCAP_DECAP_FLAG_ENCAP_COPY_ECN)
120 ip6_set_ecn_network_order (outer, ip4_header_get_ecn (inner));
123 static_always_inline void
124 tunnel_decap_fixup_4o6 (tunnel_encap_decap_flags_t flags,
125 ip4_header_t * inner, const ip6_header_t * outer)
127 if (flags & TUNNEL_ENCAP_DECAP_FLAG_DECAP_COPY_ECN)
128 ip4_header_set_ecn_w_chksum (inner, ip6_ecn_network_order (outer));
131 static_always_inline void
132 tunnel_decap_fixup_6o6 (tunnel_encap_decap_flags_t flags,
133 ip6_header_t * inner, const ip6_header_t * outer)
135 if (flags & TUNNEL_ENCAP_DECAP_FLAG_DECAP_COPY_ECN)
136 ip6_set_ecn_network_order (inner, ip6_ecn_network_order (outer));
139 static_always_inline void
140 tunnel_decap_fixup_6o4 (tunnel_encap_decap_flags_t flags,
141 ip6_header_t * inner, const ip4_header_t * outer)
143 if (flags & TUNNEL_ENCAP_DECAP_FLAG_DECAP_COPY_ECN)
144 ip6_set_ecn_network_order (inner, ip4_header_get_ecn (outer));
147 static_always_inline void
148 tunnel_decap_fixup_4o4 (tunnel_encap_decap_flags_t flags,
149 ip4_header_t * inner, const ip4_header_t * outer)
151 if (flags & TUNNEL_ENCAP_DECAP_FLAG_DECAP_COPY_ECN)
152 ip4_header_set_ecn_w_chksum (inner, ip4_header_get_ecn (outer));
158 * fd.io coding-style-patch-verification: ON
161 * eval: (c-set-style "gnu")