1 #include <avahi-client/publish.h>
2 #include <avahi-client/lookup.h>
4 #include <avahi-common/alternative.h>
5 #include <avahi-common/simple-watch.h>
6 #include <avahi-common/malloc.h>
7 #include <avahi-common/error.h>
8 #include <avahi-common/timeval.h>
9 #include <avahi-common/strlst.h>
10 #include <avahi-common/thread-watch.h>
12 #include <libvirt/libvirt.h>
17 unsigned int domainid
;
18 AvahiEntryGroup
*group
;
22 typedef struct list_el domu
;
24 domu
* domus
= NULL
; /* list of domains */
25 unsigned int domu_count
= 0;
28 static AvahiStringList
* domainProperties(virDomainPtr thisDomain
) {
29 AvahiStringList
*new = NULL
;
30 // avahi_string_list_new_from_array (NULL, 0);
32 virDomainInterfaceStatsStruct stats
;
33 virDomainGetInfo(thisDomain
, &info
);
34 new = avahi_string_list_add_printf(new, "maxMem=%lu", info
.maxMem
);
35 new = avahi_string_list_add_printf(new, "memory=%lu", info
.memory
);
36 new = avahi_string_list_add_printf(new, "cpuTime=%llu", info
.cpuTime
);
38 /* TODO: multiple network interfaces :( */
40 snprintf(vifname
, 14, "vif%d.0", virDomainGetID(thisDomain
));
44 virDomainInterfaceStats(thisDomain
, vifname
, &stats
, sizeof(stats
));
46 new = avahi_string_list_add_printf(new, "interfaces=eth%d", no
);
47 new = avahi_string_list_add_printf(new, "interface_eth%d_rxbytes=%lld", no
, stats
.rx_bytes
);
48 new = avahi_string_list_add_printf(new, "interface_eth%d_rxpackets=%lld", no
, stats
.rx_packets
);
49 // new = avahi_string_list_add_printf(new, "interface_eth%d_rxerrs=%lld", no, stats.rx_errs);
50 // new = avahi_string_list_add_printf(new, "interface_eth%d_rxdrop=%lld", no, stats.rx_drop);
51 new = avahi_string_list_add_printf(new, "interface_eth%d_txbytes=%lld", no
, stats
.tx_bytes
);
52 new = avahi_string_list_add_printf(new, "interface_eth%d_txpackets=%lld", no
, stats
.tx_packets
);
53 // new = avahi_string_list_add_printf(new, "interface_eth%d_txerrs=%lld", no, stats.tx_errs);
54 // new = avahi_string_list_add_printf(new, "interface_eth%d_txdrop=%lld", no, stats.tx_drop);
59 void remove_everything() {
62 for (i
= 0; i
< domu_count
; i
++) {
63 avahi_entry_group_free(domus
[i
].group
);
64 domus
[i
].group
= NULL
;
72 void migrate_everything() {
75 conn
= virConnectOpen(NULL
);
79 AvahiThreadedPoll
*threaded_poll
= NULL
;
80 AvahiClient
*client
= NULL
;
82 if ((threaded_poll
= avahi_threaded_poll_new())) {
83 if ((client
= avahi_client_new(avahi_threaded_poll_get(threaded_poll
), 0, NULL
, NULL
, NULL
)))
84 avahi_threaded_poll_start(threaded_poll
);
87 for (i
= 0; i
< domu_count
; i
++) {
89 dom
= virDomainLookupByID (conn
, domus
[i
].domainid
);
92 if (client
== NULL
|| migrate_domain(client
, threaded_poll
, dom
) != 0) {
93 virDomainShutdown(dom
);
103 avahi_threaded_poll_stop(threaded_poll
);
104 avahi_client_free(client
);
107 avahi_threaded_poll_free(threaded_poll
);
110 virConnectClose(conn
);
116 void create_services(AvahiClient
*c
) {
117 virConnectPtr conn
= virConnectOpenReadOnly(NULL
);
120 int maxid
= virConnectNumOfDomains(conn
);
121 if (maxid
> 1) { /* ignore dom0 */
122 int *ids
= (int *) malloc(sizeof(int) * maxid
);
123 if ((maxid
= virConnectListDomains(conn
, &ids
[0], maxid
)) < 0) {
127 unsigned int domu_count_new
= (maxid
- 1);
128 domu
* domus_old
= domus
;
129 domus
= (domu
*) malloc(sizeof(domu
) * domu_count_new
);
130 const char *type
= "_domu._tcp";
132 for (i
= 0; i
< domu_count_new
; i
++) {
134 domus
[i
].group
= NULL
;
135 domus
[i
].domainid
= ids
[i
+1];
138 // fprintf(stderr, "Looking for %d\n", domus[i].domainid);
140 for (j
= 0; j
< domu_count
; j
++) {
141 // fprintf(stderr, "--> %d\n", domus_old[j].domainid);
142 if (ids
[i
+1] == domus_old
[j
].domainid
) {
143 // fprintf(stderr, "got it\n");
144 virDomainPtr thisDomain
= virDomainLookupByID(conn
, domus
[i
].domainid
);
147 name
= virDomainGetName(thisDomain
);
149 domus
[i
].group
= domus_old
[j
].group
;
150 AvahiStringList
*domainProps
;
151 domainProps
= domainProperties(thisDomain
);
152 domus_old
[j
].keep
= 1;
153 avahi_entry_group_update_service_txt_strlst(domus
[i
].group
, AVAHI_IF_UNSPEC
, AVAHI_PROTO_UNSPEC
, 0, name
, type
, NULL
, domainProps
);
154 avahi_string_list_free(domainProps
);
156 virDomainFree(thisDomain
);
160 if (i
> domu_count
|| domus
[i
].group
== NULL
) {
162 AvahiStringList
*domainProps
;
163 virDomainPtr thisDomain
= virDomainLookupByID(conn
, ids
[i
+1]);
165 domus
[i
].group
= avahi_entry_group_new(c
, entry_group_callback
, NULL
);
166 domainProps
= domainProperties(thisDomain
);
167 name
= virDomainGetName(thisDomain
);
170 avahi_entry_group_add_service_strlst(domus
[i
].group
, AVAHI_IF_UNSPEC
, AVAHI_PROTO_UNSPEC
, 0, name
, type
, NULL
, NULL
, 651, domainProps
);
171 avahi_entry_group_commit(domus
[i
].group
);
176 avahi_string_list_free(domainProps
);
177 virDomainFree(thisDomain
);
182 for (i
= 0; i
< domu_count
; i
++) {
183 // fprintf(stderr, "id: %d, keep: %d\n", domus_old[i].domainid, domus_old[i].keep);
184 if (domus_old
[i
].keep
== 0 && domus_old
[i
].group
!= NULL
) {
185 avahi_entry_group_free(domus_old
[i
].group
);
186 domus_old
[i
].group
= NULL
;
191 domu_count
= domu_count_new
;
197 virConnectClose(conn
);
198 //fprintf(stderr, "exit: %s\n", __func__);