Skip to content

Commit a8d0dca

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

File tree

2 files changed

+46
-6
lines changed

2 files changed

+46
-6
lines changed

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

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

46+
static std::vector<std::string> parseDevices(const std::string& input)
47+
{
48+
std::vector<std::string> result;
49+
50+
size_t colon_pos = input.find(':');
51+
std::string suffix;
52+
std::string devices;
53+
54+
if (colon_pos != std::string::npos) {
55+
devices = input.substr(0, colon_pos);
56+
suffix = input.substr(colon_pos);
57+
} else {
58+
devices = input;
59+
suffix = "";
60+
}
61+
62+
std::stringstream ss(devices);
63+
std::string dev;
64+
while (std::getline(ss, dev, ',')) {
65+
result.push_back(dev + suffix);
66+
}
67+
68+
return result;
69+
}
70+
4671
NdpPacketReader::NdpPacketReader(const std::string& params)
4772
{
4873
init(params.c_str());
@@ -65,18 +90,29 @@ void NdpPacketReader::init(const char* params)
6590
if (parser.m_dev.empty()) {
6691
throw PluginError("specify device path");
6792
}
93+
6894
init_ifc(parser.m_dev);
6995
}
7096

7197
void NdpPacketReader::close()
7298
{
73-
ndpReader.close();
99+
for (size_t i = 0; i < m_readers_count; i++) {
100+
ndpReader[i].close();
101+
}
74102
}
75103

76104
void NdpPacketReader::init_ifc(const std::string& dev)
77105
{
78-
if (ndpReader.init_interface(dev) != 0) {
79-
throw PluginError(ndpReader.error_msg);
106+
const std::vector<std::string> devs = parseDevices(dev);
107+
m_readers_count = devs.size();
108+
if (m_readers_count > 2) {
109+
throw PluginError("too many devices specified");
110+
}
111+
112+
for (size_t i = 0; i < m_readers_count; i++) {
113+
if (ndpReader[i].init_interface(devs[i]) != 0) {
114+
throw PluginError(ndpReader[i].error_msg);
115+
}
80116
}
81117
}
82118

@@ -88,17 +124,19 @@ InputPlugin::Result NdpPacketReader::get(PacketBlock& packets)
88124
size_t read_pkts = 0;
89125
int ret = -1;
90126

127+
NdpReader& reader = ndpReader[m_reader_idx++ % m_readers_count];
128+
91129
packets.cnt = 0;
92130
for (unsigned i = 0; i < packets.size; i++) {
93-
ret = ndpReader.get_pkt(&ndp_packet, &timestamp);
131+
ret = reader.get_pkt(&ndp_packet, &timestamp);
94132
if (ret == 0) {
95133
if (opt.pblock->cnt) {
96134
break;
97135
}
98136
return Result::TIMEOUT;
99137
} else if (ret < 0) {
100138
// Error occured.
101-
throw PluginError(ndpReader.error_msg);
139+
throw PluginError(reader.error_msg);
102140
}
103141
read_pkts++;
104142
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)