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