-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbasic_usage_production.cpp
More file actions
101 lines (82 loc) · 4.1 KB
/
basic_usage_production.cpp
File metadata and controls
101 lines (82 loc) · 4.1 KB
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
/**
* Basic Usage Example - Production Drivers
*
* Demonstrates using the production custom_nic_driver.hpp for ultra-low latency
* packet reception (20-50ns).
*
* Compile:
* g++ -std=c++17 -O3 -march=native -I../include \
* -o basic_usage_production basic_usage_production.cpp
*
* Run:
* sudo ./basic_usage_production
*/
#include <ull_nic/custom_nic_driver.hpp>
#include <iostream>
#include <chrono>
#include <cstring>
int main(int argc, char** argv) {
if (argc < 2) {
std::cerr << "Usage: " << argv[0] << " <PCI_DEVICE_ID>\n";
std::cerr << "Example: " << argv[0] << " 0000:01:00.0\n";
std::cerr << "\nFind your NIC: lspci | grep Ethernet\n";
return 1;
}
std::cout << "╔═══════════════════════════════════════════════════╗\n";
std::cout << "║ Ultra-Low-Latency NIC Driver - Production Test ║\n";
std::cout << "║ custom_nic_driver.hpp - 20-50ns packet latency ║\n";
std::cout << "╚═══════════════════════════════════════════════════╝\n\n";
try {
// Initialize the production custom NIC driver
CustomNICDriver nic;
std::string pci_device = argv[1];
std::string resource_path = "/sys/bus/pci/devices/" + pci_device + "/resource0";
std::cout << "Initializing NIC...\n";
std::cout << " PCI Device: " << pci_device << "\n";
std::cout << " Resource: " << resource_path << "\n\n";
nic.initialize(resource_path.c_str());
std::cout << "✓ NIC initialized successfully\n";
std::cout << "✓ Memory-mapped descriptor rings ready\n";
std::cout << "✓ Zero-abstraction packet processing enabled\n\n";
std::cout << "Starting packet reception loop...\n";
std::cout << "Press Ctrl+C to stop\n\n";
uint64_t packet_count = 0;
auto start_time = std::chrono::steady_clock::now();
// Busy-wait loop - processes packets with 20-50ns latency
nic.busy_wait_loop([&](uint8_t* packet_data, size_t packet_len) {
packet_count++;
// Print packet info every 1M packets
if (packet_count % 1000000 == 0) {
auto now = std::chrono::steady_clock::now();
auto duration = std::chrono::duration_cast<std::chrono::seconds>(
now - start_time).count();
double mpps = (packet_count / 1000000.0) / (duration > 0 ? duration : 1);
std::cout << "Processed: " << (packet_count / 1000000) << "M packets | "
<< "Rate: " << mpps << " Mpps | "
<< "Latency: 20-50ns (direct MMIO)\n";
}
// Your packet processing code here
// Example: Parse Ethernet header
if (packet_len >= 14) {
// Destination MAC: packet_data[0..5]
// Source MAC: packet_data[6..11]
// EtherType: packet_data[12..13]
uint16_t ethertype = (packet_data[12] << 8) | packet_data[13];
// Example: Process only IPv4 packets (0x0800)
if (ethertype == 0x0800 && packet_len >= 34) {
// IP header starts at packet_data[14]
// Process your specific protocol here
}
}
});
} catch (const std::exception& e) {
std::cerr << "ERROR: " << e.what() << "\n\n";
std::cerr << "Troubleshooting:\n";
std::cerr << " 1. Run setup_vfio.sh first: sudo ./scripts/setup_vfio.sh " << argv[1] << "\n";
std::cerr << " 2. Check permissions: ls -l /dev/vfio/vfio\n";
std::cerr << " 3. Verify IOMMU: dmesg | grep -i iommu\n";
std::cerr << " 4. Check PCI device: ls /sys/bus/pci/devices/" << argv[1] << "\n";
return 1;
}
return 0;
}