Try another optimization in LinuxEthernetTap.

This commit is contained in:
Adam Ierymenko 2020-11-18 22:06:17 -05:00
parent e3e188109d
commit a390629371
No known key found for this signature in database
GPG Key ID: C8877CF2D7A5D7F3
2 changed files with 43 additions and 70 deletions

View File

@ -182,14 +182,7 @@ LinuxEthernetTap::LinuxEthernetTap(
_dev = ifr.ifr_name;
::fcntl(_fd,F_SETFD,fcntl(_fd,F_GETFD) | FD_CLOEXEC);
(void)::pipe(_shutdownSignalPipe);
_tapReaderThread = std::thread([this]{
fd_set readfds,nullfds;
int n,nfds,r;
void *buf = nullptr;
std::vector<void *> buffers;
{
struct ifreq ifr;
memset(&ifr,0,sizeof(ifr));
@ -232,26 +225,12 @@ LinuxEthernetTap::LinuxEthernetTap(
return;
}
fcntl(_fd,F_SETFL,O_NONBLOCK);
::close(sock);
}
FD_ZERO(&readfds);
FD_ZERO(&nullfds);
nfds = (int)std::max(_shutdownSignalPipe[0],_fd) + 1;
r = 0;
for(;;) {
FD_SET(_shutdownSignalPipe[0],&readfds);
FD_SET(_fd,&readfds);
select(nfds,&readfds,&nullfds,&nullfds,(struct timeval *)0);
if (FD_ISSET(_shutdownSignalPipe[0],&readfds)) // writes to shutdown pipe terminate thread
break;
if (FD_ISSET(_fd,&readfds)) {
for(;;) { // read until there are no more packets, then return to outer select() loop
std::vector<void *> buffers;
void *buf = nullptr;
for(int r=0;;) {
if (!buf) {
// To reduce use of the mutex, we keep a local buffer vector and
// swap (which is a pointer swap) with the global one when it's
@ -270,7 +249,7 @@ LinuxEthernetTap::LinuxEthernetTap(
}
}
n = (int)::read(_fd,reinterpret_cast<uint8_t *>(buf) + r,ZT_TAP_BUF_SIZE - r);
const int n = (int)::read(_fd,reinterpret_cast<uint8_t *>(buf) + r,ZT_TAP_BUF_SIZE - r);
if (n > 0) {
// Some tap drivers like to send the ethernet frame and the
@ -293,8 +272,6 @@ LinuxEthernetTap::LinuxEthernetTap(
break;
}
}
}
}
});
_tapProcessorThread = std::thread([this] {
@ -320,12 +297,9 @@ LinuxEthernetTap::LinuxEthernetTap(
LinuxEthernetTap::~LinuxEthernetTap()
{
(void)::write(_shutdownSignalPipe[1],"\0",1); // causes reader thread to exit
_tapq.post(std::pair<void *,int>(nullptr,0)); // causes processor thread to exit
_tapq.post(std::pair<void *,int>(nullptr,0));
::shutdown(_fd, SHUT_RDWR);
::close(_fd);
::close(_shutdownSignalPipe[0]);
::close(_shutdownSignalPipe[1]);
_tapReaderThread.join();
_tapProcessorThread.join();

View File

@ -68,7 +68,6 @@ private:
std::vector<MulticastGroup> _multicastGroups;
unsigned int _mtu;
int _fd;
int _shutdownSignalPipe[2];
std::atomic_bool _enabled;
std::thread _tapReaderThread;
std::thread _tapProcessorThread;