net: teach an interface/device about hardware addresses

Since the interface must add its own hardware address when sending packets,
it better know about them.
This commit is contained in:
Avi Kivity
2014-08-31 20:30:48 +03:00
parent ac4f9a65b5
commit ea7d91ac2c
3 changed files with 13 additions and 1 deletions

4
net.cc
View File

@@ -39,6 +39,10 @@ future<packet, ethernet_address> interface::receive(uint16_t proto_num) {
return pr.get_future();
}
interface::interface(std::unique_ptr<device> dev)
: _dev(std::move(dev)), _hw_address(_dev->hw_address()) {
}
void interface::run() {
_dev->receive().then([this] (packet p) {
auto eh = p.get_header<eth_hdr>(0);

5
net.hh
View File

@@ -132,11 +132,13 @@ private:
class interface {
std::unique_ptr<device> _dev;
std::unordered_map<uint16_t, promise<packet, ethernet_address>> _proto_map;
ethernet_address _hw_address;
private:
future<packet, ethernet_address> receive(uint16_t proto_num);
future<> send(uint16_t proto_num, ethernet_address to, packet p);
public:
explicit interface(std::unique_ptr<device> dev) : _dev(std::move(dev)) {}
explicit interface(std::unique_ptr<device> dev);
ethernet_address hw_address() { return _hw_address; }
void run();
friend class l3_protocol;
};
@@ -146,6 +148,7 @@ public:
virtual ~device() {}
virtual future<packet> receive() = 0;
virtual future<> send(packet p) = 0;
virtual ethernet_address hw_address() = 0;
};
inline

View File

@@ -339,6 +339,7 @@ public:
explicit virtio_net_device(sstring tap_device, init x = init());
virtual future<packet> receive() override;
virtual future<> send(packet p) override;
virtual ethernet_address hw_address() override;
};
virtio_net_device::txq::txq(virtio_net_device& dev, vring::config config,
@@ -514,6 +515,10 @@ void virtio_net_device::queue_rx_packet(packet p) {
_rx_queue_length.signal(1);
}
ethernet_address virtio_net_device::hw_address() {
return { 0x12, 0x23, 0x34, 0x56, 0x67, 0x78 };
}
std::unique_ptr<net::device> create_virtio_net_device(sstring tap_device) {
return std::make_unique<virtio_net_device>(tap_device);
}