Jakub Grajciar | 7c5c40d | 2017-08-30 10:13:25 +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 | #ifndef _MEMIF_H_ |
| 19 | #define _MEMIF_H_ |
| 20 | |
| 21 | #ifndef MEMIF_CACHELINE_SIZE |
| 22 | #define MEMIF_CACHELINE_SIZE 64 |
| 23 | #endif |
| 24 | |
Jakub Grajciar | ecfa2aa | 2018-03-26 11:26:34 +0200 | [diff] [blame] | 25 | #define MEMIF_COOKIE 0x3E31F20 |
| 26 | #define MEMIF_VERSION_MAJOR 2 |
Jakub Grajciar | 7c5c40d | 2017-08-30 10:13:25 +0200 | [diff] [blame] | 27 | #define MEMIF_VERSION_MINOR 0 |
| 28 | #define MEMIF_VERSION ((MEMIF_VERSION_MAJOR << 8) | MEMIF_VERSION_MINOR) |
| 29 | |
| 30 | /* |
| 31 | * Type definitions |
| 32 | */ |
| 33 | |
| 34 | typedef enum memif_msg_type |
| 35 | { |
| 36 | MEMIF_MSG_TYPE_NONE = 0, |
| 37 | MEMIF_MSG_TYPE_ACK = 1, |
| 38 | MEMIF_MSG_TYPE_HELLO = 2, |
| 39 | MEMIF_MSG_TYPE_INIT = 3, |
| 40 | MEMIF_MSG_TYPE_ADD_REGION = 4, |
| 41 | MEMIF_MSG_TYPE_ADD_RING = 5, |
| 42 | MEMIF_MSG_TYPE_CONNECT = 6, |
| 43 | MEMIF_MSG_TYPE_CONNECTED = 7, |
| 44 | MEMIF_MSG_TYPE_DISCONNECT = 8, |
| 45 | } memif_msg_type_t; |
| 46 | |
| 47 | typedef enum |
| 48 | { |
| 49 | MEMIF_RING_S2M = 0, |
| 50 | MEMIF_RING_M2S = 1 |
| 51 | } memif_ring_type_t; |
| 52 | |
| 53 | typedef enum |
| 54 | { |
| 55 | MEMIF_INTERFACE_MODE_ETHERNET = 0, |
| 56 | MEMIF_INTERFACE_MODE_IP = 1, |
| 57 | MEMIF_INTERFACE_MODE_PUNT_INJECT = 2, |
| 58 | } memif_interface_mode_t; |
| 59 | |
| 60 | typedef uint16_t memif_region_index_t; |
Jakub Grajciar | ecfa2aa | 2018-03-26 11:26:34 +0200 | [diff] [blame] | 61 | typedef uint32_t memif_region_offset_t; |
Jakub Grajciar | 7c5c40d | 2017-08-30 10:13:25 +0200 | [diff] [blame] | 62 | typedef uint64_t memif_region_size_t; |
| 63 | typedef uint16_t memif_ring_index_t; |
| 64 | typedef uint32_t memif_interface_id_t; |
| 65 | typedef uint16_t memif_version_t; |
| 66 | typedef uint8_t memif_log2_ring_size_t; |
| 67 | |
| 68 | /* |
| 69 | * Socket messages |
| 70 | */ |
| 71 | |
| 72 | typedef struct __attribute__ ((packed)) |
| 73 | { |
| 74 | uint8_t name[32]; |
| 75 | memif_version_t min_version; |
| 76 | memif_version_t max_version; |
| 77 | memif_region_index_t max_region; |
| 78 | memif_ring_index_t max_m2s_ring; |
| 79 | memif_ring_index_t max_s2m_ring; |
| 80 | memif_log2_ring_size_t max_log2_ring_size; |
| 81 | } memif_msg_hello_t; |
| 82 | |
| 83 | typedef struct __attribute__ ((packed)) |
| 84 | { |
| 85 | memif_version_t version; |
| 86 | memif_interface_id_t id; |
| 87 | memif_interface_mode_t mode:8; |
| 88 | uint8_t secret[24]; |
| 89 | uint8_t name[32]; |
| 90 | } memif_msg_init_t; |
| 91 | |
| 92 | typedef struct __attribute__ ((packed)) |
| 93 | { |
| 94 | memif_region_index_t index; |
| 95 | memif_region_size_t size; |
| 96 | } memif_msg_add_region_t; |
| 97 | |
| 98 | typedef struct __attribute__ ((packed)) |
| 99 | { |
| 100 | uint16_t flags; |
| 101 | #define MEMIF_MSG_ADD_RING_FLAG_S2M (1 << 0) |
| 102 | memif_ring_index_t index; |
| 103 | memif_region_index_t region; |
| 104 | memif_region_offset_t offset; |
| 105 | memif_log2_ring_size_t log2_ring_size; |
Jakub Grajciar | ab7c2b0 | 2018-03-28 10:21:05 +0200 | [diff] [blame] | 106 | uint16_t private_hdr_size; /* used for private metadata */ |
Jakub Grajciar | 7c5c40d | 2017-08-30 10:13:25 +0200 | [diff] [blame] | 107 | } memif_msg_add_ring_t; |
| 108 | |
| 109 | typedef struct __attribute__ ((packed)) |
| 110 | { |
| 111 | uint8_t if_name[32]; |
| 112 | } memif_msg_connect_t; |
| 113 | |
| 114 | typedef struct __attribute__ ((packed)) |
| 115 | { |
| 116 | uint8_t if_name[32]; |
| 117 | } memif_msg_connected_t; |
| 118 | |
| 119 | typedef struct __attribute__ ((packed)) |
| 120 | { |
| 121 | uint32_t code; |
| 122 | uint8_t string[96]; |
| 123 | } memif_msg_disconnect_t; |
| 124 | |
| 125 | typedef struct __attribute__ ((packed, aligned (128))) |
| 126 | { |
| 127 | memif_msg_type_t type:16; |
| 128 | union |
| 129 | { |
| 130 | memif_msg_hello_t hello; |
| 131 | memif_msg_init_t init; |
| 132 | memif_msg_add_region_t add_region; |
| 133 | memif_msg_add_ring_t add_ring; |
| 134 | memif_msg_connect_t connect; |
| 135 | memif_msg_connected_t connected; |
| 136 | memif_msg_disconnect_t disconnect; |
| 137 | }; |
| 138 | } memif_msg_t; |
| 139 | |
| 140 | _Static_assert (sizeof (memif_msg_t) == 128, |
| 141 | "Size of memif_msg_t must be 128"); |
| 142 | |
| 143 | /* |
| 144 | * Ring and Descriptor Layout |
| 145 | */ |
| 146 | |
| 147 | typedef struct __attribute__ ((packed)) |
| 148 | { |
| 149 | uint16_t flags; |
| 150 | #define MEMIF_DESC_FLAG_NEXT (1 << 0) |
| 151 | memif_region_index_t region; |
Jakub Grajciar | 7c5c40d | 2017-08-30 10:13:25 +0200 | [diff] [blame] | 152 | uint32_t length; |
Jakub Grajciar | 7c5c40d | 2017-08-30 10:13:25 +0200 | [diff] [blame] | 153 | memif_region_offset_t offset; |
Jakub Grajciar | ecfa2aa | 2018-03-26 11:26:34 +0200 | [diff] [blame] | 154 | uint32_t metadata; |
Jakub Grajciar | 7c5c40d | 2017-08-30 10:13:25 +0200 | [diff] [blame] | 155 | } memif_desc_t; |
| 156 | |
Jakub Grajciar | ecfa2aa | 2018-03-26 11:26:34 +0200 | [diff] [blame] | 157 | _Static_assert (sizeof (memif_desc_t) == 16, |
| 158 | "Size of memif_dsct_t must be 16 bytes"); |
Jakub Grajciar | 7c5c40d | 2017-08-30 10:13:25 +0200 | [diff] [blame] | 159 | |
| 160 | #define MEMIF_CACHELINE_ALIGN_MARK(mark) \ |
| 161 | uint8_t mark[0] __attribute__((aligned(MEMIF_CACHELINE_SIZE))) |
| 162 | |
| 163 | typedef struct |
| 164 | { |
| 165 | MEMIF_CACHELINE_ALIGN_MARK (cacheline0); |
| 166 | uint32_t cookie; |
| 167 | uint16_t flags; |
| 168 | #define MEMIF_RING_FLAG_MASK_INT 1 |
| 169 | volatile uint16_t head; |
| 170 | MEMIF_CACHELINE_ALIGN_MARK (cacheline1); |
| 171 | volatile uint16_t tail; |
| 172 | MEMIF_CACHELINE_ALIGN_MARK (cacheline2); |
| 173 | memif_desc_t desc[0]; |
| 174 | } memif_ring_t; |
| 175 | |
| 176 | #endif /* _MEMIF_H_ */ |
| 177 | |
| 178 | /* |
| 179 | * fd.io coding-style-patch-verification: ON |
| 180 | * |
| 181 | * Local Variables: |
| 182 | * eval: (c-set-style "gnu") |
| 183 | * End: |
| 184 | */ |