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