Subversion Repositories HelenOS

Rev

Rev 4192 | Rev 4261 | 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.  */
  35.  
  36. #include <async.h>
  37. #include <errno.h>
  38. #include <stdio.h>
  39.  
  40. #include <ipc/ipc.h>
  41. #include <ipc/services.h>
  42.  
  43. #include "../../err.h"
  44. #include "../../messages.h"
  45. #include "../../modules.h"
  46.  
  47. #include "../../structures/measured_strings.h"
  48. #include "../../structures/packet/packet_client.h"
  49.  
  50. #include "../netif.h"
  51. #include "../netif_interface.h"
  52.  
  53. #define DEFAULT_MTU 1500
  54.  
  55. #define DEFAULT_ADDR        "\0\0\0\0\0\0"
  56. #define DEFAULT_ADDR_LEN    ( sizeof( DEFAULT_ADDR ) / sizeof( char ))
  57.  
  58. #define NAME    "lo - loopback interface"
  59.  
  60. netif_globals_t netif_globals;
  61.  
  62. static struct lo_globals{
  63.     int mtu;
  64. } lo_globals;
  65.  
  66. int change_state_message( device_id_t device_id, device_state_t state );
  67. int create( device_id_t device_id, device_ref * device );
  68. void    netif_print_name( void );
  69.  
  70. int specific_message( ipc_callid_t callid, ipc_call_t * call, ipc_call_t * answer, int * answer_count ){
  71.     return ENOTSUP;
  72. }
  73.  
  74. int get_addr_message( device_id_t device_id, measured_string_ref address ){
  75.     if( ! address ) return EBADMEM;
  76.     address->value = DEFAULT_ADDR;
  77.     address->length = DEFAULT_ADDR_LEN;
  78. }
  79.  
  80. int get_device_stats( device_id_t device_id, device_stats_ref stats ){
  81.     ERROR_DECLARE;
  82.  
  83.     device_ref  device;
  84.  
  85.     if( ! stats ) return EBADMEM;
  86.     ERROR_PROPAGATE( find_device( device_id, & device ));
  87.     memcpy( stats, ( device_stats_ref ) device->specific, sizeof( device_stats_t ));
  88.     return EOK;
  89. }
  90.  
  91. int change_state_message( device_id_t device_id, device_state_t state ){
  92.     ERROR_DECLARE;
  93.  
  94.     device_ref  device;
  95.  
  96.     ERROR_PROPAGATE( find_device( device_id, & device ));
  97.     if( device->state != state ){
  98.         device->state = state;
  99.         nil_message( device, NET_NIL_DEVICE_STATE, device->state, NULL );
  100.         printf( "\nState changed to %s", ( state == NETIF_ACTIVE ) ? "ACTIVE" : "STOPPED" );
  101.     }
  102.     return EOK;
  103. }
  104.  
  105. int create( device_id_t device_id, device_ref * device ){
  106.     int index;
  107.  
  108.     if( device_map_count( & netif_globals.device_map ) > 0 ){
  109.         return EXDEV;
  110.     }else{
  111.         * device = ( device_ref ) malloc( sizeof( device_t ));
  112.         if( !( * device )) return ENOMEM;
  113.         ( ** device ).specific = malloc( sizeof( device_stats_t ));
  114.         if( ! ( ** device ).specific ){
  115.             free( * device );
  116.             return ENOMEM;
  117.         }
  118.         null_device_stats(( device_stats_ref )( ** device ).specific );
  119.         ( ** device ).device_id = device_id;
  120.         ( ** device ).nil_phone = -1;
  121.         ( ** device ).state = NETIF_STOPPED;
  122.         index = device_map_add( & netif_globals.device_map, ( ** device ).device_id, * device );
  123.         if( index < 0 ){
  124.             free( * device );
  125.             free(( ** device ).specific );
  126.             * device = NULL;
  127.             return index;
  128.         }
  129.     }
  130.     return EOK;
  131. }
  132.  
  133. int initialize( void ){
  134.     ipcarg_t    phonehash;
  135.  
  136.     return REGISTER_ME( SERVICE_LO, & phonehash );
  137. }
  138.  
  139. void netif_print_name( void ){
  140.     printf( "%s", NAME );
  141. }
  142.  
  143. int probe_auto_message( void ){
  144. /*  ERROR_DECLARE;
  145.  
  146.     device_ref  device;
  147.  
  148.     ERROR_PROPAGATE( create( arg1, & device ));
  149.     ipc_call_sync_3_3( netif_globals.networking_phone, NET_NET_DEVICE, device->device_id, NULL, NULL, NULL, NULL, NULL );
  150. */  return ENOTSUP;
  151. }
  152.  
  153. int probe_message( device_id_t device_id, int irq, int io ){
  154.     ERROR_DECLARE;
  155.  
  156.     device_ref      device;
  157.     aid_t           message;
  158.     ipc_call_t      answer;
  159.     measured_string_t   configuration[ 1 ] = {{ "MTU", 3 }};
  160.     int         count = 1;
  161.     measured_string_ref settings;
  162.     char *          data;
  163.     ipcarg_t        result;
  164.  
  165.     // create a new device
  166.     ERROR_PROPAGATE( create( device_id, & device ));
  167.     // get configuration
  168.     message = async_send_2( netif_globals.networking_phone, NET_NET_GET_DEVICE_CONF, device->device_id, count, & answer );
  169.     // send names and get settings
  170.     if( ERROR_OCCURRED( measured_strings_send( netif_globals.networking_phone, configuration, count ))
  171.     || ERROR_OCCURRED( measured_strings_return( netif_globals.networking_phone, & settings, & data, count ))){
  172.         async_wait_for( message, NULL );
  173.         return ERROR_CODE;
  174.     }
  175.     // end request
  176.     async_wait_for( message, & result );
  177.     if( ERROR_OCCURRED( result )){
  178.         if( settings ){
  179.             free( settings );
  180.             free( data );
  181.         }
  182.         return ERROR_CODE;
  183.     }
  184.     // MTU is the first one
  185.     if( settings && ( settings[ 0 ].value )){
  186.         lo_globals.mtu = strtoul( settings[ 0 ].value, NULL, 0 );
  187.         free( settings );
  188.         free( data );
  189.     }else{
  190.         lo_globals.mtu = DEFAULT_MTU;
  191.     }
  192.     // print the settings
  193.     printf("\nNew device registered:\n\tid\t= %d\n\tMTU\t= %d", device->device_id, lo_globals.mtu );
  194.     return EOK;
  195. }
  196.  
  197. int send_message( device_id_t device_id, packet_t packet ){
  198.     ERROR_DECLARE;
  199.  
  200.     device_ref  device;
  201.     size_t      length;
  202.     packet_t    next;
  203.  
  204.     ERROR_PROPAGATE( find_device( device_id, & device ));
  205.     if( device->state != NETIF_ACTIVE ) return EPERM;
  206.     next = packet;
  207.     do{
  208.         ++ (( device_stats_ref ) device->specific )->tx_packets;
  209.         ++ (( device_stats_ref ) device->specific )->rx_packets;
  210.         length = packet_get_data_length( next );
  211.         (( device_stats_ref ) device->specific )->tx_bytes += length;
  212.         (( device_stats_ref ) device->specific )->rx_bytes += length;
  213.         next = pq_next( next );
  214.     }while( next );
  215.     nil_message( device, NET_NIL_RECEIVED, packet_get_id( packet ), PACKET_SELF );
  216.     return EOK;
  217. }
  218.  
  219. int start_message( device_id_t device_id ){
  220.     return change_state_message( device_id, NETIF_ACTIVE );
  221. }
  222.  
  223. int stop_message( device_id_t device_id ){
  224.     return change_state_message( device_id, NETIF_STOPPED );
  225. }
  226.  
  227. /** @}
  228.  */
  229.