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

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

networking stack: convert to the new async framework

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