hash.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468
  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;
  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_rcu(entry, &vif->hash.cache.list, link,
  88. lockdep_is_held(&vif->hash.cache.lock)) {
  89. list_del_rcu(&entry->link);
  90. vif->hash.cache.count--;
  91. kfree_rcu(entry, rcu);
  92. }
  93. spin_unlock_irqrestore(&vif->hash.cache.lock, flags);
  94. }
  95. static u32 xenvif_find_hash(struct xenvif *vif, const u8 *data,
  96. unsigned int len)
  97. {
  98. struct xenvif_hash_cache_entry *entry;
  99. u32 val;
  100. bool found;
  101. if (len >= XEN_NETBK_HASH_TAG_SIZE)
  102. return 0;
  103. if (xenvif_hash_cache_size == 0)
  104. return xenvif_new_hash(vif, data, len);
  105. rcu_read_lock();
  106. found = false;
  107. list_for_each_entry_rcu(entry, &vif->hash.cache.list, link) {
  108. if (entry->len == len &&
  109. memcmp(entry->tag, data, len) == 0) {
  110. val = entry->val;
  111. entry->seq = atomic_inc_return(&vif->hash.cache.seq);
  112. found = true;
  113. break;
  114. }
  115. }
  116. rcu_read_unlock();
  117. if (!found)
  118. val = xenvif_new_hash(vif, data, len);
  119. return val;
  120. }
  121. void xenvif_set_skb_hash(struct xenvif *vif, struct sk_buff *skb)
  122. {
  123. struct flow_keys flow;
  124. u32 hash = 0;
  125. enum pkt_hash_types type = PKT_HASH_TYPE_NONE;
  126. u32 flags = vif->hash.flags;
  127. bool has_tcp_hdr;
  128. /* Quick rejection test: If the network protocol doesn't
  129. * correspond to any enabled hash type then there's no point
  130. * in parsing the packet header.
  131. */
  132. switch (skb->protocol) {
  133. case htons(ETH_P_IP):
  134. if (flags & (XEN_NETIF_CTRL_HASH_TYPE_IPV4_TCP |
  135. XEN_NETIF_CTRL_HASH_TYPE_IPV4))
  136. break;
  137. goto done;
  138. case htons(ETH_P_IPV6):
  139. if (flags & (XEN_NETIF_CTRL_HASH_TYPE_IPV6_TCP |
  140. XEN_NETIF_CTRL_HASH_TYPE_IPV6))
  141. break;
  142. goto done;
  143. default:
  144. goto done;
  145. }
  146. memset(&flow, 0, sizeof(flow));
  147. if (!skb_flow_dissect_flow_keys(skb, &flow, 0))
  148. goto done;
  149. has_tcp_hdr = (flow.basic.ip_proto == IPPROTO_TCP) &&
  150. !(flow.control.flags & FLOW_DIS_IS_FRAGMENT);
  151. switch (skb->protocol) {
  152. case htons(ETH_P_IP):
  153. if (has_tcp_hdr &&
  154. (flags & XEN_NETIF_CTRL_HASH_TYPE_IPV4_TCP)) {
  155. u8 data[12];
  156. memcpy(&data[0], &flow.addrs.v4addrs.src, 4);
  157. memcpy(&data[4], &flow.addrs.v4addrs.dst, 4);
  158. memcpy(&data[8], &flow.ports.src, 2);
  159. memcpy(&data[10], &flow.ports.dst, 2);
  160. hash = xenvif_find_hash(vif, data, sizeof(data));
  161. type = PKT_HASH_TYPE_L4;
  162. } else if (flags & XEN_NETIF_CTRL_HASH_TYPE_IPV4) {
  163. u8 data[8];
  164. memcpy(&data[0], &flow.addrs.v4addrs.src, 4);
  165. memcpy(&data[4], &flow.addrs.v4addrs.dst, 4);
  166. hash = xenvif_find_hash(vif, data, sizeof(data));
  167. type = PKT_HASH_TYPE_L3;
  168. }
  169. break;
  170. case htons(ETH_P_IPV6):
  171. if (has_tcp_hdr &&
  172. (flags & XEN_NETIF_CTRL_HASH_TYPE_IPV6_TCP)) {
  173. u8 data[36];
  174. memcpy(&data[0], &flow.addrs.v6addrs.src, 16);
  175. memcpy(&data[16], &flow.addrs.v6addrs.dst, 16);
  176. memcpy(&data[32], &flow.ports.src, 2);
  177. memcpy(&data[34], &flow.ports.dst, 2);
  178. hash = xenvif_find_hash(vif, data, sizeof(data));
  179. type = PKT_HASH_TYPE_L4;
  180. } else if (flags & XEN_NETIF_CTRL_HASH_TYPE_IPV6) {
  181. u8 data[32];
  182. memcpy(&data[0], &flow.addrs.v6addrs.src, 16);
  183. memcpy(&data[16], &flow.addrs.v6addrs.dst, 16);
  184. hash = xenvif_find_hash(vif, data, sizeof(data));
  185. type = PKT_HASH_TYPE_L3;
  186. }
  187. break;
  188. }
  189. done:
  190. if (type == PKT_HASH_TYPE_NONE)
  191. skb_clear_hash(skb);
  192. else
  193. __skb_set_sw_hash(skb, hash, type == PKT_HASH_TYPE_L4);
  194. }
  195. u32 xenvif_set_hash_alg(struct xenvif *vif, u32 alg)
  196. {
  197. switch (alg) {
  198. case XEN_NETIF_CTRL_HASH_ALGORITHM_NONE:
  199. case XEN_NETIF_CTRL_HASH_ALGORITHM_TOEPLITZ:
  200. break;
  201. default:
  202. return XEN_NETIF_CTRL_STATUS_INVALID_PARAMETER;
  203. }
  204. vif->hash.alg = alg;
  205. return XEN_NETIF_CTRL_STATUS_SUCCESS;
  206. }
  207. u32 xenvif_get_hash_flags(struct xenvif *vif, u32 *flags)
  208. {
  209. if (vif->hash.alg == XEN_NETIF_CTRL_HASH_ALGORITHM_NONE)
  210. return XEN_NETIF_CTRL_STATUS_NOT_SUPPORTED;
  211. *flags = XEN_NETIF_CTRL_HASH_TYPE_IPV4 |
  212. XEN_NETIF_CTRL_HASH_TYPE_IPV4_TCP |
  213. XEN_NETIF_CTRL_HASH_TYPE_IPV6 |
  214. XEN_NETIF_CTRL_HASH_TYPE_IPV6_TCP;
  215. return XEN_NETIF_CTRL_STATUS_SUCCESS;
  216. }
  217. u32 xenvif_set_hash_flags(struct xenvif *vif, u32 flags)
  218. {
  219. if (flags & ~(XEN_NETIF_CTRL_HASH_TYPE_IPV4 |
  220. XEN_NETIF_CTRL_HASH_TYPE_IPV4_TCP |
  221. XEN_NETIF_CTRL_HASH_TYPE_IPV6 |
  222. XEN_NETIF_CTRL_HASH_TYPE_IPV6_TCP))
  223. return XEN_NETIF_CTRL_STATUS_INVALID_PARAMETER;
  224. if (vif->hash.alg == XEN_NETIF_CTRL_HASH_ALGORITHM_NONE)
  225. return XEN_NETIF_CTRL_STATUS_INVALID_PARAMETER;
  226. vif->hash.flags = flags;
  227. return XEN_NETIF_CTRL_STATUS_SUCCESS;
  228. }
  229. u32 xenvif_set_hash_key(struct xenvif *vif, u32 gref, u32 len)
  230. {
  231. u8 *key = vif->hash.key;
  232. struct gnttab_copy copy_op = {
  233. .source.u.ref = gref,
  234. .source.domid = vif->domid,
  235. .dest.u.gmfn = virt_to_gfn(key),
  236. .dest.domid = DOMID_SELF,
  237. .dest.offset = xen_offset_in_page(key),
  238. .len = len,
  239. .flags = GNTCOPY_source_gref
  240. };
  241. if (len > XEN_NETBK_MAX_HASH_KEY_SIZE)
  242. return XEN_NETIF_CTRL_STATUS_INVALID_PARAMETER;
  243. if (copy_op.len != 0) {
  244. gnttab_batch_copy(&copy_op, 1);
  245. if (copy_op.status != GNTST_okay)
  246. return XEN_NETIF_CTRL_STATUS_INVALID_PARAMETER;
  247. }
  248. /* Clear any remaining key octets */
  249. if (len < XEN_NETBK_MAX_HASH_KEY_SIZE)
  250. memset(key + len, 0, XEN_NETBK_MAX_HASH_KEY_SIZE - len);
  251. xenvif_flush_hash(vif);
  252. return XEN_NETIF_CTRL_STATUS_SUCCESS;
  253. }
  254. u32 xenvif_set_hash_mapping_size(struct xenvif *vif, u32 size)
  255. {
  256. if (size > XEN_NETBK_MAX_HASH_MAPPING_SIZE)
  257. return XEN_NETIF_CTRL_STATUS_INVALID_PARAMETER;
  258. vif->hash.size = size;
  259. memset(vif->hash.mapping[vif->hash.mapping_sel], 0,
  260. sizeof(u32) * size);
  261. return XEN_NETIF_CTRL_STATUS_SUCCESS;
  262. }
  263. u32 xenvif_set_hash_mapping(struct xenvif *vif, u32 gref, u32 len,
  264. u32 off)
  265. {
  266. u32 *mapping = vif->hash.mapping[!vif->hash.mapping_sel];
  267. unsigned int nr = 1;
  268. struct gnttab_copy copy_op[2] = {{
  269. .source.u.ref = gref,
  270. .source.domid = vif->domid,
  271. .dest.domid = DOMID_SELF,
  272. .len = len * sizeof(*mapping),
  273. .flags = GNTCOPY_source_gref
  274. }};
  275. if ((off + len < off) || (off + len > vif->hash.size) ||
  276. len > XEN_PAGE_SIZE / sizeof(*mapping))
  277. return XEN_NETIF_CTRL_STATUS_INVALID_PARAMETER;
  278. copy_op[0].dest.u.gmfn = virt_to_gfn(mapping + off);
  279. copy_op[0].dest.offset = xen_offset_in_page(mapping + off);
  280. if (copy_op[0].dest.offset + copy_op[0].len > XEN_PAGE_SIZE) {
  281. copy_op[1] = copy_op[0];
  282. copy_op[1].source.offset = XEN_PAGE_SIZE - copy_op[0].dest.offset;
  283. copy_op[1].dest.u.gmfn = virt_to_gfn(mapping + off + len);
  284. copy_op[1].dest.offset = 0;
  285. copy_op[1].len = copy_op[0].len - copy_op[1].source.offset;
  286. copy_op[0].len = copy_op[1].source.offset;
  287. nr = 2;
  288. }
  289. memcpy(mapping, vif->hash.mapping[vif->hash.mapping_sel],
  290. vif->hash.size * sizeof(*mapping));
  291. if (copy_op[0].len != 0) {
  292. gnttab_batch_copy(copy_op, nr);
  293. if (copy_op[0].status != GNTST_okay ||
  294. copy_op[nr - 1].status != GNTST_okay)
  295. return XEN_NETIF_CTRL_STATUS_INVALID_PARAMETER;
  296. }
  297. while (len-- != 0)
  298. if (mapping[off++] >= vif->num_queues)
  299. return XEN_NETIF_CTRL_STATUS_INVALID_PARAMETER;
  300. vif->hash.mapping_sel = !vif->hash.mapping_sel;
  301. return XEN_NETIF_CTRL_STATUS_SUCCESS;
  302. }
  303. #ifdef CONFIG_DEBUG_FS
  304. void xenvif_dump_hash_info(struct xenvif *vif, struct seq_file *m)
  305. {
  306. unsigned int i;
  307. switch (vif->hash.alg) {
  308. case XEN_NETIF_CTRL_HASH_ALGORITHM_TOEPLITZ:
  309. seq_puts(m, "Hash Algorithm: TOEPLITZ\n");
  310. break;
  311. case XEN_NETIF_CTRL_HASH_ALGORITHM_NONE:
  312. seq_puts(m, "Hash Algorithm: NONE\n");
  313. fallthrough;
  314. default:
  315. return;
  316. }
  317. if (vif->hash.flags) {
  318. seq_puts(m, "\nHash Flags:\n");
  319. if (vif->hash.flags & XEN_NETIF_CTRL_HASH_TYPE_IPV4)
  320. seq_puts(m, "- IPv4\n");
  321. if (vif->hash.flags & XEN_NETIF_CTRL_HASH_TYPE_IPV4_TCP)
  322. seq_puts(m, "- IPv4 + TCP\n");
  323. if (vif->hash.flags & XEN_NETIF_CTRL_HASH_TYPE_IPV6)
  324. seq_puts(m, "- IPv6\n");
  325. if (vif->hash.flags & XEN_NETIF_CTRL_HASH_TYPE_IPV6_TCP)
  326. seq_puts(m, "- IPv6 + TCP\n");
  327. }
  328. seq_puts(m, "\nHash Key:\n");
  329. for (i = 0; i < XEN_NETBK_MAX_HASH_KEY_SIZE; ) {
  330. unsigned int j, n;
  331. n = 8;
  332. if (i + n >= XEN_NETBK_MAX_HASH_KEY_SIZE)
  333. n = XEN_NETBK_MAX_HASH_KEY_SIZE - i;
  334. seq_printf(m, "[%2u - %2u]: ", i, i + n - 1);
  335. for (j = 0; j < n; j++, i++)
  336. seq_printf(m, "%02x ", vif->hash.key[i]);
  337. seq_puts(m, "\n");
  338. }
  339. if (vif->hash.size != 0) {
  340. const u32 *mapping = vif->hash.mapping[vif->hash.mapping_sel];
  341. seq_puts(m, "\nHash Mapping:\n");
  342. for (i = 0; i < vif->hash.size; ) {
  343. unsigned int j, n;
  344. n = 8;
  345. if (i + n >= vif->hash.size)
  346. n = vif->hash.size - i;
  347. seq_printf(m, "[%4u - %4u]: ", i, i + n - 1);
  348. for (j = 0; j < n; j++, i++)
  349. seq_printf(m, "%4u ", mapping[i]);
  350. seq_puts(m, "\n");
  351. }
  352. }
  353. }
  354. #endif /* CONFIG_DEBUG_FS */
  355. void xenvif_init_hash(struct xenvif *vif)
  356. {
  357. if (xenvif_hash_cache_size == 0)
  358. return;
  359. BUG_ON(vif->hash.cache.count);
  360. spin_lock_init(&vif->hash.cache.lock);
  361. INIT_LIST_HEAD(&vif->hash.cache.list);
  362. }
  363. void xenvif_deinit_hash(struct xenvif *vif)
  364. {
  365. xenvif_flush_hash(vif);
  366. }