[RFC] batctl: add vlan_dyn_max command

Message ID 20241202060019.633-1-linus.luessing@c0d3.blue (mailing list archive)
State New
Delegated to: Antonio Quartulli
Headers
Series [RFC] batctl: add vlan_dyn_max command |

Commit Message

Linus Lüssing Dec. 2, 2024, 6 a.m. UTC
  This adds a vlan_dyn_max command to get and set the maximum number of
allowed learned VLANs from bridged-in clients in batman-adv.

Still TODO: Documentation in README and manpage.

Signed-off-by: Linus Lüssing <linus.luessing@c0d3.blue>
---
Posting this as a quick RFC for now as it probably doesn't make sense to
expose/advertise this setting yet as long as there are still issues with
a setting >0 while BLA is enabled?
---
 Makefile       |   1 +
 batman_adv.h   |   6 +++
 netlink.c      |   3 ++
 vlan_dyn_max.c | 100 +++++++++++++++++++++++++++++++++++++++++++++++++
 4 files changed, 110 insertions(+)
 create mode 100644 vlan_dyn_max.c
  

Patch

diff --git a/Makefile b/Makefile
index 92f84e5bf012..5f6a703acc36 100755
--- a/Makefile
+++ b/Makefile
@@ -86,6 +86,7 @@  $(eval $(call add_command,translate,y))
 $(eval $(call add_command,translocal,y))
 $(eval $(call add_command,transtable_global_json,y))
 $(eval $(call add_command,transtable_local_json,y))
+$(eval $(call add_command,vlan_dyn_max,y))
 $(eval $(call add_command,vlan_json,y))
 
 MANPAGE = man/batctl.8
diff --git a/batman_adv.h b/batman_adv.h
index 9498ccb09d67..d93f5b0fbdd6 100644
--- a/batman_adv.h
+++ b/batman_adv.h
@@ -481,6 +481,12 @@  enum batadv_nl_attrs {
 	 */
 	BATADV_ATTR_MULTICAST_FANOUT,
 
+	/**
+	 * @BATADV_ATTR_VLAN_DYN_MAX: defines the maximum number of allowed
+	 * learned VLANs from bridged-in clients.
+	 */
+	BATADV_ATTR_VLAN_DYN_MAX,
+
 	/* add attributes above here, update the policy in netlink.c */
 
 	/**
diff --git a/netlink.c b/netlink.c
index e92fa8003d3b..9cd5eca507db 100644
--- a/netlink.c
+++ b/netlink.c
@@ -231,6 +231,9 @@  struct nla_policy batadv_netlink_policy[NUM_BATADV_ATTR] = {
 	[BATADV_ATTR_MULTICAST_FANOUT] = {
 		.type = NLA_U32,
 	},
+	[BATADV_ATTR_VLAN_DYN_MAX] = {
+		.type = NLA_U16,
+	},
 };
 
 int netlink_create(struct state *state)
diff --git a/vlan_dyn_max.c b/vlan_dyn_max.c
new file mode 100644
index 000000000000..4cc45360a90f
--- /dev/null
+++ b/vlan_dyn_max.c
@@ -0,0 +1,100 @@ 
+// SPDX-License-Identifier: GPL-2.0
+/* Copyright (C) B.A.T.M.A.N. contributors:
+ *
+ * Linus Lüssing <linus.luessing@c0d3.blue>
+ *
+ * License-Filename: LICENSES/preferred/GPL-2.0
+ */
+
+#include <errno.h>
+#include <stddef.h>
+#include <stdint.h>
+#include <string.h>
+
+#include "main.h"
+#include "sys.h"
+
+static struct vlan_dyn_max_data {
+	uint16_t vlan_dyn_max;
+} vlan_dyn_max;
+
+static int parse_vlan_dyn_max(struct state *state, int argc, char *argv[])
+{
+	struct settings_data *settings = state->cmd->arg;
+	struct vlan_dyn_max_data *data = settings->data;
+	char *endptr;
+
+	if (argc != 2) {
+		fprintf(stderr, "Error - incorrect number of arguments (expected 1)\n");
+		return -EINVAL;
+	}
+
+	data->vlan_dyn_max = strtoul(argv[1], &endptr, 0);
+	if (!endptr || *endptr != '\0') {
+		fprintf(stderr, "Error - the supplied argument is invalid: %s\n", argv[1]);
+		return -EINVAL;
+	}
+
+	return 0;
+}
+
+static int print_vlan_dyn_max(struct nl_msg *msg, void *arg)
+{
+	struct nlattr *attrs[BATADV_ATTR_MAX + 1];
+	struct nlmsghdr *nlh = nlmsg_hdr(msg);
+	struct genlmsghdr *ghdr;
+	int *result = arg;
+
+	if (!genlmsg_valid_hdr(nlh, 0))
+		return NL_OK;
+
+	ghdr = nlmsg_data(nlh);
+
+	if (nla_parse(attrs, BATADV_ATTR_MAX, genlmsg_attrdata(ghdr, 0),
+		      genlmsg_len(ghdr), batadv_netlink_policy)) {
+		return NL_OK;
+	}
+
+	if (!attrs[BATADV_ATTR_VLAN_DYN_MAX])
+		return NL_OK;
+
+	printf("%u\n", nla_get_u16(attrs[BATADV_ATTR_VLAN_DYN_MAX]));
+
+	*result = 0;
+	return NL_STOP;
+}
+
+static int get_vlan_dyn_max(struct state *state)
+{
+	return sys_simple_nlquery(state, BATADV_CMD_GET_MESH,
+				  NULL, print_vlan_dyn_max);
+}
+
+static int set_attrs_vlan_dyn_max(struct nl_msg *msg, void *arg)
+{
+	struct state *state = arg;
+	struct settings_data *settings = state->cmd->arg;
+	struct vlan_dyn_max_data *data = settings->data;
+
+	nla_put_u16(msg, BATADV_ATTR_VLAN_DYN_MAX, data->vlan_dyn_max);
+
+	return 0;
+}
+
+static int set_vlan_dyn_max(struct state *state)
+{
+	return sys_simple_nlquery(state, BATADV_CMD_SET_MESH,
+				  set_attrs_vlan_dyn_max, NULL);
+}
+
+static struct settings_data batctl_settings_vlan_dyn_max = {
+	.data = &vlan_dyn_max,
+	.parse = parse_vlan_dyn_max,
+	.netlink_get = get_vlan_dyn_max,
+	.netlink_set = set_vlan_dyn_max,
+};
+
+COMMAND_NAMED(SUBCOMMAND_MIF, vlan_dyn_max, "vdm", handle_sys_setting,
+	      COMMAND_FLAG_MESH_IFACE | COMMAND_FLAG_NETLINK,
+	      &batctl_settings_vlan_dyn_max,
+	      "[0..4096]        \tdisplay or modify vlan_dyn_max setting");