ipsec: Support MPLS over IPSec[46] interface
[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_mplso4_w_chksum (tunnel_encap_decap_flags_t flags,
73                                     const mpls_unicast_header_t *inner,
74                                     ip4_header_t *outer)
75 {
76   if (flags & TUNNEL_ENCAP_DECAP_FLAG_ENCAP_COPY_DSCP)
77     {
78       ip_csum_t sum = outer->checksum;
79       u8 tos = outer->tos;
80
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));
84
85       sum =
86         ip_csum_update (outer->checksum, tos, outer->tos, ip4_header_t, tos);
87       outer->checksum = ip_csum_fold (sum);
88     }
89 }
90
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)
94 {
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)));
99 }
100
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)
105 {
106   if (flags & (TUNNEL_ENCAP_DECAP_FLAG_ENCAP_COPY_DSCP |
107                TUNNEL_ENCAP_DECAP_FLAG_ENCAP_COPY_ECN))
108     {
109       ip_csum_t sum = outer->checksum;
110       u8 tos = outer->tos;
111
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)));
116
117       sum =
118         ip_csum_update (outer->checksum, tos, outer->tos, ip4_header_t, tos);
119       outer->checksum = ip_csum_fold (sum);
120     }
121 }
122
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)
126 {
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));
131 }
132
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)
136 {
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));
141 }
142
143 static_always_inline void
144 tunnel_encap_fixup_mplso6 (tunnel_encap_decap_flags_t flags,
145                            const mpls_unicast_header_t *inner,
146                            ip6_header_t *outer)
147 {
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));
151 }
152
153 static_always_inline void
154 tunnel_encap_fixup_mplso4 (tunnel_encap_decap_flags_t flags,
155                            const mpls_unicast_header_t *inner,
156                            ip4_header_t *outer)
157 {
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));
160 }
161
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)
165 {
166   if (flags & TUNNEL_ENCAP_DECAP_FLAG_DECAP_COPY_ECN)
167     ip4_header_set_ecn_w_chksum (inner, ip6_ecn_network_order (outer));
168 }
169
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)
173 {
174   if (flags & TUNNEL_ENCAP_DECAP_FLAG_DECAP_COPY_ECN)
175     ip6_set_ecn_network_order (inner, ip6_ecn_network_order (outer));
176 }
177
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)
181 {
182   if (flags & TUNNEL_ENCAP_DECAP_FLAG_DECAP_COPY_ECN)
183     ip6_set_ecn_network_order (inner, ip4_header_get_ecn (outer));
184 }
185
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)
189 {
190   if (flags & TUNNEL_ENCAP_DECAP_FLAG_DECAP_COPY_ECN)
191     ip4_header_set_ecn_w_chksum (inner, ip4_header_get_ecn (outer));
192 }
193
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)
198 {
199 }
200
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)
205 {
206 }
207
208 #endif
209
210 /*
211  * fd.io coding-style-patch-verification: ON
212  *
213  * Local Variables:
214  * eval: (c-set-style "gnu")
215  * End:
216  */