| 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 libpacket
|
|---|
| 30 | * @{
|
|---|
| 31 | */
|
|---|
| 32 |
|
|---|
| 33 | /** @file
|
|---|
| 34 | * Packet server implementation.
|
|---|
| 35 | */
|
|---|
| 36 |
|
|---|
| 37 | #include <packet_server.h>
|
|---|
| 38 | #include <packet_local.h>
|
|---|
| 39 |
|
|---|
| 40 | #include <align.h>
|
|---|
| 41 | #include <assert.h>
|
|---|
| 42 | #include <async.h>
|
|---|
| 43 | #include <errno.h>
|
|---|
| 44 | #include <fibril_synch.h>
|
|---|
| 45 | #include <unistd.h>
|
|---|
| 46 | #include <sys/mman.h>
|
|---|
| 47 |
|
|---|
| 48 | #include <ipc/ipc.h>
|
|---|
| 49 | #include <ipc/packet.h>
|
|---|
| 50 | #include <ipc/net.h>
|
|---|
| 51 |
|
|---|
| 52 | #include <net/packet.h>
|
|---|
| 53 | #include <net/packet_header.h>
|
|---|
| 54 |
|
|---|
| 55 | #define FREE_QUEUES_COUNT 7
|
|---|
| 56 |
|
|---|
| 57 | /** The default address length reserved for new packets. */
|
|---|
| 58 | #define DEFAULT_ADDR_LEN 32
|
|---|
| 59 |
|
|---|
| 60 | /** The default prefix reserved for new packets. */
|
|---|
| 61 | #define DEFAULT_PREFIX 64
|
|---|
| 62 |
|
|---|
| 63 | /** The default suffix reserved for new packets. */
|
|---|
| 64 | #define DEFAULT_SUFFIX 64
|
|---|
| 65 |
|
|---|
| 66 | /** Packet server global data. */
|
|---|
| 67 | static struct {
|
|---|
| 68 | /** Safety lock. */
|
|---|
| 69 | fibril_mutex_t lock;
|
|---|
| 70 | /** Free packet queues. */
|
|---|
| 71 | packet_t free[FREE_QUEUES_COUNT];
|
|---|
| 72 |
|
|---|
| 73 | /**
|
|---|
| 74 | * Packet length upper bounds of the free packet queues. The maximal
|
|---|
| 75 | * lengths of packets in each queue in the ascending order. The last
|
|---|
| 76 | * queue is not limited.
|
|---|
| 77 | */
|
|---|
| 78 | size_t sizes[FREE_QUEUES_COUNT];
|
|---|
| 79 |
|
|---|
| 80 | /** Total packets allocated. */
|
|---|
| 81 | unsigned int count;
|
|---|
| 82 | } ps_globals = {
|
|---|
| 83 | .lock = FIBRIL_MUTEX_INITIALIZER(ps_globals.lock),
|
|---|
| 84 | .free = {
|
|---|
| 85 | NULL,
|
|---|
| 86 | NULL,
|
|---|
| 87 | NULL,
|
|---|
| 88 | NULL,
|
|---|
| 89 | NULL,
|
|---|
| 90 | NULL,
|
|---|
| 91 | NULL
|
|---|
| 92 | },
|
|---|
| 93 | .sizes = {
|
|---|
| 94 | PAGE_SIZE,
|
|---|
| 95 | PAGE_SIZE * 2,
|
|---|
| 96 | PAGE_SIZE * 4,
|
|---|
| 97 | PAGE_SIZE * 8,
|
|---|
| 98 | PAGE_SIZE * 16,
|
|---|
| 99 | PAGE_SIZE * 32,
|
|---|
| 100 | PAGE_SIZE * 64
|
|---|
| 101 | },
|
|---|
| 102 | .count = 0
|
|---|
| 103 | };
|
|---|
| 104 |
|
|---|
| 105 | int packet_translate_local(int phone, packet_t *packet, packet_id_t packet_id)
|
|---|
| 106 | {
|
|---|
| 107 | if (!packet)
|
|---|
| 108 | return EINVAL;
|
|---|
| 109 |
|
|---|
| 110 | *packet = pm_find(packet_id);
|
|---|
| 111 | return (*packet) ? EOK : ENOENT;
|
|---|
| 112 | }
|
|---|
| 113 |
|
|---|
| 114 | /** Clears and initializes the packet according to the given dimensions.
|
|---|
| 115 | *
|
|---|
| 116 | * @param[in] packet The packet to be initialized.
|
|---|
| 117 | * @param[in] addr_len The source and destination addresses maximal length in
|
|---|
| 118 | * bytes.
|
|---|
| 119 | * @param[in] max_prefix The maximal prefix length in bytes.
|
|---|
| 120 | * @param[in] max_content The maximal content length in bytes.
|
|---|
| 121 | * @param[in] max_suffix The maximal suffix length in bytes.
|
|---|
| 122 | */
|
|---|
| 123 | static void
|
|---|
| 124 | packet_init(packet_t packet, size_t addr_len, size_t max_prefix,
|
|---|
| 125 | size_t max_content, size_t max_suffix)
|
|---|
| 126 | {
|
|---|
| 127 | // clear the packet content
|
|---|
| 128 | bzero(((void *) packet) + sizeof(struct packet),
|
|---|
| 129 | packet->length - sizeof(struct packet));
|
|---|
| 130 |
|
|---|
| 131 | // clear the packet header
|
|---|
| 132 | packet->order = 0;
|
|---|
| 133 | packet->metric = 0;
|
|---|
| 134 | packet->previous = 0;
|
|---|
| 135 | packet->next = 0;
|
|---|
| 136 | packet->addr_len = 0;
|
|---|
| 137 | packet->src_addr = sizeof(struct packet);
|
|---|
| 138 | packet->dest_addr = packet->src_addr + addr_len;
|
|---|
| 139 | packet->max_prefix = max_prefix;
|
|---|
| 140 | packet->max_content = max_content;
|
|---|
| 141 | packet->data_start = packet->dest_addr + addr_len + packet->max_prefix;
|
|---|
| 142 | packet->data_end = packet->data_start;
|
|---|
| 143 | }
|
|---|
| 144 |
|
|---|
| 145 | /** Creates a new packet of dimensions at least as given.
|
|---|
| 146 | *
|
|---|
| 147 | * Should be used only when the global data are locked.
|
|---|
| 148 | *
|
|---|
| 149 | * @param[in] length The total length of the packet, including the header,
|
|---|
| 150 | * the addresses and the data of the packet.
|
|---|
| 151 | * @param[in] addr_len The source and destination addresses maximal length in
|
|---|
| 152 | * bytes.
|
|---|
| 153 | * @param[in] max_prefix The maximal prefix length in bytes.
|
|---|
| 154 | * @param[in] max_content The maximal content length in bytes.
|
|---|
| 155 | * @param[in] max_suffix The maximal suffix length in bytes.
|
|---|
| 156 | * @return The packet of dimensions at least as given.
|
|---|
| 157 | * @return NULL if there is not enough memory left.
|
|---|
| 158 | */
|
|---|
| 159 | static packet_t
|
|---|
| 160 | packet_create(size_t length, size_t addr_len, size_t max_prefix,
|
|---|
| 161 | size_t max_content, size_t max_suffix)
|
|---|
| 162 | {
|
|---|
| 163 | packet_t packet;
|
|---|
| 164 | int rc;
|
|---|
| 165 |
|
|---|
| 166 | // already locked
|
|---|
| 167 | packet = (packet_t) mmap(NULL, length, PROTO_READ | PROTO_WRITE,
|
|---|
| 168 | MAP_SHARED | MAP_ANONYMOUS, 0, 0);
|
|---|
| 169 | if (packet == MAP_FAILED)
|
|---|
| 170 | return NULL;
|
|---|
| 171 |
|
|---|
| 172 | ps_globals.count++;
|
|---|
| 173 | packet->packet_id = ps_globals.count;
|
|---|
| 174 | packet->length = length;
|
|---|
| 175 | packet_init(packet, addr_len, max_prefix, max_content, max_suffix);
|
|---|
| 176 | packet->magic_value = PACKET_MAGIC_VALUE;
|
|---|
| 177 | rc = pm_add(packet);
|
|---|
| 178 | if (rc != EOK) {
|
|---|
| 179 | munmap(packet, packet->length);
|
|---|
| 180 | return NULL;
|
|---|
| 181 | }
|
|---|
| 182 |
|
|---|
| 183 | return packet;
|
|---|
| 184 | }
|
|---|
| 185 |
|
|---|
| 186 | /** Return the packet of dimensions at least as given.
|
|---|
| 187 | *
|
|---|
| 188 | * Try to reuse free packets first.
|
|---|
| 189 | * Create a new packet aligned to the memory page size if none available.
|
|---|
| 190 | * Lock the global data during its processing.
|
|---|
| 191 | *
|
|---|
| 192 | * @param[in] addr_len The source and destination addresses maximal length in
|
|---|
| 193 | * bytes.
|
|---|
| 194 | * @param[in] max_prefix The maximal prefix length in bytes.
|
|---|
| 195 | * @param[in] max_content The maximal content length in bytes.
|
|---|
| 196 | * @param[in] max_suffix The maximal suffix length in bytes.
|
|---|
| 197 | * @return The packet of dimensions at least as given.
|
|---|
| 198 | * @return NULL if there is not enough memory left.
|
|---|
| 199 | */
|
|---|
| 200 | static packet_t
|
|---|
| 201 | packet_get_local(size_t addr_len, size_t max_prefix, size_t max_content,
|
|---|
| 202 | size_t max_suffix)
|
|---|
| 203 | {
|
|---|
| 204 | size_t length = ALIGN_UP(sizeof(struct packet) + 2 * addr_len +
|
|---|
| 205 | max_prefix + max_content + max_suffix, PAGE_SIZE);
|
|---|
| 206 |
|
|---|
| 207 | fibril_mutex_lock(&ps_globals.lock);
|
|---|
| 208 |
|
|---|
| 209 | packet_t packet;
|
|---|
| 210 | unsigned int index;
|
|---|
| 211 |
|
|---|
| 212 | for (index = 0; index < FREE_QUEUES_COUNT; index++) {
|
|---|
| 213 | if ((length > ps_globals.sizes[index]) &&
|
|---|
| 214 | (index < FREE_QUEUES_COUNT - 1))
|
|---|
| 215 | continue;
|
|---|
| 216 |
|
|---|
| 217 | packet = ps_globals.free[index];
|
|---|
| 218 | while (packet_is_valid(packet) && (packet->length < length))
|
|---|
| 219 | packet = pm_find(packet->next);
|
|---|
| 220 |
|
|---|
| 221 | if (packet_is_valid(packet)) {
|
|---|
| 222 | if (packet == ps_globals.free[index])
|
|---|
| 223 | ps_globals.free[index] = pq_detach(packet);
|
|---|
| 224 | else
|
|---|
| 225 | pq_detach(packet);
|
|---|
| 226 |
|
|---|
| 227 | packet_init(packet, addr_len, max_prefix, max_content,
|
|---|
| 228 | max_suffix);
|
|---|
| 229 | fibril_mutex_unlock(&ps_globals.lock);
|
|---|
| 230 |
|
|---|
| 231 | return packet;
|
|---|
| 232 | }
|
|---|
| 233 | }
|
|---|
| 234 |
|
|---|
| 235 | packet = packet_create(length, addr_len, max_prefix, max_content,
|
|---|
| 236 | max_suffix);
|
|---|
| 237 |
|
|---|
| 238 | fibril_mutex_unlock(&ps_globals.lock);
|
|---|
| 239 |
|
|---|
| 240 | return packet;
|
|---|
| 241 | }
|
|---|
| 242 |
|
|---|
| 243 | packet_t packet_get_4_local(int phone, size_t max_content, size_t addr_len,
|
|---|
| 244 | size_t max_prefix, size_t max_suffix)
|
|---|
| 245 | {
|
|---|
| 246 | return packet_get_local(addr_len, max_prefix, max_content, max_suffix);
|
|---|
| 247 | }
|
|---|
| 248 |
|
|---|
| 249 | packet_t packet_get_1_local(int phone, size_t content)
|
|---|
| 250 | {
|
|---|
| 251 | return packet_get_local(DEFAULT_ADDR_LEN, DEFAULT_PREFIX, content,
|
|---|
| 252 | DEFAULT_SUFFIX);
|
|---|
| 253 | }
|
|---|
| 254 |
|
|---|
| 255 | /** Release the packet and returns it to the appropriate free packet queue.
|
|---|
| 256 | *
|
|---|
| 257 | * Should be used only when the global data are locked.
|
|---|
| 258 | *
|
|---|
| 259 | * @param[in] packet The packet to be released.
|
|---|
| 260 | *
|
|---|
| 261 | */
|
|---|
| 262 | static void packet_release(packet_t packet)
|
|---|
| 263 | {
|
|---|
| 264 | int index;
|
|---|
| 265 | int result;
|
|---|
| 266 |
|
|---|
| 267 | for (index = 0; (index < FREE_QUEUES_COUNT - 1) &&
|
|---|
| 268 | (packet->length > ps_globals.sizes[index]); index++) {
|
|---|
| 269 | ;
|
|---|
| 270 | }
|
|---|
| 271 |
|
|---|
| 272 | result = pq_add(&ps_globals.free[index], packet, packet->length,
|
|---|
| 273 | packet->length);
|
|---|
| 274 | assert(result == EOK);
|
|---|
| 275 | }
|
|---|
| 276 |
|
|---|
| 277 | /** Releases the packet queue.
|
|---|
| 278 | *
|
|---|
| 279 | * @param[in] packet_id The first packet identifier.
|
|---|
| 280 | * @return EOK on success.
|
|---|
| 281 | * @return ENOENT if there is no such packet.
|
|---|
| 282 | */
|
|---|
| 283 | static int packet_release_wrapper(packet_id_t packet_id)
|
|---|
| 284 | {
|
|---|
| 285 | packet_t packet;
|
|---|
| 286 |
|
|---|
| 287 | packet = pm_find(packet_id);
|
|---|
| 288 | if (!packet_is_valid(packet))
|
|---|
| 289 | return ENOENT;
|
|---|
| 290 |
|
|---|
| 291 | fibril_mutex_lock(&ps_globals.lock);
|
|---|
| 292 | pq_destroy(packet, packet_release);
|
|---|
| 293 | fibril_mutex_unlock(&ps_globals.lock);
|
|---|
| 294 |
|
|---|
| 295 | return EOK;
|
|---|
| 296 | }
|
|---|
| 297 |
|
|---|
| 298 | void pq_release_local(int phone, packet_id_t packet_id)
|
|---|
| 299 | {
|
|---|
| 300 | (void) packet_release_wrapper(packet_id);
|
|---|
| 301 | }
|
|---|
| 302 |
|
|---|
| 303 | /** Shares the packet memory block.
|
|---|
| 304 | * @param[in] packet The packet to be shared.
|
|---|
| 305 | * @return EOK on success.
|
|---|
| 306 | * @return EINVAL if the packet is not valid.
|
|---|
| 307 | * @return EINVAL if the calling module does not accept the memory.
|
|---|
| 308 | * @return ENOMEM if the desired and actual sizes differ.
|
|---|
| 309 | * @return Other error codes as defined for the
|
|---|
| 310 | * async_share_in_finalize() function.
|
|---|
| 311 | */
|
|---|
| 312 | static int packet_reply(const packet_t packet)
|
|---|
| 313 | {
|
|---|
| 314 | ipc_callid_t callid;
|
|---|
| 315 | size_t size;
|
|---|
| 316 |
|
|---|
| 317 | if (!packet_is_valid(packet))
|
|---|
| 318 | return EINVAL;
|
|---|
| 319 |
|
|---|
| 320 | if (!async_share_in_receive(&callid, &size)) {
|
|---|
| 321 | ipc_answer_0(callid, EINVAL);
|
|---|
| 322 | return EINVAL;
|
|---|
| 323 | }
|
|---|
| 324 |
|
|---|
| 325 | if (size != packet->length) {
|
|---|
| 326 | ipc_answer_0(callid, ENOMEM);
|
|---|
| 327 | return ENOMEM;
|
|---|
| 328 | }
|
|---|
| 329 |
|
|---|
| 330 | return async_share_in_finalize(callid, packet,
|
|---|
| 331 | PROTO_READ | PROTO_WRITE);
|
|---|
| 332 | }
|
|---|
| 333 |
|
|---|
| 334 | /** Processes the packet server message.
|
|---|
| 335 | *
|
|---|
| 336 | * @param[in] callid The message identifier.
|
|---|
| 337 | * @param[in] call The message parameters.
|
|---|
| 338 | * @param[out] answer The message answer parameters.
|
|---|
| 339 | * @param[out] answer_count The last parameter for the actual answer in the
|
|---|
| 340 | * answer parameter.
|
|---|
| 341 | * @return EOK on success.
|
|---|
| 342 | * @return ENOMEM if there is not enough memory left.
|
|---|
| 343 | * @return ENOENT if there is no such packet as in the packet
|
|---|
| 344 | * message parameter.
|
|---|
| 345 | * @return ENOTSUP if the message is not known.
|
|---|
| 346 | * @return Other error codes as defined for the
|
|---|
| 347 | * packet_release_wrapper() function.
|
|---|
| 348 | */
|
|---|
| 349 | int
|
|---|
| 350 | packet_server_message(ipc_callid_t callid, ipc_call_t *call, ipc_call_t *answer,
|
|---|
| 351 | int *answer_count)
|
|---|
| 352 | {
|
|---|
| 353 | packet_t packet;
|
|---|
| 354 |
|
|---|
| 355 | *answer_count = 0;
|
|---|
| 356 | switch (IPC_GET_METHOD(*call)) {
|
|---|
| 357 | case IPC_M_PHONE_HUNGUP:
|
|---|
| 358 | return EOK;
|
|---|
| 359 |
|
|---|
| 360 | case NET_PACKET_CREATE_1:
|
|---|
| 361 | packet = packet_get_local(DEFAULT_ADDR_LEN, DEFAULT_PREFIX,
|
|---|
| 362 | IPC_GET_CONTENT(call), DEFAULT_SUFFIX);
|
|---|
| 363 | if (!packet)
|
|---|
| 364 | return ENOMEM;
|
|---|
| 365 | *answer_count = 2;
|
|---|
| 366 | IPC_SET_ARG1(*answer, (ipcarg_t) packet->packet_id);
|
|---|
| 367 | IPC_SET_ARG2(*answer, (ipcarg_t) packet->length);
|
|---|
| 368 | return EOK;
|
|---|
| 369 |
|
|---|
| 370 | case NET_PACKET_CREATE_4:
|
|---|
| 371 | packet = packet_get_local(
|
|---|
| 372 | ((DEFAULT_ADDR_LEN < IPC_GET_ADDR_LEN(call)) ?
|
|---|
| 373 | IPC_GET_ADDR_LEN(call) : DEFAULT_ADDR_LEN),
|
|---|
| 374 | DEFAULT_PREFIX + IPC_GET_PREFIX(call),
|
|---|
| 375 | IPC_GET_CONTENT(call),
|
|---|
| 376 | DEFAULT_SUFFIX + IPC_GET_SUFFIX(call));
|
|---|
| 377 | if (!packet)
|
|---|
| 378 | return ENOMEM;
|
|---|
| 379 | *answer_count = 2;
|
|---|
| 380 | IPC_SET_ARG1(*answer, (ipcarg_t) packet->packet_id);
|
|---|
| 381 | IPC_SET_ARG2(*answer, (ipcarg_t) packet->length);
|
|---|
| 382 | return EOK;
|
|---|
| 383 |
|
|---|
| 384 | case NET_PACKET_GET:
|
|---|
| 385 | packet = pm_find(IPC_GET_ID(call));
|
|---|
| 386 | if (!packet_is_valid(packet))
|
|---|
| 387 | return ENOENT;
|
|---|
| 388 | return packet_reply(packet);
|
|---|
| 389 |
|
|---|
| 390 | case NET_PACKET_GET_SIZE:
|
|---|
| 391 | packet = pm_find(IPC_GET_ID(call));
|
|---|
| 392 | if (!packet_is_valid(packet))
|
|---|
| 393 | return ENOENT;
|
|---|
| 394 | IPC_SET_ARG1(*answer, (ipcarg_t) packet->length);
|
|---|
| 395 | *answer_count = 1;
|
|---|
| 396 | return EOK;
|
|---|
| 397 |
|
|---|
| 398 | case NET_PACKET_RELEASE:
|
|---|
| 399 | return packet_release_wrapper(IPC_GET_ID(call));
|
|---|
| 400 | }
|
|---|
| 401 |
|
|---|
| 402 | return ENOTSUP;
|
|---|
| 403 | }
|
|---|
| 404 |
|
|---|
| 405 | /** @}
|
|---|
| 406 | */
|
|---|