Subversion Repositories HelenOS

Rev

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

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