Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
97 changes: 71 additions & 26 deletions api/fs/disk.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,37 @@
// See the License for the specific language governing permissions and
// limitations under the License.

/**
* /// Create basic FAT disk ///
*
* // create disk from a given disk-device
* auto disk = std::make_shared<Disk> (device);
* // mount filesystem on auto-detected volume
* disk->mount(
* [disk] (fs::error_t err) {
* if (err) {
* printf("Bad!\n");
* return;
* }
* // reference to filesystem
* auto& fs = disk->fs();
* // synchronous stat:
* auto dirent = fs.stat("/file");
* });
*
* /// Construct custom filesystem ///
*
* // constructing on MBR means mount on sector 0
* disk->mount<MyFileSystem>(filesystem_args..., Disk::MBR,
* [disk] {
* printf("Disk mounted!\n");
* auto& fs = disk->fs();
*
* auto dirent = fs.stat("/file");
* });
*
**/

#pragma once
#ifndef FS_DISK_HPP
#define FS_DISK_HPP
Expand All @@ -31,30 +62,25 @@ namespace fs {

class Disk {
public:
struct Partition; //< Representation of a disk partition

/** Callbacks */
struct Partition;
using on_parts_func = std::function<void(error_t, std::vector<Partition>&)>;
using on_mount_func = std::function<void(error_t)>;
using lba_t = uint32_t;

/** Constructor */
explicit Disk(hw::IDiskDevice&);

enum partition_t {
MBR = 0, //< Master Boot Record (0)
/** extended partitions (1-4) */
VBR1,
VBR1, //> extended partitions (1-4)
VBR2,
VBR3,
VBR4,

INVALID
}; //< enum partition_t

};
struct Partition {
explicit Partition(const uint8_t fl, const uint8_t Id,
const uint32_t LBA, const uint32_t sz) noexcept :
flags {fl},
const uint32_t LBA, const uint32_t sz) noexcept
: flags {fl},
id {Id},
lba_begin {LBA},
sectors {sz}
Expand All @@ -78,33 +104,52 @@ namespace fs {

}; //< struct Partition

/** Return a reference to the specified filesystem <FS> */
FileSystem& fs() noexcept
{ return *filesys; }

//************** disk functions **************//

// construct a disk with a given disk-device
explicit Disk(hw::IDiskDevice&);

// returns the device the disk is using
hw::IDiskDevice& dev() noexcept
{ return device; }

// Returns true if the disk has no sectors
bool empty() const noexcept
{ return device.size() == 0; }

// Mounts the first partition detected (MBR -> VBR1-4 -> ext)
// NOTE: Always detects and instantiates a FAT filesystem
void mount(on_mount_func func);

// Mount partition @part as the filesystem FS

// Mounts specified partition
// NOTE: Always detects and instantiates a FAT filesystem
void mount(partition_t part, on_mount_func func);

// mount custom filesystem on MBR or VBRn
template <class T, class... Args>
void mount(Args&&... args, partition_t part, on_mount_func func)
{
// construct custom filesystem
filesys.reset(new T(args...));
internal_mount(part, func);
}

/**
* Returns a vector of the partitions on a disk
*
* The disk does not need to be mounted beforehand
*/
// Creates a vector of the partitions on disk (see: on_parts_func)
// Note: The disk does not need to be mounted beforehand
void partitions(on_parts_func func);


// returns true if a filesystem is mounted
bool fs_mounted() const noexcept
{ return (bool) filesys; }

// Returns a reference to a mounted filesystem
// If no filesystem is mounted, the results are undefined
FileSystem& fs() noexcept
{ return *filesys; }

private:
void internal_mount(partition_t part, on_mount_func func);

hw::IDiskDevice& device;
std::unique_ptr<FileSystem> filesys;
}; //< class Disk
Expand Down
5 changes: 4 additions & 1 deletion api/hw/pit.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -97,7 +97,10 @@ namespace hw {
/** Stop a timer.
@param it: A valid iterator to timer */
void stop_timer(Timer_iterator it);


static void stop(Timer_iterator it)
{ instance().stop_timer(it); }

inline size_t active_timers() { return timers_.size(); }

/** No copy or move. The OS owns one instance forever. */
Expand Down
34 changes: 21 additions & 13 deletions api/net/dhcp/dh4client.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,8 @@
#ifndef NET_DHCP_DH4CLIENT_HPP
#define NET_DHCP_DH4CLIENT_HPP

#include <hw/pit.hpp>
#include "../packet.hpp"
#include <info>

namespace net
{
Expand All @@ -33,28 +33,36 @@ namespace net
{
public:
using Stack = Inet<LinkLayer, IP4>;
using On_config = delegate<void(Stack&)>;
using config_func = delegate<void(bool)>;

DHClient() = delete;
DHClient(DHClient&) = delete;
DHClient(Stack& inet)
: stack(inet) {}
DHClient(Stack& inet);

Stack& stack;
void negotiate(); // --> offer
inline void on_config(On_config handler){
config_handler = handler;
}
// negotiate with local DHCP server
void negotiate(double timeout_secs);

// handler for result of DHCP negotation
// timeout is true if the negotiation timed out
void on_config(config_func handler)
{ config_handler = handler; }

// disable or enable console spam
void set_silent(bool sil)
{ this->console_spam = !sil; }

private:
void offer(UDPSocket&, const char* data, size_t len);
void request(UDPSocket&); // --> acknowledge
void acknowledge(const char* data, size_t len);

uint32_t xid;
IP4::addr ipaddr, netmask, router, dns_server;
uint32_t lease_time;
On_config config_handler = [](Stack&){ INFO("DHCPv4::On_config","Config complete"); };
Stack& stack;
uint32_t xid;
IP4::addr ipaddr, netmask, router, dns_server;
uint32_t lease_time;
config_func config_handler;
hw::PIT::Timer_iterator timeout;
bool console_spam;
};
}

Expand Down
2 changes: 0 additions & 2 deletions api/net/inet.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -45,8 +45,6 @@ namespace net {
virtual TCP& tcp() = 0;
virtual UDP& udp() = 0;

virtual std::shared_ptr<DHClient> dhclient() = 0;

virtual constexpr uint16_t MTU() const = 0;

virtual Packet_ptr createPacket(size_t size) = 0;
Expand Down
25 changes: 14 additions & 11 deletions api/net/inet4.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -26,15 +26,15 @@
#include "ip4/arp.hpp"
#include "ip4/ip4.hpp"
#include "ip4/udp.hpp"
#include "ip4/icmpv4.hpp"
#include "dns/client.hpp"
#include "tcp.hpp"
#include "dhcp/dh4client.hpp"
#include <vector>

#include "ip4/icmpv4.hpp"

namespace net {

class DHClient;

/** A complete IP4 network stack */
template <typename DRIVER>
class Inet4 : public Inet<Ethernet, IP4>{
Expand Down Expand Up @@ -65,7 +65,7 @@ namespace net {
UDP& udp() override { return udp_; }

/** Get the DHCP client (if any) */
inline std::shared_ptr<DHClient> dhclient() override { return dhcp_; }
auto dhclient() { return dhcp_; }

/** Create a Packet, with a preallocated buffer.
@param size : the "size" reported by the allocated packet.
Expand Down Expand Up @@ -94,19 +94,22 @@ namespace net {
* @func a delegate that provides a hostname and its address, which is 0 if the
* name @hostname was not found. Note: Test with INADDR_ANY for a 0-address.
**/
inline virtual void
resolve(const std::string& hostname,
resolve_func<IP4> func) override
virtual void resolve(const std::string& hostname,
resolve_func<IP4> func) override
{
dns.resolve(this->dns_server, hostname, func);
}

inline virtual void
set_dns_server(IP4::addr server) override

virtual void set_dns_server(IP4::addr server) override
{
this->dns_server = server;
}


// handler called after the network successfully, or
// unsuccessfully negotiated with DHCP-server
// the timeout parameter indicates whether dhcp negotitation failed
void on_config(delegate<void(bool)> handler);

/** We don't want to copy or move an IP-stack. It's tied to a device. */
Inet4(Inet4&) = delete;
Inet4(Inet4&&) = delete;
Expand Down
14 changes: 11 additions & 3 deletions api/net/inet4.inc
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
//-*- C++ -*-
#define DEBUG
#include <os>
#include "dhcp/dh4client.hpp"

namespace net
{
Expand All @@ -25,7 +26,7 @@ namespace net
/** Upstream wiring */
// Packets available
nic.on_transmit_queue_available(
transmit_avail_delg::from<Inet4<T>, &Inet4<T>::process_sendq>(*this));
transmit_avail_delg::from<Inet4<T>, &Inet4<T>::process_sendq>(*this));

// Phys -> Eth (Later, this will be passed through router)
nic.set_linklayer_out(eth_bottom);
Expand Down Expand Up @@ -82,9 +83,16 @@ namespace net
{
INFO("Inet4","Applying DHCP client");
dhcp_ = std::make_shared<DHClient>(*this);
dhcp_->negotiate();
// 2 second timeout for DHCP-server negotation
dhcp_->negotiate(2.0);
}


template <typename T>
void Inet4<T>::on_config(delegate<void(bool)> handler)
{
dhcp_->on_config(handler);
}

template <typename T>
void Inet4<T>::process_sendq(size_t packets)
{
Expand Down
Loading