Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

click/etherswitch: Forward traffic to a set of ports #334

Open
wants to merge 4 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
110 changes: 93 additions & 17 deletions elements/etherswitch/etherswitch.cc
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,8 @@
#include <click/args.hh>
#include <click/straccum.hh>
#include <click/error.hh>
#include <click/confparse.hh>
#include <click/string.hh>
CLICK_DECLS

EtherSwitch::EtherSwitch()
Expand All @@ -38,24 +40,69 @@ EtherSwitch::~EtherSwitch()
int
EtherSwitch::configure(Vector<String> &conf, ErrorHandler *errh)
{
return Args(conf, this, errh)
if (Args(conf, this, errh)
.read("TIMEOUT", SecondsArg(), _timeout)
.complete();
.complete() < 0)
return -1;

int n = noutputs();
_pfrs.resize(n);
for (int i = 0; i < n; i++)
_pfrs[i].configure(i, n);
return 0;
}

void
EtherSwitch::broadcast(int source, Packet *p)
{
int n = noutputs();
assert((unsigned) source < (unsigned) n);
int sent = 0;
for (int i = n - 1; i >=0 ; i--)
if (i != source) {
Packet *pp = (sent < n - 2 ? p->clone() : p);
PortForwardRule &pfr = _pfrs[source];
int n = pfr.bv.size();
int w = pfr.w;
assert((unsigned) w <= (unsigned) n);
for (int i = 0; i < n && w > 0; i++) {
if (pfr.bv[i]) {
Packet *pp = (w > 1 ? p->clone() : p);
output(i).push(pp);
sent++;
w--;
}
assert(sent == n - 1);
}
}

int
EtherSwitch::remove_port_forwarding(String portmaps, ErrorHandler *errh)
{
int nn = noutputs();
Vector<PortForwardRule> new_pfrs(_pfrs);
Vector<String> maps_vec;
cp_argvec(portmaps, maps_vec);
Vector<String>::const_iterator i,n;
for (i = maps_vec.begin(), n = maps_vec.end(); i != n; ++i) {
int source, outport;
Args args = Args(this,errh).push_back_words(*i);
if (args.read_mp("SOURCE", source).consume() < 0)
return -1;
if (source < 0 || source > nn)
return -1;
int new_pfr_n = (new_pfrs[source].bv).size();
while (!args.empty()) {
if (args.read_p("OUTPORT", outport).consume() < 0)
return -1;
if (outport < 0 || outport > new_pfr_n)
return -1;
(new_pfrs[source].bv)[outport] = false;
}
new_pfrs[source].calculate_weight();
}
_pfrs = new_pfrs;
return 0;
}

void
EtherSwitch::reset_port_forwarding()
{
int n = noutputs();
for (int i = 0; i < n; i++)
_pfrs[i].configure(i, n);
}

void
Expand Down Expand Up @@ -83,10 +130,10 @@ EtherSwitch::push(int source, Packet *p)

if (outport < 0)
broadcast(source, p);
else if (outport == source) // Don't send back out on same interface
p->kill();
else // forward
output(outport).push(p);
else if ((_pfrs[source].bv)[outport]) // forward w/ filter
output(outport).push(p);
else
p->kill();
}

String
Expand All @@ -102,17 +149,43 @@ EtherSwitch::reader(Element* f, void *thunk)
}
case 1:
return String(sw->_timeout);
case 2: {
StringAccum sa;
int n = sw->noutputs();
for (int i = 0; i < n; i++)
sa << "weight: " << (sw->_pfrs[i].w) << "\t"
<< i << ": " << (sw->_pfrs[i].bv).unparse() << '\n';
return sa.take_string();
}
default:
return String();
}
}

int
EtherSwitch::writer(const String &s, Element *e, void *, ErrorHandler *errh)
EtherSwitch::writer(const String &s, Element *e, void *thunk, ErrorHandler *errh)
{
EtherSwitch *sw = (EtherSwitch *) e;
if (!SecondsArg().parse_saturating(s, sw->_timeout))
return errh->error("expected timeout (integer)");
switch((intptr_t) thunk) {
case 0: {
if (!SecondsArg().parse_saturating(s, sw->_timeout)) {
return errh->error("expected timeout (integer)");
}
break;
}
case 1: {
if (sw->remove_port_forwarding(s, errh) < 0) {
return errh->error("invalid port forwarding");
}
break;
}
case 2: {
sw->reset_port_forwarding();
break;
}
default:
return errh->error("bad thunk");
}
return 0;
}

Expand All @@ -121,7 +194,10 @@ EtherSwitch::add_handlers()
{
add_read_handler("table", reader, 0);
add_read_handler("timeout", reader, 1);
add_read_handler("port_forwarding", reader, 2);
add_write_handler("timeout", writer, 0);
add_write_handler("remove_port_forwarding", writer, 1);
add_write_handler("reset_port_forwarding", writer, 2);
}

EXPORT_ELEMENT(EtherSwitch)
Expand Down
37 changes: 37 additions & 0 deletions elements/etherswitch/etherswitch.hh
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@
#include <click/element.hh>
#include <click/etheraddress.hh>
#include <click/hashtable.hh>
#include <click/bitvector.hh>
#include <click/vector.hh>
CLICK_DECLS

/*
Expand Down Expand Up @@ -54,6 +56,23 @@ Returns the current port association table.

Returns or sets the TIMEOUT argument.

=e

from_port0, from_port1 :: FromDevice...;
to_port0, to_port1 :: ToDevice...;

switch :: EtherSwitch;

q0 :: Queue -> to_port0;
q1 :: Queue -> to_port1;

from_port0 -> [0] switch [0] -> q0;
from_port1 -> [1] switch [1] -> q1;

---

echo "0, 1" > /click/switch/remove_port_forwarding

=a

ListenEtherSwitch, EtherSpanTree
Expand Down Expand Up @@ -85,8 +104,26 @@ class EtherSwitch : public Element { public:
typedef HashTable<EtherAddress, AddrInfo> Table;
Table _table;
uint32_t _timeout;
struct PortForwardRule {
Bitvector bv; /* Each bit is a port used in determining forwarding to of packets */
int w; /* Sum of bv */
void calculate_weight() {
w = bv.weight();
}
void configure(int i, int n) {
bv.resize(n);
for (int j = 0; j < n; j++)
bv[j] = true;
assert((unsigned) i < (unsigned) n);
bv[i] = false;
calculate_weight();
}
};
Vector<PortForwardRule> _pfrs;

void broadcast(int source, Packet*);
int remove_port_forwarding(String portmaps, ErrorHandler *errh);
void reset_port_forwarding();

static String reader(Element *, void *);
static int writer(const String &, Element *, void *, ErrorHandler *);
Expand Down
40 changes: 40 additions & 0 deletions elements/test/bitvectortest.cc
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,46 @@ BitvectorTest::initialize(ErrorHandler *errh)
bv.resize(0);
CHECK(bv.words()[0] == 0);

CHECK(!bv.parse("3", 0, 5, -3));
CHECK(!bv.parse("3", 5, 3));

CHECK(!bv.parse("-", 0, 5));
CHECK(!bv.parse("--2", 0, 5));
CHECK(!bv.parse("2-", 0, 5));
CHECK(!bv.parse("2--3", -5, 5));
CHECK(!bv.parse("3", 0, 2));
CHECK(!bv.parse("1", 3, 5));
CHECK(!bv.parse("1-4", 3, 5));
CHECK(!bv.parse("4-6", 3, 5));

CHECK(bv.parse("1,3,5", 0, 6));
CHECK(bv.size() == 7 && !bv[0] && bv[1] && !bv[2] && bv[3] && !bv[4] && bv[5] && !bv[6]);

CHECK(bv.parse("", 1, 3));
CHECK(bv.size() == 3 && !bv[0] && !bv[1] && !bv[2]);

CHECK(bv.parse("-1,1,3", -1, 3));
CHECK(bv.size() == 5 && bv[0] && !bv[1] && bv[2] && !bv[3] && bv[4]);

CHECK(bv.parse("-1-0,2-3,5-6", -2, 7));
CHECK(bv.size() == 10 && !bv[0] && bv[1] && bv[2] && !bv[3] && bv[4] && bv[5] && !bv[6] && bv[7] && bv[8] && !bv[9]);

CHECK(bv.parse("-7--6,-4--3,-1-0", -7, 0));
CHECK(bv.size() == 8 && bv[0] && bv[1] && !bv[2] && bv[3] && bv[4] && !bv[5] && bv[6] && bv[7]);

bv.assign(6, true);
bv[2] = bv[4] = false;
CHECK(bv.unparse() == "0-1,3,5");
CHECK(bv.unparse(0) == "0-1,3,5");
CHECK(bv.unparse(-1) == "1-2,4,6");
CHECK(bv.unparse(1) == "0,2,4");
CHECK(bv.unparse(8) == "");
CHECK(bv.unparse(0, 1) == "1-2,4,6");
CHECK(bv.unparse(0, -1) == "-1-0,2,4");
CHECK(bv.unparse(1, 1) == "1,3,5");
bv[4] = true;
CHECK(bv.unparse() == "0-1,3-5");

errh->message("All tests pass!");
return 0;
}
Expand Down
19 changes: 19 additions & 0 deletions include/click/bitvector.hh
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
// -*- c-basic-offset: 4; related-file-name: "../../lib/bitvector.cc" -*-
#ifndef CLICK_BITVECTOR_HH
#define CLICK_BITVECTOR_HH
#include <click/string.hh>
#include <click/glue.hh>
CLICK_DECLS

Expand Down Expand Up @@ -73,13 +74,22 @@ class Bitvector {

void swap(Bitvector &x);

inline int weight();

bool parse(const String &str, int min_val, int max_val, int offset = 0);
String unparse(int read_offset = 0, int output_offset = 0) const;

/** @cond never */
typedef word_type data_word_type CLICK_DEPRECATED;
enum { data_word_bits = wbits };
inline word_type *data_words() CLICK_DEPRECATED;
inline const word_type *data_words() const CLICK_DEPRECATED;
/** @endcond never */

void print();

static Bitvector from_mask(unsigned long mask);

private:

enum { ninline = 2, inlinebits = ninline * wbits };
Expand Down Expand Up @@ -375,5 +385,14 @@ inline Bitvector::Bit &Bitvector::Bit::operator-=(bool x) {
return *this;
}

/** @brief Return the number of true bits */
inline int Bitvector::weight() {
int w = 0;
for (int i = 0; i < size(); i++)
if ((*this)[i])
w++;
return w;
}

CLICK_ENDDECLS
#endif
10 changes: 10 additions & 0 deletions include/click/confparse.hh
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,16 @@ String cp_shift_spacevec(String &str);

String cp_unspacevec(const String *begin, const String *end);
inline String cp_unspacevec(const Vector<String> &conf);

/// @brief Remove and return the first delimiter-separated argument from @a str.
/// @param[in,out] str delimiter-separated string
/// @param[in] delim delimiter
///
/// All characters up to the first delimiter character are removed and
/// returned. @a str is set to the remaining portion of the string,
/// with up to one delimiter character removed.
String cp_shift_delimiter(String &str, char delim);

//@}

/// @name Direct Parsing Functions
Expand Down
Loading