Skip to content

Commit

Permalink
vhost cleanup!
Browse files Browse the repository at this point in the history
  • Loading branch information
Cloud User authored and outscale-fne committed Sep 8, 2020
1 parent 83e4660 commit 5915deb
Show file tree
Hide file tree
Showing 6 changed files with 197 additions and 61 deletions.
37 changes: 21 additions & 16 deletions src/utils/qemu.c
Original file line number Diff line number Diff line change
Expand Up @@ -67,27 +67,28 @@ int pg_util_ssh(const char *host,
}

int pg_util_spawn_qemu(const char *socket_path_0,
const char *socket_path_1,
const char *mac_0,
const char *mac_1,
const int is_server_0,
const int is_server_1,
const char *vm_image_path,
const char *vm_key_path,
const char *hugepages_path,
struct pg_error **errp)
const char *socket_path_1,
const char *mac_0,
const char *mac_1,
const int is_server_0,
const int is_server_1,
const char *vm_image_path,
const char *vm_key_path,
const char *hugepages_path,
struct pg_error **errp)
{
int child_pid = 0;
static uint16_t vm_id;
char **argv = NULL;
pg_autofree char *argv_qemu = NULL;
const char *argv_sock_0 = "";
const char *argv_sock_1 = "";
const char* server_params = ",server,nowait";
const char *server_params = ",server,nowait";
pg_autofree char *argv_sock_0_t = NULL;
pg_autofree char *argv_sock_1_t = NULL;
pg_autofree char *ssh_cmd = NULL;
GError *error = NULL;

if(!is_server_1)
g_assert(g_file_test(socket_path_0, G_FILE_TEST_EXISTS));
if (socket_path_1 && !is_server_1)
Expand All @@ -102,7 +103,9 @@ int pg_util_spawn_qemu(const char *socket_path_0,
" -netdev type=vhost-user,id=mynet0,",
"chardev=char0,vhostforce",
" -device virtio-net-pci,mac=%s",
",netdev=mynet0"), socket_path_0, is_server_0 ? server_params : "", mac_0);
",netdev=mynet0"), socket_path_0,
is_server_0 ? server_params : "",
mac_0);
argv_sock_0 = argv_sock_0_t;
}

Expand All @@ -112,7 +115,9 @@ int pg_util_spawn_qemu(const char *socket_path_0,
" -netdev type=vhost-user,id=mynet1,",
"chardev=char1,vhostforce",
" -device virtio-net-pci,mac=%s",
",netdev=mynet1"), socket_path_1, is_server_0 ? server_params : "", mac_1);
",netdev=mynet1"), socket_path_1,
is_server_0 ? server_params : "",
mac_1);
argv_sock_1 = argv_sock_1_t;
}

Expand All @@ -136,10 +141,10 @@ int pg_util_spawn_qemu(const char *socket_path_0,
argv = g_strsplit(argv_qemu, " ", 0);

g_assert(g_spawn_async(NULL, argv, NULL,
(GSpawnFlags) G_SPAWN_SEARCH_PATH |
G_SPAWN_DO_NOT_REAP_CHILD,
(GSpawnChildSetupFunc) NULL,
NULL, &child_pid, &error));
(GSpawnFlags) G_SPAWN_SEARCH_PATH |
G_SPAWN_DO_NOT_REAP_CHILD,
(GSpawnChildSetupFunc) NULL,
NULL, &child_pid, &error));
g_assert(!error);

ssh_cmd = g_strdup_printf(
Expand Down
14 changes: 7 additions & 7 deletions src/utils/qemu.h
Original file line number Diff line number Diff line change
Expand Up @@ -29,15 +29,15 @@ int pg_util_ssh(const char *host,
__attribute__((__format__(__printf__, 4, 5)));

int pg_util_spawn_qemu(const char *socket_path_0,
const char *socket_path_1,
const char *mac_0,
const char *mac_1,
const char *socket_path_1,
const char *mac_0,
const char *mac_1,
const int is_server_0,
const int is_server_1,
const char *vm_image_path,
const char *vm_ssh_key_path,
const char *hugepages_path,
struct pg_error **errp);
const char *vm_image_path,
const char *vm_ssh_key_path,
const char *hugepages_path,
struct pg_error **errp);

void pg_util_stop_qemu(int qemu_pid, int signal);

Expand Down
65 changes: 28 additions & 37 deletions src/vhost.c
Original file line number Diff line number Diff line change
Expand Up @@ -117,7 +117,7 @@ static struct pg_brick_config *vhost_config_new(const char *name,
{
struct pg_brick_config *config = g_new0(struct pg_brick_config, 1);
struct pg_vhost_config *vhost_config = g_new0(struct pg_vhost_config,
1);
1);

config->brick_config = (void *) vhost_config;
vhost_config->flags = flags;
Expand All @@ -127,17 +127,18 @@ static struct pg_brick_config *vhost_config_new(const char *name,
/* To know if a vhost brick is server or not. */
static int pg_vhost_is_server(struct pg_brick *brick)
{
struct pg_vhost_state *state =
pg_brick_get_state(brick, struct pg_vhost_state);
struct pg_vhost_state *state =
pg_brick_get_state(brick,
struct pg_vhost_state);

if(state->flags & 1)
return 0;
return 1;
if(state->flags & 1)
return 0;
return -1;
}

static int vhost_burst(struct pg_brick *brick, enum pg_side from,
uint16_t edge_index, struct rte_mbuf **pkts,
uint64_t pkts_mask, struct pg_error **errp)
uint16_t edge_index, struct rte_mbuf **pkts,
uint64_t pkts_mask, struct pg_error **errp)
{
struct pg_vhost_state *state =
pg_brick_get_state(brick, struct pg_vhost_state);
Expand All @@ -149,24 +150,24 @@ static int vhost_burst(struct pg_brick *brick, enum pg_side from,

/* Try lock */
if (unlikely(check_atomic &&
!rte_atomic32_test_and_set(&state->allow_queuing)))
!rte_atomic32_test_and_set(&state->allow_queuing)))
return 0;
virtio_net = state->vid;
pkts_count = pg_packets_pack(state->out, pkts, pkts_mask);
if (pkts_count > 32) {
bursted_pkts = rte_vhost_enqueue_burst(virtio_net,
VIRTIO_RXQ,
state->out,
32);
VIRTIO_RXQ,
state->out,
32);
bursted_pkts += rte_vhost_enqueue_burst(virtio_net,
VIRTIO_RXQ,
state->out + 32,
pkts_count - 32);
} else {
bursted_pkts = rte_vhost_enqueue_burst(virtio_net,
VIRTIO_RXQ,
state->out,
pkts_count);
VIRTIO_RXQ,
state->out,
pkts_count);

}
if (check_atomic)
Expand All @@ -182,7 +183,7 @@ static int vhost_burst(struct pg_brick *brick, enum pg_side from,

if (side->burst_count_cb != NULL)
side->burst_count_cb(side->burst_count_private_data,
bursted_pkts);
bursted_pkts);
#endif /* #ifdef PG_VHOST_BENCH */
return 0;
}
Expand All @@ -202,7 +203,7 @@ void pg_vhost_request_remove(struct pg_brick *brick)
#endif /* PG_VHOST_FASTER_YET_BROKEN_POLL */

static int vhost_poll(struct pg_brick *brick, uint16_t *pkts_cnt,
struct pg_error **errp)
struct pg_error **errp)
{
struct pg_vhost_state *state =
pg_brick_get_state(brick, struct pg_vhost_state);
Expand Down Expand Up @@ -262,7 +263,7 @@ static int vhost_poll(struct pg_brick *brick, uint16_t *pkts_cnt,

pkts_mask = pg_mask_firsts(count);
ret = pg_brick_burst(s->edge.link, state->output, s->edge.pair_index,
in, pkts_mask, errp);
in, pkts_mask, errp);
pg_packets_free(in, pkts_mask);
return ret;
}
Expand All @@ -289,7 +290,7 @@ static enum pg_side vhost_get_side(struct pg_brick *brick)
}

struct pg_brick *pg_vhost_new(const char *name, uint64_t flags,
struct pg_error **errp)
struct pg_error **errp)
{
struct pg_brick_config *config = vhost_config_new(name, flags);
struct pg_brick *ret = pg_brick_new("vhost", config, errp);
Expand Down Expand Up @@ -367,13 +368,11 @@ static void on_destroy_device(int dev)
rte_vhost_get_ifname(dev, buf, 256);

printf("VM disconnecting from socket: %s\n", buf);

pthread_mutex_lock(&mutex);

LIST_FOREACH(s, &sockets, socket_list) {
if (!strcmp(s->path, buf)) {
while (!rte_atomic32_test_and_set(
&s->state->allow_queuing))
&s->state->allow_queuing))
sched_yield();
#ifdef PG_VHOST_FASTER_YET_BROKEN_POLL
s->state->check_atomic = 1024;
Expand All @@ -383,9 +382,7 @@ static void on_destroy_device(int dev)
break;
}
}

pthread_mutex_unlock(&mutex);

}
static void vhost_create_socket(struct pg_vhost_state *state, uint64_t flags,
struct pg_error **errp)
Expand All @@ -397,17 +394,11 @@ static void vhost_create_socket(struct pg_vhost_state *state, uint64_t flags,
path = g_strdup_printf("%s/qemu-%s", sockets_path, state->brick.name);
/* If the socket is CLIENT do NOT destroy the socket. */
if((flags & 1) == 0)
{
g_remove(path);
}
/*else
{
flags = flags | 31;
}
*/

printf("New vhost-user socket: %s, zero-copy %s\n", path,
(flags & RTE_VHOST_USER_DEQUEUE_ZERO_COPY) ?
"enable" : "disable");
(flags & RTE_VHOST_USER_DEQUEUE_ZERO_COPY) ?
"enable" : "disable");

flags = flags & (RTE_VHOST_USER_CLIENT | RTE_VHOST_USER_NO_RECONNECT |
RTE_VHOST_USER_DEQUEUE_ZERO_COPY);
Expand Down Expand Up @@ -454,7 +445,7 @@ static void vhost_create_socket(struct pg_vhost_state *state, uint64_t flags,
"vhost not ready, did you called vhost_start after packetgraph_start ?"

static int vhost_init(struct pg_brick *brick, struct pg_brick_config *config,
struct pg_error **errp)
struct pg_error **errp)
{
struct pg_vhost_state *state;
struct pg_vhost_config *vhost_config;
Expand Down Expand Up @@ -501,7 +492,7 @@ static int vhost_init(struct pg_brick *brick, struct pg_brick_config *config,
*
*/
static void check_and_store_base_dir(const char *base_dir,
struct pg_error **errp)
struct pg_error **errp)
{
char *resolved_path = g_malloc0(PATH_MAX);
struct stat st;
Expand Down Expand Up @@ -580,8 +571,8 @@ static struct pg_brick_ops vhost_ops = {
.init = vhost_init,
.destroy = vhost_destroy,

.link_notify = vhost_link,
.get_side = vhost_get_side,
.link_notify = vhost_link,
.get_side = vhost_get_side,
.unlink = pg_brick_generic_unlink,
.rx_bytes = rx_bytes,
.tx_bytes = tx_bytes,
Expand Down
2 changes: 2 additions & 0 deletions tests/integration/tests.c
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@

/* Copyright 2015 Outscale SAS
*
* This file is part of Packetgraph.
Expand Down Expand Up @@ -171,6 +172,7 @@ static inline int start_qemu_graph(struct branch *branch,
int qemu_pid = pg_util_spawn_qemu(sock_path_graph(branch),
sock_read_path_graph(branch),
tmp_mac, mac_reader,
0,0,
glob_vm_path,
glob_vm_key_path,
glob_hugepages_path, errp);
Expand Down
1 change: 1 addition & 0 deletions tests/vhost/bench-vhost.c
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,7 @@ void test_benchmark_vhost(char *vm_image_path,
qemu_pid = pg_util_spawn_qemu(socket_path_enter,
socket_path_exit,
mac1_str, mac2_str,
0,0,
vm_image_path, vm_ssh_key_path,
hugepages_path, &error);
g_assert(!error);
Expand Down
Loading

0 comments on commit 5915deb

Please sign in to comment.