[netfilter-cvslog] r3313 - in trunk/nfsim: core core/ipv4
kernelenv/include netfilter tools
rusty at netfilter.org
rusty at netfilter.org
Fri Dec 10 06:22:14 CET 2004
Author: rusty at netfilter.org
Date: 2004-12-10 06:22:13 +0100 (Fri, 10 Dec 2004)
New Revision: 3313
Added:
trunk/nfsim/tools/module.c
Modified:
trunk/nfsim/core/core.c
trunk/nfsim/core/core.h
trunk/nfsim/core/expect.c
trunk/nfsim/core/ipv4/ipv4.c
trunk/nfsim/core/log.c
trunk/nfsim/core/proc.c
trunk/nfsim/kernelenv/include/kernelenv.h
trunk/nfsim/netfilter/Makefile
trunk/nfsim/netfilter/Makefile.kbuild
trunk/nfsim/tools/gen_err.c
trunk/nfsim/tools/gen_ip.c
trunk/nfsim/tools/hook.c
trunk/nfsim/tools/ifconfig.c
trunk/nfsim/tools/info.c
trunk/nfsim/tools/iptables.c
trunk/nfsim/tools/proc.c
trunk/nfsim/tools/queue.c
trunk/nfsim/tools/route.c
trunk/nfsim/tools/time.c
Log:
Module support:
init_call() macro for non-kernel code to use for auto-inits.
Makefile changes for KBUILD_MODNAME define
lsmod, insmod and rmmod support
-a option to load all modules at startup
Modified: trunk/nfsim/core/core.c
===================================================================
--- trunk/nfsim/core/core.c 2004-12-10 00:29:06 UTC (rev 3312)
+++ trunk/nfsim/core/core.c 2004-12-10 05:22:13 UTC (rev 3313)
@@ -76,7 +76,7 @@
extern struct net_device *create_device(const char *name, int argc, char **);
struct net_device *loopback_dev_p;
-int core_init(void)
+static void core_init(void)
{
int i, h;
char *argv[3];
@@ -105,10 +105,9 @@
}
nfsim_log(LOG_UI, "core_init() completed");
- return 0;
}
-module_init(core_init);
+init_call(core_init);
static int enqueue_packet(struct sk_buff *skb,
struct nf_info *info, void *data)
@@ -135,26 +134,28 @@
static void run_inits(void)
{
/* Linker magic creates these to delineate section. */
- extern initcall_t __start_module_init[], __stop_module_init[];
+ extern initcall_t __start_init_call[], __stop_init_call[];
initcall_t *p;
- for (p = __start_module_init; p < __stop_module_init; p++)
+ for (p = __start_init_call; p < __stop_init_call; p++)
(*p)();
}
+
static const struct option options[] = {
{"echo", 0, 0, 'x'},
{"quiet", 0, 0, 'q'},
{"exit", 0, 0, 'e'},
+ {"load-all", 0, 0, 'a'},
{0, 0, 0, 0 }
};
int main(int argc, char **argv)
{
- int termfd;
+ int termfd, load_all = 0;
char c;
- while ((c = getopt_long(argc, argv, "xqe", options, NULL)) != EOF) {
+ while ((c = getopt_long(argc, argv, "xqea", options, NULL)) != EOF) {
switch (c) {
case 'x':
tui_echo_commands = 1;
@@ -165,6 +166,9 @@
case 'e':
tui_abort_on_fail = 1;
break;
+ case 'a':
+ load_all = 1;
+ break;
default:
fprintf(stderr, "Unknown argument %c\n", c);
return EXIT_FAILURE;
@@ -175,6 +179,9 @@
run_inits();
+ if (load_all && !load_all_modules())
+ barf("Module loading failed\n");
+
nf_register_queue_handler(PF_INET, enqueue_packet, NULL);
nfsim_log(LOG_UI, "initialisation done");
@@ -195,6 +202,7 @@
message_cleanup();
tui_cleanup(termfd);
+ unload_all_modules();
return 0;
}
Modified: trunk/nfsim/core/core.h
===================================================================
--- trunk/nfsim/core/core.h 2004-12-10 00:29:06 UTC (rev 3312)
+++ trunk/nfsim/core/core.h 2004-12-10 05:22:13 UTC (rev 3313)
@@ -269,5 +269,14 @@
int netlink_unicast(struct sock *ssk, struct sk_buff *skb, u32 pid,
int nonblock);
+bool load_all_modules(void);
+void unload_all_modules(void);
+/* init code */
+typedef void (*initcall_t)(void);
+#define init_call(fn) \
+ static initcall_t __initcall_##fn \
+ __attribute__((__unused__)) \
+ __attribute__((__section__("init_call"))) = &fn
+
#endif /* __HAVE_CORE_H */
Modified: trunk/nfsim/core/expect.c
===================================================================
--- trunk/nfsim/core/expect.c 2004-12-10 00:29:06 UTC (rev 3312)
+++ trunk/nfsim/core/expect.c 2004-12-10 05:22:13 UTC (rev 3313)
@@ -255,14 +255,11 @@
*/
}
-static int init(void)
+static void init(void)
{
tui_register_command("expect", expect_cmd, expect_help);
tui_register_pre_post_hook(expect_pre_command, expect_post_command);
- return 0;
}
+init_call(init);
-
-module_init(init);
-
Modified: trunk/nfsim/core/ipv4/ipv4.c
===================================================================
--- trunk/nfsim/core/ipv4/ipv4.c 2004-12-10 00:29:06 UTC (rev 3312)
+++ trunk/nfsim/core/ipv4/ipv4.c 2004-12-10 05:22:13 UTC (rev 3313)
@@ -72,7 +72,7 @@
- interfaces
- routes
*/
-int init(void)
+void init(void)
{
/* name our hooks */
nf_hooknames[PF_INET][0] = "NF_IP_PRE_ROUTING";
@@ -80,11 +80,9 @@
nf_hooknames[PF_INET][2] = "NF_IP_FORWARD";
nf_hooknames[PF_INET][3] = "NF_IP_LOCAL_OUT";
nf_hooknames[PF_INET][4] = "NF_IP_POST_ROUTING";
-
- return 0;
}
-module_init(init);
+init_call(init);
int register_inetaddr_notifier(struct notifier_block *nb)
{
Modified: trunk/nfsim/core/log.c
===================================================================
--- trunk/nfsim/core/log.c 2004-12-10 00:29:06 UTC (rev 3312)
+++ trunk/nfsim/core/log.c 2004-12-10 05:22:13 UTC (rev 3313)
@@ -277,7 +277,7 @@
*/
}
-int log_init(void)
+void log_init(void)
{
logstream = stdout;
if (!tui_quiet)
@@ -285,10 +285,7 @@
describe_packets = 1;
memset(printk_buf, 0, PRINTK_BUFSIZ);
- if (tui_register_command("log", log_admin, log_admin_help))
- return -1;
-
- return 0;
+ tui_register_command("log", log_admin, log_admin_help);
}
-module_init(log_init);
+init_call(log_init);
Modified: trunk/nfsim/core/proc.c
===================================================================
--- trunk/nfsim/core/proc.c 2004-12-10 00:29:06 UTC (rev 3312)
+++ trunk/nfsim/core/proc.c 2004-12-10 05:22:13 UTC (rev 3313)
@@ -145,7 +145,7 @@
return;
}
-static int proc_init(void)
+static void proc_init(void)
{
proc_root.namelen = 5;
proc_root.name = "/proc";
@@ -153,9 +153,7 @@
proc_net = proc_mkdir("net", 0);
proc_net_stat = proc_mkdir("stat", proc_net);
-
- return 0;
}
-module_init(proc_init);
+init_call(proc_init);
Modified: trunk/nfsim/kernelenv/include/kernelenv.h
===================================================================
--- trunk/nfsim/kernelenv/include/kernelenv.h 2004-12-10 00:29:06 UTC (rev 3312)
+++ trunk/nfsim/kernelenv/include/kernelenv.h 2004-12-10 05:22:13 UTC (rev 3313)
@@ -55,6 +55,9 @@
#define unlikely(x) (x)
#define likely(x) (x)
+#define __stringify_1(x) #x
+#define __stringify(x) __stringify_1(x)
+
/*
* min()/max() macros that also do
* strict type-checking.. See the
@@ -1150,35 +1153,47 @@
#define module_put(x)
#define try_module_get(x) 1
+struct __module_init {
+ int (*initcall)(void);
+ const char *name;
+};
+struct __module_exit {
+ void (*exitcall)(void);
+ const char *name;
+};
-typedef int (*initcall_t)(void);
-typedef void (*exitcall_t)(void);
-
#define __init
#define __initdata
#define __exit
-#define module_init(fn) \
- static initcall_t __initcall_##fn \
- __attribute__((__unused__)) \
- __attribute__((__section__("module_init"))) = &fn
+#define MODULE_NAME_LEN (256 - sizeof(unsigned long) * 5)
-#define module_exit(fn) \
- static exitcall_t __exitcall_##fn \
- __attribute__((__unused__)) \
- __attribute__((__section__("module_exit"))) = &fn
-
-#define MODULE_NAME_LEN (256 - sizeof(unsigned long))
-
struct module {
+ struct list_head list;
+ int (*init)(void);
+ void (*exit)(void);
+ long state;
char name[MODULE_NAME_LEN];
};
-static struct module __this __attribute__((__unused__)) = { .name = __BASE_FILE__ };
-
+#ifdef KBUILD_MODNAME
+static struct module __this __attribute__((section("__modules"), unused)) = { .name = __stringify(KBUILD_MODNAME) };
#define THIS_MODULE &__this
+#define module_init(fn) \
+ static struct __module_init __initcall_##fn \
+ __attribute__((__unused__)) \
+ __attribute__((__section__("module_init"))) = { &fn, __stringify(KBUILD_MODNAME) }
+
+#define module_exit(fn) \
+ static struct __module_exit __exitcall_##fn \
+ __attribute__((__unused__)) \
+ __attribute__((__section__("module_exit"))) = { &fn, __stringify(KBUILD_MODNAME) }
+#else
+#define THIS_MODULE 0
+#endif /* KBUILD_MODNAME */
+
#if LINUX_VERSION_CODE < KERNEL_VERSION(2,5,0)
#define MOD_INC_USE_COUNT
#define MOD_DEC_USE_COUNT
Modified: trunk/nfsim/netfilter/Makefile
===================================================================
--- trunk/nfsim/netfilter/Makefile 2004-12-10 00:29:06 UTC (rev 3312)
+++ trunk/nfsim/netfilter/Makefile 2004-12-10 05:22:13 UTC (rev 3313)
@@ -4,4 +4,3 @@
$(TYPE)/$(TYPE).o: $(TYPE)/
cd $(@D) && $(MAKE) -f ../Makefile.kbuild obj=$(@F) $(@F)
-
Modified: trunk/nfsim/netfilter/Makefile.kbuild
===================================================================
--- trunk/nfsim/netfilter/Makefile.kbuild 2004-12-10 00:29:06 UTC (rev 3312)
+++ trunk/nfsim/netfilter/Makefile.kbuild 2004-12-10 05:22:13 UTC (rev 3313)
@@ -8,11 +8,11 @@
$(TYPE).o: $(obj-y)
$(LD) $(LDFLAGS) -r -o $(@) $^
-%.o: %.c
- @$(MAKE) $@
+%.o:%.c
+ $(COMPILE.c) -DKBUILD_MODNAME=$(@:.o=) $(OUTPUT_OPTION) $<
%.o:
- @$(MAKE) $($(@:.o=-objs))
+ @$(MAKE) CFLAGS="$(CFLAGS) -DKBUILD_MODNAME=$(@:.o=)" $($(@:.o=-objs))
$(LD) $(LDFLAGS) -r -o $(@) $($(@:.o=-objs))
Modified: trunk/nfsim/tools/gen_err.c
===================================================================
--- trunk/nfsim/tools/gen_err.c 2004-12-10 00:29:06 UTC (rev 3312)
+++ trunk/nfsim/tools/gen_err.c 2004-12-10 05:22:13 UTC (rev 3313)
@@ -139,10 +139,9 @@
*/
}
-static int init(void)
+static void init(void)
{
tui_register_command("gen_err", gen_err, gen_err_help);
- return 0;
}
-module_init(init);
+init_call(init);
Modified: trunk/nfsim/tools/gen_ip.c
===================================================================
--- trunk/nfsim/tools/gen_ip.c 2004-12-10 00:29:06 UTC (rev 3312)
+++ trunk/nfsim/tools/gen_ip.c 2004-12-10 05:22:13 UTC (rev 3313)
@@ -727,11 +727,10 @@
return send_packet(&packet, interface, dump_data);
}
-static int init(void)
+static void init(void)
{
tui_register_command("gen_ip", gen_ip, gen_ip_help);
- return 0;
}
-module_init(init);
+init_call(init);
Modified: trunk/nfsim/tools/hook.c
===================================================================
--- trunk/nfsim/tools/hook.c 2004-12-10 00:29:06 UTC (rev 3312)
+++ trunk/nfsim/tools/hook.c 2004-12-10 05:22:13 UTC (rev 3313)
@@ -198,12 +198,11 @@
*/
}
-static int init(void)
+static void init(void)
{
tui_register_command("hook", hook_cmd, hook_help);
tui_register_pre_post_hook(NULL, hook_post_cleanup);
- return 0;
}
-module_init(init);
+init_call(init);
Modified: trunk/nfsim/tools/ifconfig.c
===================================================================
--- trunk/nfsim/tools/ifconfig.c 2004-12-10 00:29:06 UTC (rev 3312)
+++ trunk/nfsim/tools/ifconfig.c 2004-12-10 05:22:13 UTC (rev 3313)
@@ -285,11 +285,10 @@
*/
}
-static int init(void)
+static void init(void)
{
tui_register_command("ifconfig", ifconfig, ifconfig_help);
- return 0;
}
-module_init(init);
+init_call(init);
Modified: trunk/nfsim/tools/info.c
===================================================================
--- trunk/nfsim/tools/info.c 2004-12-10 00:29:06 UTC (rev 3312)
+++ trunk/nfsim/tools/info.c 2004-12-10 05:22:13 UTC (rev 3313)
@@ -151,13 +151,10 @@
return false;
}
-static int init(void)
+static void init(void)
{
tui_register_command("info", info, NULL);
- return 0;
}
+init_call(init);
-
-module_init(init);
-
Modified: trunk/nfsim/tools/iptables.c
===================================================================
--- trunk/nfsim/tools/iptables.c 2004-12-10 00:29:06 UTC (rev 3312)
+++ trunk/nfsim/tools/iptables.c 2004-12-10 05:22:13 UTC (rev 3313)
@@ -88,7 +88,7 @@
"\tspecified arguments", argv[0]);
}
-static int init(void)
+static void init(void)
{
char *prefix = getenv("NFSIM_IPTABLES_PREFIX");
if (prefix)
@@ -104,9 +104,7 @@
tui_register_command("ip6tables-save", run_command, run_command_help);
tui_register_command("ip6tables-restore", run_command, run_command_help);
#endif
-
- return 0;
}
-module_init(init);
+init_call(init);
Added: trunk/nfsim/tools/module.c
===================================================================
--- trunk/nfsim/tools/module.c 2004-12-10 00:29:06 UTC (rev 3312)
+++ trunk/nfsim/tools/module.c 2004-12-10 05:22:13 UTC (rev 3313)
@@ -0,0 +1,267 @@
+/*
+
+Copyright (c) 2004 Rusty Russell
+
+This file is part of nfsim.
+
+nfsim is free software; you can redistribute it and/or modify
+it under the terms of the GNU General Public License as published by
+the Free Software Foundation; either version 2 of the License, or
+(at your option) any later version.
+
+nfsim is distributed in the hope that it will be useful,
+but WITHOUT ANY WARRANTY; without even the implied warranty of
+MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+GNU General Public License for more details.
+
+You should have received a copy of the GNU General Public License
+along with nfsim; if not, write to the Free Software
+Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
+*/
+
+/* Module handling functions. */
+#include <core.h>
+#include <tui.h>
+#include <log.h>
+#include <utils.h>
+#include <linux/list.h>
+
+static LIST_HEAD(modules);
+
+static struct module *find_module(const char *name)
+{
+ struct module *i;
+
+ list_for_each_entry(i, &modules, list)
+ if (streq(i->name, name))
+ return i;
+ return NULL;
+}
+
+static void populate_inits(void)
+{
+ extern struct __module_init __start_module_init[],__stop_module_init[];
+ struct __module_init *i;
+
+ for (i = __start_module_init; i < __stop_module_init; i++) {
+ struct module *m = find_module(i->name);
+ if (!m)
+ barf("Cannot find module %s for init %p\n", i->name,
+ i->initcall);
+ if (m->init)
+ barf("Two initcalls for module %s: %p & %p\n",
+ i->name, m->init, i->initcall);
+ m->init = i->initcall;
+ }
+}
+
+static void populate_exits(void)
+{
+ extern struct __module_exit __start_module_exit[],__stop_module_exit[];
+ struct __module_exit *i;
+
+ for (i = __start_module_exit; i < __stop_module_exit; i++) {
+ struct module *m = find_module(i->name);
+ if (!m)
+ barf("Cannot find module %s for exit %p\n", i->name,
+ i->exitcall);
+ if (m->exit)
+ barf("Two exitcalls for module %s: %p & %p\n",
+ i->name, m->exit, i->exitcall);
+ m->exit = i->exitcall;
+ }
+}
+
+/* For each unique module name, create structure and populate. */
+static void find_modules(void)
+{
+ /* Linker magic creates these to delineate section. */
+ extern struct module __start___modules[], __stop___modules[];
+ struct module *i;
+
+ for (i = __start___modules; i < __stop___modules; i++) {
+ /* There will be duplicates. If not, append. */
+ if (!find_module(i->name))
+ list_add_tail(&i->list, &modules);
+ }
+
+ populate_inits();
+ populate_exits();
+}
+
+static void lsmod_help(int argc, char **argv)
+{
+#include "module-help:lsmod"
+/*** XML Help:
+ <section id="c:lsmod">
+ <title><command>lsmod</command></title>
+ <para>List modules (both available and loaded)</para>
+ <cmdsynopsis>
+ <command>lsmod</command>
+ </cmdsynopsis>
+
+ <para><command>lsmod</command> lists all the modules, one to a
+ line, indicating which ones are loaded.</para>
+
+ </section>
+*/
+}
+
+static bool lsmod(int argc, char *argv[])
+{
+ struct module *i;
+
+ list_for_each_entry(i, &modules, list)
+ nfsim_log(LOG_ALWAYS, "%s %s", i->name,
+ i->state ? "LOADED" : "UNLOADED");
+ return true;
+}
+
+static void insmod_help(int argc, char **argv)
+{
+#include "module-help:insmod"
+/*** XML Help:
+ <section id="c:insmod">
+ <title><command>insmod</command></title>
+ <para>Insert a module</para>
+ <cmdsynopsis>
+ <command>insmod</command>
+ <arg choice="opt"><replaceable>modulename</replaceable></arg>
+ <cmdsynopsis>
+ </cmdsynopsis>
+ <command>insmod</command>
+ <arg choice="req">-a</arg>
+ </cmdsynopsis>
+
+ <para><command>insmod</command> loads a module, or all modules. The caller must
+ ensure that any other module this one expects are already
+ loaded.</para>
+ </section>
+*/
+}
+
+bool load_all_modules(void)
+{
+ struct module *mod;
+ int ret;
+
+ list_for_each_entry(mod, &modules, list) {
+ if (mod->state)
+ continue;
+ ret = mod->init();
+ if (ret != 0) {
+ nfsim_log(LOG_UI,
+ "Module %s loading failed: %i.",
+ mod->name, ret);
+ return false;
+ }
+ mod->state = 1;
+ }
+ return true;
+}
+
+static bool insmod(int argc, char *argv[])
+{
+ struct module *mod;
+ int ret;
+
+ if (argc != 2) {
+ insmod_help(argc, argv);
+ return false;
+ }
+
+ if (streq(argv[1], "-a"))
+ return load_all_modules();
+
+ mod = find_module(argv[1]);
+ if (!mod) {
+ nfsim_log(LOG_UI, "Module %s not found.", argv[1]);
+ return false;
+ }
+ if (mod->state) {
+ nfsim_log(LOG_UI, "Module %s already loaded.", argv[1]);
+ return false;
+ }
+ ret = mod->init();
+ if (ret != 0) {
+ nfsim_log(LOG_UI, "Module %s loading failed: %i.", argv[1],
+ ret);
+ return false;
+ }
+ mod->state = 1;
+ return true;
+}
+
+static void rmmod_help(int argc, char **argv)
+{
+#include "module-help:rmmod"
+/*** XML Help:
+ <section id="c:rmmod">
+ <title><command>rmmod</command></title>
+ <para>Remove a module</para>
+ <cmdsynopsis>
+ <command>rmmod</command>
+ </cmdsynopsis>
+ <cmdsynopsis>
+ <command>insmod</command>
+ <arg choice="req">-a</arg>
+ </cmdsynopsis>
+
+ <para><command>rmmod</command> removes a loaded module. The
+ caller must ensure that any module which is using this is already
+ unloaded. </para>
+
+ </section>
+*/
+}
+
+void unload_all_modules(void)
+{
+ struct module *mod;
+
+ list_for_each_entry(mod, &modules, list) {
+ if (!mod->state)
+ continue;
+ mod->exit();
+ mod->state = 0;
+ }
+}
+
+static bool rmmod(int argc, char *argv[])
+{
+ struct module *mod;
+
+ if (argc != 2) {
+ rmmod_help(argc, argv);
+ return false;
+ }
+
+ if (streq(argv[1], "-a")) {
+ unload_all_modules();
+ return true;
+ }
+
+ mod = find_module(argv[1]);
+ if (!mod) {
+ nfsim_log(LOG_UI, "Module %s not found.", argv[1]);
+ return false;
+ }
+ if (!mod->state) {
+ nfsim_log(LOG_UI, "Module %s not loaded.", argv[1]);
+ return false;
+ }
+ mod->exit();
+ mod->state = 0;
+ return true;
+}
+
+static void init(void)
+{
+ find_modules();
+
+ tui_register_command("lsmod", lsmod, lsmod_help);
+ tui_register_command("insmod", insmod, insmod_help);
+ tui_register_command("rmmod", rmmod, rmmod_help);
+}
+
+init_call(init);
Modified: trunk/nfsim/tools/proc.c
===================================================================
--- trunk/nfsim/tools/proc.c 2004-12-10 00:29:06 UTC (rev 3312)
+++ trunk/nfsim/tools/proc.c 2004-12-10 05:22:13 UTC (rev 3313)
@@ -169,13 +169,10 @@
*/
}
-static int init(void)
+static void init(void)
{
tui_register_command("proc", proc, proc_help);
- return 0;
}
+init_call(init);
-
-module_init(init);
-
Modified: trunk/nfsim/tools/queue.c
===================================================================
--- trunk/nfsim/tools/queue.c 2004-12-10 00:29:06 UTC (rev 3312)
+++ trunk/nfsim/tools/queue.c 2004-12-10 05:22:13 UTC (rev 3313)
@@ -105,13 +105,10 @@
*/
}
-static int init(void)
+static void init(void)
{
tui_register_command("queue", queue, queue_help);
- return 0;
}
+init_call(init);
-
-module_init(init);
-
Modified: trunk/nfsim/tools/route.c
===================================================================
--- trunk/nfsim/tools/route.c 2004-12-10 00:29:06 UTC (rev 3312)
+++ trunk/nfsim/tools/route.c 2004-12-10 05:22:13 UTC (rev 3313)
@@ -253,11 +253,10 @@
*/
}
-static int init(void)
+static void init(void)
{
tui_register_command("route", route, route_help);
- return 0;
}
-module_init(init);
+init_call(init);
Modified: trunk/nfsim/tools/time.c
===================================================================
--- trunk/nfsim/tools/time.c 2004-12-10 00:29:06 UTC (rev 3312)
+++ trunk/nfsim/tools/time.c 2004-12-10 05:22:13 UTC (rev 3313)
@@ -97,13 +97,10 @@
}
-static int init(void)
+static void init(void)
{
tui_register_command("time", time_cmd, time_help);
- return 0;
}
+init_call(init);
-
-module_init(init);
-
More information about the netfilter-cvslog
mailing list