From bf90fd5f66493d3f20cd1a45926a7c8217c6709c Mon Sep 17 00:00:00 2001 From: Stefan Kalkowski Date: Thu, 28 Nov 2013 15:13:20 +0100 Subject: [PATCH] http_block: use generic block component/driver Ref #966 --- gems/src/server/http_block/README | 12 +- gems/src/server/http_block/http.cc | 13 +- gems/src/server/http_block/http.h | 10 +- gems/src/server/http_block/main.cc | 326 +++++++---------------------- 4 files changed, 91 insertions(+), 270 deletions(-) diff --git a/gems/src/server/http_block/README b/gems/src/server/http_block/README index 4e332d370..956a99414 100644 --- a/gems/src/server/http_block/README +++ b/gems/src/server/http_block/README @@ -11,16 +11,6 @@ Config file snippet: ! ! ! -! -! -! -! http://kc86.genode.labs:80/file.iso -! -! -! 2048 -! -! +! ! diff --git a/gems/src/server/http_block/http.cc b/gems/src/server/http_block/http.cc index a8e12eb18..51e183f71 100644 --- a/gems/src/server/http_block/http.cc +++ b/gems/src/server/http_block/http.cc @@ -191,12 +191,12 @@ void Http::do_read(void * buf, size_t size) } -Http::Http(char *uri, size_t length) : _port((char *)"80") +Http::Http(char *uri) : _port((char *)"80") { env()->heap()->alloc(HTTP_BUF, &_http_buf); /* parse URI */ - parse_uri(uri, length); + parse_uri(uri); /* search for host */ resolve_uri(); @@ -218,10 +218,11 @@ Http::~Http() } -void Http::parse_uri(char *uri, size_t length) +void Http::parse_uri(char *uri) { /* strip possible http prefix */ const char *http = "http://"; + size_t length = Genode::strlen(uri); size_t http_len = Genode::strlen(http); if (!strcmp(http, uri, http_len)) { uri += http_len; @@ -252,10 +253,10 @@ void Http::parse_uri(char *uri, size_t length) } -void Http::cmd_get(size_t file_offset, size_t size, off_t offset) +void Http::cmd_get(size_t file_offset, size_t size, addr_t buffer) { if (verbose) - PDBG("Read: offs %zu size: %zu I/O offs: %lx", file_offset, size, offset); + PDBG("Read: offs %zu size: %zu I/O buffer: %lx", file_offset, size, buffer); while (true) { @@ -288,7 +289,7 @@ void Http::cmd_get(size_t file_offset, size_t size, off_t offset) throw Http::Server_error(); } - do_read((void *)(_base_addr + offset), size); + do_read((void *)(buffer), size); return; } } diff --git a/gems/src/server/http_block/http.h b/gems/src/server/http_block/http.h index 621f98225..1fc13da11 100644 --- a/gems/src/server/http_block/http.h +++ b/gems/src/server/http_block/http.h @@ -54,7 +54,7 @@ class Http /* * Set URI of remote file */ - void parse_uri(char *uri, size_t length); + void parse_uri(char *uri); /* * Resolve host @@ -79,9 +79,9 @@ class Http public: /* - * Constructor (default block size is 512 Bytes, default host port is 80 + * Constructor (default host port is 80 */ - Http(char *uri, size_t length); + Http(char *uri); /* * Destructor @@ -107,9 +107,9 @@ class Http * * \param file_offset Read from offset of remote file * \param size Number of byts to transfer - * \param offset Offset in I/O dataspace + * \param buffer address in I/O dataspace */ - void cmd_get(size_t file_offset, size_t size, off_t offset); + void cmd_get(size_t file_offset, size_t size, addr_t buffer); /* Exceptions */ class Exception : public ::Genode::Exception { }; diff --git a/gems/src/server/http_block/main.cc b/gems/src/server/http_block/main.cc index ccffbb57a..6ea76f0ac 100644 --- a/gems/src/server/http_block/main.cc +++ b/gems/src/server/http_block/main.cc @@ -1,6 +1,7 @@ /* * \brief Block interface for HTTP block driver * \author Sebastian Sumpf + * \author Stefan Kalkowski * \date 2010-08-24 */ @@ -12,12 +13,8 @@ */ /* Genode includes */ -#include -#include -#include -#include #include -#include +#include #include /* local includes */ @@ -25,254 +22,82 @@ using namespace Genode; -namespace Block { - - class Http_interface - { - private: - - size_t _block_size; - Http *_http; - - public: - - Http_interface() : _block_size(512), _http(0) {} - - static Http_interface* obj() - { - static Http_interface _obj; - return &_obj; - } - - void block_size(size_t block_size) - { - _block_size = block_size; - } - - size_t block_size() { return _block_size; } - - void base_addr(addr_t base_addr) - { - _http->base_addr(base_addr); - } - - void read(size_t block_nr, size_t block_count, off_t offset) - { - _http->cmd_get(block_nr * _block_size, block_count * _block_size, offset); - } - - size_t block_count() - { - return _http->file_size() / _block_size; - } - - void uri(char *uri, size_t length) - { - _http = new(env()->heap()) Http(uri, length); - } - - Http * http_blk() { return _http; } - }; - - class Session_component : public Session_rpc_object - { - private: - - class Tx_thread : public Genode::Thread<8192> - { - private: - - Session_component *_session; - - public: - - Tx_thread(Session_component *session) - : Genode::Thread<8192>("worker"), _session(session) { } - - void entry() - { - using namespace Genode; - - Session_component::Tx::Sink *tx_sink = _session->tx_sink(); - Block::Packet_descriptor packet; - - _session->tx_ready(); - - /* handle requests */ - while (true) { - - /* blocking-get packet from client */ - packet = tx_sink->get_packet(); - if (!packet.valid()) { - PWRN("received invalid packet"); - continue; - } - - packet.succeeded(false); - - switch (packet.operation()) { - - case Block::Packet_descriptor::READ: - - try { - Http_interface::obj()->read(packet.block_number(), - packet.block_count(), - packet.offset()); - packet.succeeded(true); - } - catch (Http::Socket_error) { PERR("socket error"); } - catch (Http::Server_error) { PERR("server error"); } - - break; - - case Block::Packet_descriptor::WRITE: - break; - - default: - PWRN("received invalid packet"); - continue; - } - - /* acknowledge packet to the client */ - if (!tx_sink->ready_to_ack()) - PDBG("need to wait until ready-for-ack"); - tx_sink->acknowledge_packet(packet); - } - } - }; - - private: - - Genode::Dataspace_capability _tx_ds; /* buffer for tx channel */ - Genode::Semaphore _startup_sema; /* thread startup sync */ - Tx_thread _tx_thread; - - public: - - /** - * Constructor - * - * \param tx_ds dataspace used for tx channel - */ - Session_component(Genode::Dataspace_capability tx_ds, - Genode::Rpc_entrypoint &ep) - : Session_rpc_object(tx_ds, ep), _tx_ds(tx_ds), - _startup_sema(0), _tx_thread(this) - { - /* - * Map packet stream - */ - addr_t base = env()->rm_session()->attach(tx_ds); - - Http_interface::obj()->base_addr(base); - - _tx_thread.start(); - _startup_sema.down(); - } - - void info(Genode::size_t *blk_count, Genode::size_t *blk_size, - Operations *ops) - { - *blk_count = Http_interface::obj()->block_count(); - *blk_size = Http_interface::obj()->block_size(); - ops->set_operation(Packet_descriptor::READ); - } - - void sync() {} - - /** - * Signal indicating that transmit thread is ready - */ - void tx_ready() { _startup_sema.up(); } - }; - - /* - * Allow one client only - */ - - /* - * Shortcut for single-client root component - */ - typedef Genode::Root_component Root_component; - - /** - * Root component, handling new session requests - */ - class Root : public Root_component - { - protected: - - /** - * Always returns the singleton block-session component - */ - Session_component *_create_session(const char *args) - { - using namespace Genode; - - Genode::size_t ram_quota = - Arg_string::find_arg(args, "ram_quota" ).ulong_value(0); - Genode::size_t tx_buf_size = - Arg_string::find_arg(args, "tx_buf_size").ulong_value(0); - - /* delete ram quota by the memory needed for the session */ - Genode::size_t session_size = max((Genode::size_t)4096, - sizeof(Session_component) - + sizeof(Allocator_avl)); - if (ram_quota < session_size) - throw Root::Quota_exceeded(); - - /* - * Check if donated ram quota suffices for both - * communication buffers. Also check both sizes separately - * to handle a possible overflow of the sum of both sizes. - */ - if (tx_buf_size > ram_quota - session_size) { - PERR("insufficient 'ram_quota', got %zd, need %zd", - ram_quota, tx_buf_size + session_size); - throw Root::Quota_exceeded(); - } - - return new (md_alloc()) - Session_component(env()->ram_session()->alloc(tx_buf_size), *ep()); - } - - public: - - Root(Genode::Rpc_entrypoint *session_ep, - Genode::Allocator *md_alloc) - : Root_component(session_ep, md_alloc) { } - }; - - -} - - -static void process_config() +class Driver : public Block::Driver { - using namespace Genode; + private: - Xml_node config_node = config()->xml_node(); + size_t _block_size; + Http _http; - bool uri = false; - for (unsigned i = 0; i < config_node.num_sub_nodes(); ++i) { + public: - Xml_node file_node = config_node.sub_node(i); + Driver(size_t block_size, char *uri) + : _block_size(block_size), _http(uri) {} - if (file_node.has_type("uri")) { - Block::Http_interface::obj()->uri(file_node.content_addr(), file_node.content_size()); - uri = true; + + /******************************* + ** Block::Driver interface ** + *******************************/ + + Genode::size_t block_size() { return _block_size; } + Genode::size_t block_count() { + return _http.file_size() / _block_size; } + + Block::Session::Operations ops() + { + Block::Session::Operations o; + o.set_operation(Block::Packet_descriptor::READ); + return o; } - if (file_node.has_type("block-size")) { - size_t blk_size; - file_node.value(&blk_size); - Block::Http_interface::obj()->block_size(blk_size); - } - } + void read(Genode::size_t block_nr, + Genode::size_t block_count, + char *buffer) { + _http.cmd_get(block_nr * _block_size, block_count * _block_size, + (addr_t)buffer); } - if (!uri) - throw Http::Uri_error(); -} + void write(Genode::size_t, Genode::size_t, char const*) { + throw Io_error(); } + void read_dma(Genode::size_t, Genode::size_t, Genode::addr_t) { + throw Io_error(); } + void write_dma(Genode::size_t, Genode::size_t, Genode::addr_t) { + throw Io_error(); } + + bool dma_enabled() { return false; } + + Genode::Ram_dataspace_capability alloc_dma_buffer(Genode::size_t size) { + return Genode::env()->ram_session()->alloc(size, false); } + + void sync() {} + }; + + +class Factory : public Block::Driver_factory +{ + private: + + char _uri[64]; + size_t _blk_sz; + + public: + + Factory() : _blk_sz(512) + { + try { + config()->xml_node().attribute("uri").value(_uri, sizeof(_uri)); + config()->xml_node().attribute("block_size").value(&_blk_sz); + } + catch (...) { } + + PINF("Using file=%s as device with block size %zx.", _uri, _blk_sz); + } + + Block::Driver *create() { + return new (env()->heap()) Driver(_blk_sz, _uri); } + + void destroy(Block::Driver *driver) { + Genode::destroy(env()->heap(), driver); } +}; int main() @@ -281,11 +106,16 @@ int main() static Cap_connection cap; static Rpc_entrypoint ep(&cap, STACK_SIZE, "http_block_ep"); - process_config(); + static Signal_receiver receiver; + static Factory driver_factory; + static Block::Root block_root(&ep, env()->heap(), driver_factory, receiver); - static Block::Root block_root(&ep, env()->heap()); env()->parent()->announce(ep.manage(&block_root)); - sleep_forever(); + + while (true) { + Signal s = receiver.wait_for_signal(); + static_cast(s.context())->dispatch(s.num()); + } return 0; }