vde.c 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129
  1. /*
  2. * QEMU System Emulator
  3. *
  4. * Copyright (c) 2003-2008 Fabrice Bellard
  5. *
  6. * Permission is hereby granted, free of charge, to any person obtaining a copy
  7. * of this software and associated documentation files (the "Software"), to deal
  8. * in the Software without restriction, including without limitation the rights
  9. * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  10. * copies of the Software, and to permit persons to whom the Software is
  11. * furnished to do so, subject to the following conditions:
  12. *
  13. * The above copyright notice and this permission notice shall be included in
  14. * all copies or substantial portions of the Software.
  15. *
  16. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  17. * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  18. * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
  19. * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  20. * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  21. * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  22. * THE SOFTWARE.
  23. */
  24. #include "qemu/osdep.h"
  25. #include <libvdeplug.h>
  26. #include "net/net.h"
  27. #include "clients.h"
  28. #include "qemu-common.h"
  29. #include "qemu/option.h"
  30. #include "qemu/main-loop.h"
  31. #include "qapi/error.h"
  32. typedef struct VDEState {
  33. NetClientState nc;
  34. VDECONN *vde;
  35. } VDEState;
  36. static void vde_to_qemu(void *opaque)
  37. {
  38. VDEState *s = opaque;
  39. uint8_t buf[NET_BUFSIZE];
  40. int size;
  41. size = vde_recv(s->vde, (char *)buf, sizeof(buf), 0);
  42. if (size > 0) {
  43. qemu_send_packet(&s->nc, buf, size);
  44. }
  45. }
  46. static ssize_t vde_receive(NetClientState *nc, const uint8_t *buf, size_t size)
  47. {
  48. VDEState *s = DO_UPCAST(VDEState, nc, nc);
  49. ssize_t ret;
  50. do {
  51. ret = vde_send(s->vde, (const char *)buf, size, 0);
  52. } while (ret < 0 && errno == EINTR);
  53. return ret;
  54. }
  55. static void vde_cleanup(NetClientState *nc)
  56. {
  57. VDEState *s = DO_UPCAST(VDEState, nc, nc);
  58. qemu_set_fd_handler(vde_datafd(s->vde), NULL, NULL, NULL);
  59. vde_close(s->vde);
  60. }
  61. static NetClientInfo net_vde_info = {
  62. .type = NET_CLIENT_DRIVER_VDE,
  63. .size = sizeof(VDEState),
  64. .receive = vde_receive,
  65. .cleanup = vde_cleanup,
  66. };
  67. static int net_vde_init(NetClientState *peer, const char *model,
  68. const char *name, const char *sock,
  69. int port, const char *group, int mode, Error **errp)
  70. {
  71. NetClientState *nc;
  72. VDEState *s;
  73. VDECONN *vde;
  74. char *init_group = (char *)group;
  75. char *init_sock = (char *)sock;
  76. struct vde_open_args args = {
  77. .port = port,
  78. .group = init_group,
  79. .mode = mode,
  80. };
  81. vde = vde_open(init_sock, (char *)"QEMU", &args);
  82. if (!vde){
  83. error_setg_errno(errp, errno, "Could not open vde");
  84. return -1;
  85. }
  86. nc = qemu_new_net_client(&net_vde_info, peer, model, name);
  87. snprintf(nc->info_str, sizeof(nc->info_str), "sock=%s,fd=%d",
  88. sock, vde_datafd(vde));
  89. s = DO_UPCAST(VDEState, nc, nc);
  90. s->vde = vde;
  91. qemu_set_fd_handler(vde_datafd(s->vde), vde_to_qemu, NULL, s);
  92. return 0;
  93. }
  94. int net_init_vde(const Netdev *netdev, const char *name,
  95. NetClientState *peer, Error **errp)
  96. {
  97. const NetdevVdeOptions *vde;
  98. assert(netdev->type == NET_CLIENT_DRIVER_VDE);
  99. vde = &netdev->u.vde;
  100. /* missing optional values have been initialized to "all bits zero" */
  101. if (net_vde_init(peer, "vde", name, vde->sock, vde->port, vde->group,
  102. vde->has_mode ? vde->mode : 0700, errp) == -1) {
  103. return -1;
  104. }
  105. return 0;
  106. }