Skip to content

Commit e5bde65

Browse files
authored
Merge pull request #24 from afmurillo/dev-paper
Pump speed working now
2 parents 6867435 + 6b0db31 commit e5bde65

File tree

4 files changed

+126
-12
lines changed

4 files changed

+126
-12
lines changed
Lines changed: 87 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,87 @@
1+
import argparse
2+
import os
3+
import signal
4+
import subprocess
5+
import sys
6+
from pathlib import Path
7+
8+
from automatic_node import NodeControl
9+
from py2_logger import get_logger
10+
11+
empty_loc = '/dev/null'
12+
13+
14+
class RouterControl(NodeControl):
15+
"""
16+
This class is started for a router. It starts a tcpdump process.
17+
"""
18+
19+
def __init__(self, intermediate_yaml, router_name):
20+
super(RouterControl, self).__init__(intermediate_yaml)
21+
22+
self.output_path = Path(self.data["output_path"])
23+
self.logger = get_logger(self.data['log_level'])
24+
self.process_tcp_dump = None
25+
self.router_name = router_name
26+
self.interface_name = router_name + '-eth0'
27+
28+
def terminate(self):
29+
"""
30+
This function stops the tcp dump and the plc process.
31+
"""
32+
33+
self.process_tcp_dump.send_signal(signal.SIGINT)
34+
self.process_tcp_dump.wait()
35+
if self.process_tcp_dump.poll() is None:
36+
self.process_tcp_dump.terminate()
37+
if self.process_tcp_dump.poll() is None:
38+
self.process_tcp_dump.kill()
39+
40+
self.logger.debug("Stopping Router tcpdump process.")
41+
42+
43+
def main(self):
44+
"""
45+
This function starts the tcp dump and plc process and then waits for the plc
46+
process to finish.
47+
"""
48+
self.process_tcp_dump = self.start_tcpdump_capture()
49+
50+
while self.process_tcp_dump.poll() is None:
51+
pass
52+
self.terminate()
53+
54+
def start_tcpdump_capture(self):
55+
"""
56+
Start a tcp dump.
57+
"""
58+
pcap = self.output_path / (self.interface_name + '.pcap')
59+
60+
# Output is not printed to console
61+
no_output = open(empty_loc, 'w')
62+
tcp_dump = subprocess.Popen(['tcpdump', '-i', self.interface_name, '-w',
63+
str(pcap)], shell=False, stderr=no_output, stdout=no_output)
64+
return tcp_dump
65+
66+
67+
def is_valid_file(parser_instance, arg):
68+
"""
69+
Verifies whether the intermediate yaml path is valid.
70+
"""
71+
if not os.path.exists(arg):
72+
parser_instance.error(arg + " does not exist.")
73+
else:
74+
return arg
75+
76+
77+
if __name__ == "__main__":
78+
parser = argparse.ArgumentParser(description='Start everything for a plc')
79+
parser.add_argument(dest="intermediate_yaml",
80+
help="intermediate yaml file", metavar="FILE",
81+
type=lambda x: is_valid_file(parser, x))
82+
parser.add_argument(dest="index", help="Index of PLC in intermediate yaml", type=int,
83+
metavar="N")
84+
85+
args = parser.parse_args()
86+
router_control = RouterControl(Path(args.intermediate_yaml))
87+
router_control.main()

dhalsim/python2/topo/complex_topo.py

Lines changed: 34 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,9 @@
22
from mininet.node import Node
33
from mininet.net import Mininet
44
import yaml
5+
from pathlib import Path
6+
import sys
7+
import signal
58

69

710
class Error(Exception):
@@ -27,6 +30,31 @@ def config(self, **params):
2730
def terminate(self):
2831
self.cmd('sysctl net.ipv4.ip_forward=0')
2932

33+
for router_process in self.router_processes:
34+
if router_process.poll() is None:
35+
try:
36+
self.end_process(router_process)
37+
except Exception as msg:
38+
self.logger.error("Exception shutting down router: " + str(msg))
39+
40+
41+
@staticmethod
42+
def end_process(process):
43+
"""
44+
End a process.
45+
46+
:param process: the process to end
47+
"""
48+
process.terminate()
49+
50+
if process.poll() is None:
51+
process.send_signal(signal.SIGINT)
52+
process.wait()
53+
if process.poll() is None:
54+
process.terminate()
55+
if process.poll() is None:
56+
process.kill()
57+
3058

3159
class ComplexTopo(Topo):
3260
"""
@@ -64,6 +92,8 @@ def __init__(self, intermediate_yaml_path):
6492
# Initialize mininet topology
6593
Topo.__init__(self)
6694

95+
self.router_processes = []
96+
6797
@staticmethod
6898
def check_amount_of_nodes(data):
6999
"""
@@ -280,13 +310,6 @@ def setup_network_for_node(self, net, node_data, provider_router):
280310
# Add provider router as default gateway to the plcs gateway router
281311
gateway.cmd('route add default gw {ip}'.format(ip=node_data['provider_ip']))
282312

283-
# TODO what do these do? they are not necessary yet
284-
# gateway.cmd(
285-
# 'iptables -A FORWARD -o {name}-eth1 -i {name}-eth0 -s {ip} -m conntrack --ctstate NEW -j ACCEPT'.format(
286-
# name=node_data['gateway_name'], ip=node_data['local_ip']))
287-
# gateway.cmd(
288-
# 'iptables -A FORWARD -m conntrack --ctstate ESTABLISHED,RELATED -j ACCEPT')
289-
290313
# Masquerade the from ip when forwarding a request from a plc to the provider
291314
gateway.cmd('iptables -t nat -F POSTROUTING')
292315
gateway.cmd('iptables -t nat -A POSTROUTING -o {name}-eth0 -j MASQUERADE'.format(
@@ -298,3 +321,7 @@ def setup_network_for_node(self, net, node_data, provider_router):
298321
ip=node_data['local_ip']))
299322
gateway.cmd('iptables -A FORWARD -p tcp -d {ip} --dport {port} -j ACCEPT'.format(
300323
ip=node_data['local_ip'], port=self.cpppo_port))
324+
325+
automatic_router_path = Path(__file__).parent.absolute() / "automatic_router.py"
326+
cmd = ["python2", str(automatic_router_path), str(self.intermediate_yaml), node_data['gateway_name']]
327+
self.router_processes.append(node.popen(cmd, stderr=sys.stderr, stdout=sys.stdout))

examples/anytown_topology/anytown_config.yaml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ iterations: 30
44
network_topology_type: complex
55
plcs: !include anytown_plcs.yaml
66

7-
simulator: wntr
7+
simulator: epynet
88
demand: pdd
99
demand_patterns: demands_anytown_small.csv
10-
attacks !include anytown_mitm.yaml
10+
#attacks !include anytown_mitm.yaml

examples/ctown_topology/ctown_map.inp

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1116,13 +1116,13 @@ LINK PU7 CLOSED IF NODE T4 ABOVE 4.5
11161116

11171117
; ----------- T5 ---
11181118
LINK PU8 OPEN IF NODE T5 BELOW 1.5
1119-
LINK PU8 CLOSED IF NODE T5 ABOVE 4.5
1119+
LINK PU8 CLOSED IF NODE T5 ABOVE 4.0
11201120

11211121
; ----------- T7 ---
1122-
LINK PU10 OPEN IF NODE T7 BELOW 2.5
1122+
LINK PU10 OPEN IF NODE T7 BELOW 1.5
11231123
LINK PU10 CLOSED IF NODE T7 ABOVE 4.8
11241124

1125-
LINK PU11 OPEN IF NODE T7 BELOW 1
1125+
LINK PU11 OPEN IF NODE T7 BELOW 0.5
11261126
LINK PU11 CLOSED IF NODE T7 ABOVE 3
11271127

11281128

0 commit comments

Comments
 (0)