Rev 3990 | Rev 4243 | Go to most recent revision | Only display areas with differences | Ignore whitespace | Details | Blame | Last modification | View Log | RSS feed
Rev 3990 | Rev 4192 | ||
---|---|---|---|
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 <async.h> |
38 | #include <async.h> |
39 | #include <errno.h> |
39 | #include <errno.h> |
40 | #include <futex.h> |
40 | #include <futex.h> |
41 | //#include <stdio.h> |
41 | //#include <stdio.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_header.h" |
51 | #include "packet_header.h" |
52 | #include "packet_server.h" |
52 | #include "packet_server.h" |
53 | 53 | ||
- | 54 | /** The default address length reserved for new packets. |
|
- | 55 | */ |
|
- | 56 | #define DEFAULT_ADDR_LEN 32 |
|
- | 57 | ||
- | 58 | /** The default prefix reserved for new packets. |
|
- | 59 | */ |
|
- | 60 | #define DEFAULT_PREFIX 0 |
|
- | 61 | ||
- | 62 | /** The default suffix reserved for new packets. |
|
- | 63 | */ |
|
- | 64 | #define DEFAULT_SUFFIX 0 |
|
- | 65 | ||
54 | /** Returns the packet identifier message parameter. |
66 | /** Returns the packet identifier message parameter. |
55 | */ |
67 | */ |
56 | #define IPC_GET_ID( call ) ( packet_id_t ) IPC_GET_ARG1( * call ) |
68 | #define IPC_GET_ID( call ) ( packet_id_t ) IPC_GET_ARG1( * call ) |
57 | 69 | ||
58 | /** Returns the maximal content length message parameter. |
70 | /** Returns the maximal content length message parameter. |
59 | */ |
71 | */ |
60 | #define IPC_GET_CONTENT( call ) ( size_t ) IPC_GET_ARG1( * call ) |
72 | #define IPC_GET_CONTENT( call ) ( size_t ) IPC_GET_ARG1( * call ) |
61 | 73 | ||
62 | /** Returns the maximal address length message parameter. |
74 | /** Returns the maximal address length message parameter. |
63 | */ |
75 | */ |
64 | #define IPC_GET_ADDR_LEN( call ) ( size_t ) IPC_GET_ARG2( * call ) |
76 | #define IPC_GET_ADDR_LEN( call ) ( size_t ) IPC_GET_ARG2( * call ) |
65 | 77 | ||
66 | /** Returns the maximal prefix length message parameter. |
78 | /** Returns the maximal prefix length message parameter. |
67 | */ |
79 | */ |
68 | #define IPC_GET_PREFIX( call ) ( size_t ) IPC_GET_ARG3( * call ) |
80 | #define IPC_GET_PREFIX( call ) ( size_t ) IPC_GET_ARG3( * call ) |
69 | 81 | ||
70 | /** Returns the maximal suffix length message parameter. |
82 | /** Returns the maximal suffix length message parameter. |
71 | */ |
83 | */ |
72 | #define IPC_GET_SUFIX( call ) ( size_t ) IPC_GET_ARG4( * call ) |
84 | #define IPC_GET_SUFFIX( call ) ( size_t ) IPC_GET_ARG4( * call ) |
73 | 85 | ||
74 | #define FREE_QUEUES_COUNT 7 |
86 | #define FREE_QUEUES_COUNT 7 |
75 | 87 | ||
76 | /** Packet server global data. |
88 | /** Packet server global data. |
77 | */ |
89 | */ |
78 | static struct{ |
90 | static struct{ |
79 | /** Safety lock. |
91 | /** Safety lock. |
80 | */ |
92 | */ |
81 | futex_t lock; |
93 | futex_t lock; |
82 | /** Free packet queues. |
94 | /** Free packet queues. |
83 | */ |
95 | */ |
84 | packet_t free[ FREE_QUEUES_COUNT ]; |
96 | packet_t free[ FREE_QUEUES_COUNT ]; |
85 | /** Packet length upper bounds of the free packet queues. |
97 | /** Packet length upper bounds of the free packet queues. |
86 | * The maximal lengths of packets in each queue in the ascending order. |
98 | * The maximal lengths of packets in each queue in the ascending order. |
87 | * The last queue is not limited. |
99 | * The last queue is not limited. |
88 | */ |
100 | */ |
89 | int sizes[ FREE_QUEUES_COUNT ]; |
101 | int sizes[ FREE_QUEUES_COUNT ]; |
90 | /** Total packets allocated. |
102 | /** Total packets allocated. |
91 | */ |
103 | */ |
92 | unsigned int count; |
104 | unsigned int count; |
93 | } ps_globals = { |
105 | } ps_globals = { |
94 | { 1 }, |
106 | { 1 }, |
95 | { NULL, NULL, NULL, NULL, NULL, NULL, NULL }, |
107 | { NULL, NULL, NULL, NULL, NULL, NULL, NULL }, |
96 | { PAGE_SIZE, PAGE_SIZE * 2, PAGE_SIZE * 4, PAGE_SIZE * 8, PAGE_SIZE * 16, PAGE_SIZE * 32, PAGE_SIZE * 64 }, |
108 | { PAGE_SIZE, PAGE_SIZE * 2, PAGE_SIZE * 4, PAGE_SIZE * 8, PAGE_SIZE * 16, PAGE_SIZE * 32, PAGE_SIZE * 64 }, |
97 | 0 |
109 | 0 |
98 | }; |
110 | }; |
99 | 111 | ||
100 | /** Releases the packet and returns it to the appropriate free packet queue. |
112 | /** Releases the packet and returns it to the appropriate free packet queue. |
101 | * Should be used only when the global data are locked. |
113 | * Should be used only when the global data are locked. |
102 | * @param packet The packet to be released. Input parameter. |
114 | * @param packet The packet to be released. Input parameter. |
103 | */ |
115 | */ |
104 | void packet_release( packet_t packet ); |
116 | void packet_release( packet_t packet ); |
105 | 117 | ||
106 | /** Returns the packet of dimensions at least as given. |
118 | /** Returns the packet of dimensions at least as given. |
107 | * Tries to reuse free packets first. |
119 | * Tries to reuse free packets first. |
108 | * Creates a new packet aligned to the memory page size if none available. |
120 | * Creates a new packet aligned to the memory page size if none available. |
109 | * Locks the global data during its processing. |
121 | * Locks the global data during its processing. |
110 | * @param addr_len The source and destination addresses maximal length in bytes. Input parameter. |
122 | * @param addr_len The source and destination addresses maximal length in bytes. Input parameter. |
111 | * @param max_prefix The maximal prefix length in bytes. Input parameter. |
123 | * @param max_prefix The maximal prefix length in bytes. Input parameter. |
112 | * @param max_content The maximal content length in bytes. Input parameter. |
124 | * @param max_content The maximal content length in bytes. Input parameter. |
113 | * @param max_suffix The maximal suffix length in bytes. Input parameter. |
125 | * @param max_suffix The maximal suffix length in bytes. Input parameter. |
114 | * @returns The packet of dimensions at least as given. |
126 | * @returns The packet of dimensions at least as given. |
115 | * @returns NULL if there is not enough memory left. |
127 | * @returns NULL if there is not enough memory left. |
116 | */ |
128 | */ |
117 | packet_t packet_get( size_t addr_len, size_t max_prefix, size_t max_content, size_t max_suffix ); |
129 | packet_t packet_get( size_t addr_len, size_t max_prefix, size_t max_content, size_t max_suffix ); |
118 | 130 | ||
119 | /** Creates a new packet of dimensions at least as given. |
131 | /** Creates a new packet of dimensions at least as given. |
120 | * Should be used only when the global data are locked. |
132 | * Should be used only when the global data are locked. |
121 | * @param length The total length of the packet, including the header, the addresses and the data of the packet. Input parameter. |
133 | * @param length The total length of the packet, including the header, the addresses and the data of the packet. Input parameter. |
122 | * @param addr_len The source and destination addresses maximal length in bytes. Input parameter. |
134 | * @param addr_len The source and destination addresses maximal length in bytes. Input parameter. |
123 | * @param max_prefix The maximal prefix length in bytes. Input parameter. |
135 | * @param max_prefix The maximal prefix length in bytes. Input parameter. |
124 | * @param max_content The maximal content length in bytes. Input parameter. |
136 | * @param max_content The maximal content length in bytes. Input parameter. |
125 | * @param max_suffix The maximal suffix length in bytes. Input parameter. |
137 | * @param max_suffix The maximal suffix length in bytes. Input parameter. |
126 | * @returns The packet of dimensions at least as given. |
138 | * @returns The packet of dimensions at least as given. |
127 | * @returns NULL if there is not enough memory left. |
139 | * @returns NULL if there is not enough memory left. |
128 | */ |
140 | */ |
129 | packet_t packet_create( size_t length, size_t addr_len, size_t max_prefix, size_t max_content, size_t max_suffix ); |
141 | packet_t packet_create( size_t length, size_t addr_len, size_t max_prefix, size_t max_content, size_t max_suffix ); |
130 | 142 | ||
131 | /** Initializes the packet according to the given dimensions. |
143 | /** Initializes the packet according to the given dimensions. |
132 | * @param packet The packet to be initialized. Input parameter. |
144 | * @param packet The packet to be initialized. Input parameter. |
133 | * @param addr_len The source and destination addresses maximal length in bytes. Input parameter. |
145 | * @param addr_len The source and destination addresses maximal length in bytes. Input parameter. |
134 | * @param max_prefix The maximal prefix length in bytes. Input parameter. |
146 | * @param max_prefix The maximal prefix length in bytes. Input parameter. |
135 | * @param max_content The maximal content length in bytes. Input parameter. |
147 | * @param max_content The maximal content length in bytes. Input parameter. |
136 | * @param max_suffix The maximal suffix length in bytes. Input parameter. |
148 | * @param max_suffix The maximal suffix length in bytes. Input parameter. |
137 | */ |
149 | */ |
138 | void packet_init( packet_t packet, size_t addr_len, size_t max_prefix, size_t max_content, size_t max_suffix ); |
150 | void packet_init( packet_t packet, size_t addr_len, size_t max_prefix, size_t max_content, size_t max_suffix ); |
139 | 151 | ||
140 | /** Shares the packet memory block. |
152 | /** Shares the packet memory block. |
141 | * @param packet The packet to be shared. |
153 | * @param packet The packet to be shared. |
142 | * @returns EOK on success. |
154 | * @returns EOK on success. |
143 | * @returns EINVAL if the packet is not valid. |
155 | * @returns EINVAL if the packet is not valid. |
144 | * @returns EINVAL if the calling module does not accept the memory. |
156 | * @returns EINVAL if the calling module does not accept the memory. |
145 | * @returns ENOMEM if the desired and actual sizes differ. |
157 | * @returns ENOMEM if the desired and actual sizes differ. |
146 | * @returns Other error codes as defined for the ipc_share_in_finalize() function. |
158 | * @returns Other error codes as defined for the ipc_share_in_finalize() function. |
147 | */ |
159 | */ |
148 | int packet_reply( const packet_t packet ); |
160 | int packet_reply( const packet_t packet ); |
149 | 161 | ||
150 | int packet_server_message( ipc_callid_t callid, ipc_call_t * call, ipc_call_t * answer, int * answer_count ){ |
162 | int packet_server_message( ipc_callid_t callid, ipc_call_t * call, ipc_call_t * answer, int * answer_count ){ |
151 | packet_t packet; |
163 | packet_t packet; |
152 | 164 | ||
153 | * answer_count = 0; |
165 | * answer_count = 0; |
154 | switch( IPC_GET_METHOD( * call )){ |
166 | switch( IPC_GET_METHOD( * call )){ |
155 | case IPC_M_PHONE_HUNGUP: |
167 | case IPC_M_PHONE_HUNGUP: |
156 | return EOK; |
168 | return EOK; |
157 | case NET_PACKET_CREATE_1: |
169 | case NET_PACKET_CREATE_1: |
158 | packet = packet_get( 0, 0, IPC_GET_CONTENT( call ), 0 ); |
170 | packet = packet_get( DEFAULT_ADDR_LEN, DEFAULT_PREFIX, IPC_GET_CONTENT( call ), DEFAULT_SUFFIX ); |
159 | if( ! packet ) return ENOMEM; |
171 | if( ! packet ) return ENOMEM; |
160 | * answer_count = 2; |
172 | * answer_count = 2; |
161 | IPC_SET_ARG1( * answer, packet->packet_id ); |
173 | IPC_SET_ARG1( * answer, packet->packet_id ); |
162 | IPC_SET_ARG2( * answer, packet->length ); |
174 | IPC_SET_ARG2( * answer, packet->length ); |
163 | return EOK; |
175 | return EOK; |
164 | case NET_PACKET_CREATE_4: |
176 | case NET_PACKET_CREATE_4: |
165 | packet = packet_get( IPC_GET_ADDR_LEN( call ), IPC_GET_PREFIX( call ), IPC_GET_CONTENT( call ), IPC_GET_SUFIX( call )); |
177 | packet = packet_get( IPC_GET_ADDR_LEN( call ), IPC_GET_PREFIX( call ), IPC_GET_CONTENT( call ), IPC_GET_SUFFIX( call )); |
166 | if( ! packet ) return ENOMEM; |
178 | if( ! packet ) return ENOMEM; |
167 | * answer_count = 2; |
179 | * answer_count = 2; |
168 | IPC_SET_ARG1( * answer, packet->packet_id ); |
180 | IPC_SET_ARG1( * answer, packet->packet_id ); |
169 | IPC_SET_ARG2( * answer, packet->length ); |
181 | IPC_SET_ARG2( * answer, packet->length ); |
170 | return EOK; |
182 | return EOK; |
171 | case NET_PACKET_GET: |
183 | case NET_PACKET_GET: |
172 | packet = pm_find( IPC_GET_ID( call )); |
184 | packet = pm_find( IPC_GET_ID( call )); |
173 | if( ! packet_is_valid( packet )) return ENOENT; |
185 | if( ! packet_is_valid( packet )) return ENOENT; |
174 | return packet_reply( packet ); |
186 | return packet_reply( packet ); |
175 | case NET_PACKET_GET_SIZE: |
187 | case NET_PACKET_GET_SIZE: |
176 | packet = pm_find( IPC_GET_ID( call )); |
188 | packet = pm_find( IPC_GET_ID( call )); |
177 | if( ! packet_is_valid( packet )) return ENOENT; |
189 | if( ! packet_is_valid( packet )) return ENOENT; |
178 | * answer_count = 1; |
190 | * answer_count = 1; |
179 | IPC_SET_ARG1( * answer, packet->length ); |
191 | IPC_SET_ARG1( * answer, packet->length ); |
180 | return EOK; |
192 | return EOK; |
181 | case NET_PACKET_RELEASE: |
193 | case NET_PACKET_RELEASE: |
182 | packet = pm_find( IPC_GET_ID( call )); |
194 | packet = pm_find( IPC_GET_ID( call )); |
183 | if( ! packet_is_valid( packet )) return ENOENT; |
195 | if( ! packet_is_valid( packet )) return ENOENT; |
184 | futex_down( & ps_globals.lock ); |
196 | futex_down( & ps_globals.lock ); |
185 | pq_destroy( packet, packet_release ); |
197 | pq_destroy( packet, packet_release ); |
186 | futex_up( & ps_globals.lock ); |
198 | futex_up( & ps_globals.lock ); |
187 | return EOK; |
199 | return EOK; |
188 | } |
200 | } |
189 | return ENOTSUP; |
201 | return ENOTSUP; |
190 | } |
202 | } |
191 | 203 | ||
192 | void packet_release( packet_t packet ){ |
204 | void packet_release( packet_t packet ){ |
193 | int index; |
205 | int index; |
194 | 206 | ||
195 | for( index = 0; ( index < FREE_QUEUES_COUNT - 1 ) && ( packet->length > ps_globals.sizes[ index ] ); ++ index ); |
207 | for( index = 0; ( index < FREE_QUEUES_COUNT - 1 ) && ( packet->length > ps_globals.sizes[ index ] ); ++ index ); |
196 | ps_globals.free[ index ] = pq_add( ps_globals.free[ index ], packet, packet->length, packet->length ); |
208 | ps_globals.free[ index ] = pq_add( ps_globals.free[ index ], packet, packet->length, packet->length ); |
197 | } |
209 | } |
198 | 210 | ||
199 | packet_t packet_get( size_t addr_len, size_t max_prefix, size_t max_content, size_t max_suffix ){ |
211 | packet_t packet_get( size_t addr_len, size_t max_prefix, size_t max_content, size_t max_suffix ){ |
200 | int index; |
212 | int index; |
201 | packet_t packet; |
213 | packet_t packet; |
202 | size_t length; |
214 | size_t length; |
203 | 215 | ||
204 | length = ALIGN_UP( sizeof( struct packet ) + 2 * addr_len + max_prefix + max_content + max_suffix, PAGE_SIZE ); |
216 | length = ALIGN_UP( sizeof( struct packet ) + 2 * addr_len + max_prefix + max_content + max_suffix, PAGE_SIZE ); |
205 | futex_down( & ps_globals.lock ); |
217 | futex_down( & ps_globals.lock ); |
206 | for( index = 0; index < FREE_QUEUES_COUNT - 1; ++ index ){ |
218 | for( index = 0; index < FREE_QUEUES_COUNT - 1; ++ index ){ |
207 | if( length <= ps_globals.sizes[ index ] ){ |
219 | if( length <= ps_globals.sizes[ index ] ){ |
208 | packet = ps_globals.free[ index ]; |
220 | packet = ps_globals.free[ index ]; |
209 | while( packet_is_valid( packet ) && ( packet->length < length )){ |
221 | while( packet_is_valid( packet ) && ( packet->length < length )){ |
210 | packet = pm_find( packet->next ); |
222 | packet = pm_find( packet->next ); |
211 | } |
223 | } |
212 | if( packet ){ |
224 | if( packet ){ |
213 | packet_init( packet, addr_len, max_prefix, max_content, max_suffix ); |
225 | packet_init( packet, addr_len, max_prefix, max_content, max_suffix ); |
214 | futex_up( & ps_globals.lock ); |
226 | futex_up( & ps_globals.lock ); |
215 | return packet; |
227 | return packet; |
216 | } |
228 | } |
217 | } |
229 | } |
218 | } |
230 | } |
219 | packet = packet_create( length, addr_len, max_prefix, max_content, max_suffix ); |
231 | packet = packet_create( length, addr_len, max_prefix, max_content, max_suffix ); |
220 | futex_up( & ps_globals.lock ); |
232 | futex_up( & ps_globals.lock ); |
221 | return packet; |
233 | return packet; |
222 | } |
234 | } |
223 | 235 | ||
224 | packet_t packet_create( size_t length, size_t addr_len, size_t max_prefix, size_t max_content, size_t max_suffix ){ |
236 | packet_t packet_create( size_t length, size_t addr_len, size_t max_prefix, size_t max_content, size_t max_suffix ){ |
225 | ERROR_DECLARE; |
237 | ERROR_DECLARE; |
226 | 238 | ||
227 | packet_t packet; |
239 | packet_t packet; |
228 | 240 | ||
229 | // already locked |
241 | // already locked |
230 | packet = ( packet_t ) mmap( NULL, length, PROTO_READ | PROTO_WRITE, MAP_SHARED | MAP_ANONYMOUS, 0, 0 ); |
242 | packet = ( packet_t ) mmap( NULL, length, PROTO_READ | PROTO_WRITE, MAP_SHARED | MAP_ANONYMOUS, 0, 0 ); |
231 | if( packet == MAP_FAILED ) return NULL; |
243 | if( packet == MAP_FAILED ) return NULL; |
232 | ++ ps_globals.count; |
244 | ++ ps_globals.count; |
233 | packet->packet_id = ps_globals.count; |
245 | packet->packet_id = ps_globals.count; |
234 | packet->length = length; |
246 | packet->length = length; |
235 | packet_init( packet, addr_len, max_prefix, max_content, max_suffix ); |
247 | packet_init( packet, addr_len, max_prefix, max_content, max_suffix ); |
236 | packet->magic_value = PACKET_MAGIC_VALUE; |
248 | packet->magic_value = PACKET_MAGIC_VALUE; |
237 | if( ERROR_OCCURRED( pm_add( packet ))){ |
249 | if( ERROR_OCCURRED( pm_add( packet ))){ |
238 | munmap( packet, packet->length ); |
250 | munmap( packet, packet->length ); |
239 | return NULL; |
251 | return NULL; |
240 | } |
252 | } |
241 | packet_release( packet ); |
253 | packet_release( packet ); |
242 | return packet; |
254 | return packet; |
243 | } |
255 | } |
244 | 256 | ||
245 | void packet_init( packet_t packet, size_t addr_len, size_t max_prefix, size_t max_content, size_t max_suffix ){ |
257 | void packet_init( packet_t packet, size_t addr_len, size_t max_prefix, size_t max_content, size_t max_suffix ){ |
246 | packet->order = 0; |
258 | packet->order = 0; |
247 | packet->metric = 0; |
259 | packet->metric = 0; |
248 | packet->previous = 0; |
260 | packet->previous = 0; |
249 | packet->next = 0; |
261 | packet->next = 0; |
250 | packet->addr_len = addr_len; |
262 | packet->addr_len = addr_len; |
251 | packet->src_addr = sizeof( struct packet ); |
263 | packet->src_addr = sizeof( struct packet ); |
252 | packet->dest_addr = packet->src_addr + packet->addr_len; |
264 | packet->dest_addr = packet->src_addr + packet->addr_len; |
253 | packet->max_prefix = max_prefix; |
265 | packet->max_prefix = max_prefix; |
254 | packet->max_content = max_content; |
266 | packet->max_content = max_content; |
255 | packet->data_start = packet->dest_addr + packet->addr_len + packet->max_prefix; |
267 | packet->data_start = packet->dest_addr + packet->addr_len + packet->max_prefix; |
256 | packet->data_end = packet->data_start; |
268 | packet->data_end = packet->data_start; |
257 | } |
269 | } |
258 | 270 | ||
259 | int packet_reply( const packet_t packet ){ |
271 | int packet_reply( const packet_t packet ){ |
260 | ipc_callid_t callid; |
272 | ipc_callid_t callid; |
261 | size_t size; |
273 | size_t size; |
262 | 274 | ||
263 | if( ! packet_is_valid( packet )) return EINVAL; |
275 | if( ! packet_is_valid( packet )) return EINVAL; |
264 | if( ipc_share_in_receive( & callid, & size ) <= 0 ) return EINVAL; |
276 | if( ipc_share_in_receive( & callid, & size ) <= 0 ) return EINVAL; |
265 | if( size != packet->length ) return ENOMEM; |
277 | if( size != packet->length ) return ENOMEM; |
266 | return ipc_share_in_finalize( callid, packet, PROTO_READ | PROTO_WRITE ); |
278 | return ipc_share_in_finalize( callid, packet, PROTO_READ | PROTO_WRITE ); |
267 | } |
279 | } |
268 | 280 | ||
269 | /** @} |
281 | /** @} |
270 | */ |
282 | */ |
271 | 283 |