-
Notifications
You must be signed in to change notification settings - Fork 2
/
app.py
executable file
·146 lines (132 loc) · 4.08 KB
/
app.py
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
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
import subprocess
import multiprocessing
import ast
import grpc
import p4_runtimes
import p4_read_topo
import p4_counter
import p4_read_table
import p4_write_table
from flask import Flask, render_template, request, jsonify
from flask_cors import CORS
app = Flask(__name__)
CORS(app, supports_credentials=True) # 全分离跨域
# 添加路由和视图函数
@app.route("/")
def index():
return render_template("index.html")
# 读拓扑结构
@app.route("/read_topo")
def read_topo():
try:
data = p4_read_topo.read_topo()
return jsonify(data)
except Exception as e:
return str(e)
# 流量监控
@app.route("/send_probe")
def send_probe():
command = "mx h1 python3 probe.py "
e_port = "2,2,2,2,3,3,3,3,1" # 探针路径
try:
process = subprocess.Popen(command + e_port, shell=True, text=True,
stdin=subprocess.PIPE, stdout=subprocess.PIPE)
process.wait(5)
output = process.communicate()[0]
# return output # 测试用
start = output.index("{")
end = output.rindex("}") + 1
result = output[start:end]
return jsonify(ast.literal_eval(result))
except subprocess.TimeoutExpired as e:
return "请求超时"
except Exception as e:
return str(e)
# 读取计数器
@app.route("/read_counter")
def read_counter():
try:
args = p4_runtimes.check_parser()
p4info_helper, sws = p4_runtimes.sw_connection(args.p4info)
data = p4_counter.read_counters(p4info_helper, sws)
flag = True
except grpc.RpcError as e:
p4_runtimes.printGrpcError(e)
flag = False
except Exception as e:
print(e)
flag = False
p4_runtimes.shut_down()
if flag:
return jsonify(data)
else:
return "查询计数器失败"
# 读取流表
@app.route("/read_table")
def read_tables():
try:
args = p4_runtimes.check_parser()
p4info_helper, sws = p4_runtimes.sw_connection(args.p4info)
data = p4_read_table.read_tables(p4info_helper, sws)
flag = True
except grpc.RpcError as e:
p4_runtimes.printGrpcError(e)
flag = False
except Exception as e:
print(e)
flag = False
p4_runtimes.shut_down()
if flag:
return jsonify(data)
else:
return "查询流表失败"
# 下发流表
@app.route("/write_table", methods=["POST"])
def write_tables():
form_args = request.get_json()
try:
args = p4_runtimes.check_parser()
p4info_helper, sws = p4_runtimes.sw_connection(args.p4info)
p4_write_table.sw_set_P4(p4info_helper, args.bmv2_json, sws)
p4_write_table.write_tables(p4info_helper, sws, form_args)
flag = True
except grpc.RpcError as e:
p4_runtimes.printGrpcError(e)
flag = False
except Exception as e:
print(e)
flag = False
p4_runtimes.shut_down()
if flag:
return "下发流表成功"
else:
return "下发流表失败"
# 创建子进程
def new_process(command):
process = subprocess.Popen(command, shell=True, text=True,
stdin=subprocess.PIPE,
stdout=subprocess.PIPE,
stderr=subprocess.STDOUT)
result = process.communicate()[0]
while process.poll() is None: # 进程未终止
line = process.stdout.readline()
result += line
return result
if __name__ == "__main__":
try:
# new_process("make clean")
# run_p4_proc = multiprocessing.Process(target=new_process("make run"))
# run_p4_proc.start()
# print("mininet启动完成")
# 修改文件权限
new_process("sudo chmod -R 777 .")
app.run(host="127.0.0.1", port=5555, debug=True, threaded=False)
except KeyboardInterrupt:
pass
except Exception as e:
print(e)
# run_p4_proc.terminate()
# print("mininet已停止")
# new_process("make stop")
# new_process("make clean")
# print("缓存文件清理完成")