[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] 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 | *
|
---|
[14f1db0] | 71 | */
|
---|
[6b82009] | 72 | static int netif_probe_req_local(device_id_t device_id, int irq, void *io)
|
---|
[14f1db0] | 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 | *
|
---|
[774e6d1a] | 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 | *
|
---|
[14f1db0] | 91 | */
|
---|
[6b82009] | 92 | static int netif_send_msg_local(device_id_t device_id, packet_t *packet,
|
---|
| 93 | services_t sender)
|
---|
[14f1db0] | 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 | *
|
---|
[774e6d1a] | 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 | *
|
---|
[14f1db0] | 112 | */
|
---|
[6b82009] | 113 | static int netif_start_req_local(device_id_t device_id)
|
---|
[14f1db0] | 114 | {
|
---|
| 115 | fibril_rwlock_write_lock(&netif_globals.lock);
|
---|
| 116 |
|
---|
| 117 | netif_device_t *device;
|
---|
[774e6d1a] | 118 | int rc = find_device(device_id, &device);
|
---|
[0ab68f6] | 119 | if (rc != EOK) {
|
---|
[14f1db0] | 120 | fibril_rwlock_write_unlock(&netif_globals.lock);
|
---|
[0ab68f6] | 121 | return rc;
|
---|
[14f1db0] | 122 | }
|
---|
| 123 |
|
---|
| 124 | int result = netif_start_message(device);
|
---|
| 125 | if (result > NETIF_NULL) {
|
---|
[6b82009] | 126 | nil_device_state_msg(netif_globals.nil_sess, device_id, result);
|
---|
[4fc2b3b] | 127 | fibril_rwlock_write_unlock(&netif_globals.lock);
|
---|
[14f1db0] | 128 | return EOK;
|
---|
| 129 | }
|
---|
| 130 |
|
---|
| 131 | fibril_rwlock_write_unlock(&netif_globals.lock);
|
---|
| 132 |
|
---|
| 133 | return result;
|
---|
| 134 | }
|
---|
| 135 |
|
---|
| 136 | /** Stop the device.
|
---|
| 137 | *
|
---|
[774e6d1a] | 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 | *
|
---|
[14f1db0] | 146 | */
|
---|
[6b82009] | 147 | static int netif_stop_req_local(device_id_t device_id)
|
---|
[14f1db0] | 148 | {
|
---|
| 149 | fibril_rwlock_write_lock(&netif_globals.lock);
|
---|
| 150 |
|
---|
| 151 | netif_device_t *device;
|
---|
[774e6d1a] | 152 | int rc = find_device(device_id, &device);
|
---|
[0ab68f6] | 153 | if (rc != EOK) {
|
---|
[14f1db0] | 154 | fibril_rwlock_write_unlock(&netif_globals.lock);
|
---|
[0ab68f6] | 155 | return rc;
|
---|
[14f1db0] | 156 | }
|
---|
| 157 |
|
---|
| 158 | int result = netif_stop_message(device);
|
---|
| 159 | if (result > NETIF_NULL) {
|
---|
[6b82009] | 160 | nil_device_state_msg(netif_globals.nil_sess, device_id, result);
|
---|
[4fc2b3b] | 161 | fibril_rwlock_write_unlock(&netif_globals.lock);
|
---|
[14f1db0] | 162 | return EOK;
|
---|
| 163 | }
|
---|
| 164 |
|
---|
| 165 | fibril_rwlock_write_unlock(&netif_globals.lock);
|
---|
| 166 |
|
---|
| 167 | return result;
|
---|
| 168 | }
|
---|
| 169 |
|
---|
[774e6d1a] | 170 | /** Find the device specific data.
|
---|
| 171 | *
|
---|
| 172 | * @param[in] device_id Device identifier.
|
---|
| 173 | * @param[out] device Device specific data.
|
---|
[14f1db0] | 174 | *
|
---|
| 175 | * @return EOK on success.
|
---|
[774e6d1a] | 176 | * @return ENOENT if device is not found.
|
---|
| 177 | * @return EPERM if the device is not initialized.
|
---|
[14f1db0] | 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 | *
|
---|
[774e6d1a] | 197 | * @param[in] stats The usage statistics.
|
---|
| 198 | *
|
---|
[14f1db0] | 199 | */
|
---|
[f772bc55] | 200 | void null_device_stats(device_stats_t *stats)
|
---|
[14f1db0] | 201 | {
|
---|
| 202 | bzero(stats, sizeof(device_stats_t));
|
---|
| 203 | }
|
---|
| 204 |
|
---|
| 205 | /** Release the given packet.
|
---|
| 206 | *
|
---|
| 207 | * Prepared for future optimization.
|
---|
| 208 | *
|
---|
[774e6d1a] | 209 | * @param[in] packet_id The packet identifier.
|
---|
| 210 | *
|
---|
[14f1db0] | 211 | */
|
---|
| 212 | void netif_pq_release(packet_id_t packet_id)
|
---|
| 213 | {
|
---|
[6b82009] | 214 | pq_release_remote(netif_globals.sess, packet_id);
|
---|
[14f1db0] | 215 | }
|
---|
| 216 |
|
---|
| 217 | /** Allocate new packet to handle the given content size.
|
---|
| 218 | *
|
---|
[774e6d1a] | 219 | * @param[in] content Minimum content size.
|
---|
| 220 | *
|
---|
| 221 | * @return The allocated packet.
|
---|
| 222 | * @return NULL on error.
|
---|
[14f1db0] | 223 | *
|
---|
| 224 | */
|
---|
[46d4d9f] | 225 | packet_t *netif_packet_get_1(size_t content)
|
---|
[14f1db0] | 226 | {
|
---|
[6b82009] | 227 | return packet_get_1_remote(netif_globals.sess, content);
|
---|
[14f1db0] | 228 | }
|
---|
| 229 |
|
---|
[ee2fa30a] | 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 |
|
---|
[14f1db0] | 255 | /** Process the netif module messages.
|
---|
| 256 | *
|
---|
[774e6d1a] | 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.
|
---|
[14f1db0] | 266 | *
|
---|
| 267 | * @see IS_NET_NETIF_MESSAGE()
|
---|
| 268 | *
|
---|
| 269 | */
|
---|
[774e6d1a] | 270 | static int netif_module_message(ipc_callid_t callid, ipc_call_t *call,
|
---|
| 271 | ipc_call_t *answer, size_t *count)
|
---|
[14f1db0] | 272 | {
|
---|
| 273 | size_t length;
|
---|
| 274 | device_stats_t stats;
|
---|
[46d4d9f] | 275 | packet_t *packet;
|
---|
[14f1db0] | 276 | measured_string_t address;
|
---|
[0ab68f6] | 277 | int rc;
|
---|
[14f1db0] | 278 |
|
---|
[774e6d1a] | 279 | *count = 0;
|
---|
| 280 |
|
---|
[79ae36dd] | 281 | if (!IPC_GET_IMETHOD(*call))
|
---|
[42a9f27] | 282 | return EOK;
|
---|
| 283 |
|
---|
[ee2fa30a] | 284 | async_sess_t *callback =
|
---|
| 285 | async_callback_receive_start(EXCHANGE_SERIALIZE, call);
|
---|
| 286 | if (callback)
|
---|
| 287 | return register_message(callback);
|
---|
| 288 |
|
---|
[79ae36dd] | 289 | switch (IPC_GET_IMETHOD(*call)) {
|
---|
[42a9f27] | 290 | case NET_NETIF_PROBE:
|
---|
[6b82009] | 291 | return netif_probe_req_local(IPC_GET_DEVICE(*call),
|
---|
[774e6d1a] | 292 | NETIF_GET_IRQ(*call), NETIF_GET_IO(*call));
|
---|
| 293 |
|
---|
[42a9f27] | 294 | case NET_NETIF_SEND:
|
---|
[6b82009] | 295 | rc = packet_translate_remote(netif_globals.sess, &packet,
|
---|
[774e6d1a] | 296 | IPC_GET_PACKET(*call));
|
---|
[0ab68f6] | 297 | if (rc != EOK)
|
---|
| 298 | return rc;
|
---|
| 299 |
|
---|
[6b82009] | 300 | return netif_send_msg_local(IPC_GET_DEVICE(*call), packet,
|
---|
[774e6d1a] | 301 | IPC_GET_SENDER(*call));
|
---|
| 302 |
|
---|
[42a9f27] | 303 | case NET_NETIF_START:
|
---|
[6b82009] | 304 | return netif_start_req_local(IPC_GET_DEVICE(*call));
|
---|
[774e6d1a] | 305 |
|
---|
[42a9f27] | 306 | case NET_NETIF_STATS:
|
---|
| 307 | fibril_rwlock_read_lock(&netif_globals.lock);
|
---|
[774e6d1a] | 308 |
|
---|
[0ab68f6] | 309 | rc = async_data_read_receive(&callid, &length);
|
---|
| 310 | if (rc != EOK) {
|
---|
[14f1db0] | 311 | fibril_rwlock_read_unlock(&netif_globals.lock);
|
---|
[0ab68f6] | 312 | return rc;
|
---|
[42a9f27] | 313 | }
|
---|
[774e6d1a] | 314 |
|
---|
[42a9f27] | 315 | if (length < sizeof(device_stats_t)) {
|
---|
[14f1db0] | 316 | fibril_rwlock_read_unlock(&netif_globals.lock);
|
---|
[42a9f27] | 317 | return EOVERFLOW;
|
---|
| 318 | }
|
---|
[774e6d1a] | 319 |
|
---|
| 320 | rc = netif_get_device_stats(IPC_GET_DEVICE(*call), &stats);
|
---|
[0ab68f6] | 321 | if (rc == EOK) {
|
---|
| 322 | rc = async_data_read_finalize(callid, &stats,
|
---|
[42a9f27] | 323 | sizeof(device_stats_t));
|
---|
| 324 | }
|
---|
[774e6d1a] | 325 |
|
---|
[42a9f27] | 326 | fibril_rwlock_read_unlock(&netif_globals.lock);
|
---|
[0ab68f6] | 327 | return rc;
|
---|
[774e6d1a] | 328 |
|
---|
[42a9f27] | 329 | case NET_NETIF_STOP:
|
---|
[6b82009] | 330 | return netif_stop_req_local(IPC_GET_DEVICE(*call));
|
---|
[774e6d1a] | 331 |
|
---|
[42a9f27] | 332 | case NET_NETIF_GET_ADDR:
|
---|
| 333 | fibril_rwlock_read_lock(&netif_globals.lock);
|
---|
[774e6d1a] | 334 |
|
---|
| 335 | rc = netif_get_addr_message(IPC_GET_DEVICE(*call), &address);
|
---|
[0ab68f6] | 336 | if (rc == EOK)
|
---|
| 337 | rc = measured_strings_reply(&address, 1);
|
---|
[774e6d1a] | 338 |
|
---|
[42a9f27] | 339 | fibril_rwlock_read_unlock(&netif_globals.lock);
|
---|
[0ab68f6] | 340 | return rc;
|
---|
[14f1db0] | 341 | }
|
---|
| 342 |
|
---|
[774e6d1a] | 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 | */
|
---|
[9934f7d] | 352 | static void netif_client_connection(ipc_callid_t iid, ipc_call_t *icall,
|
---|
| 353 | void *arg)
|
---|
[774e6d1a] | 354 | {
|
---|
| 355 | /*
|
---|
| 356 | * Accept the connection by answering
|
---|
| 357 | * the initial IPC_M_CONNECT_ME_TO call.
|
---|
| 358 | */
|
---|
[ffa2c8ef] | 359 | async_answer_0(iid, EOK);
|
---|
[774e6d1a] | 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 */
|
---|
[79ae36dd] | 376 | if ((!IPC_GET_IMETHOD(call)) || (res == EHANGUP))
|
---|
[774e6d1a] | 377 | return;
|
---|
| 378 |
|
---|
| 379 | /* Answer the message */
|
---|
| 380 | answer_call(callid, res, &answer, count);
|
---|
| 381 | }
|
---|
[14f1db0] | 382 | }
|
---|
| 383 |
|
---|
| 384 | /** Start the network interface module.
|
---|
| 385 | *
|
---|
[42a9f27] | 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.
|
---|
[14f1db0] | 389 | *
|
---|
[774e6d1a] | 390 | * @return EOK on success.
|
---|
| 391 | * @return Other error codes as defined for each specific module
|
---|
| 392 | * message function.
|
---|
| 393 | *
|
---|
[14f1db0] | 394 | */
|
---|
[ee2fa30a] | 395 | int netif_module_start(void)
|
---|
[14f1db0] | 396 | {
|
---|
[774e6d1a] | 397 | async_set_client_connection(netif_client_connection);
|
---|
[14f1db0] | 398 |
|
---|
[6b82009] | 399 | netif_globals.sess = connect_to_service(SERVICE_NETWORKING);
|
---|
[ee2fa30a] | 400 | netif_globals.nil_sess = NULL;
|
---|
[774e6d1a] | 401 | netif_device_map_initialize(&netif_globals.device_map);
|
---|
| 402 |
|
---|
| 403 | int rc = pm_init();
|
---|
[0ab68f6] | 404 | if (rc != EOK)
|
---|
| 405 | return rc;
|
---|
[14f1db0] | 406 |
|
---|
[774e6d1a] | 407 | fibril_rwlock_initialize(&netif_globals.lock);
|
---|
| 408 |
|
---|
| 409 | rc = netif_initialize();
|
---|
| 410 | if (rc != EOK) {
|
---|
| 411 | pm_destroy();
|
---|
| 412 | return rc;
|
---|
| 413 | }
|
---|
| 414 |
|
---|
[14f1db0] | 415 | async_manager();
|
---|
| 416 |
|
---|
| 417 | pm_destroy();
|
---|
| 418 | return EOK;
|
---|
| 419 | }
|
---|
| 420 |
|
---|
| 421 | /** @}
|
---|
| 422 | */
|
---|