/*-1* Copyright (c) 2010-2011 Juniper Networks, Inc.2* All rights reserved.3*4* This software was developed by Robert N. M. Watson under contract5* to Juniper Networks, Inc.6*7* Redistribution and use in source and binary forms, with or without8* modification, are permitted provided that the following conditions9* are met:10* 1. Redistributions of source code must retain the above copyright11* notice, this list of conditions and the following disclaimer.12* 2. Redistributions in binary form must reproduce the above copyright13* notice, this list of conditions and the following disclaimer in the14* documentation and/or other materials provided with the distribution.15*16* THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND17* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE18* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE19* ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE20* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL21* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS22* OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)23* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT24* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY25* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF26* SUCH DAMAGE.27*/282930#include "opt_inet6.h"3132#include <sys/param.h>33#include <sys/mbuf.h>34#include <sys/socket.h>35#include <sys/priv.h>36#include <sys/kernel.h>37#include <sys/smp.h>38#include <sys/sysctl.h>39#include <sys/sbuf.h>4041#include <net/if.h>42#include <net/if_var.h>43#include <net/netisr.h>44#include <net/rss_config.h>4546#include <netinet/in.h>47#include <netinet/in_pcb.h>48#include <netinet6/in6_rss.h>49#include <netinet/in_var.h>5051/* for software rss hash support */52#include <netinet/ip6.h>53#include <netinet6/ip6_var.h>54#include <netinet/tcp.h>55#include <netinet/udp.h>5657/*58* Hash an IPv6 2-tuple.59*/60uint32_t61rss_hash_ip6_2tuple(const struct in6_addr *src, const struct in6_addr *dst)62{63uint8_t data[sizeof(*src) + sizeof(*dst)];64u_int datalen;6566datalen = 0;67bcopy(src, &data[datalen], sizeof(*src));68datalen += sizeof(*src);69bcopy(dst, &data[datalen], sizeof(*dst));70datalen += sizeof(*dst);71return (rss_hash(datalen, data));72}7374/*75* Hash an IPv6 4-tuple.76*/77uint32_t78rss_hash_ip6_4tuple(const struct in6_addr *src, u_short srcport,79const struct in6_addr *dst, u_short dstport)80{81uint8_t data[sizeof(*src) + sizeof(*dst) + sizeof(srcport) +82sizeof(dstport)];83u_int datalen;8485datalen = 0;86bcopy(src, &data[datalen], sizeof(*src));87datalen += sizeof(*src);88bcopy(dst, &data[datalen], sizeof(*dst));89datalen += sizeof(*dst);90bcopy(&srcport, &data[datalen], sizeof(srcport));91datalen += sizeof(srcport);92bcopy(&dstport, &data[datalen], sizeof(dstport));93datalen += sizeof(dstport);94return (rss_hash(datalen, data));95}9697/*98* Calculate an appropriate ipv6 2-tuple or 4-tuple given the given99* IPv6 source/destination address, UDP or TCP source/destination ports100* and the protocol type.101*102* The protocol code may wish to do a software hash of the given103* tuple. This depends upon the currently configured RSS hash types.104*105* This assumes that the packet in question isn't a fragment.106*107* It also assumes the packet source/destination address108* are in "incoming" packet order (ie, source is "far" address.)109*/110int111rss_proto_software_hash_v6(const struct in6_addr *s, const struct in6_addr *d,112u_short sp, u_short dp, int proto,113uint32_t *hashval, uint32_t *hashtype)114{115uint32_t hash;116117/*118* Next, choose the hash type depending upon the protocol119* identifier.120*/121if ((proto == IPPROTO_TCP) &&122(rss_gethashconfig() & RSS_HASHTYPE_RSS_TCP_IPV6)) {123hash = rss_hash_ip6_4tuple(s, sp, d, dp);124*hashval = hash;125*hashtype = M_HASHTYPE_RSS_TCP_IPV6;126return (0);127} else if ((proto == IPPROTO_UDP) &&128(rss_gethashconfig() & RSS_HASHTYPE_RSS_UDP_IPV6)) {129hash = rss_hash_ip6_4tuple(s, sp, d, dp);130*hashval = hash;131*hashtype = M_HASHTYPE_RSS_UDP_IPV6;132return (0);133} else if (rss_gethashconfig() & RSS_HASHTYPE_RSS_IPV6) {134/* RSS doesn't hash on other protocols like SCTP; so 2-tuple */135hash = rss_hash_ip6_2tuple(s, d);136*hashval = hash;137*hashtype = M_HASHTYPE_RSS_IPV6;138return (0);139}140141/* No configured available hashtypes! */142RSS_DEBUG("no available hashtypes!\n");143return (-1);144}145146/*147* Calculate an appropriate ipv6 2-tuple or 4-tuple given the given148* IPv6 source/destination address, UDP or TCP source/destination ports149* and the protocol type.150*151* The protocol code may wish to do a software hash of the given152* tuple. This depends upon the currently configured RSS hash types.153*154* It assumes the packet source/destination address155* are in "outgoin" packet order (ie, destination is "far" address.)156*/157uint32_t158xps_proto_software_hash_v6(const struct in6_addr *s, const struct in6_addr *d,159u_short sp, u_short dp, int proto, uint32_t *hashtype)160{161162uint32_t hash;163164/*165* Next, choose the hash type depending upon the protocol166* identifier.167*/168if ((proto == IPPROTO_TCP) &&169(rss_gethashconfig() & RSS_HASHTYPE_RSS_TCP_IPV6)) {170hash = rss_hash_ip6_4tuple(d, dp, s, sp);171*hashtype = M_HASHTYPE_RSS_TCP_IPV6;172return (hash);173} else if ((proto == IPPROTO_UDP) &&174(rss_gethashconfig() & RSS_HASHTYPE_RSS_UDP_IPV6)) {175hash = rss_hash_ip6_4tuple(d, dp, s, sp);176*hashtype = M_HASHTYPE_RSS_UDP_IPV6;177return (hash);178} else if (rss_gethashconfig() & RSS_HASHTYPE_RSS_IPV6) {179/* RSS doesn't hash on other protocols like SCTP; so 2-tuple */180hash = rss_hash_ip6_2tuple(d, s);181*hashtype = M_HASHTYPE_RSS_IPV6;182return (hash);183}184185*hashtype = M_HASHTYPE_NONE;186return (0);187}188189190/*191* Do a software calculation of the RSS for the given mbuf.192*193* This is typically used by the input path to recalculate the RSS after194* some form of packet processing (eg de-capsulation, IP fragment reassembly.)195*196* dir is the packet direction - RSS_HASH_PKT_INGRESS for incoming and197* RSS_HASH_PKT_EGRESS for outgoing.198*199* Returns 0 if a hash was done, -1 if no hash was done, +1 if200* the mbuf already had a valid RSS flowid.201*202* This function doesn't modify the mbuf. It's up to the caller to203* assign flowid/flowtype as appropriate.204*/205int206rss_mbuf_software_hash_v6(const struct mbuf *m, int dir, uint32_t *hashval,207uint32_t *hashtype)208{209const struct ip6_hdr *ip6;210const struct ip6_frag *ip6f;211const struct tcphdr *th;212const struct udphdr *uh;213uint32_t flowtype;214uint8_t proto;215int off, newoff;216int nxt;217218/*219* XXX For now this only handles hashing on incoming mbufs.220*/221if (dir != RSS_HASH_PKT_INGRESS) {222RSS_DEBUG("called on EGRESS packet!\n");223return (-1);224}225226off = sizeof(struct ip6_hdr);227228/*229* First, validate that the mbuf we have is long enough230* to have an IPv6 header in it.231*/232if (m->m_pkthdr.len < off) {233RSS_DEBUG("short mbuf pkthdr\n");234return (-1);235}236if (m->m_len < off) {237RSS_DEBUG("short mbuf len\n");238return (-1);239}240241/* Ok, let's dereference that */242ip6 = mtod(m, struct ip6_hdr *);243proto = ip6->ip6_nxt;244245/*246* Find the beginning of the TCP/UDP header.247*248* If this is a fragment then it shouldn't be four-tuple249* hashed just yet. Once it's reassembled into a full250* frame it should be re-hashed.251*/252while (proto != IPPROTO_FRAGMENT) {253newoff = ip6_nexthdr(m, off, proto, &nxt);254if (newoff < 0)255break;256off = newoff;257proto = nxt;258}259260/*261* Ignore the fragment header if this is an "atomic" fragment262* (offset and m bit set to 0)263*/264if (proto == IPPROTO_FRAGMENT) {265if (m->m_len < off + sizeof(struct ip6_frag)) {266RSS_DEBUG("short fragment frame?\n");267return (-1);268}269ip6f = (const struct ip6_frag *)((c_caddr_t)ip6 + off);270if ((ip6f->ip6f_offlg & ~IP6F_RESERVED_MASK) == 0) {271off = ip6_lasthdr(m, off, proto, &nxt);272if (off < 0) {273RSS_DEBUG("invalid extension header\n");274return (-1);275}276proto = nxt;277}278}279280/*281* If the mbuf flowid/flowtype matches the packet type,282* and we don't support the 4-tuple version of the given protocol,283* then signal to the owner that it can trust the flowid/flowtype284* details.285*286* This is a little picky - eg, if TCPv6 / UDPv6 hashing287* is supported but we got a TCP/UDP frame only 2-tuple hashed,288* then we shouldn't just "trust" the 2-tuple hash. We need289* a 4-tuple hash.290*/291flowtype = M_HASHTYPE_GET(m);292293if (flowtype != M_HASHTYPE_NONE) {294switch (proto) {295case IPPROTO_UDP:296if ((rss_gethashconfig() & RSS_HASHTYPE_RSS_UDP_IPV6) &&297(flowtype == M_HASHTYPE_RSS_UDP_IPV6)) {298return (1);299}300/*301* Only allow 2-tuple for UDP frames if we don't also302* support 4-tuple for UDP.303*/304if ((rss_gethashconfig() & RSS_HASHTYPE_RSS_IPV6) &&305((rss_gethashconfig() & RSS_HASHTYPE_RSS_UDP_IPV6) == 0) &&306flowtype == M_HASHTYPE_RSS_IPV6) {307return (1);308}309break;310case IPPROTO_TCP:311if ((rss_gethashconfig() & RSS_HASHTYPE_RSS_TCP_IPV6) &&312(flowtype == M_HASHTYPE_RSS_TCP_IPV6)) {313return (1);314}315/*316* Only allow 2-tuple for TCP frames if we don't also317* support 4-tuple for TCP.318*/319if ((rss_gethashconfig() & RSS_HASHTYPE_RSS_IPV6) &&320((rss_gethashconfig() & RSS_HASHTYPE_RSS_TCP_IPV6) == 0) &&321flowtype == M_HASHTYPE_RSS_IPV6) {322return (1);323}324break;325default:326if ((rss_gethashconfig() & RSS_HASHTYPE_RSS_IPV6) &&327flowtype == M_HASHTYPE_RSS_IPV6) {328return (1);329}330break;331}332}333334/*335* Decode enough information to make a hash decision.336*/337if ((rss_gethashconfig() & RSS_HASHTYPE_RSS_TCP_IPV6) &&338(proto == IPPROTO_TCP)) {339if (m->m_len < off + sizeof(struct tcphdr)) {340RSS_DEBUG("short TCP frame?\n");341return (-1);342}343th = (const struct tcphdr *)((c_caddr_t)ip6 + off);344return rss_proto_software_hash_v6(&ip6->ip6_src, &ip6->ip6_dst,345th->th_sport,346th->th_dport,347proto,348hashval,349hashtype);350} else if ((rss_gethashconfig() & RSS_HASHTYPE_RSS_UDP_IPV6) &&351(proto == IPPROTO_UDP)) {352if (m->m_len < off + sizeof(struct udphdr)) {353RSS_DEBUG("short UDP frame?\n");354return (-1);355}356uh = (const struct udphdr *)((c_caddr_t)ip6 + off);357return rss_proto_software_hash_v6(&ip6->ip6_src, &ip6->ip6_dst,358uh->uh_sport,359uh->uh_dport,360proto,361hashval,362hashtype);363} else if (rss_gethashconfig() & RSS_HASHTYPE_RSS_IPV6) {364/* Default to 2-tuple hash */365return rss_proto_software_hash_v6(&ip6->ip6_src, &ip6->ip6_dst,3660, /* source port */3670, /* destination port */3680, /* IPPROTO_IP */369hashval,370hashtype);371} else {372RSS_DEBUG("no available hashtypes!\n");373return (-1);374}375}376377/*378* Similar to rss_m2cpuid, but designed to be used by the IPv6 NETISR379* on incoming frames.380*381* If an existing RSS hash exists and it matches what the configured382* hashing is, then use it.383*384* If there's an existing RSS hash but the desired hash is different,385* or if there's no useful RSS hash, then calculate it via386* the software path.387*388* XXX TODO: definitely want statistics here!389*/390struct mbuf *391rss_soft_m2cpuid_v6(struct mbuf *m, uintptr_t source, u_int *cpuid)392{393uint32_t hash_val, hash_type;394int ret;395396M_ASSERTPKTHDR(m);397398ret = rss_mbuf_software_hash_v6(m, RSS_HASH_PKT_INGRESS,399&hash_val, &hash_type);400if (ret > 0) {401/* mbuf has a valid hash already; don't need to modify it */402*cpuid = rss_hash2cpuid(m->m_pkthdr.flowid, M_HASHTYPE_GET(m));403} else if (ret == 0) {404/* hash was done; update */405m->m_pkthdr.flowid = hash_val;406M_HASHTYPE_SET(m, hash_type);407*cpuid = rss_hash2cpuid(m->m_pkthdr.flowid, M_HASHTYPE_GET(m));408} else { /* ret < 0 */409/* no hash was done */410*cpuid = NETISR_CPUID_NONE;411}412return (m);413}414415416