Branch data Line data Source code
1 : : /* SPDX-License-Identifier: BSD-3-Clause
2 : : * Copyright (c) 2023 Red Hat, Inc.
3 : : */
4 : :
5 : : #include <stdint.h>
6 : : #include <stdio.h>
7 : : #include <unistd.h>
8 : :
9 : : #include "iotlb.h"
10 : : #include "vhost.h"
11 : : #include "virtio_net_ctrl.h"
12 : :
13 : : struct virtio_net_ctrl {
14 : : uint8_t class;
15 : : uint8_t command;
16 : : uint8_t command_data[];
17 : : };
18 : :
19 : : struct virtio_net_ctrl_elem {
20 : : struct virtio_net_ctrl *ctrl_req;
21 : : uint16_t head_idx;
22 : : uint16_t n_descs;
23 : : uint32_t data_len;
24 : : uint8_t *desc_ack;
25 : : };
26 : :
27 : : static int
28 : 0 : virtio_net_ctrl_pop(struct virtio_net *dev, struct vhost_virtqueue *cvq,
29 : : struct virtio_net_ctrl_elem *ctrl_elem)
30 : : __rte_requires_shared_capability(&cvq->iotlb_lock)
31 : : {
32 : : uint16_t avail_idx, desc_idx, n_descs = 0, nr_descs, cnt = 0;
33 : : uint64_t desc_len, desc_addr, desc_iova, data_len = 0;
34 : : uint8_t *ctrl_req;
35 : : struct vring_desc *descs;
36 : :
37 : 0 : avail_idx = rte_atomic_load_explicit((unsigned short __rte_atomic *)&cvq->avail->idx,
38 : : rte_memory_order_acquire);
39 [ # # ]: 0 : if (avail_idx == cvq->last_avail_idx) {
40 : 0 : VHOST_CONFIG_LOG(dev->ifname, DEBUG, "Control queue empty");
41 : 0 : return 0;
42 : : }
43 : :
44 : 0 : desc_idx = cvq->avail->ring[cvq->last_avail_idx & (cvq->size - 1)];
45 [ # # ]: 0 : if (desc_idx >= cvq->size) {
46 : 0 : VHOST_CONFIG_LOG(dev->ifname, ERR, "Out of range desc index, dropping");
47 : 0 : goto err;
48 : : }
49 : :
50 : 0 : ctrl_elem->head_idx = desc_idx;
51 : :
52 [ # # ]: 0 : if (cvq->desc[desc_idx].flags & VRING_DESC_F_INDIRECT) {
53 : 0 : desc_len = cvq->desc[desc_idx].len;
54 [ # # ]: 0 : desc_iova = cvq->desc[desc_idx].addr;
55 : :
56 : 0 : descs = (struct vring_desc *)(uintptr_t)vhost_iova_to_vva(dev, cvq,
57 : : desc_iova, &desc_len, VHOST_ACCESS_RO);
58 [ # # # # ]: 0 : if (!descs || desc_len != cvq->desc[desc_idx].len) {
59 : 0 : VHOST_CONFIG_LOG(dev->ifname, ERR, "Failed to map ctrl indirect descs");
60 : 0 : goto err;
61 : : }
62 : :
63 : 0 : nr_descs = desc_len / sizeof(struct vring_desc);
64 : : desc_idx = 0;
65 : : } else {
66 : : descs = cvq->desc;
67 : 0 : nr_descs = cvq->size;
68 : : }
69 : :
70 : : while (1) {
71 [ # # # # ]: 0 : if (unlikely(desc_idx >= nr_descs || cnt++ >= nr_descs)) {
72 : 0 : VHOST_CONFIG_LOG(dev->ifname, ERR, "Invalid ctrl descriptor chain");
73 : 0 : goto err;
74 : : }
75 : :
76 : 0 : desc_len = descs[desc_idx].len;
77 : 0 : desc_iova = descs[desc_idx].addr;
78 : :
79 : : n_descs++;
80 : :
81 [ # # ]: 0 : if (descs[desc_idx].flags & VRING_DESC_F_WRITE) {
82 [ # # ]: 0 : if (ctrl_elem->desc_ack) {
83 : 0 : VHOST_CONFIG_LOG(dev->ifname, ERR,
84 : : "Unexpected ctrl chain layout");
85 : 0 : goto err;
86 : : }
87 : :
88 [ # # ]: 0 : if (desc_len != sizeof(uint8_t)) {
89 : 0 : VHOST_CONFIG_LOG(dev->ifname, ERR,
90 : : "Invalid ack size for ctrl req, dropping");
91 : 0 : goto err;
92 : : }
93 : :
94 : 0 : ctrl_elem->desc_ack = (uint8_t *)(uintptr_t)vhost_iova_to_vva(dev, cvq,
95 : : desc_iova, &desc_len, VHOST_ACCESS_WO);
96 [ # # # # ]: 0 : if (!ctrl_elem->desc_ack || desc_len != sizeof(uint8_t)) {
97 : 0 : VHOST_CONFIG_LOG(dev->ifname, ERR,
98 : : "Failed to map ctrl ack descriptor");
99 : 0 : goto err;
100 : : }
101 : : } else {
102 [ # # ]: 0 : if (ctrl_elem->desc_ack) {
103 : 0 : VHOST_CONFIG_LOG(dev->ifname, ERR,
104 : : "Unexpected ctrl chain layout");
105 : 0 : goto err;
106 : : }
107 : :
108 : 0 : data_len += desc_len;
109 : : }
110 : :
111 [ # # ]: 0 : if (!(descs[desc_idx].flags & VRING_DESC_F_NEXT))
112 : : break;
113 : :
114 : 0 : desc_idx = descs[desc_idx].next;
115 : : }
116 : :
117 : 0 : desc_idx = ctrl_elem->head_idx;
118 : :
119 [ # # ]: 0 : if (cvq->desc[desc_idx].flags & VRING_DESC_F_INDIRECT)
120 : 0 : ctrl_elem->n_descs = 1;
121 : : else
122 : 0 : ctrl_elem->n_descs = n_descs;
123 : :
124 [ # # ]: 0 : if (!ctrl_elem->desc_ack) {
125 : 0 : VHOST_CONFIG_LOG(dev->ifname, ERR, "Missing ctrl ack descriptor");
126 : 0 : goto err;
127 : : }
128 : :
129 [ # # ]: 0 : if (data_len < sizeof(ctrl_elem->ctrl_req->class) + sizeof(ctrl_elem->ctrl_req->command)) {
130 : 0 : VHOST_CONFIG_LOG(dev->ifname, ERR, "Invalid control header size");
131 : 0 : goto err;
132 : : }
133 : :
134 : 0 : ctrl_elem->ctrl_req = malloc(data_len);
135 [ # # ]: 0 : if (!ctrl_elem->ctrl_req) {
136 : 0 : VHOST_CONFIG_LOG(dev->ifname, ERR, "Failed to alloc ctrl request");
137 : 0 : goto err;
138 : : }
139 : 0 : ctrl_elem->data_len = data_len;
140 : :
141 : : ctrl_req = (uint8_t *)ctrl_elem->ctrl_req;
142 : :
143 : : uint64_t copied = 0;
144 : :
145 [ # # ]: 0 : if (cvq->desc[desc_idx].flags & VRING_DESC_F_INDIRECT) {
146 : 0 : desc_len = cvq->desc[desc_idx].len;
147 [ # # ]: 0 : desc_iova = cvq->desc[desc_idx].addr;
148 : :
149 : 0 : descs = (struct vring_desc *)(uintptr_t)vhost_iova_to_vva(dev, cvq,
150 : : desc_iova, &desc_len, VHOST_ACCESS_RO);
151 [ # # # # ]: 0 : if (!descs || desc_len != cvq->desc[desc_idx].len) {
152 : 0 : VHOST_CONFIG_LOG(dev->ifname, ERR, "Failed to map ctrl indirect descs");
153 : 0 : goto free_err;
154 : : }
155 : :
156 : 0 : nr_descs = desc_len / sizeof(struct vring_desc);
157 : : desc_idx = 0;
158 : : } else {
159 : : descs = cvq->desc;
160 : 0 : nr_descs = cvq->size;
161 : : }
162 : :
163 : : cnt = 0;
164 : : while (1) {
165 [ # # # # ]: 0 : if (unlikely(desc_idx >= nr_descs || cnt++ >= nr_descs)) {
166 : 0 : VHOST_CONFIG_LOG(dev->ifname, ERR, "Invalid ctrl descriptor chain");
167 : 0 : goto free_err;
168 : : }
169 : :
170 [ # # ]: 0 : if (descs[desc_idx].flags & VRING_DESC_F_WRITE)
171 : : break;
172 : :
173 : 0 : desc_len = descs[desc_idx].len;
174 [ # # ]: 0 : desc_iova = descs[desc_idx].addr;
175 : :
176 : : desc_addr = vhost_iova_to_vva(dev, cvq, desc_iova, &desc_len, VHOST_ACCESS_RO);
177 [ # # # # ]: 0 : if (!desc_addr || desc_len < descs[desc_idx].len) {
178 : 0 : VHOST_CONFIG_LOG(dev->ifname, ERR, "Failed to map ctrl descriptor");
179 : 0 : goto free_err;
180 : : }
181 : :
182 [ # # ]: 0 : if (copied + desc_len > data_len) {
183 : 0 : VHOST_CONFIG_LOG(dev->ifname, ERR, "Descriptor length overflow detected");
184 : 0 : goto free_err;
185 : : }
186 : :
187 [ # # ]: 0 : memcpy(ctrl_req, (void *)(uintptr_t)desc_addr, desc_len);
188 : 0 : ctrl_req += desc_len;
189 : 0 : copied += desc_len;
190 : :
191 [ # # ]: 0 : if (!(descs[desc_idx].flags & VRING_DESC_F_NEXT))
192 : : break;
193 : :
194 : 0 : desc_idx = descs[desc_idx].next;
195 : : }
196 : :
197 [ # # ]: 0 : cvq->last_avail_idx++;
198 : : vhost_virtqueue_reconnect_log_split(cvq);
199 : :
200 [ # # ]: 0 : if (dev->features & (1ULL << VIRTIO_RING_F_EVENT_IDX))
201 : 0 : vhost_avail_event(cvq) = cvq->last_avail_idx;
202 : :
203 : : return 1;
204 : :
205 : 0 : free_err:
206 : 0 : free(ctrl_elem->ctrl_req);
207 : 0 : err:
208 [ # # ]: 0 : cvq->last_avail_idx++;
209 : : vhost_virtqueue_reconnect_log_split(cvq);
210 : :
211 [ # # ]: 0 : if (dev->features & (1ULL << VIRTIO_RING_F_EVENT_IDX))
212 : 0 : vhost_avail_event(cvq) = cvq->last_avail_idx;
213 : :
214 : : return -1;
215 : : }
216 : :
217 : : static uint8_t
218 : 0 : virtio_net_ctrl_handle_req(struct virtio_net *dev, struct virtio_net_ctrl *ctrl_req,
219 : : uint32_t data_len)
220 : : {
221 : : uint8_t ret = VIRTIO_NET_ERR;
222 : :
223 [ # # ]: 0 : if (ctrl_req->class == VIRTIO_NET_CTRL_MQ &&
224 [ # # ]: 0 : ctrl_req->command == VIRTIO_NET_CTRL_MQ_VQ_PAIRS_SET) {
225 : : uint16_t queue_pairs;
226 : : uint32_t i;
227 : :
228 [ # # ]: 0 : if (data_len < sizeof(*ctrl_req) + sizeof(queue_pairs)) {
229 : 0 : VHOST_CONFIG_LOG(dev->ifname, ERR,
230 : : "Invalid MQ control request size");
231 : 0 : return VIRTIO_NET_ERR;
232 : : }
233 : :
234 : 0 : queue_pairs = *(uint16_t *)(uintptr_t)ctrl_req->command_data;
235 : 0 : VHOST_CONFIG_LOG(dev->ifname, INFO, "Ctrl req: MQ %u queue pairs", queue_pairs);
236 : : ret = VIRTIO_NET_OK;
237 : :
238 [ # # ]: 0 : for (i = 0; i < dev->nr_vring; i++) {
239 : 0 : struct vhost_virtqueue *vq = dev->virtqueue[i];
240 : : bool enable;
241 : :
242 [ # # ]: 0 : if (vq == dev->cvq)
243 : 0 : continue;
244 : :
245 [ # # ]: 0 : if (i < queue_pairs * 2)
246 : : enable = true;
247 : : else
248 : : enable = false;
249 : :
250 : 0 : vq->enabled = enable;
251 [ # # ]: 0 : if (dev->notify_ops->vring_state_changed)
252 : 0 : dev->notify_ops->vring_state_changed(dev->vid, i, enable);
253 : : }
254 : : }
255 : :
256 : : return ret;
257 : : }
258 : :
259 : : static int
260 : 0 : virtio_net_ctrl_push(struct virtio_net *dev, struct virtio_net_ctrl_elem *ctrl_elem)
261 : : {
262 : 0 : struct vhost_virtqueue *cvq = dev->cvq;
263 : : struct vring_used_elem *used_elem;
264 : :
265 : 0 : used_elem = &cvq->used->ring[cvq->last_used_idx & (cvq->size - 1)];
266 : 0 : used_elem->id = ctrl_elem->head_idx;
267 : 0 : used_elem->len = sizeof(*ctrl_elem->desc_ack);
268 : :
269 : 0 : cvq->last_used_idx++;
270 : :
271 : 0 : rte_atomic_store_explicit((unsigned short __rte_atomic *)&cvq->used->idx,
272 : : cvq->last_used_idx, rte_memory_order_release);
273 : :
274 : 0 : vhost_vring_call_split(dev, dev->cvq);
275 : :
276 : 0 : free(ctrl_elem->ctrl_req);
277 : :
278 : 0 : return 0;
279 : : }
280 : :
281 : : int
282 : 0 : virtio_net_ctrl_handle(struct virtio_net *dev)
283 : : {
284 : : int ret = 0;
285 : :
286 [ # # ]: 0 : if (dev->features & (1ULL << VIRTIO_F_RING_PACKED)) {
287 : 0 : VHOST_CONFIG_LOG(dev->ifname, ERR, "Packed ring not supported yet");
288 : 0 : return -1;
289 : : }
290 : :
291 [ # # ]: 0 : if (!dev->cvq) {
292 : 0 : VHOST_CONFIG_LOG(dev->ifname, ERR, "missing control queue");
293 : 0 : return -1;
294 : : }
295 : :
296 : 0 : rte_rwlock_read_lock(&dev->cvq->access_lock);
297 : 0 : vhost_user_iotlb_rd_lock(dev->cvq);
298 : :
299 : 0 : while (1) {
300 : : struct virtio_net_ctrl_elem ctrl_elem;
301 : :
302 : : memset(&ctrl_elem, 0, sizeof(struct virtio_net_ctrl_elem));
303 : :
304 : 0 : ret = virtio_net_ctrl_pop(dev, dev->cvq, &ctrl_elem);
305 [ # # ]: 0 : if (ret <= 0)
306 : : break;
307 : :
308 : 0 : *ctrl_elem.desc_ack = virtio_net_ctrl_handle_req(dev, ctrl_elem.ctrl_req,
309 : : ctrl_elem.data_len);
310 : :
311 : 0 : ret = virtio_net_ctrl_push(dev, &ctrl_elem);
312 [ # # ]: 0 : if (ret < 0)
313 : : break;
314 : : }
315 : :
316 : 0 : vhost_user_iotlb_rd_unlock(dev->cvq);
317 : 0 : rte_rwlock_read_unlock(&dev->cvq->access_lock);
318 : :
319 : 0 : return ret;
320 : : }
|