[21580dd] | 1 | /*
|
---|
| 2 | * Copyright (c) 2009 Lukas Mejdrech
|
---|
| 3 | * All rights reserved.
|
---|
| 4 | *
|
---|
| 5 | * Redistribution and use in source and binary forms, with or without
|
---|
| 6 | * modification, are permitted provided that the following conditions
|
---|
| 7 | * are met:
|
---|
| 8 | *
|
---|
| 9 | * - Redistributions of source code must retain the above copyright
|
---|
| 10 | * notice, this list of conditions and the following disclaimer.
|
---|
| 11 | * - Redistributions in binary form must reproduce the above copyright
|
---|
| 12 | * notice, this list of conditions and the following disclaimer in the
|
---|
| 13 | * documentation and/or other materials provided with the distribution.
|
---|
| 14 | * - The name of the author may not be used to endorse or promote products
|
---|
| 15 | * derived from this software without specific prior written permission.
|
---|
| 16 | *
|
---|
| 17 | * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
|
---|
| 18 | * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
|
---|
| 19 | * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
|
---|
| 20 | * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
|
---|
| 21 | * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
|
---|
| 22 | * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
|
---|
| 23 | * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
|
---|
| 24 | * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
|
---|
| 25 | * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
|
---|
| 26 | * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
---|
| 27 | */
|
---|
| 28 |
|
---|
| 29 | /** @addtogroup packet
|
---|
| 30 | * @{
|
---|
| 31 | */
|
---|
| 32 |
|
---|
| 33 | /** @file
|
---|
| 34 | * Packet server implementation.
|
---|
| 35 | */
|
---|
| 36 |
|
---|
| 37 | #include <align.h>
|
---|
| 38 | #include <assert.h>
|
---|
| 39 | #include <async.h>
|
---|
| 40 | #include <errno.h>
|
---|
| 41 | #include <fibril_synch.h>
|
---|
| 42 | #include <unistd.h>
|
---|
| 43 |
|
---|
| 44 | #include <ipc/ipc.h>
|
---|
| 45 | #include <sys/mman.h>
|
---|
| 46 |
|
---|
[849ed54] | 47 | #include <net_err.h>
|
---|
| 48 | #include <net_messages.h>
|
---|
| 49 | #include <packet/packet.h>
|
---|
| 50 | #include <packet/packet_client.h>
|
---|
| 51 | #include <packet/packet_header.h>
|
---|
| 52 | #include <packet/packet_messages.h>
|
---|
| 53 | #include <packet/packet_server.h>
|
---|
[21580dd] | 54 |
|
---|
| 55 | #define FREE_QUEUES_COUNT 7
|
---|
| 56 |
|
---|
| 57 | /** The default address length reserved for new packets.
|
---|
| 58 | */
|
---|
| 59 | #define DEFAULT_ADDR_LEN 32
|
---|
| 60 |
|
---|
| 61 | /** The default prefix reserved for new packets.
|
---|
| 62 | */
|
---|
| 63 | #define DEFAULT_PREFIX 64
|
---|
| 64 |
|
---|
| 65 | /** The default suffix reserved for new packets.
|
---|
| 66 | */
|
---|
| 67 | #define DEFAULT_SUFFIX 64
|
---|
| 68 |
|
---|
| 69 | /** Packet server global data.
|
---|
| 70 | */
|
---|
| 71 | static struct{
|
---|
| 72 | /** Safety lock.
|
---|
| 73 | */
|
---|
| 74 | fibril_mutex_t lock;
|
---|
| 75 | /** Free packet queues.
|
---|
| 76 | */
|
---|
[aadf01e] | 77 | packet_t free[FREE_QUEUES_COUNT];
|
---|
[21580dd] | 78 | /** Packet length upper bounds of the free packet queues.
|
---|
| 79 | * The maximal lengths of packets in each queue in the ascending order.
|
---|
| 80 | * The last queue is not limited.
|
---|
| 81 | */
|
---|
[aadf01e] | 82 | size_t sizes[FREE_QUEUES_COUNT];
|
---|
[21580dd] | 83 | /** Total packets allocated.
|
---|
| 84 | */
|
---|
| 85 | unsigned int count;
|
---|
| 86 | } ps_globals = {
|
---|
| 87 | .lock = {
|
---|
| 88 | .counter = 1,
|
---|
| 89 | .waiters = {
|
---|
[aadf01e] | 90 | .prev = &ps_globals.lock.waiters,
|
---|
| 91 | .next = &ps_globals.lock.waiters,
|
---|
[21580dd] | 92 | }
|
---|
| 93 | },
|
---|
[aadf01e] | 94 | .free = {NULL, NULL, NULL, NULL, NULL, NULL, NULL},
|
---|
| 95 | .sizes = {PAGE_SIZE, PAGE_SIZE * 2, PAGE_SIZE * 4, PAGE_SIZE * 8, PAGE_SIZE * 16, PAGE_SIZE * 32, PAGE_SIZE * 64},
|
---|
[21580dd] | 96 | .count = 0
|
---|
| 97 | };
|
---|
| 98 |
|
---|
[849ed54] | 99 | int packet_translate(int phone, packet_ref packet, packet_id_t packet_id){
|
---|
| 100 | if(! packet){
|
---|
| 101 | return EINVAL;
|
---|
| 102 | }
|
---|
| 103 | *packet = pm_find(packet_id);
|
---|
| 104 | return (*packet) ? EOK : ENOENT;
|
---|
| 105 | }
|
---|
[21580dd] | 106 |
|
---|
[849ed54] | 107 | /** Clears and initializes the packet according to the given dimensions.
|
---|
| 108 | * @param[in] packet The packet to be initialized.
|
---|
[21580dd] | 109 | * @param[in] addr_len The source and destination addresses maximal length in bytes.
|
---|
| 110 | * @param[in] max_prefix The maximal prefix length in bytes.
|
---|
| 111 | * @param[in] max_content The maximal content length in bytes.
|
---|
| 112 | * @param[in] max_suffix The maximal suffix length in bytes.
|
---|
| 113 | */
|
---|
[849ed54] | 114 | static void packet_init(packet_t packet, size_t addr_len, size_t max_prefix, size_t max_content, size_t max_suffix){
|
---|
| 115 | // clear the packet content
|
---|
| 116 | bzero(((void *) packet) + sizeof(struct packet), packet->length - sizeof(struct packet));
|
---|
| 117 | // clear the packet header
|
---|
| 118 | packet->order = 0;
|
---|
| 119 | packet->metric = 0;
|
---|
| 120 | packet->previous = 0;
|
---|
| 121 | packet->next = 0;
|
---|
| 122 | packet->addr_len = 0;
|
---|
| 123 | packet->src_addr = sizeof(struct packet);
|
---|
| 124 | packet->dest_addr = packet->src_addr + addr_len;
|
---|
| 125 | packet->max_prefix = max_prefix;
|
---|
| 126 | packet->max_content = max_content;
|
---|
| 127 | packet->data_start = packet->dest_addr + addr_len + packet->max_prefix;
|
---|
| 128 | packet->data_end = packet->data_start;
|
---|
| 129 | }
|
---|
[21580dd] | 130 |
|
---|
| 131 | /** Creates a new packet of dimensions at least as given.
|
---|
| 132 | * Should be used only when the global data are locked.
|
---|
| 133 | * @param[in] length The total length of the packet, including the header, the addresses and the data of the packet.
|
---|
| 134 | * @param[in] addr_len The source and destination addresses maximal length in bytes.
|
---|
| 135 | * @param[in] max_prefix The maximal prefix length in bytes.
|
---|
| 136 | * @param[in] max_content The maximal content length in bytes.
|
---|
| 137 | * @param[in] max_suffix The maximal suffix length in bytes.
|
---|
| 138 | * @returns The packet of dimensions at least as given.
|
---|
| 139 | * @returns NULL if there is not enough memory left.
|
---|
| 140 | */
|
---|
[849ed54] | 141 | static packet_t packet_create(size_t length, size_t addr_len, size_t max_prefix, size_t max_content, size_t max_suffix){
|
---|
| 142 | ERROR_DECLARE;
|
---|
[21580dd] | 143 |
|
---|
[849ed54] | 144 | packet_t packet;
|
---|
| 145 |
|
---|
| 146 | // already locked
|
---|
| 147 | packet = (packet_t) mmap(NULL, length, PROTO_READ | PROTO_WRITE, MAP_SHARED | MAP_ANONYMOUS, 0, 0);
|
---|
| 148 | if(packet == MAP_FAILED){
|
---|
| 149 | return NULL;
|
---|
| 150 | }
|
---|
| 151 | ++ ps_globals.count;
|
---|
| 152 | packet->packet_id = ps_globals.count;
|
---|
| 153 | packet->length = length;
|
---|
| 154 | packet_init(packet, addr_len, max_prefix, max_content, max_suffix);
|
---|
| 155 | packet->magic_value = PACKET_MAGIC_VALUE;
|
---|
| 156 | if(ERROR_OCCURRED(pm_add(packet))){
|
---|
| 157 | munmap(packet, packet->length);
|
---|
| 158 | return NULL;
|
---|
| 159 | }
|
---|
| 160 | return packet;
|
---|
| 161 | }
|
---|
| 162 |
|
---|
| 163 | /** Returns the packet of dimensions at least as given.
|
---|
| 164 | * Tries to reuse free packets first.
|
---|
| 165 | * Creates a new packet aligned to the memory page size if none available.
|
---|
| 166 | * Locks the global data during its processing.
|
---|
[21580dd] | 167 | * @param[in] addr_len The source and destination addresses maximal length in bytes.
|
---|
| 168 | * @param[in] max_prefix The maximal prefix length in bytes.
|
---|
| 169 | * @param[in] max_content The maximal content length in bytes.
|
---|
| 170 | * @param[in] max_suffix The maximal suffix length in bytes.
|
---|
[849ed54] | 171 | * @returns The packet of dimensions at least as given.
|
---|
| 172 | * @returns NULL if there is not enough memory left.
|
---|
[21580dd] | 173 | */
|
---|
[849ed54] | 174 | static packet_t packet_get(size_t addr_len, size_t max_prefix, size_t max_content, size_t max_suffix){
|
---|
| 175 | int index;
|
---|
| 176 | packet_t packet;
|
---|
| 177 | size_t length;
|
---|
[21580dd] | 178 |
|
---|
[849ed54] | 179 | length = ALIGN_UP(sizeof(struct packet) + 2 * addr_len + max_prefix + max_content + max_suffix, PAGE_SIZE);
|
---|
| 180 | fibril_mutex_lock(&ps_globals.lock);
|
---|
| 181 | for(index = 0; index < FREE_QUEUES_COUNT - 1; ++ index){
|
---|
| 182 | if(length <= ps_globals.sizes[index]){
|
---|
| 183 | packet = ps_globals.free[index];
|
---|
| 184 | while(packet_is_valid(packet) && (packet->length < length)){
|
---|
| 185 | packet = pm_find(packet->next);
|
---|
| 186 | }
|
---|
| 187 | if(packet_is_valid(packet)){
|
---|
| 188 | if(packet == ps_globals.free[index]){
|
---|
| 189 | ps_globals.free[index] = pq_detach(packet);
|
---|
| 190 | }else{
|
---|
| 191 | pq_detach(packet);
|
---|
| 192 | }
|
---|
| 193 | packet_init(packet, addr_len, max_prefix, max_content, max_suffix);
|
---|
| 194 | fibril_mutex_unlock(&ps_globals.lock);
|
---|
| 195 | // remove debug dump
|
---|
| 196 | // printf("packet %d got\n", packet->packet_id);
|
---|
| 197 | return packet;
|
---|
| 198 | }
|
---|
| 199 | }
|
---|
[aadf01e] | 200 | }
|
---|
[849ed54] | 201 | packet = packet_create(length, addr_len, max_prefix, max_content, max_suffix);
|
---|
| 202 | fibril_mutex_unlock(&ps_globals.lock);
|
---|
| 203 | // remove debug dump
|
---|
| 204 | // printf("packet %d created\n", packet->packet_id);
|
---|
| 205 | return packet;
|
---|
[21580dd] | 206 | }
|
---|
| 207 |
|
---|
[aadf01e] | 208 | packet_t packet_get_4(int phone, size_t max_content, size_t addr_len, size_t max_prefix, size_t max_suffix){
|
---|
| 209 | return packet_get(addr_len, max_prefix, max_content, max_suffix);
|
---|
[21580dd] | 210 | }
|
---|
| 211 |
|
---|
[aadf01e] | 212 | packet_t packet_get_1(int phone, size_t content){
|
---|
| 213 | return packet_get(DEFAULT_ADDR_LEN, DEFAULT_PREFIX, content, DEFAULT_SUFFIX);
|
---|
[21580dd] | 214 | }
|
---|
| 215 |
|
---|
[849ed54] | 216 | /** Releases the packet and returns it to the appropriate free packet queue.
|
---|
| 217 | * Should be used only when the global data are locked.
|
---|
| 218 | * @param[in] packet The packet to be released.
|
---|
| 219 | */
|
---|
| 220 | static void packet_release(packet_t packet){
|
---|
| 221 | int index;
|
---|
| 222 | int result;
|
---|
| 223 |
|
---|
| 224 | // remove debug dump
|
---|
| 225 | // printf("packet %d released\n", packet->packet_id);
|
---|
| 226 | for(index = 0; (index < FREE_QUEUES_COUNT - 1) && (packet->length > ps_globals.sizes[index]); ++ index);
|
---|
| 227 | result = pq_add(&ps_globals.free[index], packet, packet->length, packet->length);
|
---|
| 228 | assert(result == EOK);
|
---|
| 229 | }
|
---|
| 230 |
|
---|
| 231 | /** Releases the packet queue.
|
---|
| 232 | * @param[in] packet_id The first packet identifier.
|
---|
| 233 | * @returns EOK on success.
|
---|
| 234 | * @returns ENOENT if there is no such packet.
|
---|
| 235 | */
|
---|
| 236 | static int packet_release_wrapper(packet_id_t packet_id){
|
---|
| 237 | packet_t packet;
|
---|
| 238 |
|
---|
| 239 | packet = pm_find(packet_id);
|
---|
| 240 | if(! packet_is_valid(packet)){
|
---|
| 241 | return ENOENT;
|
---|
| 242 | }
|
---|
| 243 | fibril_mutex_lock(&ps_globals.lock);
|
---|
| 244 | pq_destroy(packet, packet_release);
|
---|
| 245 | fibril_mutex_unlock(&ps_globals.lock);
|
---|
| 246 | return EOK;
|
---|
| 247 | }
|
---|
| 248 |
|
---|
[aadf01e] | 249 | void pq_release(int phone, packet_id_t packet_id){
|
---|
| 250 | (void) packet_release_wrapper(packet_id);
|
---|
[21580dd] | 251 | }
|
---|
| 252 |
|
---|
[849ed54] | 253 | /** Shares the packet memory block.
|
---|
| 254 | * @param[in] packet The packet to be shared.
|
---|
| 255 | * @returns EOK on success.
|
---|
| 256 | * @returns EINVAL if the packet is not valid.
|
---|
| 257 | * @returns EINVAL if the calling module does not accept the memory.
|
---|
| 258 | * @returns ENOMEM if the desired and actual sizes differ.
|
---|
| 259 | * @returns Other error codes as defined for the async_share_in_finalize() function.
|
---|
| 260 | */
|
---|
| 261 | static int packet_reply(const packet_t packet){
|
---|
| 262 | ipc_callid_t callid;
|
---|
| 263 | size_t size;
|
---|
| 264 |
|
---|
| 265 | if(! packet_is_valid(packet)){
|
---|
| 266 | return EINVAL;
|
---|
| 267 | }
|
---|
| 268 | if(async_share_in_receive(&callid, &size) <= 0) return EINVAL;
|
---|
| 269 | if(size != packet->length){
|
---|
| 270 | return ENOMEM;
|
---|
| 271 | }
|
---|
| 272 | return async_share_in_finalize(callid, packet, PROTO_READ | PROTO_WRITE);
|
---|
| 273 | }
|
---|
| 274 |
|
---|
[aadf01e] | 275 | int packet_server_message(ipc_callid_t callid, ipc_call_t * call, ipc_call_t * answer, int * answer_count){
|
---|
[21580dd] | 276 | packet_t packet;
|
---|
| 277 |
|
---|
[aadf01e] | 278 | *answer_count = 0;
|
---|
| 279 | switch(IPC_GET_METHOD(*call)){
|
---|
[21580dd] | 280 | case IPC_M_PHONE_HUNGUP:
|
---|
| 281 | return EOK;
|
---|
| 282 | case NET_PACKET_CREATE_1:
|
---|
[aadf01e] | 283 | packet = packet_get(DEFAULT_ADDR_LEN, DEFAULT_PREFIX, IPC_GET_CONTENT(call), DEFAULT_SUFFIX);
|
---|
| 284 | if(! packet){
|
---|
| 285 | return ENOMEM;
|
---|
| 286 | }
|
---|
| 287 | *answer_count = 2;
|
---|
[3db8889] | 288 | IPC_SET_ARG1(*answer, (ipcarg_t) packet->packet_id);
|
---|
| 289 | IPC_SET_ARG2(*answer, (ipcarg_t) packet->length);
|
---|
[21580dd] | 290 | return EOK;
|
---|
| 291 | case NET_PACKET_CREATE_4:
|
---|
[aadf01e] | 292 | packet = packet_get(((DEFAULT_ADDR_LEN < IPC_GET_ADDR_LEN(call)) ? IPC_GET_ADDR_LEN(call) : DEFAULT_ADDR_LEN), DEFAULT_PREFIX + IPC_GET_PREFIX(call), IPC_GET_CONTENT(call), DEFAULT_SUFFIX + IPC_GET_SUFFIX(call));
|
---|
| 293 | if(! packet){
|
---|
| 294 | return ENOMEM;
|
---|
| 295 | }
|
---|
| 296 | *answer_count = 2;
|
---|
[3db8889] | 297 | IPC_SET_ARG1(*answer, (ipcarg_t) packet->packet_id);
|
---|
| 298 | IPC_SET_ARG2(*answer, (ipcarg_t) packet->length);
|
---|
[21580dd] | 299 | return EOK;
|
---|
| 300 | case NET_PACKET_GET:
|
---|
[aadf01e] | 301 | packet = pm_find(IPC_GET_ID(call));
|
---|
| 302 | if(! packet_is_valid(packet)){
|
---|
| 303 | return ENOENT;
|
---|
| 304 | }
|
---|
| 305 | return packet_reply(packet);
|
---|
[21580dd] | 306 | case NET_PACKET_GET_SIZE:
|
---|
[aadf01e] | 307 | packet = pm_find(IPC_GET_ID(call));
|
---|
| 308 | if(! packet_is_valid(packet)){
|
---|
| 309 | return ENOENT;
|
---|
| 310 | }
|
---|
[3db8889] | 311 | IPC_SET_ARG1(*answer, (ipcarg_t) packet->length);
|
---|
[aadf01e] | 312 | *answer_count = 1;
|
---|
[21580dd] | 313 | return EOK;
|
---|
| 314 | case NET_PACKET_RELEASE:
|
---|
[aadf01e] | 315 | return packet_release_wrapper(IPC_GET_ID(call));
|
---|
[21580dd] | 316 | }
|
---|
| 317 | return ENOTSUP;
|
---|
| 318 | }
|
---|
| 319 |
|
---|
| 320 | /** @}
|
---|
| 321 | */
|
---|