This is an automated email from the ASF dual-hosted git repository.

RosiKyu pushed a commit to branch main
in repository https://gitbox.apache.org/repos/asf/cloudstack.git


The following commit(s) were added to refs/heads/main by this push:
     new 6a0fcd6efe7 LB: per-rule keepalive and timeouts, plus three HAProxy 
config fixes (#14131)
6a0fcd6efe7 is described below

commit 6a0fcd6efe77c8fc9e44a76c92857245184e2d1d
Author: Brad House - Nexthop <[email protected]>
AuthorDate: Wed Nov 18 05:18:49 2026 -0500

    LB: per-rule keepalive and timeouts, plus three HAProxy config fixes 
(#14131)
    
    * Use a keepalive option HAProxy still accepts
    
    With keepalive enabled, an LB rule that also uses HTTP stickiness or SSL
    offload emitted "no option forceclose". HAProxy has rejected that keyword
    since 2.0, and the system VM has shipped 2.x for several releases:
    
      [ALERT] config : parsing [haproxy.cfg:22]: option 'forceclose' is not
      supported any more since HAProxy 2.0, please just remove it, or use
      'option httpclose' if absolutely needed.
    
    It is a fatal parse error, so haproxy keeps running the previous config
    and the rule silently never takes effect.
    
    Replaced with "option http-keep-alive", which says the same thing and is
    valid on every version the system VM has shipped.
    
    Signed-off-by: Brad House <[email protected]>
    
    * Stop one load balancer's config leaking into the next
    
    The global and defaults sections are static arrays. Wrapping them in
    Arrays.asList() and calling set() writes through to the array, so a value
    from one LB rule stays there for every config generated afterwards:
    
      - keepalive on any rule leaves "no option httpclose" in the defaults
        for every later rule, in any network, in any account
      - an idle timeout from one rule becomes the default for the next
      - idleTimeout 0 blanks the timeouts permanently
    
    Copy the arrays instead. Same output, no shared state.
    
    Signed-off-by: Brad House <[email protected]>
    
    * Replace appsession, removed from HAProxy in 1.6
    
    An LB rule with AppCookie stickiness emitted "appsession", which haproxy
    has refused since 1.6:
    
      [ALERT] config : 'appsession' is not supported anymore since HAProxy 1.6.
      [ALERT] config : Fatal errors found in configuration.
    
    The whole file is rejected, so every rule on that router keeps serving
    its previous config, not just the one with the policy. The VR advertises
    AppCookie as supported, so this is reachable from the API.
    
    A stick table on the cookie is the documented replacement:
    
      stick-table type string len <length> size 10k expire <holdtime>
      stick store-response res.cook(<name>)
      stick match req.cook(<name>)
      stick store-request req.cook(<name>)      # only with request-learn
    
    The prefix and mode options have no equivalent and are now logged and
    ignored. They were never applied - the directive carrying them was
    rejected - so no working behaviour changes.
    
    Signed-off-by: Brad House <[email protected]>
    
    * Make keepalive and timeouts settable per load balancer rule
    
    #12586 made the idle timeout global. Keepalive is set on the network
    offering, at create time only, with no UI. Both land in the haproxy
    defaults section, so every rule on a router shares them.
    
    Three optional parameters on createLoadBalancerRule and
    updateLoadBalancerRule, each written to that rule's own listen section:
    
      | parameter        | haproxy directive                    | unset      |
      | keepalive        | option http-keep-alive / httpclose   | offering   |
      | idletimeout      | timeout client, timeout server       | global     |
      | keepalivetimeout | timeout http-keep-alive              | idletimeout|
    
    Held as firewall rule details, so no schema change.
    
    A rule on port 80 can now have keepalive and X-Forwarded-For together,
    which no combination of the existing settings can produce:
    
      offering flag off   mode http   closes per response   XFF works
      offering flag on    mode tcp    reuses connections    XFF lost
      per rule            mode http   reuses connections    XFF works
    
    A rule that sets nothing behaves exactly as before, including the fall
    back to tcp mode when the offering flag is on.
    
    Negative timeouts are rejected at the API and dropped in the generator.
    Haproxy treats one as a fatal parse error, which would strand every rule
    on the router.
    
    Applies to public LB rules. Application load balancers are created
    through a different command and are not covered.
    
    Signed-off-by: Brad House <[email protected]>
    
    * Check the per rule settings in the VR health check
    
    haproxy_check.py compares the router's haproxy.cfg against what the
    management server thinks it configured. It knew about maxconn and the
    global idle timeout, so a rule's own keepalive and timeouts could drift
    without anything noticing.
    
    The health check data now carries the three per rule values, empty when
    the rule inherits, and the check validates them in that rule's listen
    section:
    
      ruleKeepAlive         option http-keep-alive, or option httpclose
      ruleIdleTimeout       timeout client, timeout server
      ruleKeepAliveTimeout  timeout http-keep-alive
    
    The http mode test needed widening too. It read the offering flag alone,
    so a rule that sets keepalive itself stopped being checked at all:
    
      before   port 80 and the offering has keepalive off
      after    port 80 and (the rule sets keepalive, or the offering has it off)
    
    An older management server sends none of these keys, and a rule that sets
    nothing sends them empty. Both read as nothing to check.
    
    Signed-off-by: Brad House <[email protected]>
    
    * Pass the update command's rule id to the connection settings
    
    lb was looked up by that same id and is non null by this point, so the
    Long unboxes safely. The comment claiming otherwise was wrong, and the
    id now matches the one applyLoadBalancerConfig gets a few lines later.
    
    Signed-off-by: Brad House <[email protected]>
    
    * Let updateLoadBalancerRule put a rule's settings back to the default
    
    Once a rule had its own keepalive or timeout there was no way back to
    inheriting. Leaving a parameter out keeps the current value, and there
    is no value that means unset: negative timeouts are refused, and
    keepalive is a boolean.
    
    updateLoadBalancerRule takes cleanupconnectionsettings, after the
    cleanupdetails flag on updateVirtualMachine and updateTemplate. It drops
    all three of the rule's own settings, then sets again any passed in the
    same call, so one call can reset one and keep the rest:
    
      rule holds        call                                  rule holds after
      idletimeout=5000  cleanupconnectionsettings=true        nothing
      keepalive=true
      idletimeout=5000  cleanupconnectionsettings=true        idletimeout=5000
      keepalive=true      idletimeout=5000
      idletimeout=5000  keepalivetimeout=2000                 idletimeout=5000
                                                              
keepalivetimeout=2000
    
    A change is now worked out by comparing the settings before and after,
    so a cleanup on a rule that holds none does not reapply the config.
    
    The update parameter descriptions said an unset value inherits, which is
    only true on create. They now say it keeps the current value.
    
    Signed-off-by: Brad House <[email protected]>
    
    * Send cleanupconnectionsettings when a field is cleared in the UI
    
    Clearing keepalive, idle timeout or keepalive timeout in the edit rule
    dialog dropped the parameter, which the API reads as keep the current
    value, so the old setting stayed. When a field the rule had set is now
    blank the dialog sends cleanupconnectionsettings, along with the fields
    still filled in.
    
    Signed-off-by: Brad House <[email protected]>
    
    * Roll back a rule's connection settings when the router update fails
    
    When applying an updated rule to the router fails, updateLoadBalancerRule
    puts back the name, description, algorithm and cidrs, but not the
    keepalive and timeouts. Those stayed in the database while the router
    still ran the old values, and the next successful apply of anything on
    that router would quietly push them.
    
    The settings held before the update are now restored along with the
    rest.
    
    They were also written before the provider validated the rule, so a
    rule it rejected kept the new settings. The other fields only reach the
    database at _lbDao.update, after validation; the settings are now
    written at the same point.
    
    Signed-off-by: Brad House <[email protected]>
    
    * Mark the new load balancer parameters as since 24.0
    
    The keepalive, idle timeout and keepalive timeout parameters, and the
    cleanupconnectionsettings flag, were marked since 4.23.0. That release
    shipped without them, and main is now 24.0.0-SNAPSHOT following the
    vote to drop the "4." prefix starting with release 24.
    
    Signed-off-by: Brad House <[email protected]>
    
    * Give the update load balancer test command a rule id
    
    updateLoadBalancerRule now passes the command's id to the connection
    settings, which take a long. UpdateLoadBalancerTest built its command
    without an id, so the null unboxed and testValidateRuleBeforeUpdateLB
    threw. The API marks id as required, so only the test could get there.
    
    Set the id on the command and stub the DAO calls with it, and mock the
    firewall rule details DAO the settings are read from. testRuleNotValidated
    only passed before because the null id missed the lookup and raised the
    same exception type. It now finds the rule and fails on validation, which
    is what it means to test.
    
    Signed-off-by: Brad House <[email protected]>
    
    * Do not fail a rule update when the rule has no protocol
    
    updateLoadBalancerRule called lb.getLbProtocol().equals(...) after
    applying the config. lb_protocol is null for a rule created through the
    API without a protocol, so the update threw an NPE and reported failure
    although the change had already been applied to the router. Stock hits
    this on an algorithm or CIDR change; with the per-rule connection
    settings every keepalive or timeout update on such a rule hits it.
    
    Signed-off-by: Brad House <[email protected]>
    
    * Do not fail an update that only changes connection settings
    
    updateLoadBalancerRule took its result from _lbDao.update(), which
    returns false when no column of the rule changed. The keepalive and
    timeout settings are kept in firewall_rule_details, so an update that
    only changed them applied the new config to the router and then failed
    with "Failed to update load balancer rule". Count a settings change as
    an update. An update that changes nothing still fails, as before.
    
    Signed-off-by: Brad House <[email protected]>
    
    * Check a new rule's timeouts before the rule is created
    
    createLoadBalancerRule stored the per rule settings only after the
    rule had been created, so a refused keepalivetimeout or idletimeout
    left the rule behind in state Add. It held its port, and the next
    apply on the network pushed it to the router.
    
    Check the timeouts first, so a refused create leaves nothing. Also
    refuse a timeout above 2147483647 ms, which HAProxy rejects as a timer
    overflow; it used to reach the router and fail the whole apply.
    
    Signed-off-by: Brad House <[email protected]>
    
    ---------
    
    Signed-off-by: Brad House <[email protected]>
---
 .../com/cloud/agent/api/to/LoadBalancerTO.java     |  27 +++
 .../network/lb/LoadBalancingRulesService.java      |  14 ++
 .../java/com/cloud/network/rules/LoadBalancer.java |   5 +
 .../org/apache/cloudstack/api/ApiConstants.java    |   4 +
 .../loadbalancer/CreateLoadBalancerRuleCmd.java    |  31 +++
 .../loadbalancer/UpdateLoadBalancerRuleCmd.java    |  41 ++++
 .../api/response/LoadBalancerResponse.java         |  24 ++
 .../CreateLoadBalancerRuleCmdTest.java             |  50 ++++
 .../com/cloud/network/HAProxyConfigurator.java     |  85 ++++---
 .../com/cloud/network/HAProxyConfiguratorTest.java | 173 ++++++++++++++
 .../main/java/com/cloud/api/ApiResponseHelper.java |  21 ++
 .../network/lb/LoadBalancingRulesManagerImpl.java  | 104 +++++++-
 .../cloud/network/router/CommandSetupHelper.java   |  29 +++
 .../router/VirtualNetworkApplianceManagerImpl.java |  24 +-
 .../lb/LoadBalancingRulesManagerImplTest.java      | 262 +++++++++++++++++++++
 .../cloud/network/lb/UpdateLoadBalancerTest.java   |  15 +-
 .../debian/root/health_checks/haproxy_check.py     |  58 ++++-
 ui/public/locales/en.json                          |   7 +
 ui/src/views/network/LoadBalancing.vue             |  81 ++++++-
 19 files changed, 1011 insertions(+), 44 deletions(-)

diff --git a/api/src/main/java/com/cloud/agent/api/to/LoadBalancerTO.java 
b/api/src/main/java/com/cloud/agent/api/to/LoadBalancerTO.java
index 6c4b9e607c5..f9ab5d2d2e6 100644
--- a/api/src/main/java/com/cloud/agent/api/to/LoadBalancerTO.java
+++ b/api/src/main/java/com/cloud/agent/api/to/LoadBalancerTO.java
@@ -54,6 +54,9 @@ public class LoadBalancerTO {
     final static int MAX_HEALTHCHECK_POLICIES = 1;
 
     private String cidrList;
+    private Boolean keepAlive;
+    private Long idleTimeout;
+    private Long keepAliveTimeout;
 
     public LoadBalancerTO(String uuid, String srcIp, int srcPort, String 
protocol, String algorithm, boolean revoked, boolean alreadyAdded, boolean 
inline,
             List<LbDestination> destinations) {
@@ -249,6 +252,30 @@ public class LoadBalancerTO {
         return cidrList;
     }
 
+    public void setKeepAlive(Boolean keepAlive) {
+        this.keepAlive = keepAlive;
+    }
+
+    public Boolean getKeepAlive() {
+        return keepAlive;
+    }
+
+    public void setIdleTimeout(Long idleTimeout) {
+        this.idleTimeout = idleTimeout;
+    }
+
+    public Long getIdleTimeout() {
+        return idleTimeout;
+    }
+
+    public void setKeepAliveTimeout(Long keepAliveTimeout) {
+        this.keepAliveTimeout = keepAliveTimeout;
+    }
+
+    public Long getKeepAliveTimeout() {
+        return keepAliveTimeout;
+    }
+
     public static class StickinessPolicyTO {
         private String methodName;
         private List<Pair<String, String>> params;
diff --git 
a/api/src/main/java/com/cloud/network/lb/LoadBalancingRulesService.java 
b/api/src/main/java/com/cloud/network/lb/LoadBalancingRulesService.java
index b7fe3b26761..75bc0523e0c 100644
--- a/api/src/main/java/com/cloud/network/lb/LoadBalancingRulesService.java
+++ b/api/src/main/java/com/cloud/network/lb/LoadBalancingRulesService.java
@@ -70,6 +70,20 @@ public interface LoadBalancingRulesService {
 
     LoadBalancer updateLoadBalancerRule(UpdateLoadBalancerRuleCmd cmd);
 
+    /**
+     * Stores the optional per rule haproxy settings. A null leaves the 
current value alone.
+     *
+     * @param cleanup remove all three first, so the rule inherits again, then 
store any non null value given
+     * @return true if any value changed
+     */
+    boolean updateLoadBalancerConnectionSettings(long lbRuleId, Boolean 
keepAlive, Long idleTimeout, Long keepAliveTimeout, boolean cleanup);
+
+    /**
+     * Checks the per rule timeouts without storing them, so a create can 
refuse a bad value
+     * before the rule exists. A null is not checked.
+     */
+    void validateLoadBalancerConnectionSettings(Long idleTimeout, Long 
keepAliveTimeout);
+
     boolean deleteLoadBalancerRule(long lbRuleId, boolean apply);
 
     /**
diff --git a/api/src/main/java/com/cloud/network/rules/LoadBalancer.java 
b/api/src/main/java/com/cloud/network/rules/LoadBalancer.java
index 9b4e991b5b5..5c64e018acb 100644
--- a/api/src/main/java/com/cloud/network/rules/LoadBalancer.java
+++ b/api/src/main/java/com/cloud/network/rules/LoadBalancer.java
@@ -21,6 +21,11 @@ package com.cloud.network.rules;
  */
 public interface LoadBalancer extends FirewallRule, LoadBalancerContainer {
 
+    // Optional per rule haproxy settings, held as firewall rule details. 
Absent means inherit.
+    String KEEPALIVE = "keepalive";
+    String IDLE_TIMEOUT = "idletimeout";
+    String KEEPALIVE_TIMEOUT = "keepalivetimeout";
+
     int getDefaultPortStart();
 
     int getDefaultPortEnd();
diff --git a/api/src/main/java/org/apache/cloudstack/api/ApiConstants.java 
b/api/src/main/java/org/apache/cloudstack/api/ApiConstants.java
index 0711444c061..1004d031625 100644
--- a/api/src/main/java/org/apache/cloudstack/api/ApiConstants.java
+++ b/api/src/main/java/org/apache/cloudstack/api/ApiConstants.java
@@ -377,6 +377,7 @@ public class ApiConstants {
     public static final String IS_ISO = "isiso";
     public static final String IS_PORTABLE = "isportable";
     public static final String IS_PUBLIC = "ispublic";
+    public static final String IDLE_TIMEOUT = "idletimeout";
     public static final String IS_PERSISTENT = "ispersistent";
     public static final String EGRESS_DEFAULT_POLICY = "egressdefaultpolicy";
     public static final String IS_READY = "isready";
@@ -388,6 +389,8 @@ public class ApiConstants {
     public static final String JAVA_VERSION = "javaversion";
     public static final String JOB_ID = "jobid";
     public static final String JOB_STATUS = "jobstatus";
+    public static final String KEEPALIVE = "keepalive";
+    public static final String KEEPALIVE_TIMEOUT = "keepalivetimeout";
     public static final String KEEPALIVE_ENABLED = "keepaliveenabled";
     public static final String KERNEL_VERSION = "kernelversion";
     public static final String KEYPAIR_ID = "keypairid";
@@ -1253,6 +1256,7 @@ public class ApiConstants {
     public static final String OVM3_POOL = "ovm3pool";
     public static final String OVM3_CLUSTER = "ovm3cluster";
     public static final String OVM3_VIP = "ovm3vip";
+    public static final String CLEAN_UP_CONNECTION_SETTINGS = 
"cleanupconnectionsettings";
     public static final String CLEAN_UP_DETAILS = "cleanupdetails";
     public static final String CLEAN_UP_EXTERNAL_DETAILS = 
"cleanupexternaldetails";
     public static final String CLEAN_UP_EXTRA_CONFIG = "cleanupextraconfig";
diff --git 
a/api/src/main/java/org/apache/cloudstack/api/command/user/loadbalancer/CreateLoadBalancerRuleCmd.java
 
b/api/src/main/java/org/apache/cloudstack/api/command/user/loadbalancer/CreateLoadBalancerRuleCmd.java
index bd72f248364..3dad541b61f 100644
--- 
a/api/src/main/java/org/apache/cloudstack/api/command/user/loadbalancer/CreateLoadBalancerRuleCmd.java
+++ 
b/api/src/main/java/org/apache/cloudstack/api/command/user/loadbalancer/CreateLoadBalancerRuleCmd.java
@@ -119,6 +119,22 @@ public class CreateLoadBalancerRuleCmd extends 
BaseAsyncCreateCmd /*implements L
     @Parameter(name = ApiConstants.FOR_DISPLAY, type = CommandType.BOOLEAN, 
description = "An optional field, whether to the display the rule to the end 
user or not", since = "4.4", authorized = {RoleType.Admin})
     private Boolean display;
 
+    @Parameter(name = ApiConstants.KEEPALIVE, type = CommandType.BOOLEAN, 
since = "24.0",
+            description = "Whether the load balancer keeps client connections 
open between requests. "
+                    + "Only applies to rules the router serves in HTTP mode. 
If not set, the network offering's setting is used.")
+    private Boolean keepAlive;
+
+    @Parameter(name = ApiConstants.IDLE_TIMEOUT, type = CommandType.LONG, 
since = "24.0",
+            description = "How long an idle connection is held open, in 
milliseconds. Use 0 for infinite. "
+                    + "If not set, the global setting 
network.loadbalancer.haproxy.idle.timeout is used.")
+    private Long idleTimeout;
+
+    @Parameter(name = ApiConstants.KEEPALIVE_TIMEOUT, type = CommandType.LONG, 
since = "24.0",
+            description = "How long an idle keepalive connection is held open 
waiting for the next request, "
+                    + "in milliseconds. Only applies to rules the router 
serves in HTTP mode. "
+                    + "If not set, idletimeout applies.")
+    private Long keepAliveTimeout;
+
     /////////////////////////////////////////////////////
     /////////////////// Accessors ///////////////////////
     /////////////////////////////////////////////////////
@@ -135,6 +151,18 @@ public class CreateLoadBalancerRuleCmd extends 
BaseAsyncCreateCmd /*implements L
     public String getAlgorithm() {
         return algorithm;
     }
+    public Boolean getKeepAlive() {
+        return keepAlive;
+    }
+
+    public Long getIdleTimeout() {
+        return idleTimeout;
+    }
+    public Long getKeepAliveTimeout() {
+        return keepAliveTimeout;
+    }
+
+
 
     public String getDescription() {
         return description;
@@ -301,12 +329,15 @@ public class CreateLoadBalancerRuleCmd extends 
BaseAsyncCreateCmd /*implements L
     @Override
     public void create() {
         try {
+            // Check the settings before the rule exists: a refused create 
must leave no rule behind
+            
_lbService.validateLoadBalancerConnectionSettings(getIdleTimeout(), 
getKeepAliveTimeout());
             LoadBalancer result =
                 _lbService.createPublicLoadBalancerRule(getXid(), getName(), 
getDescription(), getSourcePortStart(), getSourcePortEnd(), 
getDefaultPortStart(),
                     getDefaultPortEnd(), getSourceIpAddressId(), 
getProtocol(), getAlgorithm(), getNetworkId(), getEntityOwnerId(), 
getOpenFirewall(), getLbProtocol(), isDisplay(),
                         getCidrList());
             this.setEntityId(result.getId());
             this.setEntityUuid(result.getUuid());
+            _lbService.updateLoadBalancerConnectionSettings(result.getId(), 
getKeepAlive(), getIdleTimeout(), getKeepAliveTimeout(), false);
         } catch (NetworkRuleConflictException e) {
             logger.warn("Exception: ", e);
             throw new 
ServerApiException(ApiErrorCode.NETWORK_RULE_CONFLICT_ERROR, e.getMessage());
diff --git 
a/api/src/main/java/org/apache/cloudstack/api/command/user/loadbalancer/UpdateLoadBalancerRuleCmd.java
 
b/api/src/main/java/org/apache/cloudstack/api/command/user/loadbalancer/UpdateLoadBalancerRuleCmd.java
index 0ac99f1c760..ebb57fbe988 100644
--- 
a/api/src/main/java/org/apache/cloudstack/api/command/user/loadbalancer/UpdateLoadBalancerRuleCmd.java
+++ 
b/api/src/main/java/org/apache/cloudstack/api/command/user/loadbalancer/UpdateLoadBalancerRuleCmd.java
@@ -68,6 +68,31 @@ public class UpdateLoadBalancerRuleCmd extends 
BaseAsyncCustomIdCmd {
     @Parameter(name = ApiConstants.CIDR_LIST, type = CommandType.LIST, 
collectionType = CommandType.STRING, description = "the cidr list to forward 
traffic from", since = "4.22")
     private List<String> cidrList;
 
+    @Parameter(name = ApiConstants.KEEPALIVE, type = CommandType.BOOLEAN, 
since = "24.0",
+            description = "Whether the load balancer keeps client connections 
open between requests. "
+                    + "Only applies to rules the router serves in HTTP mode. 
If not set, the rule's current value is kept; "
+                    + "use cleanupconnectionsettings to go back to the network 
offering's setting.")
+    private Boolean keepAlive;
+
+    @Parameter(name = ApiConstants.IDLE_TIMEOUT, type = CommandType.LONG, 
since = "24.0",
+            description = "How long an idle connection is held open, in 
milliseconds. Use 0 for infinite. "
+                    + "If not set, the rule's current value is kept; use 
cleanupconnectionsettings to go back to "
+                    + "the global setting 
network.loadbalancer.haproxy.idle.timeout.")
+    private Long idleTimeout;
+
+    @Parameter(name = ApiConstants.KEEPALIVE_TIMEOUT, type = CommandType.LONG, 
since = "24.0",
+            description = "How long an idle keepalive connection is held open 
waiting for the next request, "
+                    + "in milliseconds. Only applies to rules the router 
serves in HTTP mode. "
+                    + "If not set, the rule's current value is kept; use 
cleanupconnectionsettings to go back to "
+                    + "following idletimeout.")
+    private Long keepAliveTimeout;
+
+    @Parameter(name = ApiConstants.CLEAN_UP_CONNECTION_SETTINGS, type = 
CommandType.BOOLEAN, since = "24.0",
+            description = "If true, the rule's own keepalive, idletimeout and 
keepalivetimeout are removed, so the rule goes back "
+                    + "to the network offering and global settings. Any of the 
three passed in the same call are then set again, "
+                    + "so a single call can reset one and keep the others. If 
false or not set, no action.")
+    private Boolean cleanupConnectionSettings;
+
     /////////////////////////////////////////////////////
     /////////////////// Accessors ///////////////////////
     /////////////////////////////////////////////////////
@@ -75,6 +100,22 @@ public class UpdateLoadBalancerRuleCmd extends 
BaseAsyncCustomIdCmd {
     public String getAlgorithm() {
         return algorithm;
     }
+    public Boolean getKeepAlive() {
+        return keepAlive;
+    }
+
+    public Long getIdleTimeout() {
+        return idleTimeout;
+    }
+    public Long getKeepAliveTimeout() {
+        return keepAliveTimeout;
+    }
+
+    public boolean isCleanupConnectionSettings() {
+        return Boolean.TRUE.equals(cleanupConnectionSettings);
+    }
+
+
 
     public String getDescription() {
         return description;
diff --git 
a/api/src/main/java/org/apache/cloudstack/api/response/LoadBalancerResponse.java
 
b/api/src/main/java/org/apache/cloudstack/api/response/LoadBalancerResponse.java
index c57323d439d..9de970f205d 100644
--- 
a/api/src/main/java/org/apache/cloudstack/api/response/LoadBalancerResponse.java
+++ 
b/api/src/main/java/org/apache/cloudstack/api/response/LoadBalancerResponse.java
@@ -63,6 +63,18 @@ public class LoadBalancerResponse extends BaseResponse 
implements ControlledEnti
     @Param(description = "The ID of the guest Network the LB rule belongs to")
     private String networkId;
 
+    @SerializedName(ApiConstants.KEEPALIVE)
+    @Param(description = "whether the load balancer keeps client connections 
open between requests, unset means the network offering's setting is used", 
since = "24.0")
+    private Boolean keepAlive;
+
+    @SerializedName(ApiConstants.IDLE_TIMEOUT)
+    @Param(description = "how long an idle connection is held open, in 
milliseconds, unset means the global setting is used", since = "24.0")
+    private Long idleTimeout;
+
+    @SerializedName(ApiConstants.KEEPALIVE_TIMEOUT)
+    @Param(description = "how long an idle keepalive connection is held open 
waiting for the next request, in milliseconds", since = "24.0")
+    private Long keepAliveTimeout;
+
     @SerializedName(ApiConstants.CIDR_LIST)
     @Param(description = "The CIDR list to allow traffic, all other CIDRs will 
be blocked. Multiple entries must be separated by a single comma character 
(,).")
     private String cidrList;
@@ -143,6 +155,18 @@ public class LoadBalancerResponse extends BaseResponse 
implements ControlledEnti
         this.cidrList = cidrs;
     }
 
+    public void setKeepAlive(Boolean keepAlive) {
+        this.keepAlive = keepAlive;
+    }
+
+    public void setIdleTimeout(Long idleTimeout) {
+        this.idleTimeout = idleTimeout;
+    }
+
+    public void setKeepAliveTimeout(Long keepAliveTimeout) {
+        this.keepAliveTimeout = keepAliveTimeout;
+    }
+
     public void setAlgorithm(String algorithm) {
         this.algorithm = algorithm;
     }
diff --git 
a/api/src/test/java/org/apache/cloudstack/api/command/user/loadbalancer/CreateLoadBalancerRuleCmdTest.java
 
b/api/src/test/java/org/apache/cloudstack/api/command/user/loadbalancer/CreateLoadBalancerRuleCmdTest.java
new file mode 100644
index 00000000000..15c40911ba4
--- /dev/null
+++ 
b/api/src/test/java/org/apache/cloudstack/api/command/user/loadbalancer/CreateLoadBalancerRuleCmdTest.java
@@ -0,0 +1,50 @@
+// Licensed to the Apache Software Foundation (ASF) under one
+// or more contributor license agreements.  See the NOTICE file
+// distributed with this work for additional information
+// regarding copyright ownership.  The ASF licenses this file
+// to you under the Apache License, Version 2.0 (the
+// "License"); you may not use this file except in compliance
+// with the License.  You may obtain a copy of the License at
+//
+//   http://www.apache.org/licenses/LICENSE-2.0
+//
+// Unless required by applicable law or agreed to in writing,
+// software distributed under the License is distributed on an
+// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+// KIND, either express or implied.  See the License for the
+// specific language governing permissions and limitations
+// under the License.
+package org.apache.cloudstack.api.command.user.loadbalancer;
+
+import org.apache.cloudstack.api.ApiErrorCode;
+import org.apache.cloudstack.api.ServerApiException;
+import org.junit.Assert;
+import org.junit.Test;
+import org.mockito.Mockito;
+import org.springframework.test.util.ReflectionTestUtils;
+
+import com.cloud.exception.InvalidParameterValueException;
+import com.cloud.network.lb.LoadBalancingRulesService;
+
+public class CreateLoadBalancerRuleCmdTest {
+
+    @Test
+    public void testCreateRefusesBadTimeoutBeforeTheRuleExists() throws 
Exception {
+        LoadBalancingRulesService lbService = 
Mockito.mock(LoadBalancingRulesService.class);
+        Mockito.doThrow(new InvalidParameterValueException("keepalivetimeout 
must be 0 or greater"))
+                .when(lbService).validateLoadBalancerConnectionSettings(null, 
-1L);
+
+        CreateLoadBalancerRuleCmd cmd = new CreateLoadBalancerRuleCmd();
+        cmd._lbService = lbService;
+        ReflectionTestUtils.setField(cmd, "keepAliveTimeout", -1L);
+
+        try {
+            cmd.create();
+            Assert.fail("A negative keepalivetimeout should be refused");
+        } catch (ServerApiException e) {
+            Assert.assertEquals(ApiErrorCode.PARAM_ERROR, e.getErrorCode());
+        }
+        Mockito.verify(lbService).validateLoadBalancerConnectionSettings(null, 
-1L);
+        Mockito.verifyNoMoreInteractions(lbService);
+    }
+}
diff --git a/core/src/main/java/com/cloud/network/HAProxyConfigurator.java 
b/core/src/main/java/com/cloud/network/HAProxyConfigurator.java
index b3f7da1c6d8..3be197953b2 100644
--- a/core/src/main/java/com/cloud/network/HAProxyConfigurator.java
+++ b/core/src/main/java/com/cloud/network/HAProxyConfigurator.java
@@ -20,7 +20,6 @@
 package com.cloud.network;
 
 import java.util.ArrayList;
-import java.util.Arrays;
 import java.util.HashMap;
 import java.util.HashSet;
 import java.util.List;
@@ -46,13 +45,15 @@ public class HAProxyConfigurator implements 
LoadBalancerConfigurator {
 
     protected Logger logger = LogManager.getLogger(getClass());
     private static final String blankLine = "\t ";
-    private static String[] globalSection = {"global", "\tlog 127.0.0.1:3914   
local0 warning", "\tmaxconn 4096", "\tmaxpipes 1024", "\tchroot 
/var/lib/haproxy",
-        "\tuser haproxy", "\tgroup haproxy", "\tstats socket 
/run/haproxy/admin.sock", "\tdaemon"};
+    // Immutable so a config cannot be built by writing into the shared copy.
+    private static final List<String> globalSection = List.of("global", "\tlog 
127.0.0.1:3914   local0 warning", "\tmaxconn 4096", "\tmaxpipes 1024",
+        "\tchroot /var/lib/haproxy", "\tuser haproxy", "\tgroup haproxy", 
"\tstats socket /run/haproxy/admin.sock", "\tdaemon");
 
-    private static String[] defaultsSection = {"defaults", "\tlog     global", 
"\tmode    tcp", "\toption  dontlognull", "\tretries 3", "\toption redispatch",
-        "\toption forwardfor", "\toption httpclose", "\ttimeout connect    
5000", "\ttimeout client     50000", "\ttimeout server     50000"};
+    private static final List<String> defaultsSection = List.of("defaults", 
"\tlog     global", "\tmode    tcp", "\toption  dontlognull", "\tretries 3",
+        "\toption redispatch", "\toption forwardfor", "\toption httpclose", 
"\ttimeout connect    5000", "\ttimeout client     50000",
+        "\ttimeout server     50000");
 
-    private static String[] defaultListen = {"listen  vmops", "\tbind 
0.0.0.0:9", "\toption transparent"};
+    private static final List<String> defaultListen = List.of("listen  vmops", 
"\tbind 0.0.0.0:9", "\toption transparent");
 
     private static final String SSL_CERTS_DIR = "/etc/cloudstack/ssl/";
 
@@ -80,16 +81,16 @@ public class HAProxyConfigurator implements 
LoadBalancerConfigurator {
 
         final List<String> result = new ArrayList<String>();
 
-        result.addAll(Arrays.asList(globalSection));
+        result.addAll(globalSection);
         result.add(blankLine);
-        result.addAll(Arrays.asList(defaultsSection));
+        result.addAll(defaultsSection);
         result.add(blankLine);
 
         if (pools.isEmpty()) {
             // haproxy cannot handle empty listen / frontend or backend, so add
             // a dummy listener
             // on port 9
-            result.addAll(Arrays.asList(defaultListen));
+            result.addAll(defaultListen);
         }
         result.add(blankLine);
 
@@ -451,15 +452,17 @@ public class HAProxyConfigurator implements 
LoadBalancerConfigurator {
                     
tempSb.append("appcookie_").append(srcip.hashCode()).append("_").append(lbTO.getSrcPort());
                     cookieName = tempSb.toString();
                 }
-                sb.append("\t").append("appsession 
").append(cookieName).append(" len ").append(length).append(" timeout 
").append(holdtime).append(" ");
-                if (prefix) {
-                    sb.append("prefix ");
-                }
+                // "appsession" was removed in haproxy 1.6 and is a fatal 
parse error on the
+                // versions the system VM ships. A stick table on the cookie 
is the replacement.
+                sb.append("\t").append("stick-table type string len 
").append(length).append(" size 10k expire ").append(holdtime).append("\n");
+                sb.append("\t").append("stick store-response 
res.cook(").append(cookieName).append(")").append("\n");
+                sb.append("\t").append("stick match 
req.cook(").append(cookieName).append(")").append("\n");
                 if (requestlearn) {
-                    sb.append("request-learn").append(" ");
+                    sb.append("\t").append("stick store-request 
req.cook(").append(cookieName).append(")").append("\n");
                 }
-                if (mode != null) {
-                    sb.append("mode ").append(mode).append(" ");
+                if (prefix || mode != null) {
+                    logger.warn("Haproxy stickiness policy for lb rule: {}:{}: 
prefix and mode are not supported since haproxy 1.6 and are ignored",
+                            lbTO.getSrcIp(), lbTO.getSrcPort());
                 }
             } else {
                 /*
@@ -477,6 +480,18 @@ public class HAProxyConfigurator implements 
LoadBalancerConfigurator {
         return sb.toString();
     }
 
+    /**
+     * Haproxy rejects a negative timeout, and one bad value costs the whole 
file. Drop it and keep
+     * whatever the defaults section says, the same way the global idle 
timeout does.
+     */
+    private Long timeoutOrNull(final LoadBalancerTO lbTO, final String name, 
final Long value) {
+        if (value != null && value < 0) {
+            logger.warn("Ignoring negative {} [{}] on lb rule {}:{}", name, 
value, lbTO.getSrcIp(), lbTO.getSrcPort());
+            return null;
+        }
+        return value;
+    }
+
     private List<String> getRulesForPool(final LoadBalancerTO lbTO, final 
LoadBalancerConfigCommand lbCmd) {
         StringBuilder sb = new StringBuilder();
         final String poolName = sb.append(lbTO.getSrcIp().replace(".", 
"_")).append('-').append(lbTO.getSrcPort()).toString();
@@ -569,12 +584,30 @@ public class HAProxyConfigurator implements 
LoadBalancerConfigurator {
         if (stickinessSubRule != null && !destsAvailable) {
             logger.warn("Haproxy stickiness policy for lb rule: " + 
lbTO.getSrcIp() + ":" + lbTO.getSrcPort() + ": Not Applied, cause:  backends 
are unavailable");
         }
-        boolean keepAliveEnabled = lbCmd.keepAliveEnabled;
-        boolean http = (publicPort == NetUtils.HTTP_PORT && !keepAliveEnabled);
-        if (http || httpbasedStickiness || sslOffloading) {
+        final Boolean ruleKeepAlive = lbTO.getKeepAlive();
+        final Long ruleIdleTimeout = timeoutOrNull(lbTO, "idletimeout", 
lbTO.getIdleTimeout());
+        final Long ruleKeepAliveTimeout = timeoutOrNull(lbTO, 
"keepalivetimeout", lbTO.getKeepAliveTimeout());
+        final boolean keepAliveEnabled = ruleKeepAlive != null ? ruleKeepAlive 
: lbCmd.keepAliveEnabled;
+        // A rule that asks for keepalive itself stays in http mode on port 
80, so forwardfor keeps
+        // working. Without it, keepalive falls back to tcp mode as it always 
has.
+        final boolean port80HttpMode = publicPort == NetUtils.HTTP_PORT && 
(ruleKeepAlive != null || !keepAliveEnabled);
+        final boolean httpMode = port80HttpMode || httpbasedStickiness || 
sslOffloading;
+        if (httpMode) {
             frontendConfigs.add("\tmode http");
-            String keepAliveLine = keepAliveEnabled ? "\tno option forceclose" 
: "\toption httpclose";
-            frontendConfigs.add(keepAliveLine);
+            frontendConfigs.add(keepAliveEnabled ? "\toption http-keep-alive" 
: "\toption httpclose");
+            if (keepAliveEnabled && ruleKeepAliveTimeout != null) {
+                frontendConfigs.add("\ttimeout http-keep-alive " + 
ruleKeepAliveTimeout);
+            } else if (ruleKeepAliveTimeout != null) {
+                logger.warn("Keepalive timeout ignored for lb rule {}:{}, 
keepalive is off for this rule",
+                        lbTO.getSrcIp(), lbTO.getSrcPort());
+            }
+        } else if (ruleKeepAlive != null || ruleKeepAliveTimeout != null) {
+            logger.warn("Keepalive ignored for lb rule {}:{}, it is served in 
tcp mode. Keepalive applies on port {}, "
+                    + "with ssl offload, or with http based stickiness.", 
lbTO.getSrcIp(), lbTO.getSrcPort(), NetUtils.HTTP_PORT);
+        }
+        if (ruleIdleTimeout != null) {
+            frontendConfigs.add("\ttimeout client     " + ruleIdleTimeout);
+            frontendConfigs.add("\ttimeout server     " + ruleIdleTimeout);
         }
 
         // add line like this: "listen  65_37_141_30-80\n\tbind 
65.37.141.30:80"
@@ -617,8 +650,7 @@ public class HAProxyConfigurator implements 
LoadBalancerConfigurator {
     @Override
     public String[] generateConfiguration(final LoadBalancerConfigCommand 
lbCmd) {
         final List<String> result = new ArrayList<String>();
-        final List<String> gSection = Arrays.asList(globalSection);
-        //        note that this is overwritten on the String in the static 
ArrayList<String>
+        final List<String> gSection = new ArrayList<>(globalSection);
         gSection.set(2, "\tmaxconn " + lbCmd.maxconn);
         // TODO DH: write test for this function
         final String pipesLine = "\tmaxpipes " + 
Long.toString(Long.parseLong(lbCmd.maxconn) / 4);
@@ -631,7 +663,7 @@ public class HAProxyConfigurator implements 
LoadBalancerConfigurator {
         result.addAll(gSection);
 
         result.add(blankLine);
-        final List<String> dSection = Arrays.asList(defaultsSection);
+        final List<String> dSection = new ArrayList<>(defaultsSection);
         if (lbCmd.keepAliveEnabled) {
             dSection.set(7, "\tno option httpclose");
         }
@@ -639,8 +671,7 @@ public class HAProxyConfigurator implements 
LoadBalancerConfigurator {
             dSection.set(9, "\ttimeout client     " + 
Long.toString(lbCmd.idleTimeout));
             dSection.set(10, "\ttimeout server     " + 
Long.toString(lbCmd.idleTimeout));
         } else if (lbCmd.idleTimeout == 0) {
-            // .remove() is not allowed, only .set() operations are allowed as 
the list
-            // is a fixed size.  So lets just mark the entry as blank.
+            // blank rather than removed, so the indexes above stay valid
             dSection.set(9, "");
             dSection.set(10, "");
         } else {
@@ -695,7 +726,7 @@ public class HAProxyConfigurator implements 
LoadBalancerConfigurator {
             // haproxy cannot handle empty listen / frontend or backend, so add
             // a dummy listener
             // on port 9
-            result.addAll(Arrays.asList(defaultListen));
+            result.addAll(defaultListen);
         }
         return result.toArray(new String[result.size()]);
     }
diff --git a/core/src/test/java/com/cloud/network/HAProxyConfiguratorTest.java 
b/core/src/test/java/com/cloud/network/HAProxyConfiguratorTest.java
index 073f976719b..3bbbe20b357 100644
--- a/core/src/test/java/com/cloud/network/HAProxyConfiguratorTest.java
+++ b/core/src/test/java/com/cloud/network/HAProxyConfiguratorTest.java
@@ -31,6 +31,9 @@ import org.junit.Test;
 import com.cloud.agent.api.routing.LoadBalancerConfigCommand;
 import com.cloud.agent.api.to.LoadBalancerTO;
 import com.cloud.network.lb.LoadBalancingRule.LbDestination;
+import com.cloud.network.lb.LoadBalancingRule.LbStickinessPolicy;
+import com.cloud.network.rules.LbStickinessMethod.StickinessMethodType;
+import com.cloud.utils.Pair;
 import com.cloud.network.lb.LoadBalancingRule.LbSslCert;
 
 import java.util.List;
@@ -158,6 +161,176 @@ public class HAProxyConfiguratorTest {
         Assert.assertTrue(result.contains("bind 10.2.0.1:443 ssl crt 
/etc/cloudstack/ssl/10_2_0_1-443.pem"));
     }
 
+    @Test
+    public void generateConfigurationTestKeepAliveWithSslOffloading() {
+        LoadBalancerTO lb = new LoadBalancerTO("1", "10.2.0.1", 443, "ssl", 
"roundrobin", false, false, false, null);
+        lb.setLbSslCert(new LbSslCert("cert", "key", "password", "chain", 
"fingerprint", false));
+        LoadBalancerTO[] lba = new LoadBalancerTO[1];
+        lba[0] = lb;
+        HAProxyConfigurator hpg = new HAProxyConfigurator();
+        LoadBalancerConfigCommand cmd = new LoadBalancerConfigCommand(lba, 
"10.0.0.1", "10.1.0.1", "10.1.1.1", null, 1L, "12", true, 0L);
+        String result = genConfig(hpg, cmd);
+        Assert.assertFalse("'forceclose' is rejected by HAProxy 2.0 and 
later", result.contains("forceclose"));
+        Assert.assertTrue("keepalive should be requested explicitly", 
result.contains("\toption http-keep-alive"));
+    }
+
+    @Test
+    public void generateConfigurationTestKeepAliveDoesNotLeakToNextConfig() {
+        LoadBalancerTO lb = new LoadBalancerTO("1", "10.2.0.1", 80, "http", 
"bla", false, false, false, null);
+        LoadBalancerTO[] lba = new LoadBalancerTO[1];
+        lba[0] = lb;
+        HAProxyConfigurator hpg = new HAProxyConfigurator();
+
+        genConfig(hpg, new LoadBalancerConfigCommand(lba, "10.0.0.1", 
"10.1.0.1", "10.1.1.1", null, 1L, "12", true, 0L));
+
+        String result = genConfig(hpg, new LoadBalancerConfigCommand(lba, 
"10.0.0.1", "10.1.0.1", "10.1.1.1", null, 1L, "12", false, 0L));
+        Assert.assertFalse("keepalive from an earlier config should not 
survive into this one",
+                result.contains("\tno option httpclose"));
+    }
+
+    @Test
+    public void generateConfigurationTestIdleTimeoutDoesNotLeakToNextConfig() {
+        LoadBalancerTO lb = new LoadBalancerTO("1", "10.2.0.1", 80, "http", 
"bla", false, false, false, null);
+        LoadBalancerTO[] lba = new LoadBalancerTO[1];
+        lba[0] = lb;
+        HAProxyConfigurator hpg = new HAProxyConfigurator();
+
+        genConfig(hpg, new LoadBalancerConfigCommand(lba, "10.0.0.1", 
"10.1.0.1", "10.1.1.1", null, 1L, "12", false, 1234L));
+
+        String result = genConfig(hpg, new LoadBalancerConfigCommand(lba, 
"10.0.0.1", "10.1.0.1", "10.1.1.1", null, 1L, "12", false, -1L));
+        assertTrue("an unset idle timeout should fall back to the default", 
result.contains("\ttimeout client     50000"));
+        assertTrue("an unset idle timeout should fall back to the default", 
result.contains("\ttimeout server     50000"));
+    }
+
+    private LoadBalancerConfigCommand cmdFor(LoadBalancerTO lb, boolean 
offeringKeepAlive) {
+        LoadBalancerTO[] lba = new LoadBalancerTO[1];
+        lba[0] = lb;
+        return new LoadBalancerConfigCommand(lba, "10.0.0.1", "10.1.0.1", 
"10.1.1.1", null, 1L, "12", offeringKeepAlive, 50000L);
+    }
+
+    private LoadBalancerTO httpRule() {
+        return new LoadBalancerTO("1", "10.2.0.1", 80, "http", "roundrobin", 
false, false, false, null);
+    }
+
+    /** Just the "listen" block for this rule, so assertions cannot match the 
defaults or stats sections. */
+    private String poolSection(String config, String poolName) {
+        int start = config.indexOf("listen " + poolName + "\n");
+        Assert.assertTrue("no listen block for " + poolName, start >= 0);
+        int next = config.indexOf("\nlisten ", start + 1);
+        return next < 0 ? config.substring(start) : config.substring(start, 
next);
+    }
+
+    @Test
+    public void generateConfigurationTestPerRuleKeepAliveKeepsHttpMode() {
+        LoadBalancerTO lb = httpRule();
+        lb.setKeepAlive(true);
+        String pool = poolSection(genConfig(new HAProxyConfigurator(), 
cmdFor(lb, false)), "10_2_0_1-80");
+        assertTrue("the rule should stay in http mode so forwardfor still 
works", pool.contains("\tmode http"));
+        assertTrue(pool.contains("\toption http-keep-alive"));
+        Assert.assertFalse(pool.contains("\toption httpclose"));
+    }
+
+    @Test
+    public void 
generateConfigurationTestPerRuleKeepAliveOverridesTheOffering() {
+        LoadBalancerTO lb = httpRule();
+        lb.setKeepAlive(false);
+        String pool = poolSection(genConfig(new HAProxyConfigurator(), 
cmdFor(lb, true)), "10_2_0_1-80");
+        assertTrue("the rule should win over the offering", 
pool.contains("\toption httpclose"));
+        Assert.assertFalse(pool.contains("\toption http-keep-alive"));
+    }
+
+    @Test
+    public void 
generateConfigurationTestUnsetKeepAliveLeavesOldBehaviourAlone() {
+        String pool = poolSection(genConfig(new HAProxyConfigurator(), 
cmdFor(httpRule(), true)), "10_2_0_1-80");
+        Assert.assertFalse("keepalive from the offering still drops to tcp 
mode", pool.contains("\tmode http"));
+
+        pool = poolSection(genConfig(new HAProxyConfigurator(), 
cmdFor(httpRule(), false)), "10_2_0_1-80");
+        assertTrue(pool.contains("\tmode http"));
+        assertTrue(pool.contains("\toption httpclose"));
+    }
+
+    @Test
+    public void generateConfigurationTestKeepAliveTimeout() {
+        LoadBalancerTO lb = httpRule();
+        lb.setKeepAlive(true);
+        lb.setKeepAliveTimeout(15000L);
+        String pool = poolSection(genConfig(new HAProxyConfigurator(), 
cmdFor(lb, false)), "10_2_0_1-80");
+        assertTrue(pool.contains("\ttimeout http-keep-alive 15000"));
+    }
+
+    @Test
+    public void generateConfigurationTestKeepAliveTimeoutNeedsKeepAlive() {
+        LoadBalancerTO lb = httpRule();
+        lb.setKeepAliveTimeout(15000L);
+        String pool = poolSection(genConfig(new HAProxyConfigurator(), 
cmdFor(lb, false)), "10_2_0_1-80");
+        Assert.assertFalse("without keepalive there is no idle connection to 
time out", pool.contains("timeout http-keep-alive"));
+    }
+
+    @Test
+    public void 
generateConfigurationTestIdleTimeoutOverridesTheGlobalPerRule() {
+        LoadBalancerTO lb = new LoadBalancerTO("1", "10.2.0.1", 3306, "tcp", 
"roundrobin", false, false, false, null);
+        lb.setIdleTimeout(600000L);
+        String config = genConfig(new HAProxyConfigurator(), cmdFor(lb, 
false));
+        String pool = poolSection(config, "10_2_0_1-3306");
+        assertTrue("a tcp rule gets the timeouts too", 
pool.contains("\ttimeout client     600000"));
+        assertTrue(pool.contains("\ttimeout server     600000"));
+        assertTrue("the global still sets the defaults", 
config.contains("\ttimeout client     50000"));
+    }
+
+    @Test
+    public void generateConfigurationTestNegativeTimeoutsAreDropped() {
+        LoadBalancerTO lb = httpRule();
+        lb.setKeepAlive(true);
+        lb.setIdleTimeout(-1L);
+        lb.setKeepAliveTimeout(-5L);
+        String pool = poolSection(genConfig(new HAProxyConfigurator(), 
cmdFor(lb, false)), "10_2_0_1-80");
+        Assert.assertFalse("a negative timeout is a fatal haproxy parse 
error", pool.contains("-1"));
+        Assert.assertFalse("a negative timeout is a fatal haproxy parse 
error", pool.contains("-5"));
+        Assert.assertFalse(pool.contains("timeout client"));
+        Assert.assertFalse(pool.contains("timeout http-keep-alive"));
+    }
+
+    @Test
+    public void 
generateConfigurationTestPerRuleSettingsDoNotCrossContaminate() {
+        LoadBalancerTO tuned = httpRule();
+        tuned.setKeepAlive(true);
+        tuned.setIdleTimeout(600000L);
+        tuned.setKeepAliveTimeout(15000L);
+        LoadBalancerTO plain = new LoadBalancerTO("2", "10.2.0.1", 8080, 
"tcp", "roundrobin", false, false, false, null);
+        LoadBalancerTO[] lba = new LoadBalancerTO[] {tuned, plain};
+        LoadBalancerConfigCommand cmd = new LoadBalancerConfigCommand(lba, 
"10.0.0.1", "10.1.0.1", "10.1.1.1", null, 1L, "12", false, 50000L);
+        String config = genConfig(new HAProxyConfigurator(), cmd);
+
+        String tunedPool = poolSection(config, "10_2_0_1-80");
+        assertTrue(tunedPool.contains("\toption http-keep-alive"));
+        assertTrue(tunedPool.contains("\ttimeout client     600000"));
+
+        String plainPool = poolSection(config, "10_2_0_1-8080");
+        Assert.assertFalse("the other rule's settings must not leak here", 
plainPool.contains("http-keep-alive"));
+        Assert.assertFalse("the other rule's settings must not leak here", 
plainPool.contains("timeout client"));
+    }
+
+    @Test
+    public void generateConfigurationTestAppCookieStickinessUsesAStickTable() {
+        List<Pair<String, String>> params = new ArrayList<>();
+        params.add(new Pair<>("cookie-name", "JSESSIONID"));
+        params.add(new Pair<>("length", "52"));
+        params.add(new Pair<>("holdtime", "3h"));
+        List<LbStickinessPolicy> policies = new ArrayList<>();
+        policies.add(new 
LbStickinessPolicy(StickinessMethodType.AppCookieBased.getName(), params));
+        List<LbDestination> dests = new ArrayList<>();
+        dests.add(new LbDestination(80, 80, "10.1.10.2", false));
+        LoadBalancerTO lb = new LoadBalancerTO("1", "10.2.0.1", 80, "http", 
"roundrobin", false, false, false, dests, policies);
+        LoadBalancerTO[] lba = new LoadBalancerTO[1];
+        lba[0] = lb;
+        String result = genConfig(new HAProxyConfigurator(),
+                new LoadBalancerConfigCommand(lba, "10.0.0.1", "10.1.0.1", 
"10.1.1.1", null, 1L, "12", false, 50000L));
+        Assert.assertFalse("appsession was removed in haproxy 1.6", 
result.contains("appsession"));
+        assertTrue(result.contains("stick-table type string len 52 size 10k 
expire 3h"));
+        assertTrue(result.contains("stick store-response 
res.cook(JSESSIONID)"));
+        assertTrue(result.contains("stick match req.cook(JSESSIONID)"));
+    }
+
     private String genConfig(HAProxyConfigurator hpg, 
LoadBalancerConfigCommand cmd) {
         String[] sa = hpg.generateConfiguration(cmd);
         StringBuilder sb = new StringBuilder();
diff --git a/server/src/main/java/com/cloud/api/ApiResponseHelper.java 
b/server/src/main/java/com/cloud/api/ApiResponseHelper.java
index cd2aa050896..1464f444489 100644
--- a/server/src/main/java/com/cloud/api/ApiResponseHelper.java
+++ b/server/src/main/java/com/cloud/api/ApiResponseHelper.java
@@ -240,9 +240,12 @@ import org.apache.cloudstack.usage.Usage;
 import org.apache.cloudstack.usage.UsageService;
 import org.apache.cloudstack.usage.UsageTypes;
 import org.apache.cloudstack.vm.UnmanagedInstanceTO;
+import org.apache.cloudstack.resourcedetail.FirewallRuleDetailVO;
+import org.apache.cloudstack.resourcedetail.dao.FirewallRuleDetailsDao;
 import org.apache.commons.collections.CollectionUtils;
 import org.apache.commons.lang3.ObjectUtils;
 import org.apache.commons.lang3.StringUtils;
+import org.apache.commons.lang3.math.NumberUtils;
 import org.apache.logging.log4j.LogManager;
 import org.apache.logging.log4j.Logger;
 
@@ -467,6 +470,8 @@ public class ApiResponseHelper implements 
ResponseGenerator, ResourceIdSupport {
     protected Logger logger = LogManager.getLogger(ApiResponseHelper.class);
     private static final DecimalFormat s_percentFormat = new 
DecimalFormat("##.##");
 
+    @Inject
+    private FirewallRuleDetailsDao firewallRuleDetailsDao;
     @Inject
     private EntityManager _entityMgr;
     @Inject
@@ -1325,6 +1330,21 @@ public class ApiResponseHelper implements 
ResponseGenerator, ResourceIdSupport {
         }
     }
 
+    private void setLbConnectionSettings(LoadBalancerResponse lbResponse, long 
lbRuleId) {
+        FirewallRuleDetailVO keepAlive = 
firewallRuleDetailsDao.findDetail(lbRuleId, LoadBalancer.KEEPALIVE);
+        if (keepAlive != null) {
+            lbResponse.setKeepAlive(Boolean.valueOf(keepAlive.getValue()));
+        }
+        FirewallRuleDetailVO idleTimeout = 
firewallRuleDetailsDao.findDetail(lbRuleId, LoadBalancer.IDLE_TIMEOUT);
+        if (idleTimeout != null) {
+            
lbResponse.setIdleTimeout(NumberUtils.toLong(idleTimeout.getValue()));
+        }
+        FirewallRuleDetailVO keepAliveTimeout = 
firewallRuleDetailsDao.findDetail(lbRuleId, LoadBalancer.KEEPALIVE_TIMEOUT);
+        if (keepAliveTimeout != null) {
+            
lbResponse.setKeepAliveTimeout(NumberUtils.toLong(keepAliveTimeout.getValue()));
+        }
+    }
+
     @Override
     public LoadBalancerResponse createLoadBalancerResponse(LoadBalancer 
loadBalancer) {
         LoadBalancerResponse lbResponse = new LoadBalancerResponse();
@@ -1333,6 +1353,7 @@ public class ApiResponseHelper implements 
ResponseGenerator, ResourceIdSupport {
         lbResponse.setDescription(loadBalancer.getDescription());
         List<String> cidrs = 
ApiDBUtils.findFirewallSourceCidrs(loadBalancer.getId());
         lbResponse.setCidrList(StringUtils.join(cidrs, ","));
+        setLbConnectionSettings(lbResponse, loadBalancer.getId());
 
         IPAddressVO publicIp = 
ApiDBUtils.findIpAddressById(loadBalancer.getSourceIpAddressId());
         lbResponse.setPublicIpId(publicIp.getUuid());
diff --git 
a/server/src/main/java/com/cloud/network/lb/LoadBalancingRulesManagerImpl.java 
b/server/src/main/java/com/cloud/network/lb/LoadBalancingRulesManagerImpl.java
index b0be0df4e0b..6d90845c7a8 100644
--- 
a/server/src/main/java/com/cloud/network/lb/LoadBalancingRulesManagerImpl.java
+++ 
b/server/src/main/java/com/cloud/network/lb/LoadBalancingRulesManagerImpl.java
@@ -35,6 +35,8 @@ import com.cloud.offerings.dao.NetworkOfferingServiceMapDao;
 import org.apache.cloudstack.acl.ApiKeyPairVO;
 import org.apache.cloudstack.acl.SecurityChecker;
 import org.apache.cloudstack.api.ApiConstants;
+import org.apache.cloudstack.resourcedetail.FirewallRuleDetailVO;
+import org.apache.cloudstack.resourcedetail.dao.FirewallRuleDetailsDao;
 import org.apache.cloudstack.api.ApiErrorCode;
 import org.apache.cloudstack.api.ServerApiException;
 import 
org.apache.cloudstack.api.command.user.loadbalancer.CreateLBHealthCheckPolicyCmd;
@@ -183,6 +185,8 @@ import com.google.gson.reflect.TypeToken;
 
 public class LoadBalancingRulesManagerImpl<Type> extends ManagerBase 
implements LoadBalancingRulesManager, LoadBalancingRulesService {
 
+    @Inject
+    FirewallRuleDetailsDao _firewallRuleDetailsDao;
     @Inject
     NetworkOrchestrationService _networkMgr;
     @Inject
@@ -280,6 +284,7 @@ public class LoadBalancingRulesManagerImpl<Type> extends 
ManagerBase implements
     NicSecondaryIpDao _nicSecondaryIpDao;
 
     private static final int DNS_PORT = 53;
+    private static final List<String> CONNECTION_SETTINGS = 
List.of(LoadBalancer.KEEPALIVE, LoadBalancer.IDLE_TIMEOUT, 
LoadBalancer.KEEPALIVE_TIMEOUT);
     // Will return a string. For LB Stickiness this will be a json, for
     // autoscale this will be "," separated values
     @Override
@@ -2296,6 +2301,87 @@ public class LoadBalancingRulesManagerImpl<Type> extends 
ManagerBase implements
         return dstList;
     }
 
+    /**
+     * Haproxy rejects a negative timeout, and one above 2147483647 ms ("timer 
overflow"). A
+     * rejected file leaves every rule on the router running its previous 
config, so refuse the
+     * value here rather than let it reach the VR.
+     */
+    protected void validateConnectionTimeout(String name, Long value) {
+        if (value == null) {
+            return;
+        }
+        if (value < 0) {
+            throw new InvalidParameterValueException(String.format("%s must be 
0 or greater, got [%s]. 0 means no timeout.", name, value));
+        }
+        if (value > Integer.MAX_VALUE) {
+            throw new InvalidParameterValueException(String.format("%s must be 
at most %d milliseconds, the largest timeout HAProxy accepts, got [%s].",
+                    name, Integer.MAX_VALUE, value));
+        }
+    }
+
+    @Override
+    public void validateLoadBalancerConnectionSettings(Long idleTimeout, Long 
keepAliveTimeout) {
+        validateConnectionTimeout(ApiConstants.IDLE_TIMEOUT, idleTimeout);
+        validateConnectionTimeout(ApiConstants.KEEPALIVE_TIMEOUT, 
keepAliveTimeout);
+    }
+
+    @Override
+    public boolean updateLoadBalancerConnectionSettings(long lbRuleId, Boolean 
keepAlive, Long idleTimeout, Long keepAliveTimeout, boolean cleanup) {
+        validateLoadBalancerConnectionSettings(idleTimeout, keepAliveTimeout);
+
+        Map<String, String> current = getConnectionSettings(lbRuleId);
+        Map<String, String> wanted = cleanup ? new HashMap<>() : new 
HashMap<>(current);
+        if (keepAlive != null) {
+            wanted.put(LoadBalancer.KEEPALIVE, keepAlive.toString());
+        }
+        if (idleTimeout != null) {
+            wanted.put(LoadBalancer.IDLE_TIMEOUT, idleTimeout.toString());
+        }
+        if (keepAliveTimeout != null) {
+            wanted.put(LoadBalancer.KEEPALIVE_TIMEOUT, 
keepAliveTimeout.toString());
+        }
+        if (wanted.equals(current)) {
+            return false;
+        }
+        setConnectionSettings(lbRuleId, wanted);
+        return true;
+    }
+
+    /**
+     * The per rule settings the rule holds itself, keyed by detail name. A 
setting the rule
+     * inherits is absent.
+     */
+    protected Map<String, String> getConnectionSettings(long lbRuleId) {
+        Map<String, String> settings = new HashMap<>();
+        for (String key : CONNECTION_SETTINGS) {
+            FirewallRuleDetailVO detail = 
_firewallRuleDetailsDao.findDetail(lbRuleId, key);
+            if (detail != null) {
+                settings.put(key, detail.getValue());
+            }
+        }
+        return settings;
+    }
+
+    /**
+     * Makes the rule hold exactly these settings: a key in the map is stored, 
a key missing
+     * from it is removed so the rule inherits it again.
+     */
+    protected void setConnectionSettings(long lbRuleId, Map<String, String> 
settings) {
+        for (String key : CONNECTION_SETTINGS) {
+            String value = settings.get(key);
+            FirewallRuleDetailVO existing = 
_firewallRuleDetailsDao.findDetail(lbRuleId, key);
+            if (existing != null && Objects.equals(value, 
existing.getValue())) {
+                continue;
+            }
+            if (existing != null) {
+                _firewallRuleDetailsDao.removeDetail(lbRuleId, key);
+            }
+            if (value != null) {
+                _firewallRuleDetailsDao.addDetail(lbRuleId, key, value, true);
+            }
+        }
+    }
+
     @Override
     @ActionEvent(eventType = EventTypes.EVENT_LOAD_BALANCER_UPDATE, 
eventDescription = "updating load balancer", async = true)
     public LoadBalancer updateLoadBalancerRule(UpdateLoadBalancerRuleCmd cmd) {
@@ -2359,20 +2445,29 @@ public class LoadBalancingRulesManagerImpl<Type> 
extends ManagerBase implements
             throw new 
InvalidParameterValueException(String.format("Modifications in lb rule %s are 
not supported.", lb));
         }
 
+        // The settings are written straight to the details table, unlike the 
fields above that wait
+        // for _lbDao.update, so they are stored only once nothing else can 
reject the update
+        Map<String, String> settingsBackup = getConnectionSettings(lbRuleId);
+        boolean settingsChanged = 
updateLoadBalancerConnectionSettings(lbRuleId, cmd.getKeepAlive(), 
cmd.getIdleTimeout(), cmd.getKeepAliveTimeout(),
+                cmd.isCleanupConnectionSettings());
+
         LoadBalancerVO tmplbVo = _lbDao.findById(lbRuleId);
-        boolean success = _lbDao.update(lbRuleId, lb);
+        // update() reports false when no column of the rule changed, which is 
the case when only the
+        // connection settings change, since they are kept in 
firewall_rule_details
+        boolean success = _lbDao.update(lbRuleId, lb) || settingsChanged;
 
         // Check if algorithm, lb protocol, or cidrlist has changed, and 
reapply the lb config if needed
         boolean algorithmChanged = !Objects.equals(algorithm, 
tmplbVo.getAlgorithm());
         boolean protocolChanged = !Objects.equals(lbProtocol, 
tmplbVo.getLbProtocol());
         boolean cidrListChanged = !Objects.equals(tmplbVo.getCidrList(), 
lb.getCidrList());
 
-        if (algorithmChanged || protocolChanged || cidrListChanged) {
+        if (algorithmChanged || protocolChanged || cidrListChanged || 
settingsChanged) {
             try {
                 lb.setState(FirewallRule.State.Add);
                 _lbDao.persist(lb);
                 applyLoadBalancerConfig(lbRuleId);
-                if (!lb.getLbProtocol().equals(NetUtils.SSL_PROTO)) {
+                // lb_protocol is null for a rule created through the API 
without a protocol
+                if (!NetUtils.SSL_PROTO.equals(lb.getLbProtocol())) {
                     removeCertMapIfExists(lb);
                 }
             } catch (ResourceUnavailableException e) {
@@ -2396,6 +2491,9 @@ public class LoadBalancingRulesManagerImpl<Type> extends 
ManagerBase implements
                     lb.setState(lbBackup.getState());
                     _lbDao.update(lb.getId(), lb);
                     _lbDao.persist(lb);
+                    if (settingsChanged) {
+                        setConnectionSettings(lbRuleId, settingsBackup);
+                    }
 
                     logger.debug("LB Rollback rule: {} while updating LB 
rule.", lb);
                 }
diff --git 
a/server/src/main/java/com/cloud/network/router/CommandSetupHelper.java 
b/server/src/main/java/com/cloud/network/router/CommandSetupHelper.java
index c1397c08d93..0bf46893c79 100644
--- a/server/src/main/java/com/cloud/network/router/CommandSetupHelper.java
+++ b/server/src/main/java/com/cloud/network/router/CommandSetupHelper.java
@@ -35,6 +35,8 @@ import 
org.apache.cloudstack.framework.config.dao.ConfigurationDao;
 import org.apache.cloudstack.network.BgpPeer;
 import org.apache.cloudstack.network.BgpPeerTO;
 import org.apache.cloudstack.network.dao.BgpPeerDetailsDao;
+import org.apache.cloudstack.resourcedetail.FirewallRuleDetailVO;
+import org.apache.cloudstack.resourcedetail.dao.FirewallRuleDetailsDao;
 
 import org.apache.logging.log4j.Logger;
 import org.apache.logging.log4j.LogManager;
@@ -120,6 +122,7 @@ import com.cloud.network.lb.LoadBalancingRule;
 import com.cloud.network.lb.LoadBalancingRule.LbDestination;
 import com.cloud.network.lb.LoadBalancingRule.LbStickinessPolicy;
 import com.cloud.network.rules.FirewallRule;
+import com.cloud.network.rules.LoadBalancer;
 import com.cloud.network.rules.FirewallRule.Purpose;
 import com.cloud.network.rules.FirewallRuleVO;
 import com.cloud.network.rules.PortForwardingRule;
@@ -173,6 +176,8 @@ public class CommandSetupHelper {
     @Inject
     private DomainDao domainDao;
     @Inject
+    private FirewallRuleDetailsDao _firewallRuleDetailsDao;
+    @Inject
     private NicDao _nicDao;
     @Inject
     private NicNetworkMapDao _nicNetworkMapDao;
@@ -375,6 +380,29 @@ public class CommandSetupHelper {
         cmds.addCommand("dnsMasqConfig", dnsMasqConfigCmd);
     }
 
+    /** Optional per rule haproxy settings. Absent means the rule inherits, so 
nothing is set. */
+    protected void setConnectionSettings(final LoadBalancerTO lb, final long 
lbRuleId) {
+        final FirewallRuleDetailVO keepAlive = 
_firewallRuleDetailsDao.findDetail(lbRuleId, LoadBalancer.KEEPALIVE);
+        if (keepAlive != null) {
+            lb.setKeepAlive(Boolean.valueOf(keepAlive.getValue()));
+        }
+        lb.setIdleTimeout(longDetail(lbRuleId, LoadBalancer.IDLE_TIMEOUT));
+        lb.setKeepAliveTimeout(longDetail(lbRuleId, 
LoadBalancer.KEEPALIVE_TIMEOUT));
+    }
+
+    private Long longDetail(final long lbRuleId, final String key) {
+        final FirewallRuleDetailVO detail = 
_firewallRuleDetailsDao.findDetail(lbRuleId, key);
+        if (detail == null) {
+            return null;
+        }
+        try {
+            return Long.valueOf(detail.getValue());
+        } catch (final NumberFormatException e) {
+            logger.warn("Ignoring lb rule {} detail {}, [{}] is not a number", 
lbRuleId, key, detail.getValue());
+            return null;
+        }
+    }
+
     public void createApplyLoadBalancingRulesCommands(final 
List<LoadBalancingRule> rules, final VirtualRouter router, final Commands cmds, 
final long guestNetworkId) {
         final LoadBalancerTO[] lbs = new LoadBalancerTO[rules.size()];
         int i = 0;
@@ -398,6 +426,7 @@ public class CommandSetupHelper {
             }
             lb.setLbProtocol(lb_protocol);
             lb.setLbSslCert(rule.getLbSslCert());
+            setConnectionSettings(lb, rule.getId());
             lbs[i++] = lb;
         }
         String routerPublicIp = null;
diff --git 
a/server/src/main/java/com/cloud/network/router/VirtualNetworkApplianceManagerImpl.java
 
b/server/src/main/java/com/cloud/network/router/VirtualNetworkApplianceManagerImpl.java
index fe167b7e7e7..22c86ca34a3 100644
--- 
a/server/src/main/java/com/cloud/network/router/VirtualNetworkApplianceManagerImpl.java
+++ 
b/server/src/main/java/com/cloud/network/router/VirtualNetworkApplianceManagerImpl.java
@@ -78,6 +78,8 @@ import org.apache.cloudstack.userdata.UserDataManager;
 import org.apache.cloudstack.utils.CloudStackVersion;
 import org.apache.cloudstack.utils.identity.ManagementServerNode;
 import org.apache.cloudstack.utils.usage.UsageUtils;
+import org.apache.cloudstack.resourcedetail.FirewallRuleDetailVO;
+import org.apache.cloudstack.resourcedetail.dao.FirewallRuleDetailsDao;
 import org.apache.commons.collections4.CollectionUtils;
 import org.apache.commons.lang3.ObjectUtils;
 import org.apache.commons.lang3.StringUtils;
@@ -199,6 +201,7 @@ import com.cloud.network.lb.LoadBalancingRulesManager;
 import com.cloud.network.router.VirtualRouter.RedundantState;
 import com.cloud.network.router.VirtualRouter.Role;
 import com.cloud.network.rules.FirewallRule;
+import com.cloud.network.rules.LoadBalancer;
 import com.cloud.network.rules.FirewallRule.Purpose;
 import com.cloud.network.rules.FirewallRuleVO;
 import com.cloud.network.rules.LoadBalancerContainer.Scheme;
@@ -352,6 +355,8 @@ Configurable, StateListener<VirtualMachine.State, 
VirtualMachine.Event, VirtualM
     @Inject protected CommandSetupHelper _commandSetupHelper;
     @Inject ManagementServer mgr;
     @Inject
+    private FirewallRuleDetailsDao _firewallRuleDetailsDao;
+    @Inject
     RoutedIpv4Manager routedIpv4Manager;
     @Inject
     BGPService bgpService;
@@ -1720,7 +1725,9 @@ Configurable, StateListener<VirtualMachine.State, 
VirtualMachine.Event, VirtualM
                             
.append(",protocol=").append(appLoadBalancerVO.getLbProtocol());
                 }
                 
loadBalancingData.append(",stickiness=").append(getStickinessPolicies(firewallRuleVO.getId()));
-                
loadBalancingData.append(",keepAliveEnabled=").append(offering.isKeepAliveEnabled()).append(",vmIps=");
+                
loadBalancingData.append(",keepAliveEnabled=").append(offering.isKeepAliveEnabled());
+                appendLbRuleConnectionSettings(loadBalancingData, 
firewallRuleVO.getId());
+                loadBalancingData.append(",vmIps=");
                 for (LoadBalancerVMMapVO vmMapVO : vmMapVOs) {
                     loadBalancingData.append(vmMapVO.getInstanceIp()).append(" 
");
                 }
@@ -1729,6 +1736,21 @@ Configurable, StateListener<VirtualMachine.State, 
VirtualMachine.Event, VirtualM
         }
     }
 
+    /**
+     * The per rule haproxy settings, so the health check can tell what the 
rule's own listen
+     * section should hold. Empty when the rule inherits.
+     */
+    protected void appendLbRuleConnectionSettings(final StringBuilder 
loadBalancingData, long lbRuleId) {
+        
loadBalancingData.append(",ruleKeepAlive=").append(lbRuleDetail(lbRuleId, 
LoadBalancer.KEEPALIVE))
+                .append(",ruleIdleTimeout=").append(lbRuleDetail(lbRuleId, 
LoadBalancer.IDLE_TIMEOUT))
+                
.append(",ruleKeepAliveTimeout=").append(lbRuleDetail(lbRuleId, 
LoadBalancer.KEEPALIVE_TIMEOUT));
+    }
+
+    private String lbRuleDetail(long lbRuleId, String key) {
+        FirewallRuleDetailVO detail = 
_firewallRuleDetailsDao.findDetail(lbRuleId, key);
+        return detail == null ? "" : detail.getValue();
+    }
+
     protected void updateWithLbRuleSslCertificates(final StringBuilder 
loadBalancingData, LoadBalancerVO loadBalancerVO, String sourceIp) {
         if (NetUtils.SSL_PROTO.equals(loadBalancerVO.getLbProtocol())) {
             final LbSslCert sslCert = 
_lbMgr.getLbSslCert(loadBalancerVO.getId());
diff --git 
a/server/src/test/java/com/cloud/network/lb/LoadBalancingRulesManagerImplTest.java
 
b/server/src/test/java/com/cloud/network/lb/LoadBalancingRulesManagerImplTest.java
index deca36df56f..ca09b37b46d 100644
--- 
a/server/src/test/java/com/cloud/network/lb/LoadBalancingRulesManagerImplTest.java
+++ 
b/server/src/test/java/com/cloud/network/lb/LoadBalancingRulesManagerImplTest.java
@@ -24,6 +24,7 @@ import static org.mockito.Mockito.when;
 
 import java.util.ArrayList;
 import java.util.Arrays;
+import java.util.Collections;
 import java.util.HashMap;
 import java.util.List;
 import java.util.Map;
@@ -35,6 +36,8 @@ import org.apache.cloudstack.api.ServerApiException;
 import 
org.apache.cloudstack.api.command.user.loadbalancer.UpdateLoadBalancerRuleCmd;
 import org.apache.cloudstack.context.CallContext;
 import 
org.apache.cloudstack.engine.orchestration.service.NetworkOrchestrationService;
+import org.apache.cloudstack.resourcedetail.FirewallRuleDetailVO;
+import org.apache.cloudstack.resourcedetail.dao.FirewallRuleDetailsDao;
 import org.junit.Assert;
 import org.junit.Test;
 import org.junit.runner.RunWith;
@@ -56,6 +59,7 @@ import com.cloud.network.dao.LoadBalancerVO;
 import com.cloud.network.dao.NetworkDao;
 import com.cloud.network.dao.NetworkVO;
 import com.cloud.network.dao.SslCertVO;
+import com.cloud.network.rules.LoadBalancer;
 import com.cloud.network.vpc.VpcManager;
 import com.cloud.offering.NetworkOffering;
 import com.cloud.offerings.dao.NetworkOfferingServiceMapDao;
@@ -104,6 +108,9 @@ public class LoadBalancingRulesManagerImplTest{
     @Mock
     VpcManager vpcManager;
 
+    @Mock
+    FirewallRuleDetailsDao _firewallRuleDetailsDao;
+
     @Spy
     @InjectMocks
     LoadBalancingRulesManagerImpl lbr = new LoadBalancingRulesManagerImpl();
@@ -298,6 +305,23 @@ public class LoadBalancingRulesManagerImplTest{
         Mockito.verify(_lbCertMapDao, never()).remove(anyLong());
     }
 
+    @Test
+    public void testUpdateLoadBalancerRuleWithoutProtocol() throws Exception {
+        setupUpdateLoadBalancerRule();
+
+        // A rule created through the API without a protocol has a null 
lb_protocol
+        UpdateLoadBalancerRuleCmd cmd = new UpdateLoadBalancerRuleCmd();
+        ReflectionTestUtils.setField(cmd, ApiConstants.ID, lbRuleId);
+        ReflectionTestUtils.setField(cmd, "algorithm", "roundrobin");
+        when(loadBalancerMock.getAlgorithm()).thenReturn("source");
+        when(loadBalancerMock.getLbProtocol()).thenReturn(null);
+
+        lbr.updateLoadBalancerRule(cmd);
+
+        Mockito.verify(lbr, times(1)).applyLoadBalancerConfig(lbRuleId);
+        Mockito.verify(_lbCertMapDao, times(1)).remove(anyLong());
+    }
+
     @Test(expected = CloudRuntimeException.class)
     public void testUpdateLoadBalancerRule5() throws Exception {
         setupUpdateLoadBalancerRule();
@@ -378,4 +402,242 @@ public class LoadBalancingRulesManagerImplTest{
         lbr.createPublicLoadBalancerRule("xid", "name", "desc", 53, 53, 53, 53,
                 null, "tcp", "roundrobin", networkId, lbOwnerId, false, "tcp", 
null, null);
     }
+
+    @Test
+    public void testValidateConnectionTimeoutAcceptsZeroAndAbove() {
+        lbr.validateConnectionTimeout(ApiConstants.IDLE_TIMEOUT, null);
+        lbr.validateConnectionTimeout(ApiConstants.IDLE_TIMEOUT, 0L);
+        lbr.validateConnectionTimeout(ApiConstants.IDLE_TIMEOUT, 600000L);
+    }
+
+    @Test(expected = InvalidParameterValueException.class)
+    public void testValidateConnectionTimeoutRejectsNegative() {
+        lbr.validateConnectionTimeout(ApiConstants.IDLE_TIMEOUT, -1L);
+    }
+
+    @Test
+    public void testValidateConnectionTimeoutAcceptsHaproxyMaximum() {
+        lbr.validateConnectionTimeout(ApiConstants.IDLE_TIMEOUT, (long) 
Integer.MAX_VALUE);
+    }
+
+    @Test(expected = InvalidParameterValueException.class)
+    public void testValidateConnectionTimeoutRejectsAboveHaproxyMaximum() {
+        lbr.validateConnectionTimeout(ApiConstants.IDLE_TIMEOUT, 99999999999L);
+    }
+
+    @Test(expected = InvalidParameterValueException.class)
+    public void 
testValidateLoadBalancerConnectionSettingsChecksKeepAliveTimeout() {
+        lbr.validateLoadBalancerConnectionSettings(600000L, -1L);
+    }
+
+    private void stubConnectionSettings(String keepAlive, String idleTimeout, 
String keepAliveTimeout) {
+        stubConnectionSetting(LoadBalancer.KEEPALIVE, keepAlive);
+        stubConnectionSetting(LoadBalancer.IDLE_TIMEOUT, idleTimeout);
+        stubConnectionSetting(LoadBalancer.KEEPALIVE_TIMEOUT, 
keepAliveTimeout);
+    }
+
+    private void stubConnectionSetting(String key, String value) {
+        when(_firewallRuleDetailsDao.findDetail(lbRuleId, key))
+                .thenReturn(value == null ? null : new 
FirewallRuleDetailVO(lbRuleId, key, value, true));
+    }
+
+    private void verifyNoConnectionSettingWrites() {
+        Mockito.verify(_firewallRuleDetailsDao, 
never()).removeDetail(anyLong(), Mockito.anyString());
+        Mockito.verify(_firewallRuleDetailsDao, never()).addDetail(anyLong(), 
Mockito.anyString(), Mockito.anyString(), Mockito.anyBoolean());
+    }
+
+    @Test
+    public void testConnectionSettingsCleanupResetsAllToInherit() {
+        stubConnectionSettings("true", "5000", "1000");
+
+        Assert.assertTrue(lbr.updateLoadBalancerConnectionSettings(lbRuleId, 
null, null, null, true));
+
+        Mockito.verify(_firewallRuleDetailsDao).removeDetail(lbRuleId, 
LoadBalancer.KEEPALIVE);
+        Mockito.verify(_firewallRuleDetailsDao).removeDetail(lbRuleId, 
LoadBalancer.IDLE_TIMEOUT);
+        Mockito.verify(_firewallRuleDetailsDao).removeDetail(lbRuleId, 
LoadBalancer.KEEPALIVE_TIMEOUT);
+        Mockito.verify(_firewallRuleDetailsDao, never()).addDetail(anyLong(), 
Mockito.anyString(), Mockito.anyString(), Mockito.anyBoolean());
+    }
+
+    @Test
+    public void testConnectionSettingsCleanupKeepsValuesPassedAlongside() {
+        stubConnectionSettings("true", "5000", "1000");
+
+        Assert.assertTrue(lbr.updateLoadBalancerConnectionSettings(lbRuleId, 
null, 5000L, null, true));
+
+        Mockito.verify(_firewallRuleDetailsDao).removeDetail(lbRuleId, 
LoadBalancer.KEEPALIVE);
+        Mockito.verify(_firewallRuleDetailsDao).removeDetail(lbRuleId, 
LoadBalancer.KEEPALIVE_TIMEOUT);
+        Mockito.verify(_firewallRuleDetailsDao, 
never()).removeDetail(lbRuleId, LoadBalancer.IDLE_TIMEOUT);
+        Mockito.verify(_firewallRuleDetailsDao, never()).addDetail(anyLong(), 
Mockito.anyString(), Mockito.anyString(), Mockito.anyBoolean());
+    }
+
+    @Test
+    public void testConnectionSettingsCleanupWithNothingSetIsNoChange() {
+        stubConnectionSettings(null, null, null);
+
+        Assert.assertFalse(lbr.updateLoadBalancerConnectionSettings(lbRuleId, 
null, null, null, true));
+
+        verifyNoConnectionSettingWrites();
+    }
+
+    @Test
+    public void testConnectionSettingsNullKeepsCurrentValue() {
+        stubConnectionSettings(null, "5000", null);
+
+        Assert.assertTrue(lbr.updateLoadBalancerConnectionSettings(lbRuleId, 
null, null, 2000L, false));
+
+        Mockito.verify(_firewallRuleDetailsDao).addDetail(lbRuleId, 
LoadBalancer.KEEPALIVE_TIMEOUT, "2000", true);
+        Mockito.verify(_firewallRuleDetailsDao, 
never()).removeDetail(anyLong(), Mockito.anyString());
+    }
+
+    @Test
+    public void testConnectionSettingsSameValueIsNoChange() {
+        stubConnectionSettings(null, "5000", null);
+
+        Assert.assertFalse(lbr.updateLoadBalancerConnectionSettings(lbRuleId, 
null, 5000L, null, false));
+
+        verifyNoConnectionSettingWrites();
+    }
+
+    @Test
+    public void testConnectionSettingsChangedValueIsReplaced() {
+        stubConnectionSettings("false", null, null);
+
+        Assert.assertTrue(lbr.updateLoadBalancerConnectionSettings(lbRuleId, 
true, null, null, false));
+
+        Mockito.verify(_firewallRuleDetailsDao).removeDetail(lbRuleId, 
LoadBalancer.KEEPALIVE);
+        Mockito.verify(_firewallRuleDetailsDao).addDetail(lbRuleId, 
LoadBalancer.KEEPALIVE, "true", true);
+    }
+
+    @Test
+    public void testUpdateLoadBalancerRuleOnlyConnectionSettings() throws 
Exception {
+        setupUpdateLoadBalancerRule();
+        stubConnectionSettings(null, null, null);
+        // No column of the rule changes, so the dao reports no row updated
+        when(_lbDao.update(lbRuleId, loadBalancerMock)).thenReturn(false);
+
+        UpdateLoadBalancerRuleCmd cmd = new UpdateLoadBalancerRuleCmd();
+        ReflectionTestUtils.setField(cmd, ApiConstants.ID, lbRuleId);
+        ReflectionTestUtils.setField(cmd, "algorithm", "roundrobin");
+        ReflectionTestUtils.setField(cmd, "idleTimeout", 120000L);
+        when(loadBalancerMock.getAlgorithm()).thenReturn("roundrobin");
+
+        lbr.updateLoadBalancerRule(cmd);
+
+        Mockito.verify(_firewallRuleDetailsDao).addDetail(lbRuleId, 
LoadBalancer.IDLE_TIMEOUT, "120000", true);
+        Mockito.verify(lbr, times(1)).applyLoadBalancerConfig(lbRuleId);
+    }
+
+    @Test(expected = CloudRuntimeException.class)
+    public void testUpdateLoadBalancerRuleNothingChangedStillFails() throws 
Exception {
+        setupUpdateLoadBalancerRule();
+        stubConnectionSettings(null, "5000", null);
+        when(_lbDao.update(lbRuleId, loadBalancerMock)).thenReturn(false);
+
+        UpdateLoadBalancerRuleCmd cmd = new UpdateLoadBalancerRuleCmd();
+        ReflectionTestUtils.setField(cmd, ApiConstants.ID, lbRuleId);
+        ReflectionTestUtils.setField(cmd, "algorithm", "roundrobin");
+        ReflectionTestUtils.setField(cmd, "idleTimeout", 5000L);
+        when(loadBalancerMock.getAlgorithm()).thenReturn("roundrobin");
+
+        lbr.updateLoadBalancerRule(cmd);
+    }
+
+    @Test
+    public void testUpdateLoadBalancerRuleCleanupReappliesConfig() throws 
Exception {
+        setupUpdateLoadBalancerRule();
+        stubConnectionSettings(null, "5000", null);
+        // No column of the rule changes, so the dao reports no row updated
+        when(_lbDao.update(lbRuleId, loadBalancerMock)).thenReturn(false);
+
+        // Only the connection settings change, algorithm and protocol are the 
same
+        UpdateLoadBalancerRuleCmd cmd = new UpdateLoadBalancerRuleCmd();
+        ReflectionTestUtils.setField(cmd, ApiConstants.ID, lbRuleId);
+        ReflectionTestUtils.setField(cmd, "algorithm", "roundrobin");
+        ReflectionTestUtils.setField(cmd, "lbProtocol", NetUtils.SSL_PROTO);
+        ReflectionTestUtils.setField(cmd, "cleanupConnectionSettings", true);
+        when(loadBalancerMock.getAlgorithm()).thenReturn("roundrobin");
+        when(loadBalancerMock.getLbProtocol()).thenReturn(NetUtils.SSL_PROTO);
+
+        lbr.updateLoadBalancerRule(cmd);
+
+        Mockito.verify(_firewallRuleDetailsDao).removeDetail(lbRuleId, 
LoadBalancer.IDLE_TIMEOUT);
+        Mockito.verify(lbr, times(1)).applyLoadBalancerConfig(lbRuleId);
+    }
+
+    /**
+     * Backs the details dao with a map, so a value written and then rolled 
back can be seen.
+     */
+    private Map<String, String> fakeConnectionSettings(Map<String, String> 
initial) {
+        Map<String, String> store = new HashMap<>(initial);
+        when(_firewallRuleDetailsDao.findDetail(Mockito.eq(lbRuleId), 
Mockito.anyString())).thenAnswer(invocation -> {
+            String key = invocation.getArgument(1);
+            String value = store.get(key);
+            return value == null ? null : new FirewallRuleDetailVO(lbRuleId, 
key, value, true);
+        });
+        Mockito.doAnswer(invocation -> 
store.remove(invocation.<String>getArgument(1)))
+                
.when(_firewallRuleDetailsDao).removeDetail(Mockito.eq(lbRuleId), 
Mockito.anyString());
+        Mockito.doAnswer(invocation -> store.put(invocation.getArgument(1), 
invocation.getArgument(2)))
+                .when(_firewallRuleDetailsDao).addDetail(Mockito.eq(lbRuleId), 
Mockito.anyString(), Mockito.anyString(), Mockito.anyBoolean());
+        return store;
+    }
+
+    @Test
+    public void testUpdateLoadBalancerRuleRollsBackSettingsWhenApplyFails() 
throws Exception {
+        setupUpdateLoadBalancerRule();
+        Map<String, String> before = Map.of(LoadBalancer.KEEPALIVE, "true", 
LoadBalancer.IDLE_TIMEOUT, "5000");
+        Map<String, String> store = fakeConnectionSettings(before);
+
+        // Drop keepalive, change idletimeout, add keepalivetimeout, then fail 
to reach the router
+        UpdateLoadBalancerRuleCmd cmd = new UpdateLoadBalancerRuleCmd();
+        ReflectionTestUtils.setField(cmd, ApiConstants.ID, lbRuleId);
+        ReflectionTestUtils.setField(cmd, "algorithm", "roundrobin");
+        ReflectionTestUtils.setField(cmd, "lbProtocol", NetUtils.SSL_PROTO);
+        ReflectionTestUtils.setField(cmd, "idleTimeout", 2000L);
+        ReflectionTestUtils.setField(cmd, "keepAliveTimeout", 1000L);
+        ReflectionTestUtils.setField(cmd, "cleanupConnectionSettings", true);
+        when(loadBalancerMock.getAlgorithm()).thenReturn("roundrobin");
+        when(loadBalancerMock.getLbProtocol()).thenReturn(NetUtils.SSL_PROTO);
+        
Mockito.doThrow(ResourceUnavailableException.class).when(lbr).applyLoadBalancerConfig(lbRuleId);
+        when(_networkMgr.getProvidersForServiceInNetwork(networkMock, 
Network.Service.Lb))
+                
.thenReturn(Collections.singletonList(Network.Provider.VirtualRouter));
+
+        try {
+            lbr.updateLoadBalancerRule(cmd);
+            Assert.fail("Expected the update to fail when the router is 
unavailable");
+        } catch (CloudRuntimeException e) {
+            // expected
+        }
+
+        Mockito.verify(lbr, times(1)).applyLoadBalancerConfig(lbRuleId);
+        Assert.assertEquals(before, store);
+    }
+
+    @Test
+    public void 
testUpdateLoadBalancerRuleLeavesSettingsAloneWhenValidationFails() {
+        AccountVO account = new AccountVO("testaccount", 1L, "networkdomain", 
Account.Type.NORMAL, "uuid");
+        account.setId(accountId);
+        UserVO user = new UserVO(1, "testuser", "password", "firstname", 
"lastName", "email", "timezone",
+                UUID.randomUUID().toString(), User.Source.UNKNOWN);
+        CallContext.register(user, account);
+
+        when(_lbDao.findById(lbRuleId)).thenReturn(loadBalancerMock);
+        when(loadBalancerMock.getNetworkId()).thenReturn(networkId);
+        when(_networkDao.findById(networkId)).thenReturn(networkMock);
+        LoadBalancingRule loadBalancingRule = 
Mockito.mock(LoadBalancingRule.class);
+        
Mockito.doReturn(loadBalancingRule).when(lbr).getLoadBalancerRuleToApply(loadBalancerMock);
+        Mockito.doReturn(false).when(lbr).validateLbRule(loadBalancingRule);
+
+        UpdateLoadBalancerRuleCmd cmd = new UpdateLoadBalancerRuleCmd();
+        ReflectionTestUtils.setField(cmd, ApiConstants.ID, lbRuleId);
+        ReflectionTestUtils.setField(cmd, "idleTimeout", 2000L);
+
+        try {
+            lbr.updateLoadBalancerRule(cmd);
+            Assert.fail("Expected the provider to reject the update");
+        } catch (InvalidParameterValueException e) {
+            // expected
+        }
+
+        verifyNoConnectionSettingWrites();
+    }
 }
diff --git 
a/server/src/test/java/com/cloud/network/lb/UpdateLoadBalancerTest.java 
b/server/src/test/java/com/cloud/network/lb/UpdateLoadBalancerTest.java
index ab25d936baa..73b47ed35de 100644
--- a/server/src/test/java/com/cloud/network/lb/UpdateLoadBalancerTest.java
+++ b/server/src/test/java/com/cloud/network/lb/UpdateLoadBalancerTest.java
@@ -19,20 +19,22 @@ package com.cloud.network.lb;
 import static org.mockito.ArgumentMatchers.any;
 import static org.mockito.ArgumentMatchers.anyLong;
 import static org.mockito.ArgumentMatchers.eq;
-import static org.mockito.ArgumentMatchers.isNull;
 import static org.mockito.ArgumentMatchers.nullable;
 import static org.mockito.Mockito.when;
 
 import java.util.ArrayList;
 import java.util.UUID;
 
+import org.apache.cloudstack.api.ApiConstants;
 import 
org.apache.cloudstack.api.command.user.loadbalancer.UpdateLoadBalancerRuleCmd;
 import org.apache.cloudstack.context.CallContext;
+import org.apache.cloudstack.resourcedetail.dao.FirewallRuleDetailsDao;
 import org.junit.After;
 import org.junit.Before;
 import org.junit.Test;
 import org.mockito.InOrder;
 import org.mockito.Mockito;
+import org.springframework.test.util.ReflectionTestUtils;
 
 import com.cloud.exception.InsufficientCapacityException;
 import com.cloud.exception.InvalidParameterValueException;
@@ -70,6 +72,7 @@ public class UpdateLoadBalancerTest {
     private LoadBalancingServiceProvider lbServiceProvider= 
Mockito.mock(LoadBalancingServiceProvider.class);
 
     private static long domainId = 5L;
+    private static long lbRuleId = 10L;
     private static String accountName = "admin";
 
     @Before
@@ -83,11 +86,13 @@ public class UpdateLoadBalancerTest {
         _lbMgr._lb2stickinesspoliciesDao = 
Mockito.mock(LBStickinessPolicyDao.class);
         _lbMgr._lb2VmMapDao = Mockito.mock(LoadBalancerVMMapDao.class);
         _lbMgr._lbCertMapDao = Mockito.mock(LoadBalancerCertMapDao.class);
+        _lbMgr._firewallRuleDetailsDao = 
Mockito.mock(FirewallRuleDetailsDao.class);
         _lbMgr._lbDao = lbDao;
         _lbMgr._lbProviders = new ArrayList<LoadBalancingServiceProvider>();
         _lbMgr._lbProviders.add(lbServiceProvider);
 
         updateLbRuleCmd = new UpdateLoadBalancerRuleCmd();
+        ReflectionTestUtils.setField(updateLbRuleCmd, ApiConstants.ID, 
lbRuleId);
 
         AccountVO account = new AccountVO(accountName, domainId, 
"networkDomain", Account.Type.NORMAL, "uuid");
         UserVO user = new UserVO(1, "testuser", "password", "firstname", 
"lastName", "email", "timezone", UUID.randomUUID().toString(), 
User.Source.UNKNOWN);
@@ -99,18 +104,18 @@ public class UpdateLoadBalancerTest {
 
         LoadBalancerVO lb = new LoadBalancerVO(null, null, null, 0L, 0, 0, 
null, 0L, 0L, domainId, null, null);
 
-        when(lbDao.findById(isNull())).thenReturn(lb);
+        when(lbDao.findById(lbRuleId)).thenReturn(lb);
         
when(netModel.getPublicIpAddress(anyLong())).thenReturn(Mockito.mock(PublicIpAddress.class));
         
when(netDao.findById(anyLong())).thenReturn(Mockito.mock(NetworkVO.class));
         when(lbServiceProvider.validateLBRule(any(Network.class), 
any(LoadBalancingRule.class))).thenReturn(true);
-        when(lbDao.update(isNull(), eq(lb))).thenReturn(true);
+        when(lbDao.update(eq(lbRuleId), eq(lb))).thenReturn(true);
         
when(netDao.findById(nullable(Long.class))).thenReturn(Mockito.mock(NetworkVO.class));
         
when(ntwkOffServiceMapDao.canProviderSupportServiceInNetworkOffering(nullable(Long.class),
 any(Network.Service.class), any(Network.Provider.class))).thenReturn(false);
         _lbMgr.updateLoadBalancerRule(updateLbRuleCmd);
 
         InOrder inOrder = Mockito.inOrder(lbServiceProvider, lbDao);
         inOrder.verify(lbServiceProvider).validateLBRule(any(Network.class), 
any(LoadBalancingRule.class));
-        inOrder.verify(lbDao).update(isNull(),eq(lb));
+        inOrder.verify(lbDao).update(eq(lbRuleId), eq(lb));
     }
 
     @Test(expected = InvalidParameterValueException.class)
@@ -118,7 +123,7 @@ public class UpdateLoadBalancerTest {
 
         LoadBalancerVO lb = new LoadBalancerVO(null, null, null, 0L, 0, 0, 
null, 0L, 0L, domainId, null, null);
 
-        when(lbDao.findById(anyLong())).thenReturn(lb);
+        when(lbDao.findById(lbRuleId)).thenReturn(lb);
         
when(netModel.getPublicIpAddress(anyLong())).thenReturn(Mockito.mock(PublicIpAddress.class));
         
when(netDao.findById(anyLong())).thenReturn(Mockito.mock(NetworkVO.class));
         when(lbServiceProvider.validateLBRule(any(Network.class), 
any(LoadBalancingRule.class))).thenReturn(false);
diff --git a/systemvm/debian/root/health_checks/haproxy_check.py 
b/systemvm/debian/root/health_checks/haproxy_check.py
index cc9d90f7c18..f06f7a68597 100644
--- a/systemvm/debian/root/health_checks/haproxy_check.py
+++ b/systemvm/debian/root/health_checks/haproxy_check.py
@@ -69,6 +69,52 @@ def checkIdletimeout(haproxyData, haCfgSections):
         return False
     return True
 
+def hasOption(cfgSection, option):
+    return option in cfgSection.get("option", [])
+
+
+def timeoutValue(cfgSection, kind):
+    for tline in cfgSection.get("timeout", []):
+        parts = tline.strip().split(None, 1)
+        if len(parts) == 2 and parts[0].strip() == kind:
+            return parts[1].strip()
+    return None
+
+
+def checkRuleConnectionSettings(lbSec, cfgSection, secName, httpModeExpected):
+    """
+    Per rule keepalive and timeouts, which live in the rule's own listen 
section rather than in
+    defaults. An empty value means the rule inherits, so there is nothing to 
check.
+    """
+    correct = True
+
+    keepAlive = lbSec.get("ruleKeepAlive", "")
+    if keepAlive != "" and httpModeExpected:
+        expected = "http-keep-alive" if keepAlive == "true" else "httpclose"
+        if not hasOption(cfgSection, expected):
+            print("Expected 'option " + expected + "' in " + secName)
+            correct = False
+
+    idleTimeout = lbSec.get("ruleIdleTimeout", "")
+    if idleTimeout != "":
+        for kind in ("client", "server"):
+            found = timeoutValue(cfgSection, kind)
+            if found != idleTimeout:
+                print("Expected 'timeout " + kind + " " + idleTimeout + "' in 
" + secName +
+                      " but found " + str(found))
+                correct = False
+
+    keepAliveTimeout = lbSec.get("ruleKeepAliveTimeout", "")
+    if keepAliveTimeout != "" and keepAlive == "true" and httpModeExpected:
+        found = timeoutValue(cfgSection, "http-keep-alive")
+        if found != keepAliveTimeout:
+            print("Expected 'timeout http-keep-alive " + keepAliveTimeout + "' 
in " + secName +
+                  " but found " + str(found))
+            correct = False
+
+    return correct
+
+
 def checkLoadBalance(haproxyData, haCfgSections):
     correct = True
     for lbSec in haproxyData:
@@ -94,12 +140,20 @@ def checkLoadBalance(haproxyData, haCfgSections):
                     print("Incorrect bind string found. Expected " + bindStr + 
" but found " + cfgSection["bind"][0] + ".")
                     correct = False
 
-                if (lbSec["sourcePortStart"] == "80" and 
lbSec["sourcePortEnd"] == "80" and lbSec["keepAliveEnabled"] == "false") \
-                        or (lbSec["stickiness"].find("AppCookie") != -1 or 
lbSec["stickiness"].find("LbCookie") != -1):
+                # A rule that sets keepalive itself stays in http mode on port 
80. Without it, the
+                # network offering decides, and keepalive there drops the rule 
to tcp mode.
+                onHttpPort = lbSec["sourcePortStart"] == "80" and 
lbSec["sourcePortEnd"] == "80"
+                ruleKeepAlive = lbSec.get("ruleKeepAlive", "")
+                httpModeExpected = (onHttpPort and (ruleKeepAlive != "" or 
lbSec["keepAliveEnabled"] == "false")) \
+                    or lbSec["stickiness"].find("AppCookie") != -1 or 
lbSec["stickiness"].find("LbCookie") != -1
+                if httpModeExpected:
                     if not ("mode" in cfgSection and cfgSection["mode"][0] == 
"http"):
                         print("Expected HTTP mode but not found")
                         correct = False
 
+                if not checkRuleConnectionSettings(lbSec, cfgSection, secName, 
httpModeExpected):
+                    correct = False
+
                 expectedServerIps = lbSec["vmIps"].split(" ")
                 for expectedServerIp in expectedServerIps:
                     pattern = expectedServerIp + ":" + \
diff --git a/ui/public/locales/en.json b/ui/public/locales/en.json
index 2dc2873a21f..a3d3a2285ac 100644
--- a/ui/public/locales/en.json
+++ b/ui/public/locales/en.json
@@ -1406,6 +1406,7 @@
 "label.in.progress": "in progress",
 "label.in.progress.for": "in progress for",
 "label.info": "Info",
+"label.inherit.default": "Use default",
 "label.info.upper": "INFO",
 "label.infrastructure": "Infrastructure",
 "label.ingest.instance": "Ingest Instance",
@@ -1499,6 +1500,8 @@
 "label.isadvanced": "Show advanced settings",
 "label.iscsi": "iSCSI",
 "label.iscustomized": "Custom disk size",
+"label.idletimeout": "Idle timeout (ms)",
+"label.idletimeout.tooltip": "How long an idle connection is held open, in 
milliseconds. Use 0 for no timeout. Leave blank to use the global setting.",
 "label.iscustomizeddiskiops": "Custom IOPS",
 "label.iscustomizediops": "Custom IOPS",
 "label.isdedicated": "Dedicated",
@@ -1548,6 +1551,10 @@
 "label.key": "Key",
 "label.keybits": "Key Bits",
 "label.keyboard": "Keyboard language",
+"label.keepalive": "Keepalive",
+"label.keepalive.tooltip": "Whether the load balancer keeps client connections 
open between requests. Only applies to rules served in HTTP mode. Leave blank 
to use the Network offering setting.",
+"label.keepalivetimeout": "Keepalive timeout (ms)",
+"label.keepalivetimeout.tooltip": "How long an idle keepalive connection is 
held open waiting for the next request, in milliseconds. Only applies when 
keepalive is on.",
 "label.keyboardtype": "Keyboard type",
 "label.keypair": "SSH key pair",
 "label.keypairs": "SSH key pair(s)",
diff --git a/ui/src/views/network/LoadBalancing.vue 
b/ui/src/views/network/LoadBalancing.vue
index fc93e5ab23b..c54421a66fa 100644
--- a/ui/src/views/network/LoadBalancing.vue
+++ b/ui/src/views/network/LoadBalancing.vue
@@ -472,6 +472,61 @@
             :placeholder="$t('label.sourcecidrlist')"
           />
         </div>
+        <div v-if="lbProvider !== 'Netris'" class="edit-rule__item">
+          <p class="edit-rule__label">
+            {{ $t('label.keepalive') }}
+            <tooltip-label
+              :title="''"
+              bold
+              :tooltip="$t('label.keepalive.tooltip')"
+              :tooltip-placement="'right'"
+              style="display: inline; margin-left: 5px;"
+            />
+          </p>
+          <a-select
+            v-model:value="editRuleDetails.keepalive"
+            :placeholder="$t('label.inherit.default')">
+            <a-select-option :value="undefined" 
:label="$t('label.inherit.default')">{{ $t('label.inherit.default') 
}}</a-select-option>
+            <a-select-option :value="true" :label="$t('label.yes')">{{ 
$t('label.yes') }}</a-select-option>
+            <a-select-option :value="false" :label="$t('label.no')">{{ 
$t('label.no') }}</a-select-option>
+          </a-select>
+        </div>
+        <div v-if="lbProvider !== 'Netris'" class="edit-rule__item">
+          <p class="edit-rule__label">
+            {{ $t('label.idletimeout') }}
+            <tooltip-label
+              :title="''"
+              bold
+              :tooltip="$t('label.idletimeout.tooltip')"
+              :tooltip-placement="'right'"
+              style="display: inline; margin-left: 5px;"
+            />
+          </p>
+          <a-input
+            v-model:value="editRuleDetails.idletimeout"
+            type="number"
+            min="0"
+            :placeholder="$t('label.inherit.default')"
+          />
+        </div>
+        <div v-if="lbProvider !== 'Netris'" class="edit-rule__item">
+          <p class="edit-rule__label">
+            {{ $t('label.keepalivetimeout') }}
+            <tooltip-label
+              :title="''"
+              bold
+              :tooltip="$t('label.keepalivetimeout.tooltip')"
+              :tooltip-placement="'right'"
+              style="display: inline; margin-left: 5px;"
+            />
+          </p>
+          <a-input
+            v-model:value="editRuleDetails.keepalivetimeout"
+            type="number"
+            min="0"
+            :placeholder="$t('label.inherit.default')"
+          />
+        </div>
         <div :span="24" class="action-button">
           <a-button @click="() => editRuleModalVisible = false">{{ 
$t('label.cancel') }}</a-button>
           <a-button type="primary" @click="handleSubmitEditForm">{{ 
$t('label.ok') }}</a-button>
@@ -862,7 +917,10 @@ export default {
         name: '',
         algorithm: '',
         protocol: '',
-        cidrlist: ''
+        cidrlist: '',
+        keepalive: undefined,
+        idletimeout: undefined,
+        keepalivetimeout: undefined
       },
       newRule: {
         algorithm: 'roundrobin',
@@ -1668,6 +1726,9 @@ export default {
       this.editRuleDetails.name = this.selectedRule.name
       this.editRuleDetails.algorithm = this.lbProvider !== 'Netris' ? 
this.selectedRule.algorithm : undefined
       this.editRuleDetails.protocol = this.selectedRule.protocol
+      this.editRuleDetails.keepalive = this.selectedRule.keepalive
+      this.editRuleDetails.idletimeout = this.selectedRule.idletimeout
+      this.editRuleDetails.keepalivetimeout = 
this.selectedRule.keepalivetimeout
       // Normalize cidrlist: replace spaces with commas and clean up
       this.editRuleDetails.cidrlist = (this.selectedRule.cidrlist || '')
         .split(/[\s,]+/) // Split on spaces or commas
@@ -1686,11 +1747,19 @@ export default {
           cidrList: (this.editRuleDetails.cidrlist || '').split(',').map(c => 
c.trim()).filter(c => c)
         })
       }
-      postAPI('updateLoadBalancerRule', {
-        ...this.editRuleDetails,
-        id: this.selectedRule.id,
-        ...payload
-      }).then(response => {
+      // A blank field sends nothing, which the API reads as keep the current 
value. When a
+      // field the rule had set is now blank, ask the API to drop the rule's 
own settings;
+      // the ones still filled in are sent alongside and set again.
+      const isBlank = (value) => value === '' || value === null || value === 
undefined
+      for (const key of ['keepalive', 'idletimeout', 'keepalivetimeout']) {
+        if (isBlank(payload[key])) {
+          delete payload[key]
+          if (!isBlank(this.selectedRule[key])) {
+            payload.cleanupconnectionsettings = true
+          }
+        }
+      }
+      postAPI('updateLoadBalancerRule', payload).then(response => {
         this.$pollJob({
           jobId: response.updateloadbalancerruleresponse.jobid,
           successMessage: this.$t('message.success.edit.rule'),

Reply via email to