1 /*#############################################################################
3 # IPFire.org - A linux based firewall #
4 # Copyright (C) 2023 IPFire Network Development Team #
6 # This program is free software: you can redistribute it and/or modify #
7 # it under the terms of the GNU General Public License as published by #
8 # the Free Software Foundation, either version 3 of the License, or #
9 # (at your option) any later version. #
11 # This program is distributed in the hope that it will be useful, #
12 # but WITHOUT ANY WARRANTY; without even the implied warranty of #
13 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the #
14 # GNU General Public License for more details. #
16 # You should have received a copy of the GNU General Public License #
17 # along with this program. If not, see <http://www.gnu.org/licenses/>. #
19 #############################################################################*/
21 #include <systemd/sd-netlink.h>
27 #include "port-bonding.h"
30 const nw_string_table_t nw_port_bonding_mode
[] = {
31 { NW_BONDING_MODE_ROUNDROBIN
, "round-robin" },
32 { NW_BONDING_MODE_ACTIVEBACKUP
, "active-backup" },
33 { NW_BONDING_MODE_XOR
, "xor" },
34 { NW_BONDING_MODE_BROADCAST
, "broadcast" },
35 { NW_BONDING_MODE_8023AD
, "802.3ad" },
36 { NW_BONDING_MODE_TLB
, "tlb" },
37 { NW_BONDING_MODE_ALB
, "alb" },
41 NW_STRING_TABLE_LOOKUP(nw_port_bonding_mode_t
, nw_port_bonding_mode
)
43 static int nw_port_bonding_read_mode(nw_config
* config
,
44 const char* key
, void* value
, void* data
) {
45 int* mode
= (int*)value
;
47 const char* p
= nw_config_get(config
, key
);
49 *mode
= nw_port_bonding_mode_from_string(p
);
57 static int nw_port_bonding_write_mode(nw_config
* config
,
58 const char* key
, const void* value
, void* data
) {
59 const int* mode
= (int*)value
;
61 return nw_config_set(config
, key
, nw_port_bonding_mode_to_string(*mode
));
64 static int nw_port_bonding_setup(nw_port
* port
) {
68 r
= NW_CONFIG_OPTION(port
->config
, "BONDING_MODE", &port
->bonding
.mode
,
69 nw_port_bonding_read_mode
, nw_port_bonding_write_mode
, NULL
);
76 static int nw_port_bonding_create_link(nw_port
* port
, sd_netlink_message
* m
) {
80 r
= sd_netlink_message_append_u8(m
, IFLA_BOND_MODE
, port
->bonding
.mode
);
87 static int nw_port_bonding_to_json(nw_port
* port
, struct json_object
* o
) {
91 r
= json_object_add_string(o
, "BondingMode",
92 nw_port_bonding_mode_to_string(port
->bonding
.mode
));
100 const nw_port_info_t nw_port_info_bonding
= {
106 .setup
= nw_port_bonding_setup
,
109 .create_link
= nw_port_bonding_create_link
,
112 .to_json
= nw_port_bonding_to_json
,
116 const char* nw_port_bonding_get_mode(nw_port
* port
) {
117 return nw_port_bonding_mode_to_string(port
->bonding
.mode
);
120 int nw_port_bonding_set_mode(nw_port
* port
, const char* mode
) {
121 const int m
= nw_port_bonding_mode_from_string(mode
);
124 case BOND_MODE_ROUNDROBIN
:
125 case BOND_MODE_ACTIVEBACKUP
:
127 case BOND_MODE_BROADCAST
:
128 case BOND_MODE_8023AD
:
131 port
->bonding
.mode
= m
;
135 ERROR("%s: Unsupported bonding mode '%s'\n", port
->name
, mode
);