source: mainline/uspace/lib/socket/packet/packet_server.c@ e721462

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

Use the standard supported initializers for fibril mutexes and rwlocks.

  • Property mode set to 100644
File size: 10.4 KB
Line 
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 packet
30 * @{
31 */
32
33/** @file
34 * Packet server implementation.
35 */
36
37#include <align.h>
38#include <assert.h>
39#include <async.h>
40#include <errno.h>
41#include <err.h>
42#include <fibril_synch.h>
43#include <unistd.h>
44#include <sys/mman.h>
45
46#include <ipc/ipc.h>
47#include <ipc/packet.h>
48#include <net/packet.h>
49#include <net/packet_header.h>
50#include <packet/packet_server.h>
51
52#include <net_messages.h>
53#include <packet/packet_local.h>
54
55#define FREE_QUEUES_COUNT 7
56
57/** The default address length reserved for new packets.
58 */
59#define DEFAULT_ADDR_LEN 32
60
61/** The default prefix reserved for new packets.
62 */
63#define DEFAULT_PREFIX 64
64
65/** The default suffix reserved for new packets.
66 */
67#define DEFAULT_SUFFIX 64
68
69/** Packet server global data.
70 */
71static struct{
72 /** Safety lock.
73 */
74 fibril_mutex_t lock;
75 /** Free packet queues.
76 */
77 packet_t free[FREE_QUEUES_COUNT];
78 /** Packet length upper bounds of the free packet queues.
79 * The maximal lengths of packets in each queue in the ascending order.
80 * The last queue is not limited.
81 */
82 size_t sizes[FREE_QUEUES_COUNT];
83 /** Total packets allocated.
84 */
85 unsigned int count;
86} ps_globals = {
87 .lock = FIBRIL_MUTEX_INITIALIZER(ps_globals.lock),
88 .free = {NULL, NULL, NULL, NULL, NULL, NULL, NULL},
89 .sizes = {PAGE_SIZE, PAGE_SIZE * 2, PAGE_SIZE * 4, PAGE_SIZE * 8, PAGE_SIZE * 16, PAGE_SIZE * 32, PAGE_SIZE * 64},
90 .count = 0
91};
92
93int packet_translate_local(int phone, packet_ref packet, packet_id_t packet_id)
94{
95 if (!packet)
96 return EINVAL;
97
98 *packet = pm_find(packet_id);
99 return (*packet) ? EOK : ENOENT;
100}
101
102/** Clears and initializes the packet according to the given dimensions.
103 * @param[in] packet The packet to be initialized.
104 * @param[in] addr_len The source and destination addresses maximal length in bytes.
105 * @param[in] max_prefix The maximal prefix length in bytes.
106 * @param[in] max_content The maximal content length in bytes.
107 * @param[in] max_suffix The maximal suffix length in bytes.
108 */
109static void packet_init(packet_t packet, size_t addr_len, size_t max_prefix, size_t max_content, size_t max_suffix){
110 // clear the packet content
111 bzero(((void *) packet) + sizeof(struct packet), packet->length - sizeof(struct packet));
112 // clear the packet header
113 packet->order = 0;
114 packet->metric = 0;
115 packet->previous = 0;
116 packet->next = 0;
117 packet->addr_len = 0;
118 packet->src_addr = sizeof(struct packet);
119 packet->dest_addr = packet->src_addr + addr_len;
120 packet->max_prefix = max_prefix;
121 packet->max_content = max_content;
122 packet->data_start = packet->dest_addr + addr_len + packet->max_prefix;
123 packet->data_end = packet->data_start;
124}
125
126/** Creates a&nbsp;new packet of dimensions at least as given.
127 * Should be used only when the global data are locked.
128 * @param[in] length The total length of the packet, including the header, the addresses and the data of the packet.
129 * @param[in] addr_len The source and destination addresses maximal length in bytes.
130 * @param[in] max_prefix The maximal prefix length in bytes.
131 * @param[in] max_content The maximal content length in bytes.
132 * @param[in] max_suffix The maximal suffix length in bytes.
133 * @returns The packet of dimensions at least as given.
134 * @returns NULL if there is not enough memory left.
135 */
136static packet_t packet_create(size_t length, size_t addr_len, size_t max_prefix, size_t max_content, size_t max_suffix){
137 ERROR_DECLARE;
138
139 packet_t packet;
140
141 // already locked
142 packet = (packet_t) mmap(NULL, length, PROTO_READ | PROTO_WRITE, MAP_SHARED | MAP_ANONYMOUS, 0, 0);
143 if(packet == MAP_FAILED){
144 return NULL;
145 }
146 ++ ps_globals.count;
147 packet->packet_id = ps_globals.count;
148 packet->length = length;
149 packet_init(packet, addr_len, max_prefix, max_content, max_suffix);
150 packet->magic_value = PACKET_MAGIC_VALUE;
151 if(ERROR_OCCURRED(pm_add(packet))){
152 munmap(packet, packet->length);
153 return NULL;
154 }
155 return packet;
156}
157
158/** Return the packet of dimensions at least as given.
159 *
160 * Try to reuse free packets first.
161 * Create a new packet aligned to the memory page size if none available.
162 * Lock the global data during its processing.
163 *
164 * @param[in] addr_len The source and destination addresses
165 * maximal length in bytes.
166 * @param[in] max_prefix The maximal prefix length in bytes.
167 * @param[in] max_content The maximal content length in bytes.
168 * @param[in] max_suffix The maximal suffix length in bytes.
169 *
170 * @return The packet of dimensions at least as given.
171 * @return NULL if there is not enough memory left.
172 *
173 */
174static packet_t packet_get_local(size_t addr_len, size_t max_prefix,
175 size_t max_content, size_t max_suffix)
176{
177 size_t length = ALIGN_UP(sizeof(struct packet) + 2 * addr_len + max_prefix
178 + max_content + max_suffix, PAGE_SIZE);
179
180 fibril_mutex_lock(&ps_globals.lock);
181
182 packet_t packet;
183 unsigned int index;
184
185 for (index = 0; index < FREE_QUEUES_COUNT - 1; index++) {
186 if (length <= ps_globals.sizes[index]) {
187 packet = ps_globals.free[index];
188
189 while (packet_is_valid(packet) && (packet->length < length))
190 packet = pm_find(packet->next);
191
192 if (packet_is_valid(packet)) {
193 if (packet == ps_globals.free[index])
194 ps_globals.free[index] = pq_detach(packet);
195 else
196 pq_detach(packet);
197
198 packet_init(packet, addr_len, max_prefix, max_content,
199 max_suffix);
200 fibril_mutex_unlock(&ps_globals.lock);
201
202 return packet;
203 }
204 }
205 }
206
207 packet = packet_create(length, addr_len, max_prefix, max_content,
208 max_suffix);
209
210 fibril_mutex_unlock(&ps_globals.lock);
211
212 return packet;
213}
214
215packet_t packet_get_4_local(int phone, size_t max_content, size_t addr_len,
216 size_t max_prefix, size_t max_suffix)
217{
218 return packet_get_local(addr_len, max_prefix, max_content, max_suffix);
219}
220
221packet_t packet_get_1_local(int phone, size_t content)
222{
223 return packet_get_local(DEFAULT_ADDR_LEN, DEFAULT_PREFIX, content,
224 DEFAULT_SUFFIX);
225}
226
227/** Release the packet and returns it to the appropriate free packet queue.
228 *
229 * Should be used only when the global data are locked.
230 *
231 * @param[in] packet The packet to be released.
232 *
233 */
234static void packet_release(packet_t packet){
235 int index;
236 int result;
237
238 // remove debug dump
239// printf("packet %d released\n", packet->packet_id);
240 for(index = 0; (index < FREE_QUEUES_COUNT - 1) && (packet->length > ps_globals.sizes[index]); ++ index);
241 result = pq_add(&ps_globals.free[index], packet, packet->length, packet->length);
242 assert(result == EOK);
243}
244
245/** Releases the packet queue.
246 * @param[in] packet_id The first packet identifier.
247 * @returns EOK on success.
248 * @returns ENOENT if there is no such packet.
249 */
250static int packet_release_wrapper(packet_id_t packet_id){
251 packet_t packet;
252
253 packet = pm_find(packet_id);
254 if(! packet_is_valid(packet)){
255 return ENOENT;
256 }
257 fibril_mutex_lock(&ps_globals.lock);
258 pq_destroy(packet, packet_release);
259 fibril_mutex_unlock(&ps_globals.lock);
260 return EOK;
261}
262
263void pq_release_local(int phone, packet_id_t packet_id)
264{
265 (void) packet_release_wrapper(packet_id);
266}
267
268/** Shares the packet memory block.
269 * @param[in] packet The packet to be shared.
270 * @returns EOK on success.
271 * @returns EINVAL if the packet is not valid.
272 * @returns EINVAL if the calling module does not accept the memory.
273 * @returns ENOMEM if the desired and actual sizes differ.
274 * @returns Other error codes as defined for the async_share_in_finalize() function.
275 */
276static int packet_reply(const packet_t packet){
277 ipc_callid_t callid;
278 size_t size;
279
280 if(! packet_is_valid(packet)){
281 return EINVAL;
282 }
283 if(async_share_in_receive(&callid, &size) <= 0) return EINVAL;
284 if(size != packet->length){
285 return ENOMEM;
286 }
287 return async_share_in_finalize(callid, packet, PROTO_READ | PROTO_WRITE);
288}
289
290int packet_server_message(ipc_callid_t callid, ipc_call_t * call, ipc_call_t * answer, int * answer_count){
291 packet_t packet;
292
293 *answer_count = 0;
294 switch(IPC_GET_METHOD(*call)){
295 case IPC_M_PHONE_HUNGUP:
296 return EOK;
297 case NET_PACKET_CREATE_1:
298 packet = packet_get_local(DEFAULT_ADDR_LEN, DEFAULT_PREFIX, IPC_GET_CONTENT(call), DEFAULT_SUFFIX);
299 if(! packet){
300 return ENOMEM;
301 }
302 *answer_count = 2;
303 IPC_SET_ARG1(*answer, (ipcarg_t) packet->packet_id);
304 IPC_SET_ARG2(*answer, (ipcarg_t) packet->length);
305 return EOK;
306 case NET_PACKET_CREATE_4:
307 packet = packet_get_local(((DEFAULT_ADDR_LEN < IPC_GET_ADDR_LEN(call)) ? IPC_GET_ADDR_LEN(call) : DEFAULT_ADDR_LEN), DEFAULT_PREFIX + IPC_GET_PREFIX(call), IPC_GET_CONTENT(call), DEFAULT_SUFFIX + IPC_GET_SUFFIX(call));
308 if(! packet){
309 return ENOMEM;
310 }
311 *answer_count = 2;
312 IPC_SET_ARG1(*answer, (ipcarg_t) packet->packet_id);
313 IPC_SET_ARG2(*answer, (ipcarg_t) packet->length);
314 return EOK;
315 case NET_PACKET_GET:
316 packet = pm_find(IPC_GET_ID(call));
317 if(! packet_is_valid(packet)){
318 return ENOENT;
319 }
320 return packet_reply(packet);
321 case NET_PACKET_GET_SIZE:
322 packet = pm_find(IPC_GET_ID(call));
323 if(! packet_is_valid(packet)){
324 return ENOENT;
325 }
326 IPC_SET_ARG1(*answer, (ipcarg_t) packet->length);
327 *answer_count = 1;
328 return EOK;
329 case NET_PACKET_RELEASE:
330 return packet_release_wrapper(IPC_GET_ID(call));
331 }
332 return ENOTSUP;
333}
334
335/** @}
336 */
Note: See TracBrowser for help on using the repository browser.