qemu-devel.nongnu.org archive mirror
 help / color / mirror / Atom feed
From: "Cédric Le Goater" <clg@kaod.org>
To: qemu-arm@nongnu.org
Cc: qemu-devel@nongnu.org, "Jason Wang" <jasowang@redhat.com>,
	"Samuel Thibault" <samuel.thibault@ens-lyon.org>,
	"Cédric Le Goater" <clg@kaod.org>
Subject: [Qemu-devel] [PATCH 6/6] slirp/ncsi: add checksum support
Date: Tue, 29 May 2018 08:28:38 +0200	[thread overview]
Message-ID: <20180529062838.20556-7-clg@kaod.org> (raw)
In-Reply-To: <20180529062838.20556-1-clg@kaod.org>

The checksum field of a NC-SI packet contains a value that may be
included in each command and response. The verification is optional
but the Linux driver does so when a non-zero value is provided. Let's
extend the model to compute the checksum value and exercise a little
more the Linux driver.

See section "8.2.2.3 - 2's Complement Checksum Compensation" in the
Network Controller Sideband Interface (NC-SI) Specification for more
details.

Signed-off-by: Cédric Le Goater <clg@kaod.org>
---
 slirp/ncsi.c | 29 +++++++++++++++++++++++++----
 1 file changed, 25 insertions(+), 4 deletions(-)

diff --git a/slirp/ncsi.c b/slirp/ncsi.c
index f00253641ea4..fbfb79713f23 100644
--- a/slirp/ncsi.c
+++ b/slirp/ncsi.c
@@ -8,9 +8,23 @@
  */
 #include "qemu/osdep.h"
 #include "slirp.h"
+#include "net/checksum.h"
 
 #include "ncsi-pkt.h"
 
+static uint32_t ncsi_calculate_checksum(unsigned char *data, int len)
+{
+    uint32_t checksum = 0;
+    int i;
+
+    for (i = 0; i < len; i += 2) {
+        checksum += (((uint32_t)data[i] << 8) | data[i + 1]);
+    }
+
+    checksum = (~checksum + 1);
+    return checksum;
+}
+
 /* Get Capabilities */
 static int ncsi_rsp_handler_gc(struct ncsi_rsp_pkt_hdr *rnh)
 {
@@ -108,6 +122,9 @@ void ncsi_input(Slirp *slirp, const uint8_t *pkt, int pkt_len)
         (ncsi_reply + ETH_HLEN);
     const struct ncsi_rsp_handler *handler = NULL;
     int i;
+    int ncsi_rsp_len = sizeof(*nh);
+    uint32_t checksum;
+    uint32_t *pchecksum;
 
     memset(ncsi_reply, 0, sizeof(ncsi_reply));
 
@@ -137,15 +154,19 @@ void ncsi_input(Slirp *slirp, const uint8_t *pkt, int pkt_len)
             /* TODO: handle errors */
             handler->handler(rnh);
         }
+        ncsi_rsp_len += handler->payload;
     } else {
         rnh->common.length = 0;
         rnh->code          = htons(NCSI_PKT_RSP_C_UNAVAILABLE);
         rnh->reason        = htons(NCSI_PKT_RSP_R_UNKNOWN);
     }
 
-    /* TODO: add a checksum at the end of the frame but the specs
-     * allows it to be zero */
+    /* add a checksum at the end of the frame (the specs allows it to
+     * be zero */
+    checksum = ncsi_calculate_checksum((unsigned char *) rnh, ncsi_rsp_len);
+    pchecksum = (uint32_t *)((void *) rnh + ncsi_rsp_len);
+    *pchecksum = htonl(checksum);
+    ncsi_rsp_len += 4;
 
-    slirp_output(slirp->opaque, ncsi_reply, ETH_HLEN + sizeof(*nh) +
-                 (handler ? handler->payload : 0) + 4);
+    slirp_output(slirp->opaque, ncsi_reply, ETH_HLEN + ncsi_rsp_len);
 }
-- 
2.13.6

  parent reply	other threads:[~2018-05-29  6:29 UTC|newest]

Thread overview: 22+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2018-05-29  6:28 [Qemu-devel] [PATCH 0/6] ftgmac100 and NC-SI enhancements for the Aspeed SoC Cédric Le Goater
2018-05-29  6:28 ` [Qemu-devel] [PATCH 1/6] ftgmac100: compute maximum frame size depending on the protocol Cédric Le Goater
2018-05-29 13:29   ` [Qemu-devel] [Qemu-arm] " Philippe Mathieu-Daudé
2018-05-29  6:28 ` [Qemu-devel] [PATCH 2/6] ftgmac100: add IEEE 802.1Q VLAN support Cédric Le Goater
2018-05-29 13:34   ` [Qemu-devel] [Qemu-arm] " Philippe Mathieu-Daudé
2018-05-29 16:56     ` Cédric Le Goater
2018-05-30  3:03       ` Philippe Mathieu-Daudé
2018-05-29  6:28 ` [Qemu-devel] [PATCH 3/6] net/ftgmac100: fix multicast hash routine Cédric Le Goater
2018-05-29 12:34   ` Philippe Mathieu-Daudé
2018-05-29  6:28 ` [Qemu-devel] [PATCH 4/6] slirp/ncsi: fix "Get Version ID" payload length Cédric Le Goater
2018-05-29 12:42   ` Philippe Mathieu-Daudé
2018-05-29  6:28 ` [Qemu-devel] [PATCH 5/6] slirp/ncsi: add a "Get Parameter" response Cédric Le Goater
2018-05-29 13:11   ` Philippe Mathieu-Daudé
2018-05-29 17:12     ` Cédric Le Goater
2018-05-29  6:28 ` Cédric Le Goater [this message]
2018-05-29 13:24   ` [Qemu-devel] [PATCH 6/6] slirp/ncsi: add checksum support Philippe Mathieu-Daudé
2018-05-29 12:34 ` [Qemu-devel] [Qemu-arm] [PATCH 0/6] ftgmac100 and NC-SI enhancements for the Aspeed SoC Philippe Mathieu-Daudé
2018-05-29 16:57   ` Cédric Le Goater
2018-05-30  3:04     ` Philippe Mathieu-Daudé
2018-05-30  5:26       ` Cédric Le Goater
2018-05-30  5:35 ` [Qemu-devel] " Joel Stanley
2018-05-30  6:25   ` Cédric Le Goater

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20180529062838.20556-7-clg@kaod.org \
    --to=clg@kaod.org \
    --cc=jasowang@redhat.com \
    --cc=qemu-arm@nongnu.org \
    --cc=qemu-devel@nongnu.org \
    --cc=samuel.thibault@ens-lyon.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).