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:
7 * http://www.apache.org/licenses/LICENSE-2.0
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.
16 #ifndef included_vnet_handoff_h
17 #define included_vnet_handoff_h
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>
23 #include <vnet/mpls/packet.h>
26 ipv4_get_key (ip4_header_t * ip)
30 hash_key = *((u64 *) (&ip->address_pair)) ^ ip->protocol;
36 ipv6_get_key (ip6_header_t * ip)
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;
48 #define MPLS_BOTTOM_OF_STACK_BIT_MASK 0x00000100U
49 #define MPLS_LABEL_MASK 0xFFFFF000U
52 mpls_get_key (mpls_unicast_header_t * m)
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)))
62 goto bottom_lbl_found;
66 if (PREDICT_TRUE (m->label_exp_s_ttl &
67 clib_net_to_host_u32 (MPLS_BOTTOM_OF_STACK_BIT_MASK)))
69 goto bottom_lbl_found;
73 if (m->label_exp_s_ttl &
74 clib_net_to_host_u32 (MPLS_BOTTOM_OF_STACK_BIT_MASK))
76 goto bottom_lbl_found;
80 if (m->label_exp_s_ttl &
81 clib_net_to_host_u32 (MPLS_BOTTOM_OF_STACK_BIT_MASK))
83 goto bottom_lbl_found;
87 if (m->label_exp_s_ttl &
88 clib_net_to_host_u32 (MPLS_BOTTOM_OF_STACK_BIT_MASK))
90 goto bottom_lbl_found;
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);
100 ip_ver = (*((u8 *) m) >> 4);
102 /* find out if it is IPV4 or IPV6 header */
103 if (PREDICT_TRUE (ip_ver == 4))
105 hash_key = ipv4_get_key ((ip4_header_t *) m);
107 else if (PREDICT_TRUE (ip_ver == 6))
109 hash_key = ipv6_get_key ((ip6_header_t *) m);
113 /* use the bottom label */
115 (m - 1)->label_exp_s_ttl & clib_net_to_host_u32 (MPLS_LABEL_MASK);
123 eth_get_sym_key (ethernet_header_t * h0)
127 if (PREDICT_TRUE (h0->type) == clib_host_to_net_u16 (ETHERNET_TYPE_IP4))
129 ip4_header_t *ip = (ip4_header_t *) (h0 + 1);
131 (u64) (ip->src_address.as_u32 ^
132 ip->dst_address.as_u32 ^ ip->protocol);
134 else if (h0->type == clib_host_to_net_u16 (ETHERNET_TYPE_IP6))
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);
142 else if (h0->type == clib_host_to_net_u16 (ETHERNET_TYPE_MPLS))
144 hash_key = mpls_get_key ((mpls_unicast_header_t *) (h0 + 1));
148 ((h0->type == clib_host_to_net_u16 (ETHERNET_TYPE_VLAN))
149 || (h0->type == clib_host_to_net_u16 (ETHERNET_TYPE_DOT1AD))))
151 ethernet_vlan_header_t *outer = (ethernet_vlan_header_t *) (h0 + 1);
153 outer = (outer->type == clib_host_to_net_u16 (ETHERNET_TYPE_VLAN)) ?
155 if (PREDICT_TRUE (outer->type) ==
156 clib_host_to_net_u16 (ETHERNET_TYPE_IP4))
158 ip4_header_t *ip = (ip4_header_t *) (outer + 1);
160 (u64) (ip->src_address.as_u32 ^
161 ip->dst_address.as_u32 ^ ip->protocol);
163 else if (outer->type == clib_host_to_net_u16 (ETHERNET_TYPE_IP6))
165 ip6_header_t *ip = (ip6_header_t *) (outer + 1);
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);
171 else if (outer->type == clib_host_to_net_u16 (ETHERNET_TYPE_MPLS))
173 hash_key = mpls_get_key ((mpls_unicast_header_t *) (outer + 1));
177 hash_key = outer->type;
189 eth_get_key (ethernet_header_t * h0)
193 if (PREDICT_TRUE (h0->type) == clib_host_to_net_u16 (ETHERNET_TYPE_IP4))
195 hash_key = ipv4_get_key ((ip4_header_t *) (h0 + 1));
197 else if (h0->type == clib_host_to_net_u16 (ETHERNET_TYPE_IP6))
199 hash_key = ipv6_get_key ((ip6_header_t *) (h0 + 1));
201 else if (h0->type == clib_host_to_net_u16 (ETHERNET_TYPE_MPLS))
203 hash_key = mpls_get_key ((mpls_unicast_header_t *) (h0 + 1));
205 else if ((h0->type == clib_host_to_net_u16 (ETHERNET_TYPE_VLAN)) ||
206 (h0->type == clib_host_to_net_u16 (ETHERNET_TYPE_DOT1AD)))
208 ethernet_vlan_header_t *outer = (ethernet_vlan_header_t *) (h0 + 1);
210 outer = (outer->type == clib_host_to_net_u16 (ETHERNET_TYPE_VLAN)) ?
212 if (PREDICT_TRUE (outer->type) ==
213 clib_host_to_net_u16 (ETHERNET_TYPE_IP4))
215 hash_key = ipv4_get_key ((ip4_header_t *) (outer + 1));
217 else if (outer->type == clib_host_to_net_u16 (ETHERNET_TYPE_IP6))
219 hash_key = ipv6_get_key ((ip6_header_t *) (outer + 1));
221 else if (outer->type == clib_host_to_net_u16 (ETHERNET_TYPE_MPLS))
223 hash_key = mpls_get_key ((mpls_unicast_header_t *) (outer + 1));
227 hash_key = outer->type;
238 #endif /* included_vnet_handoff_h */
241 * fd.io coding-style-patch-verification: ON
244 * eval: (c-set-style "gnu")