Index: vde_switch.c
===================================================================
--- vde_switch.c	(revisione 470)
+++ vde_switch.c	(copia locale)
@@ -153,6 +153,7 @@
 			printlog(LOG_ERR,"realloc pollplus %s",strerror(errno));
 			exit(1);
 		}
+		fdpp->timestamp=0;
 	}
 	if (ISPRIO(type)) {
 		fds[nfds]=fds[nprio];
@@ -224,7 +225,7 @@
 					TYPE2MGR(fdpp[i]->type)->handle_input(fdpp[i]->type,fds[i].fd,fds[i].revents,&(fdpp[i]->arg));
 					if (nfds!=prenfds) /* the current fd has been deleted */
 						break; /* PERFORMANCE it is faster returning to poll */
-				}	
+				}
 /* optimization: most used descriptors migrate to the head of the poll array */
 #ifdef OPTPOLL
 				else
@@ -379,7 +380,8 @@
 		{"hashsize", 1, 0, HASH_TABLE_SIZE_ARG},
 		{"macaddr", 1, 0, MACADDR_ARG},
 #ifdef FSTP
-		{"priority", 1, 0, PRIORITY_ARG}
+		{"priority", 1, 0, PRIORITY_ARG},
+		{0,0,0,0}
 #endif
 	};
 	static struct option optail = {0,0,0,0};
@@ -471,6 +473,7 @@
 static void sig_handler(int sig)
 {
 	printlog(LOG_ERR,"Caught signal %d, cleaning up and exiting", sig);
+	if(sig==SIGTERM) exit(0); /* This is because profiling needs clean exit */
 	cleanup();
 	signal(sig, SIG_DFL);
 	kill(getpid(), sig);
Index: qtimer.c
===================================================================
--- qtimer.c	(revisione 470)
+++ qtimer.c	(copia locale)
@@ -89,7 +89,7 @@
 		qth[n]->qt_arg=arg;
 		qth[n]->qt_times=(times==0)?-1:times;
 		qtime_csexit();
-    return qth[n]->qt_n;
+		return qth[n]->qt_n;
 	} else
 		return -1;
 }
@@ -139,15 +139,17 @@
 	struct itimerval it;
 	struct sigaction sa;
 
-  sa.sa_handler = sig_alarm;
-  sa.sa_flags = SA_RESTART;
-  if(sigaction(SIGALRM, &sa, NULL) < 0){
-	  printlog(LOG_WARNING,"Setting handler for SIGALRM %s", strerror(errno));
-	    return;
-  }
+	sa.sa_handler = sig_alarm;
+	sa.sa_flags = SA_RESTART;
+	sigemptyset(&sa.sa_mask);
+
+	if(sigaction(SIGALRM, &sa, NULL) < 0){
+		printlog(LOG_WARNING,"Setting handler for SIGALRM %s", strerror(errno));
+		return;
+	}
  
 	sigemptyset(&ss_alarm);
-	sigaddset(&ss_alarm,SIGALRM);
+	sigaddset(&ss_alarm, SIGALRM);
 
 	it.it_value.tv_sec = 1;
 	it.it_value.tv_usec = 0 ;
Index: tuntap.c
===================================================================
--- tuntap.c	(revisione 470)
+++ tuntap.c	(copia locale)
@@ -100,6 +100,7 @@
 
 static struct option long_options[] = {
 	{"tap", 1, 0, 't'},
+	{0,0,0,0}
 };
 #define Nlong_options (sizeof(long_options)/sizeof(struct option));
 
Index: consmgmt.c
===================================================================
--- consmgmt.c	(revisione 470)
+++ consmgmt.c	(copia locale)
@@ -505,6 +505,7 @@
 #ifdef DEBUGOPT
 	{"debugclients",1,0,'D'},
 #endif
+	{0,0,0,0}
 };
 
 #define Nlong_options (sizeof(long_options)/sizeof(struct option));
@@ -649,6 +650,7 @@
 	if (mgmt_socket)
 		printoutc(fd,"mgmt %s perm 0%03o",mgmt_socket,mgmt_mode);
 	printoutc(fd,"unsent_pktq_len %d",packetq_count());
+	printoutc(fd,"current packet queue timeout %d",packetq_timeout);
 	return 0;
 }
 
Index: packetq.c
===================================================================
--- packetq.c	(revisione 470)
+++ packetq.c	(copia locale)
@@ -25,7 +25,9 @@
 
 int packetq_timeout= -1;
 static int countq;
-#define TIMEOUT 5
+#define TIMEOUT_UB 9 //Upper bound
+#define TIMEOUT_LB 1 //Lower bound
+#define TIMEOUT_MEAN ((TIMEOUT_UB + TIMEOUT_LB) / 2)
 #define TIMES 10
 #define MAXQLEN 4192
 
@@ -43,8 +45,14 @@
 
 static struct packetqq *pqh=NULL;
 static struct packetqq *pqt=NULL;
-static struct timeval last_try;
+static struct timespec last_try;
 
+#ifdef CLOCK_MONOTONIC_RAW
+#define CLOCK_TYPE CLOCK_MONOTONIC_RAW
+#else
+#define CLOCK_TYPE CLOCK_MONOTONIC
+#endif
+
 void packetq_add(int (*sender)(int fd, int fd_ctl, void *packet, int len, void *data, int port),
 		int fd, int fd_ctl, void *packet, int len, void *data, int port)
 {
@@ -64,8 +72,8 @@
 			new->times=TIMES;
 			new->next=NULL;
 			if (pqh==NULL) {
-				gettimeofday(&last_try,NULL);
-				packetq_timeout=TIMEOUT;
+				clock_gettime(CLOCK_TYPE,&last_try);
+				packetq_timeout=TIMEOUT_MEAN;
 				pqh=pqt=new;
 			} else {
 				pqt->next=new;
@@ -87,11 +95,13 @@
 				|| h->times<=0) { /*or max number of attempts reached*/
 			struct packetqq *next;
 			next=h->next;
+			//if(h->times<=0) printlog(LOG_WARNING,"send tries limit reached: discarding package while the queue has %d packages",countq);
 			countq--;
 			free(h->packet);
 			free(h);
 			return packetq_scantry(next,t,fds);
-		} else {
+		}
+		else {
 			FD_SET(h->fd,fds);
 			h->next=packetq_scantry(h->next,t,fds);
 			if (h->next == NULL) *t=h;
@@ -104,18 +114,24 @@
 void packetq_try(void)
 {
 	if (pqh != NULL) {
-		struct timeval this_try;
-		gettimeofday(&this_try,NULL);
-		packetq_timeout=TIMEOUT - ((this_try.tv_sec-last_try.tv_sec) * 1000 + 
-				(this_try.tv_usec-last_try.tv_usec) / 1000);
+		struct timespec this_try;
+		int packetq_timeout_p=packetq_timeout;
+		clock_gettime(CLOCK_TYPE,&this_try);
+		packetq_timeout=packetq_timeout - ((this_try.tv_sec-last_try.tv_sec) * 1000 +
+						(this_try.tv_nsec-last_try.tv_nsec) / 1000000);
 		if (packetq_timeout <= 0) {
 			fd_set fds;
 			FD_ZERO(&fds);
-			pqh=packetq_scantry(pqh,&pqt,&fds);	
+			pqh=packetq_scantry(pqh,&pqt,&fds);
 			if (pqh != NULL) {
-				gettimeofday(&last_try,NULL);
-				packetq_timeout=TIMEOUT;
-			} else
+				clock_gettime(CLOCK_TYPE,&last_try);
+				/* Change timeout based on queue crossing time */
+				/*if(((last_try.tv_sec-this_try.tv_sec) * 1000 +
+				(last_try.tv_nsec-this_try.tv_nsec) / 1000000)/2>packetq_timeout_p)*/
+				/* Change timeout based on queue length */
+				packetq_timeout=((int)TIMEOUT_UB*(1.0-((float)countq)/MAXQLEN))+TIMEOUT_LB;
+			}
+			else
 				packetq_timeout = -1;
 		}
 	}
Index: datasock.c
===================================================================
--- datasock.c	(revisione 470)
+++ datasock.c	(copia locale)
@@ -92,7 +92,7 @@
 	if(n){
 		int rv=errno;
 #ifndef VDE_PQ
-		if(errno != EAGAIN) printlog(LOG_WARNING,"send_sockaddr port %d: %s",port,strerror(errno));
+		if(errno != EAGAIN && errno != EWOULDBLOCK) printlog(LOG_WARNING,"send_sockaddr port %d: %s",port,strerror(errno));
 #endif
 		if (n>len)
 			return -rv;
@@ -199,6 +199,7 @@
 				remove_fd(fd); 
 				return -1;
 			}
+			memset(&sun_in,0,sizeof(sun_in));
 			sun_in.sun_family = AF_UNIX;
 			snprintf(sun_in.sun_path,sizeof(sun_in.sun_path),"%s/%03d",ctl_socket,port);
 			n = write(fd, &sun_in, sizeof(sun_in));
@@ -344,6 +345,7 @@
 	{"mode", 1, 0, 'm'},
 	{"dirmode", 1, 0, DIRMODEARG},
 	{"group", 1, 0, 'g'},
+	{0,0,0,0}
 };
 
 #define Nlong_options (sizeof(long_options)/sizeof(struct option));
