vde_user.c 2.4 KB

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