blob: 6302127c4e309986af11a6baf357e4144c35d5b5 [file] [log] [blame]
Tomáš Pecka5e070252022-05-20 20:41:21 +02001From 210ae903e7f5e4813feaea9bbac1f03fa3be42cc Mon Sep 17 00:00:00 2001
Tomáš Peckaae301762021-10-13 10:50:37 +02002From: 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
Tomáš Pecka5e070252022-05-20 20:41:21 +020019index 48d185195c..d93e815fdf 100644
Tomáš Peckaae301762021-10-13 10:50:37 +020020--- a/src/network/meson.build
21+++ b/src/network/meson.build
Tomáš Pecka5e070252022-05-20 20:41:21 +020022@@ -137,6 +137,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',
Tomáš Peckaae301762021-10-13 10:50:37 +020031diff --git a/src/network/networkd-manager.c b/src/network/networkd-manager.c
Tomáš Pecka5e070252022-05-20 20:41:21 +020032index 9d790224cc..5e4c5e38b4 100644
Tomáš Peckaae301762021-10-13 10:50:37 +020033--- a/src/network/networkd-manager.c
34+++ b/src/network/networkd-manager.c
Václav Kubernát8cd61562021-12-08 13:27:31 +010035@@ -41,6 +41,7 @@
Tomáš Peckaae301762021-10-13 10:50:37 +020036 #include "networkd-routing-policy-rule.h"
37 #include "networkd-speed-meter.h"
38 #include "networkd-state-file.h"
39+#include "networkd-varlink.h"
Václav Kubernát8cd61562021-12-08 13:27:31 +010040 #include "networkd-wifi.h"
Tomáš Peckaae301762021-10-13 10:50:37 +020041 #include "ordered-set.h"
42 #include "path-lookup.h"
Tomáš Pecka5e070252022-05-20 20:41:21 +020043@@ -544,6 +545,8 @@ Manager* manager_free(Manager *m) {
Tomáš Peckaae301762021-10-13 10:50:37 +020044
45 m->fw_ctx = fw_ctx_free(m->fw_ctx);
46
47+ manager_varlink_done(m);
48+
49 return mfree(m);
50 }
51
Tomáš Pecka5e070252022-05-20 20:41:21 +020052@@ -553,6 +556,10 @@ int manager_start(Manager *m) {
Tomáš Peckaae301762021-10-13 10:50:37 +020053
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
Václav Kubernát8cd61562021-12-08 13:27:31 +010064index 36313589a3..a0c0336ce5 100644
Tomáš Peckaae301762021-10-13 10:50:37 +020065--- 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;
Václav Kubernát8cd61562021-12-08 13:27:31 +010075@@ -95,6 +96,8 @@ struct Manager {
Tomáš Peckaae301762021-10-13 10:50:37 +020076 FirewallContext *fw_ctx;
77
78 OrderedSet *request_queue;
79+
80+ VarlinkServer *varlink_server;
81 };
82
Václav Kubernát8cd61562021-12-08 13:27:31 +010083 int manager_new(Manager **ret, bool test_mode);
Tomáš Peckaae301762021-10-13 10:50:37 +020084diff --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--
Tomáš Pecka5e070252022-05-20 20:41:21 +02001382.35.1
Tomáš Peckaae301762021-10-13 10:50:37 +0200139