1 #include "qemu/osdep.h" 2 #include "hw/xen/xen_backend.h" 3 #include "qemu/option.h" 4 #include "sysemu/blockdev.h" 5 6 /* ------------------------------------------------------------- */ 7 8 static int xen_config_dev_dirs(const char *ftype, const char *btype, int vdev, 9 char *fe, char *be, int len) 10 { 11 char *dom; 12 13 dom = xs_get_domain_path(xenstore, xen_domid); 14 snprintf(fe, len, "%s/device/%s/%d", dom, ftype, vdev); 15 free(dom); 16 17 dom = xs_get_domain_path(xenstore, 0); 18 snprintf(be, len, "%s/backend/%s/%d/%d", dom, btype, xen_domid, vdev); 19 free(dom); 20 21 xenstore_mkdir(fe, XS_PERM_READ | XS_PERM_WRITE); 22 xenstore_mkdir(be, XS_PERM_READ); 23 return 0; 24 } 25 26 static int xen_config_dev_all(char *fe, char *be) 27 { 28 /* frontend */ 29 if (xen_protocol) 30 xenstore_write_str(fe, "protocol", xen_protocol); 31 32 xenstore_write_int(fe, "state", XenbusStateInitialising); 33 xenstore_write_int(fe, "backend-id", 0); 34 xenstore_write_str(fe, "backend", be); 35 36 /* backend */ 37 xenstore_write_str(be, "domain", qemu_name ? qemu_name : "no-name"); 38 xenstore_write_int(be, "online", 1); 39 xenstore_write_int(be, "state", XenbusStateInitialising); 40 xenstore_write_int(be, "frontend-id", xen_domid); 41 xenstore_write_str(be, "frontend", fe); 42 43 return 0; 44 } 45 46 /* ------------------------------------------------------------- */ 47 48 int xen_config_dev_blk(DriveInfo *disk) 49 { 50 char fe[256], be[256], device_name[32]; 51 int vdev = 202 * 256 + 16 * disk->unit; 52 int cdrom = disk->media_cd; 53 const char *devtype = cdrom ? "cdrom" : "disk"; 54 const char *mode = cdrom ? "r" : "w"; 55 const char *filename = qemu_opt_get(disk->opts, "file"); 56 57 snprintf(device_name, sizeof(device_name), "xvd%c", 'a' + disk->unit); 58 xen_pv_printf(NULL, 1, "config disk %d [%s]: %s\n", 59 disk->unit, device_name, filename); 60 xen_config_dev_dirs("vbd", "qdisk", vdev, fe, be, sizeof(fe)); 61 62 /* frontend */ 63 xenstore_write_int(fe, "virtual-device", vdev); 64 xenstore_write_str(fe, "device-type", devtype); 65 66 /* backend */ 67 xenstore_write_str(be, "dev", device_name); 68 xenstore_write_str(be, "type", "file"); 69 xenstore_write_str(be, "params", filename); 70 xenstore_write_str(be, "mode", mode); 71 72 /* common stuff */ 73 return xen_config_dev_all(fe, be); 74 } 75 76 int xen_config_dev_nic(NICInfo *nic) 77 { 78 char fe[256], be[256]; 79 char mac[20]; 80 int vlan_id = -1; 81 82 net_hub_id_for_client(nic->netdev, &vlan_id); 83 snprintf(mac, sizeof(mac), "%02x:%02x:%02x:%02x:%02x:%02x", 84 nic->macaddr.a[0], nic->macaddr.a[1], nic->macaddr.a[2], 85 nic->macaddr.a[3], nic->macaddr.a[4], nic->macaddr.a[5]); 86 xen_pv_printf(NULL, 1, "config nic %d: mac=\"%s\"\n", vlan_id, mac); 87 xen_config_dev_dirs("vif", "qnic", vlan_id, fe, be, sizeof(fe)); 88 89 /* frontend */ 90 xenstore_write_int(fe, "handle", vlan_id); 91 xenstore_write_str(fe, "mac", mac); 92 93 /* backend */ 94 xenstore_write_int(be, "handle", vlan_id); 95 xenstore_write_str(be, "mac", mac); 96 97 /* common stuff */ 98 return xen_config_dev_all(fe, be); 99 } 100 101 int xen_config_dev_vfb(int vdev, const char *type) 102 { 103 char fe[256], be[256]; 104 105 xen_config_dev_dirs("vfb", "vfb", vdev, fe, be, sizeof(fe)); 106 107 /* backend */ 108 xenstore_write_str(be, "type", type); 109 110 /* common stuff */ 111 return xen_config_dev_all(fe, be); 112 } 113 114 int xen_config_dev_vkbd(int vdev) 115 { 116 char fe[256], be[256]; 117 118 xen_config_dev_dirs("vkbd", "vkbd", vdev, fe, be, sizeof(fe)); 119 return xen_config_dev_all(fe, be); 120 } 121 122 int xen_config_dev_console(int vdev) 123 { 124 char fe[256], be[256]; 125 126 xen_config_dev_dirs("console", "console", vdev, fe, be, sizeof(fe)); 127 return xen_config_dev_all(fe, be); 128 } 129