i have created a custom pox controller code i which i want to forward traffic to particular mac address but when i am using ping in mininet is is not working
from pox.core import core
import pox.openflow.libopenflow_01 as of
import codecs
import binascii
class Customc(object):
def __init__(self):
self.node_details = {} # Store node details here
self.best_node = None # Store the best node
core.openflow.addListeners(self)
self.read_node_details() # Call a method to read node details
def _handle_ConnectionUp(self, event):
self.best_node = self.find_best_node() # Call a method to find the best node
if self.best_node is not None:
self.install_flow(event.connection) # Install flow rules
'''def _handle_PacketIn(self, event):
self.log_best_node("scame into method")
if self.best_node is not None:
if event.port == 1 and event.parsed.find('eth_dst') and event.parsed.eth_dst == self.node_details[self.best_node]['mac']:
self.log_best_node(event.parsed.src)'''
'''def _handle_PacketIn(self, event):
packet = event.parsed # Get the incoming packet
# Find the best node to forward the packet to
best_node = self.find_best_node()
if best_node is not None:
# Determine the output port for the best node (assuming you have port information)
out_port = self.node_details[best_node]['port']
# Create and send a flow-mod message to forward the packet to the best node
msg = of.ofp_flow_mod()
msg.match.dl_dst = packet.dst # Set the destination MAC address
msg.actions.append(of.ofp_action_output(port=out_port))
event.connection.send(msg)'''
'''def _handle_PacketIn(self, event):
self.log_best_node("scame into method")
if self.best_node is not None:
mac_address = binascii.a2b_hex(self.node_details[self.best_node]['mac'].replace(":", ""))
self.log_best_node(mac_address)
msg = of.ofp_flow_mod()
msg.match.dl_dst = mac_address
msg.actions.append(of.ofp_action_output(port=1)) # Adjust the output port as needed
event.connection.send(msg)
self.log_best_node(event.parsed.src)'''
def read_node_details(self):
# Read and store node details from text files
# Assume node files are named node1.txt, node2.txt, ...
for node_id in range(1, 4): # Adjust range based on the number of nodes
filename = f'node{node_id}.txt'
with open(filename, 'r') as f:
details = f.read().strip() # Read details from the file
node_details = {}
for item in details.split(','):
key, value = item.split('=')
node_details[key] = value
self.node_details[node_id] = node_details
def find_best_node(self):
best_node = None
best_score = float('-inf') # Initialize with a low value
for node_id, node_details in self.node_details.items():
cpu_bandwidth = int(node_details['cpu_bandwidth'])
cpu_usage = float(node_details['cpu_usage'])
num_vehicles = int(node_details['num_vehicles'])
num_packets_dropped = int(node_details['num_packets_dropped'])
# Define a simple score formula (you can customize this)
score = cpu_bandwidth + (1 - cpu_usage) + num_vehicles - num_packets_dropped
if score > best_score:
best_node = node_id
best_score = score
return best_node
def install_flow(self, connection):
self.log_best_node("came into install flow")
if self.best_node is not None:
mac_address = binascii.a2b_hex(self.node_details[self.best_node]['mac'].replace(":", ""))
msg = of.ofp_flow_mod()
msg.match.dl_dst = (mac_address)
self.log_best_node(msg.match.dl_dst)
msg.priority = 10
msg.actions.append(of.ofp_action_output(port=1))
connection.send(msg)
def _handle_PacketIn(self, event):
self.log_best_node("came into install flow")
packet = event.parsed # Get the incoming packet
mac_address = binascii.unhexlify(self.node_details[self.best_node]['mac'].replace(":", ""))
msg = of.ofp_flow_mod()
msg.match.dl_dst = mac_address
msg.priority = 10
msg.actions.append(of.ofp_action_output(port=1))
event.connection.send(msg)
def log_best_node(self, src_address):
if self.best_node is not None:
with open('best_node_log.txt', 'a') as f:
f.write(f'Source address: {src_address}, Best node: {self.best_node}\n')
def launch():
core.registerNew(Customc)
and mininet code is
from mininet.net import Mininet
from mininet.node import RemoteController
from mininet.link import TCLink
from mininet.cli import CLI
def customTopology():
net = Mininet(controller=RemoteController, link=TCLink)
# Add controller
c0 = net.addController('c0', ip='127.0.0.1', port=6633)
# Add switches
s1 = net.addSwitch('s1')
s2 = net.addSwitch('s2')
s3 = net.addSwitch('s3')
# Add hosts
h1 = net.addHost('h1', ip='10.0.0.1', mac='00:00:00:00:00:01')
h2 = net.addHost('h2', ip='10.0.0.2', mac='00:00:00:00:00:02')
h3 = net.addHost('h3', ip='10.0.0.3', mac='00:00:00:00:00:03')
h4 = net.addHost('h4', ip='10.0.0.4', mac='00:00:00:00:00:04')
# Add links
net.addLink(h1, s1, intfName1='h1-eth0', port1=1)
net.addLink(h2, s1, intfName1='h2-eth0', port1=1)
net.addLink(h3, s2, intfName1='h3-eth0', port1=1)
net.addLink(h4, s3, intfName1='h4-eth0', port1=1)
net.addLink(s1, s2, intfName1='s1-s2-eth3', intfName2='s2-s1-eth1', port1=3, port2=1)
net.addLink(s2, s3, intfName1='s2-s3-eth2', intfName2='s3-s2-eth1', port1=2, port2=1)
return net
if __name__ == '__main__':
net = customTopology()
net.start()
# Find the port number of a host
#host_to_find = net.get('s1') # Replace 'h1' with the host you're looking for
#switch_connected_to = host_to_find.intfList()[0].link.intf1.node
#print(f"Port of {host_to_find.name} on {switch_connected_to.name}: {host_to_find.intfList()[0].link.intf1.name}")
CLI(net)
net.stop()
nodei.txt contains
mac=00:00:00:00:00:01,ip=10.0.0.1,cpu_bandwidth=1000,cpu_usage=50,num_vehicles=10,num_packets_dropped=0,port=1
i tried to ping but it is not forwarding packet to particular host and i am getting output as
mininet> pingall
*** Ping: testing ping reachability
h1 -> X X X
h2 -> X X X
h3 -> X X X
h4 -> X X X
*** Results: 100% dropped (0/12 received)
mininet> h1 ping h4
PING 10.0.0.4 (10.0.0.4) 56(84) bytes of data.
From 10.0.0.1 icmp_seq=1 Destination Host Unreachable
From 10.0.0.1 icmp_seq=2 Destination Host Unreachable
From 10.0.0.1 icmp_seq=3 Destination Host Unreachable
From 10.0.0.1 icmp_seq=4 Destination Host Unreachable
From 10.0.0.1 icmp_seq=5 Destination Host Unreachable
From 10.0.0.1 icmp_seq=6 Destination Host Unreachable