Omnet++ 5.5.1交换机配置:如何实现向特定节点定向发包而非广播
Hey there! Let’s fix that broadcast behavior and get your switch directing traffic only to specific nodes. I’ll assume you’re using the INET framework (the standard for network simulations in OMNeT++ 5.5.1)—if you’re working with a custom switch module, just let me know and I can adjust these steps.
Step 1: Set Up a Static MAC Address Table
By default, Ethernet switches broadcast packets when they don’t know the destination MAC’s port. To override this, define a static MAC-to-port mapping in your simulation’s .ini file. This tells the switch exactly where to send traffic for specific nodes.
Add this to your .ini configuration:
# Replace switch[*] with your switch module name if it's different **.switch[*].macTable = { "00:00:00:00:00:01" : "port1", # Map node1's MAC to switch port 1 "00:00:00:00:00:02" : "port2" # Map node2's MAC to switch port 2 }
Make sure the MAC addresses match the ones assigned to your target nodes (you can set these in the .ini too, see Step 3).
Step 2: Disable Dynamic Learning (Optional but Recommended)
If your switch is set to dynamically learn MAC addresses, it might add new entries over time that could cause unintended broadcasts. To lock it to your static table:
**.switch[*].learning = false # Turn off auto-learning of new MACs **.switch[*].agingTime = 0s # Prevent static entries from expiring
Step 3: Assign Fixed MAC Addresses to Target Nodes
To ensure your static mapping works, explicitly set MAC addresses for the nodes you want to target:
**.node1.eth[0].macAddress = "00:00:00:00:00:01" **.node2.eth[0].macAddress = "00:00:00:00:00:02"
Step 4: Verify the Behavior
To confirm the switch isn’t broadcasting anymore, enable packet tracing in your .ini file:
**.packetDumper.dump = true
Run the simulation and check the trace output—you should see packets only going to the specified ports for your target nodes, not being sent to all ports.
For Custom Switch Modules
If you’re using a custom switch implementation instead of INET’s EthernetSwitch, you’ll need to modify the forwarding logic in your C++ code. Here’s a simplified example of how to direct packets to specific ports:
void MyCustomSwitch::handleMessage(cMessage *msg) { EthernetFrame *frame = check_and_cast<EthernetFrame *>(msg); MACAddress destAddr = frame->getDest(); // Look up the port for the destination MAC std::string targetPort = getPortFromStaticTable(destAddr); if (!targetPort.empty()) { // Send only to the target port send(frame, targetPort.c_str()); } else { // Optional: Drop the packet or broadcast if needed // broadcast(frame); // Remove this line to disable fallback broadcast delete frame; } }
Just implement getPortFromStaticTable() to return the correct port for your target MACs.
内容的提问来源于stack exchange,提问作者Bhargav Kanumuri

