blob: bb16f0cc164448d81c8f17cd106199e69ae264f9 [file] [log] [blame]
Tomáš Pecka6936b912023-06-12 11:30:36 +02001From 00b203ad445b40faf5836a3ca9a6c1ec89165c23 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
Tomáš Pecka6936b912023-06-12 11:30:36 +02004Subject: [PATCH 2/7] network: Add varlink server
Tomáš Peckaae301762021-10-13 10:50:37 +02005
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 +++++++
Tomáš Pecka6936b912023-06-12 11:30:36 +020011 src/network/networkd-manager.h | 11 +++++++----
Tomáš Peckaae301762021-10-13 10:50:37 +020012 src/network/networkd-varlink.c | 34 ++++++++++++++++++++++++++++++++++
13 src/network/networkd-varlink.h | 7 +++++++
Tomáš Pecka6936b912023-06-12 11:30:36 +020014 5 files changed, 57 insertions(+), 4 deletions(-)
Tomáš Peckaae301762021-10-13 10:50:37 +020015 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áš Pecka6936b912023-06-12 11:30:36 +020019index b09c18dbbd..90ae4ed238 100644
Tomáš Peckaae301762021-10-13 10:50:37 +020020--- a/src/network/meson.build
21+++ b/src/network/meson.build
Tomáš Pecka6936b912023-06-12 11:30:36 +020022@@ -145,6 +145,8 @@ sources = files(
Tomáš Pecka5e070252022-05-20 20:41:21 +020023 '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',
Tomáš Pecka6936b912023-06-12 11:30:36 +020030 'networkd-wiphy.c',
Tomáš Peckaae301762021-10-13 10:50:37 +020031diff --git a/src/network/networkd-manager.c b/src/network/networkd-manager.c
Tomáš Pecka6936b912023-06-12 11:30:36 +020032index cdfd29bc0e..9f859408a1 100644
Tomáš Peckaae301762021-10-13 10:50:37 +020033--- a/src/network/networkd-manager.c
34+++ b/src/network/networkd-manager.c
Tomáš Pecka6936b912023-06-12 11:30:36 +020035@@ -44,6 +44,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áš Pecka6936b912023-06-12 11:30:36 +020041 #include "networkd-wiphy.h"
Tomáš Peckaae301762021-10-13 10:50:37 +020042 #include "ordered-set.h"
Tomáš Pecka6936b912023-06-12 11:30:36 +020043@@ -667,6 +668,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áš Pecka6936b912023-06-12 11:30:36 +020052@@ -676,6 +679,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
Tomáš Pecka6936b912023-06-12 11:30:36 +020064index 40e6092f85..dfdd44baf8 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;
Tomáš Pecka6936b912023-06-12 11:30:36 +020075@@ -62,8 +63,8 @@ struct Manager {
76 bool has_product_uuid;
77 bool product_uuid_requested;
Tomáš Peckaae301762021-10-13 10:50:37 +020078
Tomáš Pecka6936b912023-06-12 11:30:36 +020079- char* dynamic_hostname;
80- char* dynamic_timezone;
81+ char *dynamic_hostname;
82+ char *dynamic_timezone;
83
84 Set *rules;
85
86@@ -100,10 +101,12 @@ struct Manager {
Tomáš Peckaae301762021-10-13 10:50:37 +020087 OrderedSet *request_queue;
Tomáš Pecka6936b912023-06-12 11:30:36 +020088
89 Hashmap *tuntap_fds_by_name;
Tomáš Peckaae301762021-10-13 10:50:37 +020090+
91+ VarlinkServer *varlink_server;
92 };
93
Václav Kubernát8cd61562021-12-08 13:27:31 +010094 int manager_new(Manager **ret, bool test_mode);
Tomáš Pecka6936b912023-06-12 11:30:36 +020095-Manager* manager_free(Manager *m);
96+Manager *manager_free(Manager *m);
97
98 int manager_setup(Manager *m);
99 int manager_start(Manager *m);
100@@ -115,4 +118,4 @@ int manager_enumerate(Manager *m);
101 int manager_set_hostname(Manager *m, const char *hostname);
102 int manager_set_timezone(Manager *m, const char *timezone);
103
104-DEFINE_TRIVIAL_CLEANUP_FUNC(Manager*, manager_free);
105+DEFINE_TRIVIAL_CLEANUP_FUNC(Manager *, manager_free);
Tomáš Peckaae301762021-10-13 10:50:37 +0200106diff --git a/src/network/networkd-varlink.c b/src/network/networkd-varlink.c
107new file mode 100644
108index 0000000000..57d8acb967
109--- /dev/null
110+++ b/src/network/networkd-varlink.c
111@@ -0,0 +1,34 @@
112+/* SPDX-License-Identifier: LGPL-2.1-or-later */
113+
114+#include "networkd-varlink.h"
115+
116+int manager_varlink_init(Manager *m) {
117+ _cleanup_(varlink_server_unrefp) VarlinkServer *s = NULL;
118+ int r;
119+
120+ assert(m);
121+
122+ if (m->varlink_server)
123+ return 0;
124+
125+ r = varlink_server_new(&s, VARLINK_SERVER_ACCOUNT_UID);
126+ if (r < 0)
127+ return log_error_errno(r, "Failed to allocate varlink server object: %m");
128+
129+ r = varlink_server_listen_address(s, "/run/systemd/netif/io.systemd.Network", 0666);
130+ if (r < 0)
131+ return log_error_errno(r, "Failed to bind to varlink socket: %m");
132+
133+ r = varlink_server_attach_event(s, m->event, SD_EVENT_PRIORITY_NORMAL);
134+ if (r < 0)
135+ return log_error_errno(r, "Failed to attach varlink connection to event loop: %m");
136+
137+ m->varlink_server = TAKE_PTR(s);
138+ return 0;
139+}
140+
141+void manager_varlink_done(Manager *m) {
142+ assert(m);
143+
144+ m->varlink_server = varlink_server_unref(m->varlink_server);
145+}
146diff --git a/src/network/networkd-varlink.h b/src/network/networkd-varlink.h
147new file mode 100644
148index 0000000000..657e054a51
149--- /dev/null
150+++ b/src/network/networkd-varlink.h
151@@ -0,0 +1,7 @@
152+/* SPDX-License-Identifier: LGPL-2.1-or-later */
153+#pragma once
154+
155+#include "networkd-manager.h"
156+
157+int manager_varlink_init(Manager *m);
158+void manager_varlink_done(Manager *m);
159--
Tomáš Pecka6936b912023-06-12 11:30:36 +02001602.41.0
Tomáš Peckaae301762021-10-13 10:50:37 +0200161