Subversion Repositories HelenOS

Rev

Rev 4505 | Rev 4719 | Go to most recent revision | Details | Compare with Previous | Last modification | View Log | RSS feed

Rev Author Line No. Line
3466 mejdrech 1
/*
3912 mejdrech 2
 * Copyright (c) 2009 Lukas Mejdrech
3466 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 net
3912 mejdrech 30
 *  @{
3466 mejdrech 31
 */
32
 
33
/** @file
4350 mejdrech 34
 *  Networking subsystem central module implementation.
3466 mejdrech 35
 */
36
 
37
#include <async.h>
3666 mejdrech 38
#include <ctype.h>
4163 mejdrech 39
#include <ddi.h>
3466 mejdrech 40
#include <errno.h>
3666 mejdrech 41
#include <malloc.h>
3466 mejdrech 42
#include <stdio.h>
4582 mejdrech 43
#include <string.h>
3846 mejdrech 44
 
3466 mejdrech 45
#include <ipc/ipc.h>
46
#include <ipc/services.h>
47
 
3666 mejdrech 48
#include "../err.h"
49
#include "../messages.h"
3466 mejdrech 50
#include "../modules.h"
3666 mejdrech 51
//#include "../self_test.h"
3466 mejdrech 52
 
3886 mejdrech 53
#include "../structures/char_map.h"
54
#include "../structures/generic_char_map.h"
55
#include "../structures/measured_strings.h"
4192 mejdrech 56
#include "../structures/module_map.h"
3901 mejdrech 57
#include "../structures/packet/packet.h"
3886 mejdrech 58
 
4307 mejdrech 59
#include "../il/il_messages.h"
4243 mejdrech 60
#include "../include/device.h"
4307 mejdrech 61
#include "../include/netif_interface.h"
62
#include "../include/nil_interface.h"
63
#include "../include/net_interface.h"
64
#include "../include/ip_interface.h"
3466 mejdrech 65
 
4307 mejdrech 66
#include "net.h"
3466 mejdrech 67
 
4350 mejdrech 68
/** Networking module name.
69
 */
3901 mejdrech 70
#define NAME    "Networking"
71
 
4350 mejdrech 72
/** Prints the module name.
73
 *  @see NAME
74
 */
4307 mejdrech 75
void    module_print_name( void );
4350 mejdrech 76
 
77
/** Starts the networking module.
78
 *  Initializes the client connection serving function, initializes the module, registers the module service and starts the async manager, processing IPC messages in an infinite loop.
79
 *  @param client_connection The client connection processing function. The module skeleton propagates its own one. Input parameter.
80
 *  @returns EOK on successful module termination.
81
 *  @returns Other error codes as defined for the net_initialize() function.
82
 *  @returns Other error codes as defined for the REGISTER_ME() macro function.
83
 */
4307 mejdrech 84
int module_start( async_client_conn_t client_connection );
4350 mejdrech 85
 
3666 mejdrech 86
//int       parse_line( measured_strings_ref configuration, char * line );
4350 mejdrech 87
 
88
/** Reads the networking subsystem global configuration.
89
 *  @returns EOK on success.
90
 *  @returns Other error codes as defined for the add_configuration() function.
91
 */
3666 mejdrech 92
int     read_configuration( void );
4350 mejdrech 93
 
94
/** Starts the network interface according to its configuration.
95
 *  Registers the network interface with the subsystem modules.
96
 *  Starts the needed subsystem modules.
97
 *  @param netif The network interface specific data.
98
 *  @returns EOK on success.
99
 *  @returns EINVAL if there are some settings missing.
100
 *  @returns ENOENT if the internet protocol module is not known.
101
 *  @returns Other error codes as defined for the netif_probe_req() function.
102
 *  @returns Other error codes as defined for the nil_device_req() function.
103
 *  @returns Other error codes as defined for the needed internet layer registering function.
104
 */
4192 mejdrech 105
int     start_device( netif_ref netif );
4350 mejdrech 106
 
107
/** Reads the configuration and starts all network interfaces.
108
 *  @returns EOK on success.
109
 *  @returns EXDEV if there is no available system-unique device identifier.
110
 *  @returns EINVAL if any of the network interface names are not configured.
111
 *  @returns ENOMEM if there is not enough memory left.
112
 *  @returns Other error codes as defined for the read_configuration() function.
113
 *  @returns Other error codes as defined for the read_netif_configuration() function.
114
 *  @returns Other error codes as defined for the start_device() function.
115
 */
3666 mejdrech 116
int     startup( void );
4350 mejdrech 117
 
118
/** Generates new system-unique device identifier.
119
 *  @returns The system-unique devic identifier.
120
 */
3846 mejdrech 121
device_id_t generate_new_device_id( void );
3666 mejdrech 122
 
4350 mejdrech 123
/** Returns the configured values.
124
 *  The network interface configuration is searched first.
125
 *  @param netif_conf The network interface configuration setting. Input parameter.
126
 *  @param configuration The found configured values. Output parameter.
127
 *  @param count The desired settings count. Input parameter.
128
 *  @param data The found configuration settings data. Output parameter.
129
 *  @returns EOK.
130
 */
4505 mejdrech 131
int net_get_conf( measured_strings_ref netif_conf, measured_string_ref configuration, size_t count, char ** data );
4350 mejdrech 132
 
133
/** Networking module global data.
134
 */
4307 mejdrech 135
net_globals_t   net_globals;
3666 mejdrech 136
 
137
DEVICE_MAP_IMPLEMENT( netifs, netif_t )
138
 
139
GENERIC_CHAR_MAP_IMPLEMENT( measured_strings, measured_string_t )
140
 
4307 mejdrech 141
void module_print_name( void ){
4197 mejdrech 142
    printf( "%s", NAME );
3901 mejdrech 143
}
144
 
4307 mejdrech 145
int module_start( async_client_conn_t client_connection ){
3846 mejdrech 146
    ERROR_DECLARE;
3466 mejdrech 147
 
3846 mejdrech 148
    ipcarg_t    phonehash;
149
 
150
    async_set_client_connection( client_connection );
3901 mejdrech 151
    ERROR_PROPAGATE( pm_init());
4351 mejdrech 152
    if( ERROR_OCCURRED( net_initialize( client_connection ))
3914 mejdrech 153
    || ERROR_OCCURRED( REGISTER_ME( SERVICE_NETWORKING, & phonehash ))){
154
        pm_destroy();
155
        return ERROR_CODE;
156
    }
157
 
3846 mejdrech 158
    async_manager();
159
 
3914 mejdrech 160
    pm_destroy();
3666 mejdrech 161
    return EOK;
162
}
163
 
4505 mejdrech 164
int net_get_device_conf_req( int net_phone, device_id_t device_id, measured_string_ref * configuration, size_t count, char ** data ){
4307 mejdrech 165
    netif_ref   netif;
3666 mejdrech 166
 
4307 mejdrech 167
    if( !( configuration && ( count > 0 ))) return EINVAL;
168
    netif = netifs_find( & net_globals.netifs, device_id );
169
    if( netif ){
170
        return net_get_conf( & netif->configuration, * configuration, count, data );
171
    }else{
172
        return net_get_conf( NULL, * configuration, count, data );
173
    }
174
}
3666 mejdrech 175
 
4505 mejdrech 176
int net_get_conf_req( int net_phone, measured_string_ref * configuration, size_t count, char ** data ){
4307 mejdrech 177
    if( !( configuration && ( count > 0 ))) return EINVAL;
178
    return net_get_conf( NULL, * configuration, count, data );
179
}
3666 mejdrech 180
 
4505 mejdrech 181
int net_get_conf( measured_strings_ref netif_conf, measured_string_ref configuration, size_t count, char ** data ){
4307 mejdrech 182
    measured_string_ref setting;
4505 mejdrech 183
    size_t              index;
3666 mejdrech 184
 
4307 mejdrech 185
    if( data ) * data = NULL;
186
    for( index = 0; index < count; ++ index ){
4350 mejdrech 187
        setting = measured_strings_find( netif_conf, configuration[ index ].value, 0 );
4307 mejdrech 188
        if( ! setting ){
189
            setting = measured_strings_find( & net_globals.configuration, configuration[ index ].value, 0 );
190
        }
191
        if( setting ){
192
            configuration[ index ].length = setting->length;
193
            configuration[ index ].value = setting->value;
194
        }else{
195
            configuration[ index ].length = 0;
196
            configuration[ index ].value = NULL;
197
        }
198
    }
199
    return EOK;
200
}
3666 mejdrech 201
 
4307 mejdrech 202
void net_free_settings( measured_string_ref settings, char * data ){
203
}
3666 mejdrech 204
 
4307 mejdrech 205
int net_connect_module( services_t service ){
3666 mejdrech 206
    return EOK;
207
}
208
 
3901 mejdrech 209
int net_message( ipc_callid_t callid, ipc_call_t * call, ipc_call_t * answer, int * answer_count ){
3666 mejdrech 210
    ERROR_DECLARE;
211
 
212
    measured_string_ref strings;
4307 mejdrech 213
    char *              data;
3666 mejdrech 214
 
3901 mejdrech 215
    * answer_count = 0;
216
    switch( IPC_GET_METHOD( * call )){
3666 mejdrech 217
        case IPC_M_PHONE_HUNGUP:
218
            return EOK;
3846 mejdrech 219
        case NET_NET_DEVICE:
3666 mejdrech 220
            // TODO configure, register
4307 mejdrech 221
            printf( "Networking: new netif %d\n", IPC_GET_DEVICE( call ));
3666 mejdrech 222
            return EOK;
3846 mejdrech 223
        case NET_NET_GET_DEVICE_CONF:
3901 mejdrech 224
            ERROR_PROPAGATE( measured_strings_receive( & strings, & data, IPC_GET_COUNT( call )));
4307 mejdrech 225
            net_get_device_conf_req( 0, IPC_GET_DEVICE( call ), & strings, IPC_GET_COUNT( call ), NULL );
3666 mejdrech 226
            // strings should not contain received data anymore
227
            free( data );
3901 mejdrech 228
            ERROR_CODE = measured_strings_reply( strings, IPC_GET_COUNT( call ));
3666 mejdrech 229
            free( strings );
230
            return ERROR_CODE;
3846 mejdrech 231
        case NET_NET_GET_CONF:
3901 mejdrech 232
            ERROR_PROPAGATE( measured_strings_receive( & strings, & data, IPC_GET_COUNT( call )));
4307 mejdrech 233
            net_get_conf_req( 0, & strings, IPC_GET_COUNT( call ), NULL );
3666 mejdrech 234
            // strings should not contain received data anymore
235
            free( data );
3901 mejdrech 236
            ERROR_CODE = measured_strings_reply( strings, IPC_GET_COUNT( call ));
3666 mejdrech 237
            free( strings );
238
            return ERROR_CODE;
3846 mejdrech 239
        case NET_NET_STARTUP:
3666 mejdrech 240
            return startup();
241
    }
242
    return ENOTSUP;
243
}
244
 
245
/*
246
int parse_line( measured_strings_ref configuration, char * line ){
247
    ERROR_DECLARE;
248
 
249
    measured_string_ref setting;
250
    char *          name;
251
    char *          value;
252
 
253
    // from the beginning
254
    name = line;
255
    // skip spaces
256
    while( isspace( * name )) ++ name;
257
    // remember the name start
258
    value = name;
259
    // skip the name
260
    while( isalnum( * value ) || ( * value == '_' )){
261
        // make uppercase
262
//      * value = toupper( * value );
263
        ++ value;
264
    }
265
    if( * value == '=' ){
266
        // terminate the name
267
        * value = '\0';
268
    }else{
269
        // terminate the name
270
        * value = '\0';
271
        // skip until '='
272
        ++ value;
273
        while(( * value ) && ( * value != '=' )) ++ value;
274
        // not found?
275
        if( * value != '=' ) return EINVAL;
276
    }
277
    ++ value;
278
    // skip spaces
279
    while( isspace( * value )) ++ value;
280
    // create a bulk measured string till the end
281
    setting = measured_string_create_bulk( value, -1 );
282
    if( ! setting ) return ENOMEM;
283
    // add the configuration setting
3912 mejdrech 284
    if( ERROR_OCCURRED( measured_strings_add( configuration, name, 0, setting ))){
3666 mejdrech 285
        free( setting );
286
        return ERROR_CODE;
287
    }
288
    return EOK;
289
}
290
*/
291
 
292
int add_configuration( measured_strings_ref configuration, const char * name, const char * value ){
293
    ERROR_DECLARE;
294
 
295
    measured_string_ref setting;
296
 
297
    setting = measured_string_create_bulk( value, 0 );
298
    if( ! setting ) return ENOMEM;
299
    // add the configuration setting
3912 mejdrech 300
    if( ERROR_OCCURRED( measured_strings_add( configuration, name, 0, setting ))){
3666 mejdrech 301
        free( setting );
302
        return ERROR_CODE;
303
    }
304
    return EOK;
305
}
306
 
3846 mejdrech 307
device_id_t generate_new_device_id( void ){
4163 mejdrech 308
    return device_assign_devno();
3666 mejdrech 309
}
310
 
311
int read_configuration( void ){
312
    ERROR_DECLARE;
313
 
4192 mejdrech 314
    // read general configuration
4307 mejdrech 315
    ERROR_PROPAGATE( add_configuration( & net_globals.configuration, "IPV", "4" ));
316
    ERROR_PROPAGATE( add_configuration( & net_globals.configuration, "MTU", "1500" ));
4192 mejdrech 317
    return EOK;
318
}
319
 
320
int start_device( netif_ref netif ){
321
    ERROR_DECLARE;
322
 
3666 mejdrech 323
    measured_string_ref setting;
4163 mejdrech 324
    services_t          internet_service;
325
    int                 irq;
326
    int                 io;
327
    int                 mtu;
3666 mejdrech 328
 
329
    // mandatory netif
4163 mejdrech 330
    setting = measured_strings_find( & netif->configuration, CONF_NETIF, 0 );
4307 mejdrech 331
    netif->driver = get_running_module( & net_globals.modules, setting->value );
3846 mejdrech 332
    if( ! netif->driver ){
4307 mejdrech 333
        printf( "Failed to start the network interface driver %s\n", setting->value );
3666 mejdrech 334
        return EINVAL;
335
    }
4163 mejdrech 336
    // optional network interface layer
337
    setting = measured_strings_find( & netif->configuration, CONF_NIL, 0 );
3666 mejdrech 338
    if( setting ){
4307 mejdrech 339
        netif->nil = get_running_module( & net_globals.modules, setting->value );
3846 mejdrech 340
        if( ! netif->nil ){
4307 mejdrech 341
            printf( "Failed to start the network interface layer %s\n", setting->value );
3666 mejdrech 342
            return EINVAL;
343
        }
344
    }else{
3846 mejdrech 345
        netif->nil = NULL;
3666 mejdrech 346
    }
347
    // mandatory internet layer
4163 mejdrech 348
    setting = measured_strings_find( & netif->configuration, CONF_IL, 0 );
4307 mejdrech 349
    netif->il = get_running_module( & net_globals.modules, setting->value );
3846 mejdrech 350
    if( ! netif->il ){
4307 mejdrech 351
        printf( "Failed to start the internet layer %s\n", setting->value );
3666 mejdrech 352
        return EINVAL;
353
    }
3685 mejdrech 354
    // end of the static loopback initialization
355
    // startup the loopback interface
4163 mejdrech 356
    setting = measured_strings_find( & netif->configuration, CONF_IRQ, 0 );
357
    irq = setting ? strtol( setting->value, NULL, 10 ) : 0;
358
    setting = measured_strings_find( & netif->configuration, CONF_IO, 0 );
359
    io = setting ? strtol( setting->value, NULL, 16 ) : 0;
4243 mejdrech 360
    ERROR_PROPAGATE( netif_probe_req( netif->driver->phone, netif->id, irq, io ));
3846 mejdrech 361
    if( netif->nil ){
4163 mejdrech 362
        setting = measured_strings_find( & netif->configuration, CONF_MTU, 0 );
363
        if( ! setting ){
4307 mejdrech 364
            setting = measured_strings_find( & net_globals.configuration, CONF_MTU, 0 );
4163 mejdrech 365
        }
366
        mtu = setting ? strtol( setting->value, NULL, 10 ) : 0;
4261 mejdrech 367
        ERROR_PROPAGATE( nil_device_req( netif->nil->phone, netif->id, mtu, netif->driver->service ));
3846 mejdrech 368
        internet_service = netif->nil->service;
3685 mejdrech 369
    }else{
3846 mejdrech 370
        internet_service = netif->driver->service;
3685 mejdrech 371
    }
4307 mejdrech 372
    switch( netif->il->service ){
373
        case SERVICE_IP:
374
            ERROR_PROPAGATE( ip_device_req( netif->il->phone, netif->id, internet_service ));
375
            break;
376
        default:
377
            return ENOENT;
378
    }
4243 mejdrech 379
    ERROR_PROPAGATE( netif_start_req( netif->driver->phone, netif->id ));
3666 mejdrech 380
    return EOK;
381
}
382
 
383
int startup( void ){
384
    ERROR_DECLARE;
385
 
4192 mejdrech 386
    char *      conf_files[] = { "lo", "ne2k" };
387
    int         count = sizeof( conf_files ) / sizeof( char * );
388
    int         index;
389
    netif_ref   netif;
390
    int         i;
391
    measured_string_ref setting;
3666 mejdrech 392
 
4192 mejdrech 393
    // TODO dynamic configuration
394
    ERROR_PROPAGATE( read_configuration());
395
 
396
    for( i = 0; i < count; ++ i ){
397
        netif = ( netif_ref ) malloc( sizeof( netif_t ));
398
        if( ! netif ) return ENOMEM;
399
 
400
        netif->id = generate_new_device_id();
401
        if( ! netif->id ) return EXDEV;
402
        ERROR_PROPAGATE( measured_strings_initialize( & netif->configuration ));
403
        // read configuration files
404
        if( ERROR_OCCURRED( read_netif_configuration( conf_files[ i ], netif ))){
405
            measured_strings_destroy( & netif->configuration );
406
            free( netif );
407
            return ERROR_CODE;
408
        }
409
        // mandatory name
410
        setting = measured_strings_find( & netif->configuration, CONF_NAME, 0 );
411
        if( ! setting ){
4307 mejdrech 412
            printf( "The name is missing\n" );
4192 mejdrech 413
            measured_strings_destroy( & netif->configuration );
414
            free( netif );
415
            return EINVAL;
416
        }
417
        netif->name = setting->value;
418
        // add to the netifs map
4307 mejdrech 419
        index = netifs_add( & net_globals.netifs, netif->id, netif );
4192 mejdrech 420
        if( index < 0 ){
421
            measured_strings_destroy( & netif->configuration );
422
            free( netif );
423
            return index;
424
        }
425
        // add to the netif names map
4307 mejdrech 426
        if( ERROR_OCCURRED( char_map_add( & net_globals.netif_names, netif->name, 0, index ))
4192 mejdrech 427
        // start network interfaces and needed modules
428
        || ERROR_OCCURRED( start_device( netif ))){
429
            measured_strings_destroy( & netif->configuration );
4307 mejdrech 430
            netifs_exclude_index( & net_globals.netifs, index );
4192 mejdrech 431
            return ERROR_CODE;
432
        }
433
        // increment modules' usage
434
        ++ netif->driver->usage;
435
        if( netif->nil ) ++ netif->nil->usage;
436
        ++ netif->il->usage;
4307 mejdrech 437
        printf( "New network interface started:\n\tname\t= %s\n\tid\t= %d\n\tdriver\t= %s\n\tnil\t= %s\n\til\t= %s\n", netif->name, netif->id, netif->driver->name, netif->nil ? netif->nil->name : NULL, netif->il->name );
4192 mejdrech 438
    }
3666 mejdrech 439
    return EOK;
440
}
441
 
3466 mejdrech 442
/** @}
443
 */