Subversion Repositories HelenOS

Rev

Rev 4351 | Rev 4582 | 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 lo
  30.  *  @{
  31.  */
  32.  
  33. /** @file
  34.  *  Loopback network interface implementation.
  35.  */
  36.  
  37. #include <async.h>
  38. #include <errno.h>
  39. #include <stdio.h>
  40.  
  41. #include <ipc/ipc.h>
  42. #include <ipc/services.h>
  43.  
  44. #include "../../err.h"
  45. #include "../../messages.h"
  46. #include "../../modules.h"
  47.  
  48. #include "../../structures/measured_strings.h"
  49. #include "../../structures/packet/packet_client.h"
  50.  
  51. #include "../../include/device.h"
  52. #include "../../include/nil_interface.h"
  53. #include "../../include/net_interface.h"
  54.  
  55. #include "../../nil/nil_messages.h"
  56.  
  57. #include "../netif.h"
  58. #include "../netif_module.h"
  59.  
  60. /** Default maximum transmission unit.
  61.  */
  62. #define DEFAULT_MTU 1500
  63.  
  64. /** Default hardware address.
  65.  */
  66. #define DEFAULT_ADDR        "\0\0\0\0\0\0"
  67.  
  68. /** Default address length.
  69.  */
  70. #define DEFAULT_ADDR_LEN    ( sizeof( DEFAULT_ADDR ) / sizeof( char ))
  71.  
  72. /** Loopback module name.
  73.  */
  74. #define NAME    "lo - loopback interface"
  75.  
  76. /** Network interface global data.
  77.  */
  78. netif_globals_t netif_globals;
  79.  
  80. /** Loopback module global data.
  81.  */
  82. static struct lo_globals{
  83.     unsigned int mtu;
  84. } lo_globals;
  85.  
  86. /** Changes the loopback state.
  87.  *  @param device The device structure. Input parameter.
  88.  *  @param state The new device state. Input parameter.
  89.  *  @returns The new state if changed.
  90.  *  @returns EOK otherwise.
  91.  */
  92. int change_state_message( device_ref device, device_state_t state );
  93.  
  94. /** Creates and returns the loopback network interface structure.
  95.  *  @param device_id The new devce identifier. Input parameter.
  96.  *  @param device The device structure. Output parameter.
  97.  *  @returns EOK on success.
  98.  *  @returns EXDEV if one loopback network interface already exists.
  99.  *  @returns ENOMEM if there is not enough memory left.
  100.  */
  101. int create( device_id_t device_id, device_ref * device );
  102.  
  103. /** Prints the module name.
  104.  *  @see NAME
  105.  */
  106. void    module_print_name( void );
  107.  
  108. int netif_specific_message( ipc_callid_t callid, ipc_call_t * call, ipc_call_t * answer, int * answer_count ){
  109.     switch( IPC_GET_METHOD( * call )){
  110.         case NET_NIL_PACKET_SPACE:
  111.             * IPC_SET_ADDR( answer ) = 0;
  112.             * IPC_SET_PREFIX( answer ) = 0;
  113.             * IPC_SET_CONTENT( answer ) = lo_globals.mtu;
  114.             * IPC_SET_SUFFIX( answer ) = 0;
  115.             * answer_count = 4;
  116.             return EOK;
  117.         default:
  118.             return ENOTSUP;
  119.     }
  120. }
  121.  
  122. int netif_get_addr_message( device_id_t device_id, measured_string_ref address ){
  123.     if( ! address ) return EBADMEM;
  124.     address->value = DEFAULT_ADDR;
  125.     address->length = DEFAULT_ADDR_LEN;
  126.     return EOK;
  127. }
  128.  
  129. int netif_get_device_stats( device_id_t device_id, device_stats_ref stats ){
  130.     ERROR_DECLARE;
  131.  
  132.     device_ref  device;
  133.  
  134.     if( ! stats ) return EBADMEM;
  135.     ERROR_PROPAGATE( find_device( device_id, & device ));
  136.     memcpy( stats, ( device_stats_ref ) device->specific, sizeof( device_stats_t ));
  137.     return EOK;
  138. }
  139.  
  140. int change_state_message( device_ref device, device_state_t state ){
  141.     if( device->state != state ){
  142.         device->state = state;
  143.         printf( "State changed to %s\n", ( state == NETIF_ACTIVE ) ? "ACTIVE" : "STOPPED" );
  144.         return state;
  145.     }
  146.     return EOK;
  147. }
  148.  
  149. int create( device_id_t device_id, device_ref * device ){
  150.     int index;
  151.  
  152.     if( device_map_count( & netif_globals.device_map ) > 0 ){
  153.         return EXDEV;
  154.     }else{
  155.         * device = ( device_ref ) malloc( sizeof( device_t ));
  156.         if( !( * device )) return ENOMEM;
  157.         ( ** device ).specific = malloc( sizeof( device_stats_t ));
  158.         if( ! ( ** device ).specific ){
  159.             free( * device );
  160.             return ENOMEM;
  161.         }
  162.         null_device_stats(( device_stats_ref )( ** device ).specific );
  163.         ( ** device ).device_id = device_id;
  164.         ( ** device ).nil_phone = -1;
  165.         ( ** device ).state = NETIF_STOPPED;
  166.         index = device_map_add( & netif_globals.device_map, ( ** device ).device_id, * device );
  167.         if( index < 0 ){
  168.             free( * device );
  169.             free(( ** device ).specific );
  170.             * device = NULL;
  171.             return index;
  172.         }
  173.     }
  174.     return EOK;
  175. }
  176.  
  177. int netif_initialize( void ){
  178.     ipcarg_t    phonehash;
  179.  
  180.     return REGISTER_ME( SERVICE_LO, & phonehash );
  181. }
  182.  
  183. void module_print_name( void ){
  184.     printf( "%s", NAME );
  185. }
  186.  
  187. int netif_probe_auto_message( void ){
  188. /*  ERROR_DECLARE;
  189.  
  190.     device_ref  device;
  191.  
  192.     ERROR_PROPAGATE( create( arg1, & device ));
  193.     ipc_call_sync_3_3( netif_globals.net_phone, NET_NET_DEVICE, device->device_id, NULL, NULL, NULL, NULL, NULL );
  194. */  return ENOTSUP;
  195. }
  196.  
  197. int netif_probe_message( device_id_t device_id, int irq, int io ){
  198.     ERROR_DECLARE;
  199.  
  200.     device_ref          device;
  201.     measured_string_t   names[ 1 ] = {{ "MTU", 3 }};
  202.     measured_string_ref configuration;
  203.     size_t              count = sizeof( names ) / sizeof( measured_string_t );
  204.     char *              data;
  205.  
  206.     configuration = & names[ 0 ];
  207.     // create a new device
  208.     ERROR_PROPAGATE( create( device_id, & device ));
  209.     // get configuration
  210.     ERROR_PROPAGATE( net_get_device_conf_req( netif_globals.net_phone, device->device_id, & configuration, count, & data ));
  211.     // MTU is the first one
  212.     if( configuration && configuration[ 0 ].value ){
  213.         lo_globals.mtu = strtoul( configuration[ 0 ].value, NULL, 0 );
  214.         net_free_settings( configuration, data );
  215.     }else{
  216.         lo_globals.mtu = DEFAULT_MTU;
  217.     }
  218.     // print the settings
  219.     printf("New device registered:\n\tid\t= %d\n\tMTU\t= %d\n", device->device_id, lo_globals.mtu );
  220.     return EOK;
  221. }
  222.  
  223. int netif_send_message( device_id_t device_id, packet_t packet, services_t sender ){
  224.     ERROR_DECLARE;
  225.  
  226.     device_ref  device;
  227.     size_t      length;
  228.     packet_t    next;
  229.     int         phone;
  230.  
  231.     ERROR_PROPAGATE( find_device( device_id, & device ));
  232.     if( device->state != NETIF_ACTIVE ) return EPERM;
  233.     next = packet;
  234.     do{
  235.         ++ (( device_stats_ref ) device->specific )->tx_packets;
  236.         ++ (( device_stats_ref ) device->specific )->rx_packets;
  237.         length = packet_get_data_length( next );
  238.         (( device_stats_ref ) device->specific )->tx_bytes += length;
  239.         (( device_stats_ref ) device->specific )->rx_bytes += length;
  240.         next = pq_next( next );
  241.     }while( next );
  242.     phone = device->nil_phone;
  243.     rwlock_write_unlock( & netif_globals.lock );
  244.     nil_received_msg( phone, device_id, packet, sender );
  245.     rwlock_write_lock( & netif_globals.lock );
  246.     return EOK;
  247. }
  248.  
  249. int netif_start_message( device_ref device ){
  250.     return change_state_message( device, NETIF_ACTIVE );
  251. }
  252.  
  253. int netif_stop_message( device_ref device ){
  254.     return change_state_message( device, NETIF_STOPPED );
  255. }
  256.  
  257. /** @}
  258.  */
  259.