From nobody Fri Nov 29 12:38:22 2024 Delivered-To: importer@patchew.org Received-SPF: pass (zohomail.com: domain of lists.xenproject.org designates 192.237.175.120 as permitted sender) client-ip=192.237.175.120; envelope-from=xen-devel-bounces@lists.xenproject.org; helo=lists.xenproject.org; Authentication-Results: mx.zohomail.com; dkim=pass; spf=pass (zohomail.com: domain of lists.xenproject.org designates 192.237.175.120 as permitted sender) smtp.mailfrom=xen-devel-bounces@lists.xenproject.org; dmarc=pass(p=quarantine dis=none) header.from=suse.com ARC-Seal: i=1; a=rsa-sha256; t=1642322038; cv=none; d=zohomail.com; s=zohoarc; b=A+PWho7iZxMK2zyYsfLDahn5nFi0ZnIrFO+VUIUApybCSv9NYpQOzRfbuNOmalRpfgUWwUqGtD8rT/yiiwDmFVpmNx29N+uOKCdcSGkoYOxYJSkgxVD4v+vZ48kslS1yV4l1qOIodlOA/+0ctlAPKZVdHRxpL5SkEu++3nd1DUw= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=zohomail.com; s=zohoarc; t=1642322038; h=Content-Transfer-Encoding:Cc:Date:From:In-Reply-To:List-Subscribe:List-Post:List-Id:List-Help:List-Unsubscribe:MIME-Version:Message-ID:References:Sender:Subject:To; bh=V/vC7tx78nDm4GqTKex/CmQNpmhFP/CrwSTFfFKDiV0=; b=W/dhY3ftCN9/5oLq3a8VcMAGcncRkruhxlonS+KLpYFvhcMTIH1hxIfpKc40obH1ODBwQkr1uYe3f+nenRMCkWpZLbh/D4RfXtV3Y8oOGT6DzLtPfSyyAktMWVIjNp1+NvXLpS21phkirvFGswRbC11KmwjLyY3Jbozox8TXUkU= ARC-Authentication-Results: i=1; mx.zohomail.com; dkim=pass; spf=pass (zohomail.com: domain of lists.xenproject.org designates 192.237.175.120 as permitted sender) smtp.mailfrom=xen-devel-bounces@lists.xenproject.org; dmarc=pass header.from= (p=quarantine dis=none) Return-Path: Received: from lists.xenproject.org (lists.xenproject.org [192.237.175.120]) by mx.zohomail.com with SMTPS id 1642322038654855.9492261045165; Sun, 16 Jan 2022 00:33:58 -0800 (PST) Received: from list by lists.xenproject.org with outflank-mailman.257835.443268 (Exim 4.92) (envelope-from ) id 1n90yt-0001sG-Nv; Sun, 16 Jan 2022 08:33:43 +0000 Received: by outflank-mailman (output) from mailman id 257835.443268; Sun, 16 Jan 2022 08:33:43 +0000 Received: from localhost ([127.0.0.1] helo=lists.xenproject.org) by lists.xenproject.org with esmtp (Exim 4.92) (envelope-from ) id 1n90yt-0001oe-8s; Sun, 16 Jan 2022 08:33:43 +0000 Received: by outflank-mailman (input) for mailman id 257835; Sun, 16 Jan 2022 08:33:40 +0000 Received: from se1-gles-flk1-in.inumbo.com ([94.247.172.50] helo=se1-gles-flk1.inumbo.com) by lists.xenproject.org with esmtp (Exim 4.92) (envelope-from ) id 1n90yp-0008IP-PN for xen-devel@lists.xenproject.org; Sun, 16 Jan 2022 08:33:39 +0000 Received: from smtp-out2.suse.de (smtp-out2.suse.de [195.135.220.29]) by se1-gles-flk1.inumbo.com (Halon) with ESMTPS id fcddc162-76a6-11ec-a115-11989b9578b4; Sun, 16 Jan 2022 09:33:32 +0100 (CET) Received: from imap2.suse-dmz.suse.de (imap2.suse-dmz.suse.de [192.168.254.74]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature ECDSA (P-521) server-digest SHA512) (No client certificate requested) by smtp-out2.suse.de (Postfix) with ESMTPS id 00A211F3B2; Sun, 16 Jan 2022 08:33:32 +0000 (UTC) Received: from imap2.suse-dmz.suse.de (imap2.suse-dmz.suse.de [192.168.254.74]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature ECDSA (P-521) server-digest SHA512) (No client certificate requested) by imap2.suse-dmz.suse.de (Postfix) with ESMTPS id C14431346A; Sun, 16 Jan 2022 08:33:31 +0000 (UTC) Received: from dovecot-director2.suse.de ([192.168.254.65]) by imap2.suse-dmz.suse.de with ESMTPSA id SBf1LVvY42HyFQAAMHmgww (envelope-from ); Sun, 16 Jan 2022 08:33:31 +0000 X-Outflank-Mailman: Message body and most headers restored to incoming version X-BeenThere: xen-devel@lists.xenproject.org List-Id: Xen developer discussion List-Unsubscribe: , List-Post: List-Help: List-Subscribe: , Errors-To: xen-devel-bounces@lists.xenproject.org Precedence: list Sender: "Xen-devel" X-Inumbo-ID: fcddc162-76a6-11ec-a115-11989b9578b4 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=suse.com; s=susede1; t=1642322012; h=from:from:reply-to:date:date:message-id:message-id:to:to:cc:cc: mime-version:mime-version: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=V/vC7tx78nDm4GqTKex/CmQNpmhFP/CrwSTFfFKDiV0=; b=GNbSZjwy7POyyXYRYjJBwE0+AievuggavYIBPtCINlJ6RJj6rMv5rduHp2JCvUN/g40k/l msxIrrAuYJr2WFo75sNf6pGbDdMSyQgpvjV1YvM8k32Zl8rZ+P/7xJETUIZ8Dqe07LobjF C/1ZOSGWsyRMhepceTlQyAyubIgnM6s= From: Juergen Gross To: minios-devel@lists.xenproject.org, xen-devel@lists.xenproject.org Cc: samuel.thibault@ens-lyon.org, wl@xen.org, Juergen Gross Subject: [MINIOS PATCH v3 07/12] use alloc_file_type() and get_file_from_fd() in netfront Date: Sun, 16 Jan 2022 09:33:23 +0100 Message-Id: <20220116083328.26524-8-jgross@suse.com> X-Mailer: git-send-email 2.26.2 In-Reply-To: <20220116083328.26524-1-jgross@suse.com> References: <20220116083328.26524-1-jgross@suse.com> MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable X-ZohoMail-DKIM: pass (identity @suse.com) X-ZM-MESSAGEID: 1642322039482100003 Content-Type: text/plain; charset="utf-8" Allocate the file type dynamically via alloc_file_type(). Instead of directly accessing the files[] array use get_file_from_fd(). Signed-off-by: Juergen Gross Reviewed-by: Samuel Thibault --- V3: - switch to struct file * parameter for callbacks - use __attribute__((constructor)) --- include/lib.h | 3 +-- include/netfront.h | 3 ++- lib/sys.c | 23 ---------------- netfront.c | 65 +++++++++++++++++++++++++++++++++++++++------- 4 files changed, 59 insertions(+), 35 deletions(-) diff --git a/include/lib.h b/include/lib.h index 2f472b00..b0d08807 100644 --- a/include/lib.h +++ b/include/lib.h @@ -165,8 +165,7 @@ extern struct wait_queue_head event_queue; #define FTYPE_SAVEFILE 5 #define FTYPE_FB 6 #define FTYPE_KBD 7 -#define FTYPE_TAP 8 -#define FTYPE_N 9 +#define FTYPE_N 8 #define FTYPE_SPARE 16 =20 struct file { diff --git a/include/netfront.h b/include/netfront.h index ec641c80..75080c26 100644 --- a/include/netfront.h +++ b/include/netfront.h @@ -10,7 +10,8 @@ struct netfront_dev *init_netfront(char *nodename, char **ip); char *netfront_get_netmask(struct netfront_dev *dev); char *netfront_get_gateway(struct netfront_dev *dev); -void netfront_xmit(struct netfront_dev *dev, unsigned char* data,int len); +void netfront_xmit(struct netfront_dev *dev, const unsigned char *data, + int len); void shutdown_netfront(struct netfront_dev *dev); void suspend_netfront(void); void resume_netfront(void); diff --git a/lib/sys.c b/lib/sys.c index 90684335..44fe848c 100644 --- a/lib/sys.c +++ b/lib/sys.c @@ -309,17 +309,6 @@ int read(int fd, void *buf, size_t nbytes) case FTYPE_SOCKET: return lwip_read(files[fd].fd, buf, nbytes); #endif -#ifdef CONFIG_NETFRONT - case FTYPE_TAP: { - ssize_t ret; - ret =3D netfront_receive(files[fd].dev, buf, nbytes); - if (ret <=3D 0) { - errno =3D EAGAIN; - return -1; - } - return ret; - } -#endif #ifdef CONFIG_KBDFRONT case FTYPE_KBD: { int ret, n; @@ -382,11 +371,6 @@ int write(int fd, const void *buf, size_t nbytes) #ifdef HAVE_LWIP case FTYPE_SOCKET: return lwip_write(files[fd].fd, (void*) buf, nbytes); -#endif -#ifdef CONFIG_NETFRONT - case FTYPE_TAP: - netfront_xmit(files[fd].dev, (void*) buf, nbytes); - return nbytes; #endif default: break; @@ -487,11 +471,6 @@ int close(int fd) res =3D lwip_close(files[fd].fd); break; #endif -#ifdef CONFIG_NETFRONT - case FTYPE_TAP: - shutdown_netfront(files[fd].dev); - break; -#endif #ifdef CONFIG_KBDFRONT case FTYPE_KBD: shutdown_kbdfront(files[fd].dev); @@ -687,7 +666,6 @@ static const char *const file_types[] =3D { [FTYPE_NONE] =3D "none", [FTYPE_CONSOLE] =3D "console", [FTYPE_SOCKET] =3D "socket", - [FTYPE_TAP] =3D "net", [FTYPE_KBD] =3D "kbd", [FTYPE_FB] =3D "fb", }; @@ -872,7 +850,6 @@ static int select_poll(int nfds, fd_set *readfds, fd_se= t *writefds, fd_set *exce n++; FD_CLR(i, exceptfds); break; - case FTYPE_TAP: case FTYPE_KBD: case FTYPE_FB: if (FD_ISSET(i, readfds)) { diff --git a/netfront.c b/netfront.c index 76964514..164fdc66 100644 --- a/netfront.c +++ b/netfront.c @@ -248,14 +248,14 @@ void netfront_select_handler(evtchn_port_t port, stru= ct pt_regs *regs, void *dat { int flags; struct netfront_dev *dev =3D data; - int fd =3D dev->fd; + struct file *file =3D get_file_from_fd(dev->fd); =20 local_irq_save(flags); network_tx_buf_gc(dev); local_irq_restore(flags); =20 - if (fd !=3D -1) - files[fd].read =3D true; + if ( file ) + file->read =3D true; wake_up(&netfront_queue); } #endif @@ -565,8 +565,53 @@ error: } =20 #ifdef HAVE_LIBC +static int netfront_read(struct file *file, void *buf, size_t nbytes) +{ + ssize_t ret; + + ret =3D netfront_receive(file->dev, buf, nbytes); + if ( ret <=3D 0 ) + { + errno =3D EAGAIN; + return -1; + } + + return ret; +} + +static int netfront_write(struct file *file, const void *buf, size_t nbyte= s) +{ + netfront_xmit(file->dev, buf, nbytes); + + return nbytes; +} + +static int netfront_close_fd(struct file *file) +{ + shutdown_netfront(file->dev); + + return 0; +} + +static const struct file_ops netfront_ops =3D { + .name =3D "net", + .read =3D netfront_read, + .write =3D netfront_write, + .close =3D netfront_close_fd, + .select_rd =3D select_read_flag, +}; + +static unsigned int ftype_netfront; + +__attribute__((constructor)) +static void netfront_initialize(void) +{ + ftype_netfront =3D alloc_file_type(&netfront_ops); +} + int netfront_tap_open(char *nodename) { struct netfront_dev *dev; + struct file *file; =20 dev =3D init_netfront(nodename, NETIF_SELECT_RX, NULL, NULL); if (!dev) { @@ -574,9 +619,10 @@ int netfront_tap_open(char *nodename) { errno =3D EIO; return -1; } - dev->fd =3D alloc_fd(FTYPE_TAP); + dev->fd =3D alloc_fd(ftype_netfront); printk("tap_open(%s) -> %d\n", nodename, dev->fd); - files[dev->fd].dev =3D dev; + file =3D get_file_from_fd(dev->fd); + file->dev =3D dev; return dev->fd; } #endif @@ -720,7 +766,7 @@ void init_rx_buffers(struct netfront_dev *dev) } =20 =20 -void netfront_xmit(struct netfront_dev *dev, unsigned char* data,int len) +void netfront_xmit(struct netfront_dev *dev, const unsigned char *data, in= t len) { int flags; struct netif_tx_request *tx; @@ -772,7 +818,8 @@ void netfront_xmit(struct netfront_dev *dev, unsigned c= har* data,int len) ssize_t netfront_receive(struct netfront_dev *dev, unsigned char *data, si= ze_t len) { unsigned long flags; - int fd =3D dev->fd; + struct file *file =3D get_file_from_fd(dev->fd); + ASSERT(current =3D=3D main_thread); =20 dev->rlen =3D 0; @@ -781,9 +828,9 @@ ssize_t netfront_receive(struct netfront_dev *dev, unsi= gned char *data, size_t l =20 local_irq_save(flags); network_rx(dev); - if (!dev->rlen && fd !=3D -1) + if ( !dev->rlen && file ) /* No data for us, make select stop returning */ - files[fd].read =3D false; + file->read =3D false; /* Before re-enabling the interrupts, in case a packet just arrived in= the * meanwhile. */ local_irq_restore(flags); --=20 2.26.2