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 <errno.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_skel.h>
|
---|
56 |
|
---|
57 | DEVICE_MAP_IMPLEMENT(netif_device_map, netif_device_t);
|
---|
58 |
|
---|
59 | /** Network interface global data. */
|
---|
60 | netif_globals_t netif_globals;
|
---|
61 |
|
---|
62 | /** Probe the existence of the device.
|
---|
63 | *
|
---|
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 | *
|
---|
73 | */
|
---|
74 | static int netif_probe_req_local(int netif_phone, device_id_t device_id,
|
---|
75 | int irq, void *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 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 | *
|
---|
95 | */
|
---|
96 | static int netif_send_msg_local(int netif_phone, device_id_t device_id,
|
---|
97 | packet_t *packet, services_t sender)
|
---|
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 | *
|
---|
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 | *
|
---|
117 | */
|
---|
118 | static int netif_start_req_local(int netif_phone, device_id_t device_id)
|
---|
119 | {
|
---|
120 | fibril_rwlock_write_lock(&netif_globals.lock);
|
---|
121 |
|
---|
122 | netif_device_t *device;
|
---|
123 | int rc = find_device(device_id, &device);
|
---|
124 | if (rc != EOK) {
|
---|
125 | fibril_rwlock_write_unlock(&netif_globals.lock);
|
---|
126 | return rc;
|
---|
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);
|
---|
133 | fibril_rwlock_write_unlock(&netif_globals.lock);
|
---|
134 | return EOK;
|
---|
135 | }
|
---|
136 |
|
---|
137 | fibril_rwlock_write_unlock(&netif_globals.lock);
|
---|
138 |
|
---|
139 | return result;
|
---|
140 | }
|
---|
141 |
|
---|
142 | /** Stop the device.
|
---|
143 | *
|
---|
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 | *
|
---|
153 | */
|
---|
154 | static int netif_stop_req_local(int netif_phone, device_id_t device_id)
|
---|
155 | {
|
---|
156 | fibril_rwlock_write_lock(&netif_globals.lock);
|
---|
157 |
|
---|
158 | netif_device_t *device;
|
---|
159 | int rc = find_device(device_id, &device);
|
---|
160 | if (rc != EOK) {
|
---|
161 | fibril_rwlock_write_unlock(&netif_globals.lock);
|
---|
162 | return rc;
|
---|
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);
|
---|
169 | fibril_rwlock_write_unlock(&netif_globals.lock);
|
---|
170 | return EOK;
|
---|
171 | }
|
---|
172 |
|
---|
173 | fibril_rwlock_write_unlock(&netif_globals.lock);
|
---|
174 |
|
---|
175 | return result;
|
---|
176 | }
|
---|
177 |
|
---|
178 | /** Find the device specific data.
|
---|
179 | *
|
---|
180 | * @param[in] device_id Device identifier.
|
---|
181 | * @param[out] device Device specific data.
|
---|
182 | *
|
---|
183 | * @return EOK on success.
|
---|
184 | * @return ENOENT if device is not found.
|
---|
185 | * @return EPERM if the device is not initialized.
|
---|
186 | *
|
---|
187 | */
|
---|
188 | int 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 | *
|
---|
205 | * @param[in] stats The usage statistics.
|
---|
206 | *
|
---|
207 | */
|
---|
208 | void null_device_stats(device_stats_t *stats)
|
---|
209 | {
|
---|
210 | bzero(stats, sizeof(device_stats_t));
|
---|
211 | }
|
---|
212 |
|
---|
213 | /** Release the given packet.
|
---|
214 | *
|
---|
215 | * Prepared for future optimization.
|
---|
216 | *
|
---|
217 | * @param[in] packet_id The packet identifier.
|
---|
218 | *
|
---|
219 | */
|
---|
220 | void 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 | *
|
---|
227 | * @param[in] content Minimum content size.
|
---|
228 | *
|
---|
229 | * @return The allocated packet.
|
---|
230 | * @return NULL on error.
|
---|
231 | *
|
---|
232 | */
|
---|
233 | packet_t *netif_packet_get_1(size_t content)
|
---|
234 | {
|
---|
235 | return packet_get_1_remote(netif_globals.net_phone, content);
|
---|
236 | }
|
---|
237 |
|
---|
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 | *
|
---|
250 | */
|
---|
251 | static int register_message(device_id_t device_id, int phone)
|
---|
252 | {
|
---|
253 | netif_device_t *device;
|
---|
254 | int rc = find_device(device_id, &device);
|
---|
255 | if (rc != EOK)
|
---|
256 | return rc;
|
---|
257 |
|
---|
258 | if (device->nil_phone >= 0)
|
---|
259 | return ELIMIT;
|
---|
260 |
|
---|
261 | device->nil_phone = phone;
|
---|
262 | return EOK;
|
---|
263 | }
|
---|
264 |
|
---|
265 | /** Process the netif module messages.
|
---|
266 | *
|
---|
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.
|
---|
276 | *
|
---|
277 | * @see IS_NET_NETIF_MESSAGE()
|
---|
278 | *
|
---|
279 | */
|
---|
280 | static int netif_module_message(ipc_callid_t callid, ipc_call_t *call,
|
---|
281 | ipc_call_t *answer, size_t *count)
|
---|
282 | {
|
---|
283 | size_t length;
|
---|
284 | device_stats_t stats;
|
---|
285 | packet_t *packet;
|
---|
286 | measured_string_t address;
|
---|
287 | int rc;
|
---|
288 |
|
---|
289 | *count = 0;
|
---|
290 |
|
---|
291 | switch (IPC_GET_IMETHOD(*call)) {
|
---|
292 | case IPC_M_PHONE_HUNGUP:
|
---|
293 | return EOK;
|
---|
294 |
|
---|
295 | case NET_NETIF_PROBE:
|
---|
296 | return netif_probe_req_local(0, IPC_GET_DEVICE(*call),
|
---|
297 | NETIF_GET_IRQ(*call), NETIF_GET_IO(*call));
|
---|
298 |
|
---|
299 | case IPC_M_CONNECT_TO_ME:
|
---|
300 | fibril_rwlock_write_lock(&netif_globals.lock);
|
---|
301 |
|
---|
302 | rc = register_message(IPC_GET_DEVICE(*call), IPC_GET_PHONE(*call));
|
---|
303 |
|
---|
304 | fibril_rwlock_write_unlock(&netif_globals.lock);
|
---|
305 | return rc;
|
---|
306 |
|
---|
307 | case NET_NETIF_SEND:
|
---|
308 | rc = packet_translate_remote(netif_globals.net_phone, &packet,
|
---|
309 | IPC_GET_PACKET(*call));
|
---|
310 | if (rc != EOK)
|
---|
311 | return rc;
|
---|
312 |
|
---|
313 | return netif_send_msg_local(0, IPC_GET_DEVICE(*call), packet,
|
---|
314 | IPC_GET_SENDER(*call));
|
---|
315 |
|
---|
316 | case NET_NETIF_START:
|
---|
317 | return netif_start_req_local(0, IPC_GET_DEVICE(*call));
|
---|
318 |
|
---|
319 | case NET_NETIF_STATS:
|
---|
320 | fibril_rwlock_read_lock(&netif_globals.lock);
|
---|
321 |
|
---|
322 | rc = async_data_read_receive(&callid, &length);
|
---|
323 | if (rc != EOK) {
|
---|
324 | fibril_rwlock_read_unlock(&netif_globals.lock);
|
---|
325 | return rc;
|
---|
326 | }
|
---|
327 |
|
---|
328 | if (length < sizeof(device_stats_t)) {
|
---|
329 | fibril_rwlock_read_unlock(&netif_globals.lock);
|
---|
330 | return EOVERFLOW;
|
---|
331 | }
|
---|
332 |
|
---|
333 | rc = netif_get_device_stats(IPC_GET_DEVICE(*call), &stats);
|
---|
334 | if (rc == EOK) {
|
---|
335 | rc = async_data_read_finalize(callid, &stats,
|
---|
336 | sizeof(device_stats_t));
|
---|
337 | }
|
---|
338 |
|
---|
339 | fibril_rwlock_read_unlock(&netif_globals.lock);
|
---|
340 | return rc;
|
---|
341 |
|
---|
342 | case NET_NETIF_STOP:
|
---|
343 | return netif_stop_req_local(0, IPC_GET_DEVICE(*call));
|
---|
344 |
|
---|
345 | case NET_NETIF_GET_ADDR:
|
---|
346 | fibril_rwlock_read_lock(&netif_globals.lock);
|
---|
347 |
|
---|
348 | rc = netif_get_addr_message(IPC_GET_DEVICE(*call), &address);
|
---|
349 | if (rc == EOK)
|
---|
350 | rc = measured_strings_reply(&address, 1);
|
---|
351 |
|
---|
352 | fibril_rwlock_read_unlock(&netif_globals.lock);
|
---|
353 | return rc;
|
---|
354 | }
|
---|
355 |
|
---|
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 | */
|
---|
365 | static 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 | }
|
---|
395 | }
|
---|
396 |
|
---|
397 | /** Start the network interface module.
|
---|
398 | *
|
---|
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.
|
---|
402 | *
|
---|
403 | * @return EOK on success.
|
---|
404 | * @return Other error codes as defined for each specific module
|
---|
405 | * message function.
|
---|
406 | *
|
---|
407 | */
|
---|
408 | int netif_module_start(void)
|
---|
409 | {
|
---|
410 | async_set_client_connection(netif_client_connection);
|
---|
411 |
|
---|
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();
|
---|
416 | if (rc != EOK)
|
---|
417 | return rc;
|
---|
418 |
|
---|
419 | fibril_rwlock_initialize(&netif_globals.lock);
|
---|
420 |
|
---|
421 | rc = netif_initialize();
|
---|
422 | if (rc != EOK) {
|
---|
423 | pm_destroy();
|
---|
424 | return rc;
|
---|
425 | }
|
---|
426 |
|
---|
427 | async_manager();
|
---|
428 |
|
---|
429 | pm_destroy();
|
---|
430 | return EOK;
|
---|
431 | }
|
---|
432 |
|
---|
433 | /** @}
|
---|
434 | */
|
---|