2 * Copyright (c) 2007, Myricom Inc.
3 * Copyright (c) 2008, Intel Corporation.
4 * Copyright (c) 2012 The FreeBSD Foundation
5 * Copyright (c) 2016 Mellanox Technologies.
8 * Portions of this software were developed by Bjoern Zeeb
9 * under sponsorship from the FreeBSD Foundation.
11 * Redistribution and use in source and binary forms, with or without
12 * modification, are permitted provided that the following conditions
14 * 1. Redistributions of source code must retain the above copyright
15 * notice, this list of conditions and the following disclaimer.
16 * 2. Redistributions in binary form must reproduce the above copyright
17 * notice, this list of conditions and the following disclaimer in the
18 * documentation and/or other materials provided with the distribution.
20 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
21 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
22 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
23 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
24 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
25 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
26 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
27 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
28 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
29 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
33 #include <sys/cdefs.h>
34 __FBSDID("$FreeBSD$");
37 #include "opt_inet6.h"
39 #include <sys/param.h>
40 #include <sys/systm.h>
41 #include <sys/kernel.h>
42 #include <sys/malloc.h>
44 #include <sys/socket.h>
47 #include <net/if_var.h>
48 #include <net/ethernet.h>
51 #include <netinet/in_systm.h>
52 #include <netinet/in.h>
53 #include <netinet/ip6.h>
54 #include <netinet/ip.h>
55 #include <netinet/ip_var.h>
56 #include <netinet/tcp.h>
57 #include <netinet/tcp_lro.h>
59 #include <netinet6/ip6_var.h>
61 #include <machine/in_cksum.h>
63 static MALLOC_DEFINE(M_LRO, "LRO", "LRO control structures");
65 #define TCP_LRO_UPDATE_CSUM 1
66 #ifndef TCP_LRO_UPDATE_CSUM
67 #define TCP_LRO_INVALID_CSUM 0x0000
70 static void tcp_lro_rx_done(struct lro_ctrl *lc);
73 tcp_lro_init(struct lro_ctrl *lc)
75 return (tcp_lro_init_args(lc, NULL, TCP_LRO_ENTRIES, 0));
79 tcp_lro_init_args(struct lro_ctrl *lc, struct ifnet *ifp,
80 unsigned lro_entries, unsigned lro_mbufs)
90 lc->lro_mbuf_count = 0;
91 lc->lro_mbuf_max = lro_mbufs;
92 lc->lro_cnt = lro_entries;
93 lc->lro_ackcnt_lim = TCP_LRO_ACKCNT_MAX;
94 lc->lro_length_lim = TCP_LRO_LENGTH_MAX;
96 LIST_INIT(&lc->lro_free);
97 LIST_INIT(&lc->lro_active);
99 /* compute size to allocate */
100 size = (lro_mbufs * sizeof(struct mbuf *)) +
101 (lro_entries * sizeof(*le));
102 lc->lro_mbuf_data = (struct mbuf **)
103 malloc(size, M_LRO, M_NOWAIT | M_ZERO);
105 /* check for out of memory */
106 if (lc->lro_mbuf_data == NULL) {
107 memset(lc, 0, sizeof(*lc));
110 /* compute offset for LRO entries */
111 le = (struct lro_entry *)
112 (lc->lro_mbuf_data + lro_mbufs);
114 /* setup linked list */
115 for (i = 0; i != lro_entries; i++)
116 LIST_INSERT_HEAD(&lc->lro_free, le + i, next);
122 tcp_lro_free(struct lro_ctrl *lc)
124 struct lro_entry *le;
127 /* reset LRO free list */
128 LIST_INIT(&lc->lro_free);
130 /* free active mbufs, if any */
131 while ((le = LIST_FIRST(&lc->lro_active)) != NULL) {
132 LIST_REMOVE(le, next);
136 /* free mbuf array, if any */
137 for (x = 0; x != lc->lro_mbuf_count; x++)
138 m_freem(lc->lro_mbuf_data[x]);
139 lc->lro_mbuf_count = 0;
141 /* free allocated memory, if any */
142 free(lc->lro_mbuf_data, M_LRO);
143 lc->lro_mbuf_data = NULL;
146 #ifdef TCP_LRO_UPDATE_CSUM
148 tcp_lro_csum_th(struct tcphdr *th)
153 ch = th->th_sum = 0x0000;
164 ch = (ch >> 16) + (ch & 0xffff);
166 return (ch & 0xffff);
170 tcp_lro_rx_csum_fixup(struct lro_entry *le, void *l3hdr, struct tcphdr *th,
171 uint16_t tcp_data_len, uint16_t csum)
178 /* Remove length from checksum. */
179 switch (le->eh_type) {
185 ip6 = (struct ip6_hdr *)l3hdr;
186 if (le->append_cnt == 0)
191 cx = ntohs(ip6->ip6_plen);
192 cs = in6_cksum_pseudo(ip6, cx, ip6->ip6_nxt, 0);
202 ip4 = (struct ip *)l3hdr;
203 if (le->append_cnt == 0)
206 cs = in_addword(ntohs(ip4->ip_len) - sizeof(*ip4),
208 cs = in_pseudo(ip4->ip_src.s_addr, ip4->ip_dst.s_addr,
215 cs = 0; /* Keep compiler happy. */
221 /* Remove TCP header csum. */
222 cs = ~tcp_lro_csum_th(th);
225 c = (c >> 16) + (c & 0xffff);
232 tcp_lro_rx_done(struct lro_ctrl *lc)
234 struct lro_entry *le;
236 while ((le = LIST_FIRST(&lc->lro_active)) != NULL) {
237 LIST_REMOVE(le, next);
238 tcp_lro_flush(lc, le);
243 tcp_lro_flush_inactive(struct lro_ctrl *lc, const struct timeval *timeout)
245 struct lro_entry *le, *le_tmp;
248 if (LIST_EMPTY(&lc->lro_active))
252 timevalsub(&tv, timeout);
253 LIST_FOREACH_SAFE(le, &lc->lro_active, next, le_tmp) {
254 if (timevalcmp(&tv, &le->mtime, >=)) {
255 LIST_REMOVE(le, next);
256 tcp_lro_flush(lc, le);
262 tcp_lro_flush(struct lro_ctrl *lc, struct lro_entry *le)
265 if (le->append_cnt > 0) {
269 p_len = htons(le->p_len);
270 switch (le->eh_type) {
277 ip6->ip6_plen = p_len;
278 th = (struct tcphdr *)(ip6 + 1);
279 le->m_head->m_pkthdr.csum_flags = CSUM_DATA_VALID |
281 le->p_len += ETHER_HDR_LEN + sizeof(*ip6);
289 #ifdef TCP_LRO_UPDATE_CSUM
295 #ifdef TCP_LRO_UPDATE_CSUM
296 /* Fix IP header checksum for new length. */
302 cl = (cl >> 16) + (cl & 0xffff);
306 ip4->ip_sum = TCP_LRO_INVALID_CSUM;
309 th = (struct tcphdr *)(ip4 + 1);
310 le->m_head->m_pkthdr.csum_flags = CSUM_DATA_VALID |
311 CSUM_PSEUDO_HDR | CSUM_IP_CHECKED | CSUM_IP_VALID;
312 le->p_len += ETHER_HDR_LEN;
317 th = NULL; /* Keep compiler happy. */
319 le->m_head->m_pkthdr.csum_data = 0xffff;
320 le->m_head->m_pkthdr.len = le->p_len;
322 /* Incorporate the latest ACK into the TCP header. */
323 th->th_ack = le->ack_seq;
324 th->th_win = le->window;
325 /* Incorporate latest timestamp into the TCP header. */
326 if (le->timestamp != 0) {
329 ts_ptr = (uint32_t *)(th + 1);
330 ts_ptr[1] = htonl(le->tsval);
331 ts_ptr[2] = le->tsecr;
333 #ifdef TCP_LRO_UPDATE_CSUM
334 /* Update the TCP header checksum. */
335 le->ulp_csum += p_len;
336 le->ulp_csum += tcp_lro_csum_th(th);
337 while (le->ulp_csum > 0xffff)
338 le->ulp_csum = (le->ulp_csum >> 16) +
339 (le->ulp_csum & 0xffff);
340 th->th_sum = (le->ulp_csum & 0xffff);
341 th->th_sum = ~th->th_sum;
343 th->th_sum = TCP_LRO_INVALID_CSUM;
347 (*lc->ifp->if_input)(lc->ifp, le->m_head);
348 lc->lro_queued += le->append_cnt + 1;
350 bzero(le, sizeof(*le));
351 LIST_INSERT_HEAD(&lc->lro_free, le, next);
355 tcp_lro_mbuf_compare_header(const void *ppa, const void *ppb)
357 const struct mbuf *ma = *((const struct mbuf * const *)ppa);
358 const struct mbuf *mb = *((const struct mbuf * const *)ppb);
361 ret = M_HASHTYPE_GET(ma) - M_HASHTYPE_GET(mb);
365 if (ma->m_pkthdr.flowid > mb->m_pkthdr.flowid)
367 else if (ma->m_pkthdr.flowid < mb->m_pkthdr.flowid)
370 ret = TCP_LRO_SEQUENCE(ma) - TCP_LRO_SEQUENCE(mb);
376 tcp_lro_flush_all(struct lro_ctrl *lc)
382 /* check if no mbufs to flush */
383 if (lc->lro_mbuf_count == 0)
386 /* sort all mbufs according to stream */
387 qsort(lc->lro_mbuf_data, lc->lro_mbuf_count, sizeof(struct mbuf *),
388 &tcp_lro_mbuf_compare_header);
390 /* input data into LRO engine, stream by stream */
392 hashtype = M_HASHTYPE_NONE;
393 for (x = 0; x != lc->lro_mbuf_count; x++) {
396 mb = lc->lro_mbuf_data[x];
398 /* check for new stream */
399 if (mb->m_pkthdr.flowid != flowid ||
400 M_HASHTYPE_GET(mb) != hashtype) {
401 flowid = mb->m_pkthdr.flowid;
402 hashtype = M_HASHTYPE_GET(mb);
404 /* flush active streams */
407 #ifdef TCP_LRO_RESET_SEQUENCE
408 /* reset sequence number */
409 TCP_LRO_SEQUENCE(mb) = 0;
411 /* add packet to LRO engine */
412 if (tcp_lro_rx(lc, mb, 0) != 0) {
413 /* input packet to network layer */
414 (*lc->ifp->if_input)(lc->ifp, mb);
420 /* flush active streams */
423 lc->lro_mbuf_count = 0;
428 tcp_lro_rx_ipv6(struct lro_ctrl *lc, struct mbuf *m, struct ip6_hdr *ip6,
432 /* XXX-BZ we should check the flow-label. */
434 /* XXX-BZ We do not yet support ext. hdrs. */
435 if (ip6->ip6_nxt != IPPROTO_TCP)
436 return (TCP_LRO_NOT_SUPPORTED);
438 /* Find the TCP header. */
439 *th = (struct tcphdr *)(ip6 + 1);
447 tcp_lro_rx_ipv4(struct lro_ctrl *lc, struct mbuf *m, struct ip *ip4,
453 if (ip4->ip_p != IPPROTO_TCP)
454 return (TCP_LRO_NOT_SUPPORTED);
456 /* Ensure there are no options. */
457 if ((ip4->ip_hl << 2) != sizeof (*ip4))
458 return (TCP_LRO_CANNOT);
460 /* .. and the packet is not fragmented. */
461 if (ip4->ip_off & htons(IP_MF|IP_OFFMASK))
462 return (TCP_LRO_CANNOT);
464 /* Legacy IP has a header checksum that needs to be correct. */
465 csum_flags = m->m_pkthdr.csum_flags;
466 if (csum_flags & CSUM_IP_CHECKED) {
467 if (__predict_false((csum_flags & CSUM_IP_VALID) == 0)) {
469 return (TCP_LRO_CANNOT);
472 csum = in_cksum_hdr(ip4);
473 if (__predict_false((csum) != 0)) {
475 return (TCP_LRO_CANNOT);
479 /* Find the TCP header (we assured there are no IP options). */
480 *th = (struct tcphdr *)(ip4 + 1);
487 tcp_lro_rx(struct lro_ctrl *lc, struct mbuf *m, uint32_t csum)
489 struct lro_entry *le;
490 struct ether_header *eh;
492 struct ip6_hdr *ip6 = NULL; /* Keep compiler happy. */
495 struct ip *ip4 = NULL; /* Keep compiler happy. */
498 void *l3hdr = NULL; /* Keep compiler happy. */
501 int error, ip_len, l;
502 uint16_t eh_type, tcp_data_len;
504 /* We expect a contiguous header [eh, ip, tcp]. */
506 eh = mtod(m, struct ether_header *);
507 eh_type = ntohs(eh->ether_type);
512 CURVNET_SET(lc->ifp->if_vnet);
513 if (V_ip6_forwarding != 0) {
514 /* XXX-BZ stats but changing lro_ctrl is a problem. */
516 return (TCP_LRO_CANNOT);
519 l3hdr = ip6 = (struct ip6_hdr *)(eh + 1);
520 error = tcp_lro_rx_ipv6(lc, m, ip6, &th);
523 tcp_data_len = ntohs(ip6->ip6_plen);
524 ip_len = sizeof(*ip6) + tcp_data_len;
531 CURVNET_SET(lc->ifp->if_vnet);
532 if (V_ipforwarding != 0) {
533 /* XXX-BZ stats but changing lro_ctrl is a problem. */
535 return (TCP_LRO_CANNOT);
538 l3hdr = ip4 = (struct ip *)(eh + 1);
539 error = tcp_lro_rx_ipv4(lc, m, ip4, &th);
542 ip_len = ntohs(ip4->ip_len);
543 tcp_data_len = ip_len - sizeof(*ip4);
547 /* XXX-BZ what happens in case of VLAN(s)? */
549 return (TCP_LRO_NOT_SUPPORTED);
553 * If the frame is padded beyond the end of the IP packet, then we must
554 * trim the extra bytes off.
556 l = m->m_pkthdr.len - (ETHER_HDR_LEN + ip_len);
559 /* Truncated packet. */
560 return (TCP_LRO_CANNOT);
566 * Check TCP header constraints.
568 /* Ensure no bits set besides ACK or PSH. */
569 if ((th->th_flags & ~(TH_ACK | TH_PUSH)) != 0)
570 return (TCP_LRO_CANNOT);
572 /* XXX-BZ We lose a AKC|PUSH flag concatinating multiple segments. */
573 /* XXX-BZ Ideally we'd flush on PUSH? */
576 * Check for timestamps.
577 * Since the only option we handle are timestamps, we only have to
578 * handle the simple case of aligned timestamps.
580 l = (th->th_off << 2);
583 ts_ptr = (uint32_t *)(th + 1);
584 if (l != 0 && (__predict_false(l != TCPOLEN_TSTAMP_APPA) ||
585 (*ts_ptr != ntohl(TCPOPT_NOP<<24|TCPOPT_NOP<<16|
586 TCPOPT_TIMESTAMP<<8|TCPOLEN_TIMESTAMP))))
587 return (TCP_LRO_CANNOT);
589 /* If the driver did not pass in the checksum, set it now. */
593 seq = ntohl(th->th_seq);
595 /* Try to find a matching previous segment. */
596 LIST_FOREACH(le, &lc->lro_active, next) {
597 if (le->eh_type != eh_type)
599 if (le->source_port != th->th_sport ||
600 le->dest_port != th->th_dport)
605 if (bcmp(&le->source_ip6, &ip6->ip6_src,
606 sizeof(struct in6_addr)) != 0 ||
607 bcmp(&le->dest_ip6, &ip6->ip6_dst,
608 sizeof(struct in6_addr)) != 0)
614 if (le->source_ip4 != ip4->ip_src.s_addr ||
615 le->dest_ip4 != ip4->ip_dst.s_addr)
621 /* Flush now if appending will result in overflow. */
622 if (le->p_len > (lc->lro_length_lim - tcp_data_len)) {
623 LIST_REMOVE(le, next);
624 tcp_lro_flush(lc, le);
628 /* Try to append the new segment. */
629 if (__predict_false(seq != le->next_seq ||
630 (tcp_data_len == 0 && le->ack_seq == th->th_ack))) {
631 /* Out of order packet or duplicate ACK. */
632 LIST_REMOVE(le, next);
633 tcp_lro_flush(lc, le);
634 return (TCP_LRO_CANNOT);
638 uint32_t tsval = ntohl(*(ts_ptr + 1));
639 /* Make sure timestamp values are increasing. */
640 /* XXX-BZ flip and use TSTMP_GEQ macro for this? */
641 if (__predict_false(le->tsval > tsval ||
643 return (TCP_LRO_CANNOT);
645 le->tsecr = *(ts_ptr + 2);
648 le->next_seq += tcp_data_len;
649 le->ack_seq = th->th_ack;
650 le->window = th->th_win;
653 #ifdef TCP_LRO_UPDATE_CSUM
654 le->ulp_csum += tcp_lro_rx_csum_fixup(le, l3hdr, th,
655 tcp_data_len, ~csum);
658 if (tcp_data_len == 0) {
661 * Flush this LRO entry, if this ACK should not
662 * be further delayed.
664 if (le->append_cnt >= lc->lro_ackcnt_lim) {
665 LIST_REMOVE(le, next);
666 tcp_lro_flush(lc, le);
671 le->p_len += tcp_data_len;
674 * Adjust the mbuf so that m_data points to the first byte of
675 * the ULP payload. Adjust the mbuf to avoid complications and
676 * append new segment to existing mbuf chain.
678 m_adj(m, m->m_pkthdr.len - tcp_data_len);
681 le->m_tail->m_next = m;
682 le->m_tail = m_last(m);
685 * If a possible next full length packet would cause an
686 * overflow, pro-actively flush now.
688 if (le->p_len > (lc->lro_length_lim - lc->ifp->if_mtu)) {
689 LIST_REMOVE(le, next);
690 tcp_lro_flush(lc, le);
692 getmicrotime(&le->mtime);
697 /* Try to find an empty slot. */
698 if (LIST_EMPTY(&lc->lro_free))
699 return (TCP_LRO_NO_ENTRIES);
701 /* Start a new segment chain. */
702 le = LIST_FIRST(&lc->lro_free);
703 LIST_REMOVE(le, next);
704 LIST_INSERT_HEAD(&lc->lro_active, le, next);
705 getmicrotime(&le->mtime);
707 /* Start filling in details. */
712 le->source_ip6 = ip6->ip6_src;
713 le->dest_ip6 = ip6->ip6_dst;
714 le->eh_type = eh_type;
715 le->p_len = m->m_pkthdr.len - ETHER_HDR_LEN - sizeof(*ip6);
721 le->source_ip4 = ip4->ip_src.s_addr;
722 le->dest_ip4 = ip4->ip_dst.s_addr;
723 le->eh_type = eh_type;
724 le->p_len = m->m_pkthdr.len - ETHER_HDR_LEN;
728 le->source_port = th->th_sport;
729 le->dest_port = th->th_dport;
731 le->next_seq = seq + tcp_data_len;
732 le->ack_seq = th->th_ack;
733 le->window = th->th_win;
736 le->tsval = ntohl(*(ts_ptr + 1));
737 le->tsecr = *(ts_ptr + 2);
740 #ifdef TCP_LRO_UPDATE_CSUM
742 * Do not touch the csum of the first packet. However save the
743 * "adjusted" checksum of just the source and destination addresses,
744 * the next header and the TCP payload. The length and TCP header
745 * parts may change, so we remove those from the saved checksum and
746 * re-add with final values on tcp_lro_flush() if needed.
748 KASSERT(le->ulp_csum == 0, ("%s: le=%p le->ulp_csum=0x%04x\n",
749 __func__, le, le->ulp_csum));
751 le->ulp_csum = tcp_lro_rx_csum_fixup(le, l3hdr, th, tcp_data_len,
753 th->th_sum = csum; /* Restore checksum on first packet. */
757 le->m_tail = m_last(m);
763 tcp_lro_queue_mbuf(struct lro_ctrl *lc, struct mbuf *mb)
766 if (__predict_false(lc->ifp == NULL || lc->lro_mbuf_data == NULL ||
767 lc->lro_mbuf_max == 0)) {
773 /* check if packet is not LRO capable */
774 if (__predict_false(mb->m_pkthdr.csum_flags == 0 ||
775 (lc->ifp->if_capenable & IFCAP_LRO) == 0)) {
779 /* input packet to network layer */
780 (*lc->ifp->if_input) (lc->ifp, mb);
784 /* check if array is full */
785 if (__predict_false(lc->lro_mbuf_count == lc->lro_mbuf_max))
786 tcp_lro_flush_all(lc);
788 /* store sequence number */
789 TCP_LRO_SEQUENCE(mb) = lc->lro_mbuf_count;
792 lc->lro_mbuf_data[lc->lro_mbuf_count++] = mb;