Neale Ranns | e5b94dd | 2019-12-31 05:13:14 +0000 | [diff] [blame] | 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 | |
Neale Ranns | 4ec36c5 | 2020-03-31 09:21:29 -0400 | [diff] [blame] | 21 | #include <vnet/tunnel/tunnel.h> |
Neale Ranns | a91cb45 | 2021-02-04 11:02:52 +0000 | [diff] [blame] | 22 | #include <vnet/mpls/mpls_lookup.h> |
Neale Ranns | 4ec36c5 | 2020-03-31 09:21:29 -0400 | [diff] [blame] | 23 | |
Neale Ranns | e5b94dd | 2019-12-31 05:13:14 +0000 | [diff] [blame] | 24 | static_always_inline void |
| 25 | tunnel_encap_fixup_4o4 (tunnel_encap_decap_flags_t flags, |
| 26 | const ip4_header_t * inner, ip4_header_t * outer) |
| 27 | { |
Neale Ranns | a91cb45 | 2021-02-04 11:02:52 +0000 | [diff] [blame] | 28 | if (PREDICT_FALSE (flags & TUNNEL_ENCAP_DECAP_FLAG_ENCAP_COPY_DSCP)) |
Neale Ranns | e5b94dd | 2019-12-31 05:13:14 +0000 | [diff] [blame] | 29 | ip4_header_set_dscp (outer, ip4_header_get_dscp (inner)); |
Neale Ranns | a91cb45 | 2021-02-04 11:02:52 +0000 | [diff] [blame] | 30 | if (PREDICT_FALSE (flags & TUNNEL_ENCAP_DECAP_FLAG_ENCAP_COPY_ECN)) |
Neale Ranns | e5b94dd | 2019-12-31 05:13:14 +0000 | [diff] [blame] | 31 | ip4_header_set_ecn (outer, ip4_header_get_ecn (inner)); |
Neale Ranns | a91cb45 | 2021-02-04 11:02:52 +0000 | [diff] [blame] | 32 | if (PREDICT_FALSE ((flags & TUNNEL_ENCAP_DECAP_FLAG_ENCAP_COPY_DF) && |
| 33 | ip4_header_get_df (inner))) |
Neale Ranns | e5b94dd | 2019-12-31 05:13:14 +0000 | [diff] [blame] | 34 | ip4_header_set_df (outer); |
Neale Ranns | a91cb45 | 2021-02-04 11:02:52 +0000 | [diff] [blame] | 35 | if (PREDICT_FALSE (flags & TUNNEL_ENCAP_DECAP_FLAG_ENCAP_COPY_HOP_LIMIT)) |
| 36 | ip4_header_set_ttl (outer, ip4_header_get_ttl (inner)); |
Neale Ranns | e5b94dd | 2019-12-31 05:13:14 +0000 | [diff] [blame] | 37 | } |
| 38 | |
| 39 | static_always_inline void |
Neale Ranns | 041add7 | 2020-01-02 04:06:10 +0000 | [diff] [blame] | 40 | tunnel_encap_fixup_4o4_w_chksum (tunnel_encap_decap_flags_t flags, |
| 41 | const ip4_header_t * inner, |
| 42 | ip4_header_t * outer) |
| 43 | { |
Neale Ranns | a91cb45 | 2021-02-04 11:02:52 +0000 | [diff] [blame] | 44 | if (PREDICT_FALSE (flags & (TUNNEL_ENCAP_DECAP_FLAG_ENCAP_COPY_DSCP | |
| 45 | TUNNEL_ENCAP_DECAP_FLAG_ENCAP_COPY_ECN))) |
Neale Ranns | 041add7 | 2020-01-02 04:06:10 +0000 | [diff] [blame] | 46 | { |
| 47 | ip_csum_t sum = outer->checksum; |
| 48 | u8 tos = outer->tos; |
| 49 | |
| 50 | if (flags & TUNNEL_ENCAP_DECAP_FLAG_ENCAP_COPY_DSCP) |
| 51 | ip4_header_set_dscp (outer, ip4_header_get_dscp (inner)); |
| 52 | if (flags & TUNNEL_ENCAP_DECAP_FLAG_ENCAP_COPY_ECN) |
| 53 | ip4_header_set_ecn (outer, ip4_header_get_ecn (inner)); |
| 54 | |
| 55 | sum = |
| 56 | ip_csum_update (outer->checksum, tos, outer->tos, ip4_header_t, tos); |
| 57 | outer->checksum = ip_csum_fold (sum); |
| 58 | } |
Neale Ranns | a91cb45 | 2021-02-04 11:02:52 +0000 | [diff] [blame] | 59 | if (PREDICT_FALSE ((flags & TUNNEL_ENCAP_DECAP_FLAG_ENCAP_COPY_DF) && |
| 60 | ip4_header_get_df (inner))) |
Neale Ranns | 041add7 | 2020-01-02 04:06:10 +0000 | [diff] [blame] | 61 | { |
| 62 | ip_csum_t sum = outer->checksum; |
| 63 | u16 tos = outer->flags_and_fragment_offset; |
| 64 | |
| 65 | ip4_header_set_df (outer); |
| 66 | |
| 67 | sum = |
| 68 | ip_csum_update (outer->checksum, tos, outer->tos, ip4_header_t, |
| 69 | flags_and_fragment_offset); |
| 70 | outer->checksum = ip_csum_fold (sum); |
| 71 | } |
| 72 | } |
| 73 | |
| 74 | static_always_inline void |
Neale Ranns | 4a58e49 | 2020-12-21 13:19:10 +0000 | [diff] [blame] | 75 | tunnel_encap_fixup_mplso4_w_chksum (tunnel_encap_decap_flags_t flags, |
| 76 | const mpls_unicast_header_t *inner, |
| 77 | ip4_header_t *outer) |
| 78 | { |
| 79 | if (flags & TUNNEL_ENCAP_DECAP_FLAG_ENCAP_COPY_DSCP) |
| 80 | { |
| 81 | ip_csum_t sum = outer->checksum; |
| 82 | u8 tos = outer->tos; |
| 83 | |
| 84 | if (flags & TUNNEL_ENCAP_DECAP_FLAG_ENCAP_COPY_DSCP) |
| 85 | ip4_header_set_dscp (outer, |
| 86 | vnet_mpls_uc_get_exp (inner->label_exp_s_ttl)); |
| 87 | |
| 88 | sum = |
| 89 | ip_csum_update (outer->checksum, tos, outer->tos, ip4_header_t, tos); |
| 90 | outer->checksum = ip_csum_fold (sum); |
| 91 | } |
| 92 | } |
| 93 | |
| 94 | static_always_inline void |
Neale Ranns | e5b94dd | 2019-12-31 05:13:14 +0000 | [diff] [blame] | 95 | tunnel_encap_fixup_6o4 (tunnel_encap_decap_flags_t flags, |
| 96 | const ip6_header_t * inner, ip4_header_t * outer) |
| 97 | { |
Neale Ranns | a91cb45 | 2021-02-04 11:02:52 +0000 | [diff] [blame] | 98 | if (PREDICT_FALSE (flags & TUNNEL_ENCAP_DECAP_FLAG_ENCAP_COPY_DSCP)) |
Neale Ranns | e5b94dd | 2019-12-31 05:13:14 +0000 | [diff] [blame] | 99 | ip4_header_set_dscp (outer, ip6_dscp_network_order (inner)); |
Neale Ranns | a91cb45 | 2021-02-04 11:02:52 +0000 | [diff] [blame] | 100 | if (PREDICT_FALSE (flags & TUNNEL_ENCAP_DECAP_FLAG_ENCAP_COPY_ECN)) |
Neale Ranns | e5b94dd | 2019-12-31 05:13:14 +0000 | [diff] [blame] | 101 | ip4_header_set_ecn (outer, ip6_ecn_network_order ((inner))); |
Neale Ranns | a91cb45 | 2021-02-04 11:02:52 +0000 | [diff] [blame] | 102 | if (PREDICT_FALSE (flags & TUNNEL_ENCAP_DECAP_FLAG_ENCAP_COPY_HOP_LIMIT)) |
| 103 | ip4_header_set_ttl (outer, ip6_hop_limit_network_order (inner)); |
Neale Ranns | e5b94dd | 2019-12-31 05:13:14 +0000 | [diff] [blame] | 104 | } |
| 105 | |
| 106 | static_always_inline void |
Neale Ranns | 041add7 | 2020-01-02 04:06:10 +0000 | [diff] [blame] | 107 | tunnel_encap_fixup_6o4_w_chksum (tunnel_encap_decap_flags_t flags, |
| 108 | const ip6_header_t * inner, |
| 109 | ip4_header_t * outer) |
| 110 | { |
Neale Ranns | a91cb45 | 2021-02-04 11:02:52 +0000 | [diff] [blame] | 111 | if (PREDICT_FALSE (flags & (TUNNEL_ENCAP_DECAP_FLAG_ENCAP_COPY_DSCP | |
| 112 | TUNNEL_ENCAP_DECAP_FLAG_ENCAP_COPY_ECN))) |
Neale Ranns | 041add7 | 2020-01-02 04:06:10 +0000 | [diff] [blame] | 113 | { |
| 114 | ip_csum_t sum = outer->checksum; |
| 115 | u8 tos = outer->tos; |
| 116 | |
| 117 | if (flags & TUNNEL_ENCAP_DECAP_FLAG_ENCAP_COPY_DSCP) |
| 118 | ip4_header_set_dscp (outer, ip6_dscp_network_order (inner)); |
| 119 | if (flags & TUNNEL_ENCAP_DECAP_FLAG_ENCAP_COPY_ECN) |
| 120 | ip4_header_set_ecn (outer, ip6_ecn_network_order ((inner))); |
| 121 | |
| 122 | sum = |
| 123 | ip_csum_update (outer->checksum, tos, outer->tos, ip4_header_t, tos); |
| 124 | outer->checksum = ip_csum_fold (sum); |
| 125 | } |
| 126 | } |
| 127 | |
| 128 | static_always_inline void |
Neale Ranns | e5b94dd | 2019-12-31 05:13:14 +0000 | [diff] [blame] | 129 | tunnel_encap_fixup_6o6 (tunnel_encap_decap_flags_t flags, |
| 130 | const ip6_header_t * inner, ip6_header_t * outer) |
| 131 | { |
Neale Ranns | a91cb45 | 2021-02-04 11:02:52 +0000 | [diff] [blame] | 132 | if (PREDICT_FALSE (flags & TUNNEL_ENCAP_DECAP_FLAG_ENCAP_COPY_DSCP)) |
Neale Ranns | e5b94dd | 2019-12-31 05:13:14 +0000 | [diff] [blame] | 133 | ip6_set_dscp_network_order (outer, ip6_dscp_network_order (inner)); |
Neale Ranns | a91cb45 | 2021-02-04 11:02:52 +0000 | [diff] [blame] | 134 | if (PREDICT_FALSE (flags & TUNNEL_ENCAP_DECAP_FLAG_ENCAP_COPY_ECN)) |
Neale Ranns | e5b94dd | 2019-12-31 05:13:14 +0000 | [diff] [blame] | 135 | ip6_set_ecn_network_order (outer, ip6_ecn_network_order (inner)); |
Neale Ranns | a91cb45 | 2021-02-04 11:02:52 +0000 | [diff] [blame] | 136 | if (PREDICT_FALSE (flags & TUNNEL_ENCAP_DECAP_FLAG_ENCAP_COPY_FLOW_LABEL)) |
| 137 | ip6_set_flow_label_network_order (outer, |
| 138 | ip6_flow_label_network_order (inner)); |
| 139 | if (PREDICT_FALSE (flags & TUNNEL_ENCAP_DECAP_FLAG_ENCAP_COPY_HOP_LIMIT)) |
| 140 | ip6_set_hop_limit_network_order (outer, |
| 141 | ip6_hop_limit_network_order (inner)); |
Neale Ranns | e5b94dd | 2019-12-31 05:13:14 +0000 | [diff] [blame] | 142 | } |
| 143 | |
| 144 | static_always_inline void |
| 145 | tunnel_encap_fixup_4o6 (tunnel_encap_decap_flags_t flags, |
Neale Ranns | a91cb45 | 2021-02-04 11:02:52 +0000 | [diff] [blame] | 146 | const vlib_buffer_t *b, const ip4_header_t *inner, |
| 147 | ip6_header_t *outer) |
Neale Ranns | e5b94dd | 2019-12-31 05:13:14 +0000 | [diff] [blame] | 148 | { |
Neale Ranns | a91cb45 | 2021-02-04 11:02:52 +0000 | [diff] [blame] | 149 | if (PREDICT_FALSE (flags & TUNNEL_ENCAP_DECAP_FLAG_ENCAP_COPY_DSCP)) |
Neale Ranns | e5b94dd | 2019-12-31 05:13:14 +0000 | [diff] [blame] | 150 | ip6_set_dscp_network_order (outer, ip4_header_get_dscp (inner)); |
Neale Ranns | a91cb45 | 2021-02-04 11:02:52 +0000 | [diff] [blame] | 151 | if (PREDICT_FALSE (flags & TUNNEL_ENCAP_DECAP_FLAG_ENCAP_COPY_ECN)) |
Neale Ranns | e5b94dd | 2019-12-31 05:13:14 +0000 | [diff] [blame] | 152 | ip6_set_ecn_network_order (outer, ip4_header_get_ecn (inner)); |
Neale Ranns | a91cb45 | 2021-02-04 11:02:52 +0000 | [diff] [blame] | 153 | if (PREDICT_FALSE (flags & TUNNEL_ENCAP_DECAP_FLAG_ENCAP_COPY_HOP_LIMIT)) |
| 154 | ip6_set_hop_limit_network_order (outer, ip4_header_get_ttl (inner)); |
| 155 | if (PREDICT_FALSE (flags & TUNNEL_ENCAP_DECAP_FLAG_ENCAP_COPY_FLOW_LABEL)) |
| 156 | ip6_set_flow_label_network_order ( |
| 157 | outer, (0 != vnet_buffer (b)->ip.flow_hash ? |
| 158 | vnet_buffer (b)->ip.flow_hash : |
| 159 | ip4_compute_flow_hash (inner, IP_FLOW_HASH_DEFAULT))); |
Neale Ranns | e5b94dd | 2019-12-31 05:13:14 +0000 | [diff] [blame] | 160 | } |
| 161 | |
| 162 | static_always_inline void |
Neale Ranns | e294de6 | 2020-12-21 09:52:24 +0000 | [diff] [blame] | 163 | tunnel_encap_fixup_mplso6 (tunnel_encap_decap_flags_t flags, |
Neale Ranns | a91cb45 | 2021-02-04 11:02:52 +0000 | [diff] [blame] | 164 | const vlib_buffer_t *b, |
Neale Ranns | e294de6 | 2020-12-21 09:52:24 +0000 | [diff] [blame] | 165 | const mpls_unicast_header_t *inner, |
| 166 | ip6_header_t *outer) |
| 167 | { |
| 168 | if (flags & TUNNEL_ENCAP_DECAP_FLAG_ENCAP_COPY_DSCP) |
| 169 | ip6_set_dscp_network_order (outer, |
| 170 | vnet_mpls_uc_get_exp (inner->label_exp_s_ttl)); |
Neale Ranns | a91cb45 | 2021-02-04 11:02:52 +0000 | [diff] [blame] | 171 | if (PREDICT_FALSE (flags & TUNNEL_ENCAP_DECAP_FLAG_ENCAP_COPY_FLOW_LABEL)) |
| 172 | ip6_set_flow_label_network_order ( |
| 173 | outer, (0 != vnet_buffer (b)->ip.flow_hash ? |
| 174 | vnet_buffer (b)->ip.flow_hash : |
| 175 | mpls_compute_flow_hash (inner, IP_FLOW_HASH_DEFAULT))); |
Neale Ranns | e294de6 | 2020-12-21 09:52:24 +0000 | [diff] [blame] | 176 | } |
| 177 | |
| 178 | static_always_inline void |
| 179 | tunnel_encap_fixup_mplso4 (tunnel_encap_decap_flags_t flags, |
| 180 | const mpls_unicast_header_t *inner, |
| 181 | ip4_header_t *outer) |
| 182 | { |
| 183 | if (flags & TUNNEL_ENCAP_DECAP_FLAG_ENCAP_COPY_DSCP) |
| 184 | ip4_header_set_dscp (outer, vnet_mpls_uc_get_exp (inner->label_exp_s_ttl)); |
| 185 | } |
| 186 | |
| 187 | static_always_inline void |
Neale Ranns | e5b94dd | 2019-12-31 05:13:14 +0000 | [diff] [blame] | 188 | tunnel_decap_fixup_4o6 (tunnel_encap_decap_flags_t flags, |
| 189 | ip4_header_t * inner, const ip6_header_t * outer) |
| 190 | { |
Neale Ranns | a91cb45 | 2021-02-04 11:02:52 +0000 | [diff] [blame] | 191 | if (PREDICT_FALSE (flags & TUNNEL_ENCAP_DECAP_FLAG_DECAP_COPY_ECN)) |
Neale Ranns | e5b94dd | 2019-12-31 05:13:14 +0000 | [diff] [blame] | 192 | ip4_header_set_ecn_w_chksum (inner, ip6_ecn_network_order (outer)); |
| 193 | } |
| 194 | |
| 195 | static_always_inline void |
| 196 | tunnel_decap_fixup_6o6 (tunnel_encap_decap_flags_t flags, |
| 197 | ip6_header_t * inner, const ip6_header_t * outer) |
| 198 | { |
Neale Ranns | a91cb45 | 2021-02-04 11:02:52 +0000 | [diff] [blame] | 199 | if (PREDICT_FALSE (flags & TUNNEL_ENCAP_DECAP_FLAG_DECAP_COPY_ECN)) |
Neale Ranns | e5b94dd | 2019-12-31 05:13:14 +0000 | [diff] [blame] | 200 | ip6_set_ecn_network_order (inner, ip6_ecn_network_order (outer)); |
| 201 | } |
| 202 | |
| 203 | static_always_inline void |
| 204 | tunnel_decap_fixup_6o4 (tunnel_encap_decap_flags_t flags, |
| 205 | ip6_header_t * inner, const ip4_header_t * outer) |
| 206 | { |
Neale Ranns | a91cb45 | 2021-02-04 11:02:52 +0000 | [diff] [blame] | 207 | if (PREDICT_FALSE (flags & TUNNEL_ENCAP_DECAP_FLAG_DECAP_COPY_ECN)) |
Neale Ranns | e5b94dd | 2019-12-31 05:13:14 +0000 | [diff] [blame] | 208 | ip6_set_ecn_network_order (inner, ip4_header_get_ecn (outer)); |
| 209 | } |
| 210 | |
| 211 | static_always_inline void |
| 212 | tunnel_decap_fixup_4o4 (tunnel_encap_decap_flags_t flags, |
| 213 | ip4_header_t * inner, const ip4_header_t * outer) |
| 214 | { |
Neale Ranns | a91cb45 | 2021-02-04 11:02:52 +0000 | [diff] [blame] | 215 | if (PREDICT_FALSE (flags & TUNNEL_ENCAP_DECAP_FLAG_DECAP_COPY_ECN)) |
Neale Ranns | e5b94dd | 2019-12-31 05:13:14 +0000 | [diff] [blame] | 216 | ip4_header_set_ecn_w_chksum (inner, ip4_header_get_ecn (outer)); |
| 217 | } |
| 218 | |
Neale Ranns | e294de6 | 2020-12-21 09:52:24 +0000 | [diff] [blame] | 219 | static_always_inline void |
| 220 | tunnel_decap_fixup_mplso6 (tunnel_encap_decap_flags_t flags, |
| 221 | mpls_unicast_header_t *inner, |
| 222 | const ip6_header_t *outer) |
| 223 | { |
| 224 | } |
| 225 | |
| 226 | static_always_inline void |
| 227 | tunnel_decap_fixup_mplso4 (tunnel_encap_decap_flags_t flags, |
| 228 | mpls_unicast_header_t *inner, |
| 229 | const ip4_header_t *outer) |
| 230 | { |
| 231 | } |
| 232 | |
Neale Ranns | e5b94dd | 2019-12-31 05:13:14 +0000 | [diff] [blame] | 233 | #endif |
| 234 | |
| 235 | /* |
| 236 | * fd.io coding-style-patch-verification: ON |
| 237 | * |
| 238 | * Local Variables: |
| 239 | * eval: (c-set-style "gnu") |
| 240 | * End: |
| 241 | */ |