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
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 <ipc/net.h>
49
50#include <net/packet.h>
51#include <net/packet_header.h>
52
53#include <packet_server.h>
54#include <packet_local.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 */
78 packet_t free[FREE_QUEUES_COUNT];
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 */
83 size_t sizes[FREE_QUEUES_COUNT];
84 /** Total packets allocated.
85 */
86 unsigned int count;
87} ps_globals = {
88 .lock = FIBRIL_MUTEX_INITIALIZER(ps_globals.lock),
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},
91 .count = 0
92};
93
94int packet_translate_local(int phone, packet_ref packet, packet_id_t packet_id)
95{
96 if (!packet)
97 return EINVAL;
98
99 *packet = pm_find(packet_id);
100 return (*packet) ? EOK : ENOENT;
101}
102
103/** Clears and initializes the packet according to the given dimensions.
104 * @param[in] packet The packet to be initialized.
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 */
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}
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 */
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;
139
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
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 *
174 */
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
181 fibril_mutex_lock(&ps_globals.lock);
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]) {
188 packet = ps_globals.free[index];
189
190 while (packet_is_valid(packet) && (packet->length < length))
191 packet = pm_find(packet->next);
192
193 if (packet_is_valid(packet)) {
194 if (packet == ps_globals.free[index])
195 ps_globals.free[index] = pq_detach(packet);
196 else
197 pq_detach(packet);
198
199 packet_init(packet, addr_len, max_prefix, max_content,
200 max_suffix);
201 fibril_mutex_unlock(&ps_globals.lock);
202
203 return packet;
204 }
205 }
206 }
207
208 packet = packet_create(length, addr_len, max_prefix, max_content,
209 max_suffix);
210
211 fibril_mutex_unlock(&ps_globals.lock);
212
213 return packet;
214}
215
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);
220}
221
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);
226}
227
228/** Release the packet and returns it to the appropriate free packet queue.
229 *
230 * Should be used only when the global data are locked.
231 *
232 * @param[in] packet The packet to be released.
233 *
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
264void pq_release_local(int phone, packet_id_t packet_id)
265{
266 (void) packet_release_wrapper(packet_id);
267}
268
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
291int packet_server_message(ipc_callid_t callid, ipc_call_t * call, ipc_call_t * answer, int * answer_count){
292 packet_t packet;
293
294 *answer_count = 0;
295 switch(IPC_GET_METHOD(*call)){
296 case IPC_M_PHONE_HUNGUP:
297 return EOK;
298 case NET_PACKET_CREATE_1:
299 packet = packet_get_local(DEFAULT_ADDR_LEN, DEFAULT_PREFIX, IPC_GET_CONTENT(call), DEFAULT_SUFFIX);
300 if(! packet){
301 return ENOMEM;
302 }
303 *answer_count = 2;
304 IPC_SET_ARG1(*answer, (ipcarg_t) packet->packet_id);
305 IPC_SET_ARG2(*answer, (ipcarg_t) packet->length);
306 return EOK;
307 case NET_PACKET_CREATE_4:
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));
309 if(! packet){
310 return ENOMEM;
311 }
312 *answer_count = 2;
313 IPC_SET_ARG1(*answer, (ipcarg_t) packet->packet_id);
314 IPC_SET_ARG2(*answer, (ipcarg_t) packet->length);
315 return EOK;
316 case NET_PACKET_GET:
317 packet = pm_find(IPC_GET_ID(call));
318 if(! packet_is_valid(packet)){
319 return ENOENT;
320 }
321 return packet_reply(packet);
322 case NET_PACKET_GET_SIZE:
323 packet = pm_find(IPC_GET_ID(call));
324 if(! packet_is_valid(packet)){
325 return ENOENT;
326 }
327 IPC_SET_ARG1(*answer, (ipcarg_t) packet->length);
328 *answer_count = 1;
329 return EOK;
330 case NET_PACKET_RELEASE:
331 return packet_release_wrapper(IPC_GET_ID(call));
332 }
333 return ENOTSUP;
334}
335
336/** @}
337 */
Note: See TracBrowser for help on using the repository browser.