10#include "virtio_net.h"
48 std::set<l4_uint16_t> _vlan_ids;
51 {
return 1UL << (vid & 31U); }
61 char const *get_name()
const
67 inline bool is_trunk()
const
68 {
return _vlan_id == VLAN_ID_TRUNK; }
70 inline bool is_native()
const
71 {
return _vlan_id == VLAN_ID_NATIVE; }
73 inline bool is_access()
const
74 {
return !is_trunk() && !is_native(); }
85 assert(vlan_valid_id(
id));
87 _vlan_bloom_filter = 0;
106 for (
const auto id : ids)
108 assert(vlan_valid_id(
id));
109 filter |= vlan_bloom_hash(
id);
110 _vlan_ids.insert(
id);
113 _vlan_id = VLAN_ID_TRUNK;
114 _vlan_bloom_filter = filter;
125 _vlan_id = VLAN_ID_TRUNK;
126 _vlan_bloom_filter = 0;
144 if ((_vlan_bloom_filter & vlan_bloom_hash(
id)) == 0)
147 return _vlan_ids.find(
id) != _vlan_ids.end();
162 explicit Virtio_port(
unsigned vq_max,
unsigned num_ds,
char const *name,
169 strncpy(_name, name,
sizeof(_name));
170 _name[
sizeof(_name) - 1] =
'\0';
172 Features hf = _dev_config.host_features(0);
176 memcpy((
void *)_dev_config.priv_config()->mac,
mac,
177 sizeof(_dev_config.priv_config()->mac));
180 Dbg(Dbg::Port, Dbg::Info)
181 .printf(
"%s: Adding Mac to host features to %x\n", _name, hf.raw);
183 _dev_config.host_features(0) = hf.raw;
184 _dev_config.reset_hdr();
185 Dbg(Dbg::Port, Dbg::Info)
186 .printf(
"%s: Set host features to %x\n", _name,
187 _dev_config.host_features(0));
210 if (_vlan_ids.find(ret->vlan_id()) == _vlan_ids.end())
213 else if (is_access() && ret->has_vlan())
250 if (!src_port->is_trunk() && !src_port->is_native())
258 if (src_port->is_trunk())
264 case Virtio_net_transfer::Result::Delivered: [[fallthrough]];
265 case Virtio_net_transfer::Result::Exception:
return;
266 case Virtio_net_transfer::Result::Dropped:
break;
void init_mem_info(unsigned num)
Initialize the memory region list to the given maximum.
A wrapper class around the value of a MAC address.
Abstraction for a network request.
static std::optional< Virtio_net_request > get_request(Virtio_net *dev, L4virtio::Svr::Virtqueue *queue)
Construct a request from the next entry of a provided queue.
static void drop_requests(Virtio_net *dev, L4virtio::Svr::Virtqueue *queue)
Drop all requests of a specific queue.
static Result transfer(Virtio_net_request const &request, Virtio_net *dst_dev, L4virtio::Svr::Virtqueue *dst_queue, Virtio_vlan_mangle &mangle)
Deliver the request to the destination port.
The Base class of a Port.
Virtqueue * rx_q()
Getter for the receive queue.
Virtqueue * tx_q()
Getter for the transmission queue.
A Port on the Virtio Net Switch.
std::optional< Virtio_net_request > get_tx_request()
Get one request from the transmission queue.
void set_vlan_trunk(const std::vector< l4_uint16_t > &ids)
Set port as trunk port.
bool tx_work_pending() const
Check whether there is any work pending on the transmission queue.
void set_monitor()
Set this port as monitor port.
void set_vlan_access(l4_uint16_t id)
Set port as access port for a certain VLAN.
void handle_request(Virtio_port *src_port, Virtio_net_request const &request)
Handle a request - send it to the guest associated with this port.
Mac_addr mac() const
Get MAC address.
bool match_vlan(uint16_t id)
Match VLAN id.
Virtio_port(unsigned vq_max, unsigned num_ds, char const *name, l4_uint8_t const *mac)
Create a Virtio net port object.
void drop_requests()
Drop all requests pending in the transmission queue.
Class for VLAN packet rewriting.
static constexpr Virtio_vlan_mangle remove()
Construct an object that removes the VLAN tag.
static constexpr Virtio_vlan_mangle add(l4_uint16_t tci)
Construct an object that adds a VLAN tag.
unsigned char l4_uint8_t
Unsigned 8bit value.
unsigned int l4_uint32_t
Unsigned 32bit value.
unsigned short int l4_uint16_t
Unsigned 16bit value.
#define L4_LIKELY(x)
Expression is likely to execute.