Index: etc/network.subr =================================================================== --- etc/network.subr (revision 265916) +++ etc/network.subr (working copy) @@ -1139,10 +1139,13 @@ done # Handle ifconfig_IF_aliases, ifconfig_IF_aliasN, and the others. + # "vhid" keyword makes the rest of the string to be processed + # at a single ifconfig pass. _tmpargs= + _carp= for _c in `get_if_var $_if ifconfig_IF_aliases` $_aliasn; do - case $_c in - inet|inet6|link|ether) + case "$_carp$_c" in + inet|inet6|link|ether|vhid) case $_tmpargs in ${_af}\ *) eval ifalias_af_common_handler $_if $_af $_action $_tmpargs && _ret=0 @@ -1149,11 +1152,21 @@ ;; esac _tmpargs=$_c + case "$_c" in + vhid) + _carp=y + ;; + esac ;; *) _tmpargs="$_tmpargs $_c" esac done + # Process CARP + if [ -n "$_carp" -a -n "$_tmpargs" ]; then + ${IFCONFIG_CMD} $_if $_tmpargs $_action && _ret=0 + _tmpargs="" + fi # Process the last component case $_tmpargs in ${_af}\ *) Index: sbin/ifconfig/carp.c =================================================================== --- sbin/ifconfig/carp.c (revision 265916) +++ sbin/ifconfig/carp.c (working copy) @@ -65,6 +65,8 @@ static int carpr_advbase = -1; static int carpr_state = -1; static unsigned char const *carpr_key; +static int set_carpr_lladdr = 0; +static u_int8_t carpr_lladdr[6] = { 0xff, 0xff, 0xff, 0xff, 0xff, 0xff }; static void carp_status(int s) @@ -83,6 +85,10 @@ printf("\tcarp: %s vhid %d advbase %d advskew %d", carp_states[carpr[i].carpr_state], carpr[i].carpr_vhid, carpr[i].carpr_advbase, carpr[i].carpr_advskew); + printf(" lladdr %02x:%02x:%02x:%02x:%02x:%02x", + carpr[i].carpr_lladdr[0], carpr[i].carpr_lladdr[1], + carpr[i].carpr_lladdr[2], carpr[i].carpr_lladdr[3], + carpr[i].carpr_lladdr[4], carpr[i].carpr_lladdr[5]); if (printkeys && carpr[i].carpr_key[0] != '\0') printf(" key \"%s\"\n", carpr[i].carpr_key); else @@ -150,6 +156,12 @@ carpr.carpr_advbase = carpr_advbase; if (carpr_state > -1) carpr.carpr_state = carpr_state; + if (set_carpr_lladdr) { + memcpy(carpr.carpr_lladdr, carpr_lladdr, + sizeof(carpr.carpr_lladdr)); + } else { + memset(carpr.carpr_lladdr, 0xff, sizeof(carpr.carpr_lladdr)); + } if (ioctl(s, SIOCSVH, (caddr_t)&ifr) == -1) err(1, "SIOCSVH"); @@ -202,7 +214,48 @@ errx(1, "unknown state"); } +static void +setcarp_lladdr(const char *val, int d, int s, const struct afswtch *afp) +{ + if (carpr_vhid == -1) + errx(1, "lladdr requires vhid"); + + if (strcmp(val, "vrrp") == 0) { + set_carpr_lladdr = 1; + carpr_lladdr[0] = CARP_OLD_OUIBASE0; carpr_lladdr[1] = CARP_OLD_OUIBASE1; + carpr_lladdr[2] = CARP_OLD_OUIBASE2; carpr_lladdr[3] = CARP_OLD_OUIBASE3; + carpr_lladdr[4] = CARP_OLD_OUIBASE4; carpr_lladdr[5] = carpr_vhid; + return; + } else if (strcmp(val, "carp") == 0) { + set_carpr_lladdr = 1; + carpr_lladdr[0] = CARP_FBSD_OUIBASE0; carpr_lladdr[1] = CARP_FBSD_OUIBASE1; + carpr_lladdr[2] = CARP_FBSD_OUIBASE2; carpr_lladdr[3] = CARP_FBSD_OUIBASE3; + carpr_lladdr[4] = CARP_FBSD_OUIBASE4; carpr_lladdr[5] = carpr_vhid; + return; + } else { + size_t i; + unsigned int tmp_lladdr[6]; + + if (sscanf(val, "%x:%x:%x:%x:%x:%x", + &tmp_lladdr[0], &tmp_lladdr[1], &tmp_lladdr[2], + &tmp_lladdr[3], &tmp_lladdr[4], &tmp_lladdr[5]) != 6) { + errx(1, "failed to parse lladdr %s", val); + } + for (i = 0; i < sizeof(tmp_lladdr)/sizeof(tmp_lladdr[0]); i++) { + if (tmp_lladdr[i] > 0xff) + errx(1, "lladdr %s: bad octet %zd", val, i + 1); + carpr_lladdr[i] = (u_int8_t)tmp_lladdr[i]; + } + set_carpr_lladdr = 1; + return; + } + errx(1, "we shouldn't reach this point"); +} + static struct cmd carp_cmds[] = { + DEF_CMD_ARG("lladdr", setcarp_lladdr), + DEF_CMD_ARG("ether", setcarp_lladdr), + DEF_CMD_ARG("link", setcarp_lladdr), DEF_CMD_ARG("advbase", setcarp_advbase), DEF_CMD_ARG("advskew", setcarp_advskew), DEF_CMD_ARG("pass", setcarp_passwd), Index: sbin/ifconfig/ifconfig.8 =================================================================== --- sbin/ifconfig/ifconfig.8 (revision 265916) +++ sbin/ifconfig/ifconfig.8 (working copy) @@ -2514,6 +2514,24 @@ .Ar phrase . .It Cm state Ar MASTER|BACKUP Forcibly change state of a given vhid. +.It Cm link|ether|lladdr Ar vrrp|carp|de:ad:c0:ff:ee:be +Change MAC address for this instance. +Value +.Ar vrrp +sets MAC base to the standard VRRP one, 00:00:5e:00:01 (that is the +historical value introduced by +.Ox 3.5).\ +.Ar carp +sets MAC base to the FreeBSD-specific OUI 58:9c:fc:01:00 (this value +is also used by-default). +For these keywords the last octet of the MAC address will be set to the +vhid value for the interface. +When +.Cm link +changes MAC address, the corresponding interface will be brought to the +backup state to allow +.Nm +instance to go through the normal election process. .El .Pp The Index: share/man/man4/carp.4 =================================================================== --- share/man/man4/carp.4 (revision 265916) +++ share/man/man4/carp.4 (working copy) @@ -26,7 +26,7 @@ .\" .\" $FreeBSD$ .\" -.Dd February 21, 2013 +.Dd May 11, 2014 .Dt CARP 4 .Os .Sh NAME @@ -78,6 +78,18 @@ .Dv SIOCSVH .Xr ioctl 2 . .Pp +By-default, CARP uses MAC address base of 00:00:5e:00:01 with the last octet +set to the VHID of the interface. +This MAC range clashes with VRRP and HSRP protocols, so you can get layer-2 +address clashes if VRRP and CARP will use same virtual IDs. +To avoid this, +.Nm +has +.Xr ifconfig 8 , +parameter +.Cm lladdr +that allows to set arbitrary MAC address for this instance. +.Pp CARP virtual hosts can be configured on multicast-capable interfaces: Ethernet, layer 2 VLAN, FDDI and Token Ring. An arbitrary number of virtual host IDs can be configured on an interface. @@ -145,6 +157,16 @@ .Nm experiences errors sending its announcements. The default value is 240 (the maximum advskew value). +.It Va net.inet.carp.forus_total +Total number of packets passed via carp_forus() routine. +.It Va net.inet.carp.forus_missed +Total number of packets that quick first-level test inside carp_forus() +had missed, so we needed second level expensive check. +These two sysctls exist to determine how well our light-weight test +copes with incoming packets. +If you see large missed to total ratio, please, report this to +.Nm +maintainers. .El .\".Sh ARP level load balancing .\"A @@ -181,6 +203,24 @@ .\"If the reply would be load balanced to second router, it will be .\"dropped since the second router has not yet received information about .\"the connection state. +.Sh COMPATIBILITY WITH VRRP +.Pp +.Nm +currently uses protocol version 2 and protocol identifier of 112. +This clashes with VRRPv2, but modern VRRP implementations are capable of +telling that incoming packets aren't real VRRP ones. +This will make VRRP-aware devices to log warning messages about +CARP packets without operational disruptions. +So, CARP and VRRPv2 now can most of the time coexist without +any particular problems on the control plane. +.Pp +Usage of +.Cm lladdr +parameter to set MAC address value to be different from VRRP's +00:00:5e:00:01 will make CARP and VRRP to peacefully coexist MAC-wise +(provided that new OUI base doesn't make +.Nm +to clash with other MAC's in the layer-2 segment). .Sh STATE CHANGE NOTIFICATIONS Sometimes it is useful to get notified about .Nm @@ -305,8 +345,9 @@ .Xr rc.conf 5 , .Xr devd.conf 5 , .Xr ifconfig 8 , -.Xr sysctl 8 -.Xr tcpdump 8 +.Xr sysctl 8 , +.Xr tcpdump 8 , +.Xr carp 9 .Sh HISTORY The .Nm Index: share/man/man9/Makefile =================================================================== --- share/man/man9/Makefile (revision 265916) +++ share/man/man9/Makefile (working copy) @@ -46,6 +46,7 @@ BUS_SETUP_INTR.9 \ bus_space.9 \ byteorder.9 \ + carp.9 \ cd.9 \ condvar.9 \ config_intrhook.9 \ Index: share/man/man9/carp.9 =================================================================== --- share/man/man9/carp.9 (revision 0) +++ share/man/man9/carp.9 (working copy) @@ -0,0 +1,123 @@ +.\" Copyright (c) 2014, Eygene Ryabinkin +.\" +.\" Redistribution and use in source and binary forms, with or without +.\" modification, are permitted provided that the following conditions +.\" are met: +.\" 1. Redistributions of source code must retain the above copyright +.\" notice, this list of conditions and the following disclaimer. +.\" 2. Redistributions in binary form must reproduce the above copyright +.\" notice, this list of conditions and the following disclaimer in the +.\" documentation and/or other materials provided with the distribution. +.\" +.\" THIS SOFTWARE IS PROVIDED BY THE PROJECT AND CONTRIBUTORS ``AS IS'' AND +.\" ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE +.\" IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE +.\" ARE DISCLAIMED. IN NO EVENT SHALL THE PROJECT OR CONTRIBUTORS BE LIABLE +.\" FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL +.\" DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS +.\" OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) +.\" HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT +.\" LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY +.\" OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF +.\" SUCH DAMAGE. +.\" +.\" $FreeBSD$ +.\" +.Dd February 21, 2013 +.Dt CARP 9 +.Os +.Sh NAME +.Nm carp +.Nd Common Address Redundancy Protocol +.Sh SYNOPSIS +.In sys/param.h +.In sys/ioctl.h +.Pp +.In netinet/ip_carp.h +.Sh DESCRIPTION +The CARP allows multiple hosts on the same local network to share a set of +IPv4 and/or IPv6 addresses. +Its primary purpose is to ensure that these +addresses are always available. +.Sh IOCTL INTERFACE +.Pp +Most +.Nm +ioctl calls use +.Ft struct carpreq . +Pointer to this structure (or array of) is to be passed as the +.Va ifr_data +member of +.Ft struct ifreq . +Members of +.Ft struct carpreq +have the following meaning: +.Bl -tag -width indent +.It Va carpr_count +specifies the length of +.Ft struct carpreq +array passed as +.Va ifr_data . +.It Va carpr_vhid +specifies +.Nm +VHID we work with. +On +.Nm SIOCGVH +.Va carpr_vhid +can be zero, it means that we need information about all VHIDs +in the system. +.It Va carpr_state +carries interface state. +0 means +.Qq INIT , +1 means +.Qq BACKUP +and 2 is +.Qq MASTER . +.It Va carpr_advskew +advertisement skew in 1/256 of seconds. +Negative value means +.Qq do not alter the value . +.It Va carpr_advbase +base period for adverisements, in seconds. +Negative value means +.Qq do not alter the value . +.It Va carpr_lladdr[6] +specifies the value for the VHID's MAC address. +If all bytes set to 0xff, SIOCSVH will not alter MAC address. +.It Va carpr_key[CARP_KEY_LEN] +carries secret key for the VHID. +Empty string means +.Qq do not alter the value . +.El +.Pp +.Cm SIOCSVH +is used to set interface configuration. +.Va carpr_vhid +is mandatory for this ioctl. +.Pp +.Cm SIOCGVH +is used to get interface configuration. +.Va carpr_vhid +specifies VHID to get information for. +VHID of 0 will return information about every existing +.Nm +instance. +For the latter case the value of +.Va carpr_count +for the first array member on input specifies the size of +.Ft struct carpreq +array passed as +.Va ifr_data . +On output +.Va carpr_count +will specify the number of filled array items. +.Sh SEE ALSO +.Xr ioctl 2 , +.Xr carp 4 +.Sh HISTORY +The +.Nm +manual page was written for +.Fx 11 . Property changes on: share/man/man9/carp.9 ___________________________________________________________________ Added: svn:eol-style ## -0,0 +1 ## +native \ No newline at end of property Added: svn:mime-type ## -0,0 +1 ## +text/plain \ No newline at end of property Added: svn:keywords ## -0,0 +1 ## +FreeBSD=%H \ No newline at end of property Index: sys/net/ieee_oui.h =================================================================== --- sys/net/ieee_oui.h (revision 265916) +++ sys/net/ieee_oui.h (working copy) @@ -65,3 +65,7 @@ /* Allocate 64K to bhyve */ #define OUI_FREEBSD_BHYVE_LOW OUI_FREEBSD(0x000001) #define OUI_FREEBSD_BHYVE_HIGH OUI_FREEBSD(0x00ffff) + +/* Allocate 256 entries to CARP */ +#define OUI_FREEBSD_CARP_LOW OUI_FREEBSD(0x010000) +#define OUI_FREEBSD_CARP_HIGH OUI_FREEBSD(0x0100ff) Index: sys/netinet/ip_carp.c =================================================================== --- sys/netinet/ip_carp.c (revision 265916) +++ sys/netinet/ip_carp.c (working copy) @@ -36,6 +36,7 @@ #include #include #include +#include #include #include #include @@ -92,7 +93,8 @@ struct carp_softc { struct ifnet *sc_carpdev; /* Pointer to parent ifnet. */ struct ifaddr **sc_ifas; /* Our ifaddrs. */ - struct sockaddr_dl sc_addr; /* Our link level address. */ + u_char sc_lladdr[ETHER_ADDR_LEN]; + /* Our link level address, OUI-48 for now. */ struct callout sc_ad_tmo; /* Advertising timeout. */ #ifdef INET struct callout sc_md_tmo; /* Master down timeout. */ @@ -153,7 +155,34 @@ #define CARP_INET6 1 static int proto_reg[] = {-1, -1}; +static u_int32_t ouibase_hashtable[256]; + /* + * Counters for carp_forus(). + */ +/* All packets that were entered carp_forus() */ +static counter_u64_t carp_forus_total; +/* Packets that missed first-level test */ +static counter_u64_t carp_forus_missed; + +static int +sysctl_carp_forus_total(SYSCTL_HANDLER_ARGS) +{ + uint64_t count; + + count = counter_u64_fetch(carp_forus_total); + return (sysctl_handle_64(oidp, &count, sizeof(count), req)); +} +static int +sysctl_carp_forus_missed(SYSCTL_HANDLER_ARGS) +{ + uint64_t count; + + count = counter_u64_fetch(carp_forus_missed); + return (sysctl_handle_64(oidp, &count, sizeof(count), req)); +} + +/* * Brief design of carp(4). * * Any carp-capable ifnet may have a list of carp softcs hanging off @@ -231,6 +260,12 @@ SYSCTL_VNET_INT(_net_inet_carp, OID_AUTO, ifdown_demotion_factor, CTLFLAG_RW, &VNET_NAME(carp_ifdown_adj), 0, "Interface down demotion factor adjustment"); +SYSCTL_PROC(_net_inet_carp, OID_AUTO, forus_total, CTLTYPE_U64|CTLFLAG_RD, + 0, 0, sysctl_carp_forus_total, "QU", + "packets entered carp_forus() routine"); +SYSCTL_PROC(_net_inet_carp, OID_AUTO, forus_missed, CTLTYPE_U64|CTLFLAG_RD, + 0, 0, sysctl_carp_forus_missed, "QU", + "packets missed by level-1 check in carp_forus() routine"); VNET_PCPUSTAT_DEFINE(struct carpstats, carpstats); VNET_PCPUSTAT_SYSINIT(carpstats); @@ -316,6 +351,9 @@ static void carp_send_ad_all(void *, int); static void carp_demote_adj(int, char *); +static inline unsigned char + ouibase_crc8(u_int8_t[5]); + static LIST_HEAD(, carp_softc) carp_list; static struct mtx carp_mtx; static struct task carp_sendall_task = @@ -1017,7 +1055,7 @@ CARP_FOREACH_IFA(sc, ifa) if (ifa->ifa_addr->sa_family == AF_INET) - arp_ifinit2(sc->sc_carpdev, ifa, LLADDR(&sc->sc_addr)); + arp_ifinit2(sc->sc_carpdev, ifa, sc->sc_lladdr); } int @@ -1026,7 +1064,7 @@ struct carp_softc *sc = ifa->ifa_carp; if (sc->sc_state == MASTER) { - *enaddr = LLADDR(&sc->sc_addr); + *enaddr = sc->sc_lladdr; return (1); } @@ -1098,12 +1136,12 @@ sizeof(struct carp_softc *), M_NOWAIT); if (mtag == NULL) /* Better a bit than nothing. */ - return (LLADDR(&sc->sc_addr)); + return (sc->sc_lladdr); bcopy(&sc, mtag + 1, sizeof(sc)); m_tag_prepend(m, mtag); - return (LLADDR(&sc->sc_addr)); + return (sc->sc_lladdr); } IF_ADDR_RUNLOCK(ifp); @@ -1115,16 +1153,17 @@ carp_forus(struct ifnet *ifp, u_char *dhost) { struct carp_softc *sc; - uint8_t *ena = dhost; - if (ena[0] || ena[1] || ena[2] != 0x5e || ena[3] || ena[4] != 1) + counter_u64_add(carp_forus_total, 1); + + if (!ouibase_hashtable[ouibase_crc8(dhost)]) return (0); CIF_LOCK(ifp->if_carp); IFNET_FOREACH_CARP(ifp, sc) { CARP_LOCK(sc); - if (sc->sc_state == MASTER && !bcmp(dhost, LLADDR(&sc->sc_addr), - ETHER_ADDR_LEN)) { + if (sc->sc_state == MASTER && + !bcmp(dhost, sc->sc_lladdr, ETHER_ADDR_LEN)) { CARP_UNLOCK(sc); CIF_UNLOCK(ifp->if_carp); return (1); @@ -1133,6 +1172,8 @@ } CIF_UNLOCK(ifp->if_carp); + counter_u64_add(carp_forus_missed, 1); + return (0); } @@ -1434,12 +1475,7 @@ struct ether_header *eh; eh = mtod(m, struct ether_header *); - eh->ether_shost[0] = 0; - eh->ether_shost[1] = 0; - eh->ether_shost[2] = 0x5e; - eh->ether_shost[3] = 0; - eh->ether_shost[4] = 1; - eh->ether_shost[5] = sc->sc_vhid; + bcopy(sc->sc_lladdr, eh->ether_shost, ETHER_ADDR_LEN); } break; case IFT_FDDI: { @@ -1446,12 +1482,7 @@ struct fddi_header *fh; fh = mtod(m, struct fddi_header *); - fh->fddi_shost[0] = 0; - fh->fddi_shost[1] = 0; - fh->fddi_shost[2] = 0x5e; - fh->fddi_shost[3] = 0; - fh->fddi_shost[4] = 1; - fh->fddi_shost[5] = sc->sc_vhid; + bcopy(sc->sc_lladdr, fh->fddi_shost, FDDI_ADDR_LEN); } break; case IFT_ISO88025: { @@ -1547,6 +1578,7 @@ mtx_unlock(&carp_mtx); CARP_LOCK(sc); + ouibase_hashtable[ouibase_crc8(sc->sc_lladdr)]--; if (sc->sc_suppress) carp_demote_adj(-V_carp_ifdown_adj, "vhid removed"); callout_drain(&sc->sc_ad_tmo); @@ -1623,6 +1655,7 @@ bcopy(sc->sc_key, carpr->carpr_key, sizeof(carpr->carpr_key)); else bzero(carpr->carpr_key, sizeof(carpr->carpr_key)); + bcopy(sc->sc_lladdr, carpr->carpr_lladdr, sizeof(carpr->carpr_lladdr)); CARP_UNLOCK(sc); } @@ -1632,7 +1665,11 @@ struct carpreq carpr; struct ifnet *ifp; struct carp_softc *sc = NULL; - int error = 0, locked = 0; + int error = 0, locked = 0, eui_changed = 0; + /* + * If all bits in carpr_lladdr are set to 1, EUI will be ignored. + */ + u_char nop_lladdr[6] = { 0xff, 0xff, 0xff, 0xff, 0xff, 0xff }; if ((error = copyin(ifr->ifr_data, &carpr, sizeof carpr))) return (error); @@ -1678,16 +1715,32 @@ if (sc == NULL) { sc = carp_alloc(ifp); CARP_LOCK(sc); + sc->sc_lladdr[0] = CARP_OUIBASE0; + sc->sc_lladdr[1] = CARP_OUIBASE1; + sc->sc_lladdr[2] = CARP_OUIBASE2; + sc->sc_lladdr[3] = CARP_OUIBASE3; + sc->sc_lladdr[4] = CARP_OUIBASE4; + sc->sc_lladdr[5] = carpr.carpr_vhid; + ouibase_hashtable[ouibase_crc8(sc->sc_lladdr)]++; sc->sc_vhid = carpr.carpr_vhid; - LLADDR(&sc->sc_addr)[0] = 0; - LLADDR(&sc->sc_addr)[1] = 0; - LLADDR(&sc->sc_addr)[2] = 0x5e; - LLADDR(&sc->sc_addr)[3] = 0; - LLADDR(&sc->sc_addr)[4] = 1; - LLADDR(&sc->sc_addr)[5] = sc->sc_vhid; } else CARP_LOCK(sc); locked = 1; + if (bcmp(carpr.carpr_lladdr, nop_lladdr, sizeof(nop_lladdr)) != 0) { + bcopy(carpr.carpr_lladdr, sc->sc_lladdr, sizeof(sc->sc_lladdr)); + if (bcmp(carpr.carpr_lladdr, sc->sc_lladdr, + sizeof(carpr.carpr_lladdr) - 1) != 0) { + unsigned char old_bucket = + ouibase_crc8(sc->sc_lladdr); + unsigned char new_bucket = + ouibase_crc8(carpr.carpr_lladdr); + if (old_bucket != new_bucket) { + ouibase_hashtable[new_bucket]++; + ouibase_hashtable[old_bucket]--; + } + eui_changed = 1; + } + } if (carpr.carpr_advbase > 0) { if (carpr.carpr_advbase > 255 || carpr.carpr_advbase < CARP_DFLTINTV) { @@ -1707,6 +1760,13 @@ bcopy(carpr.carpr_key, sc->sc_key, sizeof(sc->sc_key)); carp_hmac_prepare(sc); } + /* + * Let CARP instance to go through BACKUP phase on EUI change: + * this, in particular, will make our stack to emit IPv4 ARP + * or IPv6 NA packet. + */ + if (sc->sc_state != INIT && eui_changed) + sc->sc_state = BACKUP; if (sc->sc_state != INIT && carpr.carpr_state != sc->sc_state) { switch (carpr.carpr_state) { @@ -2042,6 +2102,92 @@ return (0); } +#if 0 +/* + * Code to generate the below table. + * + * Polynomial was specifically chosen for the two standard + * OUI bases, 00:00:5e:00:01 and 58:9c:fc:01:00, it gives + * us the same slot for these two bases and evenly distributes + * all 5-octet values among buckets. There were 6 candidates + * with such properties, 0xef, 0xe5, 0xa1, 0x4b, 0x29, 0x27. + * First one was chosen, more-or-less randomly. + * + * See http://codelabs.ru/fbsd/carp-ouibase/ for the reference + * codes. You may also want to look at + * http://users.ece.cmu.edu/~koopman/crc/ + * though it talks about more broad cases. + */ +#include + +int main(void) +{ + unsigned char poly = 0xef; + int i, j; + + printf("static unsigned char crc8_c2[256] = {"); + for (i = 0; i < 256; i++) { + unsigned char value = (unsigned char)i; + for (j = 0; j < 8; j++) { + if (value & 0x80) + value = (value << 1) ^ poly; + else + value <<= 1; + } + if (i % 8 == 0) + printf("\n "); + printf(" 0x%02x,", (int)value); + } + printf("\n};\n"); + return 0; +} +#endif +static unsigned char crc8_c2[256] = { + 0x00, 0xef, 0x31, 0xde, 0x62, 0x8d, 0x53, 0xbc, + 0xc4, 0x2b, 0xf5, 0x1a, 0xa6, 0x49, 0x97, 0x78, + 0x67, 0x88, 0x56, 0xb9, 0x05, 0xea, 0x34, 0xdb, + 0xa3, 0x4c, 0x92, 0x7d, 0xc1, 0x2e, 0xf0, 0x1f, + 0xce, 0x21, 0xff, 0x10, 0xac, 0x43, 0x9d, 0x72, + 0x0a, 0xe5, 0x3b, 0xd4, 0x68, 0x87, 0x59, 0xb6, + 0xa9, 0x46, 0x98, 0x77, 0xcb, 0x24, 0xfa, 0x15, + 0x6d, 0x82, 0x5c, 0xb3, 0x0f, 0xe0, 0x3e, 0xd1, + 0x73, 0x9c, 0x42, 0xad, 0x11, 0xfe, 0x20, 0xcf, + 0xb7, 0x58, 0x86, 0x69, 0xd5, 0x3a, 0xe4, 0x0b, + 0x14, 0xfb, 0x25, 0xca, 0x76, 0x99, 0x47, 0xa8, + 0xd0, 0x3f, 0xe1, 0x0e, 0xb2, 0x5d, 0x83, 0x6c, + 0xbd, 0x52, 0x8c, 0x63, 0xdf, 0x30, 0xee, 0x01, + 0x79, 0x96, 0x48, 0xa7, 0x1b, 0xf4, 0x2a, 0xc5, + 0xda, 0x35, 0xeb, 0x04, 0xb8, 0x57, 0x89, 0x66, + 0x1e, 0xf1, 0x2f, 0xc0, 0x7c, 0x93, 0x4d, 0xa2, + 0xe6, 0x09, 0xd7, 0x38, 0x84, 0x6b, 0xb5, 0x5a, + 0x22, 0xcd, 0x13, 0xfc, 0x40, 0xaf, 0x71, 0x9e, + 0x81, 0x6e, 0xb0, 0x5f, 0xe3, 0x0c, 0xd2, 0x3d, + 0x45, 0xaa, 0x74, 0x9b, 0x27, 0xc8, 0x16, 0xf9, + 0x28, 0xc7, 0x19, 0xf6, 0x4a, 0xa5, 0x7b, 0x94, + 0xec, 0x03, 0xdd, 0x32, 0x8e, 0x61, 0xbf, 0x50, + 0x4f, 0xa0, 0x7e, 0x91, 0x2d, 0xc2, 0x1c, 0xf3, + 0x8b, 0x64, 0xba, 0x55, 0xe9, 0x06, 0xd8, 0x37, + 0x95, 0x7a, 0xa4, 0x4b, 0xf7, 0x18, 0xc6, 0x29, + 0x51, 0xbe, 0x60, 0x8f, 0x33, 0xdc, 0x02, 0xed, + 0xf2, 0x1d, 0xc3, 0x2c, 0x90, 0x7f, 0xa1, 0x4e, + 0x36, 0xd9, 0x07, 0xe8, 0x54, 0xbb, 0x65, 0x8a, + 0x5b, 0xb4, 0x6a, 0x85, 0x39, 0xd6, 0x08, 0xe7, + 0x9f, 0x70, 0xae, 0x41, 0xfd, 0x12, 0xcc, 0x23, + 0x3c, 0xd3, 0x0d, 0xe2, 0x5e, 0xb1, 0x6f, 0x80, + 0xf8, 0x17, 0xc9, 0x26, 0x9a, 0x75, 0xab, 0x44, +}; + +static inline unsigned char +ouibase_crc8(u_int8_t ouibase[5]) +{ + /* Initial CRC value is 0 */ + unsigned char crc = crc8_c2[ouibase[0]]; + crc = crc8_c2[ouibase[1]^crc]; + crc = crc8_c2[ouibase[2]^crc]; + crc = crc8_c2[ouibase[3]^crc]; + return crc8_c2[ouibase[4]^crc]; +} + #ifdef INET extern struct domain inetdomain; static struct protosw in_carp_protosw = { @@ -2103,6 +2249,9 @@ mtx_unlock(&carp_mtx); taskqueue_drain(taskqueue_swi, &carp_sendall_task); mtx_destroy(&carp_mtx); + + counter_u64_free(carp_forus_total); + counter_u64_free(carp_forus_missed); } static int @@ -2110,6 +2259,13 @@ { int err; + bzero (ouibase_hashtable, sizeof(ouibase_hashtable)); + + carp_forus_total = counter_u64_alloc(M_WAITOK); + carp_forus_missed = counter_u64_alloc(M_WAITOK); + counter_u64_zero(carp_forus_total); + counter_u64_zero(carp_forus_missed); + mtx_init(&carp_mtx, "carp_mtx", NULL, MTX_DEF); LIST_INIT(&carp_list); carp_get_vhid_p = carp_get_vhid; Index: sys/netinet/ip_carp.h =================================================================== --- sys/netinet/ip_carp.h (revision 265916) +++ sys/netinet/ip_carp.h (working copy) @@ -93,6 +93,27 @@ /* carp_advbase */ #define CARP_DFLTINTV 1 +/* CARP FreeBSD OUI base */ +#define CARP_FBSD_OUIBASE0 0x58 +#define CARP_FBSD_OUIBASE1 0x9c +#define CARP_FBSD_OUIBASE2 0xfc +#define CARP_FBSD_OUIBASE3 0x01 +#define CARP_FBSD_OUIBASE4 0x00 + +/* Historical CARP OUI base */ +#define CARP_OLD_OUIBASE0 0x00 +#define CARP_OLD_OUIBASE1 0x00 +#define CARP_OLD_OUIBASE2 0x5e +#define CARP_OLD_OUIBASE3 0x00 +#define CARP_OLD_OUIBASE4 0x01 + +/* Default CARP OUI base */ +#define CARP_OUIBASE0 CARP_FBSD_OUIBASE0 +#define CARP_OUIBASE1 CARP_FBSD_OUIBASE1 +#define CARP_OUIBASE2 CARP_FBSD_OUIBASE2 +#define CARP_OUIBASE3 CARP_FBSD_OUIBASE3 +#define CARP_OUIBASE4 CARP_FBSD_OUIBASE4 + /* * Statistics. */ @@ -118,7 +139,7 @@ }; /* - * Configuration structure for SIOCSVH SIOCGVH + * Configuration structure for SIOCSVH and SIOCGVH */ struct carpreq { int carpr_count; @@ -131,6 +152,7 @@ #define CARP_MAXSKEW 240 int carpr_advbase; unsigned char carpr_key[CARP_KEY_LEN]; + u_int8_t carpr_lladdr[6]; }; #define SIOCSVH _IOWR('i', 245, struct ifreq) #define SIOCGVH _IOWR('i', 246, struct ifreq)