Subversion Repositories HelenOS

Rev

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

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