Skip to contentSkip to conversation

globalvar: add multiple device support

PATCH In review Jean-Christophe PLAGNIOL-VILLARD 2 participants 9 messages Mar 13, 2013 – Mar 15, 2013

Subsystems (2)
lore
Share & tools
lore
#0 lore raw

v2 cover globalvar: add multiple device support

HI,

v2:
fix bootargs

this will allow to reduce the scope of device and params to search during boot time

we keep the retro compatibility

The following changes since commit 092bfd5eb55d1b2d7ed098aa9723a2fa63b86192:

fix another brown paper bag bug introduced with compile time loglevel (2013-03-06 23:53:04 +0100)

are available in the git repository at:

git://git.jcrosoft.org/barebox.git delivery/globalvar

for you to fetch changes up to 2a749c4b65f823da50537798a2feeaf4a01d2212:

bootargs: switch globalvar to it's own device (2013-03-13 14:35:29 +0800)

----------------------------------------------------------------
Jean-Christophe PLAGNIOL-VILLARD (7):
params: allow to access first sub-device and params via env
globalvar: add it's own bus
globalvar: allow to register multiple device
net: switch to global device
dhcp: switch globalvar to it's own device
bootm: switch globalvar to it's own device
bootargs: switch globalvar to it's own device

 commands/bootm.c    |   20 +++++++++++++-------
 commands/global.c   |   41 +++++++++++++++++++++++++++++++++--------
 common/bootargs.c   |   14 ++++++++++++--
 common/complete.c   |   45 ++++++++++++++++++++++++++++++++++++++++-----
 common/globalvar.c  |  150 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++------------------
 include/globalvar.h |   26 ++++++++++++++++++++++++++
 lib/parameter.c     |   41 ++++++++++++++++++++++++++++++++++++++++-
 net/dhcp.c          |   40 ++++++++--------------------------------
 net/net.c           |   15 +++++++--------
 9 files changed, 311 insertions(+), 81 deletions(-)

Best Regards,
J.

_______________________________________________
barebox mailing list
[email protected]
http://lists.infradead.org/mailman/listinfo/barebox

#1 lore raw

PATCH v2 1/7 params: allow to access first sub-device and params via env

this will allow to keep retro-compatiblility and adding globalvar multiple device support

barebox@Somfy Animeo IP:/

 # echo $platform.soc.
 at91sam9260
 barebox@Somfy Animeo IP:/
 # echo $platform.soc.name
 Unknown
 barebox@Somfy Animeo IP:/
 # echo $global.dhcp.vendor_id
 barebox-animeo-ip

update complete too

---
 common/complete.c |   45 ++++++++++++++++++++++++++++++++++++++++-----
 lib/parameter.c   |   41 ++++++++++++++++++++++++++++++++++++++++-
 2 files changed, 80 insertions(+), 6 deletions(-)
common/complete.c +40 -5
index 9206ef0..17ee3c4 100644
@@ -169,7 +169,7 @@ int device_complete(struct string_list *sl, char *instr)
 }
 EXPORT_SYMBOL(device_complete);
 
-static int device_param_complete(char *begin, struct device_d *dev,
+static int device_param_complete(char *begin, char *dev_fullname, struct device_d *dev,
 				 struct string_list *sl, char *instr)
 {
 	struct param_d *param;
@@ -185,7 +185,7 @@ static int device_param_complete(char *begin, struct device_d *dev,
 			continue;
 
 		string_list_add_asprintf(sl, "%s%s.%s%c",
-			begin ? begin : "", dev_name(dev), param->name,
+			begin ? begin : "", dev_fullname, param->name,
 			begin ? ' ' : '=');
 	}
 
@@ -253,12 +253,47 @@ static int env_param_complete(struct string_list *sl, char *instr, int eval)
 	}
 
 	for_each_device(dev) {
-		if (!strncmp(instr, dev_name(dev), len)) {
+		struct device_d *child;
+		char *devname = asprintf("%s", dev_name(dev));
+
+		if (!strncmp(instr, devname, len)) {
 			if (eval)
-				device_param_complete("$", dev, sl, instr_param);
+				device_param_complete("$", devname, dev, sl, instr_param);
 			else
-				device_param_complete(NULL, dev, sl, instr_param);
+				device_param_complete(NULL, devname, dev, sl, instr_param);
+		}
+
+		if (dev->bus) {
+			free(devname);
+			continue;
+		}
+
+		device_for_each_child(dev, child) {
+			char *dev_fullname;
+			char *child_instr_param;
+			int child_len;
+
+			dev_fullname = asprintf("%s.%s", devname, dev_name(child));
+			child_instr_param = strchr(instr_param, '.');
+
+			if (child_instr_param) {
+				child_len = (child_instr_param - instr);
+				child_instr_param++;
+			} else {
+				child_len = strlen(instr);
+				child_instr_param = "";
+			}
+
+			if (!strncmp(instr, dev_fullname, child_len)) {
+				if (eval)
+					device_param_complete("$", dev_fullname, child, sl, child_instr_param);
+				else
+					device_param_complete(NULL, dev_fullname, child, sl, child_instr_param);
+			}
+
+			free(dev_fullname);
 		}
+		free(devname);
 	}
 
 	return 0;
lib/parameter.c +40 -1

53 lines not on this page · raw message

Signature · 7 lines
1.7.10.4


_______________________________________________
barebox mailing list
[email protected]
http://lists.infradead.org/mailman/listinfo/barebox
#2 lore raw

PATCH v2 2/7 globalvar: add it's own bus

---
 common/globalvar.c |   31 ++++++++++++++++++++-----------
 1 file changed, 20 insertions(+), 11 deletions(-)
common/globalvar.c +20 -11
index a8aaa72..f275a38 100644
@@ -3,17 +3,14 @@
 #include <globalvar.h>
 #include <init.h>
 
-static struct device_d global_device = {
-	.name = "global",
-	.id = DEVICE_ID_SINGLE,
-};
+static struct device_d *global_device;
 
 int globalvar_add(const char *name,
 		int (*set)(struct device_d *dev, struct param_d *p, const char *val),
 		const char *(*get)(struct device_d *, struct param_d *p),
 		unsigned long flags)
 {
-	return dev_add_param(&global_device, name, set, get, flags);
+	return dev_add_param(global_device, name, set, get, flags);
 }
 
 /*
@@ -27,9 +24,9 @@ char *globalvar_get_match(const char *match, const char *seperator)
 	char *val = NULL;
 	struct param_d *param;
 
-	list_for_each_entry(param, &global_device.parameters, list) {
+	list_for_each_entry(param, global_device.parameters, list) {
 		if (!strncmp(match, param->name, strlen(match))) {
-			const char *p = dev_get_param(&global_device, param->name);
+			const char *p = dev_get_param(global_device, param->name);
 			if (val) {
 				char *new = asprintf("%s%s%s", val, seperator, p);
 				free(val);
@@ -50,9 +47,9 @@ void globalvar_set_match(const char *match, const char *val)
 {
 	struct param_d *param;
 
-	list_for_each_entry(param, &global_device.parameters, list) {
+	list_for_each_entry(param, global_device.parameters, list) {
 		if (!strncmp(match, param->name, strlen(match)))
-			dev_set_param(&global_device, param->name, val);
+			dev_set_param(global_device, param->name, val);
 	}
 }
 
@@ -66,10 +63,22 @@ int globalvar_add_simple(const char *name)
 	return globalvar_add(name, NULL, NULL, 0);
 }
 
+static int global_match(struct device_d *dev, struct driver_d *drv)
+{
+	return 1;
+}
+
+static struct bus_type global_bus = {
+	.name = "global",
+	.match = global_match,
+	.probe = dummy_probe,
+};
+
 static int globalvar_init(void)
 {
-	register_device(&global_device);
+	bus_register(&global_bus);
+	global_device = &global_bus->dev;
 
 	return 0;
 }
-postconsole_initcall(globalvar_init);
+pure_initcall(globalvar_init);
Signature · 7 lines
1.7.10.4


_______________________________________________
barebox mailing list
[email protected]
http://lists.infradead.org/mailman/listinfo/barebox
#3 lore raw

PATCH v2 3/7 globalvar: allow to register multiple device

In reply to Jean-Christophe PLAGNIOL-VILLARD

This will allow to reduce the time spend to search for globalvar (boot time)

the access to the globalvar is retro-compatible

we can now create device via the global command

barebox@Somfy Animeo IP:/

 # devinfo
 devices:
 `---- global
      `---- net
      `---- bootm
      `---- dhcp
      ...
 barebox@Somfy Animeo IP:/
 resources:
 driver: none
 bus: global
 bus: none

Parameters:
hostname =
barebox@Somfy Animeo IP:/

 # devinfo net
 resources:
 driver: none
 bus: global

Parameters:
nameserver =
domainname =
barebox@Somfy Animeo IP:/

 # devinfo dhcp
 resources:
 driver: none
 bus: global

Parameters:
rootpath =
tftp_server_name =
bootfile =
oftree_file =
vendor_id = barebox-animeo-ip
client_id =
user_class =
client_uuid =
barebox@Somfy Animeo IP:/

 # devinfo bootm
 resources:
 driver: none
 bus: global

Parameters:
image =
oftree =
initrd =

---
 commands/global.c   |   41 ++++++++++++----
 common/globalvar.c  |  133 +++++++++++++++++++++++++++++++++++++++++++++------
 include/globalvar.h |   26 ++++++++++
 3 files changed, 178 insertions(+), 22 deletions(-)
commands/global.c +33 -8
index 427a231..539fa21 100644
@@ -23,14 +23,23 @@
 #include <environment.h>
 #include <getopt.h>
 
-static int globalvar_set(char* name, char* value)
+static int globalvar_set(char *devname, char* name, char* value)
 {
 	int ret;
 
-	ret = globalvar_add_simple(name);
+	if (devname) {
+		struct device_d *dev;
+
+		dev = get_device_by_name(devname);
+		if (!dev)
+			dev = global_add_device(devname);
+		ret = global_add_simple(dev, name);
+	} else {
+		ret = globalvar_add_simple(name);
+	}
 
 	if (value) {
-		char *tmp = asprintf("global.%s", name);
+		char *tmp = asprintf("%s.%s", devname ? devname : "global", name);
 		ret = setenv(tmp, value);
 		free(tmp);
 	}
@@ -43,12 +52,16 @@ static int do_global(int argc, char *argv[])
 	int opt;
 	int do_set_match = 0;
 	char *value;
+	char *devname = NULL;
 
-	while ((opt = getopt(argc, argv, "r")) > 0) {
+	while ((opt = getopt(argc, argv, "rd:")) > 0) {
 		switch (opt) {
 		case 'r':
 			do_set_match = 1;
 			break;
+		case 'd':
+			devname = optarg;
+			break;
 		}
 	}
 
@@ -68,17 +81,29 @@ static int do_global(int argc, char *argv[])
 		if (!value)
 			value = "";
 
-		globalvar_set_match(argv[0], value);
+		if (devname) {
+			struct device_d *dev;
+
+			dev = get_device_by_name(devname);
+
+			if (!dev)
+				return -EINVAL;
+
+			global_set_match(dev, argv[0], value);
+		} else {
+			globalvar_set_match(argv[0], value);
+		}
 		return 0;
 	}
 
-	return globalvar_set(argv[0], value);
+	return globalvar_set(devname, argv[0], value);
 }
 
 BAREBOX_CMD_HELP_START(global)
-BAREBOX_CMD_HELP_USAGE("global [-r] <var>[=<value]\n")
+BAREBOX_CMD_HELP_USAGE("global [-d device] [-r] <var>[=<value]\n")
 BAREBOX_CMD_HELP_SHORT("add a new global variable named <var>, optionally set to <value>\n")
-BAREBOX_CMD_HELP_SHORT("-r to set a value to of all globalvars beginning with 'match'")
+BAREBOX_CMD_HELP_SHORT("-r to set a value to of all globalvars beginning with 'match'\n")
+BAREBOX_CMD_HELP_SHORT("-d use a specific global device if do not exist create (if -r not set), it if not set use 'global'")
 BAREBOX_CMD_HELP_END
 
 BAREBOX_CMD_START(global)
common/globalvar.c +119 -14

170 lines not on this page · raw message

include/globalvar.h +26 -0
index ddf885f..322a1cb 100644
@@ -2,6 +2,7 @@
 #define __GLOBALVAR_H
 
 #ifdef CONFIG_GLOBALVAR
+struct device_d *global_add_device(const char *name);
 int globalvar_add_simple(const char *name);
 
 int globalvar_add(const char *name,
@@ -10,6 +11,13 @@ int globalvar_add(const char *name,
 		unsigned long flags);
 char *globalvar_get_match(const char *match, const char *seperator);
 void globalvar_set_match(const char *match, const char *val);
+
+struct device_d *global_add_device(const char *name);
+int global_add_simple(struct device_d *dev, const char *name);
+void global_set_match(struct device_d *dev, const char *match,
+			const char *val);
+char *global_get_match(struct device_d *dev, const char *match,
+			const char *seperator);
 #else
 static inline int globalvar_add_simple(const char *name)
 {
@@ -30,6 +38,24 @@ static inline char *globalvar_get_match(const char *match, const char *seperator
 }
 
 static inline void globalvar_set_match(const char *match, const char *val) {}
+
+static inline struct device_d *global_add_device(const char *name)
+{
+	return NULL;
+}
+
+static inline int global_add_simple(struct device_d *dev, const char *name)
+{
+	return 0;
+}
+
+static inline void global_set_match(struct device_d *dev, const char *match,
+			const char *val) {}
+static inline char *global_get_match(struct device_d *dev, const char *match,
+			const char *seperator)
+{
+	return NULL;
+}
 #endif
 
 #endif /* __GLOBALVAR_H */
Signature · 7 lines
1.7.10.4


_______________________________________________
barebox mailing list
[email protected]
http://lists.infradead.org/mailman/listinfo/barebox
#4 lore raw

PATCH v2 4/7 net: switch to global device

In reply to Jean-Christophe PLAGNIOL-VILLARD
---
 net/net.c |   15 +++++++--------
 1 file changed, 7 insertions(+), 8 deletions(-)
net/net.c +7 -8
index 639bc2d..4888d60 100644
@@ -35,6 +35,7 @@
 #include <init.h>
 #include <linux/ctype.h>
 #include <linux/err.h>
+#include <globalvar.h>
 
 static IPaddr_t	net_netmask;		/* Our subnet mask (0=unknown)	*/
 static IPaddr_t	net_gateway;		/* Our gateways IP address	*/
@@ -664,21 +665,19 @@ out:
 	return ret;
 }
 
-static struct device_d net_device = {
-	.name = "net",
-	.id = DEVICE_ID_SINGLE,
-};
-
 static int net_init(void)
 {
+	struct device_d *dev;
 	int i;
 
 	for (i = 0; i < PKTBUFSRX; i++)
 		NetRxPackets[i] = net_alloc_packet();
 
-	register_device(&net_device);
-	dev_add_param(&net_device, "nameserver", NULL, NULL, 0);
-	dev_add_param(&net_device, "domainname", NULL, NULL, 0);
+	dev = global_add_device("net");
+	if (dev) {
+		global_add_simple(dev, "nameserver");
+		global_add_simple(dev, "domainname");
+	}
 
 	return 0;
 }
Signature · 7 lines
1.7.10.4


_______________________________________________
barebox mailing list
[email protected]
http://lists.infradead.org/mailman/listinfo/barebox
#5 lore raw

PATCH v2 5/7 dhcp: switch globalvar to it's own device

In reply to Jean-Christophe PLAGNIOL-VILLARD
---
 net/dhcp.c |   40 ++++++++--------------------------------
 1 file changed, 8 insertions(+), 32 deletions(-)
net/dhcp.c +8 -32
index 8233ec3..7fde0d8 100644
@@ -81,31 +81,16 @@ static uint32_t dhcp_leasetime;
 static IPaddr_t net_dhcp_server_ip;
 static uint64_t dhcp_start;
 static char dhcp_tftpname[256];
+static struct device_d *dhcp_dev;
 
 static const char* dhcp_get_barebox_global(const char * var)
 {
-	char * var_global = asprintf("global.dhcp.%s", var);
-	const char *val;
-
-	if (!var_global)
-		return NULL;
-
-	val = getenv(var_global);
-	free(var_global);
-	return val;
+	return dev_get_param(dhcp_dev, var);
 }
 
 static int dhcp_set_barebox_global(const char * var, char *val)
 {
-	char * var_global = asprintf("global.dhcp.%s", var);
-	int ret;
-
-	if (!var_global)
-		return -ENOMEM;
-
-	ret = setenv(var_global, val);
-	free(var_global);
-	return ret;
+	return dev_set_param(dhcp_dev, var, val);
 }
 
 struct dhcp_opt {
@@ -663,30 +648,21 @@ static void dhcp_reset_env(void)
 	}
 }
 
-static void dhcp_global_add(const char *var)
-{
-	char * var_global = asprintf("dhcp.%s", var);
-
-	if (!var_global)
-		return;
-
-	globalvar_add_simple(var_global);
-	free(var_global);
-}
-
 static int dhcp_global_init(void)
 {
 	struct dhcp_opt *opt;
 	struct dhcp_param *param;
 	int i;
 
+	dhcp_dev = global_add_device("dhcp");
+
 	for (i = 0; i < ARRAY_SIZE(dhcp_options); i++) {
 		opt = &dhcp_options[i];
 
 		if (!opt->barebox_dhcp_global)
 			continue;
 
-		dhcp_global_add(opt->barebox_dhcp_global);
+		global_add_simple(dhcp_dev, opt->barebox_dhcp_global);
 	}
 
 	for (i = 0; i < ARRAY_SIZE(dhcp_params); i++) {
@@ -695,7 +671,7 @@ static int dhcp_global_init(void)
 		if (!param->barebox_dhcp_global)
 			continue;
 
-		dhcp_global_add(param->barebox_dhcp_global);
+		global_add_simple(dhcp_dev, param->barebox_dhcp_global);
 	}
 
 	return 0;
@@ -719,7 +695,7 @@ static int do_dhcp(int argc, char *argv[])
 
 	dhcp_reset_env();
 
-	dhcp_getenv_int("global.dhcp.retries", &retries);
+	dhcp_getenv_int("dhcp.retries", &retries);
 
 	while((opt = getopt(argc, argv, "H:v:c:u:U:r:")) > 0) {
 		switch(opt) {
Signature · 7 lines
1.7.10.4


_______________________________________________
barebox mailing list
[email protected]
http://lists.infradead.org/mailman/listinfo/barebox
#6 lore raw

PATCH v2 6/7 bootm: switch globalvar to it's own device

In reply to Jean-Christophe PLAGNIOL-VILLARD
---
 commands/bootm.c |   20 +++++++++++++-------
 1 file changed, 13 insertions(+), 7 deletions(-)
commands/bootm.c +13 -7
index 4d3f022..b70cd05 100644
@@ -48,6 +48,12 @@
 #include <asm-generic/memory_layout.h>
 
 static LIST_HEAD(handler_list);
+static struct device_d *bootm_dev;
+
+static const char* bootm_get_global(const char * var)
+{
+	return dev_get_param(bootm_dev, var);
+}
 
 /*
  * Additional oftree size for the fixed tree
@@ -265,10 +271,10 @@ static int do_bootm(int argc, char *argv[])
 	data.verify = 0;
 	data.verbose = 0;
 
-	oftree = getenv("global.bootm.oftree");
-	os_file = getenv("global.bootm.image");
+	oftree = bootm_get_global("oftree");
+	os_file = bootm_get_global("image");
 	if (IS_ENABLED(CONFIG_CMD_BOOTM_INITRD))
-		initrd_file = getenv("global.bootm.initrd");
+		initrd_file = bootm_get_global("initrd");
 
 	while ((opt = getopt(argc, argv, BOOTM_OPTS)) > 0) {
 		switch(opt) {
@@ -443,11 +449,11 @@ err_out:
 
 static int bootm_init(void)
 {
-
-	globalvar_add_simple("bootm.image");
-	globalvar_add_simple("bootm.oftree");
+	bootm_dev = global_add_device("bootm");
+	global_add_simple(bootm_dev, "image");
+	global_add_simple(bootm_dev, "oftree");
 	if (IS_ENABLED(CONFIG_CMD_BOOTM_INITRD))
-		globalvar_add_simple("bootm.initrd");
+		global_add_simple(bootm_dev, "initrd");
 
 	return 0;
 }
Signature · 7 lines
1.7.10.4


_______________________________________________
barebox mailing list
[email protected]
http://lists.infradead.org/mailman/listinfo/barebox
#7 lore raw

PATCH v2 7/7 bootargs: switch globalvar to it's own device

In reply to Jean-Christophe PLAGNIOL-VILLARD
---
 common/bootargs.c |   14 ++++++++++++--
 1 file changed, 12 insertions(+), 2 deletions(-)
common/bootargs.c +12 -2
index 6624f72..e9248ab 100644
@@ -22,9 +22,11 @@
 #include <magicvar.h>
 #include <globalvar.h>
 #include <environment.h>
+#include <init.h>
 
 static char *linux_bootargs;
 static int linux_bootargs_overwritten;
+static struct device_d *global_linux_dev;
 
 /*
  * This returns the Linux bootargs
@@ -45,11 +47,11 @@ const char *linux_bootargs_get(void)
 
 	free(linux_bootargs);
 
-	bootargs = globalvar_get_match("linux.bootargs.", " ");
+	bootargs = global_get_match(global_linux_dev, "bootargs.", " ");
 	if (!strlen(bootargs))
 		return getenv("bootargs");
 
-	mtdparts = globalvar_get_match("linux.mtdparts.", ";");
+	mtdparts = global_get_match(global_linux_dev, "mtdparts.", ";");
 
 	if (strlen(mtdparts)) {
 		linux_bootargs = asprintf("%s mtdparts=%s", bootargs, mtdparts);
@@ -76,5 +78,13 @@ int linux_bootargs_overwrite(const char *bootargs)
 	return 0;
 }
 
+static int bootargs_init(void)
+{
+	global_linux_dev = global_add_device("linux");
+
+	return 0;
+}
+late_initcall(bootargs_init);
+
 BAREBOX_MAGICVAR_NAMED(global_linux_bootargs_, global.linux.bootargs.*, "Linux bootargs variables");
 BAREBOX_MAGICVAR_NAMED(global_linux_mtdparts_, global.linux.mtdparts.*, "Linux mtdparts variables");
Signature · 7 lines
1.7.10.4


_______________________________________________
barebox mailing list
[email protected]
http://lists.infradead.org/mailman/listinfo/barebox
#8 lore raw
In reply to Jean-Christophe PLAGNIOL-VILLARD
Quoted 14 lines · Jean-Christophe PLAGNIOL-VILLARD

HI,

v2:
fix bootargs

this will allow to reduce the scope of device and params to search during boot time

we keep the retro compatibility

The following changes since commit 092bfd5eb55d1b2d7ed098aa9723a2fa63b86192:

fix another brown paper bag bug introduced with compile time loglevel (2013-03-06 23:53:04 +0100)

Quoted 7 lines

are available in the git repository at:

git://git.jcrosoft.org/barebox.git delivery/globalvar

for you to fetch changes up to 2a749c4b65f823da50537798a2feeaf4a01d2212:

bootargs: switch globalvar to it's own device (2013-03-13 14:35:29 +0800)

I still don't see why we should take this. You say it's about speed. I made some measurments. I read some global variables 1000 times each, below times are in ms:

Vanilla -next tree:
-------------------------------------
global.linux.mtdparts.nand0: 49178
global.dhcp.rootpath: 45282
dhcp.rootpath: 90329
global.sumsen: 47681

-next with your patches:
-------------------------------------
global.linux.mtdparts.nand0: 11407
global.dhcp.rootpath: 13961
dhcp.rootpath: 92394
global.sumsen: 5087

-next with register_device(&global_device) moved to pure_initcall:
-------------------------------------
global.linux.mtdparts.nand0: 8467
global.dhcp.rootpath: 5162
dhcp.rootpath: 90096
global.sumsen: 6541

(Moving registration of the global device simply has the effect that it is in the beginning of the devices list, thus we can find it faster)

So your patches indeed make reading the global variables significantly faster, but a simple one-liner patch shows makes it even faster.

So, no. We don't introduce new APIs just for the sake of it.

Sascha

Signature · 9 lines
Pengutronix e.K.                           |                             |
Industrial Linux Solutions                 | http://www.pengutronix.de/  |
Peiner Str. 6-8, 31137 Hildesheim, Germany | Phone: +49-5121-206917-0    |
Amtsgericht Hildesheim, HRA 2686           | Fax:   +49-5121-206917-5555 |

_______________________________________________
barebox mailing list
[email protected]
http://lists.infradead.org/mailman/listinfo/barebox