Subversion Repositories HelenOS

Rev

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