Lines Matching +full:sock +full:- +full:priv
1 // SPDX-License-Identifier: GPL-2.0
55 int sock; member
98 memcpy(dev->mac, &ifr.ifr_hwaddr.sa_data, ETHER_ADDR_LEN); in tun_alloc()
106 dev->sock = socket(AF_PACKET, SOCK_RAW, htons(TEST_PTYPE)); in vdev_create_socket()
107 assert(dev->sock != -1); in vdev_create_socket()
110 assert(ioctl(dev->sock, SIOCGIFINDEX, &ifr) >= 0); in vdev_create_socket()
112 dev->ifindex = ifr.ifr_ifindex; in vdev_create_socket()
115 assert(ioctl(dev->sock, SIOCGIFFLAGS, &ifr) >= 0); in vdev_create_socket()
117 assert(ioctl(dev->sock, SIOCSIFFLAGS, &ifr) >= 0); in vdev_create_socket()
122 char *sendbuf = dev->test_buf + HDR_LEN; in vdev_send_packet()
124 int sockfd = dev->sock; in vdev_send_packet()
128 saddrll.sll_ifindex = dev->ifindex; in vdev_send_packet()
140 struct vq_info *info = vq->priv; in vq_notify()
144 r = write(info->kick, &v, sizeof(v)); in vq_notify()
153 .index = info->idx, in vhost_vq_setup()
154 .desc_user_addr = (uint64_t)(unsigned long)info->vring.desc, in vhost_vq_setup()
155 .avail_user_addr = (uint64_t)(unsigned long)info->vring.avail, in vhost_vq_setup()
156 .used_user_addr = (uint64_t)(unsigned long)info->vring.used, in vhost_vq_setup()
158 struct vhost_vring_state state = { .index = info->idx }; in vhost_vq_setup()
159 struct vhost_vring_file file = { .index = info->idx }; in vhost_vq_setup()
162 state.num = info->vring.num; in vhost_vq_setup()
163 r = ioctl(dev->control, VHOST_SET_VRING_NUM, &state); in vhost_vq_setup()
167 r = ioctl(dev->control, VHOST_SET_VRING_BASE, &state); in vhost_vq_setup()
170 r = ioctl(dev->control, VHOST_SET_VRING_ADDR, &addr); in vhost_vq_setup()
173 file.fd = info->kick; in vhost_vq_setup()
174 r = ioctl(dev->control, VHOST_SET_VRING_KICK, &file); in vhost_vq_setup()
180 if (info->vq) in vq_reset()
181 vring_del_virtqueue(info->vq); in vq_reset()
183 memset(info->ring, 0, vring_size(num, 4096)); in vq_reset()
184 vring_init(&info->vring, num, info->ring, 4096); in vq_reset()
185 info->vq = vring_new_virtqueue(info->idx, num, 4096, vdev, true, false, in vq_reset()
186 info->ring, vq_notify, NULL, "test"); in vq_reset()
187 assert(info->vq); in vq_reset()
188 info->vq->priv = info; in vq_reset()
194 struct vq_info *info = &dev->vqs[idx]; in vq_info_add()
197 info->idx = idx; in vq_info_add()
198 info->kick = eventfd(0, EFD_NONBLOCK); in vq_info_add()
199 r = posix_memalign(&info->ring, 4096, vring_size(num, 4096)); in vq_info_add()
201 vq_reset(info, num, &dev->vdev); in vq_info_add()
204 r = ioctl(dev->control, VHOST_NET_SET_BACKEND, &backend); in vq_info_add()
213 dev->vdev.features = features; in vdev_info_init()
214 INIT_LIST_HEAD(&dev->vdev.vqs); in vdev_info_init()
215 spin_lock_init(&dev->vdev.vqs_list_lock); in vdev_info_init()
217 dev->buf_size = (HDR_LEN + TEST_BUF_LEN) * 2; in vdev_info_init()
218 dev->buf = malloc(dev->buf_size); in vdev_info_init()
219 assert(dev->buf); in vdev_info_init()
220 dev->test_buf = dev->buf; in vdev_info_init()
221 dev->res_buf = dev->test_buf + HDR_LEN + TEST_BUF_LEN; in vdev_info_init()
223 memset(dev->test_buf, 0, HDR_LEN + TEST_BUF_LEN); in vdev_info_init()
224 eh = (struct ether_header *)(dev->test_buf + HDR_LEN); in vdev_info_init()
225 eh->ether_type = htons(TEST_PTYPE); in vdev_info_init()
226 memcpy(eh->ether_dhost, dev->mac, ETHER_ADDR_LEN); in vdev_info_init()
227 memcpy(eh->ether_shost, dev->mac, ETHER_ADDR_LEN); in vdev_info_init()
230 dev->test_buf[i + HDR_LEN] = (char)i; in vdev_info_init()
232 dev->control = open("/dev/vhost-net", O_RDWR); in vdev_info_init()
233 assert(dev->control >= 0); in vdev_info_init()
235 r = ioctl(dev->control, VHOST_SET_OWNER, NULL); in vdev_info_init()
238 dev->mem = malloc(offsetof(struct vhost_memory, regions) + in vdev_info_init()
239 sizeof(dev->mem->regions[0])); in vdev_info_init()
240 assert(dev->mem); in vdev_info_init()
241 memset(dev->mem, 0, offsetof(struct vhost_memory, regions) + in vdev_info_init()
242 sizeof(dev->mem->regions[0])); in vdev_info_init()
243 dev->mem->nregions = 1; in vdev_info_init()
244 dev->mem->regions[0].guest_phys_addr = (long)dev->buf; in vdev_info_init()
245 dev->mem->regions[0].userspace_addr = (long)dev->buf; in vdev_info_init()
246 dev->mem->regions[0].memory_size = dev->buf_size; in vdev_info_init()
248 r = ioctl(dev->control, VHOST_SET_MEM_TABLE, dev->mem); in vdev_info_init()
251 r = ioctl(dev->control, VHOST_SET_FEATURES, &features); in vdev_info_init()
254 dev->nvqs = 2; in vdev_info_init()
261 poll(&vq->fds, 1, 100); in wait_for_interrupt()
263 if (vq->fds.revents & POLLIN) in wait_for_interrupt()
264 read(vq->fds.fd, &val, sizeof(val)); in wait_for_interrupt()
284 long started_before = vq->started; in run_tx_test()
285 long completed_before = vq->completed; in run_tx_test()
287 virtqueue_disable_cb(vq->vq); in run_tx_test()
289 while (vq->started < bufs && in run_tx_test()
290 (vq->started - vq->completed) < 1) { in run_tx_test()
291 sg_init_one(&sl, dev->test_buf, HDR_LEN + TEST_BUF_LEN); in run_tx_test()
292 r = virtqueue_add_outbuf(vq->vq, &sl, 1, in run_tx_test()
293 dev->test_buf + vq->started, in run_tx_test()
298 ++vq->started; in run_tx_test()
300 if (unlikely(!virtqueue_kick(vq->vq))) { in run_tx_test()
301 r = -1; in run_tx_test()
306 if (vq->started >= bufs) in run_tx_test()
307 r = -1; in run_tx_test()
310 while (virtqueue_get_buf(vq->vq, &len)) { in run_tx_test()
313 n = recvfrom(dev->sock, dev->res_buf, TEST_BUF_LEN, 0, NULL, NULL); in run_tx_test()
315 verify_res_buf(dev->res_buf); in run_tx_test()
317 ++vq->completed; in run_tx_test()
322 if (vq->completed == completed_before && vq->started == started_before) in run_tx_test()
325 assert(vq->completed <= bufs); in run_tx_test()
326 assert(vq->started <= bufs); in run_tx_test()
327 if (vq->completed == bufs) in run_tx_test()
331 if (virtqueue_enable_cb_delayed(vq->vq)) in run_tx_test()
334 if (virtqueue_enable_cb(vq->vq)) in run_tx_test()
339 spurious, vq->started, vq->completed); in run_tx_test()
351 long started_before = vq->started; in run_rx_test()
352 long completed_before = vq->completed; in run_rx_test()
355 while (vq->started < bufs && in run_rx_test()
356 (vq->started - vq->completed) < 1) { in run_rx_test()
357 sg_init_one(&sl, dev->res_buf, HDR_LEN + TEST_BUF_LEN); in run_rx_test()
359 r = virtqueue_add_inbuf(vq->vq, &sl, 1, in run_rx_test()
360 dev->res_buf + vq->started, in run_rx_test()
365 ++vq->started; in run_rx_test()
369 if (unlikely(!virtqueue_kick(vq->vq))) { in run_rx_test()
370 r = -1; in run_rx_test()
375 if (vq->started >= bufs) in run_rx_test()
376 r = -1; in run_rx_test()
379 while (virtqueue_get_buf(vq->vq, &len)) { in run_rx_test()
382 eh = (struct ether_header *)(dev->res_buf + HDR_LEN); in run_rx_test()
387 if (eh->ether_type == htons(TEST_PTYPE)) { in run_rx_test()
389 verify_res_buf(dev->res_buf + HDR_LEN); in run_rx_test()
392 ++vq->completed; in run_rx_test()
397 if (vq->completed == completed_before && vq->started == started_before) in run_rx_test()
400 assert(vq->completed <= bufs); in run_rx_test()
401 assert(vq->started <= bufs); in run_rx_test()
402 if (vq->completed == bufs) in run_rx_test()
407 spurious, vq->started, vq->completed); in run_rx_test()
417 .name = "event-idx",
421 .name = "no-event-idx",
429 .name = "no-indirect",
433 .name = "virtio-1",
437 .name = "no-virtio-1",
441 .name = "delayed-interrupt",
445 .name = "no-delayed-interrupt",
449 .name = "buf-num",
464 fprintf(stderr, "Usage: vhost_net_test [--help]" in help()
465 " [--no-indirect]" in help()
466 " [--no-event-idx]" in help()
467 " [--no-virtio-1]" in help()
468 " [--delayed-interrupt]" in help()
469 " [--buf-num]" in help()
488 case -1: in main()