2
0

vde.c 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128
  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. typedef struct VDEState {
  32. NetClientState nc;
  33. VDECONN *vde;
  34. } VDEState;
  35. static void vde_to_qemu(void *opaque)
  36. {
  37. VDEState *s = opaque;
  38. uint8_t buf[NET_BUFSIZE];
  39. int size;
  40. size = vde_recv(s->vde, (char *)buf, sizeof(buf), 0);
  41. if (size > 0) {
  42. qemu_send_packet(&s->nc, buf, size);
  43. }
  44. }
  45. static ssize_t vde_receive(NetClientState *nc, const uint8_t *buf, size_t size)
  46. {
  47. VDEState *s = DO_UPCAST(VDEState, nc, nc);
  48. ssize_t ret;
  49. do {
  50. ret = vde_send(s->vde, (const char *)buf, size, 0);
  51. } while (ret < 0 && errno == EINTR);
  52. return ret;
  53. }
  54. static void vde_cleanup(NetClientState *nc)
  55. {
  56. VDEState *s = DO_UPCAST(VDEState, nc, nc);
  57. qemu_set_fd_handler(vde_datafd(s->vde), NULL, NULL, NULL);
  58. vde_close(s->vde);
  59. }
  60. static NetClientInfo net_vde_info = {
  61. .type = NET_CLIENT_DRIVER_VDE,
  62. .size = sizeof(VDEState),
  63. .receive = vde_receive,
  64. .cleanup = vde_cleanup,
  65. };
  66. static int net_vde_init(NetClientState *peer, const char *model,
  67. const char *name, const char *sock,
  68. int port, const char *group, int mode)
  69. {
  70. NetClientState *nc;
  71. VDEState *s;
  72. VDECONN *vde;
  73. char *init_group = (char *)group;
  74. char *init_sock = (char *)sock;
  75. struct vde_open_args args = {
  76. .port = port,
  77. .group = init_group,
  78. .mode = mode,
  79. };
  80. vde = vde_open(init_sock, (char *)"QEMU", &args);
  81. if (!vde){
  82. return -1;
  83. }
  84. nc = qemu_new_net_client(&net_vde_info, peer, model, name);
  85. snprintf(nc->info_str, sizeof(nc->info_str), "sock=%s,fd=%d",
  86. sock, vde_datafd(vde));
  87. s = DO_UPCAST(VDEState, nc, nc);
  88. s->vde = vde;
  89. qemu_set_fd_handler(vde_datafd(s->vde), vde_to_qemu, NULL, s);
  90. return 0;
  91. }
  92. int net_init_vde(const Netdev *netdev, const char *name,
  93. NetClientState *peer, Error **errp)
  94. {
  95. /* FIXME error_setg(errp, ...) on failure */
  96. const NetdevVdeOptions *vde;
  97. assert(netdev->type == NET_CLIENT_DRIVER_VDE);
  98. vde = &netdev->u.vde;
  99. /* missing optional values have been initialized to "all bits zero" */
  100. if (net_vde_init(peer, "vde", name, vde->sock, vde->port, vde->group,
  101. vde->has_mode ? vde->mode : 0700) == -1) {
  102. return -1;
  103. }
  104. return 0;
  105. }