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 nildummy
  30.  *  @{
  31.  */
  32.  
  33. /** @file
  34.  *  Dummy network interface layer module implementation.
  35.  *  @see nildummy.h
  36.  */
  37.  
  38. #include <async.h>
  39. #include <malloc.h>
  40. #include <mem.h>
  41. #include <stdio.h>
  42. #include <string.h>
  43.  
  44. #include <ipc/ipc.h>
  45. #include <ipc/services.h>
  46.  
  47. #include "../../err.h"
  48. #include "../../messages.h"
  49. #include "../../modules.h"
  50.  
  51. #include "../../include/device.h"
  52. #include "../../include/netif_interface.h"
  53. #include "../../include/nil_interface.h"
  54. #include "../../include/il_interface.h"
  55.  
  56. #include "../../structures/measured_strings.h"
  57. #include "../../structures/packet/packet.h"
  58.  
  59. #include "../nil_module.h"
  60.  
  61. #include "nildummy.h"
  62.  
  63. /** Default maximum transmission unit.
  64.  */
  65. #define DEFAULT_MTU 1500
  66.  
  67. /** Network interface layer module global data.
  68.  */
  69. nildummy_globals_t  nildummy_globals;
  70.  
  71. /** @name Message processing functions
  72.  */
  73. /*@{*/
  74.  
  75. /** Processes IPC messages from the registered device driver modules in an infinite loop.
  76.  *  @param iid The message identifier. Input parameter.
  77.  *  @param icall The message parameters. Input/output parameter.
  78.  */
  79. void    nildummy_receiver( ipc_callid_t iid, ipc_call_t * icall );
  80.  
  81. /** Registers new device or updates the MTU of an existing one.
  82.  *  Determines the device local hardware address.
  83.  *  @param device_id The new device identifier. Input parameter.
  84.  *  @param service The device driver service. Input parameter.
  85.  *  @param mtu The device maximum transmission unit. Input parameter.
  86.  *  @returns EOK on success.
  87.  *  @returns EEXIST if the device with the different service exists.
  88.  *  @returns ENOMEM if there is not enough memory left.
  89.  *  @returns Other error codes as defined for the netif_bind_service() function.
  90.  *  @returns Other error codes as defined for the netif_get_addr() function.
  91.  */
  92. int nildummy_device_message( device_id_t device_id, services_t service, size_t mtu );
  93.  
  94. /** Returns the device packet dimensions for sending.
  95.  *  @param device_id The device identifier. Input parameter.
  96.  *  @param addr_len The minimum reserved address length. Output parameter.
  97.  *  @param prefix The minimum reserved prefix size. Output parameter.
  98.  *  @param content The maximum content size. Output parameter.
  99.  *  @param suffix The minimum reserved suffix size. Output parameter.
  100.  *  @returns EOK on success.
  101.  *  @returns EBADMEM if either one of the parameters is NULL.
  102.  *  @returns ENOENT if there is no such device.
  103.  */
  104. int nildummy_packet_space_message( device_id_t device_id, size_t * addr_len, size_t * prefix, size_t * content, size_t * suffix );
  105.  
  106. /** Registers receiving module service.
  107.  *  Passes received packets for this service.
  108.  *  @param service The module service. Input parameter.
  109.  *  @param phone The service phone. Input parameter.
  110.  *  @returns EOK on success.
  111.  *  @returns ENOENT if the service is not known.
  112.  *  @returns ENOMEM if there is not enough memory left.
  113.  */
  114. int nildummy_register_message( services_t service, int phone );
  115.  
  116. /** Sends the packet queue.
  117.  *  @param device_id The device identifier. Input parameter.
  118.  *  @param packet The packet queue. Input parameter.
  119.  *  @param sender The sending module service. Input parameter.
  120.  *  @returns EOK on success.
  121.  *  @returns ENOENT if there no such device.
  122.  *  @returns EINVAL if the service parameter is not known.
  123.  */
  124. int nildummy_send_message( device_id_t device_id, packet_t packet, services_t sender );
  125.  
  126. /** Returns the device hardware address.
  127.  *  @param device_id The device identifier. Input parameter.
  128.  *  @param address The device hardware address. Output parameter.
  129.  *  @returns EOK on success.
  130.  *  @returns EBADMEM if the address parameter is NULL.
  131.  *  @returns ENOENT if there no such device.
  132.  */
  133. int nildummy_addr_message( device_id_t device_id, measured_string_ref * address );
  134.  
  135. /*@}*/
  136.  
  137. DEVICE_MAP_IMPLEMENT( nildummy_devices, nildummy_device_t )
  138.  
  139. int nil_device_state_msg( int nil_phone, device_id_t device_id, int state ){
  140.     fibril_rwlock_read_lock( & nildummy_globals.protos_lock );
  141.     if( nildummy_globals.proto.phone ) il_device_state_msg( nildummy_globals.proto.phone, device_id, state, nildummy_globals.proto.service );
  142.     fibril_rwlock_read_unlock( & nildummy_globals.protos_lock );
  143.     return EOK;
  144. }
  145.  
  146. int nil_initialize( int net_phone ){
  147.     ERROR_DECLARE;
  148.  
  149.     fibril_rwlock_initialize( & nildummy_globals.devices_lock );
  150.     fibril_rwlock_initialize( & nildummy_globals.protos_lock );
  151.     fibril_rwlock_write_lock( & nildummy_globals.devices_lock );
  152.     fibril_rwlock_write_lock( & nildummy_globals.protos_lock );
  153.     nildummy_globals.net_phone = net_phone;
  154.     nildummy_globals.proto.phone = 0;
  155.     ERROR_PROPAGATE( nildummy_devices_initialize( & nildummy_globals.devices ));
  156.     fibril_rwlock_write_unlock( & nildummy_globals.protos_lock );
  157.     fibril_rwlock_write_unlock( & nildummy_globals.devices_lock );
  158.     return EOK;
  159. }
  160.  
  161. int nildummy_device_message( device_id_t device_id, services_t service, size_t mtu ){
  162.     ERROR_DECLARE;
  163.  
  164.     nildummy_device_ref device;
  165.     int                 index;
  166.  
  167.     fibril_rwlock_write_lock( & nildummy_globals.devices_lock );
  168.     // an existing device?
  169.     device = nildummy_devices_find( & nildummy_globals.devices, device_id );
  170.     if( device ){
  171.         if( device->service != service ){
  172.             printf( "Device %d already exists\n", device->device_id );
  173.             fibril_rwlock_write_unlock( & nildummy_globals.devices_lock );
  174.             return EEXIST;
  175.         }else{
  176.             // update mtu
  177.             device->mtu = mtu;
  178.             printf( "Device %d already exists:\tMTU\t= %d\n", device->device_id, device->mtu );
  179.             fibril_rwlock_write_unlock( & nildummy_globals.devices_lock );
  180.             // notify the upper layer module
  181.             fibril_rwlock_read_lock( & nildummy_globals.protos_lock );
  182.             if( nildummy_globals.proto.phone ){
  183.                 il_mtu_changed_msg( nildummy_globals.proto.phone, device->device_id, device->mtu, nildummy_globals.proto.service );
  184.             }
  185.             fibril_rwlock_read_unlock( & nildummy_globals.protos_lock );
  186.             return EOK;
  187.         }
  188.     }else{
  189.         // create a new device
  190.         device = ( nildummy_device_ref ) malloc( sizeof( nildummy_device_t ));
  191.         if( ! device ) return ENOMEM;
  192.         device->device_id = device_id;
  193.         device->service = service;
  194.         if( mtu > 0 ){
  195.             device->mtu = mtu;
  196.         }else{
  197.             device->mtu = DEFAULT_MTU;
  198.         }
  199.         // bind the device driver
  200.         device->phone = netif_bind_service( device->service, device->device_id, SERVICE_ETHERNET, nildummy_receiver );
  201.         if( device->phone < 0 ){
  202.             fibril_rwlock_write_unlock( & nildummy_globals.devices_lock );
  203.             free( device );
  204.             return device->phone;
  205.         }
  206.         // get hardware address
  207.         if( ERROR_OCCURRED( netif_get_addr( device->phone, device->device_id, & device->addr, & device->addr_data ))){
  208.             fibril_rwlock_write_unlock( & nildummy_globals.devices_lock );
  209.             free( device );
  210.             return ERROR_CODE;
  211.         }
  212.         // add to the cache
  213.         index = nildummy_devices_add( & nildummy_globals.devices, device->device_id, device );
  214.         if( index < 0 ){
  215.             fibril_rwlock_write_unlock( & nildummy_globals.devices_lock );
  216.             free( device->addr );
  217.             free( device->addr_data );
  218.             free( device );
  219.             return index;
  220.         }
  221.         printf( "New device registered:\n\tid\t= %d\n\tservice\t= %d\n\tMTU\t= %d\n", device->device_id, device->service, device->mtu );
  222.     }
  223.     fibril_rwlock_write_unlock( & nildummy_globals.devices_lock );
  224.     return EOK;
  225. }
  226.  
  227. int nildummy_addr_message( device_id_t device_id, measured_string_ref * address ){
  228.     nildummy_device_ref device;
  229.  
  230.     if( ! address ) return EBADMEM;
  231.     fibril_rwlock_read_lock( & nildummy_globals.devices_lock );
  232.     device = nildummy_devices_find( & nildummy_globals.devices, device_id );
  233.     if( ! device ){
  234.         fibril_rwlock_read_unlock( & nildummy_globals.devices_lock );
  235.         return ENOENT;
  236.     }
  237.     * address = device->addr;
  238.     fibril_rwlock_read_unlock( & nildummy_globals.devices_lock );
  239.     return ( * address ) ? EOK : ENOENT;
  240. }
  241.  
  242. int nildummy_packet_space_message( device_id_t device_id, size_t * addr_len, size_t * prefix, size_t * content, size_t * suffix ){
  243.     nildummy_device_ref device;
  244.  
  245.     if( !( addr_len && prefix && content && suffix )) return EBADMEM;
  246.     fibril_rwlock_read_lock( & nildummy_globals.devices_lock );
  247.     device = nildummy_devices_find( & nildummy_globals.devices, device_id );
  248.     if( ! device ){
  249.         fibril_rwlock_read_unlock( & nildummy_globals.devices_lock );
  250.         return ENOENT;
  251.     }
  252.     * content = device->mtu;
  253.     fibril_rwlock_read_unlock( & nildummy_globals.devices_lock );
  254.     * addr_len = 0;
  255.     * prefix = 0;
  256.     * suffix = 0;
  257.     return EOK;
  258. }
  259.  
  260. int nil_received_msg( int nil_phone, device_id_t device_id, packet_t packet, services_t target ){
  261.     packet_t        next;
  262.  
  263.     fibril_rwlock_read_lock( & nildummy_globals.protos_lock );
  264.     if( nildummy_globals.proto.phone ){
  265.         do{
  266.             next = pq_detach( packet );
  267.             il_received_msg( nildummy_globals.proto.phone, device_id, packet, nildummy_globals.proto.service );
  268.             packet = next;
  269.         }while( packet );
  270.     }
  271.     fibril_rwlock_read_unlock( & nildummy_globals.protos_lock );
  272.     return EOK;
  273. }
  274.  
  275. int nildummy_register_message( services_t service, int phone ){
  276.     fibril_rwlock_write_lock( & nildummy_globals.protos_lock );
  277.     nildummy_globals.proto.service = service;
  278.     nildummy_globals.proto.phone = phone;
  279.     printf( "New protocol registered:\n\tservice\t= %d\n\tphone\t= %d\n", nildummy_globals.proto.service, nildummy_globals.proto.phone );
  280.     fibril_rwlock_write_unlock( & nildummy_globals.protos_lock );
  281.     return EOK;
  282. }
  283.  
  284. int nildummy_send_message( device_id_t device_id, packet_t packet, services_t sender ){
  285.     nildummy_device_ref     device;
  286.  
  287.     fibril_rwlock_read_lock( & nildummy_globals.devices_lock );
  288.     device = nildummy_devices_find( & nildummy_globals.devices, device_id );
  289.     if( ! device ){
  290.         fibril_rwlock_read_unlock( & nildummy_globals.devices_lock );
  291.         return ENOENT;
  292.     }
  293.     // send packet queue
  294.     if( packet ){
  295.         netif_send_msg( device->phone, device_id, packet, SERVICE_NILDUMMY );
  296.     }
  297.     fibril_rwlock_read_unlock( & nildummy_globals.devices_lock );
  298.     return EOK;
  299. }
  300.  
  301. int nil_message( ipc_callid_t callid, ipc_call_t * call, ipc_call_t * answer, int * answer_count ){
  302.     ERROR_DECLARE;
  303.  
  304.     measured_string_ref address;
  305.     packet_t            packet;
  306.  
  307. //  printf( "message %d - %d\n", IPC_GET_METHOD( * call ), NET_NIL_FIRST );
  308.     * answer_count = 0;
  309.     switch( IPC_GET_METHOD( * call )){
  310.         case IPC_M_PHONE_HUNGUP:
  311.             return EOK;
  312.         case NET_NIL_DEVICE:
  313.             return nildummy_device_message( IPC_GET_DEVICE( call ), IPC_GET_SERVICE( call ), IPC_GET_MTU( call ));
  314.         case NET_NIL_SEND:
  315.             ERROR_PROPAGATE( packet_translate( nildummy_globals.net_phone, & packet, IPC_GET_PACKET( call )));
  316.             return nildummy_send_message( IPC_GET_DEVICE( call ), packet, IPC_GET_SERVICE( call ));
  317.         case NET_NIL_PACKET_SPACE:
  318.             ERROR_PROPAGATE( nildummy_packet_space_message( IPC_GET_DEVICE( call ), IPC_SET_ADDR( answer ), IPC_SET_PREFIX( answer ), IPC_SET_CONTENT( answer ), IPC_SET_SUFFIX( answer )));
  319.             * answer_count = 4;
  320.             return EOK;
  321.         case NET_NIL_ADDR:
  322.             ERROR_PROPAGATE( nildummy_addr_message( IPC_GET_DEVICE( call ), & address ));
  323.             return measured_strings_reply( address, 1 );
  324.         case IPC_M_CONNECT_TO_ME:
  325.             return nildummy_register_message( NIL_GET_PROTO( call ), IPC_GET_PHONE( call ));
  326.     }
  327.     return ENOTSUP;
  328. }
  329.  
  330. void nildummy_receiver( ipc_callid_t iid, ipc_call_t * icall ){
  331.     ERROR_DECLARE;
  332.  
  333.     packet_t        packet;
  334.  
  335.     while( true ){
  336. //      printf( "message %d - %d\n", IPC_GET_METHOD( * icall ), NET_NIL_FIRST );
  337.         switch( IPC_GET_METHOD( * icall )){
  338.             case NET_NIL_DEVICE_STATE:
  339.                 ERROR_CODE = nil_device_state_msg( 0, IPC_GET_DEVICE( icall ), IPC_GET_STATE( icall ));
  340.                 ipc_answer_0( iid, ( ipcarg_t ) ERROR_CODE );
  341.                 break;
  342.             case NET_NIL_RECEIVED:
  343.                 if( ! ERROR_OCCURRED( packet_translate( nildummy_globals.net_phone, & packet, IPC_GET_PACKET( icall )))){
  344.                     ERROR_CODE = nil_received_msg( 0, IPC_GET_DEVICE( icall ), packet, 0 );
  345.                 }
  346.                 ipc_answer_0( iid, ( ipcarg_t ) ERROR_CODE );
  347.                 break;
  348.             default:
  349.                 ipc_answer_0( iid, ( ipcarg_t ) ENOTSUP );
  350.         }
  351.         iid = async_get_call( icall );
  352.     }
  353. }
  354.  
  355. /** @}
  356.  */
  357.