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