2 * Copyright (c) 2007, Myricom Inc.
3 * Copyright (c) 2008, Intel Corporation.
4 * Copyright (c) 2012 The FreeBSD Foundation
7 * Portions of this software were developed by Bjoern Zeeb
8 * under sponsorship from the FreeBSD Foundation.
10 * Redistribution and use in source and binary forms, with or without
11 * modification, are permitted provided that the following conditions
13 * 1. Redistributions of source code must retain the above copyright
14 * notice, this list of conditions and the following disclaimer.
15 * 2. Redistributions in binary form must reproduce the above copyright
16 * notice, this list of conditions and the following disclaimer in the
17 * documentation and/or other materials provided with the distribution.
19 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
20 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
21 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
22 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
23 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
24 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
25 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
26 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
27 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
28 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
32 #include <sys/cdefs.h>
33 __FBSDID("$FreeBSD$");
36 #include "opt_inet6.h"
38 #include <sys/param.h>
39 #include <sys/systm.h>
41 #include <sys/kernel.h>
42 #include <sys/socket.h>
43 #include <sys/sysctl.h>
46 #include <net/if_var.h>
47 #include <net/ethernet.h>
50 #include <netinet/in_systm.h>
51 #include <netinet/in.h>
52 #include <netinet/ip6.h>
53 #include <netinet/ip.h>
54 #include <netinet/ip_var.h>
55 #include <netinet/tcp.h>
56 #include <netinet/tcp_lro.h>
57 #include <netinet/tcp_var.h>
59 #include <netinet6/ip6_var.h>
61 #include <machine/in_cksum.h>
64 #define LRO_ENTRIES 8 /* # of LRO entries per RX queue. */
67 #define TCP_LRO_UPDATE_CSUM 1
68 #ifndef TCP_LRO_UPDATE_CSUM
69 #define TCP_LRO_INVALID_CSUM 0x0000
72 SYSCTL_NODE(_net_inet_tcp, OID_AUTO, lro, CTLFLAG_RW | CTLFLAG_MPSAFE, 0,
75 static unsigned tcp_lro_entries = LRO_ENTRIES;
76 SYSCTL_UINT(_net_inet_tcp_lro, OID_AUTO, entries,
77 CTLFLAG_RDTUN | CTLFLAG_MPSAFE, &tcp_lro_entries, 0,
78 "default number of LRO entries");
81 tcp_lro_init(struct lro_ctrl *lc)
90 SLIST_INIT(&lc->lro_free);
91 SLIST_INIT(&lc->lro_active);
94 for (i = 0; i < tcp_lro_entries; i++) {
95 le = (struct lro_entry *)malloc(sizeof(*le), M_DEVBUF,
103 SLIST_INSERT_HEAD(&lc->lro_free, le, next);
110 tcp_lro_free(struct lro_ctrl *lc)
112 struct lro_entry *le;
114 while (!SLIST_EMPTY(&lc->lro_free)) {
115 le = SLIST_FIRST(&lc->lro_free);
116 SLIST_REMOVE_HEAD(&lc->lro_free, next);
121 #ifdef TCP_LRO_UPDATE_CSUM
123 tcp_lro_csum_th(struct tcphdr *th)
128 ch = th->th_sum = 0x0000;
139 ch = (ch >> 16) + (ch & 0xffff);
141 return (ch & 0xffff);
145 tcp_lro_rx_csum_fixup(struct lro_entry *le, void *l3hdr, struct tcphdr *th,
146 uint16_t tcp_data_len, uint16_t csum)
153 /* Remove length from checksum. */
154 switch (le->eh_type) {
160 ip6 = (struct ip6_hdr *)l3hdr;
161 if (le->append_cnt == 0)
166 cx = ntohs(ip6->ip6_plen);
167 cs = in6_cksum_pseudo(ip6, cx, ip6->ip6_nxt, 0);
177 ip4 = (struct ip *)l3hdr;
178 if (le->append_cnt == 0)
181 cs = in_addword(ntohs(ip4->ip_len) - sizeof(*ip4),
183 cs = in_pseudo(ip4->ip_src.s_addr, ip4->ip_dst.s_addr,
190 cs = 0; /* Keep compiler happy. */
196 /* Remove TCP header csum. */
197 cs = ~tcp_lro_csum_th(th);
200 c = (c >> 16) + (c & 0xffff);
207 tcp_lro_flush_inactive(struct lro_ctrl *lc, const struct timeval *timeout)
209 struct lro_entry *le, *le_tmp;
212 if (SLIST_EMPTY(&lc->lro_active))
216 timevalsub(&tv, timeout);
217 SLIST_FOREACH_SAFE(le, &lc->lro_active, next, le_tmp) {
218 if (timevalcmp(&tv, &le->mtime, >=)) {
219 SLIST_REMOVE(&lc->lro_active, le, lro_entry, next);
220 tcp_lro_flush(lc, le);
226 tcp_lro_flush(struct lro_ctrl *lc, struct lro_entry *le)
229 if (le->append_cnt > 0) {
233 p_len = htons(le->p_len);
234 switch (le->eh_type) {
241 ip6->ip6_plen = p_len;
242 th = (struct tcphdr *)(ip6 + 1);
243 le->m_head->m_pkthdr.csum_flags = CSUM_DATA_VALID |
245 le->p_len += ETHER_HDR_LEN + sizeof(*ip6);
253 #ifdef TCP_LRO_UPDATE_CSUM
259 #ifdef TCP_LRO_UPDATE_CSUM
260 /* Fix IP header checksum for new length. */
266 cl = (cl >> 16) + (cl & 0xffff);
270 ip4->ip_sum = TCP_LRO_INVALID_CSUM;
273 th = (struct tcphdr *)(ip4 + 1);
274 le->m_head->m_pkthdr.csum_flags = CSUM_DATA_VALID |
275 CSUM_PSEUDO_HDR | CSUM_IP_CHECKED | CSUM_IP_VALID;
276 le->p_len += ETHER_HDR_LEN;
281 th = NULL; /* Keep compiler happy. */
283 le->m_head->m_pkthdr.csum_data = 0xffff;
284 le->m_head->m_pkthdr.len = le->p_len;
286 /* Incorporate the latest ACK into the TCP header. */
287 th->th_ack = le->ack_seq;
288 th->th_win = le->window;
289 /* Incorporate latest timestamp into the TCP header. */
290 if (le->timestamp != 0) {
293 ts_ptr = (uint32_t *)(th + 1);
294 ts_ptr[1] = htonl(le->tsval);
295 ts_ptr[2] = le->tsecr;
297 #ifdef TCP_LRO_UPDATE_CSUM
298 /* Update the TCP header checksum. */
299 le->ulp_csum += p_len;
300 le->ulp_csum += tcp_lro_csum_th(th);
301 while (le->ulp_csum > 0xffff)
302 le->ulp_csum = (le->ulp_csum >> 16) +
303 (le->ulp_csum & 0xffff);
304 th->th_sum = (le->ulp_csum & 0xffff);
305 th->th_sum = ~th->th_sum;
307 th->th_sum = TCP_LRO_INVALID_CSUM;
311 (*lc->ifp->if_input)(lc->ifp, le->m_head);
312 lc->lro_queued += le->append_cnt + 1;
314 bzero(le, sizeof(*le));
315 SLIST_INSERT_HEAD(&lc->lro_free, le, next);
320 tcp_lro_rx_ipv6(struct lro_ctrl *lc, struct mbuf *m, struct ip6_hdr *ip6,
324 /* XXX-BZ we should check the flow-label. */
326 /* XXX-BZ We do not yet support ext. hdrs. */
327 if (ip6->ip6_nxt != IPPROTO_TCP)
328 return (TCP_LRO_NOT_SUPPORTED);
330 /* Find the TCP header. */
331 *th = (struct tcphdr *)(ip6 + 1);
339 tcp_lro_rx_ipv4(struct lro_ctrl *lc, struct mbuf *m, struct ip *ip4,
345 if (ip4->ip_p != IPPROTO_TCP)
346 return (TCP_LRO_NOT_SUPPORTED);
348 /* Ensure there are no options. */
349 if ((ip4->ip_hl << 2) != sizeof (*ip4))
350 return (TCP_LRO_CANNOT);
352 /* .. and the packet is not fragmented. */
353 if (ip4->ip_off & htons(IP_MF|IP_OFFMASK))
354 return (TCP_LRO_CANNOT);
356 /* Legacy IP has a header checksum that needs to be correct. */
357 csum_flags = m->m_pkthdr.csum_flags;
358 if (csum_flags & CSUM_IP_CHECKED) {
359 if (__predict_false((csum_flags & CSUM_IP_VALID) == 0)) {
361 return (TCP_LRO_CANNOT);
364 csum = in_cksum_hdr(ip4);
365 if (__predict_false((csum) != 0)) {
367 return (TCP_LRO_CANNOT);
371 /* Find the TCP header (we assured there are no IP options). */
372 *th = (struct tcphdr *)(ip4 + 1);
379 tcp_lro_rx(struct lro_ctrl *lc, struct mbuf *m, uint32_t csum)
381 struct lro_entry *le;
382 struct ether_header *eh;
384 struct ip6_hdr *ip6 = NULL; /* Keep compiler happy. */
387 struct ip *ip4 = NULL; /* Keep compiler happy. */
390 void *l3hdr = NULL; /* Keep compiler happy. */
393 int error, ip_len, l;
394 uint16_t eh_type, tcp_data_len;
397 /* We expect a contiguous header [eh, ip, tcp]. */
399 eh = mtod(m, struct ether_header *);
400 eh_type = ntohs(eh->ether_type);
405 CURVNET_SET(lc->ifp->if_vnet);
406 if (V_ip6_forwarding != 0) {
407 /* XXX-BZ stats but changing lro_ctrl is a problem. */
409 return (TCP_LRO_CANNOT);
412 l3hdr = ip6 = (struct ip6_hdr *)(eh + 1);
413 error = tcp_lro_rx_ipv6(lc, m, ip6, &th);
416 tcp_data_len = ntohs(ip6->ip6_plen);
417 ip_len = sizeof(*ip6) + tcp_data_len;
424 CURVNET_SET(lc->ifp->if_vnet);
425 if (V_ipforwarding != 0) {
426 /* XXX-BZ stats but changing lro_ctrl is a problem. */
428 return (TCP_LRO_CANNOT);
431 l3hdr = ip4 = (struct ip *)(eh + 1);
432 error = tcp_lro_rx_ipv4(lc, m, ip4, &th);
435 ip_len = ntohs(ip4->ip_len);
436 tcp_data_len = ip_len - sizeof(*ip4);
440 /* XXX-BZ what happens in case of VLAN(s)? */
442 return (TCP_LRO_NOT_SUPPORTED);
446 * If the frame is padded beyond the end of the IP packet, then we must
447 * trim the extra bytes off.
449 l = m->m_pkthdr.len - (ETHER_HDR_LEN + ip_len);
452 /* Truncated packet. */
453 return (TCP_LRO_CANNOT);
459 * Check TCP header constraints.
461 /* Ensure no bits set besides ACK or PSH. */
462 if ((th->th_flags & ~(TH_ACK | TH_PUSH)) != 0) {
463 if (th->th_flags & TH_SYN)
464 return (TCP_LRO_CANNOT);
466 * Make sure that previously seen segements/ACKs are delivered
467 * before this segement, e.g. FIN.
472 /* XXX-BZ We lose a ACK|PUSH flag concatenating multiple segments. */
473 /* XXX-BZ Ideally we'd flush on PUSH? */
476 * Check for timestamps.
477 * Since the only option we handle are timestamps, we only have to
478 * handle the simple case of aligned timestamps.
480 l = (th->th_off << 2);
483 ts_ptr = (uint32_t *)(th + 1);
484 if (l != 0 && (__predict_false(l != TCPOLEN_TSTAMP_APPA) ||
485 (*ts_ptr != ntohl(TCPOPT_NOP<<24|TCPOPT_NOP<<16|
486 TCPOPT_TIMESTAMP<<8|TCPOLEN_TIMESTAMP)))) {
488 * Make sure that previously seen segements/ACKs are delivered
489 * before this segement.
494 /* If the driver did not pass in the checksum, set it now. */
498 seq = ntohl(th->th_seq);
500 /* Try to find a matching previous segment. */
501 SLIST_FOREACH(le, &lc->lro_active, next) {
502 if (le->eh_type != eh_type)
504 if (le->source_port != th->th_sport ||
505 le->dest_port != th->th_dport)
510 if (bcmp(&le->source_ip6, &ip6->ip6_src,
511 sizeof(struct in6_addr)) != 0 ||
512 bcmp(&le->dest_ip6, &ip6->ip6_dst,
513 sizeof(struct in6_addr)) != 0)
519 if (le->source_ip4 != ip4->ip_src.s_addr ||
520 le->dest_ip4 != ip4->ip_dst.s_addr)
527 /* Timestamps mismatch; this is a FIN, etc */
528 SLIST_REMOVE(&lc->lro_active, le, lro_entry, next);
529 tcp_lro_flush(lc, le);
530 return (TCP_LRO_CANNOT);
533 /* Flush now if appending will result in overflow. */
534 if (le->p_len > (65535 - tcp_data_len)) {
535 SLIST_REMOVE(&lc->lro_active, le, lro_entry, next);
536 tcp_lro_flush(lc, le);
540 /* Try to append the new segment. */
541 if (__predict_false(seq != le->next_seq ||
542 (tcp_data_len == 0 && le->ack_seq == th->th_ack))) {
543 /* Out of order packet or duplicate ACK. */
544 SLIST_REMOVE(&lc->lro_active, le, lro_entry, next);
545 tcp_lro_flush(lc, le);
546 return (TCP_LRO_CANNOT);
550 uint32_t tsval = ntohl(*(ts_ptr + 1));
551 /* Make sure timestamp values are increasing. */
552 /* XXX-BZ flip and use TSTMP_GEQ macro for this? */
553 if (__predict_false(le->tsval > tsval ||
555 return (TCP_LRO_CANNOT);
557 le->tsecr = *(ts_ptr + 2);
560 le->next_seq += tcp_data_len;
561 le->ack_seq = th->th_ack;
562 le->window = th->th_win;
565 #ifdef TCP_LRO_UPDATE_CSUM
566 le->ulp_csum += tcp_lro_rx_csum_fixup(le, l3hdr, th,
567 tcp_data_len, ~csum);
570 if (tcp_data_len == 0) {
575 le->p_len += tcp_data_len;
578 * Adjust the mbuf so that m_data points to the first byte of
579 * the ULP payload. Adjust the mbuf to avoid complications and
580 * append new segment to existing mbuf chain.
582 m_adj(m, m->m_pkthdr.len - tcp_data_len);
583 m->m_flags &= ~M_PKTHDR;
585 le->m_tail->m_next = m;
586 le->m_tail = m_last(m);
589 * If a possible next full length packet would cause an
590 * overflow, pro-actively flush now.
592 if (le->p_len > (65535 - lc->ifp->if_mtu)) {
593 SLIST_REMOVE(&lc->lro_active, le, lro_entry, next);
594 tcp_lro_flush(lc, le);
596 getmicrotime(&le->mtime);
603 * Nothing to flush, but this segment can not be further
604 * aggregated/delayed.
606 return (TCP_LRO_CANNOT);
609 /* Try to find an empty slot. */
610 if (SLIST_EMPTY(&lc->lro_free))
611 return (TCP_LRO_NO_ENTRIES);
613 /* Start a new segment chain. */
614 le = SLIST_FIRST(&lc->lro_free);
615 SLIST_REMOVE_HEAD(&lc->lro_free, next);
616 SLIST_INSERT_HEAD(&lc->lro_active, le, next);
617 getmicrotime(&le->mtime);
619 /* Start filling in details. */
624 le->source_ip6 = ip6->ip6_src;
625 le->dest_ip6 = ip6->ip6_dst;
626 le->eh_type = eh_type;
627 le->p_len = m->m_pkthdr.len - ETHER_HDR_LEN - sizeof(*ip6);
633 le->source_ip4 = ip4->ip_src.s_addr;
634 le->dest_ip4 = ip4->ip_dst.s_addr;
635 le->eh_type = eh_type;
636 le->p_len = m->m_pkthdr.len - ETHER_HDR_LEN;
640 le->source_port = th->th_sport;
641 le->dest_port = th->th_dport;
643 le->next_seq = seq + tcp_data_len;
644 le->ack_seq = th->th_ack;
645 le->window = th->th_win;
648 le->tsval = ntohl(*(ts_ptr + 1));
649 le->tsecr = *(ts_ptr + 2);
652 #ifdef TCP_LRO_UPDATE_CSUM
654 * Do not touch the csum of the first packet. However save the
655 * "adjusted" checksum of just the source and destination addresses,
656 * the next header and the TCP payload. The length and TCP header
657 * parts may change, so we remove those from the saved checksum and
658 * re-add with final values on tcp_lro_flush() if needed.
660 KASSERT(le->ulp_csum == 0, ("%s: le=%p le->ulp_csum=0x%04x\n",
661 __func__, le, le->ulp_csum));
663 le->ulp_csum = tcp_lro_rx_csum_fixup(le, l3hdr, th, tcp_data_len,
665 th->th_sum = csum; /* Restore checksum on first packet. */
669 le->m_tail = m_last(m);