vde_user.c 2.3 KB

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