chiark / gitweb /
9eb66ffb71e0416268ed08608b707f3b183e08ba
[elogind.git] / src / network / networkd-netdev-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-netdev-macvlan.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 static int netdev_macvlan_fill_message_create(NetDev *netdev, Link *link, sd_rtnl_message *req) {
40         int r;
41
42         assert(netdev);
43         assert(netdev->kind == NETDEV_KIND_MACVLAN);
44         assert(link);
45         assert(netdev->ifname);
46
47         r = sd_rtnl_message_append_u32(req, IFLA_LINK, link->ifindex);
48         if (r < 0) {
49                 log_error_netdev(netdev,
50                                  "Could not append IFLA_LINK attribute: %s",
51                                  strerror(-r));
52                 return r;
53         }
54
55         r = sd_rtnl_message_append_string(req, IFLA_IFNAME, netdev->ifname);
56         if (r < 0) {
57                 log_error_netdev(netdev,
58                                  "Could not append IFLA_IFNAME attribute: %s",
59                                  strerror(-r));
60                 return r;
61         }
62
63         if (netdev->mtu) {
64                 r = sd_rtnl_message_append_u32(req, IFLA_MTU, netdev->mtu);
65                 if (r < 0) {
66                         log_error_netdev(netdev,
67                                          "Could not append IFLA_MTU attribute: %s",
68                                          strerror(-r));
69                         return r;
70                 }
71         }
72
73         if (netdev->mac) {
74                 r = sd_rtnl_message_append_ether_addr(req, IFLA_ADDRESS, netdev->mac);
75                 if (r < 0) {
76                         log_error_netdev(netdev,
77                                          "Colud not append IFLA_ADDRESS attribute: %s",
78                                          strerror(-r));
79                     return r;
80                 }
81         }
82
83         r = sd_rtnl_message_open_container(req, IFLA_LINKINFO);
84         if (r < 0) {
85                 log_error_netdev(netdev,
86                                  "Could not open IFLA_LINKINFO container: %s",
87                                  strerror(-r));
88                 return r;
89         }
90
91         r = sd_rtnl_message_open_container_union(req, IFLA_INFO_DATA, "macvlan");
92         if (r < 0) {
93                 log_error_netdev(netdev,
94                                  "Could not open IFLA_INFO_DATA container: %s",
95                                   strerror(-r));
96                 return r;
97         }
98
99         if (netdev->macvlan_mode != _NETDEV_MACVLAN_MODE_INVALID) {
100         r = sd_rtnl_message_append_u32(req, IFLA_MACVLAN_MODE, netdev->macvlan_mode);
101         if (r < 0) {
102                 log_error_netdev(netdev,
103                                  "Could not append IFLA_MACVLAN_MODE attribute: %s",
104                                  strerror(-r));
105                         return r;
106                 }
107         }
108
109         r = sd_rtnl_message_close_container(req);
110         if (r < 0) {
111                 log_error_netdev(netdev,
112                                  "Could not close IFLA_INFO_DATA container %s",
113                                  strerror(-r));
114                 return r;
115         }
116
117         r = sd_rtnl_message_close_container(req);
118         if (r < 0) {
119                 log_error_netdev(netdev,
120                                  "Could not close IFLA_LINKINFO container %s",
121                                  strerror(-r));
122                 return r;
123         }
124
125         return 0;
126 }
127
128 const NetDevVTable macvlan_vtable = {
129         .fill_message_create_on_link = netdev_macvlan_fill_message_create,
130 };