| [21580dd] | 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 lo
|
|---|
| [c4fbf7fd] | 30 | * @{
|
|---|
| [21580dd] | 31 | */
|
|---|
| 32 |
|
|---|
| 33 | /** @file
|
|---|
| [c4fbf7fd] | 34 | * Loopback network interface implementation.
|
|---|
| [21580dd] | 35 | */
|
|---|
| 36 |
|
|---|
| 37 | #include <async.h>
|
|---|
| 38 | #include <errno.h>
|
|---|
| 39 | #include <stdio.h>
|
|---|
| [19f857a] | 40 | #include <str.h>
|
|---|
| [79ae36dd] | 41 | #include <ns.h>
|
|---|
| [21580dd] | 42 | #include <ipc/services.h>
|
|---|
| [f5a3479] | 43 | #include <ipc/nil.h>
|
|---|
| [c7a8442] | 44 | #include <net/modules.h>
|
|---|
| [849ed54] | 45 | #include <adt/measured_strings.h>
|
|---|
| [0a866eeb] | 46 | #include <packet_client.h>
|
|---|
| [e526f08] | 47 | #include <net/device.h>
|
|---|
| [774e6d1a] | 48 | #include <netif_skel.h>
|
|---|
| [fe8dfa6] | 49 | #include <nil_remote.h>
|
|---|
| [21580dd] | 50 |
|
|---|
| [c4fbf7fd] | 51 | /** Default address length. */
|
|---|
| [61bfc370] | 52 | #define DEFAULT_ADDR_LEN 6
|
|---|
| [21580dd] | 53 |
|
|---|
| [c4fbf7fd] | 54 | /** Loopback module name. */
|
|---|
| [24ab58b3] | 55 | #define NAME "lo"
|
|---|
| [21580dd] | 56 |
|
|---|
| [fe8dfa6] | 57 | static uint8_t default_addr[DEFAULT_ADDR_LEN] =
|
|---|
| 58 | {0, 0, 0, 0, 0, 0};
|
|---|
| [21580dd] | 59 |
|
|---|
| [fb04cba8] | 60 | int netif_specific_message(ipc_callid_t callid, ipc_call_t *call,
|
|---|
| [774e6d1a] | 61 | ipc_call_t *answer, size_t *count)
|
|---|
| [c4fbf7fd] | 62 | {
|
|---|
| [21580dd] | 63 | return ENOTSUP;
|
|---|
| 64 | }
|
|---|
| 65 |
|
|---|
| [4eca056] | 66 | int netif_get_addr_message(device_id_t device_id, measured_string_t *address)
|
|---|
| [c4fbf7fd] | 67 | {
|
|---|
| 68 | if (!address)
|
|---|
| [aadf01e] | 69 | return EBADMEM;
|
|---|
| [61bfc370] | 70 |
|
|---|
| [fe8dfa6] | 71 | address->value = default_addr;
|
|---|
| [21580dd] | 72 | address->length = DEFAULT_ADDR_LEN;
|
|---|
| [61bfc370] | 73 |
|
|---|
| [21580dd] | 74 | return EOK;
|
|---|
| 75 | }
|
|---|
| 76 |
|
|---|
| [f772bc55] | 77 | int netif_get_device_stats(device_id_t device_id, device_stats_t *stats)
|
|---|
| [c4fbf7fd] | 78 | {
|
|---|
| 79 | if (!stats)
|
|---|
| [aadf01e] | 80 | return EBADMEM;
|
|---|
| [fe8dfa6] | 81 |
|
|---|
| 82 | netif_device_t *device;
|
|---|
| 83 | int rc = find_device(device_id, &device);
|
|---|
| [2e236901] | 84 | if (rc != EOK)
|
|---|
| 85 | return rc;
|
|---|
| [fe8dfa6] | 86 |
|
|---|
| [f772bc55] | 87 | memcpy(stats, (device_stats_t *) device->specific,
|
|---|
| [c4fbf7fd] | 88 | sizeof(device_stats_t));
|
|---|
| [fe8dfa6] | 89 |
|
|---|
| [21580dd] | 90 | return EOK;
|
|---|
| 91 | }
|
|---|
| 92 |
|
|---|
| [fe8dfa6] | 93 | /** Change the loopback state.
|
|---|
| 94 | *
|
|---|
| 95 | * @param[in] device The device structure.
|
|---|
| 96 | * @param[in] state The new device state.
|
|---|
| 97 | *
|
|---|
| 98 | * @return New state if changed.
|
|---|
| 99 | * @return EOK otherwise.
|
|---|
| [c4fbf7fd] | 100 | *
|
|---|
| 101 | */
|
|---|
| [fe8dfa6] | 102 | static void change_state_message(netif_device_t *device, device_state_t state)
|
|---|
| [24ab58b3] | 103 | {
|
|---|
| 104 | if (device->state != state) {
|
|---|
| [21580dd] | 105 | device->state = state;
|
|---|
| [24ab58b3] | 106 |
|
|---|
| [fe8dfa6] | 107 | const char *desc;
|
|---|
| 108 | switch (state) {
|
|---|
| 109 | case NETIF_ACTIVE:
|
|---|
| 110 | desc = "active";
|
|---|
| 111 | break;
|
|---|
| 112 | case NETIF_STOPPED:
|
|---|
| 113 | desc = "stopped";
|
|---|
| 114 | break;
|
|---|
| 115 | default:
|
|---|
| 116 | desc = "unknown";
|
|---|
| 117 | }
|
|---|
| [24ab58b3] | 118 |
|
|---|
| [fe8dfa6] | 119 | printf("%s: State changed to %s\n", NAME, desc);
|
|---|
| [21580dd] | 120 | }
|
|---|
| 121 | }
|
|---|
| 122 |
|
|---|
| [fe8dfa6] | 123 | /** Create and return the loopback network interface structure.
|
|---|
| 124 | *
|
|---|
| 125 | * @param[in] device_id New devce identifier.
|
|---|
| 126 | * @param[out] device Device structure.
|
|---|
| 127 | *
|
|---|
| 128 | * @return EOK on success.
|
|---|
| 129 | * @return EXDEV if one loopback network interface already exists.
|
|---|
| 130 | * @return ENOMEM if there is not enough memory left.
|
|---|
| [c4fbf7fd] | 131 | *
|
|---|
| 132 | */
|
|---|
| [fe8dfa6] | 133 | static int lo_create(device_id_t device_id, netif_device_t **device)
|
|---|
| [c4fbf7fd] | 134 | {
|
|---|
| 135 | if (netif_device_map_count(&netif_globals.device_map) > 0)
|
|---|
| [21580dd] | 136 | return EXDEV;
|
|---|
| [fe8dfa6] | 137 |
|
|---|
| [c4fbf7fd] | 138 | *device = (netif_device_t *) malloc(sizeof(netif_device_t));
|
|---|
| 139 | if (!*device)
|
|---|
| 140 | return ENOMEM;
|
|---|
| [fe8dfa6] | 141 |
|
|---|
| [c4fbf7fd] | 142 | (*device)->specific = (device_stats_t *) malloc(sizeof(device_stats_t));
|
|---|
| 143 | if (!(*device)->specific) {
|
|---|
| 144 | free(*device);
|
|---|
| 145 | return ENOMEM;
|
|---|
| [21580dd] | 146 | }
|
|---|
| [fe8dfa6] | 147 |
|
|---|
| [f772bc55] | 148 | null_device_stats((device_stats_t *) (*device)->specific);
|
|---|
| [c4fbf7fd] | 149 | (*device)->device_id = device_id;
|
|---|
| 150 | (*device)->nil_phone = -1;
|
|---|
| 151 | (*device)->state = NETIF_STOPPED;
|
|---|
| [fe8dfa6] | 152 | int index = netif_device_map_add(&netif_globals.device_map,
|
|---|
| [c4fbf7fd] | 153 | (*device)->device_id, *device);
|
|---|
| [fe8dfa6] | 154 |
|
|---|
| [c4fbf7fd] | 155 | if (index < 0) {
|
|---|
| 156 | free(*device);
|
|---|
| 157 | free((*device)->specific);
|
|---|
| 158 | *device = NULL;
|
|---|
| 159 | return index;
|
|---|
| 160 | }
|
|---|
| 161 |
|
|---|
| [21580dd] | 162 | return EOK;
|
|---|
| 163 | }
|
|---|
| 164 |
|
|---|
| [c4fbf7fd] | 165 | int netif_initialize(void)
|
|---|
| 166 | {
|
|---|
| [79ae36dd] | 167 | return service_register(SERVICE_LO);
|
|---|
| [21580dd] | 168 | }
|
|---|
| 169 |
|
|---|
| [774e6d1a] | 170 | int netif_probe_message(device_id_t device_id, int irq, void *io)
|
|---|
| [c4fbf7fd] | 171 | {
|
|---|
| [fb04cba8] | 172 | /* Create a new device */
|
|---|
| [fe8dfa6] | 173 | netif_device_t *device;
|
|---|
| 174 | int rc = lo_create(device_id, &device);
|
|---|
| [2e236901] | 175 | if (rc != EOK)
|
|---|
| 176 | return rc;
|
|---|
| [fe8dfa6] | 177 |
|
|---|
| [24ab58b3] | 178 | printf("%s: Device created (id: %d)\n", NAME, device->device_id);
|
|---|
| [21580dd] | 179 | return EOK;
|
|---|
| 180 | }
|
|---|
| 181 |
|
|---|
| [46d4d9f] | 182 | int netif_send_message(device_id_t device_id, packet_t *packet, services_t sender)
|
|---|
| [c4fbf7fd] | 183 | {
|
|---|
| 184 | netif_device_t *device;
|
|---|
| [fe8dfa6] | 185 | int rc = find_device(device_id, &device);
|
|---|
| [2e236901] | 186 | if (rc != EOK)
|
|---|
| 187 | return EOK;
|
|---|
| [fe8dfa6] | 188 |
|
|---|
| [c4fbf7fd] | 189 | if (device->state != NETIF_ACTIVE) {
|
|---|
| [aadf01e] | 190 | netif_pq_release(packet_get_id(packet));
|
|---|
| [21580dd] | 191 | return EFORWARD;
|
|---|
| 192 | }
|
|---|
| [fe8dfa6] | 193 |
|
|---|
| 194 | packet_t *next = packet;
|
|---|
| [c4fbf7fd] | 195 | do {
|
|---|
| [f772bc55] | 196 | ((device_stats_t *) device->specific)->send_packets++;
|
|---|
| 197 | ((device_stats_t *) device->specific)->receive_packets++;
|
|---|
| [fe8dfa6] | 198 | size_t length = packet_get_data_length(next);
|
|---|
| [f772bc55] | 199 | ((device_stats_t *) device->specific)->send_bytes += length;
|
|---|
| 200 | ((device_stats_t *) device->specific)->receive_bytes += length;
|
|---|
| [aadf01e] | 201 | next = pq_next(next);
|
|---|
| [fe8dfa6] | 202 | } while (next);
|
|---|
| 203 |
|
|---|
| 204 | int phone = device->nil_phone;
|
|---|
| [aadf01e] | 205 | fibril_rwlock_write_unlock(&netif_globals.lock);
|
|---|
| [fe8dfa6] | 206 |
|
|---|
| [aadf01e] | 207 | nil_received_msg(phone, device_id, packet, sender);
|
|---|
| [c4fbf7fd] | 208 |
|
|---|
| [fe8dfa6] | 209 | fibril_rwlock_write_lock(&netif_globals.lock);
|
|---|
| [21580dd] | 210 | return EOK;
|
|---|
| 211 | }
|
|---|
| 212 |
|
|---|
| [c4fbf7fd] | 213 | int netif_start_message(netif_device_t *device)
|
|---|
| 214 | {
|
|---|
| [fe8dfa6] | 215 | change_state_message(device, NETIF_ACTIVE);
|
|---|
| 216 | return device->state;
|
|---|
| [21580dd] | 217 | }
|
|---|
| 218 |
|
|---|
| [c4fbf7fd] | 219 | int netif_stop_message(netif_device_t *device)
|
|---|
| 220 | {
|
|---|
| [fe8dfa6] | 221 | change_state_message(device, NETIF_STOPPED);
|
|---|
| 222 | return device->state;
|
|---|
| [21580dd] | 223 | }
|
|---|
| 224 |
|
|---|
| [849ed54] | 225 | int main(int argc, char *argv[])
|
|---|
| 226 | {
|
|---|
| 227 | /* Start the module */
|
|---|
| [774e6d1a] | 228 | return netif_module_start();
|
|---|
| [849ed54] | 229 | }
|
|---|
| 230 |
|
|---|
| [21580dd] | 231 | /** @}
|
|---|
| 232 | */
|
|---|