Skip to contentSkip to conversation

Initial refactor for hwsim frame processing

RFC In review James Prestwood 2 participants 10 messages Jun 30, 2023 – Jul 10, 2023 Kbuild

lore
Share & tools
lore
#0 lore raw

cover Initial refactor for hwsim frame processing

I wanted to put this out there just as an idea before I get too invested in extending it. The code itself needs cleanup (explained later) but I wanted some feedback on the general idea.

Eventually I'd like to extend this to support remote communication with multiple hwsim instances, e.g. via a multicast socket. As a starting point the rules code fit nicely as a frame processing module.

Cleanup:
- Find solution for the IWD_MODULE_DEPENDS issue
- Add getters for radio/interface info objects, possibly for hwsim_frame too rather than everything exposed in hwsim.h.
- The hwsim_frame cleanup in rules needs to be changed. Prior hwsim_frame_unref() would send tx_info but now this needs to be handled in rules.c. Currently we peek into the refcount and see if it will be destroyed on unref, and send tx_info then.

The dbus dependency patches by themselves are actually useful so feel free to merge those if they look ok. If you don't like the preprocessor define thats fine, its a minimal amount of code duplicated between iwd/hwsim so its not _that_ bad as it stands today.

Note: These were done on top of the ADD/DEL mac changes so if they don't apply that is likely the issue. Again, just looking for a yay or nay on this concept.

James Prestwood (5):
dbus: remove dependency on agent
dbus: remove iwd.h dependency
hwsim: use dbus.c module
hwsim: refactor rules processing into separate file
module: temporary hack to allow for no module dependencies

 Makefile.am   |    9 +-
 src/agent.c   |   12 +-
 src/agent.h   |    2 -
 src/dbus.c    |   43 +-
 src/dbus.h    |    4 +
 src/module.c  |    6 +
 tools/hwsim.c | 1115 +++----------------------------------------------
 tools/hwsim.h |   99 +++++
 tools/rules.c | 1017 ++++++++++++++++++++++++++++++++++++++++++++
 9 files changed, 1224 insertions(+), 1083 deletions(-)
 create mode 100644 tools/hwsim.h
 create mode 100644 tools/rules.c
Signature · 1 line
2.25.1
#1 lore raw

RFC 1/5 dbus: remove dependency on agent

The dbus module is general purpose enough that it can be reworked to allow apps such as hwsim to use it. First, remove the agent dependency and instead use a setter API to register the shutdown callback.

---
 src/agent.c | 12 +++++++-----
 src/agent.h |  2 --
 src/dbus.c  | 10 ++++++++--
 src/dbus.h  |  4 ++++
 4 files changed, 19 insertions(+), 9 deletions(-)
src/agent.c +7 -5
index 0f718b87..eff0df3f 100644
@@ -678,6 +678,11 @@ static bool release_agent(void *data, void *user_data)
 	return true;
 }
 
+static void agent_shutdown(void)
+{
+	l_queue_foreach_remove(agents, release_agent, NULL);
+}
+
 static int agent_init(void)
 {
 	struct l_dbus *dbus = dbus_get_bus();
@@ -701,6 +706,8 @@ static int agent_init(void)
 		return -EIO;
 	}
 
+	__dbus_set_agent_shutdown_func(agent_shutdown);
+
 	return 0;
 }
 
@@ -714,9 +721,4 @@ static void agent_exit(void)
 	agents = NULL;
 }
 
-void agent_shutdown(void)
-{
-	l_queue_foreach_remove(agents, release_agent, NULL);
-}
-
 IWD_MODULE(agent, agent_init, agent_exit);
src/agent.h +0 -2
index eb08c6b6..cd9f75d7 100644
@@ -37,8 +37,6 @@ typedef void (*agent_request_user_name_passwd_func_t) (enum agent_result result,
 					void *user_data);
 typedef void (*agent_request_destroy_func_t)(void *user_data);
 
-void agent_shutdown(void);
-
 unsigned int agent_request_passphrase(const char *path,
 				agent_request_passphrase_func_t callback,
 				struct l_dbus_message *message,
src/dbus.c +8 -2
index 32de1e1a..1e0021f2 100644
@@ -30,11 +30,11 @@
 
 #include <ell/ell.h>
 
-#include "src/agent.h"
 #include "src/iwd.h"
 #include "src/dbus.h"
 
 static struct l_dbus *g_dbus = NULL;
+static dbus_agent_shutdown_func_t agent_shutdown;
 
 struct l_dbus_message *dbus_error_busy(struct l_dbus_message *msg)
 {
@@ -220,8 +220,14 @@ void dbus_exit(void)
 	g_dbus = NULL;
 }
 
+void __dbus_set_agent_shutdown_func(dbus_agent_shutdown_func_t agent_shutdown)
+{
+	agent_shutdown = agent_shutdown;
+}
+
 void dbus_shutdown(void)
 {
 	/* Allow AgentManager to send a Release call before disconnecting */
-	agent_shutdown();
+	if (agent_shutdown)
+		agent_shutdown();
 }
src/dbus.h +4 -0
index 00c2df57..a5b667a7 100644
@@ -51,10 +51,14 @@
 #define IWD_AGENT_MANAGER_PATH IWD_BASE_PATH
 #define IWD_P2P_SERVICE_MANAGER_PATH IWD_BASE_PATH
 
+typedef void (*dbus_agent_shutdown_func_t)(void);
+
 struct l_dbus;
 
 struct l_dbus *dbus_get_bus(void);
 
+void __dbus_set_agent_shutdown_func(dbus_agent_shutdown_func_t agent_shutdown);
+
 void dbus_pending_reply(struct l_dbus_message **msg,
 				struct l_dbus_message *reply);
 bool dbus_append_dict_basic(struct l_dbus_message_builder *builder,
Signature · 1 line
2.25.1
#2 lore raw

RFC 2/5 dbus: remove iwd.h dependency

In reply to James Prestwood

Require a compile time flag to set the service

---
 Makefile.am |  1 +
 src/dbus.c  | 33 ++++++++++++++++-----------------
 2 files changed, 17 insertions(+), 17 deletions(-)
Makefile.am +1 -0
index 401d2ac5..2fb03aa9 100644
@@ -268,6 +268,7 @@ src_iwd_SOURCES = src/main.c linux/nl80211.h src/iwd.h src/missing.h \
 
 src_iwd_LDADD = $(ell_ldadd) -ldl
 src_iwd_DEPENDENCIES = $(ell_dependencies)
+src_iwd_CFLAGS = -DDBUS_SERVICE=\"net.connman.iwd\"
 
 if OFONO
 builtin_modules += ofono
src/dbus.c +16 -17
index 1e0021f2..4ffa4db1 100644
@@ -30,7 +30,6 @@
 
 #include <ell/ell.h>
 
-#include "src/iwd.h"
 #include "src/dbus.h"
 
 static struct l_dbus *g_dbus = NULL;
@@ -38,99 +37,99 @@ static dbus_agent_shutdown_func_t agent_shutdown;
 
 struct l_dbus_message *dbus_error_busy(struct l_dbus_message *msg)
 {
-	return l_dbus_message_new_error(msg, IWD_SERVICE ".InProgress",
+	return l_dbus_message_new_error(msg, DBUS_SERVICE ".InProgress",
 					"Operation already in progress");
 }
 
 struct l_dbus_message *dbus_error_failed(struct l_dbus_message *msg)
 {
-	return l_dbus_message_new_error(msg, IWD_SERVICE ".Failed",
+	return l_dbus_message_new_error(msg, DBUS_SERVICE ".Failed",
 					"Operation failed");
 }
 
 struct l_dbus_message *dbus_error_aborted(struct l_dbus_message *msg)
 {
-	return l_dbus_message_new_error(msg, IWD_SERVICE ".Aborted",
+	return l_dbus_message_new_error(msg, DBUS_SERVICE ".Aborted",
 					"Operation aborted");
 }
 
 struct l_dbus_message *dbus_error_not_available(struct l_dbus_message *msg)
 {
-	return l_dbus_message_new_error(msg, IWD_SERVICE ".NotAvailable",
+	return l_dbus_message_new_error(msg, DBUS_SERVICE ".NotAvailable",
 					"Operation not available");
 }
 
 struct l_dbus_message *dbus_error_invalid_args(struct l_dbus_message *msg)
 {
-	return l_dbus_message_new_error(msg, IWD_SERVICE ".InvalidArguments",
+	return l_dbus_message_new_error(msg, DBUS_SERVICE ".InvalidArguments",
 					"Argument type is wrong");
 }
 
 struct l_dbus_message *dbus_error_invalid_format(struct l_dbus_message *msg)
 {
-	return l_dbus_message_new_error(msg, IWD_SERVICE ".InvalidFormat",
+	return l_dbus_message_new_error(msg, DBUS_SERVICE ".InvalidFormat",
 					"Argument format is invalid");
 }
 
 struct l_dbus_message *dbus_error_already_exists(struct l_dbus_message *msg)
 {
-	return l_dbus_message_new_error(msg, IWD_SERVICE ".AlreadyExists",
+	return l_dbus_message_new_error(msg, DBUS_SERVICE ".AlreadyExists",
 					"Object already exists");
 }
 
 struct l_dbus_message *dbus_error_not_found(struct l_dbus_message *msg)
 {
-	return l_dbus_message_new_error(msg, IWD_SERVICE ".NotFound",
+	return l_dbus_message_new_error(msg, DBUS_SERVICE ".NotFound",
 					"Object not found");
 }
 
 struct l_dbus_message *dbus_error_not_supported(struct l_dbus_message *msg)
 {
-	return l_dbus_message_new_error(msg, IWD_SERVICE ".NotSupported",
+	return l_dbus_message_new_error(msg, DBUS_SERVICE ".NotSupported",
 					"Operation not supported");
 }
 
 struct l_dbus_message *dbus_error_no_agent(struct l_dbus_message *msg)
 {
-	return l_dbus_message_new_error(msg, IWD_SERVICE ".NoAgent",
+	return l_dbus_message_new_error(msg, DBUS_SERVICE ".NoAgent",
 					"No Agent registered");
 }
 
 struct l_dbus_message *dbus_error_not_connected(struct l_dbus_message *msg)
 {
-	return l_dbus_message_new_error(msg, IWD_SERVICE ".NotConnected",
+	return l_dbus_message_new_error(msg, DBUS_SERVICE ".NotConnected",
 					"Not connected");
 }
 
 struct l_dbus_message *dbus_error_not_configured(struct l_dbus_message *msg)
 {
-	return l_dbus_message_new_error(msg, IWD_SERVICE ".NotConfigured",
+	return l_dbus_message_new_error(msg, DBUS_SERVICE ".NotConfigured",
 					"Not configured");
 }
 
 struct l_dbus_message *dbus_error_not_implemented(struct l_dbus_message *msg)
 {
-	return l_dbus_message_new_error(msg, IWD_SERVICE ".NotImplemented",
+	return l_dbus_message_new_error(msg, DBUS_SERVICE ".NotImplemented",
 					"Not implemented");
 }
 
 struct l_dbus_message *dbus_error_service_set_overlap(
 						struct l_dbus_message *msg)
 {
-	return l_dbus_message_new_error(msg, IWD_SERVICE ".ServiceSetOverlap",
+	return l_dbus_message_new_error(msg, DBUS_SERVICE ".ServiceSetOverlap",
 					"Service set overlap");
 }
 
 struct l_dbus_message *dbus_error_already_provisioned(
 						struct l_dbus_message *msg)
 {
-	return l_dbus_message_new_error(msg, IWD_SERVICE ".AlreadyProvisioned",
+	return l_dbus_message_new_error(msg, DBUS_SERVICE ".AlreadyProvisioned",
 					"Already provisioned");
 }
 
 struct l_dbus_message *dbus_error_not_hidden(struct l_dbus_message *msg)
 {
-	return l_dbus_message_new_error(msg, IWD_SERVICE ".NotHidden",
+	return l_dbus_message_new_error(msg, DBUS_SERVICE ".NotHidden",
 					"Not hidden");
 }
 
Signature · 1 line
2.25.1
#3 lore raw

Re: [RFC 2/5] dbus: remove iwd.h dependency

Hi James,

Quoted 11 lines · James Prestwood

Require a compile time flag to set the service

---
  Makefile.am |  1 +
  src/dbus.c  | 33 ++++++++++++++++-----------------
  2 files changed, 17 insertions(+), 17 deletions(-)
Makefile.am +0 -0
index 401d2ac5..2fb03aa9 100644

Makefile.am src_iwd_SOURCES = src/main.c linux/nl80211.h src/iwd.h src/missing.h \ · line 272

Quoted 5 lines
Makefile.am +1 -0
@@ -268,6 +268,7 @@ src_iwd_SOURCES = src/main.c linux/nl80211.h src/iwd.h src/missing.h \
 
  src_iwd_LDADD = $(ell_ldadd) -ldl
  src_iwd_DEPENDENCIES = $(ell_dependencies)
+src_iwd_CFLAGS = -DDBUS_SERVICE=\"net.connman.iwd\"
 

I'm not really a fan of doing it this way.

Makefile.am static dbus_agent_shutdown_func_t agent_shutdown; · line 41

Quoted 7 lines
Makefile.am +1 -1
@@ -38,99 +37,99 @@ static dbus_agent_shutdown_func_t agent_shutdown;
 
  struct l_dbus_message *dbus_error_busy(struct l_dbus_message *msg)
  {
-	return l_dbus_message_new_error(msg, IWD_SERVICE ".InProgress",
+	return l_dbus_message_new_error(msg, DBUS_SERVICE ".InProgress",
  					"Operation already in progress");

I wonder if we can add this to ell instead. We have almost the same code in oFono for example. And something very similar inside connman src/error.c.

Perhaps we can simply pull the name from l_dbus_name_acquire? Our apps are unlikely to ever register more than a single service name on the bus? Other ideas?

Regards,
-Denis

#4 lore raw

Re: [RFC 2/5] dbus: remove iwd.h dependency

Hi Denis,

Quoted 30 lines · Denis Kenzior

Hi James,

Quoted 12 lines · James Prestwood

Require a compile time flag to set the service
---
  Makefile.am |  1 +
  src/dbus.c  | 33 ++++++++++++++++-----------------

  2 files changed, 17 insertions(+), 17 deletions(-)
Makefile.am +0 -0
index 401d2ac5..2fb03aa9 100644
@@ -268,6 +268,7 @@ src_iwd_SOURCES = src/main.c linux/nl80211.h 
 src/iwd.h src/missing.h \
Quoted 3 lines

  src_iwd_LDADD = $(ell_ldadd) -ldl src_iwd_DEPENDENCIES = $(ell_dependencies) +src_iwd_CFLAGS = -DDBUS_SERVICE=\"net.connman.iwd\"

I'm not really a fan of doing it this way.

Makefile.am static dbus_agent_shutdown_func_t agent_shutdown; · line 40

Quoted 6 lines
Makefile.am +1 -1
@@ -38,99 +37,99 @@ static dbus_agent_shutdown_func_t agent_shutdown;
   struct l_dbus_message *dbus_error_busy(struct l_dbus_message *msg)
   {
-    return l_dbus_message_new_error(msg, IWD_SERVICE ".InProgress",
+    return l_dbus_message_new_error(msg, DBUS_SERVICE ".InProgress",
                       "Operation already in progress");

I wonder if we can add this to ell instead.  We have almost the same code in oFono for example. And something very similar inside connman src/error.c.

Quoted 1 line

Perhaps we can simply pull the name from l_dbus_name_acquire?  Our apps are unlikely to ever register more than a single service name on the bus?  Other ideas?

Since ofono and maybe others have the same code I can add these in ELL and use the service name thats registered.

Quoted 3 lines

Regards,
-Denis

#5 lore raw

RFC 3/5 hwsim: use dbus.c module

In reply to James Prestwood

Use the dbus module from IWD and remove duplicated code.

---
 Makefile.am   |  5 ++++-
 tools/hwsim.c | 24 +++---------------------
 2 files changed, 7 insertions(+), 22 deletions(-)
Makefile.am +4 -1
index 2fb03aa9..fcdd342a 100644
@@ -405,8 +405,11 @@ tools_hwsim_SOURCES = tools/hwsim.c src/mpdu.h \
 					src/storage.h src/storage.c \
 					src/common.h src/common.c \
 					src/band.h src/band.c \
-					src/crypto.h src/crypto.c
+					src/crypto.h src/crypto.c \
+					src/dbus.h src/dbus.c
+
 tools_hwsim_LDADD = $(ell_ldadd)
+tools_hwsim_CFLAGS = -DDBUS_SERVICE=\"net.connman.hwsim\"
 
 if DBUS_POLICY
 dist_dbus_data_DATA += tools/hwsim-dbus.conf
tools/hwsim.c +3 -21
index 5cb28624..402b3622 100644
@@ -45,6 +45,7 @@
 #include "src/crypto.h"
 #include "src/nl80211util.h"
 #include "src/nl80211cmd.h"
+#include "src/dbus.h"
 
 #define HWSIM_SERVICE "net.connman.hwsim"
 
@@ -541,27 +542,6 @@ static const char *interface_get_path(const struct interface_info_rec *rec)
 	return path;
 }
 
-static struct l_dbus_message *dbus_error_failed(struct l_dbus_message *msg)
-{
-	return l_dbus_message_new_error(msg, HWSIM_SERVICE ".Failed",
-					"Operation failed");
-}
-
-static struct l_dbus_message *dbus_error_invalid_args(
-						struct l_dbus_message *msg)
-{
-	return l_dbus_message_new_error(msg, HWSIM_SERVICE ".InvalidArgs",
-					"Argument type is wrong");
-}
-
-static void dbus_pending_reply(struct l_dbus_message **msg,
-				struct l_dbus_message *reply)
-{
-	l_dbus_send(dbus, reply);
-	l_dbus_message_unref(*msg);
-	*msg = NULL;
-}
-
 static const char *rule_get_path(struct hwsim_rule *rule)
 {
 	static char path[16];
@@ -2867,6 +2847,8 @@ static bool setup_dbus_hwsim(void)
 		return false;
 	}
 
+	dbus_init(dbus);
+
 	if (!l_dbus_register_interface(dbus, HWSIM_RADIO_MANAGER_INTERFACE,
 					setup_radio_manager_interface,
 					NULL, false)) {
Signature · 1 line
2.25.1
#6 lore raw

RFC 4/5 hwsim: refactor rules processing into separate file

In reply to James Prestwood

This extends hwsim to allow for external/custom frame processing by allowing separate modules to register frame callbacks and expose APIs to send frames back to the kernel.

One benefit of this is the hwsim rules code can be separated out which reduces the size of hwsim.c and isolates that functionality. Another benefit is that additional processing modules could be dropped in cleanly without modifying existing source code.

The goal here is to isolate the basic core functions of hwsim:
- Create, list, destroy radios
- Register to medium to kernel
- Track radios and interface creation/removal

---
 Makefile.am   |    5 +-
 tools/hwsim.c | 1091 +++----------------------------------------------
 tools/hwsim.h |   99 +++++
 tools/rules.c | 1017 +++++++++++++++++++++++++++++++++++++++++++++
 4 files changed, 1176 insertions(+), 1036 deletions(-)
 create mode 100644 tools/hwsim.h
 create mode 100644 tools/rules.c
Makefile.am +4 -1
index fcdd342a..3dd426d6 100644
@@ -406,7 +406,10 @@ tools_hwsim_SOURCES = tools/hwsim.c src/mpdu.h \
 					src/common.h src/common.c \
 					src/band.h src/band.c \
 					src/crypto.h src/crypto.c \
-					src/dbus.h src/dbus.c
+					src/dbus.h src/dbus.c \
+					src/watchlist.h src/watchlist.c \
+					src/module.h src/module.c \
+					tools/rules.c
 
 tools_hwsim_LDADD = $(ell_ldadd)
 tools_hwsim_CFLAGS = -DDBUS_SERVICE=\"net.connman.hwsim\"
tools/hwsim.c +56 -1035

1,265 lines not on this page · raw message

tools/hwsim.h new file +99 -0

99 lines not on this page · raw message

tools/rules.c new file +1017 -0

1,017 lines not on this page · raw message

Signature · 1 line
2.25.1
#7 lore raw

RFC 5/5 module: temporary hack to allow for no module dependencies

In reply to James Prestwood

The module dependencies don't exist unless IWD_MODULE_DEPENDS is used somewhere. For hwsim there is just a single module (rules) so it will not compile. This needs a proper solution but add a dummy dependency for now.

---
 src/module.c | 6 ++++++
 1 file changed, 6 insertions(+)
src/module.c +6 -0
index f32190af..aee88bfb 100644
@@ -120,6 +120,10 @@ int iwd_modules_init(void)
 		struct module *src;
 		struct module *dst;
 
+		if (!strcmp(dep->self, "dummy") &&
+				!strcmp(dep->target, "dummy"))
+			continue;
+
 		src = module_find(modules, n_modules, dep->self);
 		dst = module_find(modules, n_modules, dep->target);
 		if (!src || !dst) {
@@ -185,3 +189,5 @@ void iwd_modules_exit(void)
 	l_free(modules_sorted);
 	modules_sorted = NULL;
 }
+
+IWD_MODULE_DEPENDS(dummy, dummy);
Signature · 1 line
2.25.1
#8 lore raw
In reply to James Prestwood

Hi James,

Quoted 3 lines · James Prestwood

I wanted to put this out there just as an idea before I get too invested in extending it. The code itself needs cleanup (explained later) but I wanted some feedback on the general idea.

No real objection, but if you're moving 1kloc of code around, make sure that any moves are separated from overall refinements and other changes. For example, if you're exporting API functions or structures for use in (newly created) modules, do that first.

Quoted 5 lines

Eventually I'd like to extend this to support remote communication with multiple hwsim instances, e.g. via a multicast socket. As a starting point the rules code fit nicely as a frame processing module.

I assume you saw https://github.com/Raizo62/vwifi ?

Quoted 3 lines

Cleanup:
- Find solution for the IWD_MODULE_DEPENDS issue

Well, you need to put something into the section if you don't invoke IWD_MODULE_DEPENDS at least once.

Quoted 3 lines

- Add getters for radio/interface info objects, possibly for hwsim_frame too rather than everything exposed in hwsim.h.

That would be preferable.

Quoted 5 lines

- The hwsim_frame cleanup in rules needs to be changed. Prior hwsim_frame_unref() would send tx_info but now this needs to be handled in rules.c. Currently we peek into the refcount and see if it will be destroyed on unref, and send tx_info then.

Hmm, not sure I follow yet?

Also, iwd/tools/* is for executables that get built from single files. I guess we could relax that, but it may be better to have hwsim live in its own directory, similar to ead & iwmon.

Regards,
-Denis

#9 lore raw

Hi Denis,

Quoted 8 lines · Denis Kenzior

Hi James,

Quoted 3 lines · James Prestwood

I wanted to put this out there just as an idea before I get too invested in extending it. The code itself needs cleanup (explained later) but I wanted some feedback on the general idea.

No real objection, but if you're moving 1kloc of code around, make sure that any moves are separated from overall refinements and other changes.  For example, if you're exporting API functions or structures for use in (newly created) modules, do that first.

Yep definitely.

Quoted 8 lines
Quoted 6 lines

Eventually I'd like to extend this to support remote communication with multiple hwsim instances, e.g. via a multicast socket. As a starting point the rules code fit nicely as a frame processing module.

I assume you saw https://github.com/Raizo62/vwifi ?

I had not seen that one, but its pretty close to what I'm looking for. Still would need some specific modifications but its at least being maintained somewhat. Definitely worth looking into.

There is also a similar project, but its old and doesn't look particularly finished:

https://github.com/cmu-sei/welled

Quoted 13 lines
Quoted 4 lines

Cleanup:
  - Find solution for the IWD_MODULE_DEPENDS issue

Well, you need to put something into the section if you don't invoke IWD_MODULE_DEPENDS at least once.

Quoted 3 lines

  - Add getters for radio/interface info objects, possibly for hwsim_frame too rather than everything exposed in hwsim.h.

That would be preferable.

Quoted 7 lines
Quoted 5 lines

  - The hwsim_frame cleanup in rules needs to be changed. Prior hwsim_frame_unref() would send tx_info but now this needs to be handled in rules.c. Currently we peek into the refcount and see if it will be destroyed on unref, and send tx_info then.

Hmm, not sure I follow yet?

Just minor details really, I need to just look at it closer once its in a more finished state.

Quoted 2 lines

Also, iwd/tools/* is for executables that get built from single files. I guess we could relax that, but it may be better to have hwsim live in its own directory, similar to ead & iwmon.

Ok that does make more sense.

Quoted 3 lines

Regards,
-Denis