Subversion Repositories HelenOS

Rev

Rev 4261 | Rev 4350 | Go to most recent revision | Details | Compare with Previous | Last modification | View Log | RSS feed

Rev Author Line No. Line
3666 mejdrech 1
/*
3912 mejdrech 2
 * Copyright (c) 2009 Lukas Mejdrech
3666 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
 
3912 mejdrech 29
/** @addtogroup lo
30
 *  @{
3666 mejdrech 31
 */
32
 
33
/** @file
34
 */
35
 
36
#include <async.h>
37
#include <errno.h>
38
#include <stdio.h>
3846 mejdrech 39
 
3666 mejdrech 40
#include <ipc/ipc.h>
41
#include <ipc/services.h>
42
 
3886 mejdrech 43
#include "../../err.h"
44
#include "../../messages.h"
45
#include "../../modules.h"
3666 mejdrech 46
 
3886 mejdrech 47
#include "../../structures/measured_strings.h"
3901 mejdrech 48
#include "../../structures/packet/packet_client.h"
3666 mejdrech 49
 
4243 mejdrech 50
#include "../../include/device.h"
4307 mejdrech 51
#include "../../include/nil_interface.h"
52
#include "../../include/net_interface.h"
4243 mejdrech 53
 
3886 mejdrech 54
#include "../netif.h"
4307 mejdrech 55
#include "../netif_module.h"
3886 mejdrech 56
 
3666 mejdrech 57
#define DEFAULT_MTU 1500
58
 
4192 mejdrech 59
#define DEFAULT_ADDR        "\0\0\0\0\0\0"
60
#define DEFAULT_ADDR_LEN    ( sizeof( DEFAULT_ADDR ) / sizeof( char ))
61
 
3666 mejdrech 62
#define NAME    "lo - loopback interface"
63
 
64
netif_globals_t netif_globals;
65
 
4163 mejdrech 66
static struct lo_globals{
4307 mejdrech 67
    unsigned int mtu;
4163 mejdrech 68
} lo_globals;
69
 
4261 mejdrech 70
static int  change_state_message( device_ref device, device_state_t state );
4243 mejdrech 71
static int  create( device_id_t device_id, device_ref * device );
4307 mejdrech 72
void    module_print_name( void );
3666 mejdrech 73
 
4243 mejdrech 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
4192 mejdrech 76
    return ENOTSUP;
4163 mejdrech 77
}
78
 
4243 mejdrech 79
int netif_get_addr_message( device_id_t device_id, measured_string_ref address ){
4192 mejdrech 80
    if( ! address ) return EBADMEM;
81
    address->value = DEFAULT_ADDR;
82
    address->length = DEFAULT_ADDR_LEN;
4243 mejdrech 83
    return EOK;
4192 mejdrech 84
}
85
 
4243 mejdrech 86
int netif_get_device_stats( device_id_t device_id, device_stats_ref stats ){
4192 mejdrech 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
 
4261 mejdrech 97
static int change_state_message( device_ref device, device_state_t state ){
3846 mejdrech 98
    if( device->state != state ){
99
        device->state = state;
4307 mejdrech 100
        printf( "State changed to %s\n", ( state == NETIF_ACTIVE ) ? "ACTIVE" : "STOPPED" );
4261 mejdrech 101
        return state;
3846 mejdrech 102
    }
3666 mejdrech 103
    return EOK;
104
}
105
 
4243 mejdrech 106
static int create( device_id_t device_id, device_ref * device ){
4192 mejdrech 107
    int index;
3666 mejdrech 108
 
3846 mejdrech 109
    if( device_map_count( & netif_globals.device_map ) > 0 ){
3666 mejdrech 110
        return EXDEV;
111
    }else{
3846 mejdrech 112
        * device = ( device_ref ) malloc( sizeof( device_t ));
3666 mejdrech 113
        if( !( * device )) return ENOMEM;
4192 mejdrech 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 );
3666 mejdrech 120
        ( ** device ).device_id = device_id;
3846 mejdrech 121
        ( ** device ).nil_phone = -1;
3685 mejdrech 122
        ( ** device ).state = NETIF_STOPPED;
4192 mejdrech 123
        index = device_map_add( & netif_globals.device_map, ( ** device ).device_id, * device );
124
        if( index < 0 ){
3666 mejdrech 125
            free( * device );
4192 mejdrech 126
            free(( ** device ).specific );
3666 mejdrech 127
            * device = NULL;
4192 mejdrech 128
            return index;
3666 mejdrech 129
        }
130
    }
131
    return EOK;
132
}
133
 
4243 mejdrech 134
int netif_initialize( void ){
3685 mejdrech 135
    ipcarg_t    phonehash;
3666 mejdrech 136
 
137
    return REGISTER_ME( SERVICE_LO, & phonehash );
138
}
139
 
4307 mejdrech 140
void module_print_name( void ){
4197 mejdrech 141
    printf( "%s", NAME );
3666 mejdrech 142
}
143
 
4243 mejdrech 144
int netif_probe_auto_message( void ){
3685 mejdrech 145
/*  ERROR_DECLARE;
3666 mejdrech 146
 
3846 mejdrech 147
    device_ref  device;
3666 mejdrech 148
 
3846 mejdrech 149
    ERROR_PROPAGATE( create( arg1, & device ));
4307 mejdrech 150
    ipc_call_sync_3_3( netif_globals.net_phone, NET_NET_DEVICE, device->device_id, NULL, NULL, NULL, NULL, NULL );
3685 mejdrech 151
*/  return ENOTSUP;
3666 mejdrech 152
}
153
 
4243 mejdrech 154
int netif_probe_message( device_id_t device_id, int irq, int io ){
3666 mejdrech 155
    ERROR_DECLARE;
156
 
4307 mejdrech 157
    device_ref          device;
158
    measured_string_t   names[ 1 ] = {{ "MTU", 3 }};
159
    measured_string_ref configuration;
160
    int                 count = 1;
161
    char *              data;
3666 mejdrech 162
 
4307 mejdrech 163
    configuration = & names[ 0 ];
3685 mejdrech 164
    // create a new device
3846 mejdrech 165
    ERROR_PROPAGATE( create( device_id, & device ));
3685 mejdrech 166
    // get configuration
4307 mejdrech 167
    ERROR_PROPAGATE( net_get_device_conf_req( netif_globals.net_phone, device->device_id, & configuration, count, & data ));
3685 mejdrech 168
    // MTU is the first one
4307 mejdrech 169
    if( configuration && configuration[ 0 ].value ){
170
        lo_globals.mtu = strtoul( configuration[ 0 ].value, NULL, 0 );
171
        net_free_settings( configuration, data );
3685 mejdrech 172
    }else{
4163 mejdrech 173
        lo_globals.mtu = DEFAULT_MTU;
3685 mejdrech 174
    }
4163 mejdrech 175
    // print the settings
4307 mejdrech 176
    printf("New device registered:\n\tid\t= %d\n\tMTU\t= %d\n", device->device_id, lo_globals.mtu );
3666 mejdrech 177
    return EOK;
178
}
179
 
4261 mejdrech 180
int netif_send_message( device_id_t device_id, packet_t packet, services_t sender ){
3666 mejdrech 181
    ERROR_DECLARE;
182
 
3846 mejdrech 183
    device_ref  device;
184
    size_t      length;
3991 mejdrech 185
    packet_t    next;
4261 mejdrech 186
    int         phone;
3666 mejdrech 187
 
3846 mejdrech 188
    ERROR_PROPAGATE( find_device( device_id, & device ));
189
    if( device->state != NETIF_ACTIVE ) return EPERM;
4075 mejdrech 190
    next = packet;
3991 mejdrech 191
    do{
4192 mejdrech 192
        ++ (( device_stats_ref ) device->specific )->tx_packets;
193
        ++ (( device_stats_ref ) device->specific )->rx_packets;
4075 mejdrech 194
        length = packet_get_data_length( next );
4192 mejdrech 195
        (( device_stats_ref ) device->specific )->tx_bytes += length;
196
        (( device_stats_ref ) device->specific )->rx_bytes += length;
4075 mejdrech 197
        next = pq_next( next );
198
    }while( next );
4261 mejdrech 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 );
3846 mejdrech 203
    return EOK;
3666 mejdrech 204
}
205
 
4261 mejdrech 206
int netif_start_message( device_ref device ){
207
    return change_state_message( device, NETIF_ACTIVE );
3666 mejdrech 208
}
209
 
4261 mejdrech 210
int netif_stop_message( device_ref device ){
211
    return change_state_message( device, NETIF_STOPPED );
3666 mejdrech 212
}
213
 
214
/** @}
215
 */