mirror of
https://github.com/genodelabs/genode.git
synced 2025-06-19 15:43:56 +00:00
@ -13,26 +13,24 @@
|
|||||||
* under the terms of the GNU General Public License version 2.
|
* under the terms of the GNU General Public License version 2.
|
||||||
*/
|
*/
|
||||||
|
|
||||||
#include <base/env.h>
|
#include <base/component.h>
|
||||||
#include <base/sleep.h>
|
#include <base/heap.h>
|
||||||
#include <os/server.h>
|
|
||||||
#include <root/component.h>
|
#include <root/component.h>
|
||||||
#include <util/arg_string.h>
|
#include <util/arg_string.h>
|
||||||
#include <util/misc_math.h>
|
#include <util/misc_math.h>
|
||||||
#include <nic/component.h>
|
#include <nic/component.h>
|
||||||
#include <nic/packet_allocator.h>
|
#include <nic/packet_allocator.h>
|
||||||
|
|
||||||
namespace Nic {
|
namespace Nic_loopback {
|
||||||
|
class Session_component;
|
||||||
class Loopback_component;
|
|
||||||
class Root;
|
class Root;
|
||||||
|
class Main;
|
||||||
|
|
||||||
|
using namespace Genode;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
namespace Server { struct Main; }
|
class Nic_loopback::Session_component : public Nic::Session_component
|
||||||
|
|
||||||
|
|
||||||
class Nic::Loopback_component : public Nic::Session_component
|
|
||||||
{
|
{
|
||||||
public:
|
public:
|
||||||
|
|
||||||
@ -47,19 +45,20 @@ class Nic::Loopback_component : public Nic::Session_component
|
|||||||
* \param ep entry point used for packet stream
|
* \param ep entry point used for packet stream
|
||||||
* channels
|
* channels
|
||||||
*/
|
*/
|
||||||
Loopback_component(Genode::size_t const tx_buf_size,
|
Session_component(size_t const tx_buf_size,
|
||||||
Genode::size_t const rx_buf_size,
|
size_t const rx_buf_size,
|
||||||
Genode::Allocator &rx_block_md_alloc,
|
Allocator &rx_block_md_alloc,
|
||||||
Genode::Ram_session &ram_session,
|
Ram_session &ram_session,
|
||||||
Server::Entrypoint &ep)
|
Entrypoint &ep)
|
||||||
: Session_component(tx_buf_size, rx_buf_size,
|
:
|
||||||
rx_block_md_alloc, ram_session, ep)
|
Nic::Session_component(tx_buf_size, rx_buf_size, rx_block_md_alloc,
|
||||||
|
ram_session, ep)
|
||||||
{ }
|
{ }
|
||||||
|
|
||||||
Mac_address mac_address() override
|
Nic::Mac_address mac_address() override
|
||||||
{
|
{
|
||||||
char buf[] = {1,2,3,4,5,6};
|
char buf[] = {1,2,3,4,5,6};
|
||||||
Mac_address result((void*)buf);
|
Nic::Mac_address result((void*)buf);
|
||||||
return result;
|
return result;
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -73,13 +72,11 @@ class Nic::Loopback_component : public Nic::Session_component
|
|||||||
};
|
};
|
||||||
|
|
||||||
|
|
||||||
void Nic::Loopback_component::_handle_packet_stream()
|
void Nic_loopback::Session_component::_handle_packet_stream()
|
||||||
{
|
{
|
||||||
using namespace Genode;
|
size_t const alloc_size = Nic::Packet_allocator::DEFAULT_PACKET_SIZE;
|
||||||
|
|
||||||
unsigned const alloc_size = Nic::Packet_allocator::DEFAULT_PACKET_SIZE;
|
/* loop while we can make progress */
|
||||||
|
|
||||||
/* loop unless we cannot make any progress */
|
|
||||||
for (;;) {
|
for (;;) {
|
||||||
|
|
||||||
/* flush acknowledgements for the echoes packets */
|
/* flush acknowledgements for the echoes packets */
|
||||||
@ -118,20 +115,19 @@ void Nic::Loopback_component::_handle_packet_stream()
|
|||||||
|
|
||||||
Packet_descriptor packet_to_client;
|
Packet_descriptor packet_to_client;
|
||||||
try {
|
try {
|
||||||
packet_to_client = _rx.source()->alloc_packet(alloc_size);
|
packet_to_client = _rx.source()->alloc_packet(alloc_size); }
|
||||||
} catch (Session::Rx::Source::Packet_alloc_failed) {
|
catch (Session::Rx::Source::Packet_alloc_failed) {
|
||||||
continue;
|
continue; }
|
||||||
}
|
|
||||||
|
|
||||||
/* obtain packet */
|
/* obtain packet */
|
||||||
Packet_descriptor const packet_from_client = _tx.sink()->get_packet();
|
Packet_descriptor const packet_from_client = _tx.sink()->get_packet();
|
||||||
if (!packet_from_client.size()) {
|
if (!packet_from_client.size()) {
|
||||||
Genode::warning("received zero-size packet");
|
warning("received zero-size packet");
|
||||||
_rx.source()->release_packet(packet_to_client);
|
_rx.source()->release_packet(packet_to_client);
|
||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
|
|
||||||
Genode::memcpy(_rx.source()->packet_content(packet_to_client),
|
memcpy(_rx.source()->packet_content(packet_to_client),
|
||||||
_tx.sink()->packet_content(packet_from_client),
|
_tx.sink()->packet_content(packet_from_client),
|
||||||
packet_from_client.size());
|
packet_from_client.size());
|
||||||
|
|
||||||
@ -144,24 +140,23 @@ void Nic::Loopback_component::_handle_packet_stream()
|
|||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
class Nic::Root : public Genode::Root_component<Loopback_component>
|
class Nic_loopback::Root : public Root_component<Session_component>
|
||||||
{
|
{
|
||||||
private:
|
private:
|
||||||
|
|
||||||
Server::Entrypoint &_ep;
|
Entrypoint &_ep;
|
||||||
|
Ram_session &_ram;
|
||||||
|
|
||||||
protected:
|
protected:
|
||||||
|
|
||||||
Loopback_component*_create_session(const char *args)
|
Session_component *_create_session(char const *args)
|
||||||
{
|
{
|
||||||
using namespace Genode;
|
|
||||||
|
|
||||||
size_t ram_quota = Arg_string::find_arg(args, "ram_quota" ).ulong_value(0);
|
size_t ram_quota = Arg_string::find_arg(args, "ram_quota" ).ulong_value(0);
|
||||||
size_t tx_buf_size = Arg_string::find_arg(args, "tx_buf_size").ulong_value(0);
|
size_t tx_buf_size = Arg_string::find_arg(args, "tx_buf_size").ulong_value(0);
|
||||||
size_t rx_buf_size = Arg_string::find_arg(args, "rx_buf_size").ulong_value(0);
|
size_t rx_buf_size = Arg_string::find_arg(args, "rx_buf_size").ulong_value(0);
|
||||||
|
|
||||||
/* deplete ram quota by the memory needed for the session structure */
|
/* deplete ram quota by the memory needed for the session structure */
|
||||||
size_t session_size = max(4096UL, (unsigned long)sizeof(Session_component));
|
size_t session_size = max(4096UL, (size_t)sizeof(Session_component));
|
||||||
if (ram_quota < session_size)
|
if (ram_quota < session_size)
|
||||||
throw Root::Quota_exceeded();
|
throw Root::Quota_exceeded();
|
||||||
|
|
||||||
@ -171,48 +166,39 @@ class Nic::Root : public Genode::Root_component<Loopback_component>
|
|||||||
*/
|
*/
|
||||||
if (tx_buf_size + rx_buf_size < tx_buf_size ||
|
if (tx_buf_size + rx_buf_size < tx_buf_size ||
|
||||||
tx_buf_size + rx_buf_size > ram_quota - session_size) {
|
tx_buf_size + rx_buf_size > ram_quota - session_size) {
|
||||||
Genode::error("insufficient 'ram_quota', got ", ram_quota, ", "
|
error("insufficient 'ram_quota', got ", ram_quota, ", "
|
||||||
"need ", tx_buf_size + rx_buf_size + session_size);
|
"need ", tx_buf_size + rx_buf_size + session_size);
|
||||||
throw Root::Quota_exceeded();
|
throw Root::Quota_exceeded();
|
||||||
}
|
}
|
||||||
|
|
||||||
return new (md_alloc()) Loopback_component(tx_buf_size, rx_buf_size,
|
return new (md_alloc()) Session_component(tx_buf_size, rx_buf_size,
|
||||||
*env()->heap(),
|
*md_alloc(), _ram, _ep);
|
||||||
*env()->ram_session(),
|
|
||||||
_ep);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
public:
|
public:
|
||||||
|
|
||||||
Root(Server::Entrypoint &ep, Genode::Allocator &md_alloc)
|
Root(Entrypoint &ep, Ram_session &ram, Allocator &md_alloc)
|
||||||
:
|
:
|
||||||
Genode::Root_component<Loopback_component>(&ep.rpc_ep(), &md_alloc),
|
Root_component<Session_component>(&ep.rpc_ep(), &md_alloc),
|
||||||
_ep(ep)
|
_ep(ep), _ram(ram)
|
||||||
{ }
|
{ }
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|
||||||
struct Server::Main
|
struct Nic_loopback::Main
|
||||||
{
|
{
|
||||||
Entrypoint &ep;
|
Env &_env;
|
||||||
|
|
||||||
Nic::Root nic_root { ep, *Genode::env()->heap() };
|
Heap _heap { _env.ram(), _env.rm() };
|
||||||
|
|
||||||
Main(Entrypoint &ep) : ep(ep)
|
Nic_loopback::Root _root { _env.ep(), _env.ram(), _heap };
|
||||||
|
|
||||||
|
Main(Env &env) : _env(env)
|
||||||
{
|
{
|
||||||
Genode::env()->parent()->announce(ep.manage(nic_root));
|
_env.parent().announce(_env.ep().manage(_root));
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|
||||||
namespace Server {
|
void Component::construct(Genode::Env &env) { static Nic_loopback::Main main(env); }
|
||||||
|
|
||||||
char const *name() { return "nicloop_ep"; }
|
|
||||||
|
|
||||||
size_t stack_size() { return 16*1024*sizeof(long); }
|
|
||||||
|
|
||||||
void construct(Entrypoint &ep)
|
|
||||||
{
|
|
||||||
static Main main(ep);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
@ -1,3 +1,3 @@
|
|||||||
TARGET = nic_loopback
|
TARGET = nic_loopback
|
||||||
SRC_CC = main.cc
|
SRC_CC = main.cc
|
||||||
LIBS = base server
|
LIBS = base
|
||||||
|
@ -5,192 +5,368 @@
|
|||||||
*/
|
*/
|
||||||
|
|
||||||
/*
|
/*
|
||||||
* Copyright (C) 2009-2013 Genode Labs GmbH
|
* Copyright (C) 2009-2016 Genode Labs GmbH
|
||||||
*
|
*
|
||||||
* This file is part of the Genode OS framework, which is distributed
|
* This file is part of the Genode OS framework, which is distributed
|
||||||
* under the terms of the GNU General Public License version 2.
|
* under the terms of the GNU General Public License version 2.
|
||||||
*/
|
*/
|
||||||
|
|
||||||
|
#include <base/component.h>
|
||||||
#include <base/log.h>
|
#include <base/log.h>
|
||||||
|
#include <base/heap.h>
|
||||||
#include <base/allocator_avl.h>
|
#include <base/allocator_avl.h>
|
||||||
#include <nic_session/connection.h>
|
#include <nic_session/connection.h>
|
||||||
#include <nic/packet_allocator.h>
|
#include <nic/packet_allocator.h>
|
||||||
#include <timer_session/connection.h>
|
|
||||||
|
namespace Test {
|
||||||
|
struct Base;
|
||||||
|
struct Roundtrip;
|
||||||
|
struct Batch;
|
||||||
|
struct Main;
|
||||||
|
|
||||||
|
using namespace Genode;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
namespace Genode {
|
namespace Genode {
|
||||||
|
|
||||||
static inline void print(Output &out, Packet_descriptor packet)
|
static void print(Output &out, Packet_descriptor const &packet)
|
||||||
{
|
{
|
||||||
Genode::print(out, "offset=", packet.offset(), ", size=", packet.size());
|
::Genode::print(out, "offset=", packet.offset(), ", size=", packet.size());
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
using namespace Genode;
|
struct Test::Base
|
||||||
|
|
||||||
|
|
||||||
static bool single_packet_roundtrip(Nic::Session *nic,
|
|
||||||
unsigned char content_pattern,
|
|
||||||
size_t packet_size)
|
|
||||||
{
|
{
|
||||||
|
public:
|
||||||
|
|
||||||
|
typedef String<64> Name;
|
||||||
|
|
||||||
|
virtual void handle_nic() = 0;
|
||||||
|
|
||||||
|
private:
|
||||||
|
|
||||||
|
Env &_env;
|
||||||
|
|
||||||
|
Name const _name;
|
||||||
|
|
||||||
|
Signal_context_capability _succeeded_sigh;
|
||||||
|
|
||||||
|
bool _done = false;
|
||||||
|
|
||||||
|
Heap _heap { _env.ram(), _env.rm() };
|
||||||
|
|
||||||
|
Allocator_avl _tx_block_alloc { &_heap };
|
||||||
|
|
||||||
|
enum { BUF_SIZE = Nic::Packet_allocator::DEFAULT_PACKET_SIZE * 128 };
|
||||||
|
|
||||||
|
Nic::Connection _nic { &_tx_block_alloc, BUF_SIZE, BUF_SIZE };
|
||||||
|
|
||||||
|
void _handle_nic() { if (!_done) handle_nic(); }
|
||||||
|
|
||||||
|
Signal_handler<Base> _nic_handler { _env.ep(), *this, &Base::_handle_nic };
|
||||||
|
|
||||||
|
public:
|
||||||
|
|
||||||
|
Nic::Connection &nic() { return _nic; }
|
||||||
|
|
||||||
|
void success()
|
||||||
|
{
|
||||||
|
/*
|
||||||
|
* There may still be packet-stream signals in flight, which we can
|
||||||
|
* ignore once the test succeeded.
|
||||||
|
*/
|
||||||
|
_done = true;
|
||||||
|
|
||||||
|
log("-- ", _name, " test succeeded --");
|
||||||
|
|
||||||
|
Signal_transmitter(_succeeded_sigh).submit();
|
||||||
|
}
|
||||||
|
|
||||||
|
template <typename... ARGS>
|
||||||
|
static void abort(ARGS &&... args)
|
||||||
|
{
|
||||||
|
error(args...);
|
||||||
|
class Error : Exception { };
|
||||||
|
throw Error();
|
||||||
|
}
|
||||||
|
|
||||||
|
Base(Env &env, Name const &name, Signal_context_capability succeeded_sigh)
|
||||||
|
:
|
||||||
|
_env(env), _name(name), _succeeded_sigh(succeeded_sigh)
|
||||||
|
{
|
||||||
|
log("-- starting ", _name, " test --");
|
||||||
|
|
||||||
|
_nic.tx_channel()->sigh_ready_to_submit(_nic_handler);
|
||||||
|
_nic.tx_channel()->sigh_ack_avail (_nic_handler);
|
||||||
|
_nic.rx_channel()->sigh_ready_to_ack (_nic_handler);
|
||||||
|
_nic.rx_channel()->sigh_packet_avail (_nic_handler);
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
|
||||||
|
struct Test::Roundtrip : Base
|
||||||
|
{
|
||||||
|
/*
|
||||||
|
* Each character of the string is used as pattern for one iteration.
|
||||||
|
*/
|
||||||
|
typedef String<16> Patterns;
|
||||||
|
|
||||||
|
Patterns const _patterns;
|
||||||
|
|
||||||
|
unsigned _cnt = 0;
|
||||||
|
|
||||||
|
off_t _expected_packet_offset = ~0L;
|
||||||
|
|
||||||
|
char _pattern() const { return _patterns.string()[_cnt]; }
|
||||||
|
|
||||||
|
bool _received_acknowledgement = false;
|
||||||
|
bool _received_reflected_packet = false;
|
||||||
|
|
||||||
|
enum { PACKET_SIZE = 100 };
|
||||||
|
|
||||||
|
void _produce_packet(Nic::Connection &nic)
|
||||||
|
{
|
||||||
|
log("start iteration ", _cnt, " with pattern '", Char(_pattern()), "'");
|
||||||
|
|
||||||
Packet_descriptor tx_packet;
|
Packet_descriptor tx_packet;
|
||||||
|
|
||||||
log("single_packet_roundtrip(content='", Char(content_pattern), "', "
|
/* allocate tx packet */
|
||||||
"packet_size=", packet_size, ")");
|
|
||||||
|
|
||||||
/* allocate transmit packet */
|
|
||||||
try {
|
try {
|
||||||
tx_packet = nic->tx()->alloc_packet(packet_size);
|
tx_packet = nic.tx()->alloc_packet(PACKET_SIZE); }
|
||||||
} catch (Nic::Session::Tx::Source::Packet_alloc_failed) {
|
catch (Nic::Session::Tx::Source::Packet_alloc_failed) {
|
||||||
error(__func__, ": tx packet alloc failed");
|
abort(__func__, ": tx packet alloc failed"); }
|
||||||
return false;
|
|
||||||
}
|
/*
|
||||||
|
* Remember the packet offset of the first packet. The offsets
|
||||||
|
* of all subsequent packets are expected to be the same.
|
||||||
|
*/
|
||||||
|
if (_expected_packet_offset == ~0L)
|
||||||
|
_expected_packet_offset = tx_packet.offset();
|
||||||
|
|
||||||
log("allocated tx packet ", tx_packet);
|
log("allocated tx packet ", tx_packet);
|
||||||
|
|
||||||
/* fill packet with pattern */
|
/* fill packet with pattern */
|
||||||
char *tx_content = nic->tx()->packet_content(tx_packet);
|
char *tx_content = nic.tx()->packet_content(tx_packet);
|
||||||
for (unsigned i = 0; i < packet_size; i++)
|
for (unsigned i = 0; i < PACKET_SIZE; i++)
|
||||||
tx_content[i] = content_pattern;
|
tx_content[i] = _pattern();
|
||||||
|
|
||||||
nic->tx()->submit_packet(tx_packet);
|
if (!nic.tx()->ready_to_submit())
|
||||||
|
abort(__func__, ": submit queue is unexpectedly full");
|
||||||
|
|
||||||
|
nic.tx()->submit_packet(tx_packet);
|
||||||
|
}
|
||||||
|
|
||||||
|
void _consume_and_compare_packet(Nic::Connection &nic)
|
||||||
|
{
|
||||||
|
/*
|
||||||
|
* The acknowledgement for the sent packet and the reflected packet
|
||||||
|
* may arrive in any order.
|
||||||
|
*/
|
||||||
|
|
||||||
|
if (nic.tx()->ack_avail()) {
|
||||||
|
|
||||||
/* wait for acknowledgement */
|
/* wait for acknowledgement */
|
||||||
Packet_descriptor ack_tx_packet = nic->tx()->get_acked_packet();
|
Packet_descriptor const ack_tx_packet = nic.tx()->get_acked_packet();
|
||||||
|
|
||||||
if (ack_tx_packet.size() != tx_packet.size()
|
if (ack_tx_packet.size() != PACKET_SIZE)
|
||||||
|| ack_tx_packet.offset() != tx_packet.offset()) {
|
abort(__func__, ": unexpected acked packet");
|
||||||
error("unexpected acked packet");
|
|
||||||
return false;
|
if (ack_tx_packet.offset() != _expected_packet_offset)
|
||||||
|
abort(__func__, ": unexpected offset of acknowledged packet");
|
||||||
|
|
||||||
|
/* release sent packet to free the space in the tx communication buffer */
|
||||||
|
nic.tx()->release_packet(ack_tx_packet);
|
||||||
|
|
||||||
|
_received_acknowledgement = true;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if (nic.rx()->packet_avail()) {
|
||||||
|
|
||||||
/*
|
/*
|
||||||
* Because we sent the packet to a loop-back device, we expect
|
* Because we sent the packet to a loop-back device, we expect
|
||||||
* the same packet to be available at the rx channel of the NIC
|
* the same packet to be available at the rx channel of the NIC
|
||||||
* session.
|
* session.
|
||||||
*/
|
*/
|
||||||
Packet_descriptor rx_packet = nic->rx()->get_packet();
|
Packet_descriptor const rx_packet = nic.rx()->get_packet();
|
||||||
log("received rx packet ", rx_packet);
|
log("received rx packet ", rx_packet);
|
||||||
|
|
||||||
if (rx_packet.size() != tx_packet.size()) {
|
if (rx_packet.size() != PACKET_SIZE)
|
||||||
error("sent and echoed packets differ in size");
|
abort("sent and echoed packets differ in size");
|
||||||
return false;
|
|
||||||
}
|
if (rx_packet.offset() != _expected_packet_offset)
|
||||||
|
abort(__func__, ": unexpected offset of received packet");
|
||||||
|
|
||||||
/* compare original and echoed packets */
|
/* compare original and echoed packets */
|
||||||
char *rx_content = nic->rx()->packet_content(rx_packet);
|
char const * const rx_content = nic.rx()->packet_content(rx_packet);
|
||||||
for (unsigned i = 0; i < packet_size; i++)
|
for (unsigned i = 0; i < PACKET_SIZE; i++)
|
||||||
if (rx_content[i] != tx_content[i]) {
|
if (rx_content[i] != _pattern()) {
|
||||||
error("sent and echoed packets have differnt content");
|
log("rx_content[", i, "]: ", Char(rx_content[i]));
|
||||||
return false;
|
log("pattern: ", Char(_pattern()));
|
||||||
|
abort(__func__, ":sent and echoed packets have different content");
|
||||||
}
|
}
|
||||||
|
|
||||||
/* acknowledge received packet */
|
if (!nic.rx()->ack_slots_free())
|
||||||
nic->rx()->acknowledge_packet(rx_packet);
|
abort(__func__, ": acknowledgement queue is unexpectedly full");
|
||||||
|
|
||||||
/* release sent packet to free the space in the tx communication buffer */
|
nic.rx()->acknowledge_packet(rx_packet);
|
||||||
nic->tx()->release_packet(tx_packet);
|
|
||||||
|
|
||||||
return true;
|
_received_reflected_packet = true;
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
void handle_nic() override
|
||||||
|
{
|
||||||
|
_consume_and_compare_packet(nic());
|
||||||
|
|
||||||
|
if (!_received_acknowledgement || !_received_reflected_packet)
|
||||||
|
return;
|
||||||
|
|
||||||
|
/* start next iteration */
|
||||||
|
_cnt++;
|
||||||
|
|
||||||
|
/* check if we reached the end of the pattern string */
|
||||||
|
if (_pattern() == 0) {
|
||||||
|
success();
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
_received_reflected_packet = false;
|
||||||
|
_received_acknowledgement = false;
|
||||||
|
_produce_packet(nic());
|
||||||
|
}
|
||||||
|
|
||||||
|
Roundtrip(Env &env, Signal_context_capability success_sigh, Patterns patterns)
|
||||||
|
:
|
||||||
|
Base(env, "roundtrip", success_sigh), _patterns(patterns)
|
||||||
|
{
|
||||||
|
_produce_packet(nic());
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
|
||||||
static bool batch_packets(Nic::Session *nic, unsigned num_packets)
|
struct Test::Batch : Base
|
||||||
{
|
{
|
||||||
unsigned tx_cnt = 0, acked_cnt = 0, rx_cnt = 0, batch_cnt = 0;
|
unsigned const _num_packets;
|
||||||
|
|
||||||
Genode::Signal_context tx_ready_to_submit, tx_ack_avail,
|
unsigned _tx_cnt = 0, _acked_cnt = 0, _rx_cnt = 0, _batch_cnt = 0;
|
||||||
rx_ready_to_ack, rx_packet_avail;
|
|
||||||
Genode::Signal_receiver signal_receiver;
|
|
||||||
|
|
||||||
nic->tx_channel()->sigh_ready_to_submit (signal_receiver.manage(&tx_ready_to_submit));
|
|
||||||
nic->tx_channel()->sigh_ack_avail (signal_receiver.manage(&tx_ack_avail));
|
|
||||||
nic->rx_channel()->sigh_ready_to_ack (signal_receiver.manage(&rx_ready_to_ack));
|
|
||||||
nic->rx_channel()->sigh_packet_avail (signal_receiver.manage(&rx_packet_avail));
|
|
||||||
|
|
||||||
enum { PACKET_SIZE = 100 };
|
enum { PACKET_SIZE = 100 };
|
||||||
|
|
||||||
while (acked_cnt != num_packets
|
static unsigned _send_packets(Nic::Connection &nic, unsigned limit)
|
||||||
|| tx_cnt != num_packets
|
{
|
||||||
|| rx_cnt != num_packets) {
|
unsigned cnt = 0;
|
||||||
|
while (nic.tx()->ready_to_submit() && cnt < limit) {
|
||||||
if (tx_cnt > rx_cnt || tx_cnt > acked_cnt)
|
|
||||||
signal_receiver.wait_for_signal();
|
|
||||||
|
|
||||||
/* produce as many packets as possible as one batch */
|
|
||||||
unsigned max_outstanding_requests = Nic::Session::QUEUE_SIZE - 1;
|
|
||||||
while (nic->tx()->ready_to_submit()
|
|
||||||
&& tx_cnt < num_packets
|
|
||||||
&& tx_cnt - rx_cnt < max_outstanding_requests) {
|
|
||||||
|
|
||||||
try {
|
try {
|
||||||
Packet_descriptor tx_packet = nic->tx()->alloc_packet(PACKET_SIZE);
|
Packet_descriptor tx_packet = nic.tx()->alloc_packet(PACKET_SIZE);
|
||||||
nic->tx()->submit_packet(tx_packet);
|
nic.tx()->submit_packet(tx_packet);
|
||||||
tx_cnt++;
|
cnt++;
|
||||||
} catch (Nic::Session::Tx::Source::Packet_alloc_failed) {
|
|
||||||
break;
|
|
||||||
}
|
}
|
||||||
|
catch (Nic::Session::Tx::Source::Packet_alloc_failed) { break; }
|
||||||
|
}
|
||||||
|
return cnt;
|
||||||
}
|
}
|
||||||
|
|
||||||
unsigned batch_rx_cnt = 0, batch_acked_cnt = 0;
|
/*
|
||||||
|
* \return number of received acknowledgements
|
||||||
/* check for acknowledgements */
|
*/
|
||||||
while (nic->tx()->ack_avail()) {
|
static unsigned _collect_acknowledgements(Nic::Connection &nic)
|
||||||
Packet_descriptor acked_packet = nic->tx()->get_acked_packet();
|
{
|
||||||
nic->tx()->release_packet(acked_packet);
|
unsigned cnt = 0;
|
||||||
acked_cnt++;
|
while (nic.tx()->ack_avail()) {
|
||||||
batch_acked_cnt++;
|
Packet_descriptor acked_packet = nic.tx()->get_acked_packet();
|
||||||
|
nic.tx()->release_packet(acked_packet);
|
||||||
|
cnt++;
|
||||||
|
}
|
||||||
|
return cnt;
|
||||||
}
|
}
|
||||||
|
|
||||||
/* check for available rx packets */
|
static unsigned _receive_all_incoming_packets(Nic::Connection &nic)
|
||||||
while (nic->rx()->packet_avail() && nic->rx()->ready_to_ack()) {
|
{
|
||||||
Packet_descriptor rx_packet = nic->rx()->get_packet();
|
unsigned cnt = 0;
|
||||||
|
while (nic.rx()->packet_avail() && nic.rx()->ready_to_ack()) {
|
||||||
if (!nic->rx()->ready_to_ack())
|
Packet_descriptor rx_packet = nic.rx()->get_packet();
|
||||||
warning("not ready for ack, going to blocK");
|
nic.rx()->acknowledge_packet(rx_packet);
|
||||||
|
cnt++;
|
||||||
nic->rx()->acknowledge_packet(rx_packet);
|
}
|
||||||
rx_cnt++;
|
return cnt;
|
||||||
batch_rx_cnt++;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
log("acked ", batch_acked_cnt, " packets, "
|
void _check_for_success()
|
||||||
"received ", batch_rx_cnt, " packets "
|
{
|
||||||
"-> tx: ", tx_cnt, ", acked: ", acked_cnt, ", rx: ", rx_cnt);
|
unsigned const n = _num_packets;
|
||||||
|
if (_acked_cnt == n && _tx_cnt == n && _rx_cnt == n)
|
||||||
batch_cnt++;
|
success();
|
||||||
}
|
}
|
||||||
|
|
||||||
log("test used ", batch_cnt, " batches");
|
void handle_nic() override
|
||||||
return true;
|
{
|
||||||
}
|
unsigned const max_outstanding_requests = Nic::Session::QUEUE_SIZE - 1;
|
||||||
|
unsigned const outstanding_requests = _tx_cnt - _rx_cnt;
|
||||||
|
|
||||||
|
unsigned const tx_limit = min(_num_packets - _tx_cnt,
|
||||||
|
max_outstanding_requests - outstanding_requests);
|
||||||
|
|
||||||
|
unsigned const num_tx = _send_packets(nic(), tx_limit);
|
||||||
|
unsigned const num_acks = _collect_acknowledgements(nic());
|
||||||
|
unsigned const num_rx = _receive_all_incoming_packets(nic());
|
||||||
|
|
||||||
|
_tx_cnt += num_tx;
|
||||||
|
_rx_cnt += num_rx;
|
||||||
|
_acked_cnt += num_acks;
|
||||||
|
|
||||||
|
log("acked ", num_acks, " packets, "
|
||||||
|
"received ", num_rx, " packets "
|
||||||
|
"-> tx: ", _tx_cnt, ", acked: ", _acked_cnt, ", rx: ", _rx_cnt);
|
||||||
|
|
||||||
|
_check_for_success();
|
||||||
|
}
|
||||||
|
|
||||||
|
Batch(Env &env, Signal_context_capability success_sigh, unsigned num_packets)
|
||||||
|
:
|
||||||
|
Base(env, "batch", success_sigh), _num_packets(num_packets)
|
||||||
|
{
|
||||||
|
handle_nic();
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
|
||||||
int main(int, char **)
|
struct Test::Main
|
||||||
{
|
{
|
||||||
|
Env &_env;
|
||||||
|
|
||||||
|
Constructible<Roundtrip> _roundtrip;
|
||||||
|
Constructible<Batch> _batch;
|
||||||
|
|
||||||
|
Signal_handler<Main> _test_completed_handler {
|
||||||
|
_env.ep(), *this, &Main::_handle_test_completed };
|
||||||
|
|
||||||
|
void _handle_test_completed()
|
||||||
|
{
|
||||||
|
if (_roundtrip.constructed()) {
|
||||||
|
_roundtrip.destruct();
|
||||||
|
|
||||||
|
enum { NUM_PACKETS = 1000 };
|
||||||
|
_batch.construct(_env, _test_completed_handler, NUM_PACKETS);
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (_batch.constructed()) {
|
||||||
|
_batch.destruct();
|
||||||
|
log("--- finished NIC loop-back test ---");
|
||||||
|
_env.parent().exit(0);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
Main(Env &env) : _env(env)
|
||||||
|
{
|
||||||
log("--- NIC loop-back test ---");
|
log("--- NIC loop-back test ---");
|
||||||
|
|
||||||
enum { BUF_SIZE = Nic::Packet_allocator::DEFAULT_PACKET_SIZE * 128 };
|
_roundtrip.construct(_env, _test_completed_handler, "abcdefghijklmn");
|
||||||
|
|
||||||
bool config_test_roundtrip = true;
|
|
||||||
bool config_test_batch = true;
|
|
||||||
|
|
||||||
if (config_test_roundtrip) {
|
|
||||||
log("-- test roundtrip two times (packet offsets should be the same) --");
|
|
||||||
Allocator_avl tx_block_alloc(env()->heap());
|
|
||||||
Nic::Connection nic(&tx_block_alloc, BUF_SIZE, BUF_SIZE);
|
|
||||||
single_packet_roundtrip(&nic, 'a', 100);
|
|
||||||
single_packet_roundtrip(&nic, 'b', 100);
|
|
||||||
}
|
}
|
||||||
|
};
|
||||||
|
|
||||||
if (config_test_batch) {
|
|
||||||
log("-- test submitting and receiving batches of packets --");
|
|
||||||
Allocator_avl tx_block_alloc(env()->heap());
|
|
||||||
Nic::Connection nic(&tx_block_alloc, BUF_SIZE, BUF_SIZE);
|
|
||||||
enum { NUM_PACKETS = 1000 };
|
|
||||||
batch_packets(&nic, NUM_PACKETS);
|
|
||||||
}
|
|
||||||
|
|
||||||
log("--- finished NIC loop-back test ---");
|
void Component::construct(Genode::Env &env) { static Test::Main main(env); }
|
||||||
return 0;
|
|
||||||
}
|
|
||||||
|
Reference in New Issue
Block a user