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>
---
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
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 -+-
© 2016 - 2024 Red Hat, Inc.