Subversion Repositories HelenOS

Rev

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