#include "opt_inet.h"
#include "opt_inet6.h"
#include <sys/param.h>
#include <sys/systm.h>
#include <sys/kernel.h>
#include <sys/malloc.h>
#include <sys/mbuf.h>
#include <sys/socket.h>
#include <sys/socketvar.h>
#include <sys/sockbuf.h>
#include <sys/sysctl.h>
#include <net/if.h>
#include <net/if_var.h>
#include <net/ethernet.h>
#include <net/bpf.h>
#include <net/vnet.h>
#include <net/if_dl.h>
#include <net/if_media.h>
#include <net/if_private.h>
#include <net/if_types.h>
#include <net/infiniband.h>
#include <net/if_lagg.h>
#include <netinet/in_systm.h>
#include <netinet/in.h>
#include <netinet/ip6.h>
#include <netinet/ip.h>
#include <netinet/ip_var.h>
#include <netinet/in_pcb.h>
#include <netinet6/in6_pcb.h>
#include <netinet/tcp.h>
#include <netinet/tcp_seq.h>
#include <netinet/tcp_lro.h>
#include <netinet/tcp_var.h>
#include <netinet/tcpip.h>
#include <netinet/tcp_hpts.h>
#include <netinet/tcp_log_buf.h>
#include <netinet/tcp_fsm.h>
#include <netinet/udp.h>
#include <netinet6/ip6_var.h>
#include <machine/in_cksum.h>
static MALLOC_DEFINE(M_LRO, "LRO", "LRO control structures");
static void tcp_lro_rx_done(struct lro_ctrl *lc);
static int tcp_lro_rx_common(struct lro_ctrl *lc, struct mbuf *m,
uint32_t csum, bool use_hash);
static void tcp_lro_flush(struct lro_ctrl *lc, struct lro_entry *le);
SYSCTL_NODE(_net_inet_tcp, OID_AUTO, lro, CTLFLAG_RW | CTLFLAG_MPSAFE, 0,
"TCP LRO");
long tcplro_stacks_wanting_mbufq;
int (*tcp_lro_flush_tcphpts)(struct lro_ctrl *lc, struct lro_entry *le);
void (*tcp_hpts_softclock)(void);
counter_u64_t tcp_inp_lro_direct_queue;
counter_u64_t tcp_inp_lro_wokeup_queue;
counter_u64_t tcp_inp_lro_compressed;
counter_u64_t tcp_inp_lro_locks_taken;
counter_u64_t tcp_extra_mbuf;
counter_u64_t tcp_would_have_but;
counter_u64_t tcp_comp_total;
counter_u64_t tcp_uncomp_total;
counter_u64_t tcp_bad_csums;
static unsigned tcp_lro_entries = TCP_LRO_ENTRIES;
SYSCTL_UINT(_net_inet_tcp_lro, OID_AUTO, entries,
CTLFLAG_RDTUN | CTLFLAG_MPSAFE, &tcp_lro_entries, 0,
"default number of LRO entries");
static uint32_t tcp_lro_cpu_set_thresh = TCP_LRO_CPU_DECLARATION_THRESH;
SYSCTL_UINT(_net_inet_tcp_lro, OID_AUTO, lro_cpu_threshold,
CTLFLAG_RDTUN | CTLFLAG_MPSAFE, &tcp_lro_cpu_set_thresh, 0,
"Number of interrupts in a row on the same CPU that will make us declare an 'affinity' cpu?");
static uint32_t tcp_less_accurate_lro_ts = 0;
SYSCTL_UINT(_net_inet_tcp_lro, OID_AUTO, lro_less_accurate,
CTLFLAG_MPSAFE, &tcp_less_accurate_lro_ts, 0,
"Do we trade off efficency by doing less timestamp operations for time accuracy?");
SYSCTL_COUNTER_U64(_net_inet_tcp_lro, OID_AUTO, fullqueue, CTLFLAG_RD,
&tcp_inp_lro_direct_queue, "Number of lro's fully queued to transport");
SYSCTL_COUNTER_U64(_net_inet_tcp_lro, OID_AUTO, wokeup, CTLFLAG_RD,
&tcp_inp_lro_wokeup_queue, "Number of lro's where we woke up transport via hpts");
SYSCTL_COUNTER_U64(_net_inet_tcp_lro, OID_AUTO, compressed, CTLFLAG_RD,
&tcp_inp_lro_compressed, "Number of lro's compressed and sent to transport");
SYSCTL_COUNTER_U64(_net_inet_tcp_lro, OID_AUTO, lockcnt, CTLFLAG_RD,
&tcp_inp_lro_locks_taken, "Number of lro's inp_wlocks taken");
SYSCTL_COUNTER_U64(_net_inet_tcp_lro, OID_AUTO, extra_mbuf, CTLFLAG_RD,
&tcp_extra_mbuf, "Number of times we had an extra compressed ack dropped into the tp");
SYSCTL_COUNTER_U64(_net_inet_tcp_lro, OID_AUTO, would_have_but, CTLFLAG_RD,
&tcp_would_have_but, "Number of times we would have had an extra compressed, but mget failed");
SYSCTL_COUNTER_U64(_net_inet_tcp_lro, OID_AUTO, with_m_ackcmp, CTLFLAG_RD,
&tcp_comp_total, "Number of mbufs queued with M_ACKCMP flags set");
SYSCTL_COUNTER_U64(_net_inet_tcp_lro, OID_AUTO, without_m_ackcmp, CTLFLAG_RD,
&tcp_uncomp_total, "Number of mbufs queued without M_ACKCMP");
SYSCTL_COUNTER_U64(_net_inet_tcp_lro, OID_AUTO, lro_badcsum, CTLFLAG_RD,
&tcp_bad_csums, "Number of packets that the common code saw with bad csums");
void
tcp_lro_reg_mbufq(void)
{
atomic_fetchadd_long(&tcplro_stacks_wanting_mbufq, 1);
}
void
tcp_lro_dereg_mbufq(void)
{
atomic_fetchadd_long(&tcplro_stacks_wanting_mbufq, -1);
}
static __inline void
tcp_lro_active_insert(struct lro_ctrl *lc, struct lro_head *bucket,
struct lro_entry *le)
{
LIST_INSERT_HEAD(&lc->lro_active, le, next);
LIST_INSERT_HEAD(bucket, le, hash_next);
}
static __inline void
tcp_lro_active_remove(struct lro_entry *le)
{
LIST_REMOVE(le, next);
LIST_REMOVE(le, hash_next);
}
int
tcp_lro_init(struct lro_ctrl *lc)
{
return (tcp_lro_init_args(lc, NULL, tcp_lro_entries, 0));
}
int
tcp_lro_init_args(struct lro_ctrl *lc, struct ifnet *ifp,
unsigned lro_entries, unsigned lro_mbufs)
{
struct lro_entry *le;
size_t size;
unsigned i;
lc->lro_bad_csum = 0;
lc->lro_queued = 0;
lc->lro_flushed = 0;
lc->lro_mbuf_count = 0;
lc->lro_mbuf_max = lro_mbufs;
lc->lro_cnt = lro_entries;
lc->lro_ackcnt_lim = TCP_LRO_ACKCNT_MAX;
lc->lro_length_lim = TCP_LRO_LENGTH_MAX;
lc->ifp = ifp;
LIST_INIT(&lc->lro_free);
LIST_INIT(&lc->lro_active);
lc->lro_hash = phashinit_flags(lro_entries, M_LRO, &lc->lro_hashsz,
HASH_NOWAIT);
if (lc->lro_hash == NULL) {
memset(lc, 0, sizeof(*lc));
return (ENOMEM);
}
size = (lro_mbufs * sizeof(struct lro_mbuf_sort)) +
(lro_entries * sizeof(*le));
lc->lro_mbuf_data = (struct lro_mbuf_sort *)
malloc(size, M_LRO, M_NOWAIT | M_ZERO);
if (lc->lro_mbuf_data == NULL) {
free(lc->lro_hash, M_LRO);
memset(lc, 0, sizeof(*lc));
return (ENOMEM);
}
le = (struct lro_entry *)
(lc->lro_mbuf_data + lro_mbufs);
for (i = 0; i != lro_entries; i++)
LIST_INSERT_HEAD(&lc->lro_free, le + i, next);
return (0);
}
struct vxlan_header {
uint32_t vxlh_flags;
uint32_t vxlh_vni;
};
static inline void *
tcp_lro_low_level_parser(void *ptr, struct lro_parser *parser, bool update_data, bool is_vxlan, int mlen)
{
const struct ether_vlan_header *eh;
void *old;
uint16_t eth_type;
if (update_data)
memset(parser, 0, sizeof(*parser));
old = ptr;
if (is_vxlan) {
const struct vxlan_header *vxh;
vxh = ptr;
ptr = (uint8_t *)ptr + sizeof(*vxh);
if (update_data) {
parser->data.vxlan_vni =
vxh->vxlh_vni & htonl(0xffffff00);
}
}
eh = ptr;
if (__predict_false(eh->evl_encap_proto == htons(ETHERTYPE_VLAN))) {
eth_type = eh->evl_proto;
if (update_data) {
parser->data.vlan_id = eh->evl_tag & htons(EVL_VLID_MASK);
}
ptr = (uint8_t *)ptr + ETHER_HDR_LEN + ETHER_VLAN_ENCAP_LEN;
mlen -= (ETHER_HDR_LEN + ETHER_VLAN_ENCAP_LEN);
} else {
eth_type = eh->evl_encap_proto;
mlen -= ETHER_HDR_LEN;
ptr = (uint8_t *)ptr + ETHER_HDR_LEN;
}
if (__predict_false(mlen <= 0))
return (NULL);
switch (eth_type) {
#ifdef INET
case htons(ETHERTYPE_IP):
parser->ip4 = ptr;
if (__predict_false(mlen < sizeof(struct ip)))
return (NULL);
if ((parser->ip4->ip_hl << 2) != sizeof (*parser->ip4))
break;
if (parser->ip4->ip_off & htons(IP_MF|IP_OFFMASK))
break;
if (__predict_false(parser->ip4->ip_src.s_addr == INADDR_ANY ||
parser->ip4->ip_dst.s_addr == INADDR_ANY))
break;
ptr = (uint8_t *)ptr + (parser->ip4->ip_hl << 2);
mlen -= sizeof(struct ip);
if (update_data) {
parser->data.s_addr.v4 = parser->ip4->ip_src;
parser->data.d_addr.v4 = parser->ip4->ip_dst;
}
switch (parser->ip4->ip_p) {
case IPPROTO_UDP:
if (__predict_false(mlen < sizeof(struct udphdr)))
return (NULL);
parser->udp = ptr;
if (update_data) {
parser->data.lro_type = LRO_TYPE_IPV4_UDP;
parser->data.s_port = parser->udp->uh_sport;
parser->data.d_port = parser->udp->uh_dport;
} else {
MPASS(parser->data.lro_type == LRO_TYPE_IPV4_UDP);
}
ptr = ((uint8_t *)ptr + sizeof(*parser->udp));
parser->total_hdr_len = (uint8_t *)ptr - (uint8_t *)old;
return (ptr);
case IPPROTO_TCP:
parser->tcp = ptr;
if (__predict_false(mlen < sizeof(struct tcphdr)))
return (NULL);
if (update_data) {
parser->data.lro_type = LRO_TYPE_IPV4_TCP;
parser->data.s_port = parser->tcp->th_sport;
parser->data.d_port = parser->tcp->th_dport;
} else {
MPASS(parser->data.lro_type == LRO_TYPE_IPV4_TCP);
}
if (__predict_false(mlen < (parser->tcp->th_off << 2)))
return (NULL);
ptr = (uint8_t *)ptr + (parser->tcp->th_off << 2);
parser->total_hdr_len = (uint8_t *)ptr - (uint8_t *)old;
return (ptr);
default:
break;
}
break;
#endif
#ifdef INET6
case htons(ETHERTYPE_IPV6):
parser->ip6 = ptr;
if (__predict_false(mlen < sizeof(struct ip6_hdr)))
return (NULL);
if (__predict_false(IN6_IS_ADDR_UNSPECIFIED(&parser->ip6->ip6_src) ||
IN6_IS_ADDR_UNSPECIFIED(&parser->ip6->ip6_dst)))
return (NULL);
ptr = (uint8_t *)ptr + sizeof(*parser->ip6);
if (update_data) {
parser->data.s_addr.v6 = parser->ip6->ip6_src;
parser->data.d_addr.v6 = parser->ip6->ip6_dst;
}
mlen -= sizeof(struct ip6_hdr);
switch (parser->ip6->ip6_nxt) {
case IPPROTO_UDP:
if (__predict_false(mlen < sizeof(struct udphdr)))
return (NULL);
parser->udp = ptr;
if (update_data) {
parser->data.lro_type = LRO_TYPE_IPV6_UDP;
parser->data.s_port = parser->udp->uh_sport;
parser->data.d_port = parser->udp->uh_dport;
} else {
MPASS(parser->data.lro_type == LRO_TYPE_IPV6_UDP);
}
ptr = (uint8_t *)ptr + sizeof(*parser->udp);
parser->total_hdr_len = (uint8_t *)ptr - (uint8_t *)old;
return (ptr);
case IPPROTO_TCP:
if (__predict_false(mlen < sizeof(struct tcphdr)))
return (NULL);
parser->tcp = ptr;
if (update_data) {
parser->data.lro_type = LRO_TYPE_IPV6_TCP;
parser->data.s_port = parser->tcp->th_sport;
parser->data.d_port = parser->tcp->th_dport;
} else {
MPASS(parser->data.lro_type == LRO_TYPE_IPV6_TCP);
}
if (__predict_false(mlen < (parser->tcp->th_off << 2)))
return (NULL);
ptr = (uint8_t *)ptr + (parser->tcp->th_off << 2);
parser->total_hdr_len = (uint8_t *)ptr - (uint8_t *)old;
return (ptr);
default:
break;
}
break;
#endif
default:
break;
}
return (NULL);
}
static const int vxlan_csum = CSUM_INNER_L3_CALC | CSUM_INNER_L3_VALID |
CSUM_INNER_L4_CALC | CSUM_INNER_L4_VALID;
static inline struct lro_parser *
tcp_lro_parser(struct mbuf *m, struct lro_parser *po, struct lro_parser *pi, bool update_data)
{
void *data_ptr;
data_ptr = tcp_lro_low_level_parser(m->m_data, po, update_data, false, m->m_len);
if (data_ptr == NULL || po->total_hdr_len > m->m_len)
return (NULL);
if (update_data) {
if (__predict_false(m->m_flags & M_VLANTAG)) {
po->data.vlan_id =
htons(m->m_pkthdr.ether_vtag) & htons(EVL_VLID_MASK);
}
if (__predict_false((m->m_pkthdr.csum_flags &
CSUM_TLS_MASK) == CSUM_TLS_DECRYPTED))
po->data.lro_flags |= LRO_FLAG_DECRYPTED;
}
switch (po->data.lro_type) {
case LRO_TYPE_IPV4_UDP:
case LRO_TYPE_IPV6_UDP:
if ((m->m_pkthdr.csum_flags & vxlan_csum) != vxlan_csum)
break;
data_ptr = tcp_lro_low_level_parser(data_ptr, pi, update_data, true,
(m->m_len - ((caddr_t)data_ptr - m->m_data)));
if (data_ptr == NULL || (pi->total_hdr_len + po->total_hdr_len) > m->m_len)
break;
switch (pi->data.lro_type) {
case LRO_TYPE_IPV4_TCP:
case LRO_TYPE_IPV6_TCP:
return (pi);
default:
break;
}
break;
case LRO_TYPE_IPV4_TCP:
case LRO_TYPE_IPV6_TCP:
if (update_data)
memset(pi, 0, sizeof(*pi));
return (po);
default:
break;
}
return (NULL);
}
static inline int
tcp_lro_trim_mbuf_chain(struct mbuf *m, const struct lro_parser *po)
{
int len;
switch (po->data.lro_type) {
#ifdef INET
case LRO_TYPE_IPV4_TCP:
len = ((uint8_t *)po->ip4 - (uint8_t *)m->m_data) +
ntohs(po->ip4->ip_len);
break;
#endif
#ifdef INET6
case LRO_TYPE_IPV6_TCP:
len = ((uint8_t *)po->ip6 - (uint8_t *)m->m_data) +
ntohs(po->ip6->ip6_plen) + sizeof(*po->ip6);
break;
#endif
default:
return (TCP_LRO_CANNOT);
}
if (__predict_true(m->m_pkthdr.len == len)) {
return (0);
} else if (m->m_pkthdr.len > len) {
m_adj(m, len - m->m_pkthdr.len);
return (0);
}
return (TCP_LRO_CANNOT);
}
static void
lro_free_mbuf_chain(struct mbuf *m)
{
struct mbuf *save;
while (m) {
save = m->m_nextpkt;
m->m_nextpkt = NULL;
m_freem(m);
m = save;
}
}
void
tcp_lro_free(struct lro_ctrl *lc)
{
struct lro_entry *le;
unsigned x;
LIST_INIT(&lc->lro_free);
while ((le = LIST_FIRST(&lc->lro_active)) != NULL) {
tcp_lro_active_remove(le);
lro_free_mbuf_chain(le->m_head);
}
free(lc->lro_hash, M_LRO);
lc->lro_hash = NULL;
lc->lro_hashsz = 0;
for (x = 0; x != lc->lro_mbuf_count; x++)
m_freem(lc->lro_mbuf_data[x].mb);
lc->lro_mbuf_count = 0;
free(lc->lro_mbuf_data, M_LRO);
lc->lro_mbuf_data = NULL;
}
static uint16_t
tcp_lro_rx_csum_tcphdr(const struct tcphdr *th)
{
const uint16_t *ptr;
uint32_t csum;
uint16_t len;
csum = -th->th_sum;
len = th->th_off;
ptr = (const uint16_t *)th;
while (len--) {
csum += *ptr;
ptr++;
csum += *ptr;
ptr++;
}
while (csum > 0xffff)
csum = (csum >> 16) + (csum & 0xffff);
return (csum);
}
static uint16_t
tcp_lro_rx_csum_data(const struct lro_parser *pa, uint16_t tcp_csum)
{
uint32_t c;
uint16_t cs;
c = tcp_csum;
switch (pa->data.lro_type) {
#ifdef INET6
case LRO_TYPE_IPV6_TCP:
cs = in6_cksum_pseudo(pa->ip6, ntohs(pa->ip6->ip6_plen), pa->ip6->ip6_nxt, 0);
break;
#endif
#ifdef INET
case LRO_TYPE_IPV4_TCP:
cs = in_addword(ntohs(pa->ip4->ip_len) - sizeof(*pa->ip4), IPPROTO_TCP);
cs = in_pseudo(pa->ip4->ip_src.s_addr, pa->ip4->ip_dst.s_addr, htons(cs));
break;
#endif
default:
cs = 0;
break;
}
cs = ~cs;
c += cs;
cs = ~tcp_lro_rx_csum_tcphdr(pa->tcp);
c += cs;
while (c > 0xffff)
c = (c >> 16) + (c & 0xffff);
return (c);
}
static void
tcp_lro_rx_done(struct lro_ctrl *lc)
{
struct lro_entry *le;
while ((le = LIST_FIRST(&lc->lro_active)) != NULL) {
tcp_lro_active_remove(le);
tcp_lro_flush(lc, le);
}
}
static void
tcp_lro_flush_active(struct lro_ctrl *lc)
{
struct lro_entry *le, *le_tmp;
LIST_FOREACH_SAFE(le, &lc->lro_active, next, le_tmp) {
if (le->m_head != NULL) {
tcp_lro_active_remove(le);
tcp_lro_flush(lc, le);
}
}
}
void
tcp_lro_flush_inactive(struct lro_ctrl *lc, const struct timeval *timeout)
{
struct lro_entry *le, *le_tmp;
uint64_t now, tov;
struct bintime bt;
NET_EPOCH_ASSERT();
if (LIST_EMPTY(&lc->lro_active))
return;
binuptime(&bt);
now = bintime2ns(&bt);
tov = ((timeout->tv_sec * 1000000000) + (timeout->tv_usec * 1000));
LIST_FOREACH_SAFE(le, &lc->lro_active, next, le_tmp) {
if (now >= (bintime2ns(&le->alloc_time) + tov)) {
tcp_lro_active_remove(le);
tcp_lro_flush(lc, le);
}
}
}
#ifdef INET
static int
tcp_lro_rx_ipv4(struct lro_ctrl *lc, struct mbuf *m, struct ip *ip4)
{
uint16_t csum;
if (m->m_pkthdr.csum_flags & CSUM_IP_CHECKED) {
if (__predict_false((m->m_pkthdr.csum_flags & CSUM_IP_VALID) == 0)) {
lc->lro_bad_csum++;
return (TCP_LRO_CANNOT);
}
} else {
csum = in_cksum_hdr(ip4);
if (__predict_false(csum != 0)) {
lc->lro_bad_csum++;
return (TCP_LRO_CANNOT);
}
}
return (0);
}
#endif
static inline void
tcp_lro_assign_and_checksum_16(uint16_t *ptr, uint16_t value, uint16_t *psum)
{
uint32_t csum;
csum = 0xffff - *ptr + value;
while (csum > 0xffff)
csum = (csum >> 16) + (csum & 0xffff);
*ptr = value;
*psum = csum;
}
static uint16_t
tcp_lro_update_checksum(const struct lro_parser *pa, const struct lro_entry *le,
uint16_t payload_len, uint16_t delta_sum)
{
uint32_t csum;
uint16_t tlen;
uint16_t temp[5] = {};
switch (pa->data.lro_type) {
case LRO_TYPE_IPV4_TCP:
tlen = (pa->ip4->ip_hl << 2) + (pa->tcp->th_off << 2) + payload_len;
tcp_lro_assign_and_checksum_16(&pa->ip4->ip_len, htons(tlen), &temp[0]);
csum = pa->ip4->ip_sum + 0xffff - temp[0];
while (csum > 0xffff)
csum = (csum >> 16) + (csum & 0xffff);
tcp_lro_assign_and_checksum_16(&pa->ip4->ip_sum, csum, &temp[1]);
goto update_tcp_header;
case LRO_TYPE_IPV6_TCP:
tlen = (pa->tcp->th_off << 2) + payload_len;
tcp_lro_assign_and_checksum_16(&pa->ip6->ip6_plen, htons(tlen), &temp[0]);
goto update_tcp_header;
case LRO_TYPE_IPV4_UDP:
tlen = (pa->ip4->ip_hl << 2) + sizeof(*pa->udp) + payload_len;
tcp_lro_assign_and_checksum_16(&pa->ip4->ip_len, htons(tlen), &temp[0]);
csum = pa->ip4->ip_sum + 0xffff - temp[0];
while (csum > 0xffff)
csum = (csum >> 16) + (csum & 0xffff);
tcp_lro_assign_and_checksum_16(&pa->ip4->ip_sum, csum, &temp[1]);
goto update_udp_header;
case LRO_TYPE_IPV6_UDP:
tlen = sizeof(*pa->udp) + payload_len;
tcp_lro_assign_and_checksum_16(&pa->ip6->ip6_plen, htons(tlen), &temp[0]);
goto update_udp_header;
default:
return (0);
}
update_tcp_header:
temp[2] = tcp_lro_rx_csum_tcphdr(pa->tcp);
pa->tcp->th_ack = le->ack_seq;
pa->tcp->th_win = le->window;
if (le->timestamp != 0) {
uint32_t *ts_ptr;
ts_ptr = (uint32_t *)(pa->tcp + 1);
ts_ptr[1] = htonl(le->tsval);
ts_ptr[2] = le->tsecr;
}
temp[3] = tcp_lro_rx_csum_tcphdr(pa->tcp);
csum = pa->tcp->th_sum + 0xffff - delta_sum +
0xffff - temp[0] + 0xffff - temp[3] + temp[2];
while (csum > 0xffff)
csum = (csum >> 16) + (csum & 0xffff);
tcp_lro_assign_and_checksum_16(&pa->tcp->th_sum, csum, &temp[4]);
csum = temp[0] + temp[1] + 0xffff - temp[2] +
temp[3] + temp[4] + delta_sum;
while (csum > 0xffff)
csum = (csum >> 16) + (csum & 0xffff);
return (csum);
update_udp_header:
tlen = sizeof(*pa->udp) + payload_len;
tcp_lro_assign_and_checksum_16(&pa->udp->uh_ulen, htons(tlen), &temp[2]);
if (__predict_false(pa->udp->uh_sum != 0)) {
csum = pa->udp->uh_sum + 0xffff - delta_sum +
0xffff - temp[0] + 0xffff - temp[2];
while (csum > 0xffff)
csum = (csum >> 16) + (csum & 0xffff);
tcp_lro_assign_and_checksum_16(&pa->udp->uh_sum, csum, &temp[3]);
}
csum = temp[0] + temp[1] + temp[2] + temp[3] + delta_sum;
while (csum > 0xffff)
csum = (csum >> 16) + (csum & 0xffff);
return (csum);
}
static void
tcp_flush_out_entry(struct lro_ctrl *lc, struct lro_entry *le)
{
if (le->needs_merge) {
uint16_t csum;
switch (le->inner.data.lro_type) {
case LRO_TYPE_IPV4_TCP:
csum = tcp_lro_update_checksum(&le->inner, le,
le->m_head->m_pkthdr.lro_tcp_d_len,
le->m_head->m_pkthdr.lro_tcp_d_csum);
csum = tcp_lro_update_checksum(&le->outer, NULL,
le->m_head->m_pkthdr.lro_tcp_d_len +
le->inner.total_hdr_len, csum);
le->m_head->m_pkthdr.csum_flags = CSUM_DATA_VALID |
CSUM_PSEUDO_HDR | CSUM_IP_CHECKED | CSUM_IP_VALID;
le->m_head->m_pkthdr.csum_data = 0xffff;
if (__predict_false(le->outer.data.lro_flags & LRO_FLAG_DECRYPTED))
le->m_head->m_pkthdr.csum_flags |= CSUM_TLS_DECRYPTED;
break;
case LRO_TYPE_IPV6_TCP:
csum = tcp_lro_update_checksum(&le->inner, le,
le->m_head->m_pkthdr.lro_tcp_d_len,
le->m_head->m_pkthdr.lro_tcp_d_csum);
csum = tcp_lro_update_checksum(&le->outer, NULL,
le->m_head->m_pkthdr.lro_tcp_d_len +
le->inner.total_hdr_len, csum);
le->m_head->m_pkthdr.csum_flags = CSUM_DATA_VALID |
CSUM_PSEUDO_HDR;
le->m_head->m_pkthdr.csum_data = 0xffff;
if (__predict_false(le->outer.data.lro_flags & LRO_FLAG_DECRYPTED))
le->m_head->m_pkthdr.csum_flags |= CSUM_TLS_DECRYPTED;
break;
case LRO_TYPE_NONE:
switch (le->outer.data.lro_type) {
case LRO_TYPE_IPV4_TCP:
csum = tcp_lro_update_checksum(&le->outer, le,
le->m_head->m_pkthdr.lro_tcp_d_len,
le->m_head->m_pkthdr.lro_tcp_d_csum);
le->m_head->m_pkthdr.csum_flags = CSUM_DATA_VALID |
CSUM_PSEUDO_HDR | CSUM_IP_CHECKED | CSUM_IP_VALID;
le->m_head->m_pkthdr.csum_data = 0xffff;
if (__predict_false(le->outer.data.lro_flags & LRO_FLAG_DECRYPTED))
le->m_head->m_pkthdr.csum_flags |= CSUM_TLS_DECRYPTED;
break;
case LRO_TYPE_IPV6_TCP:
csum = tcp_lro_update_checksum(&le->outer, le,
le->m_head->m_pkthdr.lro_tcp_d_len,
le->m_head->m_pkthdr.lro_tcp_d_csum);
le->m_head->m_pkthdr.csum_flags = CSUM_DATA_VALID |
CSUM_PSEUDO_HDR;
le->m_head->m_pkthdr.csum_data = 0xffff;
if (__predict_false(le->outer.data.lro_flags & LRO_FLAG_DECRYPTED))
le->m_head->m_pkthdr.csum_flags |= CSUM_TLS_DECRYPTED;
break;
default:
break;
}
break;
default:
break;
}
}
le->m_head->m_nextpkt = NULL;
lc->lro_queued += le->m_head->m_pkthdr.lro_nsegs;
(*lc->ifp->if_input)(lc->ifp, le->m_head);
}
static void
tcp_set_entry_to_mbuf(struct lro_ctrl *lc, struct lro_entry *le,
struct mbuf *m, struct tcphdr *th)
{
uint32_t *ts_ptr;
uint16_t tcp_data_len;
uint16_t tcp_opt_len;
ts_ptr = (uint32_t *)(th + 1);
tcp_opt_len = (th->th_off << 2);
tcp_opt_len -= sizeof(*th);
if (tcp_opt_len == 0 ||
__predict_false(tcp_opt_len != TCPOLEN_TSTAMP_APPA ||
*ts_ptr != TCP_LRO_TS_OPTION)) {
le->timestamp = 0;
} else {
le->timestamp = 1;
le->tsval = ntohl(*(ts_ptr + 1));
le->tsecr = *(ts_ptr + 2);
}
tcp_data_len = m->m_pkthdr.lro_tcp_d_len;
le->next_seq = ntohl(th->th_seq) + tcp_data_len;
le->ack_seq = th->th_ack;
le->window = th->th_win;
le->flags = tcp_get_flags(th);
le->needs_merge = 0;
le->m_head = m;
le->m_tail = m_last(m);
}
static void
tcp_push_and_replace(struct lro_ctrl *lc, struct lro_entry *le, struct mbuf *m)
{
struct lro_parser *pa;
struct mbuf *msave;
msave = le->m_head->m_nextpkt;
le->m_head->m_nextpkt = NULL;
tcp_flush_out_entry(lc, le);
pa = tcp_lro_parser(m, &le->outer, &le->inner, false);
KASSERT(pa != NULL,
("tcp_push_and_replace: LRO parser failed on m=%p\n", m));
tcp_set_entry_to_mbuf(lc, le, m, pa->tcp);
m->m_nextpkt = msave;
}
static void
tcp_lro_mbuf_append_pkthdr(struct lro_entry *le, const struct mbuf *p)
{
struct mbuf *m;
uint32_t csum;
m = le->m_head;
if (m->m_pkthdr.lro_nsegs == 1) {
csum = p->m_pkthdr.lro_tcp_d_csum;
} else {
csum = (uint32_t)m->m_pkthdr.lro_tcp_d_csum +
(uint32_t)p->m_pkthdr.lro_tcp_d_csum;
while (csum > 0xffff)
csum = (csum >> 16) + (csum & 0xffff);
}
m->m_pkthdr.len += p->m_pkthdr.lro_tcp_d_len;
m->m_pkthdr.lro_tcp_d_csum = csum;
m->m_pkthdr.lro_tcp_d_len += p->m_pkthdr.lro_tcp_d_len;
m->m_pkthdr.lro_nsegs += p->m_pkthdr.lro_nsegs;
le->needs_merge = 1;
}
static void
tcp_lro_condense(struct lro_ctrl *lc, struct lro_entry *le)
{
uint32_t *ts_ptr;
struct mbuf *m;
struct tcphdr *th;
uint32_t tcp_data_len_total;
uint32_t tcp_data_seg_total;
uint16_t tcp_data_len;
uint16_t tcp_opt_len;
again:
m = le->m_head->m_nextpkt;
if (m == NULL) {
return;
}
th = tcp_lro_get_th(m);
tcp_opt_len = (th->th_off << 2);
tcp_opt_len -= sizeof(*th);
ts_ptr = (uint32_t *)(th + 1);
if (tcp_opt_len != 0 && __predict_false(tcp_opt_len != TCPOLEN_TSTAMP_APPA ||
*ts_ptr != TCP_LRO_TS_OPTION)) {
le->m_head->m_nextpkt = m->m_nextpkt;
tcp_push_and_replace(lc, le, m);
goto again;
}
if ((tcp_get_flags(th) & ~(TH_ACK | TH_PUSH)) != 0) {
le->m_head->m_nextpkt = m->m_nextpkt;
tcp_push_and_replace(lc, le, m);
goto again;
}
while((m = le->m_head->m_nextpkt) != NULL) {
le->m_head->m_nextpkt = m->m_nextpkt;
m->m_nextpkt = NULL;
tcp_data_len = m->m_pkthdr.lro_tcp_d_len;
th = tcp_lro_get_th(m);
ts_ptr = (uint32_t *)(th + 1);
tcp_opt_len = (th->th_off << 2);
tcp_opt_len -= sizeof(*th);
tcp_data_len_total = le->m_head->m_pkthdr.lro_tcp_d_len + tcp_data_len;
tcp_data_seg_total = le->m_head->m_pkthdr.lro_nsegs + m->m_pkthdr.lro_nsegs;
if (tcp_data_seg_total >= lc->lro_ackcnt_lim ||
tcp_data_len_total >= lc->lro_length_lim) {
tcp_push_and_replace(lc, le, m);
goto again;
}
if (tcp_opt_len != 0 &&
__predict_false(tcp_opt_len != TCPOLEN_TSTAMP_APPA ||
*ts_ptr != TCP_LRO_TS_OPTION)) {
tcp_push_and_replace(lc, le, m);
goto again;
}
if ((tcp_get_flags(th) & ~(TH_ACK | TH_PUSH)) != 0) {
tcp_push_and_replace(lc, le, m);
goto again;
}
if (tcp_opt_len != 0) {
uint32_t tsval = ntohl(*(ts_ptr + 1));
if (TSTMP_GT(le->tsval, tsval)) {
tcp_push_and_replace(lc, le, m);
goto again;
}
le->tsval = tsval;
le->tsecr = *(ts_ptr + 2);
}
if (__predict_false(ntohl(th->th_seq) != le->next_seq ||
((tcp_get_flags(th) & TH_ACK) !=
(le->flags & TH_ACK)) ||
(tcp_data_len == 0 &&
le->ack_seq == th->th_ack &&
le->window == th->th_win))) {
tcp_push_and_replace(lc, le, m);
goto again;
}
if (tcp_data_len != 0 ||
SEQ_GT(ntohl(th->th_ack), ntohl(le->ack_seq))) {
le->next_seq += tcp_data_len;
le->ack_seq = th->th_ack;
le->window = th->th_win;
le->needs_merge = 1;
} else if (th->th_ack == le->ack_seq) {
if (WIN_GT(th->th_win, le->window)) {
le->window = th->th_win;
le->needs_merge = 1;
}
}
if (tcp_data_len == 0) {
m_freem(m);
continue;
}
tcp_lro_mbuf_append_pkthdr(le, m);
m_adj(m, m->m_pkthdr.len - tcp_data_len);
m_demote_pkthdr(m);
le->m_tail->m_next = m;
le->m_tail = m_last(m);
}
}
static void
tcp_lro_flush(struct lro_ctrl *lc, struct lro_entry *le)
{
NET_EPOCH_ASSERT();
if (tcp_lro_flush_tcphpts == NULL ||
tcp_lro_flush_tcphpts(lc, le) != 0) {
tcp_lro_condense(lc, le);
tcp_flush_out_entry(lc, le);
}
lc->lro_flushed++;
bzero(le, sizeof(*le));
LIST_INSERT_HEAD(&lc->lro_free, le, next);
}
#define tcp_lro_msb_64(x) (1ULL << (flsll(x) - 1))
static void
tcp_lro_sort(struct lro_mbuf_sort *parray, uint32_t size)
{
struct lro_mbuf_sort temp;
uint64_t ones;
uint64_t zeros;
uint32_t x;
uint32_t y;
repeat:
if (size <= 12) {
for (x = 1; x < size; x++) {
temp = parray[x];
for (y = x; y > 0 && temp.seq < parray[y - 1].seq; y--)
parray[y] = parray[y - 1];
parray[y] = temp;
}
return;
}
ones = 0;
zeros = 0;
for (x = 0; x != size; x++) {
ones |= parray[x].seq;
zeros |= ~parray[x].seq;
}
ones &= zeros;
if (ones == 0)
return;
ones = tcp_lro_msb_64(ones);
for (x = y = 0; y != size; y++) {
if (parray[y].seq & ones)
continue;
temp = parray[x];
parray[x] = parray[y];
parray[y] = temp;
x++;
}
KASSERT(x != 0 && x != size, ("Memory is corrupted\n"));
tcp_lro_sort(parray, x);
parray += x;
size -= x;
goto repeat;
}
void
tcp_lro_flush_all(struct lro_ctrl *lc)
{
uint64_t seq;
uint64_t nseq;
unsigned x;
NET_EPOCH_ASSERT();
if (lc->lro_mbuf_count == 0)
goto done;
if (lc->lro_cpu_is_set == 0) {
if (lc->lro_last_cpu == curcpu) {
lc->lro_cnt_of_same_cpu++;
if (lc->lro_cnt_of_same_cpu > tcp_lro_cpu_set_thresh)
lc->lro_cpu_is_set = 1;
} else {
lc->lro_last_cpu = curcpu;
lc->lro_cnt_of_same_cpu = 0;
}
}
CURVNET_SET(lc->ifp->if_vnet);
binuptime(&lc->lro_last_queue_time);
tcp_lro_sort(lc->lro_mbuf_data, lc->lro_mbuf_count);
seq = 0;
for (x = 0; x != lc->lro_mbuf_count; x++) {
struct mbuf *mb;
mb = lc->lro_mbuf_data[x].mb;
nseq = lc->lro_mbuf_data[x].seq & (-1ULL << 24);
if (seq != nseq) {
seq = nseq;
tcp_lro_rx_done(lc);
}
if (tcp_lro_rx_common(lc, mb, 0, false) != 0) {
tcp_lro_flush_active(lc);
(*lc->ifp->if_input)(lc->ifp, mb);
lc->lro_queued++;
lc->lro_flushed++;
}
}
CURVNET_RESTORE();
done:
tcp_lro_rx_done(lc);
if (tcp_hpts_softclock != NULL)
tcp_hpts_softclock();
lc->lro_mbuf_count = 0;
}
static struct lro_head *
tcp_lro_rx_get_bucket(struct lro_ctrl *lc, struct mbuf *m, struct lro_parser *parser)
{
u_long hash;
if (M_HASHTYPE_ISHASH(m)) {
hash = m->m_pkthdr.flowid;
} else {
for (unsigned i = hash = 0; i != LRO_RAW_ADDRESS_MAX; i++)
hash += parser->data.raw[i];
}
return (&lc->lro_hash[hash % lc->lro_hashsz]);
}
static int
tcp_lro_rx_common(struct lro_ctrl *lc, struct mbuf *m, uint32_t csum, bool use_hash)
{
struct lro_parser pi;
struct lro_parser po;
struct lro_parser *pa;
struct lro_entry *le;
struct lro_head *bucket;
struct tcphdr *th;
int tcp_data_len;
int tcp_opt_len;
int error;
uint16_t tcp_data_sum;
#ifdef INET
if (__predict_false(V_ipforwarding != 0))
return (TCP_LRO_CANNOT);
#endif
#ifdef INET6
if (__predict_false(V_ip6_forwarding != 0))
return (TCP_LRO_CANNOT);
#endif
if (((m->m_pkthdr.csum_flags & (CSUM_DATA_VALID | CSUM_PSEUDO_HDR)) !=
((CSUM_DATA_VALID | CSUM_PSEUDO_HDR))) ||
(m->m_pkthdr.csum_data != 0xffff)) {
counter_u64_add(tcp_bad_csums, 1);
return (TCP_LRO_CANNOT);
}
pa = tcp_lro_parser(m, &po, &pi, true);
if (__predict_false(pa == NULL))
return (TCP_LRO_NOT_SUPPORTED);
error = tcp_lro_trim_mbuf_chain(m, pa);
if (__predict_false(error != 0))
return (error);
#ifdef INET
switch (pa->data.lro_type) {
case LRO_TYPE_IPV4_TCP:
error = tcp_lro_rx_ipv4(lc, m, pa->ip4);
if (__predict_false(error != 0))
return (error);
break;
default:
break;
}
#endif
if ((m->m_flags & (M_TSTMP_LRO | M_TSTMP)) == 0) {
m->m_pkthdr.rcv_tstmp = bintime2ns(&lc->lro_last_queue_time);
m->m_flags |= M_TSTMP_LRO;
}
th = pa->tcp;
if (__predict_false(tcp_get_flags(th) & TH_SYN))
return (TCP_LRO_CANNOT);
tcp_opt_len = (th->th_off << 2);
tcp_data_len = m->m_pkthdr.len - ((uint8_t *)th -
(uint8_t *)m->m_data) - tcp_opt_len;
tcp_opt_len -= sizeof(*th);
if (__predict_false(tcp_opt_len < 0 || tcp_data_len < 0))
return (TCP_LRO_CANNOT);
if (tcp_data_len == 0)
tcp_data_sum = 0;
else if (__predict_false(csum != 0))
tcp_data_sum = tcp_lro_rx_csum_data(pa, ~csum);
else
tcp_data_sum = tcp_lro_rx_csum_data(pa, ~th->th_sum);
m->m_nextpkt = NULL;
m->m_pkthdr.rcvif = lc->ifp;
m->m_pkthdr.lro_tcp_d_csum = tcp_data_sum;
m->m_pkthdr.lro_tcp_d_len = tcp_data_len;
m->m_pkthdr.lro_tcp_h_off = ((uint8_t *)th - (uint8_t *)m->m_data);
m->m_pkthdr.lro_nsegs = 1;
if (!use_hash) {
bucket = &lc->lro_hash[0];
} else {
bucket = tcp_lro_rx_get_bucket(lc, m, pa);
}
LIST_FOREACH(le, bucket, hash_next) {
if (lro_address_compare(&po.data, &le->outer.data) == false ||
lro_address_compare(&pi.data, &le->inner.data) == false)
continue;
if (tcp_data_len == 0 &&
SEQ_LT(ntohl(th->th_ack), ntohl(le->ack_seq))) {
m_freem(m);
return (0);
}
le->m_last_mbuf->m_nextpkt = m;
le->m_last_mbuf = m;
return (0);
}
if (LIST_EMPTY(&lc->lro_free))
return (TCP_LRO_NO_ENTRIES);
le = LIST_FIRST(&lc->lro_free);
LIST_REMOVE(le, next);
tcp_lro_active_insert(lc, bucket, le);
le->inner = pi;
le->outer = po;
le->alloc_time = lc->lro_last_queue_time;
tcp_set_entry_to_mbuf(lc, le, m, th);
le->m_last_mbuf = m;
return (0);
}
int
tcp_lro_rx(struct lro_ctrl *lc, struct mbuf *m, uint32_t csum)
{
int error;
binuptime(&lc->lro_last_queue_time);
CURVNET_SET(lc->ifp->if_vnet);
error = tcp_lro_rx_common(lc, m, csum, true);
if (__predict_false(error != 0)) {
tcp_lro_flush_active(lc);
}
CURVNET_RESTORE();
return (error);
}
void
tcp_lro_queue_mbuf(struct lro_ctrl *lc, struct mbuf *mb)
{
NET_EPOCH_ASSERT();
if (__predict_false(lc->ifp == NULL || lc->lro_mbuf_data == NULL ||
lc->lro_mbuf_max == 0)) {
m_freem(mb);
return;
}
if (__predict_false((lc->ifp->if_capenable & IFCAP_LRO) == 0)) {
(*lc->ifp->if_input) (lc->ifp, mb);
return;
}
if ((tcplro_stacks_wanting_mbufq > 0) &&
(tcp_less_accurate_lro_ts == 0) &&
((mb->m_flags & M_TSTMP) == 0)) {
binuptime(&lc->lro_last_queue_time);
mb->m_pkthdr.rcv_tstmp = bintime2ns(&lc->lro_last_queue_time);
mb->m_flags |= M_TSTMP_LRO;
}
lc->lro_mbuf_data[lc->lro_mbuf_count].seq = lc->lro_mbuf_count;
if (M_HASHTYPE_ISHASH(mb))
lc->lro_mbuf_data[lc->lro_mbuf_count].seq |=
(((uint64_t)M_HASHTYPE_GET(mb)) << 56) |
(((uint64_t)mb->m_pkthdr.flowid) << 24);
lc->lro_mbuf_data[lc->lro_mbuf_count].mb = mb;
if (__predict_false(++lc->lro_mbuf_count == lc->lro_mbuf_max))
tcp_lro_flush_all(lc);
}