1.\" $OpenBSD: carp.4,v 1.40 2018/01/12 04:36:44 deraadt Exp $ 2.\" 3.\" Copyright (c) 2003, Ryan McBride. All rights reserved. 4.\" 5.\" Redistribution and use in source and binary forms, with or without 6.\" modification, are permitted provided that the following conditions 7.\" are met: 8.\" 1. Redistributions of source code must retain the above copyright 9.\" notice, this list of conditions and the following disclaimer. 10.\" 2. Redistributions in binary form must reproduce the above copyright 11.\" notice, this list of conditions and the following disclaimer in the 12.\" documentation and/or other materials provided with the distribution. 13.\" 14.\" THIS SOFTWARE IS PROVIDED BY THE PROJECT AND CONTRIBUTORS ``AS IS'' AND 15.\" ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 16.\" IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 17.\" ARE DISCLAIMED. IN NO EVENT SHALL THE PROJECT OR CONTRIBUTORS BE LIABLE 18.\" FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 19.\" DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 20.\" OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 21.\" HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 22.\" LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 23.\" OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 24.\" SUCH DAMAGE. 25.\" 26.Dd $Mdocdate: January 12 2018 $ 27.Dt CARP 4 28.Os 29.Sh NAME 30.Nm carp 31.Nd Common Address Redundancy Protocol 32.Sh SYNOPSIS 33.Cd "pseudo-device carp" 34.Sh DESCRIPTION 35The 36.Nm 37interface is a pseudo-device which implements and controls the 38CARP protocol. 39.Nm 40allows multiple hosts on the same local network to share a set of IP addresses. 41Its primary purpose is to ensure that these 42addresses are always available, but in some configurations 43.Nm 44can also provide load balancing functionality. 45.Pp 46A 47.Nm 48interface can be created at runtime using the 49.Ic ifconfig carp Ns Ar N Ic create 50command or by setting up a 51.Xr hostname.if 5 52configuration file for 53.Xr netstart 8 . 54.Pp 55To use 56.Nm , 57the administrator needs to configure at minimum 58a common virtual host ID (VHID) and 59virtual host IP address on each machine which is to take part in the virtual 60group. 61Additional parameters can also be set on a per-interface basis: 62.Cm advbase 63and 64.Cm advskew , 65which are used to control how frequently the host sends advertisements when it 66is the master for a virtual host, and 67.Cm pass 68which is used to authenticate carp advertisements. 69Finally 70.Cm carpdev 71is used to specify which interface the 72.Nm 73device attaches to. 74These configurations can be done using 75.Xr ifconfig 8 , 76or through the 77.Dv SIOCSVH 78ioctl. 79.Pp 80.Nm 81can also be used in conjunction with 82.Xr ifstated 8 83to respond to changes in CARP state; 84however, for most uses this will not be necessary. 85See the manual page for 86.Xr ifstated 8 87for more information. 88.Pp 89Additionally, there are a number of global parameters which can be set using 90.Xr sysctl 8 : 91.Bl -tag -width xxxxxxxxxxxxxxxxxxxxxxxxxx 92.It net.inet.carp.allow 93Accept incoming 94.Nm 95packets. 96Enabled by default. 97.It net.inet.carp.preempt 98Allow virtual hosts to preempt each other. 99Disabled by default. 100.It net.inet.carp.log 101Make 102.Nm 103log state changes, bad packets, and other errors. 104May be a value between 0 and 7 corresponding with 105.Xr syslog 3 106priorities. 107The default value is 2, which limits logging to changes in CARP state. 108.El 109.Sh LOAD BALANCING 110.Nm 111uses IP balancing to load balance incoming traffic 112over a group of 113.Nm 114hosts. 115IP balancing is not dependent on ARP and therefore works 116for traffic that comes over a router. 117However it requires the traffic that is destined towards 118the load balanced IP addresses to be received by all 119.Nm 120hosts. 121While this is always the case when connected to a hub, 122it has to play some tricks in switched networks, which 123will result in a higher network load. 124.Pp 125To configure load balancing one has to specify multiple 126carp nodes using the 127.Cm carpnodes 128option. 129Each node in a load balancing cluster is represented 130by at least one 131.Qq Cm vhid : Ns Cm advskew 132pair in a comma separated list. 133.Nm 134tries to distribute the incoming network load over all configured carpnodes. 135The following example 136creates a load balancing group consisting of three nodes, 137using vhids 3, 4 and 6: 138.Bd -literal -offset indent 139# ifconfig carp0 carpnodes 3:0,4:0,6:100 140.Ed 141.Pp 142The advskew value of the last node is set to 100, 143so that this node is designated to the BACKUP state. 144It will only become MASTER if all nodes with a lower advskew value have failed. 145By varying this value throughout the machines in the cluster 146it is possible to decide which share of the network load each node receives. 147Therefore, all carp interfaces in the cluster are configured identically, except 148for a different 149.Cm advskew 150value within the carpnodes specification. 151.Pp 152IP balancing works by utilizing the network itself to distribute 153incoming traffic to all 154.Nm 155nodes in the cluster. 156Each packet is filtered on the incoming 157.Nm 158interface so that only one node in the cluster accepts the 159packet. 160All the other nodes will just silently drop it. 161The filtering function uses a hash over the source and destination 162address of the IPv4 or IPv6 packet and compares the result against the 163state of the carpnode. 164.Pp 165IP balancing is activated by setting the balancing mode to 166.Cm ip . 167This is the recommended default setting. 168In this mode, carp uses a multicast MAC address, so that a switch 169sends incoming traffic towards all nodes. 170.Pp 171However, there are a few OS and routers that do not accept a multicast 172MAC address being mapped to a unicast IP. 173This can be resolved by using one of the following unicast options. 174For scenarios where a hub is used it is not necessary to use a multicast MAC 175and it is safe to use the 176.Cm ip-unicast 177mode. 178Manageable switches can usually be tricked into forwarding unicast 179traffic to all cluster nodes ports by configuring them into some 180sort of monitoring mode. 181If this is not possible, using the 182.Cm ip-stealth 183mode is another option, which should work on most switches. 184In this mode 185.Nm 186never sends packets with its virtual MAC address as source. 187Stealth mode prevents a switch from learning the virtual MAC 188address, so that it has to flood the traffic to all its ports. 189Please note that activating stealth mode on a 190.Nm 191interface that has already been running might not work instantly. 192As a workaround the VHID of the first carpnode can be changed to a 193previously unused one, or just wait until the MAC table entry in the 194switch times out. 195Some layer 3 switches do port learning based on ARP packets. 196Therefore the stealth mode cannot hide the virtual MAC address 197from these kind of devices. 198.Pp 199If IP balancing is being used on a firewall, it is recommended to 200configure the 201.Cm carpnodes 202in a symmetrical manner. 203This is achieved by simply using the same 204.Cm carpnodes 205list on all sides of the firewall. 206This ensures that packets of one connection will pass in and out 207on the same host and are not routed asymmetrically. 208.Sh EXAMPLES 209For most scenarios it is desirable to have a well-defined master, 210achieved by enabling the 211.Cm preempt 212option. 213Enable it on both host A and B: 214.Pp 215.Dl # sysctl net.inet.carp.preempt=1 216.Pp 217Assume that host A is the preferred master and carp should run on the physical 218interfaces em0 with the network 192.168.1.0/24 and em1 with network 219192.168.2.0/24. 220This is the setup for host A: 221.Bd -literal -offset indent 222# ifconfig carp0 192.168.1.1/24 carpdev em0 vhid 1 223# ifconfig carp1 192.168.2.1/24 carpdev em1 vhid 2 224.Ed 225.Pp 226The setup for host B is identical, but it has a higher 227.Cm advskew : 228.Bd -literal -offset indent 229# ifconfig carp0 192.168.1.1/24 carpdev em0 vhid 1 advskew 100 230# ifconfig carp1 192.168.2.1/24 carpdev em1 vhid 2 advskew 100 231.Ed 232.Ss LOAD BALANCING 233In order to set up a load balanced virtual host, it is necessary to configure 234one 235.Cm carpnodes 236entry for each physical host. 237In the following example, two physical hosts are configured to 238provide balancing and failover for the IP address 192.168.1.10. 239.Pp 240First the 241.Nm 242interface on Host A is configured. 243The 244.Cm advskew 245of 100 on the second carpnode entry means that its advertisements will be sent 246out slightly less frequently and will therefore become the designated backup. 247.Bd -literal -offset indent 248# ifconfig carp0 192.168.1.10 carpdev em0 carpnodes 1:0,2:100 \e 249 balancing ip 250.Ed 251.Pp 252The configuration for host B is identical, except the skew is on 253the carpnode entry with virtual host 1 rather than virtual host 2. 254.Bd -literal -offset indent 255# ifconfig carp0 192.168.1.10 carpdev em0 carpnodes 1:100,2:0 \e 256 balancing ip 257.Ed 258.Pp 259If a different mode of load balancing is desired the 260.Cm balancing 261mode can be adjusted accordingly. 262.Sh SEE ALSO 263.Xr sysctl 2 , 264.Xr inet 4 , 265.Xr pfsync 4 , 266.Xr hostname.if 5 , 267.Xr ifconfig 8 , 268.Xr ifstated 8 , 269.Xr netstart 8 , 270.Xr sysctl 8 271.Sh HISTORY 272The 273.Nm 274device first appeared in 275.Ox 3.5 . 276.Sh BUGS 277If load balancing is used in setups where the carpdev does not share 278an IP in the same subnet as 279.Nm , 280it is not possible to use the IP of the 281.Nm 282interface for self originated traffic. 283This is because the return packets are also subject to load balancing 284and might end up on any other node in the cluster. 285.Pp 286If an IPv6 load balanced carp interface is taken down manually, 287it will accept all incoming packets for its address. 288This will lead to duplicated packets. 289