All of lore.kernel.org
 help / color / mirror / Atom feed
From: Samuel Thibault <samuel.thibault@ens-lyon.org>
To: Juergen Gross <jgross@suse.com>
Cc: minios-devel@lists.xenproject.org,
	xen-devel@lists.xenproject.org, wl@xen.org
Subject: Re: [MINIOS PATCH v3 07/12] use alloc_file_type() and get_file_from_fd() in netfront
Date: Sun, 16 Jan 2022 21:59:27 +0100	[thread overview]
Message-ID: <20220116205927.6rmacurejuvr6jdk@begin> (raw)
In-Reply-To: <20220116083328.26524-8-jgross@suse.com>

Juergen Gross, le dim. 16 janv. 2022 09:33:23 +0100, a ecrit:
> 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 <jgross@suse.com>

Reviewed-by: Samuel Thibault <samuel.thibault@ens-lyon.org>

> ---
> 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
>  
>  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 = netfront_receive(files[fd].dev, buf, nbytes);
> -	    if (ret <= 0) {
> -		errno = 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 = 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[] = {
>      [FTYPE_NONE]    = "none",
>      [FTYPE_CONSOLE] = "console",
>      [FTYPE_SOCKET]  = "socket",
> -    [FTYPE_TAP]     = "net",
>      [FTYPE_KBD]     = "kbd",
>      [FTYPE_FB]      = "fb",
>  };
> @@ -872,7 +850,6 @@ static int select_poll(int nfds, fd_set *readfds, fd_set *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, struct pt_regs *regs, void *dat
>  {
>      int flags;
>      struct netfront_dev *dev = data;
> -    int fd = dev->fd;
> +    struct file *file = get_file_from_fd(dev->fd);
>  
>      local_irq_save(flags);
>      network_tx_buf_gc(dev);
>      local_irq_restore(flags);
>  
> -    if (fd != -1)
> -        files[fd].read = true;
> +    if ( file )
> +        file->read = true;
>      wake_up(&netfront_queue);
>  }
>  #endif
> @@ -565,8 +565,53 @@ error:
>  }
>  
>  #ifdef HAVE_LIBC
> +static int netfront_read(struct file *file, void *buf, size_t nbytes)
> +{
> +    ssize_t ret;
> +
> +    ret = netfront_receive(file->dev, buf, nbytes);
> +    if ( ret <= 0 )
> +    {
> +        errno = EAGAIN;
> +        return -1;
> +    }
> +
> +    return ret;
> +}
> +
> +static int netfront_write(struct file *file, const void *buf, size_t nbytes)
> +{
> +    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 = {
> +    .name = "net",
> +    .read = netfront_read,
> +    .write = netfront_write,
> +    .close = netfront_close_fd,
> +    .select_rd = select_read_flag,
> +};
> +
> +static unsigned int ftype_netfront;
> +
> +__attribute__((constructor))
> +static void netfront_initialize(void)
> +{
> +    ftype_netfront = alloc_file_type(&netfront_ops);
> +}
> +
>  int netfront_tap_open(char *nodename) {
>      struct netfront_dev *dev;
> +    struct file *file;
>  
>      dev = init_netfront(nodename, NETIF_SELECT_RX, NULL, NULL);
>      if (!dev) {
> @@ -574,9 +619,10 @@ int netfront_tap_open(char *nodename) {
>  	errno = EIO;
>  	return -1;
>      }
> -    dev->fd = alloc_fd(FTYPE_TAP);
> +    dev->fd = alloc_fd(ftype_netfront);
>      printk("tap_open(%s) -> %d\n", nodename, dev->fd);
> -    files[dev->fd].dev = dev;
> +    file = get_file_from_fd(dev->fd);
> +    file->dev = dev;
>      return dev->fd;
>  }
>  #endif
> @@ -720,7 +766,7 @@ void init_rx_buffers(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)
>  {
>      int flags;
>      struct netif_tx_request *tx;
> @@ -772,7 +818,8 @@ void netfront_xmit(struct netfront_dev *dev, unsigned char* data,int len)
>  ssize_t netfront_receive(struct netfront_dev *dev, unsigned char *data, size_t len)
>  {
>      unsigned long flags;
> -    int fd = dev->fd;
> +    struct file *file = get_file_from_fd(dev->fd);
> +
>      ASSERT(current == main_thread);
>  
>      dev->rlen = 0;
> @@ -781,9 +828,9 @@ ssize_t netfront_receive(struct netfront_dev *dev, unsigned char *data, size_t l
>  
>      local_irq_save(flags);
>      network_rx(dev);
> -    if (!dev->rlen && fd != -1)
> +    if ( !dev->rlen && file )
>          /* No data for us, make select stop returning */
> -        files[fd].read = false;
> +        file->read = false;
>      /* Before re-enabling the interrupts, in case a packet just arrived in the
>       * meanwhile. */
>      local_irq_restore(flags);
> -- 
> 2.26.2
> 

-- 
Samuel
<O> Ça peut être une madeleine à sous munitions (avec des composants,
par exemple)
 -+- #runtime -+-


  reply	other threads:[~2022-01-16 20:59 UTC|newest]

Thread overview: 29+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2022-01-16  8:33 [MINIOS PATCH v3 00/12] remove device specific struct file members Juergen Gross
2022-01-16  8:33 ` [MINIOS PATCH v3 01/12] remove event channel specific struct file definitions Juergen Gross
2022-01-16 20:53   ` Samuel Thibault
2022-01-16  8:33 ` [MINIOS PATCH v3 02/12] remove gnttab specific member from struct file Juergen Gross
2022-01-16  8:33 ` [MINIOS PATCH v3 03/12] use alloc_file_type() and get_file_from_fd() in xs Juergen Gross
2022-01-16 20:54   ` Samuel Thibault
2022-01-18 14:29   ` Andrew Cooper
2022-01-18 14:53     ` Juergen Gross
2022-01-16  8:33 ` [MINIOS PATCH v3 04/12] use alloc_file_type() and get_file_from_fd() in tpm_tis Juergen Gross
2022-01-16 20:56   ` Samuel Thibault
2022-01-18 14:32   ` Andrew Cooper
2022-01-18 14:54     ` Juergen Gross
2022-01-16  8:33 ` [MINIOS PATCH v3 05/12] use alloc_file_type() and get_file_from_fd() in tpmfront Juergen Gross
2022-01-16 20:57   ` Samuel Thibault
2022-01-16  8:33 ` [MINIOS PATCH v3 06/12] use alloc_file_type() and get_file_from_fd() in blkfront Juergen Gross
2022-01-16 20:58   ` Samuel Thibault
2022-01-18 14:41   ` Andrew Cooper
2022-01-18 14:55     ` Juergen Gross
2022-01-16  8:33 ` [MINIOS PATCH v3 07/12] use alloc_file_type() and get_file_from_fd() in netfront Juergen Gross
2022-01-16 20:59   ` Samuel Thibault [this message]
2022-01-16  8:33 ` [MINIOS PATCH v3 08/12] use alloc_file_type() and get_file_from_fd() in fbfront Juergen Gross
2022-01-16 21:00   ` Samuel Thibault
2022-01-16  8:33 ` [MINIOS PATCH v3 09/12] use file_ops and get_file_from_fd() for console Juergen Gross
2022-01-16 21:01   ` Samuel Thibault
2022-01-16  8:33 ` [MINIOS PATCH v3 10/12] add struct file_ops for file type socket Juergen Gross
2022-01-16 21:02   ` Samuel Thibault
2022-01-16  8:33 ` [MINIOS PATCH v3 11/12] add struct file_ops for FTYPE_FILE Juergen Gross
2022-01-16  8:33 ` [MINIOS PATCH v3 12/12] make files array private to sys.c Juergen Gross
2022-01-16 21:03 ` [MINIOS PATCH v3 00/12] remove device specific struct file members Samuel Thibault

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=20220116205927.6rmacurejuvr6jdk@begin \
    --to=samuel.thibault@ens-lyon.org \
    --cc=jgross@suse.com \
    --cc=minios-devel@lists.xenproject.org \
    --cc=wl@xen.org \
    --cc=xen-devel@lists.xenproject.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 an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.