]>
Commit | Line | Data |
---|---|---|
95c5dca2 MT |
1 | /*############################################################################# |
2 | # # | |
3 | # IPFire.org - A linux based firewall # | |
4 | # Copyright (C) 2023 IPFire Network Development Team # | |
5 | # # | |
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. # | |
10 | # # | |
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. # | |
15 | # # | |
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/>. # | |
18 | # # | |
19 | #############################################################################*/ | |
20 | ||
21 | #include <systemd/sd-netlink.h> | |
22 | ||
23 | #include "config.h" | |
24 | #include "daemon.h" | |
25 | #include "logging.h" | |
26 | #include "port.h" | |
27 | #include "port-bonding.h" | |
28 | #include "string.h" | |
29 | ||
582536b2 MT |
30 | const struct nw_string_table 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" }, | |
95c5dca2 MT |
38 | { -1, NULL }, |
39 | }; | |
40 | ||
582536b2 | 41 | NW_STRING_TABLE_LOOKUP(nw_port_bonding_mode_t, nw_port_bonding_mode) |
95c5dca2 | 42 | |
082d81a3 MT |
43 | static int nw_port_bonding_read_mode(nw_config* config, const char* key, void* data) { |
44 | int* mode = (int*)data; | |
45 | ||
46 | const char* value = nw_config_get(config, key); | |
47 | if (value) { | |
48 | *mode = nw_port_bonding_mode_from_string(value); | |
49 | if (!*mode) | |
50 | return -errno; | |
95c5dca2 MT |
51 | } |
52 | ||
53 | return 0; | |
54 | } | |
55 | ||
082d81a3 MT |
56 | static int nw_port_bonding_write_mode(nw_config* config, const char* key, const void* data) { |
57 | const int* mode = (int*)data; | |
58 | ||
59 | return nw_config_set(config, key, nw_port_bonding_mode_to_string(*mode)); | |
60 | } | |
61 | ||
62 | static int nw_port_bonding_setup(nw_port* port) { | |
95c5dca2 MT |
63 | int r; |
64 | ||
65 | // Mode | |
082d81a3 MT |
66 | r = NW_CONFIG_OPTION(port->config, "BONDING_MODE", &port->bonding.mode, |
67 | nw_port_bonding_read_mode, nw_port_bonding_write_mode); | |
68 | if (r < 0) | |
95c5dca2 MT |
69 | return r; |
70 | ||
71 | return 0; | |
72 | } | |
73 | ||
74 | static int nw_port_bonding_create_link(nw_port* port, sd_netlink_message* m) { | |
75 | int r; | |
76 | ||
77 | // Set mode | |
78 | r = sd_netlink_message_append_u8(m, IFLA_BOND_MODE, port->bonding.mode); | |
79 | if (r < 0) | |
80 | return r; | |
81 | ||
82 | return 0; | |
83 | } | |
84 | ||
e9b0614e MT |
85 | static int nw_port_bonding_to_json(nw_port* port, struct json_object* o) { |
86 | int r; | |
87 | ||
88 | // Add mode | |
89 | r = json_object_add_string(o, "BondingMode", | |
90 | nw_port_bonding_mode_to_string(port->bonding.mode)); | |
91 | if (r < 0) | |
92 | goto ERROR; | |
93 | ||
94 | ERROR: | |
95 | return r; | |
96 | } | |
97 | ||
95c5dca2 MT |
98 | const nw_port_info_t nw_port_info_bonding = { |
99 | .kind = "bond", | |
100 | ||
101 | // Operations | |
102 | .ops = { | |
103 | // Configuration | |
082d81a3 | 104 | .setup = nw_port_bonding_setup, |
95c5dca2 MT |
105 | |
106 | // Link | |
107 | .create_link = nw_port_bonding_create_link, | |
e9b0614e MT |
108 | |
109 | // JSON | |
110 | .to_json = nw_port_bonding_to_json, | |
95c5dca2 MT |
111 | }, |
112 | }; | |
113 | ||
114 | const char* nw_port_bonding_get_mode(nw_port* port) { | |
115 | return nw_port_bonding_mode_to_string(port->bonding.mode); | |
116 | } | |
117 | ||
118 | int nw_port_bonding_set_mode(nw_port* port, const char* mode) { | |
119 | const int m = nw_port_bonding_mode_from_string(mode); | |
120 | ||
121 | switch (m) { | |
122 | case BOND_MODE_ROUNDROBIN: | |
123 | case BOND_MODE_ACTIVEBACKUP: | |
124 | case BOND_MODE_XOR: | |
125 | case BOND_MODE_BROADCAST: | |
126 | case BOND_MODE_8023AD: | |
127 | case BOND_MODE_TLB: | |
128 | case BOND_MODE_ALB: | |
129 | port->bonding.mode = m; | |
130 | break; | |
131 | ||
132 | default: | |
133 | ERROR("%s: Unsupported bonding mode '%s'\n", port->name, mode); | |
134 | errno = ENOTSUP; | |
135 | return 1; | |
136 | } | |
137 | ||
138 | return 0; | |
139 | } |