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