Ignore:
Timestamp:
2010-03-07T22:51:38Z (14 years ago)
Author:
Jakub Jermar <jakub@…>
Branches:
lfn, master, serial, ticket/834-toolchain-update, topic/msim-upgrade, topic/simplify-dev-export
Children:
60ab6c3
Parents:
b5cbff4 (diff), 31c80a5 (diff)
Note: this is a merge changeset, the changes displayed below correspond to the merge itself.
Use the (diff) links above to see all the changes relative to each parent.
Message:

Merge from lp:~lukasmejdrech/helenos/network.

File:
1 edited

Legend:

Unmodified
Added
Removed
  • uspace/srv/net/nil/nildummy/nildummy.c

    rb5cbff4 r71b00dcc  
    7777 *  @param[in,out] icall The message parameters.
    7878 */
    79 void    nildummy_receiver( ipc_callid_t iid, ipc_call_t * icall );
     79void nildummy_receiver(ipc_callid_t iid, ipc_call_t * icall);
    8080
    8181/** Registers new device or updates the MTU of an existing one.
     
    9090 *  @returns Other error codes as defined for the netif_get_addr_req() function.
    9191 */
    92 int     nildummy_device_message( device_id_t device_id, services_t service, size_t mtu );
     92int nildummy_device_message(device_id_t device_id, services_t service, size_t mtu);
    9393
    9494/** Returns the device packet dimensions for sending.
     
    102102 *  @returns ENOENT if there is no such device.
    103103 */
    104 int     nildummy_packet_space_message( device_id_t device_id, size_t * addr_len, size_t * prefix, size_t * content, size_t * suffix );
     104int nildummy_packet_space_message(device_id_t device_id, size_t * addr_len, size_t * prefix, size_t * content, size_t * suffix);
    105105
    106106/** Registers receiving module service.
     
    112112 *  @returns ENOMEM if there is not enough memory left.
    113113 */
    114 int     nildummy_register_message( services_t service, int phone );
     114int nildummy_register_message(services_t service, int phone);
    115115
    116116/** Sends the packet queue.
     
    122122 *  @returns EINVAL if the service parameter is not known.
    123123 */
    124 int     nildummy_send_message( device_id_t device_id, packet_t packet, services_t sender );
     124int nildummy_send_message(device_id_t device_id, packet_t packet, services_t sender);
    125125
    126126/** Returns the device hardware address.
     
    131131 *  @returns ENOENT if there no such device.
    132132 */
    133 int     nildummy_addr_message( device_id_t device_id, measured_string_ref * address );
     133int nildummy_addr_message(device_id_t device_id, measured_string_ref * address);
    134134
    135135/*@}*/
    136136
    137 DEVICE_MAP_IMPLEMENT( nildummy_devices, nildummy_device_t )
    138 
    139 int     nil_device_state_msg( int nil_phone, device_id_t device_id, int state ){
    140         fibril_rwlock_read_lock( & nildummy_globals.protos_lock );
    141         if( nildummy_globals.proto.phone ) il_device_state_msg( nildummy_globals.proto.phone, device_id, state, nildummy_globals.proto.service );
    142         fibril_rwlock_read_unlock( & nildummy_globals.protos_lock );
    143         return EOK;
    144 }
    145 
    146 int nil_initialize( int net_phone ){
     137DEVICE_MAP_IMPLEMENT(nildummy_devices, nildummy_device_t)
     138
     139int nil_device_state_msg(int nil_phone, device_id_t device_id, int state){
     140        fibril_rwlock_read_lock(&nildummy_globals.protos_lock);
     141        if(nildummy_globals.proto.phone){
     142                il_device_state_msg(nildummy_globals.proto.phone, device_id, state, nildummy_globals.proto.service);
     143        }
     144        fibril_rwlock_read_unlock(&nildummy_globals.protos_lock);
     145        return EOK;
     146}
     147
     148int nil_initialize(int net_phone){
    147149        ERROR_DECLARE;
    148150
    149         fibril_rwlock_initialize( & nildummy_globals.devices_lock );
    150         fibril_rwlock_initialize( & nildummy_globals.protos_lock );
    151         fibril_rwlock_write_lock( & nildummy_globals.devices_lock );
    152         fibril_rwlock_write_lock( & nildummy_globals.protos_lock );
     151        fibril_rwlock_initialize(&nildummy_globals.devices_lock);
     152        fibril_rwlock_initialize(&nildummy_globals.protos_lock);
     153        fibril_rwlock_write_lock(&nildummy_globals.devices_lock);
     154        fibril_rwlock_write_lock(&nildummy_globals.protos_lock);
    153155        nildummy_globals.net_phone = net_phone;
    154156        nildummy_globals.proto.phone = 0;
    155         ERROR_PROPAGATE( nildummy_devices_initialize( & nildummy_globals.devices ));
    156         fibril_rwlock_write_unlock( & nildummy_globals.protos_lock );
    157         fibril_rwlock_write_unlock( & nildummy_globals.devices_lock );
    158         return EOK;
    159 }
    160 
    161 int nildummy_device_message( device_id_t device_id, services_t service, size_t mtu ){
     157        ERROR_PROPAGATE(nildummy_devices_initialize(&nildummy_globals.devices));
     158        fibril_rwlock_write_unlock(&nildummy_globals.protos_lock);
     159        fibril_rwlock_write_unlock(&nildummy_globals.devices_lock);
     160        return EOK;
     161}
     162
     163int nildummy_device_message(device_id_t device_id, services_t service, size_t mtu){
    162164        ERROR_DECLARE;
    163165
    164         nildummy_device_ref     device;
    165         int                                     index;
    166 
    167         fibril_rwlock_write_lock( & nildummy_globals.devices_lock );
     166        nildummy_device_ref device;
     167        int index;
     168
     169        fibril_rwlock_write_lock(&nildummy_globals.devices_lock);
    168170        // an existing device?
    169         device = nildummy_devices_find( & nildummy_globals.devices, device_id );
    170         if( device ){
    171                 if( device->service != service ){
    172                         printf( "Device %d already exists\n", device->device_id );
    173                         fibril_rwlock_write_unlock( & nildummy_globals.devices_lock );
     171        device = nildummy_devices_find(&nildummy_globals.devices, device_id);
     172        if(device){
     173                if(device->service != service){
     174                        printf("Device %d already exists\n", device->device_id);
     175                        fibril_rwlock_write_unlock(&nildummy_globals.devices_lock);
    174176                        return EEXIST;
    175177                }else{
    176178                        // update mtu
    177                         if( mtu > 0 ){
     179                        if(mtu > 0){
    178180                                device->mtu = mtu;
    179181                        }else{
    180182                                device->mtu = NET_DEFAULT_MTU;
    181183                        }
    182                         printf( "Device %d already exists:\tMTU\t= %d\n", device->device_id, device->mtu );
    183                         fibril_rwlock_write_unlock( & nildummy_globals.devices_lock );
     184                        printf("Device %d already exists:\tMTU\t= %d\n", device->device_id, device->mtu);
     185                        fibril_rwlock_write_unlock(&nildummy_globals.devices_lock);
    184186                        // notify the upper layer module
    185                         fibril_rwlock_read_lock( & nildummy_globals.protos_lock );
    186                         if( nildummy_globals.proto.phone ){
    187                                 il_mtu_changed_msg( nildummy_globals.proto.phone, device->device_id, device->mtu, nildummy_globals.proto.service );
     187                        fibril_rwlock_read_lock(&nildummy_globals.protos_lock);
     188                        if(nildummy_globals.proto.phone){
     189                                il_mtu_changed_msg(nildummy_globals.proto.phone, device->device_id, device->mtu, nildummy_globals.proto.service);
    188190                        }
    189                         fibril_rwlock_read_unlock( & nildummy_globals.protos_lock );
     191                        fibril_rwlock_read_unlock(&nildummy_globals.protos_lock);
    190192                        return EOK;
    191193                }
    192194        }else{
    193195                // create a new device
    194                 device = ( nildummy_device_ref ) malloc( sizeof( nildummy_device_t ));
    195                 if( ! device ) return ENOMEM;
     196                device = (nildummy_device_ref) malloc(sizeof(nildummy_device_t));
     197                if(! device){
     198                        return ENOMEM;
     199                }
    196200                device->device_id = device_id;
    197201                device->service = service;
    198                 if( mtu > 0 ){
     202                if(mtu > 0){
    199203                        device->mtu = mtu;
    200204                }else{
     
    202206                }
    203207                // bind the device driver
    204                 device->phone = netif_bind_service( device->service, device->device_id, SERVICE_ETHERNET, nildummy_receiver );
    205                 if( device->phone < 0 ){
    206                         fibril_rwlock_write_unlock( & nildummy_globals.devices_lock );
    207                         free( device );
     208                device->phone = netif_bind_service(device->service, device->device_id, SERVICE_ETHERNET, nildummy_receiver);
     209                if(device->phone < 0){
     210                        fibril_rwlock_write_unlock(&nildummy_globals.devices_lock);
     211                        free(device);
    208212                        return device->phone;
    209213                }
    210214                // get hardware address
    211                 if( ERROR_OCCURRED( netif_get_addr_req( device->phone, device->device_id, & device->addr, & device->addr_data ))){
    212                         fibril_rwlock_write_unlock( & nildummy_globals.devices_lock );
    213                         free( device );
     215                if(ERROR_OCCURRED(netif_get_addr_req(device->phone, device->device_id, &device->addr, &device->addr_data))){
     216                        fibril_rwlock_write_unlock(&nildummy_globals.devices_lock);
     217                        free(device);
    214218                        return ERROR_CODE;
    215219                }
    216220                // add to the cache
    217                 index = nildummy_devices_add( & nildummy_globals.devices, device->device_id, device );
    218                 if( index < 0 ){
    219                         fibril_rwlock_write_unlock( & nildummy_globals.devices_lock );
    220                         free( device->addr );
    221                         free( device->addr_data );
    222                         free( device );
     221                index = nildummy_devices_add(&nildummy_globals.devices, device->device_id, device);
     222                if(index < 0){
     223                        fibril_rwlock_write_unlock(&nildummy_globals.devices_lock);
     224                        free(device->addr);
     225                        free(device->addr_data);
     226                        free(device);
    223227                        return index;
    224228                }
    225                 printf( "New device registered:\n\tid\t= %d\n\tservice\t= %d\n\tMTU\t= %d\n", device->device_id, device->service, device->mtu );
    226         }
    227         fibril_rwlock_write_unlock( & nildummy_globals.devices_lock );
    228         return EOK;
    229 }
    230 
    231 int nildummy_addr_message( device_id_t device_id, measured_string_ref * address ){
    232         nildummy_device_ref     device;
    233 
    234         if( ! address ) return EBADMEM;
    235         fibril_rwlock_read_lock( & nildummy_globals.devices_lock );
    236         device = nildummy_devices_find( & nildummy_globals.devices, device_id );
    237         if( ! device ){
    238                 fibril_rwlock_read_unlock( & nildummy_globals.devices_lock );
     229                printf("New device registered:\n\tid\t= %d\n\tservice\t= %d\n\tMTU\t= %d\n", device->device_id, device->service, device->mtu);
     230        }
     231        fibril_rwlock_write_unlock(&nildummy_globals.devices_lock);
     232        return EOK;
     233}
     234
     235int nildummy_addr_message(device_id_t device_id, measured_string_ref * address){
     236        nildummy_device_ref device;
     237
     238        if(! address){
     239                return EBADMEM;
     240        }
     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);
    239245                return ENOENT;
    240246        }
    241         * address = device->addr;
    242         fibril_rwlock_read_unlock( & nildummy_globals.devices_lock );
    243         return ( * address ) ? EOK : ENOENT;
    244 }
    245 
    246 int nildummy_packet_space_message( device_id_t device_id, size_t * addr_len, size_t * prefix, size_t * content, size_t * suffix ){
    247         nildummy_device_ref     device;
    248 
    249         if( !( addr_len && prefix && content && suffix )) return EBADMEM;
    250         fibril_rwlock_read_lock( & nildummy_globals.devices_lock );
    251         device = nildummy_devices_find( & nildummy_globals.devices, device_id );
    252         if( ! device ){
    253                 fibril_rwlock_read_unlock( & nildummy_globals.devices_lock );
     247        *address = device->addr;
     248        fibril_rwlock_read_unlock(&nildummy_globals.devices_lock);
     249        return (*address) ? EOK : ENOENT;
     250}
     251
     252int nildummy_packet_space_message(device_id_t device_id, size_t * addr_len, size_t * prefix, size_t * content, size_t * suffix){
     253        nildummy_device_ref device;
     254
     255        if(!(addr_len && prefix && content && suffix)){
     256                return EBADMEM;
     257        }
     258        fibril_rwlock_read_lock(&nildummy_globals.devices_lock);
     259        device = nildummy_devices_find(&nildummy_globals.devices, device_id);
     260        if(! device){
     261                fibril_rwlock_read_unlock(&nildummy_globals.devices_lock);
    254262                return ENOENT;
    255263        }
    256         * content = device->mtu;
    257         fibril_rwlock_read_unlock( & nildummy_globals.devices_lock );
    258         * addr_len = 0;
    259         * prefix = 0;
    260         * suffix = 0;
    261         return EOK;
    262 }
    263 
    264 int nil_received_msg( int nil_phone, device_id_t device_id, packet_t packet, services_t target ){
    265         packet_t                next;
    266 
    267         fibril_rwlock_read_lock( & nildummy_globals.protos_lock );
    268         if( nildummy_globals.proto.phone ){
     264        *content = device->mtu;
     265        fibril_rwlock_read_unlock(&nildummy_globals.devices_lock);
     266        *addr_len = 0;
     267        *prefix = 0;
     268        *suffix = 0;
     269        return EOK;
     270}
     271
     272int nil_received_msg(int nil_phone, device_id_t device_id, packet_t packet, services_t target){
     273        packet_t next;
     274
     275        fibril_rwlock_read_lock(&nildummy_globals.protos_lock);
     276        if(nildummy_globals.proto.phone){
    269277                do{
    270                         next = pq_detach( packet );
    271                         il_received_msg( nildummy_globals.proto.phone, device_id, packet, nildummy_globals.proto.service );
     278                        next = pq_detach(packet);
     279                        il_received_msg(nildummy_globals.proto.phone, device_id, packet, nildummy_globals.proto.service);
    272280                        packet = next;
    273                 }while( packet );
    274         }
    275         fibril_rwlock_read_unlock( & nildummy_globals.protos_lock );
    276         return EOK;
    277 }
    278 
    279 int nildummy_register_message( services_t service, int phone ){
    280         fibril_rwlock_write_lock( & nildummy_globals.protos_lock );
     281                }while(packet);
     282        }
     283        fibril_rwlock_read_unlock(&nildummy_globals.protos_lock);
     284        return EOK;
     285}
     286
     287int nildummy_register_message(services_t service, int phone){
     288        fibril_rwlock_write_lock(&nildummy_globals.protos_lock);
    281289        nildummy_globals.proto.service = service;
    282290        nildummy_globals.proto.phone = phone;
    283         printf( "New protocol registered:\n\tservice\t= %d\n\tphone\t= %d\n", nildummy_globals.proto.service, nildummy_globals.proto.phone );
    284         fibril_rwlock_write_unlock( & nildummy_globals.protos_lock );
    285         return EOK;
    286 }
    287 
    288 int nildummy_send_message( device_id_t device_id, packet_t packet, services_t sender ){
    289         nildummy_device_ref             device;
    290 
    291         fibril_rwlock_read_lock( & nildummy_globals.devices_lock );
    292         device = nildummy_devices_find( & nildummy_globals.devices, device_id );
    293         if( ! device ){
    294                 fibril_rwlock_read_unlock( & nildummy_globals.devices_lock );
     291        printf("New protocol registered:\n\tservice\t= %d\n\tphone\t= %d\n", nildummy_globals.proto.service, nildummy_globals.proto.phone);
     292        fibril_rwlock_write_unlock(&nildummy_globals.protos_lock);
     293        return EOK;
     294}
     295
     296int nildummy_send_message(device_id_t device_id, packet_t packet, services_t sender){
     297        nildummy_device_ref device;
     298
     299        fibril_rwlock_read_lock(&nildummy_globals.devices_lock);
     300        device = nildummy_devices_find(&nildummy_globals.devices, device_id);
     301        if(! device){
     302                fibril_rwlock_read_unlock(&nildummy_globals.devices_lock);
    295303                return ENOENT;
    296304        }
    297305        // send packet queue
    298         if( packet ){
    299                 netif_send_msg( device->phone, device_id, packet, SERVICE_NILDUMMY );
    300         }
    301         fibril_rwlock_read_unlock( & nildummy_globals.devices_lock );
    302         return EOK;
    303 }
    304 
    305 int nil_message( ipc_callid_t callid, ipc_call_t * call, ipc_call_t * answer, int * answer_count ){
     306        if(packet){
     307                netif_send_msg(device->phone, device_id, packet, SERVICE_NILDUMMY);
     308        }
     309        fibril_rwlock_read_unlock(&nildummy_globals.devices_lock);
     310        return EOK;
     311}
     312
     313int nil_message(ipc_callid_t callid, ipc_call_t * call, ipc_call_t * answer, int * answer_count){
    306314        ERROR_DECLARE;
    307315
    308         measured_string_ref     address;
    309         packet_t                        packet;
    310 
    311 //      printf( "message %d - %d\n", IPC_GET_METHOD( * call ), NET_NIL_FIRST );
    312         * answer_count = 0;
    313         switch( IPC_GET_METHOD( * call )){
     316        measured_string_ref address;
     317        packet_t packet;
     318
     319//      printf("message %d - %d\n", IPC_GET_METHOD(*call), NET_NIL_FIRST);
     320        *answer_count = 0;
     321        switch(IPC_GET_METHOD(*call)){
    314322                case IPC_M_PHONE_HUNGUP:
    315323                        return EOK;
    316324                case NET_NIL_DEVICE:
    317                         return nildummy_device_message( IPC_GET_DEVICE( call ), IPC_GET_SERVICE( call ), IPC_GET_MTU( call ));
     325                        return nildummy_device_message(IPC_GET_DEVICE(call), IPC_GET_SERVICE(call), IPC_GET_MTU(call));
    318326                case NET_NIL_SEND:
    319                         ERROR_PROPAGATE( packet_translate( nildummy_globals.net_phone, & packet, IPC_GET_PACKET( call )));
    320                         return nildummy_send_message( IPC_GET_DEVICE( call ), packet, IPC_GET_SERVICE( call ));
     327                        ERROR_PROPAGATE(packet_translate(nildummy_globals.net_phone, &packet, IPC_GET_PACKET(call)));
     328                        return nildummy_send_message(IPC_GET_DEVICE(call), packet, IPC_GET_SERVICE(call));
    321329                case NET_NIL_PACKET_SPACE:
    322                         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 )));
    323                         * answer_count = 4;
     330                        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)));
     331                        *answer_count = 4;
    324332                        return EOK;
    325333                case NET_NIL_ADDR:
    326                         ERROR_PROPAGATE( nildummy_addr_message( IPC_GET_DEVICE( call ), & address ));
    327                         return measured_strings_reply( address, 1 );
     334                        ERROR_PROPAGATE(nildummy_addr_message(IPC_GET_DEVICE(call), &address));
     335                        return measured_strings_reply(address, 1);
    328336                case IPC_M_CONNECT_TO_ME:
    329                         return nildummy_register_message( NIL_GET_PROTO( call ), IPC_GET_PHONE( call ));
     337                        return nildummy_register_message(NIL_GET_PROTO(call), IPC_GET_PHONE(call));
    330338        }
    331339        return ENOTSUP;
    332340}
    333341
    334 void nildummy_receiver( ipc_callid_t iid, ipc_call_t * icall ){
     342void nildummy_receiver(ipc_callid_t iid, ipc_call_t * icall){
    335343        ERROR_DECLARE;
    336344
    337         packet_t                packet;
    338 
    339         while( true ){
    340 //              printf( "message %d - %d\n", IPC_GET_METHOD( * icall ), NET_NIL_FIRST );
    341                 switch( IPC_GET_METHOD( * icall )){
     345        packet_t packet;
     346
     347        while(true){
     348//              printf("message %d - %d\n", IPC_GET_METHOD(*icall), NET_NIL_FIRST);
     349                switch(IPC_GET_METHOD(*icall)){
    342350                        case NET_NIL_DEVICE_STATE:
    343                                 ERROR_CODE = nil_device_state_msg( 0, IPC_GET_DEVICE( icall ), IPC_GET_STATE( icall ));
    344                                 ipc_answer_0( iid, ( ipcarg_t ) ERROR_CODE );
     351                                ERROR_CODE = nil_device_state_msg(0, IPC_GET_DEVICE(icall), IPC_GET_STATE(icall));
     352                                ipc_answer_0(iid, (ipcarg_t) ERROR_CODE);
    345353                                break;
    346354                        case NET_NIL_RECEIVED:
    347                                 if( ! ERROR_OCCURRED( packet_translate( nildummy_globals.net_phone, & packet, IPC_GET_PACKET( icall )))){
    348                                         ERROR_CODE = nil_received_msg( 0, IPC_GET_DEVICE( icall ), packet, 0 );
     355                                if(! ERROR_OCCURRED(packet_translate(nildummy_globals.net_phone, &packet, IPC_GET_PACKET(icall)))){
     356                                        ERROR_CODE = nil_received_msg(0, IPC_GET_DEVICE(icall), packet, 0);
    349357                                }
    350                                 ipc_answer_0( iid, ( ipcarg_t ) ERROR_CODE );
     358                                ipc_answer_0(iid, (ipcarg_t) ERROR_CODE);
    351359                                break;
    352360                        default:
    353                                 ipc_answer_0( iid, ( ipcarg_t ) ENOTSUP );
    354                 }
    355                 iid = async_get_call( icall );
     361                                ipc_answer_0(iid, (ipcarg_t) ENOTSUP);
     362                }
     363                iid = async_get_call(icall);
    356364        }
    357365}
Note: See TracChangeset for help on using the changeset viewer.