Update HMP only upon mac change completion.
cover Update HMP only upon mac change completion.
Recent threads regarding e1000/rtl8139 and mac address change notifications prompted some research into the respecitive hw data sheets as well as available drivers. What I found is that each hw has a mechanism that can be used by our emulation layer to determine when the mac address change has completed (when the OS finished writing the mac address), and we can use these mechanisms to trigger HMP notifications.
This is only an RFC series. It's been tested and works well. I've split e1000 and rtl8139 changes as they are sufficiently different. e1000 make this very clean and easy, but rtl8139 isn't as nice.
Please take a look and I'd like to hear your comments.
Thanks
-vlad
Vlad Yasevich (2):
e1000: Use Address_Available bit as HW latch
rtl8139: update HMP only when the address is fully written
hw/i386/pc_piix.c | 4 ++++ hw/i386/pc_q35.c | 4 ++++ hw/net/e1000.c | 11 ++++++++++- hw/net/rtl8139.c | 50 +++++++++++++++++++++++++++++++++++++++++++++++++- include/hw/i386/pc.h | 8 ++++++++ 5 files changed, 75 insertions(+), 2 deletions(-)
Signature · 1 line
1.8.4.2
RFC PATCH 1/2 e1000: Use Address_Available bit as HW latch
e1000 provides a E1000_RAH_AV bit on every complete write to the Receive Address Register. We can use this bit 2 ways:
1) To trigger HMP notifications. When the bit is set the mac address is fully set and we can update the HMP.
2) We can turn off he bit on the write to low order bits of the Receive Address Register, so that we would not try to match received traffic to this address when it is not completely set.
--- hw/net/e1000.c | 11 ++++++++++- 1 file changed, 10 insertions(+), 1 deletion(-)
hw/net/e1000.c +10 -1
@@ -1106,10 +1106,19 @@ mac_writereg(E1000State *s, int index, uint32_t val) s->mac_reg[index] = val; -if (index ==RA || index ==RA +1 ) { +switch (index) { +case RA : +/* Mask off AV bit on the write of the low dword. The write of +* the high dword will set the bit. This way a half-written +* mac address will not be used to filter on rx. +*/ + s->mac_reg[RA +1 ] &= ~E1000_RAH_AV ; +break ; +case (RA +1 ): macaddr[0 ] =cpu_to_le32 (s->mac_reg[RA ]); macaddr[1 ] =cpu_to_le32 (s->mac_reg[RA +1 ]);qemu_format_nic_info_str (qemu_get_queue (s->nic), (uint8_t *)macaddr); +break ; } }
Signature · 1 line
1.8.4.2
RFC PATCH 2/2 rtl8139: update HMP only when the address is fully written
In reply to Vlad Yasevichrtl8139 hardware requires 9346 config register to be set into write mode before mac address can be changed even though it is not documented. Every driver inspected so far appears to do this along with comments that this is an undocumented requirement.
We can use this to help us identify when the mac address has been completely written. Simple set a flag whenever mac has changed and at the next transition of 9346 register from Write to Normal mode, we update the HMP.
--- hw/i386/pc_piix.c | 4 ++++ hw/i386/pc_q35.c | 4 ++++ hw/net/rtl8139.c | 50 +++++++++++++++++++++++++++++++++++++++++++++++++- include/hw/i386/pc.h | 8 ++++++++ 4 files changed, 65 insertions(+), 1 deletion(-)
hw/i386/pc_piix.c +4 -0
@@ -372,6 +372,10 @@ static QEMUMachine pc_i440fx_machine_v1_7 = { PC_I440FX_1_7_MACHINE_OPTIONS, .name = "pc-i440fx-1.7", .init = pc_init_pci_1_7, + .compat_props = (GlobalProperty []) { +PC_COMPAT_1_7 , + {/* end of list */ } + }, };#define PC_I440FX_1_6_MACHINE_OPTIONS PC_I440FX_1_7_MACHINE_OPTIONS
hw/i386/pc_q35.c +4 -0
@@ -292,6 +292,10 @@ static QEMUMachine pc_q35_machine_v1_7 = { PC_Q35_1_7_MACHINE_OPTIONS, .name = "pc-q35-1.7", .init = pc_q35_init_1_7, + .compat_props = (GlobalProperty []) { +PC_COMPAT_1_7 , + {/* end of list */ } + }, };#define PC_Q35_1_6_MACHINE_OPTIONS PC_Q35_1_7_MACHINE_OPTIONS
hw/net/rtl8139.c +49 -1
104 lines not on this page · raw message
include/hw/i386/pc.h +8 -0
@@ -247,7 +247,15 @@ uint16_t pvpanic_port(void);int e820_add_entry (uint64_t ,uint64_t ,uint32_t ); +#define PC_COMPAT_1_7 \ + {\ + .driver = "rtl8139",\ + .property="mac_complete",\ + .value = "off",\ + } +#define PC_COMPAT_1_6 \ + PC_COMPAT_1_7, \ {\ .driver = "e1000",\ .property = "mitigation",\
Signature · 1 line
1.8.4.2
Re: [Qemu-devel] [RFC PATCH 1/2] e1000: Use Address_Available bit as HW latch
In reply to Vlad YasevichQuoted 7 lines · Vlad Yasevich
e1000 provides a
E1000_RAH_AVbit on every complete write to the Receive Address Register. We can use this bit 2 ways:
1) To trigger HMP notifications. When the bit is set the mac address is fully set and we can update the HMP.2) We can turn off he bit on the write to low order bits of
s/he/the/
Quoted 11 lines
the Receive Address Register, so that we would not try to match received traffic to this address when it is not completely set.
Signed-off-by Vlad Yasevich <[email protected]>--- hw/net/e1000.c | 11 ++++++++++- 1 file changed, 10 insertions(+), 1 deletion(-)
hw/net/e1000.c mac_writereg(E1000State *s, int index, uint32_t val) · line 1115
Quoted 8 lines
hw/net/e1000.c +7
-1-if (index ==RA || index ==RA +1 ) { +switch (index) { +case RA : +/* Mask off AV bit on the write of the low dword. The write of +* the high dword will set the bit. This way a half-written +* mac address will not be used to filter on rx. +*/ + s->mac_reg[RA +1 ] &= ~E1000_RAH_AV ;
Does real hardware also auto-clear this bit when writing the low word (thus forcing all drivers to write high word last to make a change take effect)? Or are we risking the case of a driver that writes high word first including the bit, and where real hardware just glitches over the temporary half-written address where our emulation locks the user out entirely? (Asked by someone that has not read the datasheet, so take with a grain of salt)
Signature · 2 lines
Eric Blake eblake redhat com +1-919-301-3266 Libvirt virtualization library http://libvirt.org
Re: [Qemu-devel] [RFC PATCH 2/2] rtl8139: update HMP only when the address is fully written
In reply to Vlad YasevichQuoted 7 lines · Vlad Yasevich
rtl8139 hardware requires 9346 config register to be set into write mode before mac address can be changed even though it is not documented. Every driver inspected so far appears to do this along with comments that this is an undocumented requirement.
We can use this to help us identify when the mac address has been completely written. Simple set a flag whenever mac has changed
s/Simple/Simply/
Quoted 5 lines
and at the next transition of 9346 register from Write to Normal mode, we update the HMP.
Signed-off-by Vlad Yasevich <[email protected]>---
Comment-only review (ie. I didn't validate the code, just fixing grammar)
hw/net/rtl8139.c static void rtl8139_Cfg9346_write(RTL8139State *s, uint32_t val) · line 1575
Quoted 5 lines
hw/net/rtl8139.c +5
-0+ }else if (opmode == Cfg9346_Normal && s->mac_changed) { +/* Even though it is not documented, it is required to set +* opmode to Cfg9346_ConfigWrite when changing the mac address +* of the card and to set to Cfg9346_Normal when done. We +* can use this as an idication to kick off the notification event.
s/idication/indication/
hw/net/rtl8139.c static void rtl8139_io_writeb(void *opaque, uint8_t addr, uint32_t val) · line 2761
Quoted 4 lines
hw/net/rtl8139.c +3
-1-qemu_format_nic_info_str (qemu_get_queue (s->nic), s->phys); +if (s->compat_flags &RTL8139_FLAG_MAC_COMPLETE ) { + s->mac_changed = true; + }else if (addr ==MAC0 +5 ) {
Doesn't coding style recommend s/MAC0+5/MAC0 + 5/
Signature · 2 lines
Eric Blake eblake redhat com +1-919-301-3266 Libvirt virtualization library http://libvirt.org
Re: [Qemu-devel] [RFC PATCH 1/2] e1000: Use Address_Available bit as HW latch
In reply to Vlad YasevichQuoted 22 lines · Vlad Yasevich
e1000 provides a
E1000_RAH_AVbit on every complete write to the Receive Address Register. We can use this bit 2 ways:
1) To trigger HMP notifications. When the bit is set the mac address is fully set and we can update the HMP.2) We can turn off he bit on the write to low order bits of the Receive Address Register, so that we would not try to match received traffic to this address when it is not completely set.
Signed-off-by Vlad Yasevich <[email protected]>--- hw/net/e1000.c | 11 ++++++++++- 1 file changed, 10 insertions(+), 1 deletion(-)hw/net/e1000.c +0
-0@@ -1106,10 +1106,19 @@ mac_writereg(E1000State *s, int index, uint32_t val)
hw/net/e1000.c mac_writereg(E1000State *s, int index, uint32_t val) · line 1115
Quoted 10 lines
hw/net/e1000.c +7
-1s->mac_reg[index] = val; -if (index ==RA || index ==RA +1 ) { +switch (index) { +case RA : +/* Mask off AV bit on the write of the low dword. The write of +* the high dword will set the bit. This way a half-written +* mac address will not be used to filter on rx. +*/ + s->mac_reg[RA +1 ] &= ~E1000_RAH_AV ;
If a stupid driver write high dword first, it won't receive any packets.
hw/net/e1000.c mac_writereg(E1000State *s, int index, uint32_t val) · line 1120
Quoted 5 lines
hw/net/e1000.c +2
-0+break ; +case (RA +1 ): macaddr[0 ] =cpu_to_le32 (s->mac_reg[RA ]); macaddr[1 ] =cpu_to_le32 (s->mac_reg[RA +1 ]);qemu_format_nic_info_str (qemu_get_queue (s->nic), (uint8_t *)macaddr);
Guest may invalid the mac address by clearing the AV bit through writing to high dword. So this may notify a wrong mac address.
Generally, we could teset the AV bit before notification, and try to do the this on both high and low dword. This obeys specs and receive_filter() above.
If we don't want half-written status, driver should clear AV bit before each writing of new mac address. But looks like linux and freebsd does not do this. But the window is really small and harmless.
Quoted 4 lines
hw/net/e1000.c +1
-0+break ; } }
Re: [Qemu-devel] [RFC PATCH 1/2] e1000: Use Address_Available bit as HW latch
Quoted 25 lines · Jason Wang
Quoted 24 lines · Vlad Yasevich
e1000 provides a
E1000_RAH_AVbit on every complete write to the Receive Address Register. We can use this bit 2 ways:
1) To trigger HMP notifications. When the bit is set the mac address is fully set and we can update the HMP.2) We can turn off he bit on the write to low order bits of the Receive Address Register, so that we would not try to match received traffic to this address when it is not completely set.
Signed-off-by Vlad Yasevich <[email protected]>--- hw/net/e1000.c | 11 ++++++++++- 1 file changed, 10 insertions(+), 1 deletion(-)hw/net/e1000.c +0
-0@@ -1106,10 +1106,19 @@ mac_writereg(E1000State *s, int index, uint32_t val) s->mac_reg[index] = val;
Quoted 10 lines
hw/net/e1000.c mac_writereg(E1000State *s, int index, uint32_t val) · line 1115
Quoted 8 lines
hw/net/e1000.c +7
-1-if (index ==RA || index ==RA +1 ) { +switch (index) { +case RA : +/* Mask off AV bit on the write of the low dword. The write of +* the high dword will set the bit. This way a half-written +* mac address will not be used to filter on rx. +*/ + s->mac_reg[RA +1 ] &= ~E1000_RAH_AV ;If a stupid driver write high dword first, it won't receive any packets.
I need to ping Intel guys again. I asked them what happens when only the low register is set, but haven't heard back.
Quoted 8 lines
hw/net/e1000.c mac_writereg(E1000State *s, int index, uint32_t val) · line 1120
Quoted 5 lines
hw/net/e1000.c +2
-0+break ; +case (RA +1 ): macaddr[0 ] =cpu_to_le32 (s->mac_reg[RA ]); macaddr[1 ] =cpu_to_le32 (s->mac_reg[RA +1 ]);qemu_format_nic_info_str (qemu_get_queue (s->nic), (uint8_t *)macaddr);Guest may invalid the mac address by clearing the AV bit through writing to high dword. So this may notify a wrong mac address.
In this case, testing for the AV bit would solve this issue.
Quoted 4 lines
Generally, we could teset the AV bit before notification, and try to do the this on both high and low dword. This obeys specs and
receive_filter()above.
This will not really help since the AV bit would already be set from the prior mac address. So, if a stupid driver writes just the low word, the AV bit would already be set.
Quoted 4 lines
If we don't want half-written status, driver should clear AV bit before each writing of new mac address. But looks like linux and freebsd does not do this. But the window is really small and harmless.
We can emulate this. Thanks for the idea.
-vlad
Quoted 5 lines
Quoted 4 lines
hw/net/e1000.c +1
-0+break ; } }
Re: [Qemu-devel] [RFC PATCH 1/2] e1000: Use Address_Available bit as HW latch
Quoted 36 lines · Vlad Yasevich
Quoted 25 lines · Jason Wang
Quoted 25 lines · Vlad Yasevich
Quoted 24 lines
Quoted 24 lines
e1000 provides a
E1000_RAH_AVbit on every complete write to the Receive Address Register. We can use this bit 2 ways:
1) To trigger HMP notifications. When the bit is set the mac address is fully set and we can update the HMP.2) We can turn off he bit on the write to low order bits of the Receive Address Register, so that we would not try to match received traffic to this address when it is not completely set.
Signed-off-by Vlad Yasevich <[email protected]>--- hw/net/e1000.c | 11 ++++++++++- 1 file changed, 10 insertions(+), 1 deletion(-)hw/net/e1000.c +0
-0@@ -1106,10 +1106,19 @@ mac_writereg(E1000State *s, int index, uint32_t val) s->mac_reg[index] = val;Quoted 10 lines
Quoted 10 lines
Quoted 8 lines
hw/net/e1000.c mac_writereg(E1000State *s, int index, uint32_t val) · line 1115
Quoted 8 lines
hw/net/e1000.c +7
-1-if (index ==RA || index ==RA +1 ) { +switch (index) { +case RA : +/* Mask off AV bit on the write of the low dword. The write of +* the high dword will set the bit. This way a half-written +* mac address will not be used to filter on rx. +*/ + s->mac_reg[RA +1 ] &= ~E1000_RAH_AV ;If a stupid driver write high dword first, it won't receive any packets.
Quoted 3 lines
I need to ping Intel guys again. I asked them what happens when only the low register is set, but haven't heard back.
They probably don't have the willing to share the internals of card. I hacked the e1000 driver to check the subtle and undocumented behaviour in the past. Maybe we can do the same.