[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>
|
---|
[14f1db0] | 51 | #include <netif_interface.h>
|
---|
| 52 | #include <netif_local.h>
|
---|
[21580dd] | 53 |
|
---|
[c4fbf7fd] | 54 | /** Default hardware address. */
|
---|
[61bfc370] | 55 | #define DEFAULT_ADDR 0
|
---|
[21580dd] | 56 |
|
---|
[c4fbf7fd] | 57 | /** Default address length. */
|
---|
[61bfc370] | 58 | #define DEFAULT_ADDR_LEN 6
|
---|
[21580dd] | 59 |
|
---|
[c4fbf7fd] | 60 | /** Loopback module name. */
|
---|
[24ab58b3] | 61 | #define NAME "lo"
|
---|
[21580dd] | 62 |
|
---|
[c4fbf7fd] | 63 | /** Network interface global data. */
|
---|
[61bfc370] | 64 | netif_globals_t netif_globals;
|
---|
[21580dd] | 65 |
|
---|
[fb04cba8] | 66 | int netif_specific_message(ipc_callid_t callid, ipc_call_t *call,
|
---|
[c4fbf7fd] | 67 | ipc_call_t *answer, int *answer_count)
|
---|
| 68 | {
|
---|
[21580dd] | 69 | return ENOTSUP;
|
---|
| 70 | }
|
---|
| 71 |
|
---|
[4eca056] | 72 | int netif_get_addr_message(device_id_t device_id, measured_string_t *address)
|
---|
[c4fbf7fd] | 73 | {
|
---|
| 74 | if (!address)
|
---|
[aadf01e] | 75 | return EBADMEM;
|
---|
[61bfc370] | 76 |
|
---|
| 77 | uint8_t *addr = (uint8_t *) malloc(DEFAULT_ADDR_LEN);
|
---|
| 78 | memset(addr, DEFAULT_ADDR, DEFAULT_ADDR_LEN);
|
---|
| 79 |
|
---|
| 80 | address->value = addr;
|
---|
[21580dd] | 81 | address->length = DEFAULT_ADDR_LEN;
|
---|
[61bfc370] | 82 |
|
---|
[21580dd] | 83 | return EOK;
|
---|
| 84 | }
|
---|
| 85 |
|
---|
[f772bc55] | 86 | int netif_get_device_stats(device_id_t device_id, device_stats_t *stats)
|
---|
[c4fbf7fd] | 87 | {
|
---|
| 88 | netif_device_t *device;
|
---|
[2e236901] | 89 | int rc;
|
---|
[21580dd] | 90 |
|
---|
[c4fbf7fd] | 91 | if (!stats)
|
---|
[aadf01e] | 92 | return EBADMEM;
|
---|
[fb04cba8] | 93 |
|
---|
[2e236901] | 94 | rc = find_device(device_id, &device);
|
---|
| 95 | if (rc != EOK)
|
---|
| 96 | return rc;
|
---|
[fb04cba8] | 97 |
|
---|
[f772bc55] | 98 | memcpy(stats, (device_stats_t *) device->specific,
|
---|
[c4fbf7fd] | 99 | sizeof(device_stats_t));
|
---|
[fb04cba8] | 100 |
|
---|
[21580dd] | 101 | return EOK;
|
---|
| 102 | }
|
---|
| 103 |
|
---|
[c4fbf7fd] | 104 | /** Changes the loopback state.
|
---|
| 105 | *
|
---|
| 106 | * @param[in] device The device structure.
|
---|
| 107 | * @param[in] state The new device state.
|
---|
[1bfd3d3] | 108 | * @return The new state if changed.
|
---|
| 109 | * @return EOK otherwise.
|
---|
[c4fbf7fd] | 110 | */
|
---|
| 111 | static int change_state_message(netif_device_t *device, device_state_t state)
|
---|
[24ab58b3] | 112 | {
|
---|
| 113 | if (device->state != state) {
|
---|
[21580dd] | 114 | device->state = state;
|
---|
[24ab58b3] | 115 |
|
---|
| 116 | printf("%s: State changed to %s\n", NAME,
|
---|
| 117 | (state == NETIF_ACTIVE) ? "active" : "stopped");
|
---|
| 118 |
|
---|
[21580dd] | 119 | return state;
|
---|
| 120 | }
|
---|
[24ab58b3] | 121 |
|
---|
[21580dd] | 122 | return EOK;
|
---|
| 123 | }
|
---|
| 124 |
|
---|
[c4fbf7fd] | 125 | /** Creates and returns the loopback network interface structure.
|
---|
| 126 | *
|
---|
| 127 | * @param[in] device_id The new devce identifier.
|
---|
| 128 | * @param[out] device The device structure.
|
---|
[1bfd3d3] | 129 | * @return EOK on success.
|
---|
| 130 | * @return EXDEV if one loopback network interface already exists.
|
---|
| 131 | * @return ENOMEM if there is not enough memory left.
|
---|
[c4fbf7fd] | 132 | */
|
---|
| 133 | static int create(device_id_t device_id, netif_device_t **device)
|
---|
| 134 | {
|
---|
[aadf01e] | 135 | int index;
|
---|
[21580dd] | 136 |
|
---|
[c4fbf7fd] | 137 | if (netif_device_map_count(&netif_globals.device_map) > 0)
|
---|
[21580dd] | 138 | return EXDEV;
|
---|
[c4fbf7fd] | 139 |
|
---|
| 140 | *device = (netif_device_t *) malloc(sizeof(netif_device_t));
|
---|
| 141 | if (!*device)
|
---|
| 142 | return ENOMEM;
|
---|
[fb04cba8] | 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 | }
|
---|
[fb04cba8] | 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;
|
---|
| 154 | index = netif_device_map_add(&netif_globals.device_map,
|
---|
| 155 | (*device)->device_id, *device);
|
---|
[fb04cba8] | 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;
|
---|
[21580dd] | 170 |
|
---|
[f87c900] | 171 | return ipc_connect_to_me(PHONE_NS, SERVICE_LO, 0, 0, &phonehash);
|
---|
[21580dd] | 172 | }
|
---|
| 173 |
|
---|
[c4fbf7fd] | 174 | int netif_probe_message(device_id_t device_id, int irq, uintptr_t io)
|
---|
| 175 | {
|
---|
[25271006] | 176 | netif_device_t *device;
|
---|
[2e236901] | 177 | int rc;
|
---|
[21580dd] | 178 |
|
---|
[fb04cba8] | 179 | /* Create a new device */
|
---|
[2e236901] | 180 | rc = create(device_id, &device);
|
---|
| 181 | if (rc != EOK)
|
---|
| 182 | return rc;
|
---|
[fb04cba8] | 183 |
|
---|
| 184 | /* Print the settings */
|
---|
[24ab58b3] | 185 | printf("%s: Device created (id: %d)\n", NAME, device->device_id);
|
---|
[fb04cba8] | 186 |
|
---|
[21580dd] | 187 | return EOK;
|
---|
| 188 | }
|
---|
| 189 |
|
---|
[46d4d9f] | 190 | int netif_send_message(device_id_t device_id, packet_t *packet, services_t sender)
|
---|
[c4fbf7fd] | 191 | {
|
---|
| 192 | netif_device_t *device;
|
---|
[aadf01e] | 193 | size_t length;
|
---|
[46d4d9f] | 194 | packet_t *next;
|
---|
[aadf01e] | 195 | int phone;
|
---|
[2e236901] | 196 | int rc;
|
---|
[21580dd] | 197 |
|
---|
[2e236901] | 198 | rc = find_device(device_id, &device);
|
---|
| 199 | if (rc != EOK)
|
---|
| 200 | return EOK;
|
---|
[fb04cba8] | 201 |
|
---|
[c4fbf7fd] | 202 | if (device->state != NETIF_ACTIVE) {
|
---|
[aadf01e] | 203 | netif_pq_release(packet_get_id(packet));
|
---|
[21580dd] | 204 | return EFORWARD;
|
---|
| 205 | }
|
---|
[fb04cba8] | 206 |
|
---|
[21580dd] | 207 | next = packet;
|
---|
[c4fbf7fd] | 208 | do {
|
---|
[f772bc55] | 209 | ((device_stats_t *) device->specific)->send_packets++;
|
---|
| 210 | ((device_stats_t *) device->specific)->receive_packets++;
|
---|
[aadf01e] | 211 | length = packet_get_data_length(next);
|
---|
[f772bc55] | 212 | ((device_stats_t *) device->specific)->send_bytes += length;
|
---|
| 213 | ((device_stats_t *) device->specific)->receive_bytes += length;
|
---|
[aadf01e] | 214 | next = pq_next(next);
|
---|
[c4fbf7fd] | 215 | } while(next);
|
---|
[fb04cba8] | 216 |
|
---|
[21580dd] | 217 | phone = device->nil_phone;
|
---|
[aadf01e] | 218 | fibril_rwlock_write_unlock(&netif_globals.lock);
|
---|
| 219 | nil_received_msg(phone, device_id, packet, sender);
|
---|
| 220 | fibril_rwlock_write_lock(&netif_globals.lock);
|
---|
[c4fbf7fd] | 221 |
|
---|
[21580dd] | 222 | return EOK;
|
---|
| 223 | }
|
---|
| 224 |
|
---|
[c4fbf7fd] | 225 | int netif_start_message(netif_device_t *device)
|
---|
| 226 | {
|
---|
[aadf01e] | 227 | return change_state_message(device, NETIF_ACTIVE);
|
---|
[21580dd] | 228 | }
|
---|
| 229 |
|
---|
[c4fbf7fd] | 230 | int netif_stop_message(netif_device_t *device)
|
---|
| 231 | {
|
---|
[aadf01e] | 232 | return change_state_message(device, NETIF_STOPPED);
|
---|
[21580dd] | 233 | }
|
---|
| 234 |
|
---|
[849ed54] | 235 | /** Default thread for new connections.
|
---|
| 236 | *
|
---|
[c4fbf7fd] | 237 | * @param[in] iid The initial message identifier.
|
---|
| 238 | * @param[in] icall The initial message call structure.
|
---|
[849ed54] | 239 | */
|
---|
[14f1db0] | 240 | static void netif_client_connection(ipc_callid_t iid, ipc_call_t *icall)
|
---|
[849ed54] | 241 | {
|
---|
| 242 | /*
|
---|
| 243 | * Accept the connection
|
---|
| 244 | * - Answer the first IPC_M_CONNECT_ME_TO call.
|
---|
| 245 | */
|
---|
| 246 | ipc_answer_0(iid, EOK);
|
---|
| 247 |
|
---|
[c4fbf7fd] | 248 | while (true) {
|
---|
[849ed54] | 249 | ipc_call_t answer;
|
---|
| 250 | int answer_count;
|
---|
| 251 |
|
---|
| 252 | /* Clear the answer structure */
|
---|
| 253 | refresh_answer(&answer, &answer_count);
|
---|
| 254 |
|
---|
| 255 | /* Fetch the next message */
|
---|
| 256 | ipc_call_t call;
|
---|
| 257 | ipc_callid_t callid = async_get_call(&call);
|
---|
| 258 |
|
---|
| 259 | /* Process the message */
|
---|
[24ab58b3] | 260 | int res = netif_module_message(NAME, callid, &call, &answer,
|
---|
| 261 | &answer_count);
|
---|
[849ed54] | 262 |
|
---|
[c4fbf7fd] | 263 | /*
|
---|
[25271006] | 264 | * End if told to either by the message or the processing
|
---|
[c4fbf7fd] | 265 | * result.
|
---|
| 266 | */
|
---|
[228e490] | 267 | if ((IPC_GET_IMETHOD(call) == IPC_M_PHONE_HUNGUP) ||
|
---|
[c4fbf7fd] | 268 | (res == EHANGUP))
|
---|
[849ed54] | 269 | return;
|
---|
| 270 |
|
---|
| 271 | /* Answer the message */
|
---|
| 272 | answer_call(callid, res, &answer, answer_count);
|
---|
| 273 | }
|
---|
| 274 | }
|
---|
| 275 |
|
---|
| 276 | int main(int argc, char *argv[])
|
---|
| 277 | {
|
---|
[2e236901] | 278 | int rc;
|
---|
[849ed54] | 279 |
|
---|
| 280 | /* Start the module */
|
---|
[2e236901] | 281 | rc = netif_module_start(netif_client_connection);
|
---|
| 282 | return rc;
|
---|
[849ed54] | 283 | }
|
---|
| 284 |
|
---|
[21580dd] | 285 | /** @}
|
---|
| 286 | */
|
---|