Subversion Repositories HelenOS

Rev

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