diff --git a/repos/os/src/server/nic_router/interface.cc b/repos/os/src/server/nic_router/interface.cc index 83cc6ba234..fd7cb76157 100644 --- a/repos/os/src/server/nic_router/interface.cc +++ b/repos/os/src/server/nic_router/interface.cc @@ -927,21 +927,32 @@ void Interface::_handle_eth(void *const eth_base, } -void Interface::send(Ethernet_frame ð, Genode::size_t const size) +void Interface::send(Ethernet_frame ð, size_t eth_size) { + send(eth_size, [&] (void *pkt_base) { + Genode::memcpy(pkt_base, (void *)ð, eth_size); + }); +} + + +void Interface::_send_alloc_pkt(Packet_descriptor &pkt, + void * &pkt_base, + size_t pkt_size) +{ + pkt = _source().alloc_packet(pkt_size); + pkt_base = _source().packet_content(pkt); +} + + +void Interface::_send_submit_pkt(Packet_descriptor &pkt, + void * &pkt_base, + size_t pkt_size) +{ + _source().submit_packet(pkt); + _domain.raise_tx_bytes(pkt_size); if (_config().verbose()) { - log("(", _domain, " <- router) ", eth); } - try { - /* copy and submit packet */ - Packet_descriptor const pkt = _source().alloc_packet(size); - char *content = _source().packet_content(pkt); - Genode::memcpy((void *)content, (void *)ð, size); - _source().submit_packet(pkt); - _domain.raise_tx_bytes(size); - } - catch (Packet_stream_source::Packet_alloc_failed) { - if (_config().verbose()) { - log("Failed to allocate packet"); } + log("(", _domain, " <- router) ", + *reinterpret_cast(pkt_base)); } } diff --git a/repos/os/src/server/nic_router/interface.h b/repos/os/src/server/nic_router/interface.h index 7936423400..b9aadec292 100644 --- a/repos/os/src/server/nic_router/interface.h +++ b/repos/os/src/server/nic_router/interface.h @@ -168,6 +168,14 @@ class Net::Interface : public Genode::List::Element virtual Packet_stream_source &_source() = 0; + void _send_alloc_pkt(Genode::Packet_descriptor &pkt, + void * &pkt_base, + Genode::size_t pkt_size); + + void _send_submit_pkt(Genode::Packet_descriptor &pkt, + void * &pkt_base, + Genode::size_t pkt_size); + /*********************************** ** Packet-stream signal handlers ** @@ -212,10 +220,25 @@ class Net::Interface : public Genode::List::Element ~Interface(); - void dhcp_allocation_expired(Dhcp_allocation &allocation); - void send(Ethernet_frame ð, Genode::size_t const eth_size); + template + void send(Genode::size_t pkt_size, FUNC && write_to_pkt) + { + try { + Packet_descriptor pkt; + void *pkt_base; + + _send_alloc_pkt(pkt, pkt_base, pkt_size); + write_to_pkt(pkt_base); + _send_submit_pkt(pkt, pkt_base, pkt_size); + } + catch (Packet_stream_source::Packet_alloc_failed) { + Genode::warning("failed to allocate packet"); + } + } + + void send(Ethernet_frame ð, Genode::size_t eth_size); Link_list &dissolved_links(L3_protocol const protocol);