Apply 802.1Q VLAN Tag by Using Ethernet Send and Receive Blocks
This example shows how to use Ethernet blocks to send and receive Ethernet packets on a target computer.
On the development computer, a UDP Send block sends a sample packet. On the target computer, this packet is received by an Ethernet Receive block, individual bytes in the payload are manipulated, and the resulting payload is sent out of the target computer by an Ethernet Send block.
The Ethernet blocks work only on the target computer.
These blocks can work in the default signal input/output mode and a message input/output mode. Both modes are shown in this example.
Set Up Ethernet Send-Receive Model
Open the target model
mdl1 = 'slrt_ex_ethernetSendReceive'; open_system(mdl1);
The target model requires a valid
Interface Name parameter value in the two Receive blocks and the two Send blocks. You can obtain this information on the target computer by using the QNX Neutrino RTOS
This example uses interface name
wm0 for the target IP address '192.168.7.5'.
Enter the interface name
wm0 into the four blocks in the model:
targetIface = 'wm0'; set_param('slrt_ex_ethernetSendReceive/Ethernet Receive', 'InterfaceName', targetIface) set_param('slrt_ex_ethernetSendReceive/Ethernet Receive1', 'InterfaceName', targetIface) set_param('slrt_ex_ethernetSendReceive/Ethernet Send', 'InterfaceName', targetIface) set_param('slrt_ex_ethernetSendReceive/Ethernet Send1', 'InterfaceName', targetIface)
Operations in the Ethernet Send-Receive Real-Time Application
Every packet sent from the development computer to the target computer is received by each Ethernet Receive block on the target.
The two Receive and corresponding Send blocks demonstrate the operation in signal mode and message mode, where Simulink messages represent the packets.
In signal mode, 'slrt_ex_ethernetSendReceive/Subsystem ' uses Simulink blocks to add a 802.1Q VLAN tag 32 and send it back to the host at a port incremented by 1. If the original sender port was 8001, the loopback destination port is 8002.
In the messages mode,
slrt_ex_ethernetSendReceive/MATLAB System Object receives the packets. For each packet it then creates three new packets with the VLAN tags 24, 25, and 26.
Manipulating the Ethernet Header
For details about Ethernet header structure, refer to the standards document for IEEE 802.1Q.
For details about the IPv4 header, refer to RFC 791 for Internet Protocol https://datatracker.ietf.org/doc/html/rfc791
These changes to the header occur in the
Subsystem and the
Switch source and destination MAC address: Swap bytes 1-6 with bytes 7-12.
Switch source and destination IP Address: Swap bytes 27-30 with bytes 31-34.
Switch source and destination port numbers: Swap bytes 35-36 with bytes 37-38.
Increment the new destination port number by 1: Add 1 to the value of byte 38.
Disable checksum verification: Set bytes 41-42 to 0. Without this change, the packets that are sent back to the development computer would be discarded, since checksum value would be incorrect. For details on checksum verification including recalculating new checksums, refer to RFC: 791 for Internet Protocol.
Add 802.1Q VLAN tag: IEEE 802.1Q adds a 4-byte VLAN tag between the Source/Destination MAC address and Length/Type fields of an Ethernet frame to identify the VLAN to which the frame belongs.
For the VLAN tags:
Make space for the VLAN tag by shifting bytes 13 onward to the right by 4 to the byte location starting at 17.
Insert the VLAN tag at byte locations 13-16. For example, to add tag 24, insert
0x81 0x00 0x00 0x18. Here the first 2 bytes correspond to a Tag protocol identifier (TPID), which is a 16-bit field set to a value of 0x8100 to identify the frame as an IEEE 802.1Q-tagged frame. The other 2 bytes set the Tag control information (TCI) to 0x0018, which includes the VLAN Identifier that corresponds to 24.
Open and Set Up Packet Source Model on Development Computer
slrt_ex_udpsend. Set the IP address.
developmentIP = '192.168.7.2'; mdl2 = 'slrt_ex_udpsend'; open_system(mdl2); set_param('slrt_ex_udpsend/UDP Receive1', 'ipAddress', developmentIP)
This model uses UDP blocks to send one packet out when the execution time is 2 seconds.
To IP address parameter on
slrt_ex_udpsend/SendPacketSubsystem/UDP Send to the IP address of the target computer.
targetIP = '192.168.7.5'; set_param('slrt_ex_udpsend/SendPacketSubsystem/UDP Send', 'toAddress', targetIP)
Run Ethernet Send-Receive Real-Time Application on Target Computer
Run the target model on the target using the Run on Target button. Or, in the MATLAB Command Window, type:
evalc('slbuild(mdl1)'); tg = slrealtime; load(tg,mdl1) start(tg)
Simulate the UDP Send Model on the Development Computer
Simulate the model.
set_param('slrt_ex_udpsend', 'SimulationCommand', 'start')
Open Simulink Data Inspector
Open the Simulation Data Inspector and observe the new packets created on the target computer. In the MATLAB Command Window, type:
The Simulation Data Inspector shows that four packets are received by the UDP Receive block through four instances of the data length changing to five (the UDP packet size).
Every simulation sends out just one packet at time = 2 seconds.
The Ethernet send-receive real-time application responds with four packets, which contain the same payload but with VLAN tags 24, 25, 26, and 32.
Windows does not expose the VLAN tags to applications. Due to this using a packet capture program such as Wireshark does not show VLAN tags.
The development computer model shows four UDP packets were received. These are UDP blocks, and Ethernet header information is not output.
For Windows systems that have connections that block the VLAN tag (such as VM Ethernet connections or a network interface between the development and target computers), these connections may prevent the packets from appearing on the output display.
One way to see the tags is by using QNX Neutrino RTOS
tcpdump command on the target computer while logged in as user
root by using password
root. Use the command:
tcpdump -i <targetIface> -v -e vlan
targetIface, use the interface name
wm0 from the Set Up Ethernet Send-Receive Model section.