From: Michael Tremer Date: Thu, 8 Jun 2023 15:12:00 +0000 (+0000) Subject: ports: bonding: Convert mode to string table X-Git-Url: http://git.ipfire.org/?a=commitdiff_plain;h=582536b21276f11b040eef80e185635b312556da;p=network.git ports: bonding: Convert mode to string table Signed-off-by: Michael Tremer --- diff --git a/src/networkd/port-bonding.c b/src/networkd/port-bonding.c index a2e0fc3f..e7657919 100644 --- a/src/networkd/port-bonding.c +++ b/src/networkd/port-bonding.c @@ -27,41 +27,18 @@ #include "port-bonding.h" #include "string.h" -const struct nw_port_bonding_mode { - const int mode; - const char* string; -} nw_port_bonding_modes[] = { - { BOND_MODE_ROUNDROBIN, "round-robin" }, - { BOND_MODE_ACTIVEBACKUP, "active-backup" }, - { BOND_MODE_XOR, "xor" }, - { BOND_MODE_BROADCAST, "broadcast" }, - { BOND_MODE_8023AD, "802.3ad" }, - { BOND_MODE_TLB, "tlb" }, - { BOND_MODE_ALB, "alb" }, +const struct nw_string_table nw_port_bonding_mode[] = { + { NW_BONDING_MODE_ROUNDROBIN, "round-robin" }, + { NW_BONDING_MODE_ACTIVEBACKUP, "active-backup" }, + { NW_BONDING_MODE_XOR, "xor" }, + { NW_BONDING_MODE_BROADCAST, "broadcast" }, + { NW_BONDING_MODE_8023AD, "802.3ad" }, + { NW_BONDING_MODE_TLB, "tlb" }, + { NW_BONDING_MODE_ALB, "alb" }, { -1, NULL }, }; -static int nw_port_bonding_mode_from_string(const char* string) { - const struct nw_port_bonding_mode* m = NULL; - - for (m = nw_port_bonding_modes; m->string; m++) { - if (strcmp(m->string, string) == 0) - return m->mode; - } - - return -1; -} - -static const char* nw_port_bonding_mode_to_string(const int mode) { - const struct nw_port_bonding_mode* m = NULL; - - for (m = nw_port_bonding_modes; m->string; m++) { - if (m->mode == mode) - return m->string; - } - - return NULL; -} +NW_STRING_TABLE_LOOKUP(nw_port_bonding_mode_t, nw_port_bonding_mode) static int nw_port_bonding_read_mode(nw_config* config, const char* key, void* data) { int* mode = (int*)data; diff --git a/src/networkd/port-bonding.h b/src/networkd/port-bonding.h index 5cd2c433..e21c251d 100644 --- a/src/networkd/port-bonding.h +++ b/src/networkd/port-bonding.h @@ -25,8 +25,18 @@ #include "port.h" +typedef enum nw_port_bonding_mode { + NW_BONDING_MODE_ROUNDROBIN = BOND_MODE_ROUNDROBIN, + NW_BONDING_MODE_ACTIVEBACKUP = BOND_MODE_ACTIVEBACKUP, + NW_BONDING_MODE_XOR = BOND_MODE_XOR, + NW_BONDING_MODE_BROADCAST = BOND_MODE_BROADCAST, + NW_BONDING_MODE_8023AD = BOND_MODE_8023AD, + NW_BONDING_MODE_TLB = BOND_MODE_TLB, + NW_BONDING_MODE_ALB = BOND_MODE_ALB, +} nw_port_bonding_mode_t; + struct nw_port_bonding { - int mode; + nw_port_bonding_mode_t mode; }; extern const nw_port_info_t nw_port_info_bonding;