Skip to content

Commit

Permalink
Merge pull request #108 from polycube-network/pr/improve_helloworld
Browse files Browse the repository at this point in the history
improve helloworld
  • Loading branch information
frisso committed Apr 12, 2019
2 parents 51b46f0 + b705585 commit 8af01d1
Show file tree
Hide file tree
Showing 3 changed files with 42 additions and 53 deletions.
54 changes: 20 additions & 34 deletions src/services/pcn-helloworld/src/Helloworld.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -30,11 +30,8 @@ Helloworld::Helloworld(const std::string name, const HelloworldJsonObject &conf)
logger()->info("Creating Helloworld instance");
setAction(conf.getAction());

// set an initial state before doing any change to the configuration
// UINT16_MAX means that the port is not connected
auto ports_map = get_array_table<uint16_t>("ports_map");
ports_map.set(0, UINT16_MAX);
ports_map.set(1, UINT16_MAX);
// initialize ports map (at this point there are not ports)
update_ports_map();

addPortsList(conf.getPorts());
}
Expand Down Expand Up @@ -64,41 +61,30 @@ void Helloworld::addPorts(const std::string &name,
throw std::runtime_error("maximum number of ports reached");
}

auto p = add_port<PortsJsonObject>(name, conf);
add_port<PortsJsonObject>(name, conf);
logger()->info("port {0} was connected", name);

auto ports_table = get_array_table<uint16_t>("ports_map");

uint32_t port_map_index;
try {
// Look for first free entry to save the port id
if (ports_table.get(0x0) == UINT16_MAX) {
port_map_index = 0x0;
} else if (ports_table.get(0x1) == UINT16_MAX) {
port_map_index = 0x1;
}
} catch (std::exception &e) {
logger()->error("port {0} does not exist", name);
// TODO: should Cube::remove_port be called?
throw std::runtime_error("Port does not exist");
}

ports_table.set(port_map_index, p->index());
update_ports_map();
}

void Helloworld::delPorts(const std::string &name) {
int index = get_port(name)->index();
remove_port(name);
logger()->info("port {0} was removed", name);
update_ports_map();
}

void Helloworld::update_ports_map() {
auto ports_table = get_array_table<uint16_t>("ports_map");
auto ports = get_ports();
uint32_t i = 0;

uint32_t port_map_index;
if (ports_table.get(0x0) == index) {
port_map_index = 0x0;
} else if (ports_table.get(0x1) == index) {
port_map_index = 0x1;
for (auto &port: ports) {
ports_table.set(i, port->index());
i++;
}

// mark other ports as empty (UINT16_MAX means empty)
while (i < 2) {
ports_table.set(i, UINT16_MAX);
i++;
}
// mark entry as free
ports_table.set(port_map_index, UINT16_MAX);
remove_port(name);
logger()->info("port {0} was removed", name);
}
3 changes: 3 additions & 0 deletions src/services/pcn-helloworld/src/Helloworld.h
Original file line number Diff line number Diff line change
Expand Up @@ -41,4 +41,7 @@ class Helloworld : public HelloworldBase {
/// </summary>
HelloworldActionEnum getAction() override;
void setAction(const HelloworldActionEnum &value) override;
private:
// saves the indexes in the ports maps used when action is forward
void update_ports_map();
};
38 changes: 19 additions & 19 deletions src/services/pcn-helloworld/src/Helloworld_dp_ingress.c
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,8 @@
#include <bcc/helpers.h>
#include <bcc/proto.h>

const uint16_t UINT16_MAX = 0xffff;

enum {
SLOWPATH_REASON = 1,
};
Expand Down Expand Up @@ -56,24 +58,15 @@ BPF_ARRAY(ports_map, uint16_t, 2);
static int handle_rx(struct CTXTYPE *ctx, struct pkt_metadata *md) {
pcn_log(ctx, LOG_DEBUG, "Receiving packet from port %d", md->in_port);
pcn_pkt_log(ctx, LOG_DEBUG);

unsigned int zero = 0;
unsigned int one = 1;

uint8_t *action = action_map.lookup(&zero);
if (!action) {
return RX_DROP;
}

// Get ports ids
uint16_t *p1 = ports_map.lookup(&zero);
if (!p1) {
return RX_DROP;
}

uint16_t *p2 = ports_map.lookup(&one);
if (!p2) {
return RX_DROP;
}

// what action should be performed in the packet?
switch (*action) {
case DROP:
Expand All @@ -82,17 +75,24 @@ static int handle_rx(struct CTXTYPE *ctx, struct pkt_metadata *md) {
case SLOWPATH:
pcn_log(ctx, LOG_DEBUG, "Sending packet to slow path");
return pcn_pkt_controller(ctx, md, SLOWPATH_REASON);
case FORWARD:
pcn_log(ctx, LOG_DEBUG, "Forwarding packet");
if (md->in_port == *p1)
return pcn_pkt_redirect(ctx, md, *p2);
else if (md->in_port == *p2)
return pcn_pkt_redirect(ctx, md, *p1);
else {
pcn_log(ctx, LOG_ERR, "bad in_port: %d", md->in_port);
case FORWARD: ;
// Get ports ids
uint16_t *p1 = ports_map.lookup(&zero);
if (!p1 || *p1 == UINT16_MAX) {
return RX_DROP;
}

uint16_t *p2 = ports_map.lookup(&one);
if (!p2 || *p2 == UINT16_MAX) {
return RX_DROP;
}

pcn_log(ctx, LOG_DEBUG, "Forwarding packet");

uint16_t outport = md->in_port == *p1 ? *p2 : *p1;
return pcn_pkt_redirect(ctx, md, outport);
default:
// if control plane is well implemented this will never happen
pcn_log(ctx, LOG_ERR, "bad action %d", *action);
return RX_DROP;
}
Expand Down

0 comments on commit 8af01d1

Please sign in to comment.