From patchwork Tue Mar 22 23:11:22 2011 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Benjamin Poirier X-Patchwork-Id: 87972 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 52774B6F44 for ; Wed, 23 Mar 2011 10:13:31 +1100 (EST) Received: from localhost ([127.0.0.1]:33987 helo=lists.gnu.org) by lists.gnu.org with esmtp (Exim 4.43) id 1Q2AlU-0006Vu-9H for incoming@patchwork.ozlabs.org; Tue, 22 Mar 2011 19:13:28 -0400 Received: from [140.186.70.92] (port=58156 helo=eggs.gnu.org) by lists.gnu.org with esmtp (Exim 4.43) id 1Q2Ak1-0006TC-GM for qemu-devel@nongnu.org; Tue, 22 Mar 2011 19:11:58 -0400 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1Q2Ajz-0001Ck-Dy for qemu-devel@nongnu.org; Tue, 22 Mar 2011 19:11:57 -0400 Received: from mail-yw0-f45.google.com ([209.85.213.45]:58718) by eggs.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1Q2Ajz-0001CU-AN for qemu-devel@nongnu.org; Tue, 22 Mar 2011 19:11:55 -0400 Received: by ywl41 with SMTP id 41so3775870ywl.4 for ; Tue, 22 Mar 2011 16:11:55 -0700 (PDT) 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=P0jWLQ3E27+hMmINxg3QBcOx6yR4ICrGsu6S81bhpsE=; b=MxkpL8GMzWcEQEQ3MhkSHSlKc2n3T88g5BcI1/0xK0umAd0XhnCdPySkb+rSEcyXNJ MJWokCnWWDGme7emFrf7XbCNCBzUyCiy+VB76aFBhW5krPTubgHRA5ckdmbizgqJYXGM vSsVjveRTPIBviAU/etNCqwEBP43jv3siY7jk= 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=k/ZbORSNAov0F2NTB01C+kXq7ran9jA48vwyd24jChc4Ar7NtRvWPLaRo1W388+XyK /3sjAMM6yM+h2fSkt0QHgm9mvEg0ly1XEpaeHVCI2GitnjCydumFcHcs4UQzOfXEuzck aYuDEs6NFn3dpJdEWPjw/LD3owPT2t++1CRhU= Received: by 10.150.38.9 with SMTP id l9mr6027561ybl.4.1300835514850; Tue, 22 Mar 2011 16:11:54 -0700 (PDT) Received: from localhost.localdomain (233-91-252-216.dsl.colba.net [216.252.91.233]) by mx.google.com with ESMTPS id l43sm2288488yhn.36.2011.03.22.16.11.52 (version=TLSv1/SSLv3 cipher=OTHER); Tue, 22 Mar 2011 16:11:54 -0700 (PDT) From: Benjamin Poirier To: qemu-devel@nongnu.org Date: Tue, 22 Mar 2011 19:11:22 -0400 Message-Id: <1300835483-2266-3-git-send-email-benjamin.poirier@gmail.com> X-Mailer: git-send-email 1.7.4.1 In-Reply-To: <1300835483-2266-1-git-send-email-benjamin.poirier@gmail.com> References: <1300835483-2266-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.213.45 Cc: Blue Swirl , Jason Wang , "Michael S. Tsirkin" Subject: [Qemu-devel] [PATCH v7 2/3] 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 Cc: Blue Swirl --- 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 | 66 +++++++++++++++++++++++++++++++++++++++++++++------------ 1 files changed, 52 insertions(+), 14 deletions(-) diff --git a/hw/rtl8139.c b/hw/rtl8139.c index d843aa0..5b2f489 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_IP 0x0800 /* Internet Protocol packet */ +#define ETH_P_8021Q 0x8100 /* 802.1Q VLAN Extended Header */ +#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 @@ -812,11 +822,13 @@ static int rtl8139_can_receive(VLANClientState *nc) static ssize_t rtl8139_do_receive(VLANClientState *nc, const uint8_t *buf, size_t size_, int do_interrupt) { RTL8139State *s = DO_UPCAST(NICState, nc, nc)->opaque; + /* size is the length of the buffer passed to the driver */ int size = size_; + const uint8_t *dot1q_buf = NULL; uint32_t packet_header = 0; - uint8_t buf1[60]; + uint8_t buf1[MIN_BUF_SIZE + VLAN_HLEN]; static const uint8_t broadcast_macaddr[6] = { 0xff, 0xff, 0xff, 0xff, 0xff, 0xff }; @@ -928,12 +940,15 @@ 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) { + /* if too small buffer, then expand it + * Include some tailroom in case a vlan tag is later removed. */ + if (size < MIN_BUF_SIZE + VLAN_HLEN) { memcpy(buf1, buf, size); - memset(buf1 + size, 0, MIN_BUF_SIZE - size); + memset(buf1 + size, 0, MIN_BUF_SIZE + VLAN_HLEN - size); buf = buf1; - size = MIN_BUF_SIZE; + if (size < MIN_BUF_SIZE) { + size = MIN_BUF_SIZE; + } } if (rtl8139_cp_receiver_enabled(s)) @@ -996,6 +1011,29 @@ 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. */ + if (s->CpCmd & CPlusRxVLAN && be16_to_cpup((uint16_t *) + &buf[ETHER_ADDR_LEN * 2]) == ETH_P_8021Q) { + dot1q_buf = &buf[ETHER_ADDR_LEN * 2]; + size -= VLAN_HLEN; + /* if too small buffer, use the tailroom added duing expansion */ + if (size < MIN_BUF_SIZE) { + size = MIN_BUF_SIZE; + } + + rxdw1 &= ~CP_RX_VLAN_TAG_MASK; + /* BE + ~le_to_cpu()~ + cpu_to_le() = BE */ + rxdw1 |= CP_RX_TAVA | le16_to_cpup((uint16_t *) + &dot1q_buf[ETHER_TYPE_LEN]); + + DEBUG_PRINT(("RTL8139: C+ Rx mode : extracted vlan tag with tci: " + "%u\n", be16_to_cpup((uint16_t *) + &dot1q_buf[ETHER_TYPE_LEN]))); + } else { + /* reset VLAN tag flag */ + rxdw1 &= ~CP_RX_TAVA; + } + /* TODO: scatter the packet over available receive ring descriptors space */ if (size+4 > rx_space) @@ -1017,7 +1055,14 @@ 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); + cpu_physical_memory_write(rx_addr + 2 * ETHER_ADDR_LEN, + buf + 2 * ETHER_ADDR_LEN + VLAN_HLEN, + size - 2 * ETHER_ADDR_LEN); + } else { + cpu_physical_memory_write(rx_addr, buf, size); + } if (s->CpCmd & CPlusRxChkSum) { @@ -1025,7 +1070,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(crc32(0, buf, size_)); cpu_physical_memory_write( rx_addr+size, (uint8_t *)&val, 4); /* first segment of received packet flag */ @@ -1070,9 +1115,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); @@ -2054,10 +2096,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;