source: mainline/uspace/lib/net/netif/netif_local.c@ fb9ffb0

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

Cleanup netif local and remote interfaces.

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