Source-Changes-HG archive
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index][Old Index]
[src/bouyer-socketcan]: src/sys factor out if_attach() and bpf_attach() calls...
details: https://anonhg.NetBSD.org/src/rev/f38895404d01
branches: bouyer-socketcan
changeset: 820908:f38895404d01
user: bouyer <bouyer%NetBSD.org@localhost>
date: Mon May 22 16:11:23 2017 +0000
description:
factor out if_attach() and bpf_attach() calls in can_ifattach().
Introduce can_ifdetach().
Introduce can_bpf_mtap(), which converts the can_id to network byte
order (as required by tcpdump/wireshark). Thanks to Guy Harris for
poiting this requirement.
diffstat:
sys/arch/arm/allwinner/awin_can.c | 11 ++++-------
sys/netcan/can.c | 31 ++++++++++++++++++++++++++++---
sys/netcan/can_var.h | 4 +++-
sys/netcan/if_canloop.c | 12 ++++--------
4 files changed, 39 insertions(+), 19 deletions(-)
diffs (207 lines):
diff -r af619228d69a -r f38895404d01 sys/arch/arm/allwinner/awin_can.c
--- a/sys/arch/arm/allwinner/awin_can.c Thu May 18 11:29:54 2017 +0000
+++ b/sys/arch/arm/allwinner/awin_can.c Mon May 22 16:11:23 2017 +0000
@@ -1,4 +1,4 @@
-/* $NetBSD: awin_can.c,v 1.1.2.5 2017/04/21 13:08:55 bouyer Exp $ */
+/* $NetBSD: awin_can.c,v 1.1.2.6 2017/05/22 16:11:23 bouyer Exp $ */
/*-
* Copyright (c) 2017 The NetBSD Foundation, Inc.
@@ -36,7 +36,7 @@
#include <sys/cdefs.h>
-__KERNEL_RCSID(1, "$NetBSD: awin_can.c,v 1.1.2.5 2017/04/21 13:08:55 bouyer Exp $");
+__KERNEL_RCSID(1, "$NetBSD: awin_can.c,v 1.1.2.6 2017/05/22 16:11:23 bouyer Exp $");
#include <sys/param.h>
#include <sys/bus.h>
@@ -55,7 +55,6 @@
#include <netcan/can.h>
#include <netcan/can_var.h>
#endif
-#include <net/bpf.h>
#include <arm/allwinner/awin_reg.h>
#include <arm/allwinner/awin_var.h>
@@ -209,9 +208,7 @@
/*
* Attach the interface.
*/
- if_attach(ifp);
can_ifattach(ifp);
- bpf_attach(ifp, DLT_CAN_SOCKETCAN, 0);
rnd_attach_source(&sc->sc_rnd_source, device_xname(self),
RND_TYPE_NET, RND_FLAG_DEFAULT);
#ifdef MBUFTRACE
@@ -278,7 +275,7 @@
ifp->if_ipackets++;
ifp->if_ibytes += m->m_len;
m_set_rcvif(m, ifp);
- bpf_mtap(ifp, m);
+ can_bpf_mtap(ifp, m);
can_input(ifp, m);
}
@@ -352,7 +349,7 @@
}
ifp->if_flags |= IFF_OACTIVE;
ifp->if_timer = 5;
- bpf_mtap(ifp, m);
+ can_bpf_mtap(ifp, m);
}
static int
diff -r af619228d69a -r f38895404d01 sys/netcan/can.c
--- a/sys/netcan/can.c Thu May 18 11:29:54 2017 +0000
+++ b/sys/netcan/can.c Mon May 22 16:11:23 2017 +0000
@@ -1,4 +1,4 @@
-/* $NetBSD: can.c,v 1.1.2.13 2017/04/23 21:05:09 bouyer Exp $ */
+/* $NetBSD: can.c,v 1.1.2.14 2017/05/22 16:11:23 bouyer Exp $ */
/*-
* Copyright (c) 2003, 2017 The NetBSD Foundation, Inc.
@@ -30,7 +30,7 @@
*/
#include <sys/cdefs.h>
-__KERNEL_RCSID(0, "$NetBSD: can.c,v 1.1.2.13 2017/04/23 21:05:09 bouyer Exp $");
+__KERNEL_RCSID(0, "$NetBSD: can.c,v 1.1.2.14 2017/05/22 16:11:23 bouyer Exp $");
#include <sys/param.h>
#include <sys/systm.h>
@@ -48,6 +48,7 @@
#include <net/if_types.h>
#include <net/netisr.h>
#include <net/route.h>
+#include <net/bpf.h>
#include <netcan/can.h>
#include <netcan/can_pcb.h>
@@ -192,7 +193,9 @@
}
void
-can_ifattach(struct ifnet *ifp) {
+can_ifattach(struct ifnet *ifp)
+{
+ if_attach(ifp);
ifp->if_mtu = sizeof(struct can_frame);
ifp->if_type = IFT_OTHER;
ifp->if_hdrlen = 0;
@@ -201,6 +204,14 @@
ifp->if_output = NULL; /* unused */
IFQ_SET_READY(&ifp->if_snd);
if_alloc_sadl(ifp);
+ bpf_attach(ifp, DLT_CAN_SOCKETCAN, 0);
+}
+
+void
+can_ifdetach(struct ifnet *ifp)
+{
+ bpf_detach(ifp);
+ if_detach(ifp);
}
void
@@ -404,6 +415,20 @@
mutex_exit(softnet_lock);
}
+void
+can_bpf_mtap(struct ifnet *ifp, struct mbuf *m)
+{
+ /* bpf wants the CAN id in network byte order */
+ struct can_frame *cf;
+ canid_t oid;
+
+ cf = mtod(m, struct can_frame *);
+ oid = cf->can_id;
+ cf->can_id = htonl(oid);
+ bpf_mtap(ifp, m);
+ cf->can_id = oid;
+}
+
static int
can_attach(struct socket *so, int proto)
{
diff -r af619228d69a -r f38895404d01 sys/netcan/can_var.h
--- a/sys/netcan/can_var.h Thu May 18 11:29:54 2017 +0000
+++ b/sys/netcan/can_var.h Mon May 22 16:11:23 2017 +0000
@@ -1,4 +1,4 @@
-/* $NetBSD: can_var.h,v 1.1.2.7 2017/04/18 21:29:40 bouyer Exp $ */
+/* $NetBSD: can_var.h,v 1.1.2.8 2017/05/22 16:11:23 bouyer Exp $ */
/*-
* Copyright (c) 2003, 2017 The NetBSD Foundation, Inc.
@@ -60,6 +60,7 @@
extern const struct pr_usrreqs can_usrreqs;
void can_ifattach(struct ifnet *);
+void can_ifdetach(struct ifnet *);
void can_ifinit_timings(struct canif_softc *);
void can_mbuf_tag_clean(struct mbuf *);
void can_input(struct ifnet *, struct mbuf *);
@@ -67,6 +68,7 @@
int can_ctloutput(int, struct socket *, struct sockopt *);
void can_init(void);
void canintr(void);
+void can_bpf_mtap(struct ifnet *, struct mbuf *);
#endif
diff -r af619228d69a -r f38895404d01 sys/netcan/if_canloop.c
--- a/sys/netcan/if_canloop.c Thu May 18 11:29:54 2017 +0000
+++ b/sys/netcan/if_canloop.c Mon May 22 16:11:23 2017 +0000
@@ -1,4 +1,4 @@
-/* $NetBSD: if_canloop.c,v 1.1.2.6 2017/04/24 13:38:33 bouyer Exp $ */
+/* $NetBSD: if_canloop.c,v 1.1.2.7 2017/05/22 16:11:23 bouyer Exp $ */
/*-
* Copyright (c) 2017 The NetBSD Foundation, Inc.
@@ -35,7 +35,7 @@
*/
#include <sys/cdefs.h>
-__KERNEL_RCSID(0, "$NetBSD: if_canloop.c,v 1.1.2.6 2017/04/24 13:38:33 bouyer Exp $");
+__KERNEL_RCSID(0, "$NetBSD: if_canloop.c,v 1.1.2.7 2017/05/22 16:11:23 bouyer Exp $");
#ifdef _KERNEL_OPT
#include "opt_can.h"
@@ -62,7 +62,6 @@
#ifdef CAN
#include <netcan/can.h>
#endif
-#include <net/bpf.h>
void canloopattach(int);
void canloopinit(void);
@@ -116,9 +115,7 @@
ifp->if_extflags = IFEF_OUTPUT_MPSAFE;
ifp->if_ioctl = canloop_ioctl;
ifp->if_start = canloop_ifstart;
- if_attach(ifp);
can_ifattach(ifp);
- bpf_attach(ifp, DLT_CAN_SOCKETCAN, 0);
#ifdef MBUFTRACE
ifp->if_mowner = malloc(sizeof(struct mowner), M_DEVBUF,
M_WAITOK | M_ZERO);
@@ -140,8 +137,7 @@
free(ifp->if_mowner, M_DEVBUF);
#endif
- bpf_detach(ifp);
- if_detach(ifp);
+ can_ifdetach(ifp);
if_free(ifp);
canloop_count--;
@@ -166,7 +162,7 @@
panic("canloop_output: no header mbuf");
m_set_rcvif(m, ifp);
if (ifp->if_flags & IFF_LOOPBACK)
- bpf_mtap(ifp, m);
+ can_bpf_mtap(ifp, m);
pktlen = m->m_pkthdr.len;
ifp->if_opackets++;
Home |
Main Index |
Thread Index |
Old Index