source: mainline/uspace/srv/net/nil/nildummy/nildummy.c@ b40bfac

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

Get rid of the ERROR_CODE madness in nildummy.

  • Property mode set to 100644
File size: 13.3 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 nildummy
30 * @{
31 */
32
33/** @file
34 * Dummy network interface layer module implementation.
35 * @see nildummy.h
36 */
37
38#include <async.h>
39#include <malloc.h>
40#include <mem.h>
41#include <stdio.h>
42#include <str.h>
43#include <ipc/ipc.h>
44#include <ipc/net.h>
45#include <ipc/services.h>
46
47#include <net/modules.h>
48#include <net/device.h>
49#include <netif_interface.h>
50#include <nil_interface.h>
51#include <il_interface.h>
52#include <adt/measured_strings.h>
53#include <net/packet.h>
54#include <packet_remote.h>
55#include <nil_local.h>
56
57#include "nildummy.h"
58
59/** The module name. */
60#define NAME "nildummy"
61
62/** Default maximum transmission unit. */
63#define NET_DEFAULT_MTU 1500
64
65/** Network interface layer module global data. */
66nildummy_globals_t nildummy_globals;
67
68DEVICE_MAP_IMPLEMENT(nildummy_devices, nildummy_device_t);
69
70int nil_device_state_msg_local(int nil_phone, device_id_t device_id, int state)
71{
72 fibril_rwlock_read_lock(&nildummy_globals.protos_lock);
73 if (nildummy_globals.proto.phone)
74 il_device_state_msg(nildummy_globals.proto.phone, device_id,
75 state, nildummy_globals.proto.service);
76 fibril_rwlock_read_unlock(&nildummy_globals.protos_lock);
77
78 return EOK;
79}
80
81int nil_initialize(int net_phone)
82{
83 int rc;
84
85 fibril_rwlock_initialize(&nildummy_globals.devices_lock);
86 fibril_rwlock_initialize(&nildummy_globals.protos_lock);
87 fibril_rwlock_write_lock(&nildummy_globals.devices_lock);
88 fibril_rwlock_write_lock(&nildummy_globals.protos_lock);
89
90 nildummy_globals.net_phone = net_phone;
91 nildummy_globals.proto.phone = 0;
92 rc = nildummy_devices_initialize(&nildummy_globals.devices);
93
94 fibril_rwlock_write_unlock(&nildummy_globals.protos_lock);
95 fibril_rwlock_write_unlock(&nildummy_globals.devices_lock);
96
97 return rc;
98}
99
100/** Process IPC messages from the registered device driver modules in an
101 * infinite loop.
102 *
103 * @param[in] iid The message identifier.
104 * @param[in,out] icall The message parameters.
105 */
106static void nildummy_receiver(ipc_callid_t iid, ipc_call_t *icall)
107{
108 packet_t packet;
109 int rc;
110
111 while (true) {
112 switch (IPC_GET_METHOD(*icall)) {
113 case NET_NIL_DEVICE_STATE:
114 rc = nil_device_state_msg_local(0,
115 IPC_GET_DEVICE(icall), IPC_GET_STATE(icall));
116 ipc_answer_0(iid, (ipcarg_t) rc);
117 break;
118
119 case NET_NIL_RECEIVED:
120 rc = packet_translate_remote(nildummy_globals.net_phone,
121 &packet, IPC_GET_PACKET(icall));
122 if (rc == EOK) {
123 rc = nil_received_msg_local(0,
124 IPC_GET_DEVICE(icall), packet, 0);
125 }
126 ipc_answer_0(iid, (ipcarg_t) rc);
127 break;
128
129 default:
130 ipc_answer_0(iid, (ipcarg_t) ENOTSUP);
131 }
132
133 iid = async_get_call(icall);
134 }
135}
136
137/** Register new device or updates the MTU of an existing one.
138 *
139 * Determine the device local hardware address.
140 *
141 * @param[in] device_id The new device identifier.
142 * @param[in] service The device driver service.
143 * @param[in] mtu The device maximum transmission unit.
144 * @returns EOK on success.
145 * @returns EEXIST if the device with the different service exists.
146 * @returns ENOMEM if there is not enough memory left.
147 * @returns Other error codes as defined for the
148 * netif_bind_service() function.
149 * @returns Other error codes as defined for the
150 * netif_get_addr_req() function.
151 */
152static int
153nildummy_device_message(device_id_t device_id, services_t service, size_t mtu)
154{
155 nildummy_device_ref device;
156 int index;
157 int rc;
158
159 fibril_rwlock_write_lock(&nildummy_globals.devices_lock);
160
161 // an existing device?
162 device = nildummy_devices_find(&nildummy_globals.devices, device_id);
163 if (device) {
164 if (device->service != service) {
165 printf("Device %d already exists\n", device->device_id);
166 fibril_rwlock_write_unlock(
167 &nildummy_globals.devices_lock);
168 return EEXIST;
169 }
170
171 // update mtu
172 if (mtu > 0)
173 device->mtu = mtu;
174 else
175 device->mtu = NET_DEFAULT_MTU;
176
177 printf("Device %d already exists:\tMTU\t= %d\n",
178 device->device_id, device->mtu);
179 fibril_rwlock_write_unlock(&nildummy_globals.devices_lock);
180
181 // notify the upper layer module
182 fibril_rwlock_read_lock(&nildummy_globals.protos_lock);
183 if (nildummy_globals.proto.phone) {
184 il_mtu_changed_msg(nildummy_globals.proto.phone,
185 device->device_id, device->mtu,
186 nildummy_globals.proto.service);
187 }
188 fibril_rwlock_read_unlock(&nildummy_globals.protos_lock);
189
190 return EOK;
191 }
192
193 // create a new device
194 device = (nildummy_device_ref) malloc(sizeof(nildummy_device_t));
195 if (!device)
196 return ENOMEM;
197
198 device->device_id = device_id;
199 device->service = service;
200 if (mtu > 0)
201 device->mtu = mtu;
202 else
203 device->mtu = NET_DEFAULT_MTU;
204
205 // bind the device driver
206 device->phone = netif_bind_service(device->service, device->device_id,
207 SERVICE_ETHERNET, nildummy_receiver);
208 if (device->phone < 0) {
209 fibril_rwlock_write_unlock(&nildummy_globals.devices_lock);
210 free(device);
211 return device->phone;
212 }
213
214 // get hardware address
215 rc = netif_get_addr_req(device->phone, device->device_id, &device->addr,
216 &device->addr_data);
217 if (rc != EOK) {
218 fibril_rwlock_write_unlock(&nildummy_globals.devices_lock);
219 free(device);
220 return rc;
221 }
222
223 // add to the cache
224 index = nildummy_devices_add(&nildummy_globals.devices,
225 device->device_id, device);
226 if (index < 0) {
227 fibril_rwlock_write_unlock(&nildummy_globals.devices_lock);
228 free(device->addr);
229 free(device->addr_data);
230 free(device);
231 return index;
232 }
233
234 printf("%s: Device registered (id: %d, service: %d, mtu: %d)\n",
235 NAME, device->device_id, device->service, device->mtu);
236 fibril_rwlock_write_unlock(&nildummy_globals.devices_lock);
237 return EOK;
238}
239
240/** Return the device hardware address.
241 *
242 * @param[in] device_id The device identifier.
243 * @param[out] address The device hardware address.
244 * @return EOK on success.
245 * @return EBADMEM if the address parameter is NULL.
246 * @return ENOENT if there no such device.
247 *
248 */
249static int
250nildummy_addr_message(device_id_t device_id, measured_string_ref *address)
251{
252 nildummy_device_ref device;
253
254 if (!address)
255 return EBADMEM;
256
257 fibril_rwlock_read_lock(&nildummy_globals.devices_lock);
258 device = nildummy_devices_find(&nildummy_globals.devices, device_id);
259 if (!device) {
260 fibril_rwlock_read_unlock(&nildummy_globals.devices_lock);
261 return ENOENT;
262 }
263 *address = device->addr;
264 fibril_rwlock_read_unlock(&nildummy_globals.devices_lock);
265
266 return (*address) ? EOK : ENOENT;
267}
268
269/** Return the device packet dimensions for sending.
270 *
271 * @param[in] device_id The device identifier.
272 * @param[out] addr_len The minimum reserved address length.
273 * @param[out] prefix The minimum reserved prefix size.
274 * @param[out] content The maximum content size.
275 * @param[out] suffix The minimum reserved suffix size.
276 * @return EOK on success.
277 * @return EBADMEM if either one of the parameters is NULL.
278 * @return ENOENT if there is no such device.
279 *
280 */
281static int
282nildummy_packet_space_message(device_id_t device_id, size_t *addr_len,
283 size_t *prefix, size_t *content, size_t *suffix)
284{
285 nildummy_device_ref device;
286
287 if (!addr_len || !prefix || !content || !suffix)
288 return EBADMEM;
289
290 fibril_rwlock_read_lock(&nildummy_globals.devices_lock);
291 device = nildummy_devices_find(&nildummy_globals.devices, device_id);
292 if (!device) {
293 fibril_rwlock_read_unlock(&nildummy_globals.devices_lock);
294 return ENOENT;
295 }
296 *content = device->mtu;
297 fibril_rwlock_read_unlock(&nildummy_globals.devices_lock);
298
299 *addr_len = 0;
300 *prefix = 0;
301 *suffix = 0;
302 return EOK;
303}
304
305int
306nil_received_msg_local(int nil_phone, device_id_t device_id, packet_t packet,
307 services_t target)
308{
309 packet_t next;
310
311 fibril_rwlock_read_lock(&nildummy_globals.protos_lock);
312 if (nildummy_globals.proto.phone) {
313 do {
314 next = pq_detach(packet);
315 il_received_msg(nildummy_globals.proto.phone, device_id,
316 packet, nildummy_globals.proto.service);
317 packet = next;
318 } while(packet);
319 }
320 fibril_rwlock_read_unlock(&nildummy_globals.protos_lock);
321
322 return EOK;
323}
324
325/** Register receiving module service.
326 *
327 * Pass received packets for this service.
328 *
329 * @param[in] service The module service.
330 * @param[in] phone The service phone.
331 * @return EOK on success.
332 * @return ENOENT if the service is not known.
333 * @return ENOMEM if there is not enough memory left.
334 */
335static int nildummy_register_message(services_t service, int phone)
336{
337 fibril_rwlock_write_lock(&nildummy_globals.protos_lock);
338 nildummy_globals.proto.service = service;
339 nildummy_globals.proto.phone = phone;
340
341 printf("%s: Protocol registered (service: %d, phone: %d)\n",
342 NAME, nildummy_globals.proto.service, nildummy_globals.proto.phone);
343
344 fibril_rwlock_write_unlock(&nildummy_globals.protos_lock);
345 return EOK;
346}
347
348/** Send the packet queue.
349 *
350 * @param[in] device_id The device identifier.
351 * @param[in] packet The packet queue.
352 * @param[in] sender The sending module service.
353 * @return EOK on success.
354 * @return ENOENT if there no such device.
355 * @return EINVAL if the service parameter is not known.
356 */
357static int
358nildummy_send_message(device_id_t device_id, packet_t packet, services_t sender)
359{
360 nildummy_device_ref device;
361
362 fibril_rwlock_read_lock(&nildummy_globals.devices_lock);
363 device = nildummy_devices_find(&nildummy_globals.devices, device_id);
364 if (!device) {
365 fibril_rwlock_read_unlock(&nildummy_globals.devices_lock);
366 return ENOENT;
367 }
368 // send packet queue
369 if (packet)
370 netif_send_msg(device->phone, device_id, packet,
371 SERVICE_NILDUMMY);
372 fibril_rwlock_read_unlock(&nildummy_globals.devices_lock);
373 return EOK;
374}
375
376int
377nil_message_standalone(const char *name, ipc_callid_t callid, ipc_call_t *call,
378 ipc_call_t *answer, int *answer_count)
379{
380 measured_string_ref address;
381 packet_t packet;
382 size_t addrlen;
383 size_t prefix;
384 size_t suffix;
385 size_t content;
386 int rc;
387
388 *answer_count = 0;
389 switch (IPC_GET_METHOD(*call)) {
390 case IPC_M_PHONE_HUNGUP:
391 return EOK;
392
393 case NET_NIL_DEVICE:
394 return nildummy_device_message(IPC_GET_DEVICE(call),
395 IPC_GET_SERVICE(call), IPC_GET_MTU(call));
396
397 case NET_NIL_SEND:
398 rc = packet_translate_remote(nildummy_globals.net_phone,
399 &packet, IPC_GET_PACKET(call));
400 if (rc != EOK)
401 return rc;
402 return nildummy_send_message(IPC_GET_DEVICE(call), packet,
403 IPC_GET_SERVICE(call));
404
405 case NET_NIL_PACKET_SPACE:
406 rc = nildummy_packet_space_message(IPC_GET_DEVICE(call),
407 &addrlen, &prefix, &content, &suffix);
408 if (rc != EOK)
409 return rc;
410 IPC_SET_ADDR(answer, addrlen);
411 IPC_SET_PREFIX(answer, prefix);
412 IPC_SET_CONTENT(answer, content);
413 IPC_SET_SUFFIX(answer, suffix);
414 *answer_count = 4;
415 return EOK;
416
417 case NET_NIL_ADDR:
418 rc = nildummy_addr_message(IPC_GET_DEVICE(call), &address);
419 if (rc != EOK)
420 return rc;
421 return measured_strings_reply(address, 1);
422
423 case NET_NIL_BROADCAST_ADDR:
424 rc = nildummy_addr_message(IPC_GET_DEVICE(call), &address);
425 if (rc != EOK)
426 return rc;
427 return measured_strings_reply(address, 1);
428
429 case IPC_M_CONNECT_TO_ME:
430 return nildummy_register_message(NIL_GET_PROTO(call),
431 IPC_GET_PHONE(call));
432 }
433
434 return ENOTSUP;
435}
436
437/** Default thread for new connections.
438 *
439 * @param[in] iid The initial message identifier.
440 * @param[in] icall The initial message call structure.
441 */
442static void nil_client_connection(ipc_callid_t iid, ipc_call_t *icall)
443{
444 /*
445 * Accept the connection
446 * - Answer the first IPC_M_CONNECT_ME_TO call.
447 */
448 ipc_answer_0(iid, EOK);
449
450 while (true) {
451 ipc_call_t answer;
452 int answer_count;
453
454 /* Clear the answer structure */
455 refresh_answer(&answer, &answer_count);
456
457 /* Fetch the next message */
458 ipc_call_t call;
459 ipc_callid_t callid = async_get_call(&call);
460
461 /* Process the message */
462 int res = nil_module_message_standalone(NAME, callid, &call,
463 &answer, &answer_count);
464
465 /*
466 * End if told to either by the message or the processing
467 * result.
468 */
469 if ((IPC_GET_METHOD(call) == IPC_M_PHONE_HUNGUP) ||
470 (res == EHANGUP))
471 return;
472
473 /* Answer the message */
474 answer_call(callid, res, &answer, answer_count);
475 }
476}
477
478int main(int argc, char *argv[])
479{
480 int rc;
481
482 /* Start the module */
483 rc = nil_module_start_standalone(nil_client_connection);
484 return rc;
485}
486
487/** @}
488 */
Note: See TracBrowser for help on using the repository browser.