The following commit has been merged in the master branch: commit d67d855338f6d0cc3f7a29b0ca3f143695f8e590 Author: Marek Lindner lindner_marek@yahoo.de Date: Wed Dec 6 12:37:18 2006 +0100
- performance && memory usage improvements
diff --git a/batman.c b/batman.c index 6f17ac0..f1c1a8c 100644 --- a/batman.c +++ b/batman.c @@ -567,10 +567,10 @@ static void debug() { output( "------------------ DEBUG ------------------\n" ); output( "Forward list\n" );
- list_for_each(forw_pos, &forw_list) { - forw_node = list_entry(forw_pos, struct forw_node, list); - addr_to_string(forw_node->pack.orig, str, sizeof (str)); - output(" %s at %u\n", str, forw_node->when); + list_for_each( forw_pos, &forw_list ) { + forw_node = list_entry( forw_pos, struct forw_node, list ); + addr_to_string( ((struct packet *)forw_node->pack_buff)->orig, str, sizeof (str) ); + output( " %s at %u\n", str, forw_node->when ); }
output( "Originator list\n" ); @@ -776,158 +776,143 @@ void update_originator( struct packet *in, unsigned int neigh, struct batman_if
}
-void schedule_forward_packet( struct packet *in, int unidirectional, int direct_link, struct orig_node *orig_node, unsigned int neigh, unsigned char *hna_recv_buff, int hna_buff_len, struct batman_if *if_outgoing ) { +void schedule_forward_packet( struct packet *in, int unidirectional, int directlink, struct orig_node *orig_node, unsigned int neigh, unsigned char *hna_recv_buff, int hna_buff_len, struct batman_if *if_outgoing ) {
- struct forw_node *forw_node = NULL, *forw_node_new; - struct list_head *forw_pos, *if_pos; - struct batman_if *batman_if; + struct forw_node *forw_node_new;
if ( debug_level == 4 ) output( "schedule_forward_packet(): \n" );
if ( in->ttl <= 1 ) { + if ( debug_level == 4 ) output( "ttl exceeded \n" ); + } else {
- /* list_for_each(forw_pos, &forw_list) { - forw_node = list_entry(forw_pos, struct forw_node, list); - if ((int)(forw_node->when - forw_node_new->when) > 0) - break; - } */ + forw_node_new = alloc_memory( sizeof(struct forw_node) );
- if ( unidirectional ) { + INIT_LIST_HEAD(&forw_node_new->list);
- forw_node_new = alloc_memory(sizeof (struct forw_node)); - INIT_LIST_HEAD(&forw_node_new->list); + if ( hna_buff_len > 0 ) {
- memcpy(&forw_node_new->pack, in, sizeof (struct packet)); + forw_node_new->pack_buff = alloc_memory( sizeof(struct packet) + hna_buff_len ); + memcpy( forw_node_new->pack_buff, in, sizeof(struct packet) ); + memcpy( forw_node_new->pack_buff + sizeof(struct packet), hna_buff, hna_buff_len ); + forw_node_new->pack_buff_len = sizeof(struct packet) + hna_buff_len;
- forw_node_new->pack.ttl--; - forw_node_new->when = get_time(); + } else {
- if ( hna_buff_len > 0 ) { + forw_node_new->pack_buff = alloc_memory( sizeof(struct packet) ); + memcpy( &forw_node_new->pack_buff, in, sizeof(struct packet) ); + forw_node_new->pack_buff_len = sizeof(struct packet);
- forw_node_new->hna_buff = alloc_memory( hna_buff_len ); - forw_node_new->hna_buff_len = hna_buff_len; + }
- memcpy( forw_node_new->hna_buff, hna_recv_buff, hna_buff_len );
- } else { + ((struct packet *)forw_node_new->pack_buff)->ttl--; + forw_node_new->when = get_time();
- forw_node_new->hna_buff = NULL; - forw_node_new->hna_buff_len = 0; + forw_node_new->if_outgoing = if_outgoing;
- } + /* list_for_each(forw_pos, &forw_list) { + forw_node = list_entry(forw_pos, struct forw_node, list); + if ((int)(forw_node->when - forw_node_new->when) > 0) + break; + } */
- forw_node_new->pack.flags = ( UNIDIRECTIONAL | DIRECTLINK ); + if ( unidirectional ) {
- if ( if_outgoing != NULL ) { + ((struct packet *)forw_node_new->pack_buff)->flags = ( UNIDIRECTIONAL | DIRECTLINK );
- forw_node_new->if_outgoing = if_outgoing; - list_add( &forw_node_new->list, /* ( forw_node == NULL ? &forw_list : forw_pos )*/ &forw_list ); + } else if ( directlink ) {
- } else { + ((struct packet *)forw_node_new->pack_buff)->flags = DIRECTLINK;
- do_log( "Error - can't forward packet with UDF: outgoing iface not specified \n", "" ); + } else {
- } + ((struct packet *)forw_node_new->pack_buff)->flags = 0x00;
- } else { + }
- if ( ( direct_link ) && ( if_outgoing == NULL ) ) { + list_add( &forw_node_new->list, &forw_list );
- do_log( "Error - can't forward packet with IDF: outgoing iface not specified \n", "" ); + }
- } else { +}
- list_for_each(if_pos, &if_list) { +void send_outstanding_packets() {
- batman_if = list_entry(if_pos, struct batman_if, list); + struct forw_node *forw_node; + struct list_head *forw_pos, *if_pos, *temp; + struct batman_if *batman_if; + static char orig_str[ADDR_STR_LEN]; + int directlink;
- forw_node_new = alloc_memory(sizeof (struct forw_node)); - INIT_LIST_HEAD(&forw_node_new->list); + if ( list_empty( &forw_list ) ) + return;
- memcpy(&forw_node_new->pack, in, sizeof (struct packet)); + list_for_each_safe( forw_pos, temp, &forw_list ) { + forw_node = list_entry( forw_pos, struct forw_node, list );
- forw_node_new->pack.ttl--; - forw_node_new->when = get_time(); + if ( forw_node->when <= get_time() ) {
- if ( hna_buff_len > 0 ) { + if ( debug_level == 4 ) + addr_to_string( ((struct packet *)forw_node->pack_buff)->orig, orig_str, ADDR_STR_LEN );
- forw_node_new->hna_buff = alloc_memory( hna_buff_len ); - forw_node_new->hna_buff_len = hna_buff_len;
- memcpy( forw_node_new->hna_buff, hna_recv_buff, hna_buff_len ); + if ( ((struct packet *)forw_node->pack_buff)->flags & UNIDIRECTIONAL ) {
- } else { + if ( forw_node->if_outgoing != NULL ) {
- forw_node_new->hna_buff = NULL; - forw_node_new->hna_buff_len = 0; + if ( debug_level == 4 ) + output( "Forwarding packet (originator %s, seqno %d, TTL %d) on interface %s\n", orig_str, ((struct packet *)forw_node->pack_buff)->seqno, ((struct packet *)forw_node->pack_buff)->ttl, forw_node->if_outgoing->dev );
+ if ( send_packet( forw_node->pack_buff, forw_node->pack_buff_len, &forw_node->if_outgoing->broad, forw_node->if_outgoing->udp_send_sock ) < 0 ) { + exit( -1 ); }
- if ( ( direct_link ) && ( if_outgoing == batman_if ) ) - forw_node_new->pack.flags = DIRECTLINK; - else - forw_node_new->pack.flags = 0x00; + } else {
- forw_node_new->if_outgoing = batman_if; - list_add( &forw_node_new->list, &forw_list ); + do_log( "Error - can't forward packet with UDF: outgoing iface not specified \n", "" );
}
- } + } else {
- } + directlink = ( ( ((struct packet *)forw_node->pack_buff)->flags & DIRECTLINK ) ? 1 : 0 );
- } + if ( ( directlink ) && ( forw_node->if_outgoing == NULL ) ) {
-} - -void send_outstanding_packets() { + do_log( "Error - can't forward packet with IDF: outgoing iface not specified \n", "" );
- struct forw_node *forw_node; - struct list_head *forw_pos, *temp; - static char orig_str[ADDR_STR_LEN]; - unsigned char *send_buff; - - if ( list_empty( &forw_list ) ) - return; - - list_for_each_safe( forw_pos, temp, &forw_list ) { - forw_node = list_entry( forw_pos, struct forw_node, list ); + } else {
- if ( forw_node->when <= get_time() ) { + list_for_each(if_pos, &if_list) {
- if ( forw_node->hna_buff_len > 0 ) { + batman_if = list_entry(if_pos, struct batman_if, list);
- send_buff = alloc_memory(sizeof (struct packet) + forw_node->hna_buff_len); - memcpy(send_buff, (unsigned char *)&forw_node->pack, sizeof (struct packet)); - memcpy(send_buff + sizeof (struct packet), forw_node->hna_buff, forw_node->hna_buff_len); + if ( ( directlink ) && ( forw_node->if_outgoing == batman_if ) ) + ((struct packet *)forw_node->pack_buff)->flags = DIRECTLINK; + else + ((struct packet *)forw_node->pack_buff)->flags = 0x00;
- } else { + if ( debug_level == 4 ) + output( "Forwarding packet (originator %s, seqno %d, TTL %d) on interface %s\n", orig_str, ((struct packet *)forw_node->pack_buff)->seqno, ((struct packet *)forw_node->pack_buff)->ttl, batman_if->dev );
- send_buff = alloc_memory(sizeof (struct packet)); - memcpy( send_buff, (unsigned char *)&forw_node->pack, sizeof (struct packet) ); + if ( send_packet( forw_node->pack_buff, forw_node->pack_buff_len, &batman_if->broad, batman_if->udp_send_sock ) < 0 ) { + exit( -1 ); + }
- } + }
- if ( debug_level == 4 ) { - addr_to_string( forw_node->pack.orig, orig_str, ADDR_STR_LEN ); - output( "Forwarding packet (originator %s, seqno %d, TTL %d) on interface %s\n", orig_str, forw_node->pack.seqno, forw_node->pack.ttl, forw_node->if_outgoing->dev ); - } + }
- if ( send_packet( send_buff, sizeof (struct packet) + forw_node->hna_buff_len, &forw_node->if_outgoing->broad, forw_node->if_outgoing->udp_send_sock ) < 0 ) { - exit( -1 ); }
- free_memory( send_buff ); list_del( forw_pos );
- if ( forw_node->hna_buff_len > 0 ) - free_memory( forw_node->hna_buff ); - + free_memory( forw_node->pack_buff ); free_memory( forw_node );
} @@ -939,8 +924,8 @@ void send_outstanding_packets() { void schedule_own_packet() {
struct forw_node *forw_node_new; - struct list_head *if_pos, *if_pos_out; - struct batman_if *batman_if, *if_outgoing; + struct list_head *if_pos; + struct batman_if *batman_if; int curr_time;
@@ -952,37 +937,30 @@ void schedule_own_packet() {
batman_if = list_entry(if_pos, struct batman_if, list);
- list_for_each(if_pos_out, &if_list) { - - if_outgoing = list_entry(if_pos_out, struct batman_if, list); + forw_node_new = alloc_memory( sizeof(struct forw_node) );
- forw_node_new = alloc_memory(sizeof (struct forw_node)); - - INIT_LIST_HEAD(&forw_node_new->list); - - memcpy(&forw_node_new->pack, &batman_if->out, sizeof (struct packet)); - - if ( num_hna > 0 ) { - - forw_node_new->hna_buff = alloc_memory( num_hna * 5 * sizeof( unsigned char ) ); - forw_node_new->hna_buff_len = num_hna * 5 * sizeof( unsigned char ); - - memcpy( forw_node_new->hna_buff, hna_buff, num_hna * 5 * sizeof( unsigned char ) ); + INIT_LIST_HEAD(&forw_node_new->list);
- } else { + forw_node_new->when = curr_time + rand_num( JITTER ); + forw_node_new->if_outgoing = NULL;
- forw_node_new->hna_buff = NULL; - forw_node_new->hna_buff_len = 0; + if ( num_hna > 0 ) {
- } + forw_node_new->pack_buff = alloc_memory( sizeof(struct packet) + num_hna * 5 * sizeof(unsigned char) ); + memcpy( forw_node_new->pack_buff, (unsigned char *)&batman_if->out, sizeof(struct packet) ); + memcpy( forw_node_new->pack_buff + sizeof(struct packet), hna_buff, num_hna * 5 * sizeof(unsigned char) ); + forw_node_new->pack_buff_len = sizeof(struct packet) + num_hna * 5 * sizeof(unsigned char);
- forw_node_new->when = curr_time + rand_num( JITTER ); - forw_node_new->if_outgoing = if_outgoing; + } else {
- list_add( &forw_node_new->list, &forw_list ); + forw_node_new->pack_buff = alloc_memory( sizeof(struct packet) ); + memcpy( forw_node_new->pack_buff, &batman_if->out, sizeof(struct packet) ); + forw_node_new->pack_buff_len = sizeof(struct packet);
}
+ list_add( &forw_node_new->list, &forw_list ); + batman_if->out.seqno++;
} @@ -1275,11 +1253,11 @@ int batman() output("neighbour thinks connection is bidirectional - I disagree \n");*/
if ( in.gwflags != 0 ) - output("Is an internet gateway (class %i) \n", in.gwflags); + output( "Is an internet gateway (class %i) \n", in.gwflags );
if ( hna_buff_len > 4 ) {
- printf( "HNA information received (%i HNA network%s):\n", hna_buff_len / 5, ( hna_buff_len / 5 > 1 ? "s": "" ) ); + output( "HNA information received (%i HNA network%s):\n", hna_buff_len / 5, ( hna_buff_len / 5 > 1 ? "s": "" ) ); hna_buff_count = 0;
while ( ( hna_buff_count + 1 ) * 5 <= hna_buff_len ) { @@ -1290,9 +1268,9 @@ int batman() addr_to_string(hna, orig_str, sizeof (orig_str));
if ( ( netmask > 0 ) && ( netmask < 33 ) ) - printf( "hna: %s/%i\n", orig_str, netmask ); + output( "hna: %s/%i\n", orig_str, netmask ); else - printf( "hna: %s/%i -> ignoring (invalid netmask)\n", orig_str, netmask ); + output( "hna: %s/%i -> ignoring (invalid netmask)\n", orig_str, netmask );
hna_buff_count++;
diff --git a/batman.h b/batman.h index a237b0f..5e793de 100644 --- a/batman.h +++ b/batman.h @@ -24,7 +24,7 @@ #include <pthread.h> #include "list.h"
-#define VERSION "0.1 rc 6a" +#define VERSION "0.1 rc 6b" #define BATMAN_VERSION 1 #define PORT 1966 #define UNIDIRECTIONAL 0xF0 @@ -119,9 +119,8 @@ struct forw_node /* structure for forw_list maintaining packets { struct list_head list; unsigned int when; - struct packet pack; - unsigned char *hna_buff; - int hna_buff_len; + unsigned char *pack_buff; + int pack_buff_len; struct batman_if *if_outgoing; };