ipsec: Tunnel SA DSCP behaviour
[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
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)
26 {
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);
34 }
35
36 static_always_inline void
37 tunnel_encap_fixup_4o4_w_chksum (tunnel_encap_decap_flags_t flags,
38                                  const ip4_header_t * inner,
39                                  ip4_header_t * outer)
40 {
41   if (flags & (TUNNEL_ENCAP_DECAP_FLAG_ENCAP_COPY_DSCP |
42                TUNNEL_ENCAP_DECAP_FLAG_ENCAP_COPY_ECN))
43     {
44       ip_csum_t sum = outer->checksum;
45       u8 tos = outer->tos;
46
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));
51
52       sum =
53         ip_csum_update (outer->checksum, tos, outer->tos, ip4_header_t, tos);
54       outer->checksum = ip_csum_fold (sum);
55     }
56   if ((flags & TUNNEL_ENCAP_DECAP_FLAG_ENCAP_COPY_DF) &&
57       ip4_header_get_df (inner))
58     {
59       ip_csum_t sum = outer->checksum;
60       u16 tos = outer->flags_and_fragment_offset;
61
62       ip4_header_set_df (outer);
63
64       sum =
65         ip_csum_update (outer->checksum, tos, outer->tos, ip4_header_t,
66                         flags_and_fragment_offset);
67       outer->checksum = ip_csum_fold (sum);
68     }
69 }
70
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)
74 {
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)));
79 }
80
81 static_always_inline void
82 tunnel_encap_fixup_6o4_w_chksum (tunnel_encap_decap_flags_t flags,
83                                  const ip6_header_t * inner,
84                                  ip4_header_t * outer)
85 {
86   if (flags & (TUNNEL_ENCAP_DECAP_FLAG_ENCAP_COPY_DSCP |
87                TUNNEL_ENCAP_DECAP_FLAG_ENCAP_COPY_ECN))
88     {
89       ip_csum_t sum = outer->checksum;
90       u8 tos = outer->tos;
91
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)));
96
97       sum =
98         ip_csum_update (outer->checksum, tos, outer->tos, ip4_header_t, tos);
99       outer->checksum = ip_csum_fold (sum);
100     }
101 }
102
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)
106 {
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));
111 }
112
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)
116 {
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));
121 }
122
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)
126 {
127   if (flags & TUNNEL_ENCAP_DECAP_FLAG_DECAP_COPY_ECN)
128     ip4_header_set_ecn_w_chksum (inner, ip6_ecn_network_order (outer));
129 }
130
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)
134 {
135   if (flags & TUNNEL_ENCAP_DECAP_FLAG_DECAP_COPY_ECN)
136     ip6_set_ecn_network_order (inner, ip6_ecn_network_order (outer));
137 }
138
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)
142 {
143   if (flags & TUNNEL_ENCAP_DECAP_FLAG_DECAP_COPY_ECN)
144     ip6_set_ecn_network_order (inner, ip4_header_get_ecn (outer));
145 }
146
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)
150 {
151   if (flags & TUNNEL_ENCAP_DECAP_FLAG_DECAP_COPY_ECN)
152     ip4_header_set_ecn_w_chksum (inner, ip4_header_get_ecn (outer));
153 }
154
155 #endif
156
157 /*
158  * fd.io coding-style-patch-verification: ON
159  *
160  * Local Variables:
161  * eval: (c-set-style "gnu")
162  * End:
163  */