Fix a few bugs, tentative tun multiqueue support
This commit is contained in:
@@ -14,12 +14,16 @@
|
||||
#include <arpa/inet.h>
|
||||
#include <netinet/ip.h>
|
||||
|
||||
#ifndef IFF_MULTI_QUEUE
|
||||
#define IFF_MULTI_QUEUE 0x0100
|
||||
#endif
|
||||
|
||||
void* xmalloc( size_t bytes )
|
||||
{
|
||||
void* result = malloc( bytes );
|
||||
|
||||
if ( bytes > 0 && result == NULL ) {
|
||||
warn( "Couldn't allocate memory, exiting!" );
|
||||
warn( "Couldn't allocate %zu bytes, exiting!", bytes );
|
||||
exit(2);
|
||||
|
||||
}
|
||||
@@ -28,25 +32,52 @@ void* xmalloc( size_t bytes )
|
||||
return result;
|
||||
}
|
||||
|
||||
int create_tun( const char* name )
|
||||
int tun_has_multiqueue( int tun_fd )
|
||||
{
|
||||
int features;
|
||||
|
||||
if ( ioctl( tun_fd, TUNGETFEATURES, &features ) < 0) {
|
||||
warn("Kernel doesn't support TUNGETFEATURES, assuming no multiqueue");
|
||||
features = 0;
|
||||
}
|
||||
|
||||
return features & IFF_MULTI_QUEUE;
|
||||
}
|
||||
|
||||
int create_tun( const char *name, int multi )
|
||||
{
|
||||
int fd, err;
|
||||
struct ifreq ifr;
|
||||
|
||||
if ( ( fd = open( "/dev/net/tun", O_RDWR ) ) < 0 ) {
|
||||
warn( "Error %s opening tun to create %s", strerror(errno), name );
|
||||
if ( ( fd = open( "/dev/net/tun", O_RDWR ) ) < 0 ) {
|
||||
warn( "Error %s (%i) opening tun to create %s", strerror(errno), errno, name );
|
||||
return -1;
|
||||
}
|
||||
|
||||
memset( &ifr, 0, sizeof( struct ifreq ) );
|
||||
|
||||
ifr.ifr_flags = IFF_TUN | IFF_NO_PI | IFF_UP;
|
||||
ifr.ifr_flags = IFF_TUN | IFF_NO_PI;
|
||||
|
||||
if ( multi ) {
|
||||
if ( !tun_has_multiqueue( fd ) ) {
|
||||
warn( "multiqueue requested but kernel doesn't support it" );
|
||||
close( fd );
|
||||
return -1;
|
||||
}
|
||||
|
||||
debug( "Creating multi-queue device" );
|
||||
ifr.ifr_flags |= IFF_MULTI_QUEUE;
|
||||
}
|
||||
|
||||
|
||||
strncpy( ifr.ifr_name, name, IFNAMSIZ );
|
||||
|
||||
if ( (err = ioctl( fd, TUNSETIFF, (void*) &ifr ) ) < 0 ) {
|
||||
warn( "Error creating tun device %s: %s", name, strerror(errno) );
|
||||
close( fd );
|
||||
return -1;
|
||||
while ( (err = ioctl( fd, TUNSETIFF, (void*) &ifr ) ) < 0 ) {
|
||||
if ( errno != EBUSY ) {
|
||||
warn( "Error creating tun device %s: %s (%i)", name, strerror(errno), errno );
|
||||
close( fd );
|
||||
return -1;
|
||||
}
|
||||
}
|
||||
|
||||
return fd;
|
||||
@@ -82,14 +113,12 @@ int link_set_up( char *link_name, int state )
|
||||
}
|
||||
|
||||
|
||||
int session_setup( struct session *session, char *config_file, char *listen_if, char *output_if )
|
||||
int session_setup( struct session *session, char *config_file, char *ifname, int multi )
|
||||
{
|
||||
memset( session, 0, sizeof( struct session ) );
|
||||
session->listen_if = -1;
|
||||
session->output_if = -1;
|
||||
|
||||
session->rlocs = rlocs_new( config_file );
|
||||
|
||||
|
||||
if ( session->rlocs == NULL ) {
|
||||
warn( "Failed to get config from %s", config_file );
|
||||
return 0;
|
||||
@@ -97,35 +126,14 @@ int session_setup( struct session *session, char *config_file, char *listen_if,
|
||||
|
||||
rlocs_debug_output( session->rlocs );
|
||||
|
||||
// TODO: We can scale the tun architecture by using multiqueue and having
|
||||
// a bunch of workers, rather than this noddy scheme. If we don't jump
|
||||
// directly to something saner, anyway...
|
||||
|
||||
session->listen_if = create_tun( listen_if );
|
||||
if ( session->listen_if == -1 ) {
|
||||
warn( "Error opening %s for listening", listen_if );
|
||||
session->fd = create_tun( ifname, multi );
|
||||
if ( session->fd == -1 ) {
|
||||
warn( "Error opening %s for listening", ifname );
|
||||
rlocs_free( session->rlocs );
|
||||
return 0;
|
||||
}
|
||||
link_set_up( listen_if, 1 );
|
||||
|
||||
|
||||
if ( strcmp( listen_if, output_if ) == 0 ) {
|
||||
session->same_if = 1;
|
||||
session->output_if = session->listen_if;
|
||||
} else {
|
||||
session->same_if = 0;
|
||||
session->output_if = create_tun( output_if );
|
||||
link_set_up( ifname, 1 );
|
||||
|
||||
if ( session->output_if == -1 ) {
|
||||
warn( "Error opening %s for outputting", output_if );
|
||||
rlocs_free( session->rlocs );
|
||||
close( session->listen_if );
|
||||
return 0;
|
||||
}
|
||||
link_set_up( output_if, 1 );
|
||||
}
|
||||
|
||||
return 1;
|
||||
}
|
||||
|
||||
@@ -164,7 +172,7 @@ int session_upgrade_rlocs( struct session *session, int argc, char** args )
|
||||
return 0;
|
||||
}
|
||||
|
||||
if ( !rloc_add_private_key( rloc, filename ) ) {
|
||||
if ( !rlocs_add_private_key( session->rlocs, rloc, filename ) ) {
|
||||
warn( "Couldn't upgrade rloc %s with %s", rloc_str, filename );
|
||||
return 0;
|
||||
}
|
||||
@@ -179,12 +187,8 @@ int session_upgrade_rlocs( struct session *session, int argc, char** args )
|
||||
void session_teardown( struct session *session )
|
||||
{
|
||||
rlocs_free( session->rlocs );
|
||||
if ( session->listen_if >= 0 ) {
|
||||
close( session->listen_if );
|
||||
}
|
||||
|
||||
if ( session->output_if >= 0 && !session->same_if ) {
|
||||
close( session->output_if );
|
||||
if ( session->fd >= 0 ) {
|
||||
close( session->fd );
|
||||
}
|
||||
}
|
||||
|
||||
@@ -204,4 +208,4 @@ int sha256sum( unsigned char *src, size_t src_len, unsigned char dst[SHA256_DIGE
|
||||
|
||||
|
||||
return size == SHA256_DIGEST_LENGTH;
|
||||
}
|
||||
}
|
||||
|
Reference in New Issue
Block a user