-
Notifications
You must be signed in to change notification settings - Fork 100
/
Simplebridge.cpp
177 lines (134 loc) · 4.77 KB
/
Simplebridge.cpp
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
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
/*
* Copyright 2018 The Polycube Authors
*
* 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.
*/
//Modify these methods with your own implementation
#include "Simplebridge.h"
#include "Simplebridge_dp.h"
#include <tins/ethernetII.h>
#include <tins/tins.h>
#include <thread>
using namespace Tins;
Simplebridge::Simplebridge(const std::string name, const SimplebridgeJsonObject &conf, CubeType type)
: Cube(name, {generate_code()}, {}, type, conf.getPolycubeLoglevel()),
quit_thread_(false) {
logger()->set_pattern("[%Y-%m-%d %H:%M:%S.%e] [Simplebridge] [%n] [%l] %v");
logger()->info("Creating Simplebridge instance");
addFdb(conf.getFdb());
addPortsList(conf.getPorts());
timestamp_update_thread_ = std::thread(&Simplebridge::updateTimestampTimer, this);
}
Simplebridge::~Simplebridge() {
// we are destroying this service, prepare for it.
quitAndJoin();
Cube::dismount();
}
void Simplebridge::quitAndJoin() {
quit_thread_ = true;
timestamp_update_thread_.join();
}
void Simplebridge::updateTimestampTimer() {
do {
sleep(1);
updateTimestamp();
} while (!quit_thread_);
}
/*
* This method is in charge of updating the timestamp table
* that is used in the dataplane to avoid calling the bpf_ktime helper
* that introduces a non-negligible overhead to the eBPF program.
*/
void Simplebridge::updateTimestamp() {
try {
// get timestamp from system
struct timespec now_timespec;
clock_gettime(CLOCK_MONOTONIC, &now_timespec);
auto timestamp_table = get_array_table<uint32_t>("timestamp");
timestamp_table.set(0, now_timespec.tv_sec);
} catch (...) {
logger()->error("Error while updating the timestamp table");
}
}
void Simplebridge::update(const SimplebridgeJsonObject &conf) {
//This method updates all the object/parameter in Simplebridge object specified in the conf JsonObject.
//You can modify this implementation.
if(conf.fdbIsSet()) {
auto m = getFdb();
m->update(conf.getFdb());
}
if(conf.loglevelIsSet()) {
setLoglevel(conf.getLoglevel());
}
if(conf.portsIsSet()) {
for(auto &i : conf.getPorts()){
auto name = i.getName();
auto m = getPorts(name);
m->update(i);
}
}
}
SimplebridgeJsonObject Simplebridge::toJsonObject(){
SimplebridgeJsonObject conf;
conf.setFdb(getFdb()->toJsonObject());
conf.setUuid(getUuid());
conf.setLoglevel(getLoglevel());
conf.setType(getType());
for(auto &i : getPortsList()){
conf.addPorts(i->toJsonObject());
}
conf.setName(getName());
return conf;
}
std::string Simplebridge::generate_code(){
return simplebridge_code;
}
std::vector<std::string> Simplebridge::generate_code_vector(){
throw std::runtime_error("Method not implemented");
}
void Simplebridge::packet_in(Ports &port, polycube::service::PacketInMetadata &md, const std::vector<uint8_t> &packet){
logger()->debug("Packet received from port {0}", port.name());
try {
//logger()->debug("[{0}] packet from port: '{1}' id: '{2}' peer:'{3}'",
// get_name(), port.name(), port.index(), port.peer());
switch (static_cast<SlowPathReason>(md.reason)) {
case SlowPathReason::FLOODING:
logger()->debug("Received a pkt in slowpath - reason: FLOODING");
flood_packet(port, md, packet);
break;
default:
logger()->error("Not valid reason {0} received", md.reason);
}
} catch (const std::exception &e) {
logger()->error("exception during slowpath packet processing: '{0}'", e.what());
}
}
void Simplebridge::flood_packet(Port &port, PacketInMetadata &md,
const std::vector<uint8_t> &packet) {
EthernetII p(&packet[0], packet.size());
// avoid adding or removing ports while flooding packet
std::lock_guard<std::mutex> guard(ports_mutex_);
for (auto &it : get_ports()) {
if (it->name() == port.name()) {
continue;
}
it->send_packet_out(p);
logger()->trace("Packet sent to port {0} as result of flooding", it->name());
}
}
void Simplebridge::reloadCodeWithAgingtime(uint32_t aging_time) {
logger()->debug("Reloading code with agingtime: {0}", aging_time);
std::string aging_time_str("#define AGING_TIME " + std::to_string(aging_time));
reload(aging_time_str + simplebridge_code);
logger()->trace("New bridge code reloaded");
}