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