11eaf0ac3Slogwang /*-
2*d4a07e70Sfengbojiang * SPDX-License-Identifier: BSD-3-Clause
3*d4a07e70Sfengbojiang *
41eaf0ac3Slogwang * Copyright (c) 1982, 1986, 1993, 1995
51eaf0ac3Slogwang * The Regents of the University of California. All rights reserved.
61eaf0ac3Slogwang *
71eaf0ac3Slogwang * Redistribution and use in source and binary forms, with or without
81eaf0ac3Slogwang * modification, are permitted provided that the following conditions
91eaf0ac3Slogwang * are met:
101eaf0ac3Slogwang * 1. Redistributions of source code must retain the above copyright
111eaf0ac3Slogwang * notice, this list of conditions and the following disclaimer.
121eaf0ac3Slogwang * 2. Redistributions in binary form must reproduce the above copyright
131eaf0ac3Slogwang * notice, this list of conditions and the following disclaimer in the
141eaf0ac3Slogwang * documentation and/or other materials provided with the distribution.
15*d4a07e70Sfengbojiang * 3. Neither the name of the University nor the names of its contributors
161eaf0ac3Slogwang * may be used to endorse or promote products derived from this software
171eaf0ac3Slogwang * without specific prior written permission.
181eaf0ac3Slogwang *
191eaf0ac3Slogwang * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
201eaf0ac3Slogwang * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
211eaf0ac3Slogwang * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
221eaf0ac3Slogwang * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
231eaf0ac3Slogwang * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
241eaf0ac3Slogwang * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
251eaf0ac3Slogwang * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
261eaf0ac3Slogwang * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
271eaf0ac3Slogwang * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
281eaf0ac3Slogwang * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
291eaf0ac3Slogwang * SUCH DAMAGE.
301eaf0ac3Slogwang *
311eaf0ac3Slogwang * @(#)tcp_seq.h 8.3 (Berkeley) 6/21/95
321eaf0ac3Slogwang * $FreeBSD$
331eaf0ac3Slogwang */
341eaf0ac3Slogwang
351eaf0ac3Slogwang #ifndef _NETINET_TCP_SEQ_H_
361eaf0ac3Slogwang #define _NETINET_TCP_SEQ_H_
371eaf0ac3Slogwang /*
381eaf0ac3Slogwang * TCP sequence numbers are 32 bit integers operated
391eaf0ac3Slogwang * on with modular arithmetic. These macros can be
401eaf0ac3Slogwang * used to compare such integers.
411eaf0ac3Slogwang */
421eaf0ac3Slogwang #define SEQ_LT(a,b) ((int)((a)-(b)) < 0)
431eaf0ac3Slogwang #define SEQ_LEQ(a,b) ((int)((a)-(b)) <= 0)
441eaf0ac3Slogwang #define SEQ_GT(a,b) ((int)((a)-(b)) > 0)
451eaf0ac3Slogwang #define SEQ_GEQ(a,b) ((int)((a)-(b)) >= 0)
461eaf0ac3Slogwang
471eaf0ac3Slogwang #define SEQ_MIN(a, b) ((SEQ_LT(a, b)) ? (a) : (b))
481eaf0ac3Slogwang #define SEQ_MAX(a, b) ((SEQ_GT(a, b)) ? (a) : (b))
491eaf0ac3Slogwang
50*d4a07e70Sfengbojiang #define WIN_LT(a,b) (ntohs(a) < ntohs(b))
51*d4a07e70Sfengbojiang #define WIN_LEQ(a,b) (ntohs(a) <= ntohs(b))
52*d4a07e70Sfengbojiang #define WIN_GT(a,b) (ntohs(a) > ntohs(b))
53*d4a07e70Sfengbojiang #define WIN_GEQ(a,b) (ntohs(a) >= ntohs(b))
54*d4a07e70Sfengbojiang
55*d4a07e70Sfengbojiang #define WIN_MIN(a, b) ((WIN_LT(a, b)) ? (a) : (b))
56*d4a07e70Sfengbojiang #define WIN_MAX(a, b) ((WIN_GT(a, b)) ? (a) : (b))
57*d4a07e70Sfengbojiang
581eaf0ac3Slogwang /* for modulo comparisons of timestamps */
591eaf0ac3Slogwang #define TSTMP_LT(a,b) ((int)((a)-(b)) < 0)
601eaf0ac3Slogwang #define TSTMP_GT(a,b) ((int)((a)-(b)) > 0)
611eaf0ac3Slogwang #define TSTMP_GEQ(a,b) ((int)((a)-(b)) >= 0)
621eaf0ac3Slogwang
631eaf0ac3Slogwang /*
641eaf0ac3Slogwang * Macros to initialize tcp sequence numbers for
651eaf0ac3Slogwang * send and receive from initial send and receive
661eaf0ac3Slogwang * sequence numbers.
671eaf0ac3Slogwang */
681eaf0ac3Slogwang #define tcp_rcvseqinit(tp) \
691eaf0ac3Slogwang (tp)->rcv_adv = (tp)->rcv_nxt = (tp)->irs + 1
701eaf0ac3Slogwang
711eaf0ac3Slogwang #define tcp_sendseqinit(tp) \
721eaf0ac3Slogwang (tp)->snd_una = (tp)->snd_nxt = (tp)->snd_max = (tp)->snd_up = \
731eaf0ac3Slogwang (tp)->snd_recover = (tp)->iss
741eaf0ac3Slogwang
751eaf0ac3Slogwang #ifdef _KERNEL
761eaf0ac3Slogwang /*
771eaf0ac3Slogwang * Clock macros for RFC 1323 timestamps.
781eaf0ac3Slogwang */
791eaf0ac3Slogwang #define TCP_TS_TO_TICKS(_t) ((_t) * hz / 1000)
801eaf0ac3Slogwang
811eaf0ac3Slogwang /* Timestamp wrap-around time, 24 days. */
821eaf0ac3Slogwang #define TCP_PAWS_IDLE (24 * 24 * 60 * 60 * 1000)
831eaf0ac3Slogwang
841eaf0ac3Slogwang /*
851eaf0ac3Slogwang * tcp_ts_getticks() in ms, should be 1ms < x < 1000ms according to RFC 1323.
861eaf0ac3Slogwang * We always use 1ms granularity independent of hz.
871eaf0ac3Slogwang */
88*d4a07e70Sfengbojiang static __inline uint32_t
tcp_ts_getticks(void)891eaf0ac3Slogwang tcp_ts_getticks(void)
901eaf0ac3Slogwang {
911eaf0ac3Slogwang struct timeval tv;
921eaf0ac3Slogwang
931eaf0ac3Slogwang /*
941eaf0ac3Slogwang * getmicrouptime() should be good enough for any 1-1000ms granularity.
951eaf0ac3Slogwang * Do not use getmicrotime() here as it might break nfsroot/tcp.
961eaf0ac3Slogwang */
971eaf0ac3Slogwang getmicrouptime(&tv);
98*d4a07e70Sfengbojiang return (tv.tv_sec * 1000 + tv.tv_usec / 1000);
991eaf0ac3Slogwang }
1001eaf0ac3Slogwang #endif /* _KERNEL */
1011eaf0ac3Slogwang
1021eaf0ac3Slogwang #endif /* _NETINET_TCP_SEQ_H_ */
103