From patchwork Thu Jul 3 09:16:59 2014 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Maxim Uvarov X-Patchwork-Id: 33014 Return-Path: X-Original-To: linaro@patches.linaro.org Delivered-To: linaro@patches.linaro.org Received: from mail-qc0-f197.google.com (mail-qc0-f197.google.com [209.85.216.197]) by ip-10-151-82-157.ec2.internal (Postfix) with ESMTPS id EC91E203AC for ; Thu, 3 Jul 2014 09:17:22 +0000 (UTC) Received: by mail-qc0-f197.google.com with SMTP id i8sf29967957qcq.4 for ; Thu, 03 Jul 2014 02:17:22 -0700 (PDT) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20130820; h=x-gm-message-state:delivered-to:from:to:date:message-id:subject :precedence:list-id:list-unsubscribe:list-archive:list-post :list-help:list-subscribe:mime-version:errors-to:sender :x-original-sender:x-original-authentication-results:mailing-list :content-type:content-transfer-encoding; bh=yqkGuy3KCoxNVvVQbMDN9BzdP4J5fiZxFbWYgCcEdzQ=; b=OUJ/vXLjAmH3uFVeWRa5MW2hW17uZ2agidX+ncewoziu3IU3Gjvurd3aIQqUIQYsGH v2fASHi3dpad/A+j3F6I4cCrnSlTUftxzy2Etl+ciWi7Fg8fjLOgx/CmQ89z6NbCwSn8 CJpdDqy0jCGoBUDmZglRfcJMjiWvNFUxke6YVaTyF7nd+C1qs+RaFDLHj1tdPgTtk9P/ P5JlkqLxVnFsCStQlFLXHnfjsbEqv0O1ZLcaDMUFEP80FRMthJt+l3DzkvXyxb9JjNgw dExQlWVZt+KMG93rTPE3TnPU0eO1QhlvgogBxxupKoV/U1+I3GJzqPKCuniprwAI0EhP hReQ== X-Gm-Message-State: ALoCoQnQHxB4Qt1hPw+e0+EoFHDJ50BciaN++sQbP9CZWR9rOEsRmDQtuilh3F49Kx1s7WW5LYgL X-Received: by 10.58.200.162 with SMTP id jt2mr1705384vec.17.1404379042810; Thu, 03 Jul 2014 02:17:22 -0700 (PDT) X-BeenThere: patchwork-forward@linaro.org Received: by 10.140.32.203 with SMTP id h69ls381543qgh.56.gmail; Thu, 03 Jul 2014 02:17:22 -0700 (PDT) X-Received: by 10.58.220.230 with SMTP id pz6mr2629877vec.9.1404379042704; Thu, 03 Jul 2014 02:17:22 -0700 (PDT) Received: from mail-ve0-f175.google.com (mail-ve0-f175.google.com [209.85.128.175]) by mx.google.com with ESMTPS id ls1si13963997vcb.99.2014.07.03.02.17.22 for (version=TLSv1 cipher=ECDHE-RSA-RC4-SHA bits=128/128); Thu, 03 Jul 2014 02:17:22 -0700 (PDT) Received-SPF: pass (google.com: domain of patch+caf_=patchwork-forward=linaro.org@linaro.org designates 209.85.128.175 as permitted sender) client-ip=209.85.128.175; Received: by mail-ve0-f175.google.com with SMTP id jx11so12587552veb.20 for ; Thu, 03 Jul 2014 02:17:22 -0700 (PDT) X-Received: by 10.52.139.101 with SMTP id qx5mr2220951vdb.17.1404379042599; Thu, 03 Jul 2014 02:17:22 -0700 (PDT) X-Forwarded-To: patchwork-forward@linaro.org X-Forwarded-For: patch@linaro.org patchwork-forward@linaro.org Delivered-To: patch@linaro.org Received: by 10.221.37.5 with SMTP id tc5csp357789vcb; Thu, 3 Jul 2014 02:17:22 -0700 (PDT) X-Received: by 10.224.51.18 with SMTP id b18mr5070006qag.81.1404379042194; Thu, 03 Jul 2014 02:17:22 -0700 (PDT) Received: from ip-10-141-164-156.ec2.internal (lists.linaro.org. [54.225.227.206]) by mx.google.com with ESMTPS id u7si32274786qab.34.2014.07.03.02.17.20 for (version=TLSv1 cipher=RC4-SHA bits=128/128); Thu, 03 Jul 2014 02:17:22 -0700 (PDT) Received-SPF: none (google.com: lng-odp-bounces@lists.linaro.org does not designate permitted sender hosts) client-ip=54.225.227.206; Received: from localhost ([127.0.0.1] helo=ip-10-141-164-156.ec2.internal) by ip-10-141-164-156.ec2.internal with esmtp (Exim 4.76) (envelope-from ) id 1X2d6n-0000c8-Es; Thu, 03 Jul 2014 09:15:13 +0000 Received: from mail-la0-f45.google.com ([209.85.215.45]) by ip-10-141-164-156.ec2.internal with esmtp (Exim 4.76) (envelope-from ) id 1X2d6h-0000c2-Rh for lng-odp@lists.linaro.org; Thu, 03 Jul 2014 09:15:07 +0000 Received: by mail-la0-f45.google.com with SMTP id hr17so7843759lab.4 for ; Thu, 03 Jul 2014 02:17:08 -0700 (PDT) X-Received: by 10.152.1.228 with SMTP id 4mr401104lap.69.1404379028168; Thu, 03 Jul 2014 02:17:08 -0700 (PDT) Received: from localhost.localdomain (ppp91-76-29-79.pppoe.mtu-net.ru. [91.76.29.79]) by mx.google.com with ESMTPSA id f9sm13174540lae.16.2014.07.03.02.17.06 for (version=TLSv1.1 cipher=ECDHE-RSA-RC4-SHA bits=128/128); Thu, 03 Jul 2014 02:17:07 -0700 (PDT) From: Maxim Uvarov To: lng-odp@lists.linaro.org Date: Thu, 3 Jul 2014 13:16:59 +0400 Message-Id: <1404379019-1571-1-git-send-email-maxim.uvarov@linaro.org> X-Mailer: git-send-email 1.8.5.1.163.gd7aced9 X-Topics: patch Subject: [lng-odp] [PATCH] single raw socket for all threads X-BeenThere: lng-odp@lists.linaro.org X-Mailman-Version: 2.1.14 Precedence: list List-Id: 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-bounces@lists.linaro.org X-Removed-Original-Auth: Dkim didn't pass. X-Original-Sender: maxim.uvarov@linaro.org X-Original-Authentication-Results: mx.google.com; spf=pass (google.com: domain of patch+caf_=patchwork-forward=linaro.org@linaro.org designates 209.85.128.175 as permitted sender) smtp.mail=patch+caf_=patchwork-forward=linaro.org@linaro.org Mailing-list: list patchwork-forward@linaro.org; contact patchwork-forward+owners@linaro.org X-Google-Group-Id: 836684582541 Raw sockets does not support packets fanout across different cpus, that's lead to same packet recieved in different thread. To avoid that we do recv from the same socket in all threads. First thread add socket for current netdev to raw_sockets[] table, others thread do look up if socket for that netdev already exist. This changes for ODP_PKTIO_TYPE_SOCKET_BASIC and ODP_PKTIO_TYPE_SOCKET_MMSG. ODP_PKTIO_TYPE_SOCKET_MMAP should also set fanout to 1. Signed-off-by: Maxim Uvarov --- platform/linux-generic/source/odp_packet_socket.c | 73 +++++++++++++++++++++-- 1 file changed, 69 insertions(+), 4 deletions(-) diff --git a/platform/linux-generic/source/odp_packet_socket.c b/platform/linux-generic/source/odp_packet_socket.c index fbdc7e4..d44c333 100644 --- a/platform/linux-generic/source/odp_packet_socket.c +++ b/platform/linux-generic/source/odp_packet_socket.c @@ -42,6 +42,22 @@ #include #include + +/** Raw sockets does not support packets fanout across different cpus, + * that's lead to same packet recieved in different thread. To avoid that + * we do recv from the same socket in all threads. First thread add socket + * for current netdev to raw_sockets[] table, others thread do look up if + * socket for that netdev already exist. + */ +#define MAX_RAW_SOCKETS_NETDEVS 50 +typedef struct { + const char *netdev; + int fd; +} raw_socket_t; + +static raw_socket_t raw_sockets[MAX_RAW_SOCKETS_NETDEVS]; +static odp_spinlock_t raw_sockets_lock; + /** Eth buffer start offset from u32-aligned address to make sure the following * header (e.g. IP) starts at a 32-bit aligned address. */ @@ -84,6 +100,40 @@ static int set_pkt_sock_fanout_mmap(pkt_sock_mmap_t * const pkt_sock, return 0; } +static int add_raw_fd(const char *netdev, int fd) +{ + int i; + + for (i = 0; i < MAX_RAW_SOCKETS_NETDEVS; i++) { + if (raw_sockets[i].fd == 0) + break; + } + + if (i == (MAX_RAW_SOCKETS_NETDEVS - 1)) { + ODP_ERR("too many sockets\n"); + return -1; + } + + raw_sockets[i].fd = fd; + raw_sockets[i].netdev = netdev; + return 0; +} + +static int find_raw_fd(const char *netdev) +{ + int i; + + for (i = 0; i < MAX_RAW_SOCKETS_NETDEVS; i++) { + if (raw_sockets[i].fd == 0) + break; + + if (!strcmp(raw_sockets[i].netdev, netdev)) + return raw_sockets[i].fd; + } + return 0; +} + + /* * ODP_PACKET_SOCKET_BASIC: * ODP_PACKET_SOCKET_MMSG: @@ -119,10 +169,19 @@ int setup_pkt_sock(pkt_sock_t * const pkt_sock, const char *netdev, odp_packet_free(pkt); + odp_spinlock_lock(&raw_sockets_lock); + + sockfd = find_raw_fd(netdev); + if (sockfd) { + pkt_sock->sockfd = sockfd; + odp_spinlock_unlock(&raw_sockets_lock); + return sockfd; + } + sockfd = socket(AF_PACKET, SOCK_RAW, htons(ETH_P_ALL)); if (sockfd == -1) { perror("setup_pkt_sock() - socket()"); - return -1; + goto error; } pkt_sock->sockfd = sockfd; @@ -132,7 +191,7 @@ int setup_pkt_sock(pkt_sock_t * const pkt_sock, const char *netdev, err = ioctl(sockfd, SIOCGIFINDEX, ðreq); if (err != 0) { perror("setup_pkt_sock() - ioctl(SIOCGIFINDEX)"); - return -1; + goto error; } if_idx = ethreq.ifr_ifindex; @@ -142,7 +201,7 @@ int setup_pkt_sock(pkt_sock_t * const pkt_sock, const char *netdev, err = ioctl(sockfd, SIOCGIFHWADDR, ðreq); if (err != 0) { perror("setup_pkt_sock() - ioctl(SIOCGIFHWADDR)"); - return -1; + goto error; } ethaddr_copy(pkt_sock->if_mac, (unsigned char *)ethreq.ifr_ifru.ifru_hwaddr.sa_data); @@ -154,10 +213,16 @@ int setup_pkt_sock(pkt_sock_t * const pkt_sock, const char *netdev, sa_ll.sll_protocol = htons(ETH_P_ALL); if (bind(sockfd, (struct sockaddr *)&sa_ll, sizeof(sa_ll)) < 0) { perror("setup_pkt_sock() - bind(to IF)"); - return -1; + goto error; } + add_raw_fd(netdev, sockfd); + odp_spinlock_unlock(&raw_sockets_lock); return sockfd; + +error: + odp_spinlock_unlock(&raw_sockets_lock); + return -1; } /*