1 /*-*- Mode: C; c-basic-offset: 8; indent-tabs-mode: nil -*-*/
4 This file is part of systemd.
6 Copyright 2014 Tom Gundersen <teg@jklm.no>
7 Copyright 2014 Susant Sahani
9 systemd is free software; you can redistribute it and/or modify it
10 under the terms of the GNU Lesser General Public License as published by
11 the Free Software Foundation; either version 2.1 of the License, or
12 (at your option) any later version.
14 systemd is distributed in the hope that it will be useful, but
15 WITHOUT ANY WARRANTY; without even the implied warranty of
16 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
17 Lesser General Public License for more details.
19 You should have received a copy of the GNU Lesser General Public License
20 along with systemd; If not, see <http://www.gnu.org/licenses/>.
23 #include <netinet/ether.h>
24 #include <arpa/inet.h>
25 #include <linux/if_bonding.h>
27 #include "conf-parser.h"
32 static const char* const bond_mode_table[_NETDEV_BOND_MODE_MAX] = {
33 [NETDEV_BOND_MODE_BALANCE_RR] = "balance-rr",
34 [NETDEV_BOND_MODE_ACTIVE_BACKUP] = "active-backup",
35 [NETDEV_BOND_MODE_BALANCE_XOR] = "balance-xor",
36 [NETDEV_BOND_MODE_BROADCAST] = "broadcast",
37 [NETDEV_BOND_MODE_802_3AD] = "802.3ad",
38 [NETDEV_BOND_MODE_BALANCE_TLB] = "balance-tlb",
39 [NETDEV_BOND_MODE_BALANCE_ALB] = "balance-alb",
42 DEFINE_STRING_TABLE_LOOKUP(bond_mode, BondMode);
43 DEFINE_CONFIG_PARSE_ENUM(config_parse_bond_mode, bond_mode, BondMode, "Failed to parse bond mode");
45 static uint8_t bond_mode_to_kernel(BondMode mode) {
47 case NETDEV_BOND_MODE_BALANCE_RR:
48 return BOND_MODE_ROUNDROBIN;
49 case NETDEV_BOND_MODE_ACTIVE_BACKUP:
50 return BOND_MODE_ACTIVEBACKUP;
51 case NETDEV_BOND_MODE_BALANCE_XOR:
53 case NETDEV_BOND_MODE_BROADCAST:
54 return BOND_MODE_BROADCAST;
55 case NETDEV_BOND_MODE_802_3AD:
56 return BOND_MODE_8023AD;
57 case NETDEV_BOND_MODE_BALANCE_TLB:
59 case NETDEV_BOND_MODE_BALANCE_ALB:
66 static int netdev_fill_bond_rtnl_message(NetDev *netdev, sd_rtnl_message *m) {
71 r = sd_rtnl_message_append_string(m, IFLA_IFNAME, netdev->ifname);
73 log_error_netdev(netdev,
74 "Could not append IFLA_IFNAME, attribute: %s",
79 r = sd_rtnl_message_open_container(m, IFLA_LINKINFO);
81 log_error_netdev(netdev,
82 "Could not append IFLA_LINKINFO attribute: %s",
87 r = sd_rtnl_message_open_container_union(m, IFLA_INFO_DATA,
88 netdev_kind_to_string(netdev->kind));
90 log_error_netdev(netdev,
91 "Could not append IFLA_INFO_DATA attribute: %s",
96 if (netdev->bond_mode != _NETDEV_BOND_MODE_INVALID) {
97 r = sd_rtnl_message_append_u8(m, IFLA_BOND_MODE,
98 bond_mode_to_kernel(netdev->bond_mode));
100 log_error_netdev(netdev,
101 "Could not append IFLA_BOND_MODE attribute: %s",
107 r = sd_rtnl_message_close_container(m);
109 log_error_netdev(netdev,
110 "Could not append IFLA_LINKINFO attribute: %s",
115 r = sd_rtnl_message_close_container(m);
117 log_error_netdev(netdev,
118 "Could not append IFLA_LINKINFO attribute: %s",
126 int netdev_create_bond(NetDev *netdev, sd_rtnl_message_handler_t callback) {
127 _cleanup_rtnl_message_unref_ sd_rtnl_message *m = NULL;
131 assert(netdev->kind == NETDEV_KIND_BOND);
132 assert(netdev->ifname);
133 assert(netdev->manager);
134 assert(netdev->manager->rtnl);
136 r = sd_rtnl_message_new_link(netdev->manager->rtnl, &m, RTM_NEWLINK, 0);
138 log_error_netdev(netdev,
139 "Could not allocate RTM_NEWLINK message: %s",
144 r = netdev_fill_bond_rtnl_message(netdev, m);
148 r = sd_rtnl_call_async(netdev->manager->rtnl, m, callback, netdev, 0, NULL);
150 log_error_netdev(netdev,
151 "Could not send rtnetlink message: %s", strerror(-r));
157 log_debug_netdev(netdev, "Creating bond netdev: %s",
158 netdev_kind_to_string(netdev->kind));
160 netdev->state = NETDEV_STATE_CREATING;