blob: 0a05b80d00fc467904ac4b48482a1e027b5055f4 [file] [log] [blame]
Mohsin Kazmi41b23bc2021-06-30 18:26:25 +00001/*
2 * SPDX-License-Identifier: Apache-2.0
3 * Copyright(c) 2021 Cisco Systems, Inc.
4 */
5
6#include <vnet/vnet.h>
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>
14
Damjan Marion26abb602021-10-31 20:18:13 +010015#ifdef clib_crc32c_uses_intrinsics
16
Mohsin Kazmi41b23bc2021-06-30 18:26:25 +000017typedef union
18{
19 struct
20 {
21 ip46_address_t src_address;
22 ip46_address_t dst_address;
23 union
24 {
25 struct
26 {
27 u16 src_port;
28 u16 dst_port;
29 };
30 u32 l4_hdr;
31 };
32 };
33 u8 as_u8[36];
34} crc32c_5tuple_key_t;
35
36static 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,
41};
42
43static_always_inline void
44compute_ip6_key (ip6_header_t *ip, crc32c_5tuple_key_t *k)
45{
46 u8 pr;
47
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);
50 pr = ip->protocol;
51 /* write l4 header */
52 k->l4_hdr = *(u32 *) ip6_next_header (ip) & pow2_mask (l4_mask_bits[pr]);
53}
54
55static_always_inline void
56compute_ip4_key (ip4_header_t *ip, crc32c_5tuple_key_t *k)
57{
58 u8 pr;
59 u64 *key = (u64 *) k;
60 /* copy 8 bytes of ip src and dst addresses into hash_key_t */
61 key[0] = 0;
62 key[1] = 0;
63 key[2] = 0;
64 key[3] = *(u64 *) ((u8 *) ip + 12);
65 pr = ip->protocol;
66 /* write l4 header */
67 k->l4_hdr = *(u32 *) ip4_next_header (ip) & pow2_mask (l4_mask_bits[pr]);
68}
69static_always_inline void
70compute_ip_key (void *p, crc32c_5tuple_key_t *key)
71{
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);
76}
77
78void
79vnet_crc32c_5tuple_ip_func (void **p, u32 *hash, u32 n_packets)
80{
81 u32 n_left_from = n_packets;
82
83 while (n_left_from >= 8)
84 {
Mohsin Kazmief356f52021-09-21 11:05:15 +000085 crc32c_5tuple_key_t key[4] = {};
Mohsin Kazmi41b23bc2021-06-30 18:26:25 +000086
87 clib_prefetch_load (p[4]);
88 clib_prefetch_load (p[5]);
89 clib_prefetch_load (p[6]);
90 clib_prefetch_load (p[7]);
91
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]);
96
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]));
101
102 hash += 4;
103 n_left_from -= 4;
104 p += 4;
105 }
106
107 while (n_left_from > 0)
108 {
Mohsin Kazmief356f52021-09-21 11:05:15 +0000109 crc32c_5tuple_key_t key = {};
Mohsin Kazmi41b23bc2021-06-30 18:26:25 +0000110
111 compute_ip_key (p[0], &key);
112
113 hash[0] = clib_crc32c (key.as_u8, sizeof (key));
114
115 hash += 1;
116 n_left_from -= 1;
117 p += 1;
118 }
119}
120
121static_always_inline void
122compute_ethernet_key (void *p, crc32c_5tuple_key_t *key)
123{
124 u16 ethertype = 0, l2hdr_sz = 0;
125
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);
129
130 if (ethernet_frame_is_tagged (ethertype))
131 {
132 ethernet_vlan_header_t *vlan = (ethernet_vlan_header_t *) (eh + 1);
133
134 ethertype = clib_net_to_host_u16 (vlan->type);
135 l2hdr_sz += sizeof (*vlan);
136 while (ethernet_frame_is_tagged (ethertype))
137 {
138 vlan++;
139 ethertype = clib_net_to_host_u16 (vlan->type);
140 l2hdr_sz += sizeof (*vlan);
141 }
142 }
143
144 if (ethertype == ETHERNET_TYPE_IP4)
145 {
146 ip4_header_t *ip4 = (ip4_header_t *) (p + l2hdr_sz);
147 compute_ip4_key (ip4, key);
148 }
149 else if (ethertype == ETHERNET_TYPE_IP6)
150 {
151 ip6_header_t *ip6 = (ip6_header_t *) (p + l2hdr_sz);
152 compute_ip6_key (ip6, key);
153 }
154}
155
156void
157vnet_crc32c_5tuple_ethernet_func (void **p, u32 *hash, u32 n_packets)
158{
159 u32 n_left_from = n_packets;
160
161 while (n_left_from >= 8)
162 {
Mohsin Kazmief356f52021-09-21 11:05:15 +0000163 crc32c_5tuple_key_t key[4] = {};
Mohsin Kazmi41b23bc2021-06-30 18:26:25 +0000164
165 clib_prefetch_load (p[4]);
166 clib_prefetch_load (p[5]);
167 clib_prefetch_load (p[6]);
168 clib_prefetch_load (p[7]);
169
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]);
174
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]));
179
180 hash += 4;
181 n_left_from -= 4;
182 p += 4;
183 }
184
185 while (n_left_from > 0)
186 {
Mohsin Kazmief356f52021-09-21 11:05:15 +0000187 crc32c_5tuple_key_t key = {};
Mohsin Kazmi41b23bc2021-06-30 18:26:25 +0000188
189 compute_ethernet_key (p[0], &key);
190
191 hash[0] = clib_crc32c (key.as_u8, sizeof (key));
192
193 hash += 1;
194 n_left_from -= 1;
195 p += 1;
196 }
197}
198
199VNET_REGISTER_HASH_FUNCTION (crc32c_5tuple, static) = {
200 .name = "crc32c-5tuple",
201 .description = "IPv4/IPv6 header and TCP/UDP ports",
202 .priority = 50,
203 .function[VNET_HASH_FN_TYPE_ETHERNET] = vnet_crc32c_5tuple_ethernet_func,
204 .function[VNET_HASH_FN_TYPE_IP] = vnet_crc32c_5tuple_ip_func,
205};
Damjan Marion26abb602021-10-31 20:18:13 +0100206
207#endif