Updates with respect to migration
[handlervirt.git] / node / publish.c
bloba0c66b1453cf7d9a53c54d3cf7936d982f02d144
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>
14 #include "node.h"
16 struct list_el {
17 unsigned int domainid;
18 AvahiEntryGroup *group;
19 unsigned short keep;
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);
31 virDomainInfo info;
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 :( */
39 char vifname[16];
40 snprintf(vifname, 14, "vif%d.0", virDomainGetID(thisDomain));
41 vifname[15] = '\0';
42 unsigned int no = 0;
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);
56 return new;
59 void remove_everything() {
60 if (domus) {
61 int i;
62 for (i = 0; i < domu_count; i++) {
63 if (domus[i].group) {
64 avahi_entry_group_free(domus[i].group);
65 domus[i].group = NULL;
68 free(domus);
69 domus = NULL;
70 domu_count = 0;
74 void migrate_everything() {
75 if (domus) {
76 virConnectPtr conn;
77 conn = virConnectOpen(NULL);
79 if (conn) {
80 int i;
81 AvahiThreadedPoll *threaded_poll = NULL;
82 AvahiClient *client = NULL;
84 if ((threaded_poll = avahi_threaded_poll_new())) {
85 if ((client = avahi_client_new(avahi_threaded_poll_get(threaded_poll), 0, NULL, NULL, NULL)))
86 avahi_threaded_poll_start(threaded_poll);
89 for (i = 0; i < domu_count; i++) {
90 virDomainPtr dom;
91 dom = virDomainLookupByID (conn, domus[i].domainid);
93 if (dom) {
94 if (client == NULL || migrate_domain(client, threaded_poll, dom) != 0) {
95 virDomainShutdown(dom);
96 virDomainFree(dom);
102 if (threaded_poll) {
103 if (client) {
104 avahi_threaded_poll_stop(threaded_poll);
105 avahi_client_free(client);
108 avahi_threaded_poll_free(threaded_poll);
111 virConnectClose(conn);
116 static void entry_group_callback_publish(AvahiEntryGroup *g, AvahiEntryGroupState state, void *userdata) {
117 domu *this = userdata;
119 switch (state) {
120 case AVAHI_ENTRY_GROUP_ESTABLISHED:
121 this->group = g;
122 break;
124 case AVAHI_ENTRY_GROUP_FAILURE:
125 case AVAHI_ENTRY_GROUP_COLLISION:
126 fprintf(stderr, "Collision\n");
127 avahi_entry_group_free(g);
128 this->group = NULL;
129 break;
131 default:
132 break;
137 void create_services(AvahiClient *c) {
138 virConnectPtr conn = virConnectOpenReadOnly(NULL);
139 if (conn == NULL)
140 return;
141 int maxid = virConnectNumOfDomains(conn);
142 if (maxid > 1) { /* ignore dom0 */
143 int *ids = (int *) malloc(sizeof(int) * maxid);
144 if ((maxid = virConnectListDomains(conn, &ids[0], maxid)) < 0) {
145 // error
146 } else {
147 int i;
148 unsigned int domu_count_new = (maxid - 1);
149 domu * domus_old = domus;
150 domus = (domu *) malloc(sizeof(domu) * domu_count_new);
151 const char *type = "_domu._tcp";
153 for (i = 0; i < domu_count_new; i++) {
154 int j;
155 domus[i].group = NULL;
156 domus[i].domainid = ids[i+1];
157 domus[i].keep = 0;
159 // fprintf(stderr, "Looking for %d\n", domus[i].domainid);
161 for (j = 0; j < domu_count; j++) {
162 // fprintf(stderr, "--> %d\n", domus_old[j].domainid);
163 if (ids[i+1] == domus_old[j].domainid) {
164 // fprintf(stderr, "got it\n");
165 virDomainPtr thisDomain = virDomainLookupByID(conn, domus[i].domainid);
166 if (thisDomain) {
167 const char *name;
168 name = virDomainGetName(thisDomain);
169 if (name && domus_old[j].group) {
170 domus[i].group = domus_old[j].group;
171 AvahiStringList *domainProps;
172 domainProps = domainProperties(thisDomain);
173 domus_old[j].keep = 1;
174 avahi_entry_group_update_service_txt_strlst(domus[i].group, AVAHI_IF_UNSPEC, AVAHI_PROTO_UNSPEC, 0, name, type, NULL, domainProps);
175 avahi_string_list_free(domainProps);
177 virDomainFree(thisDomain);
181 if (i > domu_count || domus[i].group == NULL) {
182 const char *name;
183 AvahiStringList *domainProps;
184 virDomainPtr thisDomain = virDomainLookupByID(conn, ids[i+1]);
185 if (thisDomain) {
186 AvahiEntryGroup *group;
187 group = avahi_entry_group_new(c, entry_group_callback_publish, &domus[i]);
188 domainProps = domainProperties(thisDomain);
189 name = virDomainGetName(thisDomain);
191 if (name) {
192 avahi_entry_group_add_service_strlst(group, AVAHI_IF_UNSPEC, AVAHI_PROTO_UNSPEC, 0, name, type, NULL, NULL, 651, domainProps);
193 avahi_entry_group_commit(group);
194 domus[i].keep = 1;
195 } else {
196 domus[i].keep = 0;
199 avahi_string_list_free(domainProps);
200 virDomainFree(thisDomain);
205 for (i = 0; i < domu_count; i++) {
206 // fprintf(stderr, "id: %d, keep: %d\n", domus_old[i].domainid, domus_old[i].keep);
207 if (domus_old[i].keep == 0 && domus_old[i].group != NULL) {
208 avahi_entry_group_free(domus_old[i].group);
209 domus_old[i].group = NULL;
213 free(domus_old);
214 domu_count = domu_count_new;
216 free(ids);
217 } else {
218 remove_everything();
220 virConnectClose(conn);
221 //fprintf(stderr, "exit: %s\n", __func__);