Subversion Repositories HelenOS

Rev

Rev 4197 | Rev 4261 | 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"
51
 
3886 mejdrech 52
#include "../netif.h"
4192 mejdrech 53
#include "../netif_interface.h"
3886 mejdrech 54
 
3666 mejdrech 55
#define DEFAULT_MTU 1500
56
 
4192 mejdrech 57
#define DEFAULT_ADDR        "\0\0\0\0\0\0"
58
#define DEFAULT_ADDR_LEN    ( sizeof( DEFAULT_ADDR ) / sizeof( char ))
59
 
3666 mejdrech 60
#define NAME    "lo - loopback interface"
61
 
62
netif_globals_t netif_globals;
63
 
4163 mejdrech 64
static struct lo_globals{
65
    int mtu;
66
} lo_globals;
67
 
4243 mejdrech 68
static int  change_state_message( device_id_t device_id, device_state_t state );
69
static int  create( device_id_t device_id, device_ref * device );
3666 mejdrech 70
void    netif_print_name( void );
71
 
4243 mejdrech 72
int netif_specific_message( ipc_callid_t callid, ipc_call_t * call, ipc_call_t * answer, int * answer_count ){
73
    //TODO nil send message
4192 mejdrech 74
    return ENOTSUP;
4163 mejdrech 75
}
76
 
4243 mejdrech 77
int netif_get_addr_message( device_id_t device_id, measured_string_ref address ){
4192 mejdrech 78
    if( ! address ) return EBADMEM;
79
    address->value = DEFAULT_ADDR;
80
    address->length = DEFAULT_ADDR_LEN;
4243 mejdrech 81
    return EOK;
4192 mejdrech 82
}
83
 
4243 mejdrech 84
int netif_get_device_stats( device_id_t device_id, device_stats_ref stats ){
4192 mejdrech 85
    ERROR_DECLARE;
86
 
87
    device_ref  device;
88
 
89
    if( ! stats ) return EBADMEM;
90
    ERROR_PROPAGATE( find_device( device_id, & device ));
91
    memcpy( stats, ( device_stats_ref ) device->specific, sizeof( device_stats_t ));
92
    return EOK;
93
}
94
 
4243 mejdrech 95
static int change_state_message( device_id_t device_id, device_state_t state ){
3666 mejdrech 96
    ERROR_DECLARE;
97
 
3846 mejdrech 98
    device_ref  device;
3666 mejdrech 99
 
3846 mejdrech 100
    ERROR_PROPAGATE( find_device( device_id, & device ));
101
    if( device->state != state ){
102
        device->state = state;
3912 mejdrech 103
        nil_message( device, NET_NIL_DEVICE_STATE, device->state, NULL );
4163 mejdrech 104
        printf( "\nState changed to %s", ( state == NETIF_ACTIVE ) ? "ACTIVE" : "STOPPED" );
3846 mejdrech 105
    }
3666 mejdrech 106
    return EOK;
107
}
108
 
4243 mejdrech 109
static int create( device_id_t device_id, device_ref * device ){
4192 mejdrech 110
    int index;
3666 mejdrech 111
 
3846 mejdrech 112
    if( device_map_count( & netif_globals.device_map ) > 0 ){
3666 mejdrech 113
        return EXDEV;
114
    }else{
3846 mejdrech 115
        * device = ( device_ref ) malloc( sizeof( device_t ));
3666 mejdrech 116
        if( !( * device )) return ENOMEM;
4192 mejdrech 117
        ( ** device ).specific = malloc( sizeof( device_stats_t ));
118
        if( ! ( ** device ).specific ){
119
            free( * device );
120
            return ENOMEM;
121
        }
122
        null_device_stats(( device_stats_ref )( ** device ).specific );
3666 mejdrech 123
        ( ** device ).device_id = device_id;
3846 mejdrech 124
        ( ** device ).nil_phone = -1;
3685 mejdrech 125
        ( ** device ).state = NETIF_STOPPED;
4192 mejdrech 126
        index = device_map_add( & netif_globals.device_map, ( ** device ).device_id, * device );
127
        if( index < 0 ){
3666 mejdrech 128
            free( * device );
4192 mejdrech 129
            free(( ** device ).specific );
3666 mejdrech 130
            * device = NULL;
4192 mejdrech 131
            return index;
3666 mejdrech 132
        }
133
    }
134
    return EOK;
135
}
136
 
4243 mejdrech 137
int netif_initialize( void ){
3685 mejdrech 138
    ipcarg_t    phonehash;
3666 mejdrech 139
 
140
    return REGISTER_ME( SERVICE_LO, & phonehash );
141
}
142
 
143
void netif_print_name( void ){
4197 mejdrech 144
    printf( "%s", NAME );
3666 mejdrech 145
}
146
 
4243 mejdrech 147
int netif_probe_auto_message( void ){
3685 mejdrech 148
/*  ERROR_DECLARE;
3666 mejdrech 149
 
3846 mejdrech 150
    device_ref  device;
3666 mejdrech 151
 
3846 mejdrech 152
    ERROR_PROPAGATE( create( arg1, & device ));
153
    ipc_call_sync_3_3( netif_globals.networking_phone, NET_NET_DEVICE, device->device_id, NULL, NULL, NULL, NULL, NULL );
3685 mejdrech 154
*/  return ENOTSUP;
3666 mejdrech 155
}
156
 
4243 mejdrech 157
int netif_probe_message( device_id_t device_id, int irq, int io ){
3666 mejdrech 158
    ERROR_DECLARE;
159
 
3846 mejdrech 160
    device_ref      device;
3685 mejdrech 161
    aid_t           message;
162
    ipc_call_t      answer;
163
    measured_string_t   configuration[ 1 ] = {{ "MTU", 3 }};
164
    int         count = 1;
165
    measured_string_ref settings;
166
    char *          data;
4192 mejdrech 167
    ipcarg_t        result;
3666 mejdrech 168
 
3685 mejdrech 169
    // create a new device
3846 mejdrech 170
    ERROR_PROPAGATE( create( device_id, & device ));
3685 mejdrech 171
    // get configuration
3846 mejdrech 172
    message = async_send_2( netif_globals.networking_phone, NET_NET_GET_DEVICE_CONF, device->device_id, count, & answer );
3685 mejdrech 173
    // send names and get settings
3912 mejdrech 174
    if( ERROR_OCCURRED( measured_strings_send( netif_globals.networking_phone, configuration, count ))
175
    || ERROR_OCCURRED( measured_strings_return( netif_globals.networking_phone, & settings, & data, count ))){
3685 mejdrech 176
        async_wait_for( message, NULL );
177
        return ERROR_CODE;
178
    }
4192 mejdrech 179
    // end request
180
    async_wait_for( message, & result );
181
    if( ERROR_OCCURRED( result )){
182
        if( settings ){
183
            free( settings );
184
            free( data );
185
        }
186
        return ERROR_CODE;
187
    }
3685 mejdrech 188
    // MTU is the first one
189
    if( settings && ( settings[ 0 ].value )){
4163 mejdrech 190
        lo_globals.mtu = strtoul( settings[ 0 ].value, NULL, 0 );
4192 mejdrech 191
        free( settings );
192
        free( data );
3685 mejdrech 193
    }else{
4163 mejdrech 194
        lo_globals.mtu = DEFAULT_MTU;
3685 mejdrech 195
    }
4163 mejdrech 196
    // print the settings
197
    printf("\nNew device registered:\n\tid\t= %d\n\tMTU\t= %d", device->device_id, lo_globals.mtu );
3666 mejdrech 198
    return EOK;
199
}
200
 
4243 mejdrech 201
int netif_send_message( device_id_t device_id, packet_t packet ){
3666 mejdrech 202
    ERROR_DECLARE;
203
 
3846 mejdrech 204
    device_ref  device;
205
    size_t      length;
3991 mejdrech 206
    packet_t    next;
3666 mejdrech 207
 
3846 mejdrech 208
    ERROR_PROPAGATE( find_device( device_id, & device ));
209
    if( device->state != NETIF_ACTIVE ) return EPERM;
4075 mejdrech 210
    next = packet;
3991 mejdrech 211
    do{
4192 mejdrech 212
        ++ (( device_stats_ref ) device->specific )->tx_packets;
213
        ++ (( device_stats_ref ) device->specific )->rx_packets;
4075 mejdrech 214
        length = packet_get_data_length( next );
4192 mejdrech 215
        (( device_stats_ref ) device->specific )->tx_bytes += length;
216
        (( device_stats_ref ) device->specific )->rx_bytes += length;
4075 mejdrech 217
        next = pq_next( next );
218
    }while( next );
219
    nil_message( device, NET_NIL_RECEIVED, packet_get_id( packet ), PACKET_SELF );
3846 mejdrech 220
    return EOK;
3666 mejdrech 221
}
222
 
4243 mejdrech 223
int netif_start_message( device_id_t device_id ){
3846 mejdrech 224
    return change_state_message( device_id, NETIF_ACTIVE );
3666 mejdrech 225
}
226
 
4243 mejdrech 227
int netif_stop_message( device_id_t device_id ){
3846 mejdrech 228
    return change_state_message( device_id, NETIF_STOPPED );
3666 mejdrech 229
}
230
 
231
/** @}
232
 */