/* * Calculate an appropriate ipv4 2-tuple or 4-tuple given the given * IPv4 source/destination address, UDP or TCP source/destination ports * and the protocol type. * * The protocol code may wish to do a software hash of the given * tuple. This depends upon the currently configured RSS hash types. * * This assumes that the packet in question isn't a fragment. * * It also assumes the packet source/destination address * are in "incoming" packet order (ie, source is "far" address.) */ int rss_proto_software_hash_v4(struct in_addr s, struct in_addr d, u_short sp, u_short dp, int proto, uint32_t *hashval, uint32_t *hashtype) { uint32_t hash; /* * Next, choose the hash type depending upon the protocol * identifier. */ if ((proto == IPPROTO_TCP) && (rss_gethashconfig() & RSS_HASHTYPE_RSS_TCP_IPV4)) { hash = rss_hash_ip4_4tuple(s, sp, d, dp); *hashval = hash; *hashtype = M_HASHTYPE_RSS_TCP_IPV4; return (0); } else if ((proto == IPPROTO_UDP) && (rss_gethashconfig() & RSS_HASHTYPE_RSS_UDP_IPV4)) { hash = rss_hash_ip4_4tuple(s, sp, d, dp); *hashval = hash; *hashtype = M_HASHTYPE_RSS_UDP_IPV4; return (0); } else if (rss_gethashconfig() & RSS_HASHTYPE_RSS_IPV4) { /* RSS doesn't hash on other protocols like SCTP; so 2-tuple */ hash = rss_hash_ip4_2tuple(s, d); *hashval = hash; *hashtype = M_HASHTYPE_RSS_IPV4; return (0); } /* No configured available hashtypes! */ RSS_DEBUG("no available hashtypes!\n"); return (-1); }
/* * Do a software calculation of the RSS for the given mbuf. * * This is typically used by the input path to recalculate the RSS after * some form of packet processing (eg de-capsulation, IP fragment reassembly.) * * dir is the packet direction - RSS_HASH_PKT_INGRESS for incoming and * RSS_HASH_PKT_EGRESS for outgoing. * * Returns 0 if a hash was done, -1 if no hash was done, +1 if * the mbuf already had a valid RSS flowid. * * This function doesn't modify the mbuf. It's up to the caller to * assign flowid/flowtype as appropriate. */ int rss_mbuf_software_hash_v4(const struct mbuf *m, int dir, uint32_t *hashval, uint32_t *hashtype) { const struct ip *ip; const struct tcphdr *th; const struct udphdr *uh; uint32_t flowid; uint32_t flowtype; uint8_t proto; int iphlen; int is_frag = 0; /* * XXX For now this only handles hashing on incoming mbufs. */ if (dir != RSS_HASH_PKT_INGRESS) { RSS_DEBUG("called on EGRESS packet!\n"); return (-1); } /* * First, validate that the mbuf we have is long enough * to have an IPv4 header in it. */ if (m->m_pkthdr.len < (sizeof(struct ip))) { RSS_DEBUG("short mbuf pkthdr\n"); return (-1); } if (m->m_len < (sizeof(struct ip))) { RSS_DEBUG("short mbuf len\n"); return (-1); } /* Ok, let's dereference that */ ip = mtod(m, struct ip *); proto = ip->ip_p; iphlen = ip->ip_hl << 2; /* * If this is a fragment then it shouldn't be four-tuple * hashed just yet. Once it's reassembled into a full * frame it should be re-hashed. */ if (ip->ip_off & htons(IP_MF | IP_OFFMASK)) is_frag = 1; /* * If the mbuf flowid/flowtype matches the packet type, * and we don't support the 4-tuple version of the given protocol, * then signal to the owner that it can trust the flowid/flowtype * details. * * This is a little picky - eg, if TCPv4 / UDPv4 hashing * is supported but we got a TCP/UDP frame only 2-tuple hashed, * then we shouldn't just "trust" the 2-tuple hash. We need * a 4-tuple hash. */ flowid = m->m_pkthdr.flowid; flowtype = M_HASHTYPE_GET(m); if (flowtype != M_HASHTYPE_NONE) { switch (proto) { case IPPROTO_UDP: if ((rss_gethashconfig() & RSS_HASHTYPE_RSS_UDP_IPV4) && (flowtype == M_HASHTYPE_RSS_UDP_IPV4) && (is_frag == 0)) { return (1); } /* * Only allow 2-tuple for UDP frames if we don't also * support 4-tuple for UDP. */ if ((rss_gethashconfig() & RSS_HASHTYPE_RSS_IPV4) && ((rss_gethashconfig() & RSS_HASHTYPE_RSS_UDP_IPV4) == 0) && flowtype == M_HASHTYPE_RSS_IPV4) { return (1); } break; case IPPROTO_TCP: if ((rss_gethashconfig() & RSS_HASHTYPE_RSS_TCP_IPV4) && (flowtype == M_HASHTYPE_RSS_TCP_IPV4) && (is_frag == 0)) { return (1); } /* * Only allow 2-tuple for TCP frames if we don't also * support 2-tuple for TCP. */ if ((rss_gethashconfig() & RSS_HASHTYPE_RSS_IPV4) && ((rss_gethashconfig() & RSS_HASHTYPE_RSS_TCP_IPV4) == 0) && flowtype == M_HASHTYPE_RSS_IPV4) { return (1); } break; default: if ((rss_gethashconfig() & RSS_HASHTYPE_RSS_IPV4) && flowtype == M_HASHTYPE_RSS_IPV4) { return (1); } break; } } /* * Decode enough information to make a hash decision. * * XXX TODO: does the hardware hash on 4-tuple if IP * options are present? */ if ((rss_gethashconfig() & RSS_HASHTYPE_RSS_TCP_IPV4) && (proto == IPPROTO_TCP) && (is_frag == 0)) { if (m->m_len < iphlen + sizeof(struct tcphdr)) { RSS_DEBUG("short TCP frame?\n"); return (-1); } th = (const struct tcphdr *)((c_caddr_t)ip + iphlen); return rss_proto_software_hash_v4(ip->ip_src, ip->ip_dst, th->th_sport, th->th_dport, proto, hashval, hashtype); } else if ((rss_gethashconfig() & RSS_HASHTYPE_RSS_UDP_IPV4) && (proto == IPPROTO_UDP) && (is_frag == 0)) { uh = (const struct udphdr *)((c_caddr_t)ip + iphlen); if (m->m_len < iphlen + sizeof(struct udphdr)) { RSS_DEBUG("short UDP frame?\n"); return (-1); } return rss_proto_software_hash_v4(ip->ip_src, ip->ip_dst, uh->uh_sport, uh->uh_dport, proto, hashval, hashtype); } else if (rss_gethashconfig() & RSS_HASHTYPE_RSS_IPV4) { /* Default to 2-tuple hash */ return rss_proto_software_hash_v4(ip->ip_src, ip->ip_dst, 0, /* source port */ 0, /* destination port */ 0, /* IPPROTO_IP */ hashval, hashtype); } else { RSS_DEBUG("no available hashtypes!\n"); return (-1); } }
/* * Do a software calculation of the RSS for the given mbuf. * * This is typically used by the input path to recalculate the RSS after * some form of packet processing (eg de-capsulation, IP fragment reassembly.) * * dir is the packet direction - RSS_HASH_PKT_INGRESS for incoming and * RSS_HASH_PKT_EGRESS for outgoing. * * Returns 0 if a hash was done, -1 if no hash was done, +1 if * the mbuf already had a valid RSS flowid. * * This function doesn't modify the mbuf. It's up to the caller to * assign flowid/flowtype as appropriate. */ int rss_mbuf_software_hash_v6(const struct mbuf *m, int dir, uint32_t *hashval, uint32_t *hashtype) { const struct ip6_hdr *ip6; const struct tcphdr *th; const struct udphdr *uh; uint32_t flowtype; uint8_t proto; int off, newoff; int nxt; /* * XXX For now this only handles hashing on incoming mbufs. */ if (dir != RSS_HASH_PKT_INGRESS) { RSS_DEBUG("called on EGRESS packet!\n"); return (-1); } off = sizeof(struct ip6_hdr); /* * First, validate that the mbuf we have is long enough * to have an IPv6 header in it. */ if (m->m_pkthdr.len < off) { RSS_DEBUG("short mbuf pkthdr\n"); return (-1); } if (m->m_len < off) { RSS_DEBUG("short mbuf len\n"); return (-1); } /* Ok, let's dereference that */ ip6 = mtod(m, struct ip6_hdr *); proto = ip6->ip6_nxt; /* * Find the beginning of the TCP/UDP header. * * If this is a fragment then it shouldn't be four-tuple * hashed just yet. Once it's reassembled into a full * frame it should be re-hashed. */ while (proto != IPPROTO_FRAGMENT) { newoff = ip6_nexthdr(m, off, proto, &nxt); if (newoff < 0) break; off = newoff; proto = nxt; } /* * If the mbuf flowid/flowtype matches the packet type, * and we don't support the 4-tuple version of the given protocol, * then signal to the owner that it can trust the flowid/flowtype * details. * * This is a little picky - eg, if TCPv6 / UDPv6 hashing * is supported but we got a TCP/UDP frame only 2-tuple hashed, * then we shouldn't just "trust" the 2-tuple hash. We need * a 4-tuple hash. */ flowtype = M_HASHTYPE_GET(m); if (flowtype != M_HASHTYPE_NONE) { switch (proto) { case IPPROTO_UDP: if ((rss_gethashconfig() & RSS_HASHTYPE_RSS_UDP_IPV6) && (flowtype == M_HASHTYPE_RSS_UDP_IPV6)) { return (1); } /* * Only allow 2-tuple for UDP frames if we don't also * support 4-tuple for UDP. */ if ((rss_gethashconfig() & RSS_HASHTYPE_RSS_IPV6) && ((rss_gethashconfig() & RSS_HASHTYPE_RSS_UDP_IPV6) == 0) && flowtype == M_HASHTYPE_RSS_IPV6) { return (1); } break; case IPPROTO_TCP: if ((rss_gethashconfig() & RSS_HASHTYPE_RSS_TCP_IPV6) && (flowtype == M_HASHTYPE_RSS_TCP_IPV6)) { return (1); } /* * Only allow 2-tuple for TCP frames if we don't also * support 4-tuple for TCP. */ if ((rss_gethashconfig() & RSS_HASHTYPE_RSS_IPV6) && ((rss_gethashconfig() & RSS_HASHTYPE_RSS_TCP_IPV6) == 0) && flowtype == M_HASHTYPE_RSS_IPV6) { return (1); } break; default: if ((rss_gethashconfig() & RSS_HASHTYPE_RSS_IPV6) && flowtype == M_HASHTYPE_RSS_IPV6) { return (1); } break; } } /* * Decode enough information to make a hash decision. */ if ((rss_gethashconfig() & RSS_HASHTYPE_RSS_TCP_IPV6) && (proto == IPPROTO_TCP)) { if (m->m_len < off + sizeof(struct tcphdr)) { RSS_DEBUG("short TCP frame?\n"); return (-1); } th = (const struct tcphdr *)((c_caddr_t)ip6 + off); return rss_proto_software_hash_v6(&ip6->ip6_src, &ip6->ip6_dst, th->th_sport, th->th_dport, proto, hashval, hashtype); } else if ((rss_gethashconfig() & RSS_HASHTYPE_RSS_UDP_IPV6) && (proto == IPPROTO_UDP)) { if (m->m_len < off + sizeof(struct udphdr)) { RSS_DEBUG("short UDP frame?\n"); return (-1); } uh = (const struct udphdr *)((c_caddr_t)ip6 + off); return rss_proto_software_hash_v6(&ip6->ip6_src, &ip6->ip6_dst, uh->uh_sport, uh->uh_dport, proto, hashval, hashtype); } else if (rss_gethashconfig() & RSS_HASHTYPE_RSS_IPV6) { /* Default to 2-tuple hash */ return rss_proto_software_hash_v6(&ip6->ip6_src, &ip6->ip6_dst, 0, /* source port */ 0, /* destination port */ 0, /* IPPROTO_IP */ hashval, hashtype); } else { RSS_DEBUG("no available hashtypes!\n"); return (-1); } }