Skip to content

Commit 8af01d1

Browse files
authored
Merge pull request #108 from polycube-network/pr/improve_helloworld
improve helloworld
2 parents 51b46f0 + b705585 commit 8af01d1

3 files changed

Lines changed: 42 additions & 53 deletions

File tree

src/services/pcn-helloworld/src/Helloworld.cpp

Lines changed: 20 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -30,11 +30,8 @@ Helloworld::Helloworld(const std::string name, const HelloworldJsonObject &conf)
3030
logger()->info("Creating Helloworld instance");
3131
setAction(conf.getAction());
3232

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

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

67-
auto p = add_port<PortsJsonObject>(name, conf);
64+
add_port<PortsJsonObject>(name, conf);
6865
logger()->info("port {0} was connected", name);
69-
70-
auto ports_table = get_array_table<uint16_t>("ports_map");
71-
72-
uint32_t port_map_index;
73-
try {
74-
// Look for first free entry to save the port id
75-
if (ports_table.get(0x0) == UINT16_MAX) {
76-
port_map_index = 0x0;
77-
} else if (ports_table.get(0x1) == UINT16_MAX) {
78-
port_map_index = 0x1;
79-
}
80-
} catch (std::exception &e) {
81-
logger()->error("port {0} does not exist", name);
82-
// TODO: should Cube::remove_port be called?
83-
throw std::runtime_error("Port does not exist");
84-
}
85-
86-
ports_table.set(port_map_index, p->index());
66+
update_ports_map();
8767
}
8868

8969
void Helloworld::delPorts(const std::string &name) {
90-
int index = get_port(name)->index();
70+
remove_port(name);
71+
logger()->info("port {0} was removed", name);
72+
update_ports_map();
73+
}
9174

75+
void Helloworld::update_ports_map() {
9276
auto ports_table = get_array_table<uint16_t>("ports_map");
77+
auto ports = get_ports();
78+
uint32_t i = 0;
9379

94-
uint32_t port_map_index;
95-
if (ports_table.get(0x0) == index) {
96-
port_map_index = 0x0;
97-
} else if (ports_table.get(0x1) == index) {
98-
port_map_index = 0x1;
80+
for (auto &port: ports) {
81+
ports_table.set(i, port->index());
82+
i++;
83+
}
84+
85+
// mark other ports as empty (UINT16_MAX means empty)
86+
while (i < 2) {
87+
ports_table.set(i, UINT16_MAX);
88+
i++;
9989
}
100-
// mark entry as free
101-
ports_table.set(port_map_index, UINT16_MAX);
102-
remove_port(name);
103-
logger()->info("port {0} was removed", name);
10490
}

src/services/pcn-helloworld/src/Helloworld.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,4 +41,7 @@ class Helloworld : public HelloworldBase {
4141
/// </summary>
4242
HelloworldActionEnum getAction() override;
4343
void setAction(const HelloworldActionEnum &value) override;
44+
private:
45+
// saves the indexes in the ports maps used when action is forward
46+
void update_ports_map();
4447
};

src/services/pcn-helloworld/src/Helloworld_dp_ingress.c

Lines changed: 19 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,8 @@
2323
#include <bcc/helpers.h>
2424
#include <bcc/proto.h>
2525

26+
const uint16_t UINT16_MAX = 0xffff;
27+
2628
enum {
2729
SLOWPATH_REASON = 1,
2830
};
@@ -56,24 +58,15 @@ BPF_ARRAY(ports_map, uint16_t, 2);
5658
static int handle_rx(struct CTXTYPE *ctx, struct pkt_metadata *md) {
5759
pcn_log(ctx, LOG_DEBUG, "Receiving packet from port %d", md->in_port);
5860
pcn_pkt_log(ctx, LOG_DEBUG);
61+
5962
unsigned int zero = 0;
6063
unsigned int one = 1;
64+
6165
uint8_t *action = action_map.lookup(&zero);
6266
if (!action) {
6367
return RX_DROP;
6468
}
6569

66-
// Get ports ids
67-
uint16_t *p1 = ports_map.lookup(&zero);
68-
if (!p1) {
69-
return RX_DROP;
70-
}
71-
72-
uint16_t *p2 = ports_map.lookup(&one);
73-
if (!p2) {
74-
return RX_DROP;
75-
}
76-
7770
// what action should be performed in the packet?
7871
switch (*action) {
7972
case DROP:
@@ -82,17 +75,24 @@ static int handle_rx(struct CTXTYPE *ctx, struct pkt_metadata *md) {
8275
case SLOWPATH:
8376
pcn_log(ctx, LOG_DEBUG, "Sending packet to slow path");
8477
return pcn_pkt_controller(ctx, md, SLOWPATH_REASON);
85-
case FORWARD:
86-
pcn_log(ctx, LOG_DEBUG, "Forwarding packet");
87-
if (md->in_port == *p1)
88-
return pcn_pkt_redirect(ctx, md, *p2);
89-
else if (md->in_port == *p2)
90-
return pcn_pkt_redirect(ctx, md, *p1);
91-
else {
92-
pcn_log(ctx, LOG_ERR, "bad in_port: %d", md->in_port);
78+
case FORWARD: ;
79+
// Get ports ids
80+
uint16_t *p1 = ports_map.lookup(&zero);
81+
if (!p1 || *p1 == UINT16_MAX) {
82+
return RX_DROP;
83+
}
84+
85+
uint16_t *p2 = ports_map.lookup(&one);
86+
if (!p2 || *p2 == UINT16_MAX) {
9387
return RX_DROP;
9488
}
89+
90+
pcn_log(ctx, LOG_DEBUG, "Forwarding packet");
91+
92+
uint16_t outport = md->in_port == *p1 ? *p2 : *p1;
93+
return pcn_pkt_redirect(ctx, md, outport);
9594
default:
95+
// if control plane is well implemented this will never happen
9696
pcn_log(ctx, LOG_ERR, "bad action %d", *action);
9797
return RX_DROP;
9898
}

0 commit comments

Comments
 (0)