]>
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 | ||
644eb1c8 | 30 | const nw_string_table_t nw_port_bonding_mode[] = { |
582536b2 MT |
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 | 43 | static int nw_port_bonding_setup(nw_port* port) { |
95c5dca2 MT |
44 | int r; |
45 | ||
46 | // Mode | |
7442668a MT |
47 | r = NW_CONFIG_OPTION_STRING_TABLE(port->config, |
48 | "BONDING_MODE", &port->bonding.mode, nw_port_bonding_mode); | |
082d81a3 | 49 | if (r < 0) |
95c5dca2 MT |
50 | return r; |
51 | ||
52 | return 0; | |
53 | } | |
54 | ||
55 | static int nw_port_bonding_create_link(nw_port* port, sd_netlink_message* m) { | |
56 | int r; | |
57 | ||
58 | // Set mode | |
59 | r = sd_netlink_message_append_u8(m, IFLA_BOND_MODE, port->bonding.mode); | |
60 | if (r < 0) | |
61 | return r; | |
62 | ||
63 | return 0; | |
64 | } | |
65 | ||
e9b0614e MT |
66 | static int nw_port_bonding_to_json(nw_port* port, struct json_object* o) { |
67 | int r; | |
68 | ||
69 | // Add mode | |
70 | r = json_object_add_string(o, "BondingMode", | |
71 | nw_port_bonding_mode_to_string(port->bonding.mode)); | |
72 | if (r < 0) | |
73 | goto ERROR; | |
74 | ||
75 | ERROR: | |
76 | return r; | |
77 | } | |
78 | ||
c464b5d1 | 79 | const nw_port_type_t nw_port_type_bonding = { |
95c5dca2 MT |
80 | .kind = "bond", |
81 | ||
c464b5d1 MT |
82 | // Configuration |
83 | .setup = nw_port_bonding_setup, | |
95c5dca2 | 84 | |
c464b5d1 MT |
85 | // Link |
86 | .create_link = nw_port_bonding_create_link, | |
e9b0614e | 87 | |
c464b5d1 MT |
88 | // JSON |
89 | .to_json = nw_port_bonding_to_json, | |
95c5dca2 MT |
90 | }; |
91 | ||
92 | const char* nw_port_bonding_get_mode(nw_port* port) { | |
93 | return nw_port_bonding_mode_to_string(port->bonding.mode); | |
94 | } | |
95 | ||
96 | int nw_port_bonding_set_mode(nw_port* port, const char* mode) { | |
97 | const int m = nw_port_bonding_mode_from_string(mode); | |
98 | ||
99 | switch (m) { | |
5f9c4392 MT |
100 | case NW_BONDING_MODE_ROUNDROBIN: |
101 | case NW_BONDING_MODE_ACTIVEBACKUP: | |
102 | case NW_BONDING_MODE_XOR: | |
103 | case NW_BONDING_MODE_BROADCAST: | |
104 | case NW_BONDING_MODE_8023AD: | |
105 | case NW_BONDING_MODE_TLB: | |
106 | case NW_BONDING_MODE_ALB: | |
95c5dca2 MT |
107 | port->bonding.mode = m; |
108 | break; | |
109 | ||
110 | default: | |
111 | ERROR("%s: Unsupported bonding mode '%s'\n", port->name, mode); | |
112 | errno = ENOTSUP; | |
113 | return 1; | |
114 | } | |
115 | ||
116 | return 0; | |
117 | } |