os: use the server API in server/nic_loopback

This patch also introduces a run script for testing the component.
This commit is contained in:
Norman Feske 2015-06-17 16:32:07 +02:00 committed by Christian Helmuth
parent 5d678dba9e
commit 712875ade4
5 changed files with 290 additions and 187 deletions

View File

@ -144,7 +144,7 @@ class Genode::Packet_descriptor
Genode::off_t offset() const { return _offset; }
Genode::size_t size() const { return _size; }
bool valid() { return _size != 0; }
bool valid() const { return _size != 0; }
};

View File

@ -0,0 +1,63 @@
#
# Build
#
set build_components {
core init
test/nic_loopback
server/nic_loopback
}
build $build_components
create_boot_directory
#
# Generate config
#
append config {
<config>
<parent-provides>
<service name="ROM"/>
<service name="RAM"/>
<service name="IRQ"/>
<service name="IO_MEM"/>
<service name="IO_PORT"/>
<service name="CAP"/>
<service name="PD"/>
<service name="RM"/>
<service name="CPU"/>
<service name="LOG"/>
<service name="SIGNAL"/>
</parent-provides>
<default-route>
<any-service> <parent/> <any-child/> </any-service>
</default-route>
<start name="nic_loopback">
<resource name="RAM" quantum="1M"/>
<provides><service name="Nic"/></provides>
</start>
<start name="test-nic_loopback">
<resource name="RAM" quantum="2M"/>
</start>
</config>}
install_config $config
#
# Boot modules
#
# generic modules
set boot_modules {
core init
nic_loopback
test-nic_loopback
}
build_boot_image $boot_modules
append qemu_args " -nographic -serial mon:stdio -m 256 "
run_genode_until {child .* exited with exit value 0.*} 60

View File

@ -15,99 +15,55 @@
#include <base/env.h>
#include <base/sleep.h>
#include <cap_session/connection.h>
#include <os/attached_ram_dataspace.h>
#include <os/server.h>
#include <os/signal_rpc_dispatcher.h>
#include <root/component.h>
#include <util/arg_string.h>
#include <util/misc_math.h>
#include <nic_session/rpc_object.h>
#include <nic_session/client.h>
#include <timer_session/connection.h>
namespace Nic {
class Communication_buffer : Genode::Ram_dataspace_capability
{
public:
class Communication_buffers;
class Session_component;
class Root;
}
Communication_buffer(Genode::size_t size)
: Genode::Ram_dataspace_capability(Genode::env()->ram_session()->alloc(size))
namespace Server { struct Main; }
class Nic::Communication_buffers
{
protected:
Genode::Allocator_avl _rx_packet_alloc;
Genode::Attached_ram_dataspace _tx_ds, _rx_ds;
Communication_buffers(Genode::Allocator &rx_block_md_alloc,
Genode::Ram_session &ram_session,
Genode::size_t tx_size, Genode::size_t rx_size)
:
_rx_packet_alloc(&rx_block_md_alloc),
_tx_ds(&ram_session, tx_size),
_rx_ds(&ram_session, rx_size)
{ }
~Communication_buffer() { Genode::env()->ram_session()->free(*this); }
Genode::Dataspace_capability dataspace() { return *this; }
};
};
class Tx_rx_communication_buffers
{
class Nic::Session_component : Communication_buffers, public Session_rpc_object
{
private:
Communication_buffer _tx_buf, _rx_buf;
Server::Entrypoint &_ep;
public:
void _handle_packet_stream(unsigned);
Tx_rx_communication_buffers(Genode::size_t tx_size,
Genode::size_t rx_size)
: _tx_buf(tx_size), _rx_buf(rx_size) { }
Genode::Dataspace_capability tx_ds() { return _tx_buf.dataspace(); }
Genode::Dataspace_capability rx_ds() { return _rx_buf.dataspace(); }
};
enum { STACK_SIZE = 8*1024 };
class Session_component : private Genode::Allocator_avl,
private Tx_rx_communication_buffers,
private Genode::Thread<STACK_SIZE>,
public Session_rpc_object
{
private:
/**
* Packet-handling thread
*/
void entry()
{
using namespace Genode;
for (;;) {
Packet_descriptor packet_from_client, packet_to_client;
/* get packet, block until a packet is available */
packet_from_client = _tx.sink()->get_packet();
if (!packet_from_client.valid()) {
PWRN("received invalid packet");
continue;
}
size_t packet_size = packet_from_client.size();
try {
packet_to_client = _rx.source()->alloc_packet(packet_size);
Genode::memcpy(_rx.source()->packet_content(packet_to_client),
_tx.sink()->packet_content(packet_from_client),
packet_size);
_rx.source()->submit_packet(packet_to_client);
} catch (Session::Rx::Source::Packet_alloc_failed) {
PWRN("transmit packet allocation failed, drop packet");
}
if (!_tx.sink()->ready_to_ack())
printf("need to wait until ready-for-ack\n");
_tx.sink()->acknowledge_packet(packet_from_client);
/* flush acknowledgements for the echoes packets */
while (_rx.source()->ack_avail())
_rx.source()->release_packet(_rx.source()->get_acked_packet());
}
}
Genode::Signal_rpc_member<Session_component> _packet_stream_dispatcher {
_ep, *this, &Session_component::_handle_packet_stream };
public:
@ -118,23 +74,28 @@ namespace Nic {
* \param rx_buf_size buffer size for rx channel
* \param rx_block_md_alloc backing store of the meta data of the
* rx block allocator
* \param ram_session RAM session to allocate tx and rx buffers
* \param ep entry point used for packet stream
* channels
*/
Session_component(Genode::size_t tx_buf_size,
Genode::size_t rx_buf_size,
Genode::Allocator *rx_block_md_alloc,
Genode::Rpc_entrypoint &ep)
Session_component(Genode::size_t const tx_buf_size,
Genode::size_t const rx_buf_size,
Genode::Allocator &rx_block_md_alloc,
Genode::Ram_session &ram_session,
Server::Entrypoint &ep)
:
Genode::Allocator_avl(rx_block_md_alloc),
Tx_rx_communication_buffers(tx_buf_size, rx_buf_size),
Thread("nic_packet_handler"),
Session_rpc_object(Tx_rx_communication_buffers::tx_ds(),
Tx_rx_communication_buffers::rx_ds(),
static_cast<Genode::Range_allocator *>(this), ep)
Communication_buffers(rx_block_md_alloc, ram_session,
tx_buf_size, rx_buf_size),
Session_rpc_object(this->_tx_ds.cap(),
this->_rx_ds.cap(),
&this->_rx_packet_alloc, ep.rpc_ep()),
_ep(ep)
{
/* start packet-handling thread */
start();
/* install data-flow signal handlers for both packet streams */
_tx.sigh_ready_to_ack(_packet_stream_dispatcher);
_tx.sigh_packet_avail(_packet_stream_dispatcher);
_rx.sigh_ready_to_submit(_packet_stream_dispatcher);
_rx.sigh_ack_avail(_packet_stream_dispatcher);
}
Mac_address mac_address()
@ -150,13 +111,82 @@ namespace Nic {
}
void link_state_sigh(Genode::Signal_context_capability sigh) { }
};
};
class Root : public Genode::Root_component<Session_component>
{
void Nic::Session_component::_handle_packet_stream(unsigned)
{
using namespace Genode;
/* loop unless we cannot make any progress */
for (;;) {
/* flush acknowledgements for the echoes packets */
while (_rx.source()->ack_avail())
_rx.source()->release_packet(_rx.source()->get_acked_packet());
/*
* If the client cannot accept new acknowledgements for a sent packets,
* we won't consume the sent packet.
*/
if (!_tx.sink()->ready_to_ack())
return;
/*
* Nothing to be done if the client has not sent any packets.
*/
if (!_tx.sink()->packet_avail())
return;
/*
* Here we know that the client has submitted a packet to us and is also
* able it receive the corresponding acknowledgement.
*/
/*
* The client fails to pick up the packets from the rx channel. So we
* won't try to submit new packets.
*/
if (!_rx.source()->ready_to_submit())
return;
/*
* We are safe to process one packet without blocking.
*/
/* obtain packet */
Packet_descriptor const packet_from_client = _tx.sink()->get_packet();
if (!packet_from_client.valid()) {
PWRN("received invalid packet");
continue;
}
try {
size_t const packet_size = packet_from_client.size();
Packet_descriptor const packet_to_client =
_rx.source()->alloc_packet(packet_size);
Genode::memcpy(_rx.source()->packet_content(packet_to_client),
_tx.sink()->packet_content(packet_from_client),
packet_size);
_rx.source()->submit_packet(packet_to_client);
} catch (Session::Rx::Source::Packet_alloc_failed) {
PWRN("transmit packet allocation failed, drop packet");
}
_tx.sink()->acknowledge_packet(packet_from_client);
}
}
class Nic::Root : public Genode::Root_component<Session_component>
{
private:
Genode::Rpc_entrypoint &_channel_ep;
Server::Entrypoint &_ep;
protected:
@ -187,33 +217,42 @@ namespace Nic {
}
return new (md_alloc()) Session_component(tx_buf_size, rx_buf_size,
env()->heap(), _channel_ep);
*env()->heap(),
*env()->ram_session(),
_ep);
}
public:
Root(Genode::Rpc_entrypoint *session_ep,
Genode::Allocator *md_alloc)
Root(Server::Entrypoint &ep, Genode::Allocator &md_alloc)
:
Genode::Root_component<Session_component>(session_ep, md_alloc),
_channel_ep(*session_ep)
Genode::Root_component<Session_component>(&ep.rpc_ep(), &md_alloc),
_ep(ep)
{ }
};
}
};
using namespace Genode;
int main(int, char **)
struct Server::Main
{
enum { STACK_SIZE = 2*4096 };
static Cap_connection cap;
static Rpc_entrypoint ep(&cap, STACK_SIZE, "nicloop_ep");
Entrypoint &ep;
static Nic::Root nic_root(&ep, env()->heap());
env()->parent()->announce(ep.manage(&nic_root));
Nic::Root nic_root { ep, *Genode::env()->heap() };
sleep_forever();
return 0;
Main(Entrypoint &ep) : ep(ep)
{
Genode::env()->parent()->announce(ep.manage(nic_root));
}
};
namespace Server {
char const *name() { return "nicloop_ep"; }
size_t stack_size() { return 2*1024*sizeof(long); }
void construct(Entrypoint &ep)
{
static Main main(ep);
}
}

View File

@ -1,3 +1,3 @@
TARGET = nic_loopback
SRC_CC = main.cc
LIBS = base
LIBS = base server

View File

@ -52,3 +52,4 @@ vmm
bomb
cpu_quota
stdcxx
nic_loopback