Subversion Repositories HelenOS

Rev

Rev 4498 | Rev 4696 | 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>
4582 mejdrech 40
#include <string.h>
3846 mejdrech 41
 
3666 mejdrech 42
#include <ipc/ipc.h>
43
#include <ipc/services.h>
44
 
3886 mejdrech 45
#include "../../err.h"
46
#include "../../messages.h"
47
#include "../../modules.h"
3666 mejdrech 48
 
3886 mejdrech 49
#include "../../structures/measured_strings.h"
3901 mejdrech 50
#include "../../structures/packet/packet_client.h"
3666 mejdrech 51
 
4243 mejdrech 52
#include "../../include/device.h"
4307 mejdrech 53
#include "../../include/nil_interface.h"
54
#include "../../include/net_interface.h"
4243 mejdrech 55
 
4498 mejdrech 56
#include "../../nil/nil_messages.h"
57
 
3886 mejdrech 58
#include "../netif.h"
4307 mejdrech 59
#include "../netif_module.h"
3886 mejdrech 60
 
4350 mejdrech 61
/** Default maximum transmission unit.
62
 */
3666 mejdrech 63
#define DEFAULT_MTU 1500
64
 
4350 mejdrech 65
/** Default hardware address.
66
 */
4192 mejdrech 67
#define DEFAULT_ADDR        "\0\0\0\0\0\0"
4350 mejdrech 68
 
69
/** Default address length.
70
 */
4192 mejdrech 71
#define DEFAULT_ADDR_LEN    ( sizeof( DEFAULT_ADDR ) / sizeof( char ))
72
 
4350 mejdrech 73
/** Loopback module name.
74
 */
3666 mejdrech 75
#define NAME    "lo - loopback interface"
76
 
4350 mejdrech 77
/** Network interface global data.
78
 */
3666 mejdrech 79
netif_globals_t netif_globals;
80
 
4350 mejdrech 81
/** Loopback module global data.
82
 */
4163 mejdrech 83
static struct lo_globals{
4307 mejdrech 84
    unsigned int mtu;
4163 mejdrech 85
} lo_globals;
86
 
4350 mejdrech 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
 */
4307 mejdrech 107
void    module_print_name( void );
3666 mejdrech 108
 
4243 mejdrech 109
int netif_specific_message( ipc_callid_t callid, ipc_call_t * call, ipc_call_t * answer, int * answer_count ){
4498 mejdrech 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
    }
4163 mejdrech 121
}
122
 
4243 mejdrech 123
int netif_get_addr_message( device_id_t device_id, measured_string_ref address ){
4192 mejdrech 124
    if( ! address ) return EBADMEM;
125
    address->value = DEFAULT_ADDR;
126
    address->length = DEFAULT_ADDR_LEN;
4243 mejdrech 127
    return EOK;
4192 mejdrech 128
}
129
 
4243 mejdrech 130
int netif_get_device_stats( device_id_t device_id, device_stats_ref stats ){
4192 mejdrech 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
 
4350 mejdrech 141
int change_state_message( device_ref device, device_state_t state ){
3846 mejdrech 142
    if( device->state != state ){
143
        device->state = state;
4307 mejdrech 144
        printf( "State changed to %s\n", ( state == NETIF_ACTIVE ) ? "ACTIVE" : "STOPPED" );
4261 mejdrech 145
        return state;
3846 mejdrech 146
    }
3666 mejdrech 147
    return EOK;
148
}
149
 
4350 mejdrech 150
int create( device_id_t device_id, device_ref * device ){
4192 mejdrech 151
    int index;
3666 mejdrech 152
 
3846 mejdrech 153
    if( device_map_count( & netif_globals.device_map ) > 0 ){
3666 mejdrech 154
        return EXDEV;
155
    }else{
3846 mejdrech 156
        * device = ( device_ref ) malloc( sizeof( device_t ));
3666 mejdrech 157
        if( !( * device )) return ENOMEM;
4192 mejdrech 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 );
3666 mejdrech 164
        ( ** device ).device_id = device_id;
3846 mejdrech 165
        ( ** device ).nil_phone = -1;
3685 mejdrech 166
        ( ** device ).state = NETIF_STOPPED;
4192 mejdrech 167
        index = device_map_add( & netif_globals.device_map, ( ** device ).device_id, * device );
168
        if( index < 0 ){
3666 mejdrech 169
            free( * device );
4192 mejdrech 170
            free(( ** device ).specific );
3666 mejdrech 171
            * device = NULL;
4192 mejdrech 172
            return index;
3666 mejdrech 173
        }
174
    }
175
    return EOK;
176
}
177
 
4243 mejdrech 178
int netif_initialize( void ){
3685 mejdrech 179
    ipcarg_t    phonehash;
3666 mejdrech 180
 
181
    return REGISTER_ME( SERVICE_LO, & phonehash );
182
}
183
 
4307 mejdrech 184
void module_print_name( void ){
4197 mejdrech 185
    printf( "%s", NAME );
3666 mejdrech 186
}
187
 
4243 mejdrech 188
int netif_probe_auto_message( void ){
3685 mejdrech 189
/*  ERROR_DECLARE;
3666 mejdrech 190
 
3846 mejdrech 191
    device_ref  device;
3666 mejdrech 192
 
3846 mejdrech 193
    ERROR_PROPAGATE( create( arg1, & device ));
4307 mejdrech 194
    ipc_call_sync_3_3( netif_globals.net_phone, NET_NET_DEVICE, device->device_id, NULL, NULL, NULL, NULL, NULL );
3685 mejdrech 195
*/  return ENOTSUP;
3666 mejdrech 196
}
197
 
4243 mejdrech 198
int netif_probe_message( device_id_t device_id, int irq, int io ){
3666 mejdrech 199
    ERROR_DECLARE;
200
 
4307 mejdrech 201
    device_ref          device;
202
    measured_string_t   names[ 1 ] = {{ "MTU", 3 }};
203
    measured_string_ref configuration;
4498 mejdrech 204
    size_t              count = sizeof( names ) / sizeof( measured_string_t );
4307 mejdrech 205
    char *              data;
3666 mejdrech 206
 
4307 mejdrech 207
    configuration = & names[ 0 ];
3685 mejdrech 208
    // create a new device
3846 mejdrech 209
    ERROR_PROPAGATE( create( device_id, & device ));
3685 mejdrech 210
    // get configuration
4307 mejdrech 211
    ERROR_PROPAGATE( net_get_device_conf_req( netif_globals.net_phone, device->device_id, & configuration, count, & data ));
3685 mejdrech 212
    // MTU is the first one
4307 mejdrech 213
    if( configuration && configuration[ 0 ].value ){
214
        lo_globals.mtu = strtoul( configuration[ 0 ].value, NULL, 0 );
215
        net_free_settings( configuration, data );
3685 mejdrech 216
    }else{
4163 mejdrech 217
        lo_globals.mtu = DEFAULT_MTU;
3685 mejdrech 218
    }
4163 mejdrech 219
    // print the settings
4307 mejdrech 220
    printf("New device registered:\n\tid\t= %d\n\tMTU\t= %d\n", device->device_id, lo_globals.mtu );
3666 mejdrech 221
    return EOK;
222
}
223
 
4261 mejdrech 224
int netif_send_message( device_id_t device_id, packet_t packet, services_t sender ){
3666 mejdrech 225
    ERROR_DECLARE;
226
 
3846 mejdrech 227
    device_ref  device;
228
    size_t      length;
3991 mejdrech 229
    packet_t    next;
4261 mejdrech 230
    int         phone;
3666 mejdrech 231
 
3846 mejdrech 232
    ERROR_PROPAGATE( find_device( device_id, & device ));
233
    if( device->state != NETIF_ACTIVE ) return EPERM;
4075 mejdrech 234
    next = packet;
3991 mejdrech 235
    do{
4192 mejdrech 236
        ++ (( device_stats_ref ) device->specific )->tx_packets;
237
        ++ (( device_stats_ref ) device->specific )->rx_packets;
4075 mejdrech 238
        length = packet_get_data_length( next );
4192 mejdrech 239
        (( device_stats_ref ) device->specific )->tx_bytes += length;
240
        (( device_stats_ref ) device->specific )->rx_bytes += length;
4075 mejdrech 241
        next = pq_next( next );
242
    }while( next );
4261 mejdrech 243
    phone = device->nil_phone;
4582 mejdrech 244
    fibril_rwlock_write_unlock( & netif_globals.lock );
4261 mejdrech 245
    nil_received_msg( phone, device_id, packet, sender );
4582 mejdrech 246
    fibril_rwlock_write_lock( & netif_globals.lock );
3846 mejdrech 247
    return EOK;
3666 mejdrech 248
}
249
 
4261 mejdrech 250
int netif_start_message( device_ref device ){
251
    return change_state_message( device, NETIF_ACTIVE );
3666 mejdrech 252
}
253
 
4261 mejdrech 254
int netif_stop_message( device_ref device ){
255
    return change_state_message( device, NETIF_STOPPED );
3666 mejdrech 256
}
257
 
258
/** @}
259
 */