test-filter-mirror.c 2.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394
  1. /*
  2. * QTest testcase for filter-mirror
  3. *
  4. * Copyright (c) 2016 FUJITSU LIMITED
  5. * Author: Zhang Chen <zhangchen.fnst@cn.fujitsu.com>
  6. *
  7. * This work is licensed under the terms of the GNU GPL, version 2 or
  8. * later. See the COPYING file in the top-level directory.
  9. */
  10. #include "qemu/osdep.h"
  11. #include "qemu-common.h"
  12. #include "libqtest.h"
  13. #include "qapi/qmp/qdict.h"
  14. #include "qemu/iov.h"
  15. #include "qemu/sockets.h"
  16. #include "qemu/error-report.h"
  17. #include "qemu/main-loop.h"
  18. /* TODO actually test the results and get rid of this */
  19. #define qmp_discard_response(qs, ...) qobject_unref(qtest_qmp(qs, __VA_ARGS__))
  20. static void test_mirror(void)
  21. {
  22. int send_sock[2], recv_sock[2];
  23. uint32_t ret = 0, len = 0;
  24. char send_buf[] = "Hello! filter-mirror~";
  25. char *recv_buf;
  26. uint32_t size = sizeof(send_buf);
  27. size = htonl(size);
  28. const char *devstr = "e1000";
  29. QTestState *qts;
  30. if (g_str_equal(qtest_get_arch(), "s390x")) {
  31. devstr = "virtio-net-ccw";
  32. }
  33. ret = socketpair(PF_UNIX, SOCK_STREAM, 0, send_sock);
  34. g_assert_cmpint(ret, !=, -1);
  35. ret = socketpair(PF_UNIX, SOCK_STREAM, 0, recv_sock);
  36. g_assert_cmpint(ret, !=, -1);
  37. qts = qtest_initf(
  38. "-netdev socket,id=qtest-bn0,fd=%d "
  39. "-device %s,netdev=qtest-bn0,id=qtest-e0 "
  40. "-chardev socket,id=mirror0,fd=%d "
  41. "-object filter-mirror,id=qtest-f0,netdev=qtest-bn0,queue=tx,outdev=mirror0 "
  42. , send_sock[1], devstr, recv_sock[1]);
  43. struct iovec iov[] = {
  44. {
  45. .iov_base = &size,
  46. .iov_len = sizeof(size),
  47. }, {
  48. .iov_base = send_buf,
  49. .iov_len = sizeof(send_buf),
  50. },
  51. };
  52. /* send a qmp command to guarantee that 'connected' is setting to true. */
  53. qmp_discard_response(qts, "{ 'execute' : 'query-status'}");
  54. ret = iov_send(send_sock[0], iov, 2, 0, sizeof(size) + sizeof(send_buf));
  55. g_assert_cmpint(ret, ==, sizeof(send_buf) + sizeof(size));
  56. close(send_sock[0]);
  57. ret = qemu_recv(recv_sock[0], &len, sizeof(len), 0);
  58. g_assert_cmpint(ret, ==, sizeof(len));
  59. len = ntohl(len);
  60. g_assert_cmpint(len, ==, sizeof(send_buf));
  61. recv_buf = g_malloc(len);
  62. ret = qemu_recv(recv_sock[0], recv_buf, len, 0);
  63. g_assert_cmpstr(recv_buf, ==, send_buf);
  64. g_free(recv_buf);
  65. close(send_sock[0]);
  66. close(send_sock[1]);
  67. close(recv_sock[0]);
  68. close(recv_sock[1]);
  69. qtest_quit(qts);
  70. }
  71. int main(int argc, char **argv)
  72. {
  73. int ret;
  74. g_test_init(&argc, &argv, NULL);
  75. qtest_add_func("/netfilter/mirror", test_mirror);
  76. ret = g_test_run();
  77. return ret;
  78. }