Subversion Repositories HelenOS

Rev

Rev 3666 | Rev 3846 | 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>
39
#include <ipc/ipc.h>
40
#include <ipc/services.h>
41
//#include <sys/mman.h>
42
 
43
#include "../err.h"
3685 mejdrech 44
#include "../measured_strings.h"
3666 mejdrech 45
#include "../messages.h"
46
#include "../modules.h"
47
 
48
#include "netif.h"
49
 
50
#define DEFAULT_MTU 1500
51
 
52
#define NAME    "lo - loopback interface"
53
 
54
netif_globals_t netif_globals;
55
 
3685 mejdrech 56
void    change_state( netif_device_ref device, netif_state_t state );
57
int change_state_message( netif_device_id_t device_id, netif_state_t state );
3666 mejdrech 58
int netif_create( netif_device_id_t device_id, netif_device_ref * device );
59
int netif_call( ipc_callid_t callid );
60
int netif_initialize( void );
61
void    netif_print_name( void );
62
int netif_probe_auto_message( ipcarg_t arg1, ipcarg_t arg2, ipcarg_t arg3, ipcarg_t * result1, ipcarg_t * result2, ipcarg_t * result3 );
63
int netif_probe_message( ipcarg_t arg1, ipcarg_t arg2, ipcarg_t arg3, ipcarg_t * result1, ipcarg_t * result2, ipcarg_t * result3 );
64
int netif_send_message( ipcarg_t arg1, ipcarg_t arg2, ipcarg_t arg3, ipcarg_t * result1, ipcarg_t * result2, ipcarg_t * result3 );
65
int netif_start_message( ipcarg_t arg1, ipcarg_t arg2, ipcarg_t arg3, ipcarg_t * result1, ipcarg_t * result2, ipcarg_t * result3 );
66
int netif_stop_message( ipcarg_t arg1, ipcarg_t arg2, ipcarg_t arg3, ipcarg_t * result1, ipcarg_t * result2, ipcarg_t * result3 );
67
 
3685 mejdrech 68
void change_state( netif_device_ref device, netif_state_t state ){
69
    device->state = state;
70
    ll_message( device, NET_LL_DEVICE_STATE_CHANGED, device->state, NULL, NULL, NULL, NULL );
3666 mejdrech 71
}
72
 
3685 mejdrech 73
int change_state_message( netif_device_id_t device_id, netif_state_t state ){
3666 mejdrech 74
    ERROR_DECLARE;
75
 
76
    netif_device_ref    device;
77
 
78
    ERROR_PROPAGATE( netif_device_find( device_id, & device ));
3685 mejdrech 79
    change_state( device, state );
3666 mejdrech 80
    return EOK;
81
}
82
 
83
int netif_create( netif_device_id_t device_id, netif_device_ref * device ){
84
    ERROR_DECLARE;
85
 
86
    if( netif_device_map_count( & netif_globals.netif_device_map ) > 0 ){
87
        return EXDEV;
88
    }else{
89
        * device = ( netif_device_ref ) malloc( sizeof( netif_device_t ));
90
        if( !( * device )) return ENOMEM;
91
        ( ** device ).device_id = device_id;
3685 mejdrech 92
        ( ** device ).ll_registered = -1;
93
        ( ** device ).specific = NULL;
3666 mejdrech 94
        netif_device_stats_null( &(( ** device ).stats ));
3685 mejdrech 95
        ( ** device ).state = NETIF_STOPPED;
3666 mejdrech 96
        ( ** device ).flags = NULL;
97
        ( ** device ).mtu = DEFAULT_MTU;
98
        if( ERROR_OCCURED( netif_device_map_add( & netif_globals.netif_device_map, ( ** device ).device_id, * device ))){
99
            free( * device );
100
            * device = NULL;
101
            return ERROR_CODE;
102
        }
103
    }
104
    return EOK;
105
}
106
 
107
int netif_call( ipc_callid_t callid ){
108
    return EOK;
109
}
110
 
111
int netif_initialize( void ){
3685 mejdrech 112
    ipcarg_t    phonehash;
3666 mejdrech 113
 
114
    return REGISTER_ME( SERVICE_LO, & phonehash );
115
}
116
 
117
void netif_print_name( void ){
118
    printf( NAME );
119
}
120
 
121
int netif_probe_auto_message( ipcarg_t arg1, ipcarg_t arg2, ipcarg_t arg3, ipcarg_t * result1, ipcarg_t * result2, ipcarg_t * result3 ){
3685 mejdrech 122
/*  ERROR_DECLARE;
3666 mejdrech 123
 
124
    netif_device_ref    device;
125
 
126
    ERROR_PROPAGATE( netif_create( arg1, & device ));
3685 mejdrech 127
    ipc_call_sync_3_3( netif_globals.networking_phone, NET_NETWORKING_DEVICE, device->device_id, NULL, NULL, NULL, NULL, NULL );
128
*/  return ENOTSUP;
3666 mejdrech 129
}
130
 
131
int netif_probe_message( ipcarg_t arg1, ipcarg_t arg2, ipcarg_t arg3, ipcarg_t * result1, ipcarg_t * result2, ipcarg_t * result3 ){
132
    ERROR_DECLARE;
133
 
134
    netif_device_ref    device;
3685 mejdrech 135
    aid_t           message;
136
    ipc_call_t      answer;
137
    measured_string_t   configuration[ 1 ] = {{ "MTU", 3 }};
138
    int         count = 1;
139
    measured_string_ref settings;
140
    char *          data;
3666 mejdrech 141
 
3685 mejdrech 142
    // create a new device
3666 mejdrech 143
    ERROR_PROPAGATE( netif_create( arg1, & device ));
3685 mejdrech 144
    // get configuration
145
    message = async_send_2( netif_globals.networking_phone, NET_NETWORKING_GET_DEVICE_CONFIGURATION, device->device_id, count, & answer );
146
    // send names and get settings
147
    if( ERROR_OCCURED( measured_strings_send( netif_globals.networking_phone, configuration, count ))
148
    || ERROR_OCCURED( measured_strings_return( netif_globals.networking_phone, & settings, & data, count ))){
149
        async_wait_for( message, NULL );
150
        return ERROR_CODE;
151
    }
152
    // MTU is the first one
153
    if( settings && ( settings[ 0 ].value )){
154
        device->mtu = strtoul( settings[ 0 ].value, NULL, 0 );
155
    }else{
156
        device->mtu = DEFAULT_MTU;
157
    }
158
    // print the settings
159
    printf("\n -MTU=%d", device->mtu );
160
    free( settings );
161
    free( data );
162
    // end request
163
    async_wait_for( message, NULL );
3666 mejdrech 164
    return EOK;
165
}
166
 
167
int netif_send_message( ipcarg_t arg1, ipcarg_t arg2, ipcarg_t arg3, ipcarg_t * result1, ipcarg_t * result2, ipcarg_t * result3 ){
168
    ERROR_DECLARE;
169
 
170
    netif_device_ref    device;
171
 
172
    ERROR_PROPAGATE( netif_device_find( arg1, & device ));
3685 mejdrech 173
    if( device->state == NETIF_ACTIVE ){
3666 mejdrech 174
        ++ device->stats.tx_packets;
175
        ++ device->stats.rx_packets;
176
        // TODO packet size
177
        //device->stats->tx_bytes += ;
178
        //device->stats->rx_bytes += ;
179
        ll_message( device, NET_LL_RECEIVED, arg2, NULL, NULL, NULL, NULL );
180
        return EOK;
181
    }else{
182
        return EPERM;
183
    }
184
}
185
 
186
int netif_start_message( ipcarg_t arg1, ipcarg_t arg2, ipcarg_t arg3, ipcarg_t * result1, ipcarg_t * result2, ipcarg_t * result3 ){
3685 mejdrech 187
    return change_state_message( arg1, NETIF_ACTIVE );
3666 mejdrech 188
}
189
 
190
int netif_stop_message( ipcarg_t arg1, ipcarg_t arg2, ipcarg_t arg3, ipcarg_t * result1, ipcarg_t * result2, ipcarg_t * result3 ){
3685 mejdrech 191
    return change_state_message( arg1, NETIF_STOPPED );
3666 mejdrech 192
}
193
 
194
/** @}
195
 */