chiark / gitweb /
networkd: add support for Uplink Failure Detection
[elogind.git] / src / network / networkd-netdev-veth.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 Susant Sahani <susant@redhat.com>
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 #include <linux/veth.h>
24
25 #include "sd-rtnl.h"
26 #include "networkd-netdev-veth.h"
27
28 static int netdev_veth_fill_message_create(NetDev *netdev, Link *link, sd_rtnl_message *m) {
29         Veth *v = VETH(netdev);
30         int r;
31
32         assert(netdev);
33         assert(!link);
34         assert(v);
35         assert(m);
36
37         r = sd_rtnl_message_open_container(m, VETH_INFO_PEER);
38         if (r < 0) {
39                 log_netdev_error(netdev,
40                                  "Could not append VETH_INFO_PEER attribute: %s",
41                                  strerror(-r));
42                 return r;
43         }
44
45         if (v->ifname_peer) {
46                 r = sd_rtnl_message_append_string(m, IFLA_IFNAME, v->ifname_peer);
47                 if (r < 0)
48                         return log_error_errno(r, "Failed to add netlink interface name: %m");
49         }
50
51         if (v->mac_peer) {
52                 r = sd_rtnl_message_append_ether_addr(m, IFLA_ADDRESS, v->mac_peer);
53                 if (r < 0) {
54                         log_netdev_error(netdev,
55                                          "Could not append IFLA_ADDRESS attribute: %s",
56                                          strerror(-r));
57                     return r;
58                 }
59         }
60
61         r = sd_rtnl_message_close_container(m);
62         if (r < 0) {
63                 log_netdev_error(netdev,
64                                  "Could not append IFLA_INFO_DATA attribute: %s",
65                                  strerror(-r));
66                 return r;
67         }
68
69         return r;
70 }
71
72 static int netdev_veth_verify(NetDev *netdev, const char *filename) {
73         Veth *v = VETH(netdev);
74         int r;
75
76         assert(netdev);
77         assert(v);
78         assert(filename);
79
80         if (!v->ifname_peer) {
81                 log_warning("Veth NetDev without peer name configured in %s. Ignoring",
82                             filename);
83                 return -EINVAL;
84         }
85
86         if (!v->mac_peer) {
87                 r = netdev_get_mac(v->ifname_peer, &v->mac_peer);
88                 if (r < 0) {
89                         log_warning("Failed to generate predictable MAC address for %s. Ignoring",
90                                   v->ifname_peer);
91                         return -EINVAL;
92                 }
93         }
94
95         return 0;
96 }
97
98 static void veth_done(NetDev *n) {
99         Veth *v = VETH(n);
100
101         assert(n);
102         assert(v);
103
104         free(v->ifname_peer);
105         free(v->mac_peer);
106 }
107
108 const NetDevVTable veth_vtable = {
109         .object_size = sizeof(Veth),
110         .sections = "Match\0NetDev\0Peer\0",
111         .done = veth_done,
112         .fill_message_create = netdev_veth_fill_message_create,
113         .create_type = NETDEV_CREATE_INDEPENDENT,
114         .config_verify = netdev_veth_verify,
115 };