Subversion Repositories HelenOS

Rev

Rev 3914 | Rev 4192 | Go to most recent revision | Show entire file | Ignore whitespace | Details | Blame | Last modification | View Log | RSS feed

Rev 3914 Rev 3990
Line 40... Line 40...
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 <ipc/services.h>
-
 
46
#include <sys/mman.h>
45
#include <sys/mman.h>
47
 
46
 
48
#include "../../err.h"
47
#include "../../err.h"
49
#include "../../messages.h"
48
#include "../../messages.h"
50
 
49
 
Line 54... Line 53...
54
 
53
 
55
/** Returns the packet identifier message parameter.
54
/** Returns the packet identifier message parameter.
56
 */
55
 */
57
#define IPC_GET_ID( call )          ( packet_id_t ) IPC_GET_ARG1( * call )
56
#define IPC_GET_ID( call )          ( packet_id_t ) IPC_GET_ARG1( * call )
58
 
57
 
59
/** Returns the owner message parameter.
-
 
60
 */
-
 
61
#define IPC_GET_OWNER( call )       ( services_t ) IPC_GET_ARG1( * call )
-
 
62
 
-
 
63
/** Returns the maximal content length message parameter.
58
/** Returns the maximal content length message parameter.
64
 */
59
 */
65
#define IPC_GET_CONTENT( call )     ( size_t ) IPC_GET_ARG2( * call )
60
#define IPC_GET_CONTENT( call )     ( size_t ) IPC_GET_ARG1( * call )
66
 
61
 
67
/** Returns the maximal address length message parameter.
62
/** Returns the maximal address length message parameter.
68
 */
63
 */
69
#define IPC_GET_ADDR_LEN( call )    ( size_t ) IPC_GET_ARG3( * call )
64
#define IPC_GET_ADDR_LEN( call )    ( size_t ) IPC_GET_ARG2( * call )
70
 
65
 
71
/** Returns the maximal prefix length message parameter.
66
/** Returns the maximal prefix length message parameter.
72
 */
67
 */
73
#define IPC_GET_PREFIX( call )      ( size_t ) IPC_GET_ARG4( * call )
68
#define IPC_GET_PREFIX( call )      ( size_t ) IPC_GET_ARG3( * call )
74
 
69
 
75
/** Returns the maximal suffix length message parameter.
70
/** Returns the maximal suffix length message parameter.
76
 */
71
 */
77
#define IPC_GET_SUFIX( call )       ( size_t ) IPC_GET_ARG5( * call )
72
#define IPC_GET_SUFIX( call )       ( size_t ) IPC_GET_ARG4( * call )
78
 
73
 
79
#define FREE_QUEUES_COUNT   7
74
#define FREE_QUEUES_COUNT   7
80
 
75
 
81
/** Packet server global data.
76
/** Packet server global data.
82
 */
77
 */
Line 110... Line 105...
110
 
105
 
111
/** Returns the packet of dimensions at least as given.
106
/** Returns the packet of dimensions at least as given.
112
 *  Tries to reuse free packets first.
107
 *  Tries to reuse free packets first.
113
 *  Creates a&nbsp;new packet aligned to the memory page size if none available.
108
 *  Creates a&nbsp;new packet aligned to the memory page size if none available.
114
 *  Locks the global data during its processing.
109
 *  Locks the global data during its processing.
115
 *  @param owner The new owner of the packet. Input parameter.
-
 
116
 *  @param addr_len The source and destination addresses maximal length in bytes. Input parameter.
110
 *  @param addr_len The source and destination addresses maximal length in bytes. Input parameter.
117
 *  @param max_prefix The maximal prefix length in bytes. Input parameter.
111
 *  @param max_prefix The maximal prefix length in bytes. Input parameter.
118
 *  @param max_content The maximal content length in bytes. Input parameter.
112
 *  @param max_content The maximal content length in bytes. Input parameter.
119
 *  @param max_suffix The maximal suffix length in bytes. Input parameter.
113
 *  @param max_suffix The maximal suffix length in bytes. Input parameter.
120
 *  @returns The packet of dimensions at least as given.
114
 *  @returns The packet of dimensions at least as given.
121
 *  @returns NULL if there is not enough memory left.
115
 *  @returns NULL if there is not enough memory left.
122
 */
116
 */
123
packet_t packet_get( services_t owner, size_t addr_len, size_t max_prefix, size_t max_content, size_t max_suffix );
117
packet_t packet_get( size_t addr_len, size_t max_prefix, size_t max_content, size_t max_suffix );
124
 
118
 
125
/** Creates a&nbsp;new packet of dimensions at least as given.
119
/** Creates a&nbsp;new packet of dimensions at least as given.
126
 *  Should be used only when the global data are locked.
120
 *  Should be used only when the global data are locked.
127
 *  @param length The total length of the packet, including the header, the addresses and the data of the packet. Input parameter.
121
 *  @param length The total length of the packet, including the header, the addresses and the data of the packet. Input parameter.
128
 *  @param owner The new owner of the packet. Input parameter.
-
 
129
 *  @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.
130
 *  @param max_prefix The maximal prefix length in bytes. Input parameter.
123
 *  @param max_prefix The maximal prefix length in bytes. Input parameter.
131
 *  @param max_content The maximal content length in bytes. Input parameter.
124
 *  @param max_content The maximal content length in bytes. Input parameter.
132
 *  @param max_suffix The maximal suffix length in bytes. Input parameter.
125
 *  @param max_suffix The maximal suffix length in bytes. Input parameter.
133
 *  @returns The packet of dimensions at least as given.
126
 *  @returns The packet of dimensions at least as given.
134
 *  @returns NULL if there is not enough memory left.
127
 *  @returns NULL if there is not enough memory left.
135
 */
128
 */
136
packet_t    packet_create( size_t length, services_t owner, size_t addr_len, size_t max_prefix, size_t max_content, size_t max_suffix );
129
packet_t    packet_create( size_t length, size_t addr_len, size_t max_prefix, size_t max_content, size_t max_suffix );
137
 
130
 
138
/** Initializes the packet according to the given dimensions.
131
/** Initializes the packet according to the given dimensions.
139
 *  @param packet The packet to be initialized. Input parameter.
132
 *  @param packet The packet to be initialized. Input parameter.
140
 *  @param owner The new owner of the packet. Input parameter.
-
 
141
 *  @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.
142
 *  @param max_prefix The maximal prefix length in bytes. Input parameter.
134
 *  @param max_prefix The maximal prefix length in bytes. Input parameter.
143
 *  @param max_content The maximal content length in bytes. Input parameter.
135
 *  @param max_content The maximal content length in bytes. Input parameter.
144
 *  @param max_suffix The maximal suffix length in bytes. Input parameter.
136
 *  @param max_suffix The maximal suffix length in bytes. Input parameter.
145
 */
137
 */
146
void    packet_init( packet_t packet, services_t owner, size_t addr_len, size_t max_prefix, size_t max_content, size_t max_suffix );
138
void    packet_init( packet_t packet, size_t addr_len, size_t max_prefix, size_t max_content, size_t max_suffix );
147
 
139
 
148
/** Shares the packet memory block.
140
/** Shares the packet memory block.
149
 *  @param packet The packet to be shared.
141
 *  @param packet The packet to be shared.
150
 *  @returns EOK on success.
142
 *  @returns EOK on success.
151
 *  @returns EINVAL if the packet is not valid.
143
 *  @returns EINVAL if the packet is not valid.
Line 161... Line 153...
161
    * answer_count = 0;
153
    * answer_count = 0;
162
    switch( IPC_GET_METHOD( * call )){
154
    switch( IPC_GET_METHOD( * call )){
163
        case IPC_M_PHONE_HUNGUP:
155
        case IPC_M_PHONE_HUNGUP:
164
            return EOK;
156
            return EOK;
165
        case NET_PACKET_CREATE_1:
157
        case NET_PACKET_CREATE_1:
166
            packet = packet_get( IPC_GET_OWNER( call ), 0, 0, IPC_GET_CONTENT( call ), 0 );
158
            packet = packet_get( 0, 0, IPC_GET_CONTENT( call ), 0 );
167
            if( ! packet ) return ENOMEM;
159
            if( ! packet ) return ENOMEM;
168
            * answer_count = 2;
160
            * answer_count = 2;
169
            IPC_SET_ARG1( * answer, packet->packet_id );
161
            IPC_SET_ARG1( * answer, packet->packet_id );
170
            IPC_SET_ARG2( * answer, packet->length );
162
            IPC_SET_ARG2( * answer, packet->length );
171
            return EOK;
163
            return EOK;
172
        case NET_PACKET_CREATE_5:
164
        case NET_PACKET_CREATE_4:
173
            packet = packet_get( IPC_GET_OWNER( call ), IPC_GET_ADDR_LEN( call ), IPC_GET_PREFIX( call ), IPC_GET_CONTENT( call ), IPC_GET_SUFIX( call ));
165
            packet = packet_get( IPC_GET_ADDR_LEN( call ), IPC_GET_PREFIX( call ), IPC_GET_CONTENT( call ), IPC_GET_SUFIX( call ));
174
            if( ! packet ) return ENOMEM;
166
            if( ! packet ) return ENOMEM;
175
            * answer_count = 2;
167
            * answer_count = 2;
176
            IPC_SET_ARG1( * answer, packet->packet_id );
168
            IPC_SET_ARG1( * answer, packet->packet_id );
177
            IPC_SET_ARG2( * answer, packet->length );
169
            IPC_SET_ARG2( * answer, packet->length );
178
            return EOK;
170
            return EOK;
Line 202... Line 194...
202
 
194
 
203
    for( index = 0; ( index < FREE_QUEUES_COUNT - 1 ) && ( packet->length > ps_globals.sizes[ index ] ); ++ index );
195
    for( index = 0; ( index < FREE_QUEUES_COUNT - 1 ) && ( packet->length > ps_globals.sizes[ index ] ); ++ index );
204
    ps_globals.free[ index ] = pq_add( ps_globals.free[ index ], packet, packet->length, packet->length );
196
    ps_globals.free[ index ] = pq_add( ps_globals.free[ index ], packet, packet->length, packet->length );
205
}
197
}
206
 
198
 
207
packet_t packet_get( services_t owner, size_t addr_len, size_t max_prefix, size_t max_content, size_t max_suffix ){
199
packet_t packet_get( size_t addr_len, size_t max_prefix, size_t max_content, size_t max_suffix ){
208
    int index;
200
    int index;
209
    packet_t packet;
201
    packet_t packet;
210
    size_t length;
202
    size_t length;
211
 
203
 
212
    length = ALIGN_UP( sizeof( struct packet ) + 2 * addr_len + max_prefix + max_content + max_suffix, PAGE_SIZE );
204
    length = ALIGN_UP( sizeof( struct packet ) + 2 * addr_len + max_prefix + max_content + max_suffix, PAGE_SIZE );
Line 216... Line 208...
216
            packet = ps_globals.free[ index ];
208
            packet = ps_globals.free[ index ];
217
            while( packet_is_valid( packet ) && ( packet->length < length )){
209
            while( packet_is_valid( packet ) && ( packet->length < length )){
218
                packet = pm_find( packet->next );
210
                packet = pm_find( packet->next );
219
            }
211
            }
220
            if( packet ){
212
            if( packet ){
221
                packet_init( packet, owner, addr_len, max_prefix, max_content, max_suffix );
213
                packet_init( packet, addr_len, max_prefix, max_content, max_suffix );
222
                futex_up( & ps_globals.lock );
214
                futex_up( & ps_globals.lock );
223
                return packet;
215
                return packet;
224
            }
216
            }
225
        }
217
        }
226
    }
218
    }
227
    packet = packet_create( length, owner, addr_len, max_prefix, max_content, max_suffix );
219
    packet = packet_create( length, addr_len, max_prefix, max_content, max_suffix );
228
    futex_up( & ps_globals.lock );
220
    futex_up( & ps_globals.lock );
229
    return packet;
221
    return packet;
230
}
222
}
231
 
223
 
232
packet_t packet_create( size_t length, services_t owner, size_t addr_len, size_t max_prefix, size_t max_content, size_t max_suffix ){
224
packet_t packet_create( size_t length, size_t addr_len, size_t max_prefix, size_t max_content, size_t max_suffix ){
233
    ERROR_DECLARE;
225
    ERROR_DECLARE;
234
 
226
 
235
    packet_t    packet;
227
    packet_t    packet;
236
 
228
 
237
    // already locked
229
    // already locked
238
    packet = ( packet_t ) mmap( NULL, length, PROTO_READ | PROTO_WRITE, MAP_SHARED | MAP_ANONYMOUS, 0, 0 );
230
    packet = ( packet_t ) mmap( NULL, length, PROTO_READ | PROTO_WRITE, MAP_SHARED | MAP_ANONYMOUS, 0, 0 );
239
    if( packet == MAP_FAILED ) return NULL;
231
    if( packet == MAP_FAILED ) return NULL;
240
    ++ ps_globals.count;
232
    ++ ps_globals.count;
241
    packet->packet_id = ps_globals.count;
233
    packet->packet_id = ps_globals.count;
242
    packet->mode = PM_ONE_WAY;
-
 
243
    packet->length = length;
234
    packet->length = length;
244
    packet_init( packet, owner, addr_len, max_prefix, max_content, max_suffix );
235
    packet_init( packet, addr_len, max_prefix, max_content, max_suffix );
245
    packet->magic_value = PACKET_MAGIC_VALUE;
236
    packet->magic_value = PACKET_MAGIC_VALUE;
246
    if( ERROR_OCCURRED( pm_add( packet ))){
237
    if( ERROR_OCCURRED( pm_add( packet ))){
247
        munmap( packet, packet->length );
238
        munmap( packet, packet->length );
248
        return NULL;
239
        return NULL;
249
    }
240
    }
250
    packet_release( packet );
241
    packet_release( packet );
251
    return packet;
242
    return packet;
252
}
243
}
253
 
244
 
254
void packet_init( packet_t packet, services_t owner, size_t addr_len, size_t max_prefix, size_t max_content, size_t max_suffix ){
245
void packet_init( packet_t packet, size_t addr_len, size_t max_prefix, size_t max_content, size_t max_suffix ){
255
    packet->owner = owner;
-
 
256
    packet->order = 0;
246
    packet->order = 0;
257
    packet->metric = 0;
247
    packet->metric = 0;
258
    packet->previous = 0;
248
    packet->previous = 0;
259
    packet->next = 0;
249
    packet->next = 0;
260
    packet->addr_len = addr_len;
250
    packet->addr_len = addr_len;