[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>
|
---|
[d52fbaf] | 40 | #include <ipc/packet.h>
|
---|
[21580dd] | 41 | #include <sys/mman.h>
|
---|
| 42 |
|
---|
[0a866eeb] | 43 | #include <packet_client.h>
|
---|
| 44 | #include <packet_remote.h>
|
---|
| 45 |
|
---|
[c69d327] | 46 | #include <net/packet.h>
|
---|
| 47 | #include <net/packet_header.h>
|
---|
[21580dd] | 48 |
|
---|
[14f1db0] | 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 | *
|
---|
[21580dd] | 63 | */
|
---|
[2fa0ad9] | 64 | static int
|
---|
[46d4d9f] | 65 | packet_return(int phone, packet_t **packet, packet_id_t packet_id, size_t size)
|
---|
[2fa0ad9] | 66 | {
|
---|
[aadf01e] | 67 | ipc_call_t answer;
|
---|
[0ab68f6] | 68 | aid_t message;
|
---|
| 69 | int rc;
|
---|
| 70 |
|
---|
| 71 | message = async_send_1(phone, NET_PACKET_GET, packet_id, &answer);
|
---|
[2fa0ad9] | 72 |
|
---|
[46d4d9f] | 73 | *packet = (packet_t *) as_get_mappable_page(size);
|
---|
[0ab68f6] | 74 | rc = async_share_in_start_0_0(phone, *packet, size);
|
---|
| 75 | if (rc != EOK) {
|
---|
[aadf01e] | 76 | munmap(*packet, size);
|
---|
| 77 | async_wait_for(message, NULL);
|
---|
[0ab68f6] | 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;
|
---|
[21580dd] | 85 | }
|
---|
[14f1db0] | 86 |
|
---|
[96b02eb9] | 87 | sysarg_t result;
|
---|
[aadf01e] | 88 | async_wait_for(message, &result);
|
---|
[14f1db0] | 89 |
|
---|
[21580dd] | 90 | return result;
|
---|
| 91 | }
|
---|
| 92 |
|
---|
[b69ceea] | 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.
|
---|
[1bfd3d3] | 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
|
---|
[b69ceea] | 104 | * message.
|
---|
[1bfd3d3] | 105 | * @return Other error codes as defined for the packet_return()
|
---|
[b69ceea] | 106 | * function.
|
---|
| 107 | */
|
---|
[46d4d9f] | 108 | int packet_translate_remote(int phone, packet_t **packet, packet_id_t packet_id)
|
---|
[14f1db0] | 109 | {
|
---|
[0ab68f6] | 110 | int rc;
|
---|
[14f1db0] | 111 |
|
---|
| 112 | if (!packet)
|
---|
| 113 | return EINVAL;
|
---|
| 114 |
|
---|
| 115 | *packet = pm_find(packet_id);
|
---|
[2fa0ad9] | 116 | if (!*packet) {
|
---|
[96b02eb9] | 117 | sysarg_t size;
|
---|
[14f1db0] | 118 |
|
---|
[0ab68f6] | 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;
|
---|
[14f1db0] | 126 | }
|
---|
[2fa0ad9] | 127 | if ((*packet)->next) {
|
---|
[46d4d9f] | 128 | packet_t *next;
|
---|
[14f1db0] | 129 |
|
---|
[2fa0ad9] | 130 | return packet_translate_remote(phone, &next, (*packet)->next);
|
---|
[14f1db0] | 131 | }
|
---|
| 132 |
|
---|
| 133 | return EOK;
|
---|
| 134 | }
|
---|
[21580dd] | 135 |
|
---|
[b69ceea] | 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.
|
---|
[1bfd3d3] | 146 | * @return The packet reference.
|
---|
| 147 | * @return NULL on error.
|
---|
[b69ceea] | 148 | */
|
---|
[46d4d9f] | 149 | packet_t *packet_get_4_remote(int phone, size_t max_content, size_t addr_len,
|
---|
[14f1db0] | 150 | size_t max_prefix, size_t max_suffix)
|
---|
| 151 | {
|
---|
[96b02eb9] | 152 | sysarg_t packet_id;
|
---|
| 153 | sysarg_t size;
|
---|
[0ab68f6] | 154 | int rc;
|
---|
[14f1db0] | 155 |
|
---|
[0ab68f6] | 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)
|
---|
[21580dd] | 159 | return NULL;
|
---|
[14f1db0] | 160 |
|
---|
| 161 |
|
---|
[46d4d9f] | 162 | packet_t *packet = pm_find(packet_id);
|
---|
[14f1db0] | 163 | if (!packet) {
|
---|
[0ab68f6] | 164 | rc = packet_return(phone, &packet, packet_id, size);
|
---|
| 165 | if (rc != EOK)
|
---|
[21580dd] | 166 | return NULL;
|
---|
| 167 | }
|
---|
[14f1db0] | 168 |
|
---|
[21580dd] | 169 | return packet;
|
---|
| 170 | }
|
---|
| 171 |
|
---|
[b69ceea] | 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.
|
---|
[1bfd3d3] | 178 | * @return The packet reference.
|
---|
| 179 | * @return NULL on error.
|
---|
[b69ceea] | 180 | */
|
---|
[46d4d9f] | 181 | packet_t *packet_get_1_remote(int phone, size_t content)
|
---|
[14f1db0] | 182 | {
|
---|
[96b02eb9] | 183 | sysarg_t packet_id;
|
---|
| 184 | sysarg_t size;
|
---|
[0ab68f6] | 185 | int rc;
|
---|
[14f1db0] | 186 |
|
---|
[0ab68f6] | 187 | rc = async_req_1_2(phone, NET_PACKET_CREATE_1, content, &packet_id,
|
---|
| 188 | &size);
|
---|
| 189 | if (rc != EOK)
|
---|
[21580dd] | 190 | return NULL;
|
---|
[14f1db0] | 191 |
|
---|
[46d4d9f] | 192 | packet_t *packet = pm_find(packet_id);
|
---|
[14f1db0] | 193 | if (!packet) {
|
---|
[0ab68f6] | 194 | rc = packet_return(phone, &packet, packet_id, size);
|
---|
| 195 | if (rc != EOK)
|
---|
[21580dd] | 196 | return NULL;
|
---|
| 197 | }
|
---|
[14f1db0] | 198 |
|
---|
[21580dd] | 199 | return packet;
|
---|
| 200 | }
|
---|
| 201 |
|
---|
[b69ceea] | 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 | */
|
---|
[14f1db0] | 212 | void pq_release_remote(int phone, packet_id_t packet_id)
|
---|
| 213 | {
|
---|
[aadf01e] | 214 | async_msg_1(phone, NET_PACKET_RELEASE, packet_id);
|
---|
[21580dd] | 215 | }
|
---|
| 216 |
|
---|
| 217 | /** @}
|
---|
| 218 | */
|
---|