]> git.ipfire.org Git - thirdparty/systemd.git/blame - src/network/networkd-wait-online-link.c
networkd-wait-online: track links
[thirdparty/systemd.git] / src / network / networkd-wait-online-link.c
CommitLineData
7de12ae7
TG
1/*-*- Mode: C; c-basic-offset: 8; indent-tabs-mode: nil -*-*/
2
3/***
4 This file is part of systemd.
5
6 Copyright 2014 Lennart Poettering
7 Copyright 2014 Tom Gundersen
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 <net/if.h>
24
25#include "sd-network.h"
26#include "strv.h"
27
28#include "networkd-wait-online-link.h"
29
30int link_new(Manager *m, Link **ret, int ifindex, const char *ifname) {
31 _cleanup_(link_freep) Link *l = NULL;
32 int r;
33
34 assert(m);
35 assert(ifindex > 0);
36
37 r = hashmap_ensure_allocated(&m->links, NULL, NULL);
38 if (r < 0)
39 return r;
40
41 r = hashmap_ensure_allocated(&m->links_by_name,
42 string_hash_func, string_compare_func);
43 if (r < 0)
44 return r;
45
46 l = new0(Link, 1);
47 if (!l)
48 return -ENOMEM;
49
50 l->manager = m;
51
52 l->ifname = strdup(ifname);
53 if (!l->ifname)
54 return -ENOMEM;
55
56 r = hashmap_put(m->links_by_name, l->ifname, l);
57 if (r < 0)
58 return r;
59
60 l->ifindex = ifindex;
61
62 r = hashmap_put(m->links, INT_TO_PTR(ifindex), l);
63 if (r < 0)
64 return r;
65
66 if (ret)
67 *ret = l;
68 l = NULL;
69
70 return 0;
71}
72
73Link *link_free(Link *l) {
74
75 if (!l)
76 return NULL;
77
78 if (l->manager) {
79 hashmap_remove(l->manager->links, INT_TO_PTR(l->ifindex));
80 hashmap_remove(l->manager->links_by_name, l->ifname);
81 }
82
83 free(l->ifname);
84 free(l);
85 return NULL;
86 }
87
88int link_update_rtnl(Link *l, sd_rtnl_message *m) {
89 char *ifname;
90 int r;
91
92 assert(l);
93 assert(l->manager);
94 assert(m);
95
96 r = sd_rtnl_message_link_get_flags(m, &l->flags);
97 if (r < 0)
98 return r;
99
100 r = sd_rtnl_message_read_string(m, IFLA_IFNAME, &ifname);
101 if (r < 0)
102 return r;
103
104 if (!streq(l->ifname, ifname)) {
105 char *new_ifname;
106
107 new_ifname = strdup(ifname);
108 if (!new_ifname)
109 return -ENOMEM;
110
111 hashmap_remove(l->manager->links_by_name, l->ifname);
112 free(l->ifname);
113 l->ifname = new_ifname;
114
115 r = hashmap_put(l->manager->links_by_name, l->ifname, l);
116 if (r < 0)
117 return r;
118 }
119
120 return 0;
121}
122
123int link_update_monitor(Link *l) {
124 assert(l);
125
126 free(l->operational_state);
127 l->operational_state = NULL;
128
129 sd_network_get_link_operational_state(l->ifindex, &l->operational_state);
130
131 free(l->state);
132 l->state = NULL;
133
134 sd_network_get_link_state(l->ifindex, &l->state);
135
136 return 0;
137}
138
139bool link_relevant(Link *l) {
140 assert(l);
141
142 /* A link is relevant if it isn't a loopback device and has at
143 * least one relevant IP address */
144
145 if (l->flags & IFF_LOOPBACK)
146 return false;
147/*
148 if (l->operational_state && !STR_IN_SET(l->operational_state, "unknown", "degraded", "routable"))
149 return false;
150
151 LIST_FOREACH(addresses, a, l->addresses)
152 if (link_address_relevant(a))
153 return true;
154*/
155 return true;
156}