Ignore:
File:
1 edited

Legend:

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

    ra649e73f r28a3e74  
    3636
    3737#include <packet_server.h>
    38 #include <packet_local.h>
    3938
    4039#include <align.h>
     
    4241#include <async.h>
    4342#include <errno.h>
    44 #include <err.h>
    4543#include <fibril_synch.h>
    4644#include <unistd.h>
    4745#include <sys/mman.h>
    48 
    49 #include <ipc/ipc.h>
    5046#include <ipc/packet.h>
    5147#include <ipc/net.h>
    52 
    5348#include <net/packet.h>
    5449#include <net/packet_header.h>
     
    7065        fibril_mutex_t lock;
    7166        /** Free packet queues. */
    72         packet_t free[FREE_QUEUES_COUNT];
     67        packet_t *free[FREE_QUEUES_COUNT];
    7368       
    7469        /**
     
    10499};
    105100
    106 int 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 
    115101/** Clears and initializes the packet according to the given dimensions.
    116102 *
     
    123109 */
    124110static void
    125 packet_init(packet_t packet, size_t addr_len, size_t max_prefix,
     111packet_init(packet_t *packet, size_t addr_len, size_t max_prefix,
    126112    size_t max_content, size_t max_suffix)
    127113{
    128         // clear the packet content
    129         bzero(((void *) packet) + sizeof(struct packet),
    130             packet->length - sizeof(struct packet));
    131        
    132         // clear the packet header
     114        /* Clear the packet content */
     115        bzero(((void *) packet) + sizeof(packet_t),
     116            packet->length - sizeof(packet_t));
     117       
     118        /* Clear the packet header */
    133119        packet->order = 0;
    134120        packet->metric = 0;
     
    136122        packet->next = 0;
    137123        packet->addr_len = 0;
    138         packet->src_addr = sizeof(struct packet);
     124        packet->src_addr = sizeof(packet_t);
    139125        packet->dest_addr = packet->src_addr + addr_len;
    140126        packet->max_prefix = max_prefix;
     
    145131
    146132/** Creates a new packet of dimensions at least as given.
    147  *
    148  * Should be used only when the global data are locked.
    149133 *
    150134 * @param[in] length    The total length of the packet, including the header,
     
    155139 * @param[in] max_content The maximal content length in bytes.
    156140 * @param[in] max_suffix The maximal suffix length in bytes.
    157  * @returns             The packet of dimensions at least as given.
    158  * @returns             NULL if there is not enough memory left.
    159  */
    160 static packet_t
     141 * @return              The packet of dimensions at least as given.
     142 * @return              NULL if there is not enough memory left.
     143 */
     144static packet_t *
    161145packet_create(size_t length, size_t addr_len, size_t max_prefix,
    162146    size_t max_content, size_t max_suffix)
    163147{
    164         ERROR_DECLARE;
    165 
    166         packet_t packet;
    167 
    168         // already locked
    169         packet = (packet_t) mmap(NULL, length, PROTO_READ | PROTO_WRITE,
     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,
    170155            MAP_SHARED | MAP_ANONYMOUS, 0, 0);
    171156        if (packet == MAP_FAILED)
     
    177162        packet_init(packet, addr_len, max_prefix, max_content, max_suffix);
    178163        packet->magic_value = PACKET_MAGIC_VALUE;
    179         if (ERROR_OCCURRED(pm_add(packet))) {
     164        rc = pm_add(packet);
     165        if (rc != EOK) {
    180166                munmap(packet, packet->length);
    181167                return NULL;
     
    199185 * @return              NULL if there is not enough memory left.
    200186 */
    201 static packet_t
     187static packet_t *
    202188packet_get_local(size_t addr_len, size_t max_prefix, size_t max_content,
    203189    size_t max_suffix)
    204190{
    205         size_t length = ALIGN_UP(sizeof(struct packet) + 2 * addr_len +
     191        size_t length = ALIGN_UP(sizeof(packet_t) + 2 * addr_len +
    206192            max_prefix + max_content + max_suffix, PAGE_SIZE);
    207193       
    208194        fibril_mutex_lock(&ps_globals.lock);
    209195       
    210         packet_t packet;
     196        packet_t *packet;
    211197        unsigned int index;
    212198       
    213199        for (index = 0; index < FREE_QUEUES_COUNT; index++) {
    214                 if (length > ps_globals.sizes[index])
     200                if ((length > ps_globals.sizes[index]) &&
     201                    (index < FREE_QUEUES_COUNT - 1))
    215202                        continue;
    216203               
     
    241228}
    242229
    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 
    255230/** Release the packet and returns it to the appropriate free packet queue.
    256231 *
    257  * Should be used only when the global data are locked.
    258  *
    259232 * @param[in] packet    The packet to be released.
    260233 *
    261234 */
    262 static void packet_release(packet_t packet)
     235static void packet_release(packet_t *packet)
    263236{
    264237        int index;
    265238        int result;
     239
     240        assert(fibril_mutex_is_locked(&ps_globals.lock));
    266241
    267242        for (index = 0; (index < FREE_QUEUES_COUNT - 1) &&
     
    278253 *
    279254 * @param[in] packet_id The first packet identifier.
    280  * @returns             EOK on success.
    281  * @returns             ENOENT if there is no such packet.
     255 * @return              EOK on success.
     256 * @return              ENOENT if there is no such packet.
    282257 */
    283258static int packet_release_wrapper(packet_id_t packet_id)
    284259{
    285         packet_t packet;
     260        packet_t *packet;
    286261
    287262        packet = pm_find(packet_id);
     
    296271}
    297272
    298 void pq_release_local(int phone, packet_id_t packet_id)
    299 {
    300         (void) packet_release_wrapper(packet_id);
    301 }
    302 
    303273/** Shares the packet memory block.
    304274 * @param[in] packet    The packet to be shared.
    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
     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
    310280 *                      async_share_in_finalize() function.
    311281 */
    312 static int packet_reply(const packet_t packet)
     282static int packet_reply(packet_t *packet)
    313283{
    314284        ipc_callid_t callid;
     
    319289
    320290        if (!async_share_in_receive(&callid, &size)) {
    321                 ipc_answer_0(callid, EINVAL);
     291                async_answer_0(callid, EINVAL);
    322292                return EINVAL;
    323293        }
    324294
    325295        if (size != packet->length) {
    326                 ipc_answer_0(callid, ENOMEM);
     296                async_answer_0(callid, ENOMEM);
    327297                return ENOMEM;
    328298        }
     
    339309 * @param[out] answer_count The last parameter for the actual answer in the
    340310 *                      answer parameter.
    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
     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
    344314 *                      message parameter.
    345  * @returns             ENOTSUP if the message is not known.
    346  * @returns             Other error codes as defined for the
     315 * @return              ENOTSUP if the message is not known.
     316 * @return              Other error codes as defined for the
    347317 *                      packet_release_wrapper() function.
    348318 */
    349319int
    350320packet_server_message(ipc_callid_t callid, ipc_call_t *call, ipc_call_t *answer,
    351     int *answer_count)
    352 {
    353         packet_t packet;
     321    size_t *answer_count)
     322{
     323        packet_t *packet;
    354324
    355325        *answer_count = 0;
    356         switch (IPC_GET_METHOD(*call)) {
     326        switch (IPC_GET_IMETHOD(*call)) {
    357327        case IPC_M_PHONE_HUNGUP:
    358328                return EOK;
     
    360330        case NET_PACKET_CREATE_1:
    361331                packet = packet_get_local(DEFAULT_ADDR_LEN, DEFAULT_PREFIX,
    362                     IPC_GET_CONTENT(call), DEFAULT_SUFFIX);
     332                    IPC_GET_CONTENT(*call), DEFAULT_SUFFIX);
    363333                if (!packet)
    364334                        return ENOMEM;
    365335                *answer_count = 2;
    366                 IPC_SET_ARG1(*answer, (ipcarg_t) packet->packet_id);
    367                 IPC_SET_ARG2(*answer, (ipcarg_t) packet->length);
     336                IPC_SET_ARG1(*answer, (sysarg_t) packet->packet_id);
     337                IPC_SET_ARG2(*answer, (sysarg_t) packet->length);
    368338                return EOK;
    369339       
    370340        case NET_PACKET_CREATE_4:
    371341                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));
     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));
    377347                if (!packet)
    378348                        return ENOMEM;
    379349                *answer_count = 2;
    380                 IPC_SET_ARG1(*answer, (ipcarg_t) packet->packet_id);
    381                 IPC_SET_ARG2(*answer, (ipcarg_t) packet->length);
     350                IPC_SET_ARG1(*answer, (sysarg_t) packet->packet_id);
     351                IPC_SET_ARG2(*answer, (sysarg_t) packet->length);
    382352                return EOK;
    383353       
    384354        case NET_PACKET_GET:
    385                 packet = pm_find(IPC_GET_ID(call));
     355                packet = pm_find(IPC_GET_ID(*call));
    386356                if (!packet_is_valid(packet))
    387357                        return ENOENT;
     
    389359       
    390360        case NET_PACKET_GET_SIZE:
    391                 packet = pm_find(IPC_GET_ID(call));
     361                packet = pm_find(IPC_GET_ID(*call));
    392362                if (!packet_is_valid(packet))
    393363                        return ENOENT;
    394                 IPC_SET_ARG1(*answer, (ipcarg_t) packet->length);
     364                IPC_SET_ARG1(*answer, (sysarg_t) packet->length);
    395365                *answer_count = 1;
    396366                return EOK;
    397367       
    398368        case NET_PACKET_RELEASE:
    399                 return packet_release_wrapper(IPC_GET_ID(call));
     369                return packet_release_wrapper(IPC_GET_ID(*call));
    400370        }
    401371       
Note: See TracChangeset for help on using the changeset viewer.