Skip to content

Commit c799639

Browse files
Disable AF_PACKET by default in configuration file. Completely merged GoBGP integration code.
1 parent 713c295 commit c799639

6 files changed

Lines changed: 333 additions & 20 deletions

File tree

‎src/CMakeLists.txt‎

Lines changed: 51 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -231,43 +231,76 @@ endif()
231231

232232
if (ENABLE_GOBGP_SUPPORT)
233233
set(GOBGP_CUSTOM_INSTALL_PATH "/opt/libgobgp_1_0_0")
234+
set(GRPC_CUSTOM_INSTALL_PATH "/opt/grpc_0_11_1_7a94236d698477636dd06282f12f706cad527029")
235+
set(PROTOCOL_BUFFERS_CUSTOM_INSTALL_PATH "/opt/protobuf_3.0.0_alpha4")
236+
237+
add_definitions(-DENABLE_GOBGP)
238+
add_library(gobgp_action STATIC actions/gobgp_action.cpp)
234239

235240
find_path(GOBGP_INCLUDES_FOLDER NAMES libgobgp.h PATHS "${GOBGP_CUSTOM_INSTALL_PATH}/include" NO_DEFAULT_PATH)
236241
find_library(GOBGP_LIBRARY_PATH NAMES gobgp PATHS "${GOBGP_CUSTOM_INSTALL_PATH}/lib" NO_DEFAULT_PATH)
237242

238-
set(GRPC_CUSTOM_INSTALL_PATH "/opt/grpc_0_11_1_7a94236d698477636dd06282f12f706cad527029")
243+
if (GOBGP_INCLUDES_FOLDER AND GOBGP_LIBRARY_PATH)
244+
message(STATUS "We found libgobgp and will link it: ${GOBGP_INCLUDES_FOLDER} ${GOBGP_LIBRARY_PATH}")
245+
target_link_libraries(gobgp_action ${GOBGP_LIBRARY_PATH})
246+
include_directories(${GOBGP_INCLUDES_FOLDER})
247+
else()
248+
message(FATAL_ERROR "Could not find libgobgp")
249+
endif()
239250

240251
find_path(GRPC_INCLUDES_FOLDER NAMES grpc/grpc.h PATHS "${GRPC_CUSTOM_INSTALL_PATH}/include" NO_DEFAULT_PATH)
241252
find_library(GRPC_LIBRARY_GRPC_PATH NAMES grpc PATHS "${GRPC_CUSTOM_INSTALL_PATH}/lib" NO_DEFAULT_PATH)
242253
find_library(GRPC_LIBRARY_GPR_PATH NAMES gpr PATHS "${GRPC_CUSTOM_INSTALL_PATH}/lib" NO_DEFAULT_PATH)
243-
244-
if (GRPC_INCLUDES_FOLDER AND GRPC_LIBRARY_GRPC_PATH AND GRPC_LIBRARY_GPR_PATH)
254+
find_library(GRPC_LIBRARY_GRPC_CPP_UNSECURE_PATH NAMES grpc++_unsecure PATHS "${GRPC_CUSTOM_INSTALL_PATH}/lib" NO_DEFAULT_PATH)
255+
256+
if (GRPC_INCLUDES_FOLDER AND GRPC_LIBRARY_GRPC_PATH AND GRPC_LIBRARY_GPR_PATH AND GRPC_LIBRARY_GRPC_CPP_UNSECURE_PATH)
245257
include_directories(${GRPC_INCLUDES_FOLDER})
258+
target_link_libraries(gobgp_action ${GRPC_LIBRARY_GRPC_PATH})
259+
target_link_libraries(gobgp_action ${GRPC_LIBRARY_GPR_PATH})
260+
target_link_libraries(gobgp_action ${GRPC_LIBRARY_GRPC_CPP_UNSECURE_PATH})
246261
else()
247-
message(STATUS "Could not find gRPC library")
262+
message(FATAL_ERROR "Could not find gRPC library")
248263
endif()
249264

250-
message(STATUS "grpc: ${GRPC_INCLUDES_FOLDER} ${GRPC_LIBRARY_GRPC_PATH} ${GRPC_LIBRARY_GPR_PATH}")
265+
find_path(PROTOCOL_BUFFERS_INCLUDE_FOLDER NAMES "google/protobuf/stubs/common.h" PATHS "${PROTOCOL_BUFFERS_CUSTOM_INSTALL_PATH}/include")
251266

252-
message(STATUS ${PROJECT_BINARY_DIR})
253-
set(PROTOC_BINARY "/opt/protobuf_3.0.0_alpha4/bin/protoc")
254-
#set(PROTOC_BINARY "protoc")
255-
set(GRPC_CPP_PLUGIN "${GRPC_CUSTOM_INSTALL_PATH}/bin/grpc_cpp_plugin")
267+
find_library(PROTOCOL_BUFFERS_LIBRARY_PATH NAMES protobuf PATHS "${PROTOCOL_BUFFERS_CUSTOM_INSTALL_PATH}/lib")
268+
269+
if (PROTOCOL_BUFFERS_INCLUDE_FOLDER AND PROTOCOL_BUFFERS_LIBRARY_PATH)
270+
include_directories(${PROTOCOL_BUFFERS_INCLUDE_FOLDER})
271+
target_link_libraries(gobgp_action ${PROTOCOL_BUFFERS_LIBRARY_PATH})
272+
else()
273+
message(FATAL_ERROR "Could not find protocol buffers")
274+
endif()
256275

257-
set(PROTOC_COMPILATION_COMMAND "${PROTOC_BINARY} -I ${PROJECT_BINARY_DIR}/../actions --grpc_out=${PROJECT_BINARY_DIR}/../actions --plugin=protoc-gen-grpc=${GRPC_CPP_PLUGIN} ${PROJECT_BINARY_DIR}/../actions/gobgp_api_client.proto")
258-
message(STATUS ${PROTOC_COMPILATION_COMMAND})
259276

260-
execute_process(COMMAND "${PROTOC_COMPILATION_COMMAND}" ERROR_VARIABLE PROTOC_STDERR RESULT_VARIABLE PROTOC_RETURN_CODE OUTPUT_STRIP_TRAILING_WHITESPACE)
277+
# message(STATUS "grpc: ${GRPC_INCLUDES_FOLDER} ${GRPC_LIBRARY_GRPC_PATH} ${GRPC_LIBRARY_GPR_PATH}")
278+
# message(STATUS ${PROJECT_BINARY_DIR})
261279

262-
message(STATUS "Protoc return code: ${PROTOC_RETURN_CODE} std err: ${PROTOC_STDERR}")
280+
find_program(PROTOC_BINARY protoc PATHS "${PROTOCOL_BUFFERS_CUSTOM_INSTALL_PATH}/bin" NO_DEFAULT_PATH)
263281

264-
if (GOBGP_INCLUDES_FOLDER AND GOBGP_LIBRARY_PATH)
265-
message(STATUS "We found libgobgp and will link it")
266-
include_directories(${GOBGP_INCLUDES_FOLDER})
267-
add_library(gobgp_action STATIC actions/gobgp_action.cpp)
282+
if (PROTOC_BINARY)
283+
message(STATUS "Found protoc protobuf compiler: ${PROTOC_BINARY}")
268284
else()
269-
message(STATUS "Could not find libgobgp")
285+
message(FATAL_ERROR "Can't find protoc compiler")
270286
endif()
287+
288+
set(GRPC_CPP_PLUGIN "${GRPC_CUSTOM_INSTALL_PATH}/bin/grpc_cpp_plugin")
289+
290+
execute_process(COMMAND ${PROTOC_BINARY} -I ${PROJECT_BINARY_DIR}/../actions --grpc_out=${PROJECT_BINARY_DIR}/../actions --plugin=protoc-gen-grpc=${GRPC_CPP_PLUGIN} ${PROJECT_BINARY_DIR}/../actions/gobgp_api_client.proto ERROR_VARIABLE PROTOC_STDERR RESULT_VARIABLE PROTOC_RETURN_CODE OUTPUT_STRIP_TRAILING_WHITESPACE)
291+
292+
message(STATUS "Protoc return code: ${PROTOC_RETURN_CODE}")
293+
294+
execute_process(COMMAND ${PROTOC_BINARY} -I ${PROJECT_BINARY_DIR}/../actions --cpp_out=${PROJECT_BINARY_DIR}/../actions ${PROJECT_BINARY_DIR}/../actions/gobgp_api_client.proto ERROR_VARIABLE PROTOC_STDERR RESULT_VARIABLE PROTOC_RETURN_CODE OUTPUT_STRIP_TRAILING_WHITESPACE)
295+
296+
message(STATUS "Protoc return code: ${PROTOC_RETURN_CODE} std err")
297+
298+
# Build gRPC and protocol bufffers libraries and link they to gobgp_action
299+
add_library(gobgp_api_client_pb_cc STATIC actions/gobgp_api_client.pb.cc)
300+
add_library(gobgp_api_client_grpc_pb_cc STATIC actions/gobgp_api_client.grpc.pb.cc)
301+
302+
target_link_libraries(gobgp_action gobgp_api_client_pb_cc)
303+
target_link_libraries(gobgp_action gobgp_api_client_grpc_pb_cc)
271304
endif()
272305

273306
# example plugin

‎src/actions/gobgp_action.cpp‎

Lines changed: 248 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,255 @@ extern "C" {
1313
#include <grpc++/create_channel.h>
1414
#include <grpc++/security/credentials.h>
1515

16+
#include "gobgp_api_client.grpc.pb.h"
17+
18+
using grpc::Channel;
19+
using grpc::ClientContext;
20+
using grpc::Status;
21+
22+
using gobgpapi::GobgpApi;
23+
24+
class GrpcClient {
25+
public:
26+
GrpcClient(std::shared_ptr<Channel> channel) : stub_(GobgpApi::NewStub(channel)) {}
27+
28+
void GetAllActiveAnnounces(unsigned int route_family) {
29+
ClientContext context;
30+
gobgpapi::Arguments arguments;
31+
32+
arguments.set_rf(route_family);
33+
// We could specify certain neighbor here
34+
arguments.set_name("");
35+
arguments.set_resource(gobgpapi::Resource::GLOBAL);
36+
37+
auto destinations_list = stub_->GetRib(&context, arguments);
38+
39+
gobgpapi::Destination current_destination;
40+
41+
std::cout << "List of announced prefixes for route family: " << route_family << std::endl << std::endl;
42+
while (destinations_list->Read(&current_destination)) {
43+
std::cout << "Prefix: " << current_destination.prefix() << std::endl;
44+
45+
//std::cout << "Paths size: " << current_destination.paths_size() << std::endl;
46+
47+
gobgpapi::Path my_path = current_destination.paths(0);
48+
49+
// std::cout << "Pattrs size: " << my_path.pattrs_size() << std::endl;
50+
51+
buf my_nlri;
52+
my_nlri.value = (char*)my_path.nlri().c_str();
53+
my_nlri.len = my_path.nlri().size();
54+
55+
path_t gobgp_lib_path;
56+
gobgp_lib_path.nlri = my_nlri;
57+
// Not used in library code!
58+
gobgp_lib_path.path_attributes_cap = 0;
59+
gobgp_lib_path.path_attributes_len = my_path.pattrs_size();
60+
61+
buf* my_path_attributes[ my_path.pattrs_size() ];
62+
for (int i = 0; i < my_path.pattrs_size(); i++) {
63+
my_path_attributes[i] = (buf*)malloc(sizeof(buf));
64+
my_path_attributes[i]->len = my_path.pattrs(i).size();
65+
my_path_attributes[i]->value = (char*)my_path.pattrs(i).c_str();
66+
}
67+
68+
gobgp_lib_path.path_attributes = my_path_attributes;
69+
70+
std::cout << "NLRI: " << decode_path(&gobgp_lib_path) << std::endl;
71+
}
72+
73+
Status status = destinations_list->Finish();
74+
if (!status.ok()) {
75+
// error_message
76+
std::cout << "Problem with RPC: " << status.error_code() << " message " << status.error_message() << std::endl;
77+
} else {
78+
// std::cout << "RPC working well" << std::endl;
79+
}
80+
}
81+
82+
void AnnounceFlowSpecPrefix() {
83+
const gobgpapi::ModPathArguments current_mod_path_arguments;
84+
85+
unsigned int AFI_IP = 1;
86+
unsigned int SAFI_FLOW_SPEC_UNICAST = 133;
87+
unsigned int ipv4_flow_spec_route_family = AFI_IP<<16 | SAFI_FLOW_SPEC_UNICAST;
88+
89+
gobgpapi::Path* current_path = new gobgpapi::Path;
90+
// If you want withdraw, please use it
91+
// current_path->set_is_withdraw(true);
92+
93+
/*
94+
buf:
95+
char *value;
96+
int len;
97+
path:
98+
buf nlri;
99+
buf** path_attributes;
100+
int path_attributes_len;
101+
int path_attributes_cap;
102+
*/
103+
104+
path* path_c_struct = serialize_path(ipv4_flow_spec_route_family, (char*)"match destination 10.0.0.0/24 protocol tcp source 20.0.0.0/24 then redirect 10:10");
105+
106+
// printf("Decoded NLRI output: %s, length %d raw string length: %d\n", decode_path(path_c_struct), path_c_struct->nlri.len, strlen(path_c_struct->nlri.value));
107+
108+
for (int path_attribute_number = 0; path_attribute_number < path_c_struct->path_attributes_len; path_attribute_number++) {
109+
current_path->add_pattrs(path_c_struct->path_attributes[path_attribute_number]->value,
110+
path_c_struct->path_attributes[path_attribute_number]->len);
111+
}
112+
113+
current_path->set_nlri(path_c_struct->nlri.value, path_c_struct->nlri.len);
114+
115+
gobgpapi::ModPathArguments request;
116+
request.set_resource(gobgpapi::Resource::GLOBAL);
117+
118+
google::protobuf::RepeatedPtrField< ::gobgpapi::Path >* current_path_list = request.mutable_paths();
119+
current_path_list->AddAllocated(current_path);
120+
request.set_name("");
121+
122+
ClientContext context;
123+
124+
gobgpapi::Error return_error;
125+
126+
// result is a std::unique_ptr<grpc::ClientWriter<gobgpapi::ModPathArguments> >
127+
auto send_stream = stub_->ModPath(&context, &return_error);
128+
129+
bool write_result = send_stream->Write(request);
130+
131+
if (!write_result) {
132+
std::cout << "Write to API failed\n";
133+
}
134+
135+
// Finish all writes
136+
send_stream->WritesDone();
137+
138+
auto status = send_stream->Finish();
139+
140+
if (status.ok()) {
141+
//std::cout << "modpath executed correctly" << std::cout;
142+
} else {
143+
std::cout << "modpath failed with code: " << status.error_code()
144+
<< " message " << status.error_message() << std::endl;
145+
}
146+
}
147+
148+
void AnnounceUnicastPrefix(std::string announced_prefix, std::string announced_prefix_nexthop) {
149+
const gobgpapi::ModPathArguments current_mod_path_arguments;
150+
151+
unsigned int AFI_IP = 1;
152+
unsigned int SAFI_UNICAST = 1;
153+
unsigned int ipv4_unicast_route_family = AFI_IP<<16 | SAFI_UNICAST;
154+
155+
gobgpapi::Path* current_path = new gobgpapi::Path;
156+
// If you want withdraw, please use it
157+
// current_path->set_is_withdraw(true);
158+
159+
/*
160+
buf:
161+
char *value;
162+
int len;
163+
path:
164+
buf nlri;
165+
buf** path_attributes;
166+
int path_attributes_len;
167+
int path_attributes_cap;
168+
*/
169+
170+
std::string announce_line = announced_prefix + " nexthop " + announced_prefix_nexthop;
171+
172+
path* path_c_struct = serialize_path(ipv4_unicast_route_family, (char*)announce_line.c_str());
173+
174+
if (path_c_struct == NULL) {
175+
std::cerr << "Could not generate path\n";
176+
exit(-1);
177+
}
178+
179+
// printf("Decoded NLRI output: %s, length %d raw string length: %d\n", decode_path(path_c_struct), path_c_struct->nlri.len, strlen(path_c_struct->nlri.value));
180+
181+
for (int path_attribute_number = 0; path_attribute_number < path_c_struct->path_attributes_len; path_attribute_number++) {
182+
current_path->add_pattrs(path_c_struct->path_attributes[path_attribute_number]->value,
183+
path_c_struct->path_attributes[path_attribute_number]->len);
184+
}
185+
186+
current_path->set_nlri(path_c_struct->nlri.value, path_c_struct->nlri.len);
187+
188+
gobgpapi::ModPathArguments request;
189+
request.set_resource(gobgpapi::Resource::GLOBAL);
190+
google::protobuf::RepeatedPtrField< ::gobgpapi::Path >* current_path_list = request.mutable_paths();
191+
current_path_list->AddAllocated(current_path);
192+
request.set_name("");
193+
194+
ClientContext context;
195+
196+
gobgpapi::Error return_error;
197+
198+
// result is a std::unique_ptr<grpc::ClientWriter<api::ModPathArguments> >
199+
auto send_stream = stub_->ModPath(&context, &return_error);
200+
201+
bool write_result = send_stream->Write(request);
202+
203+
if (!write_result) {
204+
std::cout << "Write to API failed\n";
205+
}
206+
207+
// Finish all writes
208+
send_stream->WritesDone();
209+
210+
auto status = send_stream->Finish();
211+
212+
if (status.ok()) {
213+
//std::cout << "modpath executed correctly" << std::cout;
214+
} else {
215+
std::cout << "modpath failed with code: " << status.error_code()
216+
<< " message " << status.error_message() << std::endl;
217+
}
218+
}
219+
220+
std::string GetAllNeighbor(std::string neighbor_ip) {
221+
gobgpapi::Arguments request;
222+
request.set_rf(4);
223+
request.set_name(neighbor_ip);
224+
225+
ClientContext context;
226+
227+
gobgpapi::Peer peer;
228+
grpc::Status status = stub_->GetNeighbor(&context, request, &peer);
229+
230+
if (status.ok()) {
231+
gobgpapi::PeerConf peer_conf = peer.conf();
232+
gobgpapi::PeerInfo peer_info = peer.info();
233+
234+
std::stringstream buffer;
235+
236+
buffer
237+
<< "Peer AS: " << peer_conf.remote_as() << "\n"
238+
<< "Peer router id: " << peer_conf.id() << "\n"
239+
<< "Peer flops: " << peer_info.flops() << "\n"
240+
<< "BGP state: " << peer_info.bgp_state();
241+
242+
return buffer.str();
243+
} else {
244+
return "Something wrong";
245+
}
246+
}
247+
248+
private:
249+
std::unique_ptr<GobgpApi::Stub> stub_;
250+
};
251+
252+
GrpcClient* gobgp_client = NULL;
253+
254+
void gobgp_action_init() {
255+
logger << log4cpp::Priority::INFO << "GoBGP action module loaded";
256+
gobgp_client = new GrpcClient(grpc::CreateChannel("localhost:8080", grpc::InsecureCredentials()));
257+
}
258+
259+
void gobgp_action_shutdown() {
260+
delete gobgp_client;
261+
}
262+
16263
void gobgp_ban_manage(std::string action, std::string ip_as_string, attack_details current_attack) {
264+
gobgp_client->AnnounceUnicastPrefix("10.10.20.33/32", "10.10.1.99");
17265
// std::string subnet_as_string_with_mask = convert_subnet_to_string(current_attack.customer_network);
18266
// std::string ip_as_string_with_mask = ip_as_string + "/32";
19267
}

‎src/actions/gobgp_action.h‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,8 @@
44
#include <string>
55
#include "../fastnetmon_types.h"
66

7+
void gobgp_action_init();
8+
void gobgp_action_shutdown();
79
void gobgp_ban_manage(std::string action, std::string ip_as_string, attack_details current_attack);
810

911
#endif

‎src/fastnetmon.conf‎

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -232,6 +232,10 @@ exabgp_announce_whole_subnet = off
232232
# Please use ExaBGP v4 only (Git version), for more details: https://github.com/FastVPSEestiOu/fastnetmon/blob/master/docs/BGP_FLOW_SPEC.md
233233
exabgp_flow_spec_announces = off
234234

235+
# It's not net finished option, please do not use it
236+
# GoBGP intergation
237+
gobgp = on
238+
235239
# Graphite monitoring
236240
# InfluxDB is also supported, please check our reference:
237241
# https://github.com/FastVPSEestiOu/fastnetmon/blob/master/docs/INFLUXDB_INTEGRATION.md

0 commit comments

Comments
 (0)