@samitouri / QOSamiQemu / commits / be9ecc9df0

vhost-user: introduce vhost_user_has_protocol_feature() helper

Make all protocol feature checks in the same way. Signed-off-by: Vladimir Sementsov-Ogievskiy <vsementsov@yandex-team.ru> Reviewed-by: Daniil Tatianin <d-tatianin@yandex-team.ru> Reviewed-by: Raphael Norwitz <raphael.s.norwitz@gmail.com> Reviewed-by: Michael S. Tsirkin <mst@redhat.com> Tested-by: Lei Yang <leiyang@redhat.com> Signed-off-by: Michael S. Tsirkin <mst@redhat.com> Message-Id: <20260420200339.708640-4-vsementsov@yandex-team.ru>

Vladimir Sementsov-Ogievskiy committed Apr 20, 2026 at 23:03 UTC be9ecc9df07d8daef364f43798e78be6282c6b5f
1 file changed +62 -58
hw/virtio/vhost-user.c
+62 -58
@@ -267,6 +267,12 @@ struct scrub_regions {
267 int fd_idx;
268 };
269
270 +static bool vhost_user_has_protocol_feature(struct vhost_dev *dev,
271 + uint64_t feature)
272 +{
273 + return virtio_has_feature(dev->protocol_features, feature);
274 +}
275 +
276 static int vhost_user_read_header(struct vhost_dev *dev, VhostUserMsg *msg)
277 {
278 struct vhost_user *u = dev->opaque;
@@ -430,8 +436,8 @@ static int vhost_user_set_log_base(struct vhost_dev *dev, uint64_t base,
436 {
437 int fds[VHOST_USER_MAX_RAM_SLOTS];
438 size_t fd_num = 0;
433 - bool shmfd = virtio_has_feature(dev->protocol_features,
434 - VHOST_USER_PROTOCOL_F_LOG_SHMFD);
439 + bool shmfd =
440 + vhost_user_has_protocol_feature(dev, VHOST_USER_PROTOCOL_F_LOG_SHMFD);
441 int ret;
442 VhostUserMsg msg = {
443 .hdr.request = VHOST_USER_SET_LOG_BASE,
@@ -1001,11 +1007,11 @@ static int vhost_user_set_mem_table(struct vhost_dev *dev,
1007 int fds[VHOST_MEMORY_BASELINE_NREGIONS];
1008 size_t fd_num = 0;
1009 bool do_postcopy = u->postcopy_listen && u->postcopy_fd.handler;
1004 - bool reply_supported = virtio_has_feature(dev->protocol_features,
1005 - VHOST_USER_PROTOCOL_F_REPLY_ACK);
1010 + bool reply_supported =
1011 + vhost_user_has_protocol_feature(dev, VHOST_USER_PROTOCOL_F_REPLY_ACK);
1012 bool config_mem_slots =
1007 - virtio_has_feature(dev->protocol_features,
1008 - VHOST_USER_PROTOCOL_F_CONFIGURE_MEM_SLOTS);
1013 + vhost_user_has_protocol_feature(
1014 + dev, VHOST_USER_PROTOCOL_F_CONFIGURE_MEM_SLOTS);
1015 int ret;
1016
1017 if (do_postcopy) {
@@ -1053,8 +1059,9 @@ static int vhost_user_set_mem_table(struct vhost_dev *dev,
1059 static int vhost_user_set_vring_endian(struct vhost_dev *dev,
1060 struct vhost_vring_state *ring)
1061 {
1056 - bool cross_endian = virtio_has_feature(dev->protocol_features,
1057 - VHOST_USER_PROTOCOL_F_CROSS_ENDIAN);
1062 + bool cross_endian =
1063 + vhost_user_has_protocol_feature(
1064 + dev, VHOST_USER_PROTOCOL_F_CROSS_ENDIAN);
1065 VhostUserMsg msg = {
1066 .hdr.request = VHOST_USER_SET_VRING_ENDIAN,
1067 .hdr.flags = VHOST_USER_VERSION,
@@ -1124,8 +1131,9 @@ static int vhost_user_write_sync(struct vhost_dev *dev, VhostUserMsg *msg,
1131 int ret;
1132
1133 if (wait_for_reply) {
1127 - bool reply_supported = virtio_has_feature(dev->protocol_features,
1128 - VHOST_USER_PROTOCOL_F_REPLY_ACK);
1134 + bool reply_supported =
1135 + vhost_user_has_protocol_feature(
1136 + dev, VHOST_USER_PROTOCOL_F_REPLY_ACK);
1137 if (reply_supported) {
1138 msg->hdr.flags |= VHOST_USER_NEED_REPLY_MASK;
1139 }
@@ -1475,8 +1483,7 @@ static int vhost_user_set_features(struct vhost_dev *dev,
1483 ret = vhost_user_set_u64(dev, VHOST_USER_SET_FEATURES, features,
1484 log_enabled);
1485
1478 - if (virtio_has_feature(dev->protocol_features,
1479 - VHOST_USER_PROTOCOL_F_STATUS)) {
1486 + if (vhost_user_has_protocol_feature(dev, VHOST_USER_PROTOCOL_F_STATUS)) {
1487 if (!ret) {
1488 return vhost_user_add_status(dev, VIRTIO_CONFIG_S_FEATURES_OK);
1489 }
@@ -1530,8 +1537,8 @@ static int vhost_user_reset_device(struct vhost_dev *dev)
1537 * Historically, reset was not implemented so only reset devices
1538 * that are expecting it.
1539 */
1533 - if (!virtio_has_feature(dev->protocol_features,
1534 - VHOST_USER_PROTOCOL_F_RESET_DEVICE)) {
1540 + if (!vhost_user_has_protocol_feature(
1541 + dev, VHOST_USER_PROTOCOL_F_RESET_DEVICE)) {
1542 return -ENOSYS;
1543 }
1544
@@ -1588,8 +1595,8 @@ static int vhost_user_backend_handle_vring_host_notifier(struct vhost_dev *dev,
1595 void *addr;
1596 char *name;
1597
1591 - if (!virtio_has_feature(dev->protocol_features,
1592 - VHOST_USER_PROTOCOL_F_HOST_NOTIFIER) ||
1598 + if (!vhost_user_has_protocol_feature(
1599 + dev, VHOST_USER_PROTOCOL_F_HOST_NOTIFIER) ||
1600 vdev == NULL || queue_idx >= virtio_get_num_queues(vdev)) {
1601 return -EINVAL;
1602 }
@@ -1887,13 +1894,13 @@ static int vhost_setup_backend_channel(struct vhost_dev *dev)
1894 };
1895 struct vhost_user *u = dev->opaque;
1896 int sv[2], ret = 0;
1890 - bool reply_supported = virtio_has_feature(dev->protocol_features,
1891 - VHOST_USER_PROTOCOL_F_REPLY_ACK);
1897 + bool reply_supported =
1898 + vhost_user_has_protocol_feature(dev, VHOST_USER_PROTOCOL_F_REPLY_ACK);
1899 Error *local_err = NULL;
1900 QIOChannel *ioc;
1901
1895 - if (!virtio_has_feature(dev->protocol_features,
1896 - VHOST_USER_PROTOCOL_F_BACKEND_REQ)) {
1902 + if (!vhost_user_has_protocol_feature(
1903 + dev, VHOST_USER_PROTOCOL_F_BACKEND_REQ)) {
1904 return 0;
1905 }
1906
@@ -2141,8 +2148,8 @@ static int vhost_user_postcopy_notifier(NotifierWithReturn *notifier,
2148
2149 switch (pnd->reason) {
2150 case POSTCOPY_NOTIFY_PROBE:
2144 - if (!virtio_has_feature(dev->protocol_features,
2145 - VHOST_USER_PROTOCOL_F_PAGEFAULT)) {
2151 + if (!vhost_user_has_protocol_feature(
2152 + dev, VHOST_USER_PROTOCOL_F_PAGEFAULT)) {
2153 /* TODO: Get the device name into this error somehow */
2154 error_setg(errp,
2155 "vhost-user backend not capable of postcopy");
@@ -2240,7 +2247,7 @@ static int vhost_user_backend_init(struct vhost_dev *dev, void *opaque,
2247 }
2248
2249 /* query the max queues we support if backend supports Multiple Queue */
2243 - if (dev->protocol_features & (1ULL << VHOST_USER_PROTOCOL_F_MQ)) {
2250 + if (vhost_user_has_protocol_feature(dev, VHOST_USER_PROTOCOL_F_MQ)) {
2251 err = vhost_user_get_u64(dev, VHOST_USER_GET_QUEUE_NUM,
2252 &dev->max_queues);
2253 if (err < 0) {
@@ -2258,18 +2265,18 @@ static int vhost_user_backend_init(struct vhost_dev *dev, void *opaque,
2265 }
2266
2267 if (virtio_has_feature(features, VIRTIO_F_IOMMU_PLATFORM) &&
2261 - !(virtio_has_feature(dev->protocol_features,
2262 - VHOST_USER_PROTOCOL_F_BACKEND_REQ) &&
2263 - virtio_has_feature(dev->protocol_features,
2264 - VHOST_USER_PROTOCOL_F_REPLY_ACK))) {
2268 + !(vhost_user_has_protocol_feature(
2269 + dev, VHOST_USER_PROTOCOL_F_BACKEND_REQ) &&
2270 + vhost_user_has_protocol_feature(
2271 + dev, VHOST_USER_PROTOCOL_F_REPLY_ACK))) {
2272 error_setg(errp, "IOMMU support requires reply-ack and "
2273 "backend-req protocol features.");
2274 return -EINVAL;
2275 }
2276
2277 /* get max memory regions if backend supports configurable RAM slots */
2271 - if (!virtio_has_feature(dev->protocol_features,
2272 - VHOST_USER_PROTOCOL_F_CONFIGURE_MEM_SLOTS)) {
2278 + if (!vhost_user_has_protocol_feature(
2279 + dev, VHOST_USER_PROTOCOL_F_CONFIGURE_MEM_SLOTS)) {
2280 u->user->memory_slots = VHOST_MEMORY_BASELINE_NREGIONS;
2281 } else {
2282 err = vhost_user_get_max_memslots(dev, &ram_slots);
@@ -2293,8 +2300,8 @@ static int vhost_user_backend_init(struct vhost_dev *dev, void *opaque,
2300 }
2301
2302 if (dev->migration_blocker == NULL &&
2296 - !virtio_has_feature(dev->protocol_features,
2297 - VHOST_USER_PROTOCOL_F_LOG_SHMFD)) {
2303 + !vhost_user_has_protocol_feature(
2304 + dev, VHOST_USER_PROTOCOL_F_LOG_SHMFD)) {
2305 error_setg(&dev->migration_blocker,
2306 "Migration disabled: vhost-user backend lacks "
2307 "VHOST_USER_PROTOCOL_F_LOG_SHMFD feature.");
@@ -2363,8 +2370,8 @@ static bool vhost_user_requires_shm_log(struct vhost_dev *dev)
2370 {
2371 assert(dev->vhost_ops->backend_type == VHOST_BACKEND_TYPE_USER);
2372
2366 - return virtio_has_feature(dev->protocol_features,
2367 - VHOST_USER_PROTOCOL_F_LOG_SHMFD);
2373 + return vhost_user_has_protocol_feature(
2374 + dev, VHOST_USER_PROTOCOL_F_LOG_SHMFD);
2375 }
2376
2377 static int vhost_user_migration_done(struct vhost_dev *dev, char* mac_addr)
@@ -2379,8 +2386,7 @@ static int vhost_user_migration_done(struct vhost_dev *dev, char* mac_addr)
2386 }
2387
2388 /* if backend supports VHOST_USER_PROTOCOL_F_RARP ask it to send the RARP */
2382 - if (virtio_has_feature(dev->protocol_features,
2383 - VHOST_USER_PROTOCOL_F_RARP)) {
2389 + if (vhost_user_has_protocol_feature(dev, VHOST_USER_PROTOCOL_F_RARP)) {
2390 msg.hdr.request = VHOST_USER_SEND_RARP;
2391 msg.hdr.flags = VHOST_USER_VERSION;
2392 memcpy((char *)&msg.payload.u64, mac_addr, 6);
@@ -2394,11 +2400,11 @@ static int vhost_user_migration_done(struct vhost_dev *dev, char* mac_addr)
2400 static int vhost_user_net_set_mtu(struct vhost_dev *dev, uint16_t mtu)
2401 {
2402 VhostUserMsg msg;
2397 - bool reply_supported = virtio_has_feature(dev->protocol_features,
2398 - VHOST_USER_PROTOCOL_F_REPLY_ACK);
2403 + bool reply_supported =
2404 + vhost_user_has_protocol_feature(dev, VHOST_USER_PROTOCOL_F_REPLY_ACK);
2405 int ret;
2406
2401 - if (!(dev->protocol_features & (1ULL << VHOST_USER_PROTOCOL_F_NET_MTU))) {
2407 + if (!vhost_user_has_protocol_feature(dev, VHOST_USER_PROTOCOL_F_NET_MTU)) {
2408 return 0;
2409 }
2410
@@ -2458,8 +2464,7 @@ static int vhost_user_get_config(struct vhost_dev *dev, uint8_t *config,
2464 .hdr.size = VHOST_USER_CONFIG_HDR_SIZE + config_len,
2465 };
2466
2461 - if (!virtio_has_feature(dev->protocol_features,
2462 - VHOST_USER_PROTOCOL_F_CONFIG)) {
2467 + if (!vhost_user_has_protocol_feature(dev, VHOST_USER_PROTOCOL_F_CONFIG)) {
2468 error_setg(errp, "VHOST_USER_PROTOCOL_F_CONFIG not supported");
2469 return -EINVAL;
2470 }
@@ -2502,8 +2507,8 @@ static int vhost_user_set_config(struct vhost_dev *dev, const uint8_t *data,
2507 {
2508 int ret;
2509 uint8_t *p;
2505 - bool reply_supported = virtio_has_feature(dev->protocol_features,
2506 - VHOST_USER_PROTOCOL_F_REPLY_ACK);
2510 + bool reply_supported =
2511 + vhost_user_has_protocol_feature(dev, VHOST_USER_PROTOCOL_F_REPLY_ACK);
2512
2513 VhostUserMsg msg = {
2514 .hdr.request = VHOST_USER_SET_CONFIG,
@@ -2511,8 +2516,7 @@ static int vhost_user_set_config(struct vhost_dev *dev, const uint8_t *data,
2516 .hdr.size = VHOST_USER_CONFIG_HDR_SIZE + size,
2517 };
2518
2514 - if (!virtio_has_feature(dev->protocol_features,
2515 - VHOST_USER_PROTOCOL_F_CONFIG)) {
2519 + if (!vhost_user_has_protocol_feature(dev, VHOST_USER_PROTOCOL_F_CONFIG)) {
2520 return -ENOTSUP;
2521 }
2522
@@ -2547,8 +2551,9 @@ static int vhost_user_crypto_create_session(struct vhost_dev *dev,
2551 uint64_t *session_id)
2552 {
2553 int ret;
2550 - bool crypto_session = virtio_has_feature(dev->protocol_features,
2551 - VHOST_USER_PROTOCOL_F_CRYPTO_SESSION);
2554 + bool crypto_session =
2555 + vhost_user_has_protocol_feature(
2556 + dev, VHOST_USER_PROTOCOL_F_CRYPTO_SESSION);
2557 CryptoDevBackendSessionInfo *backend_info = session_info;
2558 VhostUserMsg msg = {
2559 .hdr.request = VHOST_USER_CREATE_CRYPTO_SESSION,
@@ -2649,8 +2654,9 @@ static int
2654 vhost_user_crypto_close_session(struct vhost_dev *dev, uint64_t session_id)
2655 {
2656 int ret;
2652 - bool crypto_session = virtio_has_feature(dev->protocol_features,
2653 - VHOST_USER_PROTOCOL_F_CRYPTO_SESSION);
2657 + bool crypto_session =
2658 + vhost_user_has_protocol_feature(
2659 + dev, VHOST_USER_PROTOCOL_F_CRYPTO_SESSION);
2660 VhostUserMsg msg = {
2661 .hdr.request = VHOST_USER_CLOSE_CRYPTO_SESSION,
2662 .hdr.flags = VHOST_USER_VERSION,
@@ -2695,8 +2701,8 @@ static int vhost_user_get_inflight_fd(struct vhost_dev *dev,
2701 .hdr.size = sizeof(msg.payload.inflight),
2702 };
2703
2698 - if (!virtio_has_feature(dev->protocol_features,
2699 - VHOST_USER_PROTOCOL_F_INFLIGHT_SHMFD)) {
2704 + if (!vhost_user_has_protocol_feature(
2705 + dev, VHOST_USER_PROTOCOL_F_INFLIGHT_SHMFD)) {
2706 return 0;
2707 }
2708
@@ -2763,8 +2769,8 @@ static int vhost_user_set_inflight_fd(struct vhost_dev *dev,
2769 .hdr.size = sizeof(msg.payload.inflight),
2770 };
2771
2766 - if (!virtio_has_feature(dev->protocol_features,
2767 - VHOST_USER_PROTOCOL_F_INFLIGHT_SHMFD)) {
2772 + if (!vhost_user_has_protocol_feature(
2773 + dev, VHOST_USER_PROTOCOL_F_INFLIGHT_SHMFD)) {
2774 return 0;
2775 }
2776
@@ -2863,8 +2869,7 @@ void vhost_user_async_close(DeviceState *d,
2869
2870 static int vhost_user_dev_start(struct vhost_dev *dev, bool started)
2871 {
2866 - if (!virtio_has_feature(dev->protocol_features,
2867 - VHOST_USER_PROTOCOL_F_STATUS)) {
2872 + if (!vhost_user_has_protocol_feature(dev, VHOST_USER_PROTOCOL_F_STATUS)) {
2873 return 0;
2874 }
2875
@@ -2889,16 +2894,15 @@ static void vhost_user_reset_status(struct vhost_dev *dev)
2894 return;
2895 }
2896
2892 - if (virtio_has_feature(dev->protocol_features,
2893 - VHOST_USER_PROTOCOL_F_STATUS)) {
2897 + if (vhost_user_has_protocol_feature(dev, VHOST_USER_PROTOCOL_F_STATUS)) {
2898 vhost_user_set_status(dev, 0);
2899 }
2900 }
2901
2902 static bool vhost_user_supports_device_state(struct vhost_dev *dev)
2903 {
2900 - return virtio_has_feature(dev->protocol_features,
2901 - VHOST_USER_PROTOCOL_F_DEVICE_STATE);
2904 + return vhost_user_has_protocol_feature(
2905 + dev, VHOST_USER_PROTOCOL_F_DEVICE_STATE);
2906 }
2907
2908 static int vhost_user_set_device_state_fd(struct vhost_dev *dev,