source: mainline/uspace/lib/net/generic/packet_remote.c@ 4ef32e0c

lfn serial ticket/834-toolchain-update topic/msim-upgrade topic/simplify-dev-export
Last change on this file since 4ef32e0c was b69ceea, checked in by Jakub Jermar <jakub@…>, 15 years ago

Cleanup packet_{client,remote}.[ch].

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