Skip to contentSkip to conversation

usbmon: add binary interface

PATCH In review Paolo Abeni 7 participants 11 messages Oct 11, 2006 – Oct 18, 2006 USB

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 usbmon: add binary interface

From: Paolo Abeni <[email protected]>

A binary interface is added to usbmon. For each USB bus present on the host system a new file is added to the debugfs directory, in the form "usb%db".

USB records are stored in a liked list, alike current text interface implementation, so most code is shared from binary and text interface. This code has been moved into mon_commom.c.

The binary interface support resizing the amount of USB data stored in each record via an ioctl method.

---
patch against linux 2.6.18

 Makefile     |    2
 mon_binary.c |  203 ++++++++++++++++++++++++++++++++++++
 mon_common.c |  329 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
 mon_main.c   |   13 ++
 mon_stat.c   |    4
 mon_text.c   |  319 +++++++--------------------------------------------------
 usb_mon.h    |   46 ++++++++
drivers/usb/mon/Makefile +1 -1
diff -uprN -X linux-2.6.18-vanilla/Documentation/dontdiff linux-2.6.18-vanilla/drivers/usb/mon/Makefile linux-2.6.18-monioctl/drivers/usb/mon/Makefile
@@ -2,7 +2,7 @@
 # Makefile for USB Core files and filesystem
 #
 
-usbmon-objs	:= mon_main.o mon_stat.o mon_text.o mon_dma.o
+usbmon-objs	:= mon_main.o mon_stat.o mon_text.o mon_dma.o mon_common.o mon_binary.o
 
 # This does not use CONFIG_USB_MON because we want this to use a tristate.
 obj-$(CONFIG_USB)	+= usbmon.o
drivers/usb/mon/mon_binary.c +203 -0

203 lines not on this page · raw message

drivers/usb/mon/mon_common.c +329 -0

329 lines not on this page · raw message

drivers/usb/mon/mon_main.c +13 -0
diff -uprN -X linux-2.6.18-vanilla/Documentation/dontdiff linux-2.6.18-vanilla/drivers/usb/mon/mon_main.c linux-2.6.18-monioctl/drivers/usb/mon/mon_main.c
@@ -214,6 +214,7 @@ static void mon_bus_remove(struct usb_bu
 	list_del(&mbus->bus_link);
 	debugfs_remove(mbus->dent_t);
 	debugfs_remove(mbus->dent_s);
+	debugfs_remove(mbus->dent_b);
 
 	mon_dissolve(mbus, ubus);
 	kref_put(&mbus->ref, mon_bus_drop);
@@ -320,11 +321,22 @@ static void mon_bus_init(struct dentry *
 		goto err_create_s;
 	mbus->dent_s = d;
 
+	rc = snprintf(name, NAMESZ, "%db", ubus->busnum);
+	if (rc <= 0 || rc >= NAMESZ)
+		goto err_print_b;
+	d = debugfs_create_file(name, 0600, mondir, mbus, &mon_fops_binary);
+	if (d == NULL)
+		goto err_create_b;
+	mbus->dent_b = d;
+
 	mutex_lock(&mon_lock);
 	list_add_tail(&mbus->bus_link, &mon_buses);
 	mutex_unlock(&mon_lock);
 	return;
 
+err_create_b:
+err_print_b:
+	debugfs_remove(mbus->dent_s);
 err_create_s:
 err_print_s:
 	debugfs_remove(mbus->dent_t);
@@ -384,6 +396,7 @@ static void __exit mon_exit(void)
 
 		debugfs_remove(mbus->dent_t);
 		debugfs_remove(mbus->dent_s);
+		debugfs_remove(mbus->dent_b);
 
 		/*
 		 * This never happens, because the open/close paths in
drivers/usb/mon/mon_stat.c +2 -2
diff -uprN -X linux-2.6.18-vanilla/Documentation/dontdiff linux-2.6.18-vanilla/drivers/usb/mon/mon_stat.c linux-2.6.18-monioctl/drivers/usb/mon/mon_stat.c
@@ -31,8 +31,8 @@ static int mon_stat_open(struct inode *i
 	mbus = inode->u.generic_ip;
 
 	sp->slen = snprintf(sp->str, STAT_BUF_SIZE,
-	    "nreaders %d events %u text_lost %u\n",
-	    mbus->nreaders, mbus->cnt_events, mbus->cnt_text_lost);
+	    "nreaders %d events %u lost %u\n",
+	    mbus->nreaders, mbus->cnt_events, mbus->cnt_lost);
 
 	file->private_data = sp;
 	return 0;
drivers/usb/mon/mon_text.c +42 -277

452 lines not on this page · raw message

drivers/usb/mon/usb_mon.h +45 -1

70 lines not on this page · raw message

--
Email.it, the professional e-mail, gratis per te: http://www.email.it/f

Sponsor:
Acquista i tuoi gioielli in tutta sicurezza ed a prezzi veramente imbattibili. Sfoglia il nostro catalogo on-line! Clicca qui: http://adv.email.it/cgi-bin/foclick.cgi?mid=5634&d=11-10

--
Email.it, the professional e-mail, gratis per te: http://www.email.it/f

Sponsor:
Refill s.r.l. - Tutto per la tua stampante a prezzi incredibili: su cartucce, toner, inchiostri, carta speciale risparmi fino al 90%! Clicca qui: http://adv.email.it/cgi-bin/foclick.cgi?mid=5189&d=11-10

#1 lore raw
Quoted 8 lines · Paolo Abeni

From: Paolo Abeni <[email protected]>

A binary interface is added to usbmon. For each USB bus present on the host system a new file is added to the debugfs directory, in the form "usb%db".

USB records are stored in a liked list, alike current text interface implementation, so most code is shared from binary and text interface. This code has been moved into mon_commom.c.

The binary interface support resizing the amount of USB data stored in each record via an ioctl method.

You forgot to update Documentation/usb/usbmon.txt.

Alan Stern

#2 lore raw
In reply to Paolo Abeni

Hi!

Quoted 8 lines

From: Paolo Abeni <[email protected]>

A binary interface is added to usbmon. For each USB bus present on the host system a new file is added to the debugfs directory, in the form "usb%db".

USB records are stored in a liked list, alike current text interface implementation, so most code is shared from binary and text interface. This code has been moved into mon_commom.c.

The binary interface support resizing the amount of USB data stored in each record via an ioctl method.

Does it mean text interface is now deprecated? Or perhaps ioctl should be added to text interface too? Or maybe we do not need binary interface if we allow resizing on text interface?

Pavel

Signature · 1 line
Thanks for all the (sleeping) penguins.
#3 lore raw
Quoted 3 lines · Pavel Machek

Does it mean text interface is now deprecated? Or perhaps ioctl should be added to text interface too? Or maybe we do not need binary interface if we allow resizing on text interface?

I haven't reviewed Paolo's patch yet, but with that in mind:
- No, text is not deprecated yet. That is only possible when a simplified command-line tool is written and distributed (e.g. usbmon(8)).
- No, I do not think an ioctl in debugfs or a text API is a good idea.
- Resizing on text interface magnifies sprintf contribution to CPU burn, so once we have the binary one, there's only disadvantage and no advantage in implementing that.

-- Pete

#4 lore raw
Quoted 6 lines · Pete Zaitcev
Quoted 3 lines · Pavel Machek

Does it mean text interface is now deprecated? Or perhaps ioctl should be added to text interface too? Or maybe we do not need binary interface if we allow resizing on text interface?

Quoted 7 lines

I haven't reviewed Paolo's patch yet, but with that in mind:
- No, text is not deprecated yet. That is only possible when a simplified command-line tool is written and distributed (e.g. usbmon(8)).
- No, I do not think an ioctl in debugfs or a text API is a good idea.
- Resizing on text interface magnifies sprintf contribution to CPU burn, so once we have the binary one, there's only disadvantage and no advantage in implementing that.

Would relayfs be a better choice than debugfs for exporting potentially large quantities of binary data?

Alan Stern

#5 lore raw
Quoted 16 lines · Alan Stern
Quoted 6 lines · Pete Zaitcev
Quoted 3 lines · Pavel Machek

Does it mean text interface is now deprecated? Or perhaps ioctl should be added to text interface too? Or maybe we do not need binary interface if we allow resizing on text interface?

Quoted 7 lines

I haven't reviewed Paolo's patch yet, but with that in mind:
- No, text is not deprecated yet. That is only possible when a simplified command-line tool is written and distributed (e.g. usbmon(8)).
- No, I do not think an ioctl in debugfs or a text API is a good idea.
- Resizing on text interface magnifies sprintf contribution to CPU burn, so once we have the binary one, there's only disadvantage and no advantage in implementing that.

Quoted 2 lines

Would relayfs be a better choice than debugfs for exporting potentially large quantities of binary data?

You can put relayfs files in debugfs. Or at least that was the goal a long time ago, hopefully it still works...

thanks,

greg k-h

#6 lore raw
In reply to Alan Stern
Quoted 2 lines · Alan Stern

Would relayfs be a better choice than debugfs for exporting potentially large quantities of binary data?

I'm sick of mounting them, so for the binary API I was going to create a bunch of character devices with a dynamic major. With udev, I do not even need to read /proc/devices myself.

Curiously enough, Marcel Holtmann argued for a device because he did NOT want to run udev. Funny how that works.

-- Pete

#7 lore raw

Hi Pete,

Quoted 9 lines
Quoted 2 lines

Would relayfs be a better choice than debugfs for exporting potentially large quantities of binary data?

I'm sick of mounting them, so for the binary API I was going to create a bunch of character devices with a dynamic major. With udev, I do not even need to read /proc/devices myself.

Curiously enough, Marcel Holtmann argued for a device because he did NOT want to run udev. Funny how that works.

can't remember that I said that. My concern was that distros might not compile debugfs and so usbmon would have been useless. Some character devices seems the right approach to me. The only concern with a dynamic major might be USB debugging in an early time of the boot process, but that seems to be really rare.

Regards

Marcel

#8 lore raw
In reply to Pete Zaitcev
Quoted 6 lines · Pete Zaitcev
Quoted 3 lines · Pavel Machek

Does it mean text interface is now deprecated? Or perhaps ioctl should be added to text interface too? Or maybe we do not need binary interface if we allow resizing on text interface?

Quoted 7 lines

I haven't reviewed Paolo's patch yet, but with that in mind:
- No, text is not deprecated yet. That is only possible when a simplified command-line tool is written and distributed (e.g. usbmon(8)).
- No, I do not think an ioctl in debugfs or a text API is a good idea.
- Resizing on text interface magnifies sprintf contribution to CPU burn, so once we have the binary one, there's only disadvantage and no advantage in implementing that.

By the way, any reason why CONFIG_USB_MON doesn't select CONFIG_DEBUG_FS? Is usbmon useful without debugfs?

Ciao,

Duncan.

#9 lore raw
Quoted 2 lines · Duncan Sands

By the way, any reason why CONFIG_USB_MON doesn't select CONFIG_DEBUG_FS? Is usbmon useful without debugfs?

It's not now, but it will be once I and Paolo are done here.

-- Pete

#10 lore raw
In reply to Paolo Abeni

On Wed, 11 Oct 2006 10:57:45 +0200, Paolo Abeni <[email protected]> wrote:

In the patch I see these main issues: object layout, and API design. The rest is minor. I am going to use most of the patch and fix it up. Unfortunately, it's not going to be binary compatible with your API.

* Objects are all screwed up

Old hierarchy:
mon_reader
mon_reader_text

New:
mon_reader
mon_reader_list
mon_reader_text

And the intent was:
mon_reader
mon_reader_text
mon_reader_bin

In the patch, text reader inherits from binary, which obviously reflects your viewpoint about the supremacy of binary. This is a matter of taste, obviously. I prefer the symmetry.

* API

I think that reading structured data from read(2) is rather dirty. This is what ioctl is for.

But even then, there were some mistakes. I'm going to point them out for the record. Look at the data layout and the read function:

Quoted 47 lines
(quoted diff) +45 -0
+struct mon_event_hdr {
+	s32 type;	/* submit, complete, etc. */
+	u32 pipe;	/* Pipe */
+	u64 id;	        /* Incremental id */
+	struct timeval tstamp;
+	u32 length;	/* Depends on type: xfer length or act length */
+	s32 status;
+	s8 setup_flag;
+	s8 data_flag;
+};
+
+struct mon_event_list {
+	struct list_head e_link;
+        struct mon_event_hdr hdr;
+	unsigned char setup[SETUP_MAX];
+	unsigned char* data;
+};
 
+static ssize_t mon_binary_read(struct file *file, char __user *buf,
+				size_t nbytes, loff_t *ppos)
+{
+	struct mon_event_list *ep;
 
+	/* check out how much URB data is present in this buffer*/
+	if ((ep->hdr.length > 0) && (ep->hdr.data_flag == 0))
+		data_len = (ep->hdr.length > rp->data_max) ? rp->data_max: 
+								ep->hdr.length;
+	
+	/* avoid overrun user buffer; copy as much data as possible*/
+	hdr_bytes = sizeof(struct mon_event_list) > nbytes ? nbytes: 
+						sizeof(struct mon_event_list);
+	nbytes -= hdr_bytes;
+	if (ep->hdr.setup_flag == 0)
+		setup_len = SETUP_MAX;
+	setup_bytes =  setup_len > nbytes ? nbytes : setup_len;
+	nbytes -= setup_bytes;
+	data_bytes = data_len > nbytes ? nbytes: data_len;
+	
+	cnt = hdr_bytes + setup_bytes + data_bytes;
+	if (copy_to_user(buf, ep, hdr_bytes))
+	{
+		cnt = -EFAULT;
+		goto out;
+	}
+	if ((setup_bytes > 0) && copy_to_user(buf+hdr_bytes, ep->setup, 
+								setup_bytes))
+	{

Review

Quoted 10 lines
(quoted diff) +10 -0
+		cnt = -EFAULT;
+		goto out;
+	}
+	if ((data_bytes > 0) && copy_to_user(buf+hdr_bytes, ep->data, 
+								data_bytes))
+		cnt = -EFAULT;
+out:        
+	kmem_cache_free(rp->e_slab, ep);
+	return cnt;
+}

- Leaving data_flag in the header makes no sense, because length is included.

- Setup packet is copied out twice.

- Internal kernel pointers are needlessly sent to user mode.

- struct timeval is written in write()

- Because of the two issues above the size of the header is not mode-independent, so you can't use a 32-bit usbmon or Wireshark on a 64-bit system. And since you use write() a translation is impossible.

Going further...

drivers/usb/mon/mon_main.c static void mon_bus_init(struct dentry * · line 329

Quoted 10 lines
drivers/usb/mon/mon_main.c +6 -0
@@ -320,11 +321,22 @@ static void mon_bus_init(struct dentry *
 		goto err_create_s;
 	mbus->dent_s = d;
 
+	rc = snprintf(name, NAMESZ, "%db", ubus->busnum);
+	if (rc <= 0 || rc >= NAMESZ)
+		goto err_print_b;
+	d = debugfs_create_file(name, 0600, mondir, mbus, &mon_fops_binary);
+	if (d == NULL)
+		goto err_create_b;

This is not a mistake, but a design decision as we discussed before. I think I'm going use a character device in preference.

drivers/usb/mon/usb_mon.h struct mon_reader { · line 62

Quoted 10 lines
drivers/usb/mon/usb_mon.h +10 -0
+struct mon_event_hdr {
+	s32 type;	/* submit, complete, etc. */
+	u32 pipe;	/* Pipe */
+	u64 id;	        /* Incremental id */
+	struct timeval tstamp;
+	u32 length;	/* Depends on type: xfer length or act length */
+	s32 status;
+	s8 setup_flag;
+	s8 data_flag;
+};

I'm going to use the header which you used with small modifications for the ioctl. For one thing, we need two lengths: the actual length of URB data, and the length of data delivered to the application.

* Reallocation - a smaller issue

drivers/usb/mon/mon_common.c line 131

Quoted 8 lines
drivers/usb/mon/mon_common.c +8 -0
+int mon_list_resize(struct mon_reader_list* rp, int data_max)
+{
+	if ((new_e_slab = kmem_cache_create(new_name,
+	    sizeof(struct mon_event_list)+data_max, sizeof(long), 0,
+	    mon_list_ctor, NULL)) == NULL) {
+		ret = -ENOMEM;
+		goto out;
+	}

The function is unfinished, because this is the only time where new_e_slab is used!

drivers/usb/mon/mon_common.c line 137

Quoted 5 lines
drivers/usb/mon/mon_common.c +5 -0
+	/* event list must be flushed because old entries have different size */
+	spin_lock_irqsave(&rp->r.m_bus->lock, flags);
+	INIT_LIST_HEAD(&rp->e_list);
+	rp->nevents = 0;
+	spin_unlock_irqrestore(&rp->r.m_bus->lock, flags);

That's wrong. And to think that you abstracted mon_list_destroy.

In any case, I think this reallocation business is sad. Let's switch to separate allocations and not reallocate on resizes. This will just happen under the mbus->lock.

* Small things

drivers/usb/mon/mon_binary.c line 166

Quoted 2 lines
drivers/usb/mon/mon_binary.c +2 -0
+	if (_IOC_TYPE(cmd) != MON_IOC_MAGIC)
+		return -ENOTTY;

Not necessary due to switch below

drivers/usb/mon/mon_common.c line 21

Quoted 6 lines
drivers/usb/mon/mon_common.c +6 -0
+/*
+ * This limit can be changed using ioctl
+ */
+#define DATA_DFL  32
+#define DATA_MIN  16
+#define DATA_MAX  1024

This is too much to put into slab. We should change mon_event_alloc if this is allowed. We discussed this.

drivers/usb/mon/mon_common.c line 29

Quoted 7 lines
drivers/usb/mon/mon_common.c +7 -0
+/*
+ * This limit exists to prevent OOMs when the user process stops reading.
+ * If usbmon were available to unprivileged processes, it might be open
+ * to a local DoS. But we have to keep to root in order to prevent
+ * password sniffing from HID devices.
+ */
+#define EVENT_MAX  (2*PAGE_SIZE / sizeof(struct mon_event_list))

This is obviosly incorrect when resizing is allowed.

Quoted 1 line

+static u64 mon_make_id(void* urb)

This is taking the idea too far, too early.

Quoted 1 line
 mon_common.c |  329 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++

I think these things can go into mon_main.c, if any.

Review

Quoted 2 lines
(quoted diff) +2 -0
+#define MON_IOCT_DATA_MAX _IO(MON_IOC_MAGIC, 1)
+#define MON_IOCG_DATA_MAX _IOR(MON_IOC_MAGIC, 2, u32)

OK, this seems like correct sizing, but I need to check.

Quoted 1 line

+#define MON_IOC_MAXNR 2

Not used anywhere.

* Spelling errors

drivers/usb/mon/mon_binary.c line 182

Quoted 2 lines
drivers/usb/mon/mon_binary.c +2 -0
+		/* try to allocate new buffer before relasing old one
+		 * to be safe*/

"releasing", and don't save on spaces

Quoted 1 line

+ * Temporary remove reader from reader list during resize.

"Temporarily"

Quoted 2 lines

+ * Initialize list reader. Must be called with mon_lock hold and before
+ * mon_reader_add

This is "held".

I'm going to post what I came up with some time this week.

Cheers,
-- Pete

P.S. I think this shouldn't be done:

Quoted 1 line

A binary interface is added to usbmon. For each USB bus present on the host system a new file is added to the debugfs directory, in the form "usb%db".

It would be better to limit the line length in changelog, because it's not just an e-mail message (in fact it would be great to do it for e-mail messages as well).

P.P.S. We have to think about the size limits very carefuly, because one of the design parameters for the universal binary interface is an intercept of all data, including that sent in very long chunks. And URBs can have virtually unlimited data lengths. Simply bumping the limit is not the answer, because then we're pressed against the limits of kmalloc(). We might need to have URBs refcounted like skbuffs. This would be helpful to reduce the copying.