]>
Commit | Line | Data |
---|---|---|
1 | /*** | |
2 | This file is part of systemd. | |
3 | ||
4 | Copyright 2014 Tom Gundersen <teg@jklm.no> | |
5 | Copyright 2014 Susant Sahani | |
6 | ||
7 | systemd is free software; you can redistribute it and/or modify it | |
8 | under the terms of the GNU Lesser General Public License as published by | |
9 | the Free Software Foundation; either version 2.1 of the License, or | |
10 | (at your option) any later version. | |
11 | ||
12 | systemd is distributed in the hope that it will be useful, but | |
13 | WITHOUT ANY WARRANTY; without even the implied warranty of | |
14 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU | |
15 | Lesser General Public License for more details. | |
16 | ||
17 | You should have received a copy of the GNU Lesser General Public License | |
18 | along with systemd; If not, see <http://www.gnu.org/licenses/>. | |
19 | ***/ | |
20 | ||
21 | #include <net/if.h> | |
22 | ||
23 | #include "missing.h" | |
24 | #include "netlink-util.h" | |
25 | #include "netdev/bridge.h" | |
26 | #include "networkd-manager.h" | |
27 | ||
28 | /* callback for brige netdev's parameter set */ | |
29 | static int netdev_bridge_set_handler(sd_netlink *rtnl, sd_netlink_message *m, void *userdata) { | |
30 | _cleanup_netdev_unref_ NetDev *netdev = userdata; | |
31 | int r; | |
32 | ||
33 | assert(netdev); | |
34 | assert(m); | |
35 | ||
36 | r = sd_netlink_message_get_errno(m); | |
37 | if (r < 0) { | |
38 | log_netdev_warning_errno(netdev, r, "Bridge parameters could not be set: %m"); | |
39 | return 1; | |
40 | } | |
41 | ||
42 | log_netdev_debug(netdev, "Bridge parameters set success"); | |
43 | ||
44 | return 1; | |
45 | } | |
46 | ||
47 | static int netdev_bridge_post_create(NetDev *netdev, Link *link, sd_netlink_message *m) { | |
48 | _cleanup_(sd_netlink_message_unrefp) sd_netlink_message *req = NULL; | |
49 | Bridge *b; | |
50 | int r; | |
51 | ||
52 | assert(netdev); | |
53 | ||
54 | b = BRIDGE(netdev); | |
55 | ||
56 | assert(b); | |
57 | ||
58 | r = sd_rtnl_message_new_link(netdev->manager->rtnl, &req, RTM_NEWLINK, netdev->ifindex); | |
59 | if (r < 0) | |
60 | return log_netdev_error_errno(netdev, r, "Could not allocate RTM_SETLINK message: %m"); | |
61 | ||
62 | r = sd_netlink_message_set_flags(req, NLM_F_REQUEST | NLM_F_ACK); | |
63 | if (r < 0) | |
64 | return log_link_error_errno(link, r, "Could not set netlink flags: %m"); | |
65 | ||
66 | r = sd_netlink_message_open_container(req, IFLA_LINKINFO); | |
67 | if (r < 0) | |
68 | return log_netdev_error_errno(netdev, r, "Could not append IFLA_PROTINFO attribute: %m"); | |
69 | ||
70 | r = sd_netlink_message_open_container_union(req, IFLA_INFO_DATA, netdev_kind_to_string(netdev->kind)); | |
71 | if (r < 0) | |
72 | return log_netdev_error_errno(netdev, r, "Could not append IFLA_INFO_DATA attribute: %m"); | |
73 | ||
74 | /* convert to jiffes */ | |
75 | if (b->forward_delay != USEC_INFINITY) { | |
76 | r = sd_netlink_message_append_u32(req, IFLA_BR_FORWARD_DELAY, usec_to_jiffies(b->forward_delay)); | |
77 | if (r < 0) | |
78 | return log_netdev_error_errno(netdev, r, "Could not append IFLA_BR_FORWARD_DELAY attribute: %m"); | |
79 | } | |
80 | ||
81 | if (b->hello_time > 0) { | |
82 | r = sd_netlink_message_append_u32(req, IFLA_BR_HELLO_TIME, usec_to_jiffies(b->hello_time)); | |
83 | if (r < 0) | |
84 | return log_netdev_error_errno(netdev, r, "Could not append IFLA_BR_HELLO_TIME attribute: %m"); | |
85 | } | |
86 | ||
87 | if (b->max_age > 0) { | |
88 | r = sd_netlink_message_append_u32(req, IFLA_BR_MAX_AGE, usec_to_jiffies(b->max_age)); | |
89 | if (r < 0) | |
90 | return log_netdev_error_errno(netdev, r, "Could not append IFLA_BR_MAX_AGE attribute: %m"); | |
91 | } | |
92 | ||
93 | if (b->ageing_time > 0) { | |
94 | r = sd_netlink_message_append_u32(req, IFLA_BR_AGEING_TIME, usec_to_jiffies(b->ageing_time)); | |
95 | if (r < 0) | |
96 | return log_netdev_error_errno(netdev, r, "Could not append IFLA_BR_AGEING_TIME attribute: %m"); | |
97 | } | |
98 | ||
99 | if (b->priority > 0) { | |
100 | r = sd_netlink_message_append_u16(req, IFLA_BR_PRIORITY, b->priority); | |
101 | if (r < 0) | |
102 | return log_netdev_error_errno(netdev, r, "Could not append IFLA_BR_PRIORITY attribute: %m"); | |
103 | } | |
104 | ||
105 | if (b->default_pvid > 0) { | |
106 | r = sd_netlink_message_append_u16(req, IFLA_BR_VLAN_DEFAULT_PVID, b->default_pvid); | |
107 | if (r < 0) | |
108 | return log_netdev_error_errno(netdev, r, "Could not append IFLA_BR_VLAN_DEFAULT_PVID attribute: %m"); | |
109 | } | |
110 | ||
111 | if (b->mcast_querier >= 0) { | |
112 | r = sd_netlink_message_append_u8(req, IFLA_BR_MCAST_QUERIER, b->mcast_querier); | |
113 | if (r < 0) | |
114 | return log_netdev_error_errno(netdev, r, "Could not append IFLA_BR_MCAST_QUERIER attribute: %m"); | |
115 | } | |
116 | ||
117 | if (b->mcast_snooping >= 0) { | |
118 | r = sd_netlink_message_append_u8(req, IFLA_BR_MCAST_SNOOPING, b->mcast_snooping); | |
119 | if (r < 0) | |
120 | return log_netdev_error_errno(netdev, r, "Could not append IFLA_BR_MCAST_SNOOPING attribute: %m"); | |
121 | } | |
122 | ||
123 | if (b->vlan_filtering >= 0) { | |
124 | r = sd_netlink_message_append_u8(req, IFLA_BR_VLAN_FILTERING, b->vlan_filtering); | |
125 | if (r < 0) | |
126 | return log_netdev_error_errno(netdev, r, "Could not append IFLA_BR_VLAN_FILTERING attribute: %m"); | |
127 | } | |
128 | ||
129 | if (b->stp >= 0) { | |
130 | r = sd_netlink_message_append_u32(req, IFLA_BR_STP_STATE, b->stp); | |
131 | if (r < 0) | |
132 | return log_netdev_error_errno(netdev, r, "Could not append IFLA_BR_STP_STATE attribute: %m"); | |
133 | } | |
134 | ||
135 | r = sd_netlink_message_close_container(req); | |
136 | if (r < 0) | |
137 | return log_netdev_error_errno(netdev, r, "Could not append IFLA_LINKINFO attribute: %m"); | |
138 | ||
139 | r = sd_netlink_message_close_container(req); | |
140 | if (r < 0) | |
141 | return log_netdev_error_errno(netdev, r, "Could not append IFLA_INFO_DATA attribute: %m"); | |
142 | ||
143 | r = sd_netlink_call_async(netdev->manager->rtnl, req, netdev_bridge_set_handler, netdev, 0, NULL); | |
144 | if (r < 0) | |
145 | return log_netdev_error_errno(netdev, r, "Could not send rtnetlink message: %m"); | |
146 | ||
147 | netdev_ref(netdev); | |
148 | ||
149 | return r; | |
150 | } | |
151 | ||
152 | static void bridge_init(NetDev *n) { | |
153 | Bridge *b; | |
154 | ||
155 | b = BRIDGE(n); | |
156 | ||
157 | assert(b); | |
158 | ||
159 | b->mcast_querier = -1; | |
160 | b->mcast_snooping = -1; | |
161 | b->vlan_filtering = -1; | |
162 | b->stp = -1; | |
163 | b->forward_delay = USEC_INFINITY; | |
164 | } | |
165 | ||
166 | const NetDevVTable bridge_vtable = { | |
167 | .object_size = sizeof(Bridge), | |
168 | .init = bridge_init, | |
169 | .sections = "Match\0NetDev\0Bridge\0", | |
170 | .post_create = netdev_bridge_post_create, | |
171 | .create_type = NETDEV_CREATE_MASTER, | |
172 | }; |