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