2
0

net-hmp-cmds.c 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170
  1. /*
  2. * Human Monitor Interface commands
  3. *
  4. * Copyright IBM, Corp. 2011
  5. *
  6. * Authors:
  7. * Anthony Liguori <aliguori@us.ibm.com>
  8. *
  9. * This work is licensed under the terms of the GNU GPL, version 2. See
  10. * the COPYING file in the top-level directory.
  11. *
  12. * Contributions after 2012-01-13 are licensed under the terms of the
  13. * GNU GPL, version 2 or (at your option) any later version.
  14. */
  15. #include "qemu/osdep.h"
  16. #include "migration/misc.h"
  17. #include "monitor/hmp.h"
  18. #include "monitor/monitor.h"
  19. #include "net/net.h"
  20. #include "net/hub.h"
  21. #include "qapi/clone-visitor.h"
  22. #include "qapi/qapi-commands-net.h"
  23. #include "qapi/qapi-visit-net.h"
  24. #include "qobject/qdict.h"
  25. #include "qemu/config-file.h"
  26. #include "qemu/help_option.h"
  27. #include "qemu/option.h"
  28. void hmp_info_network(Monitor *mon, const QDict *qdict)
  29. {
  30. NetClientState *nc, *peer;
  31. NetClientDriver type;
  32. net_hub_info(mon);
  33. QTAILQ_FOREACH(nc, &net_clients, next) {
  34. peer = nc->peer;
  35. type = nc->info->type;
  36. /* Skip if already printed in hub info */
  37. if (net_hub_id_for_client(nc, NULL) == 0) {
  38. continue;
  39. }
  40. if (!peer || type == NET_CLIENT_DRIVER_NIC) {
  41. print_net_client(mon, nc);
  42. } /* else it's a netdev connected to a NIC, printed with the NIC */
  43. if (peer && type == NET_CLIENT_DRIVER_NIC) {
  44. monitor_printf(mon, " \\ ");
  45. print_net_client(mon, peer);
  46. }
  47. }
  48. }
  49. void hmp_set_link(Monitor *mon, const QDict *qdict)
  50. {
  51. const char *name = qdict_get_str(qdict, "name");
  52. bool up = qdict_get_bool(qdict, "up");
  53. Error *err = NULL;
  54. qmp_set_link(name, up, &err);
  55. hmp_handle_error(mon, err);
  56. }
  57. void hmp_announce_self(Monitor *mon, const QDict *qdict)
  58. {
  59. const char *interfaces_str = qdict_get_try_str(qdict, "interfaces");
  60. const char *id = qdict_get_try_str(qdict, "id");
  61. AnnounceParameters *params = QAPI_CLONE(AnnounceParameters,
  62. migrate_announce_params());
  63. qapi_free_strList(params->interfaces);
  64. params->interfaces = hmp_split_at_comma(interfaces_str);
  65. params->has_interfaces = params->interfaces != NULL;
  66. params->id = g_strdup(id);
  67. qmp_announce_self(params, NULL);
  68. qapi_free_AnnounceParameters(params);
  69. }
  70. void hmp_netdev_add(Monitor *mon, const QDict *qdict)
  71. {
  72. Error *err = NULL;
  73. QemuOpts *opts;
  74. const char *type = qdict_get_try_str(qdict, "type");
  75. if (type && is_help_option(type)) {
  76. show_netdevs();
  77. return;
  78. }
  79. opts = qemu_opts_from_qdict(qemu_find_opts("netdev"), qdict, &err);
  80. if (err) {
  81. goto out;
  82. }
  83. netdev_add(opts, &err);
  84. if (err) {
  85. qemu_opts_del(opts);
  86. }
  87. out:
  88. hmp_handle_error(mon, err);
  89. }
  90. void hmp_netdev_del(Monitor *mon, const QDict *qdict)
  91. {
  92. const char *id = qdict_get_str(qdict, "id");
  93. Error *err = NULL;
  94. qmp_netdev_del(id, &err);
  95. hmp_handle_error(mon, err);
  96. }
  97. void netdev_add_completion(ReadLineState *rs, int nb_args, const char *str)
  98. {
  99. size_t len;
  100. int i;
  101. if (nb_args != 2) {
  102. return;
  103. }
  104. len = strlen(str);
  105. readline_set_completion_index(rs, len);
  106. for (i = 0; i < NET_CLIENT_DRIVER__MAX; i++) {
  107. readline_add_completion_of(rs, str, NetClientDriver_str(i));
  108. }
  109. }
  110. void set_link_completion(ReadLineState *rs, int nb_args, const char *str)
  111. {
  112. size_t len;
  113. len = strlen(str);
  114. readline_set_completion_index(rs, len);
  115. if (nb_args == 2) {
  116. NetClientState *ncs[MAX_QUEUE_NUM];
  117. int count, i;
  118. count = qemu_find_net_clients_except(NULL, ncs,
  119. NET_CLIENT_DRIVER_NONE,
  120. MAX_QUEUE_NUM);
  121. for (i = 0; i < MIN(count, MAX_QUEUE_NUM); i++) {
  122. readline_add_completion_of(rs, str, ncs[i]->name);
  123. }
  124. } else if (nb_args == 3) {
  125. readline_add_completion_of(rs, str, "on");
  126. readline_add_completion_of(rs, str, "off");
  127. }
  128. }
  129. void netdev_del_completion(ReadLineState *rs, int nb_args, const char *str)
  130. {
  131. int len, count, i;
  132. NetClientState *ncs[MAX_QUEUE_NUM];
  133. if (nb_args != 2) {
  134. return;
  135. }
  136. len = strlen(str);
  137. readline_set_completion_index(rs, len);
  138. count = qemu_find_net_clients_except(NULL, ncs, NET_CLIENT_DRIVER_NIC,
  139. MAX_QUEUE_NUM);
  140. for (i = 0; i < MIN(count, MAX_QUEUE_NUM); i++) {
  141. if (ncs[i]->is_netdev) {
  142. readline_add_completion_of(rs, str, ncs[i]->name);
  143. }
  144. }
  145. }