Skip to content
Snippets Groups Projects
Commit 517bbb88 authored by NGUYEN Do Duc Anh's avatar NGUYEN Do Duc Anh
Browse files

update inter node with ebpf + fix opp

parent 1b504a3b
Branches
No related tags found
No related merge requests found
......@@ -103,7 +103,13 @@ def modify_node_id(file_path, new_value):
file.writelines(lines)
def start_ebpf_on_interface(inter_num, b, fn):
def start_process_from_queue():
file_path = if_utils.DIR + '/ubuntu_base/op_ebpf.c' # Replace with your C file path
modify_node_id(file_path, if_utils.my_id)
b = BPF(src_file=f"op_ebpf.c")
fn = b.load_func("op_ebpf", BPF.XDP)
for inter_num in range(if_utils.MIN_INTER_NUMBER, if_utils.MAX_INTER_NUMBER):
inter_name = if_utils.PHYSICAL_INTERFACE[inter_num]
b.attach_xdp(inter_name, fn, 0)
print("Attach the ebpf program at", inter_name)
......@@ -123,20 +129,12 @@ def start_ebpf_on_interface(inter_num, b, fn):
time.sleep(1)
except KeyError:
continue
except KeyboardInterrupt:
except Exception:
print("Interrupted")
b.remove_xdp(inter_name, 0)
def start_process_from_queue():
file_path = if_utils.DIR + '/ubuntu_base/op_ebpf.c' # Replace with your C file path
modify_node_id(file_path, if_utils.my_id)
b = BPF(src_file=f"op_ebpf.c")
fn = b.load_func("op_ebpf", BPF.XDP)
for inter_num in range(if_utils.MIN_INTER_NUMBER, if_utils.MAX_INTER_NUMBER):
threading.Thread(target=start_ebpf_on_interface, args=[inter_num, b, fn]).start()
inter_name = if_utils.PHYSICAL_INTERFACE[inter_num]
b.remove_xdp(inter_name, 0)
if __name__ == "__main__":
......
# import os
# import sys
import subprocess
# import subprocess
# sys.path.append(os.path.abspath(os.path.join(os.path.dirname(__file__), '..')))
# from netfilterqueue import NetfilterQueue
# import ubuntu_base.opportunistic_utils import *
import ctypes
# import ctypes
import threading
from bcc import BPF
# from bcc.utils import printb
DIR = "/home/intermediate/Desktop/gns3_unikernel_testbed"
......@@ -31,20 +35,37 @@ DIR = "/home/intermediate/Desktop/gns3_unikernel_testbed"
def main():
rule_check = "sudo iptables -C FORWARD -i br0 -j NFQUEUE --queue-num 1"
try:
subprocess.run(rule_check, shell=True, check=True)
print("Rule already exists, no need to add it.")
except subprocess.CalledProcessError:
subprocess.run(f"sudo {DIR}/ubuntu_intermediate/./initiate.sh", shell=True, check=True)
print("./initiate is executed")
b = BPF(src_file=f"inter_op_ebpf.c")
fn = b.load_func("inter_op_ebpf", BPF.XDP)
for inter_num in range(3, 13):
inter_name = f"ens{inter_num + 1}"
b.attach_xdp(inter_name, fn, 0)
print("Attach the ebpf program at", inter_name)
try:
rule_1 = "sudo iptables -I FORWARD -i br0 -j NFQUEUE --queue-num 1"
subprocess.run(rule_1, shell=True, check=True)
print("Rule added successfully.")
except subprocess.CalledProcessError as e:
print(f"Failed to add the rule: {e}")
b.trace_print()
except Exception:
print("Interrupted")
for inter_num in range(3, 13):
inter_name = f"ens{inter_num + 1}"
b.remove_xdp(inter_name, 0)
# rule_check = "sudo iptables -C FORWARD -i br0 -j NFQUEUE --queue-num 1"
# try:
# subprocess.run(rule_check, shell=True, check=True)
# print("Rule already exists, no need to add it.")
# except subprocess.CalledProcessError:
# subprocess.run(f"sudo {DIR}/ubuntu_intermediate/./initiate.sh", shell=True, check=True)
# print("./initiate is executed")
# try:
# rule_1 = "sudo iptables -I FORWARD -i br0 -j NFQUEUE --queue-num 1"
# subprocess.run(rule_1, shell=True, check=True)
# print("Rule added successfully.")
# except subprocess.CalledProcessError as e:
# print(f"Failed to add the rule: {e}")
# q = NetfilterQueue()
# q.bind(1, process_packet) # Bind to queue number 1
......@@ -55,9 +76,9 @@ def main():
# except KeyboardInterrupt:
# print("Stopping packet listener")
lib = ctypes.CDLL(DIR + "/ubuntu_intermediate/./inter_c_library.so")
lib.start_process_from_queue.restype = None # The function returns void
threading.Thread(target=lib.start_process_from_queue).start()
# lib = ctypes.CDLL(DIR + "/ubuntu_intermediate/./inter_c_library.so")
# lib.start_process_from_queue.restype = None # The function returns void
# threading.Thread(target=lib.start_process_from_queue).start()
if __name__ == "__main__":
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment