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

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

Move network interface common module messages definitions to standard library.

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