Loading...
Searching...
No Matches
request.h
/*
* Copyright (C) 2016-2017, 2020, 2022, 2024 Kernkonzept GmbH.
* Author(s): Jean Wolter <jean.wolter@kernkonzept.com>
*
* License: see LICENSE.spdx (in this directory or the directories above)
*/
#pragma once
#include "mac_addr.h"
#include "virtio_net.h"
#include "virtio_net_buffer.h"
#include "vlan.h"
#include <l4/l4virtio/server/virtio>
class Net_transfer
{
public:
virtual ~Net_transfer() = default;
void const *req_id() const { return _req_id; }
virtual void copy_header(Virtio_net::Hdr *dst_header) const = 0;
Buffer &cur_buf() { return _cur_buf; }
virtual bool done() = 0;
protected:
Buffer _cur_buf;
void const *_req_id;
};
class Net_request
{
public:
bool has_vlan() const
{
if (!_pkt.pos || _pkt.left < 14)
return false;
uint8_t *p = reinterpret_cast<uint8_t *>(_pkt.pos);
return p[12] == 0x81U && p[13] == 0x00U;
}
uint16_t vlan_id() const
{
if (!has_vlan() || _pkt.left < 16)
return VLAN_ID_NATIVE;
uint8_t *p = reinterpret_cast<uint8_t *>(_pkt.pos);
return (uint16_t{p[14]} << 8 | p[15]) & 0xfffU;
}
uint8_t const *buffer(size_t *size) const
{
*size = _pkt.left;
return reinterpret_cast<uint8_t const *>(_pkt.pos);
}
void dump_pkt() const
{
Dbg pkt_debug(Dbg::Packet, Dbg::Debug, "PKT");
if (pkt_debug.is_active())
{
//pkt_debug.cprintf("\t");
//src_mac().print(pkt_debug);
//pkt_debug.cprintf(" -> ");
//dst_mac().print(pkt_debug);
//pkt_debug.cprintf("\n");
Dbg pkt_trace(Dbg::Packet, Dbg::Trace, "PKT");
if (pkt_trace.is_active() && _pkt.left >= 14)
{
uint8_t const *packet = reinterpret_cast<uint8_t const *>(_pkt.pos);
pkt_trace.cprintf("\n\tEthertype: ");
uint16_t ether_type = uint16_t{packet[12]} << 8 | packet[13];
char const *protocol;
switch (ether_type)
{
case 0x0800: protocol = "IPv4"; break;
case 0x0806: protocol = "ARP"; break;
case 0x8100: protocol = "Vlan"; break;
case 0x86dd: protocol = "IPv6"; break;
case 0x8863: protocol = "PPPoE Discovery"; break;
case 0x8864: protocol = "PPPoE Session"; break;
default: protocol = nullptr;
}
if (protocol)
pkt_trace.cprintf("%s\n", protocol);
else
pkt_trace.cprintf("%04x\n", ether_type);
}
}
}
protected:
Buffer _pkt;
};
Net_transfer
A network request to only a single destination.
Definition request.h:34
Net_transfer::copy_header
virtual void copy_header(Virtio_net::Hdr *dst_header) const =0
Populate the virtio-net header for the destination.
Net_transfer::req_id
void const * req_id() const
Identifier for the underlying Net_request, used for logging purposes.
Definition request.h:41
Net_transfer::done
virtual bool done()=0
Check whether the transfer has been completed, i.e.
Net_transfer::cur_buf
Buffer & cur_buf()
Buffer containing (a part of) the packet data.
Definition request.h:54
Buffer
Data buffer used to transfer packets.
Definition virtio_net_buffer.h:20
L4virtio::Svr::Data_buffer::left
l4_uint32_t left
Bytes left in buffer.
Definition virtio:309
L4virtio::Svr::Data_buffer::pos
char * pos
Current buffer position.
Definition virtio:308