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