Subversion Repositories HelenOS

Rev

Rev 3846 | Rev 3901 | Go to most recent revision | Only display areas with differences | Ignore whitespace | Details | Blame | Last modification | View Log | RSS feed

Rev 3846 Rev 3886
1
/*
1
/*
2
 * Copyright (c) 2008 Lukas Mejdrech
2
 * Copyright (c) 2008 Lukas Mejdrech
3
 * All rights reserved.
3
 * All rights reserved.
4
 *
4
 *
5
 * Redistribution and use in source and binary forms, with or without
5
 * Redistribution and use in source and binary forms, with or without
6
 * modification, are permitted provided that the following conditions
6
 * modification, are permitted provided that the following conditions
7
 * are met:
7
 * are met:
8
 *
8
 *
9
 * - Redistributions of source code must retain the above copyright
9
 * - Redistributions of source code must retain the above copyright
10
 *   notice, this list of conditions and the following disclaimer.
10
 *   notice, this list of conditions and the following disclaimer.
11
 * - Redistributions in binary form must reproduce the above copyright
11
 * - Redistributions in binary form must reproduce the above copyright
12
 *   notice, this list of conditions and the following disclaimer in the
12
 *   notice, this list of conditions and the following disclaimer in the
13
 *   documentation and/or other materials provided with the distribution.
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
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.
15
 *   derived from this software without specific prior written permission.
16
 *
16
 *
17
 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
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
18
 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
19
 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
19
 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
20
 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
20
 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
21
 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
21
 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
22
 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
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
23
 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
24
 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
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
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.
26
 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
27
 */
27
 */
28
 
28
 
29
/** @addtogroup net
29
/** @addtogroup net
30
 * @{
30
 * @{
31
 */
31
 */
32
 
32
 
33
/** @file
33
/** @file
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
 
40
#include <ipc/ipc.h>
40
#include <ipc/ipc.h>
41
#include <ipc/services.h>
41
#include <ipc/services.h>
42
 
42
 
43
#include "../err.h"
43
#include "../../err.h"
44
#include "../measured_strings.h"
-
 
45
#include "../messages.h"
44
#include "../../messages.h"
46
#include "../modules.h"
45
#include "../../modules.h"
47
#include "../packet.h"
-
 
48
 
46
 
-
 
47
#include "../../structures/measured_strings.h"
-
 
48
#include "../../structures/packet/packet.h"
-
 
49
 
49
#include "netif.h"
50
#include "../netif.h"
50
 
51
 
51
#define DEFAULT_MTU 1500
52
#define DEFAULT_MTU 1500
52
 
53
 
53
#define NAME    "lo - loopback interface"
54
#define NAME    "lo - loopback interface"
54
 
55
 
55
netif_globals_t netif_globals;
56
netif_globals_t netif_globals;
56
 
57
 
57
int change_state_message( device_id_t device_id, device_state_t state );
58
int change_state_message( device_id_t device_id, device_state_t state );
58
int create( device_id_t device_id, device_ref * device );
59
int create( device_id_t device_id, device_ref * device );
59
int initialize( void );
60
int initialize( void );
60
void    netif_print_name( void );
61
void    netif_print_name( void );
61
int probe_auto_message( void );
62
int probe_auto_message( void );
62
int probe_message( device_id_t device_id, int irq, int io );
63
int probe_message( device_id_t device_id, int irq, int io );
63
int send_message( device_id_t device_id, packet_t packet );
64
int send_message( device_id_t device_id, packet_t packet );
64
int start_message( device_id_t device_id );
65
int start_message( device_id_t device_id );
65
int stop_message( device_id_t device_id );
66
int stop_message( device_id_t device_id );
66
 
67
 
67
int change_state_message( device_id_t device_id, device_state_t state ){
68
int change_state_message( device_id_t device_id, device_state_t state ){
68
    ERROR_DECLARE;
69
    ERROR_DECLARE;
69
 
70
 
70
    device_ref  device;
71
    device_ref  device;
71
 
72
 
72
    ERROR_PROPAGATE( find_device( device_id, & device ));
73
    ERROR_PROPAGATE( find_device( device_id, & device ));
73
    if( device->state != state ){
74
    if( device->state != state ){
74
        device->state = state;
75
        device->state = state;
75
        nil_message( device, NET_NIL_DEVICE_STATE, device->state, NULL, NULL, NULL, NULL );
76
        nil_message( device, NET_NIL_DEVICE_STATE, device->state, NULL, NULL, NULL, NULL );
76
    }
77
    }
77
    return EOK;
78
    return EOK;
78
}
79
}
79
 
80
 
80
int create( device_id_t device_id, device_ref * device ){
81
int create( device_id_t device_id, device_ref * device ){
81
    ERROR_DECLARE;
82
    ERROR_DECLARE;
82
 
83
 
83
    if( device_map_count( & netif_globals.device_map ) > 0 ){
84
    if( device_map_count( & netif_globals.device_map ) > 0 ){
84
        return EXDEV;
85
        return EXDEV;
85
    }else{
86
    }else{
86
        * device = ( device_ref ) malloc( sizeof( device_t ));
87
        * device = ( device_ref ) malloc( sizeof( device_t ));
87
        if( !( * device )) return ENOMEM;
88
        if( !( * device )) return ENOMEM;
88
        ( ** device ).device_id = device_id;
89
        ( ** device ).device_id = device_id;
89
        ( ** device ).nil_phone = -1;
90
        ( ** device ).nil_phone = -1;
90
        ( ** device ).specific = NULL;
91
        ( ** device ).specific = NULL;
91
        null_device_stats( &(( ** device ).stats ));
92
        null_device_stats( &(( ** device ).stats ));
92
        ( ** device ).state = NETIF_STOPPED;
93
        ( ** device ).state = NETIF_STOPPED;
93
        ( ** device ).flags = NULL;
94
        ( ** device ).flags = NULL;
94
        ( ** device ).mtu = DEFAULT_MTU;
95
        ( ** device ).mtu = DEFAULT_MTU;
95
        if( ERROR_OCCURED( device_map_add( & netif_globals.device_map, ( ** device ).device_id, * device ))){
96
        if( ERROR_OCCURED( device_map_add( & netif_globals.device_map, ( ** device ).device_id, * device ))){
96
            free( * device );
97
            free( * device );
97
            * device = NULL;
98
            * device = NULL;
98
            return ERROR_CODE;
99
            return ERROR_CODE;
99
        }
100
        }
100
    }
101
    }
101
    return EOK;
102
    return EOK;
102
}
103
}
103
 
104
 
104
int initialize( void ){
105
int initialize( void ){
105
    ipcarg_t    phonehash;
106
    ipcarg_t    phonehash;
106
 
107
 
107
    return REGISTER_ME( SERVICE_LO, & phonehash );
108
    return REGISTER_ME( SERVICE_LO, & phonehash );
108
}
109
}
109
 
110
 
110
void netif_print_name( void ){
111
void netif_print_name( void ){
111
    printf( NAME );
112
    printf( NAME );
112
}
113
}
113
 
114
 
114
int probe_auto_message( void ){
115
int probe_auto_message( void ){
115
/*  ERROR_DECLARE;
116
/*  ERROR_DECLARE;
116
 
117
 
117
    device_ref  device;
118
    device_ref  device;
118
 
119
 
119
    ERROR_PROPAGATE( create( arg1, & device ));
120
    ERROR_PROPAGATE( create( arg1, & device ));
120
    ipc_call_sync_3_3( netif_globals.networking_phone, NET_NET_DEVICE, device->device_id, NULL, NULL, NULL, NULL, NULL );
121
    ipc_call_sync_3_3( netif_globals.networking_phone, NET_NET_DEVICE, device->device_id, NULL, NULL, NULL, NULL, NULL );
121
*/  return ENOTSUP;
122
*/  return ENOTSUP;
122
}
123
}
123
 
124
 
124
int probe_message( device_id_t device_id, int irq, int io ){
125
int probe_message( device_id_t device_id, int irq, int io ){
125
    ERROR_DECLARE;
126
    ERROR_DECLARE;
126
 
127
 
127
    device_ref      device;
128
    device_ref      device;
128
    aid_t           message;
129
    aid_t           message;
129
    ipc_call_t      answer;
130
    ipc_call_t      answer;
130
    measured_string_t   configuration[ 1 ] = {{ "MTU", 3 }};
131
    measured_string_t   configuration[ 1 ] = {{ "MTU", 3 }};
131
    int         count = 1;
132
    int         count = 1;
132
    measured_string_ref settings;
133
    measured_string_ref settings;
133
    char *          data;
134
    char *          data;
134
 
135
 
135
    // create a new device
136
    // create a new device
136
    ERROR_PROPAGATE( create( device_id, & device ));
137
    ERROR_PROPAGATE( create( device_id, & device ));
137
    // get configuration
138
    // get configuration
138
    message = async_send_2( netif_globals.networking_phone, NET_NET_GET_DEVICE_CONF, device->device_id, count, & answer );
139
    message = async_send_2( netif_globals.networking_phone, NET_NET_GET_DEVICE_CONF, device->device_id, count, & answer );
139
    // send names and get settings
140
    // send names and get settings
140
    if( ERROR_OCCURED( measured_strings_send( netif_globals.networking_phone, configuration, count ))
141
    if( ERROR_OCCURED( measured_strings_send( netif_globals.networking_phone, configuration, count ))
141
    || ERROR_OCCURED( measured_strings_return( netif_globals.networking_phone, & settings, & data, count ))){
142
    || ERROR_OCCURED( measured_strings_return( netif_globals.networking_phone, & settings, & data, count ))){
142
        async_wait_for( message, NULL );
143
        async_wait_for( message, NULL );
143
        return ERROR_CODE;
144
        return ERROR_CODE;
144
    }
145
    }
145
    // MTU is the first one
146
    // MTU is the first one
146
    if( settings && ( settings[ 0 ].value )){
147
    if( settings && ( settings[ 0 ].value )){
147
        device->mtu = strtoul( settings[ 0 ].value, NULL, 0 );
148
        device->mtu = strtoul( settings[ 0 ].value, NULL, 0 );
148
    }else{
149
    }else{
149
        device->mtu = DEFAULT_MTU;
150
        device->mtu = DEFAULT_MTU;
150
    }
151
    }
151
    // print the settings
152
    // print the settings
152
    printf("\n -MTU =\t%d", device->mtu );
153
    printf("\n -MTU =\t%d", device->mtu );
153
    free( settings );
154
    free( settings );
154
    free( data );
155
    free( data );
155
    // end request
156
    // end request
156
    async_wait_for( message, NULL );
157
    async_wait_for( message, NULL );
157
    return EOK;
158
    return EOK;
158
}
159
}
159
 
160
 
160
int send_message( device_id_t device_id, packet_t packet ){
161
int send_message( device_id_t device_id, packet_t packet ){
161
    ERROR_DECLARE;
162
    ERROR_DECLARE;
162
 
163
 
163
    device_ref  device;
164
    device_ref  device;
164
    size_t      length;
165
    size_t      length;
165
    aid_t       message;
166
    aid_t       message;
166
    ipc_call_t  answer;
167
    ipc_call_t  answer;
167
    ipcarg_t    result;
168
    ipcarg_t    result;
168
    packet_t    received;
169
    packet_t    received;
169
 
170
 
170
    ERROR_PROPAGATE( find_device( device_id, & device ));
171
    ERROR_PROPAGATE( find_device( device_id, & device ));
171
    if( device->state != NETIF_ACTIVE ) return EPERM;
172
    if( device->state != NETIF_ACTIVE ) return EPERM;
172
    ++ device->stats.tx_packets;
173
    ++ device->stats.tx_packets;
173
    ++ device->stats.rx_packets;
174
    ++ device->stats.rx_packets;
174
    length = packet_get_data_length( packet );
175
    length = packet_get_data_length( packet );
175
    device->stats.tx_bytes += length;
176
    device->stats.tx_bytes += length;
176
    device->stats.rx_bytes += length;
177
    device->stats.rx_bytes += length;
177
    received = packet_copy( packet );
178
    received = packet_copy( packet );
178
    packet_destroy( packet );
179
    packet_destroy( packet );
179
    if( ! received ){
180
    if( ! received ){
180
        ++ device->stats.rx_dropped;
181
        ++ device->stats.rx_dropped;
181
        return EOK;
182
        return EOK;
182
    }
183
    }
183
    message = async_send_1( device->nil_phone, NET_NIL_RECEIVED, ( device_id ), & answer );
184
    message = async_send_1( device->nil_phone, NET_NIL_RECEIVED, ( device_id ), & answer );
184
    if( ERROR_OCCURED( packet_send( received, device->nil_phone ))){
185
    if( ERROR_OCCURED( packet_send( received, device->nil_phone ))){
185
        ++ device->stats.rx_dropped;
186
        ++ device->stats.rx_dropped;
186
    }
187
    }
187
    async_wait_for( message, & result );
188
    async_wait_for( message, & result );
188
    if( result != EOK ) ++ device->stats.rx_dropped;
189
    if( result != EOK ) ++ device->stats.rx_dropped;
189
    return EOK;
190
    return EOK;
190
}
191
}
191
 
192
 
192
int start_message( device_id_t device_id ){
193
int start_message( device_id_t device_id ){
193
    return change_state_message( device_id, NETIF_ACTIVE );
194
    return change_state_message( device_id, NETIF_ACTIVE );
194
}
195
}
195
 
196
 
196
int stop_message( device_id_t device_id ){
197
int stop_message( device_id_t device_id ){
197
    return change_state_message( device_id, NETIF_STOPPED );
198
    return change_state_message( device_id, NETIF_STOPPED );
198
}
199
}
199
 
200
 
200
/** @}
201
/** @}
201
 */
202
 */
202
 
203