| 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 <err.h>
|
|---|
| 42 | #include <fibril_synch.h>
|
|---|
| 43 | #include <unistd.h>
|
|---|
| 44 |
|
|---|
| 45 | #include <ipc/ipc.h>
|
|---|
| 46 | #include <sys/mman.h>
|
|---|
| 47 |
|
|---|
| 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>
|
|---|
| 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 | */
|
|---|
| 77 | packet_t free[FREE_QUEUES_COUNT];
|
|---|
| 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 | */
|
|---|
| 82 | size_t sizes[FREE_QUEUES_COUNT];
|
|---|
| 83 | /** Total packets allocated.
|
|---|
| 84 | */
|
|---|
| 85 | unsigned int count;
|
|---|
| 86 | } ps_globals = {
|
|---|
| 87 | .lock = {
|
|---|
| 88 | .counter = 1,
|
|---|
| 89 | .waiters = {
|
|---|
| 90 | .prev = &ps_globals.lock.waiters,
|
|---|
| 91 | .next = &ps_globals.lock.waiters,
|
|---|
| 92 | }
|
|---|
| 93 | },
|
|---|
| 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},
|
|---|
| 96 | .count = 0
|
|---|
| 97 | };
|
|---|
| 98 |
|
|---|
| 99 | int packet_translate_local(int phone, packet_ref packet, packet_id_t packet_id)
|
|---|
| 100 | {
|
|---|
| 101 | if (!packet)
|
|---|
| 102 | return EINVAL;
|
|---|
| 103 |
|
|---|
| 104 | *packet = pm_find(packet_id);
|
|---|
| 105 | return (*packet) ? EOK : ENOENT;
|
|---|
| 106 | }
|
|---|
| 107 |
|
|---|
| 108 | /** Clears and initializes the packet according to the given dimensions.
|
|---|
| 109 | * @param[in] packet The packet to be initialized.
|
|---|
| 110 | * @param[in] addr_len The source and destination addresses maximal length in bytes.
|
|---|
| 111 | * @param[in] max_prefix The maximal prefix length in bytes.
|
|---|
| 112 | * @param[in] max_content The maximal content length in bytes.
|
|---|
| 113 | * @param[in] max_suffix The maximal suffix length in bytes.
|
|---|
| 114 | */
|
|---|
| 115 | static void packet_init(packet_t packet, size_t addr_len, size_t max_prefix, size_t max_content, size_t max_suffix){
|
|---|
| 116 | // clear the packet content
|
|---|
| 117 | bzero(((void *) packet) + sizeof(struct packet), packet->length - sizeof(struct packet));
|
|---|
| 118 | // clear the packet header
|
|---|
| 119 | packet->order = 0;
|
|---|
| 120 | packet->metric = 0;
|
|---|
| 121 | packet->previous = 0;
|
|---|
| 122 | packet->next = 0;
|
|---|
| 123 | packet->addr_len = 0;
|
|---|
| 124 | packet->src_addr = sizeof(struct packet);
|
|---|
| 125 | packet->dest_addr = packet->src_addr + addr_len;
|
|---|
| 126 | packet->max_prefix = max_prefix;
|
|---|
| 127 | packet->max_content = max_content;
|
|---|
| 128 | packet->data_start = packet->dest_addr + addr_len + packet->max_prefix;
|
|---|
| 129 | packet->data_end = packet->data_start;
|
|---|
| 130 | }
|
|---|
| 131 |
|
|---|
| 132 | /** Creates a new packet of dimensions at least as given.
|
|---|
| 133 | * Should be used only when the global data are locked.
|
|---|
| 134 | * @param[in] length The total length of the packet, including the header, the addresses and the data of the packet.
|
|---|
| 135 | * @param[in] addr_len The source and destination addresses maximal length in bytes.
|
|---|
| 136 | * @param[in] max_prefix The maximal prefix length in bytes.
|
|---|
| 137 | * @param[in] max_content The maximal content length in bytes.
|
|---|
| 138 | * @param[in] max_suffix The maximal suffix length in bytes.
|
|---|
| 139 | * @returns The packet of dimensions at least as given.
|
|---|
| 140 | * @returns NULL if there is not enough memory left.
|
|---|
| 141 | */
|
|---|
| 142 | static packet_t packet_create(size_t length, size_t addr_len, size_t max_prefix, size_t max_content, size_t max_suffix){
|
|---|
| 143 | ERROR_DECLARE;
|
|---|
| 144 |
|
|---|
| 145 | packet_t packet;
|
|---|
| 146 |
|
|---|
| 147 | // already locked
|
|---|
| 148 | packet = (packet_t) mmap(NULL, length, PROTO_READ | PROTO_WRITE, MAP_SHARED | MAP_ANONYMOUS, 0, 0);
|
|---|
| 149 | if(packet == MAP_FAILED){
|
|---|
| 150 | return NULL;
|
|---|
| 151 | }
|
|---|
| 152 | ++ ps_globals.count;
|
|---|
| 153 | packet->packet_id = ps_globals.count;
|
|---|
| 154 | packet->length = length;
|
|---|
| 155 | packet_init(packet, addr_len, max_prefix, max_content, max_suffix);
|
|---|
| 156 | packet->magic_value = PACKET_MAGIC_VALUE;
|
|---|
| 157 | if(ERROR_OCCURRED(pm_add(packet))){
|
|---|
| 158 | munmap(packet, packet->length);
|
|---|
| 159 | return NULL;
|
|---|
| 160 | }
|
|---|
| 161 | return packet;
|
|---|
| 162 | }
|
|---|
| 163 |
|
|---|
| 164 | /** Return the packet of dimensions at least as given.
|
|---|
| 165 | *
|
|---|
| 166 | * Try to reuse free packets first.
|
|---|
| 167 | * Create a new packet aligned to the memory page size if none available.
|
|---|
| 168 | * Lock the global data during its processing.
|
|---|
| 169 | *
|
|---|
| 170 | * @param[in] addr_len The source and destination addresses
|
|---|
| 171 | * maximal length in bytes.
|
|---|
| 172 | * @param[in] max_prefix The maximal prefix length in bytes.
|
|---|
| 173 | * @param[in] max_content The maximal content length in bytes.
|
|---|
| 174 | * @param[in] max_suffix The maximal suffix length in bytes.
|
|---|
| 175 | *
|
|---|
| 176 | * @return The packet of dimensions at least as given.
|
|---|
| 177 | * @return NULL if there is not enough memory left.
|
|---|
| 178 | *
|
|---|
| 179 | */
|
|---|
| 180 | static packet_t packet_get_local(size_t addr_len, size_t max_prefix,
|
|---|
| 181 | size_t max_content, size_t max_suffix)
|
|---|
| 182 | {
|
|---|
| 183 | size_t length = ALIGN_UP(sizeof(struct packet) + 2 * addr_len + max_prefix
|
|---|
| 184 | + max_content + max_suffix, PAGE_SIZE);
|
|---|
| 185 |
|
|---|
| 186 | fibril_mutex_lock(&ps_globals.lock);
|
|---|
| 187 |
|
|---|
| 188 | packet_t packet;
|
|---|
| 189 | unsigned int index;
|
|---|
| 190 |
|
|---|
| 191 | for (index = 0; index < FREE_QUEUES_COUNT - 1; index++) {
|
|---|
| 192 | if (length <= ps_globals.sizes[index]) {
|
|---|
| 193 | packet = ps_globals.free[index];
|
|---|
| 194 |
|
|---|
| 195 | while (packet_is_valid(packet) && (packet->length < length))
|
|---|
| 196 | packet = pm_find(packet->next);
|
|---|
| 197 |
|
|---|
| 198 | if (packet_is_valid(packet)) {
|
|---|
| 199 | if (packet == ps_globals.free[index])
|
|---|
| 200 | ps_globals.free[index] = pq_detach(packet);
|
|---|
| 201 | else
|
|---|
| 202 | pq_detach(packet);
|
|---|
| 203 |
|
|---|
| 204 | packet_init(packet, addr_len, max_prefix, max_content,
|
|---|
| 205 | max_suffix);
|
|---|
| 206 | fibril_mutex_unlock(&ps_globals.lock);
|
|---|
| 207 |
|
|---|
| 208 | return packet;
|
|---|
| 209 | }
|
|---|
| 210 | }
|
|---|
| 211 | }
|
|---|
| 212 |
|
|---|
| 213 | packet = packet_create(length, addr_len, max_prefix, max_content,
|
|---|
| 214 | max_suffix);
|
|---|
| 215 |
|
|---|
| 216 | fibril_mutex_unlock(&ps_globals.lock);
|
|---|
| 217 |
|
|---|
| 218 | return packet;
|
|---|
| 219 | }
|
|---|
| 220 |
|
|---|
| 221 | packet_t packet_get_4_local(int phone, size_t max_content, size_t addr_len,
|
|---|
| 222 | size_t max_prefix, size_t max_suffix)
|
|---|
| 223 | {
|
|---|
| 224 | return packet_get_local(addr_len, max_prefix, max_content, max_suffix);
|
|---|
| 225 | }
|
|---|
| 226 |
|
|---|
| 227 | packet_t packet_get_1_local(int phone, size_t content)
|
|---|
| 228 | {
|
|---|
| 229 | return packet_get_local(DEFAULT_ADDR_LEN, DEFAULT_PREFIX, content,
|
|---|
| 230 | DEFAULT_SUFFIX);
|
|---|
| 231 | }
|
|---|
| 232 |
|
|---|
| 233 | /** Release the packet and returns it to the appropriate free packet queue.
|
|---|
| 234 | *
|
|---|
| 235 | * Should be used only when the global data are locked.
|
|---|
| 236 | *
|
|---|
| 237 | * @param[in] packet The packet to be released.
|
|---|
| 238 | *
|
|---|
| 239 | */
|
|---|
| 240 | static void packet_release(packet_t packet){
|
|---|
| 241 | int index;
|
|---|
| 242 | int result;
|
|---|
| 243 |
|
|---|
| 244 | // remove debug dump
|
|---|
| 245 | // printf("packet %d released\n", packet->packet_id);
|
|---|
| 246 | for(index = 0; (index < FREE_QUEUES_COUNT - 1) && (packet->length > ps_globals.sizes[index]); ++ index);
|
|---|
| 247 | result = pq_add(&ps_globals.free[index], packet, packet->length, packet->length);
|
|---|
| 248 | assert(result == EOK);
|
|---|
| 249 | }
|
|---|
| 250 |
|
|---|
| 251 | /** Releases the packet queue.
|
|---|
| 252 | * @param[in] packet_id The first packet identifier.
|
|---|
| 253 | * @returns EOK on success.
|
|---|
| 254 | * @returns ENOENT if there is no such packet.
|
|---|
| 255 | */
|
|---|
| 256 | static int packet_release_wrapper(packet_id_t packet_id){
|
|---|
| 257 | packet_t packet;
|
|---|
| 258 |
|
|---|
| 259 | packet = pm_find(packet_id);
|
|---|
| 260 | if(! packet_is_valid(packet)){
|
|---|
| 261 | return ENOENT;
|
|---|
| 262 | }
|
|---|
| 263 | fibril_mutex_lock(&ps_globals.lock);
|
|---|
| 264 | pq_destroy(packet, packet_release);
|
|---|
| 265 | fibril_mutex_unlock(&ps_globals.lock);
|
|---|
| 266 | return EOK;
|
|---|
| 267 | }
|
|---|
| 268 |
|
|---|
| 269 | void pq_release_local(int phone, packet_id_t packet_id)
|
|---|
| 270 | {
|
|---|
| 271 | (void) packet_release_wrapper(packet_id);
|
|---|
| 272 | }
|
|---|
| 273 |
|
|---|
| 274 | /** Shares the packet memory block.
|
|---|
| 275 | * @param[in] packet The packet to be shared.
|
|---|
| 276 | * @returns EOK on success.
|
|---|
| 277 | * @returns EINVAL if the packet is not valid.
|
|---|
| 278 | * @returns EINVAL if the calling module does not accept the memory.
|
|---|
| 279 | * @returns ENOMEM if the desired and actual sizes differ.
|
|---|
| 280 | * @returns Other error codes as defined for the async_share_in_finalize() function.
|
|---|
| 281 | */
|
|---|
| 282 | static int packet_reply(const packet_t packet){
|
|---|
| 283 | ipc_callid_t callid;
|
|---|
| 284 | size_t size;
|
|---|
| 285 |
|
|---|
| 286 | if(! packet_is_valid(packet)){
|
|---|
| 287 | return EINVAL;
|
|---|
| 288 | }
|
|---|
| 289 | if(async_share_in_receive(&callid, &size) <= 0) return EINVAL;
|
|---|
| 290 | if(size != packet->length){
|
|---|
| 291 | return ENOMEM;
|
|---|
| 292 | }
|
|---|
| 293 | return async_share_in_finalize(callid, packet, PROTO_READ | PROTO_WRITE);
|
|---|
| 294 | }
|
|---|
| 295 |
|
|---|
| 296 | int packet_server_message(ipc_callid_t callid, ipc_call_t * call, ipc_call_t * answer, int * answer_count){
|
|---|
| 297 | packet_t packet;
|
|---|
| 298 |
|
|---|
| 299 | *answer_count = 0;
|
|---|
| 300 | switch(IPC_GET_METHOD(*call)){
|
|---|
| 301 | case IPC_M_PHONE_HUNGUP:
|
|---|
| 302 | return EOK;
|
|---|
| 303 | case NET_PACKET_CREATE_1:
|
|---|
| 304 | packet = packet_get_local(DEFAULT_ADDR_LEN, DEFAULT_PREFIX, IPC_GET_CONTENT(call), DEFAULT_SUFFIX);
|
|---|
| 305 | if(! packet){
|
|---|
| 306 | return ENOMEM;
|
|---|
| 307 | }
|
|---|
| 308 | *answer_count = 2;
|
|---|
| 309 | IPC_SET_ARG1(*answer, (ipcarg_t) packet->packet_id);
|
|---|
| 310 | IPC_SET_ARG2(*answer, (ipcarg_t) packet->length);
|
|---|
| 311 | return EOK;
|
|---|
| 312 | case NET_PACKET_CREATE_4:
|
|---|
| 313 | packet = packet_get_local(((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));
|
|---|
| 314 | if(! packet){
|
|---|
| 315 | return ENOMEM;
|
|---|
| 316 | }
|
|---|
| 317 | *answer_count = 2;
|
|---|
| 318 | IPC_SET_ARG1(*answer, (ipcarg_t) packet->packet_id);
|
|---|
| 319 | IPC_SET_ARG2(*answer, (ipcarg_t) packet->length);
|
|---|
| 320 | return EOK;
|
|---|
| 321 | case NET_PACKET_GET:
|
|---|
| 322 | packet = pm_find(IPC_GET_ID(call));
|
|---|
| 323 | if(! packet_is_valid(packet)){
|
|---|
| 324 | return ENOENT;
|
|---|
| 325 | }
|
|---|
| 326 | return packet_reply(packet);
|
|---|
| 327 | case NET_PACKET_GET_SIZE:
|
|---|
| 328 | packet = pm_find(IPC_GET_ID(call));
|
|---|
| 329 | if(! packet_is_valid(packet)){
|
|---|
| 330 | return ENOENT;
|
|---|
| 331 | }
|
|---|
| 332 | IPC_SET_ARG1(*answer, (ipcarg_t) packet->length);
|
|---|
| 333 | *answer_count = 1;
|
|---|
| 334 | return EOK;
|
|---|
| 335 | case NET_PACKET_RELEASE:
|
|---|
| 336 | return packet_release_wrapper(IPC_GET_ID(call));
|
|---|
| 337 | }
|
|---|
| 338 | return ENOTSUP;
|
|---|
| 339 | }
|
|---|
| 340 |
|
|---|
| 341 | /** @}
|
|---|
| 342 | */
|
|---|