Subversion Repositories HelenOS

Rev

Rev 3685 | Go to most recent revision | Blame | Compare with Previous | Last modification | View Log | Download | RSS feed

  1. /*
  2.  * Copyright (c) 2008 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 net
  30.  * @{
  31.  */
  32.  
  33. /** @file
  34.  */
  35.  
  36. #include <async.h>
  37. #include <errno.h>
  38. #include <stdio.h>
  39. #include <ipc/ipc.h>
  40. #include <ipc/services.h>
  41. //#include <sys/mman.h>
  42.  
  43. #include "../err.h"
  44. #include "../messages.h"
  45. #include "../modules.h"
  46.  
  47. #include "netif.h"
  48.  
  49. #define DEFAULT_MTU 1500
  50.  
  51. #define NAME    "lo - loopback interface"
  52.  
  53. netif_globals_t netif_globals;
  54.  
  55. void    change_status( netif_device_ref device, netif_status_t status );
  56. int change_status_message( netif_device_id_t device_id, netif_status_t status );
  57. int netif_create( netif_device_id_t device_id, netif_device_ref * device );
  58. int netif_call( ipc_callid_t callid );
  59. int netif_initialize( void );
  60. void    netif_print_name( void );
  61. int netif_probe_auto_message( ipcarg_t arg1, ipcarg_t arg2, ipcarg_t arg3, ipcarg_t * result1, ipcarg_t * result2, ipcarg_t * result3 );
  62. int netif_probe_message( ipcarg_t arg1, ipcarg_t arg2, ipcarg_t arg3, ipcarg_t * result1, ipcarg_t * result2, ipcarg_t * result3 );
  63. int netif_send_message( ipcarg_t arg1, ipcarg_t arg2, ipcarg_t arg3, ipcarg_t * result1, ipcarg_t * result2, ipcarg_t * result3 );
  64. int netif_start_message( ipcarg_t arg1, ipcarg_t arg2, ipcarg_t arg3, ipcarg_t * result1, ipcarg_t * result2, ipcarg_t * result3 );
  65. int netif_stop_message( ipcarg_t arg1, ipcarg_t arg2, ipcarg_t arg3, ipcarg_t * result1, ipcarg_t * result2, ipcarg_t * result3 );
  66.  
  67. void change_status( netif_device_ref device, netif_status_t status ){
  68.     device->status = status;
  69.     ll_message( device, NET_LL_DEVICE_STATUS_CHANGED, device->status, NULL, NULL, NULL, NULL );
  70. }
  71.  
  72. int change_status_message( netif_device_id_t device_id, netif_status_t status ){
  73.     ERROR_DECLARE;
  74.  
  75.     netif_device_ref    device;
  76.  
  77.     ERROR_PROPAGATE( netif_device_find( device_id, & device ));
  78.     change_status( device, status );
  79.     return EOK;
  80. }
  81.  
  82. int netif_create( netif_device_id_t device_id, netif_device_ref * device ){
  83.     ERROR_DECLARE;
  84.  
  85.     if( netif_device_map_count( & netif_globals.netif_device_map ) > 0 ){
  86.         return EXDEV;
  87.     }else{
  88.         * device = ( netif_device_ref ) malloc( sizeof( netif_device_t ));
  89.         if( !( * device )) return ENOMEM;
  90. //      ( ** device ).device_id = netif_device_id_generate( 1 );
  91.         ( ** device ).device_id = device_id;
  92.         ( ** device ).ll_registered = NULL;
  93.         netif_device_stats_null( &(( ** device ).stats ));
  94.         ( ** device ).status = NETIF_STOPPED;
  95.         ( ** device ).flags = NULL;
  96.         ( ** device ).mtu = DEFAULT_MTU;
  97.         if( ERROR_OCCURED( netif_device_map_add( & netif_globals.netif_device_map, ( ** device ).device_id, * device ))){
  98.             free( * device );
  99.             * device = NULL;
  100.             return ERROR_CODE;
  101.         }
  102.     }
  103.     return EOK;
  104. }
  105.  
  106. int netif_call( ipc_callid_t callid ){
  107.     return EOK;
  108. }
  109.  
  110. int netif_initialize( void ){
  111.     int phonehash;
  112.  
  113.     return REGISTER_ME( SERVICE_LO, & phonehash );
  114. }
  115.  
  116. void netif_print_name( void ){
  117.     printf( NAME );
  118. }
  119.  
  120. int netif_probe_auto_message( ipcarg_t arg1, ipcarg_t arg2, ipcarg_t arg3, ipcarg_t * result1, ipcarg_t * result2, ipcarg_t * result3 ){
  121.     ERROR_DECLARE;
  122.  
  123.     netif_device_ref    device;
  124.  
  125.     ERROR_PROPAGATE( netif_create( arg1, & device ));
  126.     networking_message( NET_NETWORKING_DEVICE, device->device_id, NULL, NULL, NULL, NULL, NULL );
  127.     return EOK;
  128. }
  129.  
  130. int netif_probe_message( ipcarg_t arg1, ipcarg_t arg2, ipcarg_t arg3, ipcarg_t * result1, ipcarg_t * result2, ipcarg_t * result3 ){
  131.     ERROR_DECLARE;
  132.  
  133.     netif_device_ref    device;
  134.  
  135.     ERROR_PROPAGATE( netif_create( arg1, & device ));
  136.     return EOK;
  137. }
  138.  
  139. int netif_send_message( ipcarg_t arg1, ipcarg_t arg2, ipcarg_t arg3, ipcarg_t * result1, ipcarg_t * result2, ipcarg_t * result3 ){
  140.     ERROR_DECLARE;
  141.  
  142.     netif_device_ref    device;
  143.  
  144.     ERROR_PROPAGATE( netif_device_find( arg1, & device ));
  145.     if( device->status == NETIF_ACTIVE ){
  146.         ++ device->stats.tx_packets;
  147.         ++ device->stats.rx_packets;
  148.         // TODO packet size
  149.         //device->stats->tx_bytes += ;
  150.         //device->stats->rx_bytes += ;
  151.         ll_message( device, NET_LL_RECEIVED, arg2, NULL, NULL, NULL, NULL );
  152.         return EOK;
  153.     }else{
  154.         return EPERM;
  155.     }
  156. }
  157.  
  158. int netif_start_message( ipcarg_t arg1, ipcarg_t arg2, ipcarg_t arg3, ipcarg_t * result1, ipcarg_t * result2, ipcarg_t * result3 ){
  159.     return change_status_message( arg1, NETIF_ACTIVE );
  160. }
  161.  
  162. int netif_stop_message( ipcarg_t arg1, ipcarg_t arg2, ipcarg_t arg3, ipcarg_t * result1, ipcarg_t * result2, ipcarg_t * result3 ){
  163.     return change_status_message( arg1, NETIF_STOPPED );
  164. }
  165.  
  166. /** @}
  167.  */
  168.