Message ID | 1299538414-22208-3-git-send-email-benjamin.poirier@gmail.com |
---|---|
State | New |
Headers | show |
Benjamin Poirier writes: > Add support to the emulated hardware to extract vlan tags in packets > going from the network to the guest. > > Signed-off-by: Benjamin Poirier <benjamin.poirier@gmail.com> > Cc: Igor V. Kovalenko <igor.v.kovalenko@gmail.com> > Cc: Jason Wang <jasowang@redhat.com> > Cc: Michael S. Tsirkin <mst@redhat.com> > Cc: Blue Swirl <blauwirbel@gmail.com> > > -- > > AFAIK, extraction is optional to get vlans working. The driver > requests rx detagging but should not assume that it was done. Under > Linux, the mac layer will catch the vlan ethertype. I only added this > part for completeness (to emulate the hardware more truthfully...) > --- > hw/rtl8139.c | 86 +++++++++++++++++++++++++++++++++++++++++++++------------ > 1 files changed, 68 insertions(+), 18 deletions(-) > > diff --git a/hw/rtl8139.c b/hw/rtl8139.c > index 3772ac1..55a386e 100644 > --- a/hw/rtl8139.c > +++ b/hw/rtl8139.c > @@ -72,6 +72,16 @@ > #define MOD2(input, size) \ > ( ( input ) & ( size - 1 ) ) > > +#define ETHER_ADDR_LEN 6 > +#define ETHER_TYPE_LEN 2 > +#define ETH_HLEN (ETHER_ADDR_LEN * 2 + ETHER_TYPE_LEN) > +#define ETH_P_8021Q 0x8100 /* 802.1Q VLAN Extended Header */ > +#define ETH_P_IP 0x0800 /* Internet Protocol packet */ > +#define ETH_MTU 1500 > + > +#define VLAN_TCI_LEN 2 > +#define VLAN_HLEN (ETHER_TYPE_LEN + VLAN_TCI_LEN) > + > #if defined (DEBUG_RTL8139) > # define DEBUG_PRINT(x) do { printf x ; } while (0) > #else > @@ -813,10 +823,13 @@ static ssize_t rtl8139_do_receive(VLANClientState *nc, const uint8_t *buf, size_ > { > RTL8139State *s = DO_UPCAST(NICState, nc, nc)->opaque; > int size = size_; > + const uint8_t *dot1q_buf; > + const uint8_t *next_part; > + size_t next_part_size; > > uint32_t packet_header = 0; > > - uint8_t buf1[60]; > + uint8_t buf1[MIN_BUF_SIZE]; > static const uint8_t broadcast_macaddr[6] = > { 0xff, 0xff, 0xff, 0xff, 0xff, 0xff }; > > @@ -928,14 +941,6 @@ static ssize_t rtl8139_do_receive(VLANClientState *nc, const uint8_t *buf, size_ > } > } > > - /* if too small buffer, then expand it */ > - if (size < MIN_BUF_SIZE) { > - memcpy(buf1, buf, size); > - memset(buf1 + size, 0, MIN_BUF_SIZE - size); > - buf = buf1; > - size = MIN_BUF_SIZE; > - } > - > if (rtl8139_cp_receiver_enabled(s)) > { > DEBUG_PRINT(("RTL8139: in C+ Rx mode ================\n")); > @@ -996,6 +1001,40 @@ static ssize_t rtl8139_do_receive(VLANClientState *nc, const uint8_t *buf, size_ > > uint32_t rx_space = rxdw0 & CP_RX_BUFFER_SIZE_MASK; > > + /* write VLAN info to descriptor variables */ > + /* next_part starts right after the vlan header (if any), at the > + * ethertype for the payload */ > + next_part = &buf[ETHER_ADDR_LEN * 2]; > + if (s->CpCmd & CPlusRxVLAN && be16_to_cpup((uint16_t *) next_part) == > + ETH_P_8021Q) { > + dot1q_buf = next_part; > + next_part += VLAN_HLEN; > + size -= VLAN_HLEN; > + > + rxdw1 &= ~CP_RX_VLAN_TAG_MASK; > + /* BE + ~le_to_cpu()~ + cpu_to_le() = BE */ > + rxdw1 |= CP_RX_TAVA | le16_to_cpup((uint16_t *) &buf[ETH_HLEN]); > + > + DEBUG_PRINT(("RTL8139: C+ Rx mode : extracted vlan tag with tci: " > + "%u\n", be16_to_cpup((uint16_t *) &buf[ETH_HLEN]))); > + } else { > + /* reset VLAN tag flag */ > + rxdw1 &= ~CP_RX_TAVA; > + dot1q_buf = NULL; > + } > + next_part_size = buf + size_ - next_part; > + > + /* if too small buffer, then expand it */ > + if (size < MIN_BUF_SIZE) { > + size_t tmp_size = MIN_BUF_SIZE - ETHER_ADDR_LEN * 2; > + > + memcpy(buf1, next_part, next_part_size); > + memset(buf1 + next_part_size, 0, tmp_size - next_part_size); > + next_part = buf1; > + next_part_size = tmp_size; > + size = MIN_BUF_SIZE; > + } > + > /* TODO: scatter the packet over available receive ring descriptors space */ > > if (size+4 > rx_space) > @@ -1017,7 +1056,17 @@ static ssize_t rtl8139_do_receive(VLANClientState *nc, const uint8_t *buf, size_ > target_phys_addr_t rx_addr = rtl8139_addr64(rxbufLO, rxbufHI); > > /* receive/copy to target memory */ > - cpu_physical_memory_write( rx_addr, buf, size ); > + if (dot1q_buf) { > + cpu_physical_memory_write(rx_addr, buf, 2 * ETHER_ADDR_LEN); > + val = crc32(0, buf, 2 * ETHER_ADDR_LEN); > + val = crc32(val, dot1q_buf, VLAN_HLEN); > + cpu_physical_memory_write(rx_addr + 2 * ETHER_ADDR_LEN, next_part, > + next_part_size); > + val = crc32(val, buf + 2 * ETHER_ADDR_LEN, next_part_size); > + } else { > + cpu_physical_memory_write(rx_addr, buf, size); You does padding for buf1, but use buf here, so the it would be missed if you have a non-vlan frame whose size is less than MIN_BUF_SIZE. Looks like the following codes just work: cpu_physical_memory_write(rx_addr, buf, 2 * ETHER_ADDR_LEN); val = crc32(0, buf, 2 * ETHER_ADDR_LEN); if (dot1q_buf) val = crc32(val, dot1q_buf, VLAN_HLEN); cpu_physical_memory_write(rx_addr + 2 * ETHER_ADDR_LEN, next_part, next_part_size); val = crc32(val, buf + 2 * ETHER_ADDR_LEN, next_part_size); > + val = crc32(0, buf, size); > + } > > if (s->CpCmd & CPlusRxChkSum) > { > @@ -1025,7 +1074,7 @@ static ssize_t rtl8139_do_receive(VLANClientState *nc, const uint8_t *buf, size_ > } > > /* write checksum */ > - val = cpu_to_le32(crc32(0, buf, size)); > + val = cpu_to_le32(val); > cpu_physical_memory_write( rx_addr+size, (uint8_t *)&val, 4); > > /* first segment of received packet flag */ > @@ -1070,9 +1119,6 @@ static ssize_t rtl8139_do_receive(VLANClientState *nc, const uint8_t *buf, size_ > rxdw0 &= ~CP_RX_BUFFER_SIZE_MASK; > rxdw0 |= (size+4); > > - /* reset VLAN tag flag */ > - rxdw1 &= ~CP_RX_TAVA; > - > /* update ring data */ > val = cpu_to_le32(rxdw0); > cpu_physical_memory_write(cplus_rx_ring_desc, (uint8_t *)&val, 4); > @@ -1099,6 +1145,14 @@ static ssize_t rtl8139_do_receive(VLANClientState *nc, const uint8_t *buf, size_ > { > DEBUG_PRINT(("RTL8139: in ring Rx mode ================\n")); > > + /* if too small buffer, then expand it */ > + if (size < MIN_BUF_SIZE) { > + memcpy(buf1, buf, size); > + memset(buf1 + size, 0, MIN_BUF_SIZE - size); > + buf = buf1; > + size = MIN_BUF_SIZE; > + } > + > /* begin ring receiver mode */ > int avail = MOD2(s->RxBufferSize + s->RxBufPtr - s->RxBufAddr, s->RxBufferSize); > > @@ -2066,10 +2120,6 @@ static int rtl8139_cplus_transmit_one(RTL8139State *s) > { > DEBUG_PRINT(("RTL8139: +++ C+ mode offloaded task checksum\n")); > > - #define ETH_P_IP 0x0800 /* Internet Protocol packet */ > - #define ETH_HLEN 14 > - #define ETH_MTU 1500 > - > /* ip packet header */ > ip_header *ip = NULL; > int hlen = 0; > -- > 1.7.2.3 >
diff --git a/hw/rtl8139.c b/hw/rtl8139.c index 3772ac1..55a386e 100644 --- a/hw/rtl8139.c +++ b/hw/rtl8139.c @@ -72,6 +72,16 @@ #define MOD2(input, size) \ ( ( input ) & ( size - 1 ) ) +#define ETHER_ADDR_LEN 6 +#define ETHER_TYPE_LEN 2 +#define ETH_HLEN (ETHER_ADDR_LEN * 2 + ETHER_TYPE_LEN) +#define ETH_P_8021Q 0x8100 /* 802.1Q VLAN Extended Header */ +#define ETH_P_IP 0x0800 /* Internet Protocol packet */ +#define ETH_MTU 1500 + +#define VLAN_TCI_LEN 2 +#define VLAN_HLEN (ETHER_TYPE_LEN + VLAN_TCI_LEN) + #if defined (DEBUG_RTL8139) # define DEBUG_PRINT(x) do { printf x ; } while (0) #else @@ -813,10 +823,13 @@ static ssize_t rtl8139_do_receive(VLANClientState *nc, const uint8_t *buf, size_ { RTL8139State *s = DO_UPCAST(NICState, nc, nc)->opaque; int size = size_; + const uint8_t *dot1q_buf; + const uint8_t *next_part; + size_t next_part_size; uint32_t packet_header = 0; - uint8_t buf1[60]; + uint8_t buf1[MIN_BUF_SIZE]; static const uint8_t broadcast_macaddr[6] = { 0xff, 0xff, 0xff, 0xff, 0xff, 0xff }; @@ -928,14 +941,6 @@ static ssize_t rtl8139_do_receive(VLANClientState *nc, const uint8_t *buf, size_ } } - /* if too small buffer, then expand it */ - if (size < MIN_BUF_SIZE) { - memcpy(buf1, buf, size); - memset(buf1 + size, 0, MIN_BUF_SIZE - size); - buf = buf1; - size = MIN_BUF_SIZE; - } - if (rtl8139_cp_receiver_enabled(s)) { DEBUG_PRINT(("RTL8139: in C+ Rx mode ================\n")); @@ -996,6 +1001,40 @@ static ssize_t rtl8139_do_receive(VLANClientState *nc, const uint8_t *buf, size_ uint32_t rx_space = rxdw0 & CP_RX_BUFFER_SIZE_MASK; + /* write VLAN info to descriptor variables */ + /* next_part starts right after the vlan header (if any), at the + * ethertype for the payload */ + next_part = &buf[ETHER_ADDR_LEN * 2]; + if (s->CpCmd & CPlusRxVLAN && be16_to_cpup((uint16_t *) next_part) == + ETH_P_8021Q) { + dot1q_buf = next_part; + next_part += VLAN_HLEN; + size -= VLAN_HLEN; + + rxdw1 &= ~CP_RX_VLAN_TAG_MASK; + /* BE + ~le_to_cpu()~ + cpu_to_le() = BE */ + rxdw1 |= CP_RX_TAVA | le16_to_cpup((uint16_t *) &buf[ETH_HLEN]); + + DEBUG_PRINT(("RTL8139: C+ Rx mode : extracted vlan tag with tci: " + "%u\n", be16_to_cpup((uint16_t *) &buf[ETH_HLEN]))); + } else { + /* reset VLAN tag flag */ + rxdw1 &= ~CP_RX_TAVA; + dot1q_buf = NULL; + } + next_part_size = buf + size_ - next_part; + + /* if too small buffer, then expand it */ + if (size < MIN_BUF_SIZE) { + size_t tmp_size = MIN_BUF_SIZE - ETHER_ADDR_LEN * 2; + + memcpy(buf1, next_part, next_part_size); + memset(buf1 + next_part_size, 0, tmp_size - next_part_size); + next_part = buf1; + next_part_size = tmp_size; + size = MIN_BUF_SIZE; + } + /* TODO: scatter the packet over available receive ring descriptors space */ if (size+4 > rx_space) @@ -1017,7 +1056,17 @@ static ssize_t rtl8139_do_receive(VLANClientState *nc, const uint8_t *buf, size_ target_phys_addr_t rx_addr = rtl8139_addr64(rxbufLO, rxbufHI); /* receive/copy to target memory */ - cpu_physical_memory_write( rx_addr, buf, size ); + if (dot1q_buf) { + cpu_physical_memory_write(rx_addr, buf, 2 * ETHER_ADDR_LEN); + val = crc32(0, buf, 2 * ETHER_ADDR_LEN); + val = crc32(val, dot1q_buf, VLAN_HLEN); + cpu_physical_memory_write(rx_addr + 2 * ETHER_ADDR_LEN, next_part, + next_part_size); + val = crc32(val, buf + 2 * ETHER_ADDR_LEN, next_part_size); + } else { + cpu_physical_memory_write(rx_addr, buf, size); + val = crc32(0, buf, size); + } if (s->CpCmd & CPlusRxChkSum) { @@ -1025,7 +1074,7 @@ static ssize_t rtl8139_do_receive(VLANClientState *nc, const uint8_t *buf, size_ } /* write checksum */ - val = cpu_to_le32(crc32(0, buf, size)); + val = cpu_to_le32(val); cpu_physical_memory_write( rx_addr+size, (uint8_t *)&val, 4); /* first segment of received packet flag */ @@ -1070,9 +1119,6 @@ static ssize_t rtl8139_do_receive(VLANClientState *nc, const uint8_t *buf, size_ rxdw0 &= ~CP_RX_BUFFER_SIZE_MASK; rxdw0 |= (size+4); - /* reset VLAN tag flag */ - rxdw1 &= ~CP_RX_TAVA; - /* update ring data */ val = cpu_to_le32(rxdw0); cpu_physical_memory_write(cplus_rx_ring_desc, (uint8_t *)&val, 4); @@ -1099,6 +1145,14 @@ static ssize_t rtl8139_do_receive(VLANClientState *nc, const uint8_t *buf, size_ { DEBUG_PRINT(("RTL8139: in ring Rx mode ================\n")); + /* if too small buffer, then expand it */ + if (size < MIN_BUF_SIZE) { + memcpy(buf1, buf, size); + memset(buf1 + size, 0, MIN_BUF_SIZE - size); + buf = buf1; + size = MIN_BUF_SIZE; + } + /* begin ring receiver mode */ int avail = MOD2(s->RxBufferSize + s->RxBufPtr - s->RxBufAddr, s->RxBufferSize); @@ -2066,10 +2120,6 @@ static int rtl8139_cplus_transmit_one(RTL8139State *s) { DEBUG_PRINT(("RTL8139: +++ C+ mode offloaded task checksum\n")); - #define ETH_P_IP 0x0800 /* Internet Protocol packet */ - #define ETH_HLEN 14 - #define ETH_MTU 1500 - /* ip packet header */ ip_header *ip = NULL; int hlen = 0;
Add support to the emulated hardware to extract vlan tags in packets going from the network to the guest. Signed-off-by: Benjamin Poirier <benjamin.poirier@gmail.com> Cc: Igor V. Kovalenko <igor.v.kovalenko@gmail.com> Cc: Jason Wang <jasowang@redhat.com> Cc: Michael S. Tsirkin <mst@redhat.com> Cc: Blue Swirl <blauwirbel@gmail.com> -- AFAIK, extraction is optional to get vlans working. The driver requests rx detagging but should not assume that it was done. Under Linux, the mac layer will catch the vlan ethertype. I only added this part for completeness (to emulate the hardware more truthfully...) --- hw/rtl8139.c | 86 +++++++++++++++++++++++++++++++++++++++++++++------------ 1 files changed, 68 insertions(+), 18 deletions(-)