Subversion Repositories HelenOS

Rev

Rev 4243 | Rev 4395 | Go to most recent revision | Blame | Compare with Previous | Last modification | View Log | Download | RSS feed

  1. /*
  2.  * Copyright (c) 2009 Lukas 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.  
  29. /** @addtogroup packet
  30.  *  @{
  31.  */
  32.  
  33. /** @file
  34.  *  Packet server implementation.
  35.  */
  36.  
  37. #include <align.h>
  38. #include <async.h>
  39. #include <errno.h>
  40. #include <futex.h>
  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"
  51. #include "packet_client.h"
  52. #include "packet_header.h"
  53. #include "packet_messages.h"
  54. #include "packet_server.h"
  55.  
  56. #define FREE_QUEUES_COUNT   7
  57.  
  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.  
  70. /** Packet server global data.
  71.  */
  72. static struct{
  73.     /** Safety lock.
  74.      */
  75.     futex_t lock;
  76.     /** Free packet queues.
  77.      */
  78.     packet_t free[ FREE_QUEUES_COUNT ];
  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.      */
  83.     size_t sizes[ FREE_QUEUES_COUNT ];
  84.     /** Total packets allocated.
  85.      */
  86.     unsigned int count;
  87. } ps_globals = {
  88.     { 1 },
  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.     0
  92. };
  93.  
  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.
  97.  *  Locks the global data during its processing.
  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.  */
  105. packet_t    packet_get( size_t addr_len, size_t max_prefix, size_t max_content, size_t max_suffix );
  106.  
  107. /** \todo
  108.  */
  109. int packet_release_wrapper( packet_id_t packet_id );
  110.  
  111. /** Releases the packet and returns it to the appropriate free packet queue.
  112.  *  Should be used only when the global data are locked.
  113.  *  @param packet The packet to be released. Input parameter.
  114.  */
  115. void packet_release( packet_t packet );
  116.  
  117. /** Creates a&nbsp;new packet of dimensions at least as given.
  118.  *  Should be used only when the global data are locked.
  119.  *  @param length The total length of the packet, including the header, the addresses and the data of the packet. Input parameter.
  120.  *  @param addr_len The source and destination addresses maximal length in bytes. Input parameter.
  121.  *  @param max_prefix The maximal prefix length in bytes. Input parameter.
  122.  *  @param max_content The maximal content length in bytes. Input parameter.
  123.  *  @param max_suffix The maximal suffix length in bytes. Input parameter.
  124.  *  @returns The packet of dimensions at least as given.
  125.  *  @returns NULL if there is not enough memory left.
  126.  */
  127. packet_t    packet_create( size_t length, size_t addr_len, size_t max_prefix, size_t max_content, size_t max_suffix );
  128.  
  129. /** Initializes the packet according to the given dimensions.
  130.  *  @param packet The packet to be initialized. Input parameter.
  131.  *  @param addr_len The source and destination addresses maximal length in bytes. Input parameter.
  132.  *  @param max_prefix The maximal prefix length in bytes. Input parameter.
  133.  *  @param max_content The maximal content length in bytes. Input parameter.
  134.  *  @param max_suffix The maximal suffix length in bytes. Input parameter.
  135.  */
  136. void    packet_init( packet_t packet, size_t addr_len, size_t max_prefix, size_t max_content, size_t max_suffix );
  137.  
  138. /** Shares the packet memory block.
  139.  *  @param packet The packet to be shared.
  140.  *  @returns EOK on success.
  141.  *  @returns EINVAL if the packet is not valid.
  142.  *  @returns EINVAL if the calling module does not accept the memory.
  143.  *  @returns ENOMEM if the desired and actual sizes differ.
  144.  *  @returns Other error codes as defined for the ipc_share_in_finalize() function.
  145.  */
  146. int packet_reply( const packet_t packet );
  147.  
  148. int packet_translate( int phone, packet_ref packet, packet_id_t packet_id ){
  149.     if( ! packet ) return EINVAL;
  150.     * packet = pm_find( packet_id );
  151.     return ( * packet ) ? EOK : ENOENT;
  152. }
  153.  
  154. packet_t packet_get_4( int phone, size_t max_content, size_t addr_len, size_t max_prefix, size_t max_suffix ){
  155.     return packet_get( addr_len, max_prefix, max_content, max_suffix );
  156. }
  157.  
  158. packet_t packet_get_1( int phone, size_t content ){
  159.     return packet_get( DEFAULT_ADDR_LEN, DEFAULT_PREFIX, content, DEFAULT_SUFFIX );
  160. }
  161.  
  162. void pq_release( int phone, packet_id_t packet_id ){
  163.     ( void ) packet_release_wrapper( packet_id );
  164. }
  165.  
  166. int packet_server_message( ipc_callid_t callid, ipc_call_t * call, ipc_call_t * answer, int * answer_count ){
  167.     packet_t packet;
  168.  
  169.     * answer_count = 0;
  170.     switch( IPC_GET_METHOD( * call )){
  171.         case IPC_M_PHONE_HUNGUP:
  172.             return EOK;
  173.         case NET_PACKET_CREATE_1:
  174.             packet = packet_get( DEFAULT_ADDR_LEN, DEFAULT_PREFIX, IPC_GET_CONTENT( call ), DEFAULT_SUFFIX );
  175.             if( ! packet ) return ENOMEM;
  176.             * answer_count = 2;
  177.             IPC_SET_ARG1( * answer, packet->packet_id );
  178.             IPC_SET_ARG2( * answer, packet->length );
  179.             return EOK;
  180.         case NET_PACKET_CREATE_4:
  181.             packet = packet_get( IPC_GET_ADDR_LEN( call ), IPC_GET_PREFIX( call ), IPC_GET_CONTENT( call ), IPC_GET_SUFFIX( call ));
  182.             if( ! packet ) return ENOMEM;
  183.             * answer_count = 2;
  184.             IPC_SET_ARG1( * answer, packet->packet_id );
  185.             IPC_SET_ARG2( * answer, packet->length );
  186.             return EOK;
  187.         case NET_PACKET_GET:
  188.             packet = pm_find( IPC_GET_ID( call ));
  189.             if( ! packet_is_valid( packet )) return ENOENT;
  190.             return packet_reply( packet );
  191.         case NET_PACKET_GET_SIZE:
  192.             packet = pm_find( IPC_GET_ID( call ));
  193.             if( ! packet_is_valid( packet )) return ENOENT;
  194.             * answer_count = 1;
  195.             IPC_SET_ARG1( * answer, packet->length );
  196.             return EOK;
  197.         case NET_PACKET_RELEASE:
  198.             return packet_release_wrapper( IPC_GET_ID( call ));
  199.     }
  200.     return ENOTSUP;
  201. }
  202.  
  203. int packet_release_wrapper( packet_id_t packet_id ){
  204.     packet_t    packet;
  205.  
  206.     packet = pm_find( packet_id );
  207.     if( ! packet_is_valid( packet )) return ENOENT;
  208.     futex_down( & ps_globals.lock );
  209.     pq_destroy( packet, packet_release );
  210.     futex_up( & ps_globals.lock );
  211.     return EOK;
  212. }
  213.  
  214. void packet_release( packet_t packet ){
  215.     int index;
  216.  
  217.     for( index = 0; ( index < FREE_QUEUES_COUNT - 1 ) && ( packet->length > ps_globals.sizes[ index ] ); ++ index );
  218.     ps_globals.free[ index ] = pq_add( ps_globals.free[ index ], packet, packet->length, packet->length );
  219. }
  220.  
  221. packet_t packet_get( size_t addr_len, size_t max_prefix, size_t max_content, size_t max_suffix ){
  222.     int index;
  223.     packet_t packet;
  224.     size_t length;
  225.  
  226.     length = ALIGN_UP( sizeof( struct packet ) + 2 * addr_len + max_prefix + max_content + max_suffix, PAGE_SIZE );
  227.     futex_down( & ps_globals.lock );
  228.     for( index = 0; index < FREE_QUEUES_COUNT - 1; ++ index ){
  229.         if( length <= ps_globals.sizes[ index ] ){
  230.             packet = ps_globals.free[ index ];
  231.             while( packet_is_valid( packet ) && ( packet->length < length )){
  232.                 packet = pm_find( packet->next );
  233.             }
  234.             if( packet ){
  235.                 packet_init( packet, addr_len, max_prefix, max_content, max_suffix );
  236.                 futex_up( & ps_globals.lock );
  237.                 return packet;
  238.             }
  239.         }
  240.     }
  241.     packet = packet_create( length, addr_len, max_prefix, max_content, max_suffix );
  242.     futex_up( & ps_globals.lock );
  243.     return packet;
  244. }
  245.  
  246. packet_t packet_create( size_t length, size_t addr_len, size_t max_prefix, size_t max_content, size_t max_suffix ){
  247.     ERROR_DECLARE;
  248.  
  249.     packet_t    packet;
  250.  
  251.     // already locked
  252.     packet = ( packet_t ) mmap( NULL, length, PROTO_READ | PROTO_WRITE, MAP_SHARED | MAP_ANONYMOUS, 0, 0 );
  253.     if( packet == MAP_FAILED ) return NULL;
  254.     ++ ps_globals.count;
  255.     packet->packet_id = ps_globals.count;
  256.     packet->length = length;
  257.     packet_init( packet, addr_len, max_prefix, max_content, max_suffix );
  258.     packet->magic_value = PACKET_MAGIC_VALUE;
  259.     if( ERROR_OCCURRED( pm_add( packet ))){
  260.         munmap( packet, packet->length );
  261.         return NULL;
  262.     }
  263.     packet_release( packet );
  264.     return packet;
  265. }
  266.  
  267. void packet_init( packet_t packet, size_t addr_len, size_t max_prefix, size_t max_content, size_t max_suffix ){
  268.     packet->order = 0;
  269.     packet->metric = 0;
  270.     packet->previous = 0;
  271.     packet->next = 0;
  272.     packet->addr_len = addr_len;
  273.     packet->src_addr = sizeof( struct packet );
  274.     packet->dest_addr = packet->src_addr + packet->addr_len;
  275.     packet->max_prefix = max_prefix;
  276.     packet->max_content = max_content;
  277.     packet->data_start = packet->dest_addr + packet->addr_len + packet->max_prefix;
  278.     packet->data_end = packet->data_start;
  279. }
  280.  
  281. int packet_reply( const packet_t packet ){
  282.     ipc_callid_t    callid;
  283.     size_t          size;
  284.  
  285.     if( ! packet_is_valid( packet )) return EINVAL;
  286.     if( ipc_share_in_receive( & callid, & size ) <= 0 ) return EINVAL;
  287.     if( size != packet->length ) return ENOMEM;
  288.     return ipc_share_in_finalize( callid, packet, PROTO_READ | PROTO_WRITE );
  289. }
  290.  
  291. /** @}
  292.  */
  293.