#include <sys/types.h>
#include <sys/mac.h>
#include <sys/mac_impl.h>
#include <sys/mac_client_priv.h>
#include <sys/mac_client_impl.h>
#include <sys/mac_soft_ring.h>
#include <sys/strsubr.h>
#include <sys/strsun.h>
#include <sys/vlan.h>
#include <sys/pattr.h>
#include <sys/pci_tools.h>
#include <inet/ip.h>
#include <inet/ip_impl.h>
#include <inet/ip6.h>
#include <sys/vtrace.h>
#include <sys/dlpi.h>
#include <sys/sunndi.h>
#include <inet/ipsec_impl.h>
#include <inet/sadb.h>
#include <inet/ipsecesp.h>
#include <inet/ipsecah.h>
#include <inet/tcp.h>
#include <inet/sctp_ip.h>
void
mac_drop_pkt(mblk_t *mp, const char *fmt, ...)
{
va_list adx;
char msg[128];
char *msgp = msg;
ASSERT3P(mp->b_next, ==, NULL);
va_start(adx, fmt);
(void) vsnprintf(msgp, sizeof (msg), fmt, adx);
va_end(adx);
DTRACE_PROBE2(mac__drop, mblk_t *, mp, char *, msgp);
freemsg(mp);
}
void
mac_drop_chain(mblk_t *chain, const char *fmt, ...)
{
va_list adx;
char msg[128];
char *msgp = msg;
va_start(adx, fmt);
(void) vsnprintf(msgp, sizeof (msg), fmt, adx);
va_end(adx);
for (mblk_t *mp = chain, *next; mp != NULL; ) {
next = mp->b_next;
DTRACE_PROBE2(mac__drop, mblk_t *, mp, char *, msgp);
mp->b_next = NULL;
freemsg(mp);
mp = next;
}
}
static mblk_t *
mac_sw_cksum(mblk_t *mp, mac_emul_t emul)
{
mac_ether_offload_info_t meoi = { 0 };
const char *err = "";
VERIFY3P(mp->b_next, ==, NULL);
uint32_t flags = DB_CKSUMFLAGS(mp);
ASSERT3U(flags & (HCK_FLAGS), !=, 0);
if ((flags & HCK_FULLCKSUM) != 0 && (flags & HCK_PARTIALCKSUM) != 0) {
err = "full and partial ULP cksum requested";
goto bail;
}
const boolean_t do_v4_cksum = (emul & MAC_IPCKSUM_EMUL) != 0 &&
(flags & HCK_IPV4_HDRCKSUM) != 0;
const boolean_t do_ulp_cksum = (emul & MAC_HWCKSUM_EMUL) != 0 &&
(flags & (HCK_FULLCKSUM | HCK_PARTIALCKSUM)) != 0;
const boolean_t ulp_prefer_partial = (flags & HCK_PARTIALCKSUM) != 0;
mac_ether_offload_info(mp, &meoi);
if ((meoi.meoi_flags & MEOI_L2INFO_SET) == 0 ||
(meoi.meoi_l3proto != ETHERTYPE_IP &&
meoi.meoi_l3proto != ETHERTYPE_IPV6)) {
return (mp);
}
if (do_v4_cksum) {
if (meoi.meoi_l3proto != ETHERTYPE_IP) {
err = "IPv4 csum requested on non-IPv4 packet";
goto bail;
}
}
if (do_ulp_cksum) {
if ((meoi.meoi_flags & MEOI_L4INFO_SET) == 0) {
err = "missing ULP header";
goto bail;
}
switch (meoi.meoi_l4proto) {
case IPPROTO_TCP:
case IPPROTO_UDP:
case IPPROTO_ICMP:
case IPPROTO_ICMPV6:
case IPPROTO_SCTP:
break;
default:
err = "unexpected ULP";
goto bail;
}
}
mblk_t *skipped_hdr = NULL;
if (MBLKL(mp) == meoi.meoi_l2hlen) {
meoi.meoi_len -= meoi.meoi_l2hlen;
meoi.meoi_l2hlen = 0;
skipped_hdr = mp;
mp = mp->b_cont;
ASSERT(mp != NULL);
}
const size_t hdr_len_reqd = (meoi.meoi_l2hlen + meoi.meoi_l3hlen) +
(do_ulp_cksum ? meoi.meoi_l4hlen : 0);
if (MBLKL(mp) < hdr_len_reqd || DB_REF(mp) > 1 ||
!OK_32PTR(mp->b_rptr + meoi.meoi_l2hlen)) {
const size_t pad_by = (4 - (meoi.meoi_l2hlen % 4)) % 4;
mblk_t *hdrmp = msgpullup_pad(mp, hdr_len_reqd, pad_by);
if (hdrmp == NULL) {
err = "could not pullup msg headers";
goto bail;
}
mac_hcksum_clone(mp, hdrmp);
if (skipped_hdr != NULL) {
ASSERT3P(skipped_hdr->b_cont, ==, mp);
skipped_hdr->b_cont = hdrmp;
}
freemsg(mp);
mp = hdrmp;
}
if (do_v4_cksum) {
ipha_t *ipha = (ipha_t *)(mp->b_rptr + meoi.meoi_l2hlen);
ipha->ipha_hdr_checksum = 0;
ipha->ipha_hdr_checksum = (uint16_t)ip_csum_hdr(ipha);
flags &= ~HCK_IPV4_HDRCKSUM;
flags |= HCK_IPV4_HDRCKSUM_OK;
}
if (do_ulp_cksum && meoi.meoi_l4proto == IPPROTO_SCTP) {
if (ulp_prefer_partial) {
err = "SCTP does not support partial checksum";
goto bail;
}
const uint_t ulp_off = meoi.meoi_l2hlen + meoi.meoi_l3hlen;
sctp_hdr_t *sctph = (sctp_hdr_t *)(mp->b_rptr + ulp_off);
sctph->sh_chksum = 0;
sctph->sh_chksum = sctp_cksum(mp, ulp_off);
flags &= ~HCK_FULLCKSUM;
flags |= HCK_FULLCKSUM_OK;
goto success;
}
if (do_ulp_cksum && !ulp_prefer_partial) {
uint32_t cksum = 0;
if (meoi.meoi_l3proto == ETHERTYPE_IP) {
const ipha_t *ipha =
(const ipha_t *)(mp->b_rptr + meoi.meoi_l2hlen);
const uint16_t *ipp =
(const uint16_t *)(&ipha->ipha_src);
cksum += ipp[0] + ipp[1] + ipp[2] + ipp[3];
const uint16_t payload_len =
ntohs(ipha->ipha_length) - meoi.meoi_l3hlen;
cksum += htons(payload_len);
} else if (meoi.meoi_l3proto == ETHERTYPE_IPV6) {
const ip6_t *ip6h =
(const ip6_t *)(mp->b_rptr + meoi.meoi_l2hlen);
const uint16_t *ipp =
(const uint16_t *)(&ip6h->ip6_src);
cksum += ipp[0] + ipp[1] + ipp[2] + ipp[3] +
ipp[4] + ipp[5] + ipp[6] + ipp[7];
cksum += ipp[8] + ipp[9] + ipp[10] + ipp[11] +
ipp[12] + ipp[13] + ipp[14] + ipp[15];
const uint16_t payload_len = meoi.meoi_len -
((uint16_t)meoi.meoi_l2hlen + meoi.meoi_l3hlen);
cksum += htons(payload_len);
} else {
panic("L3 protocol unexpectedly changed");
}
uint_t cksum_off;
switch (meoi.meoi_l4proto) {
case IPPROTO_TCP:
cksum += IP_TCP_CSUM_COMP;
cksum_off = TCP_CHECKSUM_OFFSET;
break;
case IPPROTO_UDP:
cksum += IP_UDP_CSUM_COMP;
cksum_off = UDP_CHECKSUM_OFFSET;
break;
case IPPROTO_ICMP:
cksum = 0;
cksum_off = ICMP_CHECKSUM_OFFSET;
break;
case IPPROTO_ICMPV6:
cksum += IP_ICMPV6_CSUM_COMP;
cksum_off = ICMPV6_CHECKSUM_OFFSET;
break;
default:
err = "unrecognized L4 protocol";
goto bail;
}
const uint_t l4_off = meoi.meoi_l3hlen + meoi.meoi_l2hlen;
uint16_t *up = (uint16_t *)(mp->b_rptr + l4_off + cksum_off);
*up = 0;
cksum = IP_CSUM(mp, l4_off, cksum);
if (meoi.meoi_l4proto == IPPROTO_UDP && cksum == 0) {
*up = 0xffff;
} else {
*up = cksum;
}
flags &= ~HCK_FULLCKSUM;
flags |= HCK_FULLCKSUM_OK;
goto success;
}
if (do_ulp_cksum && ulp_prefer_partial) {
uint32_t start, stuff, end, value;
mac_hcksum_get(mp, &start, &stuff, &end, &value, NULL);
ASSERT3S(end, >, start);
if (stuff + sizeof (uint16_t) > MBLKL(mp)) {
err = "partial csum request is out of bounds";
goto bail;
}
uchar_t *ipp = (uchar_t *)(mp->b_rptr + meoi.meoi_l2hlen);
uint16_t *up = (uint16_t *)(ipp + stuff);
const uint16_t partial = *up;
*up = 0;
const uint16_t cksum =
~IP_CSUM_PARTIAL(mp, start + meoi.meoi_l2hlen, partial);
*up = cksum != 0 ? cksum : ~cksum;
flags &= ~HCK_PARTIALCKSUM;
flags |= HCK_FULLCKSUM_OK;
}
success:
mac_hcksum_set(mp, 0, 0, 0, 0, flags);
if (skipped_hdr != NULL) {
ASSERT3P(skipped_hdr->b_cont, ==, mp);
mac_hcksum_clone(mp, skipped_hdr);
mp = skipped_hdr;
}
return (mp);
bail:
if (skipped_hdr != NULL) {
ASSERT3P(skipped_hdr->b_cont, ==, mp);
mp = skipped_hdr;
}
mac_drop_pkt(mp, err);
return (NULL);
}
static mblk_t *
build_data_seg(mblk_t **lso_mp, uint32_t *offset, uint32_t seg_len)
{
mblk_t *seg_head, *seg_tail, *seg_mp;
ASSERT3P(*lso_mp, !=, NULL);
ASSERT3U((*lso_mp)->b_rptr + *offset, <, (*lso_mp)->b_wptr);
seg_mp = dupb(*lso_mp);
if (seg_mp == NULL)
return (NULL);
seg_head = seg_mp;
seg_tail = seg_mp;
seg_mp->b_rptr += *offset;
last_mblk:
if ((seg_mp->b_rptr + seg_len) < seg_mp->b_wptr) {
*offset += seg_len;
seg_mp->b_wptr = seg_mp->b_rptr + seg_len;
return (seg_head);
}
if ((seg_mp->b_rptr + seg_len) == seg_mp->b_wptr) {
*offset = 0;
*lso_mp = (*lso_mp)->b_cont;
return (seg_head);
}
ASSERT3U(seg_mp->b_rptr + seg_len, >, seg_mp->b_wptr);
while ((seg_mp->b_rptr + seg_len) > seg_mp->b_wptr) {
ASSERT3U(MBLKL(seg_mp), <=, seg_len);
seg_len -= MBLKL(seg_mp);
*offset = 0;
*lso_mp = (*lso_mp)->b_cont;
seg_mp = dupb(*lso_mp);
if (seg_mp == NULL) {
freemsgchain(seg_head);
return (NULL);
}
seg_tail->b_cont = seg_mp;
seg_tail = seg_mp;
}
ASSERT3U(seg_len, <, IP_MAXPACKET);
ASSERT3U(seg_len, >, 0);
goto last_mblk;
}
static void
mac_sw_lso(mblk_t *omp, mac_emul_t emul, mblk_t **head, mblk_t **tail,
uint_t *count)
{
uint32_t ocsum_flags, ocsum_start, ocsum_stuff;
uint32_t mss;
uint32_t oehlen, oiphlen, otcphlen, ohdrslen, opktlen;
uint32_t odatalen, oleft;
uint_t nsegs, seg;
int len;
const void *oiph;
const tcph_t *otcph;
ipha_t *niph;
tcph_t *ntcph;
uint16_t ip_id;
uint32_t tcp_seq, tcp_sum, otcp_sum;
boolean_t is_v6 = B_FALSE;
ip6_t *niph6;
uint32_t offset = 0;
mblk_t *odatamp;
mblk_t *seg_chain, *prev_nhdrmp, *next_nhdrmp, *nhdrmp, *ndatamp;
mblk_t *tmptail;
mac_ether_offload_info_t meoi = { 0 };
ASSERT3P(head, !=, NULL);
ASSERT3P(tail, !=, NULL);
ASSERT3P(count, !=, NULL);
ASSERT3U((DB_CKSUMFLAGS(omp) & HW_LSO), !=, 0);
ASSERT3P(omp->b_next, ==, NULL);
mac_ether_offload_info(omp, &meoi);
opktlen = meoi.meoi_len;
oehlen = meoi.meoi_l2hlen;
oiphlen = meoi.meoi_l3hlen;
otcphlen = meoi.meoi_l4hlen;
ohdrslen = oehlen + oiphlen + otcphlen;
if ((MEOI_L4INFO_SET & meoi.meoi_flags) == 0) {
mac_drop_pkt(omp, "unable to fully parse packet to L4");
goto fail;
}
if (meoi.meoi_l3proto != ETHERTYPE_IP &&
meoi.meoi_l3proto != ETHERTYPE_IPV6) {
mac_drop_pkt(omp, "LSO'd packet has non-IP L3 header: %x",
meoi.meoi_l3proto);
goto fail;
}
if (meoi.meoi_l4proto != IPPROTO_TCP) {
mac_drop_pkt(omp, "LSO unsupported protocol: %x",
meoi.meoi_l4proto);
goto fail;
}
is_v6 = meoi.meoi_l3proto == ETHERTYPE_IPV6;
mss = DB_LSOMSS(omp);
if (mss == 0) {
mac_drop_pkt(omp, "packet misconfigured for LSO (MSS == 0)");
goto fail;
}
ASSERT3U(opktlen, <=, IP_MAXPACKET + oehlen);
const size_t pad_by = (4 - (meoi.meoi_l2hlen % 4)) % 4;
if (MBLKL(omp) < ohdrslen || !OK_32PTR(omp->b_rptr + oehlen)) {
mblk_t *tmp = msgpullup_pad(omp, ohdrslen, pad_by);
if (tmp == NULL) {
mac_drop_pkt(omp, "failed to pull up");
goto fail;
}
mac_hcksum_clone(omp, tmp);
freemsg(omp);
omp = tmp;
}
oiph = (void *)(omp->b_rptr + oehlen);
otcph = (tcph_t *)(omp->b_rptr + oehlen + oiphlen);
if (otcph->th_flags[0] & (TH_SYN | TH_RST | TH_URG)) {
mac_drop_pkt(omp, "LSO packet has SYN|RST|URG set");
goto fail;
}
len = MBLKL(omp);
if (len > ohdrslen) {
odatamp = omp;
offset = ohdrslen;
} else {
ASSERT3U(len, ==, ohdrslen);
odatamp = omp->b_cont;
offset = 0;
}
odatalen = opktlen - ohdrslen;
ASSERT3U(msgsize(odatamp), >=, odatalen);
ASSERT3S(DB_TYPE(omp), ==, M_DATA);
ocsum_flags = DB_CKSUMFLAGS(omp);
ASSERT3U(ocsum_flags & (HCK_PARTIALCKSUM | HCK_FULLCKSUM), !=, 0);
if (ocsum_flags & HCK_PARTIALCKSUM) {
ocsum_start = (uint32_t)DB_CKSUMSTART(omp);
ocsum_stuff = (uint32_t)DB_CKSUMSTUFF(omp);
}
nsegs = ((odatalen - 1) / mss) + 1;
if (nsegs < 2) {
mac_drop_pkt(omp, "LSO not enough segs: %u", nsegs);
goto fail;
}
DTRACE_PROBE6(sw__lso__start, mblk_t *, omp, void_ip_t *, oiph,
__dtrace_tcp_tcph_t *, otcph, uint_t, odatalen, uint_t, mss,
uint_t, nsegs);
seg_chain = NULL;
tmptail = seg_chain;
oleft = odatalen;
for (uint_t i = 0; i < nsegs; i++) {
boolean_t last_seg = ((i + 1) == nsegs);
uint32_t seg_len;
if ((nhdrmp = allocb(pad_by + ohdrslen, 0)) == NULL) {
freemsgchain(seg_chain);
mac_drop_pkt(omp, "failed to alloc segment header");
goto fail;
}
ASSERT3P(nhdrmp->b_cont, ==, NULL);
nhdrmp->b_rptr += pad_by;
nhdrmp->b_wptr = nhdrmp->b_rptr + ohdrslen;
bcopy(omp->b_rptr, nhdrmp->b_rptr, ohdrslen);
if (seg_chain == NULL) {
seg_chain = nhdrmp;
} else {
ASSERT3P(tmptail, !=, NULL);
tmptail->b_next = nhdrmp;
}
tmptail = nhdrmp;
seg_len = last_seg ? oleft : mss;
ASSERT3U(seg_len, <=, mss);
ndatamp = build_data_seg(&odatamp, &offset, seg_len);
if (ndatamp == NULL) {
freemsgchain(seg_chain);
mac_drop_pkt(omp, "LSO failed to segment data");
goto fail;
}
nhdrmp->b_cont = ndatamp;
DB_CKSUMFLAGS(ndatamp) &= ~HW_LSO;
ASSERT3U(seg_len, <=, oleft);
oleft -= seg_len;
if (ocsum_flags & HCK_PARTIALCKSUM) {
DB_CKSUMSTART(nhdrmp) = ocsum_start;
DB_CKSUMEND(nhdrmp) = oiphlen + otcphlen + seg_len;
DB_CKSUMSTUFF(nhdrmp) = ocsum_stuff;
}
}
ASSERT3S(oleft, ==, 0);
ASSERT3P(odatamp, ==, NULL);
ndatamp = NULL;
nhdrmp = seg_chain;
ASSERT3U(msgsize(nhdrmp->b_cont), ==, mss);
if (is_v6) {
niph6 = (ip6_t *)(nhdrmp->b_rptr + oehlen);
niph6->ip6_plen = htons(
(oiphlen - IPV6_HDR_LEN) + otcphlen + mss);
} else {
niph = (ipha_t *)(nhdrmp->b_rptr + oehlen);
niph->ipha_length = htons(oiphlen + otcphlen + mss);
if (niph->ipha_hdr_checksum != 0) {
emul |= MAC_IPCKSUM_EMUL;
ocsum_flags |= HCK_IPV4_HDRCKSUM;
}
niph->ipha_hdr_checksum = 0;
ip_id = ntohs(niph->ipha_ident);
}
ntcph = (tcph_t *)(nhdrmp->b_rptr + oehlen + oiphlen);
tcp_seq = BE32_TO_U32(ntcph->th_seq);
tcp_seq += mss;
ntcph->th_flags[0] &= ~(TH_FIN | TH_PUSH);
DB_CKSUMFLAGS(nhdrmp) = (uint16_t)(ocsum_flags & ~HW_LSO);
if (ocsum_flags & HCK_PARTIALCKSUM) {
tcp_sum = BE16_TO_U16(ntcph->th_sum);
otcp_sum = tcp_sum;
tcp_sum += mss + otcphlen;
tcp_sum = (tcp_sum >> 16) + (tcp_sum & 0xFFFF);
U16_TO_BE16(tcp_sum, ntcph->th_sum);
}
if ((ocsum_flags & HCK_TX_FLAGS) && (emul & MAC_HWCKSUM_EMULS)) {
next_nhdrmp = nhdrmp->b_next;
nhdrmp->b_next = NULL;
nhdrmp = mac_sw_cksum(nhdrmp, emul);
if (nhdrmp != NULL) {
nhdrmp->b_next = next_nhdrmp;
next_nhdrmp = NULL;
seg_chain = nhdrmp;
} else {
freemsgchain(next_nhdrmp);
seg_chain = NULL;
mac_drop_pkt(omp, "LSO cksum emulation failed");
goto fail;
}
}
ASSERT3P(nhdrmp, !=, NULL);
seg = 1;
DTRACE_PROBE5(sw__lso__seg, mblk_t *, nhdrmp, void_ip_t *,
(is_v6 ? (void *)niph6 : (void *)niph),
__dtrace_tcp_tcph_t *, ntcph, uint_t, mss, int_t, seg);
seg++;
ASSERT3P(nhdrmp->b_next, !=, NULL);
prev_nhdrmp = nhdrmp;
nhdrmp = nhdrmp->b_next;
for (; seg < nsegs; seg++) {
ASSERT3P(msgsize(nhdrmp->b_cont), ==, mss);
if (is_v6) {
niph6 = (ip6_t *)(nhdrmp->b_rptr + oehlen);
niph6->ip6_plen = htons(
(oiphlen - IPV6_HDR_LEN) + otcphlen + mss);
} else {
niph = (ipha_t *)(nhdrmp->b_rptr + oehlen);
niph->ipha_ident = htons(++ip_id);
niph->ipha_length = htons(oiphlen + otcphlen + mss);
niph->ipha_hdr_checksum = 0;
}
ntcph = (tcph_t *)(nhdrmp->b_rptr + oehlen + oiphlen);
U32_TO_BE32(tcp_seq, ntcph->th_seq);
tcp_seq += mss;
ntcph->th_flags[0] &= ~(TH_FIN | TH_PUSH);
DB_CKSUMFLAGS(nhdrmp) = (uint16_t)(ocsum_flags & ~HW_LSO);
if (ocsum_flags & HCK_PARTIALCKSUM)
U16_TO_BE16(tcp_sum, ntcph->th_sum);
if ((ocsum_flags & HCK_TX_FLAGS) &&
(emul & MAC_HWCKSUM_EMULS)) {
next_nhdrmp = nhdrmp->b_next;
nhdrmp->b_next = NULL;
nhdrmp = mac_sw_cksum(nhdrmp, emul);
if (nhdrmp != NULL) {
nhdrmp->b_next = next_nhdrmp;
next_nhdrmp = NULL;
prev_nhdrmp->b_next = nhdrmp;
} else {
freemsgchain(next_nhdrmp);
prev_nhdrmp->b_next = NULL;
freemsgchain(seg_chain);
seg_chain = NULL;
mac_drop_pkt(omp, "LSO cksum emulation failed");
goto fail;
}
}
DTRACE_PROBE5(sw__lso__seg, mblk_t *, nhdrmp, void_ip_t *,
(is_v6 ? (void *)niph6 : (void *)niph),
__dtrace_tcp_tcph_t *, ntcph, uint_t, mss, uint_t, seg);
ASSERT3P(nhdrmp->b_next, !=, NULL);
prev_nhdrmp = nhdrmp;
nhdrmp = nhdrmp->b_next;
}
ASSERT3U(seg, ==, nsegs);
ASSERT3P(nhdrmp->b_next, ==, NULL);
len = msgsize(nhdrmp->b_cont);
ASSERT3S(len, >, 0);
if (is_v6) {
niph6 = (ip6_t *)(nhdrmp->b_rptr + oehlen);
niph6->ip6_plen = htons(
(oiphlen - IPV6_HDR_LEN) + otcphlen + len);
} else {
niph = (ipha_t *)(nhdrmp->b_rptr + oehlen);
niph->ipha_ident = htons(++ip_id);
niph->ipha_length = htons(oiphlen + otcphlen + len);
niph->ipha_hdr_checksum = 0;
}
ntcph = (tcph_t *)(nhdrmp->b_rptr + oehlen + oiphlen);
U32_TO_BE32(tcp_seq, ntcph->th_seq);
DB_CKSUMFLAGS(nhdrmp) = (uint16_t)(ocsum_flags & ~HW_LSO);
if (ocsum_flags & HCK_PARTIALCKSUM) {
tcp_sum = otcp_sum;
tcp_sum += len + otcphlen;
tcp_sum = (tcp_sum >> 16) + (tcp_sum & 0xFFFF);
U16_TO_BE16(tcp_sum, ntcph->th_sum);
}
if ((ocsum_flags & HCK_TX_FLAGS) && (emul & MAC_HWCKSUM_EMULS)) {
ASSERT3P(nhdrmp->b_next, ==, NULL);
nhdrmp = mac_sw_cksum(nhdrmp, emul);
prev_nhdrmp->b_next = nhdrmp;
}
DTRACE_PROBE5(sw__lso__seg, mblk_t *, nhdrmp, void_ip_t *,
(is_v6 ? (void *)niph6 : (void *)niph),
__dtrace_tcp_tcph_t *, ntcph, uint_t, len, uint_t, seg);
freemsg(omp);
*head = seg_chain;
*tail = nhdrmp;
*count = nsegs;
return;
fail:
*head = NULL;
*tail = NULL;
*count = 0;
}
#define HCK_NEEDED (HCK_IPV4_HDRCKSUM | HCK_PARTIALCKSUM | HCK_FULLCKSUM)
void
mac_hw_emul(mblk_t **mp_chain, mblk_t **otail, uint_t *ocount, mac_emul_t emul)
{
mblk_t *head = NULL, *tail = NULL;
uint_t count = 0;
ASSERT3S(~(MAC_HWCKSUM_EMULS | MAC_LSO_EMUL) & emul, ==, 0);
ASSERT3P(mp_chain, !=, NULL);
for (mblk_t *mp = *mp_chain; mp != NULL; ) {
mblk_t *tmp, *next, *tmphead, *tmptail;
struct ether_header *ehp;
uint32_t flags;
uint_t len = MBLKL(mp), l2len;
next = mp->b_next;
mp->b_next = NULL;
if (len < sizeof (struct ether_header)) {
mac_drop_pkt(mp, "packet too short (A): %u", len);
mp = next;
continue;
}
ehp = (struct ether_header *)mp->b_rptr;
if (ntohs(ehp->ether_type) == VLAN_TPID)
l2len = sizeof (struct ether_vlan_header);
else
l2len = sizeof (struct ether_header);
if (len < l2len || (len == l2len && mp->b_cont == NULL)) {
mac_drop_pkt(mp, "packet too short (C): %u", len);
mp = next;
continue;
}
DTRACE_PROBE2(mac__emul, mblk_t *, mp, mac_emul_t, emul);
flags = DB_CKSUMFLAGS(mp);
if ((flags & HW_LSO) && (emul & MAC_LSO_EMUL)) {
uint_t tmpcount = 0;
mac_sw_lso(mp, emul, &tmphead, &tmptail,
&tmpcount);
if (tmphead == NULL) {
mp = next;
continue;
}
count += tmpcount;
} else if ((flags & HCK_NEEDED) && (emul & MAC_HWCKSUM_EMULS)) {
tmp = mac_sw_cksum(mp, emul);
if (tmp == NULL) {
mp = next;
continue;
}
tmphead = tmp;
tmptail = tmp;
count++;
} else {
tmp = mp;
tmphead = tmp;
tmptail = tmp;
count++;
}
if (head == NULL) {
head = tmphead;
tail = tmptail;
} else {
tail->b_next = tmphead;
tail = tmptail;
}
mp = next;
}
*mp_chain = head;
if (otail != NULL)
*otail = tail;
if (ocount != NULL)
*ocount = count;
}
mblk_t *
mac_add_vlan_tag(mblk_t *mp, uint_t pri, uint16_t vid)
{
mblk_t *hmp;
struct ether_vlan_header *evhp;
struct ether_header *ehp;
ASSERT(pri != 0 || vid != 0);
hmp = allocb(sizeof (struct ether_vlan_header), BPRI_MED);
if (hmp == NULL) {
freemsg(mp);
return (NULL);
}
evhp = (struct ether_vlan_header *)hmp->b_rptr;
ehp = (struct ether_header *)mp->b_rptr;
bcopy(ehp, evhp, (ETHERADDRL * 2));
evhp->ether_type = ehp->ether_type;
evhp->ether_tpid = htons(ETHERTYPE_VLAN);
hmp->b_wptr += sizeof (struct ether_vlan_header);
mp->b_rptr += sizeof (struct ether_header);
mac_hcksum_clone(mp, hmp);
if (MBLKL(mp) == 0) {
hmp->b_cont = mp->b_cont;
freeb(mp);
} else {
hmp->b_cont = mp;
}
ASSERT(MBLKL(hmp) >= sizeof (struct ether_vlan_header));
evhp->ether_tci = htons(VLAN_TCI(pri, 0, vid));
return (hmp);
}
mblk_t *
mac_add_vlan_tag_chain(mblk_t *mp_chain, uint_t pri, uint16_t vid)
{
mblk_t *next_mp, **prev, *mp;
mp = mp_chain;
prev = &mp_chain;
while (mp != NULL) {
next_mp = mp->b_next;
mp->b_next = NULL;
if ((mp = mac_add_vlan_tag(mp, pri, vid)) == NULL) {
freemsgchain(next_mp);
break;
}
*prev = mp;
prev = &mp->b_next;
mp = mp->b_next = next_mp;
}
return (mp_chain);
}
mblk_t *
mac_strip_vlan_tag(mblk_t *mp)
{
mblk_t *newmp;
struct ether_vlan_header *evhp;
evhp = (struct ether_vlan_header *)mp->b_rptr;
if (ntohs(evhp->ether_tpid) == ETHERTYPE_VLAN) {
ASSERT(MBLKL(mp) >= sizeof (struct ether_vlan_header));
if (DB_REF(mp) > 1) {
newmp = copymsg(mp);
if (newmp == NULL)
return (NULL);
freemsg(mp);
mp = newmp;
}
evhp = (struct ether_vlan_header *)mp->b_rptr;
ovbcopy(mp->b_rptr, mp->b_rptr + VLAN_TAGSZ, 2 * ETHERADDRL);
mp->b_rptr += VLAN_TAGSZ;
}
return (mp);
}
mblk_t *
mac_strip_vlan_tag_chain(mblk_t *mp_chain)
{
mblk_t *mp, *next_mp, **prev;
mp = mp_chain;
prev = &mp_chain;
while (mp != NULL) {
next_mp = mp->b_next;
mp->b_next = NULL;
if ((mp = mac_strip_vlan_tag(mp)) == NULL) {
freemsgchain(next_mp);
break;
}
*prev = mp;
prev = &mp->b_next;
mp = mp->b_next = next_mp;
}
return (mp_chain);
}
void
mac_rx_def(void *arg, mac_resource_handle_t resource, mblk_t *mp_chain,
boolean_t loopback)
{
freemsgchain(mp_chain);
}
boolean_t
mac_ip_hdr_length_v6(ip6_t *ip6h, uint8_t *endptr, uint16_t *hdr_length,
uint8_t *next_hdr, ip6_frag_t **fragp)
{
uint16_t length;
uint_t ehdrlen;
uint8_t *whereptr;
uint8_t *nexthdrp;
ip6_dest_t *desthdr;
ip6_rthdr_t *rthdr;
ip6_frag_t *fraghdr;
if (((uchar_t *)ip6h + IPV6_HDR_LEN) > endptr)
return (B_FALSE);
ASSERT(IPH_HDR_VERSION(ip6h) == IPV6_VERSION);
length = IPV6_HDR_LEN;
whereptr = ((uint8_t *)&ip6h[1]);
if (fragp != NULL)
*fragp = NULL;
nexthdrp = &ip6h->ip6_nxt;
while (whereptr < endptr) {
if (whereptr + MIN_EHDR_LEN > endptr)
break;
switch (*nexthdrp) {
case IPPROTO_HOPOPTS:
case IPPROTO_DSTOPTS:
desthdr = (ip6_dest_t *)whereptr;
ehdrlen = 8 * (desthdr->ip6d_len + 1);
if ((uchar_t *)desthdr + ehdrlen > endptr)
return (B_FALSE);
nexthdrp = &desthdr->ip6d_nxt;
break;
case IPPROTO_ROUTING:
rthdr = (ip6_rthdr_t *)whereptr;
ehdrlen = 8 * (rthdr->ip6r_len + 1);
if ((uchar_t *)rthdr + ehdrlen > endptr)
return (B_FALSE);
nexthdrp = &rthdr->ip6r_nxt;
break;
case IPPROTO_FRAGMENT:
fraghdr = (ip6_frag_t *)whereptr;
ehdrlen = sizeof (ip6_frag_t);
if ((uchar_t *)&fraghdr[1] > endptr)
return (B_FALSE);
nexthdrp = &fraghdr->ip6f_nxt;
if (fragp != NULL)
*fragp = fraghdr;
break;
case IPPROTO_NONE:
default:
*hdr_length = length;
*next_hdr = *nexthdrp;
return (B_TRUE);
}
length += ehdrlen;
whereptr += ehdrlen;
*hdr_length = length;
*next_hdr = *nexthdrp;
}
switch (*nexthdrp) {
case IPPROTO_HOPOPTS:
case IPPROTO_DSTOPTS:
case IPPROTO_ROUTING:
case IPPROTO_FRAGMENT:
return (B_FALSE);
default:
*hdr_length = length;
*next_hdr = *nexthdrp;
return (B_TRUE);
}
}
typedef struct mac_dladm_intr {
int ino;
int cpu_id;
char driver_path[MAXPATHLEN];
char nexus_path[MAXPATHLEN];
} mac_dladm_intr_t;
static int
mac_set_intr(ldi_handle_t lh, processorid_t cpu_num, int oldcpuid, int ino)
{
pcitool_intr_set_t iset;
int err;
iset.old_cpu = oldcpuid;
iset.ino = ino;
iset.cpu_id = cpu_num;
iset.user_version = PCITOOL_VERSION;
err = ldi_ioctl(lh, PCITOOL_DEVICE_SET_INTR, (intptr_t)&iset, FKIOCTL,
kcred, NULL);
return (err);
}
static boolean_t
mac_search_intrinfo(pcitool_intr_get_t *iget_p, mac_dladm_intr_t *dln)
{
int i;
char driver_path[2 * MAXPATHLEN];
for (i = 0; i < iget_p->num_devs; i++) {
(void) strlcpy(driver_path, iget_p->dev[i].path, MAXPATHLEN);
(void) snprintf(&driver_path[strlen(driver_path)], MAXPATHLEN,
":%s%d", iget_p->dev[i].driver_name,
iget_p->dev[i].dev_inst);
if (strcmp(driver_path, dln->driver_path) == 0) {
dln->ino = iget_p->ino;
dln->cpu_id = iget_p->cpu_id;
return (B_TRUE);
}
}
return (B_FALSE);
}
static boolean_t
mac_get_single_intr(ldi_handle_t lh, int oldcpuid, int ino,
mac_dladm_intr_t *dln)
{
pcitool_intr_get_t *iget_p;
int ipsz;
int nipsz;
int err;
uint8_t inum;
ipsz = PCITOOL_IGET_SIZE(0);
iget_p = kmem_zalloc(ipsz, KM_SLEEP);
iget_p->num_devs_ret = 0;
iget_p->user_version = PCITOOL_VERSION;
iget_p->cpu_id = oldcpuid;
iget_p->ino = ino;
err = ldi_ioctl(lh, PCITOOL_DEVICE_GET_INTR, (intptr_t)iget_p,
FKIOCTL, kcred, NULL);
if (err != 0) {
kmem_free(iget_p, ipsz);
return (B_FALSE);
}
if (iget_p->num_devs == 0) {
kmem_free(iget_p, ipsz);
return (B_FALSE);
}
inum = iget_p->num_devs;
if (iget_p->num_devs_ret < iget_p->num_devs) {
nipsz = PCITOOL_IGET_SIZE(iget_p->num_devs);
kmem_free(iget_p, ipsz);
ipsz = nipsz;
iget_p = kmem_zalloc(ipsz, KM_SLEEP);
iget_p->num_devs_ret = inum;
iget_p->cpu_id = oldcpuid;
iget_p->ino = ino;
iget_p->user_version = PCITOOL_VERSION;
err = ldi_ioctl(lh, PCITOOL_DEVICE_GET_INTR, (intptr_t)iget_p,
FKIOCTL, kcred, NULL);
if (err != 0) {
kmem_free(iget_p, ipsz);
return (B_FALSE);
}
if (iget_p->num_devs != iget_p->num_devs_ret) {
kmem_free(iget_p, ipsz);
return (B_FALSE);
}
}
if (mac_search_intrinfo(iget_p, dln)) {
kmem_free(iget_p, ipsz);
return (B_TRUE);
}
kmem_free(iget_p, ipsz);
return (B_FALSE);
}
static int
mac_validate_intr(ldi_handle_t lh, mac_dladm_intr_t *dln, processorid_t cpuid)
{
pcitool_intr_info_t intr_info;
int err;
int ino;
int oldcpuid;
err = ldi_ioctl(lh, PCITOOL_SYSTEM_INTR_INFO, (intptr_t)&intr_info,
FKIOCTL, kcred, NULL);
if (err != 0)
return (-1);
for (oldcpuid = 0; oldcpuid < intr_info.num_cpu; oldcpuid++) {
for (ino = 0; ino < intr_info.num_intr; ino++) {
if (mac_get_single_intr(lh, oldcpuid, ino, dln)) {
if (dln->cpu_id == cpuid)
return (0);
return (1);
}
}
}
return (-1);
}
static dev_info_t *
mac_get_nexus_node(dev_info_t *mdip, mac_dladm_intr_t *dln)
{
struct dev_info *tdip = (struct dev_info *)mdip;
struct ddi_minor_data *minordata;
dev_info_t *pdip;
char pathname[MAXPATHLEN];
while (tdip != NULL) {
if (ndi_devi_tryenter((dev_info_t *)tdip) == 0)
break;
for (minordata = tdip->devi_minor; minordata != NULL;
minordata = minordata->next) {
if (strncmp(minordata->ddm_node_type, DDI_NT_INTRCTL,
strlen(DDI_NT_INTRCTL)) == 0) {
pdip = minordata->dip;
(void) ddi_pathname(pdip, pathname);
(void) snprintf(dln->nexus_path, MAXPATHLEN,
"/devices%s:intr", pathname);
(void) ddi_pathname_minor(minordata, pathname);
ndi_devi_exit((dev_info_t *)tdip);
return (pdip);
}
}
ndi_devi_exit((dev_info_t *)tdip);
tdip = tdip->devi_parent;
}
return (NULL);
}
static boolean_t
mac_check_interrupt_binding(dev_info_t *mdip, int32_t cpuid)
{
ldi_handle_t lh = NULL;
ldi_ident_t li = NULL;
int err;
int ret;
mac_dladm_intr_t dln;
dev_info_t *dip;
struct ddi_minor_data *minordata;
dln.nexus_path[0] = '\0';
dln.driver_path[0] = '\0';
minordata = ((struct dev_info *)mdip)->devi_minor;
while (minordata != NULL) {
if (minordata->type == DDM_MINOR)
break;
minordata = minordata->next;
}
if (minordata == NULL)
return (B_FALSE);
(void) ddi_pathname_minor(minordata, dln.driver_path);
dip = mac_get_nexus_node(mdip, &dln);
if (dip == NULL)
return (B_FALSE);
err = ldi_ident_from_major(ddi_driver_major(dip), &li);
if (err != 0)
return (B_FALSE);
err = ldi_open_by_name(dln.nexus_path, FREAD|FWRITE, kcred, &lh, li);
if (err != 0)
return (B_FALSE);
ret = mac_validate_intr(lh, &dln, cpuid);
if (ret < 0) {
(void) ldi_close(lh, FREAD|FWRITE, kcred);
return (B_FALSE);
}
if (ret != 0)
if ((err = (mac_set_intr(lh, cpuid, dln.cpu_id, dln.ino)))
!= 0) {
(void) ldi_close(lh, FREAD|FWRITE, kcred);
return (B_FALSE);
}
(void) ldi_close(lh, FREAD|FWRITE, kcred);
return (B_TRUE);
}
void
mac_client_set_intr_cpu(void *arg, mac_client_handle_t mch, int32_t cpuid)
{
dev_info_t *mdip = (dev_info_t *)arg;
mac_client_impl_t *mcip = (mac_client_impl_t *)mch;
mac_resource_props_t *mrp;
mac_perim_handle_t mph;
flow_entry_t *flent = mcip->mci_flent;
mac_soft_ring_set_t *rx_srs;
mac_cpus_t *srs_cpu;
if (!mac_check_interrupt_binding(mdip, cpuid))
cpuid = -1;
mac_perim_enter_by_mh((mac_handle_t)mcip->mci_mip, &mph);
mrp = MCIP_RESOURCE_PROPS(mcip);
mrp->mrp_rx_intr_cpu = cpuid;
if (flent != NULL && flent->fe_rx_srs_cnt == 2) {
rx_srs = flent->fe_rx_srs[1];
srs_cpu = &rx_srs->srs_cpu;
srs_cpu->mc_rx_intr_cpu = cpuid;
}
mac_perim_exit(mph);
}
int32_t
mac_client_intr_cpu(mac_client_handle_t mch)
{
mac_client_impl_t *mcip = (mac_client_impl_t *)mch;
mac_cpus_t *srs_cpu;
mac_soft_ring_set_t *rx_srs;
flow_entry_t *flent = mcip->mci_flent;
mac_resource_props_t *mrp = MCIP_RESOURCE_PROPS(mcip);
mac_ring_t *ring;
mac_intr_t *mintr;
if (mac_is_primary_client(mcip) && flent->fe_rx_srs_cnt == 2) {
rx_srs = flent->fe_rx_srs[1];
srs_cpu = &rx_srs->srs_cpu;
ring = rx_srs->srs_ring;
mintr = &ring->mr_info.mri_intr;
if (mintr->mi_ddi_handle != NULL ||
((mrp->mrp_ncpus != 0) &&
(mrp->mrp_rx_intr_cpu == srs_cpu->mc_rx_pollid))) {
return (-1);
}
return (srs_cpu->mc_rx_pollid);
}
return (-1);
}
void *
mac_get_devinfo(mac_handle_t mh)
{
mac_impl_t *mip = (mac_impl_t *)mh;
return ((void *)mip->mi_dip);
}
#define PKT_HASH_2BYTES(x) ((x)[0] ^ (x)[1])
#define PKT_HASH_4BYTES(x) ((x)[0] ^ (x)[1] ^ (x)[2] ^ (x)[3])
#define PKT_HASH_MAC(x) ((x)[0] ^ (x)[1] ^ (x)[2] ^ (x)[3] ^ (x)[4] ^ (x)[5])
uint64_t
mac_pkt_hash(uint_t media, mblk_t *mp, uint8_t policy, boolean_t is_outbound)
{
struct ether_header *ehp;
uint64_t hash = 0;
uint16_t sap;
uint_t skip_len;
uint8_t proto;
boolean_t ip_fragmented;
if (media != DL_ETHER)
return (0L);
ASSERT(is_outbound);
ASSERT(IS_P2ALIGNED(mp->b_rptr, sizeof (uint16_t)));
ASSERT(MBLKL(mp) >= sizeof (struct ether_header));
ehp = (struct ether_header *)mp->b_rptr;
if ((policy & MAC_PKT_HASH_L2) != 0) {
uchar_t *mac_src = ehp->ether_shost.ether_addr_octet;
uchar_t *mac_dst = ehp->ether_dhost.ether_addr_octet;
hash = PKT_HASH_MAC(mac_src) ^ PKT_HASH_MAC(mac_dst);
policy &= ~MAC_PKT_HASH_L2;
}
if (policy == 0)
goto done;
sap = ntohs(ehp->ether_type);
if (sap == ETHERTYPE_VLAN) {
struct ether_vlan_header *evhp;
mblk_t *newmp = NULL;
skip_len = sizeof (struct ether_vlan_header);
if (MBLKL(mp) < skip_len) {
newmp = msgpullup(mp, -1);
if ((newmp == NULL) || (MBLKL(newmp) < skip_len)) {
goto done;
}
evhp = (struct ether_vlan_header *)newmp->b_rptr;
} else {
evhp = (struct ether_vlan_header *)mp->b_rptr;
}
sap = ntohs(evhp->ether_type);
freemsg(newmp);
} else {
skip_len = sizeof (struct ether_header);
}
if (MBLKL(mp) <= skip_len) {
skip_len -= MBLKL(mp);
mp = mp->b_cont;
if (mp == NULL)
goto done;
}
sap = (sap < ETHERTYPE_802_MIN) ? 0 : sap;
switch (sap) {
case ETHERTYPE_IP: {
ipha_t *iphp;
iphp = (ipha_t *)(mp->b_rptr + skip_len);
if (((unsigned char *)iphp + sizeof (ipha_t) > mp->b_wptr) ||
!OK_32PTR((char *)iphp))
goto done;
proto = iphp->ipha_protocol;
skip_len += IPH_HDR_LENGTH(iphp);
ip_fragmented = ntohs(iphp->ipha_fragment_offset_and_flags) &
IPH_OFFSET;
if (ip_fragmented || (policy & MAC_PKT_HASH_L3) != 0) {
uint8_t *ip_src = (uint8_t *)&(iphp->ipha_src);
uint8_t *ip_dst = (uint8_t *)&(iphp->ipha_dst);
hash ^= (PKT_HASH_4BYTES(ip_src) ^
PKT_HASH_4BYTES(ip_dst));
policy &= ~MAC_PKT_HASH_L3;
}
if (ip_fragmented) {
uint8_t *identp = (uint8_t *)&iphp->ipha_ident;
hash ^= PKT_HASH_2BYTES(identp);
goto done;
}
break;
}
case ETHERTYPE_IPV6: {
ip6_t *ip6hp;
ip6_frag_t *frag = NULL;
uint16_t hdr_length;
ip6hp = (ip6_t *)(mp->b_rptr + skip_len);
if (((unsigned char *)ip6hp + IPV6_HDR_LEN > mp->b_wptr) ||
!OK_32PTR((char *)ip6hp))
goto done;
if (!mac_ip_hdr_length_v6(ip6hp, mp->b_wptr, &hdr_length,
&proto, &frag))
goto done;
skip_len += hdr_length;
if (frag != NULL || (policy & MAC_PKT_HASH_L3) != 0) {
uint8_t *ip_src = &(ip6hp->ip6_src.s6_addr8[12]);
uint8_t *ip_dst = &(ip6hp->ip6_dst.s6_addr8[12]);
hash ^= (PKT_HASH_4BYTES(ip_src) ^
PKT_HASH_4BYTES(ip_dst));
policy &= ~MAC_PKT_HASH_L3;
}
if (frag != NULL) {
uint8_t *identp = (uint8_t *)&frag->ip6f_ident;
hash ^= PKT_HASH_4BYTES(identp);
goto done;
}
break;
}
default:
goto done;
}
if (policy == 0)
goto done;
if (MBLKL(mp) <= skip_len) {
skip_len -= MBLKL(mp);
mp = mp->b_cont;
if (mp == NULL)
goto done;
}
again:
switch (proto) {
case IPPROTO_TCP:
case IPPROTO_UDP:
case IPPROTO_ESP:
case IPPROTO_SCTP:
if (mp->b_rptr + skip_len + 4 > mp->b_wptr)
goto done;
hash ^= PKT_HASH_4BYTES((mp->b_rptr + skip_len));
break;
case IPPROTO_AH: {
ah_t *ah = (ah_t *)(mp->b_rptr + skip_len);
uint_t ah_length = AH_TOTAL_LEN(ah);
if ((unsigned char *)ah + sizeof (ah_t) > mp->b_wptr)
goto done;
proto = ah->ah_nexthdr;
skip_len += ah_length;
if (MBLKL(mp) <= skip_len) {
skip_len -= MBLKL(mp);
mp = mp->b_cont;
if (mp == NULL)
goto done;
}
goto again;
}
}
done:
return (hash);
}