FreeBSD kernel IPv4 code
tcp_lro.c
Go to the documentation of this file.
1/*-
2 * SPDX-License-Identifier: BSD-2-Clause-FreeBSD
3 *
4 * Copyright (c) 2007, Myricom Inc.
5 * Copyright (c) 2008, Intel Corporation.
6 * Copyright (c) 2012 The FreeBSD Foundation
7 * Copyright (c) 2016-2021 Mellanox Technologies.
8 * All rights reserved.
9 *
10 * Portions of this software were developed by Bjoern Zeeb
11 * under sponsorship from the FreeBSD Foundation.
12 *
13 * Redistribution and use in source and binary forms, with or without
14 * modification, are permitted provided that the following conditions
15 * are met:
16 * 1. Redistributions of source code must retain the above copyright
17 * notice, this list of conditions and the following disclaimer.
18 * 2. Redistributions in binary form must reproduce the above copyright
19 * notice, this list of conditions and the following disclaimer in the
20 * documentation and/or other materials provided with the distribution.
21 *
22 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
23 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
24 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
25 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
26 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
27 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
28 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
29 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
30 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
31 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
32 * SUCH DAMAGE.
33 */
34
35#include <sys/cdefs.h>
36__FBSDID("$FreeBSD$");
37
38#include "opt_inet.h"
39#include "opt_inet6.h"
40
41#include <sys/param.h>
42#include <sys/systm.h>
43#include <sys/kernel.h>
44#include <sys/malloc.h>
45#include <sys/mbuf.h>
46#include <sys/socket.h>
47#include <sys/socketvar.h>
48#include <sys/sockbuf.h>
49#include <sys/sysctl.h>
50
51#include <net/if.h>
52#include <net/if_var.h>
53#include <net/ethernet.h>
54#include <net/bpf.h>
55#include <net/vnet.h>
56
57#include <netinet/in_systm.h>
58#include <netinet/in.h>
59#include <netinet/ip6.h>
60#include <netinet/ip.h>
61#include <netinet/ip_var.h>
62#include <netinet/in_pcb.h>
63#include <netinet6/in6_pcb.h>
64#include <netinet/tcp.h>
65#include <netinet/tcp_seq.h>
66#include <netinet/tcp_lro.h>
67#include <netinet/tcp_var.h>
68#include <netinet/tcpip.h>
69#include <netinet/tcp_hpts.h>
70#include <netinet/tcp_log_buf.h>
71#include <netinet/udp.h>
72#include <netinet6/ip6_var.h>
73
74#include <machine/in_cksum.h>
75
76static MALLOC_DEFINE(M_LRO, "LRO", "LRO control structures");
77
78#define TCP_LRO_TS_OPTION \
79 ntohl((TCPOPT_NOP << 24) | (TCPOPT_NOP << 16) | \
80 (TCPOPT_TIMESTAMP << 8) | TCPOLEN_TIMESTAMP)
81
82static void tcp_lro_rx_done(struct lro_ctrl *lc);
83static int tcp_lro_rx_common(struct lro_ctrl *lc, struct mbuf *m,
84 uint32_t csum, bool use_hash);
85
86#ifdef TCPHPTS
87static bool do_bpf_strip_and_compress(struct inpcb *, struct lro_ctrl *,
88 struct lro_entry *, struct mbuf **, struct mbuf **, struct mbuf **, bool *, bool);
89
90#endif
91
92SYSCTL_NODE(_net_inet_tcp, OID_AUTO, lro, CTLFLAG_RW | CTLFLAG_MPSAFE, 0,
93 "TCP LRO");
94
100counter_u64_t tcp_extra_mbuf;
101counter_u64_t tcp_would_have_but;
102counter_u64_t tcp_comp_total;
103counter_u64_t tcp_uncomp_total;
104counter_u64_t tcp_bad_csums;
105
107SYSCTL_UINT(_net_inet_tcp_lro, OID_AUTO, entries,
108 CTLFLAG_RDTUN | CTLFLAG_MPSAFE, &tcp_lro_entries, 0,
109 "default number of LRO entries");
110
112SYSCTL_UINT(_net_inet_tcp_lro, OID_AUTO, lro_cpu_threshold,
113 CTLFLAG_RDTUN | CTLFLAG_MPSAFE, &tcp_lro_cpu_set_thresh, 0,
114 "Number of interrups in a row on the same CPU that will make us declare an 'affinity' cpu?");
115
116SYSCTL_COUNTER_U64(_net_inet_tcp_lro, OID_AUTO, fullqueue, CTLFLAG_RD,
117 &tcp_inp_lro_direct_queue, "Number of lro's fully queued to transport");
118SYSCTL_COUNTER_U64(_net_inet_tcp_lro, OID_AUTO, wokeup, CTLFLAG_RD,
119 &tcp_inp_lro_wokeup_queue, "Number of lro's where we woke up transport via hpts");
120SYSCTL_COUNTER_U64(_net_inet_tcp_lro, OID_AUTO, compressed, CTLFLAG_RD,
121 &tcp_inp_lro_compressed, "Number of lro's compressed and sent to transport");
122SYSCTL_COUNTER_U64(_net_inet_tcp_lro, OID_AUTO, lockcnt, CTLFLAG_RD,
123 &tcp_inp_lro_locks_taken, "Number of lro's inp_wlocks taken");
124SYSCTL_COUNTER_U64(_net_inet_tcp_lro, OID_AUTO, extra_mbuf, CTLFLAG_RD,
125 &tcp_extra_mbuf, "Number of times we had an extra compressed ack dropped into the tp");
126SYSCTL_COUNTER_U64(_net_inet_tcp_lro, OID_AUTO, would_have_but, CTLFLAG_RD,
127 &tcp_would_have_but, "Number of times we would have had an extra compressed, but mget failed");
128SYSCTL_COUNTER_U64(_net_inet_tcp_lro, OID_AUTO, with_m_ackcmp, CTLFLAG_RD,
129 &tcp_comp_total, "Number of mbufs queued with M_ACKCMP flags set");
130SYSCTL_COUNTER_U64(_net_inet_tcp_lro, OID_AUTO, without_m_ackcmp, CTLFLAG_RD,
131 &tcp_uncomp_total, "Number of mbufs queued without M_ACKCMP");
132SYSCTL_COUNTER_U64(_net_inet_tcp_lro, OID_AUTO, lro_badcsum, CTLFLAG_RD,
133 &tcp_bad_csums, "Number of packets that the common code saw with bad csums");
134
135void
137{
138 atomic_fetchadd_long(&tcplro_stacks_wanting_mbufq, 1);
139}
140
141void
143{
144 atomic_fetchadd_long(&tcplro_stacks_wanting_mbufq, -1);
145}
146
147static __inline void
148tcp_lro_active_insert(struct lro_ctrl *lc, struct lro_head *bucket,
149 struct lro_entry *le)
150{
151
152 LIST_INSERT_HEAD(&lc->lro_active, le, next);
153 LIST_INSERT_HEAD(bucket, le, hash_next);
154}
155
156static __inline void
158{
159
160 LIST_REMOVE(le, next); /* active list */
161 LIST_REMOVE(le, hash_next); /* hash bucket */
162}
163
164int
166{
167 return (tcp_lro_init_args(lc, NULL, tcp_lro_entries, 0));
168}
169
170int
171tcp_lro_init_args(struct lro_ctrl *lc, struct ifnet *ifp,
172 unsigned lro_entries, unsigned lro_mbufs)
173{
174 struct lro_entry *le;
175 size_t size;
176 unsigned i, elements;
177
178 lc->lro_bad_csum = 0;
179 lc->lro_queued = 0;
180 lc->lro_flushed = 0;
181 lc->lro_mbuf_count = 0;
182 lc->lro_mbuf_max = lro_mbufs;
183 lc->lro_cnt = lro_entries;
186 lc->ifp = ifp;
187 LIST_INIT(&lc->lro_free);
188 LIST_INIT(&lc->lro_active);
189
190 /* create hash table to accelerate entry lookup */
191 if (lro_entries > lro_mbufs)
192 elements = lro_entries;
193 else
194 elements = lro_mbufs;
195 lc->lro_hash = phashinit_flags(elements, M_LRO, &lc->lro_hashsz,
196 HASH_NOWAIT);
197 if (lc->lro_hash == NULL) {
198 memset(lc, 0, sizeof(*lc));
199 return (ENOMEM);
200 }
201
202 /* compute size to allocate */
203 size = (lro_mbufs * sizeof(struct lro_mbuf_sort)) +
204 (lro_entries * sizeof(*le));
205 lc->lro_mbuf_data = (struct lro_mbuf_sort *)
206 malloc(size, M_LRO, M_NOWAIT | M_ZERO);
207
208 /* check for out of memory */
209 if (lc->lro_mbuf_data == NULL) {
210 free(lc->lro_hash, M_LRO);
211 memset(lc, 0, sizeof(*lc));
212 return (ENOMEM);
213 }
214 /* compute offset for LRO entries */
215 le = (struct lro_entry *)
216 (lc->lro_mbuf_data + lro_mbufs);
217
218 /* setup linked list */
219 for (i = 0; i != lro_entries; i++)
220 LIST_INSERT_HEAD(&lc->lro_free, le + i, next);
221
222 return (0);
223}
224
228};
229
230static inline void *
231tcp_lro_low_level_parser(void *ptr, struct lro_parser *parser, bool update_data, bool is_vxlan, int mlen)
232{
233 const struct ether_vlan_header *eh;
234 void *old;
235 uint16_t eth_type;
236
237 if (update_data)
238 memset(parser, 0, sizeof(*parser));
239
240 old = ptr;
241
242 if (is_vxlan) {
243 const struct vxlan_header *vxh;
244 vxh = ptr;
245 ptr = (uint8_t *)ptr + sizeof(*vxh);
246 if (update_data) {
247 parser->data.vxlan_vni =
248 vxh->vxlh_vni & htonl(0xffffff00);
249 }
250 }
251
252 eh = ptr;
253 if (__predict_false(eh->evl_encap_proto == htons(ETHERTYPE_VLAN))) {
254 eth_type = eh->evl_proto;
255 if (update_data) {
256 /* strip priority and keep VLAN ID only */
257 parser->data.vlan_id = eh->evl_tag & htons(EVL_VLID_MASK);
258 }
259 /* advance to next header */
260 ptr = (uint8_t *)ptr + ETHER_HDR_LEN + ETHER_VLAN_ENCAP_LEN;
261 mlen -= (ETHER_HDR_LEN + ETHER_VLAN_ENCAP_LEN);
262 } else {
263 eth_type = eh->evl_encap_proto;
264 /* advance to next header */
265 mlen -= ETHER_HDR_LEN;
266 ptr = (uint8_t *)ptr + ETHER_HDR_LEN;
267 }
268 if (__predict_false(mlen <= 0))
269 return (NULL);
270 switch (eth_type) {
271#ifdef INET
272 case htons(ETHERTYPE_IP):
273 parser->ip4 = ptr;
274 if (__predict_false(mlen < sizeof(struct ip)))
275 return (NULL);
276 /* Ensure there are no IPv4 options. */
277 if ((parser->ip4->ip_hl << 2) != sizeof (*parser->ip4))
278 break;
279 /* .. and the packet is not fragmented. */
280 if (parser->ip4->ip_off & htons(IP_MF|IP_OFFMASK))
281 break;
282 ptr = (uint8_t *)ptr + (parser->ip4->ip_hl << 2);
283 mlen -= sizeof(struct ip);
284 if (update_data) {
285 parser->data.s_addr.v4 = parser->ip4->ip_src;
286 parser->data.d_addr.v4 = parser->ip4->ip_dst;
287 }
288 switch (parser->ip4->ip_p) {
289 case IPPROTO_UDP:
290 if (__predict_false(mlen < sizeof(struct udphdr)))
291 return (NULL);
292 parser->udp = ptr;
293 if (update_data) {
295 parser->data.s_port = parser->udp->uh_sport;
296 parser->data.d_port = parser->udp->uh_dport;
297 } else {
298 MPASS(parser->data.lro_type == LRO_TYPE_IPV4_UDP);
299 }
300 ptr = ((uint8_t *)ptr + sizeof(*parser->udp));
301 parser->total_hdr_len = (uint8_t *)ptr - (uint8_t *)old;
302 return (ptr);
303 case IPPROTO_TCP:
304 parser->tcp = ptr;
305 if (__predict_false(mlen < sizeof(struct tcphdr)))
306 return (NULL);
307 if (update_data) {
309 parser->data.s_port = parser->tcp->th_sport;
310 parser->data.d_port = parser->tcp->th_dport;
311 } else {
312 MPASS(parser->data.lro_type == LRO_TYPE_IPV4_TCP);
313 }
314 if (__predict_false(mlen < (parser->tcp->th_off << 2)))
315 return (NULL);
316 ptr = (uint8_t *)ptr + (parser->tcp->th_off << 2);
317 parser->total_hdr_len = (uint8_t *)ptr - (uint8_t *)old;
318 return (ptr);
319 default:
320 break;
321 }
322 break;
323#endif
324#ifdef INET6
325 case htons(ETHERTYPE_IPV6):
326 parser->ip6 = ptr;
327 if (__predict_false(mlen < sizeof(struct ip6_hdr)))
328 return (NULL);
329 ptr = (uint8_t *)ptr + sizeof(*parser->ip6);
330 if (update_data) {
331 parser->data.s_addr.v6 = parser->ip6->ip6_src;
332 parser->data.d_addr.v6 = parser->ip6->ip6_dst;
333 }
334 mlen -= sizeof(struct ip6_hdr);
335 switch (parser->ip6->ip6_nxt) {
336 case IPPROTO_UDP:
337 if (__predict_false(mlen < sizeof(struct udphdr)))
338 return (NULL);
339 parser->udp = ptr;
340 if (update_data) {
342 parser->data.s_port = parser->udp->uh_sport;
343 parser->data.d_port = parser->udp->uh_dport;
344 } else {
345 MPASS(parser->data.lro_type == LRO_TYPE_IPV6_UDP);
346 }
347 ptr = (uint8_t *)ptr + sizeof(*parser->udp);
348 parser->total_hdr_len = (uint8_t *)ptr - (uint8_t *)old;
349 return (ptr);
350 case IPPROTO_TCP:
351 if (__predict_false(mlen < sizeof(struct tcphdr)))
352 return (NULL);
353 parser->tcp = ptr;
354 if (update_data) {
356 parser->data.s_port = parser->tcp->th_sport;
357 parser->data.d_port = parser->tcp->th_dport;
358 } else {
359 MPASS(parser->data.lro_type == LRO_TYPE_IPV6_TCP);
360 }
361 if (__predict_false(mlen < (parser->tcp->th_off << 2)))
362 return (NULL);
363 ptr = (uint8_t *)ptr + (parser->tcp->th_off << 2);
364 parser->total_hdr_len = (uint8_t *)ptr - (uint8_t *)old;
365 return (ptr);
366 default:
367 break;
368 }
369 break;
370#endif
371 default:
372 break;
373 }
374 /* Invalid packet - cannot parse */
375 return (NULL);
376}
377
378static const int vxlan_csum = CSUM_INNER_L3_CALC | CSUM_INNER_L3_VALID |
379 CSUM_INNER_L4_CALC | CSUM_INNER_L4_VALID;
380
381static inline struct lro_parser *
382tcp_lro_parser(struct mbuf *m, struct lro_parser *po, struct lro_parser *pi, bool update_data)
383{
384 void *data_ptr;
385
386 /* Try to parse outer headers first. */
387 data_ptr = tcp_lro_low_level_parser(m->m_data, po, update_data, false, m->m_len);
388 if (data_ptr == NULL || po->total_hdr_len > m->m_len)
389 return (NULL);
390
391 if (update_data) {
392 /* Store VLAN ID, if any. */
393 if (__predict_false(m->m_flags & M_VLANTAG)) {
394 po->data.vlan_id =
395 htons(m->m_pkthdr.ether_vtag) & htons(EVL_VLID_MASK);
396 }
397 /* Store decrypted flag, if any. */
398 if (__predict_false((m->m_pkthdr.csum_flags &
399 CSUM_TLS_MASK) == CSUM_TLS_DECRYPTED))
401 }
402
403 switch (po->data.lro_type) {
406 /* Check for VXLAN headers. */
407 if ((m->m_pkthdr.csum_flags & vxlan_csum) != vxlan_csum)
408 break;
409
410 /* Try to parse inner headers. */
411 data_ptr = tcp_lro_low_level_parser(data_ptr, pi, update_data, true,
412 (m->m_len - ((caddr_t)data_ptr - m->m_data)));
413 if (data_ptr == NULL || (pi->total_hdr_len + po->total_hdr_len) > m->m_len)
414 break;
415
416 /* Verify supported header types. */
417 switch (pi->data.lro_type) {
420 return (pi);
421 default:
422 break;
423 }
424 break;
427 if (update_data)
428 memset(pi, 0, sizeof(*pi));
429 return (po);
430 default:
431 break;
432 }
433 return (NULL);
434}
435
436static inline int
437tcp_lro_trim_mbuf_chain(struct mbuf *m, const struct lro_parser *po)
438{
439 int len;
440
441 switch (po->data.lro_type) {
442#ifdef INET
444 len = ((uint8_t *)po->ip4 - (uint8_t *)m->m_data) +
445 ntohs(po->ip4->ip_len);
446 break;
447#endif
448#ifdef INET6
450 len = ((uint8_t *)po->ip6 - (uint8_t *)m->m_data) +
451 ntohs(po->ip6->ip6_plen) + sizeof(*po->ip6);
452 break;
453#endif
454 default:
455 return (TCP_LRO_CANNOT);
456 }
457
458 /*
459 * If the frame is padded beyond the end of the IP packet,
460 * then trim the extra bytes off:
461 */
462 if (__predict_true(m->m_pkthdr.len == len)) {
463 return (0);
464 } else if (m->m_pkthdr.len > len) {
465 m_adj(m, len - m->m_pkthdr.len);
466 return (0);
467 }
468 return (TCP_LRO_CANNOT);
469}
470
471static struct tcphdr *
472tcp_lro_get_th(struct mbuf *m)
473{
474 return ((struct tcphdr *)((uint8_t *)m->m_data + m->m_pkthdr.lro_tcp_h_off));
475}
476
477static void
478lro_free_mbuf_chain(struct mbuf *m)
479{
480 struct mbuf *save;
481
482 while (m) {
483 save = m->m_nextpkt;
484 m->m_nextpkt = NULL;
485 m_freem(m);
486 m = save;
487 }
488}
489
490void
492{
493 struct lro_entry *le;
494 unsigned x;
495
496 /* reset LRO free list */
497 LIST_INIT(&lc->lro_free);
498
499 /* free active mbufs, if any */
500 while ((le = LIST_FIRST(&lc->lro_active)) != NULL) {
503 }
504
505 /* free hash table */
506 free(lc->lro_hash, M_LRO);
507 lc->lro_hash = NULL;
508 lc->lro_hashsz = 0;
509
510 /* free mbuf array, if any */
511 for (x = 0; x != lc->lro_mbuf_count; x++)
512 m_freem(lc->lro_mbuf_data[x].mb);
513 lc->lro_mbuf_count = 0;
514
515 /* free allocated memory, if any */
516 free(lc->lro_mbuf_data, M_LRO);
517 lc->lro_mbuf_data = NULL;
518}
519
520static uint16_t
521tcp_lro_rx_csum_tcphdr(const struct tcphdr *th)
522{
523 const uint16_t *ptr;
524 uint32_t csum;
525 uint16_t len;
526
527 csum = -th->th_sum; /* exclude checksum field */
528 len = th->th_off;
529 ptr = (const uint16_t *)th;
530 while (len--) {
531 csum += *ptr;
532 ptr++;
533 csum += *ptr;
534 ptr++;
535 }
536 while (csum > 0xffff)
537 csum = (csum >> 16) + (csum & 0xffff);
538
539 return (csum);
540}
541
542static uint16_t
543tcp_lro_rx_csum_data(const struct lro_parser *pa, uint16_t tcp_csum)
544{
545 uint32_t c;
546 uint16_t cs;
547
548 c = tcp_csum;
549
550 switch (pa->data.lro_type) {
551#ifdef INET6
553 /* Compute full pseudo IPv6 header checksum. */
554 cs = in6_cksum_pseudo(pa->ip6, ntohs(pa->ip6->ip6_plen), pa->ip6->ip6_nxt, 0);
555 break;
556#endif
557#ifdef INET
559 /* Compute full pseudo IPv4 header checsum. */
560 cs = in_addword(ntohs(pa->ip4->ip_len) - sizeof(*pa->ip4), IPPROTO_TCP);
561 cs = in_pseudo(pa->ip4->ip_src.s_addr, pa->ip4->ip_dst.s_addr, htons(cs));
562 break;
563#endif
564 default:
565 cs = 0; /* Keep compiler happy. */
566 break;
567 }
568
569 /* Complement checksum. */
570 cs = ~cs;
571 c += cs;
572
573 /* Remove TCP header checksum. */
574 cs = ~tcp_lro_rx_csum_tcphdr(pa->tcp);
575 c += cs;
576
577 /* Compute checksum remainder. */
578 while (c > 0xffff)
579 c = (c >> 16) + (c & 0xffff);
580
581 return (c);
582}
583
584static void
586{
587 struct lro_entry *le;
588
589 while ((le = LIST_FIRST(&lc->lro_active)) != NULL) {
591 tcp_lro_flush(lc, le);
592 }
593}
594
595void
596tcp_lro_flush_inactive(struct lro_ctrl *lc, const struct timeval *timeout)
597{
598 struct lro_entry *le, *le_tmp;
599 uint64_t now, tov;
600 struct bintime bt;
601
602 NET_EPOCH_ASSERT();
603 if (LIST_EMPTY(&lc->lro_active))
604 return;
605
606 /* get timeout time and current time in ns */
607 binuptime(&bt);
608 now = bintime2ns(&bt);
609 tov = ((timeout->tv_sec * 1000000000) + (timeout->tv_usec * 1000));
610 LIST_FOREACH_SAFE(le, &lc->lro_active, next, le_tmp) {
611 if (now >= (bintime2ns(&le->alloc_time) + tov)) {
613 tcp_lro_flush(lc, le);
614 }
615 }
616}
617
618#ifdef INET
619static int
620tcp_lro_rx_ipv4(struct lro_ctrl *lc, struct mbuf *m, struct ip *ip4)
621{
622 uint16_t csum;
623
624 /* Legacy IP has a header checksum that needs to be correct. */
625 if (m->m_pkthdr.csum_flags & CSUM_IP_CHECKED) {
626 if (__predict_false((m->m_pkthdr.csum_flags & CSUM_IP_VALID) == 0)) {
627 lc->lro_bad_csum++;
628 return (TCP_LRO_CANNOT);
629 }
630 } else {
631 csum = in_cksum_hdr(ip4);
632 if (__predict_false(csum != 0)) {
633 lc->lro_bad_csum++;
634 return (TCP_LRO_CANNOT);
635 }
636 }
637 return (0);
638}
639#endif
640
641#ifdef TCPHPTS
642static void
643tcp_lro_log(struct tcpcb *tp, const struct lro_ctrl *lc,
644 const struct lro_entry *le, const struct mbuf *m,
645 int frm, int32_t tcp_data_len, uint32_t th_seq,
646 uint32_t th_ack, uint16_t th_win)
647{
648 if (tp->t_logstate != TCP_LOG_STATE_OFF) {
649 union tcp_log_stackspecific log;
650 struct timeval tv, btv;
651 uint32_t cts;
652
653 cts = tcp_get_usecs(&tv);
654 memset(&log, 0, sizeof(union tcp_log_stackspecific));
655 log.u_bbr.flex8 = frm;
656 log.u_bbr.flex1 = tcp_data_len;
657 if (m)
658 log.u_bbr.flex2 = m->m_pkthdr.len;
659 else
660 log.u_bbr.flex2 = 0;
661 log.u_bbr.flex3 = le->m_head->m_pkthdr.lro_nsegs;
662 log.u_bbr.flex4 = le->m_head->m_pkthdr.lro_tcp_d_len;
663 if (le->m_head) {
664 log.u_bbr.flex5 = le->m_head->m_pkthdr.len;
665 log.u_bbr.delRate = le->m_head->m_flags;
666 log.u_bbr.rttProp = le->m_head->m_pkthdr.rcv_tstmp;
667 }
668 log.u_bbr.inflight = th_seq;
669 log.u_bbr.delivered = th_ack;
670 log.u_bbr.timeStamp = cts;
671 log.u_bbr.epoch = le->next_seq;
672 log.u_bbr.lt_epoch = le->ack_seq;
673 log.u_bbr.pacing_gain = th_win;
674 log.u_bbr.cwnd_gain = le->window;
675 log.u_bbr.lost = curcpu;
676 log.u_bbr.cur_del_rate = (uintptr_t)m;
677 log.u_bbr.bw_inuse = (uintptr_t)le->m_head;
678 bintime2timeval(&lc->lro_last_queue_time, &btv);
679 log.u_bbr.flex6 = tcp_tv_to_usectick(&btv);
680 log.u_bbr.flex7 = le->compressed;
681 log.u_bbr.pacing_gain = le->uncompressed;
682 if (in_epoch(net_epoch_preempt))
683 log.u_bbr.inhpts = 1;
684 else
685 log.u_bbr.inhpts = 0;
686 TCP_LOG_EVENTP(tp, NULL,
687 &tp->t_inpcb->inp_socket->so_rcv,
688 &tp->t_inpcb->inp_socket->so_snd,
689 TCP_LOG_LRO, 0,
690 0, &log, false, &tv);
691 }
692}
693#endif
694
695static inline void
697{
698 uint32_t csum;
699
700 csum = 0xffff - *ptr + value;
701 while (csum > 0xffff)
702 csum = (csum >> 16) + (csum & 0xffff);
703 *ptr = value;
704 *psum = csum;
705}
706
707static uint16_t
708tcp_lro_update_checksum(const struct lro_parser *pa, const struct lro_entry *le,
709 uint16_t payload_len, uint16_t delta_sum)
710{
711 uint32_t csum;
712 uint16_t tlen;
713 uint16_t temp[5] = {};
714
715 switch (pa->data.lro_type) {
717 /* Compute new IPv4 length. */
718 tlen = (pa->ip4->ip_hl << 2) + (pa->tcp->th_off << 2) + payload_len;
719 tcp_lro_assign_and_checksum_16(&pa->ip4->ip_len, htons(tlen), &temp[0]);
720
721 /* Subtract delta from current IPv4 checksum. */
722 csum = pa->ip4->ip_sum + 0xffff - temp[0];
723 while (csum > 0xffff)
724 csum = (csum >> 16) + (csum & 0xffff);
725 tcp_lro_assign_and_checksum_16(&pa->ip4->ip_sum, csum, &temp[1]);
726 goto update_tcp_header;
727
729 /* Compute new IPv6 length. */
730 tlen = (pa->tcp->th_off << 2) + payload_len;
731 tcp_lro_assign_and_checksum_16(&pa->ip6->ip6_plen, htons(tlen), &temp[0]);
732 goto update_tcp_header;
733
735 /* Compute new IPv4 length. */
736 tlen = (pa->ip4->ip_hl << 2) + sizeof(*pa->udp) + payload_len;
737 tcp_lro_assign_and_checksum_16(&pa->ip4->ip_len, htons(tlen), &temp[0]);
738
739 /* Subtract delta from current IPv4 checksum. */
740 csum = pa->ip4->ip_sum + 0xffff - temp[0];
741 while (csum > 0xffff)
742 csum = (csum >> 16) + (csum & 0xffff);
743 tcp_lro_assign_and_checksum_16(&pa->ip4->ip_sum, csum, &temp[1]);
744 goto update_udp_header;
745
747 /* Compute new IPv6 length. */
748 tlen = sizeof(*pa->udp) + payload_len;
749 tcp_lro_assign_and_checksum_16(&pa->ip6->ip6_plen, htons(tlen), &temp[0]);
750 goto update_udp_header;
751
752 default:
753 return (0);
754 }
755
756update_tcp_header:
757 /* Compute current TCP header checksum. */
758 temp[2] = tcp_lro_rx_csum_tcphdr(pa->tcp);
759
760 /* Incorporate the latest ACK into the TCP header. */
761 pa->tcp->th_ack = le->ack_seq;
762 pa->tcp->th_win = le->window;
763
764 /* Incorporate latest timestamp into the TCP header. */
765 if (le->timestamp != 0) {
766 uint32_t *ts_ptr;
767
768 ts_ptr = (uint32_t *)(pa->tcp + 1);
769 ts_ptr[1] = htonl(le->tsval);
770 ts_ptr[2] = le->tsecr;
771 }
772
773 /* Compute new TCP header checksum. */
774 temp[3] = tcp_lro_rx_csum_tcphdr(pa->tcp);
775
776 /* Compute new TCP checksum. */
777 csum = pa->tcp->th_sum + 0xffff - delta_sum +
778 0xffff - temp[0] + 0xffff - temp[3] + temp[2];
779 while (csum > 0xffff)
780 csum = (csum >> 16) + (csum & 0xffff);
781
782 /* Assign new TCP checksum. */
783 tcp_lro_assign_and_checksum_16(&pa->tcp->th_sum, csum, &temp[4]);
784
785 /* Compute all modififications affecting next checksum. */
786 csum = temp[0] + temp[1] + 0xffff - temp[2] +
787 temp[3] + temp[4] + delta_sum;
788 while (csum > 0xffff)
789 csum = (csum >> 16) + (csum & 0xffff);
790
791 /* Return delta checksum to next stage, if any. */
792 return (csum);
793
794update_udp_header:
795 tlen = sizeof(*pa->udp) + payload_len;
796 /* Assign new UDP length and compute checksum delta. */
797 tcp_lro_assign_and_checksum_16(&pa->udp->uh_ulen, htons(tlen), &temp[2]);
798
799 /* Check if there is a UDP checksum. */
800 if (__predict_false(pa->udp->uh_sum != 0)) {
801 /* Compute new UDP checksum. */
802 csum = pa->udp->uh_sum + 0xffff - delta_sum +
803 0xffff - temp[0] + 0xffff - temp[2];
804 while (csum > 0xffff)
805 csum = (csum >> 16) + (csum & 0xffff);
806 /* Assign new UDP checksum. */
807 tcp_lro_assign_and_checksum_16(&pa->udp->uh_sum, csum, &temp[3]);
808 }
809
810 /* Compute all modififications affecting next checksum. */
811 csum = temp[0] + temp[1] + temp[2] + temp[3] + delta_sum;
812 while (csum > 0xffff)
813 csum = (csum >> 16) + (csum & 0xffff);
814
815 /* Return delta checksum to next stage, if any. */
816 return (csum);
817}
818
819static void
821{
822 /* Check if we need to recompute any checksums. */
823 if (le->needs_merge) {
824 uint16_t csum;
825
826 switch (le->inner.data.lro_type) {
828 csum = tcp_lro_update_checksum(&le->inner, le,
829 le->m_head->m_pkthdr.lro_tcp_d_len,
830 le->m_head->m_pkthdr.lro_tcp_d_csum);
831 csum = tcp_lro_update_checksum(&le->outer, NULL,
832 le->m_head->m_pkthdr.lro_tcp_d_len +
833 le->inner.total_hdr_len, csum);
834 le->m_head->m_pkthdr.csum_flags = CSUM_DATA_VALID |
835 CSUM_PSEUDO_HDR | CSUM_IP_CHECKED | CSUM_IP_VALID;
836 le->m_head->m_pkthdr.csum_data = 0xffff;
837 if (__predict_false(le->outer.data.lro_flags & LRO_FLAG_DECRYPTED))
838 le->m_head->m_pkthdr.csum_flags |= CSUM_TLS_DECRYPTED;
839 break;
841 csum = tcp_lro_update_checksum(&le->inner, le,
842 le->m_head->m_pkthdr.lro_tcp_d_len,
843 le->m_head->m_pkthdr.lro_tcp_d_csum);
844 csum = tcp_lro_update_checksum(&le->outer, NULL,
845 le->m_head->m_pkthdr.lro_tcp_d_len +
846 le->inner.total_hdr_len, csum);
847 le->m_head->m_pkthdr.csum_flags = CSUM_DATA_VALID |
848 CSUM_PSEUDO_HDR;
849 le->m_head->m_pkthdr.csum_data = 0xffff;
850 if (__predict_false(le->outer.data.lro_flags & LRO_FLAG_DECRYPTED))
851 le->m_head->m_pkthdr.csum_flags |= CSUM_TLS_DECRYPTED;
852 break;
853 case LRO_TYPE_NONE:
854 switch (le->outer.data.lro_type) {
856 csum = tcp_lro_update_checksum(&le->outer, le,
857 le->m_head->m_pkthdr.lro_tcp_d_len,
858 le->m_head->m_pkthdr.lro_tcp_d_csum);
859 le->m_head->m_pkthdr.csum_flags = CSUM_DATA_VALID |
860 CSUM_PSEUDO_HDR | CSUM_IP_CHECKED | CSUM_IP_VALID;
861 le->m_head->m_pkthdr.csum_data = 0xffff;
862 if (__predict_false(le->outer.data.lro_flags & LRO_FLAG_DECRYPTED))
863 le->m_head->m_pkthdr.csum_flags |= CSUM_TLS_DECRYPTED;
864 break;
866 csum = tcp_lro_update_checksum(&le->outer, le,
867 le->m_head->m_pkthdr.lro_tcp_d_len,
868 le->m_head->m_pkthdr.lro_tcp_d_csum);
869 le->m_head->m_pkthdr.csum_flags = CSUM_DATA_VALID |
870 CSUM_PSEUDO_HDR;
871 le->m_head->m_pkthdr.csum_data = 0xffff;
872 if (__predict_false(le->outer.data.lro_flags & LRO_FLAG_DECRYPTED))
873 le->m_head->m_pkthdr.csum_flags |= CSUM_TLS_DECRYPTED;
874 break;
875 default:
876 break;
877 }
878 break;
879 default:
880 break;
881 }
882 }
883
884 /*
885 * Break any chain, this is not set to NULL on the singleton
886 * case m_nextpkt points to m_head. Other case set them
887 * m_nextpkt to NULL in push_and_replace.
888 */
889 le->m_head->m_nextpkt = NULL;
890 lc->lro_queued += le->m_head->m_pkthdr.lro_nsegs;
891 (*lc->ifp->if_input)(lc->ifp, le->m_head);
892}
893
894static void
896 struct mbuf *m, struct tcphdr *th)
897{
898 uint32_t *ts_ptr;
899 uint16_t tcp_data_len;
900 uint16_t tcp_opt_len;
901
902 ts_ptr = (uint32_t *)(th + 1);
903 tcp_opt_len = (th->th_off << 2);
904 tcp_opt_len -= sizeof(*th);
905
906 /* Check if there is a timestamp option. */
907 if (tcp_opt_len == 0 ||
908 __predict_false(tcp_opt_len != TCPOLEN_TSTAMP_APPA ||
909 *ts_ptr != TCP_LRO_TS_OPTION)) {
910 /* We failed to find the timestamp option. */
911 le->timestamp = 0;
912 } else {
913 le->timestamp = 1;
914 le->tsval = ntohl(*(ts_ptr + 1));
915 le->tsecr = *(ts_ptr + 2);
916 }
917
918 tcp_data_len = m->m_pkthdr.lro_tcp_d_len;
919
920 /* Pull out TCP sequence numbers and window size. */
921 le->next_seq = ntohl(th->th_seq) + tcp_data_len;
922 le->ack_seq = th->th_ack;
923 le->window = th->th_win;
924 le->flags = tcp_get_flags(th);
925 le->needs_merge = 0;
926
927 /* Setup new data pointers. */
928 le->m_head = m;
929 le->m_tail = m_last(m);
930}
931
932static void
933tcp_push_and_replace(struct lro_ctrl *lc, struct lro_entry *le, struct mbuf *m)
934{
935 struct lro_parser *pa;
936
937 /*
938 * Push up the stack of the current entry
939 * and replace it with "m".
940 */
941 struct mbuf *msave;
942
943 /* Grab off the next and save it */
944 msave = le->m_head->m_nextpkt;
945 le->m_head->m_nextpkt = NULL;
946
947 /* Now push out the old entry */
948 tcp_flush_out_entry(lc, le);
949
950 /* Re-parse new header, should not fail. */
951 pa = tcp_lro_parser(m, &le->outer, &le->inner, false);
952 KASSERT(pa != NULL,
953 ("tcp_push_and_replace: LRO parser failed on m=%p\n", m));
954
955 /*
956 * Now to replace the data properly in the entry
957 * we have to reset the TCP header and
958 * other fields.
959 */
960 tcp_set_entry_to_mbuf(lc, le, m, pa->tcp);
961
962 /* Restore the next list */
963 m->m_nextpkt = msave;
964}
965
966static void
967tcp_lro_mbuf_append_pkthdr(struct lro_entry *le, const struct mbuf *p)
968{
969 struct mbuf *m;
970 uint32_t csum;
971
972 m = le->m_head;
973 if (m->m_pkthdr.lro_nsegs == 1) {
974 /* Compute relative checksum. */
975 csum = p->m_pkthdr.lro_tcp_d_csum;
976 } else {
977 /* Merge TCP data checksums. */
978 csum = (uint32_t)m->m_pkthdr.lro_tcp_d_csum +
979 (uint32_t)p->m_pkthdr.lro_tcp_d_csum;
980 while (csum > 0xffff)
981 csum = (csum >> 16) + (csum & 0xffff);
982 }
983
984 /* Update various counters. */
985 m->m_pkthdr.len += p->m_pkthdr.lro_tcp_d_len;
986 m->m_pkthdr.lro_tcp_d_csum = csum;
987 m->m_pkthdr.lro_tcp_d_len += p->m_pkthdr.lro_tcp_d_len;
988 m->m_pkthdr.lro_nsegs += p->m_pkthdr.lro_nsegs;
989 le->needs_merge = 1;
990}
991
992static void
993tcp_lro_condense(struct lro_ctrl *lc, struct lro_entry *le)
994{
995 /*
996 * Walk through the mbuf chain we
997 * have on tap and compress/condense
998 * as required.
999 */
1000 uint32_t *ts_ptr;
1001 struct mbuf *m;
1002 struct tcphdr *th;
1003 uint32_t tcp_data_len_total;
1004 uint32_t tcp_data_seg_total;
1005 uint16_t tcp_data_len;
1006 uint16_t tcp_opt_len;
1007
1008 /*
1009 * First we must check the lead (m_head)
1010 * we must make sure that it is *not*
1011 * something that should be sent up
1012 * right away (sack etc).
1013 */
1014again:
1015 m = le->m_head->m_nextpkt;
1016 if (m == NULL) {
1017 /* Just one left. */
1018 return;
1019 }
1020
1021 th = tcp_lro_get_th(m);
1022 tcp_opt_len = (th->th_off << 2);
1023 tcp_opt_len -= sizeof(*th);
1024 ts_ptr = (uint32_t *)(th + 1);
1025
1026 if (tcp_opt_len != 0 && __predict_false(tcp_opt_len != TCPOLEN_TSTAMP_APPA ||
1027 *ts_ptr != TCP_LRO_TS_OPTION)) {
1028 /*
1029 * Its not the timestamp. We can't
1030 * use this guy as the head.
1031 */
1032 le->m_head->m_nextpkt = m->m_nextpkt;
1033 tcp_push_and_replace(lc, le, m);
1034 goto again;
1035 }
1036 if ((tcp_get_flags(th) & ~(TH_ACK | TH_PUSH)) != 0) {
1037 /*
1038 * Make sure that previously seen segments/ACKs are delivered
1039 * before this segment, e.g. FIN.
1040 */
1041 le->m_head->m_nextpkt = m->m_nextpkt;
1042 tcp_push_and_replace(lc, le, m);
1043 goto again;
1044 }
1045 while((m = le->m_head->m_nextpkt) != NULL) {
1046 /*
1047 * condense m into le, first
1048 * pull m out of the list.
1049 */
1050 le->m_head->m_nextpkt = m->m_nextpkt;
1051 m->m_nextpkt = NULL;
1052 /* Setup my data */
1053 tcp_data_len = m->m_pkthdr.lro_tcp_d_len;
1054 th = tcp_lro_get_th(m);
1055 ts_ptr = (uint32_t *)(th + 1);
1056 tcp_opt_len = (th->th_off << 2);
1057 tcp_opt_len -= sizeof(*th);
1058 tcp_data_len_total = le->m_head->m_pkthdr.lro_tcp_d_len + tcp_data_len;
1059 tcp_data_seg_total = le->m_head->m_pkthdr.lro_nsegs + m->m_pkthdr.lro_nsegs;
1060
1061 if (tcp_data_seg_total >= lc->lro_ackcnt_lim ||
1062 tcp_data_len_total >= lc->lro_length_lim) {
1063 /* Flush now if appending will result in overflow. */
1064 tcp_push_and_replace(lc, le, m);
1065 goto again;
1066 }
1067 if (tcp_opt_len != 0 &&
1068 __predict_false(tcp_opt_len != TCPOLEN_TSTAMP_APPA ||
1069 *ts_ptr != TCP_LRO_TS_OPTION)) {
1070 /*
1071 * Maybe a sack in the new one? We need to
1072 * start all over after flushing the
1073 * current le. We will go up to the beginning
1074 * and flush it (calling the replace again possibly
1075 * or just returning).
1076 */
1077 tcp_push_and_replace(lc, le, m);
1078 goto again;
1079 }
1080 if ((tcp_get_flags(th) & ~(TH_ACK | TH_PUSH)) != 0) {
1081 tcp_push_and_replace(lc, le, m);
1082 goto again;
1083 }
1084 if (tcp_opt_len != 0) {
1085 uint32_t tsval = ntohl(*(ts_ptr + 1));
1086 /* Make sure timestamp values are increasing. */
1087 if (TSTMP_GT(le->tsval, tsval)) {
1088 tcp_push_and_replace(lc, le, m);
1089 goto again;
1090 }
1091 le->tsval = tsval;
1092 le->tsecr = *(ts_ptr + 2);
1093 }
1094 /* Try to append the new segment. */
1095 if (__predict_false(ntohl(th->th_seq) != le->next_seq ||
1096 ((tcp_get_flags(th) & TH_ACK) !=
1097 (le->flags & TH_ACK)) ||
1098 (tcp_data_len == 0 &&
1099 le->ack_seq == th->th_ack &&
1100 le->window == th->th_win))) {
1101 /* Out of order packet, non-ACK + ACK or dup ACK. */
1102 tcp_push_and_replace(lc, le, m);
1103 goto again;
1104 }
1105 if (tcp_data_len != 0 ||
1106 SEQ_GT(ntohl(th->th_ack), ntohl(le->ack_seq))) {
1107 le->next_seq += tcp_data_len;
1108 le->ack_seq = th->th_ack;
1109 le->window = th->th_win;
1110 le->needs_merge = 1;
1111 } else if (th->th_ack == le->ack_seq) {
1112 if (WIN_GT(th->th_win, le->window)) {
1113 le->window = th->th_win;
1114 le->needs_merge = 1;
1115 }
1116 }
1117
1118 if (tcp_data_len == 0) {
1119 m_freem(m);
1120 continue;
1121 }
1122
1123 /* Merge TCP data checksum and length to head mbuf. */
1125
1126 /*
1127 * Adjust the mbuf so that m_data points to the first byte of
1128 * the ULP payload. Adjust the mbuf to avoid complications and
1129 * append new segment to existing mbuf chain.
1130 */
1131 m_adj(m, m->m_pkthdr.len - tcp_data_len);
1132 m_demote_pkthdr(m);
1133 le->m_tail->m_next = m;
1134 le->m_tail = m_last(m);
1135 }
1136}
1137
1138#ifdef TCPHPTS
1139static void
1140tcp_queue_pkts(struct inpcb *inp, struct tcpcb *tp, struct lro_entry *le)
1141{
1142 INP_WLOCK_ASSERT(inp);
1143 if (tp->t_in_pkt == NULL) {
1144 /* Nothing yet there */
1145 tp->t_in_pkt = le->m_head;
1146 tp->t_tail_pkt = le->m_last_mbuf;
1147 } else {
1148 /* Already some there */
1149 tp->t_tail_pkt->m_nextpkt = le->m_head;
1150 tp->t_tail_pkt = le->m_last_mbuf;
1151 }
1152 le->m_head = NULL;
1153 le->m_last_mbuf = NULL;
1154}
1155
1156static struct mbuf *
1157tcp_lro_get_last_if_ackcmp(struct lro_ctrl *lc, struct lro_entry *le,
1158 struct inpcb *inp, int32_t *new_m)
1159{
1160 struct tcpcb *tp;
1161 struct mbuf *m;
1162
1163 tp = intotcpcb(inp);
1164 if (__predict_false(tp == NULL))
1165 return (NULL);
1166
1167 /* Look at the last mbuf if any in queue */
1168 m = tp->t_tail_pkt;
1169 if (m != NULL && (m->m_flags & M_ACKCMP) != 0) {
1170 if (M_TRAILINGSPACE(m) >= sizeof(struct tcp_ackent)) {
1171 tcp_lro_log(tp, lc, le, NULL, 23, 0, 0, 0, 0);
1172 *new_m = 0;
1173 counter_u64_add(tcp_extra_mbuf, 1);
1174 return (m);
1175 } else {
1176 /* Mark we ran out of space */
1178 }
1179 }
1180 /* Decide mbuf size. */
1181 if (inp->inp_flags2 & INP_MBUF_L_ACKS)
1182 m = m_getcl(M_NOWAIT, MT_DATA, M_ACKCMP | M_PKTHDR);
1183 else
1184 m = m_gethdr(M_NOWAIT, MT_DATA);
1185
1186 if (__predict_false(m == NULL)) {
1187 counter_u64_add(tcp_would_have_but, 1);
1188 return (NULL);
1189 }
1190 counter_u64_add(tcp_comp_total, 1);
1191 m->m_flags |= M_ACKCMP;
1192 *new_m = 1;
1193 return (m);
1194}
1195
1196static struct inpcb *
1197tcp_lro_lookup(struct ifnet *ifp, struct lro_parser *pa)
1198{
1199 struct inpcb *inp;
1200
1201 switch (pa->data.lro_type) {
1202#ifdef INET6
1203 case LRO_TYPE_IPV6_TCP:
1204 inp = in6_pcblookup(&V_tcbinfo,
1205 &pa->data.s_addr.v6,
1206 pa->data.s_port,
1207 &pa->data.d_addr.v6,
1208 pa->data.d_port,
1210 ifp);
1211 break;
1212#endif
1213#ifdef INET
1214 case LRO_TYPE_IPV4_TCP:
1215 inp = in_pcblookup(&V_tcbinfo,
1216 pa->data.s_addr.v4,
1217 pa->data.s_port,
1218 pa->data.d_addr.v4,
1219 pa->data.d_port,
1221 ifp);
1222 break;
1223#endif
1224 default:
1225 inp = NULL;
1226 break;
1227 }
1228 return (inp);
1229}
1230
1231static inline bool
1232tcp_lro_ack_valid(struct mbuf *m, struct tcphdr *th, uint32_t **ppts, bool *other_opts)
1233{
1234 /*
1235 * This function returns two bits of valuable information.
1236 * a) Is what is present capable of being ack-compressed,
1237 * we can ack-compress if there is no options or just
1238 * a timestamp option, and of course the th_flags must
1239 * be correct as well.
1240 * b) Our other options present such as SACK. This is
1241 * used to determine if we want to wakeup or not.
1242 */
1243 bool ret = true;
1244
1245 switch (th->th_off << 2) {
1246 case (sizeof(*th) + TCPOLEN_TSTAMP_APPA):
1247 *ppts = (uint32_t *)(th + 1);
1248 /* Check if we have only one timestamp option. */
1249 if (**ppts == TCP_LRO_TS_OPTION)
1250 *other_opts = false;
1251 else {
1252 *other_opts = true;
1253 ret = false;
1254 }
1255 break;
1256 case (sizeof(*th)):
1257 /* No options. */
1258 *ppts = NULL;
1259 *other_opts = false;
1260 break;
1261 default:
1262 *ppts = NULL;
1263 *other_opts = true;
1264 ret = false;
1265 break;
1266 }
1267 /* For ACKCMP we only accept ACK, PUSH, ECE and CWR. */
1268 if ((tcp_get_flags(th) & ~(TH_ACK | TH_PUSH | TH_ECE | TH_CWR)) != 0)
1269 ret = false;
1270 /* If it has data on it we cannot compress it */
1271 if (m->m_pkthdr.lro_tcp_d_len)
1272 ret = false;
1273
1274 /* ACK flag must be set. */
1275 if (!(tcp_get_flags(th) & TH_ACK))
1276 ret = false;
1277 return (ret);
1278}
1279
1280static int
1281tcp_lro_flush_tcphpts(struct lro_ctrl *lc, struct lro_entry *le)
1282{
1283 struct inpcb *inp;
1284 struct tcpcb *tp;
1285 struct mbuf **pp, *cmp, *mv_to;
1286 bool bpf_req, should_wake;
1287
1288 /* Check if packet doesn't belongs to our network interface. */
1289 if ((tcplro_stacks_wanting_mbufq == 0) ||
1290 (le->outer.data.vlan_id != 0) ||
1292 return (TCP_LRO_CANNOT);
1293
1294#ifdef INET6
1295 /*
1296 * Be proactive about unspecified IPv6 address in source. As
1297 * we use all-zero to indicate unbounded/unconnected pcb,
1298 * unspecified IPv6 address can be used to confuse us.
1299 *
1300 * Note that packets with unspecified IPv6 destination is
1301 * already dropped in ip6_input.
1302 */
1303 if (__predict_false(le->outer.data.lro_type == LRO_TYPE_IPV6_TCP &&
1304 IN6_IS_ADDR_UNSPECIFIED(&le->outer.data.s_addr.v6)))
1305 return (TCP_LRO_CANNOT);
1306
1307 if (__predict_false(le->inner.data.lro_type == LRO_TYPE_IPV6_TCP &&
1308 IN6_IS_ADDR_UNSPECIFIED(&le->inner.data.s_addr.v6)))
1309 return (TCP_LRO_CANNOT);
1310#endif
1311 /* Lookup inp, if any. */
1312 inp = tcp_lro_lookup(lc->ifp,
1313 (le->inner.data.lro_type == LRO_TYPE_NONE) ? &le->outer : &le->inner);
1314 if (inp == NULL)
1315 return (TCP_LRO_CANNOT);
1316
1317 counter_u64_add(tcp_inp_lro_locks_taken, 1);
1318
1319 /* Get TCP control structure. */
1320 tp = intotcpcb(inp);
1321
1322 /* Check if the inp is dead, Jim. */
1323 if (tp == NULL ||
1324 (inp->inp_flags & (INP_DROPPED | INP_TIMEWAIT))) {
1325 INP_WUNLOCK(inp);
1326 return (TCP_LRO_CANNOT);
1327 }
1328 if ((inp->inp_irq_cpu_set == 0) && (lc->lro_cpu_is_set == 1)) {
1329 inp->inp_irq_cpu = lc->lro_last_cpu;
1330 inp->inp_irq_cpu_set = 1;
1331 }
1332 /* Check if the transport doesn't support the needed optimizations. */
1333 if ((inp->inp_flags2 & (INP_SUPPORTS_MBUFQ | INP_MBUF_ACKCMP)) == 0) {
1334 INP_WUNLOCK(inp);
1335 return (TCP_LRO_CANNOT);
1336 }
1337
1339 should_wake = false;
1340 else
1341 should_wake = true;
1342 /* Check if packets should be tapped to BPF. */
1343 bpf_req = bpf_peers_present(lc->ifp->if_bpf);
1344
1345 /* Strip and compress all the incoming packets. */
1346 cmp = NULL;
1347 for (pp = &le->m_head; *pp != NULL; ) {
1348 mv_to = NULL;
1349 if (do_bpf_strip_and_compress(inp, lc, le, pp,
1350 &cmp, &mv_to, &should_wake, bpf_req ) == false) {
1351 /* Advance to next mbuf. */
1352 pp = &(*pp)->m_nextpkt;
1353 } else if (mv_to != NULL) {
1354 /* We are asked to move pp up */
1355 pp = &mv_to->m_nextpkt;
1356 }
1357 }
1358 /* Update "m_last_mbuf", if any. */
1359 if (pp == &le->m_head)
1360 le->m_last_mbuf = *pp;
1361 else
1362 le->m_last_mbuf = __containerof(pp, struct mbuf, m_nextpkt);
1363
1364 /* Check if any data mbufs left. */
1365 if (le->m_head != NULL) {
1366 counter_u64_add(tcp_inp_lro_direct_queue, 1);
1367 tcp_lro_log(tp, lc, le, NULL, 22, 1, inp->inp_flags2, 0, 1);
1368 tcp_queue_pkts(inp, tp, le);
1369 }
1370 if (should_wake) {
1371 /* Wakeup */
1372 counter_u64_add(tcp_inp_lro_wokeup_queue, 1);
1373 if ((*tp->t_fb->tfb_do_queued_segments)(inp->inp_socket, tp, 0))
1374 inp = NULL;
1375 }
1376 if (inp != NULL)
1377 INP_WUNLOCK(inp);
1378 return (0); /* Success. */
1379}
1380#endif
1381
1382void
1383tcp_lro_flush(struct lro_ctrl *lc, struct lro_entry *le)
1384{
1385 /* Only optimise if there are multiple packets waiting. */
1386#ifdef TCPHPTS
1387 int error;
1388#endif
1389
1390 NET_EPOCH_ASSERT();
1391#ifdef TCPHPTS
1392 CURVNET_SET(lc->ifp->if_vnet);
1393 error = tcp_lro_flush_tcphpts(lc, le);
1394 CURVNET_RESTORE();
1395 if (error != 0) {
1396#endif
1397 tcp_lro_condense(lc, le);
1398 tcp_flush_out_entry(lc, le);
1399#ifdef TCPHPTS
1400 }
1401#endif
1402 lc->lro_flushed++;
1403 bzero(le, sizeof(*le));
1404 LIST_INSERT_HEAD(&lc->lro_free, le, next);
1405}
1406
1407#ifdef HAVE_INLINE_FLSLL
1408#define tcp_lro_msb_64(x) (1ULL << (flsll(x) - 1))
1409#else
1410static inline uint64_t
1412{
1413 x |= (x >> 1);
1414 x |= (x >> 2);
1415 x |= (x >> 4);
1416 x |= (x >> 8);
1417 x |= (x >> 16);
1418 x |= (x >> 32);
1419 return (x & ~(x >> 1));
1420}
1421#endif
1422
1423/*
1424 * The tcp_lro_sort() routine is comparable to qsort(), except it has
1425 * a worst case complexity limit of O(MIN(N,64)*N), where N is the
1426 * number of elements to sort and 64 is the number of sequence bits
1427 * available. The algorithm is bit-slicing the 64-bit sequence number,
1428 * sorting one bit at a time from the most significant bit until the
1429 * least significant one, skipping the constant bits. This is
1430 * typically called a radix sort.
1431 */
1432static void
1434{
1435 struct lro_mbuf_sort temp;
1436 uint64_t ones;
1437 uint64_t zeros;
1438 uint32_t x;
1439 uint32_t y;
1440
1441repeat:
1442 /* for small arrays insertion sort is faster */
1443 if (size <= 12) {
1444 for (x = 1; x < size; x++) {
1445 temp = parray[x];
1446 for (y = x; y > 0 && temp.seq < parray[y - 1].seq; y--)
1447 parray[y] = parray[y - 1];
1448 parray[y] = temp;
1449 }
1450 return;
1451 }
1452
1453 /* compute sequence bits which are constant */
1454 ones = 0;
1455 zeros = 0;
1456 for (x = 0; x != size; x++) {
1457 ones |= parray[x].seq;
1458 zeros |= ~parray[x].seq;
1459 }
1460
1461 /* compute bits which are not constant into "ones" */
1462 ones &= zeros;
1463 if (ones == 0)
1464 return;
1465
1466 /* pick the most significant bit which is not constant */
1467 ones = tcp_lro_msb_64(ones);
1468
1469 /*
1470 * Move entries having cleared sequence bits to the beginning
1471 * of the array:
1472 */
1473 for (x = y = 0; y != size; y++) {
1474 /* skip set bits */
1475 if (parray[y].seq & ones)
1476 continue;
1477 /* swap entries */
1478 temp = parray[x];
1479 parray[x] = parray[y];
1480 parray[y] = temp;
1481 x++;
1482 }
1483
1484 KASSERT(x != 0 && x != size, ("Memory is corrupted\n"));
1485
1486 /* sort zeros */
1487 tcp_lro_sort(parray, x);
1488
1489 /* sort ones */
1490 parray += x;
1491 size -= x;
1492 goto repeat;
1493}
1494
1495void
1497{
1498 uint64_t seq;
1499 uint64_t nseq;
1500 unsigned x;
1501
1502 NET_EPOCH_ASSERT();
1503 /* check if no mbufs to flush */
1504 if (lc->lro_mbuf_count == 0)
1505 goto done;
1506 if (lc->lro_cpu_is_set == 0) {
1507 if (lc->lro_last_cpu == curcpu) {
1508 lc->lro_cnt_of_same_cpu++;
1509 /* Have we reached the threshold to declare a cpu? */
1511 lc->lro_cpu_is_set = 1;
1512 } else {
1513 lc->lro_last_cpu = curcpu;
1514 lc->lro_cnt_of_same_cpu = 0;
1515 }
1516 }
1517 CURVNET_SET(lc->ifp->if_vnet);
1518
1519 /* get current time */
1520 binuptime(&lc->lro_last_queue_time);
1521
1522 /* sort all mbufs according to stream */
1524
1525 /* input data into LRO engine, stream by stream */
1526 seq = 0;
1527 for (x = 0; x != lc->lro_mbuf_count; x++) {
1528 struct mbuf *mb;
1529
1530 /* get mbuf */
1531 mb = lc->lro_mbuf_data[x].mb;
1532
1533 /* get sequence number, masking away the packet index */
1534 nseq = lc->lro_mbuf_data[x].seq & (-1ULL << 24);
1535
1536 /* check for new stream */
1537 if (seq != nseq) {
1538 seq = nseq;
1539
1540 /* flush active streams */
1541 tcp_lro_rx_done(lc);
1542 }
1543
1544 /* add packet to LRO engine */
1545 if (tcp_lro_rx_common(lc, mb, 0, false) != 0) {
1546 /* input packet to network layer */
1547 (*lc->ifp->if_input)(lc->ifp, mb);
1548 lc->lro_queued++;
1549 lc->lro_flushed++;
1550 }
1551 }
1552 CURVNET_RESTORE();
1553done:
1554 /* flush active streams */
1555 tcp_lro_rx_done(lc);
1556
1557#ifdef TCPHPTS
1558 tcp_run_hpts();
1559#endif
1560 lc->lro_mbuf_count = 0;
1561}
1562
1563#ifdef TCPHPTS
1564static void
1565build_ack_entry(struct tcp_ackent *ae, struct tcphdr *th, struct mbuf *m,
1566 uint32_t *ts_ptr, uint16_t iptos)
1567{
1568 /*
1569 * Given a TCP ACK, summarize it down into the small TCP ACK
1570 * entry.
1571 */
1572 ae->timestamp = m->m_pkthdr.rcv_tstmp;
1573 if (m->m_flags & M_TSTMP_LRO)
1574 ae->flags = TSTMP_LRO;
1575 else if (m->m_flags & M_TSTMP)
1576 ae->flags = TSTMP_HDWR;
1577 ae->seq = ntohl(th->th_seq);
1578 ae->ack = ntohl(th->th_ack);
1579 ae->flags |= tcp_get_flags(th);
1580 if (ts_ptr != NULL) {
1581 ae->ts_value = ntohl(ts_ptr[1]);
1582 ae->ts_echo = ntohl(ts_ptr[2]);
1583 ae->flags |= HAS_TSTMP;
1584 }
1585 ae->win = ntohs(th->th_win);
1586 ae->codepoint = iptos;
1587}
1588
1589/*
1590 * Do BPF tap for either ACK_CMP packets or MBUF QUEUE type packets
1591 * and strip all, but the IPv4/IPv6 header.
1592 */
1593static bool
1594do_bpf_strip_and_compress(struct inpcb *inp, struct lro_ctrl *lc,
1595 struct lro_entry *le, struct mbuf **pp, struct mbuf **cmp, struct mbuf **mv_to,
1596 bool *should_wake, bool bpf_req)
1597{
1598 union {
1599 void *ptr;
1600 struct ip *ip4;
1601 struct ip6_hdr *ip6;
1602 } l3;
1603 struct mbuf *m;
1604 struct mbuf *nm;
1605 struct tcphdr *th;
1606 struct tcp_ackent *ack_ent;
1607 uint32_t *ts_ptr;
1608 int32_t n_mbuf;
1609 bool other_opts, can_compress;
1611 uint16_t iptos;
1612 int tcp_hdr_offset;
1613 int idx;
1614
1615 /* Get current mbuf. */
1616 m = *pp;
1617
1618 /* Let the BPF see the packet */
1619 if (__predict_false(bpf_req))
1620 ETHER_BPF_MTAP(lc->ifp, m);
1621
1622 tcp_hdr_offset = m->m_pkthdr.lro_tcp_h_off;
1624 switch (lro_type) {
1625 case LRO_TYPE_NONE:
1627 switch (lro_type) {
1628 case LRO_TYPE_IPV4_TCP:
1629 tcp_hdr_offset -= sizeof(*le->outer.ip4);
1630 m->m_pkthdr.lro_etype = ETHERTYPE_IP;
1631 break;
1632 case LRO_TYPE_IPV6_TCP:
1633 tcp_hdr_offset -= sizeof(*le->outer.ip6);
1634 m->m_pkthdr.lro_etype = ETHERTYPE_IPV6;
1635 break;
1636 default:
1637 goto compressed;
1638 }
1639 break;
1640 case LRO_TYPE_IPV4_TCP:
1641 tcp_hdr_offset -= sizeof(*le->outer.ip4);
1642 m->m_pkthdr.lro_etype = ETHERTYPE_IP;
1643 break;
1644 case LRO_TYPE_IPV6_TCP:
1645 tcp_hdr_offset -= sizeof(*le->outer.ip6);
1646 m->m_pkthdr.lro_etype = ETHERTYPE_IPV6;
1647 break;
1648 default:
1649 goto compressed;
1650 }
1651
1652 MPASS(tcp_hdr_offset >= 0);
1653
1654 m_adj(m, tcp_hdr_offset);
1655 m->m_flags |= M_LRO_EHDRSTRP;
1656 m->m_flags &= ~M_ACKCMP;
1657 m->m_pkthdr.lro_tcp_h_off -= tcp_hdr_offset;
1658
1659 th = tcp_lro_get_th(m);
1660
1661 th->th_sum = 0; /* TCP checksum is valid. */
1662
1663 /* Check if ACK can be compressed */
1664 can_compress = tcp_lro_ack_valid(m, th, &ts_ptr, &other_opts);
1665
1666 /* Now lets look at the should wake states */
1667 if ((other_opts == true) &&
1668 ((inp->inp_flags2 & INP_DONT_SACK_QUEUE) == 0)) {
1669 /*
1670 * If there are other options (SACK?) and the
1671 * tcp endpoint has not expressly told us it does
1672 * not care about SACKS, then we should wake up.
1673 */
1674 *should_wake = true;
1675 }
1676 /* Is the ack compressable? */
1677 if (can_compress == false)
1678 goto done;
1679 /* Does the TCP endpoint support ACK compression? */
1680 if ((inp->inp_flags2 & INP_MBUF_ACKCMP) == 0)
1681 goto done;
1682
1683 /* Lets get the TOS/traffic class field */
1684 l3.ptr = mtod(m, void *);
1685 switch (lro_type) {
1686 case LRO_TYPE_IPV4_TCP:
1687 iptos = l3.ip4->ip_tos;
1688 break;
1689 case LRO_TYPE_IPV6_TCP:
1690 iptos = IPV6_TRAFFIC_CLASS(l3.ip6);
1691 break;
1692 default:
1693 iptos = 0; /* Keep compiler happy. */
1694 break;
1695 }
1696 /* Now lets get space if we don't have some already */
1697 if (*cmp == NULL) {
1698new_one:
1699 nm = tcp_lro_get_last_if_ackcmp(lc, le, inp, &n_mbuf);
1700 if (__predict_false(nm == NULL))
1701 goto done;
1702 *cmp = nm;
1703 if (n_mbuf) {
1704 /*
1705 * Link in the new cmp ack to our in-order place,
1706 * first set our cmp ack's next to where we are.
1707 */
1708 nm->m_nextpkt = m;
1709 (*pp) = nm;
1710 /*
1711 * Set it up so mv_to is advanced to our
1712 * compressed ack. This way the caller can
1713 * advance pp to the right place.
1714 */
1715 *mv_to = nm;
1716 /*
1717 * Advance it here locally as well.
1718 */
1719 pp = &nm->m_nextpkt;
1720 }
1721 } else {
1722 /* We have one already we are working on */
1723 nm = *cmp;
1724 if (M_TRAILINGSPACE(nm) < sizeof(struct tcp_ackent)) {
1725 /* We ran out of space */
1727 goto new_one;
1728 }
1729 }
1730 MPASS(M_TRAILINGSPACE(nm) >= sizeof(struct tcp_ackent));
1731 counter_u64_add(tcp_inp_lro_compressed, 1);
1732 le->compressed++;
1733 /* We can add in to the one on the tail */
1734 ack_ent = mtod(nm, struct tcp_ackent *);
1735 idx = (nm->m_len / sizeof(struct tcp_ackent));
1736 build_ack_entry(&ack_ent[idx], th, m, ts_ptr, iptos);
1737
1738 /* Bump the size of both pkt-hdr and len */
1739 nm->m_len += sizeof(struct tcp_ackent);
1740 nm->m_pkthdr.len += sizeof(struct tcp_ackent);
1741compressed:
1742 /* Advance to next mbuf before freeing. */
1743 *pp = m->m_nextpkt;
1744 m->m_nextpkt = NULL;
1745 m_freem(m);
1746 return (true);
1747done:
1748 counter_u64_add(tcp_uncomp_total, 1);
1749 le->uncompressed++;
1750 return (false);
1751}
1752#endif
1753
1754static struct lro_head *
1755tcp_lro_rx_get_bucket(struct lro_ctrl *lc, struct mbuf *m, struct lro_parser *parser)
1756{
1757 u_long hash;
1758
1759 if (M_HASHTYPE_ISHASH(m)) {
1760 hash = m->m_pkthdr.flowid;
1761 } else {
1762 for (unsigned i = hash = 0; i != LRO_RAW_ADDRESS_MAX; i++)
1763 hash += parser->data.raw[i];
1764 }
1765 return (&lc->lro_hash[hash % lc->lro_hashsz]);
1766}
1767
1768static int
1769tcp_lro_rx_common(struct lro_ctrl *lc, struct mbuf *m, uint32_t csum, bool use_hash)
1770{
1771 struct lro_parser pi; /* inner address data */
1772 struct lro_parser po; /* outer address data */
1773 struct lro_parser *pa; /* current parser for TCP stream */
1774 struct lro_entry *le;
1775 struct lro_head *bucket;
1776 struct tcphdr *th;
1777 int tcp_data_len;
1778 int tcp_opt_len;
1779 int error;
1780 uint16_t tcp_data_sum;
1781
1782#ifdef INET
1783 /* Quickly decide if packet cannot be LRO'ed */
1784 if (__predict_false(V_ipforwarding != 0))
1785 return (TCP_LRO_CANNOT);
1786#endif
1787#ifdef INET6
1788 /* Quickly decide if packet cannot be LRO'ed */
1789 if (__predict_false(V_ip6_forwarding != 0))
1790 return (TCP_LRO_CANNOT);
1791#endif
1792 if (((m->m_pkthdr.csum_flags & (CSUM_DATA_VALID | CSUM_PSEUDO_HDR)) !=
1793 ((CSUM_DATA_VALID | CSUM_PSEUDO_HDR))) ||
1794 (m->m_pkthdr.csum_data != 0xffff)) {
1795 /*
1796 * The checksum either did not have hardware offload
1797 * or it was a bad checksum. We can't LRO such
1798 * a packet.
1799 */
1800 counter_u64_add(tcp_bad_csums, 1);
1801 return (TCP_LRO_CANNOT);
1802 }
1803 /* We expect a contiguous header [eh, ip, tcp]. */
1804 pa = tcp_lro_parser(m, &po, &pi, true);
1805 if (__predict_false(pa == NULL))
1806 return (TCP_LRO_NOT_SUPPORTED);
1807
1808 /* We don't expect any padding. */
1809 error = tcp_lro_trim_mbuf_chain(m, pa);
1810 if (__predict_false(error != 0))
1811 return (error);
1812
1813#ifdef INET
1814 switch (pa->data.lro_type) {
1815 case LRO_TYPE_IPV4_TCP:
1816 error = tcp_lro_rx_ipv4(lc, m, pa->ip4);
1817 if (__predict_false(error != 0))
1818 return (error);
1819 break;
1820 default:
1821 break;
1822 }
1823#endif
1824 /* If no hardware or arrival stamp on the packet add timestamp */
1825 if ((m->m_flags & (M_TSTMP_LRO | M_TSTMP)) == 0) {
1826 m->m_pkthdr.rcv_tstmp = bintime2ns(&lc->lro_last_queue_time);
1827 m->m_flags |= M_TSTMP_LRO;
1828 }
1829
1830 /* Get pointer to TCP header. */
1831 th = pa->tcp;
1832
1833 /* Don't process SYN packets. */
1834 if (__predict_false(tcp_get_flags(th) & TH_SYN))
1835 return (TCP_LRO_CANNOT);
1836
1837 /* Get total TCP header length and compute payload length. */
1838 tcp_opt_len = (th->th_off << 2);
1839 tcp_data_len = m->m_pkthdr.len - ((uint8_t *)th -
1840 (uint8_t *)m->m_data) - tcp_opt_len;
1841 tcp_opt_len -= sizeof(*th);
1842
1843 /* Don't process invalid TCP headers. */
1844 if (__predict_false(tcp_opt_len < 0 || tcp_data_len < 0))
1845 return (TCP_LRO_CANNOT);
1846
1847 /* Compute TCP data only checksum. */
1848 if (tcp_data_len == 0)
1849 tcp_data_sum = 0; /* no data, no checksum */
1850 else if (__predict_false(csum != 0))
1851 tcp_data_sum = tcp_lro_rx_csum_data(pa, ~csum);
1852 else
1853 tcp_data_sum = tcp_lro_rx_csum_data(pa, ~th->th_sum);
1854
1855 /* Save TCP info in mbuf. */
1856 m->m_nextpkt = NULL;
1857 m->m_pkthdr.rcvif = lc->ifp;
1858 m->m_pkthdr.lro_tcp_d_csum = tcp_data_sum;
1859 m->m_pkthdr.lro_tcp_d_len = tcp_data_len;
1860 m->m_pkthdr.lro_tcp_h_off = ((uint8_t *)th - (uint8_t *)m->m_data);
1861 m->m_pkthdr.lro_nsegs = 1;
1862
1863 /* Get hash bucket. */
1864 if (!use_hash) {
1865 bucket = &lc->lro_hash[0];
1866 } else {
1867 bucket = tcp_lro_rx_get_bucket(lc, m, pa);
1868 }
1869
1870 /* Try to find a matching previous segment. */
1871 LIST_FOREACH(le, bucket, hash_next) {
1872 /* Compare addresses and ports. */
1873 if (lro_address_compare(&po.data, &le->outer.data) == false ||
1874 lro_address_compare(&pi.data, &le->inner.data) == false)
1875 continue;
1876
1877 /* Check if no data and old ACK. */
1878 if (tcp_data_len == 0 &&
1879 SEQ_LT(ntohl(th->th_ack), ntohl(le->ack_seq))) {
1880 m_freem(m);
1881 return (0);
1882 }
1883
1884 /* Mark "m" in the last spot. */
1885 le->m_last_mbuf->m_nextpkt = m;
1886 /* Now set the tail to "m". */
1887 le->m_last_mbuf = m;
1888 return (0);
1889 }
1890
1891 /* Try to find an empty slot. */
1892 if (LIST_EMPTY(&lc->lro_free))
1893 return (TCP_LRO_NO_ENTRIES);
1894
1895 /* Start a new segment chain. */
1896 le = LIST_FIRST(&lc->lro_free);
1897 LIST_REMOVE(le, next);
1899
1900 /* Make sure the headers are set. */
1901 le->inner = pi;
1902 le->outer = po;
1903
1904 /* Store time this entry was allocated. */
1906
1907 tcp_set_entry_to_mbuf(lc, le, m, th);
1908
1909 /* Now set the tail to "m". */
1910 le->m_last_mbuf = m;
1911
1912 return (0);
1913}
1914
1915int
1916tcp_lro_rx(struct lro_ctrl *lc, struct mbuf *m, uint32_t csum)
1917{
1918 int error;
1919
1920 if (((m->m_pkthdr.csum_flags & (CSUM_DATA_VALID | CSUM_PSEUDO_HDR)) !=
1921 ((CSUM_DATA_VALID | CSUM_PSEUDO_HDR))) ||
1922 (m->m_pkthdr.csum_data != 0xffff)) {
1923 /*
1924 * The checksum either did not have hardware offload
1925 * or it was a bad checksum. We can't LRO such
1926 * a packet.
1927 */
1928 counter_u64_add(tcp_bad_csums, 1);
1929 return (TCP_LRO_CANNOT);
1930 }
1931 /* get current time */
1932 binuptime(&lc->lro_last_queue_time);
1933 CURVNET_SET(lc->ifp->if_vnet);
1934 error = tcp_lro_rx_common(lc, m, csum, true);
1935 CURVNET_RESTORE();
1936
1937 return (error);
1938}
1939
1940void
1941tcp_lro_queue_mbuf(struct lro_ctrl *lc, struct mbuf *mb)
1942{
1943 NET_EPOCH_ASSERT();
1944 /* sanity checks */
1945 if (__predict_false(lc->ifp == NULL || lc->lro_mbuf_data == NULL ||
1946 lc->lro_mbuf_max == 0)) {
1947 /* packet drop */
1948 m_freem(mb);
1949 return;
1950 }
1951
1952 /* check if packet is not LRO capable */
1953 if (__predict_false((lc->ifp->if_capenable & IFCAP_LRO) == 0)) {
1954 /* input packet to network layer */
1955 (*lc->ifp->if_input) (lc->ifp, mb);
1956 return;
1957 }
1958
1959 /* create sequence number */
1961 (((uint64_t)M_HASHTYPE_GET(mb)) << 56) |
1962 (((uint64_t)mb->m_pkthdr.flowid) << 24) |
1963 ((uint64_t)lc->lro_mbuf_count);
1964
1965 /* enter mbuf */
1966 lc->lro_mbuf_data[lc->lro_mbuf_count].mb = mb;
1967
1968 /* flush if array is full */
1969 if (__predict_false(++lc->lro_mbuf_count == lc->lro_mbuf_max))
1971}
1972
1973/* end */
__uint32_t uint32_t
Definition: in.h:62
__uint16_t uint16_t
Definition: in.h:57
__uint8_t uint8_t
Definition: in.h:52
#define IPPROTO_TCP
Definition: in.h:45
#define IPPROTO_UDP
Definition: in.h:46
u_short in_addword(u_short a, u_short b)
Definition: in_cksum.c:188
u_int in_cksum_hdr(const struct ip *ip)
Definition: in_cksum.c:249
u_short in_pseudo(u_int32_t a, u_int32_t b, u_int32_t c)
Definition: in_cksum.c:197
#define INP_MBUF_L_ACKS
Definition: in_pcb.h:663
struct inpcb * in_pcblookup(struct inpcbinfo *, struct in_addr, u_int, struct in_addr, u_int, int, struct ifnet *)
#define INP_DONT_SACK_QUEUE
Definition: in_pcb.h:679
#define INP_MBUF_QUEUE_READY
Definition: in_pcb.h:678
#define INP_WLOCK_ASSERT(inp)
Definition: in_pcb.h:529
#define INP_SUPPORTS_MBUFQ
Definition: in_pcb.h:677
#define INP_TIMEWAIT
Definition: in_pcb.h:644
#define INP_DROPPED
Definition: in_pcb.h:646
#define INP_WUNLOCK(inp)
Definition: in_pcb.h:522
@ INPLOOKUP_WLOCKPCB
Definition: in_pcb.h:694
#define INP_MBUF_ACKCMP
Definition: in_pcb.h:664
#define IPV6_TRAFFIC_CLASS(ip6)
Definition: ip6.h:109
#define IP_OFFMASK
Definition: ip.h:15
#define IP_MF
Definition: ip.h:14
ipfw_dyn_rule * next
Definition: ip_fw.h:0
u_int32_t bucket
Definition: ip_fw.h:9
#define V_ipforwarding
Definition: ip_var.h:203
in_addr_t s_addr
Definition: in.h:84
Definition: in_pcb.h:217
struct socket * inp_socket
Definition: in_pcb.h:254
uint8_t inp_irq_cpu_set
Definition: in_pcb.h:250
int inp_flags
Definition: in_pcb.h:246
int inp_flags2
Definition: in_pcb.h:247
volatile uint16_t inp_irq_cpu
Definition: in_pcb.h:244
Definition: ip6.h:74
struct in6_addr ip6_dst
Definition: ip6.h:85
struct in6_addr ip6_src
Definition: ip6.h:84
Definition: ip.h:51
u_char ip_p
Definition: ip.h:69
struct in_addr ip_src ip_dst
Definition: ip.h:71
u_char ip_hl
Definition: ip.h:53
u_short ip_sum
Definition: ip.h:70
u_short ip_len
Definition: ip.h:61
u_short ip_off
Definition: ip.h:63
struct lro_head * lro_hash
Definition: tcp_lro.h:173
uint64_t lro_bad_csum
Definition: tcp_lro.h:163
struct ifnet * ifp
Definition: tcp_lro.h:158
unsigned lro_mbuf_max
Definition: tcp_lro.h:166
struct bintime lro_last_queue_time
Definition: tcp_lro.h:160
unsigned lro_mbuf_count
Definition: tcp_lro.h:165
unsigned lro_cnt
Definition: tcp_lro.h:164
uint8_t lro_cpu_is_set
Definition: tcp_lro.h:176
uint64_t lro_queued
Definition: tcp_lro.h:161
u_long lro_hashsz
Definition: tcp_lro.h:170
uint32_t lro_cnt_of_same_cpu
Definition: tcp_lro.h:172
unsigned short lro_ackcnt_lim
Definition: tcp_lro.h:167
uint64_t lro_flushed
Definition: tcp_lro.h:162
struct lro_mbuf_sort * lro_mbuf_data
Definition: tcp_lro.h:159
unsigned lro_length_lim
Definition: tcp_lro.h:169
uint32_t lro_last_cpu
Definition: tcp_lro.h:171
struct lro_head lro_active
Definition: tcp_lro.h:174
struct lro_head lro_free
Definition: tcp_lro.h:175
Definition: tcp_lro.h:127
struct lro_parser outer
Definition: tcp_lro.h:133
struct mbuf * m_head
Definition: tcp_lro.h:130
uint16_t timestamp
Definition: tcp_lro.h:143
struct lro_parser inner
Definition: tcp_lro.h:134
uint32_t next_seq
Definition: tcp_lro.h:135
uint16_t compressed
Definition: tcp_lro.h:139
uint32_t tsecr
Definition: tcp_lro.h:138
uint32_t tsval
Definition: tcp_lro.h:137
struct mbuf * m_tail
Definition: tcp_lro.h:131
uint16_t uncompressed
Definition: tcp_lro.h:140
uint16_t flags
Definition: tcp_lro.h:142
uint16_t needs_merge
Definition: tcp_lro.h:144
uint16_t window
Definition: tcp_lro.h:141
uint32_t ack_seq
Definition: tcp_lro.h:136
struct mbuf * m_last_mbuf
Definition: tcp_lro.h:132
struct bintime alloc_time
Definition: tcp_lro.h:146
uint64_t seq
Definition: tcp_lro.h:152
struct mbuf * mb
Definition: tcp_lro.h:153
struct tcphdr * tcp
Definition: tcp_lro.h:120
union lro_address data
Definition: tcp_lro.h:112
struct ip6_hdr * ip6
Definition: tcp_lro.h:116
struct udphdr * udp
Definition: tcp_lro.h:121
struct ip * ip4
Definition: tcp_lro.h:115
uint16_t total_hdr_len
Definition: tcp_lro.h:123
uint64_t timestamp
Definition: tcp_lro.h:180
uint32_t ts_echo
Definition: tcp_lro.h:184
uint16_t win
Definition: tcp_lro.h:185
uint8_t codepoint
Definition: tcp_lro.h:187
uint32_t seq
Definition: tcp_lro.h:181
uint32_t ts_value
Definition: tcp_lro.h:183
uint32_t ack
Definition: tcp_lro.h:182
uint16_t flags
Definition: tcp_lro.h:186
int(* tfb_do_queued_segments)(struct socket *, struct tcpcb *, int)
Definition: tcp_var.h:354
Definition: tcp_var.h:132
uint32_t t_logstate
Definition: tcp_var.h:138
struct mbuf * t_tail_pkt
Definition: tcp_var.h:182
struct mbuf * t_in_pkt
Definition: tcp_var.h:181
struct tcp_function_block * t_fb
Definition: tcp_var.h:135
struct inpcb * t_inpcb
Definition: tcp_var.h:134
Definition: udp.h:45
u_short uh_ulen
Definition: udp.h:48
u_short uh_sport
Definition: udp.h:46
u_short uh_sum
Definition: udp.h:49
u_short uh_dport
Definition: udp.h:47
uint32_t vxlh_flags
Definition: tcp_lro.c:226
uint32_t vxlh_vni
Definition: tcp_lro.c:227
void tcp_run_hpts(void)
Definition: tcp_hpts.c:1642
static __inline uint32_t tcp_tv_to_usectick(const struct timeval *sv)
Definition: tcp_hpts.h:168
static __inline uint32_t tcp_get_usecs(struct timeval *tv)
Definition: tcp_hpts.h:198
@ TCP_LOG_STATE_OFF
Definition: tcp_log_buf.h:244
#define TCP_LOG_EVENTP(tp, th, rxbuf, txbuf, eventid, errornum, len, stackinfo, th_hostorder, tv)
Definition: tcp_log_buf.h:346
@ TCP_LOG_LRO
Definition: tcp_log_buf.h:228
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)
Definition: tcp_lro.c:708
static uint16_t tcp_lro_rx_csum_data(const struct lro_parser *pa, uint16_t tcp_csum)
Definition: tcp_lro.c:543
static void tcp_lro_sort(struct lro_mbuf_sort *parray, uint32_t size)
Definition: tcp_lro.c:1433
static uint32_t tcp_lro_cpu_set_thresh
Definition: tcp_lro.c:111
void tcp_lro_flush_all(struct lro_ctrl *lc)
Definition: tcp_lro.c:1496
SYSCTL_UINT(_net_inet_tcp_lro, OID_AUTO, entries, CTLFLAG_RDTUN|CTLFLAG_MPSAFE, &tcp_lro_entries, 0, "default number of LRO entries")
static void tcp_lro_assign_and_checksum_16(uint16_t *ptr, uint16_t value, uint16_t *psum)
Definition: tcp_lro.c:696
static void tcp_flush_out_entry(struct lro_ctrl *lc, struct lro_entry *le)
Definition: tcp_lro.c:820
static struct tcphdr * tcp_lro_get_th(struct mbuf *m)
Definition: tcp_lro.c:472
counter_u64_t tcp_inp_lro_compressed
Definition: tcp_lro.c:98
static unsigned tcp_lro_entries
Definition: tcp_lro.c:106
counter_u64_t tcp_inp_lro_locks_taken
Definition: tcp_lro.c:99
counter_u64_t tcp_uncomp_total
Definition: tcp_lro.c:103
void tcp_lro_reg_mbufq(void)
Definition: tcp_lro.c:136
static const int vxlan_csum
Definition: tcp_lro.c:378
static void tcp_push_and_replace(struct lro_ctrl *lc, struct lro_entry *le, struct mbuf *m)
Definition: tcp_lro.c:933
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")
static void lro_free_mbuf_chain(struct mbuf *m)
Definition: tcp_lro.c:478
void tcp_lro_flush(struct lro_ctrl *lc, struct lro_entry *le)
Definition: tcp_lro.c:1383
SYSCTL_NODE(_net_inet_tcp, OID_AUTO, lro, CTLFLAG_RW|CTLFLAG_MPSAFE, 0, "TCP LRO")
static int tcp_lro_rx_common(struct lro_ctrl *lc, struct mbuf *m, uint32_t csum, bool use_hash)
Definition: tcp_lro.c:1769
static int tcp_lro_trim_mbuf_chain(struct mbuf *m, const struct lro_parser *po)
Definition: tcp_lro.c:437
#define TCP_LRO_TS_OPTION
Definition: tcp_lro.c:78
void tcp_lro_dereg_mbufq(void)
Definition: tcp_lro.c:142
static uint64_t tcp_lro_msb_64(uint64_t x)
Definition: tcp_lro.c:1411
void tcp_lro_queue_mbuf(struct lro_ctrl *lc, struct mbuf *mb)
Definition: tcp_lro.c:1941
static void * tcp_lro_low_level_parser(void *ptr, struct lro_parser *parser, bool update_data, bool is_vxlan, int mlen)
Definition: tcp_lro.c:231
__FBSDID("$FreeBSD$")
counter_u64_t tcp_inp_lro_wokeup_queue
Definition: tcp_lro.c:97
static void tcp_lro_condense(struct lro_ctrl *lc, struct lro_entry *le)
Definition: tcp_lro.c:993
counter_u64_t tcp_bad_csums
Definition: tcp_lro.c:104
static long tcplro_stacks_wanting_mbufq
Definition: tcp_lro.c:95
int tcp_lro_rx(struct lro_ctrl *lc, struct mbuf *m, uint32_t csum)
Definition: tcp_lro.c:1916
static uint16_t tcp_lro_rx_csum_tcphdr(const struct tcphdr *th)
Definition: tcp_lro.c:521
counter_u64_t tcp_would_have_but
Definition: tcp_lro.c:101
counter_u64_t tcp_comp_total
Definition: tcp_lro.c:102
counter_u64_t tcp_extra_mbuf
Definition: tcp_lro.c:100
static void tcp_lro_rx_done(struct lro_ctrl *lc)
Definition: tcp_lro.c:585
static struct lro_head * tcp_lro_rx_get_bucket(struct lro_ctrl *lc, struct mbuf *m, struct lro_parser *parser)
Definition: tcp_lro.c:1755
static __inline void tcp_lro_active_remove(struct lro_entry *le)
Definition: tcp_lro.c:157
void tcp_lro_flush_inactive(struct lro_ctrl *lc, const struct timeval *timeout)
Definition: tcp_lro.c:596
static struct lro_parser * tcp_lro_parser(struct mbuf *m, struct lro_parser *po, struct lro_parser *pi, bool update_data)
Definition: tcp_lro.c:382
counter_u64_t tcp_inp_lro_direct_queue
Definition: tcp_lro.c:96
static void tcp_set_entry_to_mbuf(struct lro_ctrl *lc, struct lro_entry *le, struct mbuf *m, struct tcphdr *th)
Definition: tcp_lro.c:895
static MALLOC_DEFINE(M_LRO, "LRO", "LRO control structures")
void tcp_lro_free(struct lro_ctrl *lc)
Definition: tcp_lro.c:491
int tcp_lro_init_args(struct lro_ctrl *lc, struct ifnet *ifp, unsigned lro_entries, unsigned lro_mbufs)
Definition: tcp_lro.c:171
static __inline void tcp_lro_active_insert(struct lro_ctrl *lc, struct lro_head *bucket, struct lro_entry *le)
Definition: tcp_lro.c:148
static void tcp_lro_mbuf_append_pkthdr(struct lro_entry *le, const struct mbuf *p)
Definition: tcp_lro.c:967
int tcp_lro_init(struct lro_ctrl *lc)
Definition: tcp_lro.c:165
#define TSTMP_LRO
Definition: tcp_lro.h:57
#define TCP_LRO_ACKCNT_MAX
Definition: tcp_lro.h:197
#define TCP_LRO_ENTRIES
Definition: tcp_lro.h:41
uint8_t lro_type
Definition: tcp_lro.h:2
#define TCP_LRO_CANNOT
Definition: tcp_lro.h:211
#define TSTMP_HDWR
Definition: tcp_lro.h:58
#define LRO_TYPE_IPV6_TCP
Definition: tcp_lro.h:5
#define TCP_LRO_CPU_DECLARATION_THRESH
Definition: tcp_lro.h:64
#define LRO_FLAG_DECRYPTED
Definition: tcp_lro.h:9
#define LRO_TYPE_NONE
Definition: tcp_lro.h:3
#define TCP_LRO_LENGTH_MAX
Definition: tcp_lro.h:196
#define TCP_LRO_NOT_SUPPORTED
Definition: tcp_lro.h:212
#define LRO_TYPE_IPV6_UDP
Definition: tcp_lro.h:7
#define HAS_TSTMP
Definition: tcp_lro.h:59
#define TCP_LRO_NO_ENTRIES
Definition: tcp_lro.h:210
#define LRO_RAW_ADDRESS_MAX
Definition: tcp_lro.h:94
static bool lro_address_compare(const union lro_address *pa, const union lro_address *pb)
Definition: tcp_lro.h:100
#define M_ACKCMP
Definition: tcp_lro.h:193
#define M_LRO_EHDRSTRP
Definition: tcp_lro.h:194
#define LRO_TYPE_IPV4_UDP
Definition: tcp_lro.h:6
#define LRO_TYPE_IPV4_TCP
Definition: tcp_lro.h:4
#define SEQ_GT(a, b)
Definition: tcp_seq.h:44
#define WIN_GT(a, b)
Definition: tcp_seq.h:52
#define TSTMP_GT(a, b)
Definition: tcp_seq.h:60
#define SEQ_LT(a, b)
Definition: tcp_seq.h:42
static uint16_t tcp_get_flags(const struct tcphdr *th)
Definition: tcp_var.h:1265
#define intotcpcb(ip)
Definition: tcp_var.h:645
#define V_tcbinfo
Definition: tcp_var.h:1030
uint8_t lro_flags
Definition: tcp_lro.h:77
u_long raw[1]
Definition: tcp_lro.h:69
uint32_t vxlan_vni
Definition: tcp_lro.h:82
struct in_addr v4
Definition: tcp_lro.h:84
uint8_t lro_type
Definition: tcp_lro.h:71
uint16_t s_port
Definition: tcp_lro.h:80
uint16_t vlan_id
Definition: tcp_lro.h:79
struct in6_addr v6
Definition: tcp_lro.h:85
uint16_t d_port
Definition: tcp_lro.h:81
union lro_address::@41::@44 d_addr
union lro_address::@41::@43 s_addr