1.\" $OpenBSD: carp.4,v 1.34 2010/03/12 13:36:16 mpf 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: March 12 2010 $ 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. 74If unspecified, the kernel attempts to set it by looking for 75another interface with the same subnet. 76These configurations can be done using 77.Xr ifconfig 8 , 78or through the 79.Dv SIOCSVH 80ioctl. 81.Pp 82.Nm 83can also be used in conjunction with 84.Xr ifstated 8 85to respond to changes in CARP state; 86however, for most uses this will not be necessary. 87See the manual page for 88.Xr ifstated 8 89for more information. 90.Pp 91Additionally, there are a number of global parameters which can be set using 92.Xr sysctl 8 : 93.Bl -tag -width xxxxxxxxxxxxxxxxxxxxxxxxxx 94.It net.inet.carp.allow 95Accept incoming 96.Nm 97packets. 98Enabled by default. 99.It net.inet.carp.preempt 100Allow virtual hosts to preempt each other. 101Disabled by default. 102.It net.inet.carp.log 103Make 104.Nm 105log state changes, bad packets, and other errors. 106May be a value between 0 and 7 corresponding with 107.Xr syslog 3 108priorities. 109The default value is 2, which limits logging to changes in CARP state. 110.El 111.Sh LOAD BALANCING 112.Nm 113provides two mechanisms to load balance incoming traffic 114over a group of 115.Nm 116hosts: 117ARP balancing and IP balancing. 118.Pp 119Which one to use mainly depends on the network environment 120.Nm 121is being used in. 122ARP balancing has limited abilities for load balancing the 123incoming connections between hosts in an Ethernet network. 124It only works for clients in the local network, because 125ARP balancing spreads the load by varying ARP replies 126based on the source MAC address of the host sending the query. 127Therefore it cannot balance traffic that crosses a router, because the 128router itself will always be balanced to the same virtual host. 129.Pp 130IP balancing is not dependent on ARP and therefore also works 131for traffic that comes over a router. 132This method should work in all environments and can 133also provide more fine grained load balancing than ARP balancing. 134The downside of IP balancing is that it requires the traffic 135that is destined towards the load balanced IP addresses 136to be received by all 137.Nm 138hosts. 139While this is always the case when connected to a hub, 140it has to play some tricks in switched networks, which 141will result in a higher network load. 142.Pp 143A rule of thumb might be to use ARP balancing if there 144are many hosts on the same network segment and 145to use IP balancing for all other cases. 146.Pp 147To configure load balancing one has to specify multiple 148carp nodes using the 149.Cm carpnodes 150option. 151Each node in a load balancing cluster is represented 152by at least one 153.Qq Cm vhid : Ns Cm advskew 154pair in a comma separated list. 155.Nm 156tries to distribute the incoming network load over all configured carpnodes. 157The following example 158creates a load balancing group consisting of three nodes, 159using vhids 3, 4 and 6: 160.Bd -literal -offset indent 161# ifconfig carp0 carpnodes 3:0,4:0,6:100 162.Ed 163.Pp 164The advskew value of the last node is set to 100, 165so that this node is designated to the BACKUP state. 166It will only become MASTER if all nodes with a lower advskew value have failed. 167By varying this value throughout the machines in the cluster 168it is possible to decide which share of the network load each node receives. 169Therefore, all carp interfaces in the cluster are configured identically, except 170for a different 171.Cm advskew 172value within the carpnodes specification. 173.Pp 174See the 175.Sx EXAMPLES 176section for a practical example of load balancing. 177.Ss ARP BALANCING 178For ARP balancing, one has to configure multiple 179.Cm carpnodes 180and choose the 181.Cm balancing 182mode 183.Ar arp . 184.Pp 185Once an ARP request is received, the CARP protocol will use a hashing 186function against the source MAC address in the ARP request to determine 187which carpnode the request belongs to. 188If the corresponding 189carpnode is in master state, the ARP request will be answered, otherwise 190it will be ignored. 191.Pp 192The ARP load balancing has some limitations. 193Firstly, ARP balancing only works on the local network segment. 194It cannot balance traffic that crosses a router, because the 195router itself will always be balanced to the same carpnode. 196Secondly, ARP load balancing can lead to asymmetric routing 197of incoming and outgoing traffic, thus combining it with 198.Xr pfsync 4 199requires special care, because this can create a race condition between 200balanced routers and the host they are serving. 201ARP balancing can be safely used with pfsync if the 202.Xr pf 4 203ruleset translates the source address to an unshared address on the 204outgoing interface using a NAT rule. 205This requires multiple CARP groups with 206.Em different 207IP addresses on the outgoing interface, configured so that each host is the 208master of one group. 209.Pp 210ARP balancing also works for IPv6, but instead of ARP the Neighbor Discovery 211Protocol (NDP) is used. 212.Ss IP BALANCING 213IP load balancing works by utilizing the network itself to distribute 214incoming traffic to all 215.Nm 216nodes in the cluster. 217Each packet is filtered on the incoming 218.Nm 219interface so that only one node in the cluster accepts the 220packet. 221All the other nodes will just silently drop it. 222The filtering function uses a hash over the source and destination 223address of the IPv4 or IPv6 packet and compares the result against the 224state of the carpnode. 225.Pp 226IP balancing is activated by setting the 227.Cm balancing 228mode to 229.Ar ip . 230This is the recommended default setting. 231In this mode, carp uses a multicast MAC address, so that a switch 232sends incoming traffic towards all nodes. 233.Pp 234However, there are a few OS and routers that do not accept a multicast 235MAC address being mapped to a unicast IP. 236This can be resolved by using one of the following unicast options. 237For scenarios where a hub is used it is not necessary to use a multicast MAC 238and it is safe to use the 239.Ar ip-unicast 240mode. 241Manageable switches can usually be tricked into forwarding unicast 242traffic to all cluster nodes ports by configuring them into some 243sort of monitoring mode. 244If this is not possible, using the 245.Ar ip-stealth 246mode is another option, which should work on most switches. 247In this mode 248.Nm 249never sends packets with its virtual MAC address as source. 250Stealth mode prevents a switch from learning the virtual MAC 251address, so that it has to flood the traffic to all its ports. 252Please note that activating stealth mode on a 253.Nm 254interface that has already been running might not work instantly. 255As a workaround the VHID of the first carpnode can be changed to a 256previously unused one, or just wait until the MAC table entry in the 257switch times out. 258Some layer 3 switches do port learning based on ARP packets. 259Therefore the stealth mode cannot hide the virtual MAC address 260from these kind of devices. 261.Pp 262If IP balancing is being used on a firewall, it is recommended to 263configure the 264.Cm carpnodes 265in a symmetrical manner. 266This is achieved by simply using the same 267.Cm carpnodes 268list on all sides of the firewall. 269This ensures that packets of one connection will pass in and out 270on the same host and are not routed asymmetrically. 271.Sh EXAMPLES 272For most scenarios it is desirable to have a well-defined master, 273achieved by enabling the 274.Cm preempt 275option. 276Enable it on both host A and B: 277.Pp 278.Dl # sysctl net.inet.carp.preempt=1 279.Pp 280Assume that host A is the preferred master and 192.168.1.x/24 is 281configured on one physical interface and 192.168.2.y/24 on another. 282This is the setup for host A: 283.Bd -literal -offset indent 284# ifconfig carp0 192.168.1.1 vhid 1 285# ifconfig carp1 192.168.2.1 vhid 2 286.Ed 287.Pp 288The setup for host B is identical, but it has a higher 289.Cm advskew : 290.Bd -literal -offset indent 291# ifconfig carp0 192.168.1.1 vhid 1 advskew 100 292# ifconfig carp1 192.168.2.1 vhid 2 advskew 100 293.Ed 294.Ss LOAD BALANCING 295In order to set up a load balanced virtual host, it is necessary to configure 296one 297.Cm carpnodes 298entry for each physical host. 299In the following example, two physical hosts are configured to 300provide balancing and failover for the IP address 192.168.1.10. 301.Pp 302First the 303.Nm 304interface on Host A is configured. 305The 306.Cm advskew 307of 100 on the second carpnode entry means that its advertisements will be sent 308out slightly less frequently and will therefore become the designated backup. 309.Pp 310.Dl # ifconfig carp0 192.168.1.10 carpnodes 1:0,2:100 balancing ip 311.Pp 312The configuration for host B is identical, except the skew is on 313the carpnode entry with virtual host 1 rather than virtual host 2. 314.Pp 315.Dl # ifconfig carp0 192.168.1.10 carpnodes 1:100,2:0 balancing ip 316.Pp 317If ARP balancing or a different mode of IP balancing is desired 318the 319.Cm balancing 320mode can be adjusted accordingly. 321.Sh SEE ALSO 322.Xr sysctl 3 , 323.Xr inet 4 , 324.Xr pfsync 4 , 325.Xr hostname.if 5 , 326.Xr ifconfig 8 , 327.Xr ifstated 8 , 328.Xr netstart 8 , 329.Xr sysctl 8 330.Sh HISTORY 331The 332.Nm 333device first appeared in 334.Ox 3.5 . 335.Sh BUGS 336If load balancing is used in setups where the carpdev does not share 337an IP in the same subnet as 338.Nm , 339it is not possible to use the IP of the 340.Nm 341interface for self originated traffic. 342This is because the return packets are also subject to load balancing 343and might end up on any other node in the cluster. 344.Pp 345If an IPv6 load balanced carp interface is taken down manually, 346it will accept all incoming packets for its address. 347This will lead to duplicated packets. 348