/*-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"31#include "opt_rss.h"3233#include <sys/param.h>34#include <sys/mbuf.h>35#include <sys/socket.h>36#include <sys/priv.h>37#include <sys/kernel.h>38#include <sys/smp.h>39#include <sys/sysctl.h>40#include <sys/sbuf.h>4142#include <net/if.h>43#include <net/if_var.h>44#include <net/netisr.h>45#include <net/rss_config.h>4647#include <netinet/in.h>48#include <netinet/in_pcb.h>49#include <netinet/in_rss.h>50#include <netinet/in_var.h>5152/* for software rss hash support */53#include <netinet/ip.h>54#include <netinet/tcp.h>55#include <netinet/udp.h>5657/*58* Hash an IPv4 2-tuple.59*/60uint32_t61rss_hash_ip4_2tuple(struct in_addr src, struct in_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 IPv4 4-tuple.76*/77uint32_t78rss_hash_ip4_4tuple(struct in_addr src, u_short srcport, struct in_addr dst,79u_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 ipv4 2-tuple or 4-tuple given the given99* IPv4 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_v4(struct in_addr s, struct in_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_IPV4)) {123hash = rss_hash_ip4_4tuple(s, sp, d, dp);124*hashval = hash;125*hashtype = M_HASHTYPE_RSS_TCP_IPV4;126return (0);127} else if ((proto == IPPROTO_UDP) &&128(rss_gethashconfig() & RSS_HASHTYPE_RSS_UDP_IPV4)) {129hash = rss_hash_ip4_4tuple(s, sp, d, dp);130*hashval = hash;131*hashtype = M_HASHTYPE_RSS_UDP_IPV4;132return (0);133} else if (rss_gethashconfig() & RSS_HASHTYPE_RSS_IPV4) {134/* RSS doesn't hash on other protocols like SCTP; so 2-tuple */135hash = rss_hash_ip4_2tuple(s, d);136*hashval = hash;137*hashtype = M_HASHTYPE_RSS_IPV4;138return (0);139}140141/* No configured available hashtypes! */142RSS_DEBUG("no available hashtypes!\n");143return (-1);144}145146/*147* Calculate an appropriate ipv4 2-tuple or 4-tuple given the given148* IPv4 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 "outgoing" packet order (ie, destination is "far" address.)156*/157uint32_t158xps_proto_software_hash_v4(struct in_addr s, struct in_addr d,159u_short sp, u_short dp, int proto, uint32_t *hashtype)160{161uint32_t hash;162163/*164* Next, choose the hash type depending upon the protocol165* identifier.166*/167if ((proto == IPPROTO_TCP) &&168(rss_gethashconfig() & RSS_HASHTYPE_RSS_TCP_IPV4)) {169hash = rss_hash_ip4_4tuple(d, dp, s, sp);170*hashtype = M_HASHTYPE_RSS_TCP_IPV4;171return (hash);172} else if ((proto == IPPROTO_UDP) &&173(rss_gethashconfig() & RSS_HASHTYPE_RSS_UDP_IPV4)) {174hash = rss_hash_ip4_4tuple(d, dp, s, sp);175*hashtype = M_HASHTYPE_RSS_UDP_IPV4;176return (hash);177} else if (rss_gethashconfig() & RSS_HASHTYPE_RSS_IPV4) {178/* RSS doesn't hash on other protocols like SCTP; so 2-tuple */179hash = rss_hash_ip4_2tuple(d, s);180*hashtype = M_HASHTYPE_RSS_IPV4;181return (hash);182}183184*hashtype = M_HASHTYPE_NONE;185return (0);186}187188/*189* Do a software calculation of the RSS for the given mbuf.190*191* This is typically used by the input path to recalculate the RSS after192* some form of packet processing (eg de-capsulation, IP fragment reassembly.)193*194* dir is the packet direction - RSS_HASH_PKT_INGRESS for incoming and195* RSS_HASH_PKT_EGRESS for outgoing.196*197* Returns 0 if a hash was done, -1 if no hash was done, +1 if198* the mbuf already had a valid RSS flowid.199*200* This function doesn't modify the mbuf. It's up to the caller to201* assign flowid/flowtype as appropriate.202*/203int204rss_mbuf_software_hash_v4(const struct mbuf *m, int dir, uint32_t *hashval,205uint32_t *hashtype)206{207const struct ip *ip;208const struct tcphdr *th;209const struct udphdr *uh;210uint32_t flowtype;211uint8_t proto;212int iphlen;213int is_frag = 0;214215/*216* XXX For now this only handles hashing on incoming mbufs.217*/218if (dir != RSS_HASH_PKT_INGRESS) {219RSS_DEBUG("called on EGRESS packet!\n");220return (-1);221}222223/*224* First, validate that the mbuf we have is long enough225* to have an IPv4 header in it.226*/227if (m->m_pkthdr.len < (sizeof(struct ip))) {228RSS_DEBUG("short mbuf pkthdr\n");229return (-1);230}231if (m->m_len < (sizeof(struct ip))) {232RSS_DEBUG("short mbuf len\n");233return (-1);234}235236/* Ok, let's dereference that */237ip = mtod(m, struct ip *);238proto = ip->ip_p;239iphlen = ip->ip_hl << 2;240241/*242* If this is a fragment then it shouldn't be four-tuple243* hashed just yet. Once it's reassembled into a full244* frame it should be re-hashed.245*/246if (ip->ip_off & htons(IP_MF | IP_OFFMASK))247is_frag = 1;248249/*250* If the mbuf flowid/flowtype matches the packet type,251* and we don't support the 4-tuple version of the given protocol,252* then signal to the owner that it can trust the flowid/flowtype253* details.254*255* This is a little picky - eg, if TCPv4 / UDPv4 hashing256* is supported but we got a TCP/UDP frame only 2-tuple hashed,257* then we shouldn't just "trust" the 2-tuple hash. We need258* a 4-tuple hash.259*/260flowtype = M_HASHTYPE_GET(m);261262if (flowtype != M_HASHTYPE_NONE) {263switch (proto) {264case IPPROTO_UDP:265if ((rss_gethashconfig() & RSS_HASHTYPE_RSS_UDP_IPV4) &&266(flowtype == M_HASHTYPE_RSS_UDP_IPV4) &&267(is_frag == 0)) {268return (1);269}270/*271* Only allow 2-tuple for UDP frames if we don't also272* support 4-tuple for UDP.273*/274if ((rss_gethashconfig() & RSS_HASHTYPE_RSS_IPV4) &&275((rss_gethashconfig() & RSS_HASHTYPE_RSS_UDP_IPV4) == 0) &&276flowtype == M_HASHTYPE_RSS_IPV4) {277return (1);278}279break;280case IPPROTO_TCP:281if ((rss_gethashconfig() & RSS_HASHTYPE_RSS_TCP_IPV4) &&282(flowtype == M_HASHTYPE_RSS_TCP_IPV4) &&283(is_frag == 0)) {284return (1);285}286/*287* Only allow 2-tuple for TCP frames if we don't also288* support 4-tuple for TCP.289*/290if ((rss_gethashconfig() & RSS_HASHTYPE_RSS_IPV4) &&291((rss_gethashconfig() & RSS_HASHTYPE_RSS_TCP_IPV4) == 0) &&292flowtype == M_HASHTYPE_RSS_IPV4) {293return (1);294}295break;296default:297if ((rss_gethashconfig() & RSS_HASHTYPE_RSS_IPV4) &&298flowtype == M_HASHTYPE_RSS_IPV4) {299return (1);300}301break;302}303}304305/*306* Decode enough information to make a hash decision.307*308* XXX TODO: does the hardware hash on 4-tuple if IP309* options are present?310*/311if ((rss_gethashconfig() & RSS_HASHTYPE_RSS_TCP_IPV4) &&312(proto == IPPROTO_TCP) &&313(is_frag == 0)) {314if (m->m_len < iphlen + sizeof(struct tcphdr)) {315RSS_DEBUG("short TCP frame?\n");316return (-1);317}318th = (const struct tcphdr *)((c_caddr_t)ip + iphlen);319return rss_proto_software_hash_v4(ip->ip_src, ip->ip_dst,320th->th_sport,321th->th_dport,322proto,323hashval,324hashtype);325} else if ((rss_gethashconfig() & RSS_HASHTYPE_RSS_UDP_IPV4) &&326(proto == IPPROTO_UDP) &&327(is_frag == 0)) {328uh = (const struct udphdr *)((c_caddr_t)ip + iphlen);329if (m->m_len < iphlen + sizeof(struct udphdr)) {330RSS_DEBUG("short UDP frame?\n");331return (-1);332}333return rss_proto_software_hash_v4(ip->ip_src, ip->ip_dst,334uh->uh_sport,335uh->uh_dport,336proto,337hashval,338hashtype);339} else if (rss_gethashconfig() & RSS_HASHTYPE_RSS_IPV4) {340/* Default to 2-tuple hash */341return rss_proto_software_hash_v4(ip->ip_src, ip->ip_dst,3420, /* source port */3430, /* destination port */3440, /* IPPROTO_IP */345hashval,346hashtype);347} else {348RSS_DEBUG("no available hashtypes!\n");349return (-1);350}351}352353#ifdef RSS354/*355* Similar to rss_m2cpuid, but designed to be used by the IP NETISR356* on incoming frames.357*358* If an existing RSS hash exists and it matches what the configured359* hashing is, then use it.360*361* If there's an existing RSS hash but the desired hash is different,362* or if there's no useful RSS hash, then calculate it via363* the software path.364*365* XXX TODO: definitely want statistics here!366*/367struct mbuf *368rss_soft_m2cpuid_v4(struct mbuf *m, uintptr_t source, u_int *cpuid)369{370uint32_t hash_val, hash_type;371int ret;372373M_ASSERTPKTHDR(m);374375ret = rss_mbuf_software_hash_v4(m, RSS_HASH_PKT_INGRESS,376&hash_val, &hash_type);377if (ret > 0) {378/* mbuf has a valid hash already; don't need to modify it */379*cpuid = rss_hash2cpuid(m->m_pkthdr.flowid, M_HASHTYPE_GET(m));380} else if (ret == 0) {381/* hash was done; update */382m->m_pkthdr.flowid = hash_val;383M_HASHTYPE_SET(m, hash_type);384*cpuid = rss_hash2cpuid(m->m_pkthdr.flowid, M_HASHTYPE_GET(m));385} else { /* ret < 0 */386/* no hash was done */387*cpuid = NETISR_CPUID_NONE;388}389return (m);390}391#endif392393394