source: mainline/uspace/lib/packet/generic/packet_server.c@ c89eefb

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

Rename what remained of libsocket to libpacket.

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