17 #include <linux/sockios.h> 18 #include <linux/filter.h> 29 prefix_ senf::emu::detail::HardwareEthernetInterfaceNet::HardwareEthernetInterfaceNet()
30 : socket (
senf::
noinit), source (socket), sink (socket),
31 annotatorRx_(true, true), annotatorTx_(false, true),
32 netOutput (annotatorRx_.output), netInput (annotatorTx_.input)
34 senf::ppi::connect(source.output, annotatorRx_.input);
35 senf::ppi::connect(annotatorTx_.output, sink.input);
40 source.handle(socket_);
52 prefix_ unsigned senf::emu::detail::HardwareEthernetInterfaceNet::rcvBuf()
55 return socket.protocol().rcvbuf();
59 prefix_ void senf::emu::detail::HardwareEthernetInterfaceNet::rcvBuf(
unsigned rcvbuf)
62 socket.protocol().rcvbuf(rcvbuf);
65 prefix_ unsigned senf::emu::detail::HardwareEthernetInterfaceNet::sndBuf()
68 return socket.protocol().sndbuf();
72 prefix_ void senf::emu::detail::HardwareEthernetInterfaceNet::sndBuf(
unsigned sndbuf)
75 socket.protocol().sndbuf(sndbuf);
78 prefix_ void senf::emu::detail::HardwareEthernetInterfaceNet::setupBPF(
senf::MACAddress const & hwAddr,
bool srcOnly)
84 static struct sock_filter BPF_code_src[] = {
85 { 0x20, 0, 0, 0x00000008 },
86 { 0x15, 0, 3, (boost::uint32_t(hwAddr[3]) << 24) | (boost::uint32_t(hwAddr[2]) << 16) | (boost::uint32_t(hwAddr[1]) << 8) | boost::uint32_t(hwAddr[0]) },
87 { 0x28, 0, 0, 0x00000006 },
88 { 0x15, 0, 1, (boost::uint32_t(hwAddr[5]) << 8) | boost::uint32_t(hwAddr[4])},
89 { 0x06, 0, 0, 0x00000000 },
90 { 0x06, 0, 0, 0x0000ffff }
94 static struct sock_filter BPF_code_src_dst[] = {
95 { 0x20, 0, 0, 0x00000008 },
96 { 0x15, 0, 3, (boost::uint32_t(hwAddr[3]) << 24) | (boost::uint32_t(hwAddr[2]) << 16) | (boost::uint32_t(hwAddr[1]) << 8) | boost::uint32_t(hwAddr[0]) },
97 { 0x28, 0, 0, 0x00000006 },
98 { 0x15, 0, 1, (boost::uint32_t(hwAddr[5]) << 8) | boost::uint32_t(hwAddr[4])},
99 { 0x20, 0, 0, 0x00000002 },
100 { 0x15, 0, 3, (boost::uint32_t(hwAddr[3]) << 24) | (boost::uint32_t(hwAddr[2]) << 16) | (boost::uint32_t(hwAddr[1]) << 8) | boost::uint32_t(hwAddr[0]) },
101 { 0x28, 0, 0, 0x00000000 },
102 { 0x15, 0, 1, (boost::uint32_t(hwAddr[5]) << 8) | boost::uint32_t(hwAddr[4])},
103 { 0x06, 0, 0, 0x00000000 },
104 { 0x06, 0, 0, 0x0000ffff }
108 socket.protocol().attachSocketFilter(BPF_code_src);
110 socket.protocol().attachSocketFilter(BPF_code_src_dst);
113 prefix_ void senf::emu::detail::HardwareEthernetInterfaceNet::clearBPF()
118 socket.protocol().detachSocketFilter();
127 struct DisableInterfaceGuard
130 : iface_ (iface), enabled_ (iface_.enabled())
131 {
if (enabled_) iface_.disable(); }
133 ~DisableInterfaceGuard()
134 {
if (enabled_) iface_.enable(); }
143 rcvBufSize_ (1024), sndBufSize_ (96*1024), qlen_ (512), pvid_(
VLanId::None), accessMode_(false)
148 namespace fty = console::factory;
152 .doc(
"dumps and resets the MMAP RX/TX statistics"));
154 .
add(
"sndBuf", fty::Command(
156 .doc(
"set the send socket buffer size in bytes"));
158 .
add(
"sndBuf", fty::Command(
160 .doc(
"get the send socket buffer size in bytes"));
162 .
add(
"rcvBuf", fty::Command(
164 .doc(
"set the receive socket buffer size in bytes"));
166 .
add(
"rcvBuf", fty::Command(
168 .doc(
"get the receive socket buffer size in bytes"));
170 .
add(
"qlen", fty::Command(
172 .doc(
"set mmap read queue length in # of packets"));
174 .
add(
"qlen", fty::Command(
176 .doc(
"get mmap read queue length in # of packets"));
178 .
add(
"maxBurst", fty::Command(
180 .doc(
"set max burst rate"));
182 .
add(
"maxBurst", fty::Command(
184 .doc(
"get max burst rate"));
186 .
add(
"pvid", fty::Command(
188 .doc(
"enables filtering for a specific PVID (VLAN ID must be 0...4095)"));
190 .
add(
"pvid", fty::Command(
192 .doc(
"report the currently configured PVID (-1 means none)"));
198 annotatorRx_.id(initialId_);
217 prefix_ void senf::emu::HardwareEthernetInterface::v_mcAdd(
MACAddress const & address)
219 if (HardwareEthernetInterfaceNet::socket.valid())
220 HardwareEthernetInterfaceNet::socket.protocol().mcAdd( dev_, address);
223 prefix_ void senf::emu::HardwareEthernetInterface::v_mcDrop(
MACAddress const & address)
225 if (HardwareEthernetInterfaceNet::socket.valid())
226 HardwareEthernetInterfaceNet::socket.protocol().mcDrop( dev_, address);
229 prefix_ void senf::emu::HardwareEthernetInterface::init_sockets()
231 std::string vlanDevice (
device() +
"." + senf::str(pvid_));
247 socket_.protocol().rcvbuf( rcvBufSize_);
248 socket_.protocol().sndbuf( sndBufSize_);
251 HardwareEthernetInterfaceNet::assignSockets(socket_);
253 HardwareEthernetInterfaceNet::setupBPF(
id(),
true);
258 annotatorRx_.insertTag(pvid_);
259 annotatorTx_.removeTag(pvid_);
261 annotatorRx_.removeTag(pvid_);
262 annotatorTx_.insertTag(pvid_);
265 annotatorRx_.clearTag();
266 annotatorTx_.clearTag();
270 annotatorRx_.promisc(
promisc());
273 prefix_ void senf::emu::HardwareEthernetInterface::close_sockets()
284 HardwareEthernetInterfaceNet::assignSockets(skt);
293 if (initialId_ !=
id()) {
320 prefix_ bool senf::emu::HardwareEthernetInterface::v_enabled()
323 return HardwareEthernetInterfaceNet::socket.valid();
328 DisableInterfaceGuard guard (*
this);
330 annotatorRx_.id(
id());
339 prefix_ bool senf::emu::HardwareEthernetInterface::v_promisc()
345 prefix_ void senf::emu::HardwareEthernetInterface::v_promisc(
bool p)
352 prefix_ bool senf::emu::HardwareEthernetInterface::v_annotationMode()
355 return annotatorRx_.annotate();
358 prefix_ void senf::emu::HardwareEthernetInterface::v_annotationMode(
bool a)
360 annotatorRx_.annotate(a);
372 unsigned speed (1000);
376 speed = ctrl_.
speed();
380 if ((speed < 1000) and (v > 1500)) {
388 prefix_ void senf::emu::HardwareEthernetInterface::v_flushRxQueues()
390 HardwareEthernetInterfaceNet::source.flush();
393 prefix_ void senf::emu::HardwareEthernetInterface::v_flushTxQueues()
395 HardwareEthernetInterfaceNet::sink.flush();
401 sndBufSize_ =
std::max(sndbuf, 2048u);
402 HardwareEthernetInterfaceNet::sndBuf( sndBufSize_);
408 return sndBufSize_ = HardwareEthernetInterfaceNet::sndBuf();
414 rcvBufSize_ =
std::max(rcvbuf, 4096u);
415 HardwareEthernetInterfaceNet::rcvBuf( rcvBufSize_);
421 return rcvBufSize_ = HardwareEthernetInterfaceNet::rcvBuf();
426 if (!accessMode and p.
stag())
431 accessMode_ = accessMode;
438 return HardwareEthernetInterfaceNet::annotatorRx_.vlanMismatch();
443 return HardwareEthernetInterfaceNet::annotatorTx_.vlanMismatch();
467 return source.dropped();
472 return sink.dropped();
478 return source.maxBurst();
483 source.maxBurst(maxBurst);
488 if (HardwareEthernetInterfaceNet::socket.valid()) {
489 auto rs (HardwareEthernetInterfaceNet::socket.protocol().rxStats());
490 os <<
"MMAP Rx (fd=" << socket.protocol().fd() <<
", avail " << socket.protocol().available() <<
") stats: "; rs.dump(os);
491 auto ts (HardwareEthernetInterfaceNet::socket.protocol().txStats());
492 os <<
" MMAP Tx (fd=" << socket.protocol().fd() <<
") stats: "; ts.dump(os);
494 os <<
"Socket closed. No stats available.";
502 if (HardwareEthernetInterfaceNet::socket)
503 return socket.protocol().interfaceDead();
510 prefix_ unsigned senf::emu::HardwareEthernetInterface::burstMax()
512 return source.burstMax();
bool promisc() const
Get interface promiscuous status.
#define SENF_MEMBINDFNP(ret, cls, fn, args)
#define SENF_THROW_SYSTEM_EXCEPTION(desc)
std::uint32_t vlanMismatchTx()
void addVLAN(std::uint16_t vlanId)
bool enabled() const
true, if interface enabled
virtual ~HardwareEthernetInterface()
HardwareEthernetInterface(std::string const &name)
void reset(std::string const &interface_name)
std::tuple< unsigned, unsigned, unsigned > txDropped()
void dumpMmapStats(std::ostream &os)
console::DirectoryNode & consoleDir() const
Access interface console directory.
std::string const & device() const
NodeType & add(std::string const &name, boost::shared_ptr< NodeType > node)
std::uint32_t vlanMismatchRx()
virtual unsigned v_mtu() const override
Called to get interface MTU.
Hardware Ethernet Interface.
MACAddress const & id() const
Get interface MAC Address.
unsigned maxBurst() const
MACAddress hardwareAddress() const
void delVLAN(std::uint16_t vlanId)
VLanId const & pvid() const
console::DirectoryNode & interfaceDir()
std::string const & v_device() const override
ProtocolClientSocketHandle< ConnectedMMapPacketSocketProtocol< QueueReadPolicy, QueueWritePolicy > > ConnectedMMapPacketSocketHandle
std::pair< unsigned, unsigned > rxDropped()
virtual void v_disable() override
Called to disable interface.
HardwareEthernetInterface public header.
virtual void v_enable() override
Called to enable interface.