Subversion Repositories HelenOS

Rev

Rev 4243 | Rev 4351 | Go to most recent revision | Only display areas with differences | Ignore whitespace | Details | Blame | Last modification | View Log | RSS feed

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