xref: /linux/drivers/net/xen-netback/hash.c (revision 9410645520e9b820069761f3450ef6661418e279)
140d8abdeSPaul Durrant /*
240d8abdeSPaul Durrant  * Copyright (c) 2016 Citrix Systems Inc.
340d8abdeSPaul Durrant  *
440d8abdeSPaul Durrant  * This program is free software; you can redistribute it and/or
540d8abdeSPaul Durrant  * modify it under the terms of the GNU General Public License version 2
640d8abdeSPaul Durrant  * as published by the Free Softare Foundation; or, when distributed
740d8abdeSPaul Durrant  * separately from the Linux kernel or incorporated into other
840d8abdeSPaul Durrant  * software packages, subject to the following license:
940d8abdeSPaul Durrant  *
1040d8abdeSPaul Durrant  * Permission is hereby granted, free of charge, to any person obtaining a copy
1140d8abdeSPaul Durrant  * of this source file (the "Software"), to deal in the Software without
1240d8abdeSPaul Durrant  * restriction, including without limitation the rights to use, copy, modify,
1340d8abdeSPaul Durrant  * merge, publish, distribute, sublicense, and/or sell copies of the Software,
1440d8abdeSPaul Durrant  * and to permit persons to whom the Software is furnished to do so, subject to
1540d8abdeSPaul Durrant  * the following conditions:
1640d8abdeSPaul Durrant  *
1740d8abdeSPaul Durrant  * The above copyright notice and this permission notice shall be included in
1840d8abdeSPaul Durrant  * all copies or substantial portions of the Software.
1940d8abdeSPaul Durrant  *
2040d8abdeSPaul Durrant  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
2140d8abdeSPaul Durrant  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
2240d8abdeSPaul Durrant  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
2340d8abdeSPaul Durrant  * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
2440d8abdeSPaul Durrant  * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
2540d8abdeSPaul Durrant  * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
2640d8abdeSPaul Durrant  * IN THE SOFTWARE.
2740d8abdeSPaul Durrant  */
2840d8abdeSPaul Durrant 
2940d8abdeSPaul Durrant #define XEN_NETIF_DEFINE_TOEPLITZ
3040d8abdeSPaul Durrant 
3140d8abdeSPaul Durrant #include "common.h"
3240d8abdeSPaul Durrant #include <linux/vmalloc.h>
3340d8abdeSPaul Durrant #include <linux/rculist.h>
3440d8abdeSPaul Durrant 
xenvif_add_hash(struct xenvif * vif,const u8 * tag,unsigned int len,u32 val)3540d8abdeSPaul Durrant static void xenvif_add_hash(struct xenvif *vif, const u8 *tag,
3640d8abdeSPaul Durrant 			    unsigned int len, u32 val)
3740d8abdeSPaul Durrant {
3840d8abdeSPaul Durrant 	struct xenvif_hash_cache_entry *new, *entry, *oldest;
3940d8abdeSPaul Durrant 	unsigned long flags;
4040d8abdeSPaul Durrant 	bool found;
4140d8abdeSPaul Durrant 
429f674e48SAnoob Soman 	new = kmalloc(sizeof(*entry), GFP_ATOMIC);
4340d8abdeSPaul Durrant 	if (!new)
4440d8abdeSPaul Durrant 		return;
4540d8abdeSPaul Durrant 
4640d8abdeSPaul Durrant 	memcpy(new->tag, tag, len);
4740d8abdeSPaul Durrant 	new->len = len;
4840d8abdeSPaul Durrant 	new->val = val;
4940d8abdeSPaul Durrant 
5040d8abdeSPaul Durrant 	spin_lock_irqsave(&vif->hash.cache.lock, flags);
5140d8abdeSPaul Durrant 
5240d8abdeSPaul Durrant 	found = false;
5340d8abdeSPaul Durrant 	oldest = NULL;
54f3265971SMadhuparna Bhowmik 	list_for_each_entry_rcu(entry, &vif->hash.cache.list, link,
55f3265971SMadhuparna Bhowmik 				lockdep_is_held(&vif->hash.cache.lock)) {
5640d8abdeSPaul Durrant 		/* Make sure we don't add duplicate entries */
5740d8abdeSPaul Durrant 		if (entry->len == len &&
5840d8abdeSPaul Durrant 		    memcmp(entry->tag, tag, len) == 0)
5940d8abdeSPaul Durrant 			found = true;
6040d8abdeSPaul Durrant 		if (!oldest || entry->seq < oldest->seq)
6140d8abdeSPaul Durrant 			oldest = entry;
6240d8abdeSPaul Durrant 	}
6340d8abdeSPaul Durrant 
6440d8abdeSPaul Durrant 	if (!found) {
6540d8abdeSPaul Durrant 		new->seq = atomic_inc_return(&vif->hash.cache.seq);
6640d8abdeSPaul Durrant 		list_add_rcu(&new->link, &vif->hash.cache.list);
6740d8abdeSPaul Durrant 
6840d8abdeSPaul Durrant 		if (++vif->hash.cache.count > xenvif_hash_cache_size) {
6940d8abdeSPaul Durrant 			list_del_rcu(&oldest->link);
7040d8abdeSPaul Durrant 			vif->hash.cache.count--;
711345b1acSWei Yongjun 			kfree_rcu(oldest, rcu);
7240d8abdeSPaul Durrant 		}
7340d8abdeSPaul Durrant 	}
7440d8abdeSPaul Durrant 
7540d8abdeSPaul Durrant 	spin_unlock_irqrestore(&vif->hash.cache.lock, flags);
7640d8abdeSPaul Durrant 
7740d8abdeSPaul Durrant 	if (found)
7840d8abdeSPaul Durrant 		kfree(new);
7940d8abdeSPaul Durrant }
8040d8abdeSPaul Durrant 
xenvif_new_hash(struct xenvif * vif,const u8 * data,unsigned int len)8140d8abdeSPaul Durrant static u32 xenvif_new_hash(struct xenvif *vif, const u8 *data,
8240d8abdeSPaul Durrant 			   unsigned int len)
8340d8abdeSPaul Durrant {
8440d8abdeSPaul Durrant 	u32 val;
8540d8abdeSPaul Durrant 
8640d8abdeSPaul Durrant 	val = xen_netif_toeplitz_hash(vif->hash.key,
8740d8abdeSPaul Durrant 				      sizeof(vif->hash.key),
8840d8abdeSPaul Durrant 				      data, len);
8940d8abdeSPaul Durrant 
9040d8abdeSPaul Durrant 	if (xenvif_hash_cache_size != 0)
9140d8abdeSPaul Durrant 		xenvif_add_hash(vif, data, len, val);
9240d8abdeSPaul Durrant 
9340d8abdeSPaul Durrant 	return val;
9440d8abdeSPaul Durrant }
9540d8abdeSPaul Durrant 
xenvif_flush_hash(struct xenvif * vif)9640d8abdeSPaul Durrant static void xenvif_flush_hash(struct xenvif *vif)
9740d8abdeSPaul Durrant {
98*0fa5e94aSJeongjun Park 	struct xenvif_hash_cache_entry *entry, *n;
9940d8abdeSPaul Durrant 	unsigned long flags;
10040d8abdeSPaul Durrant 
10140d8abdeSPaul Durrant 	if (xenvif_hash_cache_size == 0)
10240d8abdeSPaul Durrant 		return;
10340d8abdeSPaul Durrant 
10440d8abdeSPaul Durrant 	spin_lock_irqsave(&vif->hash.cache.lock, flags);
10540d8abdeSPaul Durrant 
106*0fa5e94aSJeongjun Park 	list_for_each_entry_safe(entry, n, &vif->hash.cache.list, link) {
10740d8abdeSPaul Durrant 		list_del_rcu(&entry->link);
10840d8abdeSPaul Durrant 		vif->hash.cache.count--;
1091345b1acSWei Yongjun 		kfree_rcu(entry, rcu);
11040d8abdeSPaul Durrant 	}
11140d8abdeSPaul Durrant 
11240d8abdeSPaul Durrant 	spin_unlock_irqrestore(&vif->hash.cache.lock, flags);
11340d8abdeSPaul Durrant }
11440d8abdeSPaul Durrant 
xenvif_find_hash(struct xenvif * vif,const u8 * data,unsigned int len)11540d8abdeSPaul Durrant static u32 xenvif_find_hash(struct xenvif *vif, const u8 *data,
11640d8abdeSPaul Durrant 			    unsigned int len)
11740d8abdeSPaul Durrant {
11840d8abdeSPaul Durrant 	struct xenvif_hash_cache_entry *entry;
11940d8abdeSPaul Durrant 	u32 val;
12040d8abdeSPaul Durrant 	bool found;
12140d8abdeSPaul Durrant 
12240d8abdeSPaul Durrant 	if (len >= XEN_NETBK_HASH_TAG_SIZE)
12340d8abdeSPaul Durrant 		return 0;
12440d8abdeSPaul Durrant 
12540d8abdeSPaul Durrant 	if (xenvif_hash_cache_size == 0)
12640d8abdeSPaul Durrant 		return xenvif_new_hash(vif, data, len);
12740d8abdeSPaul Durrant 
12840d8abdeSPaul Durrant 	rcu_read_lock();
12940d8abdeSPaul Durrant 
13040d8abdeSPaul Durrant 	found = false;
13140d8abdeSPaul Durrant 
13240d8abdeSPaul Durrant 	list_for_each_entry_rcu(entry, &vif->hash.cache.list, link) {
13340d8abdeSPaul Durrant 		if (entry->len == len &&
13440d8abdeSPaul Durrant 		    memcmp(entry->tag, data, len) == 0) {
13540d8abdeSPaul Durrant 			val = entry->val;
13640d8abdeSPaul Durrant 			entry->seq = atomic_inc_return(&vif->hash.cache.seq);
13740d8abdeSPaul Durrant 			found = true;
13840d8abdeSPaul Durrant 			break;
13940d8abdeSPaul Durrant 		}
14040d8abdeSPaul Durrant 	}
14140d8abdeSPaul Durrant 
14240d8abdeSPaul Durrant 	rcu_read_unlock();
14340d8abdeSPaul Durrant 
14440d8abdeSPaul Durrant 	if (!found)
14540d8abdeSPaul Durrant 		val = xenvif_new_hash(vif, data, len);
14640d8abdeSPaul Durrant 
14740d8abdeSPaul Durrant 	return val;
14840d8abdeSPaul Durrant }
14940d8abdeSPaul Durrant 
xenvif_set_skb_hash(struct xenvif * vif,struct sk_buff * skb)15040d8abdeSPaul Durrant void xenvif_set_skb_hash(struct xenvif *vif, struct sk_buff *skb)
15140d8abdeSPaul Durrant {
15240d8abdeSPaul Durrant 	struct flow_keys flow;
15340d8abdeSPaul Durrant 	u32 hash = 0;
15440d8abdeSPaul Durrant 	enum pkt_hash_types type = PKT_HASH_TYPE_NONE;
15540d8abdeSPaul Durrant 	u32 flags = vif->hash.flags;
15640d8abdeSPaul Durrant 	bool has_tcp_hdr;
15740d8abdeSPaul Durrant 
15840d8abdeSPaul Durrant 	/* Quick rejection test: If the network protocol doesn't
15940d8abdeSPaul Durrant 	 * correspond to any enabled hash type then there's no point
16040d8abdeSPaul Durrant 	 * in parsing the packet header.
16140d8abdeSPaul Durrant 	 */
16240d8abdeSPaul Durrant 	switch (skb->protocol) {
16340d8abdeSPaul Durrant 	case htons(ETH_P_IP):
16440d8abdeSPaul Durrant 		if (flags & (XEN_NETIF_CTRL_HASH_TYPE_IPV4_TCP |
16540d8abdeSPaul Durrant 			     XEN_NETIF_CTRL_HASH_TYPE_IPV4))
16640d8abdeSPaul Durrant 			break;
16740d8abdeSPaul Durrant 
16840d8abdeSPaul Durrant 		goto done;
16940d8abdeSPaul Durrant 
17040d8abdeSPaul Durrant 	case htons(ETH_P_IPV6):
17140d8abdeSPaul Durrant 		if (flags & (XEN_NETIF_CTRL_HASH_TYPE_IPV6_TCP |
17240d8abdeSPaul Durrant 			     XEN_NETIF_CTRL_HASH_TYPE_IPV6))
17340d8abdeSPaul Durrant 			break;
17440d8abdeSPaul Durrant 
17540d8abdeSPaul Durrant 		goto done;
17640d8abdeSPaul Durrant 
17740d8abdeSPaul Durrant 	default:
17840d8abdeSPaul Durrant 		goto done;
17940d8abdeSPaul Durrant 	}
18040d8abdeSPaul Durrant 
18140d8abdeSPaul Durrant 	memset(&flow, 0, sizeof(flow));
18240d8abdeSPaul Durrant 	if (!skb_flow_dissect_flow_keys(skb, &flow, 0))
18340d8abdeSPaul Durrant 		goto done;
18440d8abdeSPaul Durrant 
18540d8abdeSPaul Durrant 	has_tcp_hdr = (flow.basic.ip_proto == IPPROTO_TCP) &&
18640d8abdeSPaul Durrant 		      !(flow.control.flags & FLOW_DIS_IS_FRAGMENT);
18740d8abdeSPaul Durrant 
18840d8abdeSPaul Durrant 	switch (skb->protocol) {
18940d8abdeSPaul Durrant 	case htons(ETH_P_IP):
19040d8abdeSPaul Durrant 		if (has_tcp_hdr &&
19140d8abdeSPaul Durrant 		    (flags & XEN_NETIF_CTRL_HASH_TYPE_IPV4_TCP)) {
19240d8abdeSPaul Durrant 			u8 data[12];
19340d8abdeSPaul Durrant 
19440d8abdeSPaul Durrant 			memcpy(&data[0], &flow.addrs.v4addrs.src, 4);
19540d8abdeSPaul Durrant 			memcpy(&data[4], &flow.addrs.v4addrs.dst, 4);
19640d8abdeSPaul Durrant 			memcpy(&data[8], &flow.ports.src, 2);
19740d8abdeSPaul Durrant 			memcpy(&data[10], &flow.ports.dst, 2);
19840d8abdeSPaul Durrant 
19940d8abdeSPaul Durrant 			hash = xenvif_find_hash(vif, data, sizeof(data));
20040d8abdeSPaul Durrant 			type = PKT_HASH_TYPE_L4;
20140d8abdeSPaul Durrant 		} else if (flags & XEN_NETIF_CTRL_HASH_TYPE_IPV4) {
20240d8abdeSPaul Durrant 			u8 data[8];
20340d8abdeSPaul Durrant 
20440d8abdeSPaul Durrant 			memcpy(&data[0], &flow.addrs.v4addrs.src, 4);
20540d8abdeSPaul Durrant 			memcpy(&data[4], &flow.addrs.v4addrs.dst, 4);
20640d8abdeSPaul Durrant 
20740d8abdeSPaul Durrant 			hash = xenvif_find_hash(vif, data, sizeof(data));
20840d8abdeSPaul Durrant 			type = PKT_HASH_TYPE_L3;
20940d8abdeSPaul Durrant 		}
21040d8abdeSPaul Durrant 
21140d8abdeSPaul Durrant 		break;
21240d8abdeSPaul Durrant 
21340d8abdeSPaul Durrant 	case htons(ETH_P_IPV6):
21440d8abdeSPaul Durrant 		if (has_tcp_hdr &&
21540d8abdeSPaul Durrant 		    (flags & XEN_NETIF_CTRL_HASH_TYPE_IPV6_TCP)) {
21640d8abdeSPaul Durrant 			u8 data[36];
21740d8abdeSPaul Durrant 
21840d8abdeSPaul Durrant 			memcpy(&data[0], &flow.addrs.v6addrs.src, 16);
21940d8abdeSPaul Durrant 			memcpy(&data[16], &flow.addrs.v6addrs.dst, 16);
22040d8abdeSPaul Durrant 			memcpy(&data[32], &flow.ports.src, 2);
22140d8abdeSPaul Durrant 			memcpy(&data[34], &flow.ports.dst, 2);
22240d8abdeSPaul Durrant 
22340d8abdeSPaul Durrant 			hash = xenvif_find_hash(vif, data, sizeof(data));
22440d8abdeSPaul Durrant 			type = PKT_HASH_TYPE_L4;
22540d8abdeSPaul Durrant 		} else if (flags & XEN_NETIF_CTRL_HASH_TYPE_IPV6) {
22640d8abdeSPaul Durrant 			u8 data[32];
22740d8abdeSPaul Durrant 
22840d8abdeSPaul Durrant 			memcpy(&data[0], &flow.addrs.v6addrs.src, 16);
22940d8abdeSPaul Durrant 			memcpy(&data[16], &flow.addrs.v6addrs.dst, 16);
23040d8abdeSPaul Durrant 
23140d8abdeSPaul Durrant 			hash = xenvif_find_hash(vif, data, sizeof(data));
23240d8abdeSPaul Durrant 			type = PKT_HASH_TYPE_L3;
23340d8abdeSPaul Durrant 		}
23440d8abdeSPaul Durrant 
23540d8abdeSPaul Durrant 		break;
23640d8abdeSPaul Durrant 	}
23740d8abdeSPaul Durrant 
23840d8abdeSPaul Durrant done:
23940d8abdeSPaul Durrant 	if (type == PKT_HASH_TYPE_NONE)
24040d8abdeSPaul Durrant 		skb_clear_hash(skb);
24140d8abdeSPaul Durrant 	else
24240d8abdeSPaul Durrant 		__skb_set_sw_hash(skb, hash, type == PKT_HASH_TYPE_L4);
24340d8abdeSPaul Durrant }
24440d8abdeSPaul Durrant 
xenvif_set_hash_alg(struct xenvif * vif,u32 alg)24540d8abdeSPaul Durrant u32 xenvif_set_hash_alg(struct xenvif *vif, u32 alg)
24640d8abdeSPaul Durrant {
24740d8abdeSPaul Durrant 	switch (alg) {
24840d8abdeSPaul Durrant 	case XEN_NETIF_CTRL_HASH_ALGORITHM_NONE:
24940d8abdeSPaul Durrant 	case XEN_NETIF_CTRL_HASH_ALGORITHM_TOEPLITZ:
25040d8abdeSPaul Durrant 		break;
25140d8abdeSPaul Durrant 
25240d8abdeSPaul Durrant 	default:
25340d8abdeSPaul Durrant 		return XEN_NETIF_CTRL_STATUS_INVALID_PARAMETER;
25440d8abdeSPaul Durrant 	}
25540d8abdeSPaul Durrant 
25640d8abdeSPaul Durrant 	vif->hash.alg = alg;
25740d8abdeSPaul Durrant 
25840d8abdeSPaul Durrant 	return XEN_NETIF_CTRL_STATUS_SUCCESS;
25940d8abdeSPaul Durrant }
26040d8abdeSPaul Durrant 
xenvif_get_hash_flags(struct xenvif * vif,u32 * flags)26140d8abdeSPaul Durrant u32 xenvif_get_hash_flags(struct xenvif *vif, u32 *flags)
26240d8abdeSPaul Durrant {
26340d8abdeSPaul Durrant 	if (vif->hash.alg == XEN_NETIF_CTRL_HASH_ALGORITHM_NONE)
26440d8abdeSPaul Durrant 		return XEN_NETIF_CTRL_STATUS_NOT_SUPPORTED;
26540d8abdeSPaul Durrant 
26640d8abdeSPaul Durrant 	*flags = XEN_NETIF_CTRL_HASH_TYPE_IPV4 |
26740d8abdeSPaul Durrant 		 XEN_NETIF_CTRL_HASH_TYPE_IPV4_TCP |
26840d8abdeSPaul Durrant 		 XEN_NETIF_CTRL_HASH_TYPE_IPV6 |
26940d8abdeSPaul Durrant 		 XEN_NETIF_CTRL_HASH_TYPE_IPV6_TCP;
27040d8abdeSPaul Durrant 
27140d8abdeSPaul Durrant 	return XEN_NETIF_CTRL_STATUS_SUCCESS;
27240d8abdeSPaul Durrant }
27340d8abdeSPaul Durrant 
xenvif_set_hash_flags(struct xenvif * vif,u32 flags)27440d8abdeSPaul Durrant u32 xenvif_set_hash_flags(struct xenvif *vif, u32 flags)
27540d8abdeSPaul Durrant {
27640d8abdeSPaul Durrant 	if (flags & ~(XEN_NETIF_CTRL_HASH_TYPE_IPV4 |
27740d8abdeSPaul Durrant 		      XEN_NETIF_CTRL_HASH_TYPE_IPV4_TCP |
27840d8abdeSPaul Durrant 		      XEN_NETIF_CTRL_HASH_TYPE_IPV6 |
27940d8abdeSPaul Durrant 		      XEN_NETIF_CTRL_HASH_TYPE_IPV6_TCP))
28040d8abdeSPaul Durrant 		return XEN_NETIF_CTRL_STATUS_INVALID_PARAMETER;
28140d8abdeSPaul Durrant 
28240d8abdeSPaul Durrant 	if (vif->hash.alg == XEN_NETIF_CTRL_HASH_ALGORITHM_NONE)
28340d8abdeSPaul Durrant 		return XEN_NETIF_CTRL_STATUS_INVALID_PARAMETER;
28440d8abdeSPaul Durrant 
28540d8abdeSPaul Durrant 	vif->hash.flags = flags;
28640d8abdeSPaul Durrant 
28740d8abdeSPaul Durrant 	return XEN_NETIF_CTRL_STATUS_SUCCESS;
28840d8abdeSPaul Durrant }
28940d8abdeSPaul Durrant 
xenvif_set_hash_key(struct xenvif * vif,u32 gref,u32 len)29040d8abdeSPaul Durrant u32 xenvif_set_hash_key(struct xenvif *vif, u32 gref, u32 len)
29140d8abdeSPaul Durrant {
29240d8abdeSPaul Durrant 	u8 *key = vif->hash.key;
29340d8abdeSPaul Durrant 	struct gnttab_copy copy_op = {
29440d8abdeSPaul Durrant 		.source.u.ref = gref,
29540d8abdeSPaul Durrant 		.source.domid = vif->domid,
29640d8abdeSPaul Durrant 		.dest.u.gmfn = virt_to_gfn(key),
29740d8abdeSPaul Durrant 		.dest.domid = DOMID_SELF,
29840d8abdeSPaul Durrant 		.dest.offset = xen_offset_in_page(key),
29940d8abdeSPaul Durrant 		.len = len,
30040d8abdeSPaul Durrant 		.flags = GNTCOPY_source_gref
30140d8abdeSPaul Durrant 	};
30240d8abdeSPaul Durrant 
30340d8abdeSPaul Durrant 	if (len > XEN_NETBK_MAX_HASH_KEY_SIZE)
30440d8abdeSPaul Durrant 		return XEN_NETIF_CTRL_STATUS_INVALID_PARAMETER;
30540d8abdeSPaul Durrant 
306f86911e2SPaul Durrant 	if (copy_op.len != 0) {
30740d8abdeSPaul Durrant 		gnttab_batch_copy(&copy_op, 1);
30840d8abdeSPaul Durrant 
30940d8abdeSPaul Durrant 		if (copy_op.status != GNTST_okay)
31040d8abdeSPaul Durrant 			return XEN_NETIF_CTRL_STATUS_INVALID_PARAMETER;
31140d8abdeSPaul Durrant 	}
31240d8abdeSPaul Durrant 
31340d8abdeSPaul Durrant 	/* Clear any remaining key octets */
31440d8abdeSPaul Durrant 	if (len < XEN_NETBK_MAX_HASH_KEY_SIZE)
31540d8abdeSPaul Durrant 		memset(key + len, 0, XEN_NETBK_MAX_HASH_KEY_SIZE - len);
31640d8abdeSPaul Durrant 
31740d8abdeSPaul Durrant 	xenvif_flush_hash(vif);
31840d8abdeSPaul Durrant 
31940d8abdeSPaul Durrant 	return XEN_NETIF_CTRL_STATUS_SUCCESS;
32040d8abdeSPaul Durrant }
32140d8abdeSPaul Durrant 
xenvif_set_hash_mapping_size(struct xenvif * vif,u32 size)32240d8abdeSPaul Durrant u32 xenvif_set_hash_mapping_size(struct xenvif *vif, u32 size)
32340d8abdeSPaul Durrant {
32440d8abdeSPaul Durrant 	if (size > XEN_NETBK_MAX_HASH_MAPPING_SIZE)
32540d8abdeSPaul Durrant 		return XEN_NETIF_CTRL_STATUS_INVALID_PARAMETER;
32640d8abdeSPaul Durrant 
32740d8abdeSPaul Durrant 	vif->hash.size = size;
32822f9cde3SJan Beulich 	memset(vif->hash.mapping[vif->hash.mapping_sel], 0,
32922f9cde3SJan Beulich 	       sizeof(u32) * size);
33040d8abdeSPaul Durrant 
33140d8abdeSPaul Durrant 	return XEN_NETIF_CTRL_STATUS_SUCCESS;
33240d8abdeSPaul Durrant }
33340d8abdeSPaul Durrant 
xenvif_set_hash_mapping(struct xenvif * vif,u32 gref,u32 len,u32 off)33440d8abdeSPaul Durrant u32 xenvif_set_hash_mapping(struct xenvif *vif, u32 gref, u32 len,
33540d8abdeSPaul Durrant 			    u32 off)
33640d8abdeSPaul Durrant {
33722f9cde3SJan Beulich 	u32 *mapping = vif->hash.mapping[!vif->hash.mapping_sel];
338871088bfSJan Beulich 	unsigned int nr = 1;
339871088bfSJan Beulich 	struct gnttab_copy copy_op[2] = {{
34040d8abdeSPaul Durrant 		.source.u.ref = gref,
34140d8abdeSPaul Durrant 		.source.domid = vif->domid,
34240d8abdeSPaul Durrant 		.dest.domid = DOMID_SELF,
343780e83c2SJan Beulich 		.len = len * sizeof(*mapping),
34440d8abdeSPaul Durrant 		.flags = GNTCOPY_source_gref
345871088bfSJan Beulich 	}};
34640d8abdeSPaul Durrant 
347780e83c2SJan Beulich 	if ((off + len < off) || (off + len > vif->hash.size) ||
348780e83c2SJan Beulich 	    len > XEN_PAGE_SIZE / sizeof(*mapping))
34940d8abdeSPaul Durrant 		return XEN_NETIF_CTRL_STATUS_INVALID_PARAMETER;
35040d8abdeSPaul Durrant 
351871088bfSJan Beulich 	copy_op[0].dest.u.gmfn = virt_to_gfn(mapping + off);
352871088bfSJan Beulich 	copy_op[0].dest.offset = xen_offset_in_page(mapping + off);
353871088bfSJan Beulich 	if (copy_op[0].dest.offset + copy_op[0].len > XEN_PAGE_SIZE) {
354871088bfSJan Beulich 		copy_op[1] = copy_op[0];
355871088bfSJan Beulich 		copy_op[1].source.offset = XEN_PAGE_SIZE - copy_op[0].dest.offset;
356871088bfSJan Beulich 		copy_op[1].dest.u.gmfn = virt_to_gfn(mapping + off + len);
357871088bfSJan Beulich 		copy_op[1].dest.offset = 0;
358871088bfSJan Beulich 		copy_op[1].len = copy_op[0].len - copy_op[1].source.offset;
359871088bfSJan Beulich 		copy_op[0].len = copy_op[1].source.offset;
360871088bfSJan Beulich 		nr = 2;
361871088bfSJan Beulich 	}
362780e83c2SJan Beulich 
36322f9cde3SJan Beulich 	memcpy(mapping, vif->hash.mapping[vif->hash.mapping_sel],
36422f9cde3SJan Beulich 	       vif->hash.size * sizeof(*mapping));
36540d8abdeSPaul Durrant 
366871088bfSJan Beulich 	if (copy_op[0].len != 0) {
367871088bfSJan Beulich 		gnttab_batch_copy(copy_op, nr);
36840d8abdeSPaul Durrant 
369871088bfSJan Beulich 		if (copy_op[0].status != GNTST_okay ||
370871088bfSJan Beulich 		    copy_op[nr - 1].status != GNTST_okay)
37140d8abdeSPaul Durrant 			return XEN_NETIF_CTRL_STATUS_INVALID_PARAMETER;
37240d8abdeSPaul Durrant 	}
37340d8abdeSPaul Durrant 
37422f9cde3SJan Beulich 	while (len-- != 0)
37522f9cde3SJan Beulich 		if (mapping[off++] >= vif->num_queues)
37622f9cde3SJan Beulich 			return XEN_NETIF_CTRL_STATUS_INVALID_PARAMETER;
37722f9cde3SJan Beulich 
37822f9cde3SJan Beulich 	vif->hash.mapping_sel = !vif->hash.mapping_sel;
37922f9cde3SJan Beulich 
38040d8abdeSPaul Durrant 	return XEN_NETIF_CTRL_STATUS_SUCCESS;
38140d8abdeSPaul Durrant }
38240d8abdeSPaul Durrant 
383a9339b8eSPaul Durrant #ifdef CONFIG_DEBUG_FS
xenvif_dump_hash_info(struct xenvif * vif,struct seq_file * m)384a9339b8eSPaul Durrant void xenvif_dump_hash_info(struct xenvif *vif, struct seq_file *m)
385a9339b8eSPaul Durrant {
386a9339b8eSPaul Durrant 	unsigned int i;
387a9339b8eSPaul Durrant 
388a9339b8eSPaul Durrant 	switch (vif->hash.alg) {
389a9339b8eSPaul Durrant 	case XEN_NETIF_CTRL_HASH_ALGORITHM_TOEPLITZ:
390a9339b8eSPaul Durrant 		seq_puts(m, "Hash Algorithm: TOEPLITZ\n");
391a9339b8eSPaul Durrant 		break;
392a9339b8eSPaul Durrant 
393a9339b8eSPaul Durrant 	case XEN_NETIF_CTRL_HASH_ALGORITHM_NONE:
394a9339b8eSPaul Durrant 		seq_puts(m, "Hash Algorithm: NONE\n");
395df561f66SGustavo A. R. Silva 		fallthrough;
396a9339b8eSPaul Durrant 	default:
397a9339b8eSPaul Durrant 		return;
398a9339b8eSPaul Durrant 	}
399a9339b8eSPaul Durrant 
400a9339b8eSPaul Durrant 	if (vif->hash.flags) {
401a9339b8eSPaul Durrant 		seq_puts(m, "\nHash Flags:\n");
402a9339b8eSPaul Durrant 
403a9339b8eSPaul Durrant 		if (vif->hash.flags & XEN_NETIF_CTRL_HASH_TYPE_IPV4)
404a9339b8eSPaul Durrant 			seq_puts(m, "- IPv4\n");
405a9339b8eSPaul Durrant 		if (vif->hash.flags & XEN_NETIF_CTRL_HASH_TYPE_IPV4_TCP)
406a9339b8eSPaul Durrant 			seq_puts(m, "- IPv4 + TCP\n");
407a9339b8eSPaul Durrant 		if (vif->hash.flags & XEN_NETIF_CTRL_HASH_TYPE_IPV6)
408a9339b8eSPaul Durrant 			seq_puts(m, "- IPv6\n");
409a9339b8eSPaul Durrant 		if (vif->hash.flags & XEN_NETIF_CTRL_HASH_TYPE_IPV6_TCP)
410a9339b8eSPaul Durrant 			seq_puts(m, "- IPv6 + TCP\n");
411a9339b8eSPaul Durrant 	}
412a9339b8eSPaul Durrant 
413a9339b8eSPaul Durrant 	seq_puts(m, "\nHash Key:\n");
414a9339b8eSPaul Durrant 
415a9339b8eSPaul Durrant 	for (i = 0; i < XEN_NETBK_MAX_HASH_KEY_SIZE; ) {
416a9339b8eSPaul Durrant 		unsigned int j, n;
417a9339b8eSPaul Durrant 
418a9339b8eSPaul Durrant 		n = 8;
419a9339b8eSPaul Durrant 		if (i + n >= XEN_NETBK_MAX_HASH_KEY_SIZE)
420a9339b8eSPaul Durrant 			n = XEN_NETBK_MAX_HASH_KEY_SIZE - i;
421a9339b8eSPaul Durrant 
422a9339b8eSPaul Durrant 		seq_printf(m, "[%2u - %2u]: ", i, i + n - 1);
423a9339b8eSPaul Durrant 
424a9339b8eSPaul Durrant 		for (j = 0; j < n; j++, i++)
425a9339b8eSPaul Durrant 			seq_printf(m, "%02x ", vif->hash.key[i]);
426a9339b8eSPaul Durrant 
427a9339b8eSPaul Durrant 		seq_puts(m, "\n");
428a9339b8eSPaul Durrant 	}
429a9339b8eSPaul Durrant 
430a9339b8eSPaul Durrant 	if (vif->hash.size != 0) {
43122f9cde3SJan Beulich 		const u32 *mapping = vif->hash.mapping[vif->hash.mapping_sel];
43222f9cde3SJan Beulich 
433a9339b8eSPaul Durrant 		seq_puts(m, "\nHash Mapping:\n");
434a9339b8eSPaul Durrant 
435a9339b8eSPaul Durrant 		for (i = 0; i < vif->hash.size; ) {
436a9339b8eSPaul Durrant 			unsigned int j, n;
437a9339b8eSPaul Durrant 
438a9339b8eSPaul Durrant 			n = 8;
439a9339b8eSPaul Durrant 			if (i + n >= vif->hash.size)
440a9339b8eSPaul Durrant 				n = vif->hash.size - i;
441a9339b8eSPaul Durrant 
442a9339b8eSPaul Durrant 			seq_printf(m, "[%4u - %4u]: ", i, i + n - 1);
443a9339b8eSPaul Durrant 
444a9339b8eSPaul Durrant 			for (j = 0; j < n; j++, i++)
44522f9cde3SJan Beulich 				seq_printf(m, "%4u ", mapping[i]);
446a9339b8eSPaul Durrant 
447a9339b8eSPaul Durrant 			seq_puts(m, "\n");
448a9339b8eSPaul Durrant 		}
449a9339b8eSPaul Durrant 	}
450a9339b8eSPaul Durrant }
451a9339b8eSPaul Durrant #endif /* CONFIG_DEBUG_FS */
452a9339b8eSPaul Durrant 
xenvif_init_hash(struct xenvif * vif)45340d8abdeSPaul Durrant void xenvif_init_hash(struct xenvif *vif)
45440d8abdeSPaul Durrant {
45540d8abdeSPaul Durrant 	if (xenvif_hash_cache_size == 0)
45640d8abdeSPaul Durrant 		return;
45740d8abdeSPaul Durrant 
458a2288d4eSIgor Druzhinin 	BUG_ON(vif->hash.cache.count);
459a2288d4eSIgor Druzhinin 
46040d8abdeSPaul Durrant 	spin_lock_init(&vif->hash.cache.lock);
46140d8abdeSPaul Durrant 	INIT_LIST_HEAD(&vif->hash.cache.list);
46240d8abdeSPaul Durrant }
46340d8abdeSPaul Durrant 
xenvif_deinit_hash(struct xenvif * vif)46440d8abdeSPaul Durrant void xenvif_deinit_hash(struct xenvif *vif)
46540d8abdeSPaul Durrant {
46640d8abdeSPaul Durrant 	xenvif_flush_hash(vif);
46740d8abdeSPaul Durrant }
468