| 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 |
|
|---|
| 42 | #include <ipc/ipc.h>
|
|---|
| 43 | #include <ipc/services.h>
|
|---|
| 44 | #include <ipc/nil.h>
|
|---|
| 45 |
|
|---|
| 46 | #include <net/modules.h>
|
|---|
| 47 | #include <adt/measured_strings.h>
|
|---|
| 48 | #include <packet_client.h>
|
|---|
| 49 | #include <net/device.h>
|
|---|
| 50 | #include <nil_interface.h>
|
|---|
| 51 | #include <netif_skel.h>
|
|---|
| 52 |
|
|---|
| 53 | /** Default hardware address. */
|
|---|
| 54 | #define DEFAULT_ADDR 0
|
|---|
| 55 |
|
|---|
| 56 | /** Default address length. */
|
|---|
| 57 | #define DEFAULT_ADDR_LEN 6
|
|---|
| 58 |
|
|---|
| 59 | /** Loopback module name. */
|
|---|
| 60 | #define NAME "lo"
|
|---|
| 61 |
|
|---|
| 62 | /** Network interface global data. */
|
|---|
| 63 | netif_globals_t netif_globals;
|
|---|
| 64 |
|
|---|
| 65 | int netif_specific_message(ipc_callid_t callid, ipc_call_t *call,
|
|---|
| 66 | ipc_call_t *answer, size_t *count)
|
|---|
| 67 | {
|
|---|
| 68 | return ENOTSUP;
|
|---|
| 69 | }
|
|---|
| 70 |
|
|---|
| 71 | int netif_get_addr_message(device_id_t device_id, measured_string_t *address)
|
|---|
| 72 | {
|
|---|
| 73 | if (!address)
|
|---|
| 74 | return EBADMEM;
|
|---|
| 75 |
|
|---|
| 76 | uint8_t *addr = (uint8_t *) malloc(DEFAULT_ADDR_LEN);
|
|---|
| 77 | memset(addr, DEFAULT_ADDR, DEFAULT_ADDR_LEN);
|
|---|
| 78 |
|
|---|
| 79 | address->value = addr;
|
|---|
| 80 | address->length = DEFAULT_ADDR_LEN;
|
|---|
| 81 |
|
|---|
| 82 | return EOK;
|
|---|
| 83 | }
|
|---|
| 84 |
|
|---|
| 85 | int netif_get_device_stats(device_id_t device_id, device_stats_t *stats)
|
|---|
| 86 | {
|
|---|
| 87 | netif_device_t *device;
|
|---|
| 88 | int rc;
|
|---|
| 89 |
|
|---|
| 90 | if (!stats)
|
|---|
| 91 | return EBADMEM;
|
|---|
| 92 |
|
|---|
| 93 | rc = find_device(device_id, &device);
|
|---|
| 94 | if (rc != EOK)
|
|---|
| 95 | return rc;
|
|---|
| 96 |
|
|---|
| 97 | memcpy(stats, (device_stats_t *) device->specific,
|
|---|
| 98 | sizeof(device_stats_t));
|
|---|
| 99 |
|
|---|
| 100 | return EOK;
|
|---|
| 101 | }
|
|---|
| 102 |
|
|---|
| 103 | /** Changes the loopback state.
|
|---|
| 104 | *
|
|---|
| 105 | * @param[in] device The device structure.
|
|---|
| 106 | * @param[in] state The new device state.
|
|---|
| 107 | * @return The new state if changed.
|
|---|
| 108 | * @return EOK otherwise.
|
|---|
| 109 | */
|
|---|
| 110 | static int change_state_message(netif_device_t *device, device_state_t state)
|
|---|
| 111 | {
|
|---|
| 112 | if (device->state != state) {
|
|---|
| 113 | device->state = state;
|
|---|
| 114 |
|
|---|
| 115 | printf("%s: State changed to %s\n", NAME,
|
|---|
| 116 | (state == NETIF_ACTIVE) ? "active" : "stopped");
|
|---|
| 117 |
|
|---|
| 118 | return state;
|
|---|
| 119 | }
|
|---|
| 120 |
|
|---|
| 121 | return EOK;
|
|---|
| 122 | }
|
|---|
| 123 |
|
|---|
| 124 | /** Creates and returns the loopback network interface structure.
|
|---|
| 125 | *
|
|---|
| 126 | * @param[in] device_id The new devce identifier.
|
|---|
| 127 | * @param[out] device The device structure.
|
|---|
| 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 | static int create(device_id_t device_id, netif_device_t **device)
|
|---|
| 133 | {
|
|---|
| 134 | int index;
|
|---|
| 135 |
|
|---|
| 136 | if (netif_device_map_count(&netif_globals.device_map) > 0)
|
|---|
| 137 | return EXDEV;
|
|---|
| 138 |
|
|---|
| 139 | *device = (netif_device_t *) malloc(sizeof(netif_device_t));
|
|---|
| 140 | if (!*device)
|
|---|
| 141 | return ENOMEM;
|
|---|
| 142 |
|
|---|
| 143 | (*device)->specific = (device_stats_t *) malloc(sizeof(device_stats_t));
|
|---|
| 144 | if (!(*device)->specific) {
|
|---|
| 145 | free(*device);
|
|---|
| 146 | return ENOMEM;
|
|---|
| 147 | }
|
|---|
| 148 |
|
|---|
| 149 | null_device_stats((device_stats_t *) (*device)->specific);
|
|---|
| 150 | (*device)->device_id = device_id;
|
|---|
| 151 | (*device)->nil_phone = -1;
|
|---|
| 152 | (*device)->state = NETIF_STOPPED;
|
|---|
| 153 | index = netif_device_map_add(&netif_globals.device_map,
|
|---|
| 154 | (*device)->device_id, *device);
|
|---|
| 155 |
|
|---|
| 156 | if (index < 0) {
|
|---|
| 157 | free(*device);
|
|---|
| 158 | free((*device)->specific);
|
|---|
| 159 | *device = NULL;
|
|---|
| 160 | return index;
|
|---|
| 161 | }
|
|---|
| 162 |
|
|---|
| 163 | return EOK;
|
|---|
| 164 | }
|
|---|
| 165 |
|
|---|
| 166 | int netif_initialize(void)
|
|---|
| 167 | {
|
|---|
| 168 | sysarg_t phonehash;
|
|---|
| 169 |
|
|---|
| 170 | return ipc_connect_to_me(PHONE_NS, SERVICE_LO, 0, 0, &phonehash);
|
|---|
| 171 | }
|
|---|
| 172 |
|
|---|
| 173 | int netif_probe_message(device_id_t device_id, int irq, void *io)
|
|---|
| 174 | {
|
|---|
| 175 | netif_device_t *device;
|
|---|
| 176 | int rc;
|
|---|
| 177 |
|
|---|
| 178 | /* Create a new device */
|
|---|
| 179 | rc = create(device_id, &device);
|
|---|
| 180 | if (rc != EOK)
|
|---|
| 181 | return rc;
|
|---|
| 182 |
|
|---|
| 183 | /* Print the settings */
|
|---|
| 184 | printf("%s: Device created (id: %d)\n", NAME, device->device_id);
|
|---|
| 185 |
|
|---|
| 186 | return EOK;
|
|---|
| 187 | }
|
|---|
| 188 |
|
|---|
| 189 | int netif_send_message(device_id_t device_id, packet_t *packet, services_t sender)
|
|---|
| 190 | {
|
|---|
| 191 | netif_device_t *device;
|
|---|
| 192 | size_t length;
|
|---|
| 193 | packet_t *next;
|
|---|
| 194 | int phone;
|
|---|
| 195 | int rc;
|
|---|
| 196 |
|
|---|
| 197 | rc = find_device(device_id, &device);
|
|---|
| 198 | if (rc != EOK)
|
|---|
| 199 | return EOK;
|
|---|
| 200 |
|
|---|
| 201 | if (device->state != NETIF_ACTIVE) {
|
|---|
| 202 | netif_pq_release(packet_get_id(packet));
|
|---|
| 203 | return EFORWARD;
|
|---|
| 204 | }
|
|---|
| 205 |
|
|---|
| 206 | next = packet;
|
|---|
| 207 | do {
|
|---|
| 208 | ((device_stats_t *) device->specific)->send_packets++;
|
|---|
| 209 | ((device_stats_t *) device->specific)->receive_packets++;
|
|---|
| 210 | length = packet_get_data_length(next);
|
|---|
| 211 | ((device_stats_t *) device->specific)->send_bytes += length;
|
|---|
| 212 | ((device_stats_t *) device->specific)->receive_bytes += length;
|
|---|
| 213 | next = pq_next(next);
|
|---|
| 214 | } while(next);
|
|---|
| 215 |
|
|---|
| 216 | phone = device->nil_phone;
|
|---|
| 217 | fibril_rwlock_write_unlock(&netif_globals.lock);
|
|---|
| 218 | nil_received_msg(phone, device_id, packet, sender);
|
|---|
| 219 | fibril_rwlock_write_lock(&netif_globals.lock);
|
|---|
| 220 |
|
|---|
| 221 | return EOK;
|
|---|
| 222 | }
|
|---|
| 223 |
|
|---|
| 224 | int netif_start_message(netif_device_t *device)
|
|---|
| 225 | {
|
|---|
| 226 | return change_state_message(device, NETIF_ACTIVE);
|
|---|
| 227 | }
|
|---|
| 228 |
|
|---|
| 229 | int netif_stop_message(netif_device_t *device)
|
|---|
| 230 | {
|
|---|
| 231 | return change_state_message(device, NETIF_STOPPED);
|
|---|
| 232 | }
|
|---|
| 233 |
|
|---|
| 234 | int main(int argc, char *argv[])
|
|---|
| 235 | {
|
|---|
| 236 | /* Start the module */
|
|---|
| 237 | return netif_module_start();
|
|---|
| 238 | }
|
|---|
| 239 |
|
|---|
| 240 | /** @}
|
|---|
| 241 | */
|
|---|