Subversion Repositories HelenOS

Rev

Rev 4704 | Rev 4743 | 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 <fibril_sync.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      64
  65.  
  66. /** The default suffix reserved for new packets.
  67.  */
  68. #define DEFAULT_SUFFIX      64
  69.  
  70. /** Packet server global data.
  71.  */
  72. static struct{
  73.     /** Safety lock.
  74.      */
  75.     fibril_mutex_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.     { .counter = 1, .waiters = { .prev = & ps_globals.lock.waiters, .next = & ps_globals.lock.waiters, }},
  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. /** @name Packet server support functions
  95.  */
  96. /*@{*/
  97.  
  98. /** Returns the packet of dimensions at least as given.
  99.  *  Tries to reuse free packets first.
  100.  *  Creates a&nbsp;new packet aligned to the memory page size if none available.
  101.  *  Locks the global data during its processing.
  102.  *  @param addr_len The source and destination addresses maximal length in bytes. Input parameter.
  103.  *  @param max_prefix The maximal prefix length in bytes. Input parameter.
  104.  *  @param max_content The maximal content length in bytes. Input parameter.
  105.  *  @param max_suffix The maximal suffix length in bytes. Input parameter.
  106.  *  @returns The packet of dimensions at least as given.
  107.  *  @returns NULL if there is not enough memory left.
  108.  */
  109. packet_t    packet_get( size_t addr_len, size_t max_prefix, size_t max_content, size_t max_suffix );
  110.  
  111. /** Releases the packet queue.
  112.  *  @param packet_id The first packet identifier. Input parameter.
  113.  *  @returns EOK on success.
  114.  *  @returns ENOENT if there is no such packet.
  115.  */
  116. int packet_release_wrapper( packet_id_t packet_id );
  117.  
  118. /** Releases the packet and returns it to the appropriate free packet queue.
  119.  *  Should be used only when the global data are locked.
  120.  *  @param packet The packet to be released. Input parameter.
  121.  */
  122. void packet_release( packet_t packet );
  123.  
  124. /** Creates a&nbsp;new packet of dimensions at least as given.
  125.  *  Should be used only when the global data are locked.
  126.  *  @param length The total length of the packet, including the header, the addresses and the data of the packet. Input parameter.
  127.  *  @param addr_len The source and destination addresses maximal length in bytes. Input parameter.
  128.  *  @param max_prefix The maximal prefix length in bytes. Input parameter.
  129.  *  @param max_content The maximal content length in bytes. Input parameter.
  130.  *  @param max_suffix The maximal suffix length in bytes. Input parameter.
  131.  *  @returns The packet of dimensions at least as given.
  132.  *  @returns NULL if there is not enough memory left.
  133.  */
  134. packet_t    packet_create( size_t length, size_t addr_len, size_t max_prefix, size_t max_content, size_t max_suffix );
  135.  
  136. /** Clears and initializes the packet according to the given dimensions.
  137.  *  @param packet The packet to be initialized. Input parameter.
  138.  *  @param addr_len The source and destination addresses maximal length in bytes. Input parameter.
  139.  *  @param max_prefix The maximal prefix length in bytes. Input parameter.
  140.  *  @param max_content The maximal content length in bytes. Input parameter.
  141.  *  @param max_suffix The maximal suffix length in bytes. Input parameter.
  142.  */
  143. void    packet_init( packet_t packet, size_t addr_len, size_t max_prefix, size_t max_content, size_t max_suffix );
  144.  
  145. /** Shares the packet memory block.
  146.  *  @param packet The packet to be shared.
  147.  *  @returns EOK on success.
  148.  *  @returns EINVAL if the packet is not valid.
  149.  *  @returns EINVAL if the calling module does not accept the memory.
  150.  *  @returns ENOMEM if the desired and actual sizes differ.
  151.  *  @returns Other error codes as defined for the ipc_share_in_finalize() function.
  152.  */
  153. int packet_reply( const packet_t packet );
  154.  
  155. /*@}*/
  156.  
  157. int packet_translate( int phone, packet_ref packet, packet_id_t packet_id ){
  158.     if( ! packet ) return EINVAL;
  159.     * packet = pm_find( packet_id );
  160.     return ( * packet ) ? EOK : ENOENT;
  161. }
  162.  
  163. packet_t packet_get_4( int phone, size_t max_content, size_t addr_len, size_t max_prefix, size_t max_suffix ){
  164.     return packet_get( addr_len, max_prefix, max_content, max_suffix );
  165. }
  166.  
  167. packet_t packet_get_1( int phone, size_t content ){
  168.     return packet_get( DEFAULT_ADDR_LEN, DEFAULT_PREFIX, content, DEFAULT_SUFFIX );
  169. }
  170.  
  171. void pq_release( int phone, packet_id_t packet_id ){
  172.     ( void ) packet_release_wrapper( packet_id );
  173. }
  174.  
  175. int packet_server_message( ipc_callid_t callid, ipc_call_t * call, ipc_call_t * answer, int * answer_count ){
  176.     packet_t packet;
  177.  
  178.     * answer_count = 0;
  179.     switch( IPC_GET_METHOD( * call )){
  180.         case IPC_M_PHONE_HUNGUP:
  181.             return EOK;
  182.         case NET_PACKET_CREATE_1:
  183.             packet = packet_get( DEFAULT_ADDR_LEN, DEFAULT_PREFIX, IPC_GET_CONTENT( call ), DEFAULT_SUFFIX );
  184.             if( ! packet ) return ENOMEM;
  185.             * answer_count = 2;
  186.             IPC_SET_ARG1( * answer, packet->packet_id );
  187.             IPC_SET_ARG2( * answer, packet->length );
  188.             return EOK;
  189.         case NET_PACKET_CREATE_4:
  190.             packet = packet_get( IPC_GET_ADDR_LEN( call ), IPC_GET_PREFIX( call ), IPC_GET_CONTENT( call ), IPC_GET_SUFFIX( call ));
  191.             if( ! packet ) return ENOMEM;
  192.             * answer_count = 2;
  193.             IPC_SET_ARG1( * answer, packet->packet_id );
  194.             IPC_SET_ARG2( * answer, packet->length );
  195.             return EOK;
  196.         case NET_PACKET_GET:
  197.             packet = pm_find( IPC_GET_ID( call ));
  198.             if( ! packet_is_valid( packet )) return ENOENT;
  199.             return packet_reply( packet );
  200.         case NET_PACKET_GET_SIZE:
  201.             packet = pm_find( IPC_GET_ID( call ));
  202.             if( ! packet_is_valid( packet )) return ENOENT;
  203.             IPC_SET_ARG1( * answer, packet->length );
  204.             * answer_count = 1;
  205.             return EOK;
  206.         case NET_PACKET_RELEASE:
  207.             return packet_release_wrapper( IPC_GET_ID( call ));
  208.     }
  209.     return ENOTSUP;
  210. }
  211.  
  212. int packet_release_wrapper( packet_id_t packet_id ){
  213.     packet_t    packet;
  214.  
  215.     packet = pm_find( packet_id );
  216.     if( ! packet_is_valid( packet )) return ENOENT;
  217.     fibril_mutex_lock( & ps_globals.lock );
  218.     pq_destroy( packet, packet_release );
  219.     fibril_mutex_unlock( & ps_globals.lock );
  220.     return EOK;
  221. }
  222.  
  223. void packet_release( packet_t packet ){
  224.     int index;
  225.  
  226.     for( index = 0; ( index < FREE_QUEUES_COUNT - 1 ) && ( packet->length > ps_globals.sizes[ index ] ); ++ index );
  227.     ps_globals.free[ index ] = pq_add( ps_globals.free[ index ], packet, packet->length, packet->length );
  228. }
  229.  
  230. packet_t packet_get( size_t addr_len, size_t max_prefix, size_t max_content, size_t max_suffix ){
  231.     int index;
  232.     packet_t packet;
  233.     size_t length;
  234.  
  235.     length = ALIGN_UP( sizeof( struct packet ) + 2 * addr_len + max_prefix + max_content + max_suffix, PAGE_SIZE );
  236.     fibril_mutex_lock( & ps_globals.lock );
  237.     for( index = 0; index < FREE_QUEUES_COUNT - 1; ++ index ){
  238.         if( length <= ps_globals.sizes[ index ] ){
  239.             packet = ps_globals.free[ index ];
  240.             while( packet_is_valid( packet ) && ( packet->length < length )){
  241.                 packet = pm_find( packet->next );
  242.             }
  243.             if( packet_is_valid( packet )){
  244.                 if( packet == ps_globals.free[ index ] ){
  245.                     ps_globals.free[ index ] = pq_detach( packet );
  246.                 }else{
  247.                     pq_detach( packet );
  248.                 }
  249.                 packet_init( packet, addr_len, max_prefix, max_content, max_suffix );
  250.                 fibril_mutex_unlock( & ps_globals.lock );
  251.                 return packet;
  252.             }
  253.         }
  254.     }
  255.     packet = packet_create( length, addr_len, max_prefix, max_content, max_suffix );
  256.     fibril_mutex_unlock( & ps_globals.lock );
  257.     return packet;
  258. }
  259.  
  260. packet_t packet_create( size_t length, size_t addr_len, size_t max_prefix, size_t max_content, size_t max_suffix ){
  261.     ERROR_DECLARE;
  262.  
  263.     packet_t    packet;
  264.  
  265.     // already locked
  266.     packet = ( packet_t ) mmap( NULL, length, PROTO_READ | PROTO_WRITE, MAP_SHARED | MAP_ANONYMOUS, 0, 0 );
  267.     if( packet == MAP_FAILED ) return NULL;
  268.     ++ ps_globals.count;
  269.     packet->packet_id = ps_globals.count;
  270.     packet->length = length;
  271.     packet_init( packet, addr_len, max_prefix, max_content, max_suffix );
  272.     packet->magic_value = PACKET_MAGIC_VALUE;
  273.     if( ERROR_OCCURRED( pm_add( packet ))){
  274.         munmap( packet, packet->length );
  275.         return NULL;
  276.     }
  277.     return packet;
  278. }
  279.  
  280. void packet_init( packet_t packet, size_t addr_len, size_t max_prefix, size_t max_content, size_t max_suffix ){
  281.     // clear the packet content
  282.     bzero((( void * ) packet ) + sizeof( struct packet ), packet->length - sizeof( struct packet ));
  283.     // clear the packet header
  284.     packet->order = 0;
  285.     packet->metric = 0;
  286.     packet->previous = 0;
  287.     packet->next = 0;
  288.     packet->addr_len = 0;
  289.     packet->src_addr = sizeof( struct packet );
  290.     packet->dest_addr = packet->src_addr + addr_len;
  291.     packet->max_prefix = max_prefix;
  292.     packet->max_content = max_content;
  293.     packet->data_start = packet->dest_addr + addr_len + packet->max_prefix;
  294.     packet->data_end = packet->data_start;
  295. }
  296.  
  297. int packet_reply( const packet_t packet ){
  298.     ipc_callid_t    callid;
  299.     size_t          size;
  300.  
  301.     if( ! packet_is_valid( packet )) return EINVAL;
  302.     if( ipc_share_in_receive( & callid, & size ) <= 0 ) return EINVAL;
  303.     if( size != packet->length ) return ENOMEM;
  304.     return ipc_share_in_finalize( callid, packet, PROTO_READ | PROTO_WRITE );
  305. }
  306.  
  307. /** @}
  308.  */
  309.