Subversion Repositories HelenOS

Rev

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

Rev 4698 Rev 4703
1
/*
1
/*
2
 * Copyright (c) 2009 Lukas Mejdrech
2
 * Copyright (c) 2009 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 netif
29
/** @addtogroup netif
30
 *  @{
30
 *  @{
31
 */
31
 */
32
 
32
 
33
/** @file
33
/** @file
34
 *  Network interface module skeleton implementation.
34
 *  Network interface module skeleton implementation.
35
 *  @see netif.h
35
 *  @see netif.h
36
 */
36
 */
37
 
37
 
38
#include <async.h>
38
#include <async.h>
39
#include <mem.h>
39
#include <mem.h>
40
#include <fibril_sync.h>
40
#include <fibril_sync.h>
41
#include <stdio.h>
41
#include <stdio.h>
42
 
42
 
43
#include <ipc/ipc.h>
43
#include <ipc/ipc.h>
44
#include <ipc/services.h>
44
#include <ipc/services.h>
45
 
45
 
46
#include "../err.h"
46
#include "../err.h"
47
#include "../messages.h"
47
#include "../messages.h"
48
#include "../modules.h"
48
#include "../modules.h"
49
 
49
 
50
#include "../structures/packet/packet.h"
50
#include "../structures/packet/packet.h"
51
#include "../structures/packet/packet_client.h"
51
#include "../structures/packet/packet_client.h"
52
#include "../structures/measured_strings.h"
52
#include "../structures/measured_strings.h"
53
 
53
 
54
#include "../include/device.h"
54
#include "../include/device.h"
55
#include "../include/netif_interface.h"
55
#include "../include/netif_interface.h"
56
#include "../include/nil_interface.h"
56
#include "../include/nil_interface.h"
57
 
57
 
58
#include "netif.h"
58
#include "netif.h"
59
#include "netif_messages.h"
59
#include "netif_messages.h"
60
#include "netif_module.h"
60
#include "netif_module.h"
61
 
61
 
62
/** Network interface module global data.
62
/** Network interface module global data.
63
 */
63
 */
64
extern netif_globals_t netif_globals;
64
extern netif_globals_t netif_globals;
65
 
65
 
66
DEVICE_MAP_IMPLEMENT( device_map, device_t )
66
DEVICE_MAP_IMPLEMENT( device_map, device_t )
67
 
67
 
-
 
68
/** @name Message processing functions
-
 
69
 */
-
 
70
/*@{*/
-
 
71
 
68
/** Registers the device notification receiver, the network interface layer module.
72
/** Registers the device notification receiver, the network interface layer module.
69
 *  @param device_id The device identifier. Input parameter.
73
 *  @param device_id The device identifier. Input parameter.
70
 *  @param phone The network interface layer module phone. Input parameter.
74
 *  @param phone The network interface layer module phone. Input parameter.
71
 *  @returns EOK on success.
75
 *  @returns EOK on success.
72
 *  @returns ENOENT if there is no such device.
76
 *  @returns ENOENT if there is no such device.
73
 *  @returns ELIMIT if there is another module registered.
77
 *  @returns ELIMIT if there is another module registered.
74
 */
78
 */
75
int register_message( device_id_t device_id, int phone );
79
int register_message( device_id_t device_id, int phone );
76
 
80
 
-
 
81
/*@}*/
-
 
82
 
77
int netif_probe_req( int netif_phone, device_id_t device_id, int irq, int io ){
83
int netif_probe_req( int netif_phone, device_id_t device_id, int irq, int io ){
78
    int result;
84
    int result;
79
 
85
 
80
    fibril_rwlock_write_lock( & netif_globals.lock );
86
    fibril_rwlock_write_lock( & netif_globals.lock );
81
    result = netif_probe_message( device_id, irq, io );
87
    result = netif_probe_message( device_id, irq, io );
82
    fibril_rwlock_write_unlock( & netif_globals.lock );
88
    fibril_rwlock_write_unlock( & netif_globals.lock );
83
    return result;
89
    return result;
84
}
90
}
85
 
91
 
86
int netif_send_msg( int netif_phone, device_id_t device_id, packet_t packet, services_t sender ){
92
int netif_send_msg( int netif_phone, device_id_t device_id, packet_t packet, services_t sender ){
87
    int result;
93
    int result;
88
 
94
 
89
    fibril_rwlock_write_lock( & netif_globals.lock );
95
    fibril_rwlock_write_lock( & netif_globals.lock );
90
    result = netif_send_message( device_id, packet, sender );
96
    result = netif_send_message( device_id, packet, sender );
91
    fibril_rwlock_write_unlock( & netif_globals.lock );
97
    fibril_rwlock_write_unlock( & netif_globals.lock );
92
    return result;
98
    return result;
93
}
99
}
94
 
100
 
95
int netif_start_req( int netif_phone, device_id_t device_id ){
101
int netif_start_req( int netif_phone, device_id_t device_id ){
96
    ERROR_DECLARE;
102
    ERROR_DECLARE;
97
 
103
 
98
    device_ref  device;
104
    device_ref  device;
99
    int result;
105
    int result;
100
    int phone;
106
    int phone;
101
 
107
 
102
    fibril_rwlock_write_lock( & netif_globals.lock );
108
    fibril_rwlock_write_lock( & netif_globals.lock );
103
    if( ERROR_OCCURRED( find_device( device_id, & device ))){
109
    if( ERROR_OCCURRED( find_device( device_id, & device ))){
104
        fibril_rwlock_write_unlock( & netif_globals.lock );
110
        fibril_rwlock_write_unlock( & netif_globals.lock );
105
        return ERROR_CODE;
111
        return ERROR_CODE;
106
    }
112
    }
107
    result = netif_start_message( device );
113
    result = netif_start_message( device );
108
    if( result > NETIF_NULL ){
114
    if( result > NETIF_NULL ){
109
        phone = device->nil_phone;
115
        phone = device->nil_phone;
110
        fibril_rwlock_write_unlock( & netif_globals.lock );
116
        fibril_rwlock_write_unlock( & netif_globals.lock );
111
        nil_device_state_msg( phone, device_id, result );
117
        nil_device_state_msg( phone, device_id, result );
112
        return EOK;
118
        return EOK;
113
    }else{
119
    }else{
114
        fibril_rwlock_write_unlock( & netif_globals.lock );
120
        fibril_rwlock_write_unlock( & netif_globals.lock );
115
    }
121
    }
116
    return result;
122
    return result;
117
}
123
}
118
 
124
 
119
int netif_stop_req( int netif_phone, device_id_t device_id ){
125
int netif_stop_req( int netif_phone, device_id_t device_id ){
120
    ERROR_DECLARE;
126
    ERROR_DECLARE;
121
 
127
 
122
    device_ref  device;
128
    device_ref  device;
123
    int result;
129
    int result;
124
    int phone;
130
    int phone;
125
 
131
 
126
    fibril_rwlock_write_lock( & netif_globals.lock );
132
    fibril_rwlock_write_lock( & netif_globals.lock );
127
    if( ERROR_OCCURRED( find_device( device_id, & device ))){
133
    if( ERROR_OCCURRED( find_device( device_id, & device ))){
128
        fibril_rwlock_write_unlock( & netif_globals.lock );
134
        fibril_rwlock_write_unlock( & netif_globals.lock );
129
        return ERROR_CODE;
135
        return ERROR_CODE;
130
    }
136
    }
131
    result = netif_stop_message( device );
137
    result = netif_stop_message( device );
132
    if( result > NETIF_NULL ){
138
    if( result > NETIF_NULL ){
133
        phone = device->nil_phone;
139
        phone = device->nil_phone;
134
        fibril_rwlock_write_unlock( & netif_globals.lock );
140
        fibril_rwlock_write_unlock( & netif_globals.lock );
135
        nil_device_state_msg( phone, device_id, result );
141
        nil_device_state_msg( phone, device_id, result );
136
        return EOK;
142
        return EOK;
137
    }else{
143
    }else{
138
        fibril_rwlock_write_unlock( & netif_globals.lock );
144
        fibril_rwlock_write_unlock( & netif_globals.lock );
139
    }
145
    }
140
    return result;
146
    return result;
141
}
147
}
142
 
148
 
-
 
149
int netif_stats_req( int netif_phone, device_id_t device_id, device_stats_ref stats ){
-
 
150
    int res;
-
 
151
 
-
 
152
    fibril_rwlock_read_lock( & netif_globals.lock );
-
 
153
    res = netif_get_device_stats( device_id, stats );
-
 
154
    fibril_rwlock_read_unlock( & netif_globals.lock );
-
 
155
    return res;
-
 
156
}
-
 
157
 
143
int netif_get_addr( int netif_phone, device_id_t device_id, measured_string_ref * address, char ** data ){
158
int netif_get_addr( int netif_phone, device_id_t device_id, measured_string_ref * address, char ** data ){
144
    ERROR_DECLARE;
159
    ERROR_DECLARE;
145
 
160
 
146
    measured_string_t   translation;
161
    measured_string_t   translation;
147
 
162
 
148
    if( !( address && data )) return EBADMEM;
163
    if( !( address && data )) return EBADMEM;
149
    fibril_rwlock_read_lock( & netif_globals.lock );
164
    fibril_rwlock_read_lock( & netif_globals.lock );
150
    if( ! ERROR_OCCURRED( netif_get_addr_message( device_id, & translation ))){
165
    if( ! ERROR_OCCURRED( netif_get_addr_message( device_id, & translation ))){
151
        * address = measured_string_copy( & translation );
166
        * address = measured_string_copy( & translation );
152
        ERROR_CODE = ( * address ) ? EOK : ENOMEM;
167
        ERROR_CODE = ( * address ) ? EOK : ENOMEM;
153
    }
168
    }
154
    fibril_rwlock_read_unlock( & netif_globals.lock );
169
    fibril_rwlock_read_unlock( & netif_globals.lock );
155
    * data = ( ** address ).value;
170
    * data = ( ** address ).value;
156
    return ERROR_CODE;
171
    return ERROR_CODE;
157
}
172
}
158
 
173
 
159
int netif_bind_service( services_t service, device_id_t device_id, services_t me, async_client_conn_t receiver ){
174
int netif_bind_service( services_t service, device_id_t device_id, services_t me, async_client_conn_t receiver ){
160
    return EOK;
175
    return EOK;
161
}
176
}
162
 
177
 
163
int find_device( device_id_t device_id, device_ref * device ){
178
int find_device( device_id_t device_id, device_ref * device ){
164
    if( ! device ) return EBADMEM;
179
    if( ! device ) return EBADMEM;
165
    * device = device_map_find( & netif_globals.device_map, device_id );
180
    * device = device_map_find( & netif_globals.device_map, device_id );
166
    if( ! * device ) return ENOENT;
181
    if( ! * device ) return ENOENT;
167
    if(( ** device ).state == NETIF_NULL ) return EPERM;
182
    if(( ** device ).state == NETIF_NULL ) return EPERM;
168
    return EOK;
183
    return EOK;
169
}
184
}
170
 
185
 
171
void null_device_stats( device_stats_ref stats ){
186
void null_device_stats( device_stats_ref stats ){
172
    bzero( stats, sizeof( device_stats_t ));
187
    bzero( stats, sizeof( device_stats_t ));
173
}
188
}
174
 
189
 
175
int register_message( device_id_t device_id, int phone ){
190
int register_message( device_id_t device_id, int phone ){
176
    ERROR_DECLARE;
191
    ERROR_DECLARE;
177
 
192
 
178
    device_ref  device;
193
    device_ref  device;
179
 
194
 
180
    ERROR_PROPAGATE( find_device( device_id, & device ));
195
    ERROR_PROPAGATE( find_device( device_id, & device ));
181
    if( device->nil_phone > 0 ) return ELIMIT;
196
    if( device->nil_phone > 0 ) return ELIMIT;
182
    device->nil_phone = phone;
197
    device->nil_phone = phone;
183
    printf( "New receiver of the device %d registered:\n\tphone\t= %d\n", device->device_id, device->nil_phone );
198
    printf( "New receiver of the device %d registered:\n\tphone\t= %d\n", device->device_id, device->nil_phone );
184
    return EOK;
199
    return EOK;
185
}
200
}
186
 
201
 
187
int netif_message( ipc_callid_t callid, ipc_call_t * call, ipc_call_t * answer, int * answer_count ){
202
int netif_message( ipc_callid_t callid, ipc_call_t * call, ipc_call_t * answer, int * answer_count ){
188
    ERROR_DECLARE;
203
    ERROR_DECLARE;
189
 
204
 
190
    size_t              length;
205
    size_t              length;
191
    device_stats_t      stats;
206
    device_stats_t      stats;
192
    packet_t            packet;
207
    packet_t            packet;
193
    measured_string_t   address;
208
    measured_string_t   address;
194
 
209
 
195
//  printf( "message %d - %d\n", IPC_GET_METHOD( * call ), NET_NETIF_FIRST );
210
//  printf( "message %d - %d\n", IPC_GET_METHOD( * call ), NET_NETIF_FIRST );
196
    * answer_count = 0;
211
    * answer_count = 0;
197
    switch( IPC_GET_METHOD( * call )){
212
    switch( IPC_GET_METHOD( * call )){
198
        case IPC_M_PHONE_HUNGUP:
213
        case IPC_M_PHONE_HUNGUP:
199
            return EOK;
214
            return EOK;
200
        case NET_NETIF_PROBE_AUTO:
-
 
201
            fibril_rwlock_write_lock( & netif_globals.lock );
-
 
202
            ERROR_CODE = netif_probe_auto_message();
-
 
203
            fibril_rwlock_write_unlock( & netif_globals.lock );
-
 
204
            return ERROR_CODE;
-
 
205
        case NET_NETIF_PROBE:
215
        case NET_NETIF_PROBE:
206
            return netif_probe_req( 0, IPC_GET_DEVICE( call ), NETIF_GET_IRQ( call ), NETIF_GET_IO( call ));
216
            return netif_probe_req( 0, IPC_GET_DEVICE( call ), NETIF_GET_IRQ( call ), NETIF_GET_IO( call ));
207
        case IPC_M_CONNECT_TO_ME:
217
        case IPC_M_CONNECT_TO_ME:
208
            fibril_rwlock_write_lock( & netif_globals.lock );
218
            fibril_rwlock_write_lock( & netif_globals.lock );
209
            ERROR_CODE = register_message( IPC_GET_DEVICE( call ), IPC_GET_PHONE( call ));
219
            ERROR_CODE = register_message( IPC_GET_DEVICE( call ), IPC_GET_PHONE( call ));
210
            fibril_rwlock_write_unlock( & netif_globals.lock );
220
            fibril_rwlock_write_unlock( & netif_globals.lock );
211
            return ERROR_CODE;
221
            return ERROR_CODE;
212
        case NET_NETIF_SEND:
222
        case NET_NETIF_SEND:
213
            ERROR_PROPAGATE( packet_translate( netif_globals.net_phone, & packet, IPC_GET_PACKET( call )));
223
            ERROR_PROPAGATE( packet_translate( netif_globals.net_phone, & packet, IPC_GET_PACKET( call )));
214
            return netif_send_msg( 0, IPC_GET_DEVICE( call ), packet, IPC_GET_SENDER( call ));
224
            return netif_send_msg( 0, IPC_GET_DEVICE( call ), packet, IPC_GET_SENDER( call ));
215
        case NET_NETIF_START:
225
        case NET_NETIF_START:
216
            return netif_start_req( 0, IPC_GET_DEVICE( call ));
226
            return netif_start_req( 0, IPC_GET_DEVICE( call ));
217
        case NET_NETIF_STATS:
227
        case NET_NETIF_STATS:
218
            fibril_rwlock_read_lock( & netif_globals.lock );
228
            fibril_rwlock_read_lock( & netif_globals.lock );
219
            if( ! ERROR_OCCURRED( ipc_data_read_receive( & callid, & length ))){
229
            if( ! ERROR_OCCURRED( ipc_data_read_receive( & callid, & length ))){
220
                if( length < sizeof( device_stats_t )){
230
                if( length < sizeof( device_stats_t )){
221
                    ERROR_CODE = EOVERFLOW;
231
                    ERROR_CODE = EOVERFLOW;
222
                }else{
232
                }else{
223
                    if( ! ERROR_OCCURRED( netif_get_device_stats( IPC_GET_DEVICE( call ), & stats ))){
233
                    if( ! ERROR_OCCURRED( netif_get_device_stats( IPC_GET_DEVICE( call ), & stats ))){
224
                        ERROR_CODE = ipc_data_read_finalize( callid, & stats, sizeof( device_stats_t ));
234
                        ERROR_CODE = ipc_data_read_finalize( callid, & stats, sizeof( device_stats_t ));
225
                    }
235
                    }
226
                }
236
                }
227
            }
237
            }
228
            fibril_rwlock_read_unlock( & netif_globals.lock );
238
            fibril_rwlock_read_unlock( & netif_globals.lock );
229
            return ERROR_CODE;
239
            return ERROR_CODE;
230
        case NET_NETIF_STOP:
240
        case NET_NETIF_STOP:
231
            return netif_stop_req( 0, IPC_GET_DEVICE( call ));
241
            return netif_stop_req( 0, IPC_GET_DEVICE( call ));
232
        case NET_NETIF_GET_ADDR:
242
        case NET_NETIF_GET_ADDR:
233
            fibril_rwlock_read_lock( & netif_globals.lock );
243
            fibril_rwlock_read_lock( & netif_globals.lock );
234
            if( ! ERROR_OCCURRED( netif_get_addr_message( IPC_GET_DEVICE( call ), & address ))){
244
            if( ! ERROR_OCCURRED( netif_get_addr_message( IPC_GET_DEVICE( call ), & address ))){
235
                ERROR_CODE = measured_strings_reply( & address, 1 );
245
                ERROR_CODE = measured_strings_reply( & address, 1 );
236
            }
246
            }
237
            fibril_rwlock_read_unlock( & netif_globals.lock );
247
            fibril_rwlock_read_unlock( & netif_globals.lock );
238
            return ERROR_CODE;
248
            return ERROR_CODE;
239
    }
249
    }
240
    return netif_specific_message( callid, call, answer, answer_count );
250
    return netif_specific_message( callid, call, answer, answer_count );
241
}
251
}
242
 
252
 
243
int netif_init_module( async_client_conn_t client_connection ){
253
int netif_init_module( async_client_conn_t client_connection ){
244
    ERROR_DECLARE;
254
    ERROR_DECLARE;
245
 
255
 
246
    async_set_client_connection( client_connection );
256
    async_set_client_connection( client_connection );
247
    netif_globals.net_phone = connect_to_service( SERVICE_NETWORKING );
257
    netif_globals.net_phone = connect_to_service( SERVICE_NETWORKING );
248
    device_map_initialize( & netif_globals.device_map );
258
    device_map_initialize( & netif_globals.device_map );
249
    ERROR_PROPAGATE( pm_init());
259
    ERROR_PROPAGATE( pm_init());
250
    fibril_rwlock_initialize( & netif_globals.lock );
260
    fibril_rwlock_initialize( & netif_globals.lock );
251
    if( ERROR_OCCURRED( netif_initialize())){
261
    if( ERROR_OCCURRED( netif_initialize())){
252
        pm_destroy();
262
        pm_destroy();
253
        return ERROR_CODE;
263
        return ERROR_CODE;
254
    }
264
    }
255
    return EOK;
265
    return EOK;
256
}
266
}
257
 
267
 
258
int netif_run_module( void ){
268
int netif_run_module( void ){
259
    async_manager();
269
    async_manager();
260
 
270
 
261
    pm_destroy();
271
    pm_destroy();
262
    return EOK;
272
    return EOK;
263
}
273
}
264
 
274
 
265
void netif_pq_release( packet_id_t packet_id ){
275
void netif_pq_release( packet_id_t packet_id ){
266
    pq_release( netif_globals.net_phone, packet_id );
276
    pq_release( netif_globals.net_phone, packet_id );
267
}
277
}
268
 
278
 
269
packet_t netif_packet_get_1( size_t content ){
279
packet_t netif_packet_get_1( size_t content ){
270
    return packet_get_1( netif_globals.net_phone, content );
280
    return packet_get_1( netif_globals.net_phone, content );
271
}
281
}
272
 
282
 
273
/** @}
283
/** @}
274
 */
284
 */
275
 
285