blob: f84e764f7ea30376f11a05945290a57811574be3 [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>
22
Neale Rannse5b94dd2019-12-31 05:13:14 +000023static_always_inline void
24tunnel_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
36static_always_inline void
Neale Ranns041add72020-01-02 04:06:10 +000037tunnel_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
71static_always_inline void
Neale Rannse5b94dd2019-12-31 05:13:14 +000072tunnel_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
81static_always_inline void
Neale Ranns041add72020-01-02 04:06:10 +000082tunnel_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
103static_always_inline void
Neale Rannse5b94dd2019-12-31 05:13:14 +0000104tunnel_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
113static_always_inline void
114tunnel_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
123static_always_inline void
124tunnel_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
131static_always_inline void
132tunnel_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
139static_always_inline void
140tunnel_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
147static_always_inline void
148tunnel_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 */