Subversion Repositories HelenOS

Rev

Rev 4197 | Rev 4261 | 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
34
 */
35
 
36
#include <async.h>
3666 mejdrech 37
#include <ctype.h>
4163 mejdrech 38
#include <ddi.h>
3466 mejdrech 39
#include <errno.h>
3666 mejdrech 40
#include <malloc.h>
3466 mejdrech 41
#include <stdio.h>
3846 mejdrech 42
 
3466 mejdrech 43
#include <ipc/ipc.h>
44
#include <ipc/services.h>
45
 
3666 mejdrech 46
#include "../err.h"
47
#include "../messages.h"
3466 mejdrech 48
#include "../modules.h"
3666 mejdrech 49
//#include "../self_test.h"
3466 mejdrech 50
 
3886 mejdrech 51
#include "../structures/char_map.h"
52
#include "../structures/generic_char_map.h"
53
#include "../structures/measured_strings.h"
4192 mejdrech 54
#include "../structures/module_map.h"
3901 mejdrech 55
#include "../structures/packet/packet.h"
56
#include "../structures/packet/packet_server.h"
3886 mejdrech 57
 
58
#include "../il/ip/ip_messages.h"
4243 mejdrech 59
#include "../include/device.h"
60
#include "../include/netif_messages.h"
3666 mejdrech 61
 
3846 mejdrech 62
#if IP_BUNDLE
3466 mejdrech 63
 
3846 mejdrech 64
    #include "../ip/ip_module.h"
3466 mejdrech 65
 
66
#endif
67
 
3846 mejdrech 68
#if TCP_BUNDLE
69
 
70
    #include "../tcp/tcp_module.h"
71
 
72
#endif
73
 
3901 mejdrech 74
#define NAME    "Networking"
75
 
3666 mejdrech 76
#define LO_NAME             "lo"
4155 mejdrech 77
#define LO_FILENAME         "/srv/lo"
78
#define DP8390_NAME         "dp8390"
4163 mejdrech 79
#define DP8390_FILENAME     "/srv/dp8390"
80
#define ETHERNET_NAME       "ethernet"
81
#define ETHERNET_FILENAME   "/srv/eth"
3666 mejdrech 82
#define IP_NAME             "ip"
4155 mejdrech 83
#define IP_FILENAME         "/srv/ip"
3466 mejdrech 84
 
4163 mejdrech 85
#define CONF_NAME           "NAME"
86
#define CONF_NETIF          "NETIF"
87
#define CONF_NIL            "NIL"
88
#define CONF_IL             "IL"
89
#define CONF_IRQ            "IRQ"
90
#define CONF_IO             "IO"
91
#define CONF_MTU            "MTU"
92
 
3901 mejdrech 93
#define IPC_GET_DEVICE( call )  ( device_id_t ) IPC_GET_ARG1( * call )
94
#define IPC_GET_COUNT( call )   ( int ) IPC_GET_ARG2( * call )
95
 
3666 mejdrech 96
typedef struct netif        netif_t;
97
typedef netif_t *       netif_ref;
3466 mejdrech 98
 
3666 mejdrech 99
typedef struct networking_globals   networking_globals_t;
3466 mejdrech 100
 
3666 mejdrech 101
DEVICE_MAP_DECLARE( netifs, netif_t )
3466 mejdrech 102
 
3666 mejdrech 103
GENERIC_CHAR_MAP_DECLARE( measured_strings, measured_string_t )
3466 mejdrech 104
 
3666 mejdrech 105
/** A present network interface device.
106
 */
107
struct netif{
108
    /** A system-unique network interface identifier.
109
     */
3846 mejdrech 110
    device_id_t     id;
3666 mejdrech 111
    /** A serving network interface driver module index.
112
     */
3846 mejdrech 113
    module_ref      driver;
3666 mejdrech 114
    /** A serving link layer module index.
115
     */
3846 mejdrech 116
    module_ref      nil;
3666 mejdrech 117
    /** A serving internet layer module index.
118
     */
3846 mejdrech 119
    module_ref      il;
3666 mejdrech 120
    /** A system-unique network interface name.
121
     */
122
    char *          name;
123
    /** Configuration.
124
     */
125
    measured_strings_t  configuration;
126
};
3466 mejdrech 127
 
3666 mejdrech 128
/** A networking module global variables.
129
 */
130
struct networking_globals{
131
    /** Present network interfaces.
132
     */
133
    netifs_t        netifs;
134
    /** Network interface structure indices by names.
135
     */
136
    char_map_t      netif_names;
137
    /** Available modules.
138
     */
139
    modules_t       modules;
140
    /** Global configuration.
141
     */
142
    measured_strings_t  configuration;
143
};
3466 mejdrech 144
 
3901 mejdrech 145
void    networking_print_name( void );
3666 mejdrech 146
measured_string_ref configuration_find( measured_strings_ref configuration, const char * name );
3901 mejdrech 147
int networking_start_module( async_client_conn_t client_connection );
3666 mejdrech 148
int     networking_initialize( void );
3901 mejdrech 149
int networking_message( ipc_callid_t callid, ipc_call_t * call, ipc_call_t * answer, int * answer_count );
150
int net_message( ipc_callid_t callid, ipc_call_t * call, ipc_call_t * answer, int * answer_count );
3666 mejdrech 151
//int       parse_line( measured_strings_ref configuration, char * line );
152
int     add_configuration( measured_strings_ref configuration, const char * name, const char * value );
153
int     read_configuration( void );
4192 mejdrech 154
int     read_netif_configuration( char * name, netif_ref netif );
155
int     start_device( netif_ref netif );
3666 mejdrech 156
int     startup( void );
3846 mejdrech 157
device_id_t generate_new_device_id( void );
3666 mejdrech 158
 
159
static networking_globals_t networking_globals;
160
 
161
DEVICE_MAP_IMPLEMENT( netifs, netif_t )
162
 
163
GENERIC_CHAR_MAP_IMPLEMENT( measured_strings, measured_string_t )
164
 
3901 mejdrech 165
void networking_print_name( void ){
4197 mejdrech 166
    printf( "%s", NAME );
3901 mejdrech 167
}
168
 
169
int networking_message( ipc_callid_t callid, ipc_call_t * call, ipc_call_t * answer, int * answer_count ){
3466 mejdrech 170
#ifdef NETWORKING_module
3901 mejdrech 171
        //TODO map to *_message
3666 mejdrech 172
        if( IS_NET_IL_MESSAGE( call ) || IS_NET_IP_MESSAGE( call )){
3901 mejdrech 173
            return ip_message( callid, call, answer, answer_count );
3466 mejdrech 174
/*      }else if( IS_NET_ARP_MESSAGE( call )){
3901 mejdrech 175
            return arp_message( callid, call, answer, answer_count );
3466 mejdrech 176
*//*        }else if( IS_NET_RARP_MESSAGE( call )){
3901 mejdrech 177
            return rarp_message( callid, call, answer, answer_count );
3466 mejdrech 178
*//*        }else if( IS_NET_ICMP_MESSAGE( call )){
3901 mejdrech 179
            return icmp_message( callid, call, answer, answer_count );
3466 mejdrech 180
*//*        }else if( IS_NET_UDP_MESSAGE( call )){
3901 mejdrech 181
            return udp_message( callid, call, answer, answer_count );
3466 mejdrech 182
*/      }else if( IS_NET_TCP_MESSAGE( call )){
3901 mejdrech 183
            return tcp_message( callid, call, answer, answer_count );
3466 mejdrech 184
/*      }else if( IS_NET_SOCKET_MESSAGE( call )){
3901 mejdrech 185
            return socket_message( callid, call, answer, answer_count );
3846 mejdrech 186
*//*        }else if( IS_NET_NIL_MESSAGE( call ) || IS_NET_ETHERNET_MESSAGE( call )){
3901 mejdrech 187
            return ethernet_message( callid, call, answer, answer_count );
3466 mejdrech 188
*/      }else{
189
#endif
3901 mejdrech 190
            if( IS_NET_PACKET_MESSAGE( call )){
191
                return packet_server_message( callid, call, answer, answer_count );
192
            }else{
193
                return net_message( callid, call, answer, answer_count );
194
            }
3466 mejdrech 195
#ifdef NETWORKING_module
196
        }
197
#endif
198
}
199
 
3901 mejdrech 200
int networking_start_module( async_client_conn_t client_connection ){
3846 mejdrech 201
    ERROR_DECLARE;
3466 mejdrech 202
 
3846 mejdrech 203
    ipcarg_t    phonehash;
204
 
205
    async_set_client_connection( client_connection );
3901 mejdrech 206
    ERROR_PROPAGATE( pm_init());
3914 mejdrech 207
    if( ERROR_OCCURRED( networking_initialize())
208
    || ERROR_OCCURRED( REGISTER_ME( SERVICE_NETWORKING, & phonehash ))){
209
        pm_destroy();
210
        return ERROR_CODE;
211
    }
212
 
3846 mejdrech 213
    async_manager();
214
 
3914 mejdrech 215
    pm_destroy();
3666 mejdrech 216
    return EOK;
217
}
218
 
219
int networking_initialize( void ){
220
    ERROR_DECLARE;
221
 
222
    task_id_t   task_id;
223
 
224
    netifs_initialize( & networking_globals.netifs );
225
    char_map_initialize( & networking_globals.netif_names );
226
    modules_initialize( & networking_globals.modules );
227
    measured_strings_initialize( & networking_globals.configuration );
228
 
229
    // run self tests
230
//  ERROR_PROPAGATE( self_test());
231
 
232
    ERROR_PROPAGATE( add_module( NULL, & networking_globals.modules, LO_NAME, LO_FILENAME, SERVICE_LO, 0 ));
4155 mejdrech 233
    ERROR_PROPAGATE( add_module( NULL, & networking_globals.modules, DP8390_NAME, DP8390_FILENAME, SERVICE_DP8390, 0 ));
3666 mejdrech 234
    ERROR_PROPAGATE( add_module( NULL, & networking_globals.modules, ETHERNET_NAME, ETHERNET_FILENAME, SERVICE_ETHERNET, 0 ));
235
 
236
#ifdef NETWORKING_modular
4155 mejdrech 237
    task_id = spawn( "/srv/ip" );
3666 mejdrech 238
    if( ! task_id ) return EINVAL;
239
    ERROR_PROPAGATE( add_module( NULL, & networking_globals.modules, IP_NAME, IP_FILENAME, SERVICE_IP, task_id ));
4155 mejdrech 240
//  if( ! spawn( "/srv/udp" )) return EINVAL;
4163 mejdrech 241
//  if( ! spawn( "/srv/tcp" )) return EINVAL;
4155 mejdrech 242
//  if( ! spawn( "/srv/socket" )) return EINVAL;
3666 mejdrech 243
//  not always necesssary
4155 mejdrech 244
//  if( ! spawn( "/srv/arp" )) return EINVAL;
245
//  if( ! spawn( "/srv/rarp" )) return EINVAL;
246
//  if( ! spawn( "/srv/icmp" )) return EINVAL;
3666 mejdrech 247
 
248
#else
249
#ifdef NETWORKING_module
250
    ipcarg_t    phonehash;
251
 
252
    ERROR_PROPAGATE( REGISTER_ME( SERVICE_IP, & phonehash ));
253
    ERROR_PROPAGATE( add_module( NULL, & networking_globals.modules, IP_NAME, IP_FILENAME, SERVICE_IP, task_get_id()));
254
    ERROR_PROPAGATE( ip_initialize());
255
//  ERROR_PROPAGATE( REGISTER_ME( SERVICE_ARP, & phonehash ));
256
//  ERROR_PROPAGATE( arp_initialize());
257
//  ERROR_PROPAGATE( REGISTER_ME( SERVICE_RARP, & phonehash ));
258
//  ERROR_PROPAGATE( rarp_initialize());
259
//  ERROR_PROPAGATE( REGISTER_ME( SERVICE_ICMP, & phonehash ));
260
//  ERROR_PROPAGATE( icmp_initialize());
261
//  ERROR_PROPAGATE( REGISTER_ME( SERVICE_UDP, & phonehash ));
262
//  ERROR_PROPAGATE( udp_initialize());
263
    ERROR_PROPAGATE( REGISTER_ME( SERVICE_TCP, & phonehash ));
264
    ERROR_PROPAGATE( tcp_initialize());
265
//  ERROR_PROPAGATE( REGISTER_ME( SERVICE_SOCKET, & phonehash ));
266
//  ERROR_PROPAGATE( socket_initialize());
267
//  ERROR_PROPAGATE( REGISTER_ME( SERVICE_ETHERNET, & phonehash ));
268
//  ERROR_PROPAGATE( ethernet_initialize());
269
#endif
270
#endif
271
 
272
    return EOK;
273
}
274
 
3901 mejdrech 275
//TODO as ip.c
276
int net_message( ipc_callid_t callid, ipc_call_t * call, ipc_call_t * answer, int * answer_count ){
3666 mejdrech 277
    ERROR_DECLARE;
278
 
279
    measured_string_ref strings;
280
    char *          data;
281
    int         index;
282
    measured_string_ref setting;
283
    measured_strings_ref    configuration;
284
    netif_ref       netif;
285
 
3901 mejdrech 286
    * answer_count = 0;
287
    switch( IPC_GET_METHOD( * call )){
3666 mejdrech 288
        case IPC_M_PHONE_HUNGUP:
289
            return EOK;
3846 mejdrech 290
        case NET_NET_DEVICE:
3666 mejdrech 291
            // TODO configure, register
3901 mejdrech 292
            printf( "\nNetworking: new netif %d", IPC_GET_DEVICE( call ));
3666 mejdrech 293
            return EOK;
3846 mejdrech 294
        case NET_NET_GET_DEVICE_CONF:
3901 mejdrech 295
            ERROR_PROPAGATE( measured_strings_receive( & strings, & data, IPC_GET_COUNT( call )));
296
            netif = netifs_find( & networking_globals.netifs, IPC_GET_DEVICE( call ));
3666 mejdrech 297
            if( netif ){
298
                configuration = & netif->configuration;
299
            }else{
300
                configuration = NULL;
301
            }
3901 mejdrech 302
            for( index = 0; index < IPC_GET_COUNT( call ); ++ index ){
3846 mejdrech 303
                setting = measured_strings_find( configuration, strings[ index ].value, 0 );
3666 mejdrech 304
                if( ! setting ){
3846 mejdrech 305
                    setting = measured_strings_find( & networking_globals.configuration, strings[ index ].value, 0 );
3666 mejdrech 306
                }
307
                if( setting ){
308
                    strings[ index ].length = setting->length;
309
                    strings[ index ].value = setting->value;
310
                }else{
311
                    strings[ index ].length = 0;
312
                    strings[ index ].value = NULL;
313
                }
314
            }
315
            // strings should not contain received data anymore
316
            free( data );
3901 mejdrech 317
            ERROR_CODE = measured_strings_reply( strings, IPC_GET_COUNT( call ));
3666 mejdrech 318
            free( strings );
319
            return ERROR_CODE;
3846 mejdrech 320
        case NET_NET_GET_CONF:
3666 mejdrech 321
            // arg1 = count
3901 mejdrech 322
            ERROR_PROPAGATE( measured_strings_receive( & strings, & data, IPC_GET_COUNT( call )));
323
            for( index = 0; index < IPC_GET_COUNT( call ); ++ index ){
3846 mejdrech 324
                setting = measured_strings_find( & networking_globals.configuration, strings[ index ].value, 0 );
3666 mejdrech 325
                if( setting ){
326
                    strings[ index ].length = setting->length;
327
                    strings[ index ].value = setting->value;
328
                }else{
329
                    strings[ index ].length = 0;
330
                    strings[ index ].value = NULL;
331
                }
332
            }
333
            // strings should not contain received data anymore
334
            free( data );
3901 mejdrech 335
            ERROR_CODE = measured_strings_reply( strings, IPC_GET_COUNT( call ));
3666 mejdrech 336
            free( strings );
337
            return ERROR_CODE;
3846 mejdrech 338
        case NET_NET_STARTUP:
3666 mejdrech 339
            return startup();
340
    }
341
    return ENOTSUP;
342
}
343
 
344
/*
345
int parse_line( measured_strings_ref configuration, char * line ){
346
    ERROR_DECLARE;
347
 
348
    measured_string_ref setting;
349
    char *          name;
350
    char *          value;
351
 
352
    // from the beginning
353
    name = line;
354
    // skip spaces
355
    while( isspace( * name )) ++ name;
356
    // remember the name start
357
    value = name;
358
    // skip the name
359
    while( isalnum( * value ) || ( * value == '_' )){
360
        // make uppercase
361
//      * value = toupper( * value );
362
        ++ value;
363
    }
364
    if( * value == '=' ){
365
        // terminate the name
366
        * value = '\0';
367
    }else{
368
        // terminate the name
369
        * value = '\0';
370
        // skip until '='
371
        ++ value;
372
        while(( * value ) && ( * value != '=' )) ++ value;
373
        // not found?
374
        if( * value != '=' ) return EINVAL;
375
    }
376
    ++ value;
377
    // skip spaces
378
    while( isspace( * value )) ++ value;
379
    // create a bulk measured string till the end
380
    setting = measured_string_create_bulk( value, -1 );
381
    if( ! setting ) return ENOMEM;
382
    // add the configuration setting
3912 mejdrech 383
    if( ERROR_OCCURRED( measured_strings_add( configuration, name, 0, setting ))){
3666 mejdrech 384
        free( setting );
385
        return ERROR_CODE;
386
    }
387
    return EOK;
388
}
389
*/
390
 
391
int add_configuration( measured_strings_ref configuration, const char * name, const char * value ){
392
    ERROR_DECLARE;
393
 
394
    measured_string_ref setting;
395
 
396
    setting = measured_string_create_bulk( value, 0 );
397
    if( ! setting ) return ENOMEM;
398
    // add the configuration setting
3912 mejdrech 399
    if( ERROR_OCCURRED( measured_strings_add( configuration, name, 0, setting ))){
3666 mejdrech 400
        free( setting );
401
        return ERROR_CODE;
402
    }
403
    return EOK;
404
}
405
 
3846 mejdrech 406
device_id_t generate_new_device_id( void ){
4163 mejdrech 407
    return device_assign_devno();
3666 mejdrech 408
}
409
 
410
int read_configuration( void ){
411
    ERROR_DECLARE;
412
 
4192 mejdrech 413
    // read general configuration
414
    ERROR_PROPAGATE( add_configuration( & networking_globals.configuration, "IPV", "4" ));
415
    ERROR_PROPAGATE( add_configuration( & networking_globals.configuration, "MTU", "1500" ));
416
    return EOK;
417
}
418
 
419
int read_netif_configuration( char * name, netif_ref netif ){
420
    ERROR_DECLARE;
421
 
422
    if( strncmp( name, "lo", 2 ) == 0 ){
423
        ERROR_PROPAGATE( add_configuration( & netif->configuration, "NAME", LO_NAME ));
424
        ERROR_PROPAGATE( add_configuration( & netif->configuration, "NETIF", LO_NAME ));
425
        ERROR_PROPAGATE( add_configuration( & netif->configuration, "IL", IP_NAME ));
426
        ERROR_PROPAGATE( add_configuration( & netif->configuration, "IP_CONFIG", "static" ));
427
        ERROR_PROPAGATE( add_configuration( & netif->configuration, "IP_ADDR", "127.0.0.1" ));
428
        ERROR_PROPAGATE( add_configuration( & netif->configuration, "NETMASK", "255.0.0.0" ));
429
    }else if( strncmp( name, "ne2k", 4 ) == 0 ){
430
        ERROR_PROPAGATE( add_configuration( & netif->configuration, "NAME", "eth0" ));
431
        ERROR_PROPAGATE( add_configuration( & netif->configuration, "NETIF", DP8390_NAME ));
432
        ERROR_PROPAGATE( add_configuration( & netif->configuration, "NIL", ETHERNET_NAME ));
433
        ERROR_PROPAGATE( add_configuration( & netif->configuration, "IL", IP_NAME ));
434
        ERROR_PROPAGATE( add_configuration( & netif->configuration, "IRQ", "9" ));
435
        ERROR_PROPAGATE( add_configuration( & netif->configuration, "IO", "300" ));
4243 mejdrech 436
        ERROR_PROPAGATE( add_configuration( & netif->configuration, "MTU", "1492" ));
4192 mejdrech 437
        ERROR_PROPAGATE( add_configuration( & netif->configuration, "IP_CONFIG", "static" ));
438
        ERROR_PROPAGATE( add_configuration( & netif->configuration, "IP_ADDR", "10.0.2.5" ));
439
        ERROR_PROPAGATE( add_configuration( & netif->configuration, "NETMASK", "255.255.255.0" ));
440
        ERROR_PROPAGATE( add_configuration( & netif->configuration, "BROADCAST", "10.0.2.255" ));
441
        ERROR_PROPAGATE( add_configuration( & netif->configuration, "GATEWAY", "10.0.2.2" ));
442
        ERROR_PROPAGATE( add_configuration( & netif->configuration, "DNS1", "10.0.2.2" ));
443
        ERROR_PROPAGATE( add_configuration( & netif->configuration, "DNS2", "10.0.2.2" ));
444
        ERROR_PROPAGATE( add_configuration( & netif->configuration, "ARP", "arp" ));
445
    }
446
    return EOK;
447
}
448
 
449
int start_device( netif_ref netif ){
450
    ERROR_DECLARE;
451
 
3666 mejdrech 452
    measured_string_ref setting;
4163 mejdrech 453
    services_t          internet_service;
454
    int                 irq;
455
    int                 io;
456
    int                 mtu;
3666 mejdrech 457
 
458
    // mandatory netif
4163 mejdrech 459
    setting = measured_strings_find( & netif->configuration, CONF_NETIF, 0 );
4192 mejdrech 460
    netif->driver = get_running_module( & networking_globals.modules, setting->value );
3846 mejdrech 461
    if( ! netif->driver ){
4192 mejdrech 462
        printf( "\nFailed to start the network interface driver %s", setting->value );
3666 mejdrech 463
        return EINVAL;
464
    }
4163 mejdrech 465
    // optional network interface layer
466
    setting = measured_strings_find( & netif->configuration, CONF_NIL, 0 );
3666 mejdrech 467
    if( setting ){
4192 mejdrech 468
        netif->nil = get_running_module( & networking_globals.modules, setting->value );
3846 mejdrech 469
        if( ! netif->nil ){
4192 mejdrech 470
            printf( "\nFailed to start the network interface layer %s", setting->value );
3666 mejdrech 471
            return EINVAL;
472
        }
473
    }else{
3846 mejdrech 474
        netif->nil = NULL;
3666 mejdrech 475
    }
476
    // mandatory internet layer
4163 mejdrech 477
    setting = measured_strings_find( & netif->configuration, CONF_IL, 0 );
4192 mejdrech 478
    netif->il = get_running_module( & networking_globals.modules, setting->value );
3846 mejdrech 479
    if( ! netif->il ){
4192 mejdrech 480
        printf( "\nFailed to start the internet layer %s", setting->value );
3666 mejdrech 481
        return EINVAL;
482
    }
3685 mejdrech 483
    // end of the static loopback initialization
484
    // startup the loopback interface
4163 mejdrech 485
    setting = measured_strings_find( & netif->configuration, CONF_IRQ, 0 );
486
    irq = setting ? strtol( setting->value, NULL, 10 ) : 0;
487
    setting = measured_strings_find( & netif->configuration, CONF_IO, 0 );
488
    io = setting ? strtol( setting->value, NULL, 16 ) : 0;
4243 mejdrech 489
    ERROR_PROPAGATE( netif_probe_req( netif->driver->phone, netif->id, irq, io ));
3846 mejdrech 490
    if( netif->nil ){
4163 mejdrech 491
        setting = measured_strings_find( & netif->configuration, CONF_MTU, 0 );
492
        if( ! setting ){
493
            setting = measured_strings_find( & networking_globals.configuration, CONF_MTU, 0 );
494
        }
495
        mtu = setting ? strtol( setting->value, NULL, 10 ) : 0;
4243 mejdrech 496
        ERROR_PROPAGATE( async_req_3_0( netif->nil->phone, NET_NIL_DEVICE, netif->id, mtu, netif->driver->service ));
3846 mejdrech 497
        internet_service = netif->nil->service;
3685 mejdrech 498
    }else{
3846 mejdrech 499
        internet_service = netif->driver->service;
3685 mejdrech 500
    }
3846 mejdrech 501
    // TODO IL_BUNDLE
502
    ERROR_PROPAGATE( async_req_2_0( netif->il->phone, NET_IL_DEVICE, netif->id, internet_service ));
4243 mejdrech 503
    ERROR_PROPAGATE( netif_start_req( netif->driver->phone, netif->id ));
3666 mejdrech 504
    return EOK;
505
}
506
 
507
int startup( void ){
508
    ERROR_DECLARE;
509
 
4192 mejdrech 510
    char *      conf_files[] = { "lo", "ne2k" };
511
    int         count = sizeof( conf_files ) / sizeof( char * );
512
    int         index;
513
    netif_ref   netif;
514
    int         i;
515
    measured_string_ref setting;
3666 mejdrech 516
 
4192 mejdrech 517
    // TODO dynamic configuration
518
    ERROR_PROPAGATE( read_configuration());
519
 
520
    for( i = 0; i < count; ++ i ){
521
        netif = ( netif_ref ) malloc( sizeof( netif_t ));
522
        if( ! netif ) return ENOMEM;
523
 
524
        netif->id = generate_new_device_id();
525
        if( ! netif->id ) return EXDEV;
526
        ERROR_PROPAGATE( measured_strings_initialize( & netif->configuration ));
527
        // read configuration files
528
        if( ERROR_OCCURRED( read_netif_configuration( conf_files[ i ], netif ))){
529
            measured_strings_destroy( & netif->configuration );
530
            free( netif );
531
            return ERROR_CODE;
532
        }
533
        // mandatory name
534
        setting = measured_strings_find( & netif->configuration, CONF_NAME, 0 );
535
        if( ! setting ){
536
            printf( "\nThe name is missing" );
537
            measured_strings_destroy( & netif->configuration );
538
            free( netif );
539
            return EINVAL;
540
        }
541
        netif->name = setting->value;
542
        // add to the netifs map
543
        index = netifs_add( & networking_globals.netifs, netif->id, netif );
544
        if( index < 0 ){
545
            measured_strings_destroy( & netif->configuration );
546
            free( netif );
547
            return index;
548
        }
549
        // add to the netif names map
550
        if( ERROR_OCCURRED( char_map_add( & networking_globals.netif_names, netif->name, 0, index ))
551
        // start network interfaces and needed modules
552
        || ERROR_OCCURRED( start_device( netif ))){
553
            measured_strings_destroy( & netif->configuration );
554
            netifs_exclude_index( & networking_globals.netifs, index );
555
            return ERROR_CODE;
556
        }
557
        // increment modules' usage
558
        ++ netif->driver->usage;
559
        if( netif->nil ) ++ netif->nil->usage;
560
        ++ netif->il->usage;
561
        printf( "\nNew network interface started:\n\tname\t= %s\n\tid\t= %d\n\tdriver\t= %s\n\tnil\t= %s\n\til\t= %s", netif->name, netif->id, netif->driver->name, netif->nil ? netif->nil->name : NULL, netif->il->name );
562
    }
3666 mejdrech 563
    return EOK;
564
}
565
 
3466 mejdrech 566
/** @}
567
 */