Subversion Repositories HelenOS

Rev

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

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