[PATCH 1/4] bonding: add pure source-mac-based tx hashing option

From: Jarod Wilson
Date: Tue May 18 2021 - 17:09:20 EST


As it turns out, a pure source-mac only tx hash has a place for some VM
setups. The previously added vlan+srcmac hash doesn't work as well for a
VM with a single MAC and multiple vlans -- these types of setups path
traffic more efficiently if the load is split by source mac alone.

Cc: Jay Vosburgh <j.vosburgh@xxxxxxxxx>
Cc: Veaceslav Falico <vfalico@xxxxxxxxx>
Cc: Andy Gospodarek <andy@xxxxxxxxxxxxx>
Cc: "David S. Miller" <davem@xxxxxxxxxxxxx>
Cc: Jakub Kicinski <kuba@xxxxxxxxxx>
Cc: Thomas Davis <tadavis@xxxxxxx>
Cc: netdev@xxxxxxxxxxxxxxx
Signed-off-by: Jarod Wilson <jarod@xxxxxxxxxx>
---
Documentation/networking/bonding.rst | 13 +++++++++++++
drivers/net/bonding/bond_main.c | 26 +++++++++++++++++---------
drivers/net/bonding/bond_options.c | 1 +
include/linux/netdevice.h | 1 +
include/uapi/linux/if_bonding.h | 1 +
5 files changed, 33 insertions(+), 9 deletions(-)

diff --git a/Documentation/networking/bonding.rst b/Documentation/networking/bonding.rst
index 62f2aab8eaec..66c3fa3a9040 100644
--- a/Documentation/networking/bonding.rst
+++ b/Documentation/networking/bonding.rst
@@ -964,6 +964,19 @@ xmit_hash_policy

hash = (vlan ID) XOR (source MAC vendor) XOR (source MAC dev)

+ srcmac
+
+ This policy uses a very rudimentary source mac hash to
+ load-balance traffic per-source-mac, with failover should
+ one leg fail. The intended use case is for a bond shared
+ by multiple virtual machines, each with their own virtual
+ mac address, keeping the VMs traffic all limited to the
+ same outbound interface.
+
+ The formula for the hash is simply
+
+ hash = (source MAC vendor) XOR (source MAC dev)
+
The default value is layer2. This option was added in bonding
version 2.6.3. In earlier versions of bonding, this parameter
does not exist, and the layer2 policy is the only policy. The
diff --git a/drivers/net/bonding/bond_main.c b/drivers/net/bonding/bond_main.c
index 20bbda1b36e1..d71e398642fb 100644
--- a/drivers/net/bonding/bond_main.c
+++ b/drivers/net/bonding/bond_main.c
@@ -167,7 +167,8 @@ module_param(xmit_hash_policy, charp, 0);
MODULE_PARM_DESC(xmit_hash_policy, "balance-alb, balance-tlb, balance-xor, 802.3ad hashing method; "
"0 for layer 2 (default), 1 for layer 3+4, "
"2 for layer 2+3, 3 for encap layer 2+3, "
- "4 for encap layer 3+4, 5 for vlan+srcmac");
+ "4 for encap layer 3+4, 5 for vlan+srcmac, "
+ "6 for srcmac");
module_param(arp_interval, int, 0);
MODULE_PARM_DESC(arp_interval, "arp interval in milliseconds");
module_param_array(arp_ip_target, charp, NULL, 0);
@@ -1459,6 +1460,8 @@ static enum netdev_lag_hash bond_lag_hash_type(struct bonding *bond,
return NETDEV_LAG_HASH_E34;
case BOND_XMIT_POLICY_VLAN_SRCMAC:
return NETDEV_LAG_HASH_VLAN_SRCMAC;
+ case BOND_XMIT_POLICY_SRCMAC:
+ return NETDEV_LAG_HASH_SRCMAC;
default:
return NETDEV_LAG_HASH_UNKNOWN;
}
@@ -3521,11 +3524,11 @@ static bool bond_flow_ip(struct sk_buff *skb, struct flow_keys *fk,
return true;
}

-static u32 bond_vlan_srcmac_hash(struct sk_buff *skb)
+static u32 bond_vlan_srcmac_hash(struct sk_buff *skb, bool with_vlan)
{
- struct ethhdr *mac_hdr = (struct ethhdr *)skb_mac_header(skb);
+ struct ethhdr *mac_hdr = eth_hdr(skb);
u32 srcmac_vendor = 0, srcmac_dev = 0;
- u16 vlan;
+ u32 hash;
int i;

for (i = 0; i < 3; i++)
@@ -3534,12 +3537,14 @@ static u32 bond_vlan_srcmac_hash(struct sk_buff *skb)
for (i = 3; i < ETH_ALEN; i++)
srcmac_dev = (srcmac_dev << 8) | mac_hdr->h_source[i];

- if (!skb_vlan_tag_present(skb))
- return srcmac_vendor ^ srcmac_dev;
+ hash = srcmac_vendor ^ srcmac_dev;
+
+ if (!with_vlan || !skb_vlan_tag_present(skb))
+ return hash;

- vlan = skb_vlan_tag_get(skb);
+ hash ^= skb_vlan_tag_get(skb);

- return vlan ^ srcmac_vendor ^ srcmac_dev;
+ return hash;
}

/* Extract the appropriate headers based on bond's xmit policy */
@@ -3618,8 +3623,11 @@ u32 bond_xmit_hash(struct bonding *bond, struct sk_buff *skb)
skb->l4_hash)
return skb->hash;

+ if (bond->params.xmit_policy == BOND_XMIT_POLICY_SRCMAC)
+ return bond_vlan_srcmac_hash(skb, false);
+
if (bond->params.xmit_policy == BOND_XMIT_POLICY_VLAN_SRCMAC)
- return bond_vlan_srcmac_hash(skb);
+ return bond_vlan_srcmac_hash(skb, true);

if (bond->params.xmit_policy == BOND_XMIT_POLICY_LAYER2 ||
!bond_flow_dissect(bond, skb, &flow))
diff --git a/drivers/net/bonding/bond_options.c b/drivers/net/bonding/bond_options.c
index c9d3604ae129..ff68ad2589f0 100644
--- a/drivers/net/bonding/bond_options.c
+++ b/drivers/net/bonding/bond_options.c
@@ -102,6 +102,7 @@ static const struct bond_opt_value bond_xmit_hashtype_tbl[] = {
{ "encap2+3", BOND_XMIT_POLICY_ENCAP23, 0},
{ "encap3+4", BOND_XMIT_POLICY_ENCAP34, 0},
{ "vlan+srcmac", BOND_XMIT_POLICY_VLAN_SRCMAC, 0},
+ { "srcmac", BOND_XMIT_POLICY_SRCMAC, 0},
{ NULL, -1, 0},
};

diff --git a/include/linux/netdevice.h b/include/linux/netdevice.h
index 5cbc950b34df..d88319fca1d3 100644
--- a/include/linux/netdevice.h
+++ b/include/linux/netdevice.h
@@ -2732,6 +2732,7 @@ enum netdev_lag_hash {
NETDEV_LAG_HASH_E23,
NETDEV_LAG_HASH_E34,
NETDEV_LAG_HASH_VLAN_SRCMAC,
+ NETDEV_LAG_HASH_SRCMAC,
NETDEV_LAG_HASH_UNKNOWN,
};

diff --git a/include/uapi/linux/if_bonding.h b/include/uapi/linux/if_bonding.h
index d174914a837d..f3b4d412a73f 100644
--- a/include/uapi/linux/if_bonding.h
+++ b/include/uapi/linux/if_bonding.h
@@ -95,6 +95,7 @@
#define BOND_XMIT_POLICY_ENCAP23 3 /* encapsulated layer 2+3 */
#define BOND_XMIT_POLICY_ENCAP34 4 /* encapsulated layer 3+4 */
#define BOND_XMIT_POLICY_VLAN_SRCMAC 5 /* vlan + source MAC */
+#define BOND_XMIT_POLICY_SRCMAC 6 /* source MAC only */

/* 802.3ad port state definitions (43.4.2.2 in the 802.3ad standard) */
#define LACP_STATE_LACP_ACTIVITY 0x1
--
2.30.2