2 * SPDX-License-Identifier: Apache-2.0
3 * Copyright(c) 2021 Cisco Systems, Inc.
7 #include <vnet/ethernet/ethernet.h>
8 #include <vnet/ip/ip4_packet.h>
9 #include <vnet/ip/ip6_packet.h>
10 #include <vnet/ip/ip46_address.h>
11 #include <vnet/udp/udp_packet.h>
12 #include <vnet/hash/hash.h>
13 #include <vppinfra/crc32.h>
15 #ifdef clib_crc32c_uses_intrinsics
21 ip46_address_t src_address;
22 ip46_address_t dst_address;
34 } crc32c_5tuple_key_t;
36 static const u8 l4_mask_bits[256] = {
37 [IP_PROTOCOL_ICMP] = 16, [IP_PROTOCOL_IGMP] = 8,
38 [IP_PROTOCOL_TCP] = 32, [IP_PROTOCOL_UDP] = 32,
39 [IP_PROTOCOL_IPSEC_ESP] = 32, [IP_PROTOCOL_IPSEC_AH] = 32,
40 [IP_PROTOCOL_ICMP6] = 16,
43 static_always_inline void
44 compute_ip6_key (ip6_header_t *ip, crc32c_5tuple_key_t *k)
48 /* copy 32 bytes of ip6 src and dst addresses into hash_key_t */
49 clib_memcpy_fast ((u8 *) k, (u8 *) ip + 8, sizeof (ip6_address_t) * 2);
52 k->l4_hdr = *(u32 *) ip6_next_header (ip) & pow2_mask (l4_mask_bits[pr]);
55 static_always_inline void
56 compute_ip4_key (ip4_header_t *ip, crc32c_5tuple_key_t *k)
60 /* copy 8 bytes of ip src and dst addresses into hash_key_t */
64 key[3] = *(u64 *) ((u8 *) ip + 12);
67 k->l4_hdr = *(u32 *) ip4_next_header (ip) & pow2_mask (l4_mask_bits[pr]);
69 static_always_inline void
70 compute_ip_key (void *p, crc32c_5tuple_key_t *key)
72 if ((((u8 *) p)[0] & 0xf0) == 0x40)
73 compute_ip4_key (p, key);
74 else if ((((u8 *) p)[0] & 0xf0) == 0x60)
75 compute_ip6_key (p, key);
79 vnet_crc32c_5tuple_ip_func (void **p, u32 *hash, u32 n_packets)
81 u32 n_left_from = n_packets;
83 while (n_left_from >= 8)
85 crc32c_5tuple_key_t key[4] = {};
87 clib_prefetch_load (p[4]);
88 clib_prefetch_load (p[5]);
89 clib_prefetch_load (p[6]);
90 clib_prefetch_load (p[7]);
92 compute_ip_key (p[0], &key[0]);
93 compute_ip_key (p[1], &key[1]);
94 compute_ip_key (p[2], &key[2]);
95 compute_ip_key (p[3], &key[3]);
97 hash[0] = clib_crc32c (key[0].as_u8, sizeof (key[0]));
98 hash[1] = clib_crc32c (key[1].as_u8, sizeof (key[1]));
99 hash[2] = clib_crc32c (key[2].as_u8, sizeof (key[2]));
100 hash[3] = clib_crc32c (key[3].as_u8, sizeof (key[3]));
107 while (n_left_from > 0)
109 crc32c_5tuple_key_t key = {};
111 compute_ip_key (p[0], &key);
113 hash[0] = clib_crc32c (key.as_u8, sizeof (key));
121 static_always_inline void
122 compute_ethernet_key (void *p, crc32c_5tuple_key_t *key)
124 u16 ethertype = 0, l2hdr_sz = 0;
126 ethernet_header_t *eh = (ethernet_header_t *) p;
127 ethertype = clib_net_to_host_u16 (eh->type);
128 l2hdr_sz = sizeof (ethernet_header_t);
130 if (ethernet_frame_is_tagged (ethertype))
132 ethernet_vlan_header_t *vlan = (ethernet_vlan_header_t *) (eh + 1);
134 ethertype = clib_net_to_host_u16 (vlan->type);
135 l2hdr_sz += sizeof (*vlan);
136 while (ethernet_frame_is_tagged (ethertype))
139 ethertype = clib_net_to_host_u16 (vlan->type);
140 l2hdr_sz += sizeof (*vlan);
144 if (ethertype == ETHERNET_TYPE_IP4)
146 ip4_header_t *ip4 = (ip4_header_t *) (p + l2hdr_sz);
147 compute_ip4_key (ip4, key);
149 else if (ethertype == ETHERNET_TYPE_IP6)
151 ip6_header_t *ip6 = (ip6_header_t *) (p + l2hdr_sz);
152 compute_ip6_key (ip6, key);
157 vnet_crc32c_5tuple_ethernet_func (void **p, u32 *hash, u32 n_packets)
159 u32 n_left_from = n_packets;
161 while (n_left_from >= 8)
163 crc32c_5tuple_key_t key[4] = {};
165 clib_prefetch_load (p[4]);
166 clib_prefetch_load (p[5]);
167 clib_prefetch_load (p[6]);
168 clib_prefetch_load (p[7]);
170 compute_ethernet_key (p[0], &key[0]);
171 compute_ethernet_key (p[1], &key[1]);
172 compute_ethernet_key (p[2], &key[2]);
173 compute_ethernet_key (p[3], &key[3]);
175 hash[0] = clib_crc32c (key[0].as_u8, sizeof (key[0]));
176 hash[1] = clib_crc32c (key[1].as_u8, sizeof (key[1]));
177 hash[2] = clib_crc32c (key[2].as_u8, sizeof (key[2]));
178 hash[3] = clib_crc32c (key[3].as_u8, sizeof (key[3]));
185 while (n_left_from > 0)
187 crc32c_5tuple_key_t key = {};
189 compute_ethernet_key (p[0], &key);
191 hash[0] = clib_crc32c (key.as_u8, sizeof (key));
199 VNET_REGISTER_HASH_FUNCTION (crc32c_5tuple, static) = {
200 .name = "crc32c-5tuple",
201 .description = "IPv4/IPv6 header and TCP/UDP ports",
203 .function[VNET_HASH_FN_TYPE_ETHERNET] = vnet_crc32c_5tuple_ethernet_func,
204 .function[VNET_HASH_FN_TYPE_IP] = vnet_crc32c_5tuple_ip_func,