Subversion Repositories HelenOS

Rev

Rev 4699 | Go to most recent revision | Details | Last modification | View Log | RSS feed

Rev Author Line No. Line
4696 mejdrech 1
/*
2
 * Copyright (c) 2009 Lukas Mejdrech
3
 * All rights reserved.
4
 *
5
 * Redistribution and use in source and binary forms, with or without
6
 * modification, are permitted provided that the following conditions
7
 * are met:
8
 *
9
 * - Redistributions of source code must retain the above copyright
10
 *   notice, this list of conditions and the following disclaimer.
11
 * - Redistributions in binary form must reproduce the above copyright
12
 *   notice, this list of conditions and the following disclaimer in the
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
15
 *   derived from this software without specific prior written permission.
16
 *
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
19
 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
20
 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
21
 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
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
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
26
 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
27
 */
28
 
29
/** @addtogroup nildummy
30
 *  @{
31
 */
32
 
33
/** @file
34
 *  Dummy network interface layer module implementation.
35
 *  @see nildummy.h
36
 */
37
 
38
#include <async.h>
39
#include <malloc.h>
40
#include <mem.h>
41
#include <stdio.h>
42
#include <string.h>
43
 
44
#include <ipc/ipc.h>
45
#include <ipc/services.h>
46
 
47
#include "../../err.h"
48
#include "../../messages.h"
49
#include "../../modules.h"
50
 
51
#include "../../include/device.h"
52
#include "../../include/netif_interface.h"
53
#include "../../include/nil_interface.h"
54
#include "../../include/il_interface.h"
55
 
56
#include "../../structures/measured_strings.h"
57
#include "../../structures/packet/packet.h"
58
 
59
#include "../nil_module.h"
60
 
61
#include "nildummy.h"
62
 
63
/** Default maximum transmission unit.
64
 */
65
#define DEFAULT_MTU 1500
66
 
67
/** Network interface layer module global data.
68
 */
69
nildummy_globals_t  nildummy_globals;
70
 
71
/** Processes IPC messages from the registered device driver modules in an infinite loop.
72
 *  @param iid The message identifier. Input parameter.
73
 *  @param icall The message parameters. Input/output parameter.
74
 */
75
void    nildummy_receiver( ipc_callid_t iid, ipc_call_t * icall );
76
 
77
/** Registers new device or updates the MTU of an existing one.
78
 *  Determines the device local hardware address.
79
 *  @param device_id The new device identifier. Input parameter.
80
 *  @param service The device driver service. Input parameter.
81
 *  @param mtu The device maximum transmission unit. Input parameter.
82
 *  @returns EOK on success.
83
 *  @returns EEXIST if the device with the different service exists.
84
 *  @returns ENOMEM if there is not enough memory left.
85
 *  @returns Other error codes as defined for the netif_bind_service() function.
86
 *  @returns Other error codes as defined for the netif_get_addr() function.
87
 */
88
int nildummy_device_message( device_id_t device_id, services_t service, size_t mtu );
89
 
90
/** Returns the device packet dimensions for sending.
91
 *  @param device_id The device identifier. Input parameter.
92
 *  @param addr_len The minimum reserved address length. Output parameter.
93
 *  @param prefix The minimum reserved prefix size. Output parameter.
94
 *  @param content The maximum content size. Output parameter.
95
 *  @param suffix The minimum reserved suffix size. Output parameter.
96
 *  @returns EOK on success.
97
 *  @returns EBADMEM if either one of the parameters is NULL.
98
 *  @returns ENOENT if there is no such device.
99
 */
100
int nildummy_packet_space_message( device_id_t device_id, size_t * addr_len, size_t * prefix, size_t * content, size_t * suffix );
101
 
102
/** Registers receiving module service.
103
 *  Passes received packets for this service.
104
 *  @param service The module service. Input parameter.
105
 *  @param phone The service phone. Input parameter.
106
 *  @returns EOK on success.
107
 *  @returns ENOENT if the service is not known.
108
 *  @returns ENOMEM if there is not enough memory left.
109
 */
110
int nildummy_register_message( services_t service, int phone );
111
 
112
/** Sends the packet queue.
113
 *  @param device_id The device identifier. Input parameter.
114
 *  @param packet The packet queue. Input parameter.
115
 *  @param sender The sending module service. Input parameter.
116
 *  @returns EOK on success.
117
 *  @returns ENOENT if there no such device.
118
 *  @returns EINVAL if the service parameter is not known.
119
 */
120
int nildummy_send_message( device_id_t device_id, packet_t packet, services_t sender );
121
 
122
/** Returns the device hardware address.
123
 *  @param device_id The device identifier. Input parameter.
124
 *  @param type Type of the desired address. Input parameter
125
 *  @param address The device hardware address. Output parameter.
126
 *  @returns EOK on success.
127
 *  @returns EBADMEM if the address parameter is NULL.
128
 *  @returns ENOENT if there no such device.
129
 */
130
int nildummy_addr_message( device_id_t device_id, measured_string_ref * address );
131
 
132
DEVICE_MAP_IMPLEMENT( nildummy_devices, nildummy_device_t )
133
 
134
int nil_device_state_msg( int nil_phone, device_id_t device_id, int state ){
135
    fibril_rwlock_read_lock( & nildummy_globals.protos_lock );
136
    if( nildummy_globals.proto.phone ) il_device_state_msg( nildummy_globals.proto.phone, device_id, state, nildummy_globals.proto.service );
137
    fibril_rwlock_read_unlock( & nildummy_globals.protos_lock );
138
    return EOK;
139
}
140
 
141
int nil_initialize( int net_phone ){
142
    ERROR_DECLARE;
143
 
144
    fibril_rwlock_initialize( & nildummy_globals.devices_lock );
145
    fibril_rwlock_initialize( & nildummy_globals.protos_lock );
146
    fibril_rwlock_write_lock( & nildummy_globals.devices_lock );
147
    fibril_rwlock_write_lock( & nildummy_globals.protos_lock );
148
    nildummy_globals.net_phone = net_phone;
149
    nildummy_globals.proto.phone = 0;
150
    ERROR_PROPAGATE( nildummy_devices_initialize( & nildummy_globals.devices ));
151
    fibril_rwlock_write_unlock( & nildummy_globals.protos_lock );
152
    fibril_rwlock_write_unlock( & nildummy_globals.devices_lock );
153
    return EOK;
154
}
155
 
156
int nildummy_device_message( device_id_t device_id, services_t service, size_t mtu ){
157
    ERROR_DECLARE;
158
 
159
    nildummy_device_ref device;
160
    int                 index;
161
 
162
    fibril_rwlock_write_lock( & nildummy_globals.devices_lock );
163
    // an existing device?
164
    device = nildummy_devices_find( & nildummy_globals.devices, device_id );
165
    if( device ){
166
        if( device->service != service ){
167
            printf( "Device %d already exists\n", device->device_id );
168
            fibril_rwlock_write_unlock( & nildummy_globals.devices_lock );
169
            return EEXIST;
170
        }else{
171
            // update mtu
172
            device->mtu = mtu;
173
            printf( "Device %d already exists:\tMTU\t= %d\n", device->device_id, device->mtu );
174
            fibril_rwlock_write_unlock( & nildummy_globals.devices_lock );
175
            // notify the upper layer module
176
            fibril_rwlock_read_lock( & nildummy_globals.protos_lock );
177
            if( nildummy_globals.proto.phone ){
178
                il_mtu_changed_msg( nildummy_globals.proto.phone, device->device_id, device->mtu, nildummy_globals.proto.service );
179
            }
180
            fibril_rwlock_read_unlock( & nildummy_globals.protos_lock );
181
            return EOK;
182
        }
183
    }else{
184
        // create a new device
185
        device = ( nildummy_device_ref ) malloc( sizeof( nildummy_device_t ));
186
        if( ! device ) return ENOMEM;
187
        device->device_id = device_id;
188
        device->service = service;
189
        if( mtu > 0 ){
190
            device->mtu = mtu;
191
        }else{
192
            device->mtu = DEFAULT_MTU;
193
        }
194
        // bind the device driver
195
        device->phone = netif_bind_service( device->service, device->device_id, SERVICE_ETHERNET, nildummy_receiver );
196
        if( device->phone < 0 ){
197
            fibril_rwlock_write_unlock( & nildummy_globals.devices_lock );
198
            free( device );
199
            return device->phone;
200
        }
201
        // get hardware address
202
        if( ERROR_OCCURRED( netif_get_addr( device->phone, device->device_id, & device->addr, & device->addr_data ))){
203
            fibril_rwlock_write_unlock( & nildummy_globals.devices_lock );
204
            free( device );
205
            return ERROR_CODE;
206
        }
207
        // add to the cache
208
        index = nildummy_devices_add( & nildummy_globals.devices, device->device_id, device );
209
        if( index < 0 ){
210
            fibril_rwlock_write_unlock( & nildummy_globals.devices_lock );
211
            free( device->addr );
212
            free( device->addr_data );
213
            free( device );
214
            return index;
215
        }
216
        printf( "New device registered:\n\tid\t= %d\n\tservice\t= %d\n\tMTU\t= %d\n", device->device_id, device->service, device->mtu );
217
    }
218
    fibril_rwlock_write_unlock( & nildummy_globals.devices_lock );
219
    return EOK;
220
}
221
 
222
int nildummy_addr_message( device_id_t device_id, measured_string_ref * address ){
223
    nildummy_device_ref device;
224
 
225
    if( ! address ) return EBADMEM;
226
    fibril_rwlock_read_lock( & nildummy_globals.devices_lock );
227
    device = nildummy_devices_find( & nildummy_globals.devices, device_id );
228
    if( ! device ){
229
        fibril_rwlock_read_unlock( & nildummy_globals.devices_lock );
230
        return ENOENT;
231
    }
232
    * address = device->addr;
233
    fibril_rwlock_read_unlock( & nildummy_globals.devices_lock );
234
    return ( * address ) ? EOK : ENOENT;
235
}
236
 
237
int nildummy_packet_space_message( device_id_t device_id, size_t * addr_len, size_t * prefix, size_t * content, size_t * suffix ){
238
    nildummy_device_ref device;
239
 
240
    if( !( addr_len && prefix && content && suffix )) return EBADMEM;
241
    fibril_rwlock_read_lock( & nildummy_globals.devices_lock );
242
    device = nildummy_devices_find( & nildummy_globals.devices, device_id );
243
    if( ! device ){
244
        fibril_rwlock_read_unlock( & nildummy_globals.devices_lock );
245
        return ENOENT;
246
    }
247
    * content = device->mtu;
248
    fibril_rwlock_read_unlock( & nildummy_globals.devices_lock );
249
    * addr_len = 0;
250
    * prefix = 0;
251
    * suffix = 0;
252
    return EOK;
253
}
254
 
255
int nil_received_msg( int nil_phone, device_id_t device_id, packet_t packet, services_t target ){
256
    packet_t        next;
257
 
258
    fibril_rwlock_read_lock( & nildummy_globals.protos_lock );
259
    if( nildummy_globals.proto.phone ){
260
        do{
261
            next = pq_detach( packet );
262
            il_received_msg( nildummy_globals.proto.phone, device_id, packet, nildummy_globals.proto.service );
263
            packet = next;
264
        }while( packet );
265
    }
266
    fibril_rwlock_read_unlock( & nildummy_globals.protos_lock );
267
    return EOK;
268
}
269
 
270
int nildummy_register_message( services_t service, int phone ){
271
    fibril_rwlock_write_lock( & nildummy_globals.protos_lock );
272
    nildummy_globals.proto.service = service;
273
    nildummy_globals.proto.phone = phone;
274
    printf( "New protocol registered:\n\tservice\t= %d\n\tphone\t= %d\n", nildummy_globals.proto.service, nildummy_globals.proto.phone );
275
    fibril_rwlock_write_unlock( & nildummy_globals.protos_lock );
276
    return EOK;
277
}
278
 
279
int nildummy_send_message( device_id_t device_id, packet_t packet, services_t sender ){
280
    nildummy_device_ref     device;
281
 
282
    fibril_rwlock_read_lock( & nildummy_globals.devices_lock );
283
    device = nildummy_devices_find( & nildummy_globals.devices, device_id );
284
    if( ! device ){
285
        fibril_rwlock_read_unlock( & nildummy_globals.devices_lock );
286
        return ENOENT;
287
    }
288
    // send packet queue
289
    if( packet ){
290
        netif_send_msg( device->phone, device_id, packet, SERVICE_NILDUMMY );
291
    }
292
    fibril_rwlock_read_unlock( & nildummy_globals.devices_lock );
293
    return EOK;
294
}
295
 
296
int nil_message( ipc_callid_t callid, ipc_call_t * call, ipc_call_t * answer, int * answer_count ){
297
    ERROR_DECLARE;
298
 
299
    measured_string_ref address;
300
    packet_t            packet;
301
 
302
//  printf( "message %d - %d\n", IPC_GET_METHOD( * call ), NET_NIL_FIRST );
303
    * answer_count = 0;
304
    switch( IPC_GET_METHOD( * call )){
305
        case IPC_M_PHONE_HUNGUP:
306
            return EOK;
307
        case NET_NIL_DEVICE:
308
            return nildummy_device_message( IPC_GET_DEVICE( call ), IPC_GET_SERVICE( call ), IPC_GET_MTU( call ));
309
        case NET_NIL_SEND:
310
            ERROR_PROPAGATE( packet_translate( nildummy_globals.net_phone, & packet, IPC_GET_PACKET( call )));
311
            return nildummy_send_message( IPC_GET_DEVICE( call ), packet, IPC_GET_SERVICE( call ));
312
        case NET_NIL_PACKET_SPACE:
313
            ERROR_PROPAGATE( nildummy_packet_space_message( IPC_GET_DEVICE( call ), IPC_SET_ADDR( answer ), IPC_SET_PREFIX( answer ), IPC_SET_CONTENT( answer ), IPC_SET_SUFFIX( answer )));
314
            * answer_count = 4;
315
            return EOK;
316
        case NET_NIL_ADDR:
317
            ERROR_PROPAGATE( nildummy_addr_message( IPC_GET_DEVICE( call ), & address ));
318
            return measured_strings_reply( address, 1 );
319
        case IPC_M_CONNECT_TO_ME:
320
            return nildummy_register_message( NIL_GET_PROTO( call ), IPC_GET_PHONE( call ));
321
    }
322
    return ENOTSUP;
323
}
324
 
325
void nildummy_receiver( ipc_callid_t iid, ipc_call_t * icall ){
326
    ERROR_DECLARE;
327
 
328
    packet_t        packet;
329
 
330
    while( true ){
331
//      printf( "message %d - %d\n", IPC_GET_METHOD( * icall ), NET_NIL_FIRST );
332
        switch( IPC_GET_METHOD( * icall )){
333
            case NET_NIL_DEVICE_STATE:
334
                ERROR_CODE = nil_device_state_msg( 0, IPC_GET_DEVICE( icall ), IPC_GET_STATE( icall ));
335
                ipc_answer_0( iid, ERROR_CODE );
336
                break;
337
            case NET_NIL_RECEIVED:
338
                if( ! ERROR_OCCURRED( packet_translate( nildummy_globals.net_phone, & packet, IPC_GET_PACKET( icall )))){
339
                    ERROR_CODE = nil_received_msg( 0, IPC_GET_DEVICE( icall ), packet, 0 );
340
                }
341
                ipc_answer_0( iid, ( ipcarg_t ) ERROR_CODE );
342
                break;
343
            default:
344
                ipc_answer_0( iid, ( ipcarg_t ) ENOTSUP );
345
        }
346
        iid = async_get_call( icall );
347
    }
348
}
349
 
350
/** @}
351
 */