Source-Changes-HG archive

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index][Old Index]

[src/bouyer-socketcan]: src/sys/netcan Introduce can_ifattach(), for common s...



details:   https://anonhg.NetBSD.org/src/rev/ba7d49ac0a5f
branches:  bouyer-socketcan
changeset: 820826:ba7d49ac0a5f
user:      bouyer <bouyer%NetBSD.org@localhost>
date:      Sun Feb 05 17:37:10 2017 +0000

description:
Introduce can_ifattach(), for common setups of can interfaces.
Convert output to a ifq and change canloop_output to canloop_ifstart

diffstat:

 sys/netcan/can.c        |  19 +++++++++++---
 sys/netcan/can_var.h    |   3 +-
 sys/netcan/if_canloop.c |  62 ++++++++++++++++++++++--------------------------
 3 files changed, 45 insertions(+), 39 deletions(-)

diffs (186 lines):

diff -r 3bef589ec415 -r ba7d49ac0a5f sys/netcan/can.c
--- a/sys/netcan/can.c  Sun Feb 05 12:18:20 2017 +0000
+++ b/sys/netcan/can.c  Sun Feb 05 17:37:10 2017 +0000
@@ -1,4 +1,4 @@
-/*     $NetBSD: can.c,v 1.1.2.4 2017/02/05 11:45:11 bouyer Exp $       */
+/*     $NetBSD: can.c,v 1.1.2.5 2017/02/05 17:37:10 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.4 2017/02/05 11:45:11 bouyer Exp $");
+__KERNEL_RCSID(0, "$NetBSD: can.c,v 1.1.2.5 2017/02/05 17:37:10 bouyer Exp $");
 
 #include <sys/param.h>
 #include <sys/systm.h>
@@ -45,6 +45,7 @@
 #include <sys/kauth.h>
 
 #include <net/if.h>
+#include <net/if_types.h>
 #include <net/netisr.h>
 #include <net/route.h>
 
@@ -115,6 +116,17 @@
        return 0;
 }
 
+void
+can_ifattach(struct ifnet *ifp) {
+       ifp->if_mtu = sizeof(struct can_frame);
+       ifp->if_type = IFT_OTHER;
+       ifp->if_hdrlen = 0;
+       ifp->if_addrlen = 0;
+       ifp->if_dlt = DLT_CAN_SOCKETCAN;
+       ifp->if_output = NULL; /* unused */
+       IFQ_SET_READY(&ifp->if_snd);
+}
+
 static int
 can_output(struct mbuf *m, struct canpcb *canp)
 {
@@ -143,8 +155,7 @@
 
        if (m->m_len <= ifp->if_mtu) {
                can_output_cnt++;
-               error = (*ifp->if_output)(ifp, m, NULL, 0);
-               return error;
+               return ifq_enqueue(ifp, m);
        } else
                error = EMSGSIZE;
 bad:
diff -r 3bef589ec415 -r ba7d49ac0a5f sys/netcan/can_var.h
--- a/sys/netcan/can_var.h      Sun Feb 05 12:18:20 2017 +0000
+++ b/sys/netcan/can_var.h      Sun Feb 05 17:37:10 2017 +0000
@@ -1,4 +1,4 @@
-/*     $NetBSD: can_var.h,v 1.1.2.3 2017/02/05 11:45:11 bouyer Exp $   */
+/*     $NetBSD: can_var.h,v 1.1.2.4 2017/02/05 17:37:10 bouyer Exp $   */
 
 /*-
  * Copyright (c) 2003, 2017 The NetBSD Foundation, Inc.
@@ -45,6 +45,7 @@
 
 extern const struct pr_usrreqs can_usrreqs;
 
+void can_ifattach(struct ifnet *);
 void can_mbuf_tag_clean(struct mbuf *);
 void can_input(struct ifnet *, struct mbuf *);
 void *can_ctlinput(int, struct sockaddr *, void *);
diff -r 3bef589ec415 -r ba7d49ac0a5f sys/netcan/if_canloop.c
--- a/sys/netcan/if_canloop.c   Sun Feb 05 12:18:20 2017 +0000
+++ b/sys/netcan/if_canloop.c   Sun Feb 05 17:37:10 2017 +0000
@@ -1,4 +1,4 @@
-/*     $NetBSD: if_canloop.c,v 1.1.2.3 2017/02/05 11:45:11 bouyer Exp $        */
+/*     $NetBSD: if_canloop.c,v 1.1.2.4 2017/02/05 17:37:10 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.3 2017/02/05 11:45:11 bouyer Exp $");
+__KERNEL_RCSID(0, "$NetBSD: if_canloop.c,v 1.1.2.4 2017/02/05 17:37:10 bouyer Exp $");
 
 #ifdef _KERNEL_OPT
 #include "opt_can.h"
@@ -69,8 +69,7 @@
 static int     canloop_clone_create(struct if_clone *, int);
 static int     canloop_clone_destroy(struct ifnet *);
 static int     canloop_ioctl(struct ifnet *, u_long, void *);
-static int     canloop_output(struct ifnet *,
-       struct mbuf *, const struct sockaddr *, const struct rtentry *);
+static void    canloop_ifstart(struct ifnet *);
 
 static int     canloop_count;
 
@@ -113,19 +112,13 @@
 
        if_initname(ifp, ifc->ifc_name, unit);
 
-       ifp->if_mtu = sizeof(struct can_frame);
        ifp->if_flags = IFF_LOOPBACK | IFF_RUNNING;
        ifp->if_extflags = IFEF_OUTPUT_MPSAFE;
        ifp->if_ioctl = canloop_ioctl;
-       ifp->if_output = canloop_output;
-       ifp->if_type = IFT_OTHER;
-       ifp->if_hdrlen = 0;
-       ifp->if_addrlen = 0;
-       ifp->if_dlt = DLT_CAN_SOCKETCAN;
-       IFQ_SET_READY(&ifp->if_snd);
+       ifp->if_start = canloop_ifstart;
        if_attach(ifp);
-       if_alloc_sadl(ifp);
-       bpf_attach(ifp, DLT_CAN_SOCKETCAN, sizeof(u_int));
+       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);
@@ -156,41 +149,42 @@
        return (0);
 }
 
-static int
-canloop_output(struct ifnet *ifp, struct mbuf *m, const struct sockaddr *dst,
-    const struct rtentry *rt)
+static void
+canloop_ifstart(struct ifnet *ifp)
 {
-       int error = 0;
        size_t pktlen;
-
-       MCLAIM(m, ifp->if_mowner);
+       struct mbuf *m;
 
        KERNEL_LOCK(1, NULL);
+       while (true) {
+               IF_DEQUEUE(&ifp->if_snd, m);
+               if (m == NULL)
+                       break;
+               MCLAIM(m, ifp->if_mowner);
 
-       if ((m->m_flags & M_PKTHDR) == 0)
-               panic("canloop_output: no header mbuf");
-       if (ifp->if_flags & IFF_LOOPBACK)
-               bpf_mtap_af(ifp, AF_CAN, m);
-       m_set_rcvif(m, ifp);
+               if ((m->m_flags & M_PKTHDR) == 0)
+                       panic("canloop_output: no header mbuf");
+               if (ifp->if_flags & IFF_LOOPBACK)
+                       bpf_mtap_af(ifp, AF_CAN, m);
+               m_set_rcvif(m, ifp);
 
-       pktlen = m->m_pkthdr.len;
-       ifp->if_opackets++;
-       ifp->if_obytes += pktlen;
+               pktlen = m->m_pkthdr.len;
+               ifp->if_opackets++;
+               ifp->if_obytes += pktlen;
 
 #ifdef CAN
-       can_mbuf_tag_clean(m);
-       can_input(ifp, m);
+               can_mbuf_tag_clean(m);
+               can_input(ifp, m);
 #else
-       printf("%s: can't handle CAN packet\n", ifp->if_xname);
-       m_freem(m);
-       error = EAFNOSUPPORT;
+               printf("%s: can't handle CAN packet\n", ifp->if_xname);
+               m_freem(m);
+               error = EAFNOSUPPORT;
 #endif
+       }
 
        KERNEL_UNLOCK_ONE(NULL);
-       return error;
 }
 
-
 /*
  * Process an ioctl request.
  */



Home | Main Index | Thread Index | Old Index