Skip to content

Commit b373a96

Browse files
committed
NfbPlugin: add support for reading from multiple NFB devices
1 parent 6b053db commit b373a96

File tree

2 files changed

+53
-6
lines changed

2 files changed

+53
-6
lines changed

src/plugins/input/nfb/src/ndp.cpp

Lines changed: 50 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,32 @@ static const PluginManifest ndpPluginManifest = {
4343
},
4444
};
4545

46+
static std::vector<std::string>
47+
parseDevices(const std::string& input)
48+
{
49+
std::vector<std::string> result;
50+
51+
size_t colon_pos = input.find(':');
52+
std::string suffix;
53+
std::string devices;
54+
55+
if (colon_pos != std::string::npos) {
56+
devices = input.substr(0, colon_pos);
57+
suffix = input.substr(colon_pos);
58+
} else {
59+
devices = input;
60+
suffix = "";
61+
}
62+
63+
std::stringstream ss(devices);
64+
std::string dev;
65+
while (std::getline(ss, dev, ',')) {
66+
result.push_back(dev + suffix);
67+
}
68+
69+
return result;
70+
}
71+
4672
NdpPacketReader::NdpPacketReader(const std::string& params)
4773
{
4874
init(params.c_str());
@@ -65,18 +91,35 @@ void NdpPacketReader::init(const char* params)
6591
if (parser.m_dev.empty()) {
6692
throw PluginError("specify device path");
6793
}
94+
6895
init_ifc(parser.m_dev);
6996
}
7097

7198
void NdpPacketReader::close()
7299
{
73-
ndpReader.close();
100+
for (size_t i = 0; i < m_readers_count; i++) {
101+
ndpReader[i].close();
102+
}
74103
}
75104

76105
void NdpPacketReader::init_ifc(const std::string& dev)
77106
{
78-
if (ndpReader.init_interface(dev) != 0) {
79-
throw PluginError(ndpReader.error_msg);
107+
std::vector<std::string> devs;
108+
devs = parseDevices(dev);
109+
m_readers_count = devs.size();
110+
if (m_readers_count > 2) {
111+
throw PluginError("too many devices specified");
112+
}
113+
114+
for (auto &dev : devs) {
115+
std::cout << "NDP reader*: " << dev << std::endl;
116+
}
117+
118+
119+
for (size_t i = 0; i < m_readers_count; i++) {
120+
if (ndpReader[i].init_interface(devs[i]) != 0) {
121+
throw PluginError(ndpReader[i].error_msg);
122+
}
80123
}
81124
}
82125

@@ -88,17 +131,19 @@ InputPlugin::Result NdpPacketReader::get(PacketBlock& packets)
88131
size_t read_pkts = 0;
89132
int ret = -1;
90133

134+
NdpReader& reader = ndpReader[m_reader_idx++ % m_readers_count];
135+
91136
packets.cnt = 0;
92137
for (unsigned i = 0; i < packets.size; i++) {
93-
ret = ndpReader.get_pkt(&ndp_packet, &timestamp);
138+
ret = reader.get_pkt(&ndp_packet, &timestamp);
94139
if (ret == 0) {
95140
if (opt.pblock->cnt) {
96141
break;
97142
}
98143
return Result::TIMEOUT;
99144
} else if (ret < 0) {
100145
// Error occured.
101-
throw PluginError(ndpReader.error_msg);
146+
throw PluginError(reader.error_msg);
102147
}
103148
read_pkts++;
104149
parse_packet(

src/plugins/input/nfb/src/ndp.hpp

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -81,7 +81,9 @@ class NdpPacketReader : public InputPlugin {
8181

8282
telemetry::Content get_queue_telemetry();
8383

84-
NdpReader ndpReader;
84+
NdpReader ndpReader[2];
85+
std::size_t m_readers_count;
86+
uint64_t m_reader_idx = 0;
8587
RxStats m_stats = {};
8688

8789
void init_ifc(const std::string& dev);

0 commit comments

Comments
 (0)