1/*- 2 * SPDX-License-Identifier: BSD-2-Clause-FreeBSD 3 * 4 * Copyright (c) 2010-2011 Juniper Networks, Inc. 5 * All rights reserved. 6 * 7 * This software was developed by Robert N. M. Watson under contract 8 * to Juniper Networks, Inc. 9 * 10 * Redistribution and use in source and binary forms, with or without 11 * modification, are permitted provided that the following conditions 12 * are met: 13 * 1. Redistributions of source code must retain the above copyright 14 * notice, this list of conditions and the following disclaimer. 15 * 2. Redistributions in binary form must reproduce the above copyright 16 * notice, this list of conditions and the following disclaimer in the 17 * documentation and/or other materials provided with the distribution. 18 * 19 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND 20 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 21 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 22 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE 23 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 24 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 25 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 26 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 27 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 28 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 29 * SUCH DAMAGE. 30 */ 31 32#include <sys/cdefs.h> 33 34__FBSDID("$FreeBSD$"); 35 36#include "opt_inet6.h" 37#include "opt_rss.h" 38 39#include <sys/param.h> 40#include <sys/mbuf.h> 41#include <sys/socket.h> 42 43#include <net/rss_config.h> 44 45#include <netinet/in.h> 46#include <netinet/in_pcb.h> 47#ifdef INET6 48#include <netinet6/in6_pcb.h> 49#include <netinet6/in6_rss.h> 50#endif /* INET6 */ 51 52/* 53 * Given a hash of whatever the covered tuple might be, return a pcbgroup 54 * index. Where RSS is supported, try to align bucket selection with RSS CPU 55 * affinity strategy. 56 */ 57static __inline u_int 58in6_pcbgroup_getbucket(struct inpcbinfo *pcbinfo, uint32_t hash) 59{ 60 61#ifdef RSS 62 return (rss_getbucket(hash)); 63#else 64 return (hash % pcbinfo->ipi_npcbgroups); 65#endif 66} 67 68/* 69 * Map a (hashtype, hash) tuple into a connection group, or NULL if the hash 70 * information is insufficient to identify the pcbgroup. This might occur if 71 * a TCP packet turnsup with a 2-tuple hash, or if an RSS hash is present but 72 * RSS is not compiled into the kernel. 73 */ 74struct inpcbgroup * 75in6_pcbgroup_byhash(struct inpcbinfo *pcbinfo, u_int hashtype, uint32_t hash) 76{ 77 78#ifdef RSS 79 if ((pcbinfo->ipi_hashfields == IPI_HASHFIELDS_4TUPLE && 80 hashtype == M_HASHTYPE_RSS_TCP_IPV6) || 81 (pcbinfo->ipi_hashfields == IPI_HASHFIELDS_4TUPLE && 82 hashtype == M_HASHTYPE_RSS_UDP_IPV6) || 83 (pcbinfo->ipi_hashfields == IPI_HASHFIELDS_2TUPLE && 84 hashtype == M_HASHTYPE_RSS_IPV6)) 85 return (&pcbinfo->ipi_pcbgroups[ 86 in6_pcbgroup_getbucket(pcbinfo, hash)]); 87#endif 88 return (NULL); 89} 90 91struct inpcbgroup * 92in6_pcbgroup_bymbuf(struct inpcbinfo *pcbinfo, struct mbuf *m) 93{ 94 95 return (in6_pcbgroup_byhash(pcbinfo, M_HASHTYPE_GET(m), 96 m->m_pkthdr.flowid)); 97} 98 99struct inpcbgroup * 100in6_pcbgroup_bytuple(struct inpcbinfo *pcbinfo, const struct in6_addr *laddrp, 101 u_short lport, const struct in6_addr *faddrp, u_short fport) 102{ 103 uint32_t hash; 104 105 /* 106 * RSS note: we pass foreign addr/port as source, and local addr/port 107 * as destination, as we want to align with what the hardware is 108 * doing. 109 */ 110 switch (pcbinfo->ipi_hashfields) { 111 case IPI_HASHFIELDS_4TUPLE: 112#ifdef RSS 113 hash = rss_hash_ip6_4tuple(faddrp, fport, laddrp, lport); 114#else 115 hash = faddrp->s6_addr32[3] ^ fport; 116#endif 117 break; 118 119 case IPI_HASHFIELDS_2TUPLE: 120#ifdef RSS 121 hash = rss_hash_ip6_2tuple(faddrp, laddrp); 122#else 123 hash = faddrp->s6_addr32[3] ^ laddrp->s6_addr32[3]; 124#endif 125 break; 126 127 default: 128 hash = 0; 129 } 130 return (&pcbinfo->ipi_pcbgroups[in6_pcbgroup_getbucket(pcbinfo, 131 hash)]); 132} 133 134struct inpcbgroup * 135in6_pcbgroup_byinpcb(struct inpcb *inp) 136{ 137 138#ifdef RSS 139 /* 140 * Listen sockets with INP_RSS_BUCKET_SET set have a pre-determined 141 * RSS bucket and thus we should use this pcbgroup, rather than 142 * using a tuple or hash. 143 * 144 * XXX should verify that there's actually pcbgroups and inp_rss_listen_bucket 145 * fits in that! 146 */ 147 if (inp->inp_flags2 & INP_RSS_BUCKET_SET) 148 return (&inp->inp_pcbinfo->ipi_pcbgroups[inp->inp_rss_listen_bucket]); 149#endif 150 151 return (in6_pcbgroup_bytuple(inp->inp_pcbinfo, &inp->in6p_laddr, 152 inp->inp_lport, &inp->in6p_faddr, inp->inp_fport)); 153} 154