[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 | {
|
---|
[96b02eb9] | 169 | sysarg_t phonehash;
|
---|
[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 | {
|
---|
[fb04cba8] | 175 | /* Create a new device */
|
---|
[fe8dfa6] | 176 | netif_device_t *device;
|
---|
| 177 | int rc = lo_create(device_id, &device);
|
---|
[2e236901] | 178 | if (rc != EOK)
|
---|
| 179 | return rc;
|
---|
[fe8dfa6] | 180 |
|
---|
[24ab58b3] | 181 | printf("%s: Device created (id: %d)\n", NAME, device->device_id);
|
---|
[21580dd] | 182 | return EOK;
|
---|
| 183 | }
|
---|
| 184 |
|
---|
[46d4d9f] | 185 | int netif_send_message(device_id_t device_id, packet_t *packet, services_t sender)
|
---|
[c4fbf7fd] | 186 | {
|
---|
| 187 | netif_device_t *device;
|
---|
[fe8dfa6] | 188 | int rc = find_device(device_id, &device);
|
---|
[2e236901] | 189 | if (rc != EOK)
|
---|
| 190 | return EOK;
|
---|
[fe8dfa6] | 191 |
|
---|
[c4fbf7fd] | 192 | if (device->state != NETIF_ACTIVE) {
|
---|
[aadf01e] | 193 | netif_pq_release(packet_get_id(packet));
|
---|
[21580dd] | 194 | return EFORWARD;
|
---|
| 195 | }
|
---|
[fe8dfa6] | 196 |
|
---|
| 197 | packet_t *next = packet;
|
---|
[c4fbf7fd] | 198 | do {
|
---|
[f772bc55] | 199 | ((device_stats_t *) device->specific)->send_packets++;
|
---|
| 200 | ((device_stats_t *) device->specific)->receive_packets++;
|
---|
[fe8dfa6] | 201 | size_t length = packet_get_data_length(next);
|
---|
[f772bc55] | 202 | ((device_stats_t *) device->specific)->send_bytes += length;
|
---|
| 203 | ((device_stats_t *) device->specific)->receive_bytes += length;
|
---|
[aadf01e] | 204 | next = pq_next(next);
|
---|
[fe8dfa6] | 205 | } while (next);
|
---|
| 206 |
|
---|
| 207 | int phone = device->nil_phone;
|
---|
[aadf01e] | 208 | fibril_rwlock_write_unlock(&netif_globals.lock);
|
---|
[fe8dfa6] | 209 |
|
---|
[aadf01e] | 210 | nil_received_msg(phone, device_id, packet, sender);
|
---|
[c4fbf7fd] | 211 |
|
---|
[fe8dfa6] | 212 | fibril_rwlock_write_lock(&netif_globals.lock);
|
---|
[21580dd] | 213 | return EOK;
|
---|
| 214 | }
|
---|
| 215 |
|
---|
[c4fbf7fd] | 216 | int netif_start_message(netif_device_t *device)
|
---|
| 217 | {
|
---|
[fe8dfa6] | 218 | change_state_message(device, NETIF_ACTIVE);
|
---|
| 219 | return device->state;
|
---|
[21580dd] | 220 | }
|
---|
| 221 |
|
---|
[c4fbf7fd] | 222 | int netif_stop_message(netif_device_t *device)
|
---|
| 223 | {
|
---|
[fe8dfa6] | 224 | change_state_message(device, NETIF_STOPPED);
|
---|
| 225 | return device->state;
|
---|
[21580dd] | 226 | }
|
---|
| 227 |
|
---|
[849ed54] | 228 | int main(int argc, char *argv[])
|
---|
| 229 | {
|
---|
| 230 | /* Start the module */
|
---|
[774e6d1a] | 231 | return netif_module_start();
|
---|
[849ed54] | 232 | }
|
---|
| 233 |
|
---|
[21580dd] | 234 | /** @}
|
---|
| 235 | */
|
---|