Example #1
0
static struct vhost_scsi_ctrlr *
vhost_scsi_ctrlr_construct(const char *ctrlr_name)
{
	int ret;
	struct vhost_scsi_ctrlr *ctrlr;
	char *path;
	char cwd[PATH_MAX];

	/* always use current directory */
	path = getcwd(cwd, PATH_MAX);
	if (!path) {
		fprintf(stderr, "Cannot get current working directory\n");
		return NULL;
	}
	snprintf(dev_pathname, sizeof(dev_pathname), "%s/%s", path, ctrlr_name);

	if (access(dev_pathname, F_OK) != -1) {
		if (unlink(dev_pathname) != 0)
			rte_exit(EXIT_FAILURE, "Cannot remove %s.\n",
				 dev_pathname);
	}

	if (rte_vhost_driver_register(dev_pathname, 0) != 0) {
		fprintf(stderr, "socket %s already exists\n", dev_pathname);
		return NULL;
	}

	fprintf(stdout, "socket file: %s created\n", dev_pathname);

	ret = rte_vhost_driver_set_features(dev_pathname, VIRTIO_SCSI_FEATURES);
	if (ret != 0) {
		fprintf(stderr, "Set vhost driver features failed\n");
		return NULL;
	}

	ctrlr = rte_zmalloc(NULL, sizeof(*ctrlr), RTE_CACHE_LINE_SIZE);
	if (!ctrlr)
		return NULL;

	rte_vhost_driver_callback_register(dev_pathname,
					   &vhost_scsi_device_ops);

	return ctrlr;
}
Example #2
0
// Attach a VIF to a vrf
struct vif* vif_add(char* name, uint8_t* ip, uint8_t mask, uint8_t* macaddr,
    uint32_t label, char* path, int cpus, int cpusets[])
{
    int i;
    struct vif* vif = (struct vif*) malloc (sizeof(struct vif));
    if (!vif) {
        log_crit("Failed to allocated memory for vif struct (%s)\n", name);
        return NULL;
    }

    strcpy(vif->name, name);
    vif->label = label;
    vif->mask = mask;
    memcpy(vif->ip, ip, 4);
    memcpy(vif->macaddr, macaddr, 4);
    strcpy(vif->path, path);
    rte_atomic64_clear(&vif->rx_packets);
    rte_atomic64_clear(&vif->tx_packets);
    rte_atomic64_clear(&vif->dropped_packets);
    rte_atomic64_clear(&vif->error_packets);

    vif->cpus = cpus;
    for (i = 0; i < cpus; i++) {
        CPU_ZERO(&vif->cpusets[i]);
        CPU_SET(cpusets[i], &vif->cpusets[i]);
    }

    vif->lldev = NULL;

    // Add route to this VM in VRF/RIB.
    vif->nh.data = vif;
    vif->nh.fn = virtio_tx_packet;
    ipv4_route_add(label, ip, &vif->nh);

    /* Create VHOST-User socket */
    unlink(vif->path);
    if (rte_vhost_driver_register(vif->path) < 0) {
        free(vif);
        return NULL;
    }

    return vif;
}