source: mainline/uspace/lib/net/generic/packet_remote.c@ 24cf31f1

lfn serial ticket/834-toolchain-update topic/msim-upgrade topic/simplify-dev-export
Last change on this file since 24cf31f1 was 8708be3b, checked in by Martin Decky <martin@…>, 14 years ago

do not unmap the memory in this error path, it was not mapped previously

  • Property mode set to 100644
File size: 6.4 KB
RevLine 
[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 *
[6b82009]53 * @param[in] sess Packet server module session.
54 * @param[out] packet Packet reference pointer to store the received
[14f1db0]55 * packet reference.
[6b82009]56 * @param[in] packet_id Packet identifier.
57 * @param[in] size Packet total size in bytes.
[14f1db0]58 *
59 * @return EOK on success.
60 * @return Other error codes as defined for the pm_add() function.
[6b82009]61 * @return Other error codes as defined for the async_share_in_start()
62 * function.
[14f1db0]63 *
[21580dd]64 */
[6b82009]65static int packet_return(async_sess_t *sess, packet_t **packet,
66 packet_id_t packet_id, size_t size)
[2fa0ad9]67{
[6b82009]68 *packet = (packet_t *) as_get_mappable_page(size);
69
70 async_exch_t *exch = async_exchange_begin(sess);
[aadf01e]71 ipc_call_t answer;
[6b82009]72 aid_t message = async_send_1(exch, NET_PACKET_GET, packet_id, &answer);
73 int rc = async_share_in_start_0_0(exch, *packet, size);
74 async_exchange_end(exch);
75
76 sysarg_t result;
77 async_wait_for(message, &result);
[0ab68f6]78
[8708be3b]79 if (rc != EOK)
[0ab68f6]80 return rc;
[6b82009]81
[0ab68f6]82 rc = pm_add(*packet);
83 if (rc != EOK) {
84 munmap(*packet, size);
85 return rc;
[21580dd]86 }
[14f1db0]87
[21580dd]88 return result;
89}
90
[6b82009]91/** Translate the packet identifier to the packet reference.
92 *
93 * Try to find mapping first. The packet server is asked to share
94 * the packet if the mapping is not present.
95 *
96 * @param[in] sess Packet server module session.
97 * @param[out] packet Packet reference.
98 * @param[in] packet_id Packet identifier.
99 *
100 * @return EOK on success.
101 * @return EINVAL if the packet parameter is NULL.
102 * @return Other error codes as defined for the NET_PACKET_GET_SIZE
103 * message.
104 * @return Other error codes as defined for the packet_return()
105 * function.
106 *
[b69ceea]107 */
[6b82009]108int packet_translate_remote(async_sess_t *sess, packet_t **packet,
109 packet_id_t packet_id)
[14f1db0]110{
111 if (!packet)
112 return EINVAL;
113
114 *packet = pm_find(packet_id);
[49bd793b]115 if (*packet == NULL) {
[6b82009]116 async_exch_t *exch = async_exchange_begin(sess);
[96b02eb9]117 sysarg_t size;
[6b82009]118 int rc = async_req_1_1(exch, NET_PACKET_GET_SIZE, packet_id,
[0ab68f6]119 &size);
[6b82009]120 async_exchange_end(exch);
121
[0ab68f6]122 if (rc != EOK)
123 return rc;
[6b82009]124
125 rc = packet_return(sess, packet, packet_id, size);
[0ab68f6]126 if (rc != EOK)
127 return rc;
[14f1db0]128 }
[6b82009]129
[49bd793b]130 if ((*packet != NULL) && ((*packet)->next)) {
[46d4d9f]131 packet_t *next;
[6b82009]132 return packet_translate_remote(sess, &next, (*packet)->next);
[14f1db0]133 }
134
135 return EOK;
136}
[21580dd]137
[6b82009]138/** Obtain the packet of given dimensions.
139 *
140 * Contact the packet server to return the appropriate packet.
141 *
142 * @param[in] sess Packet server module session.
143 * @param[in] addr_len Source and destination addresses maximal length
144 * in bytes.
145 * @param[in] max_prefix Maximal prefix length in bytes.
146 * @param[in] max_content Maximal content length in bytes.
147 * @param[in] max_suffix Maximal suffix length in bytes.
148 *
149 * @return The packet reference.
150 * @return NULL on error.
151 *
[b69ceea]152 */
[6b82009]153packet_t *packet_get_4_remote(async_sess_t *sess, size_t max_content,
154 size_t addr_len, size_t max_prefix, size_t max_suffix)
[14f1db0]155{
[6b82009]156 async_exch_t *exch = async_exchange_begin(sess);
[96b02eb9]157 sysarg_t packet_id;
158 sysarg_t size;
[6b82009]159 int rc = async_req_4_2(exch, NET_PACKET_CREATE_4, max_content, addr_len,
[0ab68f6]160 max_prefix, max_suffix, &packet_id, &size);
[6b82009]161 async_exchange_end(exch);
162
[0ab68f6]163 if (rc != EOK)
[21580dd]164 return NULL;
[14f1db0]165
[46d4d9f]166 packet_t *packet = pm_find(packet_id);
[14f1db0]167 if (!packet) {
[6b82009]168 rc = packet_return(sess, &packet, packet_id, size);
[0ab68f6]169 if (rc != EOK)
[21580dd]170 return NULL;
171 }
[14f1db0]172
[21580dd]173 return packet;
174}
175
[6b82009]176/** Obtain the packet of given content size.
177 *
178 * Contact the packet server to return the appropriate packet.
[b69ceea]179 *
[6b82009]180 * @param[in] sess Packet server module session.
181 * @param[in] content Maximal content length in bytes.
182 *
183 * @return The packet reference.
184 * @return NULL on error.
[b69ceea]185 *
186 */
[6b82009]187packet_t *packet_get_1_remote(async_sess_t *sess, size_t content)
[14f1db0]188{
[6b82009]189 async_exch_t *exch = async_exchange_begin(sess);
[96b02eb9]190 sysarg_t packet_id;
191 sysarg_t size;
[6b82009]192 int rc = async_req_1_2(exch, NET_PACKET_CREATE_1, content, &packet_id,
[0ab68f6]193 &size);
[6b82009]194 async_exchange_end(exch);
195
[0ab68f6]196 if (rc != EOK)
[21580dd]197 return NULL;
[14f1db0]198
[46d4d9f]199 packet_t *packet = pm_find(packet_id);
[14f1db0]200 if (!packet) {
[6b82009]201 rc = packet_return(sess, &packet, packet_id, size);
[0ab68f6]202 if (rc != EOK)
[21580dd]203 return NULL;
204 }
[14f1db0]205
[21580dd]206 return packet;
207}
208
[6b82009]209/** Release the packet queue.
[b69ceea]210 *
211 * All packets in the queue are marked as free for use.
212 * The packet queue may be one packet only.
213 * The module should not use the packets after this point until they are
214 * received or obtained again.
215 *
[6b82009]216 * @param[in] sess Packet server module session.
217 * @param[in] packet_id Packet identifier.
218 *
[b69ceea]219 */
[6b82009]220void pq_release_remote(async_sess_t *sess, packet_id_t packet_id)
[14f1db0]221{
[6b82009]222 async_exch_t *exch = async_exchange_begin(sess);
223 async_msg_1(exch, NET_PACKET_RELEASE, packet_id);
224 async_exchange_end(exch);
[21580dd]225}
226
227/** @}
228 */
Note: See TracBrowser for help on using the repository browser.