Scenario: Virtual Control with VLAN
Requirements
Scenario
Successful setup of the "Virtual Playground" Scenario: vPLC + License Server + Edge Gateway
At least two instances of CODESYS Virtual Control for Linux SL (in this example,
vPLC1
andvPLC2
)
Hardware
A VLAN-capable switch
Fieldbus devices (Ethernet-based), for example Profinet
Configuring the switch
Important
If you configure your device incorrectly, then you may lose access to the device.
Connect the adapter (where the VLANs should be defined) to the switch. In this example, two VLAN interfaces are added to the following adapter:
enp1s0: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc mq state UP group default qlen 1000 link/ether f0:b2:b9:11:b8:b3 brd ff:ff:ff:ff:ff:ff inet 192.168.2.2/24 brd 192.168.2.255 scope global noprefixroute enp1s0 valid_lft forever preferred_lft forever inet6 fe80::2e96:178c:586a:b97b/64 scope link noprefixroute valid_lft forever preferred_lft forever
Open the configuration interface of the switch. For instructions about how to open the interface and the credentials, refer to the instructions of the vendor.
After you log in, open the configuration page for VLAN. If your switch supports different protocols, then select
802.1Q VLAN
.On the
Port Config
page, configure theLink Type
of the previously connected port as aTrunk
.Connect all fieldbus devices to the switch.
On the
VLAN Config
page, add two new VLANs. In this example, you are working with VLAN IDs 100 and 200. Add the previously connected port and configured as aTrunk
to each VLAN. Add the port of a fieldbus device to each VLAN.
In the following sample configuration, the connection from the controller to the switch was established on Port 14. The Link Type
of this port has been configured as a Trunk
. A fieldbus device is connected each to Port 11 and to Port 12.
VLAN ID | Description | Ports |
---|---|---|
100 | VLAN 100 | 11, 14 |
200 | VLAN 200 | 12, 14 |
Configuring the VLAN interfaces
Before you make permanent changes to your network configuration, you should test the settings and modify them to your needs.
Create a new adapter of type
VLAN
.The command has the following format:
sudo ip link add link <physical_adapter> name <virtual_adapter_name> type vlan id <vlan_id>
The convention is to append the VLAN ID to the name of the virtual adapter. In the case of the adapter and switch configuration shown above, the command looks like this:
sudo ip link add link enp1s0 name enp1s0.100 type vlan id 100
Replace
enp1s0
and the VLAN ID with the values on your system.Now when you run
ip a
, you should see something like the following output:enp1s0.100@enp1s0: <BROADCAST,MULTICAST,PROMISC> mtu 1500 qdisc noop state DOWN group default qlen 1000
Execute this command for the second VLAN as well.
Testing the configuration
Open the CODESYS Control SL Deploy Tool and establish a connection to your controller.
For more information, see: Establishing a Connection.
Switch to the Operation tab and select the
vPLC1
instance.In the configuration view on the right side, click the
Nic
line and on the right edge click thebutton.
The Nic configuration dialog opens.
In the list of available adapters, click
enp1s0.100
and select the Map to Instance option.If necessary, configure an IP address and network mask. The network mask has the format of the CIDR notation (example:
24
).For more information, see: https://en.wikipedia.org/wiki/Classless_Inter-Domain_Routing
Click the OK and Save buttons.
Select the
vPLC2
instance and perform Steps 3-6 with theenp1s0.200
adapter.Start the instances and a gateway on your controller.
For more information, see: Starting the Runtime System
Load a small application onto each of the two instances to check the functionality of the network. For more information, see: Downloading and Starting the CODESYS Application on the Controller
Persisting the adapter configuration
If your configuration is correct, you still need to save it persistently. For instructions about how to persistently save the configuration of your switch, see the instructions of the vendor. The configuration of the two instances vPLC1
and vPLC2
are already persistent. Here you only need to set the Autostart setting to Yes.
The following steps describe how to configure the two VLAN adapters on your controller so that they are available when the system is booted.
Important
These settings can be potentially harmful to your system.
Create a backup of all important files.
Make a copy of all files which are modified beforehand so that you can easily undo changes.
Open
/etc/network/interfaces
in an editor (example:sudo nano /etc/network/interfaces
).Add the following lines (modify the adapter names and VLAN IDs to your controller):
# enp1s0.100 vlan (vPLC1) auto enp1s0.100 iface enp1s0.100 inet dhcp vlan-raw-device enp1s0 # enp1s0.200 vlan (vPLC2) auto enp1s0.200 iface enp1s0.200 inet dhcp vlan-raw-device enp1s0
For more information about this file, see: https://wiki.debian.org/NetworkConfiguration
Restart your system or execute the command
sudo systemctl restart networking
for the changes to apply.
Note
This method of configuration was tested on Debian 12. You may need to select a different method for your device or operating system in order to save the configuration persistently.
Other examples are netplan (https://netplan.readthedocs.io/en/stable/structure-id/) or NetworkManager (https://networkmanager.dev/). The appropriate instructions for your controller can be found in the respective documentation.