-
Notifications
You must be signed in to change notification settings - Fork 399
Expand file tree
/
Copy pathvlan_manager.cpp
More file actions
92 lines (76 loc) · 2.5 KB
/
Copy pathvlan_manager.cpp
File metadata and controls
92 lines (76 loc) · 2.5 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
// This file is a part of the IncludeOS unikernel - www.includeos.org
//
// Copyright 2017 Oslo and Akershus University College of Applied Sciences
// and Alfred Bratterud
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
//#define VLAN_DEBUG 1
#ifdef VLAN_DEBUG
#define PRINT(fmt, ...) printf(fmt, ##__VA_ARGS__)
#else
#define PRINT(fmt, ...) /* fmt */
#endif
#include <net/vlan_manager.hpp>
#include <hal/machine.hpp>
namespace net {
VLAN_manager& VLAN_manager::get(int N)
{
static std::map<int, std::unique_ptr<VLAN_manager>> managers;
auto it = managers.find(N);
if(it != managers.end())
{
return *it->second;
}
else
{
PRINT("<VLAN_manager> Creating VLAN manager for N=%i\n", N);
auto ptr = std::unique_ptr<VLAN_manager>(new VLAN_manager);
auto ok = managers.emplace(N, std::move(ptr));
Expects(ok.second);
return *ok.first->second;
}
}
VLAN_manager::VLAN_interface& VLAN_manager::add(hw::Nic& link, const int id)
{
Expects(id > 0 and id <= 4096 && "Outside VID range (1-4096)");
// this is very redudant if it's already been set once,
// but i'll keep it for now since it's not expensive.
this->setup(link,id);
auto vif = std::make_unique<VLAN_interface>(link, id);
auto* raw = vif.get();
// register as a device (unnecessary?)
os::machine().add<hw::Nic>(std::move(vif));
auto it = links_.emplace(id, raw);
Ensures(it.second && "Could not insert - ID is most likely already taken");
INFO("VLAN", "Added VLAN %s %s", raw->driver_name(), raw->device_name().c_str());
return *it.first->second;
}
void VLAN_manager::receive(Packet_ptr pkt)
{
auto& vlan = *reinterpret_cast<ethernet::VLAN_header*>(pkt->layer_begin());
Expects(vlan.tpid == static_cast<int>(Ethertype::VLAN));
auto id = vlan.vid();
auto it = links_.find(id);
PRINT("<VLAN_manager> Recieved frame tagged with ID %d ", id);
if(it != links_.end())
{
PRINT("- Found\n");
it->second->receive(std::move(pkt));
return;
}
else
{
PRINT("- Not found\n");
}
}
} // < namespace net