2016-11-17 10:14:34 -06:00
|
|
|
/*
|
|
|
|
Copyright (C) 2016 Srivats P.
|
|
|
|
|
|
|
|
This file is part of "Ostinato"
|
|
|
|
|
|
|
|
This is free software: you can redistribute it and/or modify
|
|
|
|
it under the terms of the GNU General Public License as published by
|
|
|
|
the Free Software Foundation, either version 3 of the License, or
|
|
|
|
(at your option) any later version.
|
|
|
|
|
|
|
|
This program is distributed in the hope that it will be useful,
|
|
|
|
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
GNU General Public License for more details.
|
|
|
|
|
|
|
|
You should have received a copy of the GNU General Public License
|
|
|
|
along with this program. If not, see <http://www.gnu.org/licenses/>
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include "pcaprxstats.h"
|
2016-11-18 09:21:30 -06:00
|
|
|
|
|
|
|
#include "pcapextra.h"
|
2023-04-30 01:20:54 -05:00
|
|
|
#include "../common/debugdefs.h"
|
2016-11-17 10:14:34 -06:00
|
|
|
#include "../common/sign.h"
|
2023-03-31 06:25:03 -05:00
|
|
|
#include "streamtiming.h"
|
2016-11-17 10:14:34 -06:00
|
|
|
|
2018-03-15 09:04:42 -05:00
|
|
|
#define Xnotify qWarning // FIXME
|
2016-11-17 10:14:34 -06:00
|
|
|
|
2023-02-08 04:48:02 -06:00
|
|
|
PcapRxStats::PcapRxStats(const char *device, StreamStats &portStreamStats, int id)
|
2016-11-17 10:14:34 -06:00
|
|
|
: streamStats_(portStreamStats)
|
|
|
|
{
|
2023-04-26 01:52:26 -05:00
|
|
|
setObjectName(QString("Rx$:%1").arg(device));
|
2018-03-15 09:04:42 -05:00
|
|
|
device_ = QString::fromLatin1(device);
|
2016-11-17 10:14:34 -06:00
|
|
|
stop_ = false;
|
|
|
|
state_ = kNotStarted;
|
2017-01-09 07:27:38 -06:00
|
|
|
isDirectional_ = true;
|
2016-11-17 10:14:34 -06:00
|
|
|
|
|
|
|
handle_ = NULL;
|
2023-02-08 04:48:02 -06:00
|
|
|
|
2023-03-31 06:28:52 -05:00
|
|
|
portId_ = id;
|
2023-03-31 06:25:03 -05:00
|
|
|
|
|
|
|
timing_ = StreamTiming::instance();
|
2016-11-17 10:14:34 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
pcap_t* PcapRxStats::handle()
|
|
|
|
{
|
|
|
|
return handle_;
|
|
|
|
}
|
|
|
|
|
|
|
|
void PcapRxStats::run()
|
|
|
|
{
|
|
|
|
int flags = PCAP_OPENFLAG_PROMISCUOUS;
|
|
|
|
char errbuf[PCAP_ERRBUF_SIZE] = "";
|
|
|
|
struct bpf_program bpf;
|
|
|
|
const int optimize = 1;
|
2017-02-16 10:02:01 -06:00
|
|
|
QString capture_filter = QString("(ether[len - 4:4] == 0x%1)").arg(
|
|
|
|
SignProtocol::magic(), 0, BASE_HEX);
|
|
|
|
// XXX: Exclude ICMP packets which contain an embedded signed packet
|
|
|
|
// For now we check upto 4 vlan tags
|
|
|
|
capture_filter.append(
|
|
|
|
"and not ("
|
|
|
|
"icmp or "
|
|
|
|
"(vlan and icmp) or "
|
|
|
|
"(vlan and icmp) or "
|
|
|
|
"(vlan and icmp) or "
|
|
|
|
"(vlan and icmp) "
|
|
|
|
")");
|
2016-11-17 10:14:34 -06:00
|
|
|
|
|
|
|
qDebug("In %s", __PRETTY_FUNCTION__);
|
|
|
|
|
|
|
|
handle_ = pcap_open_live(qPrintable(device_), 65535,
|
|
|
|
flags, 100 /* ms */, errbuf);
|
|
|
|
if (handle_ == NULL) {
|
|
|
|
if (flags && QString(errbuf).contains("promiscuous")) {
|
2018-03-15 09:04:42 -05:00
|
|
|
Xnotify("Unable to set promiscuous mode on <%s> - "
|
2016-11-17 10:14:34 -06:00
|
|
|
"stream stats rx will not work", qPrintable(device_));
|
|
|
|
goto _exit;
|
|
|
|
}
|
|
|
|
else {
|
2018-03-15 09:04:42 -05:00
|
|
|
Xnotify("Unable to open <%s> [%s] - stream stats rx will not work",
|
2016-11-17 10:14:34 -06:00
|
|
|
qPrintable(device_), errbuf);
|
|
|
|
goto _exit;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-11-18 09:27:50 -06:00
|
|
|
#ifdef Q_OS_WIN32
|
|
|
|
// pcap_setdirection() API is not supported in Windows.
|
|
|
|
// NOTE: WinPcap 4.1.1 and above exports a dummy API that returns -1
|
|
|
|
// but since we would like to work with previous versions of WinPcap
|
|
|
|
// also, we assume the API does not exist
|
2017-01-09 07:27:38 -06:00
|
|
|
isDirectional_ = false;
|
2016-11-18 09:27:50 -06:00
|
|
|
#else
|
2017-01-09 07:27:38 -06:00
|
|
|
if (pcap_setdirection(handle_, PCAP_D_IN) < 0) {
|
2016-11-18 09:27:50 -06:00
|
|
|
qDebug("RxStats: Error setting IN direction %s: %s\n",
|
2016-11-18 09:59:45 -06:00
|
|
|
qPrintable(device_), pcap_geterr(handle_));
|
2017-01-09 07:27:38 -06:00
|
|
|
isDirectional_ = false;
|
|
|
|
}
|
2016-11-18 09:27:50 -06:00
|
|
|
#endif
|
|
|
|
|
2016-11-17 10:14:34 -06:00
|
|
|
if (pcap_compile(handle_, &bpf, qPrintable(capture_filter),
|
|
|
|
optimize, 0) < 0) {
|
|
|
|
qWarning("%s: error compiling filter: %s", qPrintable(device_),
|
|
|
|
pcap_geterr(handle_));
|
|
|
|
goto _skip_filter;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (pcap_setfilter(handle_, &bpf) < 0) {
|
|
|
|
qWarning("%s: error setting filter: %s", qPrintable(device_),
|
|
|
|
pcap_geterr(handle_));
|
|
|
|
goto _skip_filter;
|
|
|
|
}
|
|
|
|
|
|
|
|
_skip_filter:
|
2023-03-23 05:12:41 -05:00
|
|
|
clearDebugStats();
|
2019-08-10 02:46:18 -05:00
|
|
|
PcapSession::preRun();
|
2016-11-17 10:14:34 -06:00
|
|
|
state_ = kRunning;
|
|
|
|
while (1) {
|
|
|
|
int ret;
|
|
|
|
struct pcap_pkthdr *hdr;
|
|
|
|
const uchar *data;
|
|
|
|
|
|
|
|
ret = pcap_next_ex(handle_, &hdr, &data);
|
|
|
|
switch (ret) {
|
|
|
|
case 1: {
|
2023-03-31 06:25:03 -05:00
|
|
|
uint ttagId, guid;
|
2023-05-06 02:45:37 -05:00
|
|
|
#ifdef Q_OS_WIN32
|
|
|
|
// Npcap (Windows) doesn't support direction, so packets
|
|
|
|
// Tx by PcapTxThread are received back by us here - use
|
|
|
|
// TxPort to filter out. TxPort is returned as byte 1 of
|
|
|
|
// ttagId (byte 0 is ttagId).
|
|
|
|
// If TxPort is us ==> Tx Packet, so skip
|
|
|
|
// FIXME: remove once npcap supports pcap direction
|
|
|
|
if (SignProtocol::packetTtagId(data, hdr->caplen, &ttagId, &guid)
|
|
|
|
&& (ttagId >> 8 != uint(portId_))) {
|
|
|
|
ttagId &= 0xFF;
|
|
|
|
timing_->recordRxTime(portId_, guid, ttagId, hdr->ts);
|
|
|
|
}
|
|
|
|
#else
|
2023-03-31 06:25:03 -05:00
|
|
|
if (SignProtocol::packetTtagId(data, hdr->caplen, &ttagId, &guid)) {
|
2023-03-31 06:28:52 -05:00
|
|
|
timing_->recordRxTime(portId_, guid, ttagId, hdr->ts);
|
2023-03-31 06:25:03 -05:00
|
|
|
}
|
2023-05-06 02:45:37 -05:00
|
|
|
#endif
|
2023-03-31 06:25:03 -05:00
|
|
|
if (guid != SignProtocol::kInvalidGuid) {
|
2016-11-17 10:14:34 -06:00
|
|
|
streamStats_[guid].rx_pkts++;
|
|
|
|
streamStats_[guid].rx_bytes += hdr->caplen;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
case 0:
|
|
|
|
// timeout: just go back to the loop
|
|
|
|
break;
|
|
|
|
case -1:
|
|
|
|
qWarning("%s: error reading packet (%d): %s",
|
|
|
|
__PRETTY_FUNCTION__, ret, pcap_geterr(handle_));
|
|
|
|
break;
|
|
|
|
case -2:
|
2019-08-10 02:46:18 -05:00
|
|
|
qDebug("Loop/signal break or some other error");
|
|
|
|
break;
|
2016-11-17 10:14:34 -06:00
|
|
|
default:
|
2019-08-10 02:46:18 -05:00
|
|
|
qWarning("%s: Unexpected return value %d", __PRETTY_FUNCTION__,
|
2016-11-17 10:14:34 -06:00
|
|
|
ret);
|
2019-08-10 02:46:18 -05:00
|
|
|
stop_ = true;
|
2016-11-17 10:14:34 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
if (stop_) {
|
2019-08-10 02:46:18 -05:00
|
|
|
qDebug("user requested rxstats stop");
|
2016-11-17 10:14:34 -06:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
2019-08-10 02:46:18 -05:00
|
|
|
PcapSession::postRun();
|
2016-11-17 10:14:34 -06:00
|
|
|
pcap_close(handle_);
|
|
|
|
handle_ = NULL;
|
|
|
|
stop_ = false;
|
|
|
|
|
|
|
|
_exit:
|
|
|
|
state_ = kFinished;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool PcapRxStats::start()
|
|
|
|
{
|
|
|
|
if (state_ == kRunning) {
|
|
|
|
qWarning("RxStats start requested but is already running!");
|
|
|
|
goto _exit;
|
|
|
|
}
|
|
|
|
|
|
|
|
state_ = kNotStarted;
|
2019-08-10 02:46:18 -05:00
|
|
|
PcapSession::start();
|
2016-11-17 10:14:34 -06:00
|
|
|
|
|
|
|
while (state_ == kNotStarted)
|
|
|
|
QThread::msleep(10);
|
|
|
|
_exit:
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool PcapRxStats::stop()
|
|
|
|
{
|
|
|
|
if (state_ == kRunning) {
|
|
|
|
stop_ = true;
|
2023-03-23 05:12:41 -05:00
|
|
|
PcapSession::stop();
|
2016-11-17 10:14:34 -06:00
|
|
|
while (state_ == kRunning)
|
|
|
|
QThread::msleep(10);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
qWarning("RxStats stop requested but is not running!");
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool PcapRxStats::isRunning()
|
|
|
|
{
|
|
|
|
return (state_ == kRunning);
|
|
|
|
}
|
2017-01-09 07:27:38 -06:00
|
|
|
|
|
|
|
bool PcapRxStats::isDirectional()
|
|
|
|
{
|
|
|
|
return isDirectional_;
|
|
|
|
}
|