Skip to content

Commit 1b9dafc

Browse files
Dynmon implementation
1 parent 0b5c7f5 commit 1b9dafc

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

54 files changed

+5324
-0
lines changed

src/services/CMakeLists.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,7 @@ add_service(iptables pcn-iptables)
3636
add_service(transparenthelloworld pcn-transparent-helloworld)
3737
add_service(synflood pcn-synflood)
3838
add_service(packetcapture pcn-packetcapture)
39+
add_service(dynmon pcn-dynmon)
3940

4041
# save string to create code that load the services
4142
SET_PROPERTY(GLOBAL PROPERTY LOAD_SERVICES_ ${LOAD_SERVICES})
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
# Swagger Codegen Ignore
2+
# Generated by swagger-codegen https://github.com/swagger-api/swagger-codegen
3+
4+
# Use this file to prevent files from being overwritten by the generator.
5+
6+
.swagger-codegen-ignore
7+
8+
src/*.cpp
9+
src/*.h
10+
11+
!src/*Interface.h
12+
!src/*JsonObject.h
13+
!src/*JsonObject.cpp
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
cmake_minimum_required (VERSION 3.2)
2+
3+
set (CMAKE_CXX_STANDARD 11)
4+
5+
add_subdirectory(src)
Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
# Loads the contents of a file into a std::string variable
2+
#
3+
# It creates a header file in ${CMAKE_CURRENT_BINARY_DIR}/${file}.h
4+
# that wrapps the contents of the file in a std::string using the raw
5+
# string literal feature of C++11. The user needs to include that file
6+
# into the source code in order to see the variable.
7+
#
8+
# parameters:
9+
# target: target to add a dependency on file
10+
# file: file to be loaded
11+
# variable_name: name variable where the file is loaded
12+
#
13+
# example:
14+
# load_file_as_variable(my-lib resource.c my_resource)
15+
# Creates a resource.h in CMAKE_CURRENT_BINARY_DIR with a string variable
16+
# my_resource with the contents of resource.c
17+
# A dependency in resource.c is added to my-lib
18+
19+
function(load_file_as_variable target file variable_name)
20+
get_filename_component(file_name ${file} NAME_WE)
21+
get_filename_component(file_dir ${file} DIRECTORY)
22+
23+
set(new_path ${file_dir}/${file_name}.h)
24+
25+
add_custom_command(
26+
OUTPUT
27+
${CMAKE_CURRENT_BINARY_DIR}/${new_path}
28+
COMMAND
29+
mkdir -p ${CMAKE_CURRENT_BINARY_DIR}/${file_dir}
30+
COMMAND
31+
echo "#pragma once" > ${CMAKE_CURRENT_BINARY_DIR}/${new_path}
32+
COMMAND
33+
echo "#include <string>" >> ${CMAKE_CURRENT_BINARY_DIR}/${new_path}
34+
COMMAND
35+
echo "const std::string ${variable_name} = R\"POLYCUBE_DP(" >> ${CMAKE_CURRENT_BINARY_DIR}/${new_path}
36+
COMMAND
37+
cat ${CMAKE_CURRENT_SOURCE_DIR}/${file} >> ${CMAKE_CURRENT_BINARY_DIR}/${new_path}
38+
COMMAND
39+
cmake -E echo ")POLYCUBE_DP\";" >> ${CMAKE_CURRENT_BINARY_DIR}/${new_path}
40+
DEPENDS ${CMAKE_CURRENT_SOURCE_DIR}/${file}
41+
VERBATIM
42+
)
43+
44+
string(REPLACE "/" "-" path_replaced ${new_path})
45+
46+
add_custom_target(
47+
generate_${path_replaced}
48+
DEPENDS ${CMAKE_CURRENT_BINARY_DIR}/${new_path}
49+
)
50+
51+
add_dependencies(${target} generate_${path_replaced})
52+
endfunction()
Lines changed: 137 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,137 @@
1+
module dynmon {
2+
yang-version 1.1;
3+
namespace "http://polycube.network/dynmon";
4+
prefix "dynmon";
5+
6+
import polycube-base {
7+
prefix "polycube-base";
8+
}
9+
import polycube-transparent-base {
10+
prefix "polycube-transparent-base";
11+
}
12+
import "ietf-inet-types" {
13+
prefix "inet";
14+
}
15+
import "ietf-yang-types" {
16+
prefix "yang";
17+
}
18+
19+
organization
20+
"Polycube open source project";
21+
description
22+
"YANG data model for the Polycube Dynamic Traffic Monitor transparent service";
23+
polycube-base:service-description "Dynamic Traffic Monitor transparent service";
24+
polycube-base:service-version "1.0";
25+
polycube-base:service-name "dynmon";
26+
polycube-base:service-min-kernel-version "4.14.0";
27+
28+
uses polycube-transparent-base:transparent-base-yang-module;
29+
30+
container dataplane {
31+
description
32+
"Running dataplane";
33+
leaf name {
34+
type string;
35+
description
36+
"eBPF program name";
37+
polycube-base:init-only-config;
38+
}
39+
leaf code {
40+
type string;
41+
description
42+
"eBPF source code";
43+
polycube-base:init-only-config;
44+
}
45+
list metrics {
46+
key "name";
47+
description
48+
"Exported Metric";
49+
polycube-base:init-only-config;
50+
leaf name {
51+
type string;
52+
description
53+
"Name of the metric (e.g., number of HTTP requests)";
54+
polycube-base:init-only-config;
55+
}
56+
leaf map-name {
57+
type string;
58+
description
59+
"Corrisponding eBPF map name";
60+
polycube-base:init-only-config;
61+
}
62+
container open-metrics-metadata {
63+
presence
64+
"The metric will be exported with the OpenMetric format";
65+
description
66+
"Open-Metrics metadata";
67+
polycube-base:init-only-config;
68+
leaf help {
69+
type string;
70+
description
71+
"Metric description";
72+
polycube-base:init-only-config;
73+
}
74+
leaf type {
75+
type enumeration{
76+
enum Counter;
77+
enum Gauge;
78+
enum Histogram;
79+
enum Summary;
80+
enum Untyped;
81+
}
82+
description
83+
"Metric type";
84+
polycube-base:init-only-config;
85+
}
86+
list labels {
87+
key "name";
88+
description
89+
"Label attached to the metric";
90+
polycube-base:init-only-config;
91+
leaf name {
92+
type string;
93+
description
94+
"Name of the label (e.g., 'method')";
95+
polycube-base:init-only-config;
96+
}
97+
leaf value {
98+
type string;
99+
description
100+
"Label value (e.g., 'POST')";
101+
polycube-base:init-only-config;
102+
}
103+
}
104+
}
105+
}
106+
}
107+
list metrics {
108+
config false;
109+
key "name";
110+
description
111+
"Collected metrics";
112+
leaf name {
113+
config false;
114+
type string;
115+
description
116+
"Name of the metric (e.g, number of HTTP requests)";
117+
}
118+
leaf value {
119+
config false;
120+
type string;
121+
description
122+
"Value of the metric";
123+
}
124+
leaf timestamp {
125+
config false;
126+
type int64;
127+
description
128+
"Timestamp";
129+
}
130+
}
131+
leaf open-metrics {
132+
config false;
133+
type string;
134+
description
135+
"Collected metrics in OpenMetrics Format";
136+
}
137+
}
Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
include(${PROJECT_SOURCE_DIR}/cmake/LoadFileAsVariable.cmake)
2+
3+
aux_source_directory(serializer SERIALIZER_SOURCES)
4+
aux_source_directory(api API_SOURCES)
5+
aux_source_directory(base BASE_SOURCES)
6+
7+
include_directories(serializer)
8+
9+
if (NOT DEFINED POLYCUBE_STANDALONE_SERVICE OR POLYCUBE_STANDALONE_SERVICE)
10+
find_package(PkgConfig REQUIRED)
11+
pkg_check_modules(POLYCUBE libpolycube)
12+
include_directories(${POLYCUBE_INCLUDE_DIRS})
13+
endif(NOT DEFINED POLYCUBE_STANDALONE_SERVICE OR POLYCUBE_STANDALONE_SERVICE)
14+
15+
# Needed to load files as variables
16+
include_directories(${CMAKE_CURRENT_BINARY_DIR})
17+
18+
add_library(pcn-dynmon SHARED
19+
${SERIALIZER_SOURCES}
20+
${API_SOURCES}
21+
${BASE_SOURCES}
22+
Dataplane.cpp
23+
DataplaneMetrics.cpp
24+
DataplaneMetricsOpenMetricsMetadata.cpp
25+
DataplaneMetricsOpenMetricsMetadataLabels.cpp
26+
Dynmon.cpp
27+
Metrics.cpp
28+
MapExtractor.cpp
29+
Dynmon-lib.cpp)
30+
31+
# load ebpf datapath code a variable
32+
load_file_as_variable(pcn-dynmon
33+
Dynmon_dp.c
34+
dynmon_code)
35+
36+
# load datamodel in a variable
37+
load_file_as_variable(pcn-dynmon
38+
../datamodel/dynmon.yang
39+
dynmon_datamodel)
40+
41+
target_link_libraries(pcn-dynmon ${POLYCUBE_LIBRARIES})
42+
43+
# Specify shared library install directory
44+
45+
set(CMAKE_INSTALL_LIBDIR /usr/lib)
46+
47+
install(
48+
TARGETS
49+
pcn-dynmon
50+
DESTINATION
51+
"${CMAKE_INSTALL_LIBDIR}"
52+
)
Lines changed: 72 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,72 @@
1+
#include "Dataplane.h"
2+
#include "Dynmon.h"
3+
4+
Dataplane::Dataplane(Dynmon &parent, const DataplaneJsonObject &conf)
5+
: DataplaneBase(parent) {
6+
// Setting the dataplane name
7+
if (conf.nameIsSet())
8+
name_ = conf.getName();
9+
else
10+
name_ = DEFAULT_NAME;
11+
12+
// Setting the dataplane code
13+
if (conf.codeIsSet())
14+
code_ = conf.getCode();
15+
else
16+
code_ = DEFAULT_CODE;
17+
18+
// Setting the dataplane metrics configuration
19+
addMetricsList(conf.getMetrics());
20+
}
21+
22+
Dataplane::~Dataplane() {}
23+
24+
std::string Dataplane::getName() {
25+
return name_;
26+
}
27+
28+
std::string Dataplane::getCode() {
29+
return code_;
30+
}
31+
32+
std::shared_ptr<DataplaneMetrics> Dataplane::getMetrics(const std::string &name) {
33+
for (auto &it : metrics_)
34+
if (it->getName() == name)
35+
return it;
36+
throw std::runtime_error("The metric does not exist");
37+
}
38+
39+
std::vector<std::shared_ptr<DataplaneMetrics>> Dataplane::getMetricsList() {
40+
return metrics_;
41+
}
42+
43+
void Dataplane::addMetrics(const std::string &name, const DataplaneMetricsJsonObject &conf) {
44+
auto entry = std::make_shared<DataplaneMetrics>(*this, conf);
45+
if (entry == nullptr)
46+
throw std::runtime_error("This should not happen");
47+
for (auto &it : metrics_)
48+
if (it->getName() == entry->getName())
49+
throw std::runtime_error("Cannot insert duplicate metrics entry");
50+
metrics_.push_back(entry);
51+
}
52+
53+
void Dataplane::addMetricsList(const std::vector<DataplaneMetricsJsonObject> &conf) {
54+
DataplaneBase::addMetricsList(conf);
55+
}
56+
57+
void Dataplane::replaceMetrics(const std::string &name, const DataplaneMetricsJsonObject &conf) {
58+
DataplaneBase::replaceMetrics(name, conf);
59+
}
60+
61+
void Dataplane::delMetrics(const std::string &name) {
62+
metrics_.erase(
63+
std::remove_if(metrics_.begin(), metrics_.end(),
64+
[name](const std::shared_ptr<DataplaneMetrics> &entry) {
65+
return entry->getName() == name;
66+
}),
67+
metrics_.end());
68+
}
69+
70+
void Dataplane::delMetricsList() {
71+
metrics_.clear();
72+
}

0 commit comments

Comments
 (0)