Subversion Repositories HelenOS

Rev

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