hash.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467
  1. /*
  2. * Copyright (c) 2016 Citrix Systems Inc.
  3. *
  4. * This program is free software; you can redistribute it and/or
  5. * modify it under the terms of the GNU General Public License version 2
  6. * as published by the Free Softare Foundation; or, when distributed
  7. * separately from the Linux kernel or incorporated into other
  8. * software packages, subject to the following license:
  9. *
  10. * Permission is hereby granted, free of charge, to any person obtaining a copy
  11. * of this source file (the "Software"), to deal in the Software without
  12. * restriction, including without limitation the rights to use, copy, modify,
  13. * merge, publish, distribute, sublicense, and/or sell copies of the Software,
  14. * and to permit persons to whom the Software is furnished to do so, subject to
  15. * the following conditions:
  16. *
  17. * The above copyright notice and this permission notice shall be included in
  18. * all copies or substantial portions of the Software.
  19. *
  20. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  21. * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  22. * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  23. * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  24. * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
  25. * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
  26. * IN THE SOFTWARE.
  27. */
  28. #define XEN_NETIF_DEFINE_TOEPLITZ
  29. #include "common.h"
  30. #include <linux/vmalloc.h>
  31. #include <linux/rculist.h>
  32. static void xenvif_add_hash(struct xenvif *vif, const u8 *tag,
  33. unsigned int len, u32 val)
  34. {
  35. struct xenvif_hash_cache_entry *new, *entry, *oldest;
  36. unsigned long flags;
  37. bool found;
  38. new = kmalloc(sizeof(*entry), GFP_ATOMIC);
  39. if (!new)
  40. return;
  41. memcpy(new->tag, tag, len);
  42. new->len = len;
  43. new->val = val;
  44. spin_lock_irqsave(&vif->hash.cache.lock, flags);
  45. found = false;
  46. oldest = NULL;
  47. list_for_each_entry_rcu(entry, &vif->hash.cache.list, link,
  48. lockdep_is_held(&vif->hash.cache.lock)) {
  49. /* Make sure we don't add duplicate entries */
  50. if (entry->len == len &&
  51. memcmp(entry->tag, tag, len) == 0)
  52. found = true;
  53. if (!oldest || entry->seq < oldest->seq)
  54. oldest = entry;
  55. }
  56. if (!found) {
  57. new->seq = atomic_inc_return(&vif->hash.cache.seq);
  58. list_add_rcu(&new->link, &vif->hash.cache.list);
  59. if (++vif->hash.cache.count > xenvif_hash_cache_size) {
  60. list_del_rcu(&oldest->link);
  61. vif->hash.cache.count--;
  62. kfree_rcu(oldest, rcu);
  63. }
  64. }
  65. spin_unlock_irqrestore(&vif->hash.cache.lock, flags);
  66. if (found)
  67. kfree(new);
  68. }
  69. static u32 xenvif_new_hash(struct xenvif *vif, const u8 *data,
  70. unsigned int len)
  71. {
  72. u32 val;
  73. val = xen_netif_toeplitz_hash(vif->hash.key,
  74. sizeof(vif->hash.key),
  75. data, len);
  76. if (xenvif_hash_cache_size != 0)
  77. xenvif_add_hash(vif, data, len, val);
  78. return val;
  79. }
  80. static void xenvif_flush_hash(struct xenvif *vif)
  81. {
  82. struct xenvif_hash_cache_entry *entry, *n;
  83. unsigned long flags;
  84. if (xenvif_hash_cache_size == 0)
  85. return;
  86. spin_lock_irqsave(&vif->hash.cache.lock, flags);
  87. list_for_each_entry_safe(entry, n, &vif->hash.cache.list, link) {
  88. list_del_rcu(&entry->link);
  89. vif->hash.cache.count--;
  90. kfree_rcu(entry, rcu);
  91. }
  92. spin_unlock_irqrestore(&vif->hash.cache.lock, flags);
  93. }
  94. static u32 xenvif_find_hash(struct xenvif *vif, const u8 *data,
  95. unsigned int len)
  96. {
  97. struct xenvif_hash_cache_entry *entry;
  98. u32 val;
  99. bool found;
  100. if (len >= XEN_NETBK_HASH_TAG_SIZE)
  101. return 0;
  102. if (xenvif_hash_cache_size == 0)
  103. return xenvif_new_hash(vif, data, len);
  104. rcu_read_lock();
  105. found = false;
  106. list_for_each_entry_rcu(entry, &vif->hash.cache.list, link) {
  107. if (entry->len == len &&
  108. memcmp(entry->tag, data, len) == 0) {
  109. val = entry->val;
  110. entry->seq = atomic_inc_return(&vif->hash.cache.seq);
  111. found = true;
  112. break;
  113. }
  114. }
  115. rcu_read_unlock();
  116. if (!found)
  117. val = xenvif_new_hash(vif, data, len);
  118. return val;
  119. }
  120. void xenvif_set_skb_hash(struct xenvif *vif, struct sk_buff *skb)
  121. {
  122. struct flow_keys flow;
  123. u32 hash = 0;
  124. enum pkt_hash_types type = PKT_HASH_TYPE_NONE;
  125. u32 flags = vif->hash.flags;
  126. bool has_tcp_hdr;
  127. /* Quick rejection test: If the network protocol doesn't
  128. * correspond to any enabled hash type then there's no point
  129. * in parsing the packet header.
  130. */
  131. switch (skb->protocol) {
  132. case htons(ETH_P_IP):
  133. if (flags & (XEN_NETIF_CTRL_HASH_TYPE_IPV4_TCP |
  134. XEN_NETIF_CTRL_HASH_TYPE_IPV4))
  135. break;
  136. goto done;
  137. case htons(ETH_P_IPV6):
  138. if (flags & (XEN_NETIF_CTRL_HASH_TYPE_IPV6_TCP |
  139. XEN_NETIF_CTRL_HASH_TYPE_IPV6))
  140. break;
  141. goto done;
  142. default:
  143. goto done;
  144. }
  145. memset(&flow, 0, sizeof(flow));
  146. if (!skb_flow_dissect_flow_keys(skb, &flow, 0))
  147. goto done;
  148. has_tcp_hdr = (flow.basic.ip_proto == IPPROTO_TCP) &&
  149. !(flow.control.flags & FLOW_DIS_IS_FRAGMENT);
  150. switch (skb->protocol) {
  151. case htons(ETH_P_IP):
  152. if (has_tcp_hdr &&
  153. (flags & XEN_NETIF_CTRL_HASH_TYPE_IPV4_TCP)) {
  154. u8 data[12];
  155. memcpy(&data[0], &flow.addrs.v4addrs.src, 4);
  156. memcpy(&data[4], &flow.addrs.v4addrs.dst, 4);
  157. memcpy(&data[8], &flow.ports.src, 2);
  158. memcpy(&data[10], &flow.ports.dst, 2);
  159. hash = xenvif_find_hash(vif, data, sizeof(data));
  160. type = PKT_HASH_TYPE_L4;
  161. } else if (flags & XEN_NETIF_CTRL_HASH_TYPE_IPV4) {
  162. u8 data[8];
  163. memcpy(&data[0], &flow.addrs.v4addrs.src, 4);
  164. memcpy(&data[4], &flow.addrs.v4addrs.dst, 4);
  165. hash = xenvif_find_hash(vif, data, sizeof(data));
  166. type = PKT_HASH_TYPE_L3;
  167. }
  168. break;
  169. case htons(ETH_P_IPV6):
  170. if (has_tcp_hdr &&
  171. (flags & XEN_NETIF_CTRL_HASH_TYPE_IPV6_TCP)) {
  172. u8 data[36];
  173. memcpy(&data[0], &flow.addrs.v6addrs.src, 16);
  174. memcpy(&data[16], &flow.addrs.v6addrs.dst, 16);
  175. memcpy(&data[32], &flow.ports.src, 2);
  176. memcpy(&data[34], &flow.ports.dst, 2);
  177. hash = xenvif_find_hash(vif, data, sizeof(data));
  178. type = PKT_HASH_TYPE_L4;
  179. } else if (flags & XEN_NETIF_CTRL_HASH_TYPE_IPV6) {
  180. u8 data[32];
  181. memcpy(&data[0], &flow.addrs.v6addrs.src, 16);
  182. memcpy(&data[16], &flow.addrs.v6addrs.dst, 16);
  183. hash = xenvif_find_hash(vif, data, sizeof(data));
  184. type = PKT_HASH_TYPE_L3;
  185. }
  186. break;
  187. }
  188. done:
  189. if (type == PKT_HASH_TYPE_NONE)
  190. skb_clear_hash(skb);
  191. else
  192. __skb_set_sw_hash(skb, hash, type == PKT_HASH_TYPE_L4);
  193. }
  194. u32 xenvif_set_hash_alg(struct xenvif *vif, u32 alg)
  195. {
  196. switch (alg) {
  197. case XEN_NETIF_CTRL_HASH_ALGORITHM_NONE:
  198. case XEN_NETIF_CTRL_HASH_ALGORITHM_TOEPLITZ:
  199. break;
  200. default:
  201. return XEN_NETIF_CTRL_STATUS_INVALID_PARAMETER;
  202. }
  203. vif->hash.alg = alg;
  204. return XEN_NETIF_CTRL_STATUS_SUCCESS;
  205. }
  206. u32 xenvif_get_hash_flags(struct xenvif *vif, u32 *flags)
  207. {
  208. if (vif->hash.alg == XEN_NETIF_CTRL_HASH_ALGORITHM_NONE)
  209. return XEN_NETIF_CTRL_STATUS_NOT_SUPPORTED;
  210. *flags = XEN_NETIF_CTRL_HASH_TYPE_IPV4 |
  211. XEN_NETIF_CTRL_HASH_TYPE_IPV4_TCP |
  212. XEN_NETIF_CTRL_HASH_TYPE_IPV6 |
  213. XEN_NETIF_CTRL_HASH_TYPE_IPV6_TCP;
  214. return XEN_NETIF_CTRL_STATUS_SUCCESS;
  215. }
  216. u32 xenvif_set_hash_flags(struct xenvif *vif, u32 flags)
  217. {
  218. if (flags & ~(XEN_NETIF_CTRL_HASH_TYPE_IPV4 |
  219. XEN_NETIF_CTRL_HASH_TYPE_IPV4_TCP |
  220. XEN_NETIF_CTRL_HASH_TYPE_IPV6 |
  221. XEN_NETIF_CTRL_HASH_TYPE_IPV6_TCP))
  222. return XEN_NETIF_CTRL_STATUS_INVALID_PARAMETER;
  223. if (vif->hash.alg == XEN_NETIF_CTRL_HASH_ALGORITHM_NONE)
  224. return XEN_NETIF_CTRL_STATUS_INVALID_PARAMETER;
  225. vif->hash.flags = flags;
  226. return XEN_NETIF_CTRL_STATUS_SUCCESS;
  227. }
  228. u32 xenvif_set_hash_key(struct xenvif *vif, u32 gref, u32 len)
  229. {
  230. u8 *key = vif->hash.key;
  231. struct gnttab_copy copy_op = {
  232. .source.u.ref = gref,
  233. .source.domid = vif->domid,
  234. .dest.u.gmfn = virt_to_gfn(key),
  235. .dest.domid = DOMID_SELF,
  236. .dest.offset = xen_offset_in_page(key),
  237. .len = len,
  238. .flags = GNTCOPY_source_gref
  239. };
  240. if (len > XEN_NETBK_MAX_HASH_KEY_SIZE)
  241. return XEN_NETIF_CTRL_STATUS_INVALID_PARAMETER;
  242. if (copy_op.len != 0) {
  243. gnttab_batch_copy(&copy_op, 1);
  244. if (copy_op.status != GNTST_okay)
  245. return XEN_NETIF_CTRL_STATUS_INVALID_PARAMETER;
  246. }
  247. /* Clear any remaining key octets */
  248. if (len < XEN_NETBK_MAX_HASH_KEY_SIZE)
  249. memset(key + len, 0, XEN_NETBK_MAX_HASH_KEY_SIZE - len);
  250. xenvif_flush_hash(vif);
  251. return XEN_NETIF_CTRL_STATUS_SUCCESS;
  252. }
  253. u32 xenvif_set_hash_mapping_size(struct xenvif *vif, u32 size)
  254. {
  255. if (size > XEN_NETBK_MAX_HASH_MAPPING_SIZE)
  256. return XEN_NETIF_CTRL_STATUS_INVALID_PARAMETER;
  257. vif->hash.size = size;
  258. memset(vif->hash.mapping[vif->hash.mapping_sel], 0,
  259. sizeof(u32) * size);
  260. return XEN_NETIF_CTRL_STATUS_SUCCESS;
  261. }
  262. u32 xenvif_set_hash_mapping(struct xenvif *vif, u32 gref, u32 len,
  263. u32 off)
  264. {
  265. u32 *mapping = vif->hash.mapping[!vif->hash.mapping_sel];
  266. unsigned int nr = 1;
  267. struct gnttab_copy copy_op[2] = {{
  268. .source.u.ref = gref,
  269. .source.domid = vif->domid,
  270. .dest.domid = DOMID_SELF,
  271. .len = len * sizeof(*mapping),
  272. .flags = GNTCOPY_source_gref
  273. }};
  274. if ((off + len < off) || (off + len > vif->hash.size) ||
  275. len > XEN_PAGE_SIZE / sizeof(*mapping))
  276. return XEN_NETIF_CTRL_STATUS_INVALID_PARAMETER;
  277. copy_op[0].dest.u.gmfn = virt_to_gfn(mapping + off);
  278. copy_op[0].dest.offset = xen_offset_in_page(mapping + off);
  279. if (copy_op[0].dest.offset + copy_op[0].len > XEN_PAGE_SIZE) {
  280. copy_op[1] = copy_op[0];
  281. copy_op[1].source.offset = XEN_PAGE_SIZE - copy_op[0].dest.offset;
  282. copy_op[1].dest.u.gmfn = virt_to_gfn(mapping + off + len);
  283. copy_op[1].dest.offset = 0;
  284. copy_op[1].len = copy_op[0].len - copy_op[1].source.offset;
  285. copy_op[0].len = copy_op[1].source.offset;
  286. nr = 2;
  287. }
  288. memcpy(mapping, vif->hash.mapping[vif->hash.mapping_sel],
  289. vif->hash.size * sizeof(*mapping));
  290. if (copy_op[0].len != 0) {
  291. gnttab_batch_copy(copy_op, nr);
  292. if (copy_op[0].status != GNTST_okay ||
  293. copy_op[nr - 1].status != GNTST_okay)
  294. return XEN_NETIF_CTRL_STATUS_INVALID_PARAMETER;
  295. }
  296. while (len-- != 0)
  297. if (mapping[off++] >= vif->num_queues)
  298. return XEN_NETIF_CTRL_STATUS_INVALID_PARAMETER;
  299. vif->hash.mapping_sel = !vif->hash.mapping_sel;
  300. return XEN_NETIF_CTRL_STATUS_SUCCESS;
  301. }
  302. #ifdef CONFIG_DEBUG_FS
  303. void xenvif_dump_hash_info(struct xenvif *vif, struct seq_file *m)
  304. {
  305. unsigned int i;
  306. switch (vif->hash.alg) {
  307. case XEN_NETIF_CTRL_HASH_ALGORITHM_TOEPLITZ:
  308. seq_puts(m, "Hash Algorithm: TOEPLITZ\n");
  309. break;
  310. case XEN_NETIF_CTRL_HASH_ALGORITHM_NONE:
  311. seq_puts(m, "Hash Algorithm: NONE\n");
  312. fallthrough;
  313. default:
  314. return;
  315. }
  316. if (vif->hash.flags) {
  317. seq_puts(m, "\nHash Flags:\n");
  318. if (vif->hash.flags & XEN_NETIF_CTRL_HASH_TYPE_IPV4)
  319. seq_puts(m, "- IPv4\n");
  320. if (vif->hash.flags & XEN_NETIF_CTRL_HASH_TYPE_IPV4_TCP)
  321. seq_puts(m, "- IPv4 + TCP\n");
  322. if (vif->hash.flags & XEN_NETIF_CTRL_HASH_TYPE_IPV6)
  323. seq_puts(m, "- IPv6\n");
  324. if (vif->hash.flags & XEN_NETIF_CTRL_HASH_TYPE_IPV6_TCP)
  325. seq_puts(m, "- IPv6 + TCP\n");
  326. }
  327. seq_puts(m, "\nHash Key:\n");
  328. for (i = 0; i < XEN_NETBK_MAX_HASH_KEY_SIZE; ) {
  329. unsigned int j, n;
  330. n = 8;
  331. if (i + n >= XEN_NETBK_MAX_HASH_KEY_SIZE)
  332. n = XEN_NETBK_MAX_HASH_KEY_SIZE - i;
  333. seq_printf(m, "[%2u - %2u]: ", i, i + n - 1);
  334. for (j = 0; j < n; j++, i++)
  335. seq_printf(m, "%02x ", vif->hash.key[i]);
  336. seq_puts(m, "\n");
  337. }
  338. if (vif->hash.size != 0) {
  339. const u32 *mapping = vif->hash.mapping[vif->hash.mapping_sel];
  340. seq_puts(m, "\nHash Mapping:\n");
  341. for (i = 0; i < vif->hash.size; ) {
  342. unsigned int j, n;
  343. n = 8;
  344. if (i + n >= vif->hash.size)
  345. n = vif->hash.size - i;
  346. seq_printf(m, "[%4u - %4u]: ", i, i + n - 1);
  347. for (j = 0; j < n; j++, i++)
  348. seq_printf(m, "%4u ", mapping[i]);
  349. seq_puts(m, "\n");
  350. }
  351. }
  352. }
  353. #endif /* CONFIG_DEBUG_FS */
  354. void xenvif_init_hash(struct xenvif *vif)
  355. {
  356. if (xenvif_hash_cache_size == 0)
  357. return;
  358. BUG_ON(vif->hash.cache.count);
  359. spin_lock_init(&vif->hash.cache.lock);
  360. INIT_LIST_HEAD(&vif->hash.cache.list);
  361. }
  362. void xenvif_deinit_hash(struct xenvif *vif)
  363. {
  364. xenvif_flush_hash(vif);
  365. }