/*-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 <netinet/in_rss.h>49#include <netinet/in_var.h>5051/* for software rss hash support */52#include <netinet/ip.h>53#include <netinet/tcp.h>54#include <netinet/udp.h>5556/*57* Hash an IPv4 2-tuple.58*/59uint32_t60rss_hash_ip4_2tuple(struct in_addr src, struct in_addr dst)61{62uint8_t data[sizeof(src) + sizeof(dst)];63u_int datalen;6465datalen = 0;66bcopy(&src, &data[datalen], sizeof(src));67datalen += sizeof(src);68bcopy(&dst, &data[datalen], sizeof(dst));69datalen += sizeof(dst);70return (rss_hash(datalen, data));71}7273/*74* Hash an IPv4 4-tuple.75*/76uint32_t77rss_hash_ip4_4tuple(struct in_addr src, u_short srcport, struct in_addr dst,78u_short dstport)79{80uint8_t data[sizeof(src) + sizeof(dst) + sizeof(srcport) +81sizeof(dstport)];82u_int datalen;8384datalen = 0;85bcopy(&src, &data[datalen], sizeof(src));86datalen += sizeof(src);87bcopy(&dst, &data[datalen], sizeof(dst));88datalen += sizeof(dst);89bcopy(&srcport, &data[datalen], sizeof(srcport));90datalen += sizeof(srcport);91bcopy(&dstport, &data[datalen], sizeof(dstport));92datalen += sizeof(dstport);93return (rss_hash(datalen, data));94}9596/*97* Calculate an appropriate ipv4 2-tuple or 4-tuple given the given98* IPv4 source/destination address, UDP or TCP source/destination ports99* and the protocol type.100*101* The protocol code may wish to do a software hash of the given102* tuple. This depends upon the currently configured RSS hash types.103*104* This assumes that the packet in question isn't a fragment.105*106* It also assumes the packet source/destination address107* are in "incoming" packet order (ie, source is "far" address.)108*/109int110rss_proto_software_hash_v4(struct in_addr s, struct in_addr d,111u_short sp, u_short dp, int proto,112uint32_t *hashval, uint32_t *hashtype)113{114uint32_t hash;115116/*117* Next, choose the hash type depending upon the protocol118* identifier.119*/120if ((proto == IPPROTO_TCP) &&121(rss_gethashconfig() & RSS_HASHTYPE_RSS_TCP_IPV4)) {122hash = rss_hash_ip4_4tuple(s, sp, d, dp);123*hashval = hash;124*hashtype = M_HASHTYPE_RSS_TCP_IPV4;125return (0);126} else if ((proto == IPPROTO_UDP) &&127(rss_gethashconfig() & RSS_HASHTYPE_RSS_UDP_IPV4)) {128hash = rss_hash_ip4_4tuple(s, sp, d, dp);129*hashval = hash;130*hashtype = M_HASHTYPE_RSS_UDP_IPV4;131return (0);132} else if (rss_gethashconfig() & RSS_HASHTYPE_RSS_IPV4) {133/* RSS doesn't hash on other protocols like SCTP; so 2-tuple */134hash = rss_hash_ip4_2tuple(s, d);135*hashval = hash;136*hashtype = M_HASHTYPE_RSS_IPV4;137return (0);138}139140/* No configured available hashtypes! */141RSS_DEBUG("no available hashtypes!\n");142return (-1);143}144145/*146* Calculate an appropriate ipv4 2-tuple or 4-tuple given the given147* IPv4 source/destination address, UDP or TCP source/destination ports148* and the protocol type.149*150* The protocol code may wish to do a software hash of the given151* tuple. This depends upon the currently configured RSS hash types.152*153* It assumes the packet source/destination address154* are in "outgoing" packet order (ie, destination is "far" address.)155*/156uint32_t157xps_proto_software_hash_v4(struct in_addr s, struct in_addr d,158u_short sp, u_short dp, int proto, uint32_t *hashtype)159{160uint32_t hash;161162/*163* Next, choose the hash type depending upon the protocol164* identifier.165*/166if ((proto == IPPROTO_TCP) &&167(rss_gethashconfig() & RSS_HASHTYPE_RSS_TCP_IPV4)) {168hash = rss_hash_ip4_4tuple(d, dp, s, sp);169*hashtype = M_HASHTYPE_RSS_TCP_IPV4;170return (hash);171} else if ((proto == IPPROTO_UDP) &&172(rss_gethashconfig() & RSS_HASHTYPE_RSS_UDP_IPV4)) {173hash = rss_hash_ip4_4tuple(d, dp, s, sp);174*hashtype = M_HASHTYPE_RSS_UDP_IPV4;175return (hash);176} else if (rss_gethashconfig() & RSS_HASHTYPE_RSS_IPV4) {177/* RSS doesn't hash on other protocols like SCTP; so 2-tuple */178hash = rss_hash_ip4_2tuple(d, s);179*hashtype = M_HASHTYPE_RSS_IPV4;180return (hash);181}182183*hashtype = M_HASHTYPE_NONE;184return (0);185}186187/*188* Do a software calculation of the RSS for the given mbuf.189*190* This is typically used by the input path to recalculate the RSS after191* some form of packet processing (eg de-capsulation, IP fragment reassembly.)192*193* dir is the packet direction - RSS_HASH_PKT_INGRESS for incoming and194* RSS_HASH_PKT_EGRESS for outgoing.195*196* Returns 0 if a hash was done, -1 if no hash was done, +1 if197* the mbuf already had a valid RSS flowid.198*199* This function doesn't modify the mbuf. It's up to the caller to200* assign flowid/flowtype as appropriate.201*/202int203rss_mbuf_software_hash_v4(const struct mbuf *m, int dir, uint32_t *hashval,204uint32_t *hashtype)205{206const struct ip *ip;207const struct tcphdr *th;208const struct udphdr *uh;209uint32_t flowtype;210uint8_t proto;211int iphlen;212int is_frag = 0;213214/*215* XXX For now this only handles hashing on incoming mbufs.216*/217if (dir != RSS_HASH_PKT_INGRESS) {218RSS_DEBUG("called on EGRESS packet!\n");219return (-1);220}221222/*223* First, validate that the mbuf we have is long enough224* to have an IPv4 header in it.225*/226if (m->m_pkthdr.len < (sizeof(struct ip))) {227RSS_DEBUG("short mbuf pkthdr\n");228return (-1);229}230if (m->m_len < (sizeof(struct ip))) {231RSS_DEBUG("short mbuf len\n");232return (-1);233}234235/* Ok, let's dereference that */236ip = mtod(m, struct ip *);237proto = ip->ip_p;238iphlen = ip->ip_hl << 2;239240/*241* If this is a fragment then it shouldn't be four-tuple242* hashed just yet. Once it's reassembled into a full243* frame it should be re-hashed.244*/245if (ip->ip_off & htons(IP_MF | IP_OFFMASK))246is_frag = 1;247248/*249* If the mbuf flowid/flowtype matches the packet type,250* and we don't support the 4-tuple version of the given protocol,251* then signal to the owner that it can trust the flowid/flowtype252* details.253*254* This is a little picky - eg, if TCPv4 / UDPv4 hashing255* is supported but we got a TCP/UDP frame only 2-tuple hashed,256* then we shouldn't just "trust" the 2-tuple hash. We need257* a 4-tuple hash.258*/259flowtype = M_HASHTYPE_GET(m);260261if (flowtype != M_HASHTYPE_NONE) {262switch (proto) {263case IPPROTO_UDP:264if ((rss_gethashconfig() & RSS_HASHTYPE_RSS_UDP_IPV4) &&265(flowtype == M_HASHTYPE_RSS_UDP_IPV4) &&266(is_frag == 0)) {267return (1);268}269/*270* Only allow 2-tuple for UDP frames if we don't also271* support 4-tuple for UDP.272*/273if ((rss_gethashconfig() & RSS_HASHTYPE_RSS_IPV4) &&274((rss_gethashconfig() & RSS_HASHTYPE_RSS_UDP_IPV4) == 0) &&275flowtype == M_HASHTYPE_RSS_IPV4) {276return (1);277}278break;279case IPPROTO_TCP:280if ((rss_gethashconfig() & RSS_HASHTYPE_RSS_TCP_IPV4) &&281(flowtype == M_HASHTYPE_RSS_TCP_IPV4) &&282(is_frag == 0)) {283return (1);284}285/*286* Only allow 2-tuple for TCP frames if we don't also287* support 4-tuple for TCP.288*/289if ((rss_gethashconfig() & RSS_HASHTYPE_RSS_IPV4) &&290((rss_gethashconfig() & RSS_HASHTYPE_RSS_TCP_IPV4) == 0) &&291flowtype == M_HASHTYPE_RSS_IPV4) {292return (1);293}294break;295default:296if ((rss_gethashconfig() & RSS_HASHTYPE_RSS_IPV4) &&297flowtype == M_HASHTYPE_RSS_IPV4) {298return (1);299}300break;301}302}303304/*305* Decode enough information to make a hash decision.306*307* XXX TODO: does the hardware hash on 4-tuple if IP308* options are present?309*/310if ((rss_gethashconfig() & RSS_HASHTYPE_RSS_TCP_IPV4) &&311(proto == IPPROTO_TCP) &&312(is_frag == 0)) {313if (m->m_len < iphlen + sizeof(struct tcphdr)) {314RSS_DEBUG("short TCP frame?\n");315return (-1);316}317th = (const struct tcphdr *)((c_caddr_t)ip + iphlen);318return rss_proto_software_hash_v4(ip->ip_src, ip->ip_dst,319th->th_sport,320th->th_dport,321proto,322hashval,323hashtype);324} else if ((rss_gethashconfig() & RSS_HASHTYPE_RSS_UDP_IPV4) &&325(proto == IPPROTO_UDP) &&326(is_frag == 0)) {327uh = (const struct udphdr *)((c_caddr_t)ip + iphlen);328if (m->m_len < iphlen + sizeof(struct udphdr)) {329RSS_DEBUG("short UDP frame?\n");330return (-1);331}332return rss_proto_software_hash_v4(ip->ip_src, ip->ip_dst,333uh->uh_sport,334uh->uh_dport,335proto,336hashval,337hashtype);338} else if (rss_gethashconfig() & RSS_HASHTYPE_RSS_IPV4) {339/* Default to 2-tuple hash */340return rss_proto_software_hash_v4(ip->ip_src, ip->ip_dst,3410, /* source port */3420, /* destination port */3430, /* IPPROTO_IP */344hashval,345hashtype);346} else {347RSS_DEBUG("no available hashtypes!\n");348return (-1);349}350}351352/*353* Similar to rss_m2cpuid, but designed to be used by the IP NETISR354* on incoming frames.355*356* If an existing RSS hash exists and it matches what the configured357* hashing is, then use it.358*359* If there's an existing RSS hash but the desired hash is different,360* or if there's no useful RSS hash, then calculate it via361* the software path.362*363* XXX TODO: definitely want statistics here!364*/365struct mbuf *366rss_soft_m2cpuid_v4(struct mbuf *m, uintptr_t source, u_int *cpuid)367{368uint32_t hash_val, hash_type;369int ret;370371M_ASSERTPKTHDR(m);372373ret = rss_mbuf_software_hash_v4(m, RSS_HASH_PKT_INGRESS,374&hash_val, &hash_type);375if (ret > 0) {376/* mbuf has a valid hash already; don't need to modify it */377*cpuid = rss_hash2cpuid(m->m_pkthdr.flowid, M_HASHTYPE_GET(m));378} else if (ret == 0) {379/* hash was done; update */380m->m_pkthdr.flowid = hash_val;381M_HASHTYPE_SET(m, hash_type);382*cpuid = rss_hash2cpuid(m->m_pkthdr.flowid, M_HASHTYPE_GET(m));383} else { /* ret < 0 */384/* no hash was done */385*cpuid = NETISR_CPUID_NONE;386}387return (m);388}389390391