nic_router: rename and move Ip_allocation

Rename Ip_allocation Dhcp_allocation and move it to dhcp_server.* .

Ref #2534
This commit is contained in:
Martin Stein 2017-10-16 13:37:06 +02:00 committed by Christian Helmuth
parent 127ceaccb5
commit 9d84d8b3bd
4 changed files with 194 additions and 185 deletions

View File

@ -13,11 +13,16 @@
/* local includes */
#include <dhcp_server.h>
#include <interface.h>
using namespace Net;
using namespace Genode;
/*****************
** Dhcp_server **
*****************/
Dhcp_server::Dhcp_server(Xml_node const node,
Allocator &alloc,
Ipv4_address_prefix const &interface)
@ -81,3 +86,71 @@ void Dhcp_server::free_ip(Ipv4_address const &ip)
{
_ip_alloc.free(ip.to_uint32_little_endian() - _ip_first_raw);
}
/*********************
** Dhcp_allocation **
*********************/
Dhcp_allocation::Dhcp_allocation(Interface &interface,
Ipv4_address const &ip,
Mac_address const &mac,
Timer::Connection &timer,
Microseconds lifetime)
:
_interface(interface), _ip(ip), _mac(mac),
_timeout(timer, *this, &Dhcp_allocation::_handle_timeout)
{
_timeout.schedule(lifetime);
}
void Dhcp_allocation::lifetime(Microseconds lifetime)
{
_timeout.schedule(lifetime);
}
bool Dhcp_allocation::_higher(Mac_address const &mac) const
{
return memcmp(mac.addr, _mac.addr, sizeof(_mac.addr)) > 0;
}
Dhcp_allocation &Dhcp_allocation::find_by_mac(Mac_address const &mac)
{
if (mac == _mac) {
return *this; }
Dhcp_allocation *const allocation = child(_higher(mac));
if (!allocation) {
throw Dhcp_allocation_tree::No_match(); }
return allocation->find_by_mac(mac);
}
void Dhcp_allocation::print(Output &output) const
{
Genode::print(output, "MAC ", _mac, " IP ", _ip);
}
void Dhcp_allocation::_handle_timeout(Duration)
{
_interface.dhcp_allocation_expired(*this);
}
/**************************
** Dhcp_allocation_tree **
**************************/
Dhcp_allocation &
Dhcp_allocation_tree::find_by_mac(Mac_address const &mac) const
{
if (!first()) {
throw No_match(); }
return first()->find_by_mac(mac);
}

View File

@ -19,11 +19,22 @@
#include <bit_allocator_dynamic.h>
/* Genode includes */
#include <net/mac_address.h>
#include <util/noncopyable.h>
#include <util/xml_node.h>
#include <os/duration.h>
#include <timer_session/connection.h>
namespace Net { class Dhcp_server; }
namespace Net {
class Dhcp_server;
class Dhcp_allocation;
class Dhcp_allocation;
class Dhcp_allocation_tree;
using Dhcp_allocation_list = Genode::List<Dhcp_allocation>;
/* forward declarations */
class Interface;
}
class Net::Dhcp_server : private Genode::Noncopyable
@ -71,4 +82,67 @@ class Net::Dhcp_server : private Genode::Noncopyable
Genode::Microseconds ip_lease_time() const { return _ip_lease_time; }
};
struct Net::Dhcp_allocation_tree : public Genode::Avl_tree<Dhcp_allocation>,
private Genode::Noncopyable
{
struct No_match : Genode::Exception { };
Dhcp_allocation &find_by_mac(Mac_address const &mac) const;
};
class Net::Dhcp_allocation : public Genode::Avl_node<Dhcp_allocation>,
public Dhcp_allocation_list::Element,
private Genode::Noncopyable
{
protected:
Interface &_interface;
Ipv4_address const _ip;
Mac_address const _mac;
Timer::One_shot_timeout<Dhcp_allocation> _timeout;
bool _bound { false };
void _handle_timeout(Genode::Duration);
bool _higher(Mac_address const &mac) const;
public:
Dhcp_allocation(Interface &interface,
Ipv4_address const &ip,
Mac_address const &mac,
Timer::Connection &timer,
Genode::Microseconds lifetime);
Dhcp_allocation &find_by_mac(Mac_address const &mac);
void lifetime(Genode::Microseconds lifetime);
/**************
** Avl_node **
**************/
bool higher(Dhcp_allocation *alloc) { return _higher(alloc->_mac); }
/*********
** Log **
*********/
void print(Genode::Output &output) const;
/***************
** Accessors **
***************/
Ipv4_address const &ip() const { return _ip; }
bool bound() const { return _bound; }
void set_bound() { _bound = true; }
};
#endif /* _DHCP_SERVER_H_ */

View File

@ -244,10 +244,10 @@ void Interface::link_closed(Link &link, L3_protocol const prot)
}
void Interface::ip_allocation_expired(Ip_allocation &allocation)
void Interface::dhcp_allocation_expired(Dhcp_allocation &allocation)
{
_release_ip_allocation(allocation);
_released_ip_allocations.insert(&allocation);
_release_dhcp_allocation(allocation);
_released_dhcp_allocations.insert(&allocation);
}
@ -397,12 +397,12 @@ void Interface::_send_dhcp_reply(Dhcp_server const &dhcp_srv,
}
void Interface::_release_ip_allocation(Ip_allocation &allocation)
void Interface::_release_dhcp_allocation(Dhcp_allocation &allocation)
{
if (_config().verbose()) {
log("Release IP allocation: ", allocation, " at ", *this);
}
_ip_allocations.remove(&allocation);
_dhcp_allocations.remove(&allocation);
}
@ -420,8 +420,8 @@ void Interface::_handle_dhcp_request(Ethernet_frame &eth,
try {
/* look up existing DHCP configuration for client */
Ip_allocation &allocation =
_ip_allocations.find_by_mac(dhcp.client_mac());
Dhcp_allocation &allocation =
_dhcp_allocations.find_by_mac(dhcp.client_mac());
switch (msg_type) {
case Dhcp_packet::Message_type::DISCOVER:
@ -467,8 +467,8 @@ void Interface::_handle_dhcp_request(Ethernet_frame &eth,
} else {
_release_ip_allocation(allocation);
_destroy_ip_allocation(allocation);
_release_dhcp_allocation(allocation);
_destroy_dhcp_allocation(allocation);
return;
}
}
@ -483,8 +483,8 @@ void Interface::_handle_dhcp_request(Ethernet_frame &eth,
case Dhcp_packet::Message_type::DECLINE:
case Dhcp_packet::Message_type::RELEASE:
_release_ip_allocation(allocation);
_destroy_ip_allocation(allocation);
_release_dhcp_allocation(allocation);
_destroy_dhcp_allocation(allocation);
return;
case Dhcp_packet::Message_type::NAK:
@ -493,18 +493,17 @@ void Interface::_handle_dhcp_request(Ethernet_frame &eth,
default: throw Bad_dhcp_request();
}
}
catch (Ip_allocation_tree::No_match) {
catch (Dhcp_allocation_tree::No_match) {
switch (msg_type) {
case Dhcp_packet::Message_type::DISCOVER:
{
Ip_allocation &allocation = *new (_alloc)
Ip_allocation(*this, _config(),
dhcp_srv.alloc_ip(),
dhcp.client_mac(), _timer,
_config().rtt());
Dhcp_allocation &allocation = *new (_alloc)
Dhcp_allocation(*this, dhcp_srv.alloc_ip(),
dhcp.client_mac(), _timer,
_config().rtt());
_ip_allocations.insert(&allocation);
_dhcp_allocations.insert(&allocation);
if (_config().verbose()) {
log("Offer IP allocation: ", allocation,
" at ", *this);
@ -789,18 +788,18 @@ void Interface::_ready_to_ack()
}
void Interface::_destroy_ip_allocation(Ip_allocation &allocation)
void Interface::_destroy_dhcp_allocation(Dhcp_allocation &allocation)
{
_domain.dhcp_server().free_ip(allocation.ip());
destroy(_alloc, &allocation);
}
void Interface::_destroy_released_ip_allocations()
void Interface::_destroy_released_dhcp_allocations()
{
while (Ip_allocation *allocation = _released_ip_allocations.first()) {
_released_ip_allocations.remove(allocation);
_destroy_ip_allocation(*allocation);
while (Dhcp_allocation *allocation = _released_dhcp_allocations.first()) {
_released_dhcp_allocations.remove(allocation);
_destroy_dhcp_allocation(*allocation);
}
}
@ -812,7 +811,7 @@ void Interface::_handle_eth(void *const eth_base,
/* do garbage collection over transport-layer links and IP allocations */
_destroy_closed_links<Udp_link>(_closed_udp_links, _alloc);
_destroy_closed_links<Tcp_link>(_closed_tcp_links, _alloc);
_destroy_released_ip_allocations();
_destroy_released_dhcp_allocations();
/* inspect and handle ethernet frame */
try {
@ -955,10 +954,10 @@ Interface::~Interface()
_destroy_links<Udp_link>(_udp_links, _closed_udp_links, _alloc);
/* destroy IP allocations */
_destroy_released_ip_allocations();
while (Ip_allocation *allocation = _ip_allocations.first()) {
_ip_allocations.remove(allocation);
_destroy_ip_allocation(*allocation);
_destroy_released_dhcp_allocations();
while (Dhcp_allocation *allocation = _dhcp_allocations.first()) {
_dhcp_allocations.remove(allocation);
_destroy_dhcp_allocation(*allocation);
}
}
@ -973,75 +972,3 @@ void Interface::print(Output &output) const
{
Genode::print(output, "\"", _domain.name(), "\"");
}
/*******************
** Ip_allocation **
*******************/
Ip_allocation::Ip_allocation(Interface &interface,
Configuration &config,
Ipv4_address const &ip,
Mac_address const &mac,
Timer::Connection &timer,
Microseconds lifetime)
:
_interface(interface),
_config(config),
_ip(ip),
_mac(mac),
_release_timeout(timer, *this, &Ip_allocation::_handle_release_timeout)
{
_release_timeout.schedule(lifetime);
}
void Ip_allocation::lifetime(Microseconds lifetime)
{
_release_timeout.schedule(lifetime);
}
bool Ip_allocation::_higher(Mac_address const &mac) const
{
return memcmp(mac.addr, _mac.addr, sizeof(_mac.addr)) > 0;
}
Ip_allocation &Ip_allocation::find_by_mac(Mac_address const &mac)
{
if (mac == _mac) {
return *this; }
Ip_allocation *const allocation = child(_higher(mac));
if (!allocation) {
throw Ip_allocation_tree::No_match(); }
return allocation->find_by_mac(mac);
}
void Ip_allocation::print(Output &output) const
{
Genode::print(output, "MAC ", _mac, " IP ", _ip);
}
void Ip_allocation::_handle_release_timeout(Duration)
{
_interface.ip_allocation_expired(*this);
}
/************************
** Ip_allocation_tree **
************************/
Ip_allocation &
Ip_allocation_tree::find_by_mac(Mac_address const &mac) const
{
if (!first()) {
throw No_match(); }
return first()->find_by_mac(mac);
}

View File

@ -20,6 +20,7 @@
#include <arp_waiter.h>
#include <l3_protocol.h>
#include <dhcp_client.h>
#include <dhcp_server.h>
/* Genode includes */
#include <nic_session/nic_session.h>
@ -35,9 +36,6 @@ namespace Net {
class Transport_rule_list;
class Ethernet_frame;
class Arp_packet;
class Ip_allocation;
class Ip_allocation_tree;
using Ip_allocation_list = Genode::List<Ip_allocation>;
class Interface;
class Dhcp_server;
class Configuration;
@ -45,69 +43,6 @@ namespace Net {
}
class Net::Ip_allocation : public Genode::Avl_node<Ip_allocation>,
public Ip_allocation_list::Element
{
protected:
Interface &_interface;
Configuration &_config;
Ipv4_address const _ip;
Mac_address const _mac;
Timer::One_shot_timeout<Ip_allocation> _release_timeout;
bool _bound { false };
void _handle_release_timeout(Genode::Duration);
bool _higher(Mac_address const &mac) const;
public:
Ip_allocation(Interface &interface,
Configuration &config,
Ipv4_address const &ip,
Mac_address const &mac,
Timer::Connection &timer,
Genode::Microseconds lifetime);
Ip_allocation &find_by_mac(Mac_address const &mac);
void lifetime(Genode::Microseconds lifetime);
/**************
** Avl_node **
**************/
bool higher(Ip_allocation *allocation) { return _higher(allocation->_mac); }
/*********
** Log **
*********/
void print(Genode::Output &output) const;
/***************
** Accessors **
***************/
Ipv4_address const &ip() const { return _ip; }
bool bound() const { return _bound; }
void set_bound() { _bound = true; }
};
struct Net::Ip_allocation_tree : public Genode::Avl_tree<Ip_allocation>
{
struct No_match : Genode::Exception { };
Ip_allocation &find_by_mac(Mac_address const &mac) const;
};
class Net::Interface
{
protected:
@ -125,19 +60,19 @@ class Net::Interface
private:
Timer::Connection &_timer;
Genode::Allocator &_alloc;
Domain &_domain;
Arp_cache _arp_cache;
Arp_waiter_list _own_arp_waiters;
Arp_waiter_list _foreign_arp_waiters;
Link_side_tree _tcp_links;
Link_side_tree _udp_links;
Link_list _closed_tcp_links;
Link_list _closed_udp_links;
Ip_allocation_tree _ip_allocations;
Ip_allocation_list _released_ip_allocations;
Dhcp_client _dhcp_client { _alloc, _timer, *this };
Timer::Connection &_timer;
Genode::Allocator &_alloc;
Domain &_domain;
Arp_cache _arp_cache;
Arp_waiter_list _own_arp_waiters;
Arp_waiter_list _foreign_arp_waiters;
Link_side_tree _tcp_links;
Link_side_tree _udp_links;
Link_list _closed_tcp_links;
Link_list _closed_udp_links;
Dhcp_allocation_tree _dhcp_allocations;
Dhcp_allocation_list _released_dhcp_allocations;
Dhcp_client _dhcp_client { _alloc, _timer, *this };
void _new_link(L3_protocol const protocol,
Link_side_id const &local_id,
@ -145,11 +80,11 @@ class Net::Interface
Interface &remote_interface,
Link_side_id const &remote_id);
void _destroy_released_ip_allocations();
void _destroy_released_dhcp_allocations();
void _destroy_ip_allocation(Ip_allocation &allocation);
void _destroy_dhcp_allocation(Dhcp_allocation &allocation);
void _release_ip_allocation(Ip_allocation &allocation);
void _release_dhcp_allocation(Dhcp_allocation &allocation);
void _send_dhcp_reply(Dhcp_server const &dhcp_srv,
Mac_address const &client_mac,
@ -267,7 +202,7 @@ class Net::Interface
void link_closed(Link &link, L3_protocol const prot);
void ip_allocation_expired(Ip_allocation &allocation);
void dhcp_allocation_expired(Dhcp_allocation &allocation);
void dissolve_link(Link_side &link_side, L3_protocol const prot);