| 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] 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 | */
|
|---|
| 72 | static 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 | */
|
|---|
| 92 | static 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 | */
|
|---|
| 113 | static 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 | */
|
|---|
| 147 | static 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 | */
|
|---|
| 180 | int 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 | */
|
|---|
| 200 | void 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 | */
|
|---|
| 212 | void 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 | */
|
|---|
| 225 | packet_t *netif_packet_get_1(size_t content)
|
|---|
| 226 | {
|
|---|
| 227 | return packet_get_1_remote(netif_globals.sess, content);
|
|---|
| 228 | }
|
|---|
| 229 |
|
|---|
| 230 | /** Register the device notification receiver,
|
|---|
| 231 | *
|
|---|
| 232 | * Register a network interface layer module as the device
|
|---|
| 233 | * notification receiver.
|
|---|
| 234 | *
|
|---|
| 235 | * @param[in] sess Session to the network interface layer module.
|
|---|
| 236 | *
|
|---|
| 237 | * @return EOK on success.
|
|---|
| 238 | * @return ELIMIT if there is another module registered.
|
|---|
| 239 | *
|
|---|
| 240 | */
|
|---|
| 241 | static int register_message(async_sess_t *sess)
|
|---|
| 242 | {
|
|---|
| 243 | fibril_rwlock_write_lock(&netif_globals.lock);
|
|---|
| 244 | if (netif_globals.nil_sess != NULL) {
|
|---|
| 245 | fibril_rwlock_write_unlock(&netif_globals.lock);
|
|---|
| 246 | return ELIMIT;
|
|---|
| 247 | }
|
|---|
| 248 |
|
|---|
| 249 | netif_globals.nil_sess = sess;
|
|---|
| 250 |
|
|---|
| 251 | fibril_rwlock_write_unlock(&netif_globals.lock);
|
|---|
| 252 | return EOK;
|
|---|
| 253 | }
|
|---|
| 254 |
|
|---|
| 255 | /** Process the netif module messages.
|
|---|
| 256 | *
|
|---|
| 257 | * @param[in] callid Mmessage identifier.
|
|---|
| 258 | * @param[in] call Message.
|
|---|
| 259 | * @param[out] answer Answer.
|
|---|
| 260 | * @param[out] count Number of arguments of the answer.
|
|---|
| 261 | *
|
|---|
| 262 | * @return EOK on success.
|
|---|
| 263 | * @return ENOTSUP if the message is unknown.
|
|---|
| 264 | * @return Other error codes as defined for each specific module
|
|---|
| 265 | * message function.
|
|---|
| 266 | *
|
|---|
| 267 | * @see IS_NET_NETIF_MESSAGE()
|
|---|
| 268 | *
|
|---|
| 269 | */
|
|---|
| 270 | static int netif_module_message(ipc_callid_t callid, ipc_call_t *call,
|
|---|
| 271 | ipc_call_t *answer, size_t *count)
|
|---|
| 272 | {
|
|---|
| 273 | size_t length;
|
|---|
| 274 | device_stats_t stats;
|
|---|
| 275 | packet_t *packet;
|
|---|
| 276 | measured_string_t address;
|
|---|
| 277 | int rc;
|
|---|
| 278 |
|
|---|
| 279 | *count = 0;
|
|---|
| 280 |
|
|---|
| 281 | if (!IPC_GET_IMETHOD(*call))
|
|---|
| 282 | return EOK;
|
|---|
| 283 |
|
|---|
| 284 | async_sess_t *callback =
|
|---|
| 285 | async_callback_receive_start(EXCHANGE_SERIALIZE, call);
|
|---|
| 286 | if (callback)
|
|---|
| 287 | return register_message(callback);
|
|---|
| 288 |
|
|---|
| 289 | switch (IPC_GET_IMETHOD(*call)) {
|
|---|
| 290 | case NET_NETIF_PROBE:
|
|---|
| 291 | return netif_probe_req_local(IPC_GET_DEVICE(*call),
|
|---|
| 292 | NETIF_GET_IRQ(*call), NETIF_GET_IO(*call));
|
|---|
| 293 |
|
|---|
| 294 | case NET_NETIF_SEND:
|
|---|
| 295 | rc = packet_translate_remote(netif_globals.sess, &packet,
|
|---|
| 296 | IPC_GET_PACKET(*call));
|
|---|
| 297 | if (rc != EOK)
|
|---|
| 298 | return rc;
|
|---|
| 299 |
|
|---|
| 300 | return netif_send_msg_local(IPC_GET_DEVICE(*call), packet,
|
|---|
| 301 | IPC_GET_SENDER(*call));
|
|---|
| 302 |
|
|---|
| 303 | case NET_NETIF_START:
|
|---|
| 304 | return netif_start_req_local(IPC_GET_DEVICE(*call));
|
|---|
| 305 |
|
|---|
| 306 | case NET_NETIF_STATS:
|
|---|
| 307 | fibril_rwlock_read_lock(&netif_globals.lock);
|
|---|
| 308 |
|
|---|
| 309 | rc = async_data_read_receive(&callid, &length);
|
|---|
| 310 | if (rc != EOK) {
|
|---|
| 311 | fibril_rwlock_read_unlock(&netif_globals.lock);
|
|---|
| 312 | return rc;
|
|---|
| 313 | }
|
|---|
| 314 |
|
|---|
| 315 | if (length < sizeof(device_stats_t)) {
|
|---|
| 316 | fibril_rwlock_read_unlock(&netif_globals.lock);
|
|---|
| 317 | return EOVERFLOW;
|
|---|
| 318 | }
|
|---|
| 319 |
|
|---|
| 320 | rc = netif_get_device_stats(IPC_GET_DEVICE(*call), &stats);
|
|---|
| 321 | if (rc == EOK) {
|
|---|
| 322 | rc = async_data_read_finalize(callid, &stats,
|
|---|
| 323 | sizeof(device_stats_t));
|
|---|
| 324 | }
|
|---|
| 325 |
|
|---|
| 326 | fibril_rwlock_read_unlock(&netif_globals.lock);
|
|---|
| 327 | return rc;
|
|---|
| 328 |
|
|---|
| 329 | case NET_NETIF_STOP:
|
|---|
| 330 | return netif_stop_req_local(IPC_GET_DEVICE(*call));
|
|---|
| 331 |
|
|---|
| 332 | case NET_NETIF_GET_ADDR:
|
|---|
| 333 | fibril_rwlock_read_lock(&netif_globals.lock);
|
|---|
| 334 |
|
|---|
| 335 | rc = netif_get_addr_message(IPC_GET_DEVICE(*call), &address);
|
|---|
| 336 | if (rc == EOK)
|
|---|
| 337 | rc = measured_strings_reply(&address, 1);
|
|---|
| 338 |
|
|---|
| 339 | fibril_rwlock_read_unlock(&netif_globals.lock);
|
|---|
| 340 | return rc;
|
|---|
| 341 | }
|
|---|
| 342 |
|
|---|
| 343 | return netif_specific_message(callid, call, answer, count);
|
|---|
| 344 | }
|
|---|
| 345 |
|
|---|
| 346 | /** Default fibril for new module connections.
|
|---|
| 347 | *
|
|---|
| 348 | * @param[in] iid Initial message identifier.
|
|---|
| 349 | * @param[in] icall Initial message call structure.
|
|---|
| 350 | *
|
|---|
| 351 | */
|
|---|
| 352 | static void netif_client_connection(ipc_callid_t iid, ipc_call_t *icall,
|
|---|
| 353 | void *arg)
|
|---|
| 354 | {
|
|---|
| 355 | /*
|
|---|
| 356 | * Accept the connection by answering
|
|---|
| 357 | * the initial IPC_M_CONNECT_ME_TO call.
|
|---|
| 358 | */
|
|---|
| 359 | async_answer_0(iid, EOK);
|
|---|
| 360 |
|
|---|
| 361 | while (true) {
|
|---|
| 362 | ipc_call_t answer;
|
|---|
| 363 | size_t count;
|
|---|
| 364 |
|
|---|
| 365 | /* Clear the answer structure */
|
|---|
| 366 | refresh_answer(&answer, &count);
|
|---|
| 367 |
|
|---|
| 368 | /* Fetch the next message */
|
|---|
| 369 | ipc_call_t call;
|
|---|
| 370 | ipc_callid_t callid = async_get_call(&call);
|
|---|
| 371 |
|
|---|
| 372 | /* Process the message */
|
|---|
| 373 | int res = netif_module_message(callid, &call, &answer, &count);
|
|---|
| 374 |
|
|---|
| 375 | /* End if said to either by the message or the processing result */
|
|---|
| 376 | if ((!IPC_GET_IMETHOD(call)) || (res == EHANGUP))
|
|---|
| 377 | return;
|
|---|
| 378 |
|
|---|
| 379 | /* Answer the message */
|
|---|
| 380 | answer_call(callid, res, &answer, count);
|
|---|
| 381 | }
|
|---|
| 382 | }
|
|---|
| 383 |
|
|---|
| 384 | /** Start the network interface module.
|
|---|
| 385 | *
|
|---|
| 386 | * Initialize the client connection serving function, initialize the module,
|
|---|
| 387 | * registers the module service and start the async manager, processing IPC
|
|---|
| 388 | * messages in an infinite loop.
|
|---|
| 389 | *
|
|---|
| 390 | * @return EOK on success.
|
|---|
| 391 | * @return Other error codes as defined for each specific module
|
|---|
| 392 | * message function.
|
|---|
| 393 | *
|
|---|
| 394 | */
|
|---|
| 395 | int netif_module_start(void)
|
|---|
| 396 | {
|
|---|
| 397 | async_set_client_connection(netif_client_connection);
|
|---|
| 398 |
|
|---|
| 399 | netif_globals.sess = connect_to_service(SERVICE_NETWORKING);
|
|---|
| 400 | netif_globals.nil_sess = NULL;
|
|---|
| 401 | netif_device_map_initialize(&netif_globals.device_map);
|
|---|
| 402 |
|
|---|
| 403 | int rc = pm_init();
|
|---|
| 404 | if (rc != EOK)
|
|---|
| 405 | return rc;
|
|---|
| 406 |
|
|---|
| 407 | fibril_rwlock_initialize(&netif_globals.lock);
|
|---|
| 408 |
|
|---|
| 409 | rc = netif_initialize();
|
|---|
| 410 | if (rc != EOK) {
|
|---|
| 411 | pm_destroy();
|
|---|
| 412 | return rc;
|
|---|
| 413 | }
|
|---|
| 414 |
|
|---|
| 415 | async_manager();
|
|---|
| 416 |
|
|---|
| 417 | pm_destroy();
|
|---|
| 418 | return EOK;
|
|---|
| 419 | }
|
|---|
| 420 |
|
|---|
| 421 | /** @}
|
|---|
| 422 | */
|
|---|