Subversion Repositories HelenOS

Compare Revisions

No changes between revisions

Ignore whitespace Rev 4306 → Rev 4307

/branches/network/uspace/srv/net/net/net.c
0,0 → 1,386
/*
* Copyright (c) 2009 Lukas Mejdrech
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
*
* - Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* - Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
* - The name of the author may not be used to endorse or promote products
* derived from this software without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
* IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
* OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
* IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
* INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
* NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
* DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
* THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
* THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
 
/** @addtogroup net
* @{
*/
 
/** @file
*/
 
#include <async.h>
#include <ctype.h>
#include <ddi.h>
#include <errno.h>
#include <malloc.h>
#include <stdio.h>
 
#include <ipc/ipc.h>
#include <ipc/services.h>
 
#include "../err.h"
#include "../messages.h"
#include "../modules.h"
//#include "../self_test.h"
 
#include "../structures/char_map.h"
#include "../structures/generic_char_map.h"
#include "../structures/measured_strings.h"
#include "../structures/module_map.h"
#include "../structures/packet/packet.h"
 
#include "../il/il_messages.h"
#include "../include/device.h"
#include "../include/netif_interface.h"
#include "../include/nil_interface.h"
#include "../include/net_interface.h"
#include "../include/ip_interface.h"
 
#include "net.h"
 
#define NAME "Networking"
 
void module_print_name( void );
measured_string_ref configuration_find( measured_strings_ref configuration, const char * name );
int module_start( async_client_conn_t client_connection );
//int parse_line( measured_strings_ref configuration, char * line );
int read_configuration( void );
int start_device( netif_ref netif );
int startup( void );
device_id_t generate_new_device_id( void );
int net_get_conf( measured_strings_ref device_conf, measured_string_ref configuration, int count, char ** data );
 
net_globals_t net_globals;
 
DEVICE_MAP_IMPLEMENT( netifs, netif_t )
 
GENERIC_CHAR_MAP_IMPLEMENT( measured_strings, measured_string_t )
 
void module_print_name( void ){
printf( "%s", NAME );
}
 
int module_start( async_client_conn_t client_connection ){
ERROR_DECLARE;
 
ipcarg_t phonehash;
 
async_set_client_connection( client_connection );
ERROR_PROPAGATE( pm_init());
if( ERROR_OCCURRED( net_initialize())
|| ERROR_OCCURRED( REGISTER_ME( SERVICE_NETWORKING, & phonehash ))){
pm_destroy();
return ERROR_CODE;
}
 
async_manager();
 
pm_destroy();
return EOK;
}
 
int net_get_device_conf_req( int net_phone, device_id_t device_id, measured_string_ref * configuration, int count, char ** data ){
netif_ref netif;
 
if( !( configuration && ( count > 0 ))) return EINVAL;
netif = netifs_find( & net_globals.netifs, device_id );
if( netif ){
return net_get_conf( & netif->configuration, * configuration, count, data );
}else{
return net_get_conf( NULL, * configuration, count, data );
}
}
 
int net_get_conf_req( int net_phone, measured_string_ref * configuration, int count, char ** data ){
if( !( configuration && ( count > 0 ))) return EINVAL;
return net_get_conf( NULL, * configuration, count, data );
}
 
int net_get_conf( measured_strings_ref device_conf, measured_string_ref configuration, int count, char ** data ){
measured_string_ref setting;
int index;
 
if( data ) * data = NULL;
for( index = 0; index < count; ++ index ){
setting = measured_strings_find( device_conf, configuration[ index ].value, 0 );
if( ! setting ){
setting = measured_strings_find( & net_globals.configuration, configuration[ index ].value, 0 );
}
if( setting ){
configuration[ index ].length = setting->length;
configuration[ index ].value = setting->value;
}else{
configuration[ index ].length = 0;
configuration[ index ].value = NULL;
}
}
return EOK;
}
 
void net_free_settings( measured_string_ref settings, char * data ){
}
 
int net_connect_module( services_t service ){
return EOK;
}
 
int net_message( ipc_callid_t callid, ipc_call_t * call, ipc_call_t * answer, int * answer_count ){
ERROR_DECLARE;
 
measured_string_ref strings;
char * data;
 
* answer_count = 0;
switch( IPC_GET_METHOD( * call )){
case IPC_M_PHONE_HUNGUP:
return EOK;
case NET_NET_DEVICE:
// TODO configure, register
printf( "Networking: new netif %d\n", IPC_GET_DEVICE( call ));
return EOK;
case NET_NET_GET_DEVICE_CONF:
ERROR_PROPAGATE( measured_strings_receive( & strings, & data, IPC_GET_COUNT( call )));
net_get_device_conf_req( 0, IPC_GET_DEVICE( call ), & strings, IPC_GET_COUNT( call ), NULL );
// strings should not contain received data anymore
free( data );
ERROR_CODE = measured_strings_reply( strings, IPC_GET_COUNT( call ));
free( strings );
return ERROR_CODE;
case NET_NET_GET_CONF:
ERROR_PROPAGATE( measured_strings_receive( & strings, & data, IPC_GET_COUNT( call )));
net_get_conf_req( 0, & strings, IPC_GET_COUNT( call ), NULL );
// strings should not contain received data anymore
free( data );
ERROR_CODE = measured_strings_reply( strings, IPC_GET_COUNT( call ));
free( strings );
return ERROR_CODE;
case NET_NET_STARTUP:
return startup();
}
return ENOTSUP;
}
 
/*
int parse_line( measured_strings_ref configuration, char * line ){
ERROR_DECLARE;
 
measured_string_ref setting;
char * name;
char * value;
 
// from the beginning
name = line;
// skip spaces
while( isspace( * name )) ++ name;
// remember the name start
value = name;
// skip the name
while( isalnum( * value ) || ( * value == '_' )){
// make uppercase
// * value = toupper( * value );
++ value;
}
if( * value == '=' ){
// terminate the name
* value = '\0';
}else{
// terminate the name
* value = '\0';
// skip until '='
++ value;
while(( * value ) && ( * value != '=' )) ++ value;
// not found?
if( * value != '=' ) return EINVAL;
}
++ value;
// skip spaces
while( isspace( * value )) ++ value;
// create a bulk measured string till the end
setting = measured_string_create_bulk( value, -1 );
if( ! setting ) return ENOMEM;
// add the configuration setting
if( ERROR_OCCURRED( measured_strings_add( configuration, name, 0, setting ))){
free( setting );
return ERROR_CODE;
}
return EOK;
}
*/
 
int add_configuration( measured_strings_ref configuration, const char * name, const char * value ){
ERROR_DECLARE;
 
measured_string_ref setting;
 
setting = measured_string_create_bulk( value, 0 );
if( ! setting ) return ENOMEM;
// add the configuration setting
if( ERROR_OCCURRED( measured_strings_add( configuration, name, 0, setting ))){
free( setting );
return ERROR_CODE;
}
return EOK;
}
 
device_id_t generate_new_device_id( void ){
return device_assign_devno();
}
 
int read_configuration( void ){
ERROR_DECLARE;
 
// read general configuration
ERROR_PROPAGATE( add_configuration( & net_globals.configuration, "IPV", "4" ));
ERROR_PROPAGATE( add_configuration( & net_globals.configuration, "MTU", "1500" ));
return EOK;
}
 
int start_device( netif_ref netif ){
ERROR_DECLARE;
 
measured_string_ref setting;
services_t internet_service;
int irq;
int io;
int mtu;
 
// mandatory netif
setting = measured_strings_find( & netif->configuration, CONF_NETIF, 0 );
netif->driver = get_running_module( & net_globals.modules, setting->value );
if( ! netif->driver ){
printf( "Failed to start the network interface driver %s\n", setting->value );
return EINVAL;
}
// optional network interface layer
setting = measured_strings_find( & netif->configuration, CONF_NIL, 0 );
if( setting ){
netif->nil = get_running_module( & net_globals.modules, setting->value );
if( ! netif->nil ){
printf( "Failed to start the network interface layer %s\n", setting->value );
return EINVAL;
}
}else{
netif->nil = NULL;
}
// mandatory internet layer
setting = measured_strings_find( & netif->configuration, CONF_IL, 0 );
netif->il = get_running_module( & net_globals.modules, setting->value );
if( ! netif->il ){
printf( "Failed to start the internet layer %s\n", setting->value );
return EINVAL;
}
// end of the static loopback initialization
// startup the loopback interface
setting = measured_strings_find( & netif->configuration, CONF_IRQ, 0 );
irq = setting ? strtol( setting->value, NULL, 10 ) : 0;
setting = measured_strings_find( & netif->configuration, CONF_IO, 0 );
io = setting ? strtol( setting->value, NULL, 16 ) : 0;
ERROR_PROPAGATE( netif_probe_req( netif->driver->phone, netif->id, irq, io ));
if( netif->nil ){
setting = measured_strings_find( & netif->configuration, CONF_MTU, 0 );
if( ! setting ){
setting = measured_strings_find( & net_globals.configuration, CONF_MTU, 0 );
}
mtu = setting ? strtol( setting->value, NULL, 10 ) : 0;
ERROR_PROPAGATE( nil_device_req( netif->nil->phone, netif->id, mtu, netif->driver->service ));
internet_service = netif->nil->service;
}else{
internet_service = netif->driver->service;
}
switch( netif->il->service ){
case SERVICE_IP:
ERROR_PROPAGATE( ip_device_req( netif->il->phone, netif->id, internet_service ));
break;
default:
return ENOENT;
}
ERROR_PROPAGATE( netif_start_req( netif->driver->phone, netif->id ));
return EOK;
}
 
int startup( void ){
ERROR_DECLARE;
 
char * conf_files[] = { "lo", "ne2k" };
int count = sizeof( conf_files ) / sizeof( char * );
int index;
netif_ref netif;
int i;
measured_string_ref setting;
 
// TODO dynamic configuration
ERROR_PROPAGATE( read_configuration());
 
for( i = 0; i < count; ++ i ){
netif = ( netif_ref ) malloc( sizeof( netif_t ));
if( ! netif ) return ENOMEM;
 
netif->id = generate_new_device_id();
if( ! netif->id ) return EXDEV;
ERROR_PROPAGATE( measured_strings_initialize( & netif->configuration ));
// read configuration files
if( ERROR_OCCURRED( read_netif_configuration( conf_files[ i ], netif ))){
measured_strings_destroy( & netif->configuration );
free( netif );
return ERROR_CODE;
}
// mandatory name
setting = measured_strings_find( & netif->configuration, CONF_NAME, 0 );
if( ! setting ){
printf( "The name is missing\n" );
measured_strings_destroy( & netif->configuration );
free( netif );
return EINVAL;
}
netif->name = setting->value;
// add to the netifs map
index = netifs_add( & net_globals.netifs, netif->id, netif );
if( index < 0 ){
measured_strings_destroy( & netif->configuration );
free( netif );
return index;
}
// add to the netif names map
if( ERROR_OCCURRED( char_map_add( & net_globals.netif_names, netif->name, 0, index ))
// start network interfaces and needed modules
|| ERROR_OCCURRED( start_device( netif ))){
measured_strings_destroy( & netif->configuration );
netifs_exclude_index( & net_globals.netifs, index );
return ERROR_CODE;
}
// increment modules' usage
++ netif->driver->usage;
if( netif->nil ) ++ netif->nil->usage;
++ netif->il->usage;
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 );
}
return EOK;
}
 
/** @}
*/
Property changes:
Added: svn:eol-style
+native
\ No newline at end of property
Added: svn:mergeinfo