[PATCH] Add ioctl to disable bpf timestamping

Bruce M Simpson bms at spc.org
Wed Sep 8 02:26:32 PDT 2004


Here's a patch against 5.3 to add a per-instance switch which allows
the user to specify if captured packets should be timestamped (and,
if so, whether microtime() or the faster but less accurate
getmicrotime() call should be used).

Comments/flames/etc to the usual...

BMS
-------------- next part --------------
Index: bpf.c
===================================================================
RCS file: /home/ncvs/src/sys/net/bpf.c,v
retrieving revision 1.130
diff -u -p -r1.130 bpf.c
--- bpf.c	5 Jul 2004 16:28:31 -0000	1.130
+++ bpf.c	8 Sep 2004 09:22:09 -0000
@@ -347,6 +347,7 @@ bpfopen(dev, flags, fmt, td)
 	d->bd_bufsize = bpf_bufsize;
 	d->bd_sig = SIGIO;
 	d->bd_seesent = 1;
+	d->bd_tstamp = BPF_TSTAMP_MICROTIME;
 #ifdef MAC
 	mac_init_bpfdesc(d);
 	mac_create_bpfdesc(td->td_ucred, d);
@@ -629,6 +630,7 @@ reset_d(d)
  *  BIOCSHDRCMPLT	Set "header already complete" flag
  *  BIOCGSEESENT	Get "see packets sent" flag
  *  BIOCSSEESENT	Set "see packets sent" flag
+ *  BIOCSTSTAMP		Set "timestamp preference" flag
  */
 /* ARGSUSED */
 static	int
@@ -880,6 +882,13 @@ bpfioctl(dev, cmd, addr, flags, td)
 		d->bd_seesent = *(u_int *)addr;
 		break;
 
+	/*
+	 * Set "timestamp preference" flag
+	 */
+	case BIOCSTSTAMP:
+		d->bd_tstamp = *(u_int *)addr;
+		break;
+
 	case FIONBIO:		/* Non-blocking I/O */
 		break;
 
@@ -1331,7 +1340,14 @@ catchpacket(d, pkt, pktlen, snaplen, cpf
 	 * Append the bpf header.
 	 */
 	hp = (struct bpf_hdr *)(d->bd_sbuf + curlen);
-	microtime(&hp->bh_tstamp);
+	/*
+	 * Only prepend a timestamp if instructed to do so.
+	 * getmicrotime() is less accurate but potentially faster.
+	 */
+	if (d->bd_tstamp == BPF_TSTAMP_MICROTIME)
+		microtime(&hp->bh_tstamp);
+	else if (d->bd_tstamp == BPF_TSTAMP_GETMICROTIME)
+		getmicrotime(&hp->bh_tstamp);
 	hp->bh_datalen = pktlen;
 	hp->bh_hdrlen = hdrlen;
 	/*
Index: bpf.h
===================================================================
RCS file: /home/ncvs/src/sys/net/bpf.h,v
retrieving revision 1.36
diff -u -p -r1.36 bpf.h
--- bpf.h	30 May 2004 17:03:48 -0000	1.36
+++ bpf.h	8 Sep 2004 09:15:35 -0000
@@ -113,6 +113,7 @@ struct bpf_version {
 #define BIOCSSEESENT	_IOW('B',119, u_int)
 #define	BIOCSDLT	_IOW('B',120, u_int)
 #define	BIOCGDLTLIST	_IOWR('B',121, struct bpf_dltlist)
+#define	BIOCSTSTAMP	_IOW('B',122, u_int)
 
 /*
  * Structure prepended to each packet.
@@ -491,4 +492,13 @@ u_int	 bpf_filter(const struct bpf_insn 
  */
 #define BPF_MEMWORDS 16
 
+/*
+ * Defines to specify timestamping preference for a BPF instance.
+ * BPF_TSTAMP_MICROTIME is the default when none is specified.
+ * Other settings may increase BPF capture performance under load.
+ */
+#define BPF_TSTAMP_NONE			0x00	/* Don't timestamp packets */
+#define BPF_TSTAMP_MICROTIME		0x01	/* Use microtime() (slower) */
+#define BPF_TSTAMP_GETMICROTIME		0x02	/* Use getmicrotime() */
+
 #endif /* _NET_BPF_H_ */
Index: bpfdesc.h
===================================================================
RCS file: /home/ncvs/src/sys/net/bpfdesc.h,v
retrieving revision 1.27
diff -u -p -r1.27 bpfdesc.h
--- bpfdesc.h	7 Apr 2004 20:46:11 -0000	1.27
+++ bpfdesc.h	8 Sep 2004 09:17:16 -0000
@@ -73,6 +73,7 @@ struct bpf_d {
 	u_char		bd_promisc;	/* true if listening promiscuously */
 	u_char		bd_state;	/* idle, waiting, or timed out */
 	u_char		bd_immediate;	/* true to return on packet arrival */
+	u_char		bd_tstamp;	/* timestamping preferences */
 	int		bd_hdrcmplt;	/* false to fill in src lladdr automatically */
 	int		bd_seesent;	/* true if bpf should see sent packets */
 	int		bd_async;	/* non-zero if packet reception should generate signal */
-------------- next part --------------
A non-text attachment was scrubbed...
Name: not available
Type: application/pgp-signature
Size: 167 bytes
Desc: not available
Url : http://lists.freebsd.org/pipermail/freebsd-net/attachments/20040908/16a029bb/attachment.bin


More information about the freebsd-net mailing list