Method for quickly taking effect of shared IP of double-CPU redundant PLC after master-slave switching
Technical Field
The invention relates to a method for quickly taking effect of a shared IP of a double-CPU redundant PLC after master-slave switching, belonging to the technical field of industrial control of the PLC.
Background
In the application of most PLC, PLC all can external touch-sensitive screen, and when PLC's upper computer system took place unusually, fortune dimension personnel can observe key operation data through the touch-sensitive screen to when PLC has the unusual emergence, in time assign control command to PLC through the touch-sensitive screen, avoid the unexpected condition to take place. The early touch screen and the CPU card of the PLC are communicated by adopting RS232 or RS485 interfaces, but most of the PLCs and the touch screen in the market are communicated by adopting network interfaces at present.
According to the running logic of the master CPU and the slave CPU of the PLC, the slave CPU cannot respond to an external control command. Therefore, the touch screen is communicated with the main CPU, and the PLC can be issued control commands. Therefore, after the master and slave CPUs of the PLC are switched, the touch screen needs to disconnect the communication with the CPU which was originally the master and establish a communication connection with the CPU which was originally the slave and was now the master. Most touch screens in the market only support communication with 1 IP, and in an early system, after master-slave switching of the PLC occurs, the communication connection IP of the touch screen needs to be manually reconfigured, and the IP is set as the IP address of the CPU which is currently the master. However, the method is inconvenient for system operation and maintenance management, the system cannot be operated fully automatically, and human errors are easy to occur.
The current mainstream solution is that a master CPU and a slave CPU share one IP, the IP is used for communicating with a touch screen, only the master CPU has the IP under any condition, after the master CPU and the slave CPU are switched, the originally master CPU unloads the currently bound shared IP from a network card of the master CPU, and the originally slave CPU binds the shared IP to the network card of the slave CPU, so that the problems of manual operation switching and inconvenient operation and maintenance are avoided. The operating system generally applied to the PLC includes a command for binding or uninstalling an IP corresponding to the network card, for example, a linux operating system, and the function of uninstalling/binding an IP address can be achieved by calling a system command ifconfig related to network card configuration.
However, after the original slave CPU is upgraded to be the master and executes the command of binding the shared IP to the network card of the touch screen, the touch screen cannot establish communication connection with the CPU immediately. It takes about tens of seconds to about one minute for the communication to return to normal, i.e. the shared IP cannot be immediately effective after the PLC master-slave switch. If the object monitored by the PLC is abnormal in the period of time, the control command cannot be issued through the touch screen for processing, so that the whole system can be fatally influenced.
Therefore, the technical problem that the external touch screen of the PLC cannot establish communication connection with the CPU immediately after switching between the master CPU and the slave CPU is urgently needed to be solved by those skilled in the art.
Disclosure of Invention
The purpose is as follows: in order to solve the problem that the shared IP cannot be immediately effective after master-slave switching of the PLC in the prior art, the invention provides a method for quickly taking effect of the shared IP of a double-CPU redundant PLC after the master-slave switching.
The technical scheme is as follows: in order to solve the technical problems, the technical scheme adopted by the invention is as follows:
a method for quickly taking effect of a shared IP of a dual-CPU redundant PLC after master-slave switching comprises the following steps:
and the main CPU of the PLC inquires ARP information of the shared IP of the main CPU at intervals of t, and acquires the IP and the MAC address of the network segment equipment where the shared IP is positioned from the ARP information.
The main CPU transmits the IP and MAC addresses of the network segment devices where the shared IP is located to the slave CPU, and the slave CPU receives the IP and MAC addresses and stores the IP and MAC addresses in a memory of the slave CPU.
When the PLC is switched between the master and the slave of the CPU, the master CPU after the switching binds the network card and the shared IP; and the switched slave CPU unloads the shared IP from the network card of the slave CPU.
And the switched main CPU creates a link layer original socket comprising an ARP simulation response message through a socket function according to the IP and the MAC address of the network segment equipment in which the shared IP is positioned in the memory, and the link layer original socket submits the link layer original socket to a network card driving layer of the local machine through a sendto function and is sent to the equipment by the network card.
After the equipment receives the link layer original socket, the own IP protocol stack compares the destination MAC address in the link layer original socket, whether the destination IP address is the same as the own MAC address and IP address or not is judged, if so, the source MAC address and the source IP address in the link layer original socket are further analyzed, then, the own ARP list is inquired, whether the IP which is the same as the source IP in the link layer original socket exists or not is judged, if so, the source MAC in the link layer original socket is used for replacing the MAC corresponding to the existing IP, and the ARP list is updated. If not, the source MAC address and source IP address in the link layer raw socket are added to the ARP list.
As a preferred scheme, the ARP simulation response packet includes: destination MAC address, source MAC address, Ethernet frame type, hardware type, protocol type, MAC address length, IP address length, ARP type, source MAC address, source IP address, destination MAC address, destination IP address.
Preferably, the time t is set to 1 minute.
Preferably, the device is a touch screen.
As a preferred scheme, the call system command for querying the ARP information of the shared IP of the main CPU is:
ARP-H ether-i eth0, which indicates that the ARP information of the network card eth0 where the main CPU shares the IP is queried.
As a preferred scheme, the calling system command for binding the main CPU network card and the shared IP is:
ifconfig eth0 shares IP, network card eth 0.
As a preferred scheme, the call system command for the slave CPU to offload the shared IP from the network card is:
IP addr del shares the IP/subnet mask dev eth0, network card eth 0.
Preferably, the first parameter of the socket function is designated as PF _ PACKET.
Has the advantages that: the invention provides a method for quickly taking effect of a shared IP of a double-CPU redundant PLC after master-slave switching, which is characterized in that an ARP simulation message is automatically constructed and is actively pushed to equipment sharing an IP network segment after the master-slave switching of the PLC, so that the equipment can immediately update an ARP list of the equipment.
Drawings
Fig. 1 is a system architecture of a dual CPU redundant PLC including a touch screen.
Detailed Description
The present invention will be further described with reference to the following examples.
A PLC system architecture employing dual CPU hot standby redundancy is generally shown in fig. 1:
the CPU card is a main controller of the PLC and is the most core, most structure and most complex part of the program, and is responsible for communicating with various IO cards through a field bus, collecting data, outputting control orders and carrying out a large amount of arithmetic and logic operation according to a certain period, and compared with other parts of the PLC, the probability of abnormity of the CPU card in the running is higher. Therefore, in important process control occasions, the overall reliability of the PLC is improved by adopting a double-CPU hot standby redundancy scheme.
When the main CPU fails, the slave CPU takes over the work of the main CPU immediately.
When an ARP list of an IP protocol stack embedded in the touch screen cannot be updated in time along with the switching of a PLC master CPU and a PLC slave CPU, the touch screen also considers that the shared IP belongs to the CPU before switching after switching, therefore, communication with the master CPU before switching is always tried until the ARP list is updated overtime, the IP protocol stack resends an ARP broadcast to request a network card MAC address corresponding to the shared IP, and after the existing master CPU responds to the ARP broadcast sent by the touch screen, communication between the touch screen and a new master CPU can not be established.
Normally, the mapping relation between the IP address and the MAC address of the main CPU is always stored in the ARP list of the touch screen, and after the PLC is switched between the main CPU and the slave CPU, although the shared IP is originally bound for the slave CPU, for the touch screen, the mapping relation between the IP address and the MAC of the main CPU in the local ARP list is not changed, and the MAC is still the MAC of the original main CPU and is not updated due to the switching between the main CPU and the slave CPU. Therefore, when the touch screen sends the message to the main CPU, the message is still sent to the original main CPU instead of the current main CPU. The IP protocol stack does not perform ARP broadcasting again until a fixed updating period of the ARP list is reached, the MAC address of the existing main CPU can not be added into the ARP list of the touch screen, and communication can not be recovered. The update period of the ARP is related to the IP protocol stack itself, and is generally not less than 30 seconds, and can reach several minutes at most.
Although the running state monitoring and control of the PLC mainly depends on the upper computer software, once the upper computer system is abnormal or a communication network between the local PLC and the upper computer system is attacked, the local touch screen is the last reserved monitoring and control means, so that the running state of the PLC can be accurately displayed under any condition by the touch screen, and control commands issued to the PLC are necessary in some key occasions.
A method for quickly taking effect of a shared IP of a dual-CPU redundant PLC after master-slave switching comprises the following steps:
1) a main CPU of the PLC periodically inquires ARP information of the same network segment of the local machine and the shared IP in the running process so as to obtain MAC addresses and IP addresses of all devices which are communicated with the PLC through the shared IP.
2) The main CPU sends the MAC address of the equipment which belongs to the same network segment with the shared IP and the IP address corresponding to the MAC address to the slave CPU through the redundant network port, and the slave CPU receives the information and stores the information in the memory of the slave CPU.
3) When the PLC is switched between a master mode and a slave mode, after the master CPU after the switching binds the network card with the shared IP, the master CPU bypasses the local IP protocol stack according to the MAC address and the IP address of the equipment which belongs to the same network segment with the shared IP and is received before the switching, constructs an ARP simulation response message by itself and actively sends the ARP simulation response message to the equipment accessing the PLC through the shared IP. The device is caused to immediately update its own ARP list, thereby causing communication between the device and the PLC to be immediately resumed.
The following describes a specific embodiment of the present invention by taking a dual-CPU redundant PLC system including 3 touch screens as an example, with reference to the accompanying drawings.
Step 1: assuming that the shared IP of the PLC is set to 192.168.2.100, the IP addresses of the 3 touchscreens are 192.168.2.1, 192.168.2.2, and 192.168.2.3, respectively. The CPU operating system of the PLC is Linux.
Step 2: the main CPU of the PLC inquires ARP information of the local computer once every 1 minute, and for a Linux operating system, a system command ARP-H ether-i eth0 is called, wherein eth0 is the name of a network card where the shared IP is located. ARP-H ether-i eth0 indicates that the ARP information of the network card where the main CPU shares the IP is queried.
The information returned by the system is as follows:
192.168.0.10 00:11:13:fb:53:0a
192.168.0.11 00:32:14:f9:13:0b
192.168.1.22 01:03:98:e9:05:03
192.168.2.1 02:05:fc:01:09:46
192.168.2.2 02:05:fc:01:0a:10
192.168.2.3 02:05:fc:01:0a:20
the main CPU needs to parse the returned information and record 3 pieces of information of 192.168.2.x in this segment, that is, the IP and MAC addresses of the touch screen located in the same IP segment as the shared IP.
And step 3: and (3) the main CPU transmits the IP and MAC information which belongs to the shared IP network segment equipment and is analyzed in the step (2) to the slave CPU through the redundant network port, and the slave CPU receives the IP and MAC information and stores the received information in the memory of the slave CPU.
And 4, step 4: when the master-slave switching of the CPU occurs in the PLC, the CPU which is originally the slave and is now the master binds the network card of the PLC with the shared IP by calling a system command (for example, in a linux system, an ifconfig eth0192.168.2.100 command is called, wherein eth0 is a descriptor of the network card, and 192.168.2.100 is the shared IP); and the CPU which is originally the master and is now the slave unloads the shared IP from the network card of the CPU (for example, in a linux system, the shared IP is unloaded by calling IP addr del 192.168.2.100/24dev eth0, wherein 192.168.2.100 is the shared IP, 24 represents a subnet mask, and eth0 is a network card descriptor).
And 5: originally, a CPU which is now upgraded mainly constructs ARP simulation response messages according to the device IP and MAC information received in step 3, and taking a touch screen with an IP address of 192.168.2.1 and a MAC address of 02:05: fc:01:09:46 as an example, the structure of the constructed ARP simulation response messages is shown in table 1:
step 6: the main CPU program uses socket function to create 1 link layer original socket, the first parameter of the function is designated as PF _ PACKET, so that the socket is used for network data receiving and transmitting, the IP protocol stack of the local machine can be bypassed, the modification of data by the IP protocol stack is avoided, then the Sendto function is used for directly submitting the constructed 3 messages to the network card driving layer of the local machine based on the original socket created above, the network card is sent to 3 touch screens, after the touch screens receive the messages, the IP protocol stack of the self compares the destination MAC address in the messages, whether the destination IP address is the same as the MAC address and the IP address of the self, if the destination IP address is the same as the MAC address and the IP address of the self, the source MAC address and the source IP address in the messages are further analyzed, then the ARP list of the self is inquired, whether the IP same as the source IP in the messages exists, if the IP address is the same as the source MAC corresponding to the existing IP is replaced by the source MAC in the messages, and updates the ARP list. The IP and source MAC are not added to the ARP list. Therefore, when the touch screen communicates with the current main CPU and organizes Ethernet frame message encapsulation, the MAC address of the current main CPU is used instead of the MAC address of the original main CPU.
The above description is only of the preferred embodiments of the present invention, and it should be noted that: it will be apparent to those skilled in the art that various modifications and adaptations can be made without departing from the principles of the invention and these are intended to be within the scope of the invention.