^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1) // SPDX-License-Identifier: GPL-2.0
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3) * Copyright (C) 2007 Luca Bigliardi (shammash@artha.org).
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6) #include <stddef.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 7) #include <errno.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 8) #include <libvdeplug.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 9) #include <net_user.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 10) #include <um_malloc.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 11) #include "vde.h"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 12)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 13) static int vde_user_init(void *data, void *dev)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 14) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 15) struct vde_data *pri = data;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 16) VDECONN *conn = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 17) int err = -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 18)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 19) pri->dev = dev;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 20)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 21) conn = vde_open(pri->vde_switch, pri->descr, pri->args);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 22)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 23) if (conn == NULL) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 24) err = -errno;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 25) printk(UM_KERN_ERR "vde_user_init: vde_open failed, "
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 26) "errno = %d\n", errno);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 27) return err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 28) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 29)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 30) printk(UM_KERN_INFO "vde backend - connection opened\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 31)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 32) pri->conn = conn;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 33)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 34) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 35) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 36)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 37) static int vde_user_open(void *data)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 38) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 39) struct vde_data *pri = data;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 40)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 41) if (pri->conn != NULL)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 42) return vde_datafd(pri->conn);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 43)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 44) printk(UM_KERN_WARNING "vde_open - we have no VDECONN to open");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 45) return -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 46) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 47)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 48) static void vde_remove(void *data)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 49) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 50) struct vde_data *pri = data;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 51)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 52) if (pri->conn != NULL) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 53) printk(UM_KERN_INFO "vde backend - closing connection\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 54) vde_close(pri->conn);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 55) pri->conn = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 56) kfree(pri->args);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 57) pri->args = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 58) return;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 59) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 60)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 61) printk(UM_KERN_WARNING "vde_remove - we have no VDECONN to remove");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 62) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 63)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 64) const struct net_user_info vde_user_info = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 65) .init = vde_user_init,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 66) .open = vde_user_open,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 67) .close = NULL,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 68) .remove = vde_remove,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 69) .add_address = NULL,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 70) .delete_address = NULL,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 71) .mtu = ETH_MAX_PACKET,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 72) .max_packet = ETH_MAX_PACKET + ETH_HEADER_OTHER,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 73) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 74)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 75) void vde_init_libstuff(struct vde_data *vpri, struct vde_init *init)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 76) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 77) struct vde_open_args *args;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 78)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 79) vpri->args = uml_kmalloc(sizeof(struct vde_open_args), UM_GFP_KERNEL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 80) if (vpri->args == NULL) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 81) printk(UM_KERN_ERR "vde_init_libstuff - vde_open_args "
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 82) "allocation failed");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 83) return;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 84) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 85)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 86) args = vpri->args;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 87)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 88) args->port = init->port;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 89) args->group = init->group;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 90) args->mode = init->mode ? init->mode : 0700;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 91)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 92) args->port ? printk("port %d", args->port) :
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 93) printk("undefined port");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 94) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 95)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 96) int vde_user_read(void *conn, void *buf, int len)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 97) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 98) VDECONN *vconn = conn;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 99) int rv;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 100)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 101) if (vconn == NULL)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 102) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 103)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 104) rv = vde_recv(vconn, buf, len, 0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 105) if (rv < 0) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 106) if (errno == EAGAIN)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 107) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 108) return -errno;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 109) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 110) else if (rv == 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 111) return -ENOTCONN;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 112)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 113) return rv;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 114) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 115)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 116) int vde_user_write(void *conn, void *buf, int len)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 117) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 118) VDECONN *vconn = conn;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 119)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 120) if (vconn == NULL)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 121) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 122)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 123) return vde_send(vconn, buf, len, 0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 124) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 125)