Merge pull request #15165 from ssahani/bonding

network: Move common functions of bonding to bond-util
This commit is contained in:
Zbigniew Jędrzejewski-Szmek 2020-05-18 08:52:13 +02:00 committed by GitHub
commit d0e3e76bb1
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
6 changed files with 188 additions and 184 deletions

View File

@ -2,13 +2,13 @@
#include "alloc-util.h"
#include "bond.h"
#include "bond-util.h"
#include "conf-parser.h"
#include "ether-addr-util.h"
#include "extract-word.h"
#include "netlink-util.h"
#include "networkd-manager.h"
#include "string-table.h"
#include "string-util.h"
/*
* Number of seconds between instances where the bonding
@ -41,84 +41,16 @@
#define GRATUITOUS_ARP_MAX 255
#define GRATUITOUS_ARP_DEFAULT 1
static const char* const bond_mode_table[_NETDEV_BOND_MODE_MAX] = {
[NETDEV_BOND_MODE_BALANCE_RR] = "balance-rr",
[NETDEV_BOND_MODE_ACTIVE_BACKUP] = "active-backup",
[NETDEV_BOND_MODE_BALANCE_XOR] = "balance-xor",
[NETDEV_BOND_MODE_BROADCAST] = "broadcast",
[NETDEV_BOND_MODE_802_3AD] = "802.3ad",
[NETDEV_BOND_MODE_BALANCE_TLB] = "balance-tlb",
[NETDEV_BOND_MODE_BALANCE_ALB] = "balance-alb",
};
DEFINE_STRING_TABLE_LOOKUP(bond_mode, BondMode);
DEFINE_CONFIG_PARSE_ENUM(config_parse_bond_mode, bond_mode, BondMode, "Failed to parse bond mode");
static const char* const bond_xmit_hash_policy_table[_NETDEV_BOND_XMIT_HASH_POLICY_MAX] = {
[NETDEV_BOND_XMIT_HASH_POLICY_LAYER2] = "layer2",
[NETDEV_BOND_XMIT_HASH_POLICY_LAYER34] = "layer3+4",
[NETDEV_BOND_XMIT_HASH_POLICY_LAYER23] = "layer2+3",
[NETDEV_BOND_XMIT_HASH_POLICY_ENCAP23] = "encap2+3",
[NETDEV_BOND_XMIT_HASH_POLICY_ENCAP34] = "encap3+4",
};
DEFINE_STRING_TABLE_LOOKUP(bond_xmit_hash_policy, BondXmitHashPolicy);
DEFINE_CONFIG_PARSE_ENUM(config_parse_bond_xmit_hash_policy,
bond_xmit_hash_policy,
BondXmitHashPolicy,
"Failed to parse bond transmit hash policy")
static const char* const bond_lacp_rate_table[_NETDEV_BOND_LACP_RATE_MAX] = {
[NETDEV_BOND_LACP_RATE_SLOW] = "slow",
[NETDEV_BOND_LACP_RATE_FAST] = "fast",
};
DEFINE_STRING_TABLE_LOOKUP(bond_lacp_rate, BondLacpRate);
DEFINE_CONFIG_PARSE_ENUM(config_parse_bond_lacp_rate, bond_lacp_rate, BondLacpRate, "Failed to parse bond lacp rate")
static const char* const bond_ad_select_table[_NETDEV_BOND_AD_SELECT_MAX] = {
[NETDEV_BOND_AD_SELECT_STABLE] = "stable",
[NETDEV_BOND_AD_SELECT_BANDWIDTH] = "bandwidth",
[NETDEV_BOND_AD_SELECT_COUNT] = "count",
};
DEFINE_STRING_TABLE_LOOKUP(bond_ad_select, BondAdSelect);
"Failed to parse bond transmit hash policy");
DEFINE_CONFIG_PARSE_ENUM(config_parse_bond_lacp_rate, bond_lacp_rate, BondLacpRate, "Failed to parse bond lacp rate");
DEFINE_CONFIG_PARSE_ENUM(config_parse_bond_ad_select, bond_ad_select, BondAdSelect, "Failed to parse bond AD select");
static const char* const bond_fail_over_mac_table[_NETDEV_BOND_FAIL_OVER_MAC_MAX] = {
[NETDEV_BOND_FAIL_OVER_MAC_NONE] = "none",
[NETDEV_BOND_FAIL_OVER_MAC_ACTIVE] = "active",
[NETDEV_BOND_FAIL_OVER_MAC_FOLLOW] = "follow",
};
DEFINE_STRING_TABLE_LOOKUP(bond_fail_over_mac, BondFailOverMac);
DEFINE_CONFIG_PARSE_ENUM(config_parse_bond_fail_over_mac, bond_fail_over_mac, BondFailOverMac, "Failed to parse bond fail over MAC");
static const char *const bond_arp_validate_table[_NETDEV_BOND_ARP_VALIDATE_MAX] = {
[NETDEV_BOND_ARP_VALIDATE_NONE] = "none",
[NETDEV_BOND_ARP_VALIDATE_ACTIVE]= "active",
[NETDEV_BOND_ARP_VALIDATE_BACKUP]= "backup",
[NETDEV_BOND_ARP_VALIDATE_ALL]= "all",
};
DEFINE_STRING_TABLE_LOOKUP(bond_arp_validate, BondArpValidate);
DEFINE_CONFIG_PARSE_ENUM(config_parse_bond_arp_validate, bond_arp_validate, BondArpValidate, "Failed to parse bond arp validate");
static const char *const bond_arp_all_targets_table[_NETDEV_BOND_ARP_ALL_TARGETS_MAX] = {
[NETDEV_BOND_ARP_ALL_TARGETS_ANY] = "any",
[NETDEV_BOND_ARP_ALL_TARGETS_ALL] = "all",
};
DEFINE_STRING_TABLE_LOOKUP(bond_arp_all_targets, BondArpAllTargets);
DEFINE_CONFIG_PARSE_ENUM(config_parse_bond_arp_all_targets, bond_arp_all_targets, BondArpAllTargets, "Failed to parse bond Arp all targets");
static const char *const bond_primary_reselect_table[_NETDEV_BOND_PRIMARY_RESELECT_MAX] = {
[NETDEV_BOND_PRIMARY_RESELECT_ALWAYS] = "always",
[NETDEV_BOND_PRIMARY_RESELECT_BETTER]= "better",
[NETDEV_BOND_PRIMARY_RESELECT_FAILURE]= "failure",
};
DEFINE_STRING_TABLE_LOOKUP(bond_primary_reselect, BondPrimaryReselect);
DEFINE_CONFIG_PARSE_ENUM(config_parse_bond_primary_reselect, bond_primary_reselect, BondPrimaryReselect, "Failed to parse bond primary reselect");
static int netdev_bond_fill_message_create(NetDev *netdev, Link *link, sd_netlink_message *m) {

View File

@ -4,85 +4,11 @@
#include <netinet/in.h>
#include <linux/if_bonding.h>
#include "in-addr-util.h"
#include "bond-util.h"
#include "macro.h"
#include "netdev.h"
#include "ordered-set.h"
/*
* Maximum number of targets supported by the kernel for a single
* bond netdev.
*/
#define NETDEV_BOND_ARP_TARGETS_MAX 16
typedef enum BondMode {
NETDEV_BOND_MODE_BALANCE_RR = BOND_MODE_ROUNDROBIN,
NETDEV_BOND_MODE_ACTIVE_BACKUP = BOND_MODE_ACTIVEBACKUP,
NETDEV_BOND_MODE_BALANCE_XOR = BOND_MODE_XOR,
NETDEV_BOND_MODE_BROADCAST = BOND_MODE_BROADCAST,
NETDEV_BOND_MODE_802_3AD = BOND_MODE_8023AD,
NETDEV_BOND_MODE_BALANCE_TLB = BOND_MODE_TLB,
NETDEV_BOND_MODE_BALANCE_ALB = BOND_MODE_ALB,
_NETDEV_BOND_MODE_MAX,
_NETDEV_BOND_MODE_INVALID = -1
} BondMode;
typedef enum BondXmitHashPolicy {
NETDEV_BOND_XMIT_HASH_POLICY_LAYER2 = BOND_XMIT_POLICY_LAYER2,
NETDEV_BOND_XMIT_HASH_POLICY_LAYER34 = BOND_XMIT_POLICY_LAYER34,
NETDEV_BOND_XMIT_HASH_POLICY_LAYER23 = BOND_XMIT_POLICY_LAYER23,
NETDEV_BOND_XMIT_HASH_POLICY_ENCAP23 = BOND_XMIT_POLICY_ENCAP23,
NETDEV_BOND_XMIT_HASH_POLICY_ENCAP34 = BOND_XMIT_POLICY_ENCAP34,
_NETDEV_BOND_XMIT_HASH_POLICY_MAX,
_NETDEV_BOND_XMIT_HASH_POLICY_INVALID = -1
} BondXmitHashPolicy;
typedef enum BondLacpRate {
NETDEV_BOND_LACP_RATE_SLOW,
NETDEV_BOND_LACP_RATE_FAST,
_NETDEV_BOND_LACP_RATE_MAX,
_NETDEV_BOND_LACP_RATE_INVALID = -1,
} BondLacpRate;
typedef enum BondAdSelect {
NETDEV_BOND_AD_SELECT_STABLE,
NETDEV_BOND_AD_SELECT_BANDWIDTH,
NETDEV_BOND_AD_SELECT_COUNT,
_NETDEV_BOND_AD_SELECT_MAX,
_NETDEV_BOND_AD_SELECT_INVALID = -1,
} BondAdSelect;
typedef enum BondFailOverMac {
NETDEV_BOND_FAIL_OVER_MAC_NONE,
NETDEV_BOND_FAIL_OVER_MAC_ACTIVE,
NETDEV_BOND_FAIL_OVER_MAC_FOLLOW,
_NETDEV_BOND_FAIL_OVER_MAC_MAX,
_NETDEV_BOND_FAIL_OVER_MAC_INVALID = -1,
} BondFailOverMac;
typedef enum BondArpValidate {
NETDEV_BOND_ARP_VALIDATE_NONE,
NETDEV_BOND_ARP_VALIDATE_ACTIVE,
NETDEV_BOND_ARP_VALIDATE_BACKUP,
NETDEV_BOND_ARP_VALIDATE_ALL,
_NETDEV_BOND_ARP_VALIDATE_MAX,
_NETDEV_BOND_ARP_VALIDATE_INVALID = -1,
} BondArpValidate;
typedef enum BondArpAllTargets {
NETDEV_BOND_ARP_ALL_TARGETS_ANY,
NETDEV_BOND_ARP_ALL_TARGETS_ALL,
_NETDEV_BOND_ARP_ALL_TARGETS_MAX,
_NETDEV_BOND_ARP_ALL_TARGETS_INVALID = -1,
} BondArpAllTargets;
typedef enum BondPrimaryReselect {
NETDEV_BOND_PRIMARY_RESELECT_ALWAYS,
NETDEV_BOND_PRIMARY_RESELECT_BETTER,
NETDEV_BOND_PRIMARY_RESELECT_FAILURE,
_NETDEV_BOND_PRIMARY_RESELECT_MAX,
_NETDEV_BOND_PRIMARY_RESELECT_INVALID = -1,
} BondPrimaryReselect;
typedef struct Bond {
NetDev meta;
@ -122,30 +48,6 @@ extern const NetDevVTable bond_vtable;
int link_set_bond(Link *link);
const char *bond_mode_to_string(BondMode d) _const_;
BondMode bond_mode_from_string(const char *d) _pure_;
const char *bond_xmit_hash_policy_to_string(BondXmitHashPolicy d) _const_;
BondXmitHashPolicy bond_xmit_hash_policy_from_string(const char *d) _pure_;
const char *bond_lacp_rate_to_string(BondLacpRate d) _const_;
BondLacpRate bond_lacp_rate_from_string(const char *d) _pure_;
const char *bond_fail_over_mac_to_string(BondFailOverMac d) _const_;
BondFailOverMac bond_fail_over_mac_from_string(const char *d) _pure_;
const char *bond_ad_select_to_string(BondAdSelect d) _const_;
BondAdSelect bond_ad_select_from_string(const char *d) _pure_;
const char *bond_arp_validate_to_string(BondArpValidate d) _const_;
BondArpValidate bond_arp_validate_from_string(const char *d) _pure_;
const char *bond_arp_all_targets_to_string(BondArpAllTargets d) _const_;
BondArpAllTargets bond_arp_all_targets_from_string(const char *d) _pure_;
const char *bond_primary_reselect_to_string(BondPrimaryReselect d) _const_;
BondPrimaryReselect bond_primary_reselect_from_string(const char *d) _pure_;
CONFIG_PARSER_PROTOTYPE(config_parse_bond_mode);
CONFIG_PARSER_PROTOTYPE(config_parse_bond_xmit_hash_policy);
CONFIG_PARSER_PROTOTYPE(config_parse_bond_lacp_rate);

View File

@ -18,6 +18,7 @@
#include "sd-network.h"
#include "alloc-util.h"
#include "bond-util.h"
#include "bus-common-errors.h"
#include "bus-error.h"
#include "bus-util.h"
@ -1531,22 +1532,10 @@ static int link_status_one(
TABLE_STRING, bridge_state_to_string(info->port_state));
}
} else if (streq_ptr(info->netdev_kind, "bond")) {
static const struct {
const char *mode;
} mode_table[] = {
{ "balance-rr" },
{ "active-backup" },
{ "balance-xor" },
{ "broadcast" },
{ "802.3ad" },
{ "balance-tlb" },
{ "balance-alb" },
};
r = table_add_many(table,
TABLE_EMPTY,
TABLE_STRING, "Mode:",
TABLE_STRING, mode_table[info->mode],
TABLE_STRING, bond_mode_to_string(info->mode),
TABLE_EMPTY,
TABLE_STRING, "Miimon:",
TABLE_TIMESPAN_MSEC, jiffies_to_usec(info->miimon),

73
src/shared/bond-util.c Normal file
View File

@ -0,0 +1,73 @@
/* SPDX-License-Identifier: LGPL-2.1+ */
#include "bond-util.h"
#include "string-table.h"
static const char* const bond_mode_table[_NETDEV_BOND_MODE_MAX] = {
[NETDEV_BOND_MODE_BALANCE_RR] = "balance-rr",
[NETDEV_BOND_MODE_ACTIVE_BACKUP] = "active-backup",
[NETDEV_BOND_MODE_BALANCE_XOR] = "balance-xor",
[NETDEV_BOND_MODE_BROADCAST] = "broadcast",
[NETDEV_BOND_MODE_802_3AD] = "802.3ad",
[NETDEV_BOND_MODE_BALANCE_TLB] = "balance-tlb",
[NETDEV_BOND_MODE_BALANCE_ALB] = "balance-alb",
};
DEFINE_STRING_TABLE_LOOKUP(bond_mode, BondMode);
static const char* const bond_xmit_hash_policy_table[_NETDEV_BOND_XMIT_HASH_POLICY_MAX] = {
[NETDEV_BOND_XMIT_HASH_POLICY_LAYER2] = "layer2",
[NETDEV_BOND_XMIT_HASH_POLICY_LAYER34] = "layer3+4",
[NETDEV_BOND_XMIT_HASH_POLICY_LAYER23] = "layer2+3",
[NETDEV_BOND_XMIT_HASH_POLICY_ENCAP23] = "encap2+3",
[NETDEV_BOND_XMIT_HASH_POLICY_ENCAP34] = "encap3+4",
};
DEFINE_STRING_TABLE_LOOKUP(bond_xmit_hash_policy, BondXmitHashPolicy);
static const char* const bond_lacp_rate_table[_NETDEV_BOND_LACP_RATE_MAX] = {
[NETDEV_BOND_LACP_RATE_SLOW] = "slow",
[NETDEV_BOND_LACP_RATE_FAST] = "fast",
};
DEFINE_STRING_TABLE_LOOKUP(bond_lacp_rate, BondLacpRate);
static const char* const bond_ad_select_table[_NETDEV_BOND_AD_SELECT_MAX] = {
[NETDEV_BOND_AD_SELECT_STABLE] = "stable",
[NETDEV_BOND_AD_SELECT_BANDWIDTH] = "bandwidth",
[NETDEV_BOND_AD_SELECT_COUNT] = "count",
};
DEFINE_STRING_TABLE_LOOKUP(bond_ad_select, BondAdSelect);
static const char* const bond_fail_over_mac_table[_NETDEV_BOND_FAIL_OVER_MAC_MAX] = {
[NETDEV_BOND_FAIL_OVER_MAC_NONE] = "none",
[NETDEV_BOND_FAIL_OVER_MAC_ACTIVE] = "active",
[NETDEV_BOND_FAIL_OVER_MAC_FOLLOW] = "follow",
};
DEFINE_STRING_TABLE_LOOKUP(bond_fail_over_mac, BondFailOverMac);
static const char *const bond_arp_validate_table[_NETDEV_BOND_ARP_VALIDATE_MAX] = {
[NETDEV_BOND_ARP_VALIDATE_NONE] = "none",
[NETDEV_BOND_ARP_VALIDATE_ACTIVE]= "active",
[NETDEV_BOND_ARP_VALIDATE_BACKUP]= "backup",
[NETDEV_BOND_ARP_VALIDATE_ALL]= "all",
};
DEFINE_STRING_TABLE_LOOKUP(bond_arp_validate, BondArpValidate);
static const char *const bond_arp_all_targets_table[_NETDEV_BOND_ARP_ALL_TARGETS_MAX] = {
[NETDEV_BOND_ARP_ALL_TARGETS_ANY] = "any",
[NETDEV_BOND_ARP_ALL_TARGETS_ALL] = "all",
};
DEFINE_STRING_TABLE_LOOKUP(bond_arp_all_targets, BondArpAllTargets);
static const char *const bond_primary_reselect_table[_NETDEV_BOND_PRIMARY_RESELECT_MAX] = {
[NETDEV_BOND_PRIMARY_RESELECT_ALWAYS] = "always",
[NETDEV_BOND_PRIMARY_RESELECT_BETTER]= "better",
[NETDEV_BOND_PRIMARY_RESELECT_FAILURE]= "failure",
};
DEFINE_STRING_TABLE_LOOKUP(bond_primary_reselect, BondPrimaryReselect);

106
src/shared/bond-util.h Normal file
View File

@ -0,0 +1,106 @@
/* SPDX-License-Identifier: LGPL-2.1+ */
#pragma once
#include <netinet/in.h>
#include <linux/if_bonding.h>
#include "macro.h"
/*
* Maximum number of targets supported by the kernel for a single
* bond netdev.
*/
#define NETDEV_BOND_ARP_TARGETS_MAX 16
typedef enum BondMode {
NETDEV_BOND_MODE_BALANCE_RR = BOND_MODE_ROUNDROBIN,
NETDEV_BOND_MODE_ACTIVE_BACKUP = BOND_MODE_ACTIVEBACKUP,
NETDEV_BOND_MODE_BALANCE_XOR = BOND_MODE_XOR,
NETDEV_BOND_MODE_BROADCAST = BOND_MODE_BROADCAST,
NETDEV_BOND_MODE_802_3AD = BOND_MODE_8023AD,
NETDEV_BOND_MODE_BALANCE_TLB = BOND_MODE_TLB,
NETDEV_BOND_MODE_BALANCE_ALB = BOND_MODE_ALB,
_NETDEV_BOND_MODE_MAX,
_NETDEV_BOND_MODE_INVALID = -1
} BondMode;
typedef enum BondXmitHashPolicy {
NETDEV_BOND_XMIT_HASH_POLICY_LAYER2 = BOND_XMIT_POLICY_LAYER2,
NETDEV_BOND_XMIT_HASH_POLICY_LAYER34 = BOND_XMIT_POLICY_LAYER34,
NETDEV_BOND_XMIT_HASH_POLICY_LAYER23 = BOND_XMIT_POLICY_LAYER23,
NETDEV_BOND_XMIT_HASH_POLICY_ENCAP23 = BOND_XMIT_POLICY_ENCAP23,
NETDEV_BOND_XMIT_HASH_POLICY_ENCAP34 = BOND_XMIT_POLICY_ENCAP34,
_NETDEV_BOND_XMIT_HASH_POLICY_MAX,
_NETDEV_BOND_XMIT_HASH_POLICY_INVALID = -1
} BondXmitHashPolicy;
typedef enum BondLacpRate {
NETDEV_BOND_LACP_RATE_SLOW,
NETDEV_BOND_LACP_RATE_FAST,
_NETDEV_BOND_LACP_RATE_MAX,
_NETDEV_BOND_LACP_RATE_INVALID = -1,
} BondLacpRate;
typedef enum BondAdSelect {
NETDEV_BOND_AD_SELECT_STABLE,
NETDEV_BOND_AD_SELECT_BANDWIDTH,
NETDEV_BOND_AD_SELECT_COUNT,
_NETDEV_BOND_AD_SELECT_MAX,
_NETDEV_BOND_AD_SELECT_INVALID = -1,
} BondAdSelect;
typedef enum BondFailOverMac {
NETDEV_BOND_FAIL_OVER_MAC_NONE,
NETDEV_BOND_FAIL_OVER_MAC_ACTIVE,
NETDEV_BOND_FAIL_OVER_MAC_FOLLOW,
_NETDEV_BOND_FAIL_OVER_MAC_MAX,
_NETDEV_BOND_FAIL_OVER_MAC_INVALID = -1,
} BondFailOverMac;
typedef enum BondArpValidate {
NETDEV_BOND_ARP_VALIDATE_NONE,
NETDEV_BOND_ARP_VALIDATE_ACTIVE,
NETDEV_BOND_ARP_VALIDATE_BACKUP,
NETDEV_BOND_ARP_VALIDATE_ALL,
_NETDEV_BOND_ARP_VALIDATE_MAX,
_NETDEV_BOND_ARP_VALIDATE_INVALID = -1,
} BondArpValidate;
typedef enum BondArpAllTargets {
NETDEV_BOND_ARP_ALL_TARGETS_ANY,
NETDEV_BOND_ARP_ALL_TARGETS_ALL,
_NETDEV_BOND_ARP_ALL_TARGETS_MAX,
_NETDEV_BOND_ARP_ALL_TARGETS_INVALID = -1,
} BondArpAllTargets;
typedef enum BondPrimaryReselect {
NETDEV_BOND_PRIMARY_RESELECT_ALWAYS,
NETDEV_BOND_PRIMARY_RESELECT_BETTER,
NETDEV_BOND_PRIMARY_RESELECT_FAILURE,
_NETDEV_BOND_PRIMARY_RESELECT_MAX,
_NETDEV_BOND_PRIMARY_RESELECT_INVALID = -1,
} BondPrimaryReselect;
const char *bond_mode_to_string(BondMode d) _const_;
BondMode bond_mode_from_string(const char *d) _pure_;
const char *bond_xmit_hash_policy_to_string(BondXmitHashPolicy d) _const_;
BondXmitHashPolicy bond_xmit_hash_policy_from_string(const char *d) _pure_;
const char *bond_lacp_rate_to_string(BondLacpRate d) _const_;
BondLacpRate bond_lacp_rate_from_string(const char *d) _pure_;
const char *bond_fail_over_mac_to_string(BondFailOverMac d) _const_;
BondFailOverMac bond_fail_over_mac_from_string(const char *d) _pure_;
const char *bond_ad_select_to_string(BondAdSelect d) _const_;
BondAdSelect bond_ad_select_from_string(const char *d) _pure_;
const char *bond_arp_validate_to_string(BondArpValidate d) _const_;
BondArpValidate bond_arp_validate_from_string(const char *d) _pure_;
const char *bond_arp_all_targets_to_string(BondArpAllTargets d) _const_;
BondArpAllTargets bond_arp_all_targets_from_string(const char *d) _pure_;
const char *bond_primary_reselect_to_string(BondPrimaryReselect d) _const_;
BondPrimaryReselect bond_primary_reselect_from_string(const char *d) _pure_;

View File

@ -17,6 +17,8 @@ shared_sources = files('''
bitmap.c
bitmap.h
blkid-util.h
bond-util.c
bond-util.h
boot-timestamps.c
boot-timestamps.h
bootspec.c