L4Re Operating System Framework
Interface and Usage Documentation
Loading...
Searching...
No Matches
vlan.h
1/*
2 * Copyright (C) 2020, 2022-2024 Kernkonzept GmbH.
3 * Author(s): Jan Klötzke <jan.kloetzke@kernkonzept.com>
4 *
5 * License: see LICENSE.spdx (in this directory or the directories above)
6 */
7#pragma once
8
9#include <l4/cxx/minmax>
10#include <l4/l4virtio/server/virtio>
11#include <l4/sys/types.h>
12#include <string.h>
13
14#include "virtio_net.h"
15#include "virtio_net_buffer.h"
16
17namespace {
18
19const l4_uint16_t VLAN_ID_NATIVE = 0xffffU;
20const l4_uint16_t VLAN_ID_TRUNK = 0xfffeU;
21
22inline bool vlan_valid_id(l4_uint16_t id)
23{
24 return id > 0U && id < 0xfffU;
25}
26
27}
37{
38 l4_uint16_t _tci;
39 l4_uint8_t _mac_remaining;
40 l4_int8_t _tag_remaining;
41
42 constexpr Virtio_vlan_mangle(l4_uint16_t tci, l4_int8_t tag_remaining)
43 : _tci{tci}, _mac_remaining{12}, _tag_remaining{tag_remaining}
44 {}
45
46public:
53 : _tci{0}, _mac_remaining{0}, _tag_remaining{0}
54 {}
55
64 static constexpr Virtio_vlan_mangle add(l4_uint16_t tci)
65 {
66 return Virtio_vlan_mangle(tci, 4);
67 }
68
75 static constexpr Virtio_vlan_mangle remove()
76 {
77 return Virtio_vlan_mangle(0xffffU, -4);
78 }
79
94 {
95 l4_uint32_t ret;
96
97 if (L4_LIKELY(_tci == 0))
98 {
99 // pass through (no tag or keep tag)
100 ret = src.copy_to(&dst);
101 }
102 else if (_mac_remaining)
103 {
104 // copy initial MAC addresses
105 ret = src.copy_to(&dst, _mac_remaining);
106 _mac_remaining -= ret;
107 }
108 else if (_tag_remaining > 0)
109 {
110 // add VLAN tag
111 l4_uint8_t tag[4] = {
112 0x81, 0x00,
113 static_cast<l4_uint8_t>(_tci >> 8),
114 static_cast<l4_uint8_t>(_tci & 0xffU)
115 };
116
117 ret = cxx::min(static_cast<l4_uint32_t>(_tag_remaining), dst.left);
118 memcpy(dst.pos, &tag[4 - _tag_remaining], ret);
119 dst.skip(ret);
120 _tag_remaining -= (int)ret;
121 }
122 else if (_tag_remaining < 0)
123 {
124 // remove VLAN tag
125 _tag_remaining += static_cast<int>(src.skip(-_tag_remaining));
126 ret = 0;
127 }
128 else
129 ret = src.copy_to(&dst);
130
131 return ret;
132 }
133
142 void rewrite_hdr(Virtio_net::Hdr *hdr)
143 {
144 if (L4_UNLIKELY(_tci != 0 && hdr->flags.need_csum()))
145 {
146 if (_tci == 0xffffU)
147 hdr->csum_start -= 4U;
148 else
149 hdr->csum_start += 4U;
150 }
151 }
152};
153
Class for VLAN packet rewriting.
Definition vlan.h:37
static constexpr Virtio_vlan_mangle remove()
Construct an object that removes the VLAN tag.
Definition vlan.h:75
void rewrite_hdr(Virtio_net::Hdr *hdr)
Rewrite the virtio network header.
Definition vlan.h:142
Virtio_vlan_mangle()
Default constructor.
Definition vlan.h:52
static constexpr Virtio_vlan_mangle add(l4_uint16_t tci)
Construct an object that adds a VLAN tag.
Definition vlan.h:64
l4_uint32_t copy_pkt(Buffer &dst, Buffer &src)
Copy packet from src to dst.
Definition vlan.h:93
unsigned char l4_uint8_t
Unsigned 8bit value.
Definition l4int.h:25
signed char l4_int8_t
Signed 8bit value.
Definition l4int.h:24
unsigned int l4_uint32_t
Unsigned 32bit value.
Definition l4int.h:29
unsigned short int l4_uint16_t
Unsigned 16bit value.
Definition l4int.h:27
#define L4_UNLIKELY(x)
Expression is unlikely to execute.
Definition compiler.h:275
#define L4_LIKELY(x)
Expression is likely to execute.
Definition compiler.h:274
Common L4 ABI Data Types.
String.
Data buffer used to transfer packets.
l4_uint32_t copy_to(Data_buffer *dst, l4_uint32_t max=UINT_MAX)
Copy contents from this buffer to the destination buffer.
Definition virtio:354
l4_uint32_t left
Bytes left in buffer.
Definition virtio:309
char * pos
Current buffer position.
Definition virtio:308
l4_uint32_t skip(l4_uint32_t bytes)
Skip given number of bytes in this buffer.
Definition virtio:375