Subversion Repositories HelenOS

Rev

Rev 4558 | Rev 4704 | Go to most recent revision | Show entire file | Ignore whitespace | Details | Blame | Last modification | View Log | RSS feed

Rev 4558 Rev 4582
Line 37... Line 37...
37
 
37
 
38
#include <async.h>
38
#include <async.h>
39
#include <malloc.h>
39
#include <malloc.h>
40
#include <mem.h>
40
#include <mem.h>
41
#include <stdio.h>
41
#include <stdio.h>
-
 
42
#include <string.h>
42
 
43
 
43
#include <ipc/ipc.h>
44
#include <ipc/ipc.h>
44
#include <ipc/services.h>
45
#include <ipc/services.h>
45
 
46
 
46
#include "../../err.h"
47
#include "../../err.h"
Line 265... Line 266...
265
int nil_device_state_msg( int nil_phone, device_id_t device_id, int state ){
266
int nil_device_state_msg( int nil_phone, device_id_t device_id, int state ){
266
    int             index;
267
    int             index;
267
    eth_proto_ref   proto;
268
    eth_proto_ref   proto;
268
 
269
 
269
    //TODO clear device if off?
270
    //TODO clear device if off?
270
    rwlock_read_lock( & eth_globals.protos_lock );
271
    fibril_rwlock_read_lock( & eth_globals.protos_lock );
271
    for( index = eth_protos_count( & eth_globals.protos ) - 1; index >= 0; -- index ){
272
    for( index = eth_protos_count( & eth_globals.protos ) - 1; index >= 0; -- index ){
272
        proto = eth_protos_get_index( & eth_globals.protos, index );
273
        proto = eth_protos_get_index( & eth_globals.protos, index );
273
        if( proto && proto->phone ) il_device_state_msg( proto->phone, device_id, state, proto->service );
274
        if( proto && proto->phone ) il_device_state_msg( proto->phone, device_id, state, proto->service );
274
    }
275
    }
275
    rwlock_read_unlock( & eth_globals.protos_lock );
276
    fibril_rwlock_read_unlock( & eth_globals.protos_lock );
276
    return EOK;
277
    return EOK;
277
}
278
}
278
 
279
 
279
int nil_initialize( int net_phone ){
280
int nil_initialize( int net_phone ){
280
    ERROR_DECLARE;
281
    ERROR_DECLARE;
281
 
282
 
282
    rwlock_initialize( & eth_globals.devices_lock );
283
    fibril_rwlock_initialize( & eth_globals.devices_lock );
283
    rwlock_initialize( & eth_globals.protos_lock );
284
    fibril_rwlock_initialize( & eth_globals.protos_lock );
284
    rwlock_write_lock( & eth_globals.devices_lock );
285
    fibril_rwlock_write_lock( & eth_globals.devices_lock );
285
    rwlock_write_lock( & eth_globals.protos_lock );
286
    fibril_rwlock_write_lock( & eth_globals.protos_lock );
286
    eth_globals.net_phone = net_phone;
287
    eth_globals.net_phone = net_phone;
287
    eth_globals.broadcast_addr = measured_string_create_bulk( "\xFF\xFF\xFF\xFF\xFF\xFF", CONVERT_SIZE( uint8_t, char, ETH_ADDR ));
288
    eth_globals.broadcast_addr = measured_string_create_bulk( "\xFF\xFF\xFF\xFF\xFF\xFF", CONVERT_SIZE( uint8_t, char, ETH_ADDR ));
288
    if( ! eth_globals.broadcast_addr ) return ENOMEM;
289
    if( ! eth_globals.broadcast_addr ) return ENOMEM;
289
    ERROR_PROPAGATE( eth_devices_initialize( & eth_globals.devices ));
290
    ERROR_PROPAGATE( eth_devices_initialize( & eth_globals.devices ));
290
    if( ERROR_OCCURRED( eth_protos_initialize( & eth_globals.protos ))){
291
    if( ERROR_OCCURRED( eth_protos_initialize( & eth_globals.protos ))){
291
        eth_devices_destroy( & eth_globals.devices );
292
        eth_devices_destroy( & eth_globals.devices );
292
        return ERROR_CODE;
293
        return ERROR_CODE;
293
    }
294
    }
294
    rwlock_write_unlock( & eth_globals.protos_lock );
295
    fibril_rwlock_write_unlock( & eth_globals.protos_lock );
295
    rwlock_write_unlock( & eth_globals.devices_lock );
296
    fibril_rwlock_write_unlock( & eth_globals.devices_lock );
296
    return EOK;
297
    return EOK;
297
}
298
}
298
 
299
 
299
int eth_device_message( device_id_t device_id, services_t service, size_t mtu ){
300
int eth_device_message( device_id_t device_id, services_t service, size_t mtu ){
300
    ERROR_DECLARE;
301
    ERROR_DECLARE;
Line 304... Line 305...
304
    measured_string_t   names[ 2 ] = {{ "ETH_MODE", 8 }, { "ETH_DUMMY", 9 }};
305
    measured_string_t   names[ 2 ] = {{ "ETH_MODE", 8 }, { "ETH_DUMMY", 9 }};
305
    measured_string_ref configuration;
306
    measured_string_ref configuration;
306
    size_t              count = sizeof( names ) / sizeof( measured_string_t );
307
    size_t              count = sizeof( names ) / sizeof( measured_string_t );
307
    char *              data;
308
    char *              data;
308
 
309
 
309
    rwlock_write_lock( & eth_globals.devices_lock );
310
    fibril_rwlock_write_lock( & eth_globals.devices_lock );
310
    // an existing device?
311
    // an existing device?
311
    device = eth_devices_find( & eth_globals.devices, device_id );
312
    device = eth_devices_find( & eth_globals.devices, device_id );
312
    if( device ){
313
    if( device ){
313
        if( device->service != service ){
314
        if( device->service != service ){
314
            printf( "Device %d already exists\n", device->device_id );
315
            printf( "Device %d already exists\n", device->device_id );
315
            rwlock_write_unlock( & eth_globals.devices_lock );
316
            fibril_rwlock_write_unlock( & eth_globals.devices_lock );
316
            return EEXIST;
317
            return EEXIST;
317
        }else{
318
        }else{
318
            // update mtu
319
            // update mtu
319
            device->mtu = mtu;
320
            device->mtu = mtu;
320
            printf( "Device %d already exists:\tMTU\t= %d\n", device->device_id, device->mtu );
321
            printf( "Device %d already exists:\tMTU\t= %d\n", device->device_id, device->mtu );
Line 327... Line 328...
327
        device->service = service;
328
        device->service = service;
328
        device->flags = 0;
329
        device->flags = 0;
329
        device->mtu = (( mtu > 0 ) && ( mtu <= ETH_MAX_TAGGED_CONTENT( device->flags ))) ? mtu : ETH_MAX_TAGGED_CONTENT( device->flags );
330
        device->mtu = (( mtu > 0 ) && ( mtu <= ETH_MAX_TAGGED_CONTENT( device->flags ))) ? mtu : ETH_MAX_TAGGED_CONTENT( device->flags );
330
        configuration = & names[ 0 ];
331
        configuration = & names[ 0 ];
331
        if( ERROR_OCCURRED( net_get_device_conf_req( eth_globals.net_phone, device->device_id, & configuration, count, & data ))){
332
        if( ERROR_OCCURRED( net_get_device_conf_req( eth_globals.net_phone, device->device_id, & configuration, count, & data ))){
332
            rwlock_write_unlock( & eth_globals.devices_lock );
333
            fibril_rwlock_write_unlock( & eth_globals.devices_lock );
333
            free( device );
334
            free( device );
334
            return ERROR_CODE;
335
            return ERROR_CODE;
335
        }
336
        }
336
        if( configuration ){
337
        if( configuration ){
337
            if( ! str_lcmp( configuration[ 0 ].value, "DIX", configuration[ 0 ].length )){
338
            if( ! str_lcmp( configuration[ 0 ].value, "DIX", configuration[ 0 ].length )){
Line 349... Line 350...
349
            device->flags |= ETH_8023_2_SNAP;
350
            device->flags |= ETH_8023_2_SNAP;
350
        }
351
        }
351
        // bind the device driver
352
        // bind the device driver
352
        device->phone = netif_bind_service( device->service, device->device_id, SERVICE_ETHERNET, eth_receiver );
353
        device->phone = netif_bind_service( device->service, device->device_id, SERVICE_ETHERNET, eth_receiver );
353
        if( device->phone < 0 ){
354
        if( device->phone < 0 ){
354
            rwlock_write_unlock( & eth_globals.devices_lock );
355
            fibril_rwlock_write_unlock( & eth_globals.devices_lock );
355
            free( device );
356
            free( device );
356
            return device->phone;
357
            return device->phone;
357
        }
358
        }
358
        // get hardware address
359
        // get hardware address
359
        if( ERROR_OCCURRED( netif_get_addr( device->phone, device->device_id, & device->addr, & device->addr_data ))){
360
        if( ERROR_OCCURRED( netif_get_addr( device->phone, device->device_id, & device->addr, & device->addr_data ))){
360
            rwlock_write_unlock( & eth_globals.devices_lock );
361
            fibril_rwlock_write_unlock( & eth_globals.devices_lock );
361
            free( device );
362
            free( device );
362
            return ERROR_CODE;
363
            return ERROR_CODE;
363
        }
364
        }
364
        // add to the cache
365
        // add to the cache
365
        index = eth_devices_add( & eth_globals.devices, device->device_id, device );
366
        index = eth_devices_add( & eth_globals.devices, device->device_id, device );
366
        if( index < 0 ){
367
        if( index < 0 ){
367
            rwlock_write_unlock( & eth_globals.devices_lock );
368
            fibril_rwlock_write_unlock( & eth_globals.devices_lock );
368
            free( device->addr );
369
            free( device->addr );
369
            free( device->addr_data );
370
            free( device->addr_data );
370
            free( device );
371
            free( device );
371
            return index;
372
            return index;
372
        }
373
        }
373
        printf( "New device registered:\n\tid\t= %d\n\tservice\t= %d\n\tMTU\t= %d\n\taddress\t= %X:%X:%X:%X:%X:%X\n\tflags\t= 0x%x\n", device->device_id, device->service, device->mtu, device->addr_data[ 0 ], device->addr_data[ 1 ], device->addr_data[ 2 ], device->addr_data[ 3 ], device->addr_data[ 4 ], device->addr_data[ 5 ], device->flags );
374
        printf( "New device registered:\n\tid\t= %d\n\tservice\t= %d\n\tMTU\t= %d\n\taddress\t= %X:%X:%X:%X:%X:%X\n\tflags\t= 0x%x\n", device->device_id, device->service, device->mtu, device->addr_data[ 0 ], device->addr_data[ 1 ], device->addr_data[ 2 ], device->addr_data[ 3 ], device->addr_data[ 4 ], device->addr_data[ 5 ], device->flags );
374
    }
375
    }
375
    rwlock_write_unlock( & eth_globals.devices_lock );
376
    fibril_rwlock_write_unlock( & eth_globals.devices_lock );
376
    return EOK;
377
    return EOK;
377
}
378
}
378
 
379
 
379
eth_proto_ref eth_process_packet( int flags, packet_t packet ){
380
eth_proto_ref eth_process_packet( int flags, packet_t packet ){
380
    ERROR_DECLARE;
381
    ERROR_DECLARE;
Line 442... Line 443...
442
    eth_proto_ref   proto;
443
    eth_proto_ref   proto;
443
    packet_t        next;
444
    packet_t        next;
444
    eth_device_ref  device;
445
    eth_device_ref  device;
445
    int             flags;
446
    int             flags;
446
 
447
 
447
    rwlock_read_lock( & eth_globals.devices_lock );
448
    fibril_rwlock_read_lock( & eth_globals.devices_lock );
448
    device = eth_devices_find( & eth_globals.devices, device_id );
449
    device = eth_devices_find( & eth_globals.devices, device_id );
449
    if( ! device ){
450
    if( ! device ){
450
        rwlock_read_unlock( & eth_globals.devices_lock );
451
        fibril_rwlock_read_unlock( & eth_globals.devices_lock );
451
        return ENOENT;
452
        return ENOENT;
452
    }
453
    }
453
    flags = device->flags;
454
    flags = device->flags;
454
    rwlock_read_unlock( & eth_globals.devices_lock );
455
    fibril_rwlock_read_unlock( & eth_globals.devices_lock );
455
    rwlock_read_lock( & eth_globals.protos_lock );
456
    fibril_rwlock_read_lock( & eth_globals.protos_lock );
456
    do{
457
    do{
457
        next = pq_detach( packet );
458
        next = pq_detach( packet );
458
        proto = eth_process_packet( flags, packet );
459
        proto = eth_process_packet( flags, packet );
459
        if( proto ){
460
        if( proto ){
460
            il_received_msg( proto->phone, device_id, packet, proto->service );
461
            il_received_msg( proto->phone, device_id, packet, proto->service );
Line 462... Line 463...
462
            // drop invalid/unknown
463
            // drop invalid/unknown
463
            pq_release( eth_globals.net_phone, packet_get_id( packet ));
464
            pq_release( eth_globals.net_phone, packet_get_id( packet ));
464
        }
465
        }
465
        packet = next;
466
        packet = next;
466
    }while( packet );
467
    }while( packet );
467
    rwlock_read_unlock( & eth_globals.protos_lock );
468
    fibril_rwlock_read_unlock( & eth_globals.protos_lock );
468
    return EOK;
469
    return EOK;
469
}
470
}
470
 
471
 
471
int eth_packet_space_message( device_id_t device_id, size_t * addr_len, size_t * prefix, size_t * content, size_t * suffix ){
472
int eth_packet_space_message( device_id_t device_id, size_t * addr_len, size_t * prefix, size_t * content, size_t * suffix ){
472
    eth_device_ref  device;
473
    eth_device_ref  device;
473
 
474
 
474
    if( !( addr_len && prefix && content && suffix )) return EBADMEM;
475
    if( !( addr_len && prefix && content && suffix )) return EBADMEM;
475
    rwlock_read_lock( & eth_globals.devices_lock );
476
    fibril_rwlock_read_lock( & eth_globals.devices_lock );
476
    device = eth_devices_find( & eth_globals.devices, device_id );
477
    device = eth_devices_find( & eth_globals.devices, device_id );
477
    if( ! device ){
478
    if( ! device ){
478
        rwlock_read_unlock( & eth_globals.devices_lock );
479
        fibril_rwlock_read_unlock( & eth_globals.devices_lock );
479
        return ENOENT;
480
        return ENOENT;
480
    }
481
    }
481
    * content = device->mtu;
482
    * content = device->mtu;
482
    rwlock_read_unlock( & eth_globals.devices_lock );
483
    fibril_rwlock_read_unlock( & eth_globals.devices_lock );
483
    * addr_len = ETH_ADDR;
484
    * addr_len = ETH_ADDR;
484
    * prefix = ETH_PREFIX;
485
    * prefix = ETH_PREFIX;
485
    * suffix = ETH_MIN_CONTENT + ETH_SUFFIX;
486
    * suffix = ETH_MIN_CONTENT + ETH_SUFFIX;
486
    return EOK;
487
    return EOK;
487
}
488
}
Line 491... Line 492...
491
 
492
 
492
    if( ! address ) return EBADMEM;
493
    if( ! address ) return EBADMEM;
493
    if( type == ETH_BROADCAST_ADDR ){
494
    if( type == ETH_BROADCAST_ADDR ){
494
        * address = eth_globals.broadcast_addr;
495
        * address = eth_globals.broadcast_addr;
495
    }else{
496
    }else{
496
        rwlock_read_lock( & eth_globals.devices_lock );
497
        fibril_rwlock_read_lock( & eth_globals.devices_lock );
497
        device = eth_devices_find( & eth_globals.devices, device_id );
498
        device = eth_devices_find( & eth_globals.devices, device_id );
498
        if( ! device ){
499
        if( ! device ){
499
            rwlock_read_unlock( & eth_globals.devices_lock );
500
            fibril_rwlock_read_unlock( & eth_globals.devices_lock );
500
            return ENOENT;
501
            return ENOENT;
501
        }
502
        }
502
        * address = device->addr;
503
        * address = device->addr;
503
        rwlock_read_unlock( & eth_globals.devices_lock );
504
        fibril_rwlock_read_unlock( & eth_globals.devices_lock );
504
    }
505
    }
505
    return ( * address ) ? EOK : ENOENT;
506
    return ( * address ) ? EOK : ENOENT;
506
}
507
}
507
 
508
 
508
int nil_register_message( services_t service, int phone ){
509
int nil_register_message( services_t service, int phone ){
Line 510... Line 511...
510
    int             protocol;
511
    int             protocol;
511
    int             index;
512
    int             index;
512
 
513
 
513
    protocol = protocol_map( SERVICE_ETHERNET, service );
514
    protocol = protocol_map( SERVICE_ETHERNET, service );
514
    if( ! protocol ) return ENOENT;
515
    if( ! protocol ) return ENOENT;
515
    rwlock_write_lock( & eth_globals.protos_lock );
516
    fibril_rwlock_write_lock( & eth_globals.protos_lock );
516
    proto = eth_protos_find( & eth_globals.protos, protocol );
517
    proto = eth_protos_find( & eth_globals.protos, protocol );
517
    if( proto ){
518
    if( proto ){
518
        proto->phone = phone;
519
        proto->phone = phone;
519
        rwlock_write_unlock( & eth_globals.protos_lock );
520
        fibril_rwlock_write_unlock( & eth_globals.protos_lock );
520
        return EOK;
521
        return EOK;
521
    }else{
522
    }else{
522
        proto = ( eth_proto_ref ) malloc( sizeof( eth_proto_t ));
523
        proto = ( eth_proto_ref ) malloc( sizeof( eth_proto_t ));
523
        if( ! proto ){
524
        if( ! proto ){
524
            rwlock_write_unlock( & eth_globals.protos_lock );
525
            fibril_rwlock_write_unlock( & eth_globals.protos_lock );
525
            return ENOMEM;
526
            return ENOMEM;
526
        }
527
        }
527
        proto->service = service;
528
        proto->service = service;
528
        proto->protocol = protocol;
529
        proto->protocol = protocol;
529
        proto->phone = phone;
530
        proto->phone = phone;
530
        index = eth_protos_add( & eth_globals.protos, protocol, proto );
531
        index = eth_protos_add( & eth_globals.protos, protocol, proto );
531
        if( index < 0 ){
532
        if( index < 0 ){
532
            rwlock_write_unlock( & eth_globals.protos_lock );
533
            fibril_rwlock_write_unlock( & eth_globals.protos_lock );
533
            free( proto );
534
            free( proto );
534
            return index;
535
            return index;
535
        }
536
        }
536
    }
537
    }
537
    printf( "New protocol registered:\n\tprotocol\t= 0x%x\n\tservice\t= %d\n\tphone\t= %d\n", proto->protocol, proto->service, proto->phone );
538
    printf( "New protocol registered:\n\tprotocol\t= 0x%x\n\tservice\t= %d\n\tphone\t= %d\n", proto->protocol, proto->service, proto->phone );
538
    rwlock_write_unlock( & eth_globals.protos_lock );
539
    fibril_rwlock_write_unlock( & eth_globals.protos_lock );
539
    return EOK;
540
    return EOK;
540
}
541
}
541
 
542
 
542
int eth_prepare_packet( int flags, packet_t packet, uint8_t * src_addr, int ethertype, size_t mtu ){
543
int eth_prepare_packet( int flags, packet_t packet, uint8_t * src_addr, int ethertype, size_t mtu ){
543
    eth_header_ex_ref   header;
544
    eth_header_ex_ref   header;
Line 606... Line 607...
606
    ethertype = htons( protocol_map( SERVICE_ETHERNET, sender ));
607
    ethertype = htons( protocol_map( SERVICE_ETHERNET, sender ));
607
    if( ! ethertype ){
608
    if( ! ethertype ){
608
        pq_release( eth_globals.net_phone, packet_get_id( packet ));
609
        pq_release( eth_globals.net_phone, packet_get_id( packet ));
609
        return EINVAL;
610
        return EINVAL;
610
    }
611
    }
611
    rwlock_read_lock( & eth_globals.devices_lock );
612
    fibril_rwlock_read_lock( & eth_globals.devices_lock );
612
    device = eth_devices_find( & eth_globals.devices, device_id );
613
    device = eth_devices_find( & eth_globals.devices, device_id );
613
    if( ! device ){
614
    if( ! device ){
614
        rwlock_read_unlock( & eth_globals.devices_lock );
615
        fibril_rwlock_read_unlock( & eth_globals.devices_lock );
615
        return ENOENT;
616
        return ENOENT;
616
    }
617
    }
617
    // process packet queue
618
    // process packet queue
618
    next = packet;
619
    next = packet;
619
    do{
620
    do{
Line 629... Line 630...
629
    }while( next );
630
    }while( next );
630
    // send packet queue
631
    // send packet queue
631
    if( packet ){
632
    if( packet ){
632
        netif_send_msg( device->phone, device_id, packet, SERVICE_ETHERNET );
633
        netif_send_msg( device->phone, device_id, packet, SERVICE_ETHERNET );
633
    }
634
    }
634
    rwlock_read_unlock( & eth_globals.devices_lock );
635
    fibril_rwlock_read_unlock( & eth_globals.devices_lock );
635
    return EOK;
636
    return EOK;
636
}
637
}
637
 
638
 
638
int nil_message( ipc_callid_t callid, ipc_call_t * call, ipc_call_t * answer, int * answer_count ){
639
int nil_message( ipc_callid_t callid, ipc_call_t * call, ipc_call_t * answer, int * answer_count ){
639
    ERROR_DECLARE;
640
    ERROR_DECLARE;