pcapplusplus-sys 0.1.0

Compile PcapPlusPlus and make its library and header files available. See also https://github.com/seladb/PcapPlusPlus
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
#ifdef USE_PF_RING

// GCOVR_EXCL_START

#define LOG_MODULE PcapLogModulePfRingDevice

#include "PfRingDevice.h"
#include "EthLayer.h"
#include "VlanLayer.h"
#include "Logger.h"
#include <errno.h>
#include <pfring.h>
#include <pthread.h>
#include <chrono>


#define DEFAULT_PF_RING_SNAPLEN 1600

namespace pcpp
{


PfRingDevice::PfRingDevice(const char* deviceName) : m_MacAddress(MacAddress::Zero)
{
	m_NumOfOpenedRxChannels = 0;
	m_DeviceOpened = false;
	m_DeviceName =  std::string(deviceName);
	m_InterfaceIndex = -1;
	m_StopThread = true;
	m_OnPacketsArriveCallback = NULL;
	m_OnPacketsArriveUserCookie = NULL;
	m_ReentrantMode = false;
	m_HwClockEnabled = false;
	m_DeviceMTU = 0;
	m_IsFilterCurrentlySet = false;

	m_PfRingDescriptors = new pfring*[MAX_NUM_RX_CHANNELS];
}

PfRingDevice::~PfRingDevice()
{
	close();
	delete [] m_PfRingDescriptors;
}


bool PfRingDevice::open()
{
	if (m_DeviceOpened)
	{
		PCPP_LOG_ERROR("Device already opened");
		return false;
	}

	m_NumOfOpenedRxChannels = 0;

	PCPP_LOG_DEBUG("Trying to open device [" << m_DeviceName << "]");
	int res = openSingleRxChannel(m_DeviceName.c_str(), &m_PfRingDescriptors[0]);
	if (res == 0)
	{
		PCPP_LOG_DEBUG("Succeeded opening device [" << m_DeviceName << "]");
		m_NumOfOpenedRxChannels = 1;
		m_DeviceOpened = true;
		return true;
	}
	else if (res == 1)
		PCPP_LOG_ERROR("Couldn't open a ring on device [" << m_DeviceName << "]");
	else if (res == 2)
		PCPP_LOG_ERROR("Unable to enable ring for device [" << m_DeviceName << "]");

	return false;
}


bool PfRingDevice::openSingleRxChannel(uint8_t channelId)
{
	uint8_t channelIds[1] = { channelId };
	return openMultiRxChannels(channelIds, 1);
}

int PfRingDevice::openSingleRxChannel(const char* deviceName, pfring** ring)
{
	if (m_DeviceOpened)
	{
		PCPP_LOG_ERROR("Device already opened");
		return false;
	}

	uint32_t flags = PF_RING_PROMISC | PF_RING_HW_TIMESTAMP | PF_RING_DNA_SYMMETRIC_RSS;
	*ring = pfring_open(deviceName, DEFAULT_PF_RING_SNAPLEN, flags);

	if (*ring == NULL)
	{
		return 1;
	}
	PCPP_LOG_DEBUG("pfring_open Succeeded for device [" << m_DeviceName << "]");

	if (getIsHwClockEnable())
	{
		setPfRingDeviceClock(*ring);
		PCPP_LOG_DEBUG("H/W clock set for device [" << m_DeviceName << "]");
	}

	if (pfring_enable_rss_rehash(*ring) < 0 || pfring_enable_ring(*ring) < 0)
	{
		pfring_close(*ring);
		return 2;
	}

	PCPP_LOG_DEBUG("pfring enabled for device [" << m_DeviceName << "]");

	return 0;
}

bool PfRingDevice::setPfRingDeviceClock(pfring* ring)
{
	struct timespec ltime;
	if (clock_gettime(CLOCK_REALTIME, &ltime) != 0)
	{
		PCPP_LOG_ERROR("Could not set pfring devices clock, clock_gettime failed");
		return false;
	}

	if (pfring_set_device_clock(ring, &ltime) < 0)
	{
		PCPP_LOG_DEBUG("Could not set pfring devices clock, pfring_set_device_clock failed");
		return false;
	}

	return true;
}

bool PfRingDevice::openMultiRxChannels(const uint8_t* channelIds, int numOfChannelIds)
{
	if (m_DeviceOpened)
	{
		PCPP_LOG_ERROR("Device already opened");
		return false;
	}

	// I needed to add this verification because PF_RING doesn't provide it.
	// It allows opening the device on a channel that doesn't exist, but of course no packets will be captured
	uint8_t totalChannels = getTotalNumOfRxChannels();
	for (int i = 0; i < numOfChannelIds; i++)
	{
		uint8_t channelId = channelIds[i];
		if (channelId >= totalChannels)
		{
			PCPP_LOG_ERROR("Trying to open the device with a RX channel that doesn't exist. Total RX channels are [" << (int)totalChannels << "], tried to open channel [" << (int)channelId << "]");
			return false;
		}
	}

	m_NumOfOpenedRxChannels = 0;

	for (int i = 0; i < numOfChannelIds; i++)
	{
		uint8_t channelId = channelIds[i];
		std::ostringstream ringNameStream;
		ringNameStream << m_DeviceName << "@" << (int)channelId;
		std::string ringName = ringNameStream.str();
		PCPP_LOG_DEBUG("Trying to open device [" << m_DeviceName << "] on channel [" << channelId << "]. Channel name [" << ringName << "]");
		int res = openSingleRxChannel(ringName.c_str(), &m_PfRingDescriptors[i]);
		if (res == 0)
		{
			PCPP_LOG_DEBUG("Succeeded opening device [" << m_DeviceName << "] on channel [" << channelId << "]. Channel name [" << ringName << "]");
			m_NumOfOpenedRxChannels++;
			continue;
		}
		else if (res == 1)
			PCPP_LOG_ERROR("Couldn't open a ring on channel [" << (int)channelId << "] for device [" << m_DeviceName << "]");
		else if (res == 2)
			PCPP_LOG_ERROR("Unable to enable ring on channel [" << (int)channelId << "] for device [" << m_DeviceName << "]");

		break;
	}

	if (m_NumOfOpenedRxChannels < numOfChannelIds)
	{
		// if an error occurred, close all rings from index=0 to index=m_NumOfOpenedRxChannels-1
		// there's no need to close m_PfRingDescriptors[m_NumOfOpenedRxChannels] because it has already been
		// closed by openSingleRxChannel
		for (int i = 0; i < m_NumOfOpenedRxChannels-1; i++)
		{
			pfring_close(m_PfRingDescriptors[i]);
		}

		m_NumOfOpenedRxChannels = 0;
		return false;
	}

	m_DeviceOpened = true;

	return true;
}

bool PfRingDevice::openMultiRxChannels(uint8_t numOfRxChannelsToOpen, ChannelDistribution dist)
{
	if (m_DeviceOpened)
	{
		PCPP_LOG_ERROR("Device already opened");
		return false;
	}

	m_NumOfOpenedRxChannels = 0;

	if (numOfRxChannelsToOpen > MAX_NUM_RX_CHANNELS)
	{
		PCPP_LOG_ERROR("Cannot open more than [" << MAX_NUM_RX_CHANNELS << "] channels");
		return false;
	}

	uint32_t flags = PF_RING_PROMISC | PF_RING_REENTRANT | PF_RING_HW_TIMESTAMP | PF_RING_DNA_SYMMETRIC_RSS;

	uint8_t numOfRxChannelsOnNIC = getTotalNumOfRxChannels();
	PCPP_LOG_DEBUG("NIC has " << (int)numOfRxChannelsOnNIC << " RX channels");

	uint8_t numOfRingsPerRxChannel = numOfRxChannelsToOpen / numOfRxChannelsOnNIC;
	uint8_t remainderRings = numOfRxChannelsToOpen % numOfRxChannelsOnNIC;

	cluster_type clusterType = (dist == RoundRobin) ? cluster_round_robin : cluster_per_flow;

	int ringsOpen = 0;
	for (uint8_t channelId = 0; channelId < numOfRxChannelsOnNIC; channelId++)
	{
		// no more channels to open
		if (numOfRingsPerRxChannel == 0 && remainderRings == 0)
			break;

		std::ostringstream ringName;
		ringName << m_DeviceName << "@" << (int)channelId;

		// open numOfRingsPerRxChannel rings per RX channel
		for (uint8_t ringId = 0; ringId < numOfRingsPerRxChannel; ringId++)
		{
			m_PfRingDescriptors[ringsOpen] = pfring_open(ringName.str().c_str(), DEFAULT_PF_RING_SNAPLEN, flags);
			if (m_PfRingDescriptors[ringsOpen] == NULL)
			{
				PCPP_LOG_ERROR("Couldn't open a ring on channel [" << (int)channelId << "]");
				break;
			}

			// setting a cluster for all rings in the same channel to enable hashing between them
			if (pfring_set_cluster(m_PfRingDescriptors[ringsOpen], channelId+1, clusterType) < 0)
			{
				PCPP_LOG_ERROR("Couldn't set ring [" << (int)ringId << "] in channel [" << (int)channelId << "] to the cluster [" << (int)(channelId+1) << "]");
				break;
			}

			ringsOpen++;
		}

		// open one more ring if remainder > 0
		if (remainderRings > 0)
		{
			m_PfRingDescriptors[ringsOpen] = pfring_open(ringName.str().c_str(), DEFAULT_PF_RING_SNAPLEN, flags);
			if (m_PfRingDescriptors[ringsOpen] == NULL)
			{
				PCPP_LOG_ERROR("Couldn't open a ring on channel [" << (int)channelId << "]");
				break;
			}

			// setting a cluster for all rings in the same channel to enable hashing between them
			if (pfring_set_cluster(m_PfRingDescriptors[ringsOpen], channelId+1, clusterType) < 0)
			{
				PCPP_LOG_ERROR("Couldn't set ring [" << (int)(numOfRingsPerRxChannel+1) << "] in channel [" << (int)channelId << "] to the cluster [" << (int)(channelId+1) << "]");
				break;
			}

			ringsOpen++;
			remainderRings--;
			PCPP_LOG_DEBUG("Opened " << (int)(numOfRingsPerRxChannel+1) << " rings on channel [" << (int)channelId << "]");
		}
		else
			PCPP_LOG_DEBUG("Opened " << (int)numOfRingsPerRxChannel << " rings on channel [" << (int)channelId << "]");
	}

	if (ringsOpen < numOfRxChannelsToOpen)
	{
		for (uint8_t i = 0; i < ringsOpen; i++)
			pfring_close(m_PfRingDescriptors[i]);
		return false;
	}

	if (getIsHwClockEnable())
	{
		for (int i = 0; i < ringsOpen; i++)
		{
			if (setPfRingDeviceClock(m_PfRingDescriptors[i]))
				PCPP_LOG_DEBUG("H/W clock set for device [" << m_DeviceName << "]");
		}
	}

	// enable all rings
	for (int i = 0; i < ringsOpen; i++)
	{
		if (pfring_enable_rss_rehash(m_PfRingDescriptors[i]) < 0 || pfring_enable_ring(m_PfRingDescriptors[i]) < 0)
		{
			PCPP_LOG_ERROR("Unable to enable ring [" << i << "] for device [" << m_DeviceName << "]");
			// close all pfring's that were enabled until now
			for (int j = 0; j <ringsOpen; j++)
				pfring_close(m_PfRingDescriptors[j]);
			return false;
		}
	}

	m_NumOfOpenedRxChannels = ringsOpen;

	m_DeviceOpened = true;
	return true;
}

uint8_t PfRingDevice::getTotalNumOfRxChannels() const
{
	if (m_NumOfOpenedRxChannels > 0)
	{
		uint8_t res = pfring_get_num_rx_channels(m_PfRingDescriptors[0]);
		return res;
	}
	else
	{
		uint32_t flags = PF_RING_PROMISC | PF_RING_REENTRANT | PF_RING_HW_TIMESTAMP | PF_RING_DNA_SYMMETRIC_RSS;
		pfring* ring = pfring_open(m_DeviceName.c_str(), DEFAULT_PF_RING_SNAPLEN, flags);
		uint8_t res = pfring_get_num_rx_channels(ring);
		pfring_close(ring);
		return res;
	}
}


SystemCore PfRingDevice::getCurrentCoreId() const
{
	return SystemCores::IdToSystemCore[sched_getcpu()];
}


bool PfRingDevice::setFilter(std::string filterAsString)
{
	if (!m_DeviceOpened)
	{
		PCPP_LOG_ERROR("Device not opened");
		return false;
	}

	for (int i = 0; i < m_NumOfOpenedRxChannels; i++)
	{
		int res = pfring_set_bpf_filter(m_PfRingDescriptors[i], (char*)filterAsString.c_str());
		if(res < 0)
		{
			if (res == PF_RING_ERROR_NOT_SUPPORTED)
				PCPP_LOG_ERROR("BPF filtering isn't supported on current PF_RING version. Please re-compile PF_RING with the --enable-bpf flag");
			else
				PCPP_LOG_ERROR("Couldn't set filter '" << filterAsString << "'");
			return false;
		}
	}

	m_IsFilterCurrentlySet = true;

	PCPP_LOG_DEBUG("Successfully set filter '" << filterAsString << "'");
	return true;
}


bool PfRingDevice::clearFilter()
{
	if (!m_IsFilterCurrentlySet)
		return true;

	for (int i = 0; i < m_NumOfOpenedRxChannels; i++)
	{
		int res = pfring_remove_bpf_filter(m_PfRingDescriptors[i]);
		if(res < 0)
		{
			PCPP_LOG_ERROR("Couldn't remove filter");
			return false;
		}
	}

	m_IsFilterCurrentlySet = false;

	PCPP_LOG_DEBUG("Successfully removed filter from all open RX channels");
	return true;
}


bool PfRingDevice::isFilterCurrentlySet() const
{
	return m_IsFilterCurrentlySet;
}


void PfRingDevice::close()
{
	for (int i = 0; i < m_NumOfOpenedRxChannels; i++)
		pfring_close(m_PfRingDescriptors[i]);
	m_DeviceOpened = false;
	clearCoreConfiguration();
	m_NumOfOpenedRxChannels = 0;
	m_IsFilterCurrentlySet = false;
	PCPP_LOG_DEBUG("Device [" << m_DeviceName << "] closed");
}

bool PfRingDevice::initCoreConfigurationByCoreMask(CoreMask coreMask)
{
	int i = 0;
	int numOfCores = getNumOfCores();
	clearCoreConfiguration();
	while ((coreMask != 0) && (i < numOfCores))
	{
		if (coreMask & 1)
		{
			m_CoreConfiguration[i].IsInUse = true;
		}

		coreMask = coreMask >> 1;
		i++;
	}

	if (coreMask != 0) // this mean coreMask contains a core that doesn't exist
	{
		PCPP_LOG_ERROR("Trying to use a core [" << i << "] that doesn't exist while machine has " << numOfCores << " cores");
		clearCoreConfiguration();
		return false;
	}

	return true;
}

bool PfRingDevice::startCaptureMultiThread(OnPfRingPacketsArriveCallback onPacketsArrive, void* onPacketsArriveUserCookie, CoreMask coreMask)
{
	if (!m_StopThread)
	{
		PCPP_LOG_ERROR("Device already capturing. Cannot start 2 capture sessions at the same time");
		return false;
	}

	if (!initCoreConfigurationByCoreMask(coreMask))
		return false;

	if (m_NumOfOpenedRxChannels != getCoresInUseCount())
	{
		PCPP_LOG_ERROR("Cannot use a different number of channels and cores. Opened " << m_NumOfOpenedRxChannels << " channels but set " << getCoresInUseCount() << " cores in core mask");
		clearCoreConfiguration();
		return false;
	}

	std::mutex mutex;
	std::condition_variable cond;
	int startThread = 0;

	m_StopThread = false;
	int rxChannel = 0;
	for (int coreId = 0; coreId < MAX_NUM_OF_CORES; coreId++)
	{
		if (!m_CoreConfiguration[coreId].IsInUse)
			continue;

		m_ReentrantMode = true;

		m_OnPacketsArriveCallback = onPacketsArrive;
		m_OnPacketsArriveUserCookie = onPacketsArriveUserCookie;

		// create a new thread
		m_CoreConfiguration[coreId].Channel = m_PfRingDescriptors[rxChannel++];
		m_CoreConfiguration[coreId].RxThread = std::thread(&pcpp::PfRingDevice::captureThreadMain, this, &cond, &mutex, &startThread);

		// set affinity to cores
		cpu_set_t cpuset;
		CPU_ZERO(&cpuset);
		CPU_SET(coreId, &cpuset);
		int err = pthread_setaffinity_np(m_CoreConfiguration[coreId].RxThread.native_handle(), sizeof(cpu_set_t), &cpuset);
		if(err != 0)
		{
			PCPP_LOG_ERROR("Error while binding thread to core " << coreId << ": errno=" << err);
			startThread = 1;
			clearCoreConfiguration();
			return false;
		}
	}

	startThread = 2;
	cond.notify_all();

	return true;
}

bool PfRingDevice::startCaptureSingleThread(OnPfRingPacketsArriveCallback onPacketsArrive, void* onPacketsArriveUserCookie)
{
	if (!m_StopThread)
	{
		PCPP_LOG_ERROR("Device already capturing. Cannot start 2 capture sessions at the same time");
		return false;
	}

	if (m_NumOfOpenedRxChannels != 1)
	{
		PCPP_LOG_ERROR("Cannot start capturing on a single thread when more than 1 RX channel is opened");
		return false;
	}

	PCPP_LOG_DEBUG("Trying to start capturing on a single thread for device [" << m_DeviceName << "]");

	clearCoreConfiguration();

	m_OnPacketsArriveCallback = onPacketsArrive;
	m_OnPacketsArriveUserCookie = onPacketsArriveUserCookie;

	m_StopThread = false;

	m_ReentrantMode = false;

	std::mutex mutex;
	std::condition_variable cond;
	int startThread = 0;

	cpu_set_t cpuset;
	CPU_ZERO(&cpuset);
	CPU_SET(0, &cpuset);
	m_CoreConfiguration[0].IsInUse = true;
	m_CoreConfiguration[0].Channel = m_PfRingDescriptors[0];
	m_CoreConfiguration[0].RxThread = std::thread(&pcpp::PfRingDevice::captureThreadMain, this, &cond, &mutex, &startThread);
	m_CoreConfiguration[0].IsAffinitySet = false;
	int err = pthread_setaffinity_np(m_CoreConfiguration[0].RxThread.native_handle(), sizeof(cpu_set_t), &cpuset);
	if(err != 0)
	{
		startThread = 1;
		PCPP_LOG_ERROR("Error while binding thread to core 0: errno=" << err);
		clearCoreConfiguration();
		return false;
	}
	startThread = 2;
	cond.notify_all();

	PCPP_LOG_DEBUG("Capturing started for device [" << m_DeviceName << "]");
	return true;
}

void PfRingDevice::stopCapture()
{
	PCPP_LOG_DEBUG("Trying to stop capturing on device [" << m_DeviceName << "]");
	m_StopThread = true;
	for (int coreId = 0; coreId < MAX_NUM_OF_CORES; coreId++)
	{
		if (!m_CoreConfiguration[coreId].IsInUse)
			continue;
		m_CoreConfiguration[coreId].RxThread.join();
		PCPP_LOG_DEBUG("Thread on core [" << coreId << "] stopped");
	}

	PCPP_LOG_DEBUG("All capturing threads stopped");
}

void PfRingDevice::captureThreadMain(std::condition_variable* startCond, std::mutex* startMutex, const int* startState)
{
	while (*startState == 0)
	{
		std::unique_lock<std::mutex> lock(*startMutex);
		startCond->wait_for(lock, std::chrono::milliseconds(100));
	}
	if (*startState == 1)
	{
		return;
	}

	int coreId = this->getCurrentCoreId().Id;
	pfring* ring = NULL;

	PCPP_LOG_DEBUG("Starting capture thread " << coreId);

	ring = this->m_CoreConfiguration[coreId].Channel;

	if (ring == NULL)
	{
		PCPP_LOG_ERROR("Couldn't find ring for core " << coreId << ". Exiting capture thread");
		return;
	}

	while (!this->m_StopThread)
	{
		// if buffer is NULL PF_RING avoids copy of the data
		uint8_t* buffer = NULL;
		uint32_t bufferLen = 0;

		// in multi-threaded mode flag PF_RING_REENTRANT is set, and this flag doesn't work with zero copy
		// so I need to allocate a buffer and set buffer to point to it
		if (this->m_ReentrantMode)
		{
			uint8_t tempBuffer[PCPP_MAX_PACKET_SIZE];
			buffer = tempBuffer;
			bufferLen = PCPP_MAX_PACKET_SIZE;
		}

		struct pfring_pkthdr pktHdr;
		int recvRes = pfring_recv(ring, &buffer, bufferLen, &pktHdr, 0);
		if (recvRes > 0)
		{
			// if caplen < len it means we don't have the whole packet. Treat this case as packet drop
			// TODO: add this packet to dropped packet stats
//			if (pktHdr.caplen != pktHdr.len)
//			{
//				PCPP_LOG_ERROR("Packet dropped due to len != caplen");
//				continue;
//			}

			RawPacket rawPacket(buffer, pktHdr.caplen, pktHdr.ts, false);
			this->m_OnPacketsArriveCallback(&rawPacket, 1, coreId, this, this->m_OnPacketsArriveUserCookie);
		}
		else if (recvRes < 0)
		{
			// cppcheck-suppress shiftNegative
			PCPP_LOG_ERROR("pfring_recv returned an error: [Err=" << recvRes << "]");
		}
	}

	PCPP_LOG_DEBUG("Exiting capture thread " << coreId);
}

void PfRingDevice::getThreadStatistics(SystemCore core, PfRingStats& stats) const
{
	pfring* ring = NULL;
	uint8_t coreId = core.Id;

	ring = m_CoreConfiguration[coreId].Channel;

	if (ring != NULL)
	{
		pfring_stat tempStats;
		if (pfring_stats(ring, &tempStats) < 0)
		{
			PCPP_LOG_ERROR("Can't retrieve statistics for core [" << (int)coreId << "], pfring_stats failed");
			return;
		}
		stats.drop = (uint64_t)tempStats.drop;
		stats.recv = (uint64_t)tempStats.recv;
	}
	else
	{
		PCPP_LOG_ERROR("Core [" << (int)coreId << "] is not in use, can't retrieve statistics");
	}
}

void PfRingDevice::getCurrentThreadStatistics(PfRingStats& stats) const
{
	getThreadStatistics(getCurrentCoreId(), stats);
}

void PfRingDevice::getStatistics(PfRingStats& stats) const
{
	stats.drop = 0;
	stats.recv = 0;

	for (int coreId = 0; coreId < MAX_NUM_OF_CORES; coreId++)
	{
		if (!m_CoreConfiguration[coreId].IsInUse)
			continue;

		PfRingStats tempStat = {};
		getThreadStatistics(SystemCores::IdToSystemCore[coreId], tempStat);
		stats.drop += tempStat.drop;
		stats.recv += tempStat.recv;

		if (!m_CoreConfiguration[coreId].IsAffinitySet)
			break;
	}
}

void PfRingDevice::clearCoreConfiguration()
{
	for (int i = 0; i < MAX_NUM_OF_CORES; i++)
		m_CoreConfiguration[i].clear();
}

int PfRingDevice::getCoresInUseCount() const
{
	int res = 0;
	for (int i = 0; i < MAX_NUM_OF_CORES; i++)
		if (m_CoreConfiguration[i].IsInUse)
			res++;

	return res;
}

void PfRingDevice::setPfRingDeviceAttributes()
{
	if (m_InterfaceIndex > -1)
		return;

	pfring* ring = NULL;
	bool closeRing = false;
	if (m_NumOfOpenedRxChannels > 0)
		ring = m_PfRingDescriptors[0];
	else
	{
		uint32_t flags = PF_RING_PROMISC | PF_RING_DNA_SYMMETRIC_RSS;
		ring = pfring_open(m_DeviceName.c_str(), DEFAULT_PF_RING_SNAPLEN, flags);
		closeRing = true;
	}

	if (ring == NULL)
	{
		PCPP_LOG_ERROR("Could not open a pfring for setting device attributes: MAC address, interface index and HW clock");
		return;
	}

	// set device MAC address

	uint8_t macAddress[6];
	if (pfring_get_bound_device_address(ring, macAddress) < 0)
		PCPP_LOG_ERROR("Unable to read the device MAC address for interface '" << m_DeviceName << "'");
	else
		m_MacAddress = MacAddress(macAddress);

	// set interface ID
	if (pfring_get_bound_device_ifindex(ring, &m_InterfaceIndex) < 0)
		PCPP_LOG_ERROR("Unable to read interface index of device");

	// try to set hardware device clock
	m_HwClockEnabled = setPfRingDeviceClock(ring);

	// set interface MTU
	int mtu = pfring_get_mtu_size(ring);
	if (mtu < 0)
		// cppcheck-suppress shiftNegative
		PCPP_LOG_ERROR("Could not get MTU. pfring_get_mtu_size returned an error: " << mtu);
	else
		m_DeviceMTU = mtu + sizeof(ether_header) + sizeof(vlan_header);

	if (Logger::getInstance().isDebugEnabled(PcapLogModulePfRingDevice))
	{
		std::string hwEnabled = (m_HwClockEnabled ? "enabled" : "disabled");
		PCPP_LOG_DEBUG("Capturing from " << m_DeviceName << " [" << m_MacAddress << "][ifIndex: " << m_InterfaceIndex << "][MTU: " << m_DeviceMTU << "], HW clock " << hwEnabled);
	}


	if (closeRing)
		pfring_close(ring);
}


bool PfRingDevice::sendData(const uint8_t* packetData, int packetDataLength, bool flushTxQueues)
{
	if (!m_DeviceOpened)
	{
		PCPP_LOG_ERROR("Device is not opened. Cannot send packets");
		return false;
	}

	uint8_t flushTxAsUint = (flushTxQueues? 1 : 0);

	#define MAX_TRIES 5

	int tries = 0;
	int res = 0;
	while (tries < MAX_TRIES)
	{
		// don't allow sending of data larger than the MTU, otherwise pfring_send will fail
		if (packetDataLength > m_DeviceMTU)
			packetDataLength = m_DeviceMTU;

		// if the device is opened, m_PfRingDescriptors[0] will always be set and enables
		res = pfring_send(m_PfRingDescriptors[0], (char*)packetData, packetDataLength, flushTxAsUint);

		// res == -1 means it's an error coming from "sendto" which is the Linux API PF_RING is using to send packets
		// errno == ENOBUFS means write buffer is full. PF_RING driver expects the userspace to handle this case
		// My implementation is to sleep for 10 usec and try again
		if (res == -1 && errno == ENOBUFS)
		{
			tries++;
			PCPP_LOG_DEBUG("Try #" << tries << ": Got ENOBUFS (write buffer full) error while sending packet. Sleeping 20 usec and trying again");
			usleep(2000);
		}
		else
			break;
	}

	if (tries >= MAX_TRIES)
	{
		PCPP_LOG_ERROR("Tried to send data " << MAX_TRIES << " times but write buffer is full");
		return false;
	}

	if (res < 0)
	{
		// res == -1 means it's an error coming from "sendto" which is the Linux API PF_RING is using to send packets
		if (res == -1)
			PCPP_LOG_ERROR("Error sending packet: Linux errno: " << strerror(errno) << " [" << errno << "]");
		else
			PCPP_LOG_ERROR("Error sending packet: pfring_send returned an error: " << res << " , errno: " << strerror(errno)<< " [" << errno << "]");
		return false;
	} else if (res != packetDataLength)
	{
		PCPP_LOG_ERROR("Couldn't send all bytes, only " << res << " bytes out of " << packetDataLength << " bytes were sent");
		return false;
	}

	return true;
}

bool PfRingDevice::sendPacket(const uint8_t* packetData, int packetDataLength)
{
	return sendData(packetData, packetDataLength, true);
}


bool PfRingDevice::sendPacket(const RawPacket& rawPacket)
{
	return sendData(rawPacket.getRawData(), rawPacket.getRawDataLen(), true);
}


bool PfRingDevice::sendPacket(const Packet& packet)
{
	return sendData(packet.getRawPacketReadOnly()->getRawData(), packet.getRawPacketReadOnly()->getRawDataLen(), true);
}


int PfRingDevice::sendPackets(const RawPacket* rawPacketsArr, int arrLength)
{
	int packetsSent = 0;
	for (int i = 0; i < arrLength; i++)
	{
		if (!sendData(rawPacketsArr[i].getRawData(), rawPacketsArr[i].getRawDataLen(), false))
			break;
		else
			packetsSent++;
	}

	// The following method isn't supported in PF_RING aware drivers, probably only in DNA and ZC
	pfring_flush_tx_packets(m_PfRingDescriptors[0]);

	PCPP_LOG_DEBUG(packetsSent << " out of " << arrLength << " raw packets were sent successfully");

	return packetsSent;
}

int PfRingDevice::sendPackets(const Packet** packetsArr, int arrLength)
{
	int packetsSent = 0;
	for (int i = 0; i < arrLength; i++)
	{
		if (!sendData(packetsArr[i]->getRawPacketReadOnly()->getRawData(), packetsArr[i]->getRawPacketReadOnly()->getRawDataLen(), false))
			break;
		else
			packetsSent++;
	}

	// The following method isn't supported in PF_RING aware drivers, probably only in DNA and ZC
	pfring_flush_tx_packets(m_PfRingDescriptors[0]);

	PCPP_LOG_DEBUG(packetsSent << " out of " << arrLength << " packets were sent successfully");

	return packetsSent;
}

int PfRingDevice::sendPackets(const RawPacketVector& rawPackets)
{
	int packetsSent = 0;
	for (RawPacketVector::ConstVectorIterator iter = rawPackets.begin(); iter != rawPackets.end(); iter++)
	{
		if (!sendData((*iter)->getRawData(), (*iter)->getRawDataLen(), false))
			break;
		else
			packetsSent++;
	}

	// The following method isn't supported in PF_RING aware drivers, probably only in DNA and ZC
	pfring_flush_tx_packets(m_PfRingDescriptors[0]);

	PCPP_LOG_DEBUG(packetsSent << " out of " << rawPackets.size() << " raw packets were sent successfully");

	return packetsSent;
}

PfRingDevice::CoreConfiguration::CoreConfiguration()
	: Channel(NULL), IsInUse(false), IsAffinitySet(true)
{
}

void PfRingDevice::CoreConfiguration::clear()
{
	Channel = NULL;
	IsInUse = false;
	IsAffinitySet = true;
}

} // namespace pcpp

// GCOVR_EXCL_STOP

#endif /* USE_PF_RING */