announce.c 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215
  1. /*
  2. * Self-announce
  3. * (c) 2017-2019 Red Hat, Inc.
  4. *
  5. * This work is licensed under the terms of the GNU GPL, version 2 or later.
  6. * See the COPYING file in the top-level directory.
  7. */
  8. #include "qemu/osdep.h"
  9. #include "qemu/cutils.h"
  10. #include "net/announce.h"
  11. #include "net/net.h"
  12. #include "qapi/clone-visitor.h"
  13. #include "qapi/qapi-visit-net.h"
  14. #include "qapi/qapi-commands-net.h"
  15. #include "trace.h"
  16. static GData *named_timers;
  17. int64_t qemu_announce_timer_step(AnnounceTimer *timer)
  18. {
  19. int64_t step;
  20. step = timer->params.initial +
  21. (timer->params.rounds - timer->round - 1) *
  22. timer->params.step;
  23. if (step < 0 || step > timer->params.max) {
  24. step = timer->params.max;
  25. }
  26. timer_mod(timer->tm, qemu_clock_get_ms(timer->type) + step);
  27. return step;
  28. }
  29. /*
  30. * If 'free_named' is true, then remove the timer from the list
  31. * and free the timer itself.
  32. */
  33. void qemu_announce_timer_del(AnnounceTimer *timer, bool free_named)
  34. {
  35. bool free_timer = false;
  36. if (timer->tm) {
  37. timer_free(timer->tm);
  38. timer->tm = NULL;
  39. }
  40. qapi_free_strList(timer->params.interfaces);
  41. timer->params.interfaces = NULL;
  42. if (free_named && timer->params.id) {
  43. AnnounceTimer *list_timer;
  44. /*
  45. * Sanity check: There should only be one timer on the list with
  46. * the id.
  47. */
  48. list_timer = g_datalist_get_data(&named_timers, timer->params.id);
  49. assert(timer == list_timer);
  50. free_timer = true;
  51. g_datalist_remove_data(&named_timers, timer->params.id);
  52. }
  53. trace_qemu_announce_timer_del(free_named, free_timer, timer->params.id);
  54. g_free(timer->params.id);
  55. timer->params.id = NULL;
  56. if (free_timer) {
  57. g_free(timer);
  58. }
  59. }
  60. /*
  61. * Under BQL/main thread
  62. * Reset the timer to the given parameters/type/notifier.
  63. */
  64. void qemu_announce_timer_reset(AnnounceTimer *timer,
  65. AnnounceParameters *params,
  66. QEMUClockType type,
  67. QEMUTimerCB *cb,
  68. void *opaque)
  69. {
  70. /*
  71. * We're under the BQL, so the current timer can't
  72. * be firing, so we should be able to delete it.
  73. */
  74. qemu_announce_timer_del(timer, false);
  75. QAPI_CLONE_MEMBERS(AnnounceParameters, &timer->params, params);
  76. timer->round = params->rounds;
  77. timer->type = type;
  78. timer->tm = timer_new_ms(type, cb, opaque);
  79. }
  80. #ifndef ETH_P_RARP
  81. #define ETH_P_RARP 0x8035
  82. #endif
  83. #define ARP_HTYPE_ETH 0x0001
  84. #define ARP_PTYPE_IP 0x0800
  85. #define ARP_OP_REQUEST_REV 0x3
  86. static int announce_self_create(uint8_t *buf,
  87. uint8_t *mac_addr)
  88. {
  89. /* Ethernet header. */
  90. memset(buf, 0xff, 6); /* destination MAC addr */
  91. memcpy(buf + 6, mac_addr, 6); /* source MAC addr */
  92. *(uint16_t *)(buf + 12) = htons(ETH_P_RARP); /* ethertype */
  93. /* RARP header. */
  94. *(uint16_t *)(buf + 14) = htons(ARP_HTYPE_ETH); /* hardware addr space */
  95. *(uint16_t *)(buf + 16) = htons(ARP_PTYPE_IP); /* protocol addr space */
  96. *(buf + 18) = 6; /* hardware addr length (ethernet) */
  97. *(buf + 19) = 4; /* protocol addr length (IPv4) */
  98. *(uint16_t *)(buf + 20) = htons(ARP_OP_REQUEST_REV); /* opcode */
  99. memcpy(buf + 22, mac_addr, 6); /* source hw addr */
  100. memset(buf + 28, 0x00, 4); /* source protocol addr */
  101. memcpy(buf + 32, mac_addr, 6); /* target hw addr */
  102. memset(buf + 38, 0x00, 4); /* target protocol addr */
  103. /* Padding to get up to 60 bytes (ethernet min packet size, minus FCS). */
  104. memset(buf + 42, 0x00, 18);
  105. return 60; /* len (FCS will be added by hardware) */
  106. }
  107. /*
  108. * Helper to print ethernet mac address
  109. */
  110. static const char *qemu_ether_ntoa(const MACAddr *mac)
  111. {
  112. static char ret[18];
  113. snprintf(ret, sizeof(ret), "%02x:%02x:%02x:%02x:%02x:%02x",
  114. mac->a[0], mac->a[1], mac->a[2], mac->a[3], mac->a[4], mac->a[5]);
  115. return ret;
  116. }
  117. static void qemu_announce_self_iter(NICState *nic, void *opaque)
  118. {
  119. AnnounceTimer *timer = opaque;
  120. uint8_t buf[60];
  121. int len;
  122. bool skip;
  123. if (timer->params.has_interfaces) {
  124. strList *entry = timer->params.interfaces;
  125. /* Skip unless we find our name in the requested list */
  126. skip = true;
  127. while (entry) {
  128. if (!strcmp(entry->value, nic->ncs->name)) {
  129. /* Found us */
  130. skip = false;
  131. break;
  132. }
  133. entry = entry->next;
  134. }
  135. } else {
  136. skip = false;
  137. }
  138. trace_qemu_announce_self_iter(timer->params.id ?: "_",
  139. nic->ncs->name,
  140. qemu_ether_ntoa(&nic->conf->macaddr), skip);
  141. if (!skip) {
  142. len = announce_self_create(buf, nic->conf->macaddr.a);
  143. qemu_send_packet_raw(qemu_get_queue(nic), buf, len);
  144. /* if the NIC provides it's own announcement support, use it as well */
  145. if (nic->ncs->info->announce) {
  146. nic->ncs->info->announce(nic->ncs);
  147. }
  148. }
  149. }
  150. static void qemu_announce_self_once(void *opaque)
  151. {
  152. AnnounceTimer *timer = (AnnounceTimer *)opaque;
  153. qemu_foreach_nic(qemu_announce_self_iter, timer);
  154. if (--timer->round) {
  155. qemu_announce_timer_step(timer);
  156. } else {
  157. qemu_announce_timer_del(timer, true);
  158. }
  159. }
  160. void qemu_announce_self(AnnounceTimer *timer, AnnounceParameters *params)
  161. {
  162. qemu_announce_timer_reset(timer, params, QEMU_CLOCK_REALTIME,
  163. qemu_announce_self_once, timer);
  164. if (params->rounds) {
  165. qemu_announce_self_once(timer);
  166. } else {
  167. qemu_announce_timer_del(timer, true);
  168. }
  169. }
  170. void qmp_announce_self(AnnounceParameters *params, Error **errp)
  171. {
  172. AnnounceTimer *named_timer;
  173. if (!params->id) {
  174. params->id = g_strdup("");
  175. }
  176. named_timer = g_datalist_get_data(&named_timers, params->id);
  177. if (!named_timer) {
  178. named_timer = g_new0(AnnounceTimer, 1);
  179. g_datalist_set_data(&named_timers, params->id, named_timer);
  180. }
  181. qemu_announce_self(named_timer, params);
  182. }