/*
* Copyright (c) 2009 Lukas Mejdrech
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
*
* - Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* - Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
* - The name of the author may not be used to endorse or promote products
* derived from this software without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
* IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
* OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
* IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
* INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
* NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
* DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
* THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
* THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
/** @addtogroup packet
* @{
*/
/** @file
* Packet server implementation.
*/
#include <async.h>
#include <align.h>
#include <errno.h>
//#include <stdio.h>
#include <unistd.h>
#include <ipc/ipc.h>
#include <ipc/services.h>
#include <sys/mman.h>
#include "../../err.h"
#include "../../messages.h"
#include "packet.h"
#include "packet_header.h"
#include "packet_server.h"
/** Returns the packet identifier message parameter.
*/
#define IPC_GET_ID( call ) ( packet_id_t ) IPC_GET_ARG1( * call )
/** Returns the owner message parameter.
*/
#define IPC_GET_OWNER( call ) ( services_t ) IPC_GET_ARG1( * call )
/** Returns the maximal content length message parameter.
*/
#define IPC_GET_CONTENT( call ) ( size_t ) IPC_GET_ARG2( * call )
/** Returns the maximal address length message parameter.
*/
#define IPC_GET_ADDR_LEN( call ) ( size_t ) IPC_GET_ARG3( * call )
/** Returns the maximal prefix length message parameter.
*/
#define IPC_GET_PREFIX( call ) ( size_t ) IPC_GET_ARG4( * call )
/** Returns the maximal suffix length message parameter.
*/
#define IPC_GET_SUFIX( call ) ( size_t ) IPC_GET_ARG5( * call )
#define FREE_QUEUES_COUNT 7
/** Packet server global data.
*/
static struct{
/** Free packet queues.
*/
packet_t
free[ FREE_QUEUES_COUNT
];
/** Packet length upper bounds of the free packet queues.
* The maximal lengths of packets in each queue in the ascending order.
* The last queue is not limited.
*/
int sizes[ FREE_QUEUES_COUNT ];
/** Total packets allocated.
*/
unsigned int count;
} ps_globals = {
{ 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
};
/** Releases the packet and returns it to the appropriate free packet queue.
* @param packet The packet to be released. Input parameter.
*/
void packet_release( packet_t packet );
/** Returns the packet of dimensions at least as given.
* Tries to reuse free packets first.
* Creates a new packet aligned to the memory page size if none available.
* @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.
* @param max_content The maximal content length in bytes. Input parameter.
* @param max_suffix The maximal suffix length in bytes. Input parameter.
* @returns The packet of dimensions at least as given.
* @returns NULL if there is not enough memory left.
*/
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 new packet of dimensions at least as given.
* @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.
* @param max_prefix The maximal prefix length in bytes. Input parameter.
* @param max_content The maximal content length in bytes. Input parameter.
* @param max_suffix The maximal suffix length in bytes. Input parameter.
* @returns The packet of dimensions at least as given.
* @returns NULL if there is not enough memory left.
*/
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 );
/** Initializes the packet according to the given dimensions.
* @param packet The packet to be initialized. 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.
* @param max_prefix The maximal prefix length in bytes. Input parameter.
* @param max_content The maximal content length in bytes. Input parameter.
* @param max_suffix The maximal suffix length in bytes. Input parameter.
*/
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 );
/** Shares the packet memory block.
* @param packet The packet to be shared.
* @returns EOK on success.
* @returns EINVAL if the packet is not valid.
* @returns EINVAL if the calling module does not accept the memory.
* @returns ENOMEM if the desired and actual sizes differ.
* @returns Other error codes as defined for the ipc_share_in_finalize() function.
*/
int packet_reply( const packet_t packet );
int packet_server_message( ipc_callid_t callid, ipc_call_t * call, ipc_call_t * answer, int * answer_count ){
packet_t packet;
* answer_count = 0;
switch( IPC_GET_METHOD( * call )){
case IPC_M_PHONE_HUNGUP:
return EOK;
case NET_PACKET_CREATE_1:
packet = packet_get( IPC_GET_OWNER( call ), 0, 0, IPC_GET_CONTENT( call ), 0 );
if( ! packet ) return ENOMEM;
* answer_count = 2;
IPC_SET_ARG1( * answer, packet->packet_id );
IPC_SET_ARG2( * answer, packet->length );
return EOK;
case NET_PACKET_CREATE_5:
packet = packet_get( IPC_GET_OWNER( call ), IPC_GET_ADDR_LEN( call ), IPC_GET_PREFIX( call ), IPC_GET_CONTENT( call ), IPC_GET_SUFIX( call ));
if( ! packet ) return ENOMEM;
* answer_count = 2;
IPC_SET_ARG1( * answer, packet->packet_id );
IPC_SET_ARG2( * answer, packet->length );
return EOK;
case NET_PACKET_GET:
packet = pm_find( IPC_GET_ID( call ));
if( ! packet_is_valid( packet )) return ENOENT;
return packet_reply( packet );
case NET_PACKET_GET_SIZE:
packet = pm_find( IPC_GET_ID( call ));
if( ! packet_is_valid( packet )) return ENOENT;
* answer_count = 1;
IPC_SET_ARG1( * answer, packet->length );
return EOK;
case NET_PACKET_RELEASE:
packet = pm_find( IPC_GET_ID( call ));
if( ! packet_is_valid( packet )) return ENOENT;
pq_destroy( packet, packet_release );
return EOK;
}
return ENOTSUP;
}
void packet_release( packet_t packet ){
int index;
for( index = 0; ( index < FREE_QUEUES_COUNT - 1 ) && ( packet->length > ps_globals.sizes[ index ] ); ++ index );
ps_globals.
free[ index
] = pq_add
( ps_globals.
free[ index
], packet
, packet
->length
, packet
->length
);
}
packet_t packet_get( services_t owner, size_t addr_len, size_t max_prefix, size_t max_content, size_t max_suffix ){
int index;
packet_t packet;
size_t length;
length = ALIGN_UP( sizeof( struct packet ) + 2 * addr_len + max_prefix + max_content + max_suffix, PAGE_SIZE );
for( index = 0; index < FREE_QUEUES_COUNT - 1; ++ index ){
if( length <= ps_globals.sizes[ index ] ){
packet
= ps_globals.
free[ index
];
while( packet_is_valid( packet ) && ( packet->length < length )){
packet = pm_find( packet->next );
}
if( packet ){
packet_init( packet, owner, addr_len, max_prefix, max_content, max_suffix );
return packet;
}
}
}
return packet_create( length, owner, addr_len, max_prefix, max_content, max_suffix );
}
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 ){
ERROR_DECLARE;
packet_t packet;
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;
packet->packet_id = ps_globals.count;
packet->mode = PM_ONE_WAY;
packet->length = length;
packet_init( packet, owner, addr_len, max_prefix, max_content, max_suffix );
packet->magic_value = PACKET_MAGIC_VALUE;
if( ERROR_OCCURRED( pm_add( packet ))){
munmap( packet, packet->length );
return NULL;
}
packet_release( packet );
return packet;
}
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 ){
packet->owner = owner;
packet->order = 0;
packet->metric = 0;
packet->previous = 0;
packet->next = 0;
packet->addr_len = addr_len;
packet->src_addr = sizeof( struct packet );
packet->dest_addr = packet->src_addr + packet->addr_len;
packet->max_prefix = max_prefix;
packet->max_content = max_content;
packet->data_start = packet->dest_addr + packet->addr_len + packet->max_prefix;
packet->data_end = packet->data_start;
}
int packet_reply( const packet_t packet ){
ipc_callid_t callid;
size_t size;
if( ! packet_is_valid( packet )) return EINVAL;
if( ipc_share_in_receive( & callid, & size ) <= 0 ) return EINVAL;
if( size != packet->length ) return ENOMEM;
return ipc_share_in_finalize( callid, packet, PROTO_READ | PROTO_WRITE );
}
/** @}
*/