Damjan Marion | 8389fb9 | 2017-10-13 18:29:53 +0200 | [diff] [blame] | 1 | /* |
| 2 | *------------------------------------------------------------------ |
| 3 | * Copyright (c) 2017 Cisco and/or its affiliates. |
| 4 | * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | * you may not use this file except in compliance with the License. |
| 6 | * You may obtain a copy of the License at: |
| 7 | * |
| 8 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | * |
| 10 | * Unless required by applicable law or agreed to in writing, software |
| 11 | * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | * See the License for the specific language governing permissions and |
| 14 | * limitations under the License. |
| 15 | *------------------------------------------------------------------ |
| 16 | */ |
| 17 | |
| 18 | #include <sys/types.h> |
| 19 | #include <sys/stat.h> |
| 20 | #include <fcntl.h> |
| 21 | #include <net/if.h> |
| 22 | #include <linux/if_tun.h> |
| 23 | #include <sys/ioctl.h> |
| 24 | #include <linux/virtio_net.h> |
| 25 | #include <linux/vhost.h> |
| 26 | #include <sys/eventfd.h> |
| 27 | |
| 28 | #include <vlib/vlib.h> |
| 29 | #include <vlib/unix/unix.h> |
| 30 | #include <vnet/ethernet/ethernet.h> |
| 31 | #include <vnet/devices/virtio/virtio.h> |
| 32 | |
| 33 | virtio_main_t virtio_main; |
| 34 | |
| 35 | #define _IOCTL(fd,a,...) \ |
| 36 | if (ioctl (fd, a, __VA_ARGS__) < 0) \ |
| 37 | { \ |
| 38 | err = clib_error_return_unix (0, "ioctl(" #a ")"); \ |
| 39 | goto error; \ |
| 40 | } |
| 41 | |
| 42 | static clib_error_t * |
| 43 | call_read_ready (clib_file_t * uf) |
| 44 | { |
| 45 | virtio_main_t *nm = &virtio_main; |
| 46 | vnet_main_t *vnm = vnet_get_main (); |
| 47 | u16 qid = uf->private_data & 0xFFFF; |
| 48 | virtio_if_t *vif = |
| 49 | vec_elt_at_index (nm->interfaces, uf->private_data >> 16); |
| 50 | u64 b; |
| 51 | |
| 52 | CLIB_UNUSED (ssize_t size) = read (uf->file_descriptor, &b, sizeof (b)); |
| 53 | if ((qid & 1) == 0) |
| 54 | vnet_device_input_set_interrupt_pending (vnm, vif->hw_if_index, qid); |
| 55 | |
| 56 | return 0; |
| 57 | } |
| 58 | |
| 59 | |
| 60 | clib_error_t * |
| 61 | virtio_vring_init (vlib_main_t * vm, virtio_if_t * vif, u16 idx, u16 sz) |
| 62 | { |
| 63 | clib_error_t *err = 0; |
| 64 | virtio_vring_t *vring; |
| 65 | struct vhost_vring_state state; |
| 66 | struct vhost_vring_addr addr; |
| 67 | struct vhost_vring_file file; |
| 68 | clib_file_t t = { 0 }; |
| 69 | int i; |
| 70 | |
| 71 | if (!is_pow2 (sz)) |
| 72 | return clib_error_return (0, "ring size must be power of 2"); |
| 73 | |
| 74 | if (sz > 32768) |
| 75 | return clib_error_return (0, "ring size must be 32768 or lower"); |
| 76 | |
| 77 | if (sz == 0) |
| 78 | sz = 256; |
| 79 | |
| 80 | vec_validate_aligned (vif->vrings, idx, CLIB_CACHE_LINE_BYTES); |
| 81 | vring = vec_elt_at_index (vif->vrings, idx); |
| 82 | |
| 83 | i = sizeof (struct vring_desc) * sz; |
| 84 | i = round_pow2 (i, CLIB_CACHE_LINE_BYTES); |
| 85 | vring->desc = clib_mem_alloc_aligned (i, CLIB_CACHE_LINE_BYTES); |
| 86 | memset (vring->desc, 0, i); |
| 87 | |
| 88 | i = sizeof (struct vring_avail) + sz * sizeof (vring->avail->ring[0]); |
| 89 | i = round_pow2 (i, CLIB_CACHE_LINE_BYTES); |
| 90 | vring->avail = clib_mem_alloc_aligned (i, CLIB_CACHE_LINE_BYTES); |
| 91 | memset (vring->avail, 0, i); |
| 92 | // tell kernel that we don't need interrupt |
| 93 | vring->avail->flags = VIRTIO_RING_FLAG_MASK_INT; |
| 94 | |
| 95 | i = sizeof (struct vring_used) + sz * sizeof (struct vring_used_elem); |
| 96 | i = round_pow2 (i, CLIB_CACHE_LINE_BYTES); |
| 97 | vring->used = clib_mem_alloc_aligned (i, CLIB_CACHE_LINE_BYTES); |
| 98 | memset (vring->used, 0, i); |
| 99 | |
| 100 | ASSERT (vring->buffers == 0); |
| 101 | vec_validate_aligned (vring->buffers, sz * 2, CLIB_CACHE_LINE_BYTES); |
| 102 | |
| 103 | vring->size = sz; |
| 104 | vring->call_fd = eventfd (0, EFD_NONBLOCK | EFD_CLOEXEC); |
| 105 | vring->kick_fd = eventfd (0, EFD_CLOEXEC); |
| 106 | |
| 107 | t.read_function = call_read_ready; |
| 108 | t.file_descriptor = vring->call_fd; |
| 109 | t.private_data = vif->dev_instance << 16 | idx; |
| 110 | vring->call_file_index = clib_file_add (&file_main, &t); |
| 111 | |
| 112 | state.index = idx; |
| 113 | state.num = sz; |
| 114 | _IOCTL (vif->fd, VHOST_SET_VRING_NUM, &state); |
| 115 | |
| 116 | addr.index = idx; |
| 117 | addr.flags = 0; |
| 118 | addr.desc_user_addr = pointer_to_uword (vring->desc); |
| 119 | addr.avail_user_addr = pointer_to_uword (vring->avail); |
| 120 | addr.used_user_addr = pointer_to_uword (vring->used); |
| 121 | _IOCTL (vif->fd, VHOST_SET_VRING_ADDR, &addr); |
| 122 | |
| 123 | file.index = idx; |
| 124 | file.fd = vring->kick_fd; |
| 125 | _IOCTL (vif->fd, VHOST_SET_VRING_KICK, &file); |
| 126 | file.fd = vring->call_fd; |
| 127 | _IOCTL (vif->fd, VHOST_SET_VRING_CALL, &file); |
| 128 | file.fd = vif->tap_fd; |
| 129 | _IOCTL (vif->fd, VHOST_NET_SET_BACKEND, &file); |
| 130 | |
| 131 | error: |
| 132 | return err; |
| 133 | } |
| 134 | |
| 135 | clib_error_t * |
| 136 | virtio_vring_free (virtio_if_t * vif, u32 idx) |
| 137 | { |
| 138 | //TODO free buffers and indirect descriptor allocs |
| 139 | virtio_vring_t *vring = vec_elt_at_index (vif->vrings, idx); |
| 140 | if (vring->desc) |
| 141 | clib_mem_free (vring->desc); |
| 142 | if (vring->avail) |
| 143 | clib_mem_free (vring->avail); |
| 144 | if (vring->used) |
| 145 | clib_mem_free (vring->used); |
| 146 | clib_file_del_by_index (&file_main, vring->call_file_index); |
| 147 | close (vring->kick_fd); |
| 148 | close (vring->call_fd); |
| 149 | vec_free (vring->buffers); |
| 150 | return 0; |
| 151 | } |
| 152 | |
| 153 | /* |
| 154 | * fd.io coding-style-patch-verification: ON |
| 155 | * |
| 156 | * Local Variables: |
| 157 | * eval: (c-set-style "gnu") |
| 158 | * End: |
| 159 | */ |