Ignore:
File:
1 edited

Legend:

Unmodified
Added
Removed
  • uspace/lib/packet/generic/packet_server.c

    r28a3e74 ra649e73f  
    3636
    3737#include <packet_server.h>
     38#include <packet_local.h>
    3839
    3940#include <align.h>
     
    4142#include <async.h>
    4243#include <errno.h>
     44#include <err.h>
    4345#include <fibril_synch.h>
    4446#include <unistd.h>
    4547#include <sys/mman.h>
     48
     49#include <ipc/ipc.h>
    4650#include <ipc/packet.h>
    4751#include <ipc/net.h>
     52
    4853#include <net/packet.h>
    4954#include <net/packet_header.h>
     
    6570        fibril_mutex_t lock;
    6671        /** Free packet queues. */
    67         packet_t *free[FREE_QUEUES_COUNT];
     72        packet_t free[FREE_QUEUES_COUNT];
    6873       
    6974        /**
     
    99104};
    100105
     106int packet_translate_local(int phone, packet_ref packet, packet_id_t packet_id)
     107{
     108        if (!packet)
     109                return EINVAL;
     110       
     111        *packet = pm_find(packet_id);
     112        return (*packet) ? EOK : ENOENT;
     113}
     114
    101115/** Clears and initializes the packet according to the given dimensions.
    102116 *
     
    109123 */
    110124static void
    111 packet_init(packet_t *packet, size_t addr_len, size_t max_prefix,
     125packet_init(packet_t packet, size_t addr_len, size_t max_prefix,
    112126    size_t max_content, size_t max_suffix)
    113127{
    114         /* Clear the packet content */
    115         bzero(((void *) packet) + sizeof(packet_t),
    116             packet->length - sizeof(packet_t));
    117        
    118         /* Clear the packet header */
     128        // clear the packet content
     129        bzero(((void *) packet) + sizeof(struct packet),
     130            packet->length - sizeof(struct packet));
     131       
     132        // clear the packet header
    119133        packet->order = 0;
    120134        packet->metric = 0;
     
    122136        packet->next = 0;
    123137        packet->addr_len = 0;
    124         packet->src_addr = sizeof(packet_t);
     138        packet->src_addr = sizeof(struct packet);
    125139        packet->dest_addr = packet->src_addr + addr_len;
    126140        packet->max_prefix = max_prefix;
     
    131145
    132146/** Creates a new packet of dimensions at least as given.
     147 *
     148 * Should be used only when the global data are locked.
    133149 *
    134150 * @param[in] length    The total length of the packet, including the header,
     
    139155 * @param[in] max_content The maximal content length in bytes.
    140156 * @param[in] max_suffix The maximal suffix length in bytes.
    141  * @return              The packet of dimensions at least as given.
    142  * @return              NULL if there is not enough memory left.
    143  */
    144 static packet_t *
     157 * @returns             The packet of dimensions at least as given.
     158 * @returns             NULL if there is not enough memory left.
     159 */
     160static packet_t
    145161packet_create(size_t length, size_t addr_len, size_t max_prefix,
    146162    size_t max_content, size_t max_suffix)
    147163{
    148         packet_t *packet;
    149         int rc;
    150 
    151         assert(fibril_mutex_is_locked(&ps_globals.lock));
    152 
    153         /* Already locked */
    154         packet = (packet_t *) mmap(NULL, length, PROTO_READ | PROTO_WRITE,
     164        ERROR_DECLARE;
     165
     166        packet_t packet;
     167
     168        // already locked
     169        packet = (packet_t) mmap(NULL, length, PROTO_READ | PROTO_WRITE,
    155170            MAP_SHARED | MAP_ANONYMOUS, 0, 0);
    156171        if (packet == MAP_FAILED)
     
    162177        packet_init(packet, addr_len, max_prefix, max_content, max_suffix);
    163178        packet->magic_value = PACKET_MAGIC_VALUE;
    164         rc = pm_add(packet);
    165         if (rc != EOK) {
     179        if (ERROR_OCCURRED(pm_add(packet))) {
    166180                munmap(packet, packet->length);
    167181                return NULL;
     
    185199 * @return              NULL if there is not enough memory left.
    186200 */
    187 static packet_t *
     201static packet_t
    188202packet_get_local(size_t addr_len, size_t max_prefix, size_t max_content,
    189203    size_t max_suffix)
    190204{
    191         size_t length = ALIGN_UP(sizeof(packet_t) + 2 * addr_len +
     205        size_t length = ALIGN_UP(sizeof(struct packet) + 2 * addr_len +
    192206            max_prefix + max_content + max_suffix, PAGE_SIZE);
    193207       
    194208        fibril_mutex_lock(&ps_globals.lock);
    195209       
    196         packet_t *packet;
     210        packet_t packet;
    197211        unsigned int index;
    198212       
    199213        for (index = 0; index < FREE_QUEUES_COUNT; index++) {
    200                 if ((length > ps_globals.sizes[index]) &&
    201                     (index < FREE_QUEUES_COUNT - 1))
     214                if (length > ps_globals.sizes[index])
    202215                        continue;
    203216               
     
    228241}
    229242
     243packet_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
     249packet_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
    230255/** Release the packet and returns it to the appropriate free packet queue.
    231256 *
     257 * Should be used only when the global data are locked.
     258 *
    232259 * @param[in] packet    The packet to be released.
    233260 *
    234261 */
    235 static void packet_release(packet_t *packet)
     262static void packet_release(packet_t packet)
    236263{
    237264        int index;
    238265        int result;
    239 
    240         assert(fibril_mutex_is_locked(&ps_globals.lock));
    241266
    242267        for (index = 0; (index < FREE_QUEUES_COUNT - 1) &&
     
    253278 *
    254279 * @param[in] packet_id The first packet identifier.
    255  * @return              EOK on success.
    256  * @return              ENOENT if there is no such packet.
     280 * @returns             EOK on success.
     281 * @returns             ENOENT if there is no such packet.
    257282 */
    258283static int packet_release_wrapper(packet_id_t packet_id)
    259284{
    260         packet_t *packet;
     285        packet_t packet;
    261286
    262287        packet = pm_find(packet_id);
     
    271296}
    272297
     298void pq_release_local(int phone, packet_id_t packet_id)
     299{
     300        (void) packet_release_wrapper(packet_id);
     301}
     302
    273303/** Shares the packet memory block.
    274304 * @param[in] packet    The packet to be shared.
    275  * @return              EOK on success.
    276  * @return              EINVAL if the packet is not valid.
    277  * @return              EINVAL if the calling module does not accept the memory.
    278  * @return              ENOMEM if the desired and actual sizes differ.
    279  * @return              Other error codes as defined for the
     305 * @returns             EOK on success.
     306 * @returns             EINVAL if the packet is not valid.
     307 * @returns             EINVAL if the calling module does not accept the memory.
     308 * @returns             ENOMEM if the desired and actual sizes differ.
     309 * @returns             Other error codes as defined for the
    280310 *                      async_share_in_finalize() function.
    281311 */
    282 static int packet_reply(packet_t *packet)
     312static int packet_reply(const packet_t packet)
    283313{
    284314        ipc_callid_t callid;
     
    289319
    290320        if (!async_share_in_receive(&callid, &size)) {
    291                 async_answer_0(callid, EINVAL);
     321                ipc_answer_0(callid, EINVAL);
    292322                return EINVAL;
    293323        }
    294324
    295325        if (size != packet->length) {
    296                 async_answer_0(callid, ENOMEM);
     326                ipc_answer_0(callid, ENOMEM);
    297327                return ENOMEM;
    298328        }
     
    309339 * @param[out] answer_count The last parameter for the actual answer in the
    310340 *                      answer parameter.
    311  * @return              EOK on success.
    312  * @return              ENOMEM if there is not enough memory left.
    313  * @return              ENOENT if there is no such packet as in the packet
     341 * @returns             EOK on success.
     342 * @returns             ENOMEM if there is not enough memory left.
     343 * @returns             ENOENT if there is no such packet as in the packet
    314344 *                      message parameter.
    315  * @return              ENOTSUP if the message is not known.
    316  * @return              Other error codes as defined for the
     345 * @returns             ENOTSUP if the message is not known.
     346 * @returns             Other error codes as defined for the
    317347 *                      packet_release_wrapper() function.
    318348 */
    319349int
    320350packet_server_message(ipc_callid_t callid, ipc_call_t *call, ipc_call_t *answer,
    321     size_t *answer_count)
    322 {
    323         packet_t *packet;
     351    int *answer_count)
     352{
     353        packet_t packet;
    324354
    325355        *answer_count = 0;
    326         switch (IPC_GET_IMETHOD(*call)) {
     356        switch (IPC_GET_METHOD(*call)) {
    327357        case IPC_M_PHONE_HUNGUP:
    328358                return EOK;
     
    330360        case NET_PACKET_CREATE_1:
    331361                packet = packet_get_local(DEFAULT_ADDR_LEN, DEFAULT_PREFIX,
    332                     IPC_GET_CONTENT(*call), DEFAULT_SUFFIX);
     362                    IPC_GET_CONTENT(call), DEFAULT_SUFFIX);
    333363                if (!packet)
    334364                        return ENOMEM;
    335365                *answer_count = 2;
    336                 IPC_SET_ARG1(*answer, (sysarg_t) packet->packet_id);
    337                 IPC_SET_ARG2(*answer, (sysarg_t) packet->length);
     366                IPC_SET_ARG1(*answer, (ipcarg_t) packet->packet_id);
     367                IPC_SET_ARG2(*answer, (ipcarg_t) packet->length);
    338368                return EOK;
    339369       
    340370        case NET_PACKET_CREATE_4:
    341371                packet = packet_get_local(
    342                     ((DEFAULT_ADDR_LEN < IPC_GET_ADDR_LEN(*call)) ?
    343                     IPC_GET_ADDR_LEN(*call) : DEFAULT_ADDR_LEN),
    344                     DEFAULT_PREFIX + IPC_GET_PREFIX(*call),
    345                     IPC_GET_CONTENT(*call),
    346                     DEFAULT_SUFFIX + IPC_GET_SUFFIX(*call));
     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));
    347377                if (!packet)
    348378                        return ENOMEM;
    349379                *answer_count = 2;
    350                 IPC_SET_ARG1(*answer, (sysarg_t) packet->packet_id);
    351                 IPC_SET_ARG2(*answer, (sysarg_t) packet->length);
     380                IPC_SET_ARG1(*answer, (ipcarg_t) packet->packet_id);
     381                IPC_SET_ARG2(*answer, (ipcarg_t) packet->length);
    352382                return EOK;
    353383       
    354384        case NET_PACKET_GET:
    355                 packet = pm_find(IPC_GET_ID(*call));
     385                packet = pm_find(IPC_GET_ID(call));
    356386                if (!packet_is_valid(packet))
    357387                        return ENOENT;
     
    359389       
    360390        case NET_PACKET_GET_SIZE:
    361                 packet = pm_find(IPC_GET_ID(*call));
     391                packet = pm_find(IPC_GET_ID(call));
    362392                if (!packet_is_valid(packet))
    363393                        return ENOENT;
    364                 IPC_SET_ARG1(*answer, (sysarg_t) packet->length);
     394                IPC_SET_ARG1(*answer, (ipcarg_t) packet->length);
    365395                *answer_count = 1;
    366396                return EOK;
    367397       
    368398        case NET_PACKET_RELEASE:
    369                 return packet_release_wrapper(IPC_GET_ID(*call));
     399                return packet_release_wrapper(IPC_GET_ID(call));
    370400        }
    371401       
Note: See TracChangeset for help on using the changeset viewer.