From patchwork Tue Oct 27 16:42:02 2015 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Stuart Haslam X-Patchwork-Id: 55642 Delivered-To: patch@linaro.org Received: by 10.112.59.35 with SMTP id w3csp1904727lbq; Tue, 27 Oct 2015 09:46:04 -0700 (PDT) X-Received: by 10.55.201.16 with SMTP id q16mr51996375qki.6.1445964364366; Tue, 27 Oct 2015 09:46:04 -0700 (PDT) Return-Path: Received: from lists.linaro.org (lists.linaro.org. [54.225.227.206]) by mx.google.com with ESMTP id f23si37768251qhc.77.2015.10.27.09.46.04; Tue, 27 Oct 2015 09:46:04 -0700 (PDT) Received-SPF: pass (google.com: domain of lng-odp-bounces@lists.linaro.org designates 54.225.227.206 as permitted sender) client-ip=54.225.227.206; Authentication-Results: mx.google.com; spf=pass (google.com: domain of lng-odp-bounces@lists.linaro.org designates 54.225.227.206 as permitted sender) smtp.mailfrom=lng-odp-bounces@lists.linaro.org; dkim=neutral (body hash did not verify) header.i=@linaro_org.20150623.gappssmtp.com Received: by lists.linaro.org (Postfix, from userid 109) id EA38F61E64; Tue, 27 Oct 2015 16:46:03 +0000 (UTC) X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on ip-10-142-244-252 X-Spam-Level: X-Spam-Status: No, score=-2.5 required=5.0 tests=BAYES_00,DKIM_SIGNED, RCVD_IN_DNSWL_LOW, RCVD_IN_MSPIKE_H3, RCVD_IN_MSPIKE_WL, T_DKIM_INVALID, T_FRT_STOCK2,URIBL_BLOCKED autolearn=disabled version=3.4.0 Received: from [127.0.0.1] (localhost [127.0.0.1]) by lists.linaro.org (Postfix) with ESMTP id 6D28161DAF; Tue, 27 Oct 2015 16:44:51 +0000 (UTC) X-Original-To: lng-odp@lists.linaro.org Delivered-To: lng-odp@lists.linaro.org Received: by lists.linaro.org (Postfix, from userid 109) id 3B84761E15; Tue, 27 Oct 2015 16:44:41 +0000 (UTC) Received: from mail-wi0-f182.google.com (mail-wi0-f182.google.com [209.85.212.182]) by lists.linaro.org (Postfix) with ESMTPS id DE50461E4C for ; Tue, 27 Oct 2015 16:42:36 +0000 (UTC) Received: by wijp11 with SMTP id p11so222690627wij.0 for ; Tue, 27 Oct 2015 09:42:35 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linaro_org.20150623.gappssmtp.com; s=20150623; h=from:to:subject:date:message-id:in-reply-to:references; bh=9hvXgGxT1dFByQjM3RQz/f/Qba52kEo1RxnaevfNvNQ=; b=K+srumAddnWeL5PUIsf7c3weEVQ8KptvvSPvMjyVuQdNuJqW31ztslMFUa/RcfG2yW j+BZRMf8EsAP4Wz37kgwoGqq2L1O/JJ/NR7SL3uDNa936udMYzwG0dR1O2FR6MPUpSqg +2q1m7LWKAl+4kEtD6NTkO/1wTZtqyjrU6oSnKb5yINHx9vmGQ8isOkwpnE3F+wYr0CV zsfKvBaBMuyO23VF8iaSxtkPp443eXWU6efdd+7CUx93z2Gef5neuAdfqT/88uCM8Dhx WzKZ08PWgXd12DYYxz21XlKwGBss8C2I3PIkaLjmVJdpsNXxjupRZy+FvCM2N+ttszJu rB+g== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20130820; h=x-gm-message-state:from:to:subject:date:message-id:in-reply-to :references; bh=9hvXgGxT1dFByQjM3RQz/f/Qba52kEo1RxnaevfNvNQ=; b=SQlRQ7azML7SLro1nSnCvi9OiChm6CIlInOfSeB6/eBhKO8xaEJK7RS/O5xLGTvcYj IDbOuKmBtY6U2033sJOYWHiFGKxXZCA3WEuO+5k5GV5cAXXoJM3xbLHFbEl7bt+rtyaJ 4Aglh6KV7E7W9UJ94LnYKTNeFqtBgbkmxTga/uv7fPYYVbc/1plYz/N9Rja1h70XL6jF n0rvYrVXAcTUQ0G0wW4q9ypShjkntbP/UfVviJRpTvljgIJzpwOuzpKdLJDv5WxUmy/a 0/3z2uBtliwbisZo+SZ8gqDBkzCHa071BZjQr8MaHUixuj7FcTWM+eKkflwSoFIf11fy KFqQ== X-Gm-Message-State: ALoCoQl8AEOaXmNPN4Ahx67zZOrAyoNTsProa7Xm81RYVjeJ4xwjZIZMXpjGlIzlQvOEzTe8+Hlc X-Received: by 10.194.175.226 with SMTP id cd2mr1334178wjc.30.1445964155715; Tue, 27 Oct 2015 09:42:35 -0700 (PDT) Received: from e106441.emea.arm.com ([2001:41d0:a:3cb4::abcd]) by smtp.gmail.com with ESMTPSA id q8sm18359749wiz.23.2015.10.27.09.42.34 (version=TLSv1.2 cipher=ECDHE-RSA-AES128-SHA bits=128/128); Tue, 27 Oct 2015 09:42:35 -0700 (PDT) From: Stuart Haslam To: lng-odp@lists.linaro.org Date: Tue, 27 Oct 2015 16:42:02 +0000 Message-Id: <1445964125-21658-3-git-send-email-stuart.haslam@linaro.org> X-Mailer: git-send-email 2.1.1 In-Reply-To: <1445964125-21658-1-git-send-email-stuart.haslam@linaro.org> References: <1445964125-21658-1-git-send-email-stuart.haslam@linaro.org> X-Topics: patch Subject: [lng-odp] [PATCHv5 2/5] linux-generic: pktio: handle transmit errors correctly X-BeenThere: lng-odp@lists.linaro.org X-Mailman-Version: 2.1.16 Precedence: list List-Id: "The OpenDataPlane \(ODP\) List" List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , MIME-Version: 1.0 Errors-To: lng-odp-bounces@lists.linaro.org Sender: "lng-odp" Errors which occur while sending packets via odp_pktio_send() aren't handled correctly or even consistently across the two socket based implementations. The problems being addressed are; - calls may block indefinitely in certain error conditions (mmsg) - packets may be freed and reported as being sent correctly even though they weren't really sent (mmap) - return value doesn't always accurately reflect number of packets sent - inconsistent use of __odp_errno Signed-off-by: Stuart Haslam --- .../linux-generic/include/odp_packet_io_internal.h | 6 ++ platform/linux-generic/pktio/socket.c | 27 +++--- platform/linux-generic/pktio/socket_mmap.c | 99 +++++++++++----------- 3 files changed, 73 insertions(+), 59 deletions(-) diff --git a/platform/linux-generic/include/odp_packet_io_internal.h b/platform/linux-generic/include/odp_packet_io_internal.h index 4745bd5..1a1118c 100644 --- a/platform/linux-generic/include/odp_packet_io_internal.h +++ b/platform/linux-generic/include/odp_packet_io_internal.h @@ -32,6 +32,12 @@ extern "C" { #define PKTIO_NAME_LEN 256 +/** Determine if a socket read/write error should be reported. Transient errors + * that simply require the caller to retry are ignored, the _send/_recv APIs + * are non-blocking and it is the caller's responsibility to retry if the + * requested number of packets were not handled. */ +#define SOCK_ERR_REPORT(e) (e != EAGAIN && e != EWOULDBLOCK && e != EINTR) + /* Forward declaration */ struct pktio_if_ops; diff --git a/platform/linux-generic/pktio/socket.c b/platform/linux-generic/pktio/socket.c index 7e30027..5f5e0ae 100644 --- a/platform/linux-generic/pktio/socket.c +++ b/platform/linux-generic/pktio/socket.c @@ -396,9 +396,7 @@ static int sock_mmsg_send(pktio_entry_t *pktio_entry, struct iovec iovecs[ODP_PACKET_SOCKET_MAX_BURST_TX][ODP_BUFFER_MAX_SEG]; int ret; int sockfd; - unsigned i; - unsigned sent_msgs = 0; - unsigned flags; + unsigned n, i; if (odp_unlikely(len > ODP_PACKET_SOCKET_MAX_BURST_TX)) return -1; @@ -412,17 +410,24 @@ static int sock_mmsg_send(pktio_entry_t *pktio_entry, iovecs[i]); } - flags = MSG_DONTWAIT; - for (i = 0; i < len; i += sent_msgs) { - ret = sendmmsg(sockfd, &msgvec[i], len - i, flags); - sent_msgs = ret > 0 ? (unsigned)ret : 0; - flags = 0; /* blocking for next rounds */ + for (i = 0; i < len; ) { + ret = sendmmsg(sockfd, &msgvec[i], len - i, MSG_DONTWAIT); + if (odp_unlikely(ret <= -1)) { + if (i == 0 && SOCK_ERR_REPORT(errno)) { + __odp_errno = errno; + ODP_ERR("sendmmsg(): %s\n", strerror(errno)); + return -1; + } + break; + } + + i += ret; } - for (i = 0; i < len; i++) - odp_packet_free(pkt_table[i]); + for (n = 0; n < i; ++n) + odp_packet_free(pkt_table[n]); - return len; + return i; } /* diff --git a/platform/linux-generic/pktio/socket_mmap.c b/platform/linux-generic/pktio/socket_mmap.c index 35d24c6..79ff82d 100644 --- a/platform/linux-generic/pktio/socket_mmap.c +++ b/platform/linux-generic/pktio/socket_mmap.c @@ -176,49 +176,73 @@ static inline unsigned pkt_mmap_v2_tx(int sock, struct ring *ring, { union frame_map ppd; uint32_t pkt_len; - unsigned frame_num, next_frame_num; + unsigned first_frame_num, frame_num, frame_count; int ret; - unsigned i = 0; + uint8_t *buf; + unsigned n, i = 0; + unsigned nb_tx = 0; + int send_errno; - frame_num = ring->frame_num; + first_frame_num = ring->frame_num; + frame_num = first_frame_num; + frame_count = ring->rd_num; while (i < len) { - if (mmap_tx_kernel_ready(ring->rd[frame_num].iov_base)) { - ppd.raw = ring->rd[frame_num].iov_base; + ppd.raw = ring->rd[frame_num].iov_base; + if (!odp_unlikely(mmap_tx_kernel_ready(ppd.raw))) + break; - next_frame_num = (frame_num + 1) % ring->rd_num; + pkt_len = odp_packet_len(pkt_table[i]); + ppd.v2->tp_h.tp_snaplen = pkt_len; + ppd.v2->tp_h.tp_len = pkt_len; - pkt_len = odp_packet_len(pkt_table[i]); - ppd.v2->tp_h.tp_snaplen = pkt_len; - ppd.v2->tp_h.tp_len = pkt_len; + buf = (uint8_t *)ppd.raw + TPACKET2_HDRLEN - + sizeof(struct sockaddr_ll); + odp_packet_copydata_out(pkt_table[i], 0, pkt_len, buf); - odp_packet_copydata_out(pkt_table[i], 0, pkt_len, - (uint8_t *)ppd.raw + - TPACKET2_HDRLEN - - sizeof(struct sockaddr_ll)); + mmap_tx_user_ready(ppd.raw); - mmap_tx_user_ready(ppd.raw); + if (++frame_num >= frame_count) + frame_num = 0; - odp_packet_free(pkt_table[i]); - frame_num = next_frame_num; - i++; - } else { + i++; + } + + ret = sendto(sock, NULL, 0, MSG_DONTWAIT, NULL, 0); + send_errno = errno; + + /* On success, the return value indicates the number of bytes sent. On + * failure a value of -1 is returned, even if the failure occurred + * after some of the packets in the ring have already been sent, so we + * need to inspect the packet status to determine which were sent. */ + for (frame_num = first_frame_num, n = 0; n < i; ++n) { + struct tpacket2_hdr *hdr = ring->rd[frame_num].iov_base; + + if (odp_likely(hdr->tp_status == TP_STATUS_AVAILABLE)) { + nb_tx++; + } else if (hdr->tp_status & TP_STATUS_WRONG_FORMAT) { + /* status will be cleared on the next send request */ break; } + + if (++frame_num >= frame_count) + frame_num = 0; } - ring->frame_num = frame_num; + ring->frame_num = (ring->frame_num + nb_tx) % frame_count; - ret = sendto(sock, NULL, 0, MSG_DONTWAIT, NULL, 0); - if (ret == -1) { - if (errno != EAGAIN) { - __odp_errno = errno; - ODP_ERR("sendto(pkt mmap): %s\n", strerror(errno)); - return -1; - } + if (odp_unlikely(ret == -1 && + nb_tx == 0 && + SOCK_ERR_REPORT(send_errno))) { + __odp_errno = send_errno; + ODP_ERR("sendto(pkt mmap): %s\n", strerror(send_errno)); + return -1; } - return i; + for (i = 0; i < nb_tx; ++i) + odp_packet_free(pkt_table[i]); + + return nb_tx; } static void mmap_fill_ring(struct ring *ring, odp_pool_t pool_hdl, int fanout) @@ -262,21 +286,6 @@ static void mmap_fill_ring(struct ring *ring, odp_pool_t pool_hdl, int fanout) ring->flen = ring->req.tp_frame_size; } -static int mmap_set_packet_loss_discard(int sock) -{ - int ret, discard = 1; - - ret = setsockopt(sock, SOL_PACKET, PACKET_LOSS, (void *)&discard, - sizeof(discard)); - if (ret == -1) { - __odp_errno = errno; - ODP_ERR("setsockopt(PACKET_LOSS): %s\n", strerror(errno)); - return -1; - } - - return 0; -} - static int mmap_setup_ring(int sock, struct ring *ring, int type, odp_pool_t pool_hdl, int fanout) { @@ -286,12 +295,6 @@ static int mmap_setup_ring(int sock, struct ring *ring, int type, ring->type = type; ring->version = TPACKET_V2; - if (type == PACKET_TX_RING) { - ret = mmap_set_packet_loss_discard(sock); - if (ret != 0) - return -1; - } - mmap_fill_ring(ring, pool_hdl, fanout); ret = setsockopt(sock, SOL_PACKET, type, &ring->req, sizeof(ring->req));