Subversion Repositories HelenOS

Rev

Rev 4351 | Rev 4505 | Go to most recent revision | Details | Compare with Previous | Last modification | View Log | RSS feed

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