| 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 libnet
|
|---|
| 30 | * @{
|
|---|
| 31 | */
|
|---|
| 32 |
|
|---|
| 33 | /** @file
|
|---|
| 34 | * Packet client interface implementation for remote modules.
|
|---|
| 35 | * @see packet_client.h
|
|---|
| 36 | */
|
|---|
| 37 |
|
|---|
| 38 | #include <async.h>
|
|---|
| 39 | #include <errno.h>
|
|---|
| 40 | #include <ipc/packet.h>
|
|---|
| 41 | #include <sys/mman.h>
|
|---|
| 42 |
|
|---|
| 43 | #include <packet_client.h>
|
|---|
| 44 | #include <packet_remote.h>
|
|---|
| 45 |
|
|---|
| 46 | #include <net/packet.h>
|
|---|
| 47 | #include <net/packet_header.h>
|
|---|
| 48 |
|
|---|
| 49 | /** Obtain the packet from the packet server as the shared memory block.
|
|---|
| 50 | *
|
|---|
| 51 | * Create the local packet mapping as well.
|
|---|
| 52 | *
|
|---|
| 53 | * @param[in] phone The packet server module phone.
|
|---|
| 54 | * @param[out] packet The packet reference pointer to store the received
|
|---|
| 55 | * packet reference.
|
|---|
| 56 | * @param[in] packet_id The packet identifier.
|
|---|
| 57 | * @param[in] size The packet total size in bytes.
|
|---|
| 58 | *
|
|---|
| 59 | * @return EOK on success.
|
|---|
| 60 | * @return Other error codes as defined for the pm_add() function.
|
|---|
| 61 | * @return Other error codes as defined for the async_share_in_start() function.
|
|---|
| 62 | *
|
|---|
| 63 | */
|
|---|
| 64 | static int
|
|---|
| 65 | packet_return(int phone, packet_t **packet, packet_id_t packet_id, size_t size)
|
|---|
| 66 | {
|
|---|
| 67 | ipc_call_t answer;
|
|---|
| 68 | aid_t message;
|
|---|
| 69 | int rc;
|
|---|
| 70 |
|
|---|
| 71 | message = async_send_1(phone, NET_PACKET_GET, packet_id, &answer);
|
|---|
| 72 |
|
|---|
| 73 | *packet = (packet_t *) as_get_mappable_page(size);
|
|---|
| 74 | rc = async_share_in_start_0_0(phone, *packet, size);
|
|---|
| 75 | if (rc != EOK) {
|
|---|
| 76 | munmap(*packet, size);
|
|---|
| 77 | async_wait_for(message, NULL);
|
|---|
| 78 | return rc;
|
|---|
| 79 | }
|
|---|
| 80 | rc = pm_add(*packet);
|
|---|
| 81 | if (rc != EOK) {
|
|---|
| 82 | munmap(*packet, size);
|
|---|
| 83 | async_wait_for(message, NULL);
|
|---|
| 84 | return rc;
|
|---|
| 85 | }
|
|---|
| 86 |
|
|---|
| 87 | sysarg_t result;
|
|---|
| 88 | async_wait_for(message, &result);
|
|---|
| 89 |
|
|---|
| 90 | return result;
|
|---|
| 91 | }
|
|---|
| 92 |
|
|---|
| 93 | /** Translates the packet identifier to the packet reference.
|
|---|
| 94 | *
|
|---|
| 95 | * Tries to find mapping first.
|
|---|
| 96 | * Contacts the packet server to share the packet if the mapping is not present.
|
|---|
| 97 | *
|
|---|
| 98 | * @param[in] phone The packet server module phone.
|
|---|
| 99 | * @param[out] packet The packet reference.
|
|---|
| 100 | * @param[in] packet_id The packet identifier.
|
|---|
| 101 | * @return EOK on success.
|
|---|
| 102 | * @return EINVAL if the packet parameter is NULL.
|
|---|
| 103 | * @return Other error codes as defined for the NET_PACKET_GET_SIZE
|
|---|
| 104 | * message.
|
|---|
| 105 | * @return Other error codes as defined for the packet_return()
|
|---|
| 106 | * function.
|
|---|
| 107 | */
|
|---|
| 108 | int packet_translate_remote(int phone, packet_t **packet, packet_id_t packet_id)
|
|---|
| 109 | {
|
|---|
| 110 | int rc;
|
|---|
| 111 |
|
|---|
| 112 | if (!packet)
|
|---|
| 113 | return EINVAL;
|
|---|
| 114 |
|
|---|
| 115 | *packet = pm_find(packet_id);
|
|---|
| 116 | if (!*packet) {
|
|---|
| 117 | sysarg_t size;
|
|---|
| 118 |
|
|---|
| 119 | rc = async_req_1_1(phone, NET_PACKET_GET_SIZE, packet_id,
|
|---|
| 120 | &size);
|
|---|
| 121 | if (rc != EOK)
|
|---|
| 122 | return rc;
|
|---|
| 123 | rc = packet_return(phone, packet, packet_id, size);
|
|---|
| 124 | if (rc != EOK)
|
|---|
| 125 | return rc;
|
|---|
| 126 | }
|
|---|
| 127 | if ((*packet)->next) {
|
|---|
| 128 | packet_t *next;
|
|---|
| 129 |
|
|---|
| 130 | return packet_translate_remote(phone, &next, (*packet)->next);
|
|---|
| 131 | }
|
|---|
| 132 |
|
|---|
| 133 | return EOK;
|
|---|
| 134 | }
|
|---|
| 135 |
|
|---|
| 136 | /** Obtains the packet of the given dimensions.
|
|---|
| 137 | *
|
|---|
| 138 | * Contacts the packet server to return the appropriate packet.
|
|---|
| 139 | *
|
|---|
| 140 | * @param[in] phone The packet server module phone.
|
|---|
| 141 | * @param[in] addr_len The source and destination addresses maximal length in
|
|---|
| 142 | * bytes.
|
|---|
| 143 | * @param[in] max_prefix The maximal prefix length in bytes.
|
|---|
| 144 | * @param[in] max_content The maximal content length in bytes.
|
|---|
| 145 | * @param[in] max_suffix The maximal suffix length in bytes.
|
|---|
| 146 | * @return The packet reference.
|
|---|
| 147 | * @return NULL on error.
|
|---|
| 148 | */
|
|---|
| 149 | packet_t *packet_get_4_remote(int phone, size_t max_content, size_t addr_len,
|
|---|
| 150 | size_t max_prefix, size_t max_suffix)
|
|---|
| 151 | {
|
|---|
| 152 | sysarg_t packet_id;
|
|---|
| 153 | sysarg_t size;
|
|---|
| 154 | int rc;
|
|---|
| 155 |
|
|---|
| 156 | rc = async_req_4_2(phone, NET_PACKET_CREATE_4, max_content, addr_len,
|
|---|
| 157 | max_prefix, max_suffix, &packet_id, &size);
|
|---|
| 158 | if (rc != EOK)
|
|---|
| 159 | return NULL;
|
|---|
| 160 |
|
|---|
| 161 |
|
|---|
| 162 | packet_t *packet = pm_find(packet_id);
|
|---|
| 163 | if (!packet) {
|
|---|
| 164 | rc = packet_return(phone, &packet, packet_id, size);
|
|---|
| 165 | if (rc != EOK)
|
|---|
| 166 | return NULL;
|
|---|
| 167 | }
|
|---|
| 168 |
|
|---|
| 169 | return packet;
|
|---|
| 170 | }
|
|---|
| 171 |
|
|---|
| 172 | /** Obtains the packet of the given content size.
|
|---|
| 173 | *
|
|---|
| 174 | * Contacts the packet server to return the appropriate packet.
|
|---|
| 175 | *
|
|---|
| 176 | * @param[in] phone The packet server module phone.
|
|---|
| 177 | * @param[in] content The maximal content length in bytes.
|
|---|
| 178 | * @return The packet reference.
|
|---|
| 179 | * @return NULL on error.
|
|---|
| 180 | */
|
|---|
| 181 | packet_t *packet_get_1_remote(int phone, size_t content)
|
|---|
| 182 | {
|
|---|
| 183 | sysarg_t packet_id;
|
|---|
| 184 | sysarg_t size;
|
|---|
| 185 | int rc;
|
|---|
| 186 |
|
|---|
| 187 | rc = async_req_1_2(phone, NET_PACKET_CREATE_1, content, &packet_id,
|
|---|
| 188 | &size);
|
|---|
| 189 | if (rc != EOK)
|
|---|
| 190 | return NULL;
|
|---|
| 191 |
|
|---|
| 192 | packet_t *packet = pm_find(packet_id);
|
|---|
| 193 | if (!packet) {
|
|---|
| 194 | rc = packet_return(phone, &packet, packet_id, size);
|
|---|
| 195 | if (rc != EOK)
|
|---|
| 196 | return NULL;
|
|---|
| 197 | }
|
|---|
| 198 |
|
|---|
| 199 | return packet;
|
|---|
| 200 | }
|
|---|
| 201 |
|
|---|
| 202 | /** Releases the packet queue.
|
|---|
| 203 | *
|
|---|
| 204 | * All packets in the queue are marked as free for use.
|
|---|
| 205 | * The packet queue may be one packet only.
|
|---|
| 206 | * The module should not use the packets after this point until they are
|
|---|
| 207 | * received or obtained again.
|
|---|
| 208 | *
|
|---|
| 209 | * @param[in] phone The packet server module phone.
|
|---|
| 210 | * @param[in] packet_id The packet identifier.
|
|---|
| 211 | */
|
|---|
| 212 | void pq_release_remote(int phone, packet_id_t packet_id)
|
|---|
| 213 | {
|
|---|
| 214 | async_msg_1(phone, NET_PACKET_RELEASE, packet_id);
|
|---|
| 215 | }
|
|---|
| 216 |
|
|---|
| 217 | /** @}
|
|---|
| 218 | */
|
|---|