Skip to content

Commit 19d8367

Browse files
committed
- fix crash when add multiple cubes to one interfaces
- add a CLI to check the cubes attaches to a interface Signed-off-by: Jianwen Pi<jianwpi@gmail.com>
1 parent 41c47cf commit 19d8367

6 files changed

Lines changed: 46 additions & 10 deletions

File tree

src/polycubed/src/extiface.cpp

Lines changed: 10 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ const std::string PARAMETER_MAC = "MAC";
3232
const std::string PARAMETER_IP = "IP";
3333
const std::string PARAMETER_PEER = "PEER";
3434

35-
std::set<std::string> ExtIface::used_ifaces;
35+
std::map<std::string, ExtIface*> ExtIface::used_ifaces;
3636

3737
ExtIface::ExtIface(const std::string &iface)
3838
: PeerIface(iface_mutex_),
@@ -43,7 +43,7 @@ ExtIface::ExtIface(const std::string &iface)
4343
throw std::runtime_error("Iface already in use");
4444
}
4545

46-
used_ifaces.insert(iface);
46+
used_ifaces.insert({iface, this});
4747

4848
// Save the ifindex
4949
ifindex_iface = if_nametoindex(iface.c_str());
@@ -60,6 +60,14 @@ ExtIface::~ExtIface() {
6060
used_ifaces.erase(iface_);
6161
}
6262

63+
ExtIface* ExtIface::get_extiface(const std::string &iface_name) {
64+
if (used_ifaces.count(iface_name) == 0) {
65+
return NULL;
66+
}
67+
68+
return used_ifaces[iface_name];
69+
}
70+
6371
uint16_t ExtIface::get_index() const {
6472
return index_;
6573
}
@@ -243,12 +251,6 @@ void ExtIface::update_indexes() {
243251
}
244252
}
245253

246-
// in external ifaces the cubes must be allocated in the inverse order.
247-
// first is the one that hits ingress traffic.
248-
int ExtIface::calculate_cube_index(int index) {
249-
return 0 - index;
250-
}
251-
252254
bool ExtIface::is_used() const {
253255
std::lock_guard<std::mutex> guard(iface_mutex_);
254256
return cubes_.size() > 0 || peer_ != nullptr;

src/polycubed/src/extiface.h

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,13 @@ class ExtIface : public PeerIface {
4141
ExtIface(const ExtIface &) = delete;
4242
ExtIface &operator=(const ExtIface &) = delete;
4343

44+
/* return ExtIface pointer given interface name
45+
* or return null if
46+
* - either the interface name doesn't exist
47+
* - or no extiface created for the interface name
48+
*/
49+
static ExtIface* get_extiface(const std::string &iface_name);
50+
4451
uint16_t get_index() const override;
4552
uint16_t get_port_id() const override;
4653
void set_peer_iface(PeerIface *peer) override;
@@ -71,7 +78,7 @@ class ExtIface : public PeerIface {
7178

7279
std::mutex event_mutex;
7380

74-
static std::set<std::string> used_ifaces;
81+
static std::map<std::string, ExtIface*> used_ifaces;
7582
int load_ingress();
7683
int load_egress();
7784
int load_tx();
@@ -82,7 +89,6 @@ class ExtIface : public PeerIface {
8289
virtual bpf_prog_type get_program_type() const = 0;
8390

8491
void update_indexes() override;
85-
int calculate_cube_index(int index) override;
8692

8793
ebpf::BPF ingress_program_;
8894
ebpf::BPF egress_program_;

src/polycubed/src/polycubed_core.cpp

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -208,6 +208,16 @@ std::string PolycubedCore::topology() {
208208
return j.dump(4);
209209
}
210210

211+
std::string PolycubedCore::get_if_topology(const std::string &if_name) {
212+
json j = {{"name", if_name}};
213+
214+
auto extIface = polycube::polycubed::ExtIface::get_extiface(if_name);
215+
if (extIface != NULL) {
216+
j["cubes"] = extIface->get_cubes_names();
217+
}
218+
return j.dump(4);
219+
}
220+
211221
std::string get_port_peer(const std::string &port) {
212222
std::smatch match;
213223
std::regex rule("(\\S+):(\\S+)");

src/polycubed/src/polycubed_core.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -64,6 +64,7 @@ class PolycubedCore {
6464
std::string get_netdevs();
6565

6666
std::string topology();
67+
std::string get_if_topology(const std::string &if_name);
6768

6869
void connect(const std::string &peer1, const std::string &peer2);
6970
void disconnect(const std::string &peer1, const std::string &peer2);

src/polycubed/src/rest_server.cpp

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -271,6 +271,8 @@ void RestServer::setup_routes() {
271271
// topology
272272
router_->get(base + std::string("/topology"),
273273
bind(&RestServer::topology, this));
274+
router_->get(base + std::string("/topology/:ifname"),
275+
bind(&RestServer::get_if_topology, this));
274276

275277
router_->options(base + std::string("/topology"),
276278
bind(&RestServer::topology_help, this));
@@ -830,6 +832,19 @@ void RestServer::topology(const Pistache::Rest::Request &request,
830832
}
831833
}
832834

835+
void RestServer::get_if_topology(const Pistache::Rest::Request &request,
836+
Pistache::Http::ResponseWriter response) {
837+
logRequest(request);
838+
try {
839+
auto ifname = request.param(":ifname").as<std::string>();
840+
std::string retJsonStr = core.get_if_topology(ifname);
841+
response.send(Pistache::Http::Code::Ok, retJsonStr);
842+
} catch (const std::runtime_error &e) {
843+
logger->error("{0}", e.what());
844+
response.send(Pistache::Http::Code::Bad_Request, e.what());
845+
}
846+
}
847+
833848
void RestServer::topology_help(const Pistache::Rest::Request &request,
834849
Pistache::Http::ResponseWriter response) {
835850
json j = json::object();

src/polycubed/src/rest_server.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -122,6 +122,8 @@ class RestServer {
122122
Pistache::Http::ResponseWriter response);
123123
void topology(const Pistache::Rest::Request &request,
124124
Pistache::Http::ResponseWriter response);
125+
void get_if_topology(const Pistache::Rest::Request &request,
126+
Pistache::Http::ResponseWriter response);
125127
void topology_help(const Pistache::Rest::Request &request,
126128
Pistache::Http::ResponseWriter response);
127129

0 commit comments

Comments
 (0)