Tue Jul 19 02:47:45 2016 UTC ()
Fix regression introduced in tests/net/bpf and tests/net/bpfilter

The rump code needs to call devsw_attach() in order to assign a dev_major
for bpf;  it then uses this to create rumps /dev/bpf node.  Unfortunately,
this leaves the devsw attached, so when the bpf module tries to initialize
itself, it gets an EEXIST error and fails.

So, once rump has figured what the dev_major should be, call devsw_detach()
to remove the devsw.  Then, when the module initialization code calls
devsw_attach() it will succeed.


(pgoyette)
diff -r1.202 -r1.203 src/sys/net/bpf.c
diff -r1.2 -r1.3 src/sys/rump/dev/lib/libbpf/bpf_component.c

cvs diff -r1.202 -r1.203 src/sys/net/bpf.c (expand / switch to unified diff)

--- src/sys/net/bpf.c 2016/07/17 02:49:52 1.202
+++ src/sys/net/bpf.c 2016/07/19 02:47:45 1.203
@@ -1,14 +1,14 @@ @@ -1,14 +1,14 @@
1/* $NetBSD: bpf.c,v 1.202 2016/07/17 02:49:52 pgoyette Exp $ */ 1/* $NetBSD: bpf.c,v 1.203 2016/07/19 02:47:45 pgoyette Exp $ */
2 2
3/* 3/*
4 * Copyright (c) 1990, 1991, 1993 4 * Copyright (c) 1990, 1991, 1993
5 * The Regents of the University of California. All rights reserved. 5 * The Regents of the University of California. All rights reserved.
6 * 6 *
7 * This code is derived from the Stanford/CMU enet packet filter, 7 * This code is derived from the Stanford/CMU enet packet filter,
8 * (net/enet.c) distributed as part of 4.3BSD, and code contributed 8 * (net/enet.c) distributed as part of 4.3BSD, and code contributed
9 * to Berkeley by Steven McCanne and Van Jacobson both of Lawrence 9 * to Berkeley by Steven McCanne and Van Jacobson both of Lawrence
10 * Berkeley Laboratory. 10 * Berkeley Laboratory.
11 * 11 *
12 * Redistribution and use in source and binary forms, with or without 12 * Redistribution and use in source and binary forms, with or without
13 * modification, are permitted provided that the following conditions 13 * modification, are permitted provided that the following conditions
14 * are met: 14 * are met:
@@ -29,47 +29,46 @@ @@ -29,47 +29,46 @@
29 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 29 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
30 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 30 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
31 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 31 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
32 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 32 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
33 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 33 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
34 * SUCH DAMAGE. 34 * SUCH DAMAGE.
35 * 35 *
36 * @(#)bpf.c 8.4 (Berkeley) 1/9/95 36 * @(#)bpf.c 8.4 (Berkeley) 1/9/95
37 * static char rcsid[] = 37 * static char rcsid[] =
38 * "Header: bpf.c,v 1.67 96/09/26 22:00:52 leres Exp "; 38 * "Header: bpf.c,v 1.67 96/09/26 22:00:52 leres Exp ";
39 */ 39 */
40 40
41#include <sys/cdefs.h> 41#include <sys/cdefs.h>
42__KERNEL_RCSID(0, "$NetBSD: bpf.c,v 1.202 2016/07/17 02:49:52 pgoyette Exp $"); 42__KERNEL_RCSID(0, "$NetBSD: bpf.c,v 1.203 2016/07/19 02:47:45 pgoyette Exp $");
43 43
44#if defined(_KERNEL_OPT) 44#if defined(_KERNEL_OPT)
45#include "opt_bpf.h" 45#include "opt_bpf.h"
46#include "sl.h" 46#include "sl.h"
47#include "strip.h" 47#include "strip.h"
48#endif 48#endif
49 49
50#include <sys/param.h> 50#include <sys/param.h>
51#include <sys/systm.h> 51#include <sys/systm.h>
52#include <sys/mbuf.h> 52#include <sys/mbuf.h>
53#include <sys/buf.h> 53#include <sys/buf.h>
54#include <sys/time.h> 54#include <sys/time.h>
55#include <sys/proc.h> 55#include <sys/proc.h>
56#include <sys/ioctl.h> 56#include <sys/ioctl.h>
57#include <sys/conf.h> 57#include <sys/conf.h>
58#include <sys/vnode.h> 58#include <sys/vnode.h>
59#include <sys/queue.h> 59#include <sys/queue.h>
60#include <sys/stat.h> 60#include <sys/stat.h>
61#include <sys/module.h> 61#include <sys/module.h>
62#include <sys/once.h> 
63#include <sys/atomic.h> 62#include <sys/atomic.h>
64 63
65#include <sys/file.h> 64#include <sys/file.h>
66#include <sys/filedesc.h> 65#include <sys/filedesc.h>
67#include <sys/tty.h> 66#include <sys/tty.h>
68#include <sys/uio.h> 67#include <sys/uio.h>
69 68
70#include <sys/protosw.h> 69#include <sys/protosw.h>
71#include <sys/socket.h> 70#include <sys/socket.h>
72#include <sys/errno.h> 71#include <sys/errno.h>
73#include <sys/kernel.h> 72#include <sys/kernel.h>
74#include <sys/poll.h> 73#include <sys/poll.h>
75#include <sys/sysctl.h> 74#include <sys/sysctl.h>
@@ -392,51 +391,50 @@ bpf_detachd(struct bpf_d *d) @@ -392,51 +391,50 @@ bpf_detachd(struct bpf_d *d)
392 p = &(*p)->bd_next; 391 p = &(*p)->bd_next;
393 if (*p == NULL) 392 if (*p == NULL)
394 panic("%s: descriptor not in list", __func__); 393 panic("%s: descriptor not in list", __func__);
395 } 394 }
396 *p = (*p)->bd_next; 395 *p = (*p)->bd_next;
397 if (bp->bif_dlist == NULL) 396 if (bp->bif_dlist == NULL)
398 /* 397 /*
399 * Let the driver know that there are no more listeners. 398 * Let the driver know that there are no more listeners.
400 */ 399 */
401 *d->bd_bif->bif_driverp = NULL; 400 *d->bd_bif->bif_driverp = NULL;
402 d->bd_bif = NULL; 401 d->bd_bif = NULL;
403} 402}
404 403
405static int 404static void
406doinit(void) 405bpf_init(void)
407{ 406{
408 407
409 mutex_init(&bpf_mtx, MUTEX_DEFAULT, IPL_NONE); 408 mutex_init(&bpf_mtx, MUTEX_DEFAULT, IPL_NONE);
410 409
411 LIST_INIT(&bpf_list); 410 LIST_INIT(&bpf_list);
412 411
413 bpf_gstats.bs_recv = 0; 412 bpf_gstats.bs_recv = 0;
414 bpf_gstats.bs_drop = 0; 413 bpf_gstats.bs_drop = 0;
415 bpf_gstats.bs_capt = 0; 414 bpf_gstats.bs_capt = 0;
416 415
417 return 0; 416 return;
418} 417}
419 418
420/* 419/*
421 * bpfilterattach() is called at boot time. 420 * bpfilterattach() is called at boot time. We don't need to do anything
 421 * here, since any initialization will happen as part of module init code.
422 */ 422 */
423/* ARGSUSED */ 423/* ARGSUSED */
424void 424void
425bpfilterattach(int n) 425bpfilterattach(int n)
426{ 426{
427 static ONCE_DECL(control); 
428 427
429 RUN_ONCE(&control, doinit); 
430} 428}
431 429
432/* 430/*
433 * Open ethernet device. Clones. 431 * Open ethernet device. Clones.
434 */ 432 */
435/* ARGSUSED */ 433/* ARGSUSED */
436int 434int
437bpfopen(dev_t dev, int flag, int mode, struct lwp *l) 435bpfopen(dev_t dev, int flag, int mode, struct lwp *l)
438{ 436{
439 struct bpf_d *d; 437 struct bpf_d *d;
440 struct file *fp; 438 struct file *fp;
441 int error, fd; 439 int error, fd;
442 440
@@ -2107,37 +2105,36 @@ struct bpf_ops bpf_ops_kernel = { @@ -2107,37 +2105,36 @@ struct bpf_ops bpf_ops_kernel = {
2107 .bpf_mtap_sl_out = _bpf_mtap_sl_out, 2105 .bpf_mtap_sl_out = _bpf_mtap_sl_out,
2108}; 2106};
2109 2107
2110MODULE(MODULE_CLASS_DRIVER, bpf, "bpf_filter"); 2108MODULE(MODULE_CLASS_DRIVER, bpf, "bpf_filter");
2111 2109
2112static int 2110static int
2113bpf_modcmd(modcmd_t cmd, void *arg) 2111bpf_modcmd(modcmd_t cmd, void *arg)
2114{ 2112{
2115#ifdef _MODULE 2113#ifdef _MODULE
2116 devmajor_t bmajor, cmajor; 2114 devmajor_t bmajor, cmajor;
2117#endif 2115#endif
2118 int error = 0; 2116 int error = 0;
2119 2117
2120 
2121 switch (cmd) { 2118 switch (cmd) {
2122 case MODULE_CMD_INIT: 2119 case MODULE_CMD_INIT:
2123 bpfilterattach(0); 2120 bpf_init();
2124#ifdef _MODULE 2121#ifdef _MODULE
2125 bmajor = cmajor = NODEVMAJOR; 2122 bmajor = cmajor = NODEVMAJOR;
2126 error = devsw_attach("bpf", NULL, &bmajor, 2123 error = devsw_attach("bpf", NULL, &bmajor,
2127 &bpf_cdevsw, &cmajor); 2124 &bpf_cdevsw, &cmajor);
2128#endif 
2129 if (error) 2125 if (error)
2130 break; 2126 break;
 2127#endif
2131 2128
2132 bpf_ops_handover_enter(&bpf_ops_kernel); 2129 bpf_ops_handover_enter(&bpf_ops_kernel);
2133 atomic_swap_ptr(&bpf_ops, &bpf_ops_kernel); 2130 atomic_swap_ptr(&bpf_ops, &bpf_ops_kernel);
2134 bpf_ops_handover_exit(); 2131 bpf_ops_handover_exit();
2135 sysctl_net_bpf_setup(); 2132 sysctl_net_bpf_setup();
2136 break; 2133 break;
2137 2134
2138 case MODULE_CMD_FINI: 2135 case MODULE_CMD_FINI:
2139 /* 2136 /*
2140 * While there is no reference counting for bpf callers, 2137 * While there is no reference counting for bpf callers,
2141 * unload could at least in theory be done similarly to  2138 * unload could at least in theory be done similarly to
2142 * system call disestablishment. This should even be 2139 * system call disestablishment. This should even be
2143 * a little simpler: 2140 * a little simpler:

cvs diff -r1.2 -r1.3 src/sys/rump/dev/lib/libbpf/bpf_component.c (expand / switch to unified diff)

--- src/sys/rump/dev/lib/libbpf/bpf_component.c 2016/01/26 23:12:14 1.2
+++ src/sys/rump/dev/lib/libbpf/bpf_component.c 2016/07/19 02:47:45 1.3
@@ -1,14 +1,14 @@ @@ -1,14 +1,14 @@
1/* $NetBSD: bpf_component.c,v 1.2 2016/01/26 23:12:14 pooka Exp $ */ 1/* $NetBSD: bpf_component.c,v 1.3 2016/07/19 02:47:45 pgoyette Exp $ */
2 2
3/* 3/*
4 * Copyright (c) 2010 Antti Kantee. All Rights Reserved. 4 * Copyright (c) 2010 Antti Kantee. All Rights Reserved.
5 * 5 *
6 * Redistribution and use in source and binary forms, with or without 6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions 7 * modification, are permitted provided that the following conditions
8 * are met: 8 * are met:
9 * 1. Redistributions of source code must retain the above copyright 9 * 1. Redistributions of source code must retain the above copyright
10 * notice, this list of conditions and the following disclaimer. 10 * notice, this list of conditions and the following disclaimer.
11 * 2. Redistributions in binary form must reproduce the above copyright 11 * 2. Redistributions in binary form must reproduce the above copyright
12 * notice, this list of conditions and the following disclaimer in the 12 * notice, this list of conditions and the following disclaimer in the
13 * documentation and/or other materials provided with the distribution. 13 * documentation and/or other materials provided with the distribution.
14 * 14 *
@@ -16,38 +16,40 @@ @@ -16,38 +16,40 @@
16 * OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED 16 * OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
17 * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE 17 * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
18 * DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE 18 * DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
19 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 19 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
20 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR 20 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
21 * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 21 * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
22 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 22 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
23 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 23 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
24 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 24 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
25 * SUCH DAMAGE. 25 * SUCH DAMAGE.
26 */ 26 */
27 27
28#include <sys/cdefs.h> 28#include <sys/cdefs.h>
29__KERNEL_RCSID(0, "$NetBSD: bpf_component.c,v 1.2 2016/01/26 23:12:14 pooka Exp $"); 29__KERNEL_RCSID(0, "$NetBSD: bpf_component.c,v 1.3 2016/07/19 02:47:45 pgoyette Exp $");
30 30
31#include <sys/param.h> 31#include <sys/param.h>
32#include <sys/conf.h> 32#include <sys/conf.h>
33#include <sys/device.h> 33#include <sys/device.h>
34#include <sys/mbuf.h> 34#include <sys/mbuf.h>
35#include <sys/stat.h> 35#include <sys/stat.h>
36 36
37#include <net/bpf.h> 37#include <net/bpf.h>
38 38
39#include <rump-sys/kern.h> 39#include <rump-sys/kern.h>
40#include <rump-sys/vfs.h> 40#include <rump-sys/vfs.h>
41 41
42RUMP_COMPONENT(RUMP_COMPONENT_NET) 42RUMP_COMPONENT(RUMP_COMPONENT_NET)
43{ 43{
44 extern const struct cdevsw bpf_cdevsw; 44 extern const struct cdevsw bpf_cdevsw;
45 devmajor_t bmaj, cmaj; 45 devmajor_t bmaj, cmaj;
46 int error; 46 int error;
47 47
48 bmaj = cmaj = NODEVMAJOR; 48 bmaj = cmaj = NODEVMAJOR;
49 if ((error = devsw_attach("bpf", NULL, &bmaj, &bpf_cdevsw, &cmaj)) != 0) 49 if ((error = devsw_attach("bpf", NULL, &bmaj, &bpf_cdevsw, &cmaj)) != 0)
50 panic("bpf devsw attach failed: %d", error); 50 panic("bpf devsw attach failed: %d", error);
51 if ((error = rump_vfs_makeonedevnode(S_IFCHR, "/dev/bpf", cmaj, 0)) !=0) 51 if ((error = rump_vfs_makeonedevnode(S_IFCHR, "/dev/bpf", cmaj, 0)) !=0)
52 panic("cannot create bpf device nodes: %d", error); 52 panic("cannot create bpf device nodes: %d", error);
 53 if ((error = devsw_detach(NULL, &bpf_cdevsw)) != 0)
 54 panic("cannot detach bpf devsw: %d", error);
53} 55}