source: mainline/uspace/srv/net/structures/packet/packet_server.c@ 3db8889

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