1.\" $OpenBSD: carp.4,v 1.38 2015/11/03 17:45:32 jmc 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: November 3 2015 $ 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 166.Cm balancing 167mode to 168.Cm ip . 169This is the recommended default setting. 170In this mode, carp uses a multicast MAC address, so that a switch 171sends incoming traffic towards all nodes. 172.Pp 173However, there are a few OS and routers that do not accept a multicast 174MAC address being mapped to a unicast IP. 175This can be resolved by using one of the following unicast options. 176For scenarios where a hub is used it is not necessary to use a multicast MAC 177and it is safe to use the 178.Ar ip-unicast 179mode. 180Manageable switches can usually be tricked into forwarding unicast 181traffic to all cluster nodes ports by configuring them into some 182sort of monitoring mode. 183If this is not possible, using the 184.Ar ip-stealth 185mode is another option, which should work on most switches. 186In this mode 187.Nm 188never sends packets with its virtual MAC address as source. 189Stealth mode prevents a switch from learning the virtual MAC 190address, so that it has to flood the traffic to all its ports. 191Please note that activating stealth mode on a 192.Nm 193interface that has already been running might not work instantly. 194As a workaround the VHID of the first carpnode can be changed to a 195previously unused one, or just wait until the MAC table entry in the 196switch times out. 197Some layer 3 switches do port learning based on ARP packets. 198Therefore the stealth mode cannot hide the virtual MAC address 199from these kind of devices. 200.Pp 201If IP balancing is being used on a firewall, it is recommended to 202configure the 203.Cm carpnodes 204in a symmetrical manner. 205This is achieved by simply using the same 206.Cm carpnodes 207list on all sides of the firewall. 208This ensures that packets of one connection will pass in and out 209on the same host and are not routed asymmetrically. 210.Sh EXAMPLES 211For most scenarios it is desirable to have a well-defined master, 212achieved by enabling the 213.Cm preempt 214option. 215Enable it on both host A and B: 216.Pp 217.Dl # sysctl net.inet.carp.preempt=1 218.Pp 219Assume that host A is the preferred master and carp should run on the physical 220interfaces em0 with the network 192.168.1.0/24 and em1 with network 221192.168.2.0/24. 222This is the setup for host A: 223.Bd -literal -offset indent 224# ifconfig carp0 192.168.1.1/24 carpdev em0 vhid 1 225# ifconfig carp1 192.168.2.1/24 carpdev em1 vhid 2 226.Ed 227.Pp 228The setup for host B is identical, but it has a higher 229.Cm advskew : 230.Bd -literal -offset indent 231# ifconfig carp0 192.168.1.1/24 carpdev em0 vhid 1 advskew 100 232# ifconfig carp1 192.168.2.1/24 carpdev em1 vhid 2 advskew 100 233.Ed 234.Ss LOAD BALANCING 235In order to set up a load balanced virtual host, it is necessary to configure 236one 237.Cm carpnodes 238entry for each physical host. 239In the following example, two physical hosts are configured to 240provide balancing and failover for the IP address 192.168.1.10. 241.Pp 242First the 243.Nm 244interface on Host A is configured. 245The 246.Cm advskew 247of 100 on the second carpnode entry means that its advertisements will be sent 248out slightly less frequently and will therefore become the designated backup. 249.Bd -literal -offset indent 250# ifconfig carp0 192.168.1.10 carpdev em0 carpnodes 1:0,2:100 \e 251 balancing ip 252.Ed 253.Pp 254The configuration for host B is identical, except the skew is on 255the carpnode entry with virtual host 1 rather than virtual host 2. 256.Bd -literal -offset indent 257# ifconfig carp0 192.168.1.10 carpdev em0 carpnodes 1:100,2:0 \e 258 balancing ip 259.Ed 260.Pp 261If a different mode of load balancing is desired the 262.Cm balancing 263mode can be adjusted accordingly. 264.Sh SEE ALSO 265.Xr sysctl 3 , 266.Xr inet 4 , 267.Xr pfsync 4 , 268.Xr hostname.if 5 , 269.Xr ifconfig 8 , 270.Xr ifstated 8 , 271.Xr netstart 8 , 272.Xr sysctl 8 273.Sh HISTORY 274The 275.Nm 276device first appeared in 277.Ox 3.5 . 278.Sh BUGS 279If load balancing is used in setups where the carpdev does not share 280an IP in the same subnet as 281.Nm , 282it is not possible to use the IP of the 283.Nm 284interface for self originated traffic. 285This is because the return packets are also subject to load balancing 286and might end up on any other node in the cluster. 287.Pp 288If an IPv6 load balanced carp interface is taken down manually, 289it will accept all incoming packets for its address. 290This will lead to duplicated packets. 291