L4Re Operating System Framework
Interface and Usage Documentation
Loading...
Searching...
No Matches
request.h
1/*
2 * Copyright (C) 2016-2017, 2020, 2022, 2024 Kernkonzept GmbH.
3 * Author(s): Jean Wolter <jean.wolter@kernkonzept.com>
4 *
5 * License: see LICENSE.spdx (in this directory or the directories above)
6 */
7#pragma once
8
9#include "mac_addr.h"
10#include "virtio_net.h"
11#include "virtio_net_buffer.h"
12#include "vlan.h"
13
14#include <l4/l4virtio/server/virtio>
15
16
21
34{
35public:
36 virtual ~Net_transfer() = default;
37
41 void const *req_id() const { return _req_id; }
42
46 virtual void copy_header(Virtio_net::Hdr *dst_header) const = 0;
47
54 Buffer &cur_buf() { return _cur_buf; }
55
65 virtual bool done() = 0;
66
67protected:
68 Buffer _cur_buf;
69 void const *_req_id;
70};
71
72class Net_request
73{
74public:
75
76 bool has_vlan() const
77 {
78 if (!_pkt.pos || _pkt.left < 14)
79 return false;
80
81 uint8_t *p = reinterpret_cast<uint8_t *>(_pkt.pos);
82 return p[12] == 0x81U && p[13] == 0x00U;
83 }
84
85 uint16_t vlan_id() const
86 {
87 if (!has_vlan() || _pkt.left < 16)
88 return VLAN_ID_NATIVE;
89
90 uint8_t *p = reinterpret_cast<uint8_t *>(_pkt.pos);
91 return (uint16_t{p[14]} << 8 | p[15]) & 0xfffU;
92 }
93
105 uint8_t const *buffer(size_t *size) const
106 {
107 *size = _pkt.left;
108 return reinterpret_cast<uint8_t const *>(_pkt.pos);
109 }
110
111 void dump_pkt() const
112 {
113 Dbg pkt_debug(Dbg::Packet, Dbg::Debug, "PKT");
114 if (pkt_debug.is_active())
115 {
116 //pkt_debug.cprintf("\t");
117 //src_mac().print(pkt_debug);
118 //pkt_debug.cprintf(" -> ");
119 //dst_mac().print(pkt_debug);
120 //pkt_debug.cprintf("\n");
121
122 Dbg pkt_trace(Dbg::Packet, Dbg::Trace, "PKT");
123 if (pkt_trace.is_active() && _pkt.left >= 14)
124 {
125 uint8_t const *packet = reinterpret_cast<uint8_t const *>(_pkt.pos);
126 pkt_trace.cprintf("\n\tEthertype: ");
127 uint16_t ether_type = uint16_t{packet[12]} << 8 | packet[13];
128 char const *protocol;
129 switch (ether_type)
130 {
131 case 0x0800: protocol = "IPv4"; break;
132 case 0x0806: protocol = "ARP"; break;
133 case 0x8100: protocol = "Vlan"; break;
134 case 0x86dd: protocol = "IPv6"; break;
135 case 0x8863: protocol = "PPPoE Discovery"; break;
136 case 0x8864: protocol = "PPPoE Session"; break;
137 default: protocol = nullptr;
138 }
139 if (protocol)
140 pkt_trace.cprintf("%s\n", protocol);
141 else
142 pkt_trace.cprintf("%04x\n", ether_type);
143 }
144 }
145 }
146
147protected:
148 Buffer _pkt;
149};
150
A network request to only a single destination.
Definition request.h:34
virtual void copy_header(Virtio_net::Hdr *dst_header) const =0
Populate the virtio-net header for the destination.
void const * req_id() const
Identifier for the underlying Net_request, used for logging purposes.
Definition request.h:41
virtual bool done()=0
Check whether the transfer has been completed, i.e.
Buffer & cur_buf()
Buffer containing (a part of) the packet data.
Definition request.h:54
Data buffer used to transfer packets.
l4_uint32_t left
Bytes left in buffer.
Definition virtio:309
char * pos
Current buffer position.
Definition virtio:308