Initial commit of OpenSPARC T2 architecture model.
[OpenSPARC-T2-SAM] / obp / obp / pkg / netinet / nbpools.fth
CommitLineData
920dae64
AT
1\ ========== Copyright Header Begin ==========================================
2\
3\ Hypervisor Software File: nbpools.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: @(#)nbpools.fth 1.1 04/09/07
43purpose: Network buffer pool management
44copyright: Copyright 2004 Sun Microsystems, Inc. All Rights Reserved
45copyright: Use is subject to license terms.
46
47\ To speed packet processing time, we must be able to allocate space
48\ quickly and must avoid copying data as packets move between protocol
49\ layers.
50\
51\ We preallocate many buffers large enough to hold a single packet
52\ and a few buffers large enough to hold large datagrams. These
53\ buffer pools are maintained as linked lists. Network level I/O is
54\ performed using small buffers; large buffers are used when generating
55\ or reassembling large datagrams. An entire buffer is passed to IP
56\ after reading a packet into it; data needs to be copied only when
57\ dealing with large datagrams.
58\
59\ To make buffer processing uniform, a self-identifying buffer scheme
60\ is used. Each buffer entry maintains its size using which the
61\ buffer is returned to the appropriate (small or large) buffer pool
62\ when freed.
63
64headerless
65
66struct
67 /queue-entry field >netbuf-link \ Queue pointers
68 /l field >netbuf-len \ Buffer size
69 0 field >netbuf-adr \ Buffer contents
70constant /netbuf-hdr
71
72\ Allocate memory for a network buffer
73: netbuf-alloc ( bufsize -- netbuf )
74 dup /netbuf-hdr + alloc-mem dup queue-init tuck >netbuf-len l!
75;
76
77\ Destroy a network buffer
78: netbuf-free ( netbuf -- )
79 dup >netbuf-len l@ /netbuf-hdr + free-mem
80;
81
82\ Initialize a buffer pool
83: netbuf-pool-create ( qhead bufsize nbufs -- )
84 2 pick queue-init
85 0 ?do 2dup netbuf-alloc enqueue loop 2drop
86;
87
88\ Destroy a buffer pool
89: netbuf-pool-destroy ( qhead -- )
90 begin dup dequeue ?dup while netbuf-free repeat drop
91;
92
93/queue-head instance buffer: netpkt-bufq \ Network packet buffer pool
94/queue-head instance buffer: lrgpkt-bufq \ Large packet buffer pool
95
96d# 32 constant NETPKT_POOL_SIZE \ Number of network packet buffers
97d# 4 constant LRGPKT_POOL_SIZE \ Number of large packet buffers
98
99\ Allocate a frame from the network packet buffer pool
100: frame-alloc ( -- frame )
101 netpkt-bufq dequeue dup if >netbuf-adr then
102;
103
104\ Return a frame to the network packet buffer pool
105: frame-free ( frame -- )
106 netpkt-bufq swap /netbuf-hdr - enqueue
107;
108
109\ Allocate a packet from the network packet buffer pool
110: pkt-alloc ( -- pkt )
111 netpkt-bufq dequeue dup if >netbuf-adr if-hdrlen@ + then
112;
113
114\ Allocate a packet from the large packet buffer pool
115: lrgpkt-alloc ( -- pkt )
116 lrgpkt-bufq dequeue dup if >netbuf-adr if-hdrlen@ + then
117;
118
119\ Return a packet to the pool from which it was allocated
120: pkt-free ( pkt -- )
121 if-hdrlen@ - /netbuf-hdr - ( netbuf )
122 dup >netbuf-len l@ if-frame-size@ = if ( netbuf )
123 netpkt-bufq ( netbuf netpkt-qhead )
124 else ( netbuf )
125 lrgpkt-bufq ( netbuf lrgpkt-qhead )
126 then ( netbuf qhead )
127 swap enqueue ( )
128;
129
130\ Enqueue a packet buffer in the specified queue
131: pkt-enqueue ( qhead pkt -- )
132 if-hdrlen@ - /netbuf-hdr - enqueue
133;
134
135\ Dequeue a packet buffer from the specified queue
136: pkt-dequeue ( qhead -- pkt )
137 dequeue dup if >netbuf-adr if-hdrlen@ + then
138;
139
140\ Initialize network buffer pools
141: init-nbpools ( -- )
142 netpkt-bufq if-frame-size@ NETPKT_POOL_SIZE netbuf-pool-create
143 lrgpkt-bufq IP_MAX_PKTSIZE if-hdrlen@ + LRGPKT_POOL_SIZE netbuf-pool-create
144;
145
146\ Destroy network buffer pools
147: free-nbpools ( -- )
148 netpkt-bufq netbuf-pool-destroy
149 lrgpkt-bufq netbuf-pool-destroy
150;
151
152headers