Rev 4743 | Go to most recent revision | Only display areas with differences | Ignore whitespace | Details | Blame | Last modification | View Log | RSS feed
Rev 4743 | Rev 4754 | ||
---|---|---|---|
1 | /* |
1 | /* |
2 | * Copyright (c) 2009 Lukas Mejdrech |
2 | * Copyright (c) 2009 Lukas Mejdrech |
3 | * All rights reserved. |
3 | * All rights reserved. |
4 | * |
4 | * |
5 | * Redistribution and use in source and binary forms, with or without |
5 | * Redistribution and use in source and binary forms, with or without |
6 | * modification, are permitted provided that the following conditions |
6 | * modification, are permitted provided that the following conditions |
7 | * are met: |
7 | * are met: |
8 | * |
8 | * |
9 | * - Redistributions of source code must retain the above copyright |
9 | * - Redistributions of source code must retain the above copyright |
10 | * notice, this list of conditions and the following disclaimer. |
10 | * notice, this list of conditions and the following disclaimer. |
11 | * - Redistributions in binary form must reproduce the above copyright |
11 | * - Redistributions in binary form must reproduce the above copyright |
12 | * notice, this list of conditions and the following disclaimer in the |
12 | * notice, this list of conditions and the following disclaimer in the |
13 | * documentation and/or other materials provided with the distribution. |
13 | * documentation and/or other materials provided with the distribution. |
14 | * - The name of the author may not be used to endorse or promote products |
14 | * - The name of the author may not be used to endorse or promote products |
15 | * derived from this software without specific prior written permission. |
15 | * derived from this software without specific prior written permission. |
16 | * |
16 | * |
17 | * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR |
17 | * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR |
18 | * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES |
18 | * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES |
19 | * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. |
19 | * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. |
20 | * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, |
20 | * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, |
21 | * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT |
21 | * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT |
22 | * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, |
22 | * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, |
23 | * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY |
23 | * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY |
24 | * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
24 | * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
25 | * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF |
25 | * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF |
26 | * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
26 | * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
27 | */ |
27 | */ |
28 | 28 | ||
29 | /** @addtogroup packet |
29 | /** @addtogroup packet |
30 | * @{ |
30 | * @{ |
31 | */ |
31 | */ |
32 | 32 | ||
33 | /** @file |
33 | /** @file |
34 | * Packet server implementation. |
34 | * Packet server implementation. |
35 | */ |
35 | */ |
36 | 36 | ||
37 | #include <align.h> |
37 | #include <align.h> |
38 | #include <assert.h> |
38 | #include <assert.h> |
39 | #include <async.h> |
39 | #include <async.h> |
40 | #include <errno.h> |
40 | #include <errno.h> |
41 | #include <fibril_sync.h> |
41 | #include <fibril_sync.h> |
42 | #include <unistd.h> |
42 | #include <unistd.h> |
43 | 43 | ||
44 | #include <ipc/ipc.h> |
44 | #include <ipc/ipc.h> |
45 | #include <sys/mman.h> |
45 | #include <sys/mman.h> |
46 | 46 | ||
47 | #include "../../err.h" |
47 | #include "../../err.h" |
48 | #include "../../messages.h" |
48 | #include "../../messages.h" |
49 | 49 | ||
50 | #include "packet.h" |
50 | #include "packet.h" |
51 | #include "packet_client.h" |
51 | #include "packet_client.h" |
52 | #include "packet_header.h" |
52 | #include "packet_header.h" |
53 | #include "packet_messages.h" |
53 | #include "packet_messages.h" |
54 | #include "packet_server.h" |
54 | #include "packet_server.h" |
55 | 55 | ||
56 | #define FREE_QUEUES_COUNT 7 |
56 | #define FREE_QUEUES_COUNT 7 |
57 | 57 | ||
58 | /** The default address length reserved for new packets. |
58 | /** The default address length reserved for new packets. |
59 | */ |
59 | */ |
60 | #define DEFAULT_ADDR_LEN 32 |
60 | #define DEFAULT_ADDR_LEN 32 |
61 | 61 | ||
62 | /** The default prefix reserved for new packets. |
62 | /** The default prefix reserved for new packets. |
63 | */ |
63 | */ |
64 | #define DEFAULT_PREFIX 64 |
64 | #define DEFAULT_PREFIX 64 |
65 | 65 | ||
66 | /** The default suffix reserved for new packets. |
66 | /** The default suffix reserved for new packets. |
67 | */ |
67 | */ |
68 | #define DEFAULT_SUFFIX 64 |
68 | #define DEFAULT_SUFFIX 64 |
69 | 69 | ||
70 | /** Packet server global data. |
70 | /** Packet server global data. |
71 | */ |
71 | */ |
72 | static struct{ |
72 | static struct{ |
73 | /** Safety lock. |
73 | /** Safety lock. |
74 | */ |
74 | */ |
75 | fibril_mutex_t lock; |
75 | fibril_mutex_t lock; |
76 | /** Free packet queues. |
76 | /** Free packet queues. |
77 | */ |
77 | */ |
78 | packet_t free[ FREE_QUEUES_COUNT ]; |
78 | packet_t free[ FREE_QUEUES_COUNT ]; |
79 | /** Packet length upper bounds of the free packet queues. |
79 | /** Packet length upper bounds of the free packet queues. |
80 | * The maximal lengths of packets in each queue in the ascending order. |
80 | * The maximal lengths of packets in each queue in the ascending order. |
81 | * The last queue is not limited. |
81 | * The last queue is not limited. |
82 | */ |
82 | */ |
83 | size_t sizes[ FREE_QUEUES_COUNT ]; |
83 | size_t sizes[ FREE_QUEUES_COUNT ]; |
84 | /** Total packets allocated. |
84 | /** Total packets allocated. |
85 | */ |
85 | */ |
86 | unsigned int count; |
86 | unsigned int count; |
87 | } ps_globals = { |
87 | } ps_globals = { |
- | 88 | .lock = { |
|
- | 89 | .counter = 1, |
|
- | 90 | .waiters = { |
|
- | 91 | .prev = & ps_globals.lock.waiters, |
|
88 | { .counter = 1, .waiters = { .prev = & ps_globals.lock.waiters, .next = & ps_globals.lock.waiters, }}, |
92 | .next = & ps_globals.lock.waiters, |
- | 93 | } |
|
- | 94 | }, |
|
89 | { NULL, NULL, NULL, NULL, NULL, NULL, NULL }, |
95 | .free = { NULL, NULL, NULL, NULL, NULL, NULL, NULL }, |
90 | { PAGE_SIZE, PAGE_SIZE * 2, PAGE_SIZE * 4, PAGE_SIZE * 8, PAGE_SIZE * 16, PAGE_SIZE * 32, PAGE_SIZE * 64 }, |
96 | .sizes = { PAGE_SIZE, PAGE_SIZE * 2, PAGE_SIZE * 4, PAGE_SIZE * 8, PAGE_SIZE * 16, PAGE_SIZE * 32, PAGE_SIZE * 64 }, |
91 | 0 |
97 | .count = 0 |
92 | }; |
98 | }; |
93 | 99 | ||
94 | /** @name Packet server support functions |
100 | /** @name Packet server support functions |
95 | */ |
101 | */ |
96 | /*@{*/ |
102 | /*@{*/ |
97 | 103 | ||
98 | /** Returns the packet of dimensions at least as given. |
104 | /** Returns the packet of dimensions at least as given. |
99 | * Tries to reuse free packets first. |
105 | * Tries to reuse free packets first. |
100 | * Creates a new packet aligned to the memory page size if none available. |
106 | * Creates a new packet aligned to the memory page size if none available. |
101 | * Locks the global data during its processing. |
107 | * Locks the global data during its processing. |
102 | * @param addr_len The source and destination addresses maximal length in bytes. Input parameter. |
108 | * @param addr_len The source and destination addresses maximal length in bytes. Input parameter. |
103 | * @param max_prefix The maximal prefix length in bytes. Input parameter. |
109 | * @param max_prefix The maximal prefix length in bytes. Input parameter. |
104 | * @param max_content The maximal content length in bytes. Input parameter. |
110 | * @param max_content The maximal content length in bytes. Input parameter. |
105 | * @param max_suffix The maximal suffix length in bytes. Input parameter. |
111 | * @param max_suffix The maximal suffix length in bytes. Input parameter. |
106 | * @returns The packet of dimensions at least as given. |
112 | * @returns The packet of dimensions at least as given. |
107 | * @returns NULL if there is not enough memory left. |
113 | * @returns NULL if there is not enough memory left. |
108 | */ |
114 | */ |
109 | packet_t packet_get( size_t addr_len, size_t max_prefix, size_t max_content, size_t max_suffix ); |
115 | packet_t packet_get( size_t addr_len, size_t max_prefix, size_t max_content, size_t max_suffix ); |
110 | 116 | ||
111 | /** Releases the packet queue. |
117 | /** Releases the packet queue. |
112 | * @param packet_id The first packet identifier. Input parameter. |
118 | * @param packet_id The first packet identifier. Input parameter. |
113 | * @returns EOK on success. |
119 | * @returns EOK on success. |
114 | * @returns ENOENT if there is no such packet. |
120 | * @returns ENOENT if there is no such packet. |
115 | */ |
121 | */ |
116 | int packet_release_wrapper( packet_id_t packet_id ); |
122 | int packet_release_wrapper( packet_id_t packet_id ); |
117 | 123 | ||
118 | /** Releases the packet and returns it to the appropriate free packet queue. |
124 | /** Releases the packet and returns it to the appropriate free packet queue. |
119 | * Should be used only when the global data are locked. |
125 | * Should be used only when the global data are locked. |
120 | * @param packet The packet to be released. Input parameter. |
126 | * @param packet The packet to be released. Input parameter. |
121 | */ |
127 | */ |
122 | void packet_release( packet_t packet ); |
128 | void packet_release( packet_t packet ); |
123 | 129 | ||
124 | /** Creates a new packet of dimensions at least as given. |
130 | /** Creates a new packet of dimensions at least as given. |
125 | * Should be used only when the global data are locked. |
131 | * Should be used only when the global data are locked. |
126 | * @param length The total length of the packet, including the header, the addresses and the data of the packet. Input parameter. |
132 | * @param length The total length of the packet, including the header, the addresses and the data of the packet. Input parameter. |
127 | * @param addr_len The source and destination addresses maximal length in bytes. Input parameter. |
133 | * @param addr_len The source and destination addresses maximal length in bytes. Input parameter. |
128 | * @param max_prefix The maximal prefix length in bytes. Input parameter. |
134 | * @param max_prefix The maximal prefix length in bytes. Input parameter. |
129 | * @param max_content The maximal content length in bytes. Input parameter. |
135 | * @param max_content The maximal content length in bytes. Input parameter. |
130 | * @param max_suffix The maximal suffix length in bytes. Input parameter. |
136 | * @param max_suffix The maximal suffix length in bytes. Input parameter. |
131 | * @returns The packet of dimensions at least as given. |
137 | * @returns The packet of dimensions at least as given. |
132 | * @returns NULL if there is not enough memory left. |
138 | * @returns NULL if there is not enough memory left. |
133 | */ |
139 | */ |
134 | packet_t packet_create( size_t length, size_t addr_len, size_t max_prefix, size_t max_content, size_t max_suffix ); |
140 | packet_t packet_create( size_t length, size_t addr_len, size_t max_prefix, size_t max_content, size_t max_suffix ); |
135 | 141 | ||
136 | /** Clears and initializes the packet according to the given dimensions. |
142 | /** Clears and initializes the packet according to the given dimensions. |
137 | * @param packet The packet to be initialized. Input parameter. |
143 | * @param packet The packet to be initialized. Input parameter. |
138 | * @param addr_len The source and destination addresses maximal length in bytes. Input parameter. |
144 | * @param addr_len The source and destination addresses maximal length in bytes. Input parameter. |
139 | * @param max_prefix The maximal prefix length in bytes. Input parameter. |
145 | * @param max_prefix The maximal prefix length in bytes. Input parameter. |
140 | * @param max_content The maximal content length in bytes. Input parameter. |
146 | * @param max_content The maximal content length in bytes. Input parameter. |
141 | * @param max_suffix The maximal suffix length in bytes. Input parameter. |
147 | * @param max_suffix The maximal suffix length in bytes. Input parameter. |
142 | */ |
148 | */ |
143 | void packet_init( packet_t packet, size_t addr_len, size_t max_prefix, size_t max_content, size_t max_suffix ); |
149 | void packet_init( packet_t packet, size_t addr_len, size_t max_prefix, size_t max_content, size_t max_suffix ); |
144 | 150 | ||
145 | /** Shares the packet memory block. |
151 | /** Shares the packet memory block. |
146 | * @param packet The packet to be shared. |
152 | * @param packet The packet to be shared. |
147 | * @returns EOK on success. |
153 | * @returns EOK on success. |
148 | * @returns EINVAL if the packet is not valid. |
154 | * @returns EINVAL if the packet is not valid. |
149 | * @returns EINVAL if the calling module does not accept the memory. |
155 | * @returns EINVAL if the calling module does not accept the memory. |
150 | * @returns ENOMEM if the desired and actual sizes differ. |
156 | * @returns ENOMEM if the desired and actual sizes differ. |
151 | * @returns Other error codes as defined for the ipc_share_in_finalize() function. |
157 | * @returns Other error codes as defined for the ipc_share_in_finalize() function. |
152 | */ |
158 | */ |
153 | int packet_reply( const packet_t packet ); |
159 | int packet_reply( const packet_t packet ); |
154 | 160 | ||
155 | /*@}*/ |
161 | /*@}*/ |
156 | 162 | ||
157 | int packet_translate( int phone, packet_ref packet, packet_id_t packet_id ){ |
163 | int packet_translate( int phone, packet_ref packet, packet_id_t packet_id ){ |
158 | if( ! packet ) return EINVAL; |
164 | if( ! packet ) return EINVAL; |
159 | * packet = pm_find( packet_id ); |
165 | * packet = pm_find( packet_id ); |
160 | return ( * packet ) ? EOK : ENOENT; |
166 | return ( * packet ) ? EOK : ENOENT; |
161 | } |
167 | } |
162 | 168 | ||
163 | packet_t packet_get_4( int phone, size_t max_content, size_t addr_len, size_t max_prefix, size_t max_suffix ){ |
169 | packet_t packet_get_4( int phone, size_t max_content, size_t addr_len, size_t max_prefix, size_t max_suffix ){ |
164 | return packet_get( addr_len, max_prefix, max_content, max_suffix ); |
170 | return packet_get( addr_len, max_prefix, max_content, max_suffix ); |
165 | } |
171 | } |
166 | 172 | ||
167 | packet_t packet_get_1( int phone, size_t content ){ |
173 | packet_t packet_get_1( int phone, size_t content ){ |
168 | return packet_get( DEFAULT_ADDR_LEN, DEFAULT_PREFIX, content, DEFAULT_SUFFIX ); |
174 | return packet_get( DEFAULT_ADDR_LEN, DEFAULT_PREFIX, content, DEFAULT_SUFFIX ); |
169 | } |
175 | } |
170 | 176 | ||
171 | void pq_release( int phone, packet_id_t packet_id ){ |
177 | void pq_release( int phone, packet_id_t packet_id ){ |
172 | ( void ) packet_release_wrapper( packet_id ); |
178 | ( void ) packet_release_wrapper( packet_id ); |
173 | } |
179 | } |
174 | 180 | ||
175 | int packet_server_message( ipc_callid_t callid, ipc_call_t * call, ipc_call_t * answer, int * answer_count ){ |
181 | int packet_server_message( ipc_callid_t callid, ipc_call_t * call, ipc_call_t * answer, int * answer_count ){ |
176 | packet_t packet; |
182 | packet_t packet; |
177 | 183 | ||
178 | * answer_count = 0; |
184 | * answer_count = 0; |
179 | switch( IPC_GET_METHOD( * call )){ |
185 | switch( IPC_GET_METHOD( * call )){ |
180 | case IPC_M_PHONE_HUNGUP: |
186 | case IPC_M_PHONE_HUNGUP: |
181 | return EOK; |
187 | return EOK; |
182 | case NET_PACKET_CREATE_1: |
188 | case NET_PACKET_CREATE_1: |
183 | packet = packet_get( DEFAULT_ADDR_LEN, DEFAULT_PREFIX, IPC_GET_CONTENT( call ), DEFAULT_SUFFIX ); |
189 | packet = packet_get( DEFAULT_ADDR_LEN, DEFAULT_PREFIX, IPC_GET_CONTENT( call ), DEFAULT_SUFFIX ); |
184 | if( ! packet ) return ENOMEM; |
190 | if( ! packet ) return ENOMEM; |
185 | * answer_count = 2; |
191 | * answer_count = 2; |
186 | IPC_SET_ARG1( * answer, packet->packet_id ); |
192 | IPC_SET_ARG1( * answer, packet->packet_id ); |
187 | IPC_SET_ARG2( * answer, packet->length ); |
193 | IPC_SET_ARG2( * answer, packet->length ); |
188 | return EOK; |
194 | return EOK; |
189 | case NET_PACKET_CREATE_4: |
195 | case NET_PACKET_CREATE_4: |
190 | packet = packet_get( IPC_GET_ADDR_LEN( call ), IPC_GET_PREFIX( call ), IPC_GET_CONTENT( call ), IPC_GET_SUFFIX( call )); |
196 | packet = packet_get( IPC_GET_ADDR_LEN( call ), IPC_GET_PREFIX( call ), IPC_GET_CONTENT( call ), IPC_GET_SUFFIX( call )); |
191 | if( ! packet ) return ENOMEM; |
197 | if( ! packet ) return ENOMEM; |
192 | * answer_count = 2; |
198 | * answer_count = 2; |
193 | IPC_SET_ARG1( * answer, packet->packet_id ); |
199 | IPC_SET_ARG1( * answer, packet->packet_id ); |
194 | IPC_SET_ARG2( * answer, packet->length ); |
200 | IPC_SET_ARG2( * answer, packet->length ); |
195 | return EOK; |
201 | return EOK; |
196 | case NET_PACKET_GET: |
202 | case NET_PACKET_GET: |
197 | packet = pm_find( IPC_GET_ID( call )); |
203 | packet = pm_find( IPC_GET_ID( call )); |
198 | if( ! packet_is_valid( packet )) return ENOENT; |
204 | if( ! packet_is_valid( packet )) return ENOENT; |
199 | return packet_reply( packet ); |
205 | return packet_reply( packet ); |
200 | case NET_PACKET_GET_SIZE: |
206 | case NET_PACKET_GET_SIZE: |
201 | packet = pm_find( IPC_GET_ID( call )); |
207 | packet = pm_find( IPC_GET_ID( call )); |
202 | if( ! packet_is_valid( packet )) return ENOENT; |
208 | if( ! packet_is_valid( packet )) return ENOENT; |
203 | IPC_SET_ARG1( * answer, packet->length ); |
209 | IPC_SET_ARG1( * answer, packet->length ); |
204 | * answer_count = 1; |
210 | * answer_count = 1; |
205 | return EOK; |
211 | return EOK; |
206 | case NET_PACKET_RELEASE: |
212 | case NET_PACKET_RELEASE: |
207 | return packet_release_wrapper( IPC_GET_ID( call )); |
213 | return packet_release_wrapper( IPC_GET_ID( call )); |
208 | } |
214 | } |
209 | return ENOTSUP; |
215 | return ENOTSUP; |
210 | } |
216 | } |
211 | 217 | ||
212 | int packet_release_wrapper( packet_id_t packet_id ){ |
218 | int packet_release_wrapper( packet_id_t packet_id ){ |
213 | packet_t packet; |
219 | packet_t packet; |
214 | 220 | ||
215 | packet = pm_find( packet_id ); |
221 | packet = pm_find( packet_id ); |
216 | if( ! packet_is_valid( packet )) return ENOENT; |
222 | if( ! packet_is_valid( packet )) return ENOENT; |
217 | fibril_mutex_lock( & ps_globals.lock ); |
223 | fibril_mutex_lock( & ps_globals.lock ); |
218 | pq_destroy( packet, packet_release ); |
224 | pq_destroy( packet, packet_release ); |
219 | fibril_mutex_unlock( & ps_globals.lock ); |
225 | fibril_mutex_unlock( & ps_globals.lock ); |
220 | return EOK; |
226 | return EOK; |
221 | } |
227 | } |
222 | 228 | ||
223 | void packet_release( packet_t packet ){ |
229 | void packet_release( packet_t packet ){ |
224 | int index; |
230 | int index; |
225 | 231 | ||
226 | // remove debug dump |
232 | // remove debug dump |
227 | // printf( "packet %d released\n", packet->packet_id ); |
233 | // printf( "packet %d released\n", packet->packet_id ); |
228 | for( index = 0; ( index < FREE_QUEUES_COUNT - 1 ) && ( packet->length > ps_globals.sizes[ index ] ); ++ index ); |
234 | for( index = 0; ( index < FREE_QUEUES_COUNT - 1 ) && ( packet->length > ps_globals.sizes[ index ] ); ++ index ); |
229 | ps_globals.free[ index ] = pq_add( ps_globals.free[ index ], packet, packet->length, packet->length ); |
235 | ps_globals.free[ index ] = pq_add( ps_globals.free[ index ], packet, packet->length, packet->length ); |
230 | assert( ps_globals.free[ index ] ); |
236 | assert( ps_globals.free[ index ] ); |
231 | } |
237 | } |
232 | 238 | ||
233 | packet_t packet_get( size_t addr_len, size_t max_prefix, size_t max_content, size_t max_suffix ){ |
239 | packet_t packet_get( size_t addr_len, size_t max_prefix, size_t max_content, size_t max_suffix ){ |
234 | int index; |
240 | int index; |
235 | packet_t packet; |
241 | packet_t packet; |
236 | size_t length; |
242 | size_t length; |
237 | 243 | ||
238 | length = ALIGN_UP( sizeof( struct packet ) + 2 * addr_len + max_prefix + max_content + max_suffix, PAGE_SIZE ); |
244 | length = ALIGN_UP( sizeof( struct packet ) + 2 * addr_len + max_prefix + max_content + max_suffix, PAGE_SIZE ); |
239 | fibril_mutex_lock( & ps_globals.lock ); |
245 | fibril_mutex_lock( & ps_globals.lock ); |
240 | for( index = 0; index < FREE_QUEUES_COUNT - 1; ++ index ){ |
246 | for( index = 0; index < FREE_QUEUES_COUNT - 1; ++ index ){ |
241 | if( length <= ps_globals.sizes[ index ] ){ |
247 | if( length <= ps_globals.sizes[ index ] ){ |
242 | packet = ps_globals.free[ index ]; |
248 | packet = ps_globals.free[ index ]; |
243 | while( packet_is_valid( packet ) && ( packet->length < length )){ |
249 | while( packet_is_valid( packet ) && ( packet->length < length )){ |
244 | packet = pm_find( packet->next ); |
250 | packet = pm_find( packet->next ); |
245 | } |
251 | } |
246 | if( packet_is_valid( packet )){ |
252 | if( packet_is_valid( packet )){ |
247 | if( packet == ps_globals.free[ index ] ){ |
253 | if( packet == ps_globals.free[ index ] ){ |
248 | ps_globals.free[ index ] = pq_detach( packet ); |
254 | ps_globals.free[ index ] = pq_detach( packet ); |
249 | }else{ |
255 | }else{ |
250 | pq_detach( packet ); |
256 | pq_detach( packet ); |
251 | } |
257 | } |
252 | packet_init( packet, addr_len, max_prefix, max_content, max_suffix ); |
258 | packet_init( packet, addr_len, max_prefix, max_content, max_suffix ); |
253 | fibril_mutex_unlock( & ps_globals.lock ); |
259 | fibril_mutex_unlock( & ps_globals.lock ); |
254 | // remove debug dump |
260 | // remove debug dump |
255 | // printf( "packet %d got\n", packet->packet_id ); |
261 | // printf( "packet %d got\n", packet->packet_id ); |
256 | return packet; |
262 | return packet; |
257 | } |
263 | } |
258 | } |
264 | } |
259 | } |
265 | } |
260 | packet = packet_create( length, addr_len, max_prefix, max_content, max_suffix ); |
266 | packet = packet_create( length, addr_len, max_prefix, max_content, max_suffix ); |
261 | fibril_mutex_unlock( & ps_globals.lock ); |
267 | fibril_mutex_unlock( & ps_globals.lock ); |
262 | // remove debug dump |
268 | // remove debug dump |
263 | printf( "packet %d created\n", packet->packet_id ); |
269 | // printf( "packet %d created\n", packet->packet_id ); |
264 | return packet; |
270 | return packet; |
265 | } |
271 | } |
266 | 272 | ||
267 | packet_t packet_create( size_t length, size_t addr_len, size_t max_prefix, size_t max_content, size_t max_suffix ){ |
273 | packet_t packet_create( size_t length, size_t addr_len, size_t max_prefix, size_t max_content, size_t max_suffix ){ |
268 | ERROR_DECLARE; |
274 | ERROR_DECLARE; |
269 | 275 | ||
270 | packet_t packet; |
276 | packet_t packet; |
271 | 277 | ||
272 | // already locked |
278 | // already locked |
273 | packet = ( packet_t ) mmap( NULL, length, PROTO_READ | PROTO_WRITE, MAP_SHARED | MAP_ANONYMOUS, 0, 0 ); |
279 | packet = ( packet_t ) mmap( NULL, length, PROTO_READ | PROTO_WRITE, MAP_SHARED | MAP_ANONYMOUS, 0, 0 ); |
274 | if( packet == MAP_FAILED ) return NULL; |
280 | if( packet == MAP_FAILED ) return NULL; |
275 | ++ ps_globals.count; |
281 | ++ ps_globals.count; |
276 | packet->packet_id = ps_globals.count; |
282 | packet->packet_id = ps_globals.count; |
277 | packet->length = length; |
283 | packet->length = length; |
278 | packet_init( packet, addr_len, max_prefix, max_content, max_suffix ); |
284 | packet_init( packet, addr_len, max_prefix, max_content, max_suffix ); |
279 | packet->magic_value = PACKET_MAGIC_VALUE; |
285 | packet->magic_value = PACKET_MAGIC_VALUE; |
280 | if( ERROR_OCCURRED( pm_add( packet ))){ |
286 | if( ERROR_OCCURRED( pm_add( packet ))){ |
281 | munmap( packet, packet->length ); |
287 | munmap( packet, packet->length ); |
282 | return NULL; |
288 | return NULL; |
283 | } |
289 | } |
284 | return packet; |
290 | return packet; |
285 | } |
291 | } |
286 | 292 | ||
287 | void packet_init( packet_t packet, size_t addr_len, size_t max_prefix, size_t max_content, size_t max_suffix ){ |
293 | void packet_init( packet_t packet, size_t addr_len, size_t max_prefix, size_t max_content, size_t max_suffix ){ |
288 | // clear the packet content |
294 | // clear the packet content |
289 | bzero((( void * ) packet ) + sizeof( struct packet ), packet->length - sizeof( struct packet )); |
295 | bzero((( void * ) packet ) + sizeof( struct packet ), packet->length - sizeof( struct packet )); |
290 | // clear the packet header |
296 | // clear the packet header |
291 | packet->order = 0; |
297 | packet->order = 0; |
292 | packet->metric = 0; |
298 | packet->metric = 0; |
293 | packet->previous = 0; |
299 | packet->previous = 0; |
294 | packet->next = 0; |
300 | packet->next = 0; |
295 | packet->addr_len = 0; |
301 | packet->addr_len = 0; |
296 | packet->src_addr = sizeof( struct packet ); |
302 | packet->src_addr = sizeof( struct packet ); |
297 | packet->dest_addr = packet->src_addr + addr_len; |
303 | packet->dest_addr = packet->src_addr + addr_len; |
298 | packet->max_prefix = max_prefix; |
304 | packet->max_prefix = max_prefix; |
299 | packet->max_content = max_content; |
305 | packet->max_content = max_content; |
300 | packet->data_start = packet->dest_addr + addr_len + packet->max_prefix; |
306 | packet->data_start = packet->dest_addr + addr_len + packet->max_prefix; |
301 | packet->data_end = packet->data_start; |
307 | packet->data_end = packet->data_start; |
302 | } |
308 | } |
303 | 309 | ||
304 | int packet_reply( const packet_t packet ){ |
310 | int packet_reply( const packet_t packet ){ |
305 | ipc_callid_t callid; |
311 | ipc_callid_t callid; |
306 | size_t size; |
312 | size_t size; |
307 | 313 | ||
308 | if( ! packet_is_valid( packet )) return EINVAL; |
314 | if( ! packet_is_valid( packet )) return EINVAL; |
309 | if( ipc_share_in_receive( & callid, & size ) <= 0 ) return EINVAL; |
315 | if( ipc_share_in_receive( & callid, & size ) <= 0 ) return EINVAL; |
310 | if( size != packet->length ) return ENOMEM; |
316 | if( size != packet->length ) return ENOMEM; |
311 | return ipc_share_in_finalize( callid, packet, PROTO_READ | PROTO_WRITE ); |
317 | return ipc_share_in_finalize( callid, packet, PROTO_READ | PROTO_WRITE ); |
312 | } |
318 | } |
313 | 319 | ||
314 | /** @} |
320 | /** @} |
315 | */ |
321 | */ |
316 | 322 |