Subversion Repositories HelenOS

Compare Revisions

Ignore whitespace Rev 3913 → Rev 3914

/branches/network/uspace/srv/net/structures/packet/packet.c
36,6 → 36,7
*/
 
#include <errno.h>
#include <futex.h>
#include <malloc.h>
//#include <stdio.h>
#include <string.h>
87,10 → 88,13
/** Packet map global data.
*/
static struct{
// TODO lock
/** Safety lock.
* Used as a&nbsp;mutex.
*/
futex_t lock;
/** Packet map.
*/
gpm_t packet_map;
gpm_t packet_map;
} pm_globals;
 
GENERIC_FIELD_IMPLEMENT( gpm, packet_map_t );
101,17 → 105,34
}
 
int pm_init( void ){
return gpm_initialize( & pm_globals.packet_map );
ERROR_DECLARE;
 
// start locked
futex_initialize( & pm_globals.lock, 0 );
ERROR_PROPAGATE( gpm_initialize( & pm_globals.packet_map ));
// release the lock
futex_up( & pm_globals.lock );
return EOK;
}
 
packet_t pm_find( packet_id_t packet_id ){
packet_map_ref map;
packet_t packet;
 
if( ! packet_id ) return NULL;
if( packet_id > PACKET_MAP_SIZE * gpm_count( & pm_globals.packet_map )) return NULL;
futex_down( & pm_globals.lock );
if( packet_id > PACKET_MAP_SIZE * gpm_count( & pm_globals.packet_map )){
futex_up( & pm_globals.lock );
return NULL;
}
map = gpm_get_index( & pm_globals.packet_map, PACKET_MAP_PAGE( packet_id ));
if( ! map ) return NULL;
return ( * map )[ PACKET_MAP_INDEX( packet_id ) ];
if( ! map ){
futex_up( & pm_globals.lock );
return NULL;
}
packet = ( * map )[ PACKET_MAP_INDEX( packet_id ) ];
futex_up( & pm_globals.lock );
return packet;
}
 
int pm_add( packet_t packet ){
119,21 → 140,27
 
packet_map_ref map;
 
if(( ! packet_is_valid( packet )) || ( gpm_count( & pm_globals.packet_map ) < 0 )) return EINVAL;
if( ! packet_is_valid( packet )) return EINVAL;
futex_down( & pm_globals.lock );
if( PACKET_MAP_PAGE( packet->packet_id ) < gpm_count( & pm_globals.packet_map )){
map = gpm_get_index( & pm_globals.packet_map, PACKET_MAP_PAGE( packet->packet_id ));
}else{
do{
map = ( packet_map_ref ) malloc( sizeof( packet_map_t ));
if( ! map ) return ENOMEM;
if( ! map ){
futex_up( & pm_globals.lock );
return ENOMEM;
}
memset( map, 0, sizeof( packet_map_t ));
if(( ERROR_CODE = gpm_add( & pm_globals.packet_map, map )) < 0 ){
free( map );
futex_up( & pm_globals.lock );
return ERROR_CODE;
}
}while( PACKET_MAP_PAGE( packet->packet_id ) >= gpm_count( & pm_globals.packet_map ));
}
( * map )[ PACKET_MAP_INDEX( packet->packet_id ) ] = packet;
futex_up( & pm_globals.lock );
return EOK;
}
 
143,6 → 170,7
packet_map_ref map;
packet_t packet;
 
futex_down( & pm_globals.lock );
count = gpm_count( & pm_globals.packet_map );
while( count > 0 ){
map = gpm_get_index( & pm_globals.packet_map, count - 1 );
154,6 → 182,7
}
}
gpm_destroy( & pm_globals.packet_map );
// leave locked
}
 
packet_t pq_add( packet_t first, packet_t packet, int order, size_t metric ){
/branches/network/uspace/srv/net/structures/packet/packet_client.h
36,6 → 36,8
* To function correctly, initialization of the packet map by the pm_init() function has to happen at the first place.
* The module should not send the packet messages to the packet server but use the functions provided.
* The packet map should be released by the pm_destroy() function during the module termination.
* The packets and the packet queues can't be locked at all.
* The processing modules should process them sequentially -&nbsp;by passing the packets to the next module and stopping using the passed ones.
* @see packet.h
*/
 
/branches/network/uspace/srv/net/structures/packet/packet_server.c
34,9 → 34,10
* Packet server implementation.
*/
 
#include <align.h>
#include <async.h>
#include <align.h>
#include <errno.h>
#include <futex.h>
//#include <stdio.h>
#include <unistd.h>
 
80,6 → 81,9
/** Packet server global data.
*/
static struct{
/** Safety lock.
*/
futex_t lock;
/** Free packet queues.
*/
packet_t free[ FREE_QUEUES_COUNT ];
92,6 → 96,7
*/
unsigned int count;
} ps_globals = {
{ 1 },
{ NULL, NULL, NULL, NULL, NULL, NULL, NULL },
{ PAGE_SIZE, PAGE_SIZE * 2, PAGE_SIZE * 4, PAGE_SIZE * 8, PAGE_SIZE * 16, PAGE_SIZE * 32, PAGE_SIZE * 64 },
0
98,6 → 103,7
};
 
/** Releases the packet and returns it to the appropriate free packet queue.
* Should be used only when the global data are locked.
* @param packet The packet to be released. Input parameter.
*/
void packet_release( packet_t packet );
105,6 → 111,7
/** Returns the packet of dimensions at least as given.
* Tries to reuse free packets first.
* Creates a&nbsp;new packet aligned to the memory page size if none available.
* Locks the global data during its processing.
* @param owner The new owner of the packet. Input parameter.
* @param addr_len The source and destination addresses maximal length in bytes. Input parameter.
* @param max_prefix The maximal prefix length in bytes. Input parameter.
116,6 → 123,7
packet_t packet_get( services_t owner, size_t addr_len, size_t max_prefix, size_t max_content, size_t max_suffix );
 
/** Creates a&nbsp;new packet of dimensions at least as given.
* Should be used only when the global data are locked.
* @param length The total length of the packet, including the header, the addresses and the data of the packet. Input parameter.
* @param owner The new owner of the packet. Input parameter.
* @param addr_len The source and destination addresses maximal length in bytes. Input parameter.
181,7 → 189,9
case NET_PACKET_RELEASE:
packet = pm_find( IPC_GET_ID( call ));
if( ! packet_is_valid( packet )) return ENOENT;
futex_down( & ps_globals.lock );
pq_destroy( packet, packet_release );
futex_up( & ps_globals.lock );
return EOK;
}
return ENOTSUP;
200,6 → 210,7
size_t length;
 
length = ALIGN_UP( sizeof( struct packet ) + 2 * addr_len + max_prefix + max_content + max_suffix, PAGE_SIZE );
futex_down( & ps_globals.lock );
for( index = 0; index < FREE_QUEUES_COUNT - 1; ++ index ){
if( length <= ps_globals.sizes[ index ] ){
packet = ps_globals.free[ index ];
208,11 → 219,14
}
if( packet ){
packet_init( packet, owner, addr_len, max_prefix, max_content, max_suffix );
futex_up( & ps_globals.lock );
return packet;
}
}
}
return packet_create( length, owner, addr_len, max_prefix, max_content, max_suffix );
packet = packet_create( length, owner, addr_len, max_prefix, max_content, max_suffix );
futex_up( & ps_globals.lock );
return packet;
}
 
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 ){
220,6 → 234,7
 
packet_t packet;
 
// already locked
packet = ( packet_t ) mmap( NULL, length, PROTO_READ | PROTO_WRITE, MAP_SHARED | MAP_ANONYMOUS, 0, 0 );
if( packet == MAP_FAILED ) return NULL;
++ ps_globals.count;