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