Mohsin Kazmi | a7a2281 | 2020-08-31 17:17:16 +0200 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (c) 2015 Cisco and/or its affiliates. |
| 3 | * Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | * you may not use this file except in compliance with the License. |
| 5 | * You may obtain a copy of the License at: |
| 6 | * |
| 7 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | * |
| 9 | * Unless required by applicable law or agreed to in writing, software |
| 10 | * distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | * See the License for the specific language governing permissions and |
| 13 | * limitations under the License. |
| 14 | */ |
| 15 | #ifndef __VIRTIO_STD_H__ |
| 16 | #define __VIRTIO_STD_H__ |
| 17 | |
| 18 | #define foreach_virtio_net_features \ |
| 19 | _ (VIRTIO_NET_F_CSUM, 0) /* Host handles pkts w/ partial csum */ \ |
| 20 | _ (VIRTIO_NET_F_GUEST_CSUM, 1) /* Guest handles pkts w/ partial csum */ \ |
| 21 | _ (VIRTIO_NET_F_CTRL_GUEST_OFFLOADS, 2) /* Dynamic offload configuration. */ \ |
| 22 | _ (VIRTIO_NET_F_MTU, 3) /* Initial MTU advice. */ \ |
| 23 | _ (VIRTIO_NET_F_MAC, 5) /* Host has given MAC address. */ \ |
| 24 | _ (VIRTIO_NET_F_GSO, 6) /* Host handles pkts w/ any GSO. */ \ |
| 25 | _ (VIRTIO_NET_F_GUEST_TSO4, 7) /* Guest can handle TSOv4 in. */ \ |
| 26 | _ (VIRTIO_NET_F_GUEST_TSO6, 8) /* Guest can handle TSOv6 in. */ \ |
| 27 | _ (VIRTIO_NET_F_GUEST_ECN, 9) /* Guest can handle TSO[6] w/ ECN in. */ \ |
| 28 | _ (VIRTIO_NET_F_GUEST_UFO, 10) /* Guest can handle UFO in. */ \ |
| 29 | _ (VIRTIO_NET_F_HOST_TSO4, 11) /* Host can handle TSOv4 in. */ \ |
| 30 | _ (VIRTIO_NET_F_HOST_TSO6, 12) /* Host can handle TSOv6 in. */ \ |
| 31 | _ (VIRTIO_NET_F_HOST_ECN, 13) /* Host can handle TSO[6] w/ ECN in. */ \ |
| 32 | _ (VIRTIO_NET_F_HOST_UFO, 14) /* Host can handle UFO in. */ \ |
| 33 | _ (VIRTIO_NET_F_MRG_RXBUF, 15) /* Host can merge receive buffers. */ \ |
| 34 | _ (VIRTIO_NET_F_STATUS, 16) /* virtio_net_config.status available */ \ |
| 35 | _ (VIRTIO_NET_F_CTRL_VQ, 17) /* Control channel available */ \ |
| 36 | _ (VIRTIO_NET_F_CTRL_RX, 18) /* Control channel RX mode support */ \ |
| 37 | _ (VIRTIO_NET_F_CTRL_VLAN, 19) /* Control channel VLAN filtering */ \ |
| 38 | _ (VIRTIO_NET_F_CTRL_RX_EXTRA, 20) /* Extra RX mode control support */ \ |
| 39 | _ (VIRTIO_NET_F_GUEST_ANNOUNCE, 21) /* Guest can announce device on the network */ \ |
| 40 | _ (VIRTIO_NET_F_MQ, 22) /* Device supports Receive Flow Steering */ \ |
| 41 | _ (VIRTIO_NET_F_CTRL_MAC_ADDR, 23) /* Set MAC address */ \ |
| 42 | _ (VIRTIO_F_NOTIFY_ON_EMPTY, 24) \ |
| 43 | _ (VHOST_F_LOG_ALL, 26) /* Log all write descriptors */ \ |
| 44 | _ (VIRTIO_F_ANY_LAYOUT, 27) /* Can the device handle any descriptor layout */ \ |
| 45 | _ (VIRTIO_RING_F_INDIRECT_DESC, 28) /* Support indirect buffer descriptors */ \ |
| 46 | _ (VIRTIO_RING_F_EVENT_IDX, 29) /* The Guest publishes the used index for which it expects an interrupt \ |
| 47 | * at the end of the avail ring. Host should ignore the avail->flags field. */ \ |
| 48 | /* The Host publishes the avail index for which it expects a kick \ |
| 49 | * at the end of the used ring. Guest should ignore the used->flags field. */ \ |
| 50 | _ (VHOST_USER_F_PROTOCOL_FEATURES, 30) \ |
| 51 | _ (VIRTIO_F_VERSION_1, 32) /* v1.0 compliant. */ \ |
| 52 | _ (VIRTIO_F_IOMMU_PLATFORM, 33) \ |
| 53 | _ (VIRTIO_F_RING_PACKED, 34) \ |
| 54 | _ (VIRTIO_F_IN_ORDER, 35) /* all buffers are used by the device in the */ \ |
| 55 | /* same order in which they have been made available */ \ |
| 56 | _ (VIRTIO_F_ORDER_PLATFORM, 36) /* memory accesses by the driver and the */ \ |
| 57 | /* device are ordered in a way described by the platfor */ \ |
| 58 | _ (VIRTIO_F_NOTIFICATION_DATA, 38) /* the driver passes extra data (besides */ \ |
| 59 | /* identifying the virtqueue) in its device notifications. */ \ |
| 60 | _ (VIRTIO_NET_F_SPEED_DUPLEX, 63) /* Device set linkspeed and duplex */ |
| 61 | |
| 62 | typedef enum |
| 63 | { |
| 64 | #define _(f,n) f = n, |
| 65 | foreach_virtio_net_features |
| 66 | #undef _ |
Mohsin Kazmi | 0f8912f | 2022-02-01 18:35:59 +0000 | [diff] [blame] | 67 | } vnet_virtio_net_feature_t; |
Mohsin Kazmi | a7a2281 | 2020-08-31 17:17:16 +0200 | [diff] [blame] | 68 | |
| 69 | #define VIRTIO_FEATURE(X) (1ULL << X) |
| 70 | |
| 71 | #define VRING_MAX_SIZE 32768 |
| 72 | |
| 73 | #define VRING_DESC_F_NEXT 1 |
| 74 | #define VRING_DESC_F_WRITE 2 |
| 75 | #define VRING_DESC_F_INDIRECT 4 |
| 76 | |
| 77 | #define VRING_DESC_F_AVAIL (1 << 7) |
| 78 | #define VRING_DESC_F_USED (1 << 15) |
| 79 | |
Steven Luong | 27ba500 | 2020-11-17 13:30:44 -0800 | [diff] [blame] | 80 | #define foreach_virtio_event_idx_flags \ |
| 81 | _ (VRING_EVENT_F_ENABLE, 0) \ |
| 82 | _ (VRING_EVENT_F_DISABLE, 1) \ |
| 83 | _ (VRING_EVENT_F_DESC, 2) |
| 84 | |
| 85 | typedef enum |
| 86 | { |
| 87 | #define _(f,n) f = n, |
| 88 | foreach_virtio_event_idx_flags |
| 89 | #undef _ |
Mohsin Kazmi | 0f8912f | 2022-02-01 18:35:59 +0000 | [diff] [blame] | 90 | } vnet_virtio_event_idx_flags_t; |
Mohsin Kazmi | a7a2281 | 2020-08-31 17:17:16 +0200 | [diff] [blame] | 91 | |
| 92 | #define VRING_USED_F_NO_NOTIFY 1 |
| 93 | #define VRING_AVAIL_F_NO_INTERRUPT 1 |
| 94 | |
| 95 | typedef struct |
| 96 | { |
| 97 | u64 addr; |
| 98 | u32 len; |
| 99 | u16 flags; |
| 100 | u16 next; |
Mohsin Kazmi | 0f8912f | 2022-02-01 18:35:59 +0000 | [diff] [blame] | 101 | } vnet_virtio_vring_desc_t; |
Mohsin Kazmi | a7a2281 | 2020-08-31 17:17:16 +0200 | [diff] [blame] | 102 | |
| 103 | typedef struct |
| 104 | { |
| 105 | u16 flags; |
| 106 | u16 idx; |
| 107 | u16 ring[0]; |
| 108 | /* u16 used_event; */ |
Mohsin Kazmi | 0f8912f | 2022-02-01 18:35:59 +0000 | [diff] [blame] | 109 | } vnet_virtio_vring_avail_t; |
Mohsin Kazmi | a7a2281 | 2020-08-31 17:17:16 +0200 | [diff] [blame] | 110 | |
| 111 | typedef struct |
| 112 | { |
| 113 | u32 id; |
| 114 | u32 len; |
Mohsin Kazmi | 0f8912f | 2022-02-01 18:35:59 +0000 | [diff] [blame] | 115 | } vnet_virtio_vring_used_elem_t; |
Mohsin Kazmi | a7a2281 | 2020-08-31 17:17:16 +0200 | [diff] [blame] | 116 | |
| 117 | typedef struct |
| 118 | { |
| 119 | u16 flags; |
| 120 | u16 idx; |
Mohsin Kazmi | 0f8912f | 2022-02-01 18:35:59 +0000 | [diff] [blame] | 121 | vnet_virtio_vring_used_elem_t ring[0]; |
Mohsin Kazmi | a7a2281 | 2020-08-31 17:17:16 +0200 | [diff] [blame] | 122 | /* u16 avail_event; */ |
Mohsin Kazmi | 0f8912f | 2022-02-01 18:35:59 +0000 | [diff] [blame] | 123 | } vnet_virtio_vring_used_t; |
Mohsin Kazmi | a7a2281 | 2020-08-31 17:17:16 +0200 | [diff] [blame] | 124 | |
| 125 | /* *INDENT-OFF* */ |
Mohsin Kazmi | 0f8912f | 2022-02-01 18:35:59 +0000 | [diff] [blame] | 126 | typedef CLIB_PACKED (struct { |
| 127 | u64 addr; // packet data buffer address |
| 128 | u32 len; // packet data buffer size |
| 129 | u16 id; // buffer id |
| 130 | u16 flags; // flags |
| 131 | }) vnet_virtio_vring_packed_desc_t; |
Mohsin Kazmi | a7a2281 | 2020-08-31 17:17:16 +0200 | [diff] [blame] | 132 | |
Mohsin Kazmi | 0f8912f | 2022-02-01 18:35:59 +0000 | [diff] [blame] | 133 | STATIC_ASSERT_SIZEOF (vnet_virtio_vring_packed_desc_t, 16); |
Mohsin Kazmi | a7a2281 | 2020-08-31 17:17:16 +0200 | [diff] [blame] | 134 | |
Mohsin Kazmi | 0f8912f | 2022-02-01 18:35:59 +0000 | [diff] [blame] | 135 | typedef CLIB_PACKED (struct { |
Mohsin Kazmi | a7a2281 | 2020-08-31 17:17:16 +0200 | [diff] [blame] | 136 | u16 off_wrap; |
| 137 | u16 flags; |
Mohsin Kazmi | 0f8912f | 2022-02-01 18:35:59 +0000 | [diff] [blame] | 138 | }) vnet_virtio_vring_desc_event_t; |
Mohsin Kazmi | a7a2281 | 2020-08-31 17:17:16 +0200 | [diff] [blame] | 139 | |
| 140 | #define VIRTIO_NET_HDR_F_NEEDS_CSUM 1 /* Use csum_start, csum_offset */ |
| 141 | #define VIRTIO_NET_HDR_F_DATA_VALID 2 /* Csum is valid */ |
| 142 | |
| 143 | #define VIRTIO_NET_HDR_GSO_NONE 0 /* Not a GSO frame */ |
| 144 | #define VIRTIO_NET_HDR_GSO_TCPV4 1 /* GSO frame, IPv4 TCP (TSO) */ |
| 145 | #define VIRTIO_NET_HDR_GSO_UDP 3 /* GSO frame, IPv4 UDP (UFO) */ |
| 146 | #define VIRTIO_NET_HDR_GSO_TCPV6 4 /* GSO frame, IPv6 TCP */ |
| 147 | #define VIRTIO_NET_HDR_GSO_ECN 0x80 /* TCP has ECN set */ |
| 148 | |
Mohsin Kazmi | 0f8912f | 2022-02-01 18:35:59 +0000 | [diff] [blame] | 149 | typedef CLIB_PACKED (struct { |
Mohsin Kazmi | a7a2281 | 2020-08-31 17:17:16 +0200 | [diff] [blame] | 150 | u8 flags; |
| 151 | u8 gso_type; |
| 152 | u16 hdr_len; /* Ethernet + IP + tcp/udp hdrs */ |
| 153 | u16 gso_size; /* Bytes to append to hdr_len per frame */ |
| 154 | u16 csum_start; /* Position to start checksumming from */ |
| 155 | u16 csum_offset; /* Offset after that to place checksum */ |
| 156 | u16 num_buffers; /* Number of merged rx buffers */ |
Mohsin Kazmi | 0f8912f | 2022-02-01 18:35:59 +0000 | [diff] [blame] | 157 | }) vnet_virtio_net_hdr_v1_t; |
Mohsin Kazmi | a7a2281 | 2020-08-31 17:17:16 +0200 | [diff] [blame] | 158 | |
Mohsin Kazmi | 0f8912f | 2022-02-01 18:35:59 +0000 | [diff] [blame] | 159 | typedef CLIB_PACKED (struct { |
Mohsin Kazmi | a7a2281 | 2020-08-31 17:17:16 +0200 | [diff] [blame] | 160 | u8 flags; |
| 161 | u8 gso_type; |
| 162 | u16 hdr_len; |
| 163 | u16 gso_size; |
| 164 | u16 csum_start; |
| 165 | u16 csum_offset; |
Mohsin Kazmi | 0f8912f | 2022-02-01 18:35:59 +0000 | [diff] [blame] | 166 | }) vnet_virtio_net_hdr_t; |
Mohsin Kazmi | a7a2281 | 2020-08-31 17:17:16 +0200 | [diff] [blame] | 167 | |
Mohsin Kazmi | 0f8912f | 2022-02-01 18:35:59 +0000 | [diff] [blame] | 168 | typedef CLIB_PACKED (struct { |
| 169 | vnet_virtio_net_hdr_t hdr; |
Mohsin Kazmi | a7a2281 | 2020-08-31 17:17:16 +0200 | [diff] [blame] | 170 | u16 num_buffers; |
Mohsin Kazmi | 0f8912f | 2022-02-01 18:35:59 +0000 | [diff] [blame] | 171 | }) vnet_virtio_net_hdr_mrg_rxbuf_t; |
Mohsin Kazmi | a7a2281 | 2020-08-31 17:17:16 +0200 | [diff] [blame] | 172 | |
| 173 | /* *INDENT-ON* */ |
Mohsin Kazmi | a7a2281 | 2020-08-31 17:17:16 +0200 | [diff] [blame] | 174 | #endif |
| 175 | |
| 176 | /* |
| 177 | * fd.io coding-style-patch-verification: ON |
| 178 | * |
| 179 | * Local Variables: |
| 180 | * eval: (c-set-style "gnu") |
| 181 | * End: |
| 182 | */ |