Initial commit of OpenSPARC T2 architecture model.
[OpenSPARC-T2-SAM] / obp / obp / pkg / netinet / tcpbuf.fth
CommitLineData
920dae64
AT
1\ ========== Copyright Header Begin ==========================================
2\
3\ Hypervisor Software File: tcpbuf.fth
4\
5\ Copyright (c) 2006 Sun Microsystems, Inc. All Rights Reserved.
6\
7\ - Do no alter or remove copyright notices
8\
9\ - Redistribution and use of this software in source and binary forms, with
10\ or without modification, are permitted provided that the following
11\ conditions are met:
12\
13\ - Redistribution of source code must retain the above copyright notice,
14\ this list of conditions and the following disclaimer.
15\
16\ - Redistribution in binary form must reproduce the above copyright notice,
17\ this list of conditions and the following disclaimer in the
18\ documentation and/or other materials provided with the distribution.
19\
20\ Neither the name of Sun Microsystems, Inc. or the names of contributors
21\ may be used to endorse or promote products derived from this software
22\ without specific prior written permission.
23\
24\ This software is provided "AS IS," without a warranty of any kind.
25\ ALL EXPRESS OR IMPLIED CONDITIONS, REPRESENTATIONS AND WARRANTIES,
26\ INCLUDING ANY IMPLIED WARRANTY OF MERCHANTABILITY, FITNESS FOR A
27\ PARTICULAR PURPOSE OR NON-INFRINGEMENT, ARE HEREBY EXCLUDED. SUN
28\ MICROSYSTEMS, INC. ("SUN") AND ITS LICENSORS SHALL NOT BE LIABLE FOR
29\ ANY DAMAGES SUFFERED BY LICENSEE AS A RESULT OF USING, MODIFYING OR
30\ DISTRIBUTING THIS SOFTWARE OR ITS DERIVATIVES. IN NO EVENT WILL SUN
31\ OR ITS LICENSORS BE LIABLE FOR ANY LOST REVENUE, PROFIT OR DATA, OR
32\ FOR DIRECT, INDIRECT, SPECIAL, CONSEQUENTIAL, INCIDENTAL OR PUNITIVE
33\ DAMAGES, HOWEVER CAUSED AND REGARDLESS OF THE THEORY OF LIABILITY,
34\ ARISING OUT OF THE USE OF OR INABILITY TO USE THIS SOFTWARE, EVEN IF
35\ SUN HAS BEEN ADVISED OF THE POSSIBILITY OF SUCH DAMAGES.
36\
37\ You acknowledge that this software is not designed, licensed or
38\ intended for use in the design, construction, operation or maintenance of
39\ any nuclear facility.
40\
41\ ========== Copyright Header End ============================================
42id: @(#)tcpbuf.fth 1.1 04/09/07
43purpose: TCP buffer management routines
44copyright: Copyright 2004 Sun Microsystems, Inc. All Rights Reserved
45copyright: Use is subject to license terms.
46
47\ TCP's send and receive buffers are maintained as circular buffers.
48\ The buffer sizes reflect the corresponding initial window sizes.
49\ Sequence space computations are translated into corresponding buffer
50\ addresses when accessing data.
51
52headerless
53
54struct
55 /n field >tcpbuf-adr \ Pointer to buffer
56 /l field >tcpbuf-size \ Buffer size
57 /l field >tcpbuf-start \ Offset to start of valid data
58 /l field >tcpbuf-count \ Data character count
59constant /tcp-buffer
60
61: tcpbuf-adr@ ( tcpbuf -- adr ) >tcpbuf-adr @ ;
62: tcpbuf-size@ ( tcpbuf -- size ) >tcpbuf-size l@ ;
63
64: tcpbuf-count@ ( tcpbuf -- n ) >tcpbuf-count l@ ;
65: tcpbuf-count! ( n tcpbuf -- ) >tcpbuf-count l! ;
66
67: tcpbuf-count+! ( tcpbuf n -- )
68 over tcpbuf-count@ + swap tcpbuf-count!
69;
70
71: tcpbuf-start@ ( tcpbuf -- offset ) >tcpbuf-start l@ ;
72: tcpbuf-start! ( offset tcpbuf -- ) >tcpbuf-start l! ;
73
74: tcpbuf-start+! ( tcpbuf n -- )
75 over tcpbuf-start@ + over tcpbuf-size@ mod swap tcpbuf-start!
76;
77
78\ Get amount of free space in buffer.
79: tcpbuf-space@ ( tcpbuf -- n )
80 dup tcpbuf-size@ swap tcpbuf-count@ -
81;
82
83\ Allocate a TCP buffer.
84: tcpbuf-init ( tcpbuf bufsize -- )
85 2dup alloc-mem swap >tcpbuf-adr !
86 over >tcpbuf-size l!
87 0 over >tcpbuf-start l!
88 0 swap >tcpbuf-count l!
89;
90
91\ Free TCP buffer resources.
92: tcpbuf-free ( tcpbuf -- )
93 dup tcpbuf-adr@ swap tcpbuf-size@ free-mem
94;
95
96\ Convert offset from start of valid data in the buffer to an index
97\ from start of the buffer.
98: tcpbuf-offset>adrindex ( tcpbuf offset -- index )
99 over tcpbuf-start@ + swap tcpbuf-size@ mod
100;
101
102\ Copy 'len' bytes of data from 'adr' into the TCP buffer, starting at
103\ 'offset' bytes from the current start of valid data in the buffer,
104\ handling wraparounds as necessary. The actual amount of data copied
105\ is limited by the free space available in the buffer.
106
107: (tcpbuf-write) ( tcpbuf index adr len -- )
108 >r >r >r tcpbuf-adr@ r> ca+ r> swap r> move
109;
110
111: tcpbuf-write ( tcpbuf offset adr len -- len' )
112 3 pick tcpbuf-space@ min dup >r ( buf offset adr len' ) ( r: len' )
113 2swap ( adr len' buf offset )
114 over swap tcpbuf-offset>adrindex ( adr len' buf index )
115 2swap 2over ( buf index adr len' buf index )
116 swap tcpbuf-size@ swap - over min ( buf index adr len' n )
117 tuck - >r ( buf index adr n ) ( r: len' rem )
118 2over 2over (tcpbuf-write) ( buf index adr n )
119 + nip 0 swap r> (tcpbuf-write) ( ) ( r: len' )
120 r> ( len' ) ( r: )
121;
122
123\ Copy 'len' bytes of data starting at 'offset' bytes from current
124\ start of valid data in the TCP buffer to the address at 'adr',
125\ handling buffer wraparounds as necessary. The actual amount of
126\ data read is limited by the amount of data at hand.
127
128: (tcpbuf-read) ( tcpbuf index adr len -- )
129 >r >r >r tcpbuf-adr@ r> ca+ r> r> move
130;
131
132: tcpbuf-read ( tcpbuf offset adr len -- len' )
133 3 pick tcpbuf-count@ min dup >r ( buf offset adr len' ) ( r: len' )
134 2swap ( adr len' buf offset )
135 over swap tcpbuf-offset>adrindex ( adr len' buf index )
136 2swap 2over ( buf index adr len' buf index )
137 swap tcpbuf-size@ swap - over min ( buf index adr len' n )
138 tuck - >r ( buf index adr n ) ( r: len' rem )
139 2over 2over (tcpbuf-read) ( buf index adr n )
140 + nip 0 swap r> (tcpbuf-read) ( ) ( r: len' )
141 r> ( len' ) ( r: )
142;
143
144\ Discard n bytes from start of buffer.
145: tcpbuf-drop ( tcpbuf n -- )
146 over tcpbuf-count@ min 2dup tcpbuf-start+! negate tcpbuf-count+!
147;
148
149headers