From 003f31bfef8175c1535dd0a8ae2b17b24f9f017f Mon Sep 17 00:00:00 2001 From: wangyu- Date: Tue, 13 Nov 2018 02:08:32 -0600 Subject: [PATCH] new option --no-pcap-mutex --- misc.cpp | 6 ++++++ network.cpp | 30 +++++++++++++++++++----------- network.h | 2 ++ 3 files changed, 27 insertions(+), 11 deletions(-) diff --git a/misc.cpp b/misc.cpp index eec3d56..f551770 100644 --- a/misc.cpp +++ b/misc.cpp @@ -297,6 +297,7 @@ void process_arg(int argc, char *argv[]) //process all options {"dns-resolve", no_argument, 0, 1}, {"pcap-send", no_argument, 0, 1}, {"easy-tcp", no_argument, 0, 1}, + {"no-pcap-mutex", no_argument, 0, 1}, {NULL, 0, 0, 0} }; @@ -756,6 +757,11 @@ void process_arg(int argc, char *argv[]) //process all options send_with_pcap=1; mylog(log_info,"--pcap-send enabled, now pcap will be used for sending packet instead of libnet\n"); } + else if(strcmp(long_options[option_index].name,"no-pcap-mutex")==0) + { + use_pcap_mutex=9; + mylog(log_warn,"--no-pcap-mutex enabled, we will assume the underlying pcap calls are threadsafe\n"); + } else if(strcmp(long_options[option_index].name,"easy-tcp")==0) { use_tcp_dummy_socket=1; diff --git a/network.cpp b/network.cpp index 9ab9b4c..a0cd749 100644 --- a/network.cpp +++ b/network.cpp @@ -73,6 +73,7 @@ queue_t my_queue; pthread_mutex_t queue_mutex = PTHREAD_MUTEX_INITIALIZER; pthread_mutex_t pcap_mutex = PTHREAD_MUTEX_INITIALIZER; +int use_pcap_mutex=1; ev_async async_watcher; @@ -326,9 +327,9 @@ void *pcap_recv_thread_entry(void *none) while(1) { - pthread_mutex_lock(&pcap_mutex); + if(use_pcap_mutex) pthread_mutex_lock(&pcap_mutex); int ret=pcap_loop(pcap_handle, -1, my_packet_handler, NULL); //use -1 instead of 0 as cnt, since 0 is undefined in old versions - pthread_mutex_unlock(&pcap_mutex); + if(use_pcap_mutex) pthread_mutex_unlock(&pcap_mutex); if(ret==-1) mylog(log_warn,"pcap_loop exited with value %d\n",ret); else @@ -845,19 +846,26 @@ void init_filter(int port) //pthread_mutex_lock(&pcap_mutex);//not sure if mutex is needed here long long tmp_cnt=0; - while(pthread_mutex_trylock(&pcap_mutex)!=0) + if(use_pcap_mutex) { - tmp_cnt++; - pcap_breakloop(pcap_handle); - if(tmp_cnt%500==0) + while(pthread_mutex_trylock(&pcap_mutex)!=0) { - mylog(log_warn,"%lld attempts of pcap_breakloop()\n", tmp_cnt); - if(tmp_cnt>5000) + tmp_cnt++; + pcap_breakloop(pcap_handle); + if(tmp_cnt==100) { - mylog(log_fatal,"we might have already run into a deadlock\n"); + mylog(log_warn,"%lld attempts of pcap_breakloop()\n", tmp_cnt); } + if(tmp_cnt%1000==0) + { + mylog(log_warn,"%lld attempts of pcap_breakloop()\n", tmp_cnt); + if(tmp_cnt>5000) + { + mylog(log_fatal,"we might have already run into a deadlock\n"); + } + } + ev_sleep(0.001); } - ev_sleep(0.001); } mylog(log_info,"breakloop() succeed after %lld attempt(s)\n", tmp_cnt); @@ -884,7 +892,7 @@ void init_filter(int port) } - pthread_mutex_unlock(&pcap_mutex); + if(use_pcap_mutex) pthread_mutex_unlock(&pcap_mutex); /* if(disable_bpf_filter) return; //if(raw_mode==mode_icmp) return ; diff --git a/network.h b/network.h index 9bdf4d7..e3ce380 100644 --- a/network.h +++ b/network.h @@ -37,6 +37,8 @@ extern ev_async async_watcher; extern ev_loop* g_default_loop; extern pthread_mutex_t queue_mutex; +extern int use_pcap_mutex; + extern int pcap_cnt; extern int pcap_link_header_len;