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