blob: 2567d7fd214e99644480a93787494e1916df1953 [file] [log] [blame]
Tomáš Peckaae301762021-10-13 10:50:37 +02001From 36f46b1f60453bdbb8841bd86724ff868149594b Mon Sep 17 00:00:00 2001
2From: Tomas Pecka <peckato1@users.noreply.github.com>
3Date: Tue, 24 Aug 2021 14:59:40 +0200
4Subject: [PATCH 2/9] network: Add varlink server
5
6Add a varlink server. In the next commits, we will implement a method
7that queries the LLDP neighbors.
8---
9 src/network/meson.build | 2 ++
10 src/network/networkd-manager.c | 7 +++++++
11 src/network/networkd-manager.h | 3 +++
12 src/network/networkd-varlink.c | 34 ++++++++++++++++++++++++++++++++++
13 src/network/networkd-varlink.h | 7 +++++++
14 5 files changed, 53 insertions(+)
15 create mode 100644 src/network/networkd-varlink.c
16 create mode 100644 src/network/networkd-varlink.h
17
18diff --git a/src/network/meson.build b/src/network/meson.build
19index 4e137d7b9e..2c97da60d3 100644
20--- a/src/network/meson.build
21+++ b/src/network/meson.build
22@@ -129,6 +129,8 @@ sources = files('''
23 networkd-sysctl.h
24 networkd-util.c
25 networkd-util.h
26+ networkd-varlink.c
27+ networkd-varlink.h
28 networkd-wifi.c
29 networkd-wifi.h
30 tc/cake.c
31diff --git a/src/network/networkd-manager.c b/src/network/networkd-manager.c
32index 374d27bef3..5e53498711 100644
33--- a/src/network/networkd-manager.c
34+++ b/src/network/networkd-manager.c
35@@ -38,6 +38,7 @@
36 #include "networkd-routing-policy-rule.h"
37 #include "networkd-speed-meter.h"
38 #include "networkd-state-file.h"
39+#include "networkd-varlink.h"
40 #include "ordered-set.h"
41 #include "path-lookup.h"
42 #include "path-util.h"
43@@ -509,6 +510,8 @@ Manager* manager_free(Manager *m) {
44
45 m->fw_ctx = fw_ctx_free(m->fw_ctx);
46
47+ manager_varlink_done(m);
48+
49 return mfree(m);
50 }
51
52@@ -518,6 +521,10 @@ int manager_start(Manager *m) {
53
54 assert(m);
55
56+ r = manager_varlink_init(m);
57+ if (r < 0)
58+ return r;
59+
60 r = manager_start_speed_meter(m);
61 if (r < 0)
62 return log_error_errno(r, "Failed to initialize speed meter: %m");
63diff --git a/src/network/networkd-manager.h b/src/network/networkd-manager.h
64index 4ee48f3468..f8d4d00db2 100644
65--- a/src/network/networkd-manager.h
66+++ b/src/network/networkd-manager.h
67@@ -16,6 +16,7 @@
68 #include "ordered-set.h"
69 #include "set.h"
70 #include "time-util.h"
71+#include "varlink.h"
72
73 struct Manager {
74 sd_netlink *rtnl;
75@@ -99,6 +100,8 @@ struct Manager {
76 FirewallContext *fw_ctx;
77
78 OrderedSet *request_queue;
79+
80+ VarlinkServer *varlink_server;
81 };
82
83 int manager_new(Manager **ret);
84diff --git a/src/network/networkd-varlink.c b/src/network/networkd-varlink.c
85new file mode 100644
86index 0000000000..57d8acb967
87--- /dev/null
88+++ b/src/network/networkd-varlink.c
89@@ -0,0 +1,34 @@
90+/* SPDX-License-Identifier: LGPL-2.1-or-later */
91+
92+#include "networkd-varlink.h"
93+
94+int manager_varlink_init(Manager *m) {
95+ _cleanup_(varlink_server_unrefp) VarlinkServer *s = NULL;
96+ int r;
97+
98+ assert(m);
99+
100+ if (m->varlink_server)
101+ return 0;
102+
103+ r = varlink_server_new(&s, VARLINK_SERVER_ACCOUNT_UID);
104+ if (r < 0)
105+ return log_error_errno(r, "Failed to allocate varlink server object: %m");
106+
107+ r = varlink_server_listen_address(s, "/run/systemd/netif/io.systemd.Network", 0666);
108+ if (r < 0)
109+ return log_error_errno(r, "Failed to bind to varlink socket: %m");
110+
111+ r = varlink_server_attach_event(s, m->event, SD_EVENT_PRIORITY_NORMAL);
112+ if (r < 0)
113+ return log_error_errno(r, "Failed to attach varlink connection to event loop: %m");
114+
115+ m->varlink_server = TAKE_PTR(s);
116+ return 0;
117+}
118+
119+void manager_varlink_done(Manager *m) {
120+ assert(m);
121+
122+ m->varlink_server = varlink_server_unref(m->varlink_server);
123+}
124diff --git a/src/network/networkd-varlink.h b/src/network/networkd-varlink.h
125new file mode 100644
126index 0000000000..657e054a51
127--- /dev/null
128+++ b/src/network/networkd-varlink.h
129@@ -0,0 +1,7 @@
130+/* SPDX-License-Identifier: LGPL-2.1-or-later */
131+#pragma once
132+
133+#include "networkd-manager.h"
134+
135+int manager_varlink_init(Manager *m);
136+void manager_varlink_done(Manager *m);
137--
1382.33.0
139