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 <async_obsolete.h>
|
---|
40 | #include <errno.h>
|
---|
41 | #include <ipc/packet.h>
|
---|
42 | #include <sys/mman.h>
|
---|
43 |
|
---|
44 | #include <packet_client.h>
|
---|
45 | #include <packet_remote.h>
|
---|
46 |
|
---|
47 | #include <net/packet.h>
|
---|
48 | #include <net/packet_header.h>
|
---|
49 |
|
---|
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_obsolete_share_in_start() function.
|
---|
63 | *
|
---|
64 | */
|
---|
65 | static int
|
---|
66 | packet_return(int phone, packet_t **packet, packet_id_t packet_id, size_t size)
|
---|
67 | {
|
---|
68 | ipc_call_t answer;
|
---|
69 | aid_t message;
|
---|
70 | int rc;
|
---|
71 |
|
---|
72 | message = async_obsolete_send_1(phone, NET_PACKET_GET, packet_id, &answer);
|
---|
73 |
|
---|
74 | *packet = (packet_t *) as_get_mappable_page(size);
|
---|
75 | rc = async_obsolete_share_in_start_0_0(phone, *packet, size);
|
---|
76 | if (rc != EOK) {
|
---|
77 | munmap(*packet, size);
|
---|
78 | async_wait_for(message, NULL);
|
---|
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;
|
---|
86 | }
|
---|
87 |
|
---|
88 | sysarg_t result;
|
---|
89 | async_wait_for(message, &result);
|
---|
90 |
|
---|
91 | return result;
|
---|
92 | }
|
---|
93 |
|
---|
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.
|
---|
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 | int packet_translate_remote(int phone, packet_t **packet, packet_id_t packet_id)
|
---|
110 | {
|
---|
111 | int rc;
|
---|
112 |
|
---|
113 | if (!packet)
|
---|
114 | return EINVAL;
|
---|
115 |
|
---|
116 | *packet = pm_find(packet_id);
|
---|
117 | if (!*packet) {
|
---|
118 | sysarg_t size;
|
---|
119 |
|
---|
120 | rc = async_obsolete_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;
|
---|
127 | }
|
---|
128 | if ((*packet)->next) {
|
---|
129 | packet_t *next;
|
---|
130 |
|
---|
131 | return packet_translate_remote(phone, &next, (*packet)->next);
|
---|
132 | }
|
---|
133 |
|
---|
134 | return EOK;
|
---|
135 | }
|
---|
136 |
|
---|
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.
|
---|
147 | * @return The packet reference.
|
---|
148 | * @return NULL on error.
|
---|
149 | */
|
---|
150 | packet_t *packet_get_4_remote(int phone, size_t max_content, size_t addr_len,
|
---|
151 | size_t max_prefix, size_t max_suffix)
|
---|
152 | {
|
---|
153 | sysarg_t packet_id;
|
---|
154 | sysarg_t size;
|
---|
155 | int rc;
|
---|
156 |
|
---|
157 | rc = async_obsolete_req_4_2(phone, NET_PACKET_CREATE_4, max_content, addr_len,
|
---|
158 | max_prefix, max_suffix, &packet_id, &size);
|
---|
159 | if (rc != EOK)
|
---|
160 | return NULL;
|
---|
161 |
|
---|
162 |
|
---|
163 | packet_t *packet = pm_find(packet_id);
|
---|
164 | if (!packet) {
|
---|
165 | rc = packet_return(phone, &packet, packet_id, size);
|
---|
166 | if (rc != EOK)
|
---|
167 | return NULL;
|
---|
168 | }
|
---|
169 |
|
---|
170 | return packet;
|
---|
171 | }
|
---|
172 |
|
---|
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.
|
---|
179 | * @return The packet reference.
|
---|
180 | * @return NULL on error.
|
---|
181 | */
|
---|
182 | packet_t *packet_get_1_remote(int phone, size_t content)
|
---|
183 | {
|
---|
184 | sysarg_t packet_id;
|
---|
185 | sysarg_t size;
|
---|
186 | int rc;
|
---|
187 |
|
---|
188 | rc = async_obsolete_req_1_2(phone, NET_PACKET_CREATE_1, content, &packet_id,
|
---|
189 | &size);
|
---|
190 | if (rc != EOK)
|
---|
191 | return NULL;
|
---|
192 |
|
---|
193 | packet_t *packet = pm_find(packet_id);
|
---|
194 | if (!packet) {
|
---|
195 | rc = packet_return(phone, &packet, packet_id, size);
|
---|
196 | if (rc != EOK)
|
---|
197 | return NULL;
|
---|
198 | }
|
---|
199 |
|
---|
200 | return packet;
|
---|
201 | }
|
---|
202 |
|
---|
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 | */
|
---|
213 | void pq_release_remote(int phone, packet_id_t packet_id)
|
---|
214 | {
|
---|
215 | async_obsolete_msg_1(phone, NET_PACKET_RELEASE, packet_id);
|
---|
216 | }
|
---|
217 |
|
---|
218 | /** @}
|
---|
219 | */
|
---|