Patch to add usbmon
This patch adds so-called "usbmon", or USB monitoring framework, similar to what tcpdump provides for Ethernet. This is an initial version, but it should be safe and useful. It adds an overhead of an if () statement into submission and giveback paths even when not monitoring, but this was deemed a lesser evil than stealth manipulation of function pointers.
The patch makes two changes to hcd.c which make usbmon more useful:
- Change the way we determine that DMA should not be mapped for root hubs, so that usbmon knows easily when it's safe to capture data.
- Return exports of usb_bus_list and usb_bus_list_lock for those who wish to build usbmon as a module.
drivers/usb/core/hcd.c +56 -10
@@ -86,6 +86,7 @@/* host controllers we manage */ LIST_HEAD (usb_bus_list); +EXPORT_SYMBOL_GPL (usb_bus_list);/* used when allocating bus numbers */ #define USB_MAXBUS 64
@@ -96,6 +97,7 @@ static struct usb_busmap busmap;/* used when updating list of hcds */ DECLARE_MUTEX (usb_bus_list_lock);/* exported only for usbfs */ +EXPORT_SYMBOL_GPL (usb_bus_list_lock);/* used when updating hcd data */ static DEFINE_SPINLOCK (hcd_data_lock);
@@ -103,6 +105,10 @@ static DEFINE_SPINLOCK(hcd_data_lock);/* wait queue for synchronous unlinks */ DECLARE_WAIT_QUEUE_HEAD (usb_kill_urb_queue); +#if defined(CONFIG_USB_MON ) || defined(CONFIG_USB_MON_MODULE ) +struct usb_mon_operations *mon_ops; +#endif /* CONFIG_USB_MON */ +/*-------------------------------------------------------------------------*/ /*
@@ -746,6 +752,7 @@ int usb_register_bus(struct usb_bus *busup (&usb_bus_list_lock);usbfs_add_bus (bus); +usbmon_notify_bus_add (bus);dev_info (bus->controller,"new USB bus registered, assigned bus number %d\n" , bus->busnum);return 0 ;
@@ -773,6 +780,7 @@ void usb_deregister_bus (struct usb_buslist_del (&bus->bus_list);up (&usb_bus_list_lock); +usbmon_notify_bus_remove (bus);usbfs_remove_bus (bus);clear_bit (bus->busnum, busmap.busmap);
@@ -1058,9 +1066,7 @@ static int hcd_submit_urb (struct urb *u* as simple as possible. */ -// NOTE: a generic device/urb monitoring hook would go here. -// hcd_monitor_hook(MONITOR_URB_SUBMIT, urb) -// It would catch submission paths for all urbs. +usbmon_urb_submit (&hcd->self, urb);/* * Atomically queue the urb, first to our records, then to the HCD.
@@ -1087,6 +1093,7 @@ static int hcd_submit_urb (struct urb *uspin_unlock_irqrestore (&hcd_data_lock, flags);if (status) {INIT_LIST_HEAD (&urb->urb_list); +usbmon_urb_submit_error (&hcd->self, urb, status);return status; }
@@ -1103,8 +1110,6 @@ static int hcd_submit_urb (struct urb *u* valid and usb_buffer_{sync,unmap}() not be needed, since * they could clobber root hub response data. */ - urb->transfer_flags |= (URB_NO_TRANSFER_DMA_MAP - |URB_NO_SETUP_DMA_MAP ); status =rh_urb_enqueue (hcd, urb); goto done; }
@@ -1139,6 +1144,7 @@ done:if (urb->reject)wake_up (&usb_kill_urb_queue);usb_put_urb (urb); +usbmon_urb_submit_error (&hcd->self, urb, status); }return status; }
@@ -1461,14 +1467,13 @@ static struct usb_operations usb_hcd_ope*/ void usb_hcd_giveback_urb (struct usb_hcd *hcd,struct urb *urb,struct pt_regs *regs) { -urb_unlink (urb); +int at_root_hub; -// NOTE: a generic device/urb monitoring hook would go here. -// hcd_monitor_hook(MONITOR_URB_FINISH, urb, dev) -// It would catch exit/unlink paths for all urbs. + at_root_hub = (urb->dev == hcd->self.root_hub); +urb_unlink (urb);/* lower level hcd code should use *_dma exclusively */ -if (hcd->self.controller->dma_mask) { +if (hcd->self.controller->dma_mask && !at_root_hub) {if (usb_pipecontrol (urb->pipe) && !(urb->transfer_flags &URB_NO_SETUP_DMA_MAP )) dma_unmap_single (hcd->self.controller, urb->setup_dma,
@@ -1484,6 +1489,7 @@ void usb_hcd_giveback_urb (struct usb_hc : DMA_TO_DEVICE); } +usbmon_urb_complete (&hcd->self, urb);/* pass ownership to the completion handler */ urb->complete (urb, regs);atomic_dec (&urb->use_count);
@@ -1591,3 +1597,43 @@ void usb_put_hcd (struct usb_hcd *hcd)usb_bus_put (&hcd->self); }EXPORT_SYMBOL (usb_put_hcd); + +/*-------------------------------------------------------------------------*/ + +#if defined(CONFIG_USB_MON ) || defined(CONFIG_USB_MON_MODULE ) + +struct usb_mon_operations *mon_ops; + +/* +* The registration is unlocked. +* We do it this way because we do not want to lock in hot paths. +* +* Notice that the code is minimally error-proof. Because usbmon needs +* symbols from usbcore, usbcore gets referenced and cannot be unloaded first. +*/ + +int usb_mon_register (struct usb_mon_operations *ops) +{ + +if (mon_ops) +return -EBUSY ; + + mon_ops = ops; +mb (); +return 0 ; +} +EXPORT_SYMBOL_GPL (usb_mon_register); + +void usb_mon_deregister (void ) +{ + +if (mon_ops ==NULL ) { +printk (KERN_ERR "USB: monitor was not registered\n" ); +return ; + } + mon_ops =NULL ; +mb (); +} +EXPORT_SYMBOL_GPL (usb_mon_deregister); + +#endif /* CONFIG_USB_MON */
drivers/usb/core/hcd.h +57 -0
63 lines not on this page · raw message
drivers/usb/Kconfig +2 -0
@@ -56,6 +56,8 @@ source "drivers/usb/media/Kconfig" source "drivers/usb/net/Kconfig" +source "drivers/usb/mon/Kconfig" + comment "USB port drivers" depends on USB
drivers/usb/Makefile +2 -0
@@ -6,6 +6,8 @@ obj-$(CONFIG_USB) += core/ +obj-$(CONFIG_USB_MON) += mon/ + obj-$(CONFIG_USB_EHCI_HCD) += host/ obj-$(CONFIG_USB_OHCI_HCD) += host/ obj-$(CONFIG_USB_UHCI_HCD) += host/
drivers/usb/mon/Kconfig +22 -0
@@ -0,0 +1,22 @@ +# +# USB Monitor configuration +# + +# In normal life, it makes little sense to have usbmon as a module, and in fact +# it is harmful, because there is no way to autoload the module. +# The 'm' option is allowed for hackers who debug the usbmon itself, +# and for those who have usbcore as a module. +config USB_MON + tristate "USB Monitor" + depends on USB + default y + help + If you say Y here, a component which captures the USB traffic + between peripheral-specific drivers and HC drivers will be built. + The USB_MON is similar in spirit and may be compatible with Dave + Harding's USBMon. + + This is somewhat experimental at this time, but it should be safe, + as long as you aren't building this as a module and then removing it. + + If unsure, say Y. Do not say M.
drivers/usb/mon/Makefile +7 -0
@@ -0,0 +1,7 @@ +# +# Makefile for USB Core files and filesystem +# + +usbmon-objs := mon_main.o mon_stat.o mon_text.o + +obj-$(CONFIG_USB_MON) += usbmon.o
drivers/usb/mon/mon_main.c +377 -0
377 lines not on this page · raw message
drivers/usb/mon/mon_stat.c +74 -0
74 lines not on this page · raw message
drivers/usb/mon/mon_text.c +395 -0
395 lines not on this page · raw message
drivers/usb/mon/usb_mon.h +51 -0
51 lines not on this page · raw message
include/linux/usb.h +4 -0
@@ -285,6 +285,10 @@ struct usb_bus {struct class_device class_dev;/* class device for this bus */ void (*release)(struct usb_bus *bus);/* function to destroy this bus's memory */ +#if defined(CONFIG_USB_MON ) || defined(CONFIG_USB_MON_MODULE ) +struct mon_bus *mon_bus;/* non-null when associated */ +int monitored;/* non-zero when monitored */ +#endif };#define to_usb_bus (d) container_of(d, struct usb_bus, class_dev)
Quoted 5 lines · Pete Zaitcev
This patch adds so-called "usbmon", or USB monitoring framework, similar to what tcpdump provides for Ethernet. This is an initial version, but it should be safe and useful. It adds an overhead of an if () statement into submission and giveback paths even when not monitoring, but this was deemed a lesser evil than stealth manipulation of function pointers.
Few minor comments on the code:
First off, why make usbmon a module? You aren't allowing it to happen, so just take out the parts of the patch that allow it.
Quoted 4 lines
/* host controllers we manage */
LIST_HEAD(usb_bus_list);
+EXPORT_SYMBOL_GPL (usb_bus_list);
Not needed if not a module.
drivers/usb/core/hcd.c static struct usb_busmap busmap; · line 100
Quoted 7 lines
/* used when allocating bus numbers */
#defineUSB_MAXBUS64drivers/usb/core/hcd.c +1
-0@@ -96,6 +97,7 @@ static struct usb_busmap busmap;/* used when updating list of hcds */ DECLARE_MUTEX (usb_bus_list_lock);/* exported only for usbfs */ +EXPORT_SYMBOL_GPL (usb_bus_list_lock);
Not needed if not a module.
drivers/usb/core/hcd.c static DEFINE_SPINLOCK(hcd_data_lock); · line 110
Quoted 10 lines
drivers/usb/core/hcd.c +3
-0/* used when updating hcd data */ static DEFINE_SPINLOCK (hcd_data_lock);@@ -103,6 +105,10 @@ static DEFINE_SPINLOCK(hcd_data_lock);/* wait queue for synchronous unlinks */ DECLARE_WAIT_QUEUE_HEAD (usb_kill_urb_queue); +#if defined(CONFIG_USB_MON ) || defined(CONFIG_USB_MON_MODULE ) +struct usb_mon_operations *mon_ops; +#endif /* CONFIG_USB_MON */
Not needed at all, as you create it down below. Ah, the .h files, no wait, you refer to it there too, so removing it here should be fine.
drivers/usb/core/hcd.c static int hcd_submit_urb (struct urb *u · line 1115
Quoted 9 lines
drivers/usb/core/hcd.c +0
-2@@ -1103,8 +1110,6 @@ static int hcd_submit_urb (struct urb *u* valid and usb_buffer_{sync,unmap}() not be needed, since * they could clobber root hub response data. */ - urb->transfer_flags |= (URB_NO_TRANSFER_DMA_MAP - |URB_NO_SETUP_DMA_MAP ); status =rh_urb_enqueue (hcd, urb); goto done; }
Why remove that statment? What does that have to do with usbmon?
Quoted 7 lines
void
usb_hcd_giveback_urb(structusb_hcd*hcd, struct urb *urb, structpt_regs*regs) {drivers/usb/core/hcd.c +1
-3-urb_unlink (urb); +int at_root_hub; -// NOTE: a generic device/urb monitoring hook would go here. -// hcd_monitor_hook(MONITOR_URB_FINISH, urb, dev)
drivers/usb/core/hcd.c static struct usb_operations usb_hcd_ope · line 1479
Quoted 10 lines
drivers/usb/core/hcd.c +3
-2-// It would catch exit/unlink paths for all urbs. + at_root_hub = (urb->dev == hcd->self.root_hub); +urb_unlink (urb);/* lower level hcd code should use *_dma exclusively */ -if (hcd->self.controller->dma_mask) { +if (hcd->self.controller->dma_mask && !at_root_hub) {if (usb_pipecontrol (urb->pipe) && !(urb->transfer_flags &URB_NO_SETUP_DMA_MAP )) dma_unmap_single (hcd->self.controller, urb->setup_dma,
You change the logic here a bit too. Why?
Quoted 11 lines
diff -urpN -X dontdiff
linux-2.6.11-rc2/drivers/usb/core/hcd.hlinux-2.6.11-rc2-lem/drivers/usb/core/hcd.hdrivers/usb/core/hcd.h +4
-0@@ -411,6 +411,63 @@ static inline void usbfs_cleanup(void) {/*-------------------------------------------------------------------------*/ +#if defined(CONFIG_USB_MON) || defined(CONFIG_USB_MON_MODULE) + +struct usb_mon_operations { + void (*urb_submit)(struct usb_bus *bus, struct urb *urb);
drivers/usb/core/hcd.h static inline void usbfs_cleanup(void) { · line 427
Quoted 10 lines
drivers/usb/core/hcd.h +10
-0+void (*urb_submit_error)(struct usb_bus *bus,struct urb *urb,int err); +void (*urb_complete)(struct usb_bus *bus,struct urb *urb); +/* void (*urb_unlink)(struct usb_bus *bus, struct urb *urb); */ +void (*bus_add)(struct usb_bus *bus); +void (*bus_remove)(struct usb_bus *bus); +}; + +extern struct usb_mon_operations *mon_ops; + +#define usbmon_urb_submit (bus, urb) \
Can you make these inlines? That makes the code nicer as we get typechecking and everything :)
drivers/usb/core/hcd.h static inline void usbfs_cleanup(void) { · line 465
Quoted 7 lines
drivers/usb/core/hcd.h +7
-0+#else + +#define usbmon_urb_submit (bus, urb)/* */ +#define usbmon_urb_submit (bus, urb, error)/* */ +#define usbmon_urb_complete (bus, urb)/* */ +#define usbmon_notify_bus_add (bus)/* */ +#define usbmon_notify_bus_remove (bus)/* */
static inlines for these too if you can.
thanks,
greg k-h
Quoted 2 lines · Greg KH
First off, why make usbmon a module? You aren't allowing it to happen, so just take out the parts of the patch that allow it.
No, I do allow it. This way I can load and unload it when debugging it. Perhaps in the future we may simply link it with usbcore, but for now it is a separate module. Also, I do not think it's possible not to build it as a module when usbcore is a module.
I do not think it's a good idea for most users to build it as a module, but it's not prohibited.
Quoted 6 lines
drivers/usb/core/hcd.c static DEFINE_SPINLOCK(hcd_data_lock); · line 110
Quoted 3 lines
drivers/usb/core/hcd.c +3
-0+#if defined(CONFIG_USB_MON ) || defined(CONFIG_USB_MON_MODULE ) +struct usb_mon_operations *mon_ops; +#endif /* CONFIG_USB_MON */ Not needed at all, as you create it down below. Ah, the .h files, no wait, you refer to it there too, so removing it here should be fine.
Right.
Quoted 11 lines
drivers/usb/core/hcd.c static int hcd_submit_urb (struct urb *u · line 1115
Quoted 9 lines
drivers/usb/core/hcd.c +0
-2@@ -1103,8 +1110,6 @@ static int hcd_submit_urb (struct urb *u* valid and usb_buffer_{sync,unmap}() not be needed, since * they could clobber root hub response data. */ - urb->transfer_flags |= (URB_NO_TRANSFER_DMA_MAP - |URB_NO_SETUP_DMA_MAP ); status =rh_urb_enqueue (hcd, urb); goto done; }Why remove that statment? What does that have to do with usbmon?
The text format reader peeks at URB data. This is done for expediency (because USBMon viewer wants it and because I'm still wrestling with the design for the binary format reader stream). Anyhow, someone has to make a decision if data is mapped into kernel at present (and if not, perhaps kmap() in the future). This is done line this:
+ if (urb->transfer_flags & URB_NO_TRANSFER_DMA_MAP)
+ return 'D'; // Cannot trust transfer_buffer
+ if ((data = urb->transfer_buffer) == NULL)
+ return 'Z'; // In case...
+ memcpy(ep->data, urb->transfer_buffer, len);
When root hub sets these bogus flags, it makes usbmon to skip perfectly mapped data.
Even without usbmon it was such an annoying way to do things. It created an illegal URB: the one which claimed that it has DMA mapped, but in fact didn't. It only worked because the root hub implementation ignored flags. This makes it much nicer, IMHO:
Quoted 11 lines
Quoted 11 lines
void
usb_hcd_giveback_urb(structusb_hcd*hcd, struct urb *urb, structpt_regs*regs) {drivers/usb/core/hcd.c +3
-4-urb_unlink (urb); +int at_root_hub; -// NOTE: a generic device/urb monitoring hook would go here. -// hcd_monitor_hook(MONITOR_URB_FINISH, urb, dev) -// It would catch exit/unlink paths for all urbs. + at_root_hub = (urb->dev == hcd->self.root_hub); +urb_unlink (urb);
Quoted 8 lines
drivers/usb/core/hcd.c static struct usb_operations usb_hcd_ope · line 1479
Quoted 6 lines
drivers/usb/core/hcd.c +1
-1/* lower level hcd code should use *_dma exclusively */ -if (hcd->self.controller->dma_mask) { +if (hcd->self.controller->dma_mask && !at_root_hub) {if (usb_pipecontrol (urb->pipe) && !(urb->transfer_flags &URB_NO_SETUP_DMA_MAP )) dma_unmap_single (hcd->self.controller, urb->setup_dma,You change the logic here a bit too. Why?
I do not think I change the logic. The check I add is exactly the same which is used to set bogus flags. So, the code works exactly as before, at least in my limited testing. Am I missing something?
Quoted 4 lines
Quoted 1 line
+#define
usbmon_urb_submit(bus, urb)\Can you make these inlines? That makes the code nicer as we get typechecking and everything :)
OK.
Quoted 3 lines
Quoted 1 line
+#define
usbmon_urb_submit(bus, urb)/* */static inlines for these too if you can.
OK
-- Pete
Hi Pete,
today I just thought to give usbmon a try. Previously I used a hacked devio thing around usbfs_snoop to monitor the USB communication between a VMware and the Linux host.
Greg, will such patch accepted for inclusion or will usbfs_snoop go away when usbmon is included?
Quoted 10 lines
Quoted 2 lines
First off, why make usbmon a module? You aren't allowing it to happen, so just take out the parts of the patch that allow it.
No, I do allow it. This way I can load and unload it when debugging it. Perhaps in the future we may simply link it with usbcore, but for now it is a separate module. Also, I do not think it's possible not to build it as a module when usbcore is a module.
I do not think it's a good idea for most users to build it as a module, but it's not prohibited.
By accident I removed the debugfs option from my kernel config and this makes usbmon totally useless. So I think the module approach is wrong from my point of view. Why not compile it always and if debugfs is available, then enable it when the usbcore gets loaded?
And btw don't you think that you split your code into too much separate files? I count less than 900 lines of code.
However it works fine for me, but I can't find a document that describes the information (without looking at the code) you get, when running cat on a specific bus. I think for using cat this format should be a lot more easier to read by humans. With my devio patch I used something like:
control urb endpoint 0x00 flags 0x00 buflen 3 actlen 0
bRequestType 0x20 bRequest 0x00 wValue 0 wIndex 0 wLength 3
pipe 0x80003900 flags 0x00 buffer f7f2e4e0 length 3 setup f64895a0 start 0 packets 0 interval 0 data 36 fc 00
Especially the information about endpoint, type of the URB and the data should be presented in a better way. I would insert a space after each byte of data to make it easier to read. Instead of using the URB pointer for the id, I think it makes sense to present the device number of that device. If you have multiple devices on the bus you can differ between them.
Only my 2 cents. Keep on the good work. I really like it and it will become very handy to me, I think.
Regards
Marcel
Quoted 4 lines · Marcel Holtmann
By accident I removed the debugfs option from my kernel config and this makes usbmon totally useless. So I think the module approach is wrong from my point of view. Why not compile it always and if debugfs is available, then enable it when the usbcore gets loaded?
This may be a good idea, but I'm too lazy to think in trinary. Care to send a patch?
Quoted 2 lines
And btw don't you think that you split your code into too much separate files? I count less than 900 lines of code.
What does it hurt? I think the split was rather clean, along well defined functional lines, with minimum dependencies. They may even be separate modules later.
Quoted 3 lines
However it works fine for me, but I can't find a document that describes the information (without looking at the code) you get, when running cat on a specific bus.
I'm sorry about that. I will add a file in Documentation/ .
Quoted 4 lines
control urb endpoint
0x00flags0x00buflen 3 actlen 0
bRequestType0x20bRequest0x00wValue 0 wIndex 0 wLength 3
pipe0x80003900flags0x00bufferf7f2e4e0length 3 setupf64895a0start 0 packets 0 interval 0 data 36 fc 00
You are right, the current text format (0t) is not easily readable.
My plan calls for the parsing of control messages and things like SCSI commands to be placed in user mode applications, like USBMon. This is how tcpdump and Ethereal operate. I don't like to keep too much of this in kernel. Do you think it's a wrong approach?
Quoted 1 line
[...] I think it makes sense to present the device number of that device.
When I read dumps, I mentally fetch the device number from the pipe. Just use that for now, please.
But if you or someone else were to hack on something like usbdump(1), it would be peachy, I think.
-- Pete
Hi Pete,
Quoted 7 lines
Quoted 4 lines
By accident I removed the debugfs option from my kernel config and this makes usbmon totally useless. So I think the module approach is wrong from my point of view. Why not compile it always and if debugfs is available, then enable it when the usbcore gets loaded?
This may be a good idea, but I'm too lazy to think in trinary. Care to send a patch?
I can do this if you think that this is a good idea. Acutally I see usbmon as an option of the USB core. This will also reduce the code a little bit, because you don't need an registration interface. You can call the usbmon functions directly.
Quoted 6 lines
Quoted 2 lines
And btw don't you think that you split your code into too much separate files? I count less than 900 lines of code.
What does it hurt? I think the split was rather clean, along well defined functional lines, with minimum dependencies. They may even be separate modules later.
I think that separation is a good idea, but dividing such a minimum code into four files didn't make it cleaner. Two much separation makes it even worse and sometimes harder to understand. I personally vote for having a single driver/usb/core/usbmon.c and link it into usbcore if selected.
Quoted 5 lines
Quoted 3 lines
However it works fine for me, but I can't find a document that describes the information (without looking at the code) you get, when running cat on a specific bus.
I'm sorry about that. I will add a file in
Documentation/.
This would be great and btw the official mount point for debugfs should be /sys/kernel/debug as Greg posted some time ago.
Quoted 11 lines
Quoted 4 lines
control urb endpoint
0x00flags0x00buflen 3 actlen 0
bRequestType0x20bRequest0x00wValue 0 wIndex 0 wLength 3
pipe0x80003900flags0x00bufferf7f2e4e0length 3 setupf64895a0start 0 packets 0 interval 0 data 36 fc 00You are right, the current text format (0t) is not easily readable.
My plan calls for the parsing of control messages and things like SCSI commands to be placed in user mode applications, like USBMon. This is how tcpdump and Ethereal operate. I don't like to keep too much of this in kernel. Do you think it's a wrong approach?
I think if cat is the prefered tool for viewing this file then it should be more human readable. If not, then a binary format should be choosen. Maybe we can implement both. Is this possible?
Quoted 4 lines
Quoted 1 line
[...] I think it makes sense to present the device number of that device.
When I read dumps, I mentally fetch the device number from the pipe. Just use that for now, please.
I don't, because I need to check the encoding in the kernel source to do that. I also think that we should abstract from the definition of pipe.
Quoted 1 line
From my view this is a kernel encoding and should not be visible to the
user. Maybe I am wrong, because I am not an expert of USB internals.
Quoted 2 lines
But if you or someone else were to hack on something like usbdump(1), it would be peachy, I think.
I can start with usbdump if we agree on an interface. I personally would prefer a binary interface for that.
Regards
Marcel
Quoted 3 lines · Marcel Holtmann
I think if cat is the prefered tool for viewing this file then it should be more human readable. If not, then a binary format should be choosen. Maybe we can implement both. Is this possible?
Yes. Now you know why files were split as they were.
Quoted 5 lines
Quoted 2 lines
But if you or someone else were to hack on something like usbdump(1), it would be peachy, I think.
I can start with usbdump if we agree on an interface. I personally would prefer a binary interface for that.
If you want to start scoping it, it's fine by me. I was going to concentrate on fixing what's needed first, such as getting control setup packets captured and things like that.
-- Pete
Hi Pete,
Quoted 5 lines
Quoted 3 lines
I think if cat is the prefered tool for viewing this file then it should be more human readable. If not, then a binary format should be choosen. Maybe we can implement both. Is this possible?
Yes. Now you know why files were split as they were.
still no reason for me to split things up.
Quoted 9 lines
Quoted 5 lines
Quoted 2 lines
But if you or someone else were to hack on something like usbdump(1), it would be peachy, I think.
I can start with usbdump if we agree on an interface. I personally would prefer a binary interface for that.
If you want to start scoping it, it's fine by me. I was going to concentrate on fixing what's needed first, such as getting control setup packets captured and things like that.
While I am really thinking about starting usbdump, I may ask why you have choosen to use debugfs as interface. This will not be available in normal distribution kernels and I think a general USB monitoring ability would be great. For example like we have it for Ethernet, Bluetooth and IrDA. So my idea is to create some /dev/usbmonX (for each bus one) where usbdump can read its information from. What do you think?
Regards
Marcel
Quoted 6 lines · Marcel Holtmann
While I am really thinking about starting usbdump, I may ask why you have choosen to use debugfs as interface. This will not be available in normal distribution kernels and I think a general USB monitoring ability would be great. For example like we have it for Ethernet, Bluetooth and IrDA. So my idea is to create some
/dev/usbmonX(for each bus one) where usbdump can read its information from. What do you think?
The debugfs will be available in distributions. And it's no different from having SOCK_PACKET enabled before tcpdump can work. There's no practical disadvantage, unless we consider a backport of usbmon into a legacy distribution such as RHEL 4 or SuSE 9.1.
Since usbmon rides raw file_operations, it can use a chardev interface with a minimal change. The advantage of debugfs is only not needing to allocate a major and dealing with minor partitioning. I also thought it would help to shut up the namespace pollution whiners (the debate of /dbg versus /sys/kernel/debug was rather mild by comparison).
It should make little difference for the tool anyway, the base path ought to be configurable. The biggest difference would be to scripts which launch the
tool: in one case they need to mount debugfs if not mounted (if initscripts haven't), in other case they mknod if necessary (if hal hasn't done it). It is too early to care about this anyway.
-- Pete
Hi Pete,
Quoted 11 lines
Quoted 6 lines
While I am really thinking about starting usbdump, I may ask why you have choosen to use debugfs as interface. This will not be available in normal distribution kernels and I think a general USB monitoring ability would be great. For example like we have it for Ethernet, Bluetooth and IrDA. So my idea is to create some
/dev/usbmonX(for each bus one) where usbdump can read its information from. What do you think?The debugfs will be available in distributions. And it's no different from having
SOCK_PACKETenabled before tcpdump can work. There's no practical disadvantage, unless we consider a backport of usbmon into a legacy distribution such as RHEL 4 or SuSE 9.1.
I am not interested in a backport.
Quoted 5 lines
Since usbmon rides raw
file_operations, it can use a chardev interface with a minimal change. The advantage of debugfs is only not needing to allocate a major and dealing with minor partitioning. I also thought it would help to shut up the namespace pollution whiners (the debate of /dbg versus/sys/kernel/debugwas rather mild by comparison).
Getting a major number should be no problem and the minor partitioning is also easy, because the root hubs are already numbered by USB.
Quoted 4 lines
It should make little difference for the tool anyway, the base path ought to be configurable. The biggest difference would be to scripts which launch the
tool: in one case they need to mount debugfs if not mounted (if initscripts haven't), in other case they mknod if necessary (if hal hasn't done it).
The mknod reason is no reason for, because we have udev (not hal btw) and this working perfect.
Quoted 1 line
It is too early to care about this anyway.
Since I am really thinking of writing usbdump, it is not to early. What stuff is missing in your usbmon patch?
Regards
Marcel
This patch adds so-called "usbmon", or USB monitoring framework, similar to what tcpdump provides for Ethernet. This is an initial version, but it should be safe and useful. It adds an overhead of an if () statement into submission and giveback paths even when not monitoring, but this was deemed a lesser evil than stealth manipulation of function pointers.
The patch makes two changes to hcd.c which make usbmon more useful:
- Change the way we determine that DMA should not be mapped for root hubs, so that usbmon knows easily when it's safe to capture data.
- Return exports of usb_bus_list and usb_bus_list_lock for those who wish to build usbmon as a module.
This version of the patch changes #define to inlines for hooks and drops extra mod_ops.
drivers/usb/core/hcd.c +52 -10
@@ -86,6 +86,7 @@/* host controllers we manage */ LIST_HEAD (usb_bus_list); +EXPORT_SYMBOL_GPL (usb_bus_list);/* used when allocating bus numbers */ #define USB_MAXBUS 64
@@ -96,6 +97,7 @@ static struct usb_busmap busmap;/* used when updating list of hcds */ DECLARE_MUTEX (usb_bus_list_lock);/* exported only for usbfs */ +EXPORT_SYMBOL_GPL (usb_bus_list_lock);/* used when updating hcd data */ static DEFINE_SPINLOCK (hcd_data_lock);
@@ -746,6 +748,7 @@ int usb_register_bus(struct usb_bus *busup (&usb_bus_list_lock);usbfs_add_bus (bus); +usbmon_notify_bus_add (bus);dev_info (bus->controller,"new USB bus registered, assigned bus number %d\n" , bus->busnum);return 0 ;
@@ -773,6 +776,7 @@ void usb_deregister_bus (struct usb_buslist_del (&bus->bus_list);up (&usb_bus_list_lock); +usbmon_notify_bus_remove (bus);usbfs_remove_bus (bus);clear_bit (bus->busnum, busmap.busmap);
@@ -1058,9 +1062,7 @@ static int hcd_submit_urb (struct urb *u* as simple as possible. */ -// NOTE: a generic device/urb monitoring hook would go here. -// hcd_monitor_hook(MONITOR_URB_SUBMIT, urb) -// It would catch submission paths for all urbs. +usbmon_urb_submit (&hcd->self, urb);/* * Atomically queue the urb, first to our records, then to the HCD.
@@ -1087,6 +1089,7 @@ static int hcd_submit_urb (struct urb *uspin_unlock_irqrestore (&hcd_data_lock, flags);if (status) {INIT_LIST_HEAD (&urb->urb_list); +usbmon_urb_submit_error (&hcd->self, urb, status);return status; }
@@ -1103,8 +1106,6 @@ static int hcd_submit_urb (struct urb *u* valid and usb_buffer_{sync,unmap}() not be needed, since * they could clobber root hub response data. */ - urb->transfer_flags |= (URB_NO_TRANSFER_DMA_MAP - |URB_NO_SETUP_DMA_MAP ); status =rh_urb_enqueue (hcd, urb); goto done; }
@@ -1139,6 +1140,7 @@ done:if (urb->reject)wake_up (&usb_kill_urb_queue);usb_put_urb (urb); +usbmon_urb_submit_error (&hcd->self, urb, status); }return status; }
@@ -1461,14 +1463,13 @@ static struct usb_operations usb_hcd_ope*/ void usb_hcd_giveback_urb (struct usb_hcd *hcd,struct urb *urb,struct pt_regs *regs) { -urb_unlink (urb); +int at_root_hub; -// NOTE: a generic device/urb monitoring hook would go here. -// hcd_monitor_hook(MONITOR_URB_FINISH, urb, dev) -// It would catch exit/unlink paths for all urbs. + at_root_hub = (urb->dev == hcd->self.root_hub); +urb_unlink (urb);/* lower level hcd code should use *_dma exclusively */ -if (hcd->self.controller->dma_mask) { +if (hcd->self.controller->dma_mask && !at_root_hub) {if (usb_pipecontrol (urb->pipe) && !(urb->transfer_flags &URB_NO_SETUP_DMA_MAP )) dma_unmap_single (hcd->self.controller, urb->setup_dma,
@@ -1484,6 +1485,7 @@ void usb_hcd_giveback_urb (struct usb_hc : DMA_TO_DEVICE); } +usbmon_urb_complete (&hcd->self, urb);/* pass ownership to the completion handler */ urb->complete (urb, regs);atomic_dec (&urb->use_count);
@@ -1591,3 +1593,43 @@ void usb_put_hcd (struct usb_hcd *hcd)usb_bus_put (&hcd->self); }EXPORT_SYMBOL (usb_put_hcd); + +/*-------------------------------------------------------------------------*/ + +#if defined(CONFIG_USB_MON ) || defined(CONFIG_USB_MON_MODULE ) + +struct usb_mon_operations *mon_ops; + +/* +* The registration is unlocked. +* We do it this way because we do not want to lock in hot paths. +* +* Notice that the code is minimally error-proof. Because usbmon needs +* symbols from usbcore, usbcore gets referenced and cannot be unloaded first. +*/ + +int usb_mon_register (struct usb_mon_operations *ops) +{ + +if (mon_ops) +return -EBUSY ; + + mon_ops = ops; +mb (); +return 0 ; +} +EXPORT_SYMBOL_GPL (usb_mon_register); + +void usb_mon_deregister (void ) +{ + +if (mon_ops ==NULL ) { +printk (KERN_ERR "USB: monitor was not registered\n" ); +return ; + } + mon_ops =NULL ; +mb (); +} +EXPORT_SYMBOL_GPL (usb_mon_deregister); + +#endif /* CONFIG_USB_MON */
drivers/usb/core/hcd.h +60 -0
66 lines not on this page · raw message
drivers/usb/Kconfig +2 -0
@@ -56,6 +56,8 @@ source "drivers/usb/media/Kconfig" source "drivers/usb/net/Kconfig" +source "drivers/usb/mon/Kconfig" + comment "USB port drivers" depends on USB
drivers/usb/Makefile +2 -0
@@ -6,6 +6,8 @@ obj-$(CONFIG_USB) += core/ +obj-$(CONFIG_USB_MON) += mon/ + obj-$(CONFIG_USB_EHCI_HCD) += host/ obj-$(CONFIG_USB_OHCI_HCD) += host/ obj-$(CONFIG_USB_UHCI_HCD) += host/
drivers/usb/mon/Kconfig +22 -0
@@ -0,0 +1,22 @@ +# +# USB Monitor configuration +# + +# In normal life, it makes little sense to have usbmon as a module, and in fact +# it is harmful, because there is no way to autoload the module. +# The 'm' option is allowed for hackers who debug the usbmon itself, +# and for those who have usbcore as a module. +config USB_MON + tristate "USB Monitor" + depends on USB + default y + help + If you say Y here, a component which captures the USB traffic + between peripheral-specific drivers and HC drivers will be built. + The USB_MON is similar in spirit and may be compatible with Dave + Harding's USBMon. + + This is somewhat experimental at this time, but it should be safe, + as long as you aren't building this as a module and then removing it. + + If unsure, say Y. Do not say M.
drivers/usb/mon/Makefile +7 -0
@@ -0,0 +1,7 @@ +# +# Makefile for USB Core files and filesystem +# + +usbmon-objs := mon_main.o mon_stat.o mon_text.o + +obj-$(CONFIG_USB_MON) += usbmon.o
drivers/usb/mon/mon_main.c +377 -0
377 lines not on this page · raw message
drivers/usb/mon/mon_stat.c +74 -0
74 lines not on this page · raw message
drivers/usb/mon/mon_text.c +395 -0
395 lines not on this page · raw message
drivers/usb/mon/usb_mon.h +51 -0
51 lines not on this page · raw message
include/linux/usb.h +4 -0
@@ -285,6 +285,10 @@ struct usb_bus {struct class_device class_dev;/* class device for this bus */ void (*release)(struct usb_bus *bus);/* function to destroy this bus's memory */ +#if defined(CONFIG_USB_MON ) || defined(CONFIG_USB_MON_MODULE ) +struct mon_bus *mon_bus;/* non-null when associated */ +int monitored;/* non-zero when monitored */ +#endif };#define to_usb_bus (d) container_of(d, struct usb_bus, class_dev)
Greg KH
AppliedQuoted 12 lines · Pete Zaitcev
This patch adds so-called "usbmon", or USB monitoring framework, similar to what tcpdump provides for Ethernet. This is an initial version, but it should be safe and useful. It adds an overhead of an if () statement into submission and giveback paths even when not monitoring, but this was deemed a lesser evil than stealth manipulation of function pointers.
The patch makes two changes to
hcd.cwhich make usbmon more useful:
- Change the way we determine that DMA should not be mapped for root hubs, so that usbmon knows easily when it's safe to capture data.
- Return exports ofusb_bus_listandusb_bus_list_lockfor those who wish to build usbmon as a module.
Quoted 4 lines
This version of the patch changes #define to inlines for hooks and drops extra
mod_ops.Signed-off-by Pete Zaitcev
Applied, thanks.
greg k-h