From patchwork Wed Mar 2 22:36:19 2011 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Benjamin Poirier X-Patchwork-Id: 85189 Return-Path: X-Original-To: incoming@patchwork.ozlabs.org Delivered-To: patchwork-incoming@bilbo.ozlabs.org Received: from lists.gnu.org (lists.gnu.org [199.232.76.165]) (using TLSv1 with cipher DHE-RSA-AES256-SHA (256/256 bits)) (Client did not present a certificate) by ozlabs.org (Postfix) with ESMTPS id 5EB4EB7156 for ; Thu, 3 Mar 2011 09:43:40 +1100 (EST) Received: from localhost ([127.0.0.1]:44342 helo=lists.gnu.org) by lists.gnu.org with esmtp (Exim 4.43) id 1PuujR-0003zv-B7 for incoming@patchwork.ozlabs.org; Wed, 02 Mar 2011 17:41:21 -0500 Received: from [140.186.70.92] (port=37043 helo=eggs.gnu.org) by lists.gnu.org with esmtp (Exim 4.43) id 1PuufF-000289-AH for qemu-devel@nongnu.org; Wed, 02 Mar 2011 17:37:02 -0500 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1PuufC-0005IJ-KZ for qemu-devel@nongnu.org; Wed, 02 Mar 2011 17:37:00 -0500 Received: from mail-qw0-f45.google.com ([209.85.216.45]:38755) by eggs.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1PuufC-0005EK-Eb for qemu-devel@nongnu.org; Wed, 02 Mar 2011 17:36:58 -0500 Received: by mail-qw0-f45.google.com with SMTP id 8so454903qwj.4 for ; Wed, 02 Mar 2011 14:36:58 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=gamma; h=domainkey-signature:from:to:cc:subject:date:message-id:x-mailer :in-reply-to:references; bh=VcUVUh3VaJ76RsFCg4tY3uw/nd5AxlBBUbgLpgIbhok=; b=mlfoQLsJmoU3hc5C2xvZs9g/pKbMwYjvQ9FlboAN1dwC6hsJNvXAqBF5jSFZQrcFVY R3H5y6Fvhu2cH4m5ZyFbcrtRE6RrwXQNigpk81/W6HBJ4O4JUzQ9FLlhWLzhOK0xorDR /zZMi0PLhDkmPPAGghg59Ux8BefxPiFQVsq1E= DomainKey-Signature: a=rsa-sha1; c=nofws; d=gmail.com; s=gamma; h=from:to:cc:subject:date:message-id:x-mailer:in-reply-to:references; b=dR6nh0uI8+Vk6V7oxfUGdAnGOTof8A5wJqYon6EjhvKtzVsW/qzExCzHNp0wPmGaBr Wvmt93y2HW89QhJKoG84s65ecpWDqUrDa4B3ZncW7vIFwe1VEu7/sVtjOBK9IhALDWWc cCfGl+CZe/D5O+wqicHYO9puF3ydszQy+wqmo= Received: by 10.224.53.210 with SMTP id n18mr406584qag.267.1299105418116; Wed, 02 Mar 2011 14:36:58 -0800 (PST) Received: from localhost.localdomain ([216.252.81.96]) by mx.google.com with ESMTPS id e29sm313348qck.27.2011.03.02.14.36.56 (version=TLSv1/SSLv3 cipher=OTHER); Wed, 02 Mar 2011 14:36:57 -0800 (PST) From: Benjamin Poirier To: qemu-devel@nongnu.org Date: Wed, 2 Mar 2011 17:36:19 -0500 Message-Id: <1299105380-24826-2-git-send-email-benjamin.poirier@gmail.com> X-Mailer: git-send-email 1.7.2.3 In-Reply-To: <1299105380-24826-1-git-send-email-benjamin.poirier@gmail.com> References: <1299105380-24826-1-git-send-email-benjamin.poirier@gmail.com> X-detected-operating-system: by eggs.gnu.org: GNU/Linux 2.6 (newer, 2) X-Received-From: 209.85.216.45 Cc: Jason Wang , "Michael S. Tsirkin" Subject: [Qemu-devel] [PATCH v4 1/2] rtl8139: add vlan tag extraction X-BeenThere: qemu-devel@nongnu.org X-Mailman-Version: 2.1.5 Precedence: list List-Id: qemu-devel.nongnu.org List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Sender: qemu-devel-bounces+incoming=patchwork.ozlabs.org@nongnu.org Errors-To: qemu-devel-bounces+incoming=patchwork.ozlabs.org@nongnu.org Add support to the emulated hardware to extract vlan tags in packets going from the network to the guest. Signed-off-by: Benjamin Poirier Cc: Igor V. Kovalenko Cc: Jason Wang Cc: Michael S. Tsirkin --- 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 | 100 +++++++++++++++++++++++++++++++++++++++++++++++----------- 1 files changed, 81 insertions(+), 19 deletions(-) diff --git a/hw/rtl8139.c b/hw/rtl8139.c index a22530c..6e770bd 100644 --- a/hw/rtl8139.c +++ b/hw/rtl8139.c @@ -68,6 +68,16 @@ #if defined(RTL8139_CALCULATE_RXCRC) /* For crc32 */ #include + +static inline uLong rtl8139_crc32(uLong crc, const Bytef *buf, uInt len) +{ + return crc32(crc, buf, len); +} +#else +static inline uLong rtl8139_crc32(uLong crc, const Bytef *buf, uInt len) +{ + return 0; +} #endif #define SET_MASKED(input, mask, curr) \ @@ -77,6 +87,14 @@ #define MOD2(input, size) \ ( ( input ) & ( size - 1 ) ) +#define ETHER_ADDR_LEN 6 +#define ETHER_TYPE_LEN 2 +#define ETHER_HDR_LEN (ETHER_ADDR_LEN * 2 + ETHER_TYPE_LEN) +#define ETHERTYPE_VLAN 0x8100 + +#define VLAN_TCI_LEN 2 +#define VLAN_HDR_LEN (ETHER_TYPE_LEN + VLAN_TCI_LEN) + #if defined (DEBUG_RTL8139) # define DEBUG_PRINT(x) do { printf x ; } while (0) #else @@ -818,10 +836,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 }; @@ -933,14 +954,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")); @@ -1001,6 +1014,41 @@ 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 *) + &buf[ETHER_ADDR_LEN * 2]) == ETHERTYPE_VLAN) { + dot1q_buf = &buf[ETHER_ADDR_LEN * 2]; + next_part += VLAN_HDR_LEN; + size -= VLAN_HDR_LEN; + + rxdw1 &= ~CP_RX_VLAN_TAG_MASK; + /* BE + ~le_to_cpu()~ + cpu_to_le() = BE */ + rxdw1 |= CP_RX_TAVA | le16_to_cpup((uint16_t *) + &buf[ETHER_HDR_LEN]); + + DEBUG_PRINT(("RTL8139: C+ Rx mode : extracted vlan tag with tci: " + "%u\n", be16_to_cpup((uint16_t *) &buf[ETHER_HDR_LEN]))); + } 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) @@ -1022,7 +1070,18 @@ 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 (unlikely(dot1q_buf)) { + cpu_physical_memory_write(rx_addr, buf, 2 * ETHER_ADDR_LEN); + val = rtl8139_crc32(0, buf, 2 * ETHER_ADDR_LEN); + val = rtl8139_crc32(val, dot1q_buf, VLAN_HDR_LEN); + cpu_physical_memory_write(rx_addr + 2 * ETHER_ADDR_LEN, next_part, + next_part_size); + val = rtl8139_crc32(val, buf + 2 * ETHER_ADDR_LEN, + next_part_size); + } else { + cpu_physical_memory_write(rx_addr, buf, size); + val = rtl8139_crc32(0, buf, size); + } if (s->CpCmd & CPlusRxChkSum) { @@ -1031,9 +1090,7 @@ static ssize_t rtl8139_do_receive(VLANClientState *nc, const uint8_t *buf, size_ /* write checksum */ #if defined (RTL8139_CALCULATE_RXCRC) - val = cpu_to_le32(crc32(0, buf, size)); -#else - val = 0; + val = cpu_to_le32(val); #endif cpu_physical_memory_write( rx_addr+size, (uint8_t *)&val, 4); @@ -1079,9 +1136,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); @@ -1108,6 +1162,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); @@ -1133,13 +1195,13 @@ static ssize_t rtl8139_do_receive(VLANClientState *nc, const uint8_t *buf, size_ rtl8139_write_buffer(s, (uint8_t *)&val, 4); + /* receive/copy to target memory */ rtl8139_write_buffer(s, buf, size); + val = rtl8139_crc32(0, buf, size); /* write checksum */ #if defined (RTL8139_CALCULATE_RXCRC) - val = cpu_to_le32(crc32(0, buf, size)); -#else - val = 0; + val = cpu_to_le32(val); #endif rtl8139_write_buffer(s, (uint8_t *)&val, 4);