Thx to 2 people who spotted a bug in my patch, (missing parenthesis).

here is the updated version.

On Mon, Oct 21, 2019 at 3:59 PM Baptiste <[email protected]> wrote:

> hi there,
>
> Following up some recent discussion about SRV record's weight and server
> weight in HAProxy, we spotted a bug in the current code: when weight in SRV
> record is set to 0, then server weight in HAProxy was 1...
> Thanks to Willy for proposing the solution applied into that patch.
>
> Baptiste
>
From a8467daeb5cf2129f5471ef117039f778c2842fd Mon Sep 17 00:00:00 2001
From: Baptiste Assmann <[email protected]>
Date: Mon, 21 Oct 2019 15:13:48 +0200
Subject: [PATCH] BUG/MINOR: dns: allow srv record weight set to 0

Processing of SRV record weight was inaccurate and when a SRV record's
weight was set to 0, HAProxy enforced it to '1'.
This patch aims at fixing this without breaking compability with
previous behavior.

Backport status: 1.8 to 2.0
---
 src/dns.c | 16 ++++++++++------
 1 file changed, 10 insertions(+), 6 deletions(-)

diff --git a/src/dns.c b/src/dns.c
index 0ce6e8302..3e1bb421e 100644
--- a/src/dns.c
+++ b/src/dns.c
@@ -543,10 +543,12 @@ static void dns_check_dns_response(struct dns_resolution *res)
 				    !memcmp(srv->hostname_dn, item->target, item->data_len)) {
 					int ha_weight;
 
-					/* Make sure weight is at least 1, so
-					 * that the server will be used.
+					/* DNS weight range if from 0 to 65535
+					 * HAProxy weight is from 0 to 255
+					 * The rule below ensures that weight 0 is well respected
+					 * while allowing a "mapping" from DNS weight into HAProxy's one.
 					 */
-					ha_weight = item->weight / 256 + 1;
+					ha_weight = (item->weight + 255) / 256;
 					if (srv->uweight != ha_weight) {
 						char weight[9];
 
@@ -590,10 +592,12 @@ static void dns_check_dns_response(struct dns_resolution *res)
 				    !(srv->flags & SRV_F_CHECKPORT))
 					srv->check.port = item->port;
 
-				/* Make sure weight is at least 1, so
-				 * that the server will be used.
+				/* DNS weight range if from 0 to 65535
+				 * HAProxy weight is from 0 to 255
+				 * The rule below ensures that weight 0 is well respected
+				 * while allowing a "mapping" from DNS weight into HAProxy's one.
 				 */
-				ha_weight = item->weight / 256 + 1;
+				ha_weight = (item->weight + 255) / 256;
 
 				snprintf(weight, sizeof(weight), "%d", ha_weight);
 				server_parse_weight_change_request(srv, weight);
-- 
2.17.1

Reply via email to