[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>
|
---|
[c5b59ce] | 41 | #include <err.h>
|
---|
[21580dd] | 42 | #include <fibril_synch.h>
|
---|
| 43 | #include <unistd.h>
|
---|
[d52fbaf] | 44 | #include <sys/mman.h>
|
---|
[21580dd] | 45 |
|
---|
| 46 | #include <ipc/ipc.h>
|
---|
[d52fbaf] | 47 | #include <ipc/packet.h>
|
---|
[c69d327] | 48 | #include <net/packet.h>
|
---|
| 49 | #include <net/packet_header.h>
|
---|
| 50 | #include <packet/packet_server.h>
|
---|
[21580dd] | 51 |
|
---|
[849ed54] | 52 | #include <net_messages.h>
|
---|
[0a866eeb] | 53 | #include <packet/packet_local.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 |
|
---|
[14f1db0] | 99 | int packet_translate_local(int phone, packet_ref packet, packet_id_t packet_id)
|
---|
| 100 | {
|
---|
| 101 | if (!packet)
|
---|
[849ed54] | 102 | return EINVAL;
|
---|
[14f1db0] | 103 |
|
---|
[849ed54] | 104 | *packet = pm_find(packet_id);
|
---|
| 105 | return (*packet) ? EOK : ENOENT;
|
---|
| 106 | }
|
---|
[21580dd] | 107 |
|
---|
[849ed54] | 108 | /** Clears and initializes the packet according to the given dimensions.
|
---|
| 109 | * @param[in] packet The packet to be initialized.
|
---|
[21580dd] | 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 | */
|
---|
[849ed54] | 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 | }
|
---|
[21580dd] | 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 | */
|
---|
[849ed54] | 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;
|
---|
[21580dd] | 144 |
|
---|
[849ed54] | 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 |
|
---|
[14f1db0] | 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 | *
|
---|
[21580dd] | 179 | */
|
---|
[14f1db0] | 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 |
|
---|
[849ed54] | 186 | fibril_mutex_lock(&ps_globals.lock);
|
---|
[14f1db0] | 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]) {
|
---|
[849ed54] | 193 | packet = ps_globals.free[index];
|
---|
[14f1db0] | 194 |
|
---|
| 195 | while (packet_is_valid(packet) && (packet->length < length))
|
---|
[849ed54] | 196 | packet = pm_find(packet->next);
|
---|
[14f1db0] | 197 |
|
---|
| 198 | if (packet_is_valid(packet)) {
|
---|
| 199 | if (packet == ps_globals.free[index])
|
---|
[849ed54] | 200 | ps_globals.free[index] = pq_detach(packet);
|
---|
[14f1db0] | 201 | else
|
---|
[849ed54] | 202 | pq_detach(packet);
|
---|
[14f1db0] | 203 |
|
---|
| 204 | packet_init(packet, addr_len, max_prefix, max_content,
|
---|
| 205 | max_suffix);
|
---|
[849ed54] | 206 | fibril_mutex_unlock(&ps_globals.lock);
|
---|
[14f1db0] | 207 |
|
---|
[849ed54] | 208 | return packet;
|
---|
| 209 | }
|
---|
| 210 | }
|
---|
[aadf01e] | 211 | }
|
---|
[14f1db0] | 212 |
|
---|
| 213 | packet = packet_create(length, addr_len, max_prefix, max_content,
|
---|
| 214 | max_suffix);
|
---|
| 215 |
|
---|
[849ed54] | 216 | fibril_mutex_unlock(&ps_globals.lock);
|
---|
[14f1db0] | 217 |
|
---|
[849ed54] | 218 | return packet;
|
---|
[21580dd] | 219 | }
|
---|
| 220 |
|
---|
[14f1db0] | 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);
|
---|
[21580dd] | 225 | }
|
---|
| 226 |
|
---|
[14f1db0] | 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);
|
---|
[21580dd] | 231 | }
|
---|
| 232 |
|
---|
[14f1db0] | 233 | /** Release the packet and returns it to the appropriate free packet queue.
|
---|
| 234 | *
|
---|
[849ed54] | 235 | * Should be used only when the global data are locked.
|
---|
[14f1db0] | 236 | *
|
---|
[849ed54] | 237 | * @param[in] packet The packet to be released.
|
---|
[14f1db0] | 238 | *
|
---|
[849ed54] | 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 |
|
---|
[14f1db0] | 269 | void pq_release_local(int phone, packet_id_t packet_id)
|
---|
| 270 | {
|
---|
[aadf01e] | 271 | (void) packet_release_wrapper(packet_id);
|
---|
[21580dd] | 272 | }
|
---|
| 273 |
|
---|
[849ed54] | 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 |
|
---|
[aadf01e] | 296 | int packet_server_message(ipc_callid_t callid, ipc_call_t * call, ipc_call_t * answer, int * answer_count){
|
---|
[21580dd] | 297 | packet_t packet;
|
---|
| 298 |
|
---|
[aadf01e] | 299 | *answer_count = 0;
|
---|
| 300 | switch(IPC_GET_METHOD(*call)){
|
---|
[21580dd] | 301 | case IPC_M_PHONE_HUNGUP:
|
---|
| 302 | return EOK;
|
---|
| 303 | case NET_PACKET_CREATE_1:
|
---|
[14f1db0] | 304 | packet = packet_get_local(DEFAULT_ADDR_LEN, DEFAULT_PREFIX, IPC_GET_CONTENT(call), DEFAULT_SUFFIX);
|
---|
[aadf01e] | 305 | if(! packet){
|
---|
| 306 | return ENOMEM;
|
---|
| 307 | }
|
---|
| 308 | *answer_count = 2;
|
---|
[3db8889] | 309 | IPC_SET_ARG1(*answer, (ipcarg_t) packet->packet_id);
|
---|
| 310 | IPC_SET_ARG2(*answer, (ipcarg_t) packet->length);
|
---|
[21580dd] | 311 | return EOK;
|
---|
| 312 | case NET_PACKET_CREATE_4:
|
---|
[14f1db0] | 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));
|
---|
[aadf01e] | 314 | if(! packet){
|
---|
| 315 | return ENOMEM;
|
---|
| 316 | }
|
---|
| 317 | *answer_count = 2;
|
---|
[3db8889] | 318 | IPC_SET_ARG1(*answer, (ipcarg_t) packet->packet_id);
|
---|
| 319 | IPC_SET_ARG2(*answer, (ipcarg_t) packet->length);
|
---|
[21580dd] | 320 | return EOK;
|
---|
| 321 | case NET_PACKET_GET:
|
---|
[aadf01e] | 322 | packet = pm_find(IPC_GET_ID(call));
|
---|
| 323 | if(! packet_is_valid(packet)){
|
---|
| 324 | return ENOENT;
|
---|
| 325 | }
|
---|
| 326 | return packet_reply(packet);
|
---|
[21580dd] | 327 | case NET_PACKET_GET_SIZE:
|
---|
[aadf01e] | 328 | packet = pm_find(IPC_GET_ID(call));
|
---|
| 329 | if(! packet_is_valid(packet)){
|
---|
| 330 | return ENOENT;
|
---|
| 331 | }
|
---|
[3db8889] | 332 | IPC_SET_ARG1(*answer, (ipcarg_t) packet->length);
|
---|
[aadf01e] | 333 | *answer_count = 1;
|
---|
[21580dd] | 334 | return EOK;
|
---|
| 335 | case NET_PACKET_RELEASE:
|
---|
[aadf01e] | 336 | return packet_release_wrapper(IPC_GET_ID(call));
|
---|
[21580dd] | 337 | }
|
---|
| 338 | return ENOTSUP;
|
---|
| 339 | }
|
---|
| 340 |
|
---|
| 341 | /** @}
|
---|
| 342 | */
|
---|