new option --no-pcap-mutex

This commit is contained in:
wangyu- 2018-11-13 02:08:32 -06:00
parent 7980103bd5
commit 003f31bfef
3 changed files with 27 additions and 11 deletions

@ -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;

@ -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 ;

@ -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;