source: mainline/uspace/lib/net/netif/netif_skel.c@ fe8dfa6

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

networking: streamline NIL skeleton and NIL modules (nildummy, eth)

  • Property mode set to 100644
File size: 10.9 KB
RevLine 
[14f1db0]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
[774e6d1a]29/** @addtogroup libnet
[14f1db0]30 * @{
31 */
32
33/** @file
34 * Network interface module skeleton implementation.
[fe8dfa6]35 * @see netif_skel.h
[14f1db0]36 */
37
38#include <async.h>
39#include <mem.h>
40#include <fibril_synch.h>
41#include <stdio.h>
42#include <ipc/ipc.h>
43#include <ipc/services.h>
[995689d1]44#include <ipc/netif.h>
[0ab68f6]45#include <errno.h>
[14f1db0]46
[514ee46]47#include <generic.h>
[c7a8442]48#include <net/modules.h>
[c69d327]49#include <net/packet.h>
[0a866eeb]50#include <packet_client.h>
[14f1db0]51#include <packet_remote.h>
52#include <adt/measured_strings.h>
[e526f08]53#include <net/device.h>
[774e6d1a]54#include <netif_skel.h>
[fe8dfa6]55#include <nil_remote.h>
[14f1db0]56
57DEVICE_MAP_IMPLEMENT(netif_device_map, netif_device_t);
58
[42a9f27]59/** Network interface global data. */
[14f1db0]60netif_globals_t netif_globals;
61
62/** Probe the existence of the device.
63 *
[774e6d1a]64 * @param[in] netif_phone Network interface phone.
65 * @param[in] device_id Device identifier.
66 * @param[in] irq Device interrupt number.
67 * @param[in] io Device input/output address.
68 *
69 * @return EOK on success.
70 * @return Other error codes as defined for the
71 * netif_probe_message().
72 *
[14f1db0]73 */
[774e6d1a]74static int netif_probe_req_local(int netif_phone, device_id_t device_id,
75 int irq, void *io)
[14f1db0]76{
77 fibril_rwlock_write_lock(&netif_globals.lock);
78 int result = netif_probe_message(device_id, irq, io);
79 fibril_rwlock_write_unlock(&netif_globals.lock);
80
81 return result;
82}
83
84/** Send the packet queue.
85 *
[774e6d1a]86 * @param[in] netif_phone Network interface phone.
87 * @param[in] device_id Device identifier.
88 * @param[in] packet Packet queue.
89 * @param[in] sender Sending module service.
90 *
91 * @return EOK on success.
92 * @return Other error codes as defined for the generic_send_msg()
93 * function.
94 *
[14f1db0]95 */
[774e6d1a]96static int netif_send_msg_local(int netif_phone, device_id_t device_id,
[46d4d9f]97 packet_t *packet, services_t sender)
[14f1db0]98{
99 fibril_rwlock_write_lock(&netif_globals.lock);
100 int result = netif_send_message(device_id, packet, sender);
101 fibril_rwlock_write_unlock(&netif_globals.lock);
102
103 return result;
104}
105
106/** Start the device.
107 *
[774e6d1a]108 * @param[in] netif_phone Network interface phone.
109 * @param[in] device_id Device identifier.
110 *
111 * @return EOK on success.
112 * @return Other error codes as defined for the find_device()
113 * function.
114 * @return Other error codes as defined for the
115 * netif_start_message() function.
116 *
[14f1db0]117 */
[774e6d1a]118static int netif_start_req_local(int netif_phone, device_id_t device_id)
[14f1db0]119{
120 fibril_rwlock_write_lock(&netif_globals.lock);
121
122 netif_device_t *device;
[774e6d1a]123 int rc = find_device(device_id, &device);
[0ab68f6]124 if (rc != EOK) {
[14f1db0]125 fibril_rwlock_write_unlock(&netif_globals.lock);
[0ab68f6]126 return rc;
[14f1db0]127 }
128
129 int result = netif_start_message(device);
130 if (result > NETIF_NULL) {
131 int phone = device->nil_phone;
132 nil_device_state_msg(phone, device_id, result);
[4fc2b3b]133 fibril_rwlock_write_unlock(&netif_globals.lock);
[14f1db0]134 return EOK;
135 }
136
137 fibril_rwlock_write_unlock(&netif_globals.lock);
138
139 return result;
140}
141
142/** Stop the device.
143 *
[774e6d1a]144 * @param[in] netif_phone Network interface phone.
145 * @param[in] device_id Device identifier.
146 *
147 * @return EOK on success.
148 * @return Other error codes as defined for the find_device()
149 * function.
150 * @return Other error codes as defined for the
151 * netif_stop_message() function.
152 *
[14f1db0]153 */
[774e6d1a]154static int netif_stop_req_local(int netif_phone, device_id_t device_id)
[14f1db0]155{
156 fibril_rwlock_write_lock(&netif_globals.lock);
157
158 netif_device_t *device;
[774e6d1a]159 int rc = find_device(device_id, &device);
[0ab68f6]160 if (rc != EOK) {
[14f1db0]161 fibril_rwlock_write_unlock(&netif_globals.lock);
[0ab68f6]162 return rc;
[14f1db0]163 }
164
165 int result = netif_stop_message(device);
166 if (result > NETIF_NULL) {
167 int phone = device->nil_phone;
168 nil_device_state_msg(phone, device_id, result);
[4fc2b3b]169 fibril_rwlock_write_unlock(&netif_globals.lock);
[14f1db0]170 return EOK;
171 }
172
173 fibril_rwlock_write_unlock(&netif_globals.lock);
174
175 return result;
176}
177
[774e6d1a]178/** Find the device specific data.
179 *
180 * @param[in] device_id Device identifier.
181 * @param[out] device Device specific data.
[14f1db0]182 *
183 * @return EOK on success.
[774e6d1a]184 * @return ENOENT if device is not found.
185 * @return EPERM if the device is not initialized.
[14f1db0]186 *
187 */
188int find_device(device_id_t device_id, netif_device_t **device)
189{
190 if (!device)
191 return EBADMEM;
192
193 *device = netif_device_map_find(&netif_globals.device_map, device_id);
194 if (*device == NULL)
195 return ENOENT;
196
197 if ((*device)->state == NETIF_NULL)
198 return EPERM;
199
200 return EOK;
201}
202
203/** Clear the usage statistics.
204 *
[774e6d1a]205 * @param[in] stats The usage statistics.
206 *
[14f1db0]207 */
[f772bc55]208void null_device_stats(device_stats_t *stats)
[14f1db0]209{
210 bzero(stats, sizeof(device_stats_t));
211}
212
213/** Release the given packet.
214 *
215 * Prepared for future optimization.
216 *
[774e6d1a]217 * @param[in] packet_id The packet identifier.
218 *
[14f1db0]219 */
220void netif_pq_release(packet_id_t packet_id)
221{
222 pq_release_remote(netif_globals.net_phone, packet_id);
223}
224
225/** Allocate new packet to handle the given content size.
226 *
[774e6d1a]227 * @param[in] content Minimum content size.
228 *
229 * @return The allocated packet.
230 * @return NULL on error.
[14f1db0]231 *
232 */
[46d4d9f]233packet_t *netif_packet_get_1(size_t content)
[14f1db0]234{
235 return packet_get_1_remote(netif_globals.net_phone, content);
236}
237
[774e6d1a]238/** Register the device notification receiver,
239 *
240 * Register a network interface layer module as the device
241 * notification receiver.
242 *
243 * @param[in] device_id Device identifier.
244 * @param[in] phone Network interface layer module phone.
245 *
246 * @return EOK on success.
247 * @return ENOENT if there is no such device.
248 * @return ELIMIT if there is another module registered.
249 *
[14f1db0]250 */
[774e6d1a]251static int register_message(device_id_t device_id, int phone)
[14f1db0]252{
253 netif_device_t *device;
[774e6d1a]254 int rc = find_device(device_id, &device);
[0ab68f6]255 if (rc != EOK)
256 return rc;
257
[774e6d1a]258 if (device->nil_phone >= 0)
[14f1db0]259 return ELIMIT;
260
261 device->nil_phone = phone;
262 return EOK;
263}
264
265/** Process the netif module messages.
266 *
[774e6d1a]267 * @param[in] callid Mmessage identifier.
268 * @param[in] call Message.
269 * @param[out] answer Answer.
270 * @param[out] count Number of arguments of the answer.
271 *
272 * @return EOK on success.
273 * @return ENOTSUP if the message is unknown.
274 * @return Other error codes as defined for each specific module
275 * message function.
[14f1db0]276 *
277 * @see IS_NET_NETIF_MESSAGE()
278 *
279 */
[774e6d1a]280static int netif_module_message(ipc_callid_t callid, ipc_call_t *call,
281 ipc_call_t *answer, size_t *count)
[14f1db0]282{
283 size_t length;
284 device_stats_t stats;
[46d4d9f]285 packet_t *packet;
[14f1db0]286 measured_string_t address;
[0ab68f6]287 int rc;
[14f1db0]288
[774e6d1a]289 *count = 0;
290
[228e490]291 switch (IPC_GET_IMETHOD(*call)) {
[42a9f27]292 case IPC_M_PHONE_HUNGUP:
293 return EOK;
294
295 case NET_NETIF_PROBE:
[774e6d1a]296 return netif_probe_req_local(0, IPC_GET_DEVICE(*call),
297 NETIF_GET_IRQ(*call), NETIF_GET_IO(*call));
298
[42a9f27]299 case IPC_M_CONNECT_TO_ME:
300 fibril_rwlock_write_lock(&netif_globals.lock);
[774e6d1a]301
302 rc = register_message(IPC_GET_DEVICE(*call), IPC_GET_PHONE(*call));
303
[42a9f27]304 fibril_rwlock_write_unlock(&netif_globals.lock);
[0ab68f6]305 return rc;
[774e6d1a]306
[42a9f27]307 case NET_NETIF_SEND:
[0ab68f6]308 rc = packet_translate_remote(netif_globals.net_phone, &packet,
[774e6d1a]309 IPC_GET_PACKET(*call));
[0ab68f6]310 if (rc != EOK)
311 return rc;
312
[774e6d1a]313 return netif_send_msg_local(0, IPC_GET_DEVICE(*call), packet,
314 IPC_GET_SENDER(*call));
315
[42a9f27]316 case NET_NETIF_START:
[774e6d1a]317 return netif_start_req_local(0, IPC_GET_DEVICE(*call));
318
[42a9f27]319 case NET_NETIF_STATS:
320 fibril_rwlock_read_lock(&netif_globals.lock);
[774e6d1a]321
[0ab68f6]322 rc = async_data_read_receive(&callid, &length);
323 if (rc != EOK) {
[14f1db0]324 fibril_rwlock_read_unlock(&netif_globals.lock);
[0ab68f6]325 return rc;
[42a9f27]326 }
[774e6d1a]327
[42a9f27]328 if (length < sizeof(device_stats_t)) {
[14f1db0]329 fibril_rwlock_read_unlock(&netif_globals.lock);
[42a9f27]330 return EOVERFLOW;
331 }
[774e6d1a]332
333 rc = netif_get_device_stats(IPC_GET_DEVICE(*call), &stats);
[0ab68f6]334 if (rc == EOK) {
335 rc = async_data_read_finalize(callid, &stats,
[42a9f27]336 sizeof(device_stats_t));
337 }
[774e6d1a]338
[42a9f27]339 fibril_rwlock_read_unlock(&netif_globals.lock);
[0ab68f6]340 return rc;
[774e6d1a]341
[42a9f27]342 case NET_NETIF_STOP:
[774e6d1a]343 return netif_stop_req_local(0, IPC_GET_DEVICE(*call));
344
[42a9f27]345 case NET_NETIF_GET_ADDR:
346 fibril_rwlock_read_lock(&netif_globals.lock);
[774e6d1a]347
348 rc = netif_get_addr_message(IPC_GET_DEVICE(*call), &address);
[0ab68f6]349 if (rc == EOK)
350 rc = measured_strings_reply(&address, 1);
[774e6d1a]351
[42a9f27]352 fibril_rwlock_read_unlock(&netif_globals.lock);
[0ab68f6]353 return rc;
[14f1db0]354 }
355
[774e6d1a]356 return netif_specific_message(callid, call, answer, count);
357}
358
359/** Default fibril for new module connections.
360 *
361 * @param[in] iid Initial message identifier.
362 * @param[in] icall Initial message call structure.
363 *
364 */
365static void netif_client_connection(ipc_callid_t iid, ipc_call_t *icall)
366{
367 /*
368 * Accept the connection by answering
369 * the initial IPC_M_CONNECT_ME_TO call.
370 */
371 ipc_answer_0(iid, EOK);
372
373 while (true) {
374 ipc_call_t answer;
375 size_t count;
376
377 /* Clear the answer structure */
378 refresh_answer(&answer, &count);
379
380 /* Fetch the next message */
381 ipc_call_t call;
382 ipc_callid_t callid = async_get_call(&call);
383
384 /* Process the message */
385 int res = netif_module_message(callid, &call, &answer, &count);
386
387 /* End if said to either by the message or the processing result */
388 if ((IPC_GET_IMETHOD(call) == IPC_M_PHONE_HUNGUP) ||
389 (res == EHANGUP))
390 return;
391
392 /* Answer the message */
393 answer_call(callid, res, &answer, count);
394 }
[14f1db0]395}
396
397/** Start the network interface module.
398 *
[42a9f27]399 * Initialize the client connection serving function, initialize the module,
400 * registers the module service and start the async manager, processing IPC
401 * messages in an infinite loop.
[14f1db0]402 *
[774e6d1a]403 * @return EOK on success.
404 * @return Other error codes as defined for each specific module
405 * message function.
406 *
[14f1db0]407 */
[774e6d1a]408int netif_module_start(void)
[14f1db0]409{
[774e6d1a]410 async_set_client_connection(netif_client_connection);
[14f1db0]411
[774e6d1a]412 netif_globals.net_phone = connect_to_service(SERVICE_NETWORKING);
413 netif_device_map_initialize(&netif_globals.device_map);
414
415 int rc = pm_init();
[0ab68f6]416 if (rc != EOK)
417 return rc;
[14f1db0]418
[774e6d1a]419 fibril_rwlock_initialize(&netif_globals.lock);
420
421 rc = netif_initialize();
422 if (rc != EOK) {
423 pm_destroy();
424 return rc;
425 }
426
[14f1db0]427 async_manager();
428
429 pm_destroy();
430 return EOK;
431}
432
433/** @}
434 */
Note: See TracBrowser for help on using the repository browser.