Subversion Repositories HelenOS

Rev

Rev 4261 | Rev 4351 | 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 "../../include/device.h"
  51. #include "../../include/nil_interface.h"
  52. #include "../../include/net_interface.h"
  53.  
  54. #include "../netif.h"
  55. #include "../netif_module.h"
  56.  
  57. #define DEFAULT_MTU 1500
  58.  
  59. #define DEFAULT_ADDR        "\0\0\0\0\0\0"
  60. #define DEFAULT_ADDR_LEN    ( sizeof( DEFAULT_ADDR ) / sizeof( char ))
  61.  
  62. #define NAME    "lo - loopback interface"
  63.  
  64. netif_globals_t netif_globals;
  65.  
  66. static struct lo_globals{
  67.     unsigned int mtu;
  68. } lo_globals;
  69.  
  70. static int  change_state_message( device_ref device, device_state_t state );
  71. static int  create( device_id_t device_id, device_ref * device );
  72. void    module_print_name( void );
  73.  
  74. int netif_specific_message( ipc_callid_t callid, ipc_call_t * call, ipc_call_t * answer, int * answer_count ){
  75.     //TODO nil send message
  76.     return ENOTSUP;
  77. }
  78.  
  79. int netif_get_addr_message( device_id_t device_id, measured_string_ref address ){
  80.     if( ! address ) return EBADMEM;
  81.     address->value = DEFAULT_ADDR;
  82.     address->length = DEFAULT_ADDR_LEN;
  83.     return EOK;
  84. }
  85.  
  86. int netif_get_device_stats( device_id_t device_id, device_stats_ref stats ){
  87.     ERROR_DECLARE;
  88.  
  89.     device_ref  device;
  90.  
  91.     if( ! stats ) return EBADMEM;
  92.     ERROR_PROPAGATE( find_device( device_id, & device ));
  93.     memcpy( stats, ( device_stats_ref ) device->specific, sizeof( device_stats_t ));
  94.     return EOK;
  95. }
  96.  
  97. static int change_state_message( device_ref device, device_state_t state ){
  98.     if( device->state != state ){
  99.         device->state = state;
  100.         printf( "State changed to %s\n", ( state == NETIF_ACTIVE ) ? "ACTIVE" : "STOPPED" );
  101.         return state;
  102.     }
  103.     return EOK;
  104. }
  105.  
  106. static int create( device_id_t device_id, device_ref * device ){
  107.     int index;
  108.  
  109.     if( device_map_count( & netif_globals.device_map ) > 0 ){
  110.         return EXDEV;
  111.     }else{
  112.         * device = ( device_ref ) malloc( sizeof( device_t ));
  113.         if( !( * device )) return ENOMEM;
  114.         ( ** device ).specific = malloc( sizeof( device_stats_t ));
  115.         if( ! ( ** device ).specific ){
  116.             free( * device );
  117.             return ENOMEM;
  118.         }
  119.         null_device_stats(( device_stats_ref )( ** device ).specific );
  120.         ( ** device ).device_id = device_id;
  121.         ( ** device ).nil_phone = -1;
  122.         ( ** device ).state = NETIF_STOPPED;
  123.         index = device_map_add( & netif_globals.device_map, ( ** device ).device_id, * device );
  124.         if( index < 0 ){
  125.             free( * device );
  126.             free(( ** device ).specific );
  127.             * device = NULL;
  128.             return index;
  129.         }
  130.     }
  131.     return EOK;
  132. }
  133.  
  134. int netif_initialize( void ){
  135.     ipcarg_t    phonehash;
  136.  
  137.     return REGISTER_ME( SERVICE_LO, & phonehash );
  138. }
  139.  
  140. void module_print_name( void ){
  141.     printf( "%s", NAME );
  142. }
  143.  
  144. int netif_probe_auto_message( void ){
  145. /*  ERROR_DECLARE;
  146.  
  147.     device_ref  device;
  148.  
  149.     ERROR_PROPAGATE( create( arg1, & device ));
  150.     ipc_call_sync_3_3( netif_globals.net_phone, NET_NET_DEVICE, device->device_id, NULL, NULL, NULL, NULL, NULL );
  151. */  return ENOTSUP;
  152. }
  153.  
  154. int netif_probe_message( device_id_t device_id, int irq, int io ){
  155.     ERROR_DECLARE;
  156.  
  157.     device_ref          device;
  158.     measured_string_t   names[ 1 ] = {{ "MTU", 3 }};
  159.     measured_string_ref configuration;
  160.     int                 count = 1;
  161.     char *              data;
  162.  
  163.     configuration = & names[ 0 ];
  164.     // create a new device
  165.     ERROR_PROPAGATE( create( device_id, & device ));
  166.     // get configuration
  167.     ERROR_PROPAGATE( net_get_device_conf_req( netif_globals.net_phone, device->device_id, & configuration, count, & data ));
  168.     // MTU is the first one
  169.     if( configuration && configuration[ 0 ].value ){
  170.         lo_globals.mtu = strtoul( configuration[ 0 ].value, NULL, 0 );
  171.         net_free_settings( configuration, data );
  172.     }else{
  173.         lo_globals.mtu = DEFAULT_MTU;
  174.     }
  175.     // print the settings
  176.     printf("New device registered:\n\tid\t= %d\n\tMTU\t= %d\n", device->device_id, lo_globals.mtu );
  177.     return EOK;
  178. }
  179.  
  180. int netif_send_message( device_id_t device_id, packet_t packet, services_t sender ){
  181.     ERROR_DECLARE;
  182.  
  183.     device_ref  device;
  184.     size_t      length;
  185.     packet_t    next;
  186.     int         phone;
  187.  
  188.     ERROR_PROPAGATE( find_device( device_id, & device ));
  189.     if( device->state != NETIF_ACTIVE ) return EPERM;
  190.     next = packet;
  191.     do{
  192.         ++ (( device_stats_ref ) device->specific )->tx_packets;
  193.         ++ (( device_stats_ref ) device->specific )->rx_packets;
  194.         length = packet_get_data_length( next );
  195.         (( device_stats_ref ) device->specific )->tx_bytes += length;
  196.         (( device_stats_ref ) device->specific )->rx_bytes += length;
  197.         next = pq_next( next );
  198.     }while( next );
  199.     phone = device->nil_phone;
  200.     rwlock_write_unlock( & netif_globals.lock );
  201.     nil_received_msg( phone, device_id, packet, sender );
  202.     rwlock_write_lock( & netif_globals.lock );
  203.     return EOK;
  204. }
  205.  
  206. int netif_start_message( device_ref device ){
  207.     return change_state_message( device, NETIF_ACTIVE );
  208. }
  209.  
  210. int netif_stop_message( device_ref device ){
  211.     return change_state_message( device, NETIF_STOPPED );
  212. }
  213.  
  214. /** @}
  215.  */
  216.