-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathWANSelector.java
79 lines (64 loc) · 2.14 KB
/
WANSelector.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
import java.util.*;
import maple.core.*;
public class WANSelector extends MapleFunction {
HashMap<Integer, String> ipToDomainName; // ip -> domain name mapping
MapleMap<String,Integer> policy; // app policy
Variable<Integer> broadbandQuality; // broadband quality, 0 or 1.
Counter c1 = newCounter("HIGH");
Counter c2 = newCounter("LOW");
Counter iperf1Counter = newCounter("H1_H5_port_5001");
Counter iperf2Counter = newCounter("H2_H6_port_5001");
// Constants
public final int UP_PORT = 1;
public final int ToS_GOOD = 4;
public WANSelector() {
// Setup IP->DNS mapping
ipToDomainName = new HashMap<Integer, String>();
ipToDomainName.put(IPv4.toIPv4Address("192.168.0.5"), "www.youtube.com");
ipToDomainName.put(IPv4.toIPv4Address("192.168.0.6"), "www.facebook.com");
policy = newMap("policy", String.class, Integer.class);
broadbandQuality = newVariable("broadbandQuality", 0, Integer.class);
}
@Override
protected Route onPacket(Packet p) {
List<Mod> mods = new LinkedList<Mod>();
if (p.ingressPort().portID != UP_PORT) {
if (desiredQuality(p) > broadbandQuality.read()) {
mods.add(Mod.setIPTypeOfService(ToS_GOOD));
c1.count();
} else {
c2.count();
}
}
monitorFlows(p);
SwitchPort dstLoc = hostLocation(p.ethDst());
if (null == dstLoc) {
return Route.multicast(null, edgePorts(), mods);
}
return Route.unicast(dstLoc, null, mods);
}
private void monitorFlows(Packet p) {
if (p.satisfies(
Assertion.ipSrcIn(IPv4.toIPv4Address("192.168.0.1"), 32),
Assertion.ipDstIn(IPv4.toIPv4Address("192.168.0.5"), 32),
Assertion.tcpDstEquals(5001))) {
iperf1Counter.count();
}
if (p.satisfies(
Assertion.ipSrcIn(IPv4.toIPv4Address("192.168.0.2"), 32),
Assertion.ipDstIn(IPv4.toIPv4Address("192.168.0.6"), 32),
Assertion.tcpDstEquals(5001))) {
iperf2Counter.count();
}
}
private int desiredQuality(Packet p) {
if (p.satisfies(Assertion.isIPv4())) {
String name = ipToDomainName.get(p.ipDst());
if (null == name) return 1;
Integer qual = policy.get(name);
if (null == qual) return 1;
return qual;
}
return 1;
}
}