Subversion Repositories HelenOS

Rev

Rev 4722 | Rev 4731 | 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. #include <string.h>
  41.  
  42. #include <ipc/ipc.h>
  43. #include <ipc/services.h>
  44.  
  45. #include "../../err.h"
  46. #include "../../messages.h"
  47. #include "../../modules.h"
  48.  
  49. #include "../../structures/measured_strings.h"
  50. #include "../../structures/packet/packet_client.h"
  51.  
  52. #include "../../include/device.h"
  53. #include "../../include/nil_interface.h"
  54.  
  55. #include "../../nil/nil_messages.h"
  56.  
  57. #include "../netif.h"
  58. #include "../netif_module.h"
  59.  
  60. /** Default hardware address.
  61.  */
  62. #define DEFAULT_ADDR        "\0\0\0\0\0\0"
  63.  
  64. /** Default address length.
  65.  */
  66. #define DEFAULT_ADDR_LEN    ( sizeof( DEFAULT_ADDR ) / sizeof( char ))
  67.  
  68. /** Loopback module name.
  69.  */
  70. #define NAME    "lo - loopback interface"
  71.  
  72. /** Network interface global data.
  73.  */
  74. netif_globals_t netif_globals;
  75.  
  76. /** Changes the loopback state.
  77.  *  @param device The device structure. Input parameter.
  78.  *  @param state The new device state. Input parameter.
  79.  *  @returns The new state if changed.
  80.  *  @returns EOK otherwise.
  81.  */
  82. int change_state_message( device_ref device, device_state_t state );
  83.  
  84. /** Creates and returns the loopback network interface structure.
  85.  *  @param device_id The new devce identifier. Input parameter.
  86.  *  @param device The device structure. Output parameter.
  87.  *  @returns EOK on success.
  88.  *  @returns EXDEV if one loopback network interface already exists.
  89.  *  @returns ENOMEM if there is not enough memory left.
  90.  */
  91. int create( device_id_t device_id, device_ref * device );
  92.  
  93. /** Prints the module name.
  94.  *  @see NAME
  95.  */
  96. void    module_print_name( void );
  97.  
  98. int netif_specific_message( ipc_callid_t callid, ipc_call_t * call, ipc_call_t * answer, int * answer_count ){
  99.     return ENOTSUP;
  100. }
  101.  
  102. int netif_get_addr_message( device_id_t device_id, measured_string_ref address ){
  103.     if( ! address ) return EBADMEM;
  104.     address->value = DEFAULT_ADDR;
  105.     address->length = DEFAULT_ADDR_LEN;
  106.     return EOK;
  107. }
  108.  
  109. int netif_get_device_stats( device_id_t device_id, device_stats_ref stats ){
  110.     ERROR_DECLARE;
  111.  
  112.     device_ref  device;
  113.  
  114.     if( ! stats ) return EBADMEM;
  115.     ERROR_PROPAGATE( find_device( device_id, & device ));
  116.     memcpy( stats, ( device_stats_ref ) device->specific, sizeof( device_stats_t ));
  117.     return EOK;
  118. }
  119.  
  120. int change_state_message( device_ref device, device_state_t state ){
  121.     if( device->state != state ){
  122.         device->state = state;
  123.         printf( "State changed to %s\n", ( state == NETIF_ACTIVE ) ? "ACTIVE" : "STOPPED" );
  124.         return state;
  125.     }
  126.     return EOK;
  127. }
  128.  
  129. int create( device_id_t device_id, device_ref * device ){
  130.     int index;
  131.  
  132.     if( device_map_count( & netif_globals.device_map ) > 0 ){
  133.         return EXDEV;
  134.     }else{
  135.         * device = ( device_ref ) malloc( sizeof( device_t ));
  136.         if( !( * device )) return ENOMEM;
  137.         ( ** device ).specific = malloc( sizeof( device_stats_t ));
  138.         if( ! ( ** device ).specific ){
  139.             free( * device );
  140.             return ENOMEM;
  141.         }
  142.         null_device_stats(( device_stats_ref )( ** device ).specific );
  143.         ( ** device ).device_id = device_id;
  144.         ( ** device ).nil_phone = -1;
  145.         ( ** device ).state = NETIF_STOPPED;
  146.         index = device_map_add( & netif_globals.device_map, ( ** device ).device_id, * device );
  147.         if( index < 0 ){
  148.             free( * device );
  149.             free(( ** device ).specific );
  150.             * device = NULL;
  151.             return index;
  152.         }
  153.     }
  154.     return EOK;
  155. }
  156.  
  157. int netif_initialize( void ){
  158.     ipcarg_t    phonehash;
  159.  
  160.     return REGISTER_ME( SERVICE_LO, & phonehash );
  161. }
  162.  
  163. void module_print_name( void ){
  164.     printf( "%s", NAME );
  165. }
  166.  
  167. int netif_probe_message( device_id_t device_id, int irq, int io ){
  168.     ERROR_DECLARE;
  169.  
  170.     device_ref          device;
  171.  
  172.     // create a new device
  173.     ERROR_PROPAGATE( create( device_id, & device ));
  174.     // print the settings
  175.     printf("New device created:\n\tid\t= %d\n", device->device_id );
  176.     return EOK;
  177. }
  178.  
  179. int netif_send_message( device_id_t device_id, packet_t packet, services_t sender ){
  180.     ERROR_DECLARE;
  181.  
  182.     device_ref  device;
  183.     size_t      length;
  184.     packet_t    next;
  185.     int         phone;
  186.  
  187.     ERROR_PROPAGATE( find_device( device_id, & device ));
  188.     if( device->state != NETIF_ACTIVE ) return EPERM;
  189.     next = packet;
  190.     do{
  191.         ++ (( device_stats_ref ) device->specific )->send_packets;
  192.         ++ (( device_stats_ref ) device->specific )->receive_packets;
  193.         length = packet_get_data_length( next );
  194.         (( device_stats_ref ) device->specific )->send_bytes += length;
  195.         (( device_stats_ref ) device->specific )->receive_bytes += length;
  196.         next = pq_next( next );
  197.     }while( next );
  198.     phone = device->nil_phone;
  199.     fibril_rwlock_write_unlock( & netif_globals.lock );
  200.     nil_received_msg( phone, device_id, packet, sender );
  201.     fibril_rwlock_write_lock( & netif_globals.lock );
  202.     return EOK;
  203. }
  204.  
  205. int netif_start_message( device_ref device ){
  206.     return change_state_message( device, NETIF_ACTIVE );
  207. }
  208.  
  209. int netif_stop_message( device_ref device ){
  210.     return change_state_message( device, NETIF_STOPPED );
  211. }
  212.  
  213. /** @}
  214.  */
  215.