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

lfn serial ticket/834-toolchain-update topic/msim-upgrade topic/simplify-dev-export
Last change on this file since 849ed54 was 849ed54, checked in by Martin Decky <martin@…>, 15 years ago

Networking work:
Split the networking stack into end-user library (libsocket) and two helper libraries (libnet and libnetif).
Don't use over-the-hand compiling and linking, but rather separation of conserns.
There might be still some issues and the non-modular networking architecture is currently broken, but this will be fixed soon.

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