Subversion Repositories HelenOS

Rev

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

Rev Author Line No. Line
3666 mejdrech 1
/*
2
 * Copyright (c) 2008 Lukas 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
 
29
/** @addtogroup net
30
 * @{
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"
48
#include "../../structures/packet/packet.h"
3666 mejdrech 49
 
3886 mejdrech 50
#include "../netif.h"
51
 
3666 mejdrech 52
#define DEFAULT_MTU 1500
53
 
54
#define NAME    "lo - loopback interface"
55
 
56
netif_globals_t netif_globals;
57
 
3846 mejdrech 58
int change_state_message( device_id_t device_id, device_state_t state );
59
int create( device_id_t device_id, device_ref * device );
60
int initialize( void );
3666 mejdrech 61
void    netif_print_name( void );
3846 mejdrech 62
int probe_auto_message( void );
63
int probe_message( device_id_t device_id, int irq, int io );
64
int send_message( device_id_t device_id, packet_t packet );
65
int start_message( device_id_t device_id );
66
int stop_message( device_id_t device_id );
3666 mejdrech 67
 
3846 mejdrech 68
int change_state_message( device_id_t device_id, device_state_t state ){
3666 mejdrech 69
    ERROR_DECLARE;
70
 
3846 mejdrech 71
    device_ref  device;
3666 mejdrech 72
 
3846 mejdrech 73
    ERROR_PROPAGATE( find_device( device_id, & device ));
74
    if( device->state != state ){
75
        device->state = state;
76
        nil_message( device, NET_NIL_DEVICE_STATE, device->state, NULL, NULL, NULL, NULL );
77
    }
3666 mejdrech 78
    return EOK;
79
}
80
 
3846 mejdrech 81
int create( device_id_t device_id, device_ref * device ){
3666 mejdrech 82
    ERROR_DECLARE;
83
 
3846 mejdrech 84
    if( device_map_count( & netif_globals.device_map ) > 0 ){
3666 mejdrech 85
        return EXDEV;
86
    }else{
3846 mejdrech 87
        * device = ( device_ref ) malloc( sizeof( device_t ));
3666 mejdrech 88
        if( !( * device )) return ENOMEM;
89
        ( ** device ).device_id = device_id;
3846 mejdrech 90
        ( ** device ).nil_phone = -1;
3685 mejdrech 91
        ( ** device ).specific = NULL;
3846 mejdrech 92
        null_device_stats( &(( ** device ).stats ));
3685 mejdrech 93
        ( ** device ).state = NETIF_STOPPED;
3666 mejdrech 94
        ( ** device ).flags = NULL;
95
        ( ** device ).mtu = DEFAULT_MTU;
3846 mejdrech 96
        if( ERROR_OCCURED( device_map_add( & netif_globals.device_map, ( ** device ).device_id, * device ))){
3666 mejdrech 97
            free( * device );
98
            * device = NULL;
99
            return ERROR_CODE;
100
        }
101
    }
102
    return EOK;
103
}
104
 
3846 mejdrech 105
int initialize( void ){
3685 mejdrech 106
    ipcarg_t    phonehash;
3666 mejdrech 107
 
108
    return REGISTER_ME( SERVICE_LO, & phonehash );
109
}
110
 
111
void netif_print_name( void ){
112
    printf( NAME );
113
}
114
 
3846 mejdrech 115
int probe_auto_message( void ){
3685 mejdrech 116
/*  ERROR_DECLARE;
3666 mejdrech 117
 
3846 mejdrech 118
    device_ref  device;
3666 mejdrech 119
 
3846 mejdrech 120
    ERROR_PROPAGATE( create( arg1, & device ));
121
    ipc_call_sync_3_3( netif_globals.networking_phone, NET_NET_DEVICE, device->device_id, NULL, NULL, NULL, NULL, NULL );
3685 mejdrech 122
*/  return ENOTSUP;
3666 mejdrech 123
}
124
 
3846 mejdrech 125
int probe_message( device_id_t device_id, int irq, int io ){
3666 mejdrech 126
    ERROR_DECLARE;
127
 
3846 mejdrech 128
    device_ref      device;
3685 mejdrech 129
    aid_t           message;
130
    ipc_call_t      answer;
131
    measured_string_t   configuration[ 1 ] = {{ "MTU", 3 }};
132
    int         count = 1;
133
    measured_string_ref settings;
134
    char *          data;
3666 mejdrech 135
 
3685 mejdrech 136
    // create a new device
3846 mejdrech 137
    ERROR_PROPAGATE( create( device_id, & device ));
3685 mejdrech 138
    // get configuration
3846 mejdrech 139
    message = async_send_2( netif_globals.networking_phone, NET_NET_GET_DEVICE_CONF, device->device_id, count, & answer );
3685 mejdrech 140
    // send names and get settings
141
    if( ERROR_OCCURED( measured_strings_send( netif_globals.networking_phone, configuration, count ))
142
    || ERROR_OCCURED( measured_strings_return( netif_globals.networking_phone, & settings, & data, count ))){
143
        async_wait_for( message, NULL );
144
        return ERROR_CODE;
145
    }
146
    // MTU is the first one
147
    if( settings && ( settings[ 0 ].value )){
148
        device->mtu = strtoul( settings[ 0 ].value, NULL, 0 );
149
    }else{
150
        device->mtu = DEFAULT_MTU;
151
    }
152
    // print the settings
3846 mejdrech 153
    printf("\n -MTU =\t%d", device->mtu );
3685 mejdrech 154
    free( settings );
155
    free( data );
156
    // end request
157
    async_wait_for( message, NULL );
3666 mejdrech 158
    return EOK;
159
}
160
 
3846 mejdrech 161
int send_message( device_id_t device_id, packet_t packet ){
3666 mejdrech 162
    ERROR_DECLARE;
163
 
3846 mejdrech 164
    device_ref  device;
165
    size_t      length;
166
    aid_t       message;
167
    ipc_call_t  answer;
168
    ipcarg_t    result;
169
    packet_t    received;
3666 mejdrech 170
 
3846 mejdrech 171
    ERROR_PROPAGATE( find_device( device_id, & device ));
172
    if( device->state != NETIF_ACTIVE ) return EPERM;
173
    ++ device->stats.tx_packets;
174
    ++ device->stats.rx_packets;
175
    length = packet_get_data_length( packet );
176
    device->stats.tx_bytes += length;
177
    device->stats.rx_bytes += length;
178
    received = packet_copy( packet );
179
    packet_destroy( packet );
180
    if( ! received ){
181
        ++ device->stats.rx_dropped;
3666 mejdrech 182
        return EOK;
183
    }
3846 mejdrech 184
    message = async_send_1( device->nil_phone, NET_NIL_RECEIVED, ( device_id ), & answer );
185
    if( ERROR_OCCURED( packet_send( received, device->nil_phone ))){
186
        ++ device->stats.rx_dropped;
187
    }
188
    async_wait_for( message, & result );
189
    if( result != EOK ) ++ device->stats.rx_dropped;
190
    return EOK;
3666 mejdrech 191
}
192
 
3846 mejdrech 193
int start_message( device_id_t device_id ){
194
    return change_state_message( device_id, NETIF_ACTIVE );
3666 mejdrech 195
}
196
 
3846 mejdrech 197
int stop_message( device_id_t device_id ){
198
    return change_state_message( device_id, NETIF_STOPPED );
3666 mejdrech 199
}
200
 
201
/** @}
202
 */