Damjan Marion | 0247b46 | 2016-06-08 01:37:11 +0200 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (c) 2016 Cisco and/or its affiliates. |
| 3 | * Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | * you may not use this file except in compliance with the License. |
| 5 | * You may obtain a copy of the License at: |
| 6 | * |
| 7 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | * |
| 9 | * Unless required by applicable law or agreed to in writing, software |
| 10 | * distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | * See the License for the specific language governing permissions and |
| 13 | * limitations under the License. |
| 14 | */ |
| 15 | |
| 16 | #ifndef included_vnet_handoff_h |
| 17 | #define included_vnet_handoff_h |
| 18 | |
| 19 | #include <vlib/vlib.h> |
| 20 | #include <vnet/ethernet/ethernet.h> |
| 21 | #include <vnet/ip/ip4_packet.h> |
| 22 | #include <vnet/ip/ip6_packet.h> |
Neale Ranns | 0bfe5d8 | 2016-08-25 15:29:12 +0100 | [diff] [blame] | 23 | #include <vnet/mpls/packet.h> |
Damjan Marion | 0247b46 | 2016-06-08 01:37:11 +0200 | [diff] [blame] | 24 | |
Dave Barach | ba868bb | 2016-08-08 09:51:21 -0400 | [diff] [blame] | 25 | static inline u64 |
| 26 | ipv4_get_key (ip4_header_t * ip) |
Damjan Marion | 0247b46 | 2016-06-08 01:37:11 +0200 | [diff] [blame] | 27 | { |
Dave Barach | ba868bb | 2016-08-08 09:51:21 -0400 | [diff] [blame] | 28 | u64 hash_key; |
Damjan Marion | 0247b46 | 2016-06-08 01:37:11 +0200 | [diff] [blame] | 29 | |
Dave Barach | ba868bb | 2016-08-08 09:51:21 -0400 | [diff] [blame] | 30 | hash_key = *((u64 *) (&ip->address_pair)) ^ ip->protocol; |
Damjan Marion | 0247b46 | 2016-06-08 01:37:11 +0200 | [diff] [blame] | 31 | |
Dave Barach | ba868bb | 2016-08-08 09:51:21 -0400 | [diff] [blame] | 32 | return hash_key; |
Damjan Marion | 0247b46 | 2016-06-08 01:37:11 +0200 | [diff] [blame] | 33 | } |
| 34 | |
Dave Barach | ba868bb | 2016-08-08 09:51:21 -0400 | [diff] [blame] | 35 | static inline u64 |
| 36 | ipv6_get_key (ip6_header_t * ip) |
Damjan Marion | 0247b46 | 2016-06-08 01:37:11 +0200 | [diff] [blame] | 37 | { |
Dave Barach | ba868bb | 2016-08-08 09:51:21 -0400 | [diff] [blame] | 38 | u64 hash_key; |
Damjan Marion | 0247b46 | 2016-06-08 01:37:11 +0200 | [diff] [blame] | 39 | |
Dave Barach | ba868bb | 2016-08-08 09:51:21 -0400 | [diff] [blame] | 40 | hash_key = ip->src_address.as_u64[0] ^ |
| 41 | rotate_left (ip->src_address.as_u64[1], 13) ^ |
| 42 | rotate_left (ip->dst_address.as_u64[0], 26) ^ |
| 43 | rotate_left (ip->dst_address.as_u64[1], 39) ^ ip->protocol; |
Damjan Marion | 0247b46 | 2016-06-08 01:37:11 +0200 | [diff] [blame] | 44 | |
Dave Barach | ba868bb | 2016-08-08 09:51:21 -0400 | [diff] [blame] | 45 | return hash_key; |
Damjan Marion | 0247b46 | 2016-06-08 01:37:11 +0200 | [diff] [blame] | 46 | } |
| 47 | |
| 48 | #define MPLS_BOTTOM_OF_STACK_BIT_MASK 0x00000100U |
| 49 | #define MPLS_LABEL_MASK 0xFFFFF000U |
| 50 | |
Dave Barach | ba868bb | 2016-08-08 09:51:21 -0400 | [diff] [blame] | 51 | static inline u64 |
| 52 | mpls_get_key (mpls_unicast_header_t * m) |
Damjan Marion | 0247b46 | 2016-06-08 01:37:11 +0200 | [diff] [blame] | 53 | { |
Dave Barach | ba868bb | 2016-08-08 09:51:21 -0400 | [diff] [blame] | 54 | u64 hash_key; |
| 55 | u8 ip_ver; |
Damjan Marion | 0247b46 | 2016-06-08 01:37:11 +0200 | [diff] [blame] | 56 | |
| 57 | |
Dave Barach | ba868bb | 2016-08-08 09:51:21 -0400 | [diff] [blame] | 58 | /* find the bottom of the MPLS label stack. */ |
| 59 | if (PREDICT_TRUE (m->label_exp_s_ttl & |
| 60 | clib_net_to_host_u32 (MPLS_BOTTOM_OF_STACK_BIT_MASK))) |
| 61 | { |
| 62 | goto bottom_lbl_found; |
| 63 | } |
| 64 | m++; |
Damjan Marion | 0247b46 | 2016-06-08 01:37:11 +0200 | [diff] [blame] | 65 | |
Dave Barach | ba868bb | 2016-08-08 09:51:21 -0400 | [diff] [blame] | 66 | if (PREDICT_TRUE (m->label_exp_s_ttl & |
| 67 | clib_net_to_host_u32 (MPLS_BOTTOM_OF_STACK_BIT_MASK))) |
| 68 | { |
| 69 | goto bottom_lbl_found; |
| 70 | } |
| 71 | m++; |
Damjan Marion | 0247b46 | 2016-06-08 01:37:11 +0200 | [diff] [blame] | 72 | |
Dave Barach | ba868bb | 2016-08-08 09:51:21 -0400 | [diff] [blame] | 73 | if (m->label_exp_s_ttl & |
| 74 | clib_net_to_host_u32 (MPLS_BOTTOM_OF_STACK_BIT_MASK)) |
| 75 | { |
| 76 | goto bottom_lbl_found; |
| 77 | } |
| 78 | m++; |
Damjan Marion | 0247b46 | 2016-06-08 01:37:11 +0200 | [diff] [blame] | 79 | |
Dave Barach | ba868bb | 2016-08-08 09:51:21 -0400 | [diff] [blame] | 80 | if (m->label_exp_s_ttl & |
| 81 | clib_net_to_host_u32 (MPLS_BOTTOM_OF_STACK_BIT_MASK)) |
| 82 | { |
| 83 | goto bottom_lbl_found; |
| 84 | } |
| 85 | m++; |
Damjan Marion | 0247b46 | 2016-06-08 01:37:11 +0200 | [diff] [blame] | 86 | |
Dave Barach | ba868bb | 2016-08-08 09:51:21 -0400 | [diff] [blame] | 87 | if (m->label_exp_s_ttl & |
| 88 | clib_net_to_host_u32 (MPLS_BOTTOM_OF_STACK_BIT_MASK)) |
| 89 | { |
| 90 | goto bottom_lbl_found; |
| 91 | } |
Damjan Marion | 0247b46 | 2016-06-08 01:37:11 +0200 | [diff] [blame] | 92 | |
Dave Barach | ba868bb | 2016-08-08 09:51:21 -0400 | [diff] [blame] | 93 | /* the bottom label was not found - use the last label */ |
| 94 | hash_key = m->label_exp_s_ttl & clib_net_to_host_u32 (MPLS_LABEL_MASK); |
Damjan Marion | 0247b46 | 2016-06-08 01:37:11 +0200 | [diff] [blame] | 95 | |
Dave Barach | ba868bb | 2016-08-08 09:51:21 -0400 | [diff] [blame] | 96 | return hash_key; |
Damjan Marion | 0247b46 | 2016-06-08 01:37:11 +0200 | [diff] [blame] | 97 | |
| 98 | bottom_lbl_found: |
Dave Barach | ba868bb | 2016-08-08 09:51:21 -0400 | [diff] [blame] | 99 | m++; |
| 100 | ip_ver = (*((u8 *) m) >> 4); |
Damjan Marion | 0247b46 | 2016-06-08 01:37:11 +0200 | [diff] [blame] | 101 | |
Dave Barach | ba868bb | 2016-08-08 09:51:21 -0400 | [diff] [blame] | 102 | /* find out if it is IPV4 or IPV6 header */ |
| 103 | if (PREDICT_TRUE (ip_ver == 4)) |
| 104 | { |
| 105 | hash_key = ipv4_get_key ((ip4_header_t *) m); |
| 106 | } |
| 107 | else if (PREDICT_TRUE (ip_ver == 6)) |
| 108 | { |
| 109 | hash_key = ipv6_get_key ((ip6_header_t *) m); |
| 110 | } |
| 111 | else |
| 112 | { |
| 113 | /* use the bottom label */ |
| 114 | hash_key = |
| 115 | (m - 1)->label_exp_s_ttl & clib_net_to_host_u32 (MPLS_LABEL_MASK); |
| 116 | } |
Damjan Marion | 0247b46 | 2016-06-08 01:37:11 +0200 | [diff] [blame] | 117 | |
Dave Barach | ba868bb | 2016-08-08 09:51:21 -0400 | [diff] [blame] | 118 | return hash_key; |
Damjan Marion | 0247b46 | 2016-06-08 01:37:11 +0200 | [diff] [blame] | 119 | |
| 120 | } |
| 121 | |
Gabriel Ganne | c0a36c0 | 2016-11-16 16:57:00 +0100 | [diff] [blame] | 122 | static inline u64 |
| 123 | eth_get_sym_key (ethernet_header_t * h0) |
| 124 | { |
| 125 | u64 hash_key; |
| 126 | |
| 127 | if (PREDICT_TRUE (h0->type) == clib_host_to_net_u16 (ETHERNET_TYPE_IP4)) |
| 128 | { |
| 129 | ip4_header_t *ip = (ip4_header_t *) (h0 + 1); |
| 130 | hash_key = |
| 131 | (u64) (ip->src_address.as_u32 ^ |
| 132 | ip->dst_address.as_u32 ^ ip->protocol); |
| 133 | } |
| 134 | else if (h0->type == clib_host_to_net_u16 (ETHERNET_TYPE_IP6)) |
| 135 | { |
| 136 | ip6_header_t *ip = (ip6_header_t *) (h0 + 1); |
| 137 | hash_key = (u64) (ip->src_address.as_u64[0] ^ |
| 138 | ip->src_address.as_u64[1] ^ |
| 139 | ip->dst_address.as_u64[0] ^ |
| 140 | ip->dst_address.as_u64[1] ^ ip->protocol); |
| 141 | } |
Neale Ranns | 0f26c5a | 2017-03-01 15:12:11 -0800 | [diff] [blame] | 142 | else if (h0->type == clib_host_to_net_u16 (ETHERNET_TYPE_MPLS)) |
Gabriel Ganne | c0a36c0 | 2016-11-16 16:57:00 +0100 | [diff] [blame] | 143 | { |
| 144 | hash_key = mpls_get_key ((mpls_unicast_header_t *) (h0 + 1)); |
| 145 | } |
| 146 | else |
| 147 | if (PREDICT_FALSE |
| 148 | ((h0->type == clib_host_to_net_u16 (ETHERNET_TYPE_VLAN)) |
| 149 | || (h0->type == clib_host_to_net_u16 (ETHERNET_TYPE_DOT1AD)))) |
| 150 | { |
| 151 | ethernet_vlan_header_t *outer = (ethernet_vlan_header_t *) (h0 + 1); |
| 152 | |
| 153 | outer = (outer->type == clib_host_to_net_u16 (ETHERNET_TYPE_VLAN)) ? |
| 154 | outer + 1 : outer; |
| 155 | if (PREDICT_TRUE (outer->type) == |
| 156 | clib_host_to_net_u16 (ETHERNET_TYPE_IP4)) |
| 157 | { |
| 158 | ip4_header_t *ip = (ip4_header_t *) (outer + 1); |
| 159 | hash_key = |
| 160 | (u64) (ip->src_address.as_u32 ^ |
| 161 | ip->dst_address.as_u32 ^ ip->protocol); |
| 162 | } |
| 163 | else if (outer->type == clib_host_to_net_u16 (ETHERNET_TYPE_IP6)) |
| 164 | { |
| 165 | ip6_header_t *ip = (ip6_header_t *) (outer + 1); |
| 166 | hash_key = |
| 167 | (u64) (ip->src_address.as_u64[0] ^ ip->src_address.as_u64[1] ^ |
| 168 | ip->dst_address.as_u64[0] ^ |
| 169 | ip->dst_address.as_u64[1] ^ ip->protocol); |
| 170 | } |
Neale Ranns | 0f26c5a | 2017-03-01 15:12:11 -0800 | [diff] [blame] | 171 | else if (outer->type == clib_host_to_net_u16 (ETHERNET_TYPE_MPLS)) |
Gabriel Ganne | c0a36c0 | 2016-11-16 16:57:00 +0100 | [diff] [blame] | 172 | { |
| 173 | hash_key = mpls_get_key ((mpls_unicast_header_t *) (outer + 1)); |
| 174 | } |
| 175 | else |
| 176 | { |
| 177 | hash_key = outer->type; |
| 178 | } |
| 179 | } |
| 180 | else |
| 181 | { |
| 182 | hash_key = 0; |
| 183 | } |
| 184 | |
| 185 | return hash_key; |
| 186 | } |
Damjan Marion | 0247b46 | 2016-06-08 01:37:11 +0200 | [diff] [blame] | 187 | |
| 188 | static inline u64 |
Dave Barach | ba868bb | 2016-08-08 09:51:21 -0400 | [diff] [blame] | 189 | eth_get_key (ethernet_header_t * h0) |
Damjan Marion | 0247b46 | 2016-06-08 01:37:11 +0200 | [diff] [blame] | 190 | { |
Dave Barach | ba868bb | 2016-08-08 09:51:21 -0400 | [diff] [blame] | 191 | u64 hash_key; |
Damjan Marion | 0247b46 | 2016-06-08 01:37:11 +0200 | [diff] [blame] | 192 | |
Dave Barach | ba868bb | 2016-08-08 09:51:21 -0400 | [diff] [blame] | 193 | if (PREDICT_TRUE (h0->type) == clib_host_to_net_u16 (ETHERNET_TYPE_IP4)) |
| 194 | { |
| 195 | hash_key = ipv4_get_key ((ip4_header_t *) (h0 + 1)); |
| 196 | } |
| 197 | else if (h0->type == clib_host_to_net_u16 (ETHERNET_TYPE_IP6)) |
| 198 | { |
| 199 | hash_key = ipv6_get_key ((ip6_header_t *) (h0 + 1)); |
| 200 | } |
Neale Ranns | 0f26c5a | 2017-03-01 15:12:11 -0800 | [diff] [blame] | 201 | else if (h0->type == clib_host_to_net_u16 (ETHERNET_TYPE_MPLS)) |
Dave Barach | ba868bb | 2016-08-08 09:51:21 -0400 | [diff] [blame] | 202 | { |
| 203 | hash_key = mpls_get_key ((mpls_unicast_header_t *) (h0 + 1)); |
| 204 | } |
| 205 | else if ((h0->type == clib_host_to_net_u16 (ETHERNET_TYPE_VLAN)) || |
| 206 | (h0->type == clib_host_to_net_u16 (ETHERNET_TYPE_DOT1AD))) |
| 207 | { |
| 208 | ethernet_vlan_header_t *outer = (ethernet_vlan_header_t *) (h0 + 1); |
Damjan Marion | 0247b46 | 2016-06-08 01:37:11 +0200 | [diff] [blame] | 209 | |
Dave Barach | ba868bb | 2016-08-08 09:51:21 -0400 | [diff] [blame] | 210 | outer = (outer->type == clib_host_to_net_u16 (ETHERNET_TYPE_VLAN)) ? |
| 211 | outer + 1 : outer; |
| 212 | if (PREDICT_TRUE (outer->type) == |
| 213 | clib_host_to_net_u16 (ETHERNET_TYPE_IP4)) |
| 214 | { |
| 215 | hash_key = ipv4_get_key ((ip4_header_t *) (outer + 1)); |
| 216 | } |
| 217 | else if (outer->type == clib_host_to_net_u16 (ETHERNET_TYPE_IP6)) |
| 218 | { |
| 219 | hash_key = ipv6_get_key ((ip6_header_t *) (outer + 1)); |
| 220 | } |
Neale Ranns | 0f26c5a | 2017-03-01 15:12:11 -0800 | [diff] [blame] | 221 | else if (outer->type == clib_host_to_net_u16 (ETHERNET_TYPE_MPLS)) |
Dave Barach | ba868bb | 2016-08-08 09:51:21 -0400 | [diff] [blame] | 222 | { |
| 223 | hash_key = mpls_get_key ((mpls_unicast_header_t *) (outer + 1)); |
| 224 | } |
| 225 | else |
| 226 | { |
| 227 | hash_key = outer->type; |
| 228 | } |
| 229 | } |
| 230 | else |
| 231 | { |
| 232 | hash_key = 0; |
| 233 | } |
Damjan Marion | 0247b46 | 2016-06-08 01:37:11 +0200 | [diff] [blame] | 234 | |
Dave Barach | ba868bb | 2016-08-08 09:51:21 -0400 | [diff] [blame] | 235 | return hash_key; |
Damjan Marion | 0247b46 | 2016-06-08 01:37:11 +0200 | [diff] [blame] | 236 | } |
| 237 | |
| 238 | #endif /* included_vnet_handoff_h */ |
Dave Barach | ba868bb | 2016-08-08 09:51:21 -0400 | [diff] [blame] | 239 | |
| 240 | /* |
| 241 | * fd.io coding-style-patch-verification: ON |
| 242 | * |
| 243 | * Local Variables: |
| 244 | * eval: (c-set-style "gnu") |
| 245 | * End: |
| 246 | */ |