chiark / gitweb /
networkd: netdev - add missing refs
[elogind.git] / src / network / networkd-macvlan.c
1 /*-*- Mode: C; c-basic-offset: 8; indent-tabs-mode: nil -*-*/
2
3 /***
4   This file is part of systemd.
5
6   Copyright 2013 Tom Gundersen <teg@jklm.no>
7
8   systemd is free software; you can redistribute it and/or modify it
9   under the terms of the GNU Lesser General Public License as published by
10   the Free Software Foundation; either version 2.1 of the License, or
11   (at your option) any later version.
12
13   systemd is distributed in the hope that it will be useful, but
14   WITHOUT ANY WARRANTY; without even the implied warranty of
15   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16   Lesser General Public License for more details.
17
18   You should have received a copy of the GNU Lesser General Public License
19   along with systemd; If not, see <http://www.gnu.org/licenses/>.
20 ***/
21
22 #include <net/if.h>
23
24 #include "networkd.h"
25 #include "network-internal.h"
26 #include "conf-parser.h"
27 #include "list.h"
28
29 static const char* const macvlan_mode_table[_NETDEV_MACVLAN_MODE_MAX] = {
30         [NETDEV_MACVLAN_MODE_PRIVATE] = "private",
31         [NETDEV_MACVLAN_MODE_VEPA] = "vepa",
32         [NETDEV_MACVLAN_MODE_BRIDGE] = "bridge",
33         [NETDEV_MACVLAN_MODE_PASSTHRU] = "passthru",
34 };
35
36 DEFINE_STRING_TABLE_LOOKUP(macvlan_mode, MacVlanMode);
37 DEFINE_CONFIG_PARSE_ENUM(config_parse_macvlan_mode, macvlan_mode, MacVlanMode, "Failed to parse macvlan mode");
38
39 int netdev_create_macvlan(NetDev *netdev, Link *link, sd_rtnl_message_handler_t callback) {
40         _cleanup_rtnl_message_unref_ sd_rtnl_message *req = NULL;
41         const char *kind;
42         int r;
43
44         assert(netdev);
45         assert(netdev->kind == NETDEV_KIND_MACVLAN);
46         assert(link);
47         assert(callback);
48         assert(netdev->ifname);
49         assert(netdev->manager);
50         assert(netdev->manager->rtnl);
51
52         r = sd_rtnl_message_new_link(netdev->manager->rtnl, &req, RTM_NEWLINK, 0);
53         if (r < 0) {
54                 log_error_netdev(netdev,
55                                  "Could not allocate RTM_NEWLINK message: %s",
56                                  strerror(-r));
57                 return r;
58         }
59
60         if (link) {
61                 r = sd_rtnl_message_append_u32(req, IFLA_LINK, link->ifindex);
62                 if (r < 0) {
63                         log_error_netdev(netdev,
64                                          "Could not append IFLA_LINK attribute: %s",
65                                          strerror(-r));
66                         return r;
67                 }
68         }
69
70         r = sd_rtnl_message_append_string(req, IFLA_IFNAME, netdev->ifname);
71         if (r < 0) {
72                 log_error_netdev(netdev,
73                                  "Could not append IFLA_IFNAME attribute: %s",
74                                  strerror(-r));
75                 return r;
76         }
77
78         if (netdev->mtu) {
79                 r = sd_rtnl_message_append_u32(req, IFLA_MTU, netdev->mtu);
80                 if (r < 0) {
81                         log_error_netdev(netdev,
82                                          "Could not append IFLA_MTU attribute: %s",
83                                          strerror(-r));
84                         return r;
85                 }
86         }
87
88         if (netdev->mac) {
89                 r = sd_rtnl_message_append_ether_addr(req, IFLA_ADDRESS, netdev->mac);
90                 if (r < 0) {
91                         log_error_netdev(netdev,
92                                          "Colud not append IFLA_ADDRESS attribute: %s",
93                                          strerror(-r));
94                     return r;
95                 }
96         }
97
98         r = sd_rtnl_message_open_container(req, IFLA_LINKINFO);
99         if (r < 0) {
100                 log_error_netdev(netdev,
101                                  "Could not open IFLA_LINKINFO container: %s",
102                                  strerror(-r));
103                 return r;
104         }
105
106         kind = netdev_kind_to_string(netdev->kind);
107         if (!kind) {
108                 log_error_netdev(netdev, "Invalid kind");
109                 return -EINVAL;
110         }
111
112         r = sd_rtnl_message_open_container_union(req, IFLA_INFO_DATA, kind);
113         if (r < 0) {
114                 log_error_netdev(netdev,
115                                  "Could not open IFLA_INFO_DATA container: %s",
116                                   strerror(-r));
117                 return r;
118         }
119
120         if (netdev->macvlan_mode != _NETDEV_MACVLAN_MODE_INVALID) {
121         r = sd_rtnl_message_append_u32(req, IFLA_MACVLAN_MODE, netdev->macvlan_mode);
122         if (r < 0) {
123                 log_error_netdev(netdev,
124                                  "Could not append IFLA_MACVLAN_MODE attribute: %s",
125                                  strerror(-r));
126                         return r;
127                 }
128         }
129
130         r = sd_rtnl_message_close_container(req);
131         if (r < 0) {
132                 log_error_netdev(netdev,
133                                  "Could not close IFLA_INFO_DATA container %s",
134                                  strerror(-r));
135                 return r;
136         }
137
138         r = sd_rtnl_message_close_container(req);
139         if (r < 0) {
140                 log_error_netdev(netdev,
141                                  "Could not close IFLA_LINKINFO container %s",
142                                  strerror(-r));
143                 return r;
144         }
145
146         r = sd_rtnl_call_async(netdev->manager->rtnl, req, callback, link, 0, NULL);
147         if (r < 0) {
148                 log_error_netdev(netdev,
149                                  "Could not send rtnetlink message: %s", strerror(-r));
150                 return r;
151         }
152
153         link_ref(link);
154
155         log_debug_netdev(netdev, "creating netdev");
156
157         netdev->state = NETDEV_STATE_CREATING;
158
159         return 0;
160 }