chiark / gitweb /
networkd: netdev - introduce vtable for netdev kinds
[elogind.git] / src / network / networkd-netdev-bond.c
1 /*-*- Mode: C; c-basic-offset: 8; indent-tabs-mode: nil -*-*/
2
3 /***
4     This file is part of systemd.
5
6     Copyright 2014  Tom Gundersen <teg@jklm.no>
7     Copyright 2014  Susant Sahani
8
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.
13
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.
18
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/>.
21 ***/
22
23 #include <netinet/ether.h>
24 #include <arpa/inet.h>
25 #include <linux/if_bonding.h>
26
27 #include "conf-parser.h"
28 #include "sd-rtnl.h"
29 #include "networkd-netdev-bond.h"
30 #include "missing.h"
31
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",
40 };
41
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");
44
45 static uint8_t bond_mode_to_kernel(BondMode mode) {
46         switch (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:
52                 return BOND_MODE_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:
58                 return BOND_MODE_TLB;
59         case NETDEV_BOND_MODE_BALANCE_ALB:
60                 return BOND_MODE_ALB;
61         default:
62                 return (uint8_t) -1;
63         }
64 }
65
66 static int netdev_bond_fill_message_create(NetDev *netdev, sd_rtnl_message *m) {
67         int r;
68
69         assert(m);
70
71         r = sd_rtnl_message_append_string(m, IFLA_IFNAME, netdev->ifname);
72         if (r < 0) {
73                 log_error_netdev(netdev,
74                                  "Could not append IFLA_IFNAME, attribute: %s",
75                                  strerror(-r));
76                 return r;
77         }
78
79         r = sd_rtnl_message_open_container(m, IFLA_LINKINFO);
80         if (r < 0) {
81                 log_error_netdev(netdev,
82                                  "Could not append IFLA_LINKINFO attribute: %s",
83                                  strerror(-r));
84                 return r;
85         }
86
87         r = sd_rtnl_message_open_container_union(m, IFLA_INFO_DATA,
88                                                  netdev_kind_to_string(netdev->kind));
89         if (r < 0) {
90                 log_error_netdev(netdev,
91                                  "Could not append IFLA_INFO_DATA attribute: %s",
92                                  strerror(-r));
93                 return r;
94         }
95
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));
99                 if (r < 0) {
100                         log_error_netdev(netdev,
101                                          "Could not append IFLA_BOND_MODE attribute: %s",
102                                          strerror(-r));
103                         return r;
104                 }
105         }
106
107         r = sd_rtnl_message_close_container(m);
108         if (r < 0) {
109                 log_error_netdev(netdev,
110                                  "Could not append IFLA_LINKINFO attribute: %s",
111                                  strerror(-r));
112                 return r;
113         }
114
115         r = sd_rtnl_message_close_container(m);
116         if (r < 0) {
117                 log_error_netdev(netdev,
118                                  "Could not append IFLA_LINKINFO attribute: %s",
119                                  strerror(-r));
120                 return r;
121         }
122
123         return r;
124 }
125
126 const NetDevVTable bond_vtable = {
127         .fill_message_create = netdev_bond_fill_message_create,
128         .enslave = netdev_enslave,
129 };