Subversion Repositories HelenOS

Rev

Rev 3685 | Go to most recent revision | Show entire file | Ignore whitespace | Details | Blame | Last modification | View Log | RSS feed

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